Advertisement
Fooman

Anvil Dropping Player

May 16th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.85 KB | None | 0 0
  1. local difficulty
  2. local player = 2
  3. local monitorSide = "back"
  4. local modemSide = "bottom"
  5. local running = true
  6. local xpos, ypos
  7. --Arrays Cooresponding to position of buttons on monitor
  8. --16 Difficulties, P1 Ready, P2 Ready, X to Reboot
  9. local xd = {8, 14, 20, 26, 8, 14, 20, 26, 8, 14, 20, 26, 8, 14, 20, 26, 8, 23, 33}
  10. local yd = {7, 7, 7, 7, 10, 10, 10, 10, 13, 13, 13, 13, 16, 16, 16, 16, 20, 20, 2}
  11. --Color (decimal) of Difficulty when printed
  12. local difficultyState = {}
  13. --Color of playerOne
  14. local playerOne = 1
  15. --Color of playerTwo
  16. local playerTwo = 1
  17. --Menu State and Table of Run Fuctions
  18. local menuState = "difficulty"
  19. local mopt = {
  20.     ["difficulty"] = {draw = drawDifficulty, changer = changeDifficulty},
  21.     ["ready"] = {draw = drawReady, changer = changeReady}
  22.     }
  23.  
  24.    
  25.    
  26. --Monitor Setup
  27. m = peripheral.wrap(monitorSide)
  28. m.setTextScale(0.5)
  29. local wm,hm = m.getSize()
  30.  
  31. function aSum(array,int)
  32.     arraySum = 0
  33.     for i=1,int do
  34.         arraySum = arraySum + array[i]
  35.     end
  36.     return arraySum
  37. end
  38.  
  39. function round(num, idp)
  40.   local mult = 10^(idp or 0)
  41.   return math.floor(num * mult + 0.5) / mult
  42. end
  43.  
  44. function monitorCentered(str,ypos)
  45.     m.setCursorPos(wm/2 - #str/2, ypos)
  46.     m.write(str)
  47. end
  48.  
  49. function monitorRight(str,ypos)
  50.     m.setCursorPos(wm - #str, ypos)
  51.     m.write(str)
  52. end
  53.  
  54. function monitorLeft(str,ypos)
  55.     m.setCursorPos(1 , ypos)
  56.     m.write(str)
  57. end
  58.  
  59. function drawMonitorBG(title)
  60.     m.clear()
  61.     m.setTextColor(colors.gray)
  62.     monitorCentered(string.rep("-",wm),1)
  63.     m.setTextColor(colors.lightGray)
  64.     monitorCentered(title, 2)
  65.     m.setTextColor(colors.gray)
  66.     monitorCentered(string.rep("-",wm),3)
  67.     monitorLeft("|",2)
  68.     monitorRight("|",2)
  69.         local th = 4
  70.     for th=4,hm-1 do
  71.         monitorLeft("|",th)
  72.         monitorRight("|",th)
  73.     end
  74.     monitorCentered(string.rep("-",wm),hm)
  75.     m.setTextColor(colors.red)
  76.     m.setCursorPos(wm-3,2)
  77.     m.write("X")
  78. end
  79.  
  80. function drawDifficulty()
  81.     for i=1,16 do
  82.     m.setTextColor(difficultyState[i])
  83.     m.setCursorPos(xd[i], yd[i])
  84.     m.write(tostring(i))
  85.     end
  86.    
  87.     m.setTextColor(playerOne)
  88.     m.setCursorPos(8,20)
  89.     m.write("1P Ready")
  90.     m.setTextColor(playerTwo)
  91.     m.setCursorPos(20,20)
  92.     m.write("2P Ready")
  93. end
  94.  
  95. function changeDifficulty()
  96.  
  97.     --For difficulty selection
  98.     for i=1,16 do
  99.         local xMax, xMin, yMax, yMin
  100.         xMax = xd[i] + 2
  101.         xMin = xd[i] - 2
  102.         yMax = yd[i] + 1
  103.         yMin = yd[i] - 1
  104.         if xpos >= xMin and xpos <= xMax and ypos >= yMin and ypos <= yMax then
  105.             --Resets Difficulty Colors
  106.             for j=1,16 do
  107.                 difficultyState[j] = 1
  108.             end
  109.             difficulty=i
  110.             --Makes active choice orange
  111.             difficultyState[i] = 2
  112.             --Locks Door...lol
  113.             rednet.broadcast("S"..player)
  114.         end
  115.     end
  116.  
  117.     --For bottom buttons
  118.     for i=17,18 do
  119.         local xMax, xMin, yMax, yMin
  120.         xMax = xd[i] + 5
  121.         xMin = xd[i] - 5
  122.         yMax = yd[i] + 1
  123.         yMin = yd[i] - 1
  124.         if xpos >= xMin and xpos <= xMax and ypos >= yMin and ypos <= yMax and aSum(difficultyState,16)==17 then
  125.             if i == 17 then
  126.                 playerOne = 2
  127.                 playerTwo = 1
  128.                 print("ready p1")
  129.                 rednet.broadcast("D"..difficulty)
  130.                 rednet.broadcast("P1")
  131.             end
  132.             if i == 18 then
  133.                 playerOne = 1
  134.                 playerTwo = 2
  135.                 print("ready p2")
  136.                 rednet.broadcast("D"..difficulty)
  137.                 rednet.broadcast("P2")
  138.             end
  139.             --Locks Door...lol
  140.             rednet.broadcast("S"..player)
  141.         end
  142.     end
  143.    
  144.     --To reboot
  145.     local xMax, xMin, yMax, yMin
  146.     xMax = xd[19] + 1
  147.     xMin = xd[19] - 1
  148.     yMax = yd[19] + 1
  149.     yMin = yd[19] - 1
  150.     if xpos >= xMin and xpos <= xMax and ypos >= yMin and ypos <= yMax then
  151.         os.reboot()
  152.     end
  153.        
  154. end
  155.  
  156.  
  157. --Initialization
  158.  
  159. --Open Rednet
  160. rednet.open(modemSide)
  161. --Broadcasts Signal to Unlock forcefield if locked
  162. rednet.broadcast("N"..player)
  163.  
  164. --Background Black
  165. m.setBackgroundColor(colors.black)
  166. --Resets Difficulty Array
  167. for i=1,16 do
  168.     difficultyState[i] = 1
  169. end
  170. --Extend for touchscreen
  171.  
  172.  
  173.    
  174.    
  175. --Draws Initial Page
  176. drawMonitorBG("Select Difficulty")
  177. drawDifficulty()
  178.  
  179.  
  180.  
  181.  
  182.  
  183. --Operation
  184. while running do
  185.     event, a, xpos, ypos = os.pullEvent()
  186.     if event == "monitor_touch" then
  187.             --Push Red X in Top Right to Reboot System
  188.             if (xpos == 33 and ypos == 2) then
  189.                 os.reboot()
  190.             end
  191.             changeDifficulty()
  192.             drawDifficulty()
  193.     end
  194.     if event == "rednet_message" then
  195.         if xpos == "reboot" then
  196.         os.reboot()
  197.         end
  198.     end
  199. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement