forked from Whales/Cataclysm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mutation.cpp
281 lines (249 loc) · 7.45 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
#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);
void player::mutate(game *g)
{
bool force_bad = one_in(2); // 50% chance!
if (has_trait(PF_ROBUST) && force_bad && one_in(2))
force_bad = false; // 25% 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;
}
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; i < prereq.size(); i++) {
if (has_trait(prereq[i])) {
prereq.erase(prereq.begin() + i);
i--;
}
}
if (!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_effect(g, *this, replacing);
} else
g->add_msg("You lose your %s.", traits[mut].name.c_str());
}
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;
}
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--;
}
}
}
}