libraryaddict

Untitled

Apr 25th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.51 KB | None | 0 0
  1. --Lets make a game.. No plagurizing or sharing this to any site other then Computercraft.info
  2. local Version = 0.1
  3. local Player = {"<", ">", "^", "V"}
  4. local Laser = {}
  5. local Lasers = {"-", "-", "i", "!"}
  6. local Pulsing = {"o", "O", "0", "O"}
  7. local PulseExplodes = {}
  8. local Pulse = {}
  9. local Cooldown = 0
  10. local CooldownPulse = 0
  11. local Map = {}
  12. local MapView = {}
  13. local Health = 10
  14. local Lives = 3
  15. local Score = 10
  16. local Name = ""
  17. function clear()
  18.   term.clear()
  19.   term.setCursorPos(1,1)
  20. end
  21.  
  22. function LoadFile()
  23.   repeat
  24.     clear()
  25.     term.setCursorPos(1,2)
  26.     print(fs.list("Maps"))
  27.     term.setCursorPos(1,1)
  28.     write("A map file: ")
  29.     Name = read()
  30.     if not fs.exists("Maps/"..Name) then
  31.       print("That file doesnt exist")
  32.       sleep(3)
  33.     end
  34.   until fs.exists("Maps/"..Name)
  35.   clear()
  36.   File = io.open("Maps/"..Name, "r")
  37.   for line in File:lines() do
  38.     Map[#Map+1] = line.."\n"
  39.   end
  40.   PlayerCords = Map[1]
  41.   PlayerDir = tonumber(string.sub(PlayerCords, 5, 5))
  42.   PlayerX = tonumber(string.sub(PlayerCords, 1, 2))
  43.   PlayerY = tonumber(string.sub(PlayerCords, 3, 4))
  44.   table.remove(Map, 1)
  45.   File:close()
  46.   p = io.open("Debug", "w")
  47.   for n=1,#Map do
  48.     p:write(Map[n])
  49.   end
  50.   for n=1,#Map do
  51.     MapView[n] = Map[n]
  52.   end
  53.   p:close()
  54.   StatusBox()
  55.   reDraw()
  56. end
  57.  
  58. function Load()
  59.   Name = ""
  60.   LoadFile()
  61. end
  62.  
  63. function Tick()
  64.   if Cooldown > 0 then
  65.     Cooldown = Cooldown-1
  66.   end
  67.   if CooldownPulse > 0 then
  68.     CooldownPulse = CooldownPulse-1
  69.   end
  70.   PulseMove()
  71.   LaserMove()
  72.   CheckUnder()
  73.   UpdateExplode()
  74.   reDraw()
  75.   Ticker = os.startTimer(0.1)
  76. end
  77.  
  78. function CheckUnder() -- Check under the player
  79.   if CheckMapView(PlayerX, PlayerY) ~= " " then
  80.     if CheckMapView(PlayerX, PlayerY) == "x" then --Pickup pack!
  81.       StatusBox("Health", 5)
  82.       deleteMapView(PlayerX, PlayerY)
  83.     elseif CheckMapView(PlayerX, PlayerY) == "o" then -- Hit by explosion!
  84.       StatusBox("Health", -3)
  85.       deleteMapView(PlayerX, PlayerY)
  86.     elseif CheckMapView(PlayerX, PlayerY) == "i" or CheckMapView(PlayerX, PlayerY) == "!" or CheckMapView(PlayerX, PlayerY) == "-" then --Laser!
  87.       StatusBox("Health", -5)
  88.       deleteMapView(PlayerX, PlayerY)
  89.       for n=1,#Laser do
  90.         if Laser[n] then
  91.           local LaserX = tonumber(string.sub(Laser[n], 1, 2))
  92.           local LaserY = tonumber(string.sub(Laser[n], 3, 4))
  93.           if LaserX == PlayerX and LaserY == PlayerY then
  94.             table.remove(Laser, n)
  95.             n = n-1
  96.           end
  97.         end
  98.       end
  99.     elseif checkPulse() then --pulse!
  100.       StatusBox("Health", -10)
  101.       deleteMapView(PlayerX, PlayerY)
  102.       PulseExplode(checkPulse())
  103.       for n=1,#Pulse do
  104.         if Pulse[n] then
  105.           local PulseX = tonumber(string.sub(Pulse[n], 1, 2))
  106.           local PulseY = tonumber(string.sub(Pulse[n], 3, 4))
  107.           if PulseX == PlayerX and PulseY == PlayerY then
  108.             table.remove(Pulse, n)
  109.             n = n-1
  110.           end
  111.         end
  112.       end
  113.     end
  114.   end
  115. end
  116.  
  117. function checkPulse()
  118.   for n=1,#Pulse do
  119.     if Pulse[n] then
  120.       local PulseX = tonumber(string.sub(Pulse[n], 1, 2))
  121.       local PulseY = tonumber(string.sub(Pulse[n], 3, 4))
  122.       if PulseX == PlayerX and PulseY == PlayerY then
  123.         return PulseX, PulseY
  124.       end
  125.     end
  126.   end
  127.   return false
  128. end
  129.  
  130. function StatusBox(Type, Modifier)
  131.   FlashHP = false
  132.   FlashLives = false
  133.   if Type == "Health" then
  134.     Health = tonumber(Health)+tonumber(Modifier)
  135.     FlashHP = true
  136.     if Health <= 0 then
  137.       if Lives > 1 then
  138.         Lives = Lives-1
  139.         Health = 10
  140.         FlashLives = true
  141.       else
  142.         clear()
  143.         print("You died")
  144.         error()
  145.       end
  146.     end
  147.   end
  148.   DrawStatus()
  149. end
  150.  
  151. function DrawStatus()
  152.   for n=1,3 do
  153.     MapView[n] = string.sub(MapView[n], 14, string.len(MapView[4]))
  154.   end
  155.   local ScoreNum = tonumber("4")-string.len(Score)
  156.   local Spaces = string.rep(" ", ScoreNum)
  157.   MapView[1] = "Score: "..Score..Spaces.." O"..MapView[1]
  158.   if FlashLives == true then
  159.     MapView[2] = "Lives:      O"..MapView[2]
  160.   else
  161.     MapView[2] = "Lives: "..Lives.."    O"..MapView[2]
  162.   end
  163.   local HealthNum = tonumber("4")-string.len(Health)
  164.   local Spaces = string.rep(" ", HealthNum)
  165.   if FlashHP == true then
  166.     MapView[3] = "Health:     O"..MapView[3]
  167.   else
  168.     MapView[3] = "Health: "..Health..Spaces.."O"..MapView[3]
  169.   end
  170.   MapView[4] = string.sub(MapView[4], 14, string.len(MapView[4]))
  171.   MapView[4] = "OOOOOOOOOOOOO"..MapView[4]
  172.   Draw = os.startTimer(0.15)
  173. end
  174.  
  175. function CreateLaser(x, y, dir)
  176.   if CheckMapView(x, y) ~= "O" then
  177.     Laser[#Laser+1] = Num(x)..Num(y)..dir
  178.     EditMapView(x, y, Lasers[dir])
  179.   end
  180.   Cooldown = 5
  181. end
  182.  
  183. function CreatePulse(x, y, dir)
  184.   if CheckMapView(x, y) ~= "O" then
  185.     Pulse[#Pulse+1] = Num(x)..Num(y)..dir.."1".."1"
  186.     EditMapView(x, y, Pulsing[dir])
  187.   end
  188.   CooldownPulse = 30
  189. end
  190.  
  191. function Num(num)
  192.   if string.len(num) == 1 then
  193.     return "0"..num
  194.   else
  195.     return num
  196.   end
  197. end
  198.  
  199. function LaserMove()
  200.   for n=1,#Laser do
  201.     if Laser[n] then
  202.       local LaserX = tonumber(string.sub(Laser[n], 1, 2))
  203.       local LaserY = tonumber(string.sub(Laser[n], 3, 4))
  204.       local LaserDir = tonumber(string.sub(Laser[n], 5, 5))
  205.       deleteMapView(LaserX, LaserY)
  206.       if LaserDir == 1 then
  207.         LaserX = LaserX-1
  208.       elseif LaserDir == 2 then
  209.         LaserX = LaserX+1
  210.       elseif LaserDir == 3 then
  211.         LaserY = LaserY-1
  212.       elseif LaserDir == 4 then
  213.         LaserY = LaserY+1
  214.       else
  215.         error("LaserDir errored")
  216.       end
  217.       if CheckMapView(LaserX, LaserY) == " " then
  218.         EditMapView(LaserX, LaserY, Lasers[LaserDir])
  219.         Laser[n] = Num(LaserX)..Num(LaserY)..LaserDir
  220.       else
  221.         table.remove(Laser, n)
  222.         n = n-1
  223.       end
  224.     end
  225.   end
  226. end
  227.  
  228. function PulseMove()
  229.   for n=1,#Pulse do
  230.     if Pulse[n] then
  231.       local PulseX = tonumber(string.sub(Pulse[n], 1, 2))
  232.       local PulseY = tonumber(string.sub(Pulse[n], 3, 4))
  233.       local PulseDir = tonumber(string.sub(Pulse[n], 5, 5))
  234.       local PulseState = tonumber(string.sub(Pulse[n], 6, 6))
  235.       local Speed = tonumber(string.sub(Pulse[n], 7, 7))
  236.       deleteMapView(PulseX, PulseY)
  237.       local Sparex = PulseX
  238.       local Sparey = PulseY
  239.       if Speed == 1 then
  240.         if PulseDir == 1 then
  241.           PulseX = PulseX-1
  242.         elseif PulseDir == 2 then
  243.           PulseX = PulseX+1
  244.         elseif PulseDir == 3 then
  245.           PulseY = PulseY-1
  246.         elseif PulseDir == 4 then
  247.           PulseY = PulseY+1
  248.         else
  249.           error("LaserDir errored")
  250.         end
  251.         Speed = 0
  252.       else
  253.         Speed = 1
  254.       end
  255.       if PulseState == 4 then
  256.         PulseState = 1
  257.       else
  258.         PulseState = PulseState+1
  259.       end
  260.       if CheckMap(PulseX, PulseY) == " " then
  261.         EditMapView(PulseX, PulseY, Pulsing[PulseState])
  262.         Pulse[n] = Num(PulseX)..Num(PulseY)..PulseDir..PulseState..Speed
  263.       else
  264.         PulseExplode(Sparex, Sparey)
  265.         table.remove(Pulse, n)
  266.         n = n-1
  267.       end
  268.     end
  269.   end
  270. end
  271.  
  272. function PulseExplode(x, y, Spread)
  273.   if not Spread then Spread = "3" end
  274.   local Nope = 5
  275.   for n=1,#PulseExplodes do
  276.     if PulseExplodes[n] then
  277.       local PulseX = tonumber(string.sub(PulseExplodes[n], 1, 2))
  278.       local PulseY = tonumber(string.sub(PulseExplodes[n], 3, 4))
  279.       if PulseX == x and PulseY == y then -- Matches another explosion. Dont make it! Abort abort!
  280.         Nope = 4
  281.       end
  282.     end
  283.   end
  284.   if Nope == 5 then -- Nothing is here.. right?
  285.     if CheckMapView(x, y) ~= "O" then
  286.       PulseExplodes[#PulseExplodes+1] = Num(x)..Num(y).."3"..Spread
  287.       EditMapView(x, y, "o")
  288.     end
  289.   end
  290. end
  291. fs.delete("Log")
  292. function Logger(loggy)
  293.   p = io.open("Log", "a")
  294.   p:write(loggy.."\n")
  295.   p:close()
  296. end
  297.  
  298. function UpdateExplode()
  299.   for n=1,#PulseExplodes do
  300.     if PulseExplodes[n] then
  301.       local PulseX = tonumber(string.sub(PulseExplodes[n], 1, 2))
  302.       local PulseY = tonumber(string.sub(PulseExplodes[n], 3, 4))
  303.       local PulseStatus = tonumber(string.sub(PulseExplodes[n], 5, 5))
  304.       local Spread = tonumber(string.sub(PulseExplodes[n], 6, 6))
  305.       if PulseStatus < 1 then -- How long it has left in the world
  306.         deleteMapView(PulseX, PulseY)
  307.         table.remove(PulseExplodes, n)
  308.       elseif Spread ~= 0 then
  309.         PulseExplode(PulseX+1, PulseY, Spread-1)
  310.         PulseExplode(PulseX-1, PulseY, Spread-1)
  311.         PulseExplode(PulseX, PulseY+1, Spread-1)
  312.         PulseExplode(PulseX, PulseY-1, Spread-1)
  313.       end
  314.       if PulseExplodes[n] then
  315.         if PulseStatus ~= 0 then
  316.         PulseExplodes[n] = Num(PulseX)..Num(PulseY)..(PulseStatus-1)..(Spread-1)
  317.       end
  318.       end
  319.     end
  320.   end
  321. end
  322.  
  323. function deleteMapView(x,y)
  324.   local First = string.sub(MapView[y], 1, x-1)
  325.   local Second = string.sub(MapView[y], x+1, string.len(MapView[y]))
  326.   MapView[y] = First.." "..Second
  327. end
  328.  
  329. function deleteMap(x,y)
  330.   local First = string.sub(MapView[y], 1, x-1)
  331.   local Second = string.sub(MapView[y], x+1, string.len(MapView[y]))
  332.   MapView[y] = First.." "..Second
  333. end
  334.  
  335. function Direction(xory, dir)
  336.   if xory == "x" then
  337.     if dir == 1 then --Left
  338.       return -1
  339.     elseif dir == 2 then --Right
  340.       return 1
  341.     elseif dir == 3 or dir == 4 then
  342.       return 0
  343.     else
  344.       error("funtion Direction was handled incorrectly")
  345.     end
  346.   elseif xory == "y" then
  347.     if dir == 3 then --Up
  348.       return -1
  349.     elseif dir == 4 then --Down
  350.       return 1
  351.     elseif dir == 1 or dir == 2 then
  352.       return 0
  353.     else
  354.       error("funtion Direction was handled incorrectly")
  355.     end
  356.   else
  357.     error("You need to use x or y!!!! in function direction!")
  358.   end
  359. end
  360.  
  361. function reDraw()
  362.   clear()
  363.   for n=1,#MapView do
  364.     term.setCursorPos(1, n)
  365.     term.write(MapView[n])
  366.   end
  367.   term.setCursorPos(PlayerX, PlayerY)
  368.   term.write(Player[PlayerDir])
  369. end
  370.  
  371. function CheckMap(x, y)
  372.   if Map[y] then
  373.     local MapString = Map[y]
  374.     MapString = string.sub(MapString, x, x)
  375.     return MapString
  376.   else
  377.     return "O"
  378.   end
  379. end
  380.  
  381. function CheckMapView(x, y)
  382.   if MapView[y] then
  383.     local Lens = string.len(MapView[y])
  384.     if x > 0 and x < Lens then
  385.       local MapString = MapView[y]
  386.       MapString = string.sub(MapString, x, x)
  387.       return MapString
  388.     else
  389.       return "O"
  390.     end
  391.   end
  392.   return "O"
  393. end
  394.  
  395. function EditMapView(x, y, Get)
  396.   local First = string.sub(MapView[y], 1, x-1)
  397.   local Second = string.sub(MapView[y], x+1, string.len(MapView[y]))
  398.   MapView[y] = First..Get..Second
  399. end
  400.  
  401. function EditMap(x, y, Get)
  402.   local First = string.sub(Map[y], 1, x-1)
  403.   local Second = string.sub(Map[y], x+1, string.len(Map[y]))
  404.   Map[y] = First..Get..Second
  405. end
  406.  
  407. function MovePlayer()
  408.   Tick()
  409.   while true do
  410.     Events = {os.pullEvent()}
  411.     if Events[1] == "key" then
  412.       if Events[2] == 200 or Events[2] == 17 then -- Up
  413.         PlayerDir = 3
  414.         if CheckMapView(PlayerX, PlayerY+Direction("y", PlayerDir)) ~= "O" then
  415.           PlayerY = PlayerY+Direction("y", PlayerDir)
  416.           CheckUnder()
  417.         end
  418.         reDraw()
  419.       elseif Events[2] == 208 or Events[2] == 31 then -- Down
  420.         PlayerDir = 4
  421.         if CheckMapView(PlayerX, PlayerY+Direction("y", PlayerDir)) ~= "O" then
  422.           PlayerY = PlayerY+Direction("y", PlayerDir)
  423.           CheckUnder()
  424.         end
  425.         reDraw()
  426.       elseif Events[2] == 203 or Events[2] == 30 then -- Left
  427.         PlayerDir = 1
  428.         if CheckMapView(PlayerX+Direction("x", PlayerDir), PlayerY) ~= "O" then
  429.           PlayerX = PlayerX+Direction("x", PlayerDir)
  430.           CheckUnder()
  431.         end
  432.         reDraw()
  433.       elseif Events[2] == 205 or Events[2] == 32 then -- Right
  434.         PlayerDir = 2
  435.         if CheckMapView(PlayerX+Direction("x", PlayerDir), PlayerY) ~= "O" then
  436.           PlayerX = PlayerX+Direction("x", PlayerDir)
  437.           CheckUnder()
  438.         end
  439.         reDraw()
  440.       elseif Events[2] == 57 then --Spacebar
  441.         if Cooldown == 0 then
  442.           CreateLaser(PlayerX+Direction("x", PlayerDir), PlayerY+Direction("y", PlayerDir), PlayerDir)
  443.         end
  444.       elseif Events[2] == 45 then -- x button
  445.         if CooldownPulse == 0 then
  446.           CreatePulse(PlayerX+Direction("x", PlayerDir), PlayerY+Direction("y", PlayerDir), PlayerDir)
  447.         end
  448.       end
  449.     elseif Events[1] == "timer" and Events[2] == Ticker then --Tick
  450.       Tick()
  451.     elseif Events[1] == "timer" and Events[2] == Draw then
  452.       StatusBox("Nothing", "Ignore")
  453.     end
  454.   end
  455. end
  456. Load()
  457. MovePlayer()
Advertisement
Add Comment
Please, Sign In to add comment