var maxPop; var pop; var working; var resources; var maxResources; var numResources; var buildings; var unlockedBuildings; var has_shelter; var researches; var unlockedResearch; var research_points; var max_research_points; var workers; var all_buildings={ hut:{ ings:{ wood:15 }, build_effects:{ maxPop:2 } }, house:{ ings:{ wood:30, metal:5 }, build_effects:{ maxPop:4 } }, lab:{ ings:{ wood:30, metal:15 }, build_effects:{ max_research_points:150 } }, storehouse:{ ings:{ wood:60 }, build_effects: { max_resources:100 } }, "trading post":{ ings:{ wood:100, metal:50 } } }; var allWorkers={ lumberjack:"wood", miner:"metal", scientist:"research_points" }; var worker_rate=0.1; var all_researches={ "Faster workers":{ maxLevel:6, cost:100, effects:{ worker_rate:0.2 } } }; var multiplier=1.17; var trading_rates={ wood:1, metal:10 }; function tenthRound(number) { return Math.round(number*10)/10; } function updateShown() { var has_resources=Object.keys(resources).length>0; var has_lab=Object.keys(buildings).includes("lab"); var has_tpost=Object.keys(buildings).includes("trading post"); $(".shelter_required").toggle(has_shelter); $("#link_population").toggle(has_shelter); $(".resources_required").toggle(has_resources); $("#link_buildings").toggle(has_resources); $("#link_research").toggle(has_lab); $(".research_required").toggle(has_lab); $("#link_trading").toggle(has_tpost); } function incResource(name,amount=1) { if (name=="research_points") { if (research_points>max_research_points) { return; } research_points+=amount; if (research_points>max_research_points) { research_points-=amount; return; } research_points=tenthRound(research_points); 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]+"

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

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

"); } } function updateCraftButtons() { $("#craft_buttons").html(""); for (var name in all_buildings) { 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) { $("#craft_buttons").append("
"); } else { $("#craft_buttons").append("
"); } } } } function updatePopulation() { if(pop>0) { updateWorkerInfo(); } if (pop"+name+" ("+cost+" points)
"); } else { $("#research_buttons").append("
"); } } } } function hire(type) { if (workers[type]) { workers[type]+=1; } else { workers[type]=1; } working+=1; updateWorkerInfo(); } function fire(type) { if (workers[type]) { workers[type]-=1; } else { throw new Error("Cannot fire a never hired employee"); } working-=1; updateWorkerInfo(); } function autoInc() { for (var worker in Object.keys(allWorkers)) { worker_amount=workers[worker]; var amount=worker_rate*worker_amount; if (amount>0) { if (worker=="scientist") { var usedMetal=Math.ceil(worker_amount*0.4); if (resources["metal"]>=usedMetal) { incResource(allWorkers[worker],amount); decResource("metal",usedMetal); } } else { incResource(allWorkers[worker],amount); } } } } function save() { gamestate={ maxPop:maxPop, pop:pop, working:working, workers:workers, resources:resources, maxResources:maxResources, numResources:numResources, buildings:buildings, unlockedBuildings:unlockedBuildings, has_shelter:has_shelter, researches:researches, unlockedResearch:unlockedResearch, research_points:research_points, max_research_points:max_research_points } localStorage.setItem("game",JSON.stringify(gamestate)); } function load() { var gamestate=JSON.parse(localStorage.getItem("game")); if (gamestate==null) { return false; } maxPop=gamestate.maxPop; pop=gamestate.pop; working=gamestate.working; workers=gamestate.workers; resources=gamestate.resources; maxResources=gamestate.maxResources; numResources=gamestate.numResources; buildings=gamestate.buildings; unlockedBuildings=gamestate.unlockedBuildings; has_shelter=gamestate.has_shelter; researches=gamestate.researches; unlockedResearch=gamestate.unlockedResearch; research_points=gamestate.research_points; max_research_points=gamestate.max_research_points; return true; } function reset() { localStorage.removeItem("game"); if (load()!=false) { alert("Unable to reset game"); } init(); } function init() { if (!load()) { maxPop=0; pop=0; working=0; resources={}; maxResources=100; numResources=0; buildings={}; unlockedBuildings=[]; has_shelter=false; researches={}; unlockedResearch=[]; research_points=0; max_research_points=0; workers={ lumberjack:0, miner:0, scientist:0 }; } set_tab("main"); updateShown(); updateResourceInfo(); updateBuildingInfo(); updatePopulationInfo(); updateWorkerInfo(); updateCraftButtons(); updateResearchPointInfo(); updateResearchButtons(); updateResearchInfo(); applyResearches(); updateTradingButtons(); } function set_tab(tab) { if ($("#link_"+tab).is(":visible")) { $("[id^='tab_']").hide(); $("[id^='link_']").removeClass("active"); $("#tab_"+tab).show(); $("#link_"+tab).addClass("active"); } } function applyResearches() { for (var research in researches) { var effects=all_researches[research].effects; var level=researches[research]; for (var effect in effects) { if (effect=="worker_rate") { worker_rate=0.5; worker_rate+=effects[effect]*level; } } } } function updateResearchInfo() { $("#researches").html(""); for (var name in researches) { $("#researches").append("

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

"); } } function canSell(name,amount=1) { var goldGotten=trading_rates[name]*amount; var spacesNeeded=goldGotten-amount; return maxResources-numResources>=spacesNeeded; } function buy(name,amount=1) { decResource("gold",trading_rates[name]*amount); incResource(name,amount); } function sell(name,amount=1) { decResource(name,amount); incResource("gold",trading_rates[name]*amount); } function updateTradingButtons() { $("#tab_trading").html(""); for (var name in trading_rates) { var rate=trading_rates[name]; $("#tab_trading").append("

"+capitalizeFirst(name)+": "+rate+" gold"+"

"); $("#tab_trading").append(" "); $("#tab_trading").append("
"); $("#tab_trading").append(" "); $("#tab_trading").append(""); $("#tab_trading #sell1").prop("disabled",!canSell(name)).removeAttr("id"); $("#tab_trading #buy1").prop("disabled",resources["gold"]