Advertisement
jwow22

Untitled

Mar 25th, 2024 (edited)
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.37 KB | None | 0 0
  1. -------------------------------------------------------------
  2. -- COMMANDS
  3. -------------------------------------------------------------
  4.  
  5. local command = {}
  6. command.new = function()
  7.     local self = {}
  8.     self.name = "default"
  9.  
  10.     function self:execute()
  11.         table.insert(Tu.commandStack, self)
  12.         print(string.format("execute: %s", self.name))
  13.     end
  14.  
  15.     function self:undo()
  16.         print(string.format("undo: %s", self.name))
  17.     end
  18.  
  19.     return self
  20. end
  21.  
  22. -- command forward
  23. local com_forward = {}
  24. com_forward.new = function ()
  25.     local self = command.new()
  26.     self.name = "forward"
  27.  
  28.     local super_execute = self.execute
  29.     function self:execute()
  30.         local valid = turtle.forward()
  31.         if valid == false then
  32.             return false
  33.         end
  34.         super_execute(self)
  35.         return true
  36.     end
  37.  
  38.     local super_undo = self.undo
  39.     function self:undo()
  40.         local valid = turtle.back()
  41.         if valid == false then
  42.             return false
  43.         end
  44.         super_undo(self)
  45.         return true
  46.     end
  47.  
  48.     return self
  49. end
  50.  
  51. -- command back
  52. local com_back = {}
  53. com_back.new = function ()
  54.     local self = command.new()
  55.     self.name = "back"
  56.  
  57.     local super_execute = self.execute
  58.     function self:execute()
  59.         local valid = turtle.back()
  60.         if valid == false then
  61.             return false
  62.         end
  63.         super_execute(self)
  64.         return true
  65.     end
  66.  
  67.     local super_undo = self.undo
  68.     function self:undo()
  69.         local valid = turtle.forward()
  70.         if valid == false then
  71.             return false
  72.         end
  73.         super_undo(self)
  74.         return true
  75.     end
  76.  
  77.     return self
  78. end
  79.  
  80. -- command up
  81. local com_up = {}
  82. com_up.new = function ()
  83.     local self = command.new()
  84.     self.name = "up"
  85.  
  86.     local super_execute = self.execute
  87.     function self:execute()
  88.         local valid = turtle.up()
  89.         if valid == false then
  90.             return false
  91.         end
  92.         super_execute(self)
  93.         return true
  94.     end
  95.  
  96.     local super_undo = self.undo
  97.     function self:undo()
  98.         local valid = turtle.down()
  99.         if valid == false then
  100.             return false
  101.         end
  102.         super_undo(self)
  103.         return true
  104.     end
  105.  
  106.     return self
  107. end
  108.  
  109. -- command down
  110. local com_down = {}
  111. com_down.new = function ()
  112.     local self = command.new()
  113.     self.name = "down"
  114.  
  115.     local super_execute = self.execute
  116.     function self:execute()
  117.         local valid = turtle.down()
  118.         if valid == false then
  119.             return false
  120.         end
  121.         super_execute(self)
  122.         return true
  123.     end
  124.  
  125.     local super_undo = self.undo
  126.     function self:undo()
  127.         local valid = turtle.up()
  128.         if valid == false then
  129.             return false
  130.         end
  131.         super_undo(self)
  132.         return true
  133.     end
  134.  
  135.     return self
  136. end
  137.  
  138. -- command turnLeft
  139. local com_turnLeft = {}
  140. com_turnLeft.new = function ()
  141.     local self = command.new()
  142.     self.name = "down"
  143.  
  144.     local super_execute = self.execute
  145.     function self:execute()
  146.         local valid = turtle.turnLeft()
  147.         if valid == false then
  148.             return false
  149.         end
  150.         super_execute(self)
  151.         return true
  152.     end
  153.  
  154.     local super_undo = self.undo
  155.     function self:undo()
  156.         local valid = turtle.turnRight()
  157.         if valid == false then
  158.             return false
  159.         end
  160.         super_undo(self)
  161.         return true
  162.     end
  163.  
  164.     return self
  165. end
  166.  
  167. -- command turnRight
  168. local com_turnRight = {}
  169. com_turnRight.new = function ()
  170.     local self = command.new()
  171.     self.name = "down"
  172.  
  173.     local super_execute = self.execute
  174.     function self:execute()
  175.         local valid = turtle.turnRight()
  176.         if valid == false then
  177.             return false
  178.         end
  179.         super_execute(self)
  180.         return true
  181.     end
  182.  
  183.     local super_undo = self.undo
  184.     function self:undo()
  185.         local valid = turtle.turnLeft()
  186.         if valid == false then
  187.             return false
  188.         end
  189.         super_undo(self)
  190.         return true
  191.     end
  192.  
  193.     return self
  194. end
  195.  
  196.  
  197. -------------------------------------------------------------
  198. -- INSTANCE
  199. -------------------------------------------------------------
  200.  
  201. -- instance
  202. Tu = {}
  203. Tu.active = false
  204. Tu.updateTime = 1.0
  205. Tu.elapsedTime = 0.0
  206.  
  207. -- commands
  208. Tu.commandStack = {}
  209. Tu.forward = com_forward.new()
  210. Tu.back = com_back.new()
  211. Tu.up = com_up.new()
  212. Tu.down = com_down.new()
  213. Tu.turnLeft = com_turnLeft.new()
  214. Tu.turnRight = com_turnRight.new()
  215.  
  216. function Tu.undoStack()
  217.     for i = 1, #Tu.commandStack, 1 do
  218.         Tu.commandStack[i]:undo()
  219.     end
  220. end
  221.  
  222. function Tu.getLastCommand()
  223.     if #Tu.commandStack == 0 then
  224.         return nil
  225.     end
  226.     return Tu.commandStack[#Tu.commandStack]
  227. end
  228.  
  229. function Tu.start()
  230.     print("start")
  231.     Tu.turnRight:execute()
  232.     Tu.turnLeft:execute()
  233.     for i = 1, 20, 1 do
  234.         turtle.digDown()
  235.         Tu.down:execute()
  236.     end
  237.     Tu.undoStack()
  238. end
  239.  
  240. function Tu.update()
  241.     if turtle.getFuelLevel() > 0 then
  242.        
  243.     end
  244. end
  245.  
  246. Tu.start()
  247. while Tu.active == true do
  248.     Tu.elapsedTime = os.clock()
  249.  
  250.     if Tu.updateTime == 0.0 then
  251.         Tu.update()
  252.         return
  253.     end
  254.  
  255.     local currentTime = Tu.elapsedTime % Tu.updateTime
  256.     if currentTime == 0.0 then
  257.         Tu.update()
  258.     end
  259. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement