Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --since I keep having to rewrite this code but can't find a way to pass and execute functions at runtime in lua, I will just make a template for it.
- --this just repeats something in 3d, doing each row, layer, and column as efficiently as possible
- --you need global variables that tell where the turtle's x y and z position is, usually relative to it's origin or home position.
- --this assumes that it is starting in the bottom left corner. Can be adjusted.
- --inventory functions
- --getnumber(id) --gets the number of items in the inventory. For most cases use anyleft instead as it doesn't usually need to perform an entire inventory update
- --getslot(id) --gets the first slot that the item can be found in
- --asignidorder(1, 2, 3, ...) --assigns ids to each item in the order they occur in the inventory without knowing the actual slots they occur in. Spaces between items will stop the count
- --anyleft(id) --returns true if there are more than one of the item in the turtles inventory, else false (including if there are but it cannot identify them.)
- --updateinventory() --updates the inventory and tells where each item is located
- --sortinventory()
- --updateitem(id, number) --tells the inventory that number of items was added
- --itemadded(morethanone) --tells the inventory that an item was added, but not what it was. argument is true if more than one item was added
- --fuel(id, id2, ...) --asigns each item as a fuel useable by the turtle, in the order of priorty (reasigns priority if was used before)
- --notfuel(id) --removes an item from the fuel list
- --refuel(level) --if level = 0 it will consume all fuels, otherwise consumes fuel until the turtles fuel level is up the the level specified
- --selectitem(id) --selects the first instance of item if not already selected. Does not update the stack or inventory unless it can not find any
- --selectempty() --selects an empty slot
- function refuel()
- for i = 1, 16 do
- turtle.select(i)
- turtle.refuel()
- end
- slot = 1
- turtle.select(1)
- end
- function digforward(n)
- for i = 1, n do
- repeat
- if turtle.detect() then turtle.dig() end
- until turtle.forward()
- if direction == 1 then x = x + 1 end
- if direction == 2 then y = y + 1 end
- if direction == 3 then x = x - 1 end
- if direction == 4 then y = y - 1 end
- end
- --refueling and other functions should go here, as well as the up and down functions
- end
- function digdown(n)
- for i = 1, n do
- repeat
- if turtle.detectDown() then turtle.digDown() end
- until turtle.down()
- z = z - 1
- end
- end
- function digup(n)
- for i = 1, n do
- repeat
- if turtle.detectUp() then turtle.digUp() end
- until turtle.up()
- z = z + 1
- end
- end
- function turnright(n)
- for i = 1, n do
- turtle.turnRight()
- direction = direction + 1
- if direction > 4 then direction = 1 end
- end
- end
- function turnleft(n)
- for i = 1, n do
- turtle.turnLeft()
- direction = direction - 1
- if direction < 1 then direction = 4 end
- end
- end
- function turn(isright)
- if isright then turnright(1)
- else turnleft(1) end
- end
- function turncorner(isright)
- turn(isright)
- digforward(1)
- turn(isright)
- end
- function gotox(x)
- if gx > x then face(3)
- elseif gx < x then face(1)
- elseif gx == x then return end
- digforward(1)
- gotox(x)
- end
- function gotoy(y)
- if gy > y then face(4)
- elseif gy < y then face(2)
- elseif gy == y then return end
- digforward(1)
- gotoy(y)
- end
- function gotoz(z)
- if gz < z then digup(1) gotoz(z) end
- if gz > z then digdown(1) gotoz(z) end
- end
- function face(n)
- if direction == n then return end
- if direction < 3 then turnleft(1)
- else turnright(1) end
- face(n)
- end
- function iseven(n)
- if math.fmod(n, 2) == 0 then return true
- else return false end
- end
- function doblock()
- if turtle.detectDown() then turtle.digDown() end
- if turtle.getItemCount(slot) < 1 then
- slot = slot + 1
- if slot > 16 then print("Out of materials.\nRefill turtle and press Enter when ready.") io.read() slot = 1 end
- turtle.select(slot)
- end
- turtle.placeDown()
- end
- function dorow(fx)
- while true do
- doblock()
- if fx < 2 then break end
- digforward(1)
- fx = fx - 1
- end
- end
- function dolayer(fx, fy, isright)
- while true do
- dorow(fx)
- fy = fy - 1
- if fy < 2 then break end
- turncorner(isright)
- isright = not isright
- end
- end
- function fillfloor(fx, fy, fz)
- local isright = true
- while true do
- dolayer(fx, fy, isright)
- fz = fz - 1
- if fz < 2 then break end
- digup()
- turnright(2)
- if iseven(fy) then isright = not isright end
- end
- end
- function sethome()
- direction = 1
- x = 0
- y = 0
- z = 0
- end
- print("Enter forward dimension: ")
- forward = tonumber(io.read())
- print("Enter sidways dimension: ")
- sideways = tonumber(io.read())
- print("Fill turtle with fuel then press Enter.")
- io.read()
- refuel()
- print("Fill full of building materials and press Enter when ready to begin.")
- io.read()
- sethome()
- fillfloor(forward, sideways, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement