/
Character.ts
361 lines (284 loc) · 9.89 KB
/
Character.ts
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
import {computed, observable} from 'mobx';
import {StaticState} from '../StaticState';
import {identifier, list, map, object, primitive, reference, serializable} from 'serializr';
import {Experienced} from './Experienced';
import {CharacterClassInfo} from './CharacterClassInfo';
import {Item} from './Item';
import {QuirkInfo} from './QuirkInfo';
import {CharacterStatus} from './CharacterStatus';
import {SkillId, SkillTargetObject} from './SkillInfo';
import {Stats, TurnStats} from './Stats';
import {cap, contains, removeItem, without} from '../../lib/Helpers';
import {v4 as uuid} from 'uuid';
import {BuildingInfoId} from './BuildingInfo';
import {Skill} from './Skill';
import {ItemType} from './ItemInfo';
import {randomizeItem} from '../../lib/Helpers';
export type CharacterId = string;
export class Character extends Experienced {
@serializable(identifier()) id: CharacterId = uuid();
@serializable @observable name: string;
@serializable(reference(CharacterClassInfo, StaticState.lookup((i) => i.classes)))
classInfo: CharacterClassInfo;
@serializable(reference(QuirkInfo, StaticState.lookup((i) => i.quirks)))
affliction: QuirkInfo = null;
@serializable(list(reference(QuirkInfo, StaticState.lookup((i) => i.quirks))))
quirks: QuirkInfo[] = [];
@computed get perks () {
return Object.freeze(
this.quirks.filter((q) => !q.isDisease && q.isPositive)
);
}
@computed get flaws () {
return Object.freeze(
this.quirks.filter((q) => !q.isDisease && q.isNegative)
);
}
@computed get diseases () {
return Object.freeze(
this.quirks.filter((q) => q.isDisease)
);
}
@serializable(list(object(Item)))
@observable
items: Item[] = [];
@serializable @observable hunger: number = 0;
@serializable @observable classInfoStatsScale = 1;
@serializable @observable buffSourceName: string;
@serializable(object(TurnStats))
@observable
buff: TurnStats;
@serializable(map(object(TurnStats)))
@observable
dots = new Map<CharacterStatus, TurnStats>();
@serializable(object(Stats))
@observable mutableStats = new Stats();
@serializable(map(primitive()))
@observable
private skillLevels = new Map<SkillId, number>();
@serializable(map(primitive()))
@observable
private skillSelections = new Map<SkillId, boolean>();
get skills () {
return this.classInfo.skills.map((info) => new Skill(this.skillLevels, this.skillSelections, info));
}
get selectedSkills () {
return this.skills.filter((skill) => skill.isSelected);
}
@computed get scaledClassInfo () {
const scaled = {...this.classInfo}; // Clone to avoid mutating static instance
scaled.stats = new Stats();
scaled.stats.add(this.classInfo.stats);
scaled.stats.scale(this.classInfoStatsScale);
return scaled;
}
@computed get armor () {
return this.items.find((i) => i.info.type === ItemType.Armor);
}
@computed get weapon () {
return this.items.find((i) => i.info.type === ItemType.Weapon);
}
@computed get trinkets () {
return this.items.filter((i) => i.info.type === ItemType.Trinket);
}
@computed get stats () {
const sum = new Stats();
sum.add(this.scaledClassInfo);
sum.add(this.mutableStats);
if (this.buff) {
sum.add(this.buff, {
stats: new Stats(),
name: this.buffSourceName,
statsSourceName: this.buff.isPositive ? 'Buff' : 'Debuff'
});
}
if (this.affliction) {
sum.add(this.affliction);
}
this.quirks
.forEach((source) => sum.add(source));
this.items
.forEach((source) => sum.add(source));
return sum;
}
@computed get isAlive () {
return this.stats.health.value > 0;
}
@computed get isDead () {
return this.stats.health.value === 0;
}
acceptsTreatmentFrom (treatmentId: BuildingInfoId) {
for (const quirk of this.quirks) {
if (contains(quirk.bannedTreatmentIds, treatmentId)) {
return false;
}
}
const allForcedTreatmentIds = this.quirks.reduce(
(all, q) => {
all.push(...q.forcedTreatmentIds);
return all;
},
[]
);
if (allForcedTreatmentIds.length > 0) {
return contains(allForcedTreatmentIds, treatmentId);
}
return true;
}
offsetStats (offset: Stats) {
const updatedStats = new Stats();
updatedStats.add(this.mutableStats);
updatedStats.add(offset);
this.mutableStats = updatedStats;
}
resetMutableStats () {
const reset = new Stats();
reset.health.value = this.stats.maxHealth.value;
reset.stress.value = 100; // cap(reset.stress.value, 0, this.stats.maxStress.value / 2);
this.mutableStats = reset;
this.dots.clear();
this.removeBuffs();
}
processTurn () {
if (this.buff) {
this.buff.turns -= 1;
if (this.buff.turns <= 0) {
this.removeBuffs();
}
}
const memento = new Stats();
this.dots.forEach((stats, status) => {
const delta = this.applyStats(true, stats);
// Add damage memento
memento.add(delta);
// Add status memento
memento.statuses.get(status).value = 1;
stats.turns -= 1;
if (stats.turns <= 0) {
this.dots.delete(status);
}
});
return memento;
}
applyStats (isCrit: boolean, actionStats: Stats) {
const targetStats = this.stats;
// Calculate stats changes in the target
const deltaStats = new Stats();
// Reduce health by damage, ignoring protect for critical hits
deltaStats.health.value += Math.min(
0, (isCrit ? 0 : 1) * targetStats.protect.value - actionStats.damage.value
);
// Apply flat stress/health changes (ie. heals), double value for crits
deltaStats.health.value += (isCrit ? 2 : 1) * actionStats.health.value;
deltaStats.stress.value += (isCrit ? 2 : 1) * actionStats.stress.value;
// Cap stress and health changes
const minStressDelta = -targetStats.stress.value;
const maxStressDelta = targetStats.maxStress.value - targetStats.stress.value;
deltaStats.stress.value = cap(deltaStats.stress.value, minStressDelta, maxStressDelta);
const minHealthDelta = -targetStats.health.value;
const maxHealthDelta = targetStats.maxHealth.value - targetStats.health.value;
deltaStats.health.value = cap(deltaStats.health.value, minHealthDelta, maxHealthDelta);
deltaStats.round();
this.offsetStats(deltaStats);
return deltaStats;
}
replaceQuirk (newQuirk?: QuirkInfo) {
if (newQuirk === undefined) {
const newQuirksForHero = without(StaticState.instance.quirks, this.quirks);
newQuirk = randomizeItem(newQuirksForHero);
}
let replacedQuirk;
if (this.quirks.length) {
replacedQuirk = randomizeItem(this.quirks);
removeItem(this.quirks, replacedQuirk);
}
this.quirks.push(newQuirk);
return {
replacedQuirk,
newQuirk
};
}
applyBuff (buff: TurnStats, sourceName: string) {
// TODO support multiple buffs
const buffClone = new TurnStats();
buffClone.turns = buff.turns;
buffClone.add(buff);
this.buffSourceName = sourceName;
this.buff = buffClone;
}
removeBuffs () {
this.buff = null;
this.buffSourceName = null;
}
useSkill (skill: Skill, target: Character) {
const targetStats = target.stats;
// Turn character and skill stats into action stats
const actionStats = new Stats();
actionStats.add(this.stats.battle);
actionStats.add(skill.stats);
// Make accuracy and damage numbers slightly random
const strength = 0.8 + Math.random () * 0.2;
const precision = 0.8 + Math.random () * 0.2;
actionStats.damage.value *= skill.damageScale * strength;
actionStats.accuracy.value *= precision;
actionStats.health.value *= strength;
actionStats.stress.value *= strength;
const isAllyTarget = skill.info.target.object === SkillTargetObject.Ally;
const isCrit = Math.random() <= actionStats.criticalChance.value;
const willBasicHit = isCrit || actionStats.accuracy.value > targetStats.dodge.value;
const willBuffHit = Math.random() <= Character.getStatusHitChance(
CharacterStatus.Buff, actionStats, targetStats
);
const willHit = isAllyTarget || willBasicHit;
const memento = willHit ? target.applyStats(isCrit, actionStats) : new Stats();
// Add crit memento
if (isCrit) {
memento.criticalChance.value = 1;
}
// Apply buff for this skill
if (skill.buff && (isAllyTarget || willBuffHit)) {
target.applyBuff(skill.buff, skill.info.name);
// Add buff memento
memento.statuses.get(CharacterStatus.Buff).value = 1;
}
// Apply status effects for this skill
skill.stats.statuses.forEach((s, status) => {
const willStatusHit = Math.random() <= Character.getStatusHitChance(status, actionStats, targetStats);
if (!willStatusHit) {
return;
}
const turnStats = new TurnStats();
const damageScale = actionStats.statusDamageScales.get(status).value;
turnStats.turns = skill.info.statusTurns;
turnStats.damage.value = actionStats.damage.value * damageScale;
turnStats.round();
// Damage or stuns last over time
if (turnStats.damage.value || status === CharacterStatus.Stun) {
target.dots.set(status, turnStats);
}
// Add status memento
memento.statuses.get(status).value = 1;
});
return memento;
}
applyItem (item: Item) {
this.applyStats(false, item.stats);
item.stats.statuses.forEach((value, status) => {
this.dots.delete(status);
});
if (item.info.removeBuffs) {
this.removeBuffs();
}
if (item.info.buff) {
this.applyBuff(item.info.buff, item.name);
}
if (item.info.resetHunger) {
this.hunger = 0;
}
}
static getStatusHitChance (status: CharacterStatus, actionStats: Stats, targetStats: Stats) {
const chance = actionStats.statuses.get(status).value;
const resistance = targetStats.resistances.get(status).value;
return chance - resistance;
}
}