Posted 22 October 2013 - 08:30 PM
Hi :)/>
I started learning Lua to write script for turtles. I went through a bunch of the Youtube tutorials and have written some basic movement and mining scripts. I also have some other mining, quarry, xzy goto, and tree farm scripts that I've modified a little.
I started coding some house building, but I need a sorting function for the turtle inventory. Right now, I have to select() specific turtle slots to pull blocks from, and I have to count the blocks used so I know when to select() the next inventory slot. This makes it more cumbersome to code, but also to use the script and load up the turtle inventory at the start.
What I want to do is use getItemCount() or getItemSpace() to determine how much is left in a slot's stack, and then transfer items to the main slots. If I only use 3 or 4 block types (for example), the rest of the inventory space can have more building blocks, but it doesn't matter which slot the extra blocks are in. The function can compare the "main slot" blocks to the "storage slot" blocks, and if they are the same, top up the stack of the "main slots". I searched this forum and on the net for a sorting script, and only found this one. It's coded by Ashvathaman on youtube, and his pastebin is CS9S6CGq. I added comments to the code to list what "I" think the parts of code are doing. His function that I'd like to use is this:
I've used his code to build a couple houses and it's works awesome, especially the inventory management, which is why I'd like to use it :)/>/>/>/>/> I checked through it and it doesn't seem to use any other functions that I don't have in my script, but it doesn't work. The only other part of his code that calls this function is this:
The fmod stuff is pretty much over my head, and I think he uses it to define the blocks to use, the slots to select and the type of movement of the turtle. If that's the case, it's an amazingly compact script for the kind of house it builds. I think, possibly, that by changing the fmod values, if I could decifer what they all mean, you could build just about any house style, while leaving the main code "engine" untouched. Pretty amazing, to me.
http://www.youtube.com/watch?v=1YGqgPFH2Lo
Any help would be appreciated in getting the sorting() function to work.
Thanks :)/>
EDIT: Also, I think I understand that "local" parameters and functions only operate inside the "container" that they are defined in. So for example, the "space" and "items" parameters have no value, and aren't modified or recorded, outside the "sorting()" function, correct?
I started learning Lua to write script for turtles. I went through a bunch of the Youtube tutorials and have written some basic movement and mining scripts. I also have some other mining, quarry, xzy goto, and tree farm scripts that I've modified a little.
I started coding some house building, but I need a sorting function for the turtle inventory. Right now, I have to select() specific turtle slots to pull blocks from, and I have to count the blocks used so I know when to select() the next inventory slot. This makes it more cumbersome to code, but also to use the script and load up the turtle inventory at the start.
What I want to do is use getItemCount() or getItemSpace() to determine how much is left in a slot's stack, and then transfer items to the main slots. If I only use 3 or 4 block types (for example), the rest of the inventory space can have more building blocks, but it doesn't matter which slot the extra blocks are in. The function can compare the "main slot" blocks to the "storage slot" blocks, and if they are the same, top up the stack of the "main slots". I searched this forum and on the net for a sorting script, and only found this one. It's coded by Ashvathaman on youtube, and his pastebin is CS9S6CGq. I added comments to the code to list what "I" think the parts of code are doing. His function that I'd like to use is this:
local function sorting()
local space = 0
local items = 0
for n=1,8 do ----- "n" is referencing whichever of the first 8 slots is selected
turtle.select(n) ----- whichever slot is currently selected
if turtle.getItemCount(n) >40 then
space = turtle.getItemSpace(n) ----- asking for how much space is left in the stack
if space ~= 0 then
for m=7,15 do ----- not sure what defines "m" Is it the other slots that hold the extra blocks from 7 to 15?
if turtle.compareTo(m) then ----- this compares the 7 to 15 slot to one of the first 6 slots with building blocks, and if it is the same it gets selected?
turtle.select(m) ----- this selects on of the "storage slots"?
items = turtle.getItemCount(m) ----- gets the item count of the storage stack it just compared?
if items > space then ----- if the # of items are more than the space in the original stack
turtle.transferTo(n, space) ----- either tops up the "main slot" stack, or
else
turtle.transferTo(n, items) ----- transfers the entire storage stack to the "main slot"?
end
end
end
end
end
end
end
I've used his code to build a couple houses and it's works awesome, especially the inventory management, which is why I'd like to use it :)/>/>/>/>/> I checked through it and it doesn't seem to use any other functions that I don't have in my script, but it doesn't work. The only other part of his code that calls this function is this:
for k=1,14 do
for i=1,12 do
for j=1,12 do
placed = placed + 1
if house[placed] == nil then
return
end
if house[placed] ~= 0 then
turtle.select(house[placed])
turtle.placeDown()
used = used + 1
if used == 16 then
used = 0
sorting()
end
end
if j ~= 12 then
tryForward()
end
end
if i ~= 12 then
if math.fmod(i, 2) == 0 then
turtle.turnRight()
tryForward()
turtle.turnRight()
else
turtle.turnLeft()
tryForward()
turtle.turnLeft()
end
end
end
turtle.turnLeft()
for m=1,11 do
tryForward()
end
turtle.turnLeft()
tryUp()
The fmod stuff is pretty much over my head, and I think he uses it to define the blocks to use, the slots to select and the type of movement of the turtle. If that's the case, it's an amazingly compact script for the kind of house it builds. I think, possibly, that by changing the fmod values, if I could decifer what they all mean, you could build just about any house style, while leaving the main code "engine" untouched. Pretty amazing, to me.
http://www.youtube.com/watch?v=1YGqgPFH2Lo
Any help would be appreciated in getting the sorting() function to work.
Thanks :)/>
EDIT: Also, I think I understand that "local" parameters and functions only operate inside the "container" that they are defined in. So for example, the "space" and "items" parameters have no value, and aren't modified or recorded, outside the "sorting()" function, correct?
Edited on 22 October 2013 - 08:12 PM