forked from Whales/Cataclysm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem_type.cpp
672 lines (576 loc) · 16.3 KB
/
item_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
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#include "item_type.h"
#include "stringfunc.h"
#include "window.h"
#include <sstream>
Item_type::Item_type()
{
uid = -1;
name = "bug";
sym = glyph();
weight = 0;
volume = 0;
for (int i = 0; i < DAMAGE_MAX; i++) {
damage[i] = 0;
}
to_hit = 0;
attack_speed = 0;
thrown_variance = Dice(8, 20, 0);
thrown_dmg_percent = 50;
thrown_speed = 0;
for (int i = 0; i < ITEM_FLAG_MAX; i++) {
flags.push_back(false);
}
}
Item_type::~Item_type()
{
}
Item_type_clothing::Item_type_clothing()
{
carry_capacity = 0;
armor_bash = 0;
armor_cut = 0;
armor_pierce = 0;
encumbrance = 0;
}
std::string Item_type_clothing::get_property_description()
{
std::stringstream ret;
ret << "<c=white>Covers:<c=/> ";
bool printed_any = false;
for (int i = 0; i < BODY_PART_MAX; i++) {
if (covers[i]) {
if (printed_any) {
ret << ", ";
} else {
printed_any = true;
}
ret << body_part_name( Body_part(i) );
}
}
ret << std::endl;
ret << "<c=ltblue>Storage space: ";
if (carry_capacity == 0) {
ret << "<c=dkgray>";
} else if (carry_capacity >= 25) { // TODO: Change / don't hardcode this
ret << "<c=white>";
} else {
ret << "<c=ltgray>";
}
ret << carry_capacity << "<c=/>\n";
ret << "<c=magenta>Encumbrance: ";
if (encumbrance == 0) {
ret << "<c=white>";
} else if (encumbrance < 3) {
ret << "<c=ltgray>";
} else if (encumbrance < 5) {
ret << "<c=ltred>";
} else {
ret << "<c=red>";
}
ret << encumbrance << std::endl;
// Set up vectors for use in color_gradient() below
std::vector<int> breakpoints;
breakpoints.push_back(0);
breakpoints.push_back(10);
std::vector<nc_color> colors;
colors.push_back( c_dkgray );
colors.push_back( c_ltgray );
colors.push_back( c_white );
ret << "<c=brown>Armor (bash): " <<
color_gradient(armor_bash, breakpoints, colors) << armor_bash <<
"<c=/>\n";
ret << "<c=brown>Armor (cut): " <<
color_gradient(armor_cut, breakpoints, colors) << armor_cut <<
"<c=/>\n";
ret << "<c=brown>Armor (pierce): " <<
color_gradient(armor_pierce, breakpoints, colors) << armor_pierce <<
"<c=/>\n";
return ret.str();
}
Item_type_ammo::Item_type_ammo()
{
damage = 0;
armor_pierce = 10;
range = 0;
count = 100;
}
std::string Item_type_ammo::get_property_description()
{
std::stringstream ret;
ret << "<c=white>Type:<c=/> " << ammo_type << std::endl;
ret << "<c=cyan>Accuracy:<c=/> " << accuracy.str() << std::endl;
ret << "<c=red>Damage:<c=white> " << damage << "\n<c=ltblue>Armor Piercing: ";
if (armor_pierce == 10) {
ret << "<c=dkgray>None";
} else {
if (armor_pierce < 10) {
ret << "<c=red>";
} else {
ret << "<c=white>";
}
ret << "x" << armor_pierce / 10 << "." << armor_pierce % 10;
}
ret << "<c=/>\n";
ret << "<c=ltgray>Range: " << range << "<c=/>\n";
return ret.str();
}
Item_type_launcher::Item_type_launcher()
{
damage = 0;
recoil = 0;
durability = 100;
capacity = 15;
reload_ap = 300;
fire_ap = 100;
}
std::string Item_type_launcher::get_property_description()
{
std::stringstream ret;
ret << "<c=white>Ammo:<c=/> " << ammo_type << std::endl;
ret << "<c=ltred>Damage bonus:<c=/> " << (damage >= 0 ? "+" : "") <<
damage << std::endl;
ret << "<c=cyan>Accuracy:<c=/> " << accuracy.str() << std::endl;
ret << "<c=magenta>Recoil:<c=/> " << (recoil >= 0 ? "+" : "") <<
recoil << std::endl;
ret << "<c=brown>Durability:<c=/> " << durability << std::endl;
ret << "<c=ltblue>Capacity:<c=/> " << capacity << " rounds" << std::endl;
ret << "<c=green>Reload time:<c=/> " << reload_ap / 100 << "." <<
(reload_ap % 100) / 10 << reload_ap % 10 << " turns" << std::endl;
ret << "<c=ltgreen>Fire time:<c=/> " << fire_ap / 100 << "." <<
(fire_ap % 100) / 10 << fire_ap % 10 << " turns" << std::endl;
ret << "<c=red>Fire modes:<c=/> ";
// TODO: If there's special mods, like "snipe," handle them specially
for (int i = 0; i < modes.size(); i++) {
ret << "[" << modes[i] << "] ";
}
return ret.str();
}
Item_type_food::Item_type_food()
{
food = 0;
water = 0;
charges = 1;
verb = "eat";
}
std::string Item_type_food::get_property_description()
{
std::stringstream ret;
ret << "<c=green>Nutrition: <c=/>" << food << std::endl;
ret << "<c=ltblue>Water: <c=/>" << water << std::endl;
return ret.str();
}
Item_type_tool::Item_type_tool()
{
default_charges = 0;
max_charges = 0;
countdown_timer = 0;
}
// We won't try to handle the Tool_action here - that's better done by hand
std::string Item_type_tool::get_property_description()
{
std::stringstream ret;
ret << "<c=yellow>Fuel:<c=/> " << fuel << std::endl;
ret << "<c=white>Max charges:<c=/> " << max_charges << std::endl;
ret << "<c=green>Time to apply:<c=/> ";
if (applied_action.real) {
ret << applied_action.ap_cost / 100 << ".";
if (applied_action.ap_cost % 100 < 10) {
ret << "0";
}
ret << applied_action.ap_cost % 100 << " turns";
} else {
ret << "N/A";
}
return ret.str();
}
void Item_type::assign_uid(int id)
{
uid = id;
}
std::string Item_type::get_data_name()
{
return name;
}
std::string Item_type::get_name()
{
if (display_name.empty()) {
return name;
}
return display_name;
}
bool Item_type::load_data(std::istream &data)
{
std::string ident, junk;
bool set_name = false, set_glyph = false;
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);
set_name = true;
} else if (ident == "display_name:") {
std::getline(data, display_name);
display_name = trim(display_name);
} else if (ident == "description:") {
std::string desc;
description = "";
while (no_caps(desc) != "done") {
std::getline(data, desc);
desc = trim(desc);
if (no_caps(desc) != "done") {
description = description + " " + desc;
}
}
description = trim(description); // Get rid of extra " "
} else if (ident == "glyph:") {
sym.load_data_text(data);
std::getline(data, junk);
set_glyph = true;
} else if (ident == "weight:") {
data >> weight;
std::getline(data, junk);
} else if (ident == "volume:") {
data >> volume;
std::getline(data, junk);
} else if (ident == "bash:") {
data >> damage[DAMAGE_BASH];
std::getline(data, junk);
} else if (ident == "cut:") {
data >> damage[DAMAGE_CUT];
std::getline(data, junk);
} else if (ident == "pierce:") {
data >> damage[DAMAGE_PIERCE];
std::getline(data, junk);
} else if (ident == "to_hit:") {
data >> to_hit;
std::getline(data, junk);
} else if (ident == "speed:" || ident == "attack_speed:") {
data >> attack_speed;
std::getline(data, junk);
} else if (ident == "thrown_variance:") {
if (!thrown_variance.load_data(data, name)) {
return false;
}
} else if (ident == "thrown_dmg_percent:") {
data >> thrown_dmg_percent;
std::getline(data, junk);
} else if (ident == "thrown_speed:") {
data >> thrown_speed;
std::getline(data, junk);
} else if (ident == "flags:") {
std::string flag_line;
std::getline(data, flag_line);
std::istringstream flag_data(flag_line);
std::string flag_name;
while (flag_data >> flag_name) {
Item_flag flag = lookup_item_flag(flag_name);
if (flag == ITEM_FLAG_NULL) {
debugmsg("Unknown item flag '%s' (%s)",
flag_name.c_str(), name.c_str());
return false;
}
flags[flag] = true;
}
} else if (!handle_data(ident, data)) {
debugmsg("Unknown item_type flag '%s' (%s)", ident.c_str(), name.c_str());
return false;
}
}
// Ensure that we set a glyph and name!
if (!set_name) {
debugmsg("Item created without a name!");
return false;
}
if (!set_glyph) {
debugmsg("Item '%s' created without a glyph!", name.c_str());
return false;
}
return true;
}
bool Item_type::handle_data(std::string ident, std::istream &data)
{
if (ident == "done") {
return true;
}
return false;
}
bool Item_type::has_flag(Item_flag flag)
{
return flags[flag];
}
/* TODO: Right now, armor{_bash,_cut,_pierce} is hard-coded here. But what if
* we add a damage type and want to protect against it with armor? We
* should generalize and look up damage type names instead.
*/
bool Item_type_clothing::handle_data(std::string ident, std::istream &data)
{
std::string junk;
if (ident == "carries:") {
data >> carry_capacity;
std::getline(data, junk);
} else if (ident == "armor_bash:") {
data >> armor_bash;
std::getline(data, junk);
} else if (ident == "armor_cut:") {
data >> armor_cut;
std::getline(data, junk);
} else if (ident == "armor_pierce:") {
data >> armor_pierce;
std::getline(data, junk);
} else if (ident == "encumbrance:") {
data >> encumbrance;
std::getline(data, junk);
} else if (ident == "covers:") {
std::string line;
std::getline(data, line);
std::istringstream cover_data(line);
std::string body_part_name;
while (cover_data >> body_part_name) {
std::vector<Body_part> parts = get_body_part_list( body_part_name );
for (int i = 0; i < parts.size(); i++) {
covers[ parts[i] ] = true;
}
if (parts.empty()) {
debugmsg("Unknown body part '%s' (%s)", body_part_name.c_str(),
name.c_str());
return false;
}
}
} else if (ident != "done") {
return false;
}
return true;
}
bool Item_type_ammo::handle_data(std::string ident, std::istream &data)
{
std::string junk;
if (ident == "type:") {
std::getline(data, ammo_type);
} else if (ident == "damage:") {
data >> damage;
std::getline(data, junk);
} else if (ident == "armor_pierce:" || ident == "pierce:") {
data >> armor_pierce;
if (armor_pierce <= 0) {
armor_pierce = 10;
}
std::getline(data, junk);
} else if (ident == "range:") {
data >> range;
std::getline(data, junk);
} else if (ident == "accuracy:") {
if (!accuracy.load_data(data, name)) {
return false;
}
} else if (ident == "count:") {
data >> count;
std::getline(data, junk);
} else if (ident != "done") {
return false;
}
return true;
}
bool Item_type_launcher::handle_data(std::string ident, std::istream &data)
{
std::string junk;
if (ident == "type:" || ident == "ammo_type:") {
std::getline(data, ammo_type);
} else if (ident == "damage:") {
data >> damage;
std::getline(data, junk);
} else if (ident == "accuracy:") {
if (!accuracy.load_data(data, name)) {
return false;
}
} else if (ident == "recoil:") {
data >> recoil;
std::getline(data, junk);
} else if (ident == "durability:") {
data >> durability;
std::getline(data, junk);
// "Clip" and "magazine" to satisfy the gun nerds. what up gun nerds
} else if (ident == "clip:" || ident == "magazine:" || ident == "capacity:") {
data >> capacity;
std::getline(data, junk);
} else if (ident == "reload_time:" || ident == "reload_ap:") {
data >> reload_ap;
std::getline(data, junk);
} else if (ident == "fire_time:" || ident == "fire_ap:") {
data >> fire_ap;
std::getline(data, junk);
} else if (ident == "modes:" || ident == "mode:") {
std::string mode_line;
std::getline(data, mode_line);
std::istringstream mode_ss(mode_line);
int mode;
while (mode_ss >> mode) {
modes.push_back(mode);
}
} else if (ident != "done") {
return false;
}
return true;
}
bool Item_type_food::handle_data(std::string ident, std::istream &data)
{
std::string junk;
if (ident == "food:") {
data >> food;
std::getline(data, junk);
} else if (ident == "water:") {
data >> water;
std::getline(data, junk);
} else if (ident == "effect:") {
if (!effect.load_data(data, name)) {
return false;
}
} else if (ident == "charges:") {
data >> charges;
std::getline(data, junk);
} else if (ident == "container:") {
std::getline(data, container);
container = no_caps(container);
container = trim(container);
if (container.empty()) {
debugmsg("Empty container (%s)", name.c_str());
return false;
}
} else if (ident == "verb:") {
std::getline(data, verb);
verb = no_caps(verb);
verb = trim(verb);
if (verb.empty()) {
debugmsg("Empty verb (%s)", name.c_str());
return false;
}
} else if (ident != "done") {
return false;
}
return true;
}
bool Item_type_tool::handle_data(std::string ident, std::istream &data)
{
std::string junk;
if (ident == "applied:") {
if (!applied_action.load_data(data, name)) {
return false;
}
} else if (ident == "powered:") {
if (!powered_action.load_data(data, name)) {
return false;
}
} else if (ident == "countdown:") {
if (!countdown_action.load_data(data, name)) {
return false;
}
} else if (ident == "countdown_timer:") {
data >> countdown_timer;
std::getline(data, junk);
} else if (ident == "default_charges:") {
data >> default_charges;
std::getline(data, junk);
} else if (ident == "max_charges:") {
data >> max_charges;
std::getline(data, junk);
} else if (ident == "fuel:") {
std::getline(data, fuel);
fuel = no_caps(fuel);
fuel = trim(fuel);
} else if (ident == "powered_text:") {
std::getline(data, powered_text);
powered_text = no_caps(powered_text);
powered_text = trim(powered_text);
} else if (ident != "done") {
debugmsg("Unknown Tool property '%s' (%s)", ident.c_str(), name.c_str());
return false;
}
return true;
}
bool Item_type_tool::uses_charges()
{
return (max_charges > 0 && applied_action.charge_cost > 0);
}
Item_type_container::Item_type_container()
{
capacity = 0;
}
std::string Item_type_container::get_property_description()
{
std::stringstream ret;
ret << "<c=white>Capacity:<c=/> " << capacity;
return ret.str();
}
bool Item_type_container::handle_data(std::string ident, std::istream &data)
{
std::string junk;
if (ident == "capacity:") {
data >> capacity;
std::getline(data, junk);
} else if (ident != "done") {
debugmsg("Unknown Container property '%s' (%s)",
ident.c_str(), name.c_str());
return false;
}
return true;
}
Item_class lookup_item_class(std::string name)
{
name = no_caps(name);
for (int i = 0; i < ITEM_CLASS_MAX; i++) {
Item_class ret = Item_class(i);
if ( no_caps( item_class_name(ret) ) == name ) {
return ret;
}
}
return ITEM_CLASS_MISC;
}
std::string item_class_name_plural(Item_class iclass)
{
return item_class_name(iclass, true);
}
// plural defaults to false
std::string item_class_name(Item_class iclass, bool plural)
{
switch (iclass) {
case ITEM_CLASS_MISC: return "Misc";
case ITEM_CLASS_CLOTHING: return "Clothing";
case ITEM_CLASS_AMMO: return "Ammo";
case ITEM_CLASS_LAUNCHER: return (plural ? "Launchers" : "Launcher");
case ITEM_CLASS_FOOD: return "Food";
case ITEM_CLASS_TOOL: return (plural ? "Tools" : "Tool");
case ITEM_CLASS_CONTAINER: return (plural ? "Containers" : "Container");
case ITEM_CLASS_MAX: return "BUG - ITEM_CLASS_MAX";
default: return "BUG - Unnamed Item_class";
}
return "BUG - Escaped item_class_name switch";
}
Item_flag lookup_item_flag(std::string name)
{
name = no_caps(name);
for (int i = 0; i < ITEM_FLAG_MAX; i++) {
Item_flag ret = Item_flag(i);
if ( no_caps( item_flag_name(ret) ) == name ) {
return ret;
}
}
return ITEM_FLAG_NULL;
}
std::string item_flag_name(Item_flag flag)
{
switch (flag) {
case ITEM_FLAG_NULL: return "NULL";
case ITEM_FLAG_LIQUID: return "liquid";
case ITEM_FLAG_FLAMMABLE: return "flammable";
case ITEM_FLAG_PLURAL: return "plural";
case ITEM_FLAG_CONSTANT: return "constant_volume_weight";
case ITEM_FLAG_MAX: return "BUG - ITEM_FLAG_MAX";
default: return "BUG - Unnamed Item_flag";
}
return "BUG - Escaped item_flag_name switch";
}