Advertisement
BombBloke

Turtle Server (ComputerCraft)

Jan 30th, 2014
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.10 KB | None | 0 0
  1. -- Turtle Tracking
  2. -- ----------------------------------------------------------
  3.  
  4. -- Which turtles are alive?
  5.  
  6. -- ----------------------------------------------------------
  7.  
  8. -- Initialise important values:
  9.  
  10. local turtles = {
  11. ["BBCrafter1"] = {["timer"] = os.startTimer(300), ["strike"] = 1},
  12. ["BBFarmer1"] = {["timer"] = os.startTimer(300), ["strike"] = 1},
  13. ["BBChopper1"] = {["timer"] = os.startTimer(300), ["strike"] = 1},
  14. ["BBChopper2"] = {["timer"] = os.startTimer(300), ["strike"] = 1},
  15. ["BBChopper3"] = {["timer"] = os.startTimer(300), ["strike"] = 1}}
  16.  
  17. local bgs = {colours.green,colours.orange,colours.red,colours.grey}
  18.  
  19. local mon, myEvent = peripheral.wrap("left")
  20.  
  21. local function monPrint(someText)
  22.     mon.write(someText)
  23.     local monX,monY = mon.getCursorPos()
  24.     mon.setCursorPos(1,monY+1)
  25. end
  26.  
  27. mon.setTextScale(1.5)
  28.  
  29. rednet.open("top")
  30.  
  31. term.clear()
  32. term.setCursorPos(1,1)
  33.  
  34. print("I'm a turtle server.")
  35. print("")
  36. print("My goal is to keep track of which turtles are active.")
  37.  
  38. os.startTimer(0)
  39.  
  40. while true do
  41.     myEvent = {os.pullEvent()}
  42.    
  43.     if myEvent[1] == "rednet_message" and type(myEvent[3]) == "table" and myEvent[3][1] == "Hello TurtleServer" then
  44.         turtles[myEvent[3][2]].id = myEvent[2]
  45.         turtles[myEvent[3][2]].timer = os.startTimer(300)
  46.         turtles[myEvent[3][2]].strike = 1
  47.         os.startTimer(0)
  48.     elseif myEvent[1] == "timer" then
  49.         mon.setBackgroundColor(colours.black)
  50.         mon.clear()
  51.         mon.setCursorPos(1,1)
  52.        
  53.         monPrint("Alive Turtles")
  54.         monPrint("=============")
  55.         monPrint("")
  56.        
  57.         for aTurtle,data in pairs(turtles) do
  58.             if data.timer == myEvent[2] then
  59.                 if data.id then rednet.send(data.id,"Hey, you still there?") end
  60.                 data.strike = data.strike + 1
  61.                 if data.strike > 4 then
  62.                     data.strike = 4
  63.                     if aTurtle:sub(1,9) == "BBChopper" and turtles["BBCrafter1"].id then rednet.send(turtles["BBCrafter1"].id,{"We need a new turtle.",aTurtle}) end
  64.                     data.timer = os.startTimer(1800)
  65.                 else
  66.                     data.timer = os.startTimer(300)
  67.                 end
  68.             end
  69.            
  70.             mon.setBackgroundColor(bgs[data.strike])
  71.             monPrint(aTurtle)
  72.             monPrint("")
  73.         end
  74.     end
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement