-
Notifications
You must be signed in to change notification settings - Fork 30
/
monster_type.cpp
609 lines (535 loc) · 15.5 KB
/
monster_type.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
#include "window.h"
#include "stringfunc.h"
#include "monster_type.h"
#include "globals.h"
#include "rng.h"
#include "monster_ability.h"
#include <sstream>
Monster_type::Monster_type()
{
genus = NULL;
name = "Unknown";
uid = -1;
sym = glyph();
size = MON_SIZE_NULL;
total_attack_weight = 0;
speed = 0;
accuracy = 0;
/* Since 0 is a valid value for dodge, we default it to -1; -1 means "hasn't
* been set," and may be copied from a default.
*/
dodge = -1;
chance = 100;
hp_set = false;
attacks_copied_from_genus = false;
ranged_attacks_copied_from_genus = false;
abilities_copied_from_genus = false;
senses_copied_from_genus = false;
idle_sound_chance = 0;
attack_sound_chance = 0;
idle_sound_volume = 0;
attack_sound_volume = 0;
for (int i = 0; i < SENSE_MAX; i++) {
senses.push_back(false);
}
for (int i = 0; i < DAMAGE_MAX; i++) {
armor.push_back(0);
}
}
Monster_type::~Monster_type()
{
if (!abilities_copied_from_genus) {
for (int i = 0; i < abilities.size(); i++) {
delete abilities[i];
}
}
}
void Monster_type::set_genus(Monster_genus *mg)
{
if (!mg) {
genus = NULL;
return;
}
genus = mg;
// Copy any unset values from the genus
if (size == MON_SIZE_NULL) {
size = mg->default_values.size;
}
if (attacks.empty()) {
attacks = mg->default_values.attacks;
if (!attacks.empty()) {
attacks_copied_from_genus = true;
}
total_attack_weight = mg->default_values.total_attack_weight;
}
if (ranged_attacks.empty()) {
ranged_attacks = mg->default_values.ranged_attacks;
if (!ranged_attacks.empty()) {
ranged_attacks_copied_from_genus = true;
}
total_ranged_attack_weight = mg->default_values.total_ranged_attack_weight;
}
if (abilities.empty()) {
abilities = mg->default_values.abilities;
if (!abilities.empty()) {
abilities_copied_from_genus = true;
}
total_ability_weight = mg->default_values.total_ability_weight;
}
if (idle_sounds.empty()) {
idle_sounds = mg->default_values.idle_sounds;
idle_sound_chance = mg->default_values.idle_sound_chance;
idle_sound_volume = mg->default_values.idle_sound_volume;
}
if (attack_sounds.empty()) {
attack_sounds = mg->default_values.attack_sounds;
attack_sound_chance = mg->default_values.attack_sound_chance;
attack_sound_volume = mg->default_values.attack_sound_volume;
}
if (!hp_set) { // hp_set is true if monsters.dat includes HP for this monster
hp_dice = mg->default_values.hp_dice;
}
if (speed == 0) {
speed = mg->default_values.speed;
}
if (accuracy == 0) {
accuracy = mg->default_values.accuracy;
}
if (dodge == -1) {
dodge = mg->default_values.dodge;
}
AI = mg->default_values.AI;
// Only copy senses if we haven't set any already
bool any_senses_set = false;
for (int i = 0; !any_senses_set && i < senses.size(); i++) {
any_senses_set = senses[i];
}
if (!any_senses_set) {
senses_copied_from_genus = true;
senses = mg->default_values.senses;
}
// Only copy armor if we haven't set any already
bool any_armor_set = false;
for (int i = 0; !any_armor_set && i < armor.size(); i++) {
if (armor[i] > 0) {
any_armor_set = true;
}
}
if (!any_armor_set) {
armor_copied_from_genus = true;
armor = mg->default_values.armor;
}
}
void Monster_type::assign_uid(int id)
{
uid = id;
}
std::string Monster_type::get_data_name()
{
return name;
}
std::string Monster_type::get_name()
{
if (display_name.empty()) {
return name;
}
return display_name;
}
std::string Monster_type::get_name_plural()
{
if (display_name_plural.empty()) {
return get_name() + "s"; // Guess at plural.
}
return display_name_plural;
}
bool Monster_type::load_data(std::istream &data)
{
std::string ident, junk;
while (ident != "done" && !data.eof()) {
if ( ! (data >> ident) ) {
return false;
}
ident = no_caps(ident);
if (!ident.empty() && ident[0] == '#') {
// It's a comment
std::getline(data, junk);
} else if (ident == "name:") {
std::getline(data, name);
name = trim(name);
} else if (ident == "display_name:") {
std::getline(data, display_name);
display_name = trim(display_name);
} else if (ident == "name_plural:") {
std::getline(data, display_name_plural);
display_name_plural = trim(display_name_plural);
} else if (ident == "genus:") {
std::string genus_name;
std::getline(data, genus_name);
genus_name = trim(genus_name);
Monster_genus *mg = MONSTER_GENERA.lookup_name(genus_name);
if (!mg) {
debugmsg("Unknown Monster_genus '%s' (%s)",
genus_name.c_str(), name.c_str());
} else {
set_genus(mg);
}
} else if (ident == "glyph:") {
sym.load_data_text(data);
std::getline(data, junk);
} else if (ident == "size:") {
std::string size_text;
std::getline(data, size_text);
size_text = no_caps( trim( size_text ) );
size = lookup_monster_size(size_text);
if (size == MON_SIZE_NULL) {
debugmsg("Unknown monster size '%s' (%s)", size_text.c_str(),
name.c_str());
return false;
}
} else if (ident == "hp:") {
if (!hp_dice.load_data(data, name)) {
return false;
}
hp_set = true;
} else if (ident == "armor:") {
// Reset all armor to 0, if they were copied from our genus
// (Because we want to completely override the defaults)
if (armor_copied_from_genus) {
armor_copied_from_genus = false;
for (int i = 0; i < armor.size(); i++) {
armor[i] = 0;
}
}
std::getline(data, junk);
std::string armor_ident;
do {
data >> armor_ident;
armor_ident = trim( no_caps( armor_ident ) );
if (armor_ident != "done") {
Damage_type dam = lookup_damage_type(armor_ident);
if (dam == DAMAGE_NULL) {
debugmsg("Unknown armor type '%s' (%s)", armor_ident.c_str(),
name.c_str());
return false;
}
data >> armor[dam];
}
} while (armor_ident != "done");
std::getline(data, junk);
// End of "armor:" block
} else if (ident == "speed:") {
data >> speed;
std::getline(data, junk);
} else if (ident == "accuracy:") {
data >> accuracy;
std::getline(data, junk);
} else if (ident == "dodge:") {
data >> dodge;
std::getline(data, junk);
} else if (ident == "chance:") {
data >> chance;
std::getline(data, junk);
} else if (ident == "senses:") {
// Reset all senses to false, if they were copied from our genus
// (Because we want to completely override the defaults)
if (senses_copied_from_genus) {
senses_copied_from_genus = false;
for (int i = 0; i < senses.size(); i++) {
senses[i] = false;
}
}
std::string sense_line;
std::getline(data, sense_line);
std::istringstream sense_data(sense_line);
std::string sense_name;
while (sense_data >> sense_name) {
senses[ lookup_sense_type(sense_name) ] = true;
}
} else if (ident == "attack:") {
// Remove all attacks, if they were copied from our genus
// (Because we want to completely override the defaults)
if (attacks_copied_from_genus) {
attacks_copied_from_genus = false;
attacks.clear();
total_attack_weight = 0;
}
std::getline(data, junk);
Attack tmpattack;
if (!tmpattack.load_data(data, name)) {
debugmsg("Attack failed to load (%s)", name.c_str());
return false;
}
attacks.push_back(tmpattack);
total_attack_weight += tmpattack.weight;
} else if (ident == "ranged_attack:") {
// Remove all ranged attacks, if they were copied from our genus
// (Because we want to completely override the defaults)
if (ranged_attacks_copied_from_genus) {
ranged_attacks_copied_from_genus = false;
ranged_attacks.clear();
total_ranged_attack_weight = 0;
}
std::getline(data, junk);
Ranged_attack tmpattack;
tmpattack.type = RANGED_ATT_OTHER; // It's a natural attack
if (!tmpattack.load_data(data, name)) {
debugmsg("Ranged attack failed to load (%s)", name.c_str());
return false;
}
ranged_attacks.push_back(tmpattack);
total_ranged_attack_weight += tmpattack.weight;
} else if (ident == "ability:") {
// Remove all abilities, if they were copied from our genus
// (Because we want to completely override the defaults)
if (abilities_copied_from_genus) {
abilities_copied_from_genus = false;
abilities.clear();
total_ability_weight = 0;
}
std::string type_name;
std::getline(data, type_name);
Monster_ability_type abil_type = lookup_monster_ability_type(type_name);
if (abil_type == MON_ABILITY_NULL) {
debugmsg("Unknown Monster_ability_type '%s' (%s)",
type_name.c_str(), name.c_str());
return false;
}
Monster_ability* abil;
switch (abil_type) {
case MON_ABILITY_SUMMON:
abil = new Monster_ability_summon;
break;
case MON_ABILITY_SIGNAL:
abil = new Monster_ability_signal;
break;
case MON_ABILITY_TERRAIN:
abil = new Monster_ability_terrain;
break;
case MON_ABILITY_TELEPORT:
abil = new Monster_ability_teleport;
break;
case MON_ABILITY_FIELDS:
abil = new Monster_ability_fields;
break;
default:
debugmsg("BUG - need to edit monster_type.cpp to handle %s.",
monster_ability_type_name(abil_type).c_str());
return false;
break;
}
if (!abil->load_data(data, name)) {
debugmsg("Failed to load ability '%s' (%s).",
monster_ability_type_name(abil_type).c_str(), name.c_str());
return false;
}
total_ability_weight += abil->weight;
abilities.push_back(abil);
} else if (ident == "ai:") {
std::getline(data, junk);
AI.load_data(data, name);
} else if (ident == "idle_sound_chance:") {
data >> idle_sound_chance;
std::getline(data, junk);
} else if (ident == "attack_sound_chance:") {
data >> attack_sound_chance;
std::getline(data, junk);
} else if (ident == "idle_sound_volume:") {
data >> idle_sound_volume;
std::getline(data, junk);
} else if (ident == "attack_sound_volume:") {
data >> attack_sound_volume;
std::getline(data, junk);
} else if (ident == "idle_sounds:") {
std::string sound_line;
std::getline(data, sound_line);
std::istringstream sound_data(sound_line);
if (!idle_sounds.load_data(sound_data, name)) {
debugmsg("'%s' failed to load idle sounds.", name.c_str());
return false;
}
} else if (ident == "attack_sounds:") {
std::string sound_line;
std::getline(data, sound_line);
std::istringstream sound_data(sound_line);
if (!attack_sounds.load_data(sound_data, name)) {
debugmsg("'%s' failed to load attack sounds.", name.c_str());
return false;
}
} else if (ident != "done") {
debugmsg("Unknown monster property '%s' (%s)",
ident.c_str(), name.c_str());
return false;
}
}
// Don't add ourselfs to the genus until *after* we've loaded our values!
if (genus) {
genus->add_member(this);
}
/* If dodge is still -1, it wasn't set by the genus OR this specific monster;
* so set it to 0.
*/
if (dodge < 0) {
dodge = 0;
}
// Likewise, if we never set size, then set it to medium (a good default)
if (size == MON_SIZE_NULL) {
size = MON_SIZE_MEDIUM;
}
return true;
}
bool Monster_type::has_sense(Sense_type sense)
{
return senses[sense];
}
int Monster_type::get_weight()
{
switch (size) {
case MON_SIZE_NULL: return 0;
case MON_SIZE_TINY: return 40;
case MON_SIZE_SMALL: return 400;
case MON_SIZE_MEDIUM: return 1500;
case MON_SIZE_LARGE: return 5000;
case MON_SIZE_HUGE: return 20000;
}
return 0;
}
int Monster_type::get_volume()
{
switch (size) {
case MON_SIZE_NULL: return 0;
case MON_SIZE_TINY: return 50;
case MON_SIZE_SMALL: return 1200;
case MON_SIZE_MEDIUM: return 3000;
case MON_SIZE_LARGE: return 12000;
case MON_SIZE_HUGE: return 50000;
}
return 0;
}
Sound Monster_type::get_sound(bool attacking)
{
if (!one_in( (attacking ? attack_sound_chance : idle_sound_chance ) )) {
return Sound();
}
Variable_string* sound_bin = (attacking ? &attack_sounds : &idle_sounds);
if (sound_bin->empty()) {
return Sound();
}
Sound ret;
ret.description = sound_bin->pick();
ret.volume = (attacking ? attack_sound_volume : idle_sound_volume);
return ret;
}
Monster_ability* Monster_type::get_ability()
{
if (abilities.empty()) {
return NULL;
}
int index = rng(1, total_ability_weight);
for (int i = 0; i < abilities.size(); i++) {
index -= abilities[i]->weight;
if (index <= 0) {
return abilities[i];
}
}
return abilities.back();
}
Monster_genus::Monster_genus()
{
uid = -1;
total_chance = 0;
}
Monster_genus::~Monster_genus()
{
}
void Monster_genus::add_member(Monster_type* member)
{
if (!member) {
return;
}
total_chance += member->chance;
members.push_back(member);
}
Monster_type* Monster_genus::random_member()
{
if (members.empty()) {
return NULL;
}
int index = rng(1, total_chance);
for (int i = 0; i < members.size(); i++) {
index -= members[i]->chance;
if (index <= 0) {
return members[i];
}
}
return members.back();
}
void Monster_genus::assign_uid(int id)
{
uid = id;
}
std::string Monster_genus::get_data_name()
{
return name;
}
std::string Monster_genus::get_name()
{
if (display_name.empty()) {
return name;
}
return display_name;
}
bool Monster_genus::load_data(std::istream &data)
{
std::string ident, junk;
while (ident != "done" && !data.eof()) {
if ( ! (data >> ident) ) {
return false;
}
ident = no_caps(ident);
if (!ident.empty() && ident[0] == '#') {
// It's a comment
std::getline(data, junk);
} else if (ident == "name:") {
std::getline(data, name);
name = trim(name);
} else if (ident == "display_name:") {
std::getline(data, display_name);
display_name = trim(display_name);
} else if (ident == "default:") {
if (!default_values.load_data(data)) {
debugmsg("Genus %s failed to load default values", name.c_str());
return false;
}
} else if (ident != "done") {
debugmsg("Unknown monster genus property '%s' (%s)",
ident.c_str(), name.c_str());
return false;
}
}
return true;
}
Monster_size lookup_monster_size(std::string name)
{
name = no_caps( trim( name ) );
for (int i = 0; i < MON_SIZE_MAX; i++) {
Monster_size ret = Monster_size(i);
if (no_caps( monster_size_name(ret) ) == name) {
return ret;
}
}
return MON_SIZE_NULL;
}
std::string monster_size_name(Monster_size size)
{
switch (size) {
case MON_SIZE_NULL: return "NULL";
case MON_SIZE_TINY: return "tiny";
case MON_SIZE_SMALL: return "small";
case MON_SIZE_MEDIUM: return "medium";
case MON_SIZE_LARGE: return "large";
case MON_SIZE_HUGE: return "huge";
case MON_SIZE_MAX: return "BUG - MON_SIZE_MAX";
default: return "BUG - Unnamed Monster_size";
}
return "BUG - Escaped monster_size_name switch";
}