forked from Whales/Cataclysm2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitem.cpp
More file actions
1271 lines (1143 loc) · 30.2 KB
/
item.cpp
File metadata and controls
1271 lines (1143 loc) · 30.2 KB
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "item.h"
#include "item_type.h"
#include "game.h"
#include "globals.h"
#include "cuss.h"
#include "entity.h"
#include "attack.h"
#include "files.h" // For CUSS_DIR
#include "rng.h"
#include <sstream>
Item::Item(Item_type* T)
{
type = T;
count = 1;
ammo = NULL;
charges = 0;
subcharges = 0;
fire_mode = 0;
hp = 100; // TODO: Use Item_type durability instead
corpse = NULL;
active = ITEM_ACTIVE_OFF;
if (type) {
uid = GAME.get_item_uid();
charges = type->default_charges();
if (charges > 0 && get_item_class() == ITEM_CLASS_TOOL) {
// Tools have subcharges, so set that up
Item_type_tool* tool = static_cast<Item_type_tool*>(type);
subcharges = tool->subcharges;
}
if (type->volume < 100 && type->volume > 0) {
hp = type->volume;
}
if (hp < 5) {
hp = 5;
}
} else {
//debugmsg("WARNING: Item without type (and without UID) created.");
uid = -1;
}
}
Item::Item(const Item &rhs)
{
type = rhs.get_type();
count = rhs.count;
uid = rhs.uid;
ammo = rhs.ammo;
charges = rhs.charges;
subcharges = rhs.subcharges;
fire_mode = rhs.fire_mode;
active = rhs.active;
corpse = rhs.corpse;
// Note lack of UID copy - is this correct?
if (!rhs.contents.empty()) {
contents.clear();
for (int i = 0; i < rhs.contents.size(); i++) {
contents.push_back( rhs.contents[i] );
}
}
}
Item::~Item()
{
}
Item& Item::operator=(const Item& rhs)
{
type = rhs.get_type();
ammo = rhs.ammo;
count = rhs.count;
charges = rhs.charges;
subcharges = rhs.subcharges;
fire_mode = rhs.fire_mode;
uid = rhs.uid; // Will this cause bugs?
corpse = rhs.corpse;
contents.clear();
if (!rhs.contents.empty()) {
for (int i = 0; i < rhs.contents.size(); i++) {
contents.push_back( rhs.contents[i] );
}
}
return *this;
}
void Item::set_type(Item_type* T)
{
if (!type) {
debugmsg("Item::set_type(NULL)!");
return;
}
type = T;
uid = GAME.get_item_uid();
// TODO: The rest of the type setup?
}
Item_class Item::get_item_class()
{
if (!type) {
return ITEM_CLASS_MISC;
}
return type->get_class();
}
Item_action Item::default_action()
{
if (!type) {
return IACT_NULL;
}
return type->default_action();
}
bool Item::is_real()
{
return type;
}
bool Item::can_reload()
{
// TODO: Reloadable tools
return get_item_class() == ITEM_CLASS_LAUNCHER;
}
int Item::time_to_reload()
{
if (!can_reload() || !is_real()) {
return 0;
}
return type->time_to_reload();
}
int Item::time_to_fire()
{
if (!is_real()) {
return 0;
}
return type->time_to_fire();
}
int Item::time_to_read()
{
if (!is_real()) {
return 0;
}
return type->time_to_read();
}
Item_type* Item::get_type() const
{
return type;
}
int Item::get_uid()
{
if (!is_real()) {
return -1;
}
return uid;
}
glyph Item::top_glyph()
{
if (!type) {
return glyph();
}
glyph ret = type->sym;
if (get_item_class() == ITEM_CLASS_CORPSE && corpse) {
ret.fg = corpse->sym.fg;
}
return ret;
}
std::string Item::get_data_name()
{
if (!type) {
return "Typeless item";
}
return type->get_data_name();
}
std::string Item::get_name()
{
if (!type) {
return "Typeless item";
}
if (get_item_class() == ITEM_CLASS_CORPSE && corpse) {
return corpse->get_name() + " " + type->get_name();
}
return type->get_name();
}
std::string Item::get_name_indefinite()
{
// TODO: Unique items?
if (!type) {
return "<c=red>a typeless item<c=/>";
}
std::string article = (has_flag(ITEM_FLAG_PLURAL) ? "some" : "a");
std::stringstream ret;
ret << article << " ";
if (get_item_class() == ITEM_CLASS_CORPSE && corpse) {
ret << corpse->get_name() << " ";
}
ret << type->get_name_singular();
// Display FULL info on contained items
ret << get_contents_suffix();
return ret.str();
}
std::string Item::get_name_definite()
{
if (!type) {
return "the typeless items";
}
std::stringstream ret;
ret << "the ";
if (get_item_class() == ITEM_CLASS_CORPSE && corpse) {
ret << corpse->get_name() << " ";
}
ret << type->get_name_singular();
return ret.str();
}
std::string Item::get_name_plural()
{
if (!type) {
return "typeless items";
}
std::stringstream ret;
if (get_item_class() == ITEM_CLASS_CORPSE && corpse) {
ret << corpse->get_name() << " ";
}
ret << type->get_name_plural();
return ret.str();
}
std::string Item::get_name_full()
{
if (!type) {
return "typeless item (0)";
}
std::stringstream ret;
ret << get_name();
ret << get_contents_suffix();
// Display the number of charges for items that use them
if (type->uses_charges() || active == ITEM_ACTIVE_TIMER) {
ret << " (" << charges << ")";
}
if (count > 1) {
ret << " <c=ltblue>x" << count << "<c=/>";
}
if (is_active()) {
ret << " <c=yellow>[on]<c=/>";
}
return ret.str();
}
std::string Item::get_contents_suffix()
{
if (contents.empty()) {
return "";
}
std::stringstream ret;
std::string preposition = "containing";
bool use_article = true;
if (get_item_class() == ITEM_CLASS_CONTAINER) {
Item_type_container* cont = static_cast<Item_type_container*>(type);
preposition = cont->preposition;
use_article = cont->use_article;
}
ret << " " << preposition << " ";
if (use_article) {
ret << contents[0].get_name_indefinite();
} else {
ret << contents[0].get_name_full();
}
return ret.str();
}
std::string Item::get_description()
{
if (type) {
return type->description;
}
return "Undefined item - type is NULL (this is a bug)";
}
std::string Item::get_description_full()
{
if (!type) {
return "Undefined item - type is NULL (this is a bug)";
}
std::stringstream ret;
ret << type->description << std::endl << std::endl <<
type->get_property_description();
// If we're a corpse, then we need a special description based on the monster
if (get_item_class() == ITEM_CLASS_CORPSE && corpse) {
ret << "Type: " << corpse->get_name() << std::endl;
// TODO: Put in a list of relevant monster flags, like "poison to eat"
}
// If we are a container, include info on our contents!
if (get_item_class() == ITEM_CLASS_CONTAINER && !contents.empty()) {
ret << std::endl << "<c=yellow>Contents:<c=/> " <<
contents[0].get_name_full() << std::endl <<
contents[0].get_description_full();
}
return ret.str();
}
std::vector<Item_action> Item::get_applicable_actions()
{
std::vector<Item_action> ret;
// We can always(?) drop and wield items.
ret.push_back(IACT_WIELD);
ret.push_back(IACT_DROP);
switch (get_item_class()) {
case ITEM_CLASS_CLOTHING:
ret.push_back(IACT_WEAR);
break;
case ITEM_CLASS_AMMO:
break; // Can't do anything with ammo by itself!
case ITEM_CLASS_LAUNCHER:
ret.push_back(IACT_RELOAD);
ret.push_back(IACT_UNLOAD);
break;
case ITEM_CLASS_FOOD:
ret.push_back(IACT_EAT);
break;
case ITEM_CLASS_TOOL:
ret.push_back(IACT_APPLY);
break;
case ITEM_CLASS_CONTAINER:
ret.push_back(IACT_UNLOAD);
break;
case ITEM_CLASS_CORPSE:
ret.push_back(IACT_BUTCHER);
break;
}
// Now, check the contents.
for (int i = 0; i < contents.size(); i++) {
std::vector<Item_action> cont_acts = contents[i].get_applicable_actions();
// Gotta make sure we don't have duplicates...
for (int n = 0; n < cont_acts.size(); n++) {
bool found = false;
for (int m = 0; !found && m < ret.size(); m++) {
if (cont_acts[n] == ret[m]) {
found = true;
}
}
if (!found) {
ret.push_back( cont_acts[n] );
}
}
}
return ret;
}
int Item::get_weight()
{
if (!type) {
return 0;
}
if (get_item_class() == ITEM_CLASS_AMMO) {
return (charges * type->weight) / 100;
}
if (has_flag(ITEM_FLAG_LIQUID) ||
(get_item_class() == ITEM_CLASS_FOOD && !has_flag(ITEM_FLAG_CONSTANT))) {
return (charges * type->weight);
}
if (get_item_class() == ITEM_CLASS_CORPSE) {
if (corpse) {
return corpse->get_weight();
}
return 0;
}
int ret = type->weight;
for (int i = 0; i < contents.size(); i++) {
ret += contents[i].get_weight();
}
return ret * count;
}
int Item::get_volume()
{
if (!type) {
return 0;
}
if (get_item_class() == ITEM_CLASS_AMMO) {
return (charges * type->volume) / 100;
}
if (has_flag(ITEM_FLAG_LIQUID) ||
(get_item_class() == ITEM_CLASS_FOOD && !has_flag(ITEM_FLAG_CONSTANT))) {
return (charges * type->volume);
}
/* Some containers, like a plastic bag, can hold more than their own volume
* (since when they're empty you can wad them up). If that's the case, return
* the total volume of its contents instead!
*/
if (get_item_class() == ITEM_CLASS_CONTAINER) {
int own_volume = type->volume;
int contents_volume = get_volume_capacity_used();
if (own_volume > contents_volume) {
return own_volume;
}
return contents_volume;
}
if (get_item_class() == ITEM_CLASS_CORPSE) {
if (corpse) {
return corpse->get_volume() * count;
}
return 0;
}
// Everything else just returns its type's volume.
return type->volume * count;
}
int Item::get_volume_capacity()
{
if (!is_real()) {
return 0;
}
if (get_item_class() == ITEM_CLASS_CONTAINER) {
Item_type_container* container = static_cast<Item_type_container*>(type);
return container->capacity;
}
/* Even though "volume capacity" means something totally different for
* containers and clothing, put clothing in this function too. It makes sense,
* and there shouldn't be any overlap.
*/
if (get_item_class() == ITEM_CLASS_CLOTHING) {
Item_type_clothing* clothing = static_cast<Item_type_clothing*>(type);
return clothing->carry_capacity;
}
// We're neither a container nor clothing, return 0.
return 0;
}
int Item::get_volume_capacity_used()
{
int ret = 0;
for (int i = 0; i < contents.size(); i++) {
ret += contents[i].get_volume();
}
return ret;
}
bool Item::has_flag(Item_flag itf)
{
if (!type) {
return false;
}
return type->has_flag(itf);
}
bool Item::covers(Body_part part)
{
if (!is_real() || get_item_class() != ITEM_CLASS_CLOTHING) {
return false;
}
Item_type_clothing* clothing = static_cast<Item_type_clothing*>(type);
return clothing->covers[part];
}
int Item::get_damage(Damage_type dtype)
{
if (type) {
return type->damage[dtype];
}
return 0;
}
int Item::get_to_hit()
{
if (type) {
return type->to_hit;
}
return 0;
}
int Item::get_base_attack_speed()
{
Stats stats;
return get_base_attack_speed(stats);
}
int Item::get_base_attack_speed(Stats stats)
{
if (!type) {
return 0;
}
// TODO: Do we really need Item_type::attack_speed?
int ret = 50 + type->attack_speed;
int min_weight_penalty = stats.strength;
int penalty_per_pound = (2 * (20 - stats.strength) ) / 3;
int wgt = get_weight();
if (stats.strength < 20 && wgt >= min_weight_penalty) {
wgt -= min_weight_penalty;
// Divide by 10 since the penalty is per pound - 1 unit of weight is 0.1 lbs
ret += (wgt * penalty_per_pound) / 10;
}
// TODO: Tweak this section - this is very guess-y.
int min_volume_penalty = stats.dexterity * 3;
int penalty_per_10_volume = (2 * (20 - stats.dexterity) ) / 3;
int vol = get_volume();
if (stats.dexterity < 20 && vol >= min_volume_penalty) {
vol -= min_volume_penalty;
ret += (vol * penalty_per_10_volume) / 10;
}
return ret;
}
int Item::get_shots_fired()
{
if (get_item_class() != ITEM_CLASS_LAUNCHER) {
return 0;
}
Item_type_launcher* launcher = static_cast<Item_type_launcher*>(type);
// Sanity check
if (fire_mode < 0 || fire_mode >= launcher->modes.size()) {
fire_mode = 0;
}
int ret = 1;
if (!launcher->modes.empty()) { // Should always happen but let's be safe
ret = launcher->modes[fire_mode];
}
if (ret > charges) {
ret = charges;
}
return ret;
}
int Item::get_max_charges()
{
if (get_item_class() == ITEM_CLASS_LAUNCHER) {
Item_type_launcher* launcher = static_cast<Item_type_launcher*>(type);
return launcher->capacity;
}
return 0;
}
bool Item::combines()
{
if (!is_real()) {
return false;
}
return type->always_combines();
}
bool Item::combine_by_charges()
{
if (!is_real()) {
return false;
}
return type->combine_by_charges();
}
Ranged_attack Item::get_thrown_attack(Entity* ent)
{
Ranged_attack ret;
if (!type) {
return ret;
}
ret.type = RANGED_ATT_THROW;
// If the ranged speed is 0, then set it based on our weight
if (type->thrown_speed == 0) {
ret.speed = 50 + 5 * type->weight;
ret.speed += type->volume / 10;
} else {
ret.speed = type->thrown_speed;
}
// Copy variance from type
ret.variance = type->thrown_variance;
// Add variance for heavy/bulky items
Dice extra_variance;
int weight_divisor = 20, volume_divisor = 5;
if (ent) {
weight_divisor = 10 + ent->stats.strength;
volume_divisor = random_round(ent->stats.dexterity, 3) +
random_round(ent->skills.get_level(SKILL_THROWING), 2);
}
extra_variance.number = 1 + type->weight / weight_divisor;
extra_variance.sides = type->volume / volume_divisor;
if (extra_variance.sides > 0) {
ret.variance += extra_variance;
}
ret.range = 20 - (type->weight / (weight_divisor * 2));
ret.range -= type->volume / (volume_divisor * 5);
// Copy damage; note that thrown_dmg_percent defaults to 50
for (int i = 0; i < DAMAGE_MAX; i++) {
ret.damage[i] = (type->damage[i] * type->thrown_dmg_percent) / 100;
}
// Now apply ent's stats / skills
if (ent) {
int skill = ent->skills.get_level(SKILL_THROWING);
ret.variance.sides -= skill;
if (ent->stats.dexterity > 10) {
ret.variance.sides -= random_round(ent->stats.dexterity - 10, 4);
}
if (ret.variance.sides < 3) {
ret.variance.sides = 3;
}
int extra_dice = 0;
while (rng(1, 10) > ent->stats.dexterity) {
extra_dice++;
}
while (rng(1, 10) > ent->stats.perception && !one_in(3)) {
extra_dice++;
}
ret.variance.number += extra_dice;
ret.speed = (100 * ret.speed) / (100 + skill * 4); // TODO: Don't hardcode 4
ret.range += skill / 4; // No random rounding here
int damage_percent = 100 + (ent->stats.strength - 10) * 2 + skill * 4;
for (int i = 0; i < DAMAGE_MAX; i++) {
ret.damage[i] = (damage_percent * ret.damage[i]) / 100;
}
}
return ret;
}
Ranged_attack Item::get_fired_attack()
{
if (!type || get_item_class() != ITEM_CLASS_LAUNCHER || charges <= 0 ||
!ammo) {
return Ranged_attack();
}
Item_type_launcher* launcher = static_cast<Item_type_launcher*>(type);
Item_type_ammo* itammo = static_cast<Item_type_ammo*> (ammo);
Ranged_attack ret;
ret.type = RANGED_ATT_FIRE;
ret.speed = launcher->fire_ap;
ret.range = itammo->range;
ret.variance = launcher->accuracy + itammo->accuracy;
ret.pellets = itammo->pellets;
ret.rounds = get_shots_fired();
// TODO: Can fired items ever be non-pierce?
ret.damage [DAMAGE_PIERCE] = itammo->damage + launcher->damage;
ret.armor_divisor[DAMAGE_PIERCE] = itammo->armor_pierce;
return ret;
}
bool Item::combine_with(const Item& rhs)
{
// TODO: Handle combining items of different damage levels etc.
if (!combines()) {
return false;
}
if (type != rhs.get_type()) {
return false;
}
if (contents.size() != rhs.contents.size()) {
return false;
}
for (int i = 0; i < contents.size(); i++) {
if (contents[i].get_type() != rhs.contents[i].get_type()) {
return false;
}
}
if (combine_by_charges()) {
charges += rhs.charges;
} else {
count += rhs.count;
}
return true;
}
void Item::prep_for_generation()
{
place_in_its_container();
}
bool Item::place_in_its_container()
{
if (type->container.empty()) {
return false;
}
Item_type* container_type = ITEM_TYPES.lookup_name( type->container );
if (container_type) {
Item tmp(container_type);
if (tmp.get_item_class() != ITEM_CLASS_CONTAINER) {
debugmsg("%s has non-container container (%s; %s)",
get_data_name().c_str(), type->container.c_str(),
item_class_name(tmp.get_item_class()).c_str());
return false;
}
if (!tmp.add_contents(*this)) {
debugmsg("%s could not be placed in its container (%s)",
get_data_name().c_str(), type->container.c_str());
return false;
}
*this = tmp;
return true;
}
debugmsg("%s has non-existant container '%s'", get_data_name().c_str(),
type->container.c_str());
return false;
}
bool Item::add_contents(Item it)
{
// TODO: Require water-tight containers for liquids?
if (!is_real() || get_item_class() != ITEM_CLASS_CONTAINER ||
!it.is_real()) {
return false;
}
int capacity = get_volume_capacity();
if (has_flag(ITEM_FLAG_OPEN_END)) {
capacity *= 5;
}
if (get_volume_capacity_used() + it.get_volume() > capacity) {
return false;
}
contents.push_back(it);
return true;
}
bool Item::advance_fire_mode()
{
if (get_item_class() != ITEM_CLASS_LAUNCHER) {
return false;
}
Item_type_launcher* launcher = static_cast<Item_type_launcher*>(type);
if (launcher->modes.size() <= 1) {
return false; // It only has one mode, so we can't change it!
}
fire_mode++;
if (fire_mode >= launcher->modes.size()) {
fire_mode = 0;
}
return true;
}
bool Item::reload(Entity* owner, int ammo_uid)
{
if (!owner) {
return false;
}
Item* ammo_used = owner->ref_item_uid(ammo_uid);
if (!ammo_used) {
return false;
}
int charges_available = get_max_charges() - charges;
if (charges_available <= 0) {
return false;
}
if (ammo_used->charges > charges_available) {
ammo_used->charges -= charges_available;
charges = get_max_charges();
} else {
charges += ammo_used->charges;
owner->remove_item_uid(ammo_uid);
}
ammo = ammo_used->get_type();
if (get_item_class() == ITEM_CLASS_TOOL) { // Need to set up subcharges too
Item_type_tool* tool = static_cast<Item_type_tool*>(type);
subcharges = tool->subcharges;
}
return true;
}
bool Item::damage(int dam)
{
if (dam < 0) {
return false;
}
hp -= dam;
if (hp <= 0) {
return true;
}
return false;
}
bool Item::absorb_damage(Damage_type damtype, int dam)
{
if (!is_real() || get_item_class() == ITEM_CLASS_CLOTHING) {
return false; // Don't do anything
}
Item_type_clothing* clothing = static_cast<Item_type_clothing*>(type);
/* TODO: Item_type_clothing should, eventually, use a Damage_set for its armor
* ratings. Once it does, the following can be simplified.
*/
int armor = 0;
switch (damtype) {
case DAMAGE_BASH:
armor = clothing->armor_bash;
break;
case DAMAGE_CUT:
armor = clothing->armor_cut;
break;
case DAMAGE_PIERCE:
armor = clothing->armor_pierce;
break;
}
armor = rng(0, armor); // Absorb some of the damage!
int damage_to_item = armor;
if (dam < armor) {
damage_to_item = dam;
}
damage_to_item = dice(2, damage_to_item + 1) - 2;
// Reduce the damage by the amount of the armor used
dam -= armor;
return damage(damage_to_item);
}
bool Item::power_on()
{
// TODO: Can we power on other classes?
if (!is_real() || get_item_class() != ITEM_CLASS_TOOL) {
return false;
}
if (charges == 0 && subcharges == 0) {
return false;
}
if (is_active()) {
return false;
}
active = ITEM_ACTIVE_POWERED;
GAME.add_active_item(this);
return true;
}
bool Item::power_off()
{
if (!is_active()) {
return false;
}
active = ITEM_ACTIVE_OFF;
GAME.remove_active_item(this);
return true;
}
bool Item::start_countdown()
{
if (!is_real() || get_item_class() != ITEM_CLASS_TOOL) {
return false;
}
if (is_active()) {
return false;
}
Item_type_tool* tool = static_cast<Item_type_tool*>(type);
if (!tool->countdown_action.real) {
return false;
}
charges = tool->countdown_timer;
active = ITEM_ACTIVE_TIMER;
GAME.add_active_item(this);
return true;
}
bool Item::finish_countdown()
{
if (!is_real()) {
return false;
}
if (active != ITEM_ACTIVE_TIMER || get_item_class() != ITEM_CLASS_TOOL) {
return false;
}
Item_type_tool* tool = static_cast<Item_type_tool*>(type);
tool->countdown_action.activate(this);
GAME.remove_active_item(this);
active = ITEM_ACTIVE_OFF;
if (tool->countdown_action.destroy_if_chargeless) {
if (!GAME.destroy_item_uid(get_uid())) {
debugmsg("Couldn't destroy item!");
}
}
return true;
}
bool Item::is_active()
{
if (!is_real()) {
return false;
}
return (active == ITEM_ACTIVE_POWERED || active == ITEM_ACTIVE_TIMER);
}
bool Item::process_active()
{
// TODO: Can we power on other classes?
if (get_item_class() != ITEM_CLASS_TOOL) {
return false;
}
if (active == ITEM_ACTIVE_OFF) {
debugmsg("Item::process_active() run on a non-active item (%s)!",
get_name_full().c_str());
return false;
}
Item_type_tool* tool = static_cast<Item_type_tool*>(type);
if (active == ITEM_ACTIVE_POWERED) {
subcharges--;
if (subcharges <= 0) {
charges--;
if (charges >= 0) {
subcharges += tool->subcharges;
} else {
// We're truly out of power!
charges = 0;
subcharges = 0;
// If we're on a timer, activate the timer function!
power_off(); // This removes us from Game::active_items
// We have to destroy the item AFTER removing it from Game:active_items!
if (tool->powered_action.destroy_if_chargeless) {
GAME.destroy_item(this);
return true;
}
return false;
}
}
// Finally, do what we're meant to do while active.
if (tool->powered_action.real) {
tool->powered_action.activate(this);
} else {
debugmsg("%s is ITEM_ACTIVE_POWERED but its powered_action isn't real!",
get_name_full().c_str());
}
return false;
} else if (active == ITEM_ACTIVE_TIMER) {
if (!tool->countdown_action.real) {
debugmsg("%s is ITEM_ACTIVE_TIMER but its countdown_action isn't real!",
get_name_full().c_str());
return false;
}
charges--;
if (charges <= 0) { // We hit the countdown!
finish_countdown();
}
return true;
}
return false;
}
Item_action Item::show_info(Entity* user)
{
if (!type) {
return IACT_NULL;
}
Window w_info(0, 0, 80, 24);
cuss::interface i_info;
if (!i_info.load_from_file(CUSS_DIR + "/i_item_info.cuss")) {
return IACT_NULL;
}
i_info.set_data("item_name", get_name_full());
i_info.set_data("num_weight", get_weight());
i_info.set_data("num_volume", get_volume());
i_info.set_data("num_bash", get_damage(DAMAGE_BASH));
i_info.set_data("num_cut", get_damage(DAMAGE_CUT));
i_info.set_data("num_pierce", get_damage(DAMAGE_PIERCE));
i_info.set_data("num_to_hit", get_to_hit());
if (user) {
i_info.set_data("num_speed", get_base_attack_speed(user->stats));
} else {
i_info.set_data("num_speed", get_base_attack_speed());
}