-
Notifications
You must be signed in to change notification settings - Fork 2
/
executeAttack.js
151 lines (118 loc) · 5.74 KB
/
executeAttack.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
/**
Copyright 2014 Andrew P. Sillers
This file is part of Lords of the Fey.
Lords of the Fey is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Lords of the Fey is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Lords of the Fey. If not, see <http://www.gnu.org/licenses/>.
*/
/** @module executeAttack */
/**
Represents a single swing of an attack
@typedef {Object} SwingResult
@prop {boolean} offense - is swing by initiator?
@prop {string} event - "hit" or "miss"
@prop {number} damage - damage inflicted by this swing
@prop {boolean} kill - did this swing kill the hitee?
*/
/**
Offender attacks defender with the attack of the given index
@param {Unit} offender - unit initiating attack
@param {boolean} attackIndex - index of attack used by offensive unit
@returns {SwingResult[]} an array of objects representing swings from this combat, in order
*/
module.exports = function executeAttack(offender, attackIndex, attackSpace, defender, units, mapData, game) {
var battleRecord = [];
var swingResult;
var defenseIndex;
var offense = offender.attacks[attackIndex];
offense = defender.applyAttack(offense, offender, game.timeOfDay, attackSpace, units, mapData);
var defenderCover = defender.getCoverOnSpace(mapData[defender.x+","+defender.y], offense, true);
var offenderCover = offender.getCoverOnSpace(mapData[offender.x+","+offender.y]);
var defenseChoice = defender.selectDefense(offender, offense, game.timeOfDay, offenderCover, defenderCover);
var defense = offender.applyAttack(defenseChoice.defense, defender, game.timeOfDay, defender, units, mapData);
var defenseIndex = defenseChoice.defenseIndex;
offenderCover = offender.getCoverOnSpace(mapData[offender.x+","+offender.y], defense, false);
if(defense) {
var defenseFirst = ((defense.properties||[]).indexOf("firststrike") != -1) > ((offense.properties||[]).indexOf("firststrike") != -1);
} else {
var defenseFirst = false;
}
for(var round = 0; round < offense.number || (defense && round < defense.number); round++) {
if(defenseFirst) {
if(defense && round < defense.number) {
swingResult = attackSwing(false, defense, defender, offender, offenderCover, units);
battleRecord.push(swingResult);
if(swingResult.kill) { break; }
}
}
if(round < offense.number) {
swingResult = attackSwing(true, offense, offender, defender, defenderCover, units);
battleRecord.push(swingResult);
if(swingResult.kill) { break; }
}
if(!defenseFirst) {
if(defense && round < defense.number) {
swingResult = attackSwing(false, defense, defender, offender, offenderCover, units);
battleRecord.push(swingResult);
if(swingResult.kill) { break; }
}
}
}
function awardXp(thisUnit, enemy, isOffender) {
if(thisUnit.hp > 0) {
if(enemy.hp > 0) {
thisUnit.xp += enemy.level || 1;
} else {
thisUnit.xp += (enemy.level * 8) || 4;
}
var xpBeforeLevel = thisUnit.xp;
var owner = game.players.filter(function(p) { return p.team == thisUnit.team; })[0];
var originalUnit = thisUnit;
while(thisUnit.xp >= thisUnit.maxXp) {
var oldXp = thisUnit.xp;
thisUnit = require("./levelUp").getLevelUp(thisUnit, owner, isOffender);
// if the XP is above leveling threshold but did not decrease, we hit a branching prompt
if(oldXp == thisUnit.xp) { break; }
}
// we must apply the new leveled-up properties to the original Unit object passed into the function
require("./levelUp").applyNewProperties(originalUnit, thisUnit);
return xpBeforeLevel;
}
return thisUnit.xp;
}
var offenseXp = awardXp(offender, defender, true);
var defenseXp = awardXp(defender, offender, false);
return { record: battleRecord, offender: {x: offender.x, y: offender.y}, defender: {x: defender.x, y: defender.y}, offenseIndex: attackIndex, defenseIndex: defenseIndex, xp: {offense: offenseXp, defense: defenseXp } };
}
// perform one swing of an attack, by the hitter, on the hittee
// return a swing record object
function attackSwing(isOffense, attack, hitter, hittee, hitteeCover, units) {
var swingRecord;
if(Math.random() > hitteeCover) {
hittee.hp -= attack.damage;
swingRecord = { event: "hit", offense: isOffense, damage: attack.damage };
if(hittee.hp <= 0) {
swingRecord.kill = true;
}
if(attack.properties) {
if(attack.properties.indexOf("poison") != -1 && !hittee.hasCondition("poisoned")) {
hittee.addCondition("poisoned");
swingRecord.poisoned = true;
}
if(attack.properties.indexOf("slows") != -1 && !hittee.hasCondition("slowed")) {
hittee.addCondition({ name: "slowed", countdown: 2 });
swingRecord.slowed = { name: "slowed", countdown: 2 };
}
}
} else {
swingRecord = { event: "miss", offense: isOffense };
}
return swingRecord;
}