Properties that can be computed efficiently are
This commit is contained in:
parent
f83769c9ab
commit
ef7eca07bf
218
main.js
218
main.js
@ -1,23 +1,18 @@
|
|||||||
var maxPop;
|
|
||||||
var pop;
|
var pop;
|
||||||
var working;
|
|
||||||
var resources;
|
var resources;
|
||||||
var maxResources;
|
|
||||||
var numResources;
|
var numResources;
|
||||||
var buildings;
|
var buildings;
|
||||||
var unlockedBuildings;
|
var unlockedBuildings;
|
||||||
var has_shelter;
|
|
||||||
var researches;
|
var researches;
|
||||||
var unlockedResearch;
|
var unlockedResearch;
|
||||||
var research_points;
|
var research_points;
|
||||||
var max_research_points;
|
|
||||||
var workers;
|
var workers;
|
||||||
var all_buildings={
|
var all_buildings={
|
||||||
hut:{
|
hut:{
|
||||||
ings:{
|
ings:{
|
||||||
wood:15
|
wood:15
|
||||||
},
|
},
|
||||||
build_effects:{
|
attributes:{
|
||||||
maxPop:2
|
maxPop:2
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -26,7 +21,7 @@ var all_buildings={
|
|||||||
wood:30,
|
wood:30,
|
||||||
metal:5
|
metal:5
|
||||||
},
|
},
|
||||||
build_effects:{
|
attributes:{
|
||||||
maxPop:4
|
maxPop:4
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -35,7 +30,7 @@ var all_buildings={
|
|||||||
wood:30,
|
wood:30,
|
||||||
metal:15
|
metal:15
|
||||||
},
|
},
|
||||||
build_effects:{
|
attributes:{
|
||||||
max_research_points:150
|
max_research_points:150
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -43,7 +38,7 @@ var all_buildings={
|
|||||||
ings:{
|
ings:{
|
||||||
wood:60
|
wood:60
|
||||||
},
|
},
|
||||||
build_effects: {
|
attributes: {
|
||||||
max_resources:100
|
max_resources:100
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -74,13 +69,70 @@ var trading_rates={
|
|||||||
wood:1,
|
wood:1,
|
||||||
metal:10
|
metal:10
|
||||||
};
|
};
|
||||||
|
var startingStorage=100;
|
||||||
|
var shelters=[];
|
||||||
|
function initializeShelterArray() {
|
||||||
|
for (var name in all_buildings) {
|
||||||
|
var building=all_buildings[name];
|
||||||
|
var attrbs=building.attributes;
|
||||||
|
if (attrbs) {
|
||||||
|
var maxpop=attrbs.maxPop;
|
||||||
|
if (maxpop) {
|
||||||
|
shelters.push(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function getBuildingAttribute(name,attrname) {
|
||||||
|
var attr=all_buildings[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) {
|
function tenthRound(number) {
|
||||||
return Math.round(number*10)/10;
|
return Math.round(number*10)/10;
|
||||||
}
|
}
|
||||||
|
function maxResources() {
|
||||||
|
var numStorehouses=numOfBuilding("storehouse");
|
||||||
|
var storehouseResources=getBuildingAttribute("storehouse","max_resources");
|
||||||
|
var extraStorage=numStorehouses*storehouseResources;
|
||||||
|
return startingStorage+extraStorage;
|
||||||
|
}
|
||||||
|
function max_research_points() {
|
||||||
|
var numLabs=numOfBuilding("labs");
|
||||||
|
var labRpoints=getBuildingAttribute("lab","max_research_points");
|
||||||
|
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() {
|
function updateShown() {
|
||||||
var has_resources=Object.keys(resources).length>0;
|
var has_resources=Object.keys(resources).length>0;
|
||||||
var has_lab=Object.keys(buildings).includes("lab");
|
var has_lab=Object.keys(buildings).includes("lab");
|
||||||
var has_tpost=Object.keys(buildings).includes("trading post");
|
var has_tpost=Object.keys(buildings).includes("trading post");
|
||||||
|
var has_shelter=false
|
||||||
|
for (var i in shelters) {
|
||||||
|
var shelter=shelters[i];
|
||||||
|
if (Object.keys(buildings).includes(shelter)) {
|
||||||
|
has_shelter=true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
$(".shelter_required").toggle(has_shelter);
|
$(".shelter_required").toggle(has_shelter);
|
||||||
$("#link_population").toggle(has_shelter);
|
$("#link_population").toggle(has_shelter);
|
||||||
$(".resources_required").toggle(has_resources);
|
$(".resources_required").toggle(has_resources);
|
||||||
@ -91,11 +143,11 @@ function updateShown() {
|
|||||||
}
|
}
|
||||||
function incResource(name,amount=1) {
|
function incResource(name,amount=1) {
|
||||||
if (name=="research_points") {
|
if (name=="research_points") {
|
||||||
if (research_points>max_research_points) {
|
if (research_points>max_research_points()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
research_points+=amount;
|
research_points+=amount;
|
||||||
if (research_points>max_research_points) {
|
if (research_points>max_research_points()) {
|
||||||
research_points-=amount;
|
research_points-=amount;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -103,7 +155,7 @@ function incResource(name,amount=1) {
|
|||||||
updateResearchPointInfo();
|
updateResearchPointInfo();
|
||||||
updateResearchButtons();
|
updateResearchButtons();
|
||||||
} else {
|
} else {
|
||||||
if (numResources>=maxResources) {
|
if (numResources>=maxResources()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (resources[name]) {
|
if (resources[name]) {
|
||||||
@ -112,7 +164,7 @@ function incResource(name,amount=1) {
|
|||||||
resources[name]=amount;
|
resources[name]=amount;
|
||||||
}
|
}
|
||||||
numResources+=amount;
|
numResources+=amount;
|
||||||
if (numResources>maxResources) {
|
if (numResources>maxResources()) {
|
||||||
decResource(name,amount);
|
decResource(name,amount);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -142,20 +194,9 @@ function incBuilding(name) {
|
|||||||
} else {
|
} else {
|
||||||
buildings[name]=1;
|
buildings[name]=1;
|
||||||
}
|
}
|
||||||
var effects=all_buildings[name]["build_effects"];
|
if (shelters.includes(name)) {
|
||||||
for (effect in effects) {
|
updatePopulationInfo();
|
||||||
if (effect=="maxPop") {
|
updateWorkerInfo();
|
||||||
maxPop+=effects[effect];
|
|
||||||
has_shelter=true;
|
|
||||||
updatePopulationInfo();
|
|
||||||
updateWorkerInfo();
|
|
||||||
}
|
|
||||||
if (effect=="max_resources") {
|
|
||||||
maxResources+=effects[effect];
|
|
||||||
}
|
|
||||||
if (effect=="max_research_points") {
|
|
||||||
max_research_points+=effects[effect];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
updateShown();
|
updateShown();
|
||||||
updateBuildingInfo();
|
updateBuildingInfo();
|
||||||
@ -202,7 +243,7 @@ function updateResourceInfo() {
|
|||||||
var capName=capitalizeFirst(name);
|
var capName=capitalizeFirst(name);
|
||||||
$("#resources").append("<p>"+capName+": "+resources[name]+"</p>");
|
$("#resources").append("<p>"+capName+": "+resources[name]+"</p>");
|
||||||
}
|
}
|
||||||
$("#max_resources").text("Resources: ("+numResources+"/"+maxResources+")");
|
$("#max_resources").text("Resources: ("+numResources+"/"+maxResources()+")");
|
||||||
}
|
}
|
||||||
function updateBuildingInfo() {
|
function updateBuildingInfo() {
|
||||||
$("#buildings").html("");
|
$("#buildings").html("");
|
||||||
@ -264,40 +305,46 @@ function updatePopulation() {
|
|||||||
if(pop>0) {
|
if(pop>0) {
|
||||||
updateWorkerInfo();
|
updateWorkerInfo();
|
||||||
}
|
}
|
||||||
if (pop<maxPop) {
|
if (pop<maxPop()) {
|
||||||
pop+=1;
|
pop+=1;
|
||||||
updateWorkerInfo();
|
updateWorkerInfo();
|
||||||
}
|
}
|
||||||
updatePopulationInfo();
|
updatePopulationInfo();
|
||||||
}
|
}
|
||||||
function updatePopulationInfo() {
|
function updatePopulationInfo() {
|
||||||
$("#pop").text("Population: "+pop+"/"+maxPop);
|
$("#pop").text("Population: "+pop+"/"+maxPop());
|
||||||
}
|
}
|
||||||
function updateWorkerInfo() {
|
function updateWorkerInfo() {
|
||||||
$("#work_pop").text("Working: "+working+"/"+pop);
|
var working=0;
|
||||||
$(".hire").attr("disabled",working==pop);
|
for (var worker in allWorkers) {
|
||||||
for (var worker in Object.keys(allWorkers)) {
|
|
||||||
var workerAmount=workers[worker];
|
var workerAmount=workers[worker];
|
||||||
if (workerAmount) {
|
if (workerAmount) {
|
||||||
workerAmount=0;
|
workerAmount=0;
|
||||||
}
|
}
|
||||||
|
working+=workerAmount;
|
||||||
$("#fire_"+worker).attr("disabled",workerAmount==0);
|
$("#fire_"+worker).attr("disabled",workerAmount==0);
|
||||||
$("#num_"+worker+"s").text(capitalizeFirst(worker)+"s: "+workerAmount);
|
$("#num_"+worker+"s").text(capitalizeFirst(worker)+"s: "+workerAmount);
|
||||||
}
|
}
|
||||||
|
$("#work_pop").text("Working: "+working+"/"+pop);
|
||||||
|
$(".hire").attr("disabled",working==pop);
|
||||||
}
|
}
|
||||||
function research(name) {
|
function updateTradingButtons() {
|
||||||
research_points-=all_researches[name].cost;
|
$("#tab_trading").html("");
|
||||||
if (researches[name]) {
|
for (var name in trading_rates) {
|
||||||
researches[name]+=1;
|
var rate=trading_rates[name];
|
||||||
} else {
|
$("#tab_trading").append("<p>"+capitalizeFirst(name)+": "+rate+" gold"+"</p>");
|
||||||
researches[name]=1;
|
$("#tab_trading").append("<button id=sell1 onclick=\"sell('"+name+"')\">Sell 1</button> ");
|
||||||
|
$("#tab_trading").append("<button id=buy1 onclick=\"buy('"+name+"')\">Buy 1</button><br>");
|
||||||
|
$("#tab_trading").append("<button id=sell10 onclick=\"sell('"+name+"',10)\">Sell 10</button> ");
|
||||||
|
$("#tab_trading").append("<button id=buy10 onclick=\"buy('"+name+"',10)\">Buy 10</button>");
|
||||||
|
$("#tab_trading #sell1").prop("disabled",!canSell(name)).removeAttr("id");
|
||||||
|
$("#tab_trading #buy1").prop("disabled",resources["gold"]<trading_rates[name]).removeAttr("id");
|
||||||
|
$("#tab_trading #sell10").prop("disabled",!canSell(name,10)).removeAttr("id");
|
||||||
|
$("#tab_trading #buy10").prop("disabled",resources["gold"]<trading_rates[name]*10).removeAttr("id");
|
||||||
}
|
}
|
||||||
updateResearchButtons();
|
|
||||||
updateResearchInfo();
|
|
||||||
applyResearches();
|
|
||||||
}
|
}
|
||||||
function updateResearchPointInfo() {
|
function updateResearchPointInfo() {
|
||||||
$("#research_points").text("Research points: "+research_points+"/"+max_research_points);
|
$("#research_points").text("Research points: "+research_points+"/"+max_research_points());
|
||||||
}
|
}
|
||||||
function updateResearchButtons() {
|
function updateResearchButtons() {
|
||||||
$("#research_buttons").html("");
|
$("#research_buttons").html("");
|
||||||
@ -332,13 +379,41 @@ function updateResearchButtons() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
function updateResearchInfo() {
|
||||||
|
$("#researches").html("");
|
||||||
|
for (var name in researches) {
|
||||||
|
$("#researches").append("<p>"+name+": Level "+researches[name]+"</p>");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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 research(name) {
|
||||||
|
research_points-=all_researches[name].cost;
|
||||||
|
if (researches[name]) {
|
||||||
|
researches[name]+=1;
|
||||||
|
} else {
|
||||||
|
researches[name]=1;
|
||||||
|
}
|
||||||
|
updateResearchButtons();
|
||||||
|
updateResearchInfo();
|
||||||
|
applyResearches();
|
||||||
|
}
|
||||||
function hire(type) {
|
function hire(type) {
|
||||||
if (workers[type]) {
|
if (workers[type]) {
|
||||||
workers[type]+=1;
|
workers[type]+=1;
|
||||||
} else {
|
} else {
|
||||||
workers[type]=1;
|
workers[type]=1;
|
||||||
}
|
}
|
||||||
working+=1;
|
|
||||||
updateWorkerInfo();
|
updateWorkerInfo();
|
||||||
}
|
}
|
||||||
function fire(type) {
|
function fire(type) {
|
||||||
@ -347,11 +422,10 @@ function fire(type) {
|
|||||||
} else {
|
} else {
|
||||||
throw new Error("Cannot fire a never hired employee");
|
throw new Error("Cannot fire a never hired employee");
|
||||||
}
|
}
|
||||||
working-=1;
|
|
||||||
updateWorkerInfo();
|
updateWorkerInfo();
|
||||||
}
|
}
|
||||||
function autoInc() {
|
function autoInc() {
|
||||||
for (var worker in Object.keys(allWorkers)) {
|
for (var worker in allWorkers) {
|
||||||
worker_amount=workers[worker];
|
worker_amount=workers[worker];
|
||||||
var amount=worker_rate*worker_amount;
|
var amount=worker_rate*worker_amount;
|
||||||
if (amount>0) {
|
if (amount>0) {
|
||||||
@ -369,20 +443,15 @@ function autoInc() {
|
|||||||
}
|
}
|
||||||
function save() {
|
function save() {
|
||||||
gamestate={
|
gamestate={
|
||||||
maxPop:maxPop,
|
|
||||||
pop:pop,
|
pop:pop,
|
||||||
working:working,
|
|
||||||
workers:workers,
|
workers:workers,
|
||||||
resources:resources,
|
resources:resources,
|
||||||
maxResources:maxResources,
|
|
||||||
numResources:numResources,
|
numResources:numResources,
|
||||||
buildings:buildings,
|
buildings:buildings,
|
||||||
unlockedBuildings:unlockedBuildings,
|
unlockedBuildings:unlockedBuildings,
|
||||||
has_shelter:has_shelter,
|
|
||||||
researches:researches,
|
researches:researches,
|
||||||
unlockedResearch:unlockedResearch,
|
unlockedResearch:unlockedResearch,
|
||||||
research_points:research_points,
|
research_points:research_points,
|
||||||
max_research_points:max_research_points
|
|
||||||
}
|
}
|
||||||
localStorage.setItem("game",JSON.stringify(gamestate));
|
localStorage.setItem("game",JSON.stringify(gamestate));
|
||||||
}
|
}
|
||||||
@ -391,23 +460,17 @@ function load() {
|
|||||||
if (gamestate==null) {
|
if (gamestate==null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
maxPop=gamestate.maxPop;
|
|
||||||
pop=gamestate.pop;
|
pop=gamestate.pop;
|
||||||
working=gamestate.working;
|
|
||||||
workers=gamestate.workers;
|
workers=gamestate.workers;
|
||||||
resources=gamestate.resources;
|
resources=gamestate.resources;
|
||||||
maxResources=gamestate.maxResources;
|
|
||||||
numResources=gamestate.numResources;
|
numResources=gamestate.numResources;
|
||||||
buildings=gamestate.buildings;
|
buildings=gamestate.buildings;
|
||||||
unlockedBuildings=gamestate.unlockedBuildings;
|
unlockedBuildings=gamestate.unlockedBuildings;
|
||||||
has_shelter=gamestate.has_shelter;
|
|
||||||
researches=gamestate.researches;
|
researches=gamestate.researches;
|
||||||
unlockedResearch=gamestate.unlockedResearch;
|
unlockedResearch=gamestate.unlockedResearch;
|
||||||
research_points=gamestate.research_points;
|
research_points=gamestate.research_points;
|
||||||
max_research_points=gamestate.max_research_points;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function reset() {
|
function reset() {
|
||||||
localStorage.removeItem("game");
|
localStorage.removeItem("game");
|
||||||
if (load()!=false) {
|
if (load()!=false) {
|
||||||
@ -417,19 +480,14 @@ function reset() {
|
|||||||
}
|
}
|
||||||
function init() {
|
function init() {
|
||||||
if (!load()) {
|
if (!load()) {
|
||||||
maxPop=0;
|
|
||||||
pop=0;
|
pop=0;
|
||||||
working=0;
|
|
||||||
resources={};
|
resources={};
|
||||||
maxResources=100;
|
|
||||||
numResources=0;
|
numResources=0;
|
||||||
buildings={};
|
buildings={};
|
||||||
unlockedBuildings=[];
|
unlockedBuildings=[];
|
||||||
has_shelter=false;
|
|
||||||
researches={};
|
researches={};
|
||||||
unlockedResearch=[];
|
unlockedResearch=[];
|
||||||
research_points=0;
|
research_points=0;
|
||||||
max_research_points=0;
|
|
||||||
workers={
|
workers={
|
||||||
lumberjack:0,
|
lumberjack:0,
|
||||||
miner:0,
|
miner:0,
|
||||||
@ -457,28 +515,10 @@ function set_tab(tab) {
|
|||||||
$("#link_"+tab).addClass("active");
|
$("#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("<p>"+name+": Level "+researches[name]+"</p>");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function canSell(name,amount=1) {
|
function canSell(name,amount=1) {
|
||||||
var goldGotten=trading_rates[name]*amount;
|
var goldGotten=trading_rates[name]*amount;
|
||||||
var spacesNeeded=goldGotten-amount;
|
var spacesNeeded=goldGotten-amount;
|
||||||
return maxResources-numResources>=spacesNeeded;
|
return maxResources()-numResources>=spacesNeeded;
|
||||||
}
|
}
|
||||||
function buy(name,amount=1) {
|
function buy(name,amount=1) {
|
||||||
decResource("gold",trading_rates[name]*amount);
|
decResource("gold",trading_rates[name]*amount);
|
||||||
@ -488,22 +528,8 @@ function sell(name,amount=1) {
|
|||||||
decResource(name,amount);
|
decResource(name,amount);
|
||||||
incResource("gold",trading_rates[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("<p>"+capitalizeFirst(name)+": "+rate+" gold"+"</p>");
|
|
||||||
$("#tab_trading").append("<button id=sell1 onclick=\"sell('"+name+"')\">Sell 1</button> ");
|
|
||||||
$("#tab_trading").append("<button id=buy1 onclick=\"buy('"+name+"')\">Buy 1</button><br>");
|
|
||||||
$("#tab_trading").append("<button id=sell10 onclick=\"sell('"+name+"',10)\">Sell 10</button> ");
|
|
||||||
$("#tab_trading").append("<button id=buy10 onclick=\"buy('"+name+"',10)\">Buy 10</button>");
|
|
||||||
$("#tab_trading #sell1").prop("disabled",!canSell(name)).removeAttr("id");
|
|
||||||
$("#tab_trading #buy1").prop("disabled",resources["gold"]<trading_rates[name]).removeAttr("id");
|
|
||||||
$("#tab_trading #sell10").prop("disabled",!canSell(name,10)).removeAttr("id");
|
|
||||||
$("#tab_trading #buy10").prop("disabled",resources["gold"]<trading_rates[name]*10).removeAttr("id");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
|
initializeShelterArray();
|
||||||
init();
|
init();
|
||||||
$(window).on("unload",function(e){
|
$(window).on("unload",function(e){
|
||||||
save();
|
save();
|
||||||
|
Loading…
Reference in New Issue
Block a user