-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathship.js
612 lines (589 loc) · 23.7 KB
/
ship.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
function Ship(coords, momentum, type=SHIP_TYPE_OTHER, flag=SHIP_FLAG_UNKNOWN)
{
this.name = TEAM_NAME[flag] + " " + SHIP_NAMES[type];
this.xCoord = coords[0];
this.yCoord = coords[1];
this.char = SHIP_SYMBOL[type];
this.player = false;
this.xMoment = momentum[0];
this.yMoment = momentum[1];
this.xCursor = momentum[0];
this.yCursor = momentum[1];
this.facing = getEightWayDirection(momentum[0], momentum[1]);
if (this.facing == CENTER)
this.facing = EAST;
this.weapons = [];
this.warpCore = 20;
this.warpCoreMax = 20;
this.hullMax = SHIP_HULL[type];
this.hull = SHIP_HULL[type];
this.shields = SHIP_SHIELD[type];
this.shieldsMax = SHIP_SHIELD[type];
this.maneuverLevel = type == SHIP_TYPE_FIGHTER ? 2 : 1;
this.maneuverCost = SHIP_MANEUVER_COST[type];
this.energyRegen = SHIP_ENERGY_RECHARGE[type];
this.energy = SHIP_ENERGY[type];
this.energyMax = SHIP_ENERGY[type];
this.accuracyBoost = 0;
this.crew = SHIP_MAX_CREW[type];
this.minCrew = SHIP_MIN_CREW[type];
this.maxCrew = SHIP_MAX_CREW[type];
this.maxPrisoners = Math.floor(SHIP_MAX_CREW[type]/2);
this.prisoners = randomNumber(0, this.maxPrisoners);
this.credits = SHIP_LOOT[type];
this.type = type;
this.flag = flag;
this.hasOrbitron = false;
if (flag == SHIP_FLAG_MERCHANT)
this.credits = 2*SHIP_LOOT[type];
switch (type) {
case SHIP_TYPE_FIGHTER:
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_FWD);
this.maxSpeed = 4;
break;
case SHIP_TYPE_SLOOP:
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_PORT);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_STBD);
this.maxSpeed = 3;
break;
case SHIP_TYPE_FRIGATE:
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_ION_CANNON), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_PORT);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_STBD);
this.maxSpeed = 3;
break;
case SHIP_TYPE_TRANSPORT:
this.mountWeapon(new Weapon(WEAPON_ION_CANNON), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_TRACTOR_BEAM), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_ION_CANNON), MOUNT_PORT);
this.mountWeapon(new Weapon(WEAPON_ION_CANNON), MOUNT_STBD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_AFT);
this.maxSpeed = 2;
break;
case SHIP_TYPE_CARRIER:
this.mountWeapon(new Weapon(WEAPON_HEAVY_ION), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_TRACTOR_BEAM), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_PORT);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_STBD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_AFT);
this.maxSpeed = 2;
break;
case SHIP_TYPE_BATTLESHIP:
this.mountWeapon(new Weapon(WEAPON_SIEGE_LASER), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_HEAVY_LASER), MOUNT_PORT);
this.mountWeapon(new Weapon(WEAPON_HEAVY_LASER), MOUNT_STBD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_STBD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_PORT);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_AFT);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_AFT);
this.maxSpeed = 2;
break;
case SHIP_TYPE_DREADNOUGHT:
this.mountWeapon(new Weapon(WEAPON_HEAVY_LASER), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_HEAVY_LASER), MOUNT_PORT);
this.mountWeapon(new Weapon(WEAPON_HEAVY_LASER), MOUNT_STBD);
this.mountWeapon(new Weapon(WEAPON_NEUTRON_BEAM), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_NEUTRON_BEAM), MOUNT_STBD);
this.mountWeapon(new Weapon(WEAPON_NEUTRON_BEAM), MOUNT_PORT);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_STBD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_PORT);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_AFT);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_AFT);
this.maxSpeed = 3;
break;
case SHIP_TYPE_STATION:
this.mountWeapon(new Weapon(WEAPON_SIEGE_LASER), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_SIEGE_LASER), MOUNT_STBD);
this.mountWeapon(new Weapon(WEAPON_SIEGE_LASER), MOUNT_PORT);
this.mountWeapon(new Weapon(WEAPON_SIEGE_LASER), MOUNT_AFT);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_STBD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_PORT);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_AFT);
this.maxSpeed = 0;
break;
case SHIP_TYPE_WRAITH:
this.mountWeapon(new Weapon(WEAPON_ION_CANNON), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_STBD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_PORT);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_AFT);
this.maxSpeed = 3;
break;
case SHIP_TYPE_ARBITER:
this.mountWeapon(new Weapon(WEAPON_ION_CANNON), MOUNT_FWD);
this.mountWeapon(new Weapon([WEAPON_SIPHON, WEAPON_MINDCONTROL, WEAPON_ZERO_POINT, WEAPON_REACTOR_OVERCHARGE, WEAPON_SINGULARITY, WEAPON_NEURAL_STATIC_PROJECTOR, WEAPON_GRAVATIC_SHEAR, WEAPON_PURIFICATION].random()), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_FWD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_STBD);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_PORT);
this.mountWeapon(new Weapon(WEAPON_LASER_CANNON), MOUNT_AFT);
this.maxSpeed = 3;
break;
}
switch (flag) {
case SHIP_FLAG_UNKNOWN:
case SHIP_FLAG_MERCHANT:
this.followPlayer = false;
this.attackPlayer = false;
this.followEnemies = false;
this.attackEnemies = false;
this.prisoners = 0;
this.maxPrisoners = 0;
break;
case SHIP_FLAG_PLAYER:
this.followPlayer = false;
this.attackPlayer = false;
this.followEnemies = true;
this.attackEnemies = true;
break;
case SHIP_FLAG_PIRATE:
this.followPlayer = true;
this.attackPlayer = true;
this.followEnemies = false;
this.attackEnemies = false;
this.prisoners = Math.min(2*this.prisoners, this.maxPrisoners);
break;
case SHIP_FLAG_PRECURSOR:
case SHIP_FLAG_KHAN:
this.followPlayer = true;
this.attackPlayer = true;
this.followEnemies = false;
this.attackEnemies = false;
break;
}
this.destroyed = false;
this.abandoned = false;
this.toBeDisintegrated = false;
this.mindControlByPlayerDuration = 0;
this.mindControlByEnemyDuration = 0;
this.currentWaypoint = null; //for npc navigation only
this.event = null; // Event triggered by investigating this ship
this.known_systems = [];
}
Ship.prototype = {
mountWeapon: function(weapon, mount) {
weapon.mount = mount;
weapon.readyToFire = false;
weapon.selected = false;
this.weapons.push(weapon);
},
powerDown: function() {
this.shields = 0;
this.energy = 0;
this.weapons.forEach((w) => {
w.selected = false;
w.readyToFire = false;
});
},
regenerateSystems: function() {
if(percentChance(this.crew-this.minCrew) && this.hull < this.hullMax && this.energy > 0) {
addTextToCombatLog(`The extra crew of the ${this.name} focus on damage control, and recover 1 point of hull.`);
this.hull = Math.min(this.hull + 1, this.hullMax);
}
if(this.shields > this.shieldsMax) {
this.shields--; // if you are overcharged, you slowly leak extra shields
} else if(this.energy >= this.energyMax/2) {
this.shields = Math.min(this.shields + 1, this.shieldsMax);
}
if(this.energy >= this.energyMax/2) {
this.warpCore = Math.min(this.warpCore + 1, this.warpCoreMax);
}
if(this.energy > 2*this.energyMax) {
addTextToCombatLog(`DANGER! Reactor meltdown in progress on board ${this.name}!`);
this.takeHullDamage(1); // reactor meltdown!
}
if(percentChance(-1*(this.crew-this.minCrew)) && this.energyRegen > 0) {
addTextToCombatLog(`With insufficient manning, the ${this.name} is unable to make use of reactor output.`);
} else if(this.energy > this.energyMax) {
this.energy--; // if you are overcharged, you slowly leak extra energy
} else {
this.energy = Math.min(this.energy + this.energyRegen, this.energyMax);
}
this.weapons.forEach((w) => { w.readyToFire = true; });
this.mindControlByPlayerDuration = Math.max(0, this.mindControlByPlayerDuration - 1);
this.mindControlByEnemyDuration = Math.max(0, this.mindControlByEnemyDuration - 1);
},
stop: function() {
this.xMoment = 0;
this.yMoment = 0;
this.xCursor = 0;
this.yCursor = 0;
},
loseCrew: function(number) {
this.crew = Math.max(0, this.crew - number);
if (this.crew <= 0 && this.minCrew > 0) {
this.abandoned = true;
this.energyRegen = 0;
this.maneuverLevel = 0;
this.crew = 0;
this.powerDown();
let weaponLoot = this.getLootWeapon();
if (!this.destroyed) // no double dipping
this.event = new LootAbandonedShipEvent(this);
addTextToCombatLog(this.name + ' has lost all crew and is defenseless.');
}
},
takeDamage: function(damage, damageType, attacker) {
switch (damageType) {
case DAMAGE_NORMAL:
this.takeNormalDamage(damage);
break;
case DAMAGE_ION:
this.takeIonDamage(damage);
break;
case DAMAGE_TRACTOR:
this.takeTractorDamage(damage);
break;
case DAMAGE_NEUTRON:
this.takeNeutronDamage(damage);
break;
case DAMAGE_MINDCONTROL:
this.takeMindControlDamage(damage, attacker);
break;
case DAMAGE_SIPHON:
this.takeSiphonDamage(damage, attacker);
break;
case DAMAGE_OVERLOAD:
this.takeOverchargeDamage(damage);
break;
}
return this.destroyed;
},
takeNormalDamage: function(damage) {
let damageAfterShields = Math.max(0, damage - Math.max(0, this.shields));
this.shields = Math.max(0, this.shields - damage);
this.hull = Math.max(0, this.hull - damageAfterShields);
if (percentChance(damageAfterShields*20)) {
this.loseCrew(1);
this.prisoners = Math.max(0, this.prisoners - 1);
}
if (!this.hull)
this.destroy();
},
takeHullDamage: function(damage) {
this.hull = Math.max(0, this.hull - damage);
if (!this.hull)
this.destroy();
},
takeIonDamage: function(damage) {
let damageAfterShields = Math.max(0, damage - Math.max(0, this.shields));
this.shields = Math.max(0, this.shields - damage);
this.energy -= damageAfterShields; // can go negative
},
takeTractorDamage: function(damage) {
if (this.shields > 0)
return;
if (this.xMoment > 0)
this.xMoment = Math.max(0, this.xMoment - damage);
if (this.xMoment < 0)
this.xMoment = Math.min(0, this.xMoment + damage);
if (this.yMoment > 0)
this.yMoment = Math.max(0, this.yMoment - damage);
if (this.yMoment < 0)
this.yMoment = Math.min(0, this.yMoment + damage);
},
takeNeutronDamage: function(damage) {
if (this.shields > 0)
return;
this.prisoners = Math.max(0, this.prisoners - damage);
this.loseCrew(damage);
},
takeMindControlDamage: function(damage, attacker) {
if (this.shields > 0)
return;
if (this.player)
return;
console.log(`${this.name} hit with mindcontrol by ${attacker.name} of the team ${TEAM_NAME[attacker.flag]}`);
switch (attacker.flag) {
case SHIP_FLAG_KHAN:
case SHIP_FLAG_PIRATE:
case SHIP_FLAG_PRECURSOR:
default:
this.mindControlByPlayerDuration = 0;
this.mindControlByEnemyDuration += damage;
break;
case SHIP_FLAG_PLAYER:
this.mindControlByEnemyDuration = 0;
this.mindControlByPlayerDuration += damage;
break;
}
console.log(`${this.name} mindcontrol by (player: ${this.mindControlByPlayerDuration}) (enemy: ${this.mindControlByEnemyDuration})`);
},
takeSiphonDamage: function(damage, attacker) {
let amountSiphoned = Math.min(this.shields, damage);
this.shields = Math.max(0, this.shields - damage);
attacker.shields += amountSiphoned;
},
takeOverchargeDamage: function(damage) {
this.energy += damage;
},
getLootWeapon: function() {
switch (this.type) {
case SHIP_TYPE_FIGHTER:
return WEAPON_LASER_CANNON;
case SHIP_TYPE_SLOOP:
return WEAPON_LASER_CANNON;
case SHIP_TYPE_FRIGATE:
return [WEAPON_LASER_CANNON, WEAPON_ION_CANNON].random();
case SHIP_TYPE_TRANSPORT:
return [WEAPON_TRACTOR_BEAM, WEAPON_ION_CANNON].random();
case SHIP_TYPE_CARRIER:
return [WEAPON_HEAVY_ION, WEAPON_ION_CANNON].random();
case SHIP_TYPE_BATTLESHIP:
return [WEAPON_SIEGE_LASER, WEAPON_HEAVY_LASER, WEAPON_LASER_CANNON].random();
case SHIP_TYPE_DREADNOUGHT:
return [WEAPON_HEAVY_LASER, WEAPON_NEUTRON_BEAM].random();
case SHIP_TYPE_STATION:
return [WEAPON_SIEGE_LASER, WEAPON_LASER_CANNON].random();
case SHIP_TYPE_WRAITH:
return [WEAPON_LASER_CANNON, WEAPON_ION_CANNON].random();
case SHIP_TYPE_ARBITER:
return _.find(this.weapons, (w) => { return w.artifact }).type;
}
},
destroy: function() {
if (!this.destroyed) {
this.hull = 0;
this.char = '#';
this.energyRegen = 0;
this.maneuverLevel = 0;
this.crew = 0;
this.prisoners = 0;
this.powerDown();
this.destroyed = true;
this.credits = Math.floor(this.credits/2);
let weaponLoot = this.getLootWeapon();
if (!this.abandoned) // no double dipping
this.event = new LootDestroyedShipEvent(this);
console.log(this.name + ' is destroyed');
}
},
getTurnsUntilCollision: function(map) {
for (let i = 0; i < 4; i++) {
if(!_.has(map, [this.xCoord+this.xMoment*i, this.yCoord+this.yMoment*i, 'forbiddenToAI']))
continue;
let space = map[this.xCoord+this.xMoment*i][this.yCoord+this.yMoment*i];
if (space.forbiddenToAI) {
return i;
}
}
return 5;
},
plotBetterCourse: function(map, astar) {
let caution = 3;
if (this.energy < this.maneuverCost)
return; //don't bother
let nextX = this.xCoord + this.xMoment;
let nextY = this.yCoord + this.yMoment;
let distToTargetX = Math.abs(astar._toX - nextX);
let distToTargetY = Math.abs(astar._toY - nextY);
let distToTarget = Math.max(distToTargetX, distToTargetY);
if (nextX >= MAP_WIDTH || nextX < 0 || nextY >= MAP_HEIGHT || nextY < 0) {
// dont' go off the map!
let directionBackToMapX = MAP_WIDTH/2-this.xMoment;
let directionBackToMapY = MAP_HEIGHT/2-this.yMoment;
this.xCursor = this.xMoment + Math.sign(directionBackToMapX);
this.yCursor = this.yMoment + Math.sign(directionBackToMapY);
return;
}
let currentSpeed = freeDiagonalDistance([this.xMoment, this.yMoment], [0,0]);
let desiredSpeed = Math.min(currentSpeed + 1, this.maxSpeed);
//reduce desired speed here if too close to destination
if (desiredSpeed && distToTarget/desiredSpeed < caution)
desiredSpeed--;
if (desiredSpeed && this.getTurnsUntilCollision(map) < caution) {
desiredSpeed = Math.max(1, desiredSpeed - 1);
}
let desiredCourse = [0, 0];
if (this.followPlayer) {
let stepCount = 0;
astar.compute(nextX, nextY, function(x, y) {
if (stepCount == desiredSpeed) {
desiredCourse = [x,y];
}
stepCount++;
});
}
//slow down!
while (desiredCourse[0] + this.xMoment - nextX > desiredSpeed)
desiredCourse[0]--;
while (desiredCourse[1] + this.yMoment - nextY > desiredSpeed)
desiredCourse[1]--;
while (desiredCourse[0] + this.xMoment - nextX < -desiredSpeed)
desiredCourse[0]++;
while (desiredCourse[1] + this.yMoment - nextY < -desiredSpeed)
desiredCourse[1]++;
this.xCursor = this.xMoment + Math.sign(desiredCourse[0] - nextX);
this.yCursor = this.yMoment + Math.sign(desiredCourse[1] - nextY);
},
getHighlightColor: function() {
if (this.player)
return "#0E4";
if (this.mindControlByPlayerDuration || this.mindControlByEnemyDuration)
return "purple";
if (this.attackPlayer)
return "red";
return "yellow";
},
toggleSelectedWeapon: function() { //only useful for player
let selectedWeapon = _.find(this.weapons, (w) => w.selected);
let startingIndex = 0;
if (selectedWeapon) {
startingIndex = Math.min(this.weapons.length - 1, _.indexOf(this.weapons, selectedWeapon) + 1);
}
let nextUnselectedWeapon = _.find(this.weapons, (w) => { return !w.selected && w.readyToFire && this.energy >= w.energy; }, startingIndex);
if (!nextUnselectedWeapon)
nextUnselectedWeapon = _.find(this.weapons, (w) => { return !w.selected && w.readyToFire && this.energy >= w.energy; });
if (nextUnselectedWeapon) {
nextUnselectedWeapon.selected = true;
if(selectedWeapon)
selectedWeapon.selected = false;
}
},
fireSelectedWeapon: function() { //only useful for player
let selectedWeapon = _.find(this.weapons, (w) => w.selected);
if(selectedWeapon) {
this.fireWeapon(selectedWeapon);
}
},
fireWeapon: function(weapon) {
if (this.canFireWeapon(weapon)) {
console.log(`firing ${weapon.name}`);
this.energy -= weapon.energy;
weapon.readyToFire = false;
this.toggleSelectedWeapon();
} else {
console.log(`${this.name} does not have enough energy to fire ${weapon.name}`);
}
},
canFireWeapon: function(weapon) {
return weapon.readyToFire && this.energy >= weapon.energy;
},
activeWeapon: function() {
return _.find(this.weapons, (w) => { return this.canFireWeapon(w) && w.selected; });
},
canBeHitByWeapon: function(attacker, weapon) {
if (freeDiagonalDistance([attacker.xCoord, attacker.yCoord], [this.xCoord, this.yCoord]) > weapon.range)
return false;
return this.inFiringArc(attacker, weapon);
},
inFiringArc: function(attacker, weapon) {
if (this.xCoord == attacker.xCoord && this.yCoord == attacker.yCoord)
return false; //can't hit someone right on top of you
let octant = getFiringOctant(attacker.facing, weapon.mount);
// doing this based off the direction vector left as an exercise for the reader
switch (octant) {
case SE:
return this.yCoord >= attacker.yCoord && this.xCoord >= attacker.xCoord;
case SW:
return this.yCoord >= attacker.yCoord && this.xCoord <= attacker.xCoord;
case NW:
return this.yCoord <= attacker.yCoord && this.xCoord <= attacker.xCoord;
case NE:
return this.yCoord <= attacker.yCoord && this.xCoord >= attacker.xCoord;
case EAST:
return this.xCoord > attacker.xCoord && Math.abs(this.yCoord - attacker.yCoord) <= this.xCoord - attacker.xCoord;
case WEST:
return this.xCoord < attacker.xCoord && Math.abs(this.yCoord - attacker.yCoord) <= attacker.xCoord - this.xCoord;
case NORTH:
return this.yCoord < attacker.yCoord && Math.abs(this.xCoord - attacker.xCoord) <= attacker.yCoord - this.yCoord;
case SOUTH:
return this.yCoord > attacker.yCoord && Math.abs(this.xCoord - attacker.xCoord) <= this.yCoord - attacker.yCoord;
}
return true;
},
getChanceToHit: function(weapon, target) {
let distance = freeDiagonalDistance([this.xCoord, this.yCoord], [target.xCoord, target.yCoord]);
let targetSpeed = target.speed();
if (distance > weapon.range) {
return { prob: 0, modifiers: ['Out of range'] }
}
if (!target.inFiringArc(this, weapon)) {
return { prob: 0, modifiers: ['Not in firing arc'] };
}
let totalHitProb = weapon.accuracy;
let hitModifiers = [`+${weapon.accuracy.toString().padEnd(2)} Base weapon accuracy`];
if (this.accuracyBoost) {
totalHitProb += this.accuracyBoost;
hitModifiers.push(`+${this.accuracyBoost.toString().padEnd(2)} Targeting computer`);
}
totalHitProb -= distance;
hitModifiers.push(`-${distance.toString().padEnd(2)} Distance`);
let speedMod = 5*targetSpeed;
totalHitProb -= speedMod;
hitModifiers.push(`-${speedMod.toString().padEnd(2)} Target speed`);
let firingDirection = unitVector(this.xCoord - target.xCoord, this.yCoord - target.yCoord);
let transverseMod = Math.floor(10*crossProduct(firingDirection, [target.xMoment, target.yMoment]));
totalHitProb -= transverseMod;
hitModifiers.push(`-${transverseMod.toString().padEnd(2)} Transverse velocity`);
return { prob: Math.min(95, Math.max(5, totalHitProb)), modifiers: hitModifiers };
},
fireAt: function(weapon, target) {
this.fireWeapon(weapon);
if (weapon.makeNeutralsHostile) {
target.attackPlayer = true;
target.followPlayer = true;
}
let prob = this.getChanceToHit(weapon, target).prob;
if (percentChance(prob)) {
let initiallyDestroyed = target.destroyed;
let initialShields = target.shields;
target.takeDamage(weapon.damage, weapon.damageType, this);
let messageString = `${this.name} fires at ${target.name} with its ${weapon.name} (${prob}%) and hits`;
if (!initiallyDestroyed && target.destroyed) {
addTextToCombatLog(messageString + `, destroying it.`);
} else {
switch (weapon.damageType) {
case DAMAGE_NORMAL:
addTextToCombatLog(messageString + `, dealing ${weapon.damage} damage.`);
break;
case DAMAGE_ION:
addTextToCombatLog(messageString + `, draining shields and energy for ${weapon.damage} damage.`);
break;
case DAMAGE_TRACTOR:
if (target.shields > 0)
addTextToCombatLog(messageString + `, but the target's shields prevent the weapon from taking effect.`);
else
addTextToCombatLog(messageString + `, reducing the target's speed to ${target.speed()}.`);
break;
case DAMAGE_NEUTRON:
if (target.shields > 0)
addTextToCombatLog(messageString + `, but the target's shields prevent the weapon from taking effect.`);
else
addTextToCombatLog(messageString + `, dealing ${weapon.damage} directly to the crew.`);
break;
case DAMAGE_MINDCONTROL:
if (target.shields > 0)
addTextToCombatLog(messageString + `, but the target's shields prevent the weapon from taking effect.`);
else
addTextToCombatLog(messageString + `, seizing control of the hostile crew's minds.`);
break;
case DAMAGE_SIPHON:
if (initialShields <= 0)
addTextToCombatLog(messageString + `, but the target has no shields to drain.`);
else
addTextToCombatLog(messageString + `, draining the target's shields of ${initialShields-target.shields} points.`);
break;
case DAMAGE_OVERLOAD:
addTextToCombatLog(messageString + `, sending a massive surge of energy directly into the target's reactor.`);
break;
default:
addTextToCombatLog(messageString);
break;
}
}
return true;
}
addTextToCombatLog(`${this.name} fires at ${target.name} with its ${weapon.name} (${prob}%) and misses`);
return false;
},
speed: function() {
return freeDiagonalDistance([this.xMoment, this.yMoment], [0, 0]);
}
}