-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.js
594 lines (554 loc) · 17.2 KB
/
model.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
var gamejs = require('gamejs');
var objects = require('gamejs/utils/objects');
var Island = require('./model/island').Island;
var MoveableUnit = require('./model/unit').MoveableUnit;
var MONTH_MS = 2000;
var DIRS = [
[-1,-1],
[1,1],
[-1,1],
[1,-1],
[-1,0],
[0,-1],
[1,0],
[0,1]
];
exports.World = World;
/**
* World
*/
function World(size, maxRounds) {
var islands = [];
var units = [];
var monthMs = 0;
var freeplay = false;
var months = 0;
if (maxRounds === 0) {
freeplay = true;
} else {
months = (maxRounds * 12) + 12;
}
/**
*/
this.update = function(msDuration) {
if (!freeplay && months === 0) return {};
monthMs += msDuration;
var results = [];
// year, month advance
var monthChanged = false;
var yearChanged = false;
if (monthMs >= MONTH_MS) {
if (freeplay) {
months++;
} else {
months--;
}
monthMs = 0;
monthChanged = true;
gamejs.debug('World.update: month', months);
if (months && months % 12 === 0) {
yearChanged = true;
}
}
// create weather
if (monthChanged && months && months % 2 === 0) {
results.push(this.add('weather'));
}
// create pirates
if (monthChanged && months && months % 8 === 0) {
results.push(this.add('pirate'));
}
// create fish
if (monthChanged && months && months % 6 === 0) {
results.push(this.add('schoolfish'));
}
// if a unit moves, execute its postMove handler
units.forEach(function(u) {
var moved = u.update(msDuration);
if (moved === true) {
var res = postMove[u.type](u);
results.push(res);
results.push({
move: [u],
});
} else if (moved === null) {
killUnit(u);
results.push({
remove: [u],
});
}
});
// update island. some properties evolve per month, others per year
islands.forEach(function(i, islandIdx) {
if (monthChanged) {
var res = i.nextMonth();
results.push(res);
};
if (yearChanged) {
var res = i.nextYear();
results.push(res);
if (i.census.unhappy > 0) {
// TWEAK rebel spawn
var rebelCount = (i.census.unhappy + i.census.hungry - 100) / 150;
for (var j =0; j < rebelCount; j++) {
results.push(this.add('rebel', islandIdx));
}
if (rebelCount > 0) {
results.push({messages: ['Rebel alarm!']});
}
}
// DEBUG
gamejs.debug('World.update, island census', JSON.stringify(i.census));
}
}, this);
// merge results
var finalResults = {
months: months,
add: [],
remove: [],
island: {},
move: [],
messages: []
};
results.forEach(function(r) {
if (r) {
if (r.add) {
finalResults.add = finalResults.add.concat(r.add);
}
if (r.remove) {
finalResults.remove = finalResults.remove.concat(r.remove);
}
if (r.move) {
finalResults.move = finalResults.move.concat(r.move);
}
if (r.island) {
objects.keys(r.island).forEach(function(key) {
finalResults.island[key] = r.island[key];
});
}
if (r.messages) {
finalResults.messages = finalResults.messages.concat(r.messages);
}
}
});
// serialize units
['add', 'remove', 'move'].forEach(function(key) {
if (!finalResults[key]) return;
finalResults[key] = finalResults[key].map(function(u) { return u.serialize(); });
});
return finalResults;
}; // end update
/**
* Add unit
*/
this.add = function(type, islandIdx, cell) {
var island = islands[islandIdx];
// TWEAK costs
var cost = {
crop: 3,
factory: 40,
fishing: 25,
fort: 50,
hospital: 75,
housing: 60,
patrol: 40,
school: 35
}[type];
if (island && cost) {
if (cost > island.goldCount) {
return null;
}
island.goldCount -= cost;
}
var unit = null;
if (type === 'weather') {
// TWEAK weather
var rnd = Math.random();
var realType = 'rain';
if (rnd < 0.1) {
realType = 'storm';
} else if (rnd > 0.97) {
realType = 'hurrican';
}
var speed = 2 + parseInt(Math.random() * 3, 10);
unit = new MoveableUnit(realType, this.randomCell('weatherbottom'), speed);
var pathLen = 8 + (Math.random() * 10);
unit.path = this.randomPath(unit.cell, pathLen, true);
} else if (type === 'schoolfish') {
unit = new MoveableUnit(type, this.randomCell('top'), 5);
unit.path = this.randomPath(unit.cell, 5);
} else if (type === 'pirate') {
unit = new MoveableUnit(type, this.randomCell('right'), 2);
unit.path = this.randomPath(unit.cell, 18);
} else if (type === 'rebel') {
unit = new MoveableUnit(type, island.randomCell(), 10);
unit.path = island.randomPath(unit.cell, 2);
} else if (['fishing', 'patrol'].indexOf(type) > -1) {
var speed = type === 'patrol' ? 0.8 : 1;
unit = new MoveableUnit(type, island.dock.cell, speed);
// HACK fishing count
island.fishingCount += 1;
unit.islandIdx = islandIdx;
}
// construct result object
var result = null;
if (unit) {
units.push(unit);
result = {
add: [unit],
move: [],
remove: []
}
} else {
result = island.add(type, cell);
}
return result;
};
/**
* setPath for unit
*/
this.setPath = function(unitId, path) {
var unit = getUnit(unitId);
if (!unit) {
throw new Error('no unit with id', unitId);
}
unit.setPath(path);
return;
};
this.dataSerialize = function(homeIslandIdx) {
return islands[homeIslandIdx].dataSerialize();
};
/**
* create json-able dump of world status
*/
this.serialize = function() {
var unitDump = units.map(function(u) { return u.serialize(); });
islands.forEach(function(i) {
unitDump = unitDump.concat(i.units.map(function(u) {
return u.serialize();
}));
});
var islandDump = islands.map(function(i) { return i.serialize(); });
return {
months: months,
units: unitDump,
islands: islandDump,
};
};
/**
* post unit move handlers
* called after that type of unit has moved, triggers model changes
*/
var postMove = {
rain: function(w) {
var unit = getUnit(w.cell, w.id);
if (!unit) return null;
var result = {
remove: [],
messages: []
};
if (unit.type === 'crop') {
var island = getIsland(w.cell);
island.goldCount += 1;
result.island = island.dataSerialize();
result.messages.push('1 gold bar crop revenue from rain');
}
return result;
},
storm: function(w) {
var unit = getUnit(w.cell, w.id);
if (!unit) return null;
var result = {
remove: [],
messages: []
};
var island = getIsland(w.cell);
// storm over crop
// give gold OR destory
if (unit.type === 'crop') {
if (Math.random() < 0.25) {
killUnit(unit);
result.remove.push(unit);
result.messages.push('Crop lost in storm');
} else {
island.goldCount += 1;
result.island = island.dataSerialize();
result.messages.push('1 gold bar crop revenue from rain');
}
} else if (['fishing', 'patrol'].indexOf(unit.type) > -1) {
if (Math.random() < 0.5) {
killUnit(unit);
if (unit.type === 'fishing') {
// HACK fishing count
var island = islands[unit.islandIdx];
island.fishingCount -= 1;
}
result.remove.push(unit);
var type = unit.type === 'fishing' ? 'Fishing boat' : 'Patrol boat';
result.messages.push(type + ' lost in storm');
}
} else if (unit && ['fort', 'factory', 'housing', 'crop',
'school', 'hospital'].indexOf(unit.type) > -1) {
if (Math.random() < 0.01) {
killUnit(unit);
result.remove.push(unit);
var type = unit.type.charAt(0).toUpperCase() + unit.type.slice(1);
result.messages.push(type + ' lost in storm');
}
}
return result;
},
hurrican: function(w) {
var unit = getUnit(w.cell, w.id);
if (!unit) return null;
var result = {
remove: [],
messages: []
};
// fishing boats: killed (unless anchored, then 2/3)
// 2/3 chance to kill anything else
if (unit.type === 'fishing') {
killUnit(unit);
// HACK fishing count
var island = islands[unit.islandIdx];
island.fishingCount -= 1;
result.remove.push(unit);
result.messages.push('Fishing boat lost in hurricane');
} else if (unit.type === 'patrol') {
if (Math.random() < 0.75) {
killUnit(unit);
result.remove.push(unit);
result.messages.push('Patrol boat lost in hurricane');
}
} else if (['fort', 'factory', 'housing', 'crop',
'school', 'hospital'].indexOf(unit.type) > -1) {
if (Math.random() < 0.8) {
killUnit(unit);
result.remove.push(unit);
var type = unit.type.charAt(0).toUpperCase() + unit.type.slice(1);
result.messages.push(type + ' lost in hurrican');
}
}
return result;
},
rebel: function(r) {
// if over island unit && no fort near -> kill building
var unit = getUnit(r.cell, r.id);
if (!unit) return null;
if (['fort', 'factory', 'housing', 'crop',
'school', 'hospital'].indexOf(unit.type) === -1) return;
var neighborForts = DIRS.map(function(d) {
return [
r.cell[0] + d[0],
r.cell[1] + d[1]
];
}).map(function(c) {
return getUnit(c);
}).filter(function(u) {
return u && u.type === 'fort';
});
if (neighborForts.length > 0) return null;
killUnit(unit);
var type = unit.type.charAt(0).toUpperCase() + unit.type.slice(1);
return {
remove: unit,
messages: [type + ' destroyed by rebels']
}
},
pirate: function(p) {
// pirate kills fishing boat
var unit = getUnit(p.cell, p.id);
if (!unit) return null;
if (unit.type === 'fishing') {
// HACK fishing count
var island = islands[unit.islandIdx];
island.fishingCount -= 1;
killUnit(unit);
return {
remove: unit,
messages: ['Fishing boat destroyed by pirate ship']
}
} else if (unit.type === 'patrol') {
killUnit(p);
return {
remove: p,
messages: ['Pirate ship destroyed by your patrol boat']
}
}
return null;
},
patrol: function(p) {
// patrol kills pirate
var unit = getUnit(p.cell, p.id);
if (!unit) return null;
if (unit.type === 'pirate') {
killUnit(unit);
return {
remove: unit,
messages: ['Pirate ship destroyed by patrol boat']
}
}
return null;
},
fishing: function(f) {
// boat over school of fish -> extra gold
var unit = getUnit(f.cell, f.id);
if (!unit) return null;
if (unit.type === 'schoolfish') {
var island = islands[f.islandIdx];
island.goldCount += 1;
return {
island: island.dataSerialize(),
messages: ['1 gold bar extra fish income']
};
}
return null;
},
schoolfish: function(f) {
return /*
// fish over boat -> extra gold
var unit = getUnit(f.cell, f.id);
if (!unit) return null;
if (unit.type === 'fishing') {
var island = islands[unit.islandIdx];
island.goldCount += 1;
return {
island: island.dataSerialize(),
messages: ['1 gold bar extra fish income']
};
}
*/
return null;
}
};
this.cellCollide = function(cell, noIslandCollide) {
if (cell[0] < 0 || cell[0] > size[0] || cell[1] < 0 || cell[1] > size[1]) {
return false;
}
if (noIslandCollide === true) return true;
return !islands.some(function(i) {
return i.cellCollide(cell);
});
};
this.randomCell = function(area) {
var cell = null;
while (!cell || !this.cellCollide(cell)) {
cell = [
parseInt(Math.random() * size[0], 10),
parseInt(Math.random() * size[1], 10)
];
if (area === 'right') {
cell[0] = size[0] - 1;
} else if (area === 'top') {
cell[1] = 0;
} else if (area === 'bottom') {
cell[1] = size[1] - 1;
} else if (area === 'left') {
cell[0] = 1;
} else if (area === 'weatherbottom') {
cell[0] = 3 + parseInt(Math.random() * size[0] / 3, 10);
cell[1] = size[1];
}
}
return cell;
};
this.randomPath = function(cell, len, noIslandCollide) {
var len = len || 10;
var path = [cell];
var dir = null;
var i = 0;
var newCell = null;
while (i < 1000 && path.length < len) {
i++;
var isOkayDirection = newCell && this.cellCollide(newCell, noIslandCollide);
if (!dir || !isOkayDirection || path.indexOf(newCell) > -1) {
if (!isOkayDirection) {
dir = DIRS[parseInt(Math.random() * (DIRS.length - 2), 10)];
}
newCell = [
cell[0] + dir[0],
cell[1] + dir[1]
];
continue;
}
path.push(newCell);
cell = [newCell[0], newCell[1]];
};
return path;
};
/**
* get unit by id or cell
* will search world & all islands for that unit
*/
function getUnit(id, notId) {
if (typeof id === 'number') {
var us = units.filter(function(u) {
return u.id === id && u.id !== notId;
});
var result = us.length > 0 ? us[0] : null;
if (!result) {
islands.some(function(i) {
return i.units.some(function(u) {
if (u.id === id && u.id !== notId) {
result = u;
return true;
}
return false;
});
});
}
} else if (id instanceof Array) {
var us = units.filter(function(u) {
return u.cell[0] === id[0] && u.cell[1] === id[1] && u.id !== notId;
});
var result = us.length > 0 ? us[0] : null;
if (!result) {
islands.some(function(i) {
return i.units.some(function(u) {
if (u.cell[0] === id[0] && u.cell[1] === id[1] && u.id !== notId) {
result = u;
return true;
}
return false;
});
});
}
}
return result;
};
function killUnit(unit) {
var id = unit && unit.id;
if (typeof id === 'number') {
units = units.filter(function(u) {
return u.id !== id;
});
// FIXME optimize, don't go through islands if above killed unit
islands = islands.map(function(i) {
i.units = i.units.filter(function(u) {
return u.id !== id;
});
return i;
});
} else {
throw Error('killUnit: must pass unit with .id property. id not number', id);
}
};
/**
* find island that holds cell if any
*/
function getIsland(cell) {
var island = null;
islands.some(function(i) {
return i.cells.some(function(c) {
if (c[0] === cell[0] && c[1] === cell[1]) {
island = i;
return true;
}
return false;
});
});
return island;
};
islands.push(new Island());
return this;
};