libraryaddict

Untitled

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