Advertisement
VikeStep

Computercraft Cookie Clicker

Sep 21st, 2013
1,306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.33 KB | None | 0 0
  1. --COOKIE CLICKER made By VikeStep
  2.  
  3. -- Edit these to the numbers that were retrieved when you placed the modem on the monitors, cookieMon is the middle monitor, purchaseMon is the monitor on the right
  4.     cookieMonString="monitor_0"
  5.     purchaseMonString="monitor_1"
  6.     statsMonString="monitor_2"
  7.  
  8. -- Misc/Helper Functions
  9.  
  10. function drawButton(inputData)
  11.     local x1, x2, y1, y2, text, text2, monitor = unpack(inputData)
  12.     local mon = peripheral.wrap(monitor)
  13.     mon.setBackgroundColor(colors.lightGray)
  14.     for i=x1, x2 do
  15.         for j=y1, y2 do
  16.         mon.setCursorPos(i,j)
  17.         mon.write(" ")
  18.         end
  19.     end
  20.     mon.setCursorPos(math.floor(((x2+x1)/2)-(string.len(text)/2)),math.floor((y2+y1)/2))
  21.     mon.write(text)
  22.     mon.setCursorPos(math.floor(((x2+x1)/2)-(string.len(text2)/2)),math.floor((y2+y1)/2)+1)
  23.     mon.write(text2)
  24.     mon.setBackgroundColor(colors.cyan)
  25. end
  26.  
  27. function round(num)
  28.     return math.floor(num + 0.5)
  29. end
  30.  
  31. function split(pString, pPattern)
  32. numchanged = 0
  33.    local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
  34.    local fpat = "(.-)" .. pPattern
  35.    local last_end = 1
  36.    local s, e, cap = pString:find(fpat, 1)
  37.    while s do
  38.           if s ~= 1 or cap ~= "" then
  39.          table.insert(Table,cap)
  40. numchanged = numchanged +1
  41.           end
  42.           last_end = e+1
  43.           s, e, cap = pString:find(fpat, last_end)
  44.    end
  45.    if last_end <= #pString then
  46.           cap = pString:sub(last_end)
  47.           table.insert(Table, cap)
  48.           numchanged = numchanged +1
  49.    end
  50.    return Table
  51. end
  52.  
  53. -- Variable Initialisation
  54.  
  55. function loadSaveData()
  56.     --check if save data exists
  57.     if fs.exists("cookieclickersavedata") == false then
  58.         --create save data file
  59.         local file = fs.open("cookieclickersavedata","w")
  60.         file.writeLine("0:0:0:0:0:0:0:0:0:0:0")
  61.         file.close()       
  62.     end
  63.     --import save data file
  64.     local file = fs.open("cookieclickersavedata","r")
  65.     saveData = split(file.readLine(), ":")
  66.     file.close()
  67. end
  68.  
  69. function initialiseVariables()
  70.     loadSaveData()
  71.    
  72.     --Monitor Variables
  73.    
  74.     cookieMon=peripheral.wrap(cookieMonString)
  75.     purchaseMon=peripheral.wrap(purchaseMonString)
  76.     statsMon=peripheral.wrap(statsMonString)
  77.    
  78.     monWidth, monHeight = cookieMon.getSize()
  79.    
  80.     -- Game Variables
  81.    
  82.     cookiesPerSecond = 0
  83.     cookiesInBank = tonumber(saveData[11])
  84.     cookiesPerClick = 1
  85.    
  86.     buildings = {}
  87.     for i = 1, 10 do
  88.         buildings[i] = {}
  89.     end
  90.    
  91.     -- buildings[i] = {name, cost, cps}
  92.     buildings[1] = {"Cursor", 15, 0.1}
  93.     buildings[2] = {"Grandma", 100, 0.5}
  94.     buildings[3] = {"Farm", 500, 4}
  95.     buildings[4] = {"Factory", 3000, 10}
  96.     buildings[5] = {"Mine", 10000, 40}
  97.     buildings[6] = {"Shipment", 40000, 100}
  98.     buildings[7] = {"Alchemy Lab", 200000, 400}
  99.     buildings[8] = {"Portal", 1666666, 6666}
  100.     buildings[9] = {"Time Machine", 123456789, 98765}
  101.     buildings[10] = {"Antimatter Condensers", 3999999999, 999999}
  102.    
  103.     for i = 1, 10 do
  104.         buildings[i][4] = tonumber(saveData[i]) --Amount Owned
  105.         buildings[i][5] = buildings[i][2] --Current Cost
  106.         buildings[i][6] = buildings[i][3] --Current Building CPS Increase
  107.     end
  108.    
  109.     --Button Variables
  110.    
  111.     buttonInfo = {}
  112.     for i = 1, 11 do
  113.         buttonInfo[i] = {}
  114.     end
  115.    
  116.     --buttonInfo[i] = {x1, x2, y1, y2, text1, text2, monitor}
  117.     buttonInfo[1] = {1, 15, monHeight - 38, monHeight - 36, "Buy Cursor", "", purchaseMonString}
  118.     buttonInfo[2] = {1, 15, monHeight - 34, monHeight - 32, "Buy Grandma", "", purchaseMonString}
  119.     buttonInfo[3] = {1, 15, monHeight - 30, monHeight - 28, "Buy Farm", "", purchaseMonString}
  120.     buttonInfo[4] = {1, 15, monHeight - 26, monHeight - 24, "Buy Factory", "", purchaseMonString}
  121.     buttonInfo[5] = {1, 15, monHeight - 22, monHeight - 20, "Buy Mine", "", purchaseMonString}
  122.     buttonInfo[6] = {1, 15, monHeight - 18, monHeight - 16, "Buy Shipment", "", purchaseMonString}
  123.     buttonInfo[7] = {1, 15, monHeight - 14, monHeight - 12, "Buy Alchemy", "Lab", purchaseMonString}
  124.     buttonInfo[8] = {1, 15, monHeight - 10, monHeight - 8, "Buy Portal", "", purchaseMonString}
  125.     buttonInfo[9] = {1, 15, monHeight - 6, monHeight - 4, "Buy Time", "Machine", purchaseMonString}
  126.     buttonInfo[10] = {1, 15, monHeight - 2, monHeight, "Buy Antimatter", "Condenser", purchaseMonString}
  127.     buttonInfo[11] = {1, 15, math.floor(monHeight/2)-1, math.floor(monHeight/2)+1, "Reset", "Game", statsMonString}
  128. end
  129.  
  130. function saveVariables(variables)
  131.     local file = fs.open("cookieclickersavedata","w")
  132.     file.writeLine(variables)
  133.     file.close()
  134. end
  135.  
  136. -- Monitor Initialisation
  137.  
  138. function loadCookie()
  139.     local cookie=paintutils.loadImage("Cookie.nfp")
  140.     term.redirect(cookieMon)
  141.     paintutils.drawImage(cookie,1,1)
  142.     term.restore()
  143. end
  144.  
  145. function loadPurchases()
  146.     updateVariables()
  147.     purchaseMon.setBackgroundColor(colors.cyan)
  148.     for i = 1, monWidth do
  149.         for j = 1, monHeight do
  150.             purchaseMon.setCursorPos(i,j)
  151.             purchaseMon.write(" ")
  152.         end
  153.     end
  154.     for i = 1, 10 do
  155.         drawButton(buttonInfo[i])
  156.         purchaseMon.setCursorPos(buttonInfo[i][2]+1,buttonInfo[i][3])
  157.         purchaseMon.write("Cost: "..buildings[i][5])
  158.         purchaseMon.setCursorPos(buttonInfo[i][2]+1,(buttonInfo[i][3]+buttonInfo[i][4])/2)
  159.         purchaseMon.write("Owned: "..buildings[i][4])
  160.         purchaseMon.setCursorPos(buttonInfo[i][2]+1,buttonInfo[i][4])
  161.         purchaseMon.write("Cookies Per Second: "..buildings[i][6])
  162.     end
  163. end
  164.  
  165. function loadStats()
  166.     statsMon.setBackgroundColor(colors.cyan)
  167.     for i = 1, monWidth do
  168.         for j = 1, monHeight do
  169.             statsMon.setCursorPos(i,j)
  170.             statsMon.write(" ")
  171.         end
  172.     end
  173.     drawButton(buttonInfo[11])
  174.     statsMon.setCursorPos(monWidth-string.len("Cookies: "..(math.floor(cookiesInBank+1)-1)),math.floor(monHeight/2)-1)
  175.     statsMon.write("Cookies: "..math.floor(cookiesInBank))
  176.     statsMon.setCursorPos(monWidth-string.len("Cookies per Second: "..cookiesPerSecond),math.floor(monHeight/2))
  177.     statsMon.write("Cookies per Second: "..cookiesPerSecond)
  178.     statsMon.setCursorPos(monWidth-string.len("Cookies Per Click:"..cookiesPerClick),math.floor(monHeight/2)+1)
  179.     statsMon.write("Cookies Per Click:"..cookiesPerClick)
  180. end
  181.  
  182. function determineCost(baseCPS,amount)
  183.     return round(baseCPS * math.pow(1.15,amount))
  184. end
  185.  
  186. function updateVariables()
  187.     cookiesPerSecond = 0
  188.     local saveString = ""
  189.     for i = 1, 10 do
  190.         cookiesPerSecond = cookiesPerSecond + (buildings[i][6] * buildings[i][4])
  191.         buildings[i][5] = determineCost(buildings[i][2],buildings[i][4])
  192.         saveString = saveString..buildings[i][4]..":"
  193.     end
  194.     saveVariables(saveString..cookiesInBank)
  195. end
  196.  
  197. function isValidCookieClick(xClick, yClick)
  198.     local isValid = false
  199.     if xClick > 17 and xClick < 66 and yClick > 10 and yClick < 31 then
  200.         isValid = true
  201.     elseif xClick > 20 and xClick < 62 and yClick > 7 and yClick < 34 then
  202.         isValid = true
  203.     elseif xClick > 24 and xClick < 59 and yClick > 3 and yClick < 38 then
  204.         isValid = true
  205.     elseif xClick > 30 and xClick < 52 then
  206.         isValid = true
  207.     end
  208. return isValid
  209. end
  210.  
  211. function isValidTowerBought(xClick, yClick)
  212.     local isValid = false
  213.     local towerBought = "none"
  214.     for i = 1, 10 do
  215.         if xClick > buttonInfo[i][1] and xClick < buttonInfo[i][2] and yClick > buttonInfo[i][3] and yClick < buttonInfo[i][4] then
  216.             isValid = true
  217.             towerBought = i
  218.         end
  219.     end
  220.     return {isValid, towerBought}
  221. end
  222.  
  223. function isResetClicked(xClick, yClick)
  224.     local isValid = false
  225.     if xClick > buttonInfo[11][1] and xClick < buttonInfo[11][2] and yClick > buttonInfo[11][3] and yClick < buttonInfo[11][4] then
  226.         isValid = true
  227.     end
  228.     return isValid
  229. end
  230.  
  231. function handleClickEvent(side, xPos, yPos)
  232.     if side == cookieMonString then
  233.         if isValidCookieClick(xPos, yPos) then
  234.             cookiesInBank = cookiesInBank + cookiesPerClick
  235.         end
  236.     elseif side == purchaseMonString then
  237.         if isValidTowerBought(xPos,yPos)[1] and buildings[isValidTowerBought(xPos,yPos)[2]][5] <= cookiesInBank then
  238.             towerBought = isValidTowerBought(xPos,yPos)[2]
  239.             buildings[towerBought][4] = buildings[towerBought][4] + 1
  240.             cookiesInBank = cookiesInBank - buildings[towerBought][5]
  241.             loadPurchases()
  242.         end
  243.     elseif side == statsMonString then
  244.         if isResetClicked(xPos, yPos) then
  245.             fs.delete("cookieclickersavedata")
  246.             os.reboot()
  247.         end
  248.     end
  249. end
  250. initialiseVariables()
  251. updateVariables()
  252. loadCookie()
  253. loadPurchases()
  254. loadStats()
  255. os.startTimer(1)
  256. while true do
  257.     local event,side,xPos,yPos = os.pullEvent()
  258.     if event == "monitor_touch" then
  259.         handleClickEvent(side, xPos, yPos)
  260.     elseif event == "timer" then
  261.         cookiesInBank = cookiesInBank + cookiesPerSecond
  262.         os.startTimer(1)
  263.     end
  264.     updateVariables()
  265.     loadStats()
  266. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement