Advertisement
Saldor010

MissileCanyon

Feb 14th, 2016
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.15 KB | None | 0 0
  1. local oldPull = os.pullEvent
  2. os.pullEvent = os.pullEventRaw
  3. local args = {...}
  4.  
  5. local tdifficulty = 2
  6. if args[1] then
  7.   tdifficulty = args[1]
  8. end
  9.  
  10. local wD,hT = term.getSize()
  11. hT = hT-2
  12. local difficulty = tonumber(tdifficulty) -- 1 = Easy, 2 = Mid, 3 = Hard
  13.  
  14. if difficulty ~= 1 and difficulty ~= 2 and difficulty ~= 3 then
  15.   difficulty = 2
  16. end
  17.  
  18. local points = {}
  19. points.left = 0
  20. points.right = 0
  21. points.win = 10
  22.  
  23. local gameBoard = {}
  24. local msg = ""
  25. local someoneWasDelayed = 0
  26.  
  27. for i=1,wD do
  28.   gameBoard[i] = {}
  29.   for j=1,hT do
  30.     gameBoard[i][j] = nil
  31.   end
  32. end
  33.  
  34. for i=1,hT do
  35.  
  36.   gameBoard[math.floor((wD/2)-5)][i] = {
  37.     ["char"] = "@",
  38.     ["charColor"] = colors.red,
  39.     ["bgColor"] = colors.pink,
  40.   }
  41.  
  42.   gameBoard[math.floor((wD/2)+5)][i] = {
  43.     ["char"] = "@",
  44.     ["charColor"] = colors.blue,
  45.     ["bgColor"] = colors.lightBlue,
  46.   }
  47.  
  48. end
  49.  
  50. local function drawGameBoard()
  51.   for k,v in pairs(gameBoard) do
  52.     for p,b in pairs(v) do
  53.       if b ~= nil then
  54.         if b["bgColor"] then paintutils.drawPixel(k,p+1,b["bgColor"]) else paintutils.drawPixel(k,p,colors.black) end
  55.         term.setCursorPos(k,p+1)
  56.         term.setTextColor(b["charColor"])
  57.         term.write(b["char"])
  58.       end
  59.     end
  60.   end
  61. end
  62.  
  63. local function updateGameBoard()
  64.   local updatedBoard = {}
  65.   for k,v in pairs(gameBoard) do
  66.     updatedBoard[k] = {}
  67.     for p,b in pairs(v) do
  68.       updatedBoard[k][p] = b
  69.     end
  70.   end
  71.   for k,v in pairs(gameBoard) do
  72.     --print(k)
  73.     --print(v)
  74.     for p,b in pairs(v) do
  75.       if b ~= nil then
  76.         if b.velocity then
  77.           local newPos = k+b.velocity
  78.           if newPos > wD or newPos <= 0 then
  79.             if newPos > wD then points.left = points.left + 1 end
  80.             if newPos <= 0 then points.right = points.right + 1 end
  81.             updatedBoard[k][p] = nil
  82.           else
  83.             if updatedBoard[newPos][p] ~= nil then
  84.               -- collision!
  85.               local collided = updatedBoard[newPos][p]
  86.               if collided.explosion == nil and updatedBoard[k][p].explosion ~= true and updatedBoard[newPos][p].onCollision == nil then
  87.                 -- create an explosion! :D
  88.                 for i=1,3 do
  89.                   for j=1,3 do
  90.                     local charChoice = math.random(1,3)
  91.                     local charChoice2 = ""
  92.                     if charChoice == 1 then
  93.                       charChoice2 = "@"
  94.                     elseif charChoice == 2 then
  95.                       charChoice2 = "#"
  96.                     elseif charChoice == 3 then
  97.                       charChoice2 = "*"
  98.                     end
  99.                    
  100.                     local colorChoice = math.random(1,3)
  101.                     local colorChoice2 = 0
  102.                     if colorChoice == 1 then
  103.                       colorChoice2 = colors.red
  104.                     elseif colorChoice == 2 then
  105.                       colorChoice2 = colors.orange
  106.                     elseif colorChoice == 3 then
  107.                       colorChoice2 = colors.yellow
  108.                     end
  109.                    
  110.                     if newPos+i-2 <= wD or p+j-2 > 0 then
  111.                       if not updatedBoard[newPos+i-2] then
  112.                      
  113.                       else
  114.                         updatedBoard[newPos+i-2][p+j-2] = {
  115.                           ["char"] = charChoice2,
  116.                           ["charColor"] = colors.white,
  117.                           ["bgColor"] = colorChoice2,
  118.                           ["explosion"] = 6+(math.random(-1,2)),
  119.                         }
  120.                       end
  121.                     end
  122.                   end
  123.                 end
  124.               else
  125.                 -- aw, just delete me already :(
  126.                 if updatedBoard[newPos][p].onCollision then
  127.                   updatedBoard[newPos][p].onCollision(gameBoard[k][p])
  128.                 end
  129.                 updatedBoard[k][p] = nil
  130.                 updatedBoard[newPos][p] = nil
  131.               end
  132.             else
  133.               -- empty..
  134.               updatedBoard[newPos][p] = updatedBoard[k][p]
  135.              
  136.               local colorChoice = math.random(1,3)
  137.               local colorChoice2 = nil
  138.              
  139.               if colorChoice == 1 then
  140.                 colorChoice2 = colors.orange
  141.               elseif colorChoice == 2 then
  142.                 colorChoice2 = colors.gray
  143.               elseif colorChoice == 3 then
  144.                 colorChoice2 = colors.red
  145.               end
  146.               updatedBoard[k][p] = {
  147.                 ["char"] = "@",
  148.                 ["charColor"] = colorChoice2,
  149.                 ["bgColor"] = nil,
  150.                 ["explosion"] = math.random(2,3)
  151.               }
  152.             end
  153.           end
  154.         end
  155.         if b.explosion then
  156.           b.explosion = b.explosion - 1
  157.           if b.explosion <= 0 then
  158.             updatedBoard[k][p] = nil
  159.           end
  160.         end
  161.       end
  162.     end
  163.   end
  164.   gameBoard = updatedBoard
  165.   term.setBackgroundColor(colors.black)
  166.   term.clear()
  167.   drawGameBoard()
  168.   paintutils.drawLine(1,hT+2,wD,hT+2,colors.gray)
  169.   term.setCursorPos(1,hT+2)
  170.   term.setTextColor(colors.white)
  171.   term.write(msg)
  172.  
  173.   paintutils.drawLine(1,1,wD,1,colors.gray)
  174.   term.setCursorPos(1,1)
  175.   term.setTextColor(colors.blue)
  176.   term.write(points.left)
  177.   term.setCursorPos(wD,1)
  178.   term.setTextColor(colors.red)
  179.   term.write(points.right)
  180.   if points.left >= points.win then
  181.     term.setBackgroundColor(colors.black)
  182.     term.clear()
  183.     term.setCursorPos(1,1)
  184.     term.setTextColor(colors.lightBlue)
  185.     print("The player has won!")
  186.     term.setTextColor(colors.white)
  187.     os.pullEvent = oldPull
  188.     error()
  189.   elseif points.right >= points.win then
  190.     term.setBackgroundColor(colors.black)
  191.     term.clear()
  192.     term.setCursorPos(1,1)
  193.     term.setTextColor(colors.red)
  194.     print("The AI has won!")
  195.     term.setTextColor(colors.white)
  196.     os.pullEvent = oldPull
  197.     error()
  198.   end
  199. end
  200.  
  201. local playerWait = 0
  202. local function spawnMissile(y,v,plr)
  203.   local x = 0
  204.   local charChoice = "<"
  205.   if v <= -1 then
  206.     x = wD
  207.     charChoice = "<"
  208.   elseif v >= 1 then
  209.     x = 1
  210.     charChoice = ">"
  211.   end
  212.   gameBoard[x][y] = {
  213.     ["char"] = charChoice,
  214.     ["charColor"] = colors.white,
  215.     ["velocity"] = v,
  216.   }
  217.   if plr then
  218.     playerWait = 0.5
  219.   end
  220. end
  221.  
  222. local function scanForMissiles(enemy)
  223.   local missiles = {}
  224.   for k,v in pairs(gameBoard) do
  225.     for p,b in pairs(v) do
  226.       if enemy and b.char == ">" then
  227.         missiles[#missiles+1] = {
  228.           ["x"] = k,
  229.           ["y"] = p,
  230.         }
  231.       elseif enemy == false and b.char == "<" then
  232.         missiles[#missiles+1] = {
  233.           ["x"] = k,
  234.           ["y"] = p,
  235.         }
  236.       end
  237.     end
  238.   end
  239.  
  240.   return missiles
  241. end
  242.  
  243. local function AIUpdate()
  244.   local missiles = scanForMissiles(true)
  245.   local counterMissiles = scanForMissiles(false)
  246.  
  247.   local enemyMissile = false
  248.   for k,v in pairs(missiles) do
  249.     local notice = math.random(1,5-difficulty)
  250.     if notice == 1 then
  251.       local found = false
  252.       for p,b in pairs(counterMissiles) do
  253.         if b.y == v.y then
  254.           found = true
  255.         end
  256.       end
  257.       if not found then
  258.         enemyMissile = true
  259.         os.queueEvent("fireMissile",v.y)
  260.       end
  261.     end
  262.   end
  263.   if not enemyMissile and difficulty >= 2 then
  264.     local chance = math.random(1,30/difficulty)
  265.     os.queueEvent("fireMissile",math.random(1,hT))
  266.   end
  267. end
  268.  
  269. local AIWait = 0
  270. local function powerUp()
  271.   --sleep(math.random(8,20))
  272.   local chance = math.random(1,200)
  273.   if chance == 7 then
  274.     msg = "A bonus box has spawned!"
  275.     local X = math.random(math.floor(wD/2)-4,math.floor(wD/2)+4)
  276.     local Y = math.random(math.floor(hT/2)-4,math.floor(hT/2)+4)
  277.     gameBoard[X][Y] = {
  278.       ["char"] = "$",
  279.       ["charColor"] = colors.white,
  280.       ["bgColor"] = colors.lime,
  281.       ["onCollision"] = function(collided)
  282.         if someoneWasDelayed == 0 then
  283.           if collided.char == "<" then
  284.             --msg = "You cannot fire missiles for 3 seconds!"
  285.             playerWait = 3
  286.             someoneWasDelayed = 1
  287.           elseif collided.char == ">" then
  288.             --msg = "The enemy cannot fire missiles for 3 seconds!"
  289.             AIWait = 3
  290.             someoneWasDelayed = 2
  291.           end
  292.         end
  293.       end
  294.     }
  295.   end
  296. end
  297. --lastTime = 0
  298. parallel.waitForAny(
  299.   function()
  300.     while true do
  301.       sleep(0.1)
  302.       powerUp()
  303.       AIWait = AIWait - 0.1
  304.       playerWait = playerWait - 0.1
  305.       msg = tostring(someoneWasDelayed)
  306.       if playerWait > 0 and someoneWasDelayed == 1 then
  307.         msg = "You cannot fire a missile for "..playerWait.." seconds!"
  308.       elseif playerWait <= 0 and someoneWasDelayed == 1 then
  309.         msg = ""
  310.         someoneWasDelayed = 0
  311.       end
  312.       if AIWait > 0 and someoneWasDelayed == 2 then
  313.         msg = "The AI cannot fire a missile for "..AIWait.." seconds!"
  314.       elseif AIWait <= 0 and someoneWasDelayed == 2 then
  315.         --msg = "2"
  316.         someoneWasDelayed = 0
  317.       end
  318.       AIUpdate()
  319.       updateGameBoard()
  320.     end
  321.   end,
  322.   function()
  323.     while true do
  324.       local ev = {os.pullEvent()}
  325.       if ev[1] == "mouse_click" then
  326.         ev[4] = ev[4]-1
  327.         if ev[4] > hT then
  328.           ev[4] = hT
  329.         end
  330.         if ev[2] == 1 and playerWait <= 0 then
  331.           spawnMissile(ev[4],1,true)
  332.         end
  333.       elseif ev[1] == "fireMissile" then
  334.         if AIWait <= 0 then
  335.           --print(os.time()-lastTime)
  336.           --lastTime = os.time()
  337.           if ev[2] > hT then
  338.             ev[2] = hT
  339.           end
  340.           spawnMissile(ev[2],-1)
  341.           if tdifficulty == 1 then
  342.             AIWait = 0.8
  343.           elseif tdifficulty == 2 then
  344.             AIWait = 0.6
  345.           elseif tdifficulty == 3 then
  346.             AIWait = 0.5
  347.           end
  348.           sleep()
  349.         end
  350.       elseif ev[1] == "terminate" then
  351.         term.setBackgroundColor(colors.black)
  352.         term.clear()
  353.         term.setCursorPos(1,1)
  354.         term.setTextColor(colors.white)
  355.         print("Aw, you quit the game early..")
  356.         print("Oh well, thanks for playing!")
  357.         os.pullEvent = oldPull
  358.         error()
  359.       end
  360.     end
  361.   end
  362. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement