forked from Whales/Cataclysm
-
Notifications
You must be signed in to change notification settings - Fork 3
/
iuse.cpp
2513 lines (2179 loc) · 76.2 KB
/
iuse.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
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 "iuse.h"
#include "game.h"
#include "mapdata.h"
#include "keypress.h"
#include "output.h"
#include "rng.h"
#include "line.h"
#include "monattack.h"
#include "submap.h"
#include "recent_msg.h"
#include "zero.h"
#include "stl_limits.h"
#include "stl_typetraits.h"
#include <sstream>
struct to_GPS
{
GPS_loc operator()(const GPS_loc& src) { return src; }
template<class PC, class X>
GPS_loc operator()(const std::pair<PC*, X>& src) requires requires() { src.first->GPSpos; }
{ return src.first->GPSpos; }
};
// all callers of these handlers have a well-defined item available (and thus *should* have a valid pos from game::find_item
/* To mark an item as "removed from inventory", set its invlet to 0
This is useful for traps (placed on ground), inactive bots, etc
*/
void iuse::sewage(player& p)
{
p.vomit();
if (one_in(4)) p.mutate();
}
void iuse::royal_jelly(player& p)
{
// TODO: Add other diseases here; royal jelly is a cure-all!
p.pkill += 5;
if (p.rem_disease(DI_FUNGUS))
p.subjective_message("You feel cleansed inside!");
if (p.rem_disease(DI_BLIND))
p.subjective_message("Your sight returns!");
static const decltype(DI_POISON) toxic[] = {
DI_POISON,
DI_BADPOISON,
DI_FOODPOISON
};
static auto detox = [&](disease& ill) { return cataclysm::any(toxic, ill.type); };
if (p.rem_disease(detox))
p.subjective_message("You feel much better!");
if (p.rem_disease(DI_ASTHMA))
p.subjective_message("Your breathing clears up!");
static const decltype(DI_POISON) cold_like[] = {
DI_COMMON_COLD,
DI_FLU
};
static auto cure_common_cold = [&](disease& ill) { return cataclysm::any(toxic, ill.type); };
if (p.rem_disease(cure_common_cold))
p.subjective_message("You feel healther!");
}
static void _display_hp(WINDOW* w, player* p, int curhp, int i)
{
const auto cur_hp_color = p->hp_color(hp_part(i));
if (p->has_trait(PF_HPIGNORANT))
mvwaddstrz(w, i + 2, 15, cur_hp_color.second, "***");
else {
mvwprintz(w, i + 2, 17-int_log10(curhp), cur_hp_color.second, "%d", cur_hp_color.first);
}
}
static std::optional<hp_part> _get_heal_target(pc& p)
{
int ch;
do {
ch = getch();
if (ch == '1') return hp_head;
else if (ch == '2') return hp_torso;
else if (ch == '3') {
if (p.hp_cur[hp_arm_l] == 0) throw std::string("That arm is broken. It needs surgical attention.");
return hp_arm_l;
} else if (ch == '4') {
if (p.hp_cur[hp_arm_r] == 0) throw std::string("That arm is broken. It needs surgical attention.");
return hp_arm_r;
} else if (ch == '5') {
if (p.hp_cur[hp_leg_l] == 0) throw std::string("That leg is broken. It needs surgical attention.");
return hp_leg_l;
} else if (ch == '6') {
if (p.hp_cur[hp_leg_r] == 0) throw std::string("That leg is broken. It needs surgical attention.");
return hp_leg_r;
} else if (ch == '7') throw std::string("Never mind.");
} while (true);
}
// apparently NPCs have full surgery kits automatically?
// returns -1 if nothing needs healing
static int _npc_get_heal_target(const npc& p)
{
int healed = -1;
int highest_damage = 0;
for (int i = 0; i < num_hp_parts; i++) {
int damage = p.hp_max[i] - p.hp_cur[i];
if (i == hp_head) cataclysm::rational_scale<3,2>(damage);
if (i == hp_torso) cataclysm::rational_scale<6,5>(damage);
if (damage > highest_damage) {
highest_damage = damage;
healed = i;
}
}
return healed;
}
static auto _predicted_bandage(const player& actor, hp_part healed) {
int bonus = actor.sklevel[sk_firstaid]; // may want to be a parameter
switch (healed)
{
case hp_head: return 1 + cataclysm::rational_scaled<4, 5>(bonus);
case hp_torso: return 4 + cataclysm::rational_scaled<3, 2>(bonus);
default: return 3 + bonus;
}
}
static void _bandage(player& actor, player& target, hp_part healed)
{
int bonus = actor.sklevel[sk_firstaid]; // may want to be a parameter
actor.practice(sk_firstaid, 8);
target.heal(healed, _predicted_bandage(actor, healed));
}
// \todo adjust to allow treating other (N)PCs
void iuse::bandage(pc& p)
{
// Player--present a menu
static constexpr const char* bandage_options[] = {
"Bandage where?",
"1: Head",
"2: Torso",
"3: Left Arm",
"4: Right Arm",
"5: Left Leg",
"6: Right Leg",
"7: Exit"
};
constexpr const int bandage_height = sizeof(bandage_options) / sizeof(*bandage_options) + 2;
// maximum string length...possible function target
// C++20: std::ranges::max?
int bandage_width = 0;
for (const char* const line : bandage_options) bandage_width = cataclysm::max(bandage_width, strlen(line));
bandage_width += 5; // historical C:Whales; 15 is magic constant for layout so likely want 20 width explicitly
WINDOW* w = newwin(bandage_height, bandage_width, (VIEW - bandage_height) / 2, (SCREEN_WIDTH - bandage_width) / 2);
wborder(w, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX,
LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX);
int row = 0;
for (const char* const line : bandage_options) {
++row;
mvwaddstrz(w, row, 1, 1 == row ? c_ltred : c_ltgray, line);
}
for (int i = 0; i < num_hp_parts; i++) {
int curhp = p.hp_cur[i];
if (curhp != 0) {
clamp_ub(curhp += _predicted_bandage(p, hp_part(i)), p.hp_max[i]);
_display_hp(w, &p, curhp, i);
} else // curhp is 0; requires surgical attention
mvwaddstrz(w, i + 2, 15, c_dkgray, "---");
}
wrefresh(w);
const auto ok = _get_heal_target(p);
werase(w);
wrefresh(w);
delwin(w);
refresh();
if (ok) _bandage(p, p, *ok);
}
void iuse::bandage(npc& p)
{
// NPCs heal whichever has sustained the most damage
if (auto code = _npc_get_heal_target(p); 0 <= code) _bandage(p, p, hp_part(code));
}
static auto _predicted_first_aid(const player& actor, hp_part healed) {
int bonus = actor.sklevel[sk_firstaid]; // may want to be a parameter
switch (healed)
{
case hp_head: return 10 + cataclysm::rational_scaled<4, 5>(bonus);
case hp_torso: return 18 + cataclysm::rational_scaled<3, 2>(bonus);
default: return 14 + bonus;
}
}
static void _first_aid(player& actor, player& target, hp_part healed)
{
int bonus = actor.sklevel[sk_firstaid]; // may want to be a parameter
actor.practice(sk_firstaid, 8);
target.heal(healed, _predicted_first_aid(actor, healed));
}
// \todo adjust to allow treating other (N)PCs
void iuse::firstaid(pc& p)
{
// Player--present a menu
static constexpr const char* firstaid_options[] = {
"Bandage where?",
"1: Head",
"2: Torso",
"3: Left Arm",
"4: Right Arm",
"5: Left Leg",
"6: Right Leg",
"7: Exit"
};
constexpr const int firstaid_height = sizeof(firstaid_options) / sizeof(*firstaid_options) + 2;
// maximum string length...possible function target
// C++20: std::ranges::max?
int firstaid_width = 0;
for (const char* const line : firstaid_options) firstaid_width = cataclysm::max(firstaid_width, strlen(line));
firstaid_width += 5; // historical C:Whales; 15 is magic constant for layout so likely want 20 width explicitly
WINDOW* w = newwin(firstaid_height, firstaid_width, (VIEW - firstaid_height) / 2, (SCREEN_WIDTH - firstaid_width) / 2);
wborder(w, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX,
LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX);
int row = 0;
for (const char* const line : firstaid_options) {
++row;
mvwaddstrz(w, row, 1, 1 == row ? c_ltred : c_ltgray, line);
}
for (int i = 0; i < num_hp_parts; i++) {
int curhp = p.hp_cur[i];
if (curhp != 0) {
clamp_ub(curhp += _predicted_first_aid(p, hp_part(i)), p.hp_max[i]);
_display_hp(w, &p, curhp, i);
} else // curhp is 0; requires surgical attention
mvwaddstrz(w, i + 2, 15, c_dkgray, "---");
}
wrefresh(w);
const auto ok = _get_heal_target(p);
werase(w);
wrefresh(w);
delwin(w);
refresh();
if (ok) _first_aid(p, p, *ok);
}
// \todo adjust to allow treating other (N)PCs
void iuse::firstaid(npc& p)
{
// NPCs heal whichever has sustained the most damage
if (auto code = _npc_get_heal_target(p); 0 <= code) _first_aid(p, p, hp_part(code));
}
void iuse::vitamins(player& p)
{
p.add_disease(DI_TOOK_VITAMINS, DAYS(1));
if (p.health <= -2) p.health -= (p.health / 2);
p.health += 3;
}
// Aspirin
void iuse::pkill_1(player& p, const it_comest& med)
{
static auto take = [&]() { return grammar::SVO_sentence(p, "take", "some " + med.name); };
static auto reset_duration = [](disease& ill) {
if (DI_PKILL1 == ill.type) {
ill.duration = MINUTES(12);
return true;
}
return false;
};
p.if_visible_message(take);
if (!p.do_foreach(reset_duration)) p.add_disease(DI_PKILL1, MINUTES(12));
}
// Codeine
void iuse::pkill_2(player& p, const it_comest& med)
{
static auto take = [&]() { return grammar::SVO_sentence(p, "take", "some " + med.name); };
p.if_visible_message(take);
p.add_disease(DI_PKILL2, MINUTES(18));
}
void iuse::pkill_3(player& p, const it_comest& med)
{
static auto take = [&]() { return grammar::SVO_sentence(p, "take", "some " + med.name); };
p.if_visible_message(take);
p.add_disease(DI_PKILL3, MINUTES(2));
p.add_disease(DI_PKILL2, MINUTES(20));
}
void iuse::pkill_4(player& p)
{
static auto shoot_up = [&]() { return grammar::SVO_sentence(p, "shoot", "up"); };
p.if_visible_message(shoot_up);
p.add_disease(DI_PKILL3, MINUTES(8));
p.add_disease(DI_PKILL2, MINUTES(20));
}
void iuse::pkill_l(player& p, const it_comest& med)
{
static auto take = [&]() { return grammar::SVO_sentence(p, "take", "some "+ med.name); };
p.if_visible_message(take);
p.add_disease(DI_PKILL_L, rng(12, 18) * MINUTES(30));
}
void iuse::xanax(player& p, const it_comest& med)
{
static auto take = [&]() { return grammar::SVO_sentence(p, "take", "some " + med.name); };
p.if_visible_message(take);
p.add_disease(DI_TOOK_XANAX, (!p.has_disease(DI_TOOK_XANAX) ? MINUTES(90) : MINUTES(20)));
}
void iuse::caff(player& p, const it_comest& drink)
{
p.fatigue -= drink.stim * 3;
}
void iuse::alcohol(player& p)
{
int duration = MINUTES(68 - p.str_max); // Weaker characters are cheap drunks
if (p.has_trait(PF_LIGHTWEIGHT)) duration += MINUTES(30);
p.pkill += 8;
p.add_disease(DI_DRUNK, duration);
if (p.has_bionic(bio_ethanol)) p.charge_power(rng(2, 8));
}
void iuse::cig(player& p)
{
static auto smoke = [&]() {return p.subject() + " " + p.VO_phrase("light", "a cigarette") + "and" + p.VO_phrase("smoke", "it") + "."; };
static auto feel_gross = [](const disease& ill) {
if (DI_CIG == ill.type && HOURS(1) < ill.duration) {
messages.add("Ugh, too much smoke... you feel gross."); // hard-code subjective_message body
return true;
}
return false;
};
p.if_visible_message(smoke);
p.add_disease(DI_CIG, MINUTES(20));
if (!p.is_npc()) p.do_foreach(feel_gross); // hard-code subjective_message test
}
void iuse::weed(player& p)
{
static auto feel_good = [&]() {return std::string("Good stuff, man!"); };
static auto smoke = [&]() {return p.subject() + " " + p.VO_phrase("light", "a toke") + "and" + p.VO_phrase("smoke", "it") + "."; };
p.if_visible_message(feel_good, smoke);
int duration = MINUTES(6);
if (p.has_trait(PF_LIGHTWEIGHT)) duration = MINUTES(9);
p.hunger += 8;
if (p.pkill < 15) p.pkill += 5;
p.add_disease(DI_THC, duration);
}
void iuse::coke(player& p)
{
static auto snort = [&]() { return grammar::SVO_sentence(p, "snort", "a bump"); };
p.if_visible_message(snort);
int duration = TURNS(21 - p.str_cur);
if (p.has_trait(PF_LIGHTWEIGHT)) duration += MINUTES(2);
p.hunger -= 8;
p.add_disease(DI_HIGH, duration);
}
void iuse::meth(player& p)
{
static auto smoke = [&]() { return grammar::SVO_sentence(p, "smoke", "some crystals"); };
static auto snort = [&]() { return grammar::SVO_sentence(p, "snort", "some crystals"); };
int duration = MINUTES(1) * (40 - p.str_cur);
if (p.has_charges(itm_lighter, 1)) {
p.if_visible_message(smoke);
duration *= 1.5;
p.use_charges(itm_lighter, 1); // not free
} else
p.if_visible_message(snort);
if (!p.has_disease(DI_METH)) duration += HOURS(1);
p.hunger -= clamped_ub<20>(30 - p.str_cur);
p.add_disease(DI_METH, duration);
}
void iuse::poison(player& p)
{
p.add_disease(DI_POISON, HOURS(1));
p.add_disease(DI_FOODPOISON, HOURS(3));
}
// the mushroom version
void iuse::hallu(player& p) { p.add_disease(DI_HALLU, HOURS(4)); }
// \todo want DI_TOOK_THORAZINE
void iuse::thorazine(player& p)
{
p.fatigue += 15;
p.rem_disease(DI_HALLU);
p.rem_disease(DI_VISUALS);
p.rem_disease(DI_HIGH);
p.rem_disease(DI_THC); // B-movie; situation in real life is more complicated
if (!p.has_disease(DI_DERMATIK)) p.rem_disease(DI_FORMICATION);
p.subjective_message("You feel somewhat sedated.");
}
// Cf. https://www.rxlist.com/prozac-drug.htm
// General in-game indication for prozac is negative morale -- proxy for clinical depression.
// proper pharmakinetic modeling is an invasive rewrite (at least triple buffer setup, not just double buffer)
// detox pathway is CYP2D6; half-life situation is complicated (lower bound one day)
// ok to treat SSRI reuptake inhibitors as lethal stimulants in-game, for now
void iuse::prozac(player& p)
{
static auto reset_duration = [](disease& ill) {
constexpr const int raw_dose = HOURS(24);
constexpr const int ineffective_dose = HOURS(18);
if (DI_TOOK_PROZAC == ill.type) {
// not remotely correct.
int current_dose = ill.duration + ineffective_dose;
int delta = raw_dose;
int burn_rate = 1;
int new_dose = 0;
while (current_dose >= raw_dose) {
new_dose += raw_dose;
current_dose -= raw_dose;
burn_rate *= 2;
if (0 >= (delta /= 2)) return true;
}
current_dose += delta;
while (current_dose >= raw_dose) {
new_dose += raw_dose;
current_dose -= raw_dose;
burn_rate *= 2;
current_dose /= 2;
}
ill.duration = new_dose + current_dose;
return true;
}
return false;
};
if (!p.do_foreach(reset_duration)) p.add_disease(DI_TOOK_PROZAC, HOURS(6));
p.stim += 3;
}
void iuse::sleep(player& p)
{
static auto i_took_med = [&]() {
return std::string("You feel very sleepy...");
};
static auto other_took_med = [&]() {
return grammar::SVO_sentence(p, "take", "a sleeping pill");
};
p.fatigue += 40; // starts as placebo effect, ends up as chemistry
p.if_visible_message(i_took_med, other_took_med);
}
// Iodine is a specific blocker for I-131; it dilutes the existing
// dose and triggers excretion.
// \todo track both current radioactivity, and accumulated radiation damage.
void iuse::iodine(player& p)
{
static auto took_med = [&]() {
return grammar::SVO_sentence(p, "take", "an iodine tablet");
};
p.add_disease(DI_IODINE, HOURS(2));
p.if_visible_message(took_med);
}
void iuse::flumed(player& p, const it_comest& med)
{
static auto took_med = [&]() {
return grammar::SVO_sentence(p, "take", std::string("some ") + med.name);
};
p.add_disease(DI_TOOK_FLUMED, HOURS(10));
p.if_visible_message(took_med);
}
void iuse::flusleep(player& p, const it_comest& med)
{
static auto took_med = [&]() {
return grammar::SVO_sentence(p, "take", std::string("some ") + med.name);
};
p.add_disease(DI_TOOK_FLUMED, HOURS(12));
p.fatigue += 30;
p.if_visible_message(took_med);
p.subjective_message("You feel very sleepy...");
}
void iuse::inhaler(player& p, const it_comest& med)
{
static auto use_inhaler = [&]() {
return grammar::SVO_sentence(p, "take", std::string("a puff from ") + p.possessive() + " " + med.name);
};
p.rem_disease(DI_ASTHMA);
p.if_visible_message(use_inhaler);
}
void iuse::blech(player& p)
{
// TODO: Add more effects?
p.subjective_message("Blech, that burns your throat!");
p.vomit();
}
void iuse::mutagen(player& p)
{
if (!one_in(3)) p.mutate();
}
void iuse::mutagen_3(player& p)
{
p.mutate();
if (!one_in(3)) p.mutate();
if (one_in(2)) p.mutate();
}
void iuse::purifier(player& p)
{
std::vector<int> valid; // Which flags the player has
for (int i = 1; i < PF_MAX2; i++) {
if (p.has_trait(pl_flag(i)) && p.has_mutation(pl_flag(i))) valid.push_back(i);
}
if (valid.empty()) {
p.subjective_message("You feel cleansed.");
return;
}
int num_cured = clamped_ub<4>(rng(1, valid.size()));
while (0 <= --num_cured) {
int index = rng(0, valid.size() - 1);
p.remove_mutation(pl_flag(valid[index]));
valid.erase(valid.begin() + index);
}
}
void iuse::marloss(pc& p, item& it)
{
const auto g = game::active();
// If we have the marloss in our veins, we are a "breeder" and will spread
// alien lifeforms.
if (p.has_trait(PF_MARLOSS)) {
messages.add("As you eat the berry, you have a near-religious experience, feeling at one with your surroundings...");
p.add_morale(MORALE_MARLOSS, 100, 1000);
p.hunger = -100;
monster goo(mtype::types[mon_blob]);
goo.friendly = -1;
int goo_spawned = 0;
for (int x = p.pos.x - 4; x <= p.pos.x + 4; x++) {
for (int y = p.pos.y - 4; y <= p.pos.y + 4; y++) {
const auto dist = trig_dist(x, y, p.pos);
if (rng(0, 10) > dist && rng(0, 10) > dist) g->m.marlossify(x, y);
if (one_in(10 + 5 * dist) && (goo_spawned == 0 || one_in(goo_spawned * 2))) {
goo.spawn(x, y);
g->spawn(goo);
goo_spawned++;
}
}
}
return;
}
/* If we're not already carriers of Marloss, roll for a random effect:
* 1 - Mutate
* 2 - Mutate
* 3 - Mutate
* 4 - Purify
* 5 - Purify
* 6 - Cleanse radiation + Purify
* 7 - Fully satiate
* 8 - Vomit
* 9 - Give Marloss mutation
*/
int effect = rng(1, 9);
if (effect <= 3) {
messages.add("This berry tastes extremely strange!");
p.mutate();
} else if (effect <= 6) { // Radiation cleanse is below
messages.add("This berry makes you feel better all over.");
p.pkill += 30;
purifier(p);
} else if (effect == 7) {
messages.add("This berry is delicious, and very filling!");
p.hunger = -100;
} else if (effect == 8) {
messages.add("You take one bite, and immediately vomit!");
p.vomit();
} else if (!p.has_trait(PF_MARLOSS)) {
messages.add("You feel a strange warmth spreading throughout your body...");
p.toggle_trait(PF_MARLOSS);
}
if (effect == 6) p.radiation = 0;
}
void iuse::dogfood(pc& p, item& it)
{
const auto g = game::active();
it.invlet = 0; // C:Whales: unconditional discard after use(opening)
g->draw();
mvprintw(0, 0, "Which direction?");
point dir(get_direction(input()));
if (dir.x == -2) {
messages.add("Invalid direction.");
return;
}
p.moves -= (mobile::mp_turn / 20) * 3;
if (monster* const m_at = g->mon(p.GPSpos+dir)) {
if (m_at->type->id == mon_dog) {
messages.add("The dog seems to like you!");
m_at->friendly = -1;
} else
messages.add("The %s seems quit unimpressed!", m_at->type->name.c_str());
} else
messages.add("You spill the dogfood all over the ground.");
}
// TOOLS below this point!
void iuse::lighter(pc& p, item& it)
{
const auto g = game::active();
g->draw();
mvprintw(0, 0, "Light where?");
const point dir(get_direction(input()));
if (dir.x == -2) {
messages.add("Invalid direction.");
it.charges++;
return;
}
p.moves -= (3 * mobile::mp_turn) / 20;
auto dest = p.GPSpos + dir;
if (contains_ignitable(dest.items_at())) {
dest.add(field(fd_fire, 1, 30));
} else {
messages.add("There's nothing to light there.");
it.charges++;
}
}
void iuse::sew(pc& p, item& it)
{
char ch = p.get_invlet("Repair what?");
const auto src = p.from_invlet(ch);
if (!src) {
messages.add("You do not have that item!");
it.charges++;
return;
}
item* fix = src->first; // backward compatibility
if (!fix->is_armor()) {
messages.add("That isn't clothing!");
it.charges++;
return;
}
if (!fix->made_of(COTTON) && !fix->made_of(WOOL)) {
messages.add("Your %s is not made of cotton or wool.", fix->tname().c_str());
it.charges++;
return;
}
if (fix->damage < 0) {
messages.add("Your %s is already enhanced.", fix->tname().c_str());
it.charges++;
return;
};
p.moves -= 5 * mobile::mp_turn;
int rn = dice(4, 2 + p.sklevel[sk_tailor]);
if (p.dex_cur < 8 && one_in(p.dex_cur)) rn -= rng(2, 6);
if (p.dex_cur >= 16 || (p.dex_cur > 8 && one_in(16 - p.dex_cur))) rn += rng(2, 6);
if (p.dex_cur > 16) rn += rng(0, p.dex_cur - 16);
if (fix->damage == 0) {
p.practice(sk_tailor, 10);
if (rn <= 4) {
messages.add("You damage your %s!", fix->tname().c_str());
fix->damage++;
} else if (rn >= 12) {
messages.add("You make your %s extra-sturdy.", fix->tname().c_str());
fix->damage--;
} else
messages.add("You practice your sewing.");
} else {
p.practice(sk_tailor, 8);
rn -= rng(fix->damage, fix->damage * 2); // unclear that repair is actually more difficult than enhancement
if (rn <= 4) {
messages.add("You damage your %s further!", fix->tname().c_str());
fix->damage++;
if (fix->damage >= 5) {
messages.add("You destroy it!");
p.remove_discard(*src);
}
} else if (rn <= 6) {
messages.add("You don't repair your %s, but you waste lots of thread.", fix->tname().c_str());
clamp_lb<0>(it.charges -= rng(1, 8));
} else if (rn <= 8) {
messages.add("You repair your %s, but waste lots of thread.", fix->tname().c_str());
fix->damage--;
clamp_lb<0>(it.charges -= rng(1, 8));
} else if (rn <= 16) {
messages.add("You repair your %s!", fix->tname().c_str());
fix->damage--;
} else {
messages.add("You repair your %s completely!", fix->tname().c_str());
fix->damage = 0;
}
}
}
void iuse::scissors(pc& p, item& it)
{
char ch = p.get_invlet("Chop up what?");
auto src = p.from_invlet(ch);
if (!src) {
messages.add("You do not have that item!");
return;
}
item* cut = src->first; // backward compatibility
static auto is_string_like = [](int it) {
switch (it)
{
case itm_string_36: return std::optional(std::pair("string", std::pair(6, itm_string_6)));
case itm_rope_30: return std::optional(std::pair("rope", std::pair(5, itm_rope_6)));
default: return std::optional<std::pair<const char*, std::pair<int, itype_id> > >();
};
};
static auto drop_n_clone = [&](int n, item&& obj) {
bool drop = false;
while (0 < n--) {
if (!drop) {
// \todo do we want stacking here?
if (p.volume_carried() >= p.volume_capacity() || !p.assign_invlet(obj)) drop = true;
}
if (drop) p.GPSpos.add(std::move(obj));
else p.i_add(std::move(obj));
}
};
if (cut->type->id == itm_rag) {
messages.add("There's no point in cutting a rag.");
return;
}
if (const auto dest = is_string_like(cut->type->id)) {
p.moves -= 3 * (mobile::mp_turn) / 2;
int pieces = dest->second.first; // backward compatibility
auto i_type = dest->second.second;
messages.add("You cut the %s into %d smaller pieces.", dest->first, dest->second.first);
p.remove_discard(*src);
drop_n_clone(pieces, item(item::types[i_type], int(messages.turn)));
return;
}
if (!cut->made_of(COTTON)) {
messages.add("You can only slice items made of cotton.");
return;
}
const auto vol = cut->volume();
p.moves -= (mobile::mp_turn / 4) * vol;
int count = vol;
if (p.sklevel[sk_tailor] == 0) count = rng(0, count);
else if (p.sklevel[sk_tailor] == 1 && count >= 2) count -= rng(0, 2);
if (dice(3, 3) > p.dex_cur) count -= rng(1, 3);
if (count <= 0) {
messages.add("You clumsily cut the %s into useless ribbons.", cut->tname().c_str());
p.remove_discard(*src);
return;
}
messages.add("You slice the %s into %d rag%s.", cut->tname().c_str(), count,
(count == 1 ? "" : "s"));
p.remove_discard(*src);
drop_n_clone(count, item(item::types[itm_rag], int(messages.turn)));
}
void iuse::extinguisher(pc& p, item& it)
{
const auto g = game::active();
g->draw();
mvprintz(0, 0, c_red, "Pick a direction to spray:");
const point dir(get_direction(input()));
if (dir.x == -2) {
messages.add("Invalid direction!");
it.charges++;
return;
}
p.moves -= (mobile::mp_turn / 5) * 7;
auto pt(dir + p.pos);
{
auto& fd = g->m.field_at(pt);
if (fd.type == fd_fire) {
if (0 >= (fd.density -= rng(2, 3))) g->m.remove_field(pt);
}
}
if (monster* const m_at = g->mon(pt)) {
m_at->moves -= (mobile::mp_turn / 2) * 3;
const bool is_liquid = m_at->made_of(LIQUID); // assumed that liquid is water or similar -- dry ice extinguisher?
if (p.see(*m_at)) {
const auto z_name = grammar::capitalize(m_at->desc(grammar::noun::role::subject, grammar::article::definite));
messages.add("%s is sprayed!", z_name.c_str());
if (is_liquid) messages.add("%s is frozen!", z_name.c_str());
}
if (is_liquid) {
if (m_at->hurt(rng(20, 60))) g->kill_mon(*m_at, &p);
else m_at->speed /= 2;
}
}
if (g->m.move_cost(pt) != 0) {
pt += dir;
auto& fd = g->m.field_at(pt);
if (fd.type == fd_fire) {
fd.density -= rng(0, 1) + rng(0, 1);
if (fd.density <= 0) g->m.remove_field(pt);
else if (3 < fd.density) fd.density = 3;
}
}
}
// xref: construction.cpp/"Board Up Door"
// interpretation: start terrain, dest terrain, # nails, # boards
static constexpr const std::pair<ter_id, std::tuple<ter_id, int, int> > deconstruct_boarded[] = {
std::pair(t_window_boarded,std::tuple(t_window_empty, 8, 3)),
std::pair(t_door_boarded,std::tuple(t_door_b, 12, 3))
};
// competes with std::find_if
// interpretation is iterating over std::pair<key, ....>
template<class range, class K>
auto linear_search(K key, range origin, range _end) // \todo migrate to zero.h
{
do if (key == origin->first) return &origin->second;
while (++origin != _end);
return decltype(&origin->second)(nullptr);
}
void iuse::hammer(pc& p, item& it)
{
const auto g = game::active();
g->draw();
mvprintz(0, 0, c_red, "Pick a direction in which to pry:");
const point dir(get_direction(input()));
if (dir.x == -2) {
messages.add("Invalid direction!");
return;
}
auto& type = g->m.ter(dir + p.pos);
auto deconstruct = linear_search(type, std::begin(deconstruct_boarded), std::end(deconstruct_boarded));
if (!deconstruct) {
messages.add("Hammers can only remove boards from windows and doors.");
messages.add("To board up a window or door, press *");
return;
}
p.moves -= 5 * mobile::mp_turn;
item it_nails(item::types[itm_nail], 0); // assumed pre-apocalypse
it_nails.charges = std::get<1>(*deconstruct);
p.GPSpos.add(std::move(it_nails));
item board(item::types[itm_2x4], 0);
for (int i = 0; i < std::get<2>(*deconstruct); i++) p.GPSpos.add(board);
type = std::get<0>(*deconstruct);
}
void iuse::light_off(player &p, item& it)
{
static auto me = []() { return "You turn the flashlight on."; };
static auto other = [&]() { return p.subject() + " turns the flashlight on."; };
p.if_visible_message(me, other);
it.make(item::types[itm_flashlight_on]);
it.active = true;
}
class message_relay
{
const char* msg;
public:
message_relay(decltype(msg) msg) noexcept : msg(msg) {}
void operator()(const GPS_loc& view) {
game::active()->if_visible_message(msg, view);
}
void operator()(const std::pair<pc*, int>& view) { messages.add(msg); }
void operator()(const std::pair<npc*, int>& view) {
game::active()->if_visible_message(msg, *(view.first));
}
};
void iuse::light_on_off(item& it)
{
const auto g = game::active();
const auto pos = g->find(it).value();
// Turning it off
std::visit(message_relay("The flashlight flicks off."), pos);
it.make(item::types[itm_flashlight]);
it.active = false;
}
void iuse::water_purifier(pc& p, item& it)
{
const auto purify = p.from_invlet(p.get_invlet("Purify what?"));
if (!purify) {
messages.add("You do not have that idea!");
return;
}
if (purify->first->contents.empty()) {
only_water:
messages.add("You can only purify water.");
return;
}
auto& pure = purify->first->contents[0];
if (pure.type->id != itm_water && pure.type->id != itm_salt_water) goto only_water;
pure.make(item::types[itm_water]);
pure.poison = 0;
}
void iuse::two_way_radio(pc& p, item& it)
{
// TODO: More options here. Thoughts...
// > Respond to the SOS of an NPC
// > Report something to a faction
// > Call another player
static constexpr const char* radio_options[] = {
"1: Radio a faction for help...",
"2: Call Acquaintance...", // not implemented properly
"3: General S.O.S.",
"0: Cancel"
};
constexpr const int radio_height = sizeof(radio_options) / sizeof(*radio_options) + 2;
// maximum string length...possible function target
// C++20: std::ranges::max?
int radio_width = 0;
for (const char* const line : radio_options) radio_width = cataclysm::max(radio_width, strlen(line));
radio_width += 5; // historical C:Whales
const auto g = game::active();
WINDOW* w = newwin(radio_height, radio_width, (VIEW - radio_height) / 2, (SCREEN_WIDTH - radio_width) / 2);
wborder(w, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX,
LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX );
// TODO: More options here. Thoughts...
// > Respond to the SOS of an NPC
// > Report something to a faction
// > Call another player
int row = 0;
for (const char* const line : radio_options) mvwaddstrz(w, ++row, 1, c_white, line);
wrefresh(w);
int ch = getch();
if (ch == '1') {
p.moves -= 3 * mobile::mp_turn;
faction* fac = g->list_factions("Call for help...");
if (fac == nullptr) {
it.charges++;
return;
}
int bonus = 0;
if (fac->goal == FACGOAL_CIVILIZATION) bonus += 2;
if (fac->has_job(FACJOB_MERCENARIES)) bonus += 4;
if (fac->has_job(FACJOB_DOCTORS)) bonus += 2;
if (fac->has_value(FACVAL_CHARITABLE)) bonus += 3;
if (fac->has_value(FACVAL_LONERS)) bonus -= 3;
if (fac->has_value(FACVAL_TREACHERY)) bonus -= rng(0, 8);
bonus += fac->respects_u + 3 * fac->likes_u;
if (bonus >= 25) {
popup("They reply, \"Help is on the way!\"");
event::add(event(EVENT_HELP, int(messages.turn) + fac->response_time(g->lev), fac->id, -1, -1));
fac->respects_u -= rng(0, 8);
fac->likes_u -= rng(3, 5);
} else if (bonus >= -5) {