Advertisement
fremnet

MultiMobController

Mar 14th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.89 KB | None | 0 0
  1. RADIO="top"
  2. MONITOR="left"
  3. BUNDLE="right"
  4. DELAY=0.5
  5.  
  6. XPGate         = colors.white
  7. MidwayGate     = colors.lightBlue
  8. RoofGate       = colors.brown
  9. AttackGate     = colors.green
  10. WitherSkeleton = colors.lime
  11. Cow            = colors.red
  12.  
  13. Clamp          = colors.orange
  14.  
  15. LongDrop       = XPGate + MidwayGate + AttackGate
  16. XP             = 0
  17.  
  18. Safe           = LongDrop + RoofGate + Clamp
  19.  
  20. BotResponses   = 0
  21. Roof           = nil
  22. LavaBot        = nil
  23. monitor        = nil
  24. state          = 0
  25.  
  26. selection      = nil
  27.  
  28. function colorOn(color)
  29.     local c = redstone.getBundledOutput(BUNDLE)
  30.     c = colors.combine(c, color)
  31.     redstone.setBundledOutput(BUNDLE, c)
  32. end
  33.  
  34. function colorOff(color)
  35.     local c = redstone.getBundledOutput(BUNDLE)
  36.     c = colors.subtract(c, color)
  37.     redstone.setBundledOutput(BUNDLE, c)
  38. end
  39.  
  40. function colorToggle(color)
  41.     local c = redstone.getBundledOutput(BUNDLE)
  42.     if colors.test(c, color) then
  43.         c = colors.subtract(c, color)
  44.     else
  45.         c = colors.combine(c, color)
  46.     end
  47.     redstone.setBundledOutput(BUNDLE, c)
  48. end
  49.  
  50. function doorIsDown()
  51.     print("- doorIsDown?")
  52.     c = redstone.getBundledInput(BUNDLE)
  53.     return colors.test(c, colors.pink)
  54. end
  55.    
  56.  
  57. function moveDoor(color)
  58.     for i = 1, 5 do
  59.         colorOn(color)
  60.         os.sleep(DELAY)
  61.         colorOff(color)
  62.         os.sleep(DELAY)
  63.     end
  64. end
  65.  
  66. function moveDoorUp()
  67.     print("- moveDoorUp")
  68.     colorOff(Clamp)
  69.     sleep(0.2)
  70.     moveDoor(colors.yellow)
  71. end
  72.  
  73. function moveDoorDown()
  74.     print("- moveDoorDown")
  75.     moveDoor(colors.purple)
  76.     colorOn(Clamp)
  77. end
  78.  
  79. function reset()
  80.     print("- reset")
  81.     roofMobOff()
  82.     disarmAttackBots()
  83.     if not doorIsDown() then
  84.         moveDoorDown()
  85.     end
  86.     redstone.setBundledOutput(BUNDLE, Safe)
  87.     lava(false)
  88. end
  89.  
  90. function lava(on)
  91.     packet = {action="lava", type="multimob", device="controller"}
  92.     if on then
  93.         packet["state"] = "on"
  94.     else
  95.         packet["state"] = "off"
  96.     end
  97.     rednet.send(LavaBot, textutils.serialize(packet))
  98. end
  99.  
  100. function armAttackBots()
  101.     print("- armAttackBots")
  102.     colorOff(AttackGate)
  103.     sleep(0.2)
  104.     -- This is lazy, should look for a response from the turtles and do it in the event loop
  105.     for i = 1, 5 do
  106.         rednet.broadcast(textutils.serialize({action="attack", state="on", type="multimob", device="controller"}))
  107.         sleep(0.1)
  108.     end
  109. end
  110.  
  111. function disarmAttackBots()
  112.     print("- disarmAttackBots")
  113.     -- This is lazy, should look for a response from the turtles and do it in the event loop
  114.     for i = 1, 5 do
  115.         rednet.broadcast(textutils.serialize({action="attack", state="off", type="multimob", device="controller"}))
  116.         sleep(0.1)
  117.     end
  118.     sleep(0.5)
  119.     colorOn(AttackGate)
  120.     sleep(5)
  121. end
  122.  
  123. function beef()
  124.     roofMobOff()
  125.     lava(true)
  126.     colorOn(RoofGate + XPGate)
  127.     colorOff(MidwayGate)
  128.     if doorIsDown() then
  129.         moveDoorUp()
  130.     end
  131.     disarmAttackBots()
  132.     colorOn(Cow)
  133. end
  134.  
  135. function witherSkeleton()
  136.     roofMobOff()
  137.     lava(false)
  138.     colorOn(RoofGate)
  139.     colorOff(XPGate + MidwayGate)
  140.     armAttackBots()
  141.     if not doorIsDown() then
  142.         moveDoorDown()
  143.     end
  144.     colorOn(WitherSkeleton)
  145. end
  146.  
  147. function roofMob(mob)
  148.     lava(false)
  149.     colorOn(XPGate)
  150.     if doorIsDown() then
  151.         moveDoorUp()
  152.     else
  153.         sleep(2)
  154.     end
  155.     disarmAttackBots()
  156.     colorOff(RoofGate)
  157.     colorOn(LongDrop)
  158.     rednet.send(Roof, textutils.serialize({action="mobon", mob=mob, type="multimob", device="controller"}))
  159. end
  160.  
  161. function roofMobOff()
  162.     rednet.send(Roof, textutils.serialize({action="off", type="multimob", device="controller"}))
  163. end
  164.  
  165. function skeleton()
  166.     roofMob("skeleton")
  167. end
  168.  
  169. function creaper()
  170.     roofMob("creaper")
  171. end
  172.  
  173. function pigmen()
  174.     roofMob("pigmen")
  175. end
  176.  
  177. function processAcknowledge(data)
  178.         if data["device"] == "roof" then
  179.             print("Found roof device " .. data["computer_id"])
  180.             Roof = data["computer_id"]
  181.         elseif data["device"] == "lavabot" then
  182.             print("Found lavabot device " .. data["computer_id"])
  183.             LavaBot = data["computer_id"]
  184.         elseif data["device"] == "xpbot" then
  185.             print("Found xpbot device " .. data["computer_id"])
  186.             BotResponses = BotResponses + 1
  187.             -- do nothing
  188.         end
  189.  
  190.         if not(Roof == nil) and not(LavaBot == nil) and BotResponses == 2 and state == 0 then
  191.             state = 1
  192.             renderMenu()
  193.         end
  194.        
  195.         if data["action"] == "announce" then
  196.             rednet.send(data["computer_id"], textutils.serialize({action="acknowledge", type="multimob", device="controller"}))
  197.         end
  198. end
  199.  
  200. function processMessage(data)
  201.     if data["action"] == "acknowledge" or (data["action"] == "announce") then
  202.         processAcknowledge(data)
  203.     elseif data["action"] == "attack" then
  204.         if data["state"] == "on" and not attacking then
  205.             turtle.forward()
  206.             attacking = true
  207.             currentTimer = os.startTimer(SLEEP)
  208.             rednet.send(controller, textutils.serialize({action="attack", state="on", type="multimob", device="xpbot"}))
  209.         elseif data["state"] == "off" and attacking then
  210.             currentTimer = nil
  211.             emptyItems(true)
  212.             attacking = false
  213.             rednet.send(controller, textutils.serialize({action="attack", state="off", type="multimob", device="xpbot"}))
  214.         end
  215.     end
  216. end
  217.  
  218. function renderMode(mode)
  219.     monitor.clear()
  220.     monitor.setCursorPos(1,1)
  221.     monitor.write("MultiMob")
  222.     monitor.setCursorPos(1,2)
  223.     monitor.write("----------------------------")
  224.     monitor.setCursorPos(1,3)
  225.     monitor.write("Mode selected:")
  226.     monitor.setCursorPos(3,4)
  227.     monitor.write(mode)
  228.     renderWait()
  229.     selection = mode
  230. end
  231.  
  232. function renderWait()
  233.     monitor.setCursorPos(10,5)
  234.     monitor.clearLine()
  235.     monitor.write("Please wait...")
  236. end
  237.  
  238. function renderMenu()
  239.     monitor.clear()
  240.     monitor.setCursorPos(1,1)
  241.     monitor.write("MultiMob - Right click mode")
  242.     monitor.setCursorPos(1,2)
  243.     monitor.write("----------------------------")
  244.     monitor.setCursorPos(2,3)
  245.     monitor.write("1) XP Farm/Wither Skeleton")
  246.     monitor.setCursorPos(2,4)
  247.     monitor.write("2) Cooked beef")
  248.     monitor.setCursorPos(2,5)
  249.     monitor.write("3) Skeletons/Bones&Arrows")
  250.     monitor.setCursorPos(2,6)
  251.     monitor.write("4) Creeper/Gunpowder")
  252.     monitor.setCursorPos(2,7)
  253.     monitor.write("5) Zombie Pigman")
  254. end
  255.  
  256. function checkButton(x, y)
  257.     if state == 1 and y >= 3 and y <= 7 then
  258.         state = 2
  259.         if y == 3 then
  260.             renderMode("XP Farm/Wither Skeleton")
  261.             witherSkeleton()
  262.         elseif y == 4 then
  263.             renderMode("Cooked beef")
  264.             beef()
  265.         elseif y == 5 then
  266.             renderMode("Skeletons")
  267.             skeleton()
  268.         elseif y == 6 then
  269.             renderMode("Creeper")
  270.             creaper()
  271.         elseif y == 7 then
  272.             renderMode("Zombie Pigman")
  273.             pigmen()
  274.         end
  275.  
  276.         monitor.setCursorPos(1,5)
  277.         monitor.clearLine()
  278.         monitor.write("Ready")
  279.         monitor.setCursorPos(18,12)
  280.         monitor.write("End/Done")
  281.         state = 3
  282.     elseif state == 3 and y == 12 and x >= 12 then
  283.         state = 4
  284.         monitor.clear()
  285.         monitor.setCursorPos(1,1)
  286.         monitor.write("MultiMob")
  287.         monitor.setCursorPos(1,2)
  288.         monitor.write("----------------------------")
  289.         monitor.setCursorPos(1,3)
  290.         monitor.write("Resetting...")
  291.         renderWait()
  292.         if selection == "XP Farm/Wither Skeleton" or selection == "Cooked beef" then
  293.             colorOff(WitherSkeleton + Cow)
  294.             sleep(5)
  295.             colorOff(MidwayGate)
  296.             sleep(30)
  297.             reset()
  298.         else
  299.             reset()
  300.         end
  301.         state = 1
  302.         renderMenu()
  303.     end
  304.    
  305. end
  306.  
  307. monitor = peripheral.wrap(MONITOR)
  308. monitor.clear()
  309. monitor.setCursorPos(1,1)
  310. monitor.write("Startup....")
  311. monitor.setCursorPos(2,2)
  312. monitor.write("Looking for MultiMob parts...")
  313.  
  314. if peripheral.isPresent(RADIO) and peripheral.getType(RADIO) == "modem" then
  315.     rednet.open(RADIO)
  316.     rednet.broadcast(textutils.serialize({action="announce", type="multimob", device="controller"}))
  317. else
  318.     print("Modem not found")
  319.     return
  320. end
  321.  
  322. while true do
  323.     ev,p1,p2,p3 = os.pullEvent()
  324.     if ev == "rednet_message" then
  325.         data = textutils.unserialize(p2)
  326.         if data["type"] == "multimob" then
  327.             data["computer_id"] = p1
  328.             processMessage(data)
  329.         end
  330.     elseif ev == "char" then
  331.         if p1 == "r" then
  332.             reset()
  333.         elseif p1 == "s" then
  334.             skeleton()
  335.         elseif p1 == "c" then
  336.             creaper()
  337.         elseif p1 == "w" then
  338.             witherSkeleton()
  339.         elseif p1 == "b" then
  340.             beef()
  341.         end
  342.     elseif ev == "monitor_touch" then
  343.         checkButton(p2, p3)
  344.     end
  345. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement