Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --COOKIE CLICKER made By VikeStep
- -- 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
- cookieMonString="monitor_0"
- purchaseMonString="monitor_1"
- statsMonString="monitor_2"
- -- Misc/Helper Functions
- function drawButton(inputData)
- local x1, x2, y1, y2, text, text2, monitor = unpack(inputData)
- local mon = peripheral.wrap(monitor)
- mon.setBackgroundColor(colors.lightGray)
- for i=x1, x2 do
- for j=y1, y2 do
- mon.setCursorPos(i,j)
- mon.write(" ")
- end
- end
- mon.setCursorPos(math.floor(((x2+x1)/2)-(string.len(text)/2)),math.floor((y2+y1)/2))
- mon.write(text)
- mon.setCursorPos(math.floor(((x2+x1)/2)-(string.len(text2)/2)),math.floor((y2+y1)/2)+1)
- mon.write(text2)
- mon.setBackgroundColor(colors.cyan)
- end
- function round(num)
- return math.floor(num + 0.5)
- end
- function split(pString, pPattern)
- numchanged = 0
- local Table = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pPattern
- local last_end = 1
- local s, e, cap = pString:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(Table,cap)
- numchanged = numchanged +1
- end
- last_end = e+1
- s, e, cap = pString:find(fpat, last_end)
- end
- if last_end <= #pString then
- cap = pString:sub(last_end)
- table.insert(Table, cap)
- numchanged = numchanged +1
- end
- return Table
- end
- -- Variable Initialisation
- function loadSaveData()
- --check if save data exists
- if fs.exists("cookieclickersavedata") == false then
- --create save data file
- local file = fs.open("cookieclickersavedata","w")
- file.writeLine("0:0:0:0:0:0:0:0:0:0:0")
- file.close()
- end
- --import save data file
- local file = fs.open("cookieclickersavedata","r")
- saveData = split(file.readLine(), ":")
- file.close()
- end
- function initialiseVariables()
- loadSaveData()
- --Monitor Variables
- cookieMon=peripheral.wrap(cookieMonString)
- purchaseMon=peripheral.wrap(purchaseMonString)
- statsMon=peripheral.wrap(statsMonString)
- monWidth, monHeight = cookieMon.getSize()
- -- Game Variables
- cookiesPerSecond = 0
- cookiesInBank = tonumber(saveData[11])
- cookiesPerClick = 1
- buildings = {}
- for i = 1, 10 do
- buildings[i] = {}
- end
- -- buildings[i] = {name, cost, cps}
- buildings[1] = {"Cursor", 15, 0.1}
- buildings[2] = {"Grandma", 100, 0.5}
- buildings[3] = {"Farm", 500, 4}
- buildings[4] = {"Factory", 3000, 10}
- buildings[5] = {"Mine", 10000, 40}
- buildings[6] = {"Shipment", 40000, 100}
- buildings[7] = {"Alchemy Lab", 200000, 400}
- buildings[8] = {"Portal", 1666666, 6666}
- buildings[9] = {"Time Machine", 123456789, 98765}
- buildings[10] = {"Antimatter Condensers", 3999999999, 999999}
- for i = 1, 10 do
- buildings[i][4] = tonumber(saveData[i]) --Amount Owned
- buildings[i][5] = buildings[i][2] --Current Cost
- buildings[i][6] = buildings[i][3] --Current Building CPS Increase
- end
- --Button Variables
- buttonInfo = {}
- for i = 1, 11 do
- buttonInfo[i] = {}
- end
- --buttonInfo[i] = {x1, x2, y1, y2, text1, text2, monitor}
- buttonInfo[1] = {1, 15, monHeight - 38, monHeight - 36, "Buy Cursor", "", purchaseMonString}
- buttonInfo[2] = {1, 15, monHeight - 34, monHeight - 32, "Buy Grandma", "", purchaseMonString}
- buttonInfo[3] = {1, 15, monHeight - 30, monHeight - 28, "Buy Farm", "", purchaseMonString}
- buttonInfo[4] = {1, 15, monHeight - 26, monHeight - 24, "Buy Factory", "", purchaseMonString}
- buttonInfo[5] = {1, 15, monHeight - 22, monHeight - 20, "Buy Mine", "", purchaseMonString}
- buttonInfo[6] = {1, 15, monHeight - 18, monHeight - 16, "Buy Shipment", "", purchaseMonString}
- buttonInfo[7] = {1, 15, monHeight - 14, monHeight - 12, "Buy Alchemy", "Lab", purchaseMonString}
- buttonInfo[8] = {1, 15, monHeight - 10, monHeight - 8, "Buy Portal", "", purchaseMonString}
- buttonInfo[9] = {1, 15, monHeight - 6, monHeight - 4, "Buy Time", "Machine", purchaseMonString}
- buttonInfo[10] = {1, 15, monHeight - 2, monHeight, "Buy Antimatter", "Condenser", purchaseMonString}
- buttonInfo[11] = {1, 15, math.floor(monHeight/2)-1, math.floor(monHeight/2)+1, "Reset", "Game", statsMonString}
- end
- function saveVariables(variables)
- local file = fs.open("cookieclickersavedata","w")
- file.writeLine(variables)
- file.close()
- end
- -- Monitor Initialisation
- function loadCookie()
- local cookie=paintutils.loadImage("Cookie.nfp")
- term.redirect(cookieMon)
- paintutils.drawImage(cookie,1,1)
- term.restore()
- end
- function loadPurchases()
- updateVariables()
- purchaseMon.setBackgroundColor(colors.cyan)
- for i = 1, monWidth do
- for j = 1, monHeight do
- purchaseMon.setCursorPos(i,j)
- purchaseMon.write(" ")
- end
- end
- for i = 1, 10 do
- drawButton(buttonInfo[i])
- purchaseMon.setCursorPos(buttonInfo[i][2]+1,buttonInfo[i][3])
- purchaseMon.write("Cost: "..buildings[i][5])
- purchaseMon.setCursorPos(buttonInfo[i][2]+1,(buttonInfo[i][3]+buttonInfo[i][4])/2)
- purchaseMon.write("Owned: "..buildings[i][4])
- purchaseMon.setCursorPos(buttonInfo[i][2]+1,buttonInfo[i][4])
- purchaseMon.write("Cookies Per Second: "..buildings[i][6])
- end
- end
- function loadStats()
- statsMon.setBackgroundColor(colors.cyan)
- for i = 1, monWidth do
- for j = 1, monHeight do
- statsMon.setCursorPos(i,j)
- statsMon.write(" ")
- end
- end
- drawButton(buttonInfo[11])
- statsMon.setCursorPos(monWidth-string.len("Cookies: "..(math.floor(cookiesInBank+1)-1)),math.floor(monHeight/2)-1)
- statsMon.write("Cookies: "..math.floor(cookiesInBank))
- statsMon.setCursorPos(monWidth-string.len("Cookies per Second: "..cookiesPerSecond),math.floor(monHeight/2))
- statsMon.write("Cookies per Second: "..cookiesPerSecond)
- statsMon.setCursorPos(monWidth-string.len("Cookies Per Click:"..cookiesPerClick),math.floor(monHeight/2)+1)
- statsMon.write("Cookies Per Click:"..cookiesPerClick)
- end
- function determineCost(baseCPS,amount)
- return round(baseCPS * math.pow(1.15,amount))
- end
- function updateVariables()
- cookiesPerSecond = 0
- local saveString = ""
- for i = 1, 10 do
- cookiesPerSecond = cookiesPerSecond + (buildings[i][6] * buildings[i][4])
- buildings[i][5] = determineCost(buildings[i][2],buildings[i][4])
- saveString = saveString..buildings[i][4]..":"
- end
- saveVariables(saveString..cookiesInBank)
- end
- function isValidCookieClick(xClick, yClick)
- local isValid = false
- if xClick > 17 and xClick < 66 and yClick > 10 and yClick < 31 then
- isValid = true
- elseif xClick > 20 and xClick < 62 and yClick > 7 and yClick < 34 then
- isValid = true
- elseif xClick > 24 and xClick < 59 and yClick > 3 and yClick < 38 then
- isValid = true
- elseif xClick > 30 and xClick < 52 then
- isValid = true
- end
- return isValid
- end
- function isValidTowerBought(xClick, yClick)
- local isValid = false
- local towerBought = "none"
- for i = 1, 10 do
- if xClick > buttonInfo[i][1] and xClick < buttonInfo[i][2] and yClick > buttonInfo[i][3] and yClick < buttonInfo[i][4] then
- isValid = true
- towerBought = i
- end
- end
- return {isValid, towerBought}
- end
- function isResetClicked(xClick, yClick)
- local isValid = false
- if xClick > buttonInfo[11][1] and xClick < buttonInfo[11][2] and yClick > buttonInfo[11][3] and yClick < buttonInfo[11][4] then
- isValid = true
- end
- return isValid
- end
- function handleClickEvent(side, xPos, yPos)
- if side == cookieMonString then
- if isValidCookieClick(xPos, yPos) then
- cookiesInBank = cookiesInBank + cookiesPerClick
- end
- elseif side == purchaseMonString then
- if isValidTowerBought(xPos,yPos)[1] and buildings[isValidTowerBought(xPos,yPos)[2]][5] <= cookiesInBank then
- towerBought = isValidTowerBought(xPos,yPos)[2]
- buildings[towerBought][4] = buildings[towerBought][4] + 1
- cookiesInBank = cookiesInBank - buildings[towerBought][5]
- loadPurchases()
- end
- elseif side == statsMonString then
- if isResetClicked(xPos, yPos) then
- fs.delete("cookieclickersavedata")
- os.reboot()
- end
- end
- end
- initialiseVariables()
- updateVariables()
- loadCookie()
- loadPurchases()
- loadStats()
- os.startTimer(1)
- while true do
- local event,side,xPos,yPos = os.pullEvent()
- if event == "monitor_touch" then
- handleClickEvent(side, xPos, yPos)
- elseif event == "timer" then
- cookiesInBank = cookiesInBank + cookiesPerSecond
- os.startTimer(1)
- end
- updateVariables()
- loadStats()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement