Advertisement
neonerz

spawner

Mar 17th, 2013
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.16 KB | None | 0 0
  1. --
  2. --Killing Floor Spawner Touchscreen by neonerZ v1.0
  3. --http://youtube.com/neonerz
  4. --
  5.  
  6. --Variables
  7. transSide="back"
  8. receiveSide="right"
  9. wirelessModem="top"
  10. --os.loadAPI("button")
  11.  
  12. m = peripheral.wrap("front")
  13. m.clear()
  14. m.setBackgroundColor(colors.black)
  15. trans = peripheral.wrap(transSide)
  16. receive = peripheral.wrap(receiveSide)
  17.  
  18.  
  19. local button={}
  20.  
  21. --Function to create button table
  22. function setTable(name, text, xmin, ymin, width, height, onFreq, offFreq, bcolor, tcolor)
  23.    button[name] = {}
  24.    button[name]["text"] = text
  25.    button[name]["active"] = false
  26.    button[name]["xmin"] = xmin
  27.    button[name]["ymin"] = ymin
  28.    button[name]["width"] = width
  29.    button[name]["height"] = height
  30.    button[name]["onFreq"] = onFreq
  31.    button[name]["offFreq"] = offFreq
  32.    button[name]["bcolor"] = bcolor
  33.    button[name]["tcolor"] = tcolor
  34. end
  35.  
  36. --Toggle button on and off
  37. function toggleButton(name, state)
  38.     if state=="on" then
  39.         button[name]["active"] = true
  40.     else
  41.         button[name]["active"] = false
  42.     end
  43. end    
  44.  
  45. --Draw button on screen
  46. function drawButton(x, y, width, height, bcolor, tcolor, text)
  47.     m.setBackgroundColor(bcolor)
  48.     --Draw the background
  49.     for i=1,height do
  50.         m.setCursorPos(x,y+i-1)
  51.         m.write(string.rep(" ", width))
  52.     end
  53. --  m.setBackgroundColor(colors.black)
  54.     --Write the text
  55.      m.setTextColor(tcolor)
  56.     local textX = x + math.floor(width/2 - string.len(text)/2)
  57.     local textY = y + math.floor(height/2)
  58.     m.setCursorPos(textX, textY)
  59.     m.write(text)
  60. end
  61.  
  62. --Check if a button within the table has been clicked
  63. --(returns button name if matches, or false if not)
  64. function checkxy(x, y)
  65.     returnCheckxy=false
  66.     for name, data in pairs(button) do
  67.         if data["height"] == 1 then
  68.             ymax = data["ymin"]
  69.         else
  70.             ymax = data["height"] + data["ymin"]
  71.         end
  72.        
  73.         if data["width"] == 1 then
  74.             xmax = data["xmin"]
  75.         else
  76.             xmax = data["width"] + data["xmin"]
  77.         end
  78.         if y>=data["ymin"] and  y <= ymax then
  79.             if x>=data["xmin"] and x<= xmax then
  80.                 returnCheckxy = {name = name,
  81.                                 text = data["text"],
  82.                                 onFreq = data["onFreq"],
  83.                                 offFreq = data["offFreq"],
  84.                                 bcolor = data["bcolor"],
  85.                                 tcolor = data["tcolor"],
  86.                                 active = data["active"]}
  87.             end
  88.         end
  89.     end
  90. end
  91.  
  92. --name, text, xmin, ymin, width, height, onFreq, offFreq, bcolor, tcolor
  93. --Register buttons
  94. function regButtons()
  95.     setTable("creeper", "Creeper", 2, 1, 7, 1, 4009 ,4109, colors.blue, colors.white)
  96.     setTable("endermen", "Endermen", 10, 1, 8, 1, 4010 ,4110, colors.blue, colors.white)
  97.     setTable("angryzombie", "Angry Zombie", 19, 1, 12, 1, 4002,4102, colors.blue, colors.white)
  98.     setTable("cavespider", "Cave Spider", 32, 1, 11, 1, 4003,4103, colors.blue, colors.white)
  99.     setTable("pigman", "Pigmen", 44, 1, 6, 1, 4004,4104, colors.blue, colors.white)
  100.     setTable("witherskeleton", "Wither Skeleton", 2, 3, 15, 1, 4011,4111, colors.blue, colors.white)
  101.     setTable("skeleton", "Skeleton", 18, 3, 8, 1, 4007,4107, colors.blue, colors.white)
  102.     setTable("blaze", "Blaze", 27, 3, 5, 1, 4008,4108, colors.blue, colors.white)
  103.     setTable("mooshroom", "Mooshroom", 33, 3, 9, 1, 4006,4106, colors.blue, colors.white)
  104.     setTable("chicken", "Chicken", 43, 3, 7, 1, 4005,4105, colors.blue, colors.white)
  105.     setTable("pig", "Pig", 2, 5, 3, 1, 4001,4101, colors.blue, colors.white)
  106.     setTable("twmobs", "Twilight Forest Mobs", 6 ,5 ,20 ,1 , 4009,4109, colors.blue, colors.white)
  107.     setTable("xp_mode", "XP Mode", 43, 5, 7, 1 , 0,0, colors.red, colors.white)
  108.  
  109. end
  110.  
  111. --Draw out GUI using the buttons registered in the regButtons function
  112. function displayGUI()
  113.     m.clear()
  114.     m.setBackgroundColor(colors.black)
  115.     for name,data in pairs(button) do
  116. --      print(name)
  117.         text=data["text"]
  118.         xmin=data["xmin"]
  119.         ymin=data["ymin"]
  120.         width=data["width"]
  121.         height=data["height"]
  122.         tcolor=data["tcolor"]
  123.         active=data["active"]
  124.         if active == true then
  125.             bcolor = colors.lime
  126.         else
  127.             bcolor=data["bcolor"]
  128.         end
  129.         drawButton(xmin, ymin, width, height, bcolor, tcolor, text)
  130.     end
  131.  
  132. end
  133.  
  134. --Turns the spawners on    
  135. function enableSpawer(mob)
  136.     toggle="on"
  137.     --If you select XP Mode, make sure to turn off all other mobs first
  138.     --Then turn on the required mobs for XP mode
  139.     if mob=="xp_mode" and button["xp_mode"]["active"] == false then
  140.         disableAll()
  141.         enableSpawer("witherskeleton")
  142.         enableSpawer("pigman")
  143. --      enableSpawer("angryzombie")
  144.         enableSpawer("mooshroom")
  145.         return
  146.     end
  147.     --Don't allow any other mobs to be activated if XP mode is active
  148.     if button["xp_mode"]["active"] == true and mob~="xp_mode"  then
  149.         print("no other mobs allowed when in XP mode")
  150.         toggle="off"
  151.         return
  152.     end
  153.     --If turning on creepers, disable all other mobs
  154.     if mob=="creeper" then
  155.         disableAll()
  156.     elseif button["creeper"]["active"] == true then
  157.         print("no other mobs allowed when in creeper mode")
  158.         toggle="off"
  159.         return
  160.     end
  161.     --Check to see if the mob is disabled before activating it
  162.     -- if running, do nothing, if not, enable it
  163.     if checkMobDisabled(mob) then
  164.         freq = button[mob]["onFreq"]
  165.         trans.setFreq(freq)
  166.         redstone.setOutput(transSide, true)
  167.         sleep(.4)
  168.         redstone.setOutput(transSide, false)
  169.         print(mob.." enabled")
  170.     else
  171.         print(mob.." already enabled")
  172.     end
  173.  
  174.     trans.setFreq(0)
  175. end
  176.  
  177. --Disable spawners
  178. function disableSpawer(mob)
  179.     if mob=="xp_mode" then
  180.         print("new test")
  181.         disableAll()
  182.         return
  183.     end
  184.     if checkMobDisabled(mob) and mob~="xp_mode" then
  185.         print(mob.." already disabled")
  186.     else
  187.         freq = button[mob]["onFreq"]
  188.         trans.setFreq(freq)
  189.         redstone.setOutput(transSide, true)
  190.         sleep(.4)
  191.         redstone.setOutput(transSide, false)
  192.         print(mob.." disabled")
  193.     end
  194.     trans.setFreq(0)
  195. end
  196.  
  197. --Check to see if a mob(button) is disabled or not
  198. --checks offFreq to see if there's a redstone signal
  199. --if there is, spawner is enabled
  200. function checkMobDisabled(mob)
  201.     freq = button[mob]["offFreq"]
  202.     receive.setFreq(freq)
  203.     if redstone.getInput(receiveSide) then
  204.         return true
  205.     else
  206.         return false
  207.     end
  208. end
  209.  
  210. --Quick disable all function (used in various parts)
  211. function disableAll()
  212.     for name,data in pairs(button) do
  213.         if name ~= "xp_mode" then
  214.         disableSpawer(name)
  215.         toggleButton(name,"off")
  216.         end
  217.     end
  218. end
  219.  
  220. tArgs = {...}
  221.  
  222. --register buttons and disable all spawners
  223. --this is necessary as sometimes wireless redstone
  224. --messes up when the chunks load and some spawners
  225. --get enabled
  226. regButtons()
  227. disableAll()
  228.  
  229. --If you run the script without passing a variable
  230. --show usage commands
  231. if tArgs[1] == nil then
  232.     print("Usage: spawner enable|disable <mob>")
  233.     print("       spawner gui | spawner disableAll")
  234.     print("       spawner disableAll")
  235.     print("Mob options:")
  236.     for name,data in pairs(button) do
  237.         print(name)
  238.         sleep(0.1)
  239.     end
  240. --If you send it "spawner enable <mob>" turn just that mob on
  241. elseif string.lower(tArgs[1]) == "enable" and string.lower(tArgs[2]) ~= nil then
  242.     enableSpawer(string.lower(tArgs[2]))
  243. --If you send it "spawner disable <mob>", disable just that mob
  244. elseif string.lower(tArgs[1]) == "disable" and tArgs[2] ~= nil then
  245.     disableSpawer(string.lower(tArgs[2]))
  246. --If you send it "spawner disableall", disable all mobs
  247. elseif string.lower(tArgs[1]) == "disableall" then
  248.     disableAll()
  249. --If you send is "spawner gui", start the script in GUI mode
  250. elseif string.lower(tArgs[1]) == "gui" then
  251.     --Display GUI and wait for touch input
  252.     displayGUI()
  253.     while true do
  254.         local e, side, x,y = os.pullEvent("monitor_touch")
  255.         checkxy(x,y)
  256.         if returnCheckxy ~= false then
  257.             --If touch input is detected, and the x,y of the click
  258.             --matches a registered button, and that mob is disabled
  259.             --activate that mob, else deactivate the spawner
  260.             if not returnCheckxy["active"] then
  261.                 print(returnCheckxy["name"].." turned on")
  262.                 enableSpawer(returnCheckxy["name"])
  263.                 toggleButton(returnCheckxy["name"],"on")
  264.                 if toggle=="off" then toggleButton(returnCheckxy["name"],"off") end
  265.                 m.clear()
  266.                 m.setBackgroundColor(colors.black)
  267.                 displayGUI()
  268.             else
  269.                 print(returnCheckxy["name"].." turned off")
  270.                 toggleButton(returnCheckxy["name"],"off")
  271.                 disableSpawer(returnCheckxy["name"])
  272.                 m.clear()
  273.                 m.setBackgroundColor(colors.black)
  274.                 displayGUI()
  275.             end
  276.         end
  277.     end
  278. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement