Advertisement
TwoThe

OC Robot Base

Sep 7th, 2014
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.40 KB | None | 0 0
  1. local component = require "component"
  2. local sides = require "sides"
  3. local inventory = component.inventory_controller
  4. local robot = require "robot"
  5. local vector = require "vector"
  6. local generator = component.generator
  7.  
  8. local class = {}
  9.  
  10. class.ID = { coal = 263 }
  11. class.position = vector.new(0, 0, 0)
  12. class.direction = vector.new(1, 0, 0)
  13. local UP = vector.new(0, 1, 0)
  14.  
  15. -----------------------------------------
  16. -- Returns a directional vector based on the given facing
  17. -- facing (string): either "north", "east", "south" or "west"
  18. -- Return: vector with x, y, z set to point into the requested direction
  19. -----------------------------------------
  20. function class.facingToDirection(facing)
  21.   local d = string.upper(facing)
  22.   if "EAST" == d then
  23.     return vector.new(1, 0, 0)
  24.   elseif "SOUTH" == d then
  25.     return vector.new(0, 0, 1)
  26.   elseif "WEST" == d then
  27.     return vector.new(-1, 0, 0)
  28.   elseif "NORTH" == d then
  29.     return vector.new(0, 0, -1)
  30.   else
  31.     assert(false, "Unknown facing "..tostring(facing)..". Must be either EAST, SOUTH, WEST or NORTH")
  32.   end
  33. end
  34.  
  35. -----------------------------------------
  36. -- Returns the current facing of this robot as string
  37. -- Returns: string either "north", "east", "south" or "west"
  38. -----------------------------------------
  39. function class.getFacing()
  40.   if class.direction.x == 0 then
  41.     if class.direction.z == 1 then
  42.       return "SOUTH"
  43.     else
  44.       return "NORTH"
  45.     end  
  46.   elseif class.direction.x == 1 then
  47.     return "EAST"
  48.   else
  49.     return "WEST"
  50.   end
  51. end
  52.  
  53. -----------------------------------------
  54. -- Returns the current direction as vector.
  55. -- Returns: vector of length 1 pointing in the current look direction.
  56. -----------------------------------------
  57. function class.getDirection()
  58.   return class.direction:copy()
  59. end
  60.  
  61. -----------------------------------------
  62. -- Sets the current position and facing of this robot.
  63. -- There are no checks if the given values are the actual position and facing.
  64. -- facing (string): either "north", "east", "south" or "west"
  65. -- x, y, z (number - optional): the current location. If not present the current location is not changed.
  66. -----------------------------------------
  67. function class.setPosition(facing, x, y, z)
  68.   local newX = x or class.position.x
  69.   local newY = y or class.position.y
  70.   local newZ = z or class.position.z
  71.   assert(type(newX) == "number", "Invalid x value")
  72.   assert(type(newY) == "number", "Invalid y value")
  73.   assert(type(newZ) == "number", "Invalid z value")
  74.   assert(type(facing) == "string", "Invalid facing")
  75.   class.position = vector.new(newX, newY, newZ)
  76.   class.direction = class.facingToDirection(facing)
  77. end
  78.  
  79. -----------------------------------------
  80. -- Returns the current position.
  81. -- Returns: vector containing the current position relative to what this robot considers 0, 0, 0.
  82. -----------------------------------------
  83. function class.getPosition()
  84.   return class.position:copy()
  85. end
  86.  
  87. -----------------------------------------
  88. -- Returns the current position as x y z.
  89. -- Returns: x, y and z of the current position relative to what this robot considers 0, 0, 0.
  90. -----------------------------------------
  91. function class.getPositionXYZ()
  92.   return class.position.x, class.position.y, class.position.z
  93. end
  94.  
  95. -----------------------------------------
  96. -- Turns into the requested direction.
  97. -- facing (string): either "north", "east", "south" or "west"
  98. -----------------------------------------
  99. function class.turn(facing)
  100.   local newDirection = class.facingToDirection(facing)
  101.   if (class.direction.x == newDirection.x) and (class.direction.z == newDirection.z) then
  102.     return
  103.   end  
  104.   if (newDirection.x == class.direction.x) or (newDirection.z == class.direction.z) then
  105.     if (math.random(0, 1) == 0) then -- because we are robot!
  106.       class.turnRight(2) -- turn around
  107.     else
  108.       class.turnLeft(2) -- turn around
  109.     end
  110.   elseif ((class.direction.x == 1) and (newDirection.z == 1)) or
  111.          ((class.direction.z == 1) and (newDirection.x == -1)) or
  112.          ((class.direction.x == -1) and (newDirection.z == -1)) or
  113.          ((class.direction.z == -1) and (newDirection.x == 1)) then
  114.     class.turnRight(1)
  115.   else  
  116.     class.turnLeft(1)
  117.   end
  118. end
  119.  
  120. -----------------------------------------
  121. -- Turn left with direction tracking
  122. -----------------------------------------
  123. function class.turnLeft(steps)
  124.   for step = 1,(steps or 1) do
  125.     if (robot.turnLeft()) then
  126.       if (class.direction.x == 0) then
  127.         if (class.direction.z == 1) then
  128.           class.direction.x = 1
  129.         else
  130.           class.direction.x = -1
  131.         end
  132.         class.direction.z = 0
  133.       else
  134.         if class.direction.x == 1 then
  135.           class.direction.z = -1
  136.         else
  137.           class.direction.z = 1
  138.         end
  139.         class.direction.x = 0
  140.       end
  141.     else  
  142.       return false
  143.     end
  144.   end
  145.   return true
  146. end
  147.  
  148. -----------------------------------------
  149. -- Turn right with direction tracking
  150. -----------------------------------------
  151. function class.turnRight(steps)
  152.   for step = 1,(steps or 1) do
  153.     if (robot.turnRight()) then
  154.       if (class.direction.x == 0) then
  155.         if (class.direction.z == 1) then
  156.           class.direction.x = -1
  157.         else
  158.           class.direction.x = 1
  159.         end
  160.         class.direction.z = 0
  161.       else
  162.         if class.direction.x == 1 then
  163.           class.direction.z = 1
  164.         else
  165.           class.direction.z = -1
  166.         end
  167.         class.direction.x = 0
  168.       end  
  169.     else  
  170.       return false
  171.     end
  172.   end
  173.   return true
  174. end
  175.  
  176. -----------------------------------------
  177. -- Moves forward with position tracking
  178. -----------------------------------------
  179. function class.forward(steps)
  180.   local success, reason
  181.   for step=1,(steps or 1) do
  182.     if (not class.checkFuel()) then
  183.       return false
  184.     end
  185.     success, reason = robot.forward()
  186.     if success then
  187.       class.position:add(class.direction)
  188.     else
  189.       return false, reason
  190.     end
  191.   end
  192. end
  193.  
  194. -----------------------------------------
  195. -- Moves backwards with position tracking
  196. -----------------------------------------
  197. function class.back(steps)
  198.   for step=1,(steps or 1) do
  199.     if (not class.checkFuel()) then
  200.       return false
  201.     end
  202.     success, reason = robot.back()
  203.     if success then
  204.       class.position:sub(class.direction)
  205.     else
  206.       return false, reason
  207.     end
  208.   end
  209. end
  210.  
  211. -----------------------------------------
  212. -- Moves upwards with position tracking
  213. -----------------------------------------
  214. function class.up(steps)
  215.   for step=1,(steps or 1) do
  216.     if (not class.checkFuel()) then
  217.       return false
  218.     end
  219.     success, reason = robot.up()
  220.     if success then
  221.       class.position:add(UP)
  222.     else
  223.       return false, reason
  224.     end
  225.   end
  226. end
  227.  
  228. -----------------------------------------
  229. -- Moves upwards with position tracking
  230. -----------------------------------------
  231. function class.down(steps)
  232.   for step=1,(steps or 1) do
  233.     if (not class.checkFuel()) then
  234.       return false
  235.     end
  236.     success, reason = robot.down()
  237.     if success then
  238.       class.position:sub(UP)
  239.     else
  240.       return false, reason
  241.     end
  242.   end
  243. end
  244.  
  245. -----------------------------------------
  246. function class.getInventorySize()
  247.   return inventory.getInventorySize(sides.back)
  248. end
  249.  
  250. -----------------------------------------
  251. function class.isFuel()
  252.   if (inventory) then
  253.     local item = inventory.getStackInSlot(sides.back, robot.select())
  254.     if (item) then
  255.       return (item.id == class.ID.coal ) -- coal only, as pickaxes burn too
  256.     else
  257.       return false    
  258.     end  
  259.   else
  260.     local result,_ = generator.insert(0)
  261.     return result
  262.   end
  263. end
  264.  
  265. -----------------------------------------
  266. function class.selectFuel()
  267.   for slot = 1,class.getInventorySize() do
  268.     if (robot.count(slot) > 0) then
  269.       robot.select(slot)
  270.       if (class.isFuel()) then
  271.         return true
  272.       end
  273.     end
  274.   end
  275.   return false
  276. end
  277.  
  278. -----------------------------------------
  279. function class.shouldRefill()
  280.   return (generator.count() < 1)
  281. end
  282.  
  283. -----------------------------------------
  284. function class.refill()
  285.   if (class.selectFuel()) then
  286.     return generator.insert(1)
  287.   end
  288.   return false
  289. end
  290.  
  291. -----------------------------------------
  292. function class.checkFuel()
  293.   if (class.shouldRefill()) then
  294.     return class.refill()
  295.   else
  296.     return true
  297.   end
  298. end
  299.  
  300. return class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement