Advertisement
billysback

Shipper

Nov 11th, 2012
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 32.83 KB | None | 0 0
  1. os.loadAPI("art")
  2.  
  3. if type(art) ~= "table" then
  4.     print("Installing ART API")
  5.     shell.run("pastebin get ERf5QAkK art")
  6. end
  7.  
  8. local timer
  9.  
  10. local tick = 0
  11.  
  12. local interval = 0.2
  13.  
  14. local background
  15. local function genBackground()
  16.  
  17.     local maxx, maxy = term.getSize()
  18.  
  19.     local cols = {colors.white, colors.yellow, colors.gray, colors.black, colors.orange}
  20.  
  21.     local pixels = {}
  22.     for i = 1, math.random(30, 110) do
  23.         pixels[#pixels + 1] = art.setPixel(math.random(maxx), math.random(maxy), nil, cols[math.random(#cols)], "*")
  24.     end
  25.     local pixarr = art.createPixelArray(pixels)
  26.    
  27.     return pixarr
  28. end
  29.  
  30. local function getBackground()
  31.     local maxx, maxy = term.getSize()
  32.    
  33.     if background == nil then background = genBackground() end
  34.     for i=1,#background.pixs do
  35.         local pixel = background.pixs[i]
  36.         local x = pixel:getX()
  37.         local y = pixel:getY()+1
  38.         while y > maxy do
  39.             y = y - maxy
  40.         end
  41.         pixel:setX(x)
  42.         pixel:setY(y)
  43.     end
  44.    
  45.     return background
  46. end
  47.  
  48. local sc = 0
  49.  
  50. local function drawBackground()
  51.     local maxx, maxy = term.getSize()
  52.    
  53.     term.clear()
  54.     term.setCursorPos(1,1)
  55.     local back = getBackground()
  56.     back:draw()
  57. end
  58.  
  59. local bon = true
  60. local function doBackground(pixarr)
  61.     local btim = os.startTimer(0)
  62.    
  63.     background = nil
  64.    
  65.     for i=1,#pixarr.pixs do
  66.         local pixel = pixarr.pixs[i]
  67.         pixel:setX(pixel:getX() + 19)
  68.         pixel:setY(pixel:getY() + 6)
  69.     end
  70.    
  71.     local bint = 0.025
  72.     while bon do
  73.         local event, p1 = os.pullEvent()
  74.         if event == "timer" and p1 == btim then
  75.             btim = os.startTimer(bint)
  76.             drawBackground()
  77.             pixarr:draw()
  78.         elseif event == "key" then
  79.             local key = p1
  80.             if key == 28 then
  81.                 bon = false
  82.             end
  83.         end
  84.     end
  85.    
  86.     for i=1,#pixarr.pixs do
  87.         local pixel = pixarr.pixs[i]
  88.         pixel:setX(pixel:getX() - 19)
  89.         pixel:setY(pixel:getY() - 6)
  90.     end
  91.     bon = true
  92.    
  93.     timer = os.startTimer(interval)
  94.    
  95.    
  96.     return true
  97. end
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. local function split(str, pat)
  110.     local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  111.     if str ~= nil then
  112.        local fpat = "(.-)" .. pat
  113.        local last_end = 1
  114.        local s, e, cap = str:find(fpat, 1)
  115.        while s do
  116.           if s ~= 1 or cap ~= "" then
  117.          table.insert(t,cap)
  118.           end
  119.           last_end = e+1
  120.           s, e, cap = str:find(fpat, last_end)
  121.        end
  122.        if last_end <= #str then
  123.           cap = str:sub(last_end)
  124.           table.insert(t, cap)
  125.        end
  126.     else
  127.         print("##SHIPPER ERROR failed to split ["..str.."] by:"..pat)
  128.     end
  129.     return t
  130. end
  131.  
  132. -- 1: = repair (1)
  133. -- 2: = repair (all)
  134. -- 3: = upgrade armour (1)
  135. -- 4: = upgrade damage (1)
  136. -- 5: = upgrade accuracy (1)
  137. -- 6: = upgrade speed (1)
  138. -- 7: = upgrade wep-speed (1)
  139. -- 8: = upgrade health (1)
  140. -- 9: = updrade evasivness (1)
  141. -- 10: = buy crew (1)
  142. local maxSel = 10
  143.  
  144. --function to get the choice, price and pixelarray based off the current selection
  145. --sel = selection
  146. --shp = ship being used by the shop
  147. local function getShop(sel, shp)
  148.     local choices = {}
  149.     choices = {
  150.                  {"|=============|",
  151.                   "| REPAIR 1 (1)|",
  152.                   "|=============|"},
  153.                  {"|============|",
  154.                   "| REPAIR ALL |",
  155.                   "|============|"},
  156.                  {"|====================|",
  157.                   "|UPGRADE ARMOUR (175)|",
  158.                   "|====================|"},
  159.                  {"|====================|",
  160.                   "|UPGRADE DAMAGE (200)|",
  161.                   "|====================|"},
  162.                  {"|======================|",
  163.                   "|UPGRADE ACCURACY (375)|",
  164.                   "|======================|"},
  165.                  {"|===================|",
  166.                   "|UPGRADE SPEED (225)|",
  167.                   "|===================|"},
  168.                  {"|=======================|",
  169.                   "|UPGRADE WEP-SPEED (300)|",
  170.                   "|=======================|"},
  171.                  {"|====================|",
  172.                   "|UPGRADE HEALTH (175)|",
  173.                   "|====================|"},
  174.                  {"|=========================|",
  175.                   "|UPGRADE EVASIVENESS (375)|",
  176.                   "|=========================|"},
  177.                  {"|==============|",
  178.                   "|BUY CREW (300)|",
  179.                   "|==============|"}
  180.               }
  181.     prices = {
  182.                 1,
  183.                 shp.stats.maxhealth - shp.stats.health,
  184.                 175,
  185.                 200,
  186.                 375,
  187.                 225,
  188.                 300,
  189.                 175,
  190.                 375,
  191.                 300
  192.              }
  193.    
  194.     local pixels = {}
  195.     local choice = choices[sel]
  196.     local price = prices[sel]
  197.     for y=1,#choice do
  198.         local l = choice[y]
  199.         for x=1,string.len(l) do
  200.             local ch = string.sub(l, x, x)
  201.             pixels[#pixels + 1] = art.setPixel(x+1, y+1, nil, colors.blue, ch)
  202.         end
  203.     end
  204.     local pixarr = art.createPixelArray(pixels)
  205.     return choice, price, pixarr
  206. end
  207.  
  208. --boolean to determine if the game is on
  209. local on = true
  210.  
  211. --increases as the game progresses, makes the game harder but the loot better
  212. local progress = 1
  213. --the current location of your ship.
  214. local loc = {1, 1}
  215. --the destination of your ship
  216. local dest = nil
  217. --the distance between your destination and you
  218. local dist = 0
  219. --the tile that you are traveling to
  220. local target = "+"
  221.  
  222. --the amount of ship blueprints there are
  223. local ship_types = 5
  224. --the colors that the blueprint builder uses
  225. local ship_colors = {colors.white, colors.red, colors.lime, colors.orange}
  226.  
  227. --the map height & width
  228. local mwidth = 10
  229. local mheight = 11
  230.  
  231. --returns the symbol found at x, y
  232. local function getTarget(x, y)
  233.     local blup = {}
  234.     blup = {"+-------------------+",
  235.             "|+++++++++++@+++++++|",
  236.             "|++@+++++++++++++@++|",
  237.             "|+++++++@+++++++++++|",
  238.             "|+@+++++++++++@+++++|",
  239.             "|+++++++++@+++++++++|",
  240.             "|++++@++++++++++++++|",
  241.             "|++++++++++@+++++@++|",
  242.             "|+@+++++++++++++++++|",
  243.             "|++++++@++++++++@+++|",
  244.             "+-------------------+"}
  245.     return string.sub(blup[y], x, x)
  246. end
  247.  
  248. --returns a pixelarray containing the spacemap
  249. local function getSpaceMap()
  250.     local blup = {}
  251.     blup = {"+-------------------+",
  252.             "|+++++++++++@+++++++|",
  253.             "|++@+++++++++++++@++|",
  254.             "|+++++++@+++++++++++|",
  255.             "|+@+++++++++++@+++++|",
  256.             "|+++++++++@+++++++++|",
  257.             "|++++@++++++++++++++|",
  258.             "|++++++++++@+++++@++|",
  259.             "|+@+++++++++++++++++|",
  260.             "|++++++@++++++++@+++|",
  261.             "+-------------------+"}
  262.    
  263.     mwidth = string.len(blup[1])
  264.     mheight = #blup
  265.            
  266.     local pixels = {}
  267.     for y=1,#blup do
  268.         local l = blup[y]
  269.         for x=1,string.len(l) do
  270.             local ch = string.sub(l, x, x)
  271.             local color = colors.blue
  272.             if ch == "#" then color = colors.blue
  273.             elseif ch == "+" then color = colors.white
  274.             elseif ch == "@" then color = colors.red end
  275.             pixels[#pixels + 1] = art.setPixel(x, y, nil, color, ch)
  276.         end
  277.     end
  278.     local smap = art.createPixelArray(pixels)
  279.     return smap
  280. end
  281.  
  282. --creates the appropriate ship at the x, y coordinates with those colors based off the given type
  283. local function createShip(x, y, colors, type)
  284.     local cur = colors[1]
  285.     local blup = {}
  286.     local weapons = {}
  287.     local stats = {}
  288.     if type == 1 then
  289.         blup = {"    _    ",
  290.                 "   /3^1\\   ",
  291.                 "  2_1|4=1|2_1 ",
  292.                 " /4=1@3+1@4=1\\ ",
  293.                 "  \\@@@/  ",
  294.                 "   |4:1|  ",
  295.                 "   \\_/",
  296.                 "    2V"}
  297.         weapons = {{3+x, 3+y}, {7+x, 3+y}}
  298.         stats = {
  299.             ["armour"] = 60,
  300.             ["damage"] = {80, 100},
  301.             ["wepspeed"] = 3.5,
  302.             ["speed"] = 10,
  303.             ["accuracy"] = 60,
  304.             ["evasivness"] = 15,
  305.             ["health"] = 700,
  306.             ["maxhealth"] = 700,
  307.             ["type"] = type
  308.         }
  309.     elseif type == 2 then
  310.         blup = {"",
  311.                 "  /3^1\\   ",
  312.                 " 2_1|@|2_1 ",
  313.                 "|4=1@3+1@4=1|",
  314.                 " \\_@_/ ",
  315.                 "   2V1    "}
  316.         weapons = {{2+x, 2+y}, {6+x, 2+y}}
  317.         stats = {
  318.             ["armour"] = 45,
  319.             ["damage"] = {55, 75},
  320.             ["wepspeed"] = 2.5,
  321.             ["speed"] = 15,
  322.             ["accuracy"] = 50,
  323.             ["evasivness"] = 25,
  324.             ["health"] = 575,
  325.             ["maxhealth"] = 575,
  326.             ["type"] = type
  327.         }
  328.     elseif type == 3 then
  329.         blup = {"        ",
  330.                 "  /2^1\\",
  331.                 " 2_1|3:1|2_1",
  332.                 "|4=1@_@4=1|",
  333.                 "|3:1| |3:1|",
  334.                 " 2V1   2V1"}
  335.         --weapons = {{4+x, 1+y}, {2+x, 3+y}, {6+x, 3+y}}
  336.         weapons = {{4+x, 1+y}}
  337.         stats = {
  338.             ["armour"] = 35,
  339.             ["damage"] = {50, 65},
  340.             ["wepspeed"] = 1.5,
  341.             ["speed"] = 30,
  342.             ["accuracy"] = 40,
  343.             ["evasivness"] = 35,
  344.             ["health"] = 500,
  345.             ["maxhealth"] = 500,
  346.             ["type"] = type
  347.         }
  348.     elseif type == 4 then
  349.         blup = {"",
  350.                 "    2_1     ",
  351.                 "   /3^1\\    ",
  352.                 "   |=|   ",
  353.                 "  /4:2+4:1\\  ",
  354.                 " /2_1|4=1|2_1\\ ",
  355.                 "    2V1     "}
  356.         weapons = {{5+x, 2+y}}
  357.         stats = {
  358.             ["armour"] = 50,
  359.             ["damage"] = {45, 60},
  360.             ["wepspeed"] = 2,
  361.             ["speed"] = 25,
  362.             ["accuracy"] = 45,
  363.             ["evasivness"] = 35,
  364.             ["health"] = 500,
  365.             ["maxhealth"] = 500,
  366.             ["type"] = type
  367.         }
  368.     elseif type == 5 then
  369.         blup = {"        ",
  370.                 "   /3^1\\   ",
  371.                 "   |4:1|   ",
  372.                 " 2_1/4=1 4=1\\2_1 ",
  373.                 " \\_ 3+1 _/ ",
  374.                 "   \\2_1/  ",
  375.                 "    2V1    "}
  376.         weapons = {{2+x, 5+y}, {8+x, 5+y}}
  377.         stats = {
  378.             ["armour"] = 35,
  379.             ["damage"] = {90, 110},
  380.             ["wepspeed"] = 4,
  381.             ["speed"] = 15,
  382.             ["accuracy"] = 70,
  383.             ["evasivness"] = 10,
  384.             ["health"] = 600,
  385.             ["maxhealth"] = 600,
  386.             ["type"] = type
  387.         }
  388.     end
  389.     local pixels = {}
  390.     for i=1,#blup do
  391.         local l = blup[i]
  392.         local off = 0
  393.         for j=1,string.len(l) do
  394.             local ch = string.sub(l, j, j)
  395.             if ch ~= " " and ch ~= "" and ch ~= nil then
  396.                 local n = tonumber(ch)
  397.                 if n ~= nil then
  398.                     cur = colors[n]
  399.                     off = off + 1
  400.                 else
  401.                     ch = string.gsub(ch, "@", " ")
  402.                     local pixel = art.setPixel(x+j-1-off, y+i-1, nil, cur, ch)
  403.                     pixels[#pixels + 1] = pixel
  404.                 end
  405.             end
  406.         end
  407.     end
  408.     local parr = art.createPixelArray(pixels)
  409.     return parr, weapons, stats
  410. end
  411.  
  412. local mtype = 1
  413. local curship, rrw, rrs = createShip(2, 2, ship_colors, mtype)
  414.  
  415. local data = {
  416.     ["name"] = "Your",
  417.     ["crew"] = 5,
  418.     ["money"] = 500,
  419.     ["stats"] = rrs,
  420.     ["dmg_boost"] = 0,
  421.     ["armour_boost"] = 0,
  422. }
  423.  
  424. --writes the stats at the screen dependant on x, y and the gap is the gap imbetween the double lines information
  425. function writeStats(sx, sy, gap)
  426.     term.setCursorPos(sx, sy)
  427.     term.write("HP: "..data.stats.health)
  428.     term.setCursorPos(sx+gap, sy)
  429.     term.write("Crew: "..data.crew)
  430.     term.setCursorPos(sx, sy+1)
  431.     term.write("Money: "..data.money)
  432.     term.setCursorPos(sx+gap, sy+1)
  433.     term.write("SPD: "..data.stats.speed)
  434.     term.setCursorPos(sx, sy+2)
  435.     term.write("ACC: "..data.stats.accuracy)
  436.     term.setCursorPos(sx+gap, sy+2)
  437.     term.write("WEP_SPD: "..data.stats.wepspeed)
  438.     term.setCursorPos(sx, sy+3)
  439.     term.write("EVS: "..data.stats.evasivness)
  440.     term.setCursorPos(sx+gap, sy+3)
  441.     term.write("ARM: "..data.stats.armour)
  442.     term.setCursorPos(sx, sy+4)
  443.     term.write("DMG: "..data.stats.damage[1].." TO "..data.stats.damage[2])
  444.     term.setCursorPos(sx, sy+5)
  445.     term.write("TARGET: "..target)
  446.     if dest ~= nil then
  447.         term.setCursorPos(sx, sy+6)
  448.         term.write("DEST: "..dest[1]..","..dest[2])
  449.         term.setCursorPos(sx+gap, sy+5)
  450.         term.write("DIST: "..dist)
  451.     end
  452.    
  453.    
  454. end
  455.  
  456. --sets the current type of ship selected to a valid one
  457. local function checkType()
  458.     if mtype < 1 then mtype = ship_types end
  459.     if mtype > ship_types then mtype = 1 end
  460. end
  461.  
  462. local con = true
  463.  
  464. local ctim = os.startTimer(0)
  465.  
  466. local interv = 0.5
  467. --ship selection loop
  468. while con do
  469.     local event, p1 = os.pullEvent()
  470.     if event == "timer" and p1 == ctim then
  471.         term.clear()
  472.         term.setCursorPos(1, 1)
  473.         print("Choose your ship:")
  474.         curship:draw()
  475.         writeStats(2, 13, 15)
  476.         ctim = os.startTimer(interv)
  477.     elseif event == "key" then
  478.         local key = p1
  479.         if key == 203 then --left
  480.             mtype = mtype - 1
  481.             checkType()
  482.             local cursh, rw, rs = createShip(2, 2, ship_colors, mtype)
  483.             rrs = rs
  484.             rrw = rw
  485.             curship = cursh
  486.             data = {
  487.                             ["name"] = "Your",
  488.                             ["crew"] = 5,
  489.                             ["money"] = 500,
  490.                             ["stats"] = rs,
  491.                             ["dmg_boost"] = 0,
  492.                             ["armour_boost"] = 0,
  493.                         }
  494.         elseif key == 205 then --right
  495.             mtype = mtype + 1
  496.             checkType()
  497.             local cursh, rw, rs = createShip(2, 2, ship_colors, mtype)
  498.             rrs = rs
  499.             rrw = rw
  500.             curship = cursh
  501.             data = {
  502.                             ["name"] = "Your",
  503.                             ["crew"] = 5,
  504.                             ["money"] = 500,
  505.                             ["stats"] = rs,
  506.                             ["dmg_boost"] = 0,
  507.                             ["armour_boost"] = 0,
  508.                         }
  509.         elseif key == 28 then
  510.             con = false
  511.         elseif key == 29 then
  512.             on = false
  513.             con = false
  514.         end
  515.     end
  516. end
  517.  
  518.  
  519. local ship, weapons, stats = createShip(2, 2, ship_colors, mtype)
  520.  
  521. data = {
  522.     ["name"] = "Your",
  523.     ["crew"] = 5,
  524.     ["money"] = 500,
  525.     ["stats"] = stats,
  526.     ["dmg_boost"] = 0,
  527.     ["armour_boost"] = 0,
  528. }
  529.  
  530. local wep_array = nil
  531. --returns a pixel array of the ships weapons.
  532. local function getFire(weparray)
  533.     local pixels = {}
  534.     for i=1,#weparray do
  535.         local weapon = weparray[i]
  536.         local x = weapon[1]-1
  537.         local y = weapon[2]-1
  538.         while y >= 1 do
  539.             pixel = art.setPixel(x, y, nil, colors.red, "|")
  540.             pixels[#pixels + 1] = pixel
  541.             y = y-1
  542.         end
  543.     end
  544.     local pixarr = art.createPixelArray(pixels)
  545.     return pixarr
  546. end
  547.  
  548. --used to improve the stats of an enemy ship
  549. local function improveStats(csts)
  550.     local sts = csts
  551.     local amt = ((progress-5) * 2)
  552.     if amt > 0 then
  553.         amt = amt + math.random(progress)
  554.         for i=1,amt do
  555.             local type = math.random(7)
  556.             if type == 1 then sts.armour = sts.armour + 1 end
  557.             if type == 2 then
  558.                 sts.damage[1] = sts.damage[1] + 1
  559.                 sts.damage[2] = sts.damage[2] + 1
  560.             end
  561.             if type == 3 then
  562.                 if sts.wepspeed > 0.1 then
  563.                     sts.wepspeed = sts.wepspeed - 0.1
  564.                 end
  565.             end
  566.             if type == 4 then
  567.                 sts.speed = sts.speed + 1
  568.             end
  569.             if type == 5 then
  570.                 if sts.accuracy < 90 then
  571.                     sts.accuracy = sts.accuracy + 1
  572.                 end
  573.             end
  574.             if type == 6 then
  575.                 if sts.evasivness < 90 then
  576.                     sts.evasivness = sts.evasivness
  577.                 end
  578.             end
  579.             if type == 7 then
  580.                 sts.health = sts.health + 1
  581.                 sts.maxhealth = sts.maxhealth + 1
  582.             end
  583.         end
  584.     end
  585.     return sts
  586. end
  587.  
  588.  
  589. --fighting ship data
  590. local f_weps = nil
  591. local f_ship = nil
  592. local fighting = nil
  593. local isfighting = false
  594.  
  595. local canfire = true
  596. local e_canfire = true
  597.  
  598. local canrepair = true
  599. local e_canrepair = true
  600.  
  601. local cancharge = true
  602. local e_cancharge = true
  603.  
  604. local e_wdraw = false
  605.  
  606. --weapon timer
  607. local wtimer
  608. local e_wtimer
  609.  
  610. --canfire timers
  611. local cftimer
  612. local e_cftimer
  613.  
  614. --repair timers
  615. local rtimer
  616. local e_rtimer
  617.  
  618. --charge timers
  619. local ctimer
  620. local e_ctimer
  621.  
  622. local msg = "Game started"
  623.  
  624. --shoots from ship "from" to ship "to"
  625. local function shoot(from, to)
  626.     if math.random(100) >= to.stats.evasivness then
  627.         if math.random(100) <= from.stats.accuracy then
  628.             local dmg = math.random(from.stats.damage[1], from.stats.damage[2]) + from.dmg_boost
  629.             from.dmg_boost = 0
  630.             local armour = to.stats.armour + to.armour_boost
  631.             to.armour_boost = to.armour_boost - 1
  632.             fdmg = dmg - ((dmg / 100) * armour)
  633.             to.stats.health = to.stats.health - dmg
  634.             msg = from.name.." ship hit "..to.name.." ship for "..dmg.. " damage!"
  635.         end
  636.     end
  637. end
  638.  
  639. --repairs "from"
  640. local function repair(from, to)
  641.     from.armour_boost = from.armour_boost + 1
  642. end
  643.  
  644. --charge "from"
  645. local function charge(from, to)
  646.     from.dmg_boost = from.dmg_boost + 1
  647. end
  648.  
  649. local function fleeFight(from, to)
  650.     shoot(from, to)
  651.     msg = from.name.." ship is tring to flee from the fight!"
  652.     local n = 20
  653.     while math.random(60) > from.stats.speed and on and n >= 0 do
  654.         shoot(to, from)
  655.         checkFight()
  656.        
  657.         if from.stats.health > 0 then
  658.             msg = from.name.." ship was hit while fleeing!"
  659.         end
  660.     end
  661.     checkFight()
  662.     if from.stats.health > 0 then
  663.         msg = from.name.." ship fled the fight without dying!"
  664.         isfighting = false
  665.     else
  666.         isfighting = false
  667.     end
  668. end
  669.  
  670. local function enemyMove(from, to)
  671.     local move = math.random(3)
  672.     if from.stats.health < 100 and math.random(100) < 20 then move = 4 end
  673.     if move == 1 then --fire
  674.         if e_canfire then
  675.             e_wdraw = true
  676.             e_wtimer = os.startTimer(from.stats.wepspeed)
  677.        
  678.             shoot(from, to)
  679.             e_canfire = false
  680.             e_cftimer = os.startTimer(from.stats.wepspeed)
  681.         end
  682.     elseif move == 2 then --repair
  683.         if e_canrepair then
  684.             repair(from, to)
  685.            
  686.             e_canrepair = false
  687.             e_rtimer = os.startTimer(((100-fighting.crew) / 10)*2)
  688.         end
  689.     elseif move == 3 then --charge
  690.         if e_cancharge then
  691.                 charge(from, to)
  692.                
  693.                 e_cancharge = false
  694.                 e_ctimer = os.startTimer(((100-fighting.crew) / 10)*4)
  695.         end
  696.     elseif move == 4 then --flee
  697.         fleeFight(from, to)
  698.     end
  699. end
  700.  
  701. function checkFight()
  702.     if fighting ~= nil and isfighting then
  703.         if data.stats.health <= 0 then
  704.             on = false
  705.             term.clear()
  706.             term.setCursorPos(1,1)
  707.             print(" --- GAME OVER --- ")
  708.             sleep(2)
  709.         elseif fighting.stats.health <= 0 then
  710.             isfighting = false
  711.             local loot = {math.random(fighting.money), math.random(fighting.crew)-1}
  712.             msg = fighting.name.." ship was shot down, looted: £"..loot[1].." and found "..loot[2].." crew-members!"
  713.             data.money = data.money + loot[1]
  714.             data.crew = data.crew + loot[2]
  715.             if data.crew > 99 then data.crew = 99 end
  716.         end
  717.     end
  718. end
  719.  
  720. local function fire(bypass)
  721.     if (isfighting or bypass == true) and fighting ~= nil then
  722.         if wep_array == nil then
  723.             wep_array = getFire(weapons)
  724.             if canfire then
  725.                 weparray:draw()
  726.             end
  727.             wtimer = os.startTimer(2)
  728.         else
  729.             if canfire then
  730.                 wep_array:draw()
  731.             end
  732.             wtimer = os.startTimer(2)
  733.         end
  734.         if canfire then
  735.        
  736.             shoot(data, fighting)
  737.        
  738.             canfire = false
  739.             cftimer = os.startTimer(data.stats.wepspeed)
  740.         end
  741.     end
  742. end
  743.  
  744. local function printFightStats()
  745.     term.setCursorPos(2, 13)
  746.     term.write("HP: "..data.stats.health)
  747.     term.setCursorPos(13, 13)
  748.     term.write("HP: "..fighting.stats.health)
  749.     term.setCursorPos(2, 13)
  750.     term.write("DMG: "..data.dmg_boost)
  751.     term.setCursorPos(13, 13)
  752.     term.write("DMG: "..fighting.dmg_boost)
  753.     term.setCursorPos(2, 13)
  754.     term.write("ARM: "..data.armour_boost)
  755.     term.setCursorPos(13, 13)
  756.     term.write("ARM: "..fighting.armour_boost)
  757. end
  758.  
  759. function save(dir)
  760.     local finfo = nil
  761.     if isfighting then
  762.         finfo = {"isfight=true",
  763.                  "fship=money~"..fighting.money..":crew~"..fighting.crew..":name~"..fighting.name..":dmgb~"..fighting.dmg_boost..":armb~"..fighting.armour_boost..":stats~hp,"..fighting.stats.health.."~arm,"..fighting.stats.armour.."~dmg,"..fighting.stats.damage[1]..";"..fighting.stats.damage[2].."~wepspd,"..fighting.stats.wepspeed.."~spd,"..fighting.stats.speed.."~acc,"..fighting.stats.accuracy.."~evs,"..fighting.stats.evasivness.."~type,"..fighting.stats.type.."~maxhp,"..fighting.stats.maxhealth,
  764.                  }
  765.     end
  766.     sinfo = {"ship=money~"..data.money..":crew~"..data.crew..":name~"..data.name..":dmgb~"..data.dmg_boost..":armb~"..data.armour_boost..":stats~hp,"..data.stats.health.."~arm,"..data.stats.armour.."~dmg,"..data.stats.damage[1]..";"..data.stats.damage[2].."~wepspd,"..data.stats.wepspeed.."~spd,"..data.stats.speed.."~acc,"..data.stats.accuracy.."~evs,"..data.stats.evasivness.."~type,"..data.stats.type.."~maxhp,"..data.stats.maxhealth,
  767.              }
  768.     local data = {"prog="..progress, "dist="..dist, "loc="..loc[1]..","..loc[2], "target="..target}
  769.     if dest ~= nil then
  770.         data[#data + 1] = "dest="..dest[1]..","..dest[2]
  771.     end
  772.     for i=1,#sinfo do data[#data + 1] = sinfo[i] end
  773.     if finfo ~= nil then
  774.         for i=1,#finfo do data[#data + 1] = finfo[i] end
  775.     end
  776.    
  777.     if fs.exists(dir) then fs.delete(dir) end
  778.     local file = fs.open(dir, "a")
  779.     for i=1,#data do
  780.         file.writeLine(data[i])
  781.     end
  782.     file.close()
  783. end
  784.  
  785. function loadGame(dir)
  786.     local lns = {}
  787.     if fs.exists(dir) then
  788.         local file = fs.open(dir, "r")
  789.         local line = file.readLine()
  790.         while line ~= nil do
  791.             lns[#lns + 1] = line
  792.             line = file.readLine()
  793.         end
  794.         file.close()
  795.     end
  796.     local isf = false
  797.     local shp = nil
  798.     local fshp = nil
  799.     local prog = 1
  800.     local lc = {1, 1}
  801.     local dis = 0
  802.     local des = nil
  803.     term.clear()
  804.     term.setCursorPos(1,1)
  805.     for i=1,#lns do
  806.         local splt1 = split(lns[i], "=")
  807.         if splt1[1] == "isfight" then
  808.             if splt1[2] == "true" then isfighting = true end
  809.             print("READ ISFIGHT")
  810.         elseif splt1[1] == "prog" then
  811.             prog = tonumber(splt1[2])
  812.         elseif splt1[1] == "dist" then
  813.             dis = tonumber(splt1[2])
  814.         elseif splt1[1] == "loc" then
  815.             local lsplt = split(splt1[2], ",")
  816.             lc = {tonumber(lsplt[1]), tonumber(lsplt[2])}
  817.         elseif splt1[1] == "dest" then
  818.             local lsplt = split(splt1[2], ",")
  819.             des = {tonumber(lsplt[1]), tonumber(lsplt[2])}
  820.         elseif splt1[1] == "target" then
  821.             target = splt1[2]
  822.         elseif splt1[1] == "fship" or splt1[1] == "ship" then
  823.             print("LOADING SHIP")
  824.             local dat = {}
  825.             local splt2 = split(splt1[2], ":")
  826.             for j=1,#splt2 do
  827.                 local part = splt2[j]
  828.                 local splt = split(part, "~")
  829.                 if splt[1] == "money" then
  830.                     print("LOADING MONEY")
  831.                     dat["money"] = tonumber(splt[2])
  832.                 elseif splt[1] == "crew" then
  833.                     dat["crew"] = tonumber(splt[2])
  834.                 elseif splt[1] == "name" then
  835.                     dat["name"] = splt[2]
  836.                 elseif splt[1] == "dmgb" then
  837.                     dat["dmg_boost"] = tonumber(splt[2])
  838.                 elseif splt[1] == "armb" then
  839.                     dat["armour_boost"] = tonumber(splt[2])
  840.                 elseif splt[1] == "stats" then
  841.                     print("LOADING STATS")
  842.                     local stat = {}
  843.                     for i=2,#splt do
  844.                         local spl = split(splt[i], ",")
  845.                         if spl[1] == "hp" then
  846.                             stat["health"] = tonumber(spl[2])
  847.                         elseif spl[1] == "arm" then
  848.                             stat["armour"] = tonumber(spl[2])
  849.                         elseif spl[1] == "dmg" then
  850.                             local spll = split(spl[2], ";")
  851.                             local dmg = {tonumber(spll[1]), tonumber(spll[2])}
  852.                             stat["damage"] = dmg
  853.                         elseif spl[1] == "wepspd" then
  854.                             stat["wepspeed"] = tonumber(spl[2])
  855.                         elseif spl[1] == "spd" then
  856.                             stat["speed"] = tonumber(spl[2])
  857.                         elseif spl[1] == "acc" then
  858.                             stat["accuracy"] = tonumber(spl[2])
  859.                         elseif spl[1] == "evs" then
  860.                             stat["evasivness"] = tonumber(spl[2])
  861.                         elseif spl[1] == "maxhp" then
  862.                             stat["maxhealth"] = tonumber(spl[2])
  863.                         elseif spl[1] == "type" then
  864.                             stat["type"] = tonumber(spl[2])
  865.                         end
  866.                     end
  867.                     print(stat.health.."!")
  868.                     dat["stats"] = stat
  869.                     print(dat.stats.health.."!")
  870.                 end
  871.             end
  872.             print(dat.stats.health.."!")
  873.             --dat["stats"] = {
  874.             --  ["health"] = stat.health,
  875.             --  ["armour"] = stat.armour,
  876.             --  ["damage"] = stat.damage,
  877.             --  ["wepspeed"] = stat.wepspeed,
  878.             --  ["speed"] = stat.speed,
  879.             --  ["accuracy"] = stat.accuracy,
  880.             --  ["evasivness"] = stat.evasivness,
  881.             --  ["type"] = stat.type
  882.             --}
  883.             if splt1[1] == "fship" then
  884.                 fshp = dat
  885.             elseif splt1[1] == "ship" then
  886.                 shp = dat
  887.             end
  888.         end
  889.     end
  890.     print(shp.stats.health.."!")
  891.     return isf, fshp, shp, prog, lc, des, dis
  892. end
  893.  
  894.  
  895.  
  896. wep_array = getFire(weapons)
  897.  
  898. local sel = 1
  899. -- 1: = repair (1)
  900. -- 2: = repair (all)
  901. -- 3: = upgrade armour (1)
  902. -- 4: = upgrade damage (1)
  903. -- 5: = upgrade accuracy (1)
  904. -- 6: = upgrade speed (1)
  905. -- 7: = upgrade wep-speed (1)
  906. -- 8: = upgrade health (1)
  907. -- 9: = updrade evasivness (1)
  908.  
  909. local function doShop(selc)
  910.     local choice, price, pixarr = getShop(selc, data)
  911.     if data.money - price < 0 then
  912.         msg = "Not enough money!"
  913.     else
  914.         data.money = data.money - price
  915.         if selc == 1 then
  916.             if data.stats.health + 1 > data.stats.maxhealth then
  917.                 data.money = data.money + price
  918.                 msg = "Already max health"
  919.             else
  920.                 data.stats.health = data.stats.health + 1
  921.             end
  922.         elseif selc == 2 then
  923.             data.stats.health = data.stats.maxhealth
  924.         elseif selc == 3 then
  925.             if data.stats.armour >= 90 then
  926.                 data.money = data.money + price
  927.                 msg = "Already max armour"
  928.             else
  929.                 data.stats.armour = data.stats.armour + 1
  930.             end
  931.         elseif selc == 4 then
  932.             data.stats.damage[1] = data.stats.damage[1] + 1
  933.             data.stats.damage[2] = data.stats.damage[2] + 1
  934.         elseif selc == 5 then
  935.             if data.stats.accuracy >= 90 then
  936.                 data.money = data.money + price
  937.                 msg = "Already max accuracy"
  938.             else
  939.                 data.stats.accuracy = data.stats.accuracy + 1
  940.             end
  941.         elseif selc == 6 then data.stats.speed = data.stats.speed + 1
  942.         elseif selc == 7 then
  943.         if data.stats.wepspeed <= 0.1 then
  944.             data.money = data.money + price
  945.             msg = "Already max wepspeed"
  946.         else
  947.             data.stats.wepspeed = data.stats.wepspeed - 0.1
  948.         end
  949.         elseif selc == 8 then
  950.             data.stats.health = data.stats.health + 1
  951.             data.stats.maxhealth = data.stats.maxhealth + 1
  952.         elseif selc == 9 then
  953.             if data.stats.evasivness >= 90 then
  954.                 data.money = data.money + price
  955.                 msg = "Already max evasivness"
  956.             else
  957.                 data.stats.evasivness = data.stats.evasivness + 1
  958.             end
  959.         elseif selc == 10 then
  960.             if data.stats.crew >= 99 then
  961.                 data.money = data.money + price
  962.                 msg = "Already max crew"
  963.             else
  964.                 data.stats.crew = data.stats.crew + 1
  965.             end
  966.         end
  967.         msg = "Spent £"..price.."!"
  968.         progress = progress + 1
  969.     end
  970. end
  971. local function updateSelection()
  972.     if sel < 1 then sel = maxSel end
  973.     if sel > maxSel then sel = 1 end
  974. end
  975.  
  976. local cshop = nil
  977.  
  978. local pause = false
  979. local iswriting = false
  980. local isloading = false
  981. local ismap = false
  982. local isshop = false
  983. local curdir = ""
  984.  
  985. local playerl = {2, 2}
  986. local oplayerl = nil
  987. local function checkPlayer()
  988.     if playerl[1] < 2 then playerl[1] = 2 end
  989.     if playerl[1] > mwidth - 1 then playerl[1] = mwidth - 1 end
  990.     if playerl[2] < 2 then playerl[2] = 2 end
  991.     if playerl[2] > mheight - 1 then playerl[2] = mheight - 1 end
  992. end
  993.  
  994. local smap = nil
  995.  
  996. local firing = true
  997.  
  998. timer = os.startTimer(0)
  999. while on do
  1000.     local event, p1, p2 = os.pullEvent()
  1001.     if event == "timer" and p1 == timer then
  1002.    
  1003.         term.scroll(-1)
  1004.         term.clear()
  1005.         term.setCursorPos(1,1)
  1006.         timer = os.startTimer(interval)
  1007.         if ismap then
  1008.             if smap == nil then
  1009.                 smap = getSpaceMap()
  1010.             end
  1011.             smap:draw()
  1012.            
  1013.             local pixel1 = art.createPixel(oplayerl[1], oplayerl[2], nil, colors.blue, "^")
  1014.             local pixel2 = art.createPixel(playerl[1], playerl[2], nil, colors.yellow, "=")
  1015.             pixel1:draw()
  1016.             pixel2:draw()
  1017.         else
  1018.             if isshop then
  1019.                 if cshop == nil then
  1020.                     local choice, price, pixs = getShop(sel, data)
  1021.                     cshop = pixs
  1022.                 end
  1023.                 cshop:draw()
  1024.                 term.setTextColor(colors.red)
  1025.                 writeStats(2, 8, 15)
  1026.                 term.setCursorPos(2, 6)
  1027.                 term.write(msg)
  1028.             else
  1029.                 ship:draw()
  1030.                 if firing then fire() end
  1031.                 if iswriting then
  1032.                     local tx, ty = term.getSize()
  1033.                     term.setCursorPos(1, ty)
  1034.                     term.write("Enter save directory: "..curdir)
  1035.                 end
  1036.                 if isloading then
  1037.                     local tx, ty = term.getSize()
  1038.                     term.setCursorPos(1, ty)
  1039.                     term.write("Enter load directory: "..curdir)
  1040.                 end
  1041.                 if isfighting == false then
  1042.                     if not pause then
  1043.                         tick = tick + 1
  1044.                         if math.random(100) <= 2 and tick >= 7 then
  1045.                             tick = 0
  1046.                             isfighting = true
  1047.                             local f_stats = {}
  1048.                             f_ship, f_weps, f_stats = createShip(14, 2, ship_colors, math.random(ship_types))
  1049.                             f_stats = improveStats(f_stats)
  1050.                             f_weps = getFire(f_weps)
  1051.                             local cre = progress+9
  1052.                             if cre > 99 then cre = 99 end
  1053.                             fighting = {
  1054.                                 ["name"] = "Enemy",
  1055.                                 ["crew"] = math.random(3, cre),
  1056.                                 ["money"] = math.random(5*progress, 250*progress),
  1057.                                 ["stats"] = f_stats,
  1058.                                 ["dmg_boost"] = 0,
  1059.                                 ["armour_boost"] = 0,
  1060.                             }
  1061.                             local m_f_h = ((fighting.crew*100) + fighting.money) * 3
  1062.                             m_f_h = m_f_h / 200
  1063.                             local chance = fighting.money + fighting.crew
  1064.                             while chance > 100 do
  1065.                                 chance = chance / math.random(2, 30)
  1066.                             end
  1067.                             if chance < 10 then change = 10 end
  1068.                             if math.random(100) < chance then
  1069.                                 fighting.stats.health = fighting.stats.health - m_f_h
  1070.                             end
  1071.                             msg = "Started fighting with "..fighting.name.." ship!"
  1072.                         end
  1073.                     end
  1074.                     writeStats(2, 13, 15)
  1075.                     if dest ~= nil and not iswriting and not isloading and not pause then
  1076.                         dist = dist - data.stats.speed
  1077.                     end
  1078.                     if dist <= 0 and dest ~= nil then
  1079.                         loc = dest
  1080.                         dest = nil
  1081.                     end
  1082.                 else
  1083.                         checkFight()
  1084.                         if e_wdraw then
  1085.                             f_weps:draw()
  1086.                         end
  1087.                         f_ship:draw()
  1088.                         if not pause then
  1089.                             enemyMove(fighting, data)
  1090.                         end
  1091.                         term.setCursorPos(2, 13)
  1092.                         term.write("HP: "..data.stats.health)
  1093.                         term.setCursorPos(13, 13)
  1094.                         term.write("HP: "..fighting.stats.health)
  1095.                    
  1096.                 end
  1097.                 if pause then
  1098.                     term.setCursorPos(1, 1)
  1099.                     term.write("-GAME PAUSED-")
  1100.                 end
  1101.             term.setCursorPos(2, 11)
  1102.             term.write(msg)
  1103.             end
  1104.         end
  1105.     elseif event == "timer" and p1 == wtimer then
  1106.         fire()
  1107.         firing = false
  1108.     elseif event == "timer" and p1 == cftimer then
  1109.         canfire = true
  1110.     elseif event == "timer" and p1 == rtimer then
  1111.         canrepair = true
  1112.     elseif event == "timer" and p1 == ctimer then
  1113.         cancharge = true
  1114.     elseif event == "timer" and p1 == e_cftimer then
  1115.         e_canfire = true
  1116.     elseif event == "timer" and p1 == e_rtimer then
  1117.         e_canrepair = true
  1118.     elseif event == "timer" and p1 == e_ctimer then
  1119.         e_cancharge = true
  1120.     elseif event == "timer" and p1 == e_wtimer then
  1121.         e_wdraw = false
  1122.     elseif event == "char" and (iswriting or isloading or pause) then
  1123.         local ch = p1
  1124.         curdir = curdir..ch
  1125.     elseif event == "key" then
  1126.         local key = p1
  1127.        
  1128.         if iswriting or isloading or pause or dest ~= nil then else
  1129.             if key == 41 then
  1130.                 doBackground(ship)
  1131.             end
  1132.             if key == 50 then
  1133.                 if ismap then ismap = false
  1134.                 elseif not isfighting then
  1135.                     oplayerl = {playerl[1], playerl[2]}
  1136.                     ismap = true
  1137.                 end
  1138.             end
  1139.             if key == 16 then
  1140.                 if isshop then isshop = false
  1141.                 elseif not isfighting and target == "@" then
  1142.                     isshop = true
  1143.                 end
  1144.             end
  1145.         end
  1146.         if ismap then
  1147.             if key == 200 then --up
  1148.                 playerl[2] = playerl[2] - 1
  1149.             elseif key == 203 then --left
  1150.                 playerl[1] = playerl[1] - 1
  1151.             elseif key == 208 then --down
  1152.                 playerl[2] = playerl[2] + 1
  1153.             elseif key == 205 then --right
  1154.                 playerl[1] = playerl[1] + 1
  1155.             elseif key == 28 then
  1156.                 ismap = false
  1157.                 dest = {playerl[1], playerl[2]}
  1158.                 local difx, dify = 0
  1159.                 difx = oplayerl[1] - playerl[1]
  1160.                 if difx < 0 then difx = difx * -1 end
  1161.                 dify = oplayerl[2] - playerl[2]
  1162.                 if dify < 0 then dify = dify * -1 end
  1163.                 dist = difx + dify * 1000
  1164.                
  1165.                 target = getTarget(playerl[1], playerl[2])
  1166.                 progress = progress + 1
  1167.             elseif key == 29 then
  1168.                 on = false
  1169.             end
  1170.             checkPlayer()
  1171.         else
  1172.             if isshop then
  1173.                 if key == 203 then --left
  1174.                     sel = sel - 1
  1175.                     updateSelection()
  1176.                     local choice, price, pixs = getShop(sel, data)
  1177.                     cshop = pixs
  1178.                 elseif key == 205 then --right
  1179.                     sel = sel + 1
  1180.                     updateSelection()
  1181.                     local choice, price, pixs = getShop(sel, data)
  1182.                     cshop = pixs
  1183.                 elseif key == 28 then --enter
  1184.                     doShop(sel)
  1185.                 end
  1186.             else
  1187.                 if key == 29 then
  1188.                     on = false
  1189.                
  1190.                 elseif key == 57 then
  1191.                     if isfighting and not (iswriting or isloading or pause) then
  1192.                         firing = true
  1193.                         fire()
  1194.                     end
  1195.                
  1196.                 elseif key == 14 then
  1197.                     if isfighting and not (iswriting or isloading or pause) then
  1198.                         fleeFight(data, fighting)
  1199.                     end
  1200.                     if (iswriting or isloading) then
  1201.                         if curdir ~= "" then
  1202.                             if string.len(curdir) > 1 then
  1203.                                 curdir = string.sub(curdir, 1, -2)
  1204.                             else
  1205.                                 curdir = ""
  1206.                             end
  1207.                         end
  1208.                     end
  1209.                 elseif key == 44 then
  1210.                     if isfighting and not (iswriting or isloading or pause) then
  1211.                         if canrepair then
  1212.                             repair(data, fighting)
  1213.                            
  1214.                             canrepair = false
  1215.                             rtimer = os.startTimer(((100-data.crew) / 10)*2)
  1216.                         end
  1217.                     end
  1218.                 elseif key == 45 then
  1219.                     if cancharge and isfighting and not (iswriting or isloading or pause) then
  1220.                         charge(data, fighting)
  1221.                        
  1222.                         cancharge = false
  1223.                         ctimer = os.startTimer(((100-data.crew) / 10)*4)
  1224.                     end
  1225.                 elseif key == 25 then
  1226.                     if not (iswriting or isloading) then
  1227.                         if pause then
  1228.                             pause = false
  1229.                         else
  1230.                             pause = true
  1231.                         end
  1232.                     end
  1233.                 elseif key == 31 then
  1234.                     if not iswriting and not isloading then
  1235.                         local tx, ty = term.getSize()
  1236.                         term.setCursorPos(1, ty-1)
  1237.                         term.write("Enter save directory:")
  1238.                         pause = true
  1239.                         iswriting = true
  1240.                     else
  1241.                         --if curdir ~= "" and string.gsub(curdir, " ", "") ~= "" and curdir ~= nil then
  1242.                         --  save(curdir)
  1243.                         --end
  1244.                         --iswriting = false
  1245.                         --curdir = ""
  1246.                     end
  1247.                 elseif key == 38 then
  1248.                     if not isloading and not iswriting then
  1249.                         term.write("Enter load directory:")
  1250.                         isloading = true
  1251.                         pause = true
  1252.                     end
  1253.                 elseif key == 28 then
  1254.                     if iswriting then
  1255.                         if curdir ~= "" and string.gsub(curdir, " ", "") ~= "" and curdir ~= nil then
  1256.                             save(curdir)
  1257.                         end
  1258.                         iswriting = false
  1259.                         pause = false
  1260.                         curdir = ""
  1261.                        
  1262.                         msg = "Game saved!"
  1263.                     end
  1264.                     if isloading then
  1265.                         if curdir ~= "" and string.gsub(curdir, " ", "") ~= "" and curdir ~= nil then
  1266.                             local isf, fshp, shp, prog, lc, des, dis = loadGame(curdir)
  1267.                             if shp ~= nil then
  1268.                                 progress = prog
  1269.                                 loc = lc
  1270.                                 dest = des
  1271.                                 dist = dis
  1272.                                 isfighting = isf
  1273.                                 fighting = fsh
  1274.                                 if fighting ~= nil then
  1275.                                     f_ship, f_weps, f_stats = createShip(14, 2, ship_colors, fighting.stats.type)
  1276.                                     f_weps = getFire(f_weps)
  1277.                                 end
  1278.                                
  1279.                                 msg = shp.name
  1280.                                 data = shp
  1281.                                 local tshp, weapos, stas = createShip(2, 2, ship_colors, data["stats"].type)
  1282.                                 ship = tshp
  1283.                                 wep_array = getFire(weapos)
  1284.                                 msg = "Game loaded!"
  1285.                                 isloading = false
  1286.                                 pause = false
  1287.                             else
  1288.                                 msg = "Failed to load game!"
  1289.                                 isloading = false
  1290.                                 pause = false
  1291.                             end
  1292.                         end
  1293.                     end
  1294.                 end
  1295.             end
  1296.         end
  1297.     end
  1298.    
  1299. end
  1300. term.clear()
  1301. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement