/
buildings.js
642 lines (541 loc) · 16.7 KB
/
buildings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
var Buildings = {
basic: function(xy, type){
if (type !== undefined){
this.location = xy;
this.image = Resources.images[type];
this.type = type;
// set a bunch of defaults
this.width = 1;
this.height = 1;
this.jobs = 0;
this.workers = 0;
this.isHouse = false;
this.needsRoad = true;
this.jobFinder = false;
this.yOffset = 0;
this.usesLabourCamp = false;
}
},
corn_field: function(xy){
Buildings.basic.call(this, xy, "corn_field");
this.width = this.height = 3;
this.production = 0;
this.totalProduction = 1000;
this.jobs = 1;
this.workersHere = [];
this.usesLabourCamp = true;
this.goodsAtProduction = {
corn: 50
};
this.state = "working";
},
market: function(xy){
Buildings.basic.call(this, xy, "market");
this.width = this.height = 2;
this.jobs = 5;
this.merchant = false;
this.state = "merchant_here";
this.contents = {
corn: 0
};
},
plot: function(xy){
Buildings.basic.call(this, xy, "plot");
this.capacity = 1;
this.people = 0;
this.peopleComing = 0;
this.isHouse = true;
this.level = 0;
this.levelUpgradeTime = 0;
this.nextLevel = 0;
this.needs = {
water: false,
corn: 0
};
},
silo: function(xy){
Buildings.basic.call(this, xy, "silo");
this.width = this.height = 2;
this.yOffset = 29;
this.capacity = 1000;
this.pendingCapacity = 0; // for stuff on its way
this.contents = {
corn: 0
};
this.jobs = 10;
},
water_hole: function(xy){
Buildings.basic.call(this, xy, "water_hole");
this.width = this.height = 2;
this.carrier = false;
this.state = "carrier_here";
this.jobs = 2;
},
work_camp: function(xy){
Buildings.basic.call(this, xy, "work_camp");
this.width = this.height = 2;
this.jobs = 10;
this.workerObjs = new Array();
this.lastWorkerSent = 0;
}
};
Buildings.plot.prototype = new Buildings.basic();
Buildings.water_hole.prototype = new Buildings.basic();
Buildings.corn_field.prototype = new Buildings.basic();
Buildings.work_camp.prototype = new Buildings.basic();
Buildings.silo.prototype = new Buildings.basic();
Buildings.market.prototype = new Buildings.basic();
Buildings.basic.prototype.update = function() {
// certain buildings use a job finder to find labourers,
// so send one out if we need workers
if (GameLogic.population > 0 && this.usesLabourCamp === false && this.needsWorkers() && this.jobFinder === false){
var road = this.findRoad(1);
if (road){
this.jobFinder = new JobFinder(this);
GameLogic.addPerson(this.jobFinder);
this.jobFinder.wander();
}
}
};
Buildings.basic.prototype.getCapacity = function() { return 0; }
Buildings.basic.prototype.getText = function(which){
if (which == "header"){
return t(this.type);
}else{
return t(this.type + "_body");
}
};
// overridden for custom building rendering
Buildings.basic.prototype.render = function(tiler, context){
coords = tiler.toScreenCoords(this.location, false);
context.drawImage(this.image.image, coords[0], coords[1] - (tiler.jhat * (this.height - 1)) - this.yOffset);
};
Buildings.basic.prototype.needsWorkers = function() { return this.workers < this.jobs; }
Buildings.basic.prototype.placed = function(coords){
this.location = coords;
// certain buildings need to be near a road, so output a message if that is the case
// here
if (this.needsRoad && !this.findRoad(1)){
Game.addMessage(t("must_be_near_road"));
}
// cover up nearby tiles
for (var i = 0; i < this.height; i++){
for (var j = 0; j < this.width; j++){
Game.tiles[this.location[1] + i][this.location[0] + j].building = this;
}
}
};
Buildings.basic.prototype.remove = function(){
// clear off all my tiles
for (var i = 0; i < this.width; i++){
for (var j = 0; j < this.height; j++){
Game.tiles[this.location[1] + j][this.location[0] + i].building = false;
}
}
GameLogic.removeJob(this.workers);
// TODO: work camp places need to fire workers
// TODO: Housing plots need to remove population
};
Buildings.basic.prototype.canStore = function(item){
return false;
}
Buildings.basic.prototype.addWorker = function(){
this.workers++;
};
Buildings.basic.prototype.findRoad = function(radius, width, height){
if (radius === undefined){
radius = 2;
}
if (width === undefined){
width = this.width;
}
if (height === undefined){
height = this.height;
}
for (var y = Math.max(0, this.location[1] - radius); y <= Math.min(Game.tiles.length - 1, this.location[1] + height - 1 + radius); y++){
for (var x = Math.max(0, this.location[0] - radius); x <= Math.min(Game.tiles[y].length - 1, this.location[0] + width - 1 + radius); x++){
if (Game.tiles[y][x].road){
return Game.tiles[y][x];
}
}
}
return false;
};
/****************
* Plot Methods *
****************/
Buildings.plot.capacities = [
1, 1, 2, 3
];
Buildings.plot.prototype.upgradeTo = function(level){
this.levelUpgradeTime = 200;
this.nextLevel = level;
};
Buildings.plot.prototype.update = function(){
if (this.people === 0){
this.level = 0;
}else{
if (this.level == 0){
// jumps to level 1 from 0 are instant
this.level = 1;
}else{
if (this.nextLevel > 0){
if (--this.levelUpgradeTime <= 0){
this.level = this.nextLevel;
this.nextLevel = 0;
this.levelUpgradeTime = 0;
}
}else if (this.level == 1){
if (this.needs.water === true){
this.upgradeTo(2);
}
}else if (this.level == 2){
if (this.needs.water === false){
this.upgradeTo(1);
}else if (this.needs.corn > 0){
this.upgradeTo(3);
}
}else if (this.level == 3){
if (this.needs.corn === 0){
this.upgradeTo(2);
}
}
}
}
this.capacity = Buildings.plot.capacities[this.level];
this.updateImage();
};
Buildings.plot.prototype.placed = function(coords){
this.location = coords;
Game.tiles[this.location[1]][this.location[0]].building = this;
// check for a road nearby
if (!this.findRoad()){
Game.addMessage(t("plot_too_far"));
}
}
Buildings.plot.prototype.arrived = function(person){
person.state = "hidden";
this.peopleComing--;
this.people++;
};
Buildings.plot.prototype.addPerson = function(person){
this.peopleComing++;
};
Buildings.plot.prototype.getCapacity = function(person){
if (this.capacity == this.people + this.peopleComing || !this.findRoad()){
return 0;
}else{
return this.capacity - this.people - this.peopleComing;
}
};
Buildings.plot.prototype.updateImage = function(){
var lastImage = this.image;
if (this.nextLevel > 0){
var min = Math.min(this.level, this.nextLevel);
var max = Math.max(this.level, this.nextLevel);
this.image = Resources.images["house_shift_" + min + "_" + max];
}else if (this.level === 0){
this.image = Resources.images.plot;
}else if (this.level == 1){
this.image = Resources.images.hovel;
}else if (this.level == 2){
this.image = Resources.images.shack;
}else if (this.level == 3){
this.image = Resources.images.hut;
}
if (this.image.yOffset !== undefined){
this.yOffset = this.image.yOffset;
}else{
this.yOffset = 0;
}
if (this.image != lastImage){
Game.display.tiler.renderBuildings();
}
};
Buildings.plot.prototype.addResource = function(resource, amount){
// a house can only get resources if there is someone living there
if (this.level > 0){
if (amount === undefined){
this.needs[resource] = true;
}else if (this.needs[resource] === undefined){
this.needs[resource] = amount;
}else{
this.needs[resource] += amount;
}
}
};
Buildings.plot.prototype.getText = function(which){
var base = Buildings.basic.prototype.getText.call(this, which);
if (which == "body"){
base += "<br /><br />";
if (this.nextLevel != 0){
if (this.nextLevel > this.level){
base += t("improving");
}else{
base += t("worsening");
}
}else{
base += t("house_level_" + this.level);
}
}
return base;
};
/*************************
* Watering Hole Methods *
*************************/
Buildings.water_hole.prototype.update = function(){
Buildings.basic.prototype.update.call(this);
// if we have any workers we should put out a water carrier
if (this.workers > 0){
if (this.state == "carrier_here"){
// TODO: should randomly place water carrier based on how well-staffed we are
if (!this.carrier){
this.carrier = new WaterCarrier(this);
GameLogic.addPerson(this.carrier);
}
}
}
};
/*************************
* Work Camp Methods *
*************************/
Buildings.work_camp.prototype.update = function(){
Buildings.basic.prototype.update.call(this);
// if we have any workers look for places that need work
if (this.lastWorkerSent <= 0){
var idleWorkers = this.idleWorkers();
if (idleWorkers.length > 0){
// Might need to iterate over non-housing buildings
for (var i = 0; i < Game.buildings.length; i++){
var building = Game.buildings[i];
if (building.usesLabourCamp && building.needsWorkers()){
// send a worker to this building
var worker = idleWorkers.shift();
// not sure why this is ever undefined, but it is sometimes
if (worker){
worker.assignWorkplace(building);
building.addWorker();
var road = this.findRoad(1);
if (road){
Game.addPerson(worker);
worker.location = road.xy;
worker.moveToBuildingByRoad(building);
this.lastWorkerSent = 100;
}
}
}
}
}
}else{
this.lastWorkerSent--;
}
};
Buildings.work_camp.prototype.addWorker = function(){
this.workers++;
this.workerObjs.push(new Worker(this.location, this));
};
Buildings.work_camp.prototype.idleWorkers = function(){
return $.grep(this.workerObjs, function(obj) { return !obj.isWorking(); });
};
/*****************************
* Corn Field Methods *
*****************************/
Buildings.corn_field.prototype.workerArrived = function(who){
this.workersHere.push(who);
this.whileInside(who);
};
Buildings.corn_field.prototype.update = function(){
Buildings.basic.prototype.update.call(this);
if (this.state == "working"){
if (this.workersHere.length > 0){
this.production++;
var pct = this.getProductionPct();
if (pct % 10 == 0){
Game.display.tiler.renderBuildings();
}
if (this.production >= this.totalProduction){
this.production = this.totalProduction;
this.state = "production_done";
}
}
}else if (this.state == "production_done"){
var farm = this;
var silo = Game.findBuilding("silo", function(silo){
return silo.hasRoom(farm.goodsAtProduction.corn);
});
if (silo){
// send the worker to this silo
if (this.workersHere[0].transportGoodsTo(this.goodsAtProduction, silo)){
// only do it if a path can be found
this.production = 0;
this.state = "waiting";
Game.display.tiler.renderBuildings();
}
}
}else{
// do nothing
}
};
Buildings.corn_field.prototype.whileInside = function(worker){
// TODO: make the worker wander around, show farming animation
};
Buildings.corn_field.prototype.getText = function(which){
var base = Buildings.basic.prototype.getText.call(this, which);
if (which == "body"){
return base +
"<br /><br />" +
"Production is at " + this.getProductionPct() + "%.";
}else{
return base;
}
};
Buildings.corn_field.prototype.getProductionPct = function(){
return Math.round(this.production / this.totalProduction * 100);
};
Buildings.corn_field.prototype.getWorkerSprite = function(workerState){
if (workerState == "inside"){
return Resources.sprites.worker_farming;
}else{
return Resources.sprites.worker;
}
};
Buildings.corn_field.prototype.render = function(tiler, context){
Buildings.basic.prototype.render.call(this, tiler, context);
var pct = this.getProductionPct();
var corn = Resources.images.corn;
var coords;
// render the different cells
if (pct >= 10){
coords = tiler.toScreenCoords(this.location, false);
context.drawImage(corn.image, coords[0], coords[1]);
}
if (pct >= 20){
coords = tiler.toScreenCoords([this.location[0] + 1, this.location[1]], false);
context.drawImage(corn.image, coords[0] + 1, coords[1]);
}
if (pct >= 30){
coords = tiler.toScreenCoords([this.location[0] + 2, this.location[1]], false);
context.drawImage(corn.image, coords[0] + 2, coords[1]);
}
if (pct >= 40){
coords = tiler.toScreenCoords([this.location[0], this.location[1] + 1], false);
context.drawImage(corn.image, coords[0], coords[1] + 1);
}
if (pct >= 50){
coords = tiler.toScreenCoords([this.location[0] + 1, this.location[1] + 1], false);
context.drawImage(corn.image, coords[0] + 1, coords[1] + 1);
}
if (pct >= 60){
coords = tiler.toScreenCoords([this.location[0] + 2, this.location[1] + 1], false);
context.drawImage(corn.image, coords[0] + 2, coords[1] + 1);
}
if (pct >= 70){
coords = tiler.toScreenCoords([this.location[0], this.location[1] + 2], false);
context.drawImage(corn.image, coords[0], coords[1] + 2);
}
if (pct >= 80){
coords = tiler.toScreenCoords([this.location[0] + 1, this.location[1] + 2], false);
context.drawImage(corn.image, coords[0] + 1, coords[1] + 2);
}
if (pct >= 90){
coords = tiler.toScreenCoords([this.location[0] + 2, this.location[1] + 2], false);
context.drawImage(corn.image, coords[0] + 2, coords[1] + 2);
}
}
/*****************************
* Silo Methods *
*****************************/
Buildings.silo.prototype.hasRoom = function(amount){
var total = this.pendingCapacity + amount;
for (var type in this.contents){
total += this.contents[type];
}
return total <= this.capacity;
};
Buildings.silo.prototype.contains = function(what, amount){
if (amount === undefined){
amount = 1;
}
return this.contents[what] === undefined ? false : this.contents[what] >= amount;
};
Buildings.silo.prototype.addGoods = function(what){
for (var type in what){
if (this.contents[type] === undefined){
this.contents[type] = 0;
}
this.contents[type] += what[type];
}
};
Buildings.silo.prototype.removeGoods = function(what){
for (var type in what){
if (this.contents[type] === undefined){
this.contents[type] = 0;
}
this.contents[type] -= what[type];
if (this.contents[type] < 0){
this.contents[type] = 0;
}
}
};
Buildings.silo.prototype.getText = function(which){
var base = Buildings.basic.prototype.getText.call(this, which);
if (which == "body"){
base +=
"<br /><br />";
for (var crop in this.contents){
if (crop !== undefined){
base += crop.capitalize() + ": " + this.contents[crop] + "<br />";
}
}
}
return base;
};
/***********************
* Market Methods *
***********************/
Buildings.market.prototype.update = function(){
Buildings.basic.prototype.update.call(this);
if (this.workers > 0){
// if we have any workers we should put out a merchant
if (this.contents.corn == 0){
if (this.state != "waiting_for_goods"){
// send the merchant to a nearby silo with food
var silo = Game.findBuilding("silo", function(silo){
return silo.contains("corn", 200);
});
if (silo){
var road = this.findRoad(1);
if (road){
// send the merchant to this silo
if (!this.merchant){
this.merchant = new Merchant(this);
GameLogic.addPerson(this.merchant);
}else{
this.merchant.location = road.xy;
}
this.merchant.silo = silo;
this.merchant.moveToBuildingByRoad(silo);
this.merchant.state = "getting_goods";
this.state = "waiting_for_goods";
}
}
}
}else if (this.state != "merchant_out"){
var road = this.findRoad(1);
if (road){
if (!this.merchant){
this.merchant = new Merchant(this);
GameLogic.addPerson(this.merchant);
}else{
this.merchant.chooseRandomPath();
}
this.state = "merchant_out";
// give the merchant some goods to share
var amount = Math.min(200, this.contents.corn);
this.contents.corn -= amount;
this.merchant.addResources({corn: amount});
}
}
}
};
Buildings.market.prototype.addGoods = Buildings.silo.prototype.addGoods;