Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------------------------
- -- Copyright 2019 Fatboychummy
- -- You can edit and redistribute, but keep this
- -- copyright info in the code, untampered with.
- -------------------------------------------------
- if not turtle then
- error("Why are you trying to run this on a non-turtle computer?", 0)
- -- seriuzly tho y
- end
- local funcs = {}
- local nt = {}
- -------------------------------------------------
- -- function: doIf [private]
- -- info : runs a function, if it's first returned thing is true, run a second
- -- function with the extra inputs
- -- inputs : self, ifThis [function], thenThis[function], [extra]
- -- returns : whatever ifThis returns
- -------------------------------------------------
- local function doIf(self, ifThis, thenThis, ...)
- local ptal = {ifThis()} -- run the func and put output to table
- if ptal[1] then -- if the first item is true
- thenThis(self, ...) -- run the second func
- end
- return table.unpack(ptal) -- return what the first function returned
- end
- -------------------------------------------------
- -- function: moveBy
- -- info : Changes the turtle object's position by df and du
- -- inputs : self, df (distance moved forward), du (distance moved up)
- -- returns : nil
- -------------------------------------------------
- local function moveBy(self, df, du)
- if not df then df = 0 end -- default df to 0
- if not du then du = 0 end -- defualt du to 0
- if self.facing == 0 then -- facing towards positive Z
- self.z = self.z + df
- elseif self.facing == 1 then -- facing towards negative X
- self.x = self.x - df
- elseif self.facing == 2 then -- facing towards negative Z
- self.z = self.z - df
- elseif self.facing == 3 then -- facing towards positive X
- self.x = self.x + df
- end
- self.y = self.y + du -- add du, which is default 0
- end
- -------------------------------------------------
- -- function: turtle movement functions
- -- info : move the turtle AND set turtle data
- -- inputs : nil
- -- returns : whatever the equivalent turtle function returns
- -------------------------------------------------
- function nt:forward()
- return doIf(self, turtle.forward, moveBy, 1, 0)
- end
- function nt:back()
- return doIf(self, turtle.back, moveBy, -1, 0)
- end
- function nt:up()
- return doIf(self, turtle.up, moveBy, 0, 1)
- end
- function nt:down()
- return doIf(self, turtle.down, moveBy, 0, -1)
- end
- function nt:turnLeft()
- return doIf(self, turtle.turnLeft,
- function()
- self.facing = self.facing - 1
- self.facing = self.facing % 4
- end
- )
- end
- function nt:turnRight()
- return doIf(self, turtle.turnRight,
- function()
- self.facing = self.facing + 1
- self.facing = self.facing % 4
- end
- )
- end
- -------------------------------------------------
- -- function: setHome
- -- info : sets the turtle's "home" position
- -- inputs : nil
- -- returns : nil
- -------------------------------------------------
- function nt:setHome()
- self.home = {
- x = self.x,
- y = self.y,
- z = self.z
- }
- end
- -------------------------------------------------
- -- function: getHome
- -- info : gets the turtle's "home" position
- -- inputs : nil
- -- returns : home[x], home[y], home[z]
- -------------------------------------------------
- function nt:getHome()
- return self.home.x, self.home.y, self.home.z
- end
- -------------------------------------------------
- -- function: getPos
- -- info : gets the turtle's current relative position
- -- (from it's starting point)
- -- inputs : nil
- -- returns : pos[x], pos[y], pos[z]
- -------------------------------------------------
- function nt:getPos()
- return self.x, self.y, self.z
- end
- -------------------------------------------------
- -- function: getFacing
- -- info : gets the relative direction the turtle is facing
- -- (from it's starting point)
- -- inputs : nil
- -- returns : the turtle's direction (0-3)
- -------------------------------------------------
- function nt:getFacing()
- return self.facing
- end
- -------------------------------------------------
- -- function: load
- -- info : load turtle positional data from file
- -- inputs : fileFrom[string]
- -- returns : nil
- -------------------------------------------------
- function nt:load(fileFrom)
- fileFrom = type(fileFrom) == "string" and fileFrom
- or error("NO DAT IS NOT FILE NAME", 2)
- local dat = loadfile(fileFrom)()
- self.facing = dat.facing
- self.x = self.x
- self.y = self.y
- self.z = self.z
- self.home = {}
- self.home.x = dat.home.x
- self.home.y = dat.home.y
- self.home.z = dat.home.z
- end
- -------------------------------------------------
- -- function: save
- -- info : save turtle positional data to file
- -- inputs : fileTo[string]
- -- returns : nil
- -------------------------------------------------
- function nt:save(fileTo)
- fileTo = type(fileTo) == "string" and fileTo
- or error("NO DAT IS NOT FILE NAME", 2)
- local h = io.open(fileTo, 'w')
- h:write("return {\n")
- h:write(" facing = " .. tostring(self.facing) .. ",\n")
- h:write(" x = " .. tostring(self.x) .. ",\n")
- h:write(" y = " .. tostring(self.y) .. ",\n")
- h:write(" z = " .. tostring(self.z) .. ",\n")
- h:write(" home = {\n")
- h:write(" x = " .. tostring(self.home.x) .. ",\n")
- h:write(" y = " .. tostring(self.home.y) .. ",\n")
- h:write(" z = " .. tostring(self.home.z) .. '\n')
- h:write(" }\n")
- h:write("}")
- h:close()
- end
- -------------------------------------------------
- -- function: equip
- -- info : creates a turtle positional object
- -- inputs : nil
- -- returns : Tortois
- -------------------------------------------------
- function funcs.equip()
- local tmp = setmetatable(
- {
- facing = 0,
- x = 0,
- y = 0,
- z = 0,
- home = {
- x = 0,
- y = 0,
- z = 0
- }
- },
- {__index = nt}
- )
- return tmp
- end
- -------------------------------------------------
- -- function: uhhhhhhhhhhhhhhhhh
- -- info : uhhhhhhhhhhhhhhhhhh
- -- inputs : uhhhhhhhhhhhhhhhhhhhhhhh
- -- returns : uhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
- -------------------------------------------------
- function funcs.unequip()
- -- is this actually needed?
- end
- return funcs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement