forked from CleverRaven/Cataclysm-DDA
/
mutation.cpp
395 lines (354 loc) · 9.26 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
#include "player.h"
#include "mutation.h"
#include "game.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);
void player::mutate(game *g)
{
bool force_bad = one_in(3); // 33% chance!
if (has_trait(PF_ROBUST) && force_bad && one_in(3))
force_bad = false; // 11% chance!
// First, see if we should ugrade/extend an existing mutation
std::vector<pl_flag> upgrades;
for (int i = 1; i < PF_MAX2; i++) {
if (has_trait(i)) {
for (int j = 0; j < g->mutation_data[i].replacements.size(); j++) {
pl_flag tmp = g->mutation_data[i].replacements[j];
if (!has_trait(tmp) && !has_child_flag(g, tmp) &&
(!force_bad || traits[tmp].points <= 0))
upgrades.push_back(tmp);
}
for (int j = 0; j < g->mutation_data[i].additions.size(); j++) {
pl_flag tmp = g->mutation_data[i].additions[j];
if (!has_trait(tmp) && !has_child_flag(g, tmp) &&
(!force_bad || traits[tmp].points <= 0))
upgrades.push_back(tmp);
}
}
}
if (upgrades.size() > 0 && rng(0, upgrades.size() + 4) < upgrades.size()) {
mutate_towards(g, upgrades[ rng(0, upgrades.size() - 1) ]);
return;
}
// Next, see if we should mutate within a given category
mutation_category cat = MUTCAT_NULL;
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];
}
}
if (rng(0, total) > highest)
cat = MUTCAT_NULL; // Not a strong enough pull, just mutate something random!
std::vector<pl_flag> valid; // Valid mutations
bool first_pass = (cat != MUTCAT_NULL);
do {
// If we tried once with a non-NULL category, and couldn't find anything valid
// there, try again with MUTCAT_NULL
if (cat != MUTCAT_NULL && !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, or that we have a child of, or that's
// positive and we're forcing bad
for (int i = 0; i < valid.size(); i++) {
if (has_trait(valid[i]) || has_child_flag(g, valid[i]) ||
(force_bad && traits[ valid[i] ].points > 0)) {
valid.erase(valid.begin() + i);
i--;
}
}
if (valid.empty())
first_pass = false; // So we won't repeat endlessly
} while (valid.empty() && cat != MUTCAT_NULL);
if (valid.empty())
return; // Couldn't find anything at all!
pl_flag selection = valid[ rng(0, valid.size() - 1) ]; // Pick one!
mutate_towards(g, selection);
}
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;
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--;
}
}
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_trait(mut);
if (replacing != PF_NULL) {
g->add_msg("Your %s turns into %s!", traits[replacing].name.c_str(),
traits[mut].name.c_str());
toggle_trait(replacing);
} else
g->add_msg("You gain %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 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;
}
}
toggle_trait(mut);
if (replacing != PF_NULL) {
g->add_msg("Your %s turns into %s.", traits[mut].name.c_str(),
traits[replacing].name.c_str());
toggle_trait(replacing);
mutation_loss_effect(g, *this, mut);
mutation_effect(g, *this, replacing);
} else {
g->add_msg("You lose your %s.", traits[mut].name.c_str());
mutation_loss_effect(g, *this, mut);
}
}
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, skip_cloth = false;
std::vector<body_part> bps;
switch (mut) {
// 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:
skip_cloth = true;
bps.push_back(bp_head);
break;
case PF_STR_UP:
p.str_max ++;
break;
case PF_STR_UP_2:
p.str_max ++;
break;
case PF_STR_UP_3:
p.str_max += 2;
break;
case PF_STR_UP_4:
p.str_max += 3;
break;
case PF_DEX_UP:
p.dex_max ++;
break;
case PF_DEX_UP_2:
p.dex_max ++;
break;
case PF_DEX_UP_3:
p.dex_max += 2;
break;
case PF_DEX_UP_4:
p.dex_max += 3 ;
break;
case PF_INT_UP:
p.int_max ++;
break;
case PF_INT_UP_2:
p.int_max ++;
break;
case PF_INT_UP_3:
p.int_max += 2;
break;
case PF_INT_UP_4:
p.int_max += 3;
break;
case PF_PER_UP:
p.per_max ++;
break;
case PF_PER_UP_2:
p.per_max ++;
break;
case PF_PER_UP_3:
p.per_max += 2;
break;
case PF_PER_UP_4:
p.per_max += 3;
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());
} else {
if (is_u)
g->add_msg("Your %s is pushed off.", p.worn[i].tname().c_str());
g->m.add_item(p.posx, p.posy, p.worn[i]);
}
p.worn.erase(p.worn.begin() + i);
i--;
}
}
}
}
void mutation_loss_effect(game *g, player &p, pl_flag mut)
{
switch (mut) {
case PF_STR_UP:
p.str_max--;
break;
case PF_STR_UP_2:
p.str_max -= 2;
break;
case PF_STR_UP_3:
p.str_max -= 3;
break;
case PF_STR_UP_4:
p.str_max -= 4;
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 -= 3;
break;
case PF_DEX_UP_4:
p.dex_max -= 4;
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 -= 3;
break;
case PF_INT_UP_4:
p.int_max -= 4;
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 -= 3;
break;
case PF_PER_UP_4:
p.per_max -= 4;
break;
}
}