var pop; var resources; var numResources; var buildings; var unlockedBuildings; var researches; var unlockedResearch; var researchPoints; var workers; var allBuildings={ hut:{ ings:{ wood:15 }, attributes:{ maxPop:2 } }, house:{ ings:{ wood:30, metal:5 }, attributes:{ maxPop:4 } }, lab:{ ings:{ wood:30, metal:15 }, attributes:{ maxResearchPoints:100 } }, storehouse:{ ings:{ wood:60 }, attributes: { maxResources:100 } }, "trading post":{ ings:{ wood:100, metal:50 } } }; var allWorkers={ lumberjack:"wood", miner:"metal", scientist:"researchPoints" }; var workerRate=0.1; var allResearches={ "Faster workers":{ maxLevel:6, cost:100, effects:{ workerRate:0.2 } } }; var multiplier=1.17; var tradingRates={ wood:1, metal:10 }; var startingStorage=100; var shelters=[]; function initializeShelterArray() { for (var name in allBuildings) { var building=allBuildings[name]; var attrbs=building.attributes; if (attrbs) { var maxpop=attrbs.maxPop; if (maxpop) { shelters.push(name); } } } } function getBuildingAttribute(name,attrname) { var attr=allBuildings[name].attributes[attrname]; if (attr==undefined) { attr=0; } return attr; } function numOfBuilding(name) { var count=buildings[name] if (count==undefined) { count=0; } return count; } function tenthRound(number) { return Math.round(number*10)/10; } function maxResources() { var numStorehouses=numOfBuilding("storehouse"); var storehouseResources=getBuildingAttribute("storehouse","maxResources"); var extraStorage=numStorehouses*storehouseResources; return startingStorage+extraStorage; } function maxResearchPoints() { var numLabs=numOfBuilding("lab"); var labRpoints=getBuildingAttribute("lab","maxResearchPoints"); return numLabs*labRpoints; } function maxPop() { var maxpop=0; for (var i in shelters) { var shelter=shelters[i]; if (numOfBuilding(shelter)>0) { maxpop+=numOfBuilding(shelter)*getBuildingAttribute(shelter,"maxPop"); } } return maxpop; } function updateShown() { var hasResources=Object.keys(resources).length>0; var hasLab=Object.keys(buildings).includes("lab"); var hasTpost=Object.keys(buildings).includes("trading post"); var hasShelter=false for (var i in shelters) { var shelter=shelters[i]; if (Object.keys(buildings).includes(shelter)) { hasShelter=true; break; } } $(".shelterRequired").toggle(hasShelter); $("#linkPopulation").toggle(hasShelter); $(".resourcesRequired").toggle(hasResources); $("#linkBuildings").toggle(hasResources); $("#linkResearch").toggle(hasLab); $(".researchRequired").toggle(hasLab); $("#linkTrading").toggle(hasTpost); } function incResource(name,amount=1) { if (name=="researchPoints") { if (researchPoints>maxResearchPoints()) { return; } researchPoints+=amount; if (researchPoints>maxResearchPoints()) { researchPoints-=amount; return; } researchPoints=tenthRound(researchPoints); updateResearchPointInfo(); updateResearchButtons(); } else { if (numResources>=maxResources()) { return; } if (resources[name]) { resources[name]+=amount; } else { resources[name]=amount; } numResources+=amount; if (numResources>maxResources()) { decResource(name,amount); return; } resources[name]=tenthRound(resources[name]); numResources=tenthRound(numResources); updateResourceInfo(); updateCraftButtons(); updateTradingButtons(); } updateShown(); } function decResource(name,amount=1) { if (resources[name]=ings[ing])) { enough=false; break; } } if (enough) { for (var ing in ings) { var amount=ings[ing]; decResource(ing,amount); } updateResourceInfo(); incBuilding(name); updateCraftButtons(); } } function capitalizeFirst(str) { return str.charAt(0).toUpperCase()+str.slice(1); } function updateResourceInfo() { $("#resources").html(""); for (var name in resources) { var capName=capitalizeFirst(name); $("#resources").append("

"+capName+": "+resources[name]+"

"); } $("#maxResources").text("Resources: ("+numResources+"/"+maxResources()+")"); } function updateBuildingInfo() { $("#buildings").html(""); for (var name in buildings) { var capName=capitalizeFirst(name); $("#buildings").append("

"+capName+": "+buildings[name]+"

"); } } function updateCraftButtons() { $("#craftButtons").html(""); for (var name in allBuildings) { if (name=="trading post" && buildings["trading post"]>0) { continue; } var ings=buildingCost(name); var show=true; var disabled=false; if (!unlockedBuildings.includes(name)) { for (var ing in ings) { if (!(resources[ing]>=(ings[ing]/2))) { show=false; break; } if (!(resources[ing]>=ings[ing])) { disabled=true; } } } else { for (var ing in ings) { if (!(resources[ing]>=ings[ing])) { disabled=true; } } } if (show) { if (!unlockedBuildings.includes(name)) { unlockedBuildings.push(name); } var btext=name+" ("; var i=0; var ingsize=Object.keys(ings).length; for (var ing in ings) { if (i==(ingsize-1)) { btext+=ings[ing]+" "+ing+")"; } else { btext+=ings[ing]+" "+ing+", " } i++; } if (disabled) { $("#craftButtons").append("
"); } else { $("#craftButtons").append("
"); } } } } function updatePopulation() { if(pop>0) { updateWorkerInfo(); } if (pop"+capitalizeFirst(name)+": "+rate+" gold"+"

"); $("#tabTrading").append(" "); $("#tabTrading").append("
"); $("#tabTrading").append(" "); $("#tabTrading").append(""); $("#tabTrading #sell1").prop("disabled",!canSell(name)).removeAttr("id"); $("#tabTrading #buy1").prop("disabled",resources["gold"]"+name+" ("+cost+" points)
"); } else { $("#researchButtons").append("
"); } } } } function updateResearchInfo() { $("#researches").html(""); for (var name in researches) { $("#researches").append("

"+name+": Level "+researches[name]+"

"); } } function applyResearches() { for (var research in researches) { var effects=allResearches[research].effects; var level=researches[research]; for (var effect in effects) { if (effect=="workerRate") { workerRate=0.5; workerRate+=effects[effect]*level; } } } } function research(name) { researchPoints-=allResearches[name].cost; if (researches[name]) { researches[name]+=1; } else { researches[name]=1; } updateResearchButtons(); updateResearchInfo(); applyResearches(); } function hire(type) { if (workers[type]) { workers[type]+=1; } else { workers[type]=1; } updateWorkerInfo(); } function fire(type) { if (workers[type]) { workers[type]-=1; } else { throw new Error("Cannot fire a never hired employee"); } updateWorkerInfo(); } function autoInc() { for (var worker in allWorkers) { workerAmount=workers[worker]; var amount=workerRate*workerAmount; if (amount>0) { if (worker=="scientist") { var usedMetal=Math.ceil(workerAmount*0.4); if (resources["metal"]>=usedMetal) { incResource(allWorkers[worker],amount); decResource("metal",usedMetal); } } else { incResource(allWorkers[worker],amount); } } } } function save() { gamestate={ pop:pop, workers:workers, resources:resources, numResources:numResources, buildings:buildings, unlockedBuildings:unlockedBuildings, researches:researches, unlockedResearch:unlockedResearch, researchPoints:researchPoints, } localStorage.setItem("game",JSON.stringify(gamestate)); } function load() { var gamestate=JSON.parse(localStorage.getItem("game")); if (gamestate==null) { return false; } pop=gamestate.pop; workers=gamestate.workers; resources=gamestate.resources; numResources=gamestate.numResources; buildings=gamestate.buildings; unlockedBuildings=gamestate.unlockedBuildings; researches=gamestate.researches; unlockedResearch=gamestate.unlockedResearch; researchPoints=gamestate.researchPoints; return true; } function reset() { localStorage.removeItem("game"); if (load()!=false) { alert("Unable to reset game"); } init(); } function init() { if (!load()) { pop=0; resources={}; numResources=0; buildings={}; unlockedBuildings=[]; researches={}; unlockedResearch=[]; researchPoints=0; workers={ lumberjack:0, miner:0, scientist:0 }; } setTab("main"); updateShown(); updateResourceInfo(); updateBuildingInfo(); updatePopulationInfo(); updateWorkerInfo(); updateCraftButtons(); updateResearchPointInfo(); updateResearchButtons(); updateResearchInfo(); applyResearches(); updateTradingButtons(); } function setTab(tab) { tab=capitalizeFirst(tab); if ($("#link"+tab).is(":visible")) { $("[id^='tab']").hide(); $("[id^='link']").removeClass("active"); $("#tab"+tab).show(); $("#link"+tab).addClass("active"); } } function canSell(name,amount=1) { var goldGotten=tradingRates[name]*amount; var spacesNeeded=goldGotten-amount; return maxResources()-numResources>=spacesNeeded; } function buy(name,amount=1) { decResource("gold",tradingRates[name]*amount); incResource(name,amount); } function sell(name,amount=1) { decResource(name,amount); incResource("gold",tradingRates[name]*amount); } $(document).ready(function() { initializeShelterArray(); init(); $(window).on("unload",function(e){ save(); }); window.setInterval(function() { updatePopulation(); }, 10000); window.setInterval(function() { autoInc(); }, 2000); });