Jharii

turtleStateManager

Jan 20th, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.03 KB | None | 0 0
  1. -- The turtle will always be in a loop unless it is ordered to shut down.
  2. -- Shut down will return the turtle to the OS after one final broadcast of its location.
  3. turtleShutDown = false
  4.  
  5. -- The turtle will always be running unless it receives a signal to deactivate.
  6. turtleActive = true
  7.  
  8. currentState = "Initialize"
  9. previoiusState = ""
  10.  
  11. turtleStates = {"Initialize", "Refuel", "Travel", "Mine", "Tunnel", "Chop", "Escape", "Home", "Sleep", "Explore", "Build", "Organize"}
  12.  
  13. -- Allows for turtles to be dedicated to one or more jobs.  Initialize, Refuel, Travel, Escape, Home, and Sleep will always be true.
  14. allowedTurtleStates = {true, true, true, true, true, true, true, true, true, true, true, true}
  15.  
  16. -- The turtle will keep track of its location and facing and will broadcast its location to the server.
  17.  
  18. worldCoord = vector.new(0,0,0)
  19. relativeCoord = vector.new(0,0,0)
  20.  
  21. facing = 1
  22. facings = {"West", "North", "East", "South"}
  23.  
  24. -- A fuel slot will default to 1 unless defined otherwise.
  25. -- A turtle can have more than one fuel slot.
  26.  
  27. fuelSlot = 1
  28.  
  29. function isTurtleHome()
  30.   isHome = false
  31.   if xHome == 0 and zHome == 0 and yHome == 0 then
  32.     isHome = true
  33.   end
  34.   return isHome
  35. end
  36.  
  37. function activateState(stateName)
  38.   message = ""
  39.   for stateNumber = 1,12 do
  40.     if stateName == turtleStates[stateNumber] then
  41.       allowedTurtleStates = true
  42.     else
  43.       message = "Unknown state."
  44.     end
  45.   end
  46.   message = turtleStates[stateNumber] .. " is active."
  47.   return message
  48. end
  49.  
  50. function deactivateState(stateName)
  51.   message = ""
  52.   for stateNumber = 1,12 do
  53.     if stateName == turtleStates[stateNumber] then
  54.       if stateNumber == 1 or stateNumber == 2 or stateNumber == 3 or stateNumber == 7 or stateNumber == 8 or stateNumber == 9 then
  55.         message = turtleStates(stateName) .. " cannot be deactivated."
  56.       else
  57.         allowedTurtleStates = false
  58.         message = turtleStates(stateName) .. " is deactivated."
  59.       end
  60.     else
  61.       message = "Unknown state."
  62.     end
  63.   end
  64.   return message
  65. end
  66.  
  67. while not (turtleShutDown) do
  68.   -- Active state manager.  As long as the turtle is active, this loop will always run.
  69.   while turtleActive do
  70.     -- LISTEN FOR SIGNAL
  71.     -- The server can send a signal to the turtle to activate/deactivate or to change its state
  72.  
  73.     -- The refueling function should be called within the individual states, as necessary.
  74.     -- Refueling may return to the core functionality of the turtle.
  75.  
  76.     -- CORE FUNCTIONALITY
  77.     -- THIS IS WHERE ALL OF THE CORE FUNCTIONS AND BEHAVIORS WILL BE LOCATED
  78.     -- The functions found within will be located in their own files.  (new API?)
  79.  
  80.  
  81.  
  82.     -- END CORE FUNCTIONALITY
  83.  
  84.     -- Broadcast location
  85.   end
  86.  
  87.   -- Inactive manager.  As long as the turtle is inactive, this loop will always run.
  88.   while not(turtleActive) do
  89.     -- LISTEN FOR SIGNAL
  90.  
  91.     -- If the turtle is not home, it repeatedly broadcasts its location for retrieval.
  92.     if not(isTurtleHome()) then
  93.     -- Broadcast location
  94.     end
  95.   end
  96.  
  97. -- Broadcast location
Advertisement
Add Comment
Please, Sign In to add comment