Wired2coffee

v -1 RPG

Apr 17th, 2012
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.94 KB | None | 0 0
  1. function checkLines(file)
  2.     if fs.exists(file) == true then
  3.         sFile = fs.open(file, "r")
  4.         sCount = 0
  5.         for i = 1, math.huge do
  6.             line = sFile:readLine()
  7.             if line ~= nil then
  8.                 sCount = sCount+1
  9.             else
  10.                 return sCount
  11.             end
  12.         end
  13.         sFile:close()
  14.     else
  15.         return false
  16.     end
  17. end
  18. --POWER/HEALTH
  19. function loadHealth(health, maxHealth)
  20.     term.setCursorPos(1,1)
  21.     if health <= 0 then
  22.         initDeath()
  23.     end
  24.     io.write("H: {")
  25.     for i = 1, health/maxHealth*30 do
  26.         io.write("|")
  27.     end
  28.     term.setCursorPos(35,1)
  29.     io.write("}")
  30.     term.setCursorPos(37,1)
  31.     if health == math.floor(health) then
  32.         io.write(health..".")
  33.     else
  34.         io.write(health)
  35.     end
  36. end
  37. function loadPower(power, maxPower)
  38.     term.setCursorPos(1,2)
  39.     if power < 0 then
  40.         power = 0
  41.     end
  42.     io.write("P: {")
  43.     for i = 1, power/maxPower*30 do
  44.         io.write("|")
  45.     end
  46.     term.setCursorPos(35,2)
  47.     io.write("}")
  48.     term.setCursorPos(37,2)
  49.     if power == math.floor(power) then
  50.         io.write(power..".")
  51.     else
  52.         io.write(power)
  53.     end
  54. end
  55. --POWER/HEALTH
  56. --EXPERIENCE
  57. function loadEXP()
  58.     term.setCursorPos(1,18)
  59.     io.write("{")
  60.     for i = 1, experience/(level^2*50)*46 do
  61.         io.write("|")
  62.     end
  63.     term.setCursorPos(49,18)
  64.     io.write("}")
  65.     term.setCursorPos(24,17)
  66.     io.write(level)
  67. end
  68. --EXPERIENCE
  69. --SAVE/LOAD STUFF
  70. function Save()
  71.     if fs.exists(".WiredGameSave") == true then
  72.         fs.delete(".WiredGameSave")
  73.         file = io.open(".WiredGameSave","w")
  74.     else
  75.         file = io.open(".WiredGameSave","w")
  76.     end
  77.     file:write(tostring(health).."\n")
  78.     file:write(tostring(maxHealth).."\n")
  79.     file:write(tostring(power).."\n")
  80.     file:write(tostring(maxPower).."\n")
  81.     file:write(tostring(charPos[1]).."\n")
  82.     file:write(tostring(charPos[2]).."\n")
  83.     file:write(char.."\n")
  84.     file:write(tostring(healthRegen).."\n")
  85.     file:write(tostring(powerRegen).."\n")
  86.     file:write(tostring(level).."\n")
  87.     file:write(tostring(armor).."\n")
  88.     file:write(tostring(experience).."\n")
  89.     --MORE STUFF TO SAVE(positions and all that)
  90.     file:close()
  91. end
  92. function Load()
  93.     file = fs.open(".WiredGameSave","r")
  94.     health = tonumber(file:readLine())
  95.     maxHealth = tonumber(file:readLine())
  96.     power = tonumber(file:readLine())
  97.     maxPower = tonumber(file:readLine())
  98.     charPos[1] = tonumber(file:readLine())
  99.     charPos[2] = tonumber(file:readLine())
  100.     char = tostring(file:readLine())
  101.     healthRegen = tonumber(file:readLine())
  102.     powerRegen = tonumber(file:readLine())
  103.     level = tonumber(file:readLine())
  104.     armor = tonumber(file:readLine())
  105.     experience = tonumber(file:readLine())
  106.     --MORE STUFF TO LOAD(positions and all that)
  107.     file:close()
  108. end
  109. --SAVE/LOAD STUFF
  110. --DEATH
  111. function initDeath()
  112.     term.clear()
  113.     term.setCursorPos(1,1)
  114.     print("You died, please submit score.")
  115.     EXPERIENCEZZZ = 0
  116.     for i=1, level do
  117.         EXPERIENCEZZZ = EXPERIENCEZZZ+i^2*50
  118.     end
  119.     print("Experience: "..EXPERIENCEZZZ)
  120.     io.write("Name: ")
  121.     inp = read()
  122.     file = fs.open(".WiredGameScores","r")
  123.     scores = {}
  124.     linesZZZ = checkLines(".WiredGameScores")
  125.     for i = 1, linesZZZ do
  126.         table.insert(scores, tostring(file:readLine()))
  127.     end
  128.     file:close()
  129.     file = io.open(".WiredGameScores","w")
  130.     file:write(tostring(inp)..": "..EXPERIENCEZZZ.."\n")
  131.     for i = 1, linesZZZ do
  132.         file:write(tostring(scores[i]).."\n")
  133.     end
  134.     fs.delete(".WiredGameSave")
  135.     os.reboot()
  136. end
  137. --DEATH
  138. --SPELLS
  139. function castSpell(spell, currentTick)
  140.     if spell == "Fire" then
  141.         fireSpell = {true, currentTick, charPos[1], charPos[2]}
  142.     end
  143.     if spell == "Fireball" then
  144.         fireballSpell = {true, currentTick, charPos[1], charPos[2], char}
  145.     end
  146.     if spell == "Fire Walk" then
  147.         firewalkSpell = {true, currentTick, charPos[1], charPos[2]}
  148.     end
  149.     if spell == "Super Fire" then
  150.         superfireSpell = {true, currentTick, charPos[1], charPos[2]}
  151.     end
  152.     if spell == "Sprint" then
  153.         sprint = true
  154.     else
  155.         sprint = false
  156.     end
  157. end
  158. --SPELLS
  159. --INPUT
  160. function processInput()
  161.     while true do
  162.         a, b = os.pullEvent()
  163.         if a == "key" then
  164.             if stopProcesses == false then --Asks if game is paused
  165.                 if b == 17 then
  166.                     if 3 < charPos[2] then
  167.                         charPos[2] = charPos[2]-1
  168.                         char = "^"
  169.                     end
  170.                     if sprint == true then
  171.                         if 3 < charPos[2] then
  172.                             charPos[2] = charPos[2]-2
  173.                             char = "^"
  174.                         else
  175.                             charPos[2] = 3
  176.                         end
  177.                     end
  178.                 end
  179.                 if b == 30 then
  180.                     if charPos[1] > 1 then
  181.                         charPos[1] = charPos[1]-1
  182.                         char = "<"
  183.                     end
  184.                     if sprint == true then
  185.                         if charPos[1] > 1 then
  186.                             charPos[1] = charPos[1]-2
  187.                             char = "<"
  188.                         else
  189.                             charPos[1] = 1
  190.                         end
  191.                     end
  192.                 end
  193.                 if b == 32 then
  194.                     if charPos[1] < 49 then
  195.                         charPos[1] = charPos[1]+1
  196.                         char = ">"
  197.                     end
  198.                     if sprint == true then
  199.                         if charPos[1] < 49 then
  200.                             charPos[1] = charPos[1]+2
  201.                             char = ">" 
  202.                         else
  203.                             charPos[1] = 49
  204.                         end
  205.                     end
  206.                 end
  207.                 if b == 31 then
  208.                     if charPos[2] < 17 then
  209.                         charPos[2] = charPos[2]+1
  210.                         char = "v"
  211.                     end
  212.                     if sprint == true then
  213.                         if charPos[2] < 17 then
  214.                             charPos[2] = charPos[2]+2
  215.                             char = "v"
  216.                         else
  217.                             charPos[2] = 17
  218.                         end
  219.                     end
  220.                 end
  221.                 if b == 57 then
  222.                     castSpell(currentSpell, tick)
  223.                 end
  224.             end
  225.             if b == 2 then
  226.                 if level>=1 then
  227.                     currentSpell = "Fire"
  228.                 end
  229.             end
  230.             if b == 3 then
  231.                 if level>=10 then
  232.                     currentSpell = "Fireball"
  233.                 end
  234.             end
  235.             if b == 4 then
  236.                 if level>=20 then
  237.                     currentSpell = "Fire Walk"
  238.                 end        
  239.             end
  240.             if b == 5 then
  241.                 if level>=30 then
  242.                     currentSpell = "Super Fire"
  243.                 end
  244.             end
  245.             if b == 6 then
  246.                 if level>=40 then
  247.                     currentSpell = "Sprint"
  248.                 end
  249.             end
  250.             if b == 7 then
  251.                 if level>=50 then
  252.                     currentSpell = ""
  253.                 end
  254.             end
  255.             if b == 18 then
  256.                 if statsMenu == nil then
  257.                     statsMenu = true
  258.                 end
  259.                 if statsMenu == true then
  260.                     statsMenu = false
  261.                     stopProcesses = false
  262.                 else
  263.                     statsMenu = true
  264.                     stopProcesses = true
  265.                 end
  266.                 if statsMenu == true then
  267.                     if scoreMenu == true then
  268.                         scoreMenu = false
  269.                         statsMenu = true
  270.                     end
  271.                 end
  272.             end
  273.             if b == 25 then
  274.                 if scoreMenu == nil then
  275.                     scoreMenu = true
  276.                 end
  277.                 if scoreMenu == true then
  278.                     scoreMenu = false
  279.                     stopProcesses = false
  280.                 else
  281.                     scoreMenu = true
  282.                     stopProcesses = true
  283.                 end
  284.                 if statsMenu == true then
  285.                     if scoreMenu == true then
  286.                         scoreMenu = false
  287.                         statsMenu = true
  288.                     end
  289.                 end
  290.             end
  291.         end            
  292.     end
  293. end
  294. --INPUT
  295. --TICK
  296. function processTick()
  297.     while true do
  298.         --Tick System
  299.         if tick/20 == math.floor(tick/20) then
  300.             Save() --Saves every 1s automatically.
  301.         end
  302.         sleep(.05)
  303.         term.clear()
  304.         tick = tick+1
  305.         if statsMenu == true then
  306.             stopProcesses = true
  307.         else
  308.             stopProcesses = false
  309.         end
  310.         if scoreMenu == true then
  311.             stopProcesses = true
  312.         else
  313.             stopProcesses = false
  314.         end
  315.         --Tick System
  316.         --STATS
  317.         maxHealth = level*2
  318.         maxPower = math.floor(level*1.5)
  319.         healthRegen = maxHealth/3000
  320.         powerRegen = maxPower/1000
  321.         --STATS
  322.         --EXPBoxes
  323.         if stopProcesses == false then
  324.             ran = math.random(1000,1500)
  325.             ran2 = math.random(3,17)
  326.             ran3 = math.random(1,49)
  327.             ran4 = math.random(1,50)
  328.             ran5 = math.random(1,5)
  329.             if tick/ran == math.floor(tick/ran) then
  330.                 table.insert(box, ran2)
  331.                 table.insert(box2, ran3)
  332.                 table.insert(box3, ran4)
  333.                 if ran5 == 5 then
  334.                     table.insert(box4, 1)
  335.                 elseif ran5 == 4 then
  336.                     table.insert(box4, 2)
  337.                 elseif ran5 == 3 then
  338.                     table.insert(box4, 3)
  339.                 end
  340.             end
  341.             for i = 1, #box do
  342.                 term.setCursorPos(box2[i], box[i])
  343.                 if box4[i] == 2 or box4[i] == 3 then
  344.                     io.write("O")
  345.                 else
  346.                     io.write("o")
  347.                     if box4[i] == 1 then
  348.                         box3[i] = 250
  349.                     end
  350.                 end
  351.             end
  352.             for i = 1, #box do
  353.                 if charPos[1] == box2[i] then
  354.                     if charPos[2] == box[i] then
  355.                         term.setCursorPos(charPos[1]-1,charPos[2]-2)
  356.                         io.write(tostring(box3[i]))
  357.                         sleep(.08)
  358.                         box[i] = -99
  359.                         box2[i] = -99
  360.                         experience = experience+box3[i]
  361.                         box3[i] = -99
  362.                         if box4[i] == 1 then
  363.                             armor = armor+box4[i]
  364.                         elseif box4[i] == 2 then
  365.                             if armor>1 then
  366.                                 armor = armor-1
  367.                             else
  368.                                 health = health-1
  369.                             end
  370.                         end
  371.                     end
  372.                 end
  373.             end
  374.         end
  375.         --EXPBoxes
  376.         --Power Regeneration
  377.         if statsMenu == false then --Asks if game is paused.
  378.             if health ~= maxHealth then
  379.                 health = health+healthRegen
  380.             end
  381.             if power ~= maxPower then
  382.                 power = power+powerRegen
  383.              end
  384.             if maxPower <= power then
  385.                 power = maxPower
  386.             end
  387.             if maxHealth <= health then
  388.                 health = maxHealth
  389.             end
  390.             term.setCursorPos(37, 1)
  391.             io.write("0.00000000000")
  392.             term.setCursorPos(37, 2)
  393.             io.write("0.00000000000")
  394.             loadHealth(health, maxHealth)
  395.             loadPower(power, maxPower)
  396.         end
  397.         --Power Regeneration
  398.         --Character
  399.         if statsMenu == false then --Asks if game is paused.
  400.             term.setCursorPos(charPos[1], charPos[2])
  401.             io.write(char)
  402.             if lastCharPos ~= charPos then
  403.                 term.setCursorPos(lastCharPos[1],lastCharPos[2])
  404.                 io.write(" ")
  405.                 term.setCursorPos(charPos[1], charPos[2])
  406.                 io.write(char)
  407.             end
  408.             lastCharPos[1] = charPos[1]
  409.             lastCharPos[2] = charPos[2]
  410.         end
  411.         --Character
  412.         --DING
  413.         if statsMenu == false then
  414.             loadEXP()
  415.             if experience >= level^2*50 then
  416.                 level = level+1
  417.                 experience = experience-(level-1)^2*50
  418.             end
  419.             if level == 100 then
  420.                 experience = 0
  421.             end
  422.         end
  423.         --DING!
  424.         --Stats Screen
  425.         if scoreMenu == false then
  426.             if statsMenu == true then
  427.                 stopProcesses = true
  428.                 tick = tick-1
  429.                 term.clear()
  430.                 term.setCursorPos(1,1)
  431.                 print("                  -GAME PAUSED-")
  432.                 term.setCursorPos(1,4)
  433.                 print("Health: "..health.."/"..maxHealth)
  434.                 print("Health Regen: "..healthRegen*20 .." per second \n")
  435.                 print("Power: "..power.."/"..maxPower)
  436.                 print("Power Regen: "..powerRegen*20 .." per second \n")
  437.                 print("Armor: "..armor.."/"..maxArmor.."\n")
  438.                 print("Level: "..level)
  439.                 print("Experience: "..experience.."/"..tostring(level^2*50).."\n")
  440.                 io.write("Selected Spell: ")
  441.                 if currentSpell == "" or currentSpell == nil then
  442.                     io.write("None")
  443.                 else
  444.                     io.write(currentSpell)
  445.                 end
  446.                 sleep(.05)
  447.             else
  448.                 stopProcesses = false
  449.             end
  450.         end
  451.         if statsMenu == true then
  452.             if scoreMenu == true then
  453.                 scoreMenu = false
  454.                 statsMenu = true
  455.             end
  456.         end
  457.         --Stats Screen
  458.         --Score Screen
  459.         if statsMenu == false then
  460.             if scoreMenu == true then
  461.                 stopProcesses = true
  462.                 term.clear()
  463.                 tick = tick-1
  464.                 file = fs.open(".WiredGameScores","r")
  465.                 term.setCursorPos(21,1)
  466.                 io.write("-SCORES-\n")
  467.                 for i = 1,14 do
  468.                     term.setCursorPos(1,i+3)
  469.                     print(file:readLine())
  470.                 end
  471.                 sleep(.05)
  472.             else
  473.                 stopProcesses = false
  474.                 file:close()
  475.             end
  476.         end
  477.         --Score Screen
  478.         --ARMOR
  479.         if armor >= maxArmor then
  480.             armor = maxArmor
  481.         end
  482.         if armor <= 0 then
  483.             armor = 0
  484.         end
  485.         --ARMOR
  486.         --Spells
  487.         if statsMenu == false and scoreMenu == false then
  488.             if fireSpell[1] == true then
  489.                 currentTick = fireSpell[2]
  490.                 if power>0 then
  491.                     if tick < currentTick+10 then
  492.                     charPos[1] = fireSpell[3]
  493.                         charPos[2] = fireSpell[4]
  494.                         term.setCursorPos(charPos[1]+1,charPos[2])
  495.                         io.write("#")
  496.                         term.setCursorPos(charPos[1]-1,charPos[2])
  497.                         io.write("#")
  498.                         term.setCursorPos(charPos[1],charPos[2]+1)
  499.                         io.write("#")
  500.                         term.setCursorPos(charPos[1],charPos[2]-1)
  501.                         io.write("#")
  502.                         power = power-.01
  503.                     end
  504.                 end
  505.             end
  506.             if fireballSpell[1] == true then
  507.                 currentTick = fireSpell[2]
  508.                 if power>0 then
  509.                     vchar = fireballSpell[5]
  510.                     if vchar == "^" then
  511.                         if fireballSpell[4]-offsetY < 3 then
  512.                             fireballSpell[1] = false
  513.                             offsetY = 1
  514.                         end
  515.                         term.setCursorPos(fireballSpell[3], fireballSpell[4]-offsetY)
  516.                         io.write("|")
  517.                         if tick/1 == math.floor(tick/1) then
  518.                             offsetY = offsetY+1
  519.                             power = power-(1/9)
  520.                         end
  521.                     end
  522.                     if vchar == "v" then
  523.                         if fireballSpell[4]+offsetY > 17 then
  524.                             fireballSpell[1] = false
  525.                             offsetY = 1
  526.                         end
  527.                         term.setCursorPos(fireballSpell[3], fireballSpell[4]+offsetY)
  528.                         io.write("|")
  529.                         if tick/1 == math.floor(tick/1) then
  530.                             offsetY = offsetY+1
  531.                             power = power-(1/9)
  532.                         end
  533.                     end
  534.                     if vchar == ">" then
  535.                         if fireballSpell[3]+offsetX > 48 then
  536.                             fireballSpell[1] = false
  537.                             offsetX = 1
  538.                         end
  539.                         term.setCursorPos(fireballSpell[3]+offsetX, fireballSpell[4])
  540.                         io.write("-")
  541.                         if tick/1 == math.floor(tick/1) then
  542.                             offsetX = offsetX+1
  543.                             power = power-(1/9)
  544.                         end
  545.                     end
  546.                     if vchar == "<" then
  547.                         if fireballSpell[3]-offsetX < 1 then
  548.                             fireballSpell[1] = false
  549.                             offsetX = 1
  550.                         end
  551.                         term.setCursorPos(fireballSpell[3]-offsetX, fireballSpell[4])
  552.                         io.write("-")
  553.                         if tick/1 == math.floor(tick/1) then
  554.                             offsetX = offsetX+1
  555.                             power = power-(1/9)
  556.                         end
  557.                     end
  558.                 end
  559.             end
  560.             if firewalkSpell[1] == true then
  561.                 currentTick = firewalkSpell[2]
  562.                 if power>0 then
  563.                     if tick < currentTick+10 then
  564.                         term.setCursorPos(charPos[1]+1,charPos[2])
  565.                         io.write("&")
  566.                         term.setCursorPos(charPos[1]-1,charPos[2])
  567.                         io.write("&")
  568.                         term.setCursorPos(charPos[1],charPos[2]+1)
  569.                         io.write("&")
  570.                         term.setCursorPos(charPos[1],charPos[2]-1)
  571.                         io.write("&")
  572.                         power = power-.015
  573.                     end
  574.                 end
  575.             end
  576.             if superfireSpell[1] == true then
  577.                 currentTick = superfireSpell[2]
  578.                 if power>0 then
  579.                     if tick < currentTick+10 then
  580.                         term.setCursorPos(charPos[1]-1,charPos[2]+1)
  581.                         io.write("&")
  582.                         term.setCursorPos(charPos[1],charPos[2]+1)
  583.                         io.write("&")
  584.                         term.setCursorPos(charPos[1]+1,charPos[2]+1)
  585.                         io.write("&")
  586.                         term.setCursorPos(charPos[1]-1,charPos[2])
  587.                         io.write("&")
  588.                         term.setCursorPos(charPos[1]-2,charPos[2])
  589.                         io.write("&")
  590.                         term.setCursorPos(charPos[1]+1,charPos[2])
  591.                         io.write("&")
  592.                         term.setCursorPos(charPos[1]+2,charPos[2])
  593.                         io.write("&")
  594.                         term.setCursorPos(charPos[1],charPos[2]+2)
  595.                         io.write("&")
  596.                         term.setCursorPos(charPos[1]-1,charPos[2]-1)
  597.                         io.write("&")
  598.                         term.setCursorPos(charPos[1]+1,charPos[2]-1)
  599.                         io.write("&")
  600.                         term.setCursorPos(charPos[1],charPos[2]-1)
  601.                         io.write("&")
  602.                         term.setCursorPos(charPos[1],charPos[2]-2)
  603.                         io.write("&")
  604.                         power = power-.25
  605.                     end
  606.                 end
  607.             end
  608.         end
  609.         --Spells
  610.     end
  611. end
  612. --TICK
  613. --MAIN
  614. function MAIN()
  615.     charPos = {}
  616.     if lastCharPos == nil then
  617.         lastCharPos = {50, 50}
  618.     end
  619.     if fs.exists(".WiredGameScores") ~= true then
  620.         file = io.open(".WiredGameScores","w")
  621.         file:write(" ")
  622.         file:close()
  623.     end
  624.     if fs.exists(".WiredGameSave") ~= true then
  625.         file = io.open(".WiredGameSave","w")
  626.         file:write(tostring(3).."\n") --health
  627.         file:write(tostring(3).."\n") --maxHealth
  628.         file:write(tostring(3).."\n") --power
  629.         file:write(tostring(3).."\n") --MaxPower
  630.         file:write(tostring(1).."\n") --character xPos
  631.         file:write(tostring(3).."\n") --character yPos
  632.         file:write(tostring(">").."\n") --character looks
  633.         file:write(tostring(.00099999).."\n") --healthRegen
  634.         file:write(tostring(.005).."\n") --powerRegen
  635.         file:write(tostring(1).."\n") --level
  636.         file:write(tostring(0).."\n") --armor
  637.         file:write(tostring(0).."\n") --experience
  638.         file:write(tostring(0).."\n") --game time
  639.         --MORE STUFF TO SAVE(positions and all that)
  640.         file:close()
  641.     end
  642.     Load()
  643.     if tick == nil then
  644.         tick = 0
  645.     end
  646.     term.clear()
  647.     maxArmor = level*10
  648.     fireSpell = {}
  649.     superfireSpell = {}
  650.     fireballSpell = {}
  651.     firewalkSpell = {}
  652.     currentSpell = "Fire"
  653.     offsetX = 1
  654.     offsetY = 1
  655.     maxHealth = level*2
  656.     maxPower = math.floor(level*1.5)
  657.     healthRegen = maxHealth/3000
  658.     powerRegen = maxPower/1000
  659.     box = {}
  660.     box2 = {}
  661.     box3 = {}
  662.     box4 = {}
  663.     stopProcesses = false
  664.     statsMenu = false
  665.     scoreMenu = false
  666.     parallel.waitForAny(processTick, processInput)
  667. end
  668. --MAIN
  669.  
  670.  
  671.  
  672.  
  673.  
  674. MAIN()
Advertisement
Add Comment
Please, Sign In to add comment