Advertisement
OldDragon2A

Murder Box Extreme

Jan 22nd, 2013
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.43 KB | None | 0 0
  1. local attack_speed = 0.1
  2. local drop_speed   = 0.1
  3.  
  4. local active   = false -- false is off by default, true is on by default
  5. local sides    = { "bottom", "left", "front", "right", "back", "top" }
  6. local detected = {}
  7. local drop     = nil
  8. local attack   = {}
  9.  
  10. local function boot()
  11.   print("Booting Neighbors")
  12.   for k,v in pairs(sides) do
  13.     if peripheral.isPresent(v) then
  14.       local type = peripheral.getType(v)
  15.       detected[v] = type  == "computer" or type == "turtle"
  16.       if detected[v] then
  17.         peripheral.call(v, "turnOn")
  18.       end
  19.     end
  20.   end
  21. end
  22.  
  23. -- Shutdown any computers in the mix
  24. local function notTurtle()
  25.   sleep(5)
  26.   os.shutdown()
  27. end
  28.  
  29. -- determine drop and attack directions
  30. local function configure()
  31.   print("Configuring")
  32.  
  33.   -- Label computer if not already labeled
  34.   if (os.getComputerLabel() == nil) then os.setComputerLabel('MBE') end
  35.  
  36.   -- drop based on adjacent turtles or lack of
  37.   if detected["bottom"] then
  38.     drop = turtle.dropDown
  39.   elseif detected["front"] then
  40.     drop = turtle.drop
  41.   elseif detected["top"] and not (detected["left"] or detected["right"] or detected["back"]) then
  42.     drop = turtle.dropDown
  43.   end
  44.  
  45.   -- Attack in open directions
  46.   if not detected['top'] and not turtle.detectUp() then
  47.     table.insert(attack, turtle.attackUp)
  48.   end
  49.   if not detected['front'] and not turtle.detect() then
  50.     table.insert(attack, turtle.attack)
  51.   end
  52.  
  53.   -- Open the modem
  54.   rednet.open("right")
  55. end
  56.  
  57. local function dropAll()
  58.   -- check all of the slots
  59.   for i = 1, 16 do
  60.     -- skip empty slots
  61.     if turtle.getItemCount(i) ~= 0 then
  62.       turtle.select(i)
  63.       while drop() do sleep(drop_speed) end
  64.     end
  65.   end
  66.   turtle.select(1)
  67. end
  68.  
  69. local function pastebin(code, filename)
  70.   local response = http.get("http://pastebin.com/raw.php?i=" .. textutils.urlEncode(code))
  71.   if response then
  72.     local file = fs.open(filename, "w")
  73.     file.write(response.readAll())
  74.     file.close()
  75.     response.close()
  76.   end
  77.   return response
  78. end
  79.  
  80. local function menu()
  81.   clear()
  82.   print("MBE Online")
  83.   print("")
  84.   print("M - Murder")
  85.   print("O - Turn Off")
  86.   print("R - Reboot")
  87.   print("U - Update")
  88. end
  89.  
  90. -- event handler
  91. local function ready()
  92.   if active then os.startTimer(attack_speed) end
  93.   while true do
  94.     local event, param1, param2, param3 = os.pullEvent()
  95.     if event == "timer" then -- kill things
  96.       for k,v in pairs(attack) do v() end
  97.       if drop then dropAll() end
  98.       if active then os.startTimer(attack_speed) end
  99.     elseif event == "rednet_message" then -- listen for messages
  100.       if param2 == "MBE-Off" then
  101.         active = false
  102.       elseif param2 == "MBE-On" then
  103.         active = true
  104.         os.startTimer(attack_speed)
  105.       elseif param2 == "MBE-Reboot" then
  106.         os.reboot()
  107.       elseif param2 == "MBE-Update" then
  108.         pastebin("VYrjGe9G", "startup")
  109.         os.reboot()
  110.       end
  111.     elseif event == "char" then -- handle direct input
  112.       if     param1 == "m" or param1 == "M" then rednet.broadcast("MBE-On")
  113.       elseif param1 == "o" or param1 == "O" then rednet.broadcast("MBE-Off")
  114.       elseif param1 == "r" or param1 == "R" then rednet.broadcast("MBE-Reboot")
  115.       elseif param1 == "u" or param1 == "U" then rednet.broadcast("MBE-Update")
  116.       end
  117.     end
  118.   end
  119. end
  120.  
  121. local function main()
  122.   boot()
  123.   if turtle == nil then notTurtle() end
  124.   configure()
  125.   menu()
  126.   ready()
  127. end
  128.  
  129. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement