Advertisement
fremnet

MultiMobBot

Mar 14th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.93 KB | None | 0 0
  1. RADIO="right"
  2. SLEEP = 0.1
  3.  
  4. currentTimer = nil
  5. attacking = false
  6.  
  7. controller = nil
  8.  
  9. function emptyItems(stay)
  10.     rednet.send(controller, textutils.serialize({action="dump", type="multimob", device="xpbot"}))
  11.     turtle.turnRight()
  12.     turtle.turnRight()
  13.     turtle.forward()
  14.     for i = 1, 16 do
  15.         turtle.select(i)
  16.         turtle.drop(turtle.getItemCount(i))
  17.     end
  18.     turtle.turnRight()
  19.     turtle.turnRight()
  20.     if stay == nil then
  21.         turtle.forward()
  22.     end
  23. end
  24.  
  25. function processMessage(data)
  26.     if data["action"] == "acknowledge" or (data["action"] == "announce" and data["device"] == "controller") then
  27.         controller = data["computer_id"]
  28.         if data["action"] == "announce" then
  29.             rednet.send(data["computer_id"], textutils.serialize({action="acknowledge", type="multimob", device="xpbot"}))
  30.         end
  31.     elseif data["action"] == "attack" then
  32.         if data["state"] == "on" and not attacking then
  33.             turtle.forward()
  34.             attacking = true
  35.             currentTimer = os.startTimer(SLEEP)
  36.             rednet.send(controller, textutils.serialize({action="attack", state="on", type="multimob", device="xpbot"}))
  37.         elseif data["state"] == "off" and attacking then
  38.             currentTimer = nil
  39.             emptyItems(true)
  40.             attacking = false
  41.             rednet.send(controller, textutils.serialize({action="attack", state="off", type="multimob", device="xpbot"}))
  42.         end
  43.     end
  44. end
  45.  
  46. if peripheral.isPresent(RADIO) and peripheral.getType(RADIO) == "modem" then
  47.     rednet.open(RADIO)
  48.     rednet.broadcast(textutils.serialize({action="announce", type="multimob", device="xpbot"}))
  49. else
  50.     print("Modem not found")
  51.     return
  52. end
  53.  
  54. while true do
  55.     ev,p1,p2,p3 = os.pullEvent()
  56.     if ev == "rednet_message" then
  57.         data = textutils.unserialize(p2)
  58.         if data["type"] == "multimob" then
  59.             data["computer_id"] = p1
  60.             processMessage(data)
  61.         end
  62.     elseif ev == "timer" and currentTimer == p1 then
  63.         turtle.attack()
  64.         if turtle.getItemCount(16) > 16 then
  65.             emptyItems()
  66.         end
  67.         currentTimer = os.startTimer(SLEEP)
  68.     end
  69. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement