forked from CleverRaven/Cataclysm-DDA
/
mutation.cpp
637 lines (579 loc) · 16.5 KB
/
mutation.cpp
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
#include "player.h"
#include "mutation.h"
#include "game.h"
#include "translations.h"
// mutation_effect handles things like destruction of armor, etc.
void mutation_effect(game *g, player &p, pl_flag mut);
// mutation_loss_effect handles what happens when you lose a mutation
void mutation_loss_effect(game *g, player &p, pl_flag mut);
bool player::mutation_ok(game *g, pl_flag mutation, bool force_good, bool force_bad)
{
if (has_trait(mutation) || has_child_flag(g, mutation))
{
// We already have this mutation or something that replaces it.
return false;
}
if (force_bad && traits[mutation].points > 0)
{
// This is a good mutation, and we're due for a bad one.
return false;
}
if (force_good && traits[mutation].points < 0)
{
// This is a bad mutation, and we're due for a good one.
return false;
}
return true;
}
void player::mutate(game *g)
{
bool force_bad = one_in(3);
bool force_good = false;
if (has_trait(PF_ROBUST) && force_bad)
{
// Robust Genetics gives you a 33% chance for a good mutation,
// instead of the 33% chance of a bad one.
force_bad = false;
force_good = true;
}
// Determine the mutation categorie
mutation_category cat = MUTCAT_NULL;
// Count up the players number of mutations in categories and find
// the category with the highest single count.
int total = 0, highest = 0;
for (int i = 0; i < NUM_MUTATION_CATEGORIES; i++)
{
total += mutation_category_level[i];
if (mutation_category_level[i] > highest)
{
cat = mutation_category(i);
highest = mutation_category_level[i];
}
}
// See if we should ugrade/extend an existing mutation...
std::vector<pl_flag> upgrades;
// ... or remove one that is not in our highest category
std::vector<pl_flag> downgrades;
// For each mutation...
for (int base_mutation_index = 1; base_mutation_index < PF_MAX2; base_mutation_index++)
{
pl_flag base_mutation = (pl_flag) base_mutation_index;
// ...that we have...
if (has_trait(base_mutation))
{
// ...consider the mutations that replace it.
for (int i = 0; i < g->mutation_data[base_mutation].replacements.size(); i++)
{
pl_flag mutation = g->mutation_data[base_mutation].replacements[i];
if (mutation_ok(g, mutation, force_good, force_bad))
{
upgrades.push_back(mutation);
}
}
// ...consider the mutations that add to it.
for (int i = 0; i < g->mutation_data[base_mutation].additions.size(); i++)
{
pl_flag mutation = g->mutation_data[base_mutation].additions[i];
if (mutation_ok(g, mutation, force_good, force_bad))
{
upgrades.push_back(mutation);
}
}
// ...consider whether its in our highest category
if( has_trait(base_mutation) && !has_base_trait(base_mutation) ){ // Starting traits don't count toward categories
std::vector<pl_flag> group = mutations_from_category(cat);
bool in_cat = false;
for (int j = 0; j < group.size(); j++)
{
if (group[j] == base_mutation)
{
in_cat = true;
break;
}
}
// mark for removal
if(!in_cat) downgrades.push_back(base_mutation);
}
}
}
// Preliminary round to either upgrade or remove existing mutations
if(one_in(2)){
if (upgrades.size() > 0)
{
// (upgrade count) chances to pick an upgrade, 4 chances to pick something else.
int roll = rng(0, upgrades.size() + 4);
if (roll < upgrades.size())
{
// We got a valid upgrade index, so use it and return.
mutate_towards(g, upgrades[roll]);
return;
}
}
}
else {
// Remove existing mutations that don't fit into our category
if (downgrades.size() > 0 && cat != MUTCAT_NULL)
{
int roll = rng(0, downgrades.size() + 4);
if (roll < downgrades.size())
{
remove_mutation(g, downgrades[roll]);
return;
}
}
}
std::vector<pl_flag> valid; // Valid mutations
bool first_pass = true;
do
{
// If we tried once with a non-NULL category, and couldn't find anything valid
// there, try again with MUTCAT_NULL
if (!first_pass)
{
cat = MUTCAT_NULL;
}
if (cat == MUTCAT_NULL)
{
// Pull the full list
for (int i = 1; i < PF_MAX2; i++)
{
if (g->mutation_data[i].valid)
{
valid.push_back( pl_flag(i) );
}
}
}
else
{
// Pull the category's list
valid = mutations_from_category(cat);
}
// Remove anything we already have, that we have a child of, or that
// goes against our intention of a good/bad mutation
for (int i = 0; i < valid.size(); i++)
{
if (!mutation_ok(g, valid[i], force_good, force_bad))
{
valid.erase(valid.begin() + i);
i--;
}
}
if (valid.empty())
{
// So we won't repeat endlessly
first_pass = false;
}
}
while (valid.empty() && cat != MUTCAT_NULL);
if (valid.empty())
{
// Couldn't find anything at all!
return;
}
pl_flag selection = valid[ rng(0, valid.size() - 1) ]; // Pick one!
mutate_towards(g, selection);
}
void player::mutate_category(game *g, mutation_category cat)
{
bool force_bad = one_in(3);
bool force_good = false;
if (has_trait(PF_ROBUST) && force_bad)
{
// Robust Genetics gives you a 33% chance for a good mutation,
// instead of the 33% chance of a bad one.
force_bad = false;
force_good = true;
}
// Pull the category's list for valid mutations
std::vector<pl_flag> valid;
valid = mutations_from_category(cat);
// Remove anything we already have, that we have a child of, or that
// goes against our intention of a good/bad mutation
for (int i = 0; i < valid.size(); i++)
{
if (!mutation_ok(g, valid[i], force_good, force_bad))
{
valid.erase(valid.begin() + i);
i--;
}
}
// if we can't mutate in the category do nothing
if (valid.empty()) return;
pl_flag selection = valid[ rng(0, valid.size() - 1) ]; // Pick one!
mutate_towards(g, selection);
return;
}
void player::mutate_towards(game *g, pl_flag mut)
{
if (has_child_flag(g, mut)) {
remove_child_flag(g, mut);
return;
}
bool has_prereqs = false;
pl_flag canceltrait = PF_NULL;
std::vector<pl_flag> prereq = g->mutation_data[mut].prereqs;
std::vector<pl_flag> cancel = g->mutation_data[mut].cancels;
for (int i = 0; i < cancel.size(); i++) {
if (!has_trait( cancel[i] )) {
cancel.erase(cancel.begin() + i);
i--;
} else {
//If we have the trait, but it's a base trait, don't allow it to be removed normally
if (has_base_trait( cancel[i] )) {
canceltrait = cancel[i];
cancel.erase(cancel.begin() + i);
i--;
}
}
}
if (!cancel.empty()) {
pl_flag removed = cancel[ rng(0, cancel.size() - 1) ];
remove_mutation(g, removed);
return;
}
for (int i = 0; !has_prereqs && i < prereq.size(); i++) {
if (has_trait(prereq[i]))
has_prereqs = true;
}
if (!has_prereqs && !prereq.empty()) {
pl_flag devel = prereq[ rng(0, prereq.size() - 1) ];
mutate_towards(g, devel);
return;
}
// Check if one of the prereqs that we have TURNS INTO this one
pl_flag replacing = PF_NULL;
prereq = g->mutation_data[mut].prereqs; // Reset it
for (int i = 0; i < prereq.size(); i++) {
if (has_trait(prereq[i])) {
pl_flag pre = prereq[i];
for (int j = 0; replacing == PF_NULL &&
j < g->mutation_data[pre].replacements.size(); j++) {
if (g->mutation_data[pre].replacements[j] == mut)
replacing = pre;
}
}
}
toggle_mutation(mut);
if (replacing != PF_NULL)
{
g->add_msg(_("Your %1$s mutation turns into %2$s!"), traits[replacing].name.c_str(),
traits[mut].name.c_str());
toggle_mutation(replacing);
mutation_loss_effect(g, *this, replacing);
mutation_effect(g, *this, mut);
}
else
// If this new mutation cancels a base trait, remove it and add the mutation at the same time
if (canceltrait != PF_NULL)
{
g->add_msg(_("Your innate %1$s trait turns into %2$s!"), traits[canceltrait].name.c_str(),
traits[mut].name.c_str());
toggle_mutation(canceltrait);
mutation_loss_effect(g, *this, canceltrait);
mutation_effect(g, *this, mut);
}
else
{
g->add_msg(_("You gain a mutation called %s!"), traits[mut].name.c_str());
mutation_effect(g, *this, mut);
}
// Weight us towards any categories that include this mutation
for (int i = 0; i < NUM_MUTATION_CATEGORIES; i++) {
std::vector<pl_flag> group = mutations_from_category(mutation_category(i));
bool found = false;
for (int j = 0; !found && j < group.size(); j++) {
if (group[j] == mut)
found = true;
}
if (found)
mutation_category_level[i] += 8;
else if (mutation_category_level[i] > 0 &&
!one_in(mutation_category_level[i]))
mutation_category_level[i]--;
}
}
void player::remove_mutation(game *g, pl_flag mut)
{
// Check for dependant mutations first
std::vector<pl_flag> dependant;
for (int i = 0; i < PF_MAX2; i++)
{
for (std::vector<pl_flag>::iterator it = g->mutation_data[i].prereqs.begin();
it != g->mutation_data[i].prereqs.end(); it++)
{
if (*it == i)
{
dependant.push_back((pl_flag)i);
break;
}
}
}
if (dependant.size() != 0)
{
remove_mutation(g,dependant[rng(0,dependant.size())]);
return;
}
// Check if there's a prereq we should shrink back into
pl_flag replacing = PF_NULL;
std::vector<pl_flag> originals = g->mutation_data[mut].prereqs;
for (int i = 0; replacing == PF_NULL && i < originals.size(); i++) {
pl_flag pre = originals[i];
for (int j = 0; replacing == PF_NULL &&
j < g->mutation_data[pre].replacements.size(); j++) {
if (g->mutation_data[pre].replacements[j] == mut)
replacing = pre;
}
}
// See if this mutation is cancelled by a base trait
//Only if there's no prereq to shrink to, thus we're at the bottom of the trait line
if (replacing == PF_NULL) {
//Check each mutation until we reach the end or find a trait to revert to
for (int i = 1; replacing == PF_NULL && i < PF_MAX2; i++) {
//See if it's in our list of base traits but not active
if (has_base_trait(i) && !has_trait(i)) {
//See if that base trait cancels the mutation we are using
std::vector<pl_flag> traitcheck = g->mutation_data[i].cancels;
if (!traitcheck.empty()) {
for (int j = 0; replacing == PF_NULL && j < traitcheck.size(); j++) {
if (g->mutation_data[i].cancels[j] == mut)
replacing = ((pl_flag)i);
}
}
}
}
}
// This should revert back to a removed base trait rather than simply removing the mutation
toggle_mutation(mut);
if (replacing != PF_NULL) {
g->add_msg(_("Your %1$s mutation turns into %2$s."), traits[mut].name.c_str(),
traits[replacing].name.c_str());
toggle_mutation(replacing);
mutation_loss_effect(g, *this, mut);
mutation_effect(g, *this, replacing);
} else {
g->add_msg(_("You lose your %s mutation."), traits[mut].name.c_str());
mutation_loss_effect(g, *this, mut);
}
// Reduce the strength of the categories the removed mutation is a part of
for (int i = 0; i < NUM_MUTATION_CATEGORIES; i++) {
std::vector<pl_flag> group = mutations_from_category(mutation_category(i));
bool found = false;
for (int j = 0; !found && j < group.size(); j++) {
if (group[j] == mut)
found = true;
}
if (found) {
mutation_category_level[i] -= 8;
// If the category strength is below 0, set it to 0. We don't want negative category strength.
if (mutation_category_level[i] < 0) {
mutation_category_level[i] = 0;
}
}
}
}
bool player::has_child_flag(game *g, pl_flag flag)
{
for (int i = 0; i < g->mutation_data[flag].replacements.size(); i++) {
pl_flag tmp = g->mutation_data[flag].replacements[i];
if (has_trait(tmp) || has_child_flag(g, tmp))
return true;
}
return false;
}
void player::remove_child_flag(game *g, pl_flag flag)
{
for (int i = 0; i < g->mutation_data[flag].replacements.size(); i++) {
pl_flag tmp = g->mutation_data[flag].replacements[i];
if (has_trait(tmp)) {
remove_mutation(g, tmp);
return;
} else if (has_child_flag(g, tmp)) {
remove_child_flag(g, tmp);
return;
}
}
}
void mutation_effect(game *g, player &p, pl_flag mut)
{
bool is_u = (&p == &(g->u));
bool destroy = false;
std::vector<body_part> bps;
switch (mut) {
case PF_TOUGH:
case PF_GLASSJAW:
case PF_HARDCORE:
p.recalc_hp();
// Push off gloves
case PF_WEBBED:
case PF_ARM_TENTACLES:
case PF_ARM_TENTACLES_4:
case PF_ARM_TENTACLES_8:
bps.push_back(bp_hands);
break;
// Destroy gloves
case PF_TALONS:
destroy = true;
bps.push_back(bp_hands);
break;
// Destroy mouthwear
case PF_BEAK:
case PF_MANDIBLES:
destroy = true;
bps.push_back(bp_mouth);
break;
// Destroy footwear
case PF_HOOVES:
destroy = true;
bps.push_back(bp_feet);
break;
// Destroy torsowear
case PF_SHELL:
destroy = true;
bps.push_back(bp_torso);
break;
// Push off all helmets
case PF_HORNS_CURLED:
case PF_CHITIN3:
bps.push_back(bp_head);
break;
// Push off non-cloth helmets
case PF_HORNS_POINTED:
case PF_ANTENNAE:
case PF_ANTLERS:
bps.push_back(bp_head);
break;
case PF_STR_UP:
p.str_max ++;
p.recalc_hp();
break;
case PF_STR_UP_2:
p.str_max += 2;
p.recalc_hp();
break;
case PF_STR_UP_3:
p.str_max += 4;
p.recalc_hp();
break;
case PF_STR_UP_4:
p.str_max += 7;
p.recalc_hp();
break;
case PF_DEX_UP:
p.dex_max ++;
break;
case PF_DEX_UP_2:
p.dex_max += 2;
break;
case PF_DEX_UP_3:
p.dex_max += 4;
break;
case PF_DEX_UP_4:
p.dex_max += 7;
break;
case PF_INT_UP:
p.int_max ++;
break;
case PF_INT_UP_2:
p.int_max += 2;
break;
case PF_INT_UP_3:
p.int_max += 4;
break;
case PF_INT_UP_4:
p.int_max += 7;
break;
case PF_PER_UP:
p.per_max ++;
break;
case PF_PER_UP_2:
p.per_max += 2;
break;
case PF_PER_UP_3:
p.per_max += 4;
break;
case PF_PER_UP_4:
p.per_max += 7;
break;
}
for (int i = 0; i < p.worn.size(); i++) {
for (int j = 0; j < bps.size(); j++) {
if ((dynamic_cast<it_armor*>(p.worn[i].type))->covers & mfb(bps[j])) {
if (destroy) {
if (is_u)
g->add_msg(_("Your %s is destroyed!"), p.worn[i].tname().c_str());
p.worn.erase(p.worn.begin() + i);
} else {
if (is_u)
g->add_msg(_("Your %s is pushed off."), p.worn[i].tname().c_str());
char tmp_invlet = p.worn[i].invlet;
g->m.add_item(p.posx, p.posy, p.worn[i]);
p.takeoff( g, p.worn[i].invlet, true );
p.i_rem( tmp_invlet );
}
// Reset to the start of the vector
i = 0;
}
}
}
}
void mutation_loss_effect(game *g, player &p, pl_flag mut)
{
switch (mut) {
case PF_TOUGH:
case PF_GLASSJAW:
case PF_HARDCORE:
p.recalc_hp();
case PF_STR_UP:
p.str_max--;
p.recalc_hp();
break;
case PF_STR_UP_2:
p.str_max -= 2;
p.recalc_hp();
break;
case PF_STR_UP_3:
p.str_max -= 4;
p.recalc_hp();
break;
case PF_STR_UP_4:
p.str_max -= 7;
p.recalc_hp();
break;
case PF_DEX_UP:
p.dex_max--;
break;
case PF_DEX_UP_2:
p.dex_max -= 2;
break;
case PF_DEX_UP_3:
p.dex_max -= 4;
break;
case PF_DEX_UP_4:
p.dex_max -= 7;
break;
case PF_INT_UP:
p.int_max--;
break;
case PF_INT_UP_2:
p.int_max -= 2;
break;
case PF_INT_UP_3:
p.int_max -= 4;
break;
case PF_INT_UP_4:
p.int_max -= 7;
break;
case PF_PER_UP:
p.per_max--;
break;
case PF_PER_UP_2:
p.per_max -= 2;
break;
case PF_PER_UP_3:
p.per_max -= 4;
break;
case PF_PER_UP_4:
p.per_max -= 7;
break;
}
}