incgame/main.js

544 lines
13 KiB
JavaScript
Raw Normal View History

2018-10-14 18:53:56 -05:00
var pop;
var resources;
var numResources;
var buildings;
var unlockedBuildings;
var researches;
var unlockedResearch;
var research_points;
var workers;
2018-09-30 13:30:40 -05:00
var all_buildings={
hut:{
ings:{
wood:15
},
attributes:{
2018-09-30 13:30:40 -05:00
maxPop:2
}
},
house:{
ings:{
wood:30,
metal:5
},
attributes:{
2018-09-30 13:30:40 -05:00
maxPop:4
}
},
lab:{
ings:{
wood:30,
metal:15
},
attributes:{
2018-09-30 13:30:40 -05:00
max_research_points:150
}
},
storehouse:{
ings:{
wood:60
2018-09-30 13:30:40 -05:00
},
attributes: {
max_resources:100
2018-09-30 13:30:40 -05:00
}
2018-10-06 11:34:12 -05:00
},
"trading post":{
ings:{
wood:100,
metal:50
}
2018-09-30 13:30:40 -05:00
}
};
2018-10-14 19:03:54 -05:00
var allWorkers={
2018-09-30 13:30:40 -05:00
lumberjack:"wood",
miner:"metal",
scientist:"research_points"
};
2018-10-06 11:34:12 -05:00
var worker_rate=0.1;
2018-09-30 13:30:40 -05:00
var all_researches={
2018-10-06 11:34:12 -05:00
"Faster workers":{
maxLevel:6,
2018-09-30 13:30:40 -05:00
cost:100,
effects:{
2018-10-06 11:34:12 -05:00
worker_rate:0.2
2018-09-30 13:30:40 -05:00
}
}
};
var multiplier=1.17;
2018-10-06 11:34:12 -05:00
var trading_rates={
wood:1,
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;
}
2018-10-06 11:34:12 -05:00
function tenthRound(number) {
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;
}
2018-09-30 13:30:40 -05:00
function updateShown() {
var has_resources=Object.keys(resources).length>0;
2018-10-06 11:34:12 -05:00
var has_lab=Object.keys(buildings).includes("lab");
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;
}
}
2018-09-30 13:30:40 -05:00
$(".shelter_required").toggle(has_shelter);
$("#link_population").toggle(has_shelter);
$(".resources_required").toggle(has_resources);
$("#link_buildings").toggle(has_resources);
2018-10-06 11:34:12 -05:00
$("#link_research").toggle(has_lab);
$(".research_required").toggle(has_lab);
$("#link_trading").toggle(has_tpost);
2018-09-30 13:30:40 -05:00
}
function incResource(name,amount=1) {
if (name=="research_points") {
if (research_points>max_research_points()) {
2018-09-30 13:30:40 -05:00
return;
}
research_points+=amount;
if (research_points>max_research_points()) {
2018-09-30 13:30:40 -05:00
research_points-=amount;
return;
}
2018-10-06 11:34:12 -05:00
research_points=tenthRound(research_points);
2018-09-30 13:30:40 -05:00
updateResearchPointInfo();
updateResearchButtons();
} else {
if (numResources>=maxResources()) {
2018-09-30 13:30:40 -05:00
return;
}
if (resources[name]) {
resources[name]+=amount;
} else {
resources[name]=amount;
}
numResources+=amount;
if (numResources>maxResources()) {
2018-09-30 13:30:40 -05:00
decResource(name,amount);
return;
}
2018-10-06 11:34:12 -05:00
resources[name]=tenthRound(resources[name]);
numResources=tenthRound(numResources);
2018-09-30 13:30:40 -05:00
updateResourceInfo();
updateCraftButtons();
2018-10-06 11:34:12 -05:00
updateTradingButtons();
2018-09-30 13:30:40 -05:00
}
updateShown();
}
2018-10-06 11:34:12 -05:00
function decResource(name,amount=1) {
2018-09-30 13:30:40 -05:00
if (resources[name]<amount) {
return;
}
numResources-=amount;
resources[name]-=amount;
2018-10-06 11:34:12 -05:00
resources[name]=tenthRound(resources[name]);
numResources=tenthRound(numResources);
2018-09-30 13:30:40 -05:00
updateResourceInfo();
updateCraftButtons();
2018-10-06 11:34:12 -05:00
updateTradingButtons();
2018-09-30 13:30:40 -05:00
}
function incBuilding(name) {
if (buildings[name]) {
buildings[name]+=1;
} else {
buildings[name]=1;
}
if (shelters.includes(name)) {
updatePopulationInfo();
updateWorkerInfo();
2018-09-30 13:30:40 -05:00
}
updateShown();
updateBuildingInfo();
}
function buildingCost(name) {
var newIngs={};
var ings=all_buildings[name].ings;
var building_amount=buildings[name];
if (building_amount==undefined) {
building_amount=0;
}
for (var ing in ings) {
var ing_amount=ings[ing];
ing_amount=Math.floor(ing_amount*(multiplier**building_amount));
newIngs[ing]=ing_amount;
}
return newIngs;
}
function craft(name) {
var ings=buildingCost(name);
var enough=true;
for (var ing in ings) {
if (!(resources[ing]>=ings[ing])) {
enough=false;
break;
}
}
if (enough) {
for (var ing in ings) {
var amount=ings[ing];
decResource(ing,amount);
}
updateResourceInfo();
incBuilding(name);
2018-10-06 11:34:12 -05:00
updateCraftButtons();
2018-09-30 13:30:40 -05:00
}
}
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("<p>"+capName+": "+resources[name]+"</p>");
}
$("#max_resources").text("Resources: ("+numResources+"/"+maxResources()+")");
2018-09-30 13:30:40 -05:00
}
function updateBuildingInfo() {
$("#buildings").html("");
for (var name in buildings) {
var capName=capitalizeFirst(name);
$("#buildings").append("<p>"+capName+": "+buildings[name]+"</p>");
}
}
function updateCraftButtons() {
$("#craft_buttons").html("");
for (var name in all_buildings) {
2018-10-06 11:34:12 -05:00
if (name=="trading post" && buildings["trading post"]>0) {
continue;
}
2018-09-30 13:30:40 -05:00
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) {
2018-10-06 11:34:12 -05:00
$("#craft_buttons").append("<button disabled onclick=\"craft('"+name+"')\">Craft a "+btext+"</button><br>");
2018-09-30 13:30:40 -05:00
} else {
2018-10-06 11:34:12 -05:00
$("#craft_buttons").append("<button onclick=\"craft('"+name+"')\">Craft a "+btext+"</button><br>");
2018-09-30 13:30:40 -05:00
}
}
}
}
function updatePopulation() {
if(pop>0) {
updateWorkerInfo();
}
if (pop<maxPop()) {
2018-09-30 13:30:40 -05:00
pop+=1;
updateWorkerInfo();
}
updatePopulationInfo();
}
function updatePopulationInfo() {
$("#pop").text("Population: "+pop+"/"+maxPop());
2018-09-30 13:30:40 -05:00
}
function updateWorkerInfo() {
var working=0;
for (var worker in allWorkers) {
2018-10-14 19:03:54 -05:00
var workerAmount=workers[worker];
if (workerAmount) {
workerAmount=0;
}
working+=workerAmount;
2018-10-14 19:03:54 -05:00
$("#fire_"+worker).attr("disabled",workerAmount==0);
$("#num_"+worker+"s").text(capitalizeFirst(worker)+"s: "+workerAmount);
2018-09-30 13:30:40 -05:00
}
$("#work_pop").text("Working: "+working+"/"+pop);
$(".hire").attr("disabled",working==pop);
2018-09-30 13:30:40 -05:00
}
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>&nbsp");
$("#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>&nbsp");
$("#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");
2018-09-30 13:30:40 -05:00
}
}
function updateResearchPointInfo() {
$("#research_points").text("Research points: "+research_points+"/"+max_research_points());
2018-09-30 13:30:40 -05:00
}
function updateResearchButtons() {
$("#research_buttons").html("");
for (var name in all_researches) {
var research=all_researches[name];
var cost=research.cost;
var maxLevel=research.maxLevel;
var show=true;
var disabled=false;
if (!unlockedResearch.includes(name)) {
if (research_points<cost/2) {
show=false;
} else if (research_points<cost) {
disabled=true;
}
} else {
if (researches[name]==maxLevel) {
show=false
} else if (research_points<cost) {
disabled=true;
}
}
if (show) {
if (!unlockedResearch.includes(name)) {
unlockedResearch.push(name);
}
if (disabled) {
$("#research_buttons").append("<button disabled onclick=\"research('"+name+"')\">"+name+" ("+cost+" points)</button><br>");
} else {
$("#research_buttons").append("<button onclick=\"research('"+name+"')\">"+name+" ("+cost+" points)</button><br>");
}
}
}
}
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();
}
2018-09-30 13:30:40 -05:00
function hire(type) {
2018-10-14 19:03:54 -05:00
if (workers[type]) {
workers[type]+=1;
} else {
workers[type]=1;
}
2018-09-30 13:30:40 -05:00
updateWorkerInfo();
}
function fire(type) {
2018-10-14 19:03:54 -05:00
if (workers[type]) {
workers[type]-=1;
} else {
throw new Error("Cannot fire a never hired employee");
}
2018-09-30 13:30:40 -05:00
updateWorkerInfo();
}
function autoInc() {
for (var worker in allWorkers) {
2018-09-30 13:30:40 -05:00
worker_amount=workers[worker];
2018-10-06 11:34:12 -05:00
var amount=worker_rate*worker_amount;
2018-09-30 13:30:40 -05:00
if (amount>0) {
if (worker=="scientist") {
var usedMetal=Math.ceil(worker_amount*0.4);
if (resources["metal"]>=usedMetal) {
2018-10-14 19:03:54 -05:00
incResource(allWorkers[worker],amount);
2018-09-30 13:30:40 -05:00
decResource("metal",usedMetal);
}
} else {
2018-10-14 19:03:54 -05:00
incResource(allWorkers[worker],amount);
2018-09-30 13:30:40 -05:00
}
}
}
}
function save() {
gamestate={
pop:pop,
workers:workers,
resources:resources,
numResources:numResources,
buildings:buildings,
unlockedBuildings:unlockedBuildings,
researches:researches,
unlockedResearch:unlockedResearch,
research_points:research_points,
}
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;
research_points=gamestate.research_points;
return true;
}
function reset() {
localStorage.removeItem("game");
if (load()!=false) {
alert("Unable to reset game");
}
init();
}
function init() {
if (!load()) {
pop=0;
resources={};
2018-10-14 18:53:56 -05:00
numResources=0;
2018-09-30 13:30:40 -05:00
buildings={};
unlockedBuildings=[];
researches={};
unlockedResearch=[];
research_points=0;
workers={
lumberjack:0,
miner:0,
scientist:0
};
}
set_tab("main");
updateShown();
updateResourceInfo();
updateBuildingInfo();
updatePopulationInfo();
updateWorkerInfo();
updateCraftButtons();
updateResearchPointInfo();
updateResearchButtons();
updateResearchInfo();
applyResearches();
2018-10-06 11:34:12 -05:00
updateTradingButtons();
2018-09-30 13:30:40 -05:00
}
function set_tab(tab) {
if ($("#link_"+tab).is(":visible")) {
$("[id^='tab_']").hide();
$("[id^='link_']").removeClass("active");
$("#tab_"+tab).show();
$("#link_"+tab).addClass("active");
}
}
2018-10-06 11:34:12 -05:00
function canSell(name,amount=1) {
var goldGotten=trading_rates[name]*amount;
var spacesNeeded=goldGotten-amount;
return maxResources()-numResources>=spacesNeeded;
2018-10-06 11:34:12 -05:00
}
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);
}
2018-09-30 13:30:40 -05:00
$(document).ready(function() {
initializeShelterArray();
2018-09-30 13:30:40 -05:00
init();
$(window).on("unload",function(e){
save();
});
window.setInterval(function() {
updatePopulation();
}, 10000);
window.setInterval(function() {
autoInc();
}, 2000);
});