Advertisement
JustDoesGames

Menu Tycoon I

Feb 23rd, 2020
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.18 KB | None | 0 0
  1. -- Menu Tycoon 1 --
  2. -- JDG Brian --
  3. -- 2/20/2020 5:30 --
  4.  
  5. dir = fs.getDir(shell.getRunningProgram())
  6. version = "1.0."..string.sub(tostring(fs.getSize(shell.getRunningProgram()))-1,-6)
  7. w,h = term.getSize()
  8. -- temp
  9. if not term.isColor() then return print("Requires Advanced Computer (soon any computer).") end
  10. local cols
  11. if term.isColor() then
  12.     cols = {"white", "orange", "magenta", "lightBlue", "yellow", "lime", "pink", "gray", "lightGray", "cyan", "purple", "blue", "brown", "green", "red", "black"}
  13. else
  14.     cols = {"white", "gray", "lightGray", "black"}
  15. end
  16.  
  17. -- Mini-Engine --
  18.  
  19. clr = function() term.clear() end
  20. cp = function(x,y) term.setCursorPos(x,y) end
  21. slowtext = function(speed, text)
  22.     for i=1, string.len(text) do
  23.         write(string.sub(text, i,i))
  24.         sleep(speed)
  25.     end
  26. end
  27. local p = function(text) print(text) end
  28.  
  29. local print = function(text, speed)
  30.     if speed ~= nil and speed ~= 0 then
  31.         local alt = true
  32.         for i=1, string.len(text) do
  33.             write(string.sub(text,i,i))
  34.             if alt then
  35.             sleep(speed)
  36.             end
  37.             alt = not alt
  38.         end
  39.         p("")
  40.     else
  41.         p(text)
  42.     end
  43. end
  44.  
  45.  
  46. -- Mini-Engine --
  47.  
  48.  
  49.  
  50. -- Vars --
  51.  
  52. function save()
  53.     local file = fs.open(dir.."/saves/sv.lua", "w")
  54.     file.write(textutils.serialize(data))
  55.     file.close()
  56. end
  57.  
  58. function load()
  59.     if fs.exists(dir.."/saves/sv.lua") then
  60.         local file = fs.open(dir.."/saves/sv.lua", "r")
  61.         data = textutils.unserialize(file.readAll())
  62.         file.close()
  63.     end
  64. end
  65.  
  66. function exists(thing)
  67.     if thing ~= nil then
  68.         return true
  69.     end
  70.     return false
  71. end
  72.  
  73. load()
  74.  
  75. if not exists(data) then data = {} end
  76.  
  77. if not exists(data.player) then data.player = {} end
  78. if not exists(data.player.cash) then data.player.cash = 0 end
  79. if not exists(data.player.generate) then data.player.generate = 1 end
  80.  
  81. if not exists(data.settings) then data.settings = {} end
  82. if not exists(data.settings.selectedtextcolor) then data.settings.selectedtextcolor = colors.white end
  83. if not exists(data.settings.textcolor) then data.settings.textcolor = colors.gray end
  84. if not exists(data.settings.backgroundcolor) then data.settings.backgroundcolor = colors.black end
  85.  
  86.  
  87. if not exists(data.genStats) then data.genStats = {} end
  88. if not exists(data.genStats.scale) then data.genStats.scale = 1 end -- use this to scale money generation
  89. if not exists(data.genStats.costScale) then data.genStats.costScale = 500 end
  90. if not exists(data.genStats.generators) then data.genStats.generators = {} end
  91.  
  92. if not exists(data.genStats.generators.starter) then
  93.     data.genStats.generators.starter = {}
  94.     for i=1, 10 do
  95.         data.genStats.generators.starter[i] = {}
  96.         data.genStats.generators.starter[i].name = "Weak Generator "..i
  97.         data.genStats.generators.starter[i].status = false -- create 10 disabled generators
  98.         data.genStats.generators.starter[i].value = i*data.genStats.scale -- scales each generator's earn rate
  99.         data.genStats.generators.starter[i].cost = i*data.genStats.costScale -- scales each generator's cost
  100.     end
  101. end
  102.  
  103. if not exists(data.genStats.generators.inheritor) then
  104.     data.genStats.generators.inheritor = {}
  105.     for i=1, 10 do
  106.         data.genStats.generators.inheritor[i] = {}
  107.         data.genStats.generators.inheritor[i].name = "Rusty Generator "..i
  108.         data.genStats.generators.inheritor[i].status = false -- create 10 disabled generators
  109.         data.genStats.generators.inheritor[i].value = i*data.genStats.scale -- scales each generator's earn rate
  110.         data.genStats.generators.inheritor[i].cost = i*data.genStats.costScale*4 -- scales each generator's cost
  111.     end
  112. end
  113.  
  114. if not exists(data.genStats.generators.noteworthy) then
  115.     data.genStats.generators.noteworthy = {}
  116.     for i=1, 10 do
  117.         data.genStats.generators.noteworthy[i] = {}
  118.         data.genStats.generators.noteworthy[i].name = "Dusty Generator "..i
  119.         data.genStats.generators.noteworthy[i].status = false -- create 10 disabled generators
  120.         data.genStats.generators.noteworthy[i].value = i*data.genStats.scale -- scales each generator's earn rate
  121.         data.genStats.generators.noteworthy[i].cost = i*data.genStats.costScale*8 -- scales each generator's cost
  122.     end
  123. end
  124.  
  125. menu = {}
  126.  
  127. function reloadMenu()
  128.     menu = {}
  129.     menu.main = {"Generate Cash", "Generators", "Settings", "Save and Exit"}
  130.     menu.settings = {"Selected Text Color", "Text Color", "Background Color", "Reset Game", "Back"}
  131.  
  132.     menu.generatorlist = {"Starter", "Inheritor", "Noteworthy", "Back"}
  133.     menu.generators = {}
  134.  
  135.     menu.generators.starter = {}
  136.     for i=1, #data.genStats.generators.starter do
  137.         menu.generators.starter[i] = data.genStats.generators.starter[i].name
  138.         if data.genStats.generators.starter[i].status then
  139.             menu.generators.starter[i] = menu.generators.starter[i].." Owned"
  140.         else
  141.             menu.generators.starter[i] = menu.generators.starter[i].." $ "..data.genStats.generators.starter[i].cost.." ("..data.genStats.generators.starter[i].value..")"
  142.         end
  143.     end
  144.     menu.generators.starter[#menu.generators.starter+1] = "Back"
  145.    
  146.     menu.generators.inheritor = {}
  147.     for i=1, #data.genStats.generators.inheritor do
  148.         menu.generators.inheritor[i] = data.genStats.generators.inheritor[i].name
  149.         if data.genStats.generators.inheritor[i].status then
  150.             menu.generators.inheritor[i] = menu.generators.inheritor[i].." Owned"
  151.         else
  152.             menu.generators.inheritor[i] = menu.generators.inheritor[i].." $ "..data.genStats.generators.inheritor[i].cost.." ("..data.genStats.generators.inheritor[i].value..")"
  153.         end
  154.     end
  155.     menu.generators.inheritor[#menu.generators.inheritor+1] = "Back"
  156.    
  157.     menu.generators.noteworthy = {}
  158.     for i=1, #data.genStats.generators.noteworthy do
  159.         menu.generators.noteworthy[i] = data.genStats.generators.noteworthy[i].name
  160.         if data.genStats.generators.noteworthy[i].status then
  161.             menu.generators.noteworthy[i] = menu.generators.noteworthy[i].." Owned"
  162.         else
  163.             menu.generators.noteworthy[i] = menu.generators.noteworthy[i].." $ "..data.genStats.generators.noteworthy[i].cost.." ("..data.genStats.generators.noteworthy[i].value..")"
  164.         end
  165.     end
  166.     menu.generators.noteworthy[#menu.generators.noteworthy+1] = "Back"
  167. end
  168.  
  169. reloadMenu()
  170.  
  171. local currentmenu, prevmenu, selected, updat, running = menu.main, {}, 1, true, true
  172.  
  173. -- Vars --
  174.  
  175.  
  176.  
  177. -- Functions --
  178.  
  179. function splash()
  180.     term.setBackgroundColor(colors.black)
  181.     term.setTextColor(colors.lime)
  182.     clr() cp(1,1)
  183.     slowtext(.02, "Menu Tycoon  by  JDG Brian")
  184.     sleep(.5) cp(1,2) term.setTextColor(colors.gray)
  185.     slowtext(.01, "Version "..version) sleep(1) clr()
  186. end
  187.  
  188. function colorselect()
  189.     local run, update, select = true, true, 1
  190.     term.setBackgroundColor(data.settings.backgroundcolor)
  191.     while run do
  192.         if update then
  193.             update = false clr() cp(1,1)
  194.             term.setTextColor(data.settings.selectedtextcolor)
  195.             for i=1, string.len(cols[select])/2 do write(" ") end
  196.             print(string.char(30))
  197.             term.setTextColor(colors[cols[select]])
  198.             print(cols[select])
  199.             term.setTextColor(data.settings.selectedtextcolor)
  200.             for i=1, string.len(cols[select])/2 do write(" ") end
  201.             print(string.char(31))
  202.             print("Press 'q' to exit")
  203.         end
  204.         a,i = os.pullEvent("key")
  205.         if i == keys.w or i == keys.up then
  206.             if select == 1 then select = #cols else select = select - 1 end update = true
  207.         elseif i == keys.s or i == keys.down then
  208.             if select == #cols then select = 1 else select = select + 1 end update = true
  209.         elseif i == keys.e or i == keys.enter then
  210.             clr() return colors[cols[select]]
  211.         elseif i == keys.q then
  212.             clr() run = false
  213.         end
  214.     end
  215.     return false
  216. end
  217.  
  218.  
  219. function runMenuCommand(command)
  220.     command = string.lower(string.gsub(command, " ", "")) -- removes all spaces and lowers everything
  221.     local list = {
  222.         generatecash = function() -- main generation
  223.             data.player.cash = data.player.cash + data.player.generate sleep(.1)
  224.         end,
  225.        
  226.         saveandexit = function() -- save and exit program
  227.             save()
  228.             running = false
  229.         end,
  230.        
  231.         settings = function()
  232.             prevmenu[#prevmenu+1] = currentmenu
  233.             currentmenu = menu.settings
  234.             selected = 1
  235.         end,
  236.        
  237.         resetgame = function()
  238.             term.setBackgroundColor(colors.black) term.setTextColor(colors.red)
  239.             clr() cp(1,1) write("type 'confirm' to erase data: ")
  240.             term.setTextColor(colors.white)
  241.             sleep(.2)
  242.             if string.lower(read()) == "confirm" then
  243.                 if fs.exists(dir.."/saves/") then
  244.                     fs.delete(dir.."/saves/")
  245.                 end
  246.                 print("All save data deleted!") sleep(1) running = false
  247.             end
  248.         end,
  249.        
  250.         generators = function() -- main list of generators
  251.             prevmenu[#prevmenu+1] = currentmenu
  252.             currentmenu = menu.generatorlist
  253.             selected = 1
  254.         end,
  255.        
  256.         starter = function() -- starter list of generators
  257.             prevmenu[#prevmenu+1] = currentmenu
  258.             currentmenu = menu.generators.starter
  259.             selected = 1
  260.         end,
  261.        
  262.         inheritor = function()
  263.             prevmenu[#prevmenu+1] = currentmenu
  264.             currentmenu = menu.generators.inheritor
  265.             selected = 1
  266.         end,
  267.        
  268.         noteworthy = function()
  269.             prevmenu[#prevmenu+1] = currentmenu
  270.             currentmenu = menu.generators.noteworthy
  271.             selected = 1
  272.         end,
  273.        
  274.         back = function() -- go back a menu
  275.             if #prevmenu == 0 then
  276.                 running = false
  277.             else
  278.                 currentmenu = prevmenu[#prevmenu]
  279.                 prevmenu[#prevmenu] = nil
  280.                 selected = 1
  281.             end
  282.         end,
  283.        
  284.         selectedtextcolor = function()
  285.             local tmp = colorselect()
  286.             if tmp ~= false then
  287.                 data.settings.selectedtextcolor = tmp clr()
  288.             end
  289.         end,
  290.        
  291.         textcolor = function()
  292.             local tmp = colorselect()
  293.             if tmp ~= false then
  294.                 data.settings.textcolor = tmp clr()
  295.             end
  296.         end,
  297.        
  298.         backgroundcolor = function()
  299.             local tmp = colorselect()
  300.             if tmp ~= false then
  301.                 data.settings.backgroundcolor = tmp
  302.                 term.setBackgroundColor(tmp)
  303.                 clr()
  304.             end
  305.         end,
  306.     }
  307.     if list[command] ~= nil then list[command]() end
  308.     if data.settings.textcolor == data.settings.backgroundcolor and data.settings.selectedtextcolor == data.settings.backgroundcolor then
  309.         data.settings.backgroundcolor = colors.black
  310.         data.settings.selectedtextcolor = colors.white
  311.         data.settings.textcolor = colors.gray
  312.     end
  313. end
  314.  
  315. function purchase(id)
  316.     if currentmenu == menu.generators.starter then
  317.         if data.player.cash >= data.genStats.generators.starter[id].cost and data.genStats.generators.starter[id].status == false then
  318.             data.player.cash = data.player.cash - data.genStats.generators.starter[id].cost
  319.             data.genStats.generators.starter[id].status = true
  320.             data.player.generate = data.player.generate + data.genStats.generators.starter[id].value
  321.             reloadMenu()
  322.             currentmenu, prevmenu, selected = menu.main, {}, 1
  323.         end
  324.     elseif currentmenu == menu.generators.inheritor then
  325.         if data.player.cash >= data.genStats.generators.inheritor[id].cost and data.genStats.generators.inheritor[id].status == false then
  326.             data.player.cash = data.player.cash - data.genStats.generators.inheritor[id].cost
  327.             data.genStats.generators.inheritor[id].status = true
  328.             data.player.generate = data.player.generate + data.genStats.generators.inheritor[id].value
  329.             reloadMenu()
  330.             currentmenu, prevmenu, selected = menu.main, {}, 1
  331.         end
  332.     elseif currentmenu == menu.generators.noteworthy then
  333.         if data.player.cash >= data.genStats.generators.noteworthy[id].cost and data.genStats.generators.noteworthy[id].status == false then
  334.             data.player.cash = data.player.cash - data.genStats.generators.noteworthy[id].cost
  335.             data.genStats.generators.noteworthy[id].status = true
  336.             data.player.generate = data.player.generate + data.genStats.generators.noteworthy[id].value
  337.             reloadMenu()
  338.             currentmenu, prevmenu, selected = menu.main, {}, 1
  339.         end
  340.     end
  341. end
  342.  
  343. -- Functions --
  344.  
  345.  
  346.  
  347. -- Main --
  348.  
  349. function t_reserve()
  350.     while true do
  351.         sleep(1)
  352.     end
  353. end
  354.  
  355. function t_menus()
  356.     local nprev = #prevmenu
  357.     while running do
  358.         if updat then
  359.             updat = false
  360.             local spd
  361.             if #prevmenu ~= nprev then spd = .00001 clr() else spd = 0 end nprev = #prevmenu
  362.             term.setBackgroundColor(data.settings.backgroundcolor)
  363.             cp(1,h) term.setTextColor(colors.white) write("$ ") term.setTextColor(colors.lime)
  364.             if string.len(data.player.cash) >= 10 then
  365.                 write("Infinity and Beyond")
  366.             elseif string.len(data.player.cash) >= 9 then
  367.                 write(string.sub(data.player.cash, 1,1).."T ($"..data.player.cash..")")
  368.             elseif string.len(data.player.cash) >= 8 then
  369.                 write(string.sub(data.player.cash, 1,1).."B ($"..data.player.cash..")")
  370.             elseif string.len(data.player.cash) >= 7 then
  371.                 write(string.sub(data.player.cash, 1,1).."M ($"..data.player.cash..")")
  372.             elseif string.len(data.player.cash) >= 6 then
  373.                 write(string.sub(data.player.cash, 1,3).."K ($"..data.player.cash..")")
  374.             else
  375.                 write(data.player.cash)
  376.             end
  377.             cp(1,h-1) term.setTextColor(colors.white) write("^ ") term.setTextColor(colors.yellow) write(data.player.generate)
  378.             cp(1,1) term.setTextColor(colors.cyan) write("Menu Tycoon I")
  379.             cp(w-string.len("v."..version),1) term.setTextColor(colors.gray) write("v."..version)
  380.             cp(1,3)
  381.             for i=1, #currentmenu do
  382.                 if selected == i then
  383.                     term.setTextColor(data.settings.selectedtextcolor)
  384.                     print("> "..currentmenu[i], spd)
  385.                 else
  386.                     term.setTextColor(data.settings.textcolor)
  387.                     print(currentmenu[i].."  ", spd)
  388.                 end
  389.             end
  390.             term.setTextColor(colors.white)
  391.         end
  392.         a,i = os.pullEvent("key")
  393.         if i == keys.up or i == keys.w then
  394.             if selected == 1 then selected = #currentmenu else selected = selected - 1 end updat = true
  395.         elseif i == keys.down or i == keys.s then
  396.             if selected == #currentmenu then selected = 1 else selected = selected + 1 end updat = true
  397.         elseif i == keys.e or i == keys.enter then
  398.             if selected == #currentmenu then
  399.                 runMenuCommand(currentmenu[selected]) updat = true
  400.             elseif prevmenu[#prevmenu] == menu.generatorlist then
  401.                 purchase(selected) updat = true
  402.             else
  403.                 runMenuCommand(currentmenu[selected]) updat = true
  404.             end
  405.         elseif i == keys.q then
  406.             runMenuCommand("Back") updat = true
  407.         end
  408.     end
  409. end
  410.  
  411. splash() res, er = pcall(function() parallel.waitForAny(t_reserve, t_menus) end)
  412. if not res then
  413.     sleep(1)
  414.     term.setTextColor(colors.white)
  415.     term.setBackgroundColor(colors.black)
  416.     clr() cp(1,1)
  417.     print("Game has Crashed. :(")
  418.     print("")
  419.     print("Game Version: "..version)
  420.     print("Crash Report: "..er)
  421.     print("")
  422.     sleep(2)
  423.     print("Press any key to continue...") os.pullEvent("char")
  424. end
  425. term.setTextColor(colors.white) term.setBackgroundColor(colors.black) clr() cp(1,1) print("MT1 by JDG Brian") sleep(.1)
  426.  
  427. -- Main --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement