2019-06-23 10:40:33 -05:00
|
|
|
|
2018-12-23 16:05:00 -06:00
|
|
|
class Gamestate {
|
|
|
|
constructor() {
|
|
|
|
this.pop=0,
|
|
|
|
this.resources={},
|
|
|
|
this.numResources=0,
|
|
|
|
this.buildings={},
|
|
|
|
this.unlockedBuildings=[],
|
|
|
|
this.researches={},
|
|
|
|
this.unlockedResearch=[],
|
|
|
|
this.researchPoints=0,
|
|
|
|
this.workers={
|
|
|
|
lumberjack:0,
|
|
|
|
miner:0,
|
|
|
|
scientist:0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var gamestate=new Gamestate();
|
2018-10-15 14:19:42 -05:00
|
|
|
var allBuildings={
|
2018-09-30 13:30:40 -05:00
|
|
|
hut:{
|
|
|
|
ings:{
|
|
|
|
wood:15
|
|
|
|
},
|
2018-10-15 13:54:23 -05:00
|
|
|
attributes:{
|
2018-09-30 13:30:40 -05:00
|
|
|
maxPop:2
|
|
|
|
}
|
|
|
|
},
|
|
|
|
house:{
|
|
|
|
ings:{
|
|
|
|
wood:30,
|
|
|
|
metal:5
|
|
|
|
},
|
2018-10-15 13:54:23 -05:00
|
|
|
attributes:{
|
2018-09-30 13:30:40 -05:00
|
|
|
maxPop:4
|
|
|
|
}
|
|
|
|
},
|
|
|
|
lab:{
|
|
|
|
ings:{
|
|
|
|
wood:30,
|
|
|
|
metal:15
|
|
|
|
},
|
2018-10-15 13:54:23 -05:00
|
|
|
attributes:{
|
2018-10-15 14:19:42 -05:00
|
|
|
maxResearchPoints:100
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
storehouse:{
|
|
|
|
ings:{
|
2018-10-13 16:19:07 -05:00
|
|
|
wood:60
|
2018-09-30 13:30:40 -05:00
|
|
|
},
|
2018-10-15 13:54:23 -05:00
|
|
|
attributes: {
|
2018-10-15 14:19:42 -05:00
|
|
|
maxResources: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-10-16 09:18:10 -05:00
|
|
|
},
|
|
|
|
farm:{
|
|
|
|
ings:{
|
|
|
|
wood:30,
|
|
|
|
metal:15
|
|
|
|
},
|
|
|
|
attributes:{
|
|
|
|
maxFarmers:2
|
|
|
|
}
|
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",
|
2018-10-16 09:35:19 -05:00
|
|
|
scientist:"researchPoints",
|
|
|
|
farmer:"food"
|
2018-09-30 13:30:40 -05:00
|
|
|
};
|
2018-10-15 14:19:42 -05:00
|
|
|
var workerRate=0.1;
|
|
|
|
var allResearches={
|
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-15 14:19:42 -05:00
|
|
|
workerRate:0.2
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
2018-10-16 09:18:10 -05:00
|
|
|
},
|
|
|
|
"Better Yield": {
|
|
|
|
maxLevel:5,
|
|
|
|
cost:120,
|
|
|
|
effects:{
|
|
|
|
yield:3
|
|
|
|
}
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
var multiplier=1.17;
|
2018-10-15 14:19:42 -05:00
|
|
|
var tradingRates={
|
2018-10-06 11:34:12 -05:00
|
|
|
wood:1,
|
2018-10-16 09:37:04 -05:00
|
|
|
food:5,
|
2018-10-06 11:34:12 -05:00
|
|
|
metal:10
|
|
|
|
};
|
2018-10-15 13:54:23 -05:00
|
|
|
var shelters=[];
|
2018-10-16 09:18:10 -05:00
|
|
|
var startingStorage=100;
|
|
|
|
var startingYield=3;
|
2018-10-15 13:54:23 -05:00
|
|
|
function initializeShelterArray() {
|
2018-10-15 14:19:42 -05:00
|
|
|
for (var name in allBuildings) {
|
|
|
|
var building=allBuildings[name];
|
2018-10-15 13:54:23 -05:00
|
|
|
var attrbs=building.attributes;
|
|
|
|
if (attrbs) {
|
|
|
|
var maxpop=attrbs.maxPop;
|
|
|
|
if (maxpop) {
|
|
|
|
shelters.push(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-16 09:18:10 -05:00
|
|
|
function buildingAttribute(name,attrname) {
|
2018-10-15 14:19:42 -05:00
|
|
|
var attr=allBuildings[name].attributes[attrname];
|
2018-10-15 13:54:23 -05:00
|
|
|
if (attr==undefined) {
|
|
|
|
attr=0;
|
|
|
|
}
|
|
|
|
return attr;
|
|
|
|
}
|
|
|
|
function numOfBuilding(name) {
|
2018-12-23 16:05:00 -06:00
|
|
|
var count=gamestate.buildings[name]
|
2018-10-15 13:54:23 -05:00
|
|
|
if (count==undefined) {
|
|
|
|
count=0;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2018-10-16 09:18:10 -05:00
|
|
|
function researchLevel(name) {
|
2018-12-23 16:05:00 -06:00
|
|
|
var level=gamestate.researches[name]
|
2018-10-16 09:18:10 -05:00
|
|
|
if (level==undefined) {
|
|
|
|
level=0;
|
|
|
|
}
|
|
|
|
return level;
|
|
|
|
}
|
|
|
|
function researchEffect(name,attrname) {
|
|
|
|
var effect=allResearches[name].effects[attrname];
|
|
|
|
if (effect==undefined) {
|
|
|
|
effect=0;
|
|
|
|
}
|
|
|
|
return effect;
|
|
|
|
}
|
2018-10-06 11:34:12 -05:00
|
|
|
function tenthRound(number) {
|
|
|
|
return Math.round(number*10)/10;
|
|
|
|
}
|
2018-10-15 13:54:23 -05:00
|
|
|
function maxResources() {
|
|
|
|
var numStorehouses=numOfBuilding("storehouse");
|
2018-10-16 09:18:10 -05:00
|
|
|
var storehouseResources=buildingAttribute("storehouse","maxResources");
|
2018-10-15 13:54:23 -05:00
|
|
|
var extraStorage=numStorehouses*storehouseResources;
|
|
|
|
return startingStorage+extraStorage;
|
|
|
|
}
|
2018-10-15 14:19:42 -05:00
|
|
|
function maxResearchPoints() {
|
|
|
|
var numLabs=numOfBuilding("lab");
|
2018-10-16 09:18:10 -05:00
|
|
|
var labRpoints=buildingAttribute("lab","maxResearchPoints");
|
2018-10-15 13:54:23 -05:00
|
|
|
return numLabs*labRpoints;
|
|
|
|
}
|
|
|
|
function maxPop() {
|
|
|
|
var maxpop=0;
|
|
|
|
for (var i in shelters) {
|
|
|
|
var shelter=shelters[i];
|
|
|
|
if (numOfBuilding(shelter)>0) {
|
2018-10-16 09:18:10 -05:00
|
|
|
maxpop+=numOfBuilding(shelter)*buildingAttribute(shelter,"maxPop");
|
2018-10-15 13:54:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return maxpop;
|
|
|
|
}
|
2018-10-16 09:18:10 -05:00
|
|
|
function maxFarmers() {
|
|
|
|
return numOfBuilding("farm")*buildingAttribute("farm","maxFarmers");
|
|
|
|
}
|
|
|
|
function farmYield() {
|
|
|
|
var extraYield=researchLevel("Better Yield")*researchEffect("Better Yield","yield");
|
|
|
|
return extraYield+startingYield;
|
|
|
|
}
|
2018-09-30 13:30:40 -05:00
|
|
|
function updateShown() {
|
2018-12-23 16:05:00 -06:00
|
|
|
var hasResources=Object.keys(gamestate.resources).length>0;
|
|
|
|
var hasLab=Object.keys(gamestate.buildings).includes("lab");
|
|
|
|
var hasTpost=Object.keys(gamestate.buildings).includes("trading post");
|
2018-10-16 09:35:19 -05:00
|
|
|
var hasShelter=false;
|
|
|
|
var hasFarm=numOfBuilding("farm")>0;
|
2018-10-15 13:54:23 -05:00
|
|
|
for (var i in shelters) {
|
|
|
|
var shelter=shelters[i];
|
2018-12-23 16:05:00 -06:00
|
|
|
if (Object.keys(gamestate.buildings).includes(shelter)) {
|
2018-10-15 14:19:42 -05:00
|
|
|
hasShelter=true;
|
2018-10-15 13:54:23 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-10-15 14:19:42 -05:00
|
|
|
$(".shelterRequired").toggle(hasShelter);
|
|
|
|
$("#linkPopulation").toggle(hasShelter);
|
|
|
|
$(".resourcesRequired").toggle(hasResources);
|
|
|
|
$("#linkBuildings").toggle(hasResources);
|
|
|
|
$("#linkResearch").toggle(hasLab);
|
|
|
|
$(".researchRequired").toggle(hasLab);
|
|
|
|
$("#linkTrading").toggle(hasTpost);
|
2018-10-16 09:35:19 -05:00
|
|
|
$(".farmRequired").toggle(hasFarm);
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
|
|
|
function incResource(name,amount=1) {
|
2018-10-15 14:19:42 -05:00
|
|
|
if (name=="researchPoints") {
|
2018-12-23 16:05:00 -06:00
|
|
|
if (gamestate.researchPoints>maxResearchPoints()) {
|
2018-09-30 13:30:40 -05:00
|
|
|
return;
|
|
|
|
}
|
2018-12-23 16:05:00 -06:00
|
|
|
gamestate.researchPoints+=amount;
|
|
|
|
if (gamestate.researchPoints>maxResearchPoints()) {
|
|
|
|
gamestate.researchPoints-=amount;
|
2018-09-30 13:30:40 -05:00
|
|
|
return;
|
|
|
|
}
|
2018-12-23 16:05:00 -06:00
|
|
|
gamestate.researchPoints=tenthRound(gamestate.researchPoints);
|
2018-09-30 13:30:40 -05:00
|
|
|
updateResearchPointInfo();
|
|
|
|
updateResearchButtons();
|
|
|
|
} else {
|
2018-12-23 16:05:00 -06:00
|
|
|
if (gamestate.numResources>=maxResources()) {
|
2018-09-30 13:30:40 -05:00
|
|
|
return;
|
|
|
|
}
|
2018-12-23 16:05:00 -06:00
|
|
|
if (gamestate.resources[name]) {
|
|
|
|
gamestate.resources[name]+=amount;
|
2018-09-30 13:30:40 -05:00
|
|
|
} else {
|
2018-12-23 16:05:00 -06:00
|
|
|
gamestate.resources[name]=amount;
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
2018-12-23 16:05:00 -06:00
|
|
|
gamestate.numResources+=amount;
|
|
|
|
if (gamestate.numResources>maxResources()) {
|
2018-09-30 13:30:40 -05:00
|
|
|
decResource(name,amount);
|
|
|
|
return;
|
|
|
|
}
|
2018-12-23 16:05:00 -06:00
|
|
|
gamestate.resources[name]=tenthRound(gamestate.resources[name]);
|
|
|
|
gamestate.numResources=tenthRound(gamestate.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-12-23 16:05:00 -06:00
|
|
|
if (gamestate.resources[name]<amount) {
|
2018-09-30 13:30:40 -05:00
|
|
|
return;
|
|
|
|
}
|
2018-12-23 16:05:00 -06:00
|
|
|
gamestate.numResources-=amount;
|
|
|
|
gamestate.resources[name]-=amount;
|
|
|
|
gamestate.resources[name]=tenthRound(gamestate.resources[name]);
|
|
|
|
gamestate.numResources=tenthRound(gamestate.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) {
|
2018-12-23 16:05:00 -06:00
|
|
|
if (gamestate.buildings[name]) {
|
|
|
|
gamestate.buildings[name]+=1;
|
2018-09-30 13:30:40 -05:00
|
|
|
} else {
|
2018-12-23 16:05:00 -06:00
|
|
|
gamestate.buildings[name]=1;
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
2018-10-15 13:54:23 -05:00
|
|
|
if (shelters.includes(name)) {
|
|
|
|
updatePopulationInfo();
|
|
|
|
updateWorkerInfo();
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
|
|
|
updateShown();
|
|
|
|
updateBuildingInfo();
|
|
|
|
}
|
|
|
|
function buildingCost(name) {
|
|
|
|
var newIngs={};
|
2018-10-15 14:19:42 -05:00
|
|
|
var ings=allBuildings[name].ings;
|
2018-12-23 16:05:00 -06:00
|
|
|
var buildingAmount=gamestate.buildings[name];
|
2018-10-15 14:19:42 -05:00
|
|
|
if (buildingAmount==undefined) {
|
|
|
|
buildingAmount=0;
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
|
|
|
for (var ing in ings) {
|
2018-10-15 14:19:42 -05:00
|
|
|
var ingAmount=ings[ing];
|
|
|
|
ingAmount=Math.floor(ingAmount*(multiplier**buildingAmount));
|
|
|
|
newIngs[ing]=ingAmount;
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
|
|
|
return newIngs;
|
|
|
|
}
|
|
|
|
function craft(name) {
|
|
|
|
var ings=buildingCost(name);
|
|
|
|
var enough=true;
|
|
|
|
for (var ing in ings) {
|
2018-12-23 16:05:00 -06:00
|
|
|
if (!(gamestate.resources[ing]>=ings[ing])) {
|
2018-09-30 13:30:40 -05:00
|
|
|
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("");
|
2018-12-23 16:05:00 -06:00
|
|
|
for (var name in gamestate.resources) {
|
2018-09-30 13:30:40 -05:00
|
|
|
var capName=capitalizeFirst(name);
|
2018-12-23 16:05:00 -06:00
|
|
|
$("#resources").append("<p>"+capName+": "+gamestate.resources[name]+"</p>");
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
2018-12-23 16:05:00 -06:00
|
|
|
$("#maxResources").text("Resources: ("+gamestate.numResources+"/"+maxResources()+")");
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
|
|
|
function updateBuildingInfo() {
|
|
|
|
$("#buildings").html("");
|
2018-12-23 16:05:00 -06:00
|
|
|
for (var name in gamestate.buildings) {
|
2018-09-30 13:30:40 -05:00
|
|
|
var capName=capitalizeFirst(name);
|
2018-12-23 16:05:00 -06:00
|
|
|
$("#buildings").append("<p>"+capName+": "+gamestate.buildings[name]+"</p>");
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function updateCraftButtons() {
|
2018-10-15 14:19:42 -05:00
|
|
|
$("#craftButtons").html("");
|
|
|
|
for (var name in allBuildings) {
|
2018-12-23 16:05:00 -06:00
|
|
|
if (name=="trading post" && gamestate.buildings["trading post"]>0) {
|
2018-10-06 11:34:12 -05:00
|
|
|
continue;
|
|
|
|
}
|
2018-09-30 13:30:40 -05:00
|
|
|
var ings=buildingCost(name);
|
|
|
|
var show=true;
|
|
|
|
var disabled=false;
|
2018-12-23 16:05:00 -06:00
|
|
|
if (!gamestate.unlockedBuildings.includes(name)) {
|
2018-09-30 13:30:40 -05:00
|
|
|
for (var ing in ings) {
|
2018-12-23 16:05:00 -06:00
|
|
|
if (!(gamestate.resources[ing]>=(ings[ing]/2))) {
|
2018-09-30 13:30:40 -05:00
|
|
|
show=false;
|
|
|
|
break;
|
|
|
|
}
|
2018-12-23 16:05:00 -06:00
|
|
|
if (!(gamestate.resources[ing]>=ings[ing])) {
|
2018-09-30 13:30:40 -05:00
|
|
|
disabled=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (var ing in ings) {
|
2018-12-23 16:05:00 -06:00
|
|
|
if (!(gamestate.resources[ing]>=ings[ing])) {
|
2018-09-30 13:30:40 -05:00
|
|
|
disabled=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (show) {
|
2018-12-23 16:05:00 -06:00
|
|
|
if (!gamestate.unlockedBuildings.includes(name)) {
|
|
|
|
gamestate.unlockedBuildings.push(name);
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
|
|
|
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-15 14:19:42 -05:00
|
|
|
$("#craftButtons").append("<button disabled onclick=\"craft('"+name+"')\">Craft a "+btext+"</button><br>");
|
2018-09-30 13:30:40 -05:00
|
|
|
} else {
|
2018-10-15 14:19:42 -05:00
|
|
|
$("#craftButtons").append("<button onclick=\"craft('"+name+"')\">Craft a "+btext+"</button><br>");
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function updatePopulation() {
|
2018-12-23 16:05:00 -06:00
|
|
|
if(gamestate.pop>0) {
|
2018-09-30 13:30:40 -05:00
|
|
|
updateWorkerInfo();
|
|
|
|
}
|
2018-12-23 16:05:00 -06:00
|
|
|
if (gamestate.pop<maxPop()) {
|
|
|
|
gamestate.pop+=1;
|
2018-09-30 13:30:40 -05:00
|
|
|
updateWorkerInfo();
|
|
|
|
}
|
|
|
|
updatePopulationInfo();
|
|
|
|
}
|
|
|
|
function updatePopulationInfo() {
|
2018-12-23 16:05:00 -06:00
|
|
|
$("#pop").text("Population: "+gamestate.pop+"/"+maxPop());
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
|
|
|
function updateWorkerInfo() {
|
2018-10-15 13:54:23 -05:00
|
|
|
var working=0;
|
2018-10-16 09:35:19 -05:00
|
|
|
var disableFarmer=false;
|
2018-10-15 13:54:23 -05:00
|
|
|
for (var worker in allWorkers) {
|
2018-12-23 16:05:00 -06:00
|
|
|
var workerAmount=gamestate.workers[worker];
|
2018-10-15 14:19:42 -05:00
|
|
|
if (!workerAmount) {
|
2018-10-14 19:03:54 -05:00
|
|
|
workerAmount=0;
|
|
|
|
}
|
2018-10-15 13:54:23 -05:00
|
|
|
working+=workerAmount;
|
2018-10-16 09:35:19 -05:00
|
|
|
if (worker=="farmer") {
|
|
|
|
disableFarmer=workerAmount==maxFarmers();
|
|
|
|
}
|
2018-10-15 14:19:42 -05:00
|
|
|
worker=capitalizeFirst(worker);
|
|
|
|
$("#fire"+worker).attr("disabled",workerAmount==0);
|
|
|
|
$("#num"+worker+"s").text(worker+"s: "+workerAmount);
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
2018-12-23 16:05:00 -06:00
|
|
|
$("#workPop").text("Working: "+working+"/"+gamestate.pop);
|
|
|
|
$(".hire").attr("disabled",working==gamestate.pop);
|
2018-10-16 09:35:19 -05:00
|
|
|
$("#hireFarmer").attr("disabled",disableFarmer);
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
2018-10-15 13:54:23 -05:00
|
|
|
function updateTradingButtons() {
|
2018-10-15 14:19:42 -05:00
|
|
|
$("#tabTrading").html("");
|
|
|
|
for (var name in tradingRates) {
|
|
|
|
var rate=tradingRates[name];
|
|
|
|
$("#tabTrading").append("<p>"+capitalizeFirst(name)+": "+rate+" gold"+"</p>");
|
|
|
|
$("#tabTrading").append("<button id=sell1 onclick=\"sell('"+name+"')\">Sell 1</button> ");
|
|
|
|
$("#tabTrading").append("<button id=buy1 onclick=\"buy('"+name+"')\">Buy 1</button><br>");
|
|
|
|
$("#tabTrading").append("<button id=sell10 onclick=\"sell('"+name+"',10)\">Sell 10</button> ");
|
|
|
|
$("#tabTrading").append("<button id=buy10 onclick=\"buy('"+name+"',10)\">Buy 10</button>");
|
|
|
|
$("#tabTrading #sell1").prop("disabled",!canSell(name)).removeAttr("id");
|
2018-12-23 16:05:00 -06:00
|
|
|
$("#tabTrading #buy1").prop("disabled",gamestate.resources["gold"]<tradingRates[name]).removeAttr("id");
|
2018-10-15 14:19:42 -05:00
|
|
|
$("#tabTrading #sell10").prop("disabled",!canSell(name,10)).removeAttr("id");
|
2018-12-23 16:05:00 -06:00
|
|
|
$("#tabTrading #buy10").prop("disabled",gamestate.resources["gold"]<tradingRates[name]*10).removeAttr("id");
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function updateResearchPointInfo() {
|
2018-12-23 16:05:00 -06:00
|
|
|
$("#researchPoints").text("Research points: "+gamestate.researchPoints+"/"+maxResearchPoints());
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
|
|
|
function updateResearchButtons() {
|
2018-10-15 14:19:42 -05:00
|
|
|
$("#researchButtons").html("");
|
|
|
|
for (var name in allResearches) {
|
|
|
|
var research=allResearches[name];
|
2018-09-30 13:30:40 -05:00
|
|
|
var cost=research.cost;
|
|
|
|
var maxLevel=research.maxLevel;
|
|
|
|
var show=true;
|
|
|
|
var disabled=false;
|
2018-12-23 16:05:00 -06:00
|
|
|
if (!gamestate.unlockedResearch.includes(name)) {
|
|
|
|
if (gamestate.researchPoints<cost/2) {
|
2018-09-30 13:30:40 -05:00
|
|
|
show=false;
|
2018-12-23 16:05:00 -06:00
|
|
|
} else if (gamestate.researchPoints<cost) {
|
2018-09-30 13:30:40 -05:00
|
|
|
disabled=true;
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-23 16:05:00 -06:00
|
|
|
if (gamestate.researches[name]==maxLevel) {
|
2018-09-30 13:30:40 -05:00
|
|
|
show=false
|
2018-12-23 16:05:00 -06:00
|
|
|
} else if (gamestate.researchPoints<cost) {
|
2018-09-30 13:30:40 -05:00
|
|
|
disabled=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (show) {
|
2018-12-23 16:05:00 -06:00
|
|
|
if (!gamestate.unlockedResearch.includes(name)) {
|
|
|
|
gamestate.unlockedResearch.push(name);
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
|
|
|
if (disabled) {
|
2018-10-15 14:19:42 -05:00
|
|
|
$("#researchButtons").append("<button disabled onclick=\"research('"+name+"')\">"+name+" ("+cost+" points)</button><br>");
|
2018-09-30 13:30:40 -05:00
|
|
|
} else {
|
2018-10-15 14:19:42 -05:00
|
|
|
$("#researchButtons").append("<button onclick=\"research('"+name+"')\">"+name+" ("+cost+" points)</button><br>");
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-15 13:54:23 -05:00
|
|
|
function updateResearchInfo() {
|
|
|
|
$("#researches").html("");
|
2018-12-23 16:05:00 -06:00
|
|
|
for (var name in gamestate.researches) {
|
|
|
|
$("#researches").append("<p>"+name+": Level "+gamestate.researches[name]+"</p>");
|
2018-10-15 13:54:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function applyResearches() {
|
2018-12-23 16:05:00 -06:00
|
|
|
for (var research in gamestate.researches) {
|
2018-10-15 14:19:42 -05:00
|
|
|
var effects=allResearches[research].effects;
|
2018-12-23 16:05:00 -06:00
|
|
|
var level=gamestate.researches[research];
|
2018-10-15 13:54:23 -05:00
|
|
|
for (var effect in effects) {
|
2018-10-15 14:19:42 -05:00
|
|
|
if (effect=="workerRate") {
|
|
|
|
workerRate=0.5;
|
|
|
|
workerRate+=effects[effect]*level;
|
2018-10-15 13:54:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function research(name) {
|
2018-12-23 16:05:00 -06:00
|
|
|
gamestate.researchPoints-=allResearches[name].cost;
|
|
|
|
if (gamestate.researches[name]) {
|
|
|
|
gamestate.researches[name]+=1;
|
2018-10-15 13:54:23 -05:00
|
|
|
} else {
|
2018-12-23 16:05:00 -06:00
|
|
|
gamestate.researches[name]=1;
|
2018-10-15 13:54:23 -05:00
|
|
|
}
|
|
|
|
updateResearchButtons();
|
|
|
|
updateResearchInfo();
|
|
|
|
applyResearches();
|
|
|
|
}
|
2018-09-30 13:30:40 -05:00
|
|
|
function hire(type) {
|
2018-12-23 16:05:00 -06:00
|
|
|
if (gamestate.workers[type]) {
|
|
|
|
gamestate.workers[type]+=1;
|
2018-10-14 19:03:54 -05:00
|
|
|
} else {
|
2018-12-23 16:05:00 -06:00
|
|
|
gamestate.workers[type]=1;
|
2018-10-14 19:03:54 -05:00
|
|
|
}
|
2018-09-30 13:30:40 -05:00
|
|
|
updateWorkerInfo();
|
|
|
|
}
|
|
|
|
function fire(type) {
|
2018-12-23 16:05:00 -06:00
|
|
|
if (gamestate.workers[type]) {
|
|
|
|
gamestate.workers[type]-=1;
|
2018-10-14 19:03:54 -05:00
|
|
|
} else {
|
|
|
|
throw new Error("Cannot fire a never hired employee");
|
|
|
|
}
|
2018-09-30 13:30:40 -05:00
|
|
|
updateWorkerInfo();
|
|
|
|
}
|
|
|
|
function autoInc() {
|
2018-10-15 13:54:23 -05:00
|
|
|
for (var worker in allWorkers) {
|
2018-12-23 16:05:00 -06:00
|
|
|
workerAmount=gamestate.workers[worker];
|
2018-10-15 14:19:42 -05:00
|
|
|
var amount=workerRate*workerAmount;
|
2018-12-23 16:05:00 -06:00
|
|
|
var amountFood=gamestate.resources.food
|
2018-10-16 09:35:19 -05:00
|
|
|
if (amountFood>amount*0.75 && amount>0) {
|
2018-09-30 13:30:40 -05:00
|
|
|
if (worker=="scientist") {
|
2018-10-15 14:19:42 -05:00
|
|
|
var usedMetal=Math.ceil(workerAmount*0.4);
|
2018-12-23 16:05:00 -06:00
|
|
|
if (gamestate.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);
|
|
|
|
}
|
2018-10-16 09:35:19 -05:00
|
|
|
} else if (worker=="farmer") {
|
|
|
|
incResource(allWorkers[worker],amount*farmYield());
|
2018-09-30 13:30:40 -05:00
|
|
|
} else {
|
2018-10-14 19:03:54 -05:00
|
|
|
incResource(allWorkers[worker],amount);
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
2018-10-16 09:35:19 -05:00
|
|
|
decResource("food",amount*0.75);
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function save() {
|
|
|
|
localStorage.setItem("game",JSON.stringify(gamestate));
|
|
|
|
}
|
|
|
|
function load() {
|
2018-12-23 16:05:00 -06:00
|
|
|
gamestate=JSON.parse(localStorage.getItem("game"));
|
2018-09-30 13:30:40 -05:00
|
|
|
if (gamestate==null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
function reset() {
|
|
|
|
localStorage.removeItem("game");
|
|
|
|
if (load()!=false) {
|
|
|
|
alert("Unable to reset game");
|
2018-12-23 16:05:00 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
oldstate=gamestate;
|
|
|
|
gamestate=new Gamestate();
|
|
|
|
save();
|
|
|
|
if (!load()) {
|
|
|
|
alert("Unable to reset game");
|
|
|
|
gamestate=oldstate;
|
|
|
|
save();
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
function init() {
|
|
|
|
if (!load()) {
|
2018-12-23 16:05:00 -06:00
|
|
|
var gamestate=new Gamestate();
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
2018-10-15 14:19:42 -05:00
|
|
|
setTab("main");
|
2018-09-30 13:30:40 -05:00
|
|
|
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
|
|
|
}
|
2018-10-15 14:19:42 -05:00
|
|
|
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");
|
2018-09-30 13:30:40 -05:00
|
|
|
}
|
|
|
|
}
|
2018-10-06 11:34:12 -05:00
|
|
|
function canSell(name,amount=1) {
|
2018-10-15 14:19:42 -05:00
|
|
|
var goldGotten=tradingRates[name]*amount;
|
2018-10-06 11:34:12 -05:00
|
|
|
var spacesNeeded=goldGotten-amount;
|
2018-12-23 16:05:00 -06:00
|
|
|
return maxResources()-gamestate.numResources>=spacesNeeded;
|
2018-10-06 11:34:12 -05:00
|
|
|
}
|
|
|
|
function buy(name,amount=1) {
|
2018-10-15 14:19:42 -05:00
|
|
|
decResource("gold",tradingRates[name]*amount);
|
2018-10-06 11:34:12 -05:00
|
|
|
incResource(name,amount);
|
|
|
|
}
|
|
|
|
function sell(name,amount=1) {
|
|
|
|
decResource(name,amount);
|
2018-10-15 14:19:42 -05:00
|
|
|
incResource("gold",tradingRates[name]*amount);
|
2018-10-06 11:34:12 -05:00
|
|
|
}
|
2018-09-30 13:30:40 -05:00
|
|
|
$(document).ready(function() {
|
2018-10-15 13:54:23 -05:00
|
|
|
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);
|
|
|
|
});
|