forked from Whales/Cataclysm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.cpp
1891 lines (1772 loc) · 48.3 KB
/
map.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 "map.h"
#include "output.h"
#include "rng.h"
#include "game.h"
#include "line.h"
#include <cmath>
#include <stdlib.h>
#include <fstream>
#define SGN(a) (((a)<0) ? -1 : 1)
#define INBOUNDS(x, y) \
(x >= 0 && x < SEEX * my_MAPSIZE && y >= 0 && y < SEEY * my_MAPSIZE)
enum astar_list {
ASL_NONE,
ASL_OPEN,
ASL_CLOSED
};
map::map()
{
nulter = t_null;
nultrap = tr_null;
if (is_tiny())
my_MAPSIZE = 2;
else
my_MAPSIZE = MAPSIZE;
}
map::map(std::vector<itype*> *itptr, std::vector<itype_id> (*miptr)[num_itloc],
std::vector<trap*> *trptr)
{
nulter = t_null;
nultrap = tr_null;
itypes = itptr;
mapitems = miptr;
traps = trptr;
if (is_tiny())
my_MAPSIZE = 2;
else
my_MAPSIZE = MAPSIZE;
for (int n = 0; n < my_MAPSIZE * my_MAPSIZE; n++) {
grid[n].active_item_count = 0;
grid[n].field_count = 0;
for (int x = 0; x < SEEX; x++) {
for (int y = 0; y < SEEY; y++) {
grid[n].trp[x][y] = tr_null;
grid[n].rad[x][y] = 0;
}
}
}
}
map::~map()
{
}
ter_id& map::ter(int x, int y)
{
if (!INBOUNDS(x, y)) {
nulter = t_null;
return nulter; // Out-of-bounds - null terrain
}
/*
int nonant;
cast_to_nonant(x, y, nonant);
*/
int nonant = int(x / SEEX) + int(y / SEEY) * my_MAPSIZE;
x %= SEEX;
y %= SEEY;
return grid[nonant].ter[x][y];
}
std::string map::tername(int x, int y)
{
return terlist[ter(x, y)].name;
}
std::string map::features(int x, int y)
{
// This is used in an info window that is 46 characters wide, and is expected
// to take up one line. So, make sure it does that.
std::string ret;
if (has_flag(bashable, x, y))
ret += "Smashable. "; // 11 chars (running total)
if (has_flag(diggable, x, y))
ret += "Diggable. "; // 21 chars
if (has_flag(rough, x, y))
ret += "Rough. "; // 28 chars
if (has_flag(sharp, x, y))
ret += "Sharp. "; // 35 chars
return ret;
}
int map::move_cost(int x, int y)
{
return terlist[ter(x, y)].movecost;
}
bool map::trans(int x, int y)
{
// Control statement is a problem. Normally returning false on an out-of-bounds
// is how we stop rays from going on forever. Instead we'll have to include
// this check in the ray loop.
return terlist[ter(x, y)].flags & mfb(transparent) &&
(field_at(x, y).type == 0 || // Fields may obscure the view, too
fieldlist[field_at(x, y).type].transparent[field_at(x, y).density - 1]);
}
bool map::has_flag(t_flag flag, int x, int y)
{
return terlist[ter(x, y)].flags & mfb(flag);
}
bool map::is_destructable(int x, int y)
{
return (has_flag(bashable, x, y) ||
(move_cost(x, y) == 0 && !has_flag(transparent, x, y)));
}
bool map::is_outside(int x, int y)
{
return (ter(x , y ) != t_floor && ter(x - 1, y - 1) != t_floor &&
ter(x - 1, y ) != t_floor && ter(x - 1, y + 1) != t_floor &&
ter(x , y - 1) != t_floor && ter(x , y ) != t_floor &&
ter(x , y + 1) != t_floor && ter(x + 1, y - 1) != t_floor &&
ter(x + 1, y ) != t_floor && ter(x + 1, y + 1) != t_floor &&
ter(x , y ) != t_floor_wax &&
ter(x - 1, y - 1) != t_floor_wax &&
ter(x - 1, y ) != t_floor_wax &&
ter(x - 1, y + 1) != t_floor_wax &&
ter(x , y - 1) != t_floor_wax &&
ter(x , y ) != t_floor_wax &&
ter(x , y + 1) != t_floor_wax &&
ter(x + 1, y - 1) != t_floor_wax &&
ter(x + 1, y ) != t_floor_wax &&
ter(x + 1, y + 1) != t_floor_wax );
}
bool map::flammable_items_at(int x, int y)
{
for (int i = 0; i < i_at(x, y).size(); i++) {
item *it = &(i_at(x, y)[i]);
if (it->made_of(PAPER) || it->made_of(WOOD) || it->made_of(COTTON) ||
it->made_of(POWDER) || it->made_of(VEGGY) || it->is_ammo() ||
it->type->id == itm_whiskey || it->type->id == itm_vodka ||
it->type->id == itm_rum || it->type->id == itm_tequila)
return true;
}
return false;
}
point map::random_outdoor_tile()
{
std::vector<point> options;
for (int x = 0; x < SEEX * my_MAPSIZE; x++) {
for (int y = 0; y < SEEY * my_MAPSIZE; y++) {
if (is_outside(x, y))
options.push_back(point(x, y));
}
}
if (options.empty()) // Nowhere is outdoors!
return point(-1, -1);
return options[rng(0, options.size() - 1)];
}
bool map::bash(int x, int y, int str, std::string &sound)
{
sound = "";
bool smashed_web = false;
if (field_at(x, y).type == fd_web) {
smashed_web = true;
remove_field(x, y);
}
for (int i = 0; i < i_at(x, y).size(); i++) { // Destroy glass items (maybe)
if (i_at(x, y)[i].made_of(GLASS) && one_in(2)) {
if (sound == "")
sound = "A " + i_at(x, y)[i].tname() + " shatters! ";
else
sound = "Some items shatter! ";
for (int j = 0; j < i_at(x, y)[i].contents.size(); j++)
i_at(x, y).push_back(i_at(x, y)[i].contents[j]);
i_rem(x, y, i);
i--;
}
}
switch (ter(x, y)) {
case t_wall_wood:
if (str >= rng(0, 120)) {
sound += "crash!";
ter(x, y) = t_dirt;
int num_boards = rng(8, 20);
for (int i = 0; i < num_boards; i++)
add_item(x, y, (*itypes)[itm_2x4], 0);
return true;
} else {
sound += "whump!";
return true;
}
break;
case t_door_c:
case t_door_locked:
case t_door_locked_alarm:
if (str >= rng(0, 40)) {
sound += "smash!";
ter(x, y) = t_door_b;
return true;
} else {
sound += "whump!";
return true;
}
break;
case t_door_b:
if (str >= rng(0, 30)) {
sound += "crash!";
ter(x, y) = t_door_frame;
int num_boards = rng(2, 6);
for (int i = 0; i < num_boards; i++)
add_item(x, y, (*itypes)[itm_2x4], 0);
return true;
} else {
sound += "wham!";
return true;
}
break;
case t_window:
case t_window_alarm:
if (str >= rng(0, 6)) {
sound += "glass breaking!";
ter(x, y) = t_window_frame;
return true;
} else {
sound += "whack!";
return true;
}
break;
case t_door_boarded:
if (str >= dice(3, 50)) {
sound += "crash!";
ter(x, y) = t_door_frame;
int num_boards = rng(0, 2);
for (int i = 0; i < num_boards; i++)
add_item(x, y, (*itypes)[itm_2x4], 0);
return true;
} else {
sound += "wham!";
return true;
}
break;
case t_window_boarded:
if (str >= dice(3, 30)) {
sound += "crash!";
ter(x, y) = t_window_frame;
int num_boards = rng(0, 2) * rng(0, 1);
for (int i = 0; i < num_boards; i++)
add_item(x, y, (*itypes)[itm_2x4], 0);
return true;
} else {
sound += "wham!";
return true;
}
break;
case t_paper:
if (str >= dice(2, 6) - 2) {
sound += "rrrrip!";
ter(x, y) = t_dirt;
return true;
} else {
sound += "slap!";
return true;
}
break;
case t_toilet:
if (str >= dice(8, 4) - 8) {
sound += "porcelain breaking!";
ter(x, y) = t_rubble;
return true;
} else {
sound += "whunk!";
return true;
}
break;
case t_dresser:
case t_bookcase:
if (str >= dice(3, 45)) {
sound += "smash!";
ter(x, y) = t_floor;
int num_boards = rng(4, 12);
for (int i = 0; i < num_boards; i++)
add_item(x, y, (*itypes)[itm_2x4], 0);
return true;
} else {
sound += "whump.";
return true;
}
break;
case t_wall_glass_h:
case t_wall_glass_v:
case t_wall_glass_h_alarm:
case t_wall_glass_v_alarm:
case t_door_glass_c:
if (str >= rng(0, 20)) {
sound += "glass breaking!";
ter(x, y) = t_floor;
return true;
} else {
sound += "whack!";
return true;
}
break;
case t_reinforced_glass_h:
case t_reinforced_glass_v:
if (str >= rng(60, 100)) {
sound += "glass breaking!";
ter(x, y) = t_floor;
return true;
} else {
sound += "whack!";
return true;
}
break;
case t_tree_young:
if (str >= rng(0, 50)) {
sound += "crunch!";
ter(x, y) = t_underbrush;
int num_sticks = rng(0, 3);
for (int i = 0; i < num_sticks; i++)
add_item(x, y, (*itypes)[itm_stick], 0);
return true;
} else {
sound += "whack!";
return true;
}
break;
case t_underbrush:
if (str >= rng(0, 30) && !one_in(4)) {
sound += "crunch.";
ter(x, y) = t_dirt;
return true;
} else {
sound += "brush.";
return true;
}
break;
case t_marloss:
if (str > rng(0, 40)) {
sound += "crunch!";
ter(x, y) = t_fungus;
return true;
} else {
sound += "whack!";
return true;
}
break;
case t_vat:
if (str >= dice(2, 20)) {
sound += "ker-rash!";
ter(x, y) = t_floor;
return true;
} else {
sound += "plunk.";
return true;
}
case t_crate_c:
case t_crate_o:
if (str >= dice(4, 20)) {
sound += "smash";
ter(x, y) = t_dirt;
int num_boards = rng(1, 5);
for (int i = 0; i < num_boards; i++)
add_item(x, y, (*itypes)[itm_2x4], 0);
return true;
} else {
sound += "wham!";
return true;
}
}
if (move_cost(x, y) == 0) {
sound += "thump!";
return true;
}
return smashed_web;// If we kick empty space, the action is cancelled
}
// map::destroy is only called (?) if the terrain is NOT bashable.
void map::destroy(game *g, int x, int y, bool makesound)
{
switch (ter(x, y)) {
case t_gas_pump:
if (one_in(4))
g->explosion(x, y, 40, 0, true);
else
for (int i = x - 2; i <= x + 2; i++) {
for (int j = y - 2; j <= y + 2; j++) {
if (move_cost(i, j) > 0 && one_in(3))
add_item(i, j, g->itypes[itm_gasoline], 0);
if (move_cost(i, j) > 0 && one_in(6))
add_item(i, j, g->itypes[itm_steel_chunk], 0);
}
}
ter(x, y) = t_rubble;
break;
case t_door_c:
case t_door_b:
case t_door_locked:
case t_door_boarded:
ter(x, y) = t_door_frame;
for (int i = x - 2; i <= x + 2; i++) {
for (int j = y - 2; j <= y + 2; j++) {
if (move_cost(i, j) > 0 && one_in(6))
add_item(i, j, g->itypes[itm_2x4], 0);
}
}
break;
case t_wall_v:
case t_wall_h:
for (int i = x - 2; i <= x + 2; i++) {
for (int j = y - 2; j <= y + 2; j++) {
if (move_cost(i, j) > 0 && one_in(5))
add_item(i, j, g->itypes[itm_rock], 0);
if (move_cost(i, j) > 0 && one_in(4))
add_item(i, j, g->itypes[itm_2x4], 0);
}
}
ter(x, y) = t_rubble;
break;
default:
if (has_flag(explodes, x, y))
g->explosion(x, y, 40, 0, true);
ter(x, y) = t_rubble;
}
if (makesound)
g->sound(x, y, 40, "SMASH!!");
}
void map::shoot(game *g, int x, int y, int &dam, bool hit_items, unsigned flags)
{
if (has_flag(alarmed, x, y) && !g->event_queued(EVENT_WANTED)) {
g->sound(g->u.posx, g->u.posy, 30, "An alarm sounds!");
g->add_event(EVENT_WANTED, int(g->turn) + 300, 0, g->levx, g->levy);
}
switch (ter(x, y)) {
case t_door_c:
case t_door_locked:
case t_door_locked_alarm:
dam -= rng(15, 30);
if (dam > 0)
ter(x, y) = t_door_b;
break;
case t_door_b:
if (hit_items || one_in(8)) { // 1 in 8 chance of hitting the door
dam -= rng(10, 30);
if (dam > 0)
ter(x, y) = t_door_frame;
} else
dam -= rng(0, 1);
break;
case t_door_boarded:
dam -= rng(15, 35);
if (dam > 0)
ter(x, y) = t_door_b;
break;
case t_window:
case t_window_alarm:
dam -= rng(0, 5);
ter(x, y) = t_window_frame;
break;
case t_window_boarded:
dam -= rng(10, 30);
if (dam > 0)
ter(x, y) = t_window_frame;
break;
case t_wall_glass_h:
case t_wall_glass_v:
case t_wall_glass_h_alarm:
case t_wall_glass_v_alarm:
dam -= rng(0, 8);
ter(x, y) = t_floor;
break;
case t_paper:
dam -= rng(4, 16);
if (dam > 0)
ter(x, y) = t_dirt;
if (flags & mfb(IF_AMMO_INCENDIARY))
add_field(g, x, y, fd_fire, 1);
break;
case t_gas_pump:
if (hit_items || one_in(3)) {
if (dam > 15) {
if (flags & mfb(IF_AMMO_INCENDIARY) || flags & mfb(IF_AMMO_FLAME))
g->explosion(x, y, 40, 0, true);
else {
for (int i = x - 2; i <= x + 2; i++) {
for (int j = y - 2; j <= y + 2; j++) {
if (move_cost(i, j) > 0 && one_in(3))
add_item(i, j, g->itypes[itm_gasoline], 0);
}
}
}
ter(x, y) = t_gas_pump_smashed;
}
dam -= 60;
}
break;
case t_vat:
if (dam >= 10) {
g->sound(x, y, 15, "ke-rash!");
ter(x, y) = t_floor;
} else
dam = 0;
break;
default:
if (move_cost(x, y) == 0 && !trans(x, y))
dam = 0; // TODO: Bullets can go through some walls?
else
dam -= (rng(0, 1) * rng(0, 1) * rng(0, 1));
}
if (flags & mfb(IF_AMMO_TRAIL) && !one_in(4))
add_field(g, x, y, fd_smoke, rng(1, 2));
// Now, destroy items on that tile.
if ((move_cost(x, y) == 2 && !hit_items) || !INBOUNDS(x, y))
return; // Items on floor-type spaces won't be shot up.
bool destroyed;
for (int i = 0; i < i_at(x, y).size(); i++) {
destroyed = false;
switch (i_at(x, y)[i].type->m1) {
case GLASS:
case PAPER:
if (dam > rng(2, 8) && one_in(i_at(x, y)[i].volume()))
destroyed = true;
break;
case PLASTIC:
if (dam > rng(2, 10) && one_in(i_at(x, y)[i].volume() * 3))
destroyed = true;
break;
case VEGGY:
case FLESH:
if (dam > rng(10, 40))
destroyed = true;
break;
case COTTON:
case WOOL:
i_at(x, y)[i].damage++;
if (i_at(x, y)[i].damage >= 5)
destroyed = true;
break;
}
if (destroyed) {
for (int j = 0; j < i_at(x, y)[i].contents.size(); j++)
i_at(x, y).push_back(i_at(x, y)[i].contents[j]);
i_rem(x, y, i);
i--;
}
}
}
bool map::hit_with_acid(game *g, int x, int y)
{
if (move_cost(x, y) != 0)
return false; // Didn't hit the tile!
switch (ter(x, y)) {
case t_wall_glass_v:
case t_wall_glass_h:
case t_wall_glass_v_alarm:
case t_wall_glass_h_alarm:
case t_vat:
ter(x, y) = t_floor;
break;
case t_door_c:
case t_door_locked:
case t_door_locked_alarm:
if (one_in(3))
ter(x, y) = t_door_b;
break;
case t_door_b:
if (one_in(4))
ter(x, y) = t_door_frame;
else
return false;
break;
case t_window:
case t_window_alarm:
ter(x, y) = t_window_empty;
break;
case t_wax:
ter(x, y) = t_floor_wax;
break;
case t_toilet:
case t_gas_pump:
case t_gas_pump_smashed:
return false;
case t_card_science:
case t_card_military:
ter(x, y) = t_card_reader_broken;
break;
}
return true;
}
void map::marlossify(int x, int y)
{
int type = rng(1, 9);
switch (type) {
case 1:
case 2:
case 3:
case 4: ter(x, y) = t_fungus; break;
case 5:
case 6:
case 7: ter(x, y) = t_marloss; break;
case 8: ter(x, y) = t_tree_fungal; break;
case 9: ter(x, y) = t_slime; break;
}
}
bool map::open_door(int x, int y, bool inside)
{
if (ter(x, y) == t_door_c) {
ter(x, y) = t_door_o;
return true;
} else if (ter(x, y) == t_door_metal_c) {
ter(x, y) = t_door_metal_o;
return true;
} else if (ter(x, y) == t_door_glass_c) {
ter(x, y) = t_door_glass_o;
return true;
} else if (inside &&
(ter(x, y) == t_door_locked || ter(x, y) == t_door_locked_alarm)) {
ter(x, y) = t_door_o;
return true;
}
return false;
}
void map::translate(ter_id from, ter_id to)
{
if (from == to) {
debugmsg("map::translate %s => %s", terlist[from].name.c_str(),
terlist[from].name.c_str());
return;
}
for (int x = 0; x < SEEX * my_MAPSIZE; x++) {
for (int y = 0; y < SEEY * my_MAPSIZE; y++) {
if (ter(x, y) == from)
ter(x, y) = to;
}
}
}
bool map::close_door(int x, int y)
{
if (ter(x, y) == t_door_o) {
ter(x, y) = t_door_c;
return true;
} else if (ter(x, y) == t_door_metal_o) {
ter(x, y) = t_door_metal_c;
return true;
} else if (ter(x, y) == t_door_glass_o) {
ter(x, y) = t_door_glass_c;
return true;
}
return false;
}
int& map::radiation(int x, int y)
{
if (!INBOUNDS(x, y)) {
nulrad = 0;
return nulrad;
}
/*
int nonant;
cast_to_nonant(x, y, nonant);
*/
int nonant = int(x / SEEX) + int(y / SEEY) * my_MAPSIZE;
x %= SEEX;
y %= SEEY;
return grid[nonant].rad[x][y];
}
std::vector<item>& map::i_at(int x, int y)
{
if (!INBOUNDS(x, y)) {
nulitems.clear();
return nulitems;
}
/*
int nonant;
cast_to_nonant(x, y, nonant);
*/
int nonant = int(x / SEEX) + int(y / SEEY) * my_MAPSIZE;
x %= SEEX;
y %= SEEY;
return grid[nonant].itm[x][y];
}
item map::water_from(int x, int y)
{
item ret((*itypes)[itm_water], 0);
if (ter(x, y) == t_water_sh && one_in(3))
ret.poison = rng(1, 4);
else if (ter(x, y) == t_water_dp && one_in(4))
ret.poison = rng(1, 4);
else if (ter(x, y) == t_sewage)
ret.poison = rng(1, 7);
else if (ter(x, y) == t_toilet && !one_in(3))
ret.poison = rng(1, 3);
return ret;
}
void map::i_rem(int x, int y, int index)
{
if (index > i_at(x, y).size() - 1)
return;
i_at(x, y).erase(i_at(x, y).begin() + index);
}
void map::i_clear(int x, int y)
{
i_at(x, y).clear();
}
point map::find_item(item *it)
{
point ret;
for (ret.x = 0; ret.x < SEEX * my_MAPSIZE; ret.x++) {
for (ret.y = 0; ret.y < SEEY * my_MAPSIZE; ret.y++) {
for (int i = 0; i < i_at(ret.x, ret.y).size(); i++) {
if (it == &i_at(ret.x, ret.y)[i])
return ret;
}
}
}
ret.x = -1;
ret.y = -1;
return ret;
}
void map::add_item(int x, int y, itype* type, int birthday)
{
item tmp(type, birthday);
tmp = tmp.in_its_container(itypes);
if (tmp.made_of(LIQUID) && has_flag(swimmable, x, y))
return;
add_item(x, y, tmp);
}
void map::add_item(int x, int y, item new_item)
{
if (!INBOUNDS(x, y))
return;
if (new_item.made_of(LIQUID) && has_flag(swimmable, x, y))
return;
if (has_flag(noitem, x, y) || i_at(x, y).size() >= 26) {// Too many items there
std::vector<point> okay;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (INBOUNDS(i, j) && move_cost(i, j) > 0 && !has_flag(noitem, i, j) &&
i_at(i, j).size() < 26)
okay.push_back(point(i, j));
}
}
if (okay.size() == 0) {
for (int i = x - 2; i <= x + 2; i++) {
for (int j = y - 2; j <= y + 2; j++) {
if (INBOUNDS(i, j) && move_cost(i, j) > 0 && !has_flag(noitem, i, j) &&
i_at(i, j).size() < 26)
okay.push_back(point(i, j));
}
}
}
if (okay.size() == 0)// STILL?
return;
point choice = okay[rng(0, okay.size() - 1)];
add_item(choice.x, choice.y, new_item);
return;
}
/*
int nonant;
cast_to_nonant(x, y, nonant);
*/
int nonant = int(x / SEEX) + int(y / SEEY) * my_MAPSIZE;
x %= SEEX;
y %= SEEY;
grid[nonant].itm[x][y].push_back(new_item);
if (new_item.active)
grid[nonant].active_item_count++;
}
void map::process_active_items(game *g)
{
for (int gx = 0; gx < my_MAPSIZE; gx++) {
for (int gy = 0; gy < my_MAPSIZE; gy++) {
if (grid[gx + gy * my_MAPSIZE].active_item_count > 0)
process_active_items_in_submap(g, gx + gy * my_MAPSIZE);
}
}
}
void map::process_active_items_in_submap(game *g, int nonant)
{
it_tool* tmp;
iuse use;
for (int i = 0; i < SEEX; i++) {
for (int j = 0; j < SEEY; j++) {
std::vector<item> *items = &(grid[nonant].itm[i][j]);
for (int n = 0; n < items->size(); n++) {
if ((*items)[n].active) {
tmp = dynamic_cast<it_tool*>((*items)[n].type);
(use.*tmp->use)(g, &(g->u), &((*items)[n]), true);
if (tmp->turns_per_charge > 0 && int(g->turn) % tmp->turns_per_charge == 0)
(*items)[n].charges--;
if ((*items)[n].charges <= 0) {
(use.*tmp->use)(g, &(g->u), &((*items)[n]), false);
if (tmp->revert_to == itm_null || (*items)[n].charges == -1) {
items->erase(items->begin() + n);
grid[nonant].active_item_count--;
n--;
} else
(*items)[n].type = g->itypes[tmp->revert_to];
}
}
}
}
}
}
void map::use_amount(point origin, int range, itype_id type, int quantity,
bool use_container)
{
for (int radius = 0; radius <= range && quantity > 0; radius++) {
for (int x = origin.x - radius; x <= origin.x + radius; x++) {
for (int y = origin.y - radius; y <= origin.y + radius; y++) {
if (rl_dist(origin.x, origin.y, x, y) < radius)
y++; // Skip over already-examined tiles
else {
for (int n = 0; n < i_at(x, y).size() && quantity > 0; n++) {
item* curit = &(i_at(x, y)[n]);
bool used_contents = false;
for (int m = 0; m < curit->contents.size() && quantity > 0; m++) {
if (curit->contents[m].type->id == type) {
quantity--;
curit->contents.erase(curit->contents.begin() + m);
m--;
used_contents = true;
}
}
if (use_container && used_contents) {
i_rem(x, y, n);
n--;
} else if (curit->type->id == type && quantity > 0) {
quantity--;
i_rem(x, y, n);
n--;
}
}
}
}
}
}
}
void map::use_charges(point origin, int range, itype_id type, int quantity)
{
for (int radius = 0; radius <= range && quantity > 0; radius++) {
for (int x = origin.x - radius; x <= origin.x + radius; x++) {
for (int y = origin.y - radius; y <= origin.y + radius; y++) {
if (rl_dist(origin.x, origin.y, x, y) < radius)
y++; // Skip over already-examined tiles
else {
for (int n = 0; n < i_at(x, y).size(); n++) {
item* curit = &(i_at(x, y)[n]);
// Check contents first
for (int m = 0; m < curit->contents.size() && quantity > 0; m++) {
if (curit->contents[m].type->id == type) {
if (curit->contents[m].charges <= quantity) {
quantity -= curit->contents[m].charges;
if (curit->contents[m].destroyed_at_zero_charges()) {
curit->contents.erase(curit->contents.begin() + m);
m--;
} else
curit->contents[m].charges = 0;
} else {
curit->contents[m].charges -= quantity;
return;
}
}
}
// Now check the actual item
if (curit->type->id == type) {
if (curit->charges <= quantity) {
quantity -= curit->charges;
if (curit->destroyed_at_zero_charges()) {
i_rem(x, y, n);
n--;
} else
curit->charges = 0;
} else {
curit->charges -= quantity;
return;
}
}
}
}
}
}
}
}
trap_id& map::tr_at(int x, int y)
{
if (!INBOUNDS(x, y)) {
nultrap = tr_null;
return nultrap; // Out-of-bounds, return our null trap
}
/*
int nonant;
cast_to_nonant(x, y, nonant);
*/
int nonant = int(x / SEEX) + int(y / SEEY) * my_MAPSIZE;
x %= SEEX;
y %= SEEY;
if (x < 0 || x >= SEEX || y < 0 || y >= SEEY) {
debugmsg("tr_at contained bad x:y %d:%d", x, y);
nultrap = tr_null;
return nultrap; // Out-of-bounds, return our null trap
}
return grid[nonant].trp[x][y];
}
void map::add_trap(int x, int y, trap_id t)
{
/*
int nonant;
cast_to_nonant(x, y, nonant);
*/
int nonant = int(x / SEEX) + int(y / SEEY) * my_MAPSIZE;
x %= SEEX;
y %= SEEY;
grid[nonant].trp[x][y] = t;
}
void map::disarm_trap(game *g, int x, int y)
{
if (tr_at(x, y) == tr_null) {
debugmsg("Tried to disarm a trap where there was none (%d %d)", x, y);
return;
}
int diff = g->traps[tr_at(x, y)]->difficulty;
int roll = rng(g->u.sklevel[sk_traps], 4 * g->u.sklevel[sk_traps]);
while ((rng(5, 20) < g->u.per_cur || rng(1, 20) < g->u.dex_cur) && roll < 50)
roll++;
if (roll >= diff) {
g->add_msg("You disarm the trap!");
std::vector<itype_id> comp = g->traps[tr_at(x, y)]->components;
for (int i = 0; i < comp.size(); i++) {
if (comp[i] != itm_null)
add_item(x, y, g->itypes[comp[i]], 0);
}
tr_at(x, y) = tr_null;
} else if (roll >= diff * .8)
g->add_msg("You fail to disarm the trap.");
else {
g->add_msg("You fail to disarm the trap, and you set it off!");
trap* tr = g->traps[tr_at(x, y)];