jay5476

code

Feb 6th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.19 KB | None | 0 0
  1. -- Made by Jay5476
  2. -- Credit To:
  3. -- Version 0.1
  4.  
  5. -- Important values
  6. local w,h = term.getSize()
  7. local nBuildingScroll = 0
  8. local sGameDir = "Idle"
  9. local tLocalFiles = {["SAVES"] = false}
  10. local tPasteBinFiles = {["API"] = "CodeHere"}
  11.  
  12.     tBuildingsDisplay = {
  13.     [1] = 1,
  14.     [2] = 2,
  15.     [3] = 3,
  16.     [4] = 4,
  17.     [5] = 5,
  18.     [6] = 6,
  19.     [7] = 7,
  20.     [8] = 8,
  21.     [9] = 9,
  22.     [10] = 10,
  23.     [11] = 11,
  24.     [12] = 12,
  25.     [13] = 13,
  26.     [14] = 14,
  27.     [15] = 15,
  28.     [16] = 16,
  29.     [17] = 17,
  30.     [18] = 18
  31.     }
  32.  
  33. local tGameValues = {
  34. ["TotalAutoCPS"] = 0,
  35. ["AutoCPSValue"] = 1,
  36. ["TotalClicks"] = 0,
  37. ["ClickValue"] = 1,
  38. ["Coins"] = 0,
  39. ["B1"] = {
  40.             ["Value"] = 10,
  41.             ["StartPrice"] = 100,
  42.             ["Price"] = 100,
  43.             ["Owned"] = 0
  44.                 },
  45. ["B2"] = {
  46.             ["Value"] = 15,
  47.             ["StartPrice"] = 300,
  48.             ["Price"] = 300,
  49.             ["Owned"] = 0
  50.                 },
  51. ["B3"] = {
  52.             ["Value"] = 50,
  53.             ["Price"] = 1000,
  54.             ["Owned"] = 0
  55.                 },
  56. [""] = 0,
  57. } -- Add buildings
  58. local sGameValues = textutils.serialize(tGameValues)
  59.  
  60. -- Base Functions
  61.  
  62. local function clear() -- Clears Screen
  63.     term.clear()
  64.     term.setCursorPos(1,1)
  65. end
  66.  
  67. local function cPrint(str) -- Print to centre
  68.  
  69.     local _, y = term.getCursorPos()
  70.     term.setCursorPos(math.floor((w-#str)/2),y)
  71.     print(str)
  72. end
  73.  
  74. local function tNumber(tab)
  75.    
  76.     i = 0
  77.     for k,v in pairs(tab) do
  78.        
  79.         i = i + 1
  80.     end
  81.     return i
  82. end
  83.  
  84. local function Counter()
  85.     tBuildingsDisplay = {
  86.     [1] = 1,
  87.     [2] = 2,
  88.     [3] = 3,
  89.     [4] = 4,
  90.     [5] = 5,
  91.     [6] = 6,
  92.     [7] = 7,
  93.     [8] = 8,
  94.     [9] = 9,
  95.     [10] = 10,
  96.     [11] = 11,
  97.     [12] = 12,
  98.     [13] = 13,
  99.     [14] = 14,
  100.     [15] = 15,
  101.     [16] = 16,
  102.     [17] = 17,
  103.     [18] = 18
  104.     }
  105.    
  106.     clear()
  107.     cPrint("Coins: "..math.floor(tGameValues.Coins).." (+"..math.floor(tGameValues.ClickValue).." Per Click)")
  108.     cPrint("Auto Coins: "..math.floor(tGameValues.AutoCPSValue).." Per 1s")
  109.     --cPrint("Total Clicks: "..tGameValues.TotalClicks)
  110.     for i = 1, w do
  111.         term.write("-")
  112.     end
  113.    
  114.     for building = 1, 4 do -- Show for buildings at a time
  115.    
  116.         for i = 1, 3 do
  117.        
  118.             term.setCursorPos(1,i+4*building-1)
  119.             print(tBuildingsDisplay[i+3*building-3+(nBuildingScroll*3)] or "Coming Soon")
  120.        
  121.         end
  122.        
  123.        
  124.         for x = 1, 20 do
  125.             term.setCursorPos(x,3+4*building)
  126.             term.write("-")
  127.             term.setCursorPos(x,h)
  128.             term.write("-")
  129.         end
  130.        
  131.     end
  132.    
  133. end
  134. -- Debug Functions/values
  135.  
  136. bDebug = false
  137.  
  138. local function dPrint(str, reason)
  139.  
  140. reason = reason or "Debugging"
  141. str = str or ""
  142.  
  143. if bDebug then
  144.    
  145.     local sColor = term.getTextColor()
  146.     term.setTextColor(colors.green)
  147.     print("Debug Text\n"..reason..": ")
  148.     term.setTextColor(colors.red)
  149.     print(str.."\nPress any key to continue")
  150.     term.setTextColor(sColor)
  151.     os.pullEvent("key")
  152. end
  153.  
  154. end
  155. -- Game Save/load
  156.  
  157. local function intFiles()
  158.  
  159.    
  160.     local bFirstTime = true
  161.  
  162.     if fs.exists(sGameDir) then
  163.  
  164.       bFirstTime = false
  165.       dPrint("True", "Checking Directory Return")
  166.      
  167.       for k,v in pairs(tLocalFiles) do
  168.      
  169.        if fs.exists(sGameDir.."/"..k) then
  170.          tLocalFiles[k] = true
  171.        end
  172.        
  173.        if not tLocalFiles[k] then
  174.          print(k.." Doesnt Exist, Creating")
  175.          local file = fs.open(sGameDir.."/"..k, "a")
  176.            file.write(sGameValues,a)
  177.            file.close()
  178.        end
  179.          
  180.       end
  181.      
  182.     else
  183.  
  184.       print("Creating Directory")
  185.       fs.makeDir(sGameDir)
  186.       for k,v in pairs(tLocalFiles) do
  187.         local file = fs.open(sGameDir.."/"..k, "w")
  188.         file.write(sGameValues)
  189.         file.close()
  190.         print("Created "..k)
  191.       end
  192.      
  193.     end
  194.    
  195. end
  196.  
  197. local function loadSaves()
  198.  
  199.     local file = fs.open(sGameDir.."/SAVES", "r")
  200.         sLoadGameValues = file.readAll()
  201.         tLoadGameValues = textutils.unserialize(sLoadGameValues)
  202.     file.close()
  203.  
  204.     clear()
  205.     print("Loaded game values..")
  206.     for k, v in pairs(tLoadGameValues) do -- Get all loaded values and put into current
  207.        
  208.         if tGameValues[k] then
  209.             tGameValues[k] = v
  210.         end
  211.            
  212.     end
  213.     for k,v in pairs(tGameValues) do
  214.         if type(v)=="table" then
  215.             for k1, v1 in pairs(v) do
  216.                 print("Playing with "..k..k1..": "..v1)
  217.             end
  218.         else
  219.             print("Playing With "..k..": "..v)
  220.         end
  221.     end
  222.        
  223.  
  224.     print("Press any key to continue")
  225.     os.pullEvent("key")
  226. end
  227.    
  228. local function saveFile()
  229.  
  230.     local tSaveGameValues = tGameValues -- Grab current values
  231.     sSaveGameValues = textutils.serialize(tSaveGameValues) -- Store to string
  232.     local file = fs.open(sGameDir.."/SAVES", "w") -- Open save file
  233.         file.write(sSaveGameValues) -- Overwrite save
  234.     file.close()
  235.  
  236. end
  237.  
  238. -- Game functions/code
  239.  
  240. local function buyBuilding(building)
  241.     local tBuilding = tGameValues[building]
  242.     if tBuilding and type(tBuilding) == "table" then
  243.         if tGameValues.Coins > tBuilding.Price then
  244.             tBuilding.Owned = tBuilding.Owned + 1
  245.             tGameValues.Coins = tGameValues.Coins - tBuilding.Price
  246.             tBuilding.Price = tBuilding.Price * 1.15
  247.         else
  248.             dPrint("Not enough","Building not bought")
  249.             return
  250.         end
  251.     else
  252.         dPrint("Doesn't exist","Building not bought")
  253.         return
  254.     end
  255. end
  256.  
  257. local function playerClick()
  258.  
  259.     tGameValues.TotalClicks = tGameValues.TotalClicks + 1
  260.     tGameValues.Coins = tGameValues.ClickValue + tGameValues.Coins
  261.     tGameValues.ClickValue = 1 + (0.01 * tGameValues.TotalClicks)
  262.    
  263. end
  264.  
  265. local function autoCPS()
  266.    
  267.    
  268.     tGameValues.TotalAutoCPS = tGameValues.TotalAutoCPS + 1
  269.     tGameValues.Coins = tGameValues.AutoCPSValue + tGameValues.Coins
  270.    
  271.     buyBuilding("B1")
  272.     buyBuilding("B2")
  273.     buyBuilding("B3")
  274.    
  275.     tGameValues.AutoCPSValue =
  276.     1 + tGameValues.B1.Owned * tGameValues.B1.Value
  277.       + tGameValues.B2.Owned * tGameValues.B2.Value
  278.       + tGameValues.B3.Owned * tGameValues.B3.Value
  279.    
  280.    
  281. end
  282.  
  283. clear()
  284. intFiles()
  285. loadSaves()
  286. clear()
  287. os.startTimer(1)
  288.  
  289. while true do
  290.     Counter()
  291.     e, p1, p2, p3 = os.pullEvent() 
  292.     -- Event, Button, X, Y for mouseclick
  293.     term.setCursorPos(30,10)
  294.  
  295.     if e == "mouse_click" then
  296.         if p2 >21 then
  297.             playerClick()
  298.         end
  299.        
  300.     elseif e == "timer" then
  301.         os.startTimer(1)
  302.         autoCPS()
  303.     elseif e == "key" or "key_up" then
  304.        
  305.         if p1 == 200 and nBuildingScroll > 0 then
  306.            
  307.             nBuildingScroll = nBuildingScroll -1
  308.         elseif p1 == 208 and nBuildingScroll < ((#tBuildingsDisplay/3) - 4) then
  309.             nBuildingScroll = nBuildingScroll + 1
  310.         end
  311.     term.write(nBuildingScroll.."|")
  312.     term.write(#tBuildingsDisplay/3)
  313.     term.write("|"..e.."|"..p1)
  314.     sleep(1)   
  315.            
  316.     end
  317.    
  318.    
  319.    
  320.     saveFile()
  321. end
Advertisement
Add Comment
Please, Sign In to add comment