This repository has been archived by the owner on Apr 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathItems.c
7757 lines (7051 loc) · 283 KB
/
Items.c
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
/*
* Items.c
* Brogue
*
* Created by Brian Walker on 1/17/09.
* Copyright 2012. All rights reserved.
*
* This file is part of Brogue.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Rogue.h"
#include "IncludeGlobals.h"
#include <math.h>
item *initializeItem() {
short i;
item *theItem;
theItem = (item *) malloc(sizeof(item));
memset(theItem, '\0', sizeof(item) );
theItem->category = 0;
theItem->kind = 0;
theItem->flags = 0;
theItem->displayChar = '&';
theItem->foreColor = &itemColor;
theItem->inventoryColor = &white;
theItem->armor = 0;
theItem->strengthRequired = 0;
theItem->enchant1 = 0;
theItem->enchant2 = 0;
theItem->timesEnchanted = 0;
theItem->vorpalEnemy = 0;
theItem->charges = 0;
theItem->quantity = 1;
theItem->quiverNumber = 0;
theItem->originDepth = 0;
theItem->inscription[0] = '\0';
theItem->nextItem = NULL;
for (i=0; i < KEY_ID_MAXIMUM; i++) {
theItem->keyLoc[i].x = 0;
theItem->keyLoc[i].y = 0;
theItem->keyLoc[i].machine = 0;
theItem->keyLoc[i].disposableHere = false;
}
return theItem;
}
// Allocates space, generates a specified item (or random category/kind if -1)
// and returns a pointer to that item. The item is not given a location here
// and is not inserted into the item chain!
item *generateItem(unsigned short theCategory, short theKind) {
item *theItem = initializeItem();
makeItemInto(theItem, theCategory, theKind);
return theItem;
}
unsigned long pickItemCategory(unsigned long theCategory) {
short i, sum, randIndex;
short probabilities[13] = {50, 42, 52, 3, 3, 10, 8, 2, 3, 2, 0, 0, 0};
unsigned short correspondingCategories[13] = {GOLD, SCROLL, POTION, STAFF, WAND, WEAPON, ARMOR, FOOD, RING, CHARM, AMULET, GEM, KEY};
sum = 0;
for (i=0; i<13; i++) {
if (theCategory <= 0 || theCategory & correspondingCategories[i]) {
sum += probabilities[i];
}
}
if (sum == 0) {
return theCategory; // e.g. when you pass in AMULET or GEM, since they have no frequency
}
randIndex = rand_range(1, sum);
for (i=0; ; i++) {
if (theCategory <= 0 || theCategory & correspondingCategories[i]) {
if (randIndex <= probabilities[i]) {
return correspondingCategories[i];
}
randIndex -= probabilities[i];
}
}
}
// Sets an item to the given type and category (or chooses randomly if -1) with all other stats
item *makeItemInto(item *theItem, unsigned long itemCategory, short itemKind) {
itemTable *theEntry;
if (itemCategory <= 0) {
itemCategory = ALL_ITEMS;
}
itemCategory = pickItemCategory(itemCategory);
theItem->category = itemCategory;
switch (itemCategory) {
case FOOD:
if (itemKind < 0) {
itemKind = chooseKind(foodTable, NUMBER_FOOD_KINDS);
}
theEntry = &foodTable[itemKind];
theItem->displayChar = FOOD_CHAR;
theItem->flags |= ITEM_IDENTIFIED;
break;
case WEAPON:
if (itemKind < 0) {
itemKind = chooseKind(weaponTable, NUMBER_WEAPON_KINDS);
}
theEntry = &weaponTable[itemKind];
theItem->damage = weaponTable[itemKind].range;
theItem->strengthRequired = weaponTable[itemKind].strengthRequired;
theItem->displayChar = WEAPON_CHAR;
switch (itemKind) {
case DAGGER:
theItem->flags |= ITEM_SNEAK_ATTACK_BONUS;
break;
case MACE:
case HAMMER:
theItem->flags |= ITEM_ATTACKS_HIT_SLOWLY;
break;
case WHIP:
theItem->flags |= (ITEM_ATTACKS_EXTEND | ITEM_AGGRAVATE_ATTACKS);
break;
case RAPIER:
theItem->flags |= (ITEM_ATTACKS_QUICKLY | ITEM_LUNGE_ATTACKS);
break;
case FLAIL:
theItem->flags |= ITEM_PASS_ATTACKS;
break;
case SPEAR:
case PIKE:
theItem->flags |= ITEM_ATTACKS_PENETRATE;
break;
case AXE:
case WAR_AXE:
theItem->flags |= ITEM_ATTACKS_ALL_ADJACENT;
break;
default:
break;
}
if (rand_percent(40)) {
theItem->enchant1 += rand_range(1, 3);
if (rand_percent(50)) {
// cursed
theItem->enchant1 *= -1;
theItem->flags |= ITEM_CURSED;
if (rand_percent(33)) { // give it a bad runic
theItem->enchant2 = rand_range(NUMBER_GOOD_WEAPON_ENCHANT_KINDS, NUMBER_WEAPON_RUNIC_KINDS - 1);
theItem->flags |= ITEM_RUNIC;
//makes bad runics something you want to use
if (rand_percent(40)) {
theItem->enchant1 *= -1;
while (rand_percent(15)) {
theItem->enchant1++;
}
}
}
} else if (rand_range(3, 10)
* ((theItem->flags & ITEM_ATTACKS_HIT_SLOWLY) ? 2 : 1)
/ ((theItem->flags & ITEM_ATTACKS_QUICKLY) ? 2 : 1)
> theItem->damage.lowerBound) {
// give it a good runic; lower damage items are more likely to be runic
theItem->enchant2 = rand_range(0, NUMBER_GOOD_WEAPON_ENCHANT_KINDS - 1);
theItem->flags |= ITEM_RUNIC;
if (theItem->enchant2 == W_SLAYING) {
theItem->vorpalEnemy = chooseVorpalEnemy();
}
} else {
while (rand_percent(10)) {
theItem->enchant1++;
}
}
}
if (itemKind == DART || itemKind == INCENDIARY_DART || itemKind == JAVELIN) {
if (itemKind == INCENDIARY_DART) {
theItem->quantity = rand_range(3, 6);
} else {
theItem->quantity = rand_range(5, 18);
}
theItem->quiverNumber = rand_range(1, 60000);
theItem->flags &= ~(ITEM_CURSED | ITEM_RUNIC); // throwing weapons can't be cursed or runic
theItem->enchant1 = 0; // throwing weapons can't be magical
}
theItem->charges = WEAPON_KILLS_TO_AUTO_ID; // kill 20 enemies to auto-identify
break;
case ARMOR:
if (itemKind < 0) {
itemKind = chooseKind(armorTable, NUMBER_ARMOR_KINDS);
}
theEntry = &armorTable[itemKind];
theItem->armor = randClump(armorTable[itemKind].range);
theItem->strengthRequired = armorTable[itemKind].strengthRequired;
theItem->displayChar = ARMOR_CHAR;
theItem->charges = ARMOR_DELAY_TO_AUTO_ID; // this many turns until it reveals its enchants and whether runic
if (rand_percent(40)) {
theItem->enchant1 += rand_range(1, 3);
if (rand_percent(50)) {
// cursed
theItem->enchant1 *= -1;
theItem->flags |= ITEM_CURSED;
if (rand_percent(33)) { // give it a bad runic
theItem->enchant2 = rand_range(NUMBER_GOOD_ARMOR_ENCHANT_KINDS, NUMBER_ARMOR_ENCHANT_KINDS - 1);
theItem->flags |= ITEM_RUNIC;
theItem->flags |= ITEM_CURSED;
//makes bad runics something you want to use
if (rand_percent(40)) {
theItem->enchant1 *= -1;
while (rand_percent(15)) {
theItem->enchant1++;
}
}
}
} else if (rand_range(0, 95) > theItem->armor) { // give it a good runic
theItem->enchant2 = rand_range(0, NUMBER_GOOD_ARMOR_ENCHANT_KINDS - 1);
theItem->flags |= ITEM_RUNIC;
if (theItem->enchant2 == A_IMMUNITY) {
theItem->vorpalEnemy = chooseVorpalEnemy();
}
} else {
while (rand_percent(10)) {
theItem->enchant1++;
}
}
}
break;
case SCROLL:
if (itemKind < 0) {
itemKind = chooseKind(scrollTable, NUMBER_SCROLL_KINDS);
}
theEntry = &scrollTable[itemKind];
theItem->displayChar = SCROLL_CHAR;
theItem->flags |= ITEM_FLAMMABLE;
break;
case POTION:
if (itemKind < 0) {
itemKind = chooseKind(potionTable, NUMBER_POTION_KINDS);
}
theEntry = &potionTable[itemKind];
theItem->displayChar = POTION_CHAR;
break;
case STAFF:
if (itemKind < 0) {
itemKind = chooseKind(staffTable, NUMBER_STAFF_KINDS);
}
theEntry = &staffTable[itemKind];
theItem->displayChar = STAFF_CHAR;
theItem->charges = 2;
if (rand_percent(50)) {
theItem->charges++;
if (rand_percent(15)) {
theItem->charges++;
while (rand_percent(10)) {
theItem->charges++;
}
}
}
theItem->enchant1 = theItem->charges;
theItem->enchant2 = (itemKind == STAFF_BLINKING || itemKind == STAFF_OBSTRUCTION ? 1000 : 500); // start with no recharging mojo
break;
case WAND:
if (itemKind < 0) {
itemKind = chooseKind(wandTable, NUMBER_WAND_KINDS);
}
theEntry = &wandTable[itemKind];
theItem->displayChar = WAND_CHAR;
theItem->charges = randClump(wandTable[itemKind].range);
break;
case RING:
if (itemKind < 0) {
itemKind = chooseKind(ringTable, NUMBER_RING_KINDS);
}
theEntry = &ringTable[itemKind];
theItem->displayChar = RING_CHAR;
theItem->enchant1 = randClump(ringTable[itemKind].range);
theItem->charges = RING_DELAY_TO_AUTO_ID; // how many turns of being worn until it auto-identifies
if (rand_percent(16)) {
// cursed
theItem->enchant1 *= -1;
theItem->flags |= ITEM_CURSED;
} else {
while (rand_percent(10)) {
theItem->enchant1++;
}
}
break;
case CHARM:
if (itemKind < 0) {
itemKind = chooseKind(charmTable, NUMBER_CHARM_KINDS);
}
theItem->displayChar = CHARM_CHAR;
theItem->charges = 0; // Charms are initially ready for use.
theItem->enchant1 = randClump(charmTable[itemKind].range);
while (rand_percent(7)) {
theItem->enchant1++;
}
theItem->flags |= ITEM_IDENTIFIED;
break;
case GOLD:
theEntry = NULL;
theItem->displayChar = GOLD_CHAR;
theItem->quantity = rand_range(50 + rogue.depthLevel * 10, 100 + rogue.depthLevel * 15);
break;
case AMULET:
theEntry = NULL;
theItem->displayChar = AMULET_CHAR;
itemKind = 0;
theItem->flags |= ITEM_IDENTIFIED;
break;
case GEM:
theEntry = NULL;
theItem->displayChar = GEM_CHAR;
itemKind = 0;
theItem->flags |= ITEM_IDENTIFIED;
break;
case KEY:
theEntry = NULL;
theItem->displayChar = KEY_CHAR;
theItem->flags |= ITEM_IDENTIFIED;
break;
default:
theEntry = NULL;
message("something has gone terribly wrong!", true);
break;
}
if (theItem
&& !(theItem->flags & ITEM_IDENTIFIED)
&& (!(theItem->category & (POTION | SCROLL) ) || (theEntry && !theEntry->identified))) {
theItem->flags |= ITEM_CAN_BE_IDENTIFIED;
}
theItem->kind = itemKind;
return theItem;
}
short chooseKind(itemTable *theTable, short numKinds) {
short i, totalFrequencies = 0, randomFrequency;
for (i=0; i<numKinds; i++) {
totalFrequencies += max(0, theTable[i].frequency);
}
randomFrequency = rand_range(1, totalFrequencies);
for (i=0; randomFrequency > theTable[i].frequency; i++) {
randomFrequency -= max(0, theTable[i].frequency);
}
return i;
}
// Places an item at (x,y) if provided or else a random location if they're 0. Inserts item into the floor list.
item *placeItem(item *theItem, short x, short y) {
short loc[2];
enum dungeonLayers layer;
char theItemName[DCOLS], buf[DCOLS];
if (x <= 0 || y <= 0) {
randomMatchingLocation(&(loc[0]), &(loc[1]), FLOOR, NOTHING, -1);
theItem->xLoc = loc[0];
theItem->yLoc = loc[1];
} else {
theItem->xLoc = x;
theItem->yLoc = y;
}
removeItemFromChain(theItem, floorItems); // just in case; double-placing an item will result in game-crashing loops in the item list
addItemToChain(theItem, floorItems);
pmap[theItem->xLoc][theItem->yLoc].flags |= HAS_ITEM;
if ((theItem->flags & ITEM_MAGIC_DETECTED) && itemMagicChar(theItem)) {
pmap[theItem->xLoc][theItem->yLoc].flags |= ITEM_DETECTED;
}
if (cellHasTerrainFlag(x, y, T_IS_DF_TRAP)
&& !cellHasTerrainFlag(x, y, T_MOVES_ITEMS)
&& !(pmap[x][y].flags & PRESSURE_PLATE_DEPRESSED)) {
pmap[x][y].flags |= PRESSURE_PLATE_DEPRESSED;
if (playerCanSee(x, y)) {
if (cellHasTMFlag(x, y, TM_IS_SECRET)) {
discover(x, y);
refreshDungeonCell(x, y);
}
itemName(theItem, theItemName, false, false, NULL);
sprintf(buf, "a pressure plate clicks underneath the %s!", theItemName);
message(buf, true);
}
for (layer = 0; layer < NUMBER_TERRAIN_LAYERS; layer++) {
if (tileCatalog[pmap[x][y].layers[layer]].flags & T_IS_DF_TRAP) {
spawnDungeonFeature(x, y, &(dungeonFeatureCatalog[tileCatalog[pmap[x][y].layers[layer]].fireType]), true, false);
promoteTile(x, y, layer, false);
}
}
}
return theItem;
}
void fillItemSpawnHeatMap(unsigned short heatMap[DCOLS][DROWS], unsigned short heatLevel, short x, short y) {
enum directions dir;
short newX, newY;
if (pmap[x][y].layers[DUNGEON] == DOOR) {
heatLevel += 10;
} else if (pmap[x][y].layers[DUNGEON] == SECRET_DOOR) {
heatLevel += 3000;
}
if (heatMap[x][y] > heatLevel) {
heatMap[x][y] = heatLevel;
}
for (dir = 0; dir < 4; dir++) {
newX = x + nbDirs[dir][0];
newY = y + nbDirs[dir][1];
if (coordinatesAreInMap(newX, newY)
&& (!cellHasTerrainFlag(newX, newY, T_OBSTRUCTS_PASSABILITY | T_IS_DEEP_WATER | T_LAVA_INSTA_DEATH | T_AUTO_DESCENT)
|| cellHasTMFlag(newX, newY, TM_IS_SECRET))
&& heatLevel < heatMap[newX][newY]) {
fillItemSpawnHeatMap(heatMap, heatLevel, newX, newY);
}
}
}
void coolHeatMapAt(unsigned short heatMap[DCOLS][DROWS], short x, short y, unsigned long *totalHeat) {
short k, l;
unsigned short currentHeat;
currentHeat = heatMap[x][y];
*totalHeat -= heatMap[x][y];
heatMap[x][y] = 0;
// lower the heat near the chosen location
for (k = -5; k <= 5; k++) {
for (l = -5; l <= 5; l++) {
if (coordinatesAreInMap(x+k, y+l) && heatMap[x+k][y+l] == currentHeat) {
heatMap[x+k][y+l] = max(1, heatMap[x+k][y+l]/10);
*totalHeat -= (currentHeat - heatMap[x+k][y+l]);
}
}
}
}
// Returns false if no place could be found.
// That should happen only if the total heat is zero.
boolean getItemSpawnLoc(unsigned short heatMap[DCOLS][DROWS], short *x, short *y, unsigned long *totalHeat) {
unsigned long randIndex;
unsigned short currentHeat;
short i, j;
if (*totalHeat <= 0) {
return false;
}
randIndex = rand_range(1, *totalHeat);
//printf("\nrandIndex: %i", randIndex);
for (i=0; i<DCOLS; i++) {
for (j=0; j<DROWS; j++) {
currentHeat = heatMap[i][j];
if (randIndex <= currentHeat) { // this is the spot!
*x = i;
*y = j;
return true;
}
randIndex -= currentHeat;
}
}
brogueAssert(0); // should never get here!
return false;
}
#define aggregateGoldLowerBound(d) (pow((double) (d), 3.05) + 320 * (d) + FLOAT_FUDGE)
#define aggregateGoldUpperBound(d) (pow((double) (d), 3.05) + 420 * (d) + FLOAT_FUDGE)
// Generates and places items for the level. Must pass the location of the up-stairway on the level.
void populateItems(short upstairsX, short upstairsY) {
if (!ITEMS_ENABLED) {
return;
}
item *theItem;
unsigned short itemSpawnHeatMap[DCOLS][DROWS];
short i, j, numberOfItems, numberOfGoldPiles, goldBonusProbability, x = 0, y = 0;
unsigned long totalHeat;
short theCategory, theKind, randomDepthOffset = 0;
#ifdef AUDIT_RNG
char RNGmessage[100];
#endif
if (rogue.depthLevel > AMULET_LEVEL) {
if (rogue.depthLevel - AMULET_LEVEL - 1 >= 8) {
numberOfItems = 1;
} else {
const short lumenstoneDistribution[8] = {3, 3, 3, 2, 2, 2, 2, 2};
numberOfItems = lumenstoneDistribution[rogue.depthLevel - AMULET_LEVEL - 1];
}
numberOfGoldPiles = 0;
} else {
rogue.lifePotionFrequency += 34;
rogue.strengthPotionFrequency += 17;
rogue.enchantScrollFrequency += 30;
numberOfItems = 3;
while (rand_percent(60)) {
numberOfItems++;
}
if (rogue.depthLevel <= 2) {
numberOfItems += 2; // 4 extra items to kickstart your career as a rogue
} else if (rogue.depthLevel <= 4) {
numberOfItems++; // and 2 more here
}
numberOfGoldPiles = min(5, (int) (rogue.depthLevel / 4 + FLOAT_FUDGE));
for (goldBonusProbability = 60;
rand_percent(goldBonusProbability) && numberOfGoldPiles <= 10;
goldBonusProbability -= 15) {
numberOfGoldPiles++;
}
// Adjust the amount of gold if we're past depth 5 and we were below or above
// the production schedule as of the previous depth.
if (rogue.depthLevel > 5) {
if (rogue.goldGenerated < aggregateGoldLowerBound(rogue.depthLevel - 1)) {
numberOfGoldPiles += 2;
} else if (rogue.goldGenerated > aggregateGoldUpperBound(rogue.depthLevel - 1)) {
numberOfGoldPiles -= 2;
}
}
}
// Create an item spawn heat map to bias item generation behind secret doors (and, to a lesser
// extent, regular doors). This is in terms of the number of secret/regular doors that must be
// passed to reach the area when pathing to it from the upward staircase.
// This is why there are often several good items in well hidden secret rooms. Without it,
// those rooms are usually empty, which is demoralizing after you took the trouble to find them.
for (i=0; i<DCOLS; i++) {
for (j=0; j<DROWS; j++) {
itemSpawnHeatMap[i][j] = 50000;
}
}
fillItemSpawnHeatMap(itemSpawnHeatMap, 5, upstairsX, upstairsY);
totalHeat = 0;
#ifdef AUDIT_RNG
sprintf(RNGmessage, "\n\nInitial heat map for level %i:\n", rogue.currentTurnNumber);
RNGLog(RNGmessage);
#endif
for (j=0; j<DROWS; j++) {
for (i=0; i<DCOLS; i++) {
if (cellHasTerrainFlag(i, j, T_OBSTRUCTS_ITEMS | T_PATHING_BLOCKER)
|| (pmap[i][j].flags & (IS_CHOKEPOINT | IN_LOOP | IS_IN_MACHINE))
|| passableArcCount(i, j) > 1) { // Not in hallways, quest rooms, loops or chokepoints, please.
itemSpawnHeatMap[i][j] = 0;
} else if (itemSpawnHeatMap[i][j] == 50000) {
itemSpawnHeatMap[i][j] = 0;
pmap[i][j].layers[DUNGEON] = WALL; // due to a bug that created occasional isolated one-cell islands;
// not sure if it's still around, but this is a good-enough failsafe
}
#ifdef AUDIT_RNG
sprintf(RNGmessage, "%u%s%s\t%s",
itemSpawnHeatMap[i][j],
((pmap[i][j].flags & IS_CHOKEPOINT) ? " (C)": ""), // chokepoint
((pmap[i][j].flags & IN_LOOP) ? " (L)": ""), // loop
(i == DCOLS-1 ? "\n" : ""));
RNGLog(RNGmessage);
#endif
totalHeat += itemSpawnHeatMap[i][j];
}
}
if (D_INSPECT_LEVELGEN) {
short **map = allocGrid();
for (i=0; i<DCOLS; i++) {
for (j=0; j<DROWS; j++) {
map[i][j] = itemSpawnHeatMap[i][j] * -1;
}
}
dumpLevelToScreen();
displayGrid(map);
freeGrid(map);
temporaryMessage("Item spawn heat map:", true);
}
if (rogue.depthLevel > 2) {
// Include a random factor in food and potion of life generation to make things slightly less predictable.
randomDepthOffset = rand_range(-1, 1);
randomDepthOffset += rand_range(-1, 1);
}
for (i=0; i<numberOfItems; i++) {
theCategory = ALL_ITEMS & ~GOLD; // gold is placed separately, below, so it's not a punishment
theKind = -1;
scrollTable[SCROLL_ENCHANTING].frequency = rogue.enchantScrollFrequency;
potionTable[POTION_STRENGTH].frequency = rogue.strengthPotionFrequency;
potionTable[POTION_LIFE].frequency = rogue.lifePotionFrequency;
// Adjust the desired item category if necessary.
if ((rogue.foodSpawned + foodTable[RATION].strengthRequired / 3) * 4
<= (pow(rogue.depthLevel, 1.35) + randomDepthOffset) * foodTable[RATION].strengthRequired * 0.45 + FLOAT_FUDGE) {
// Guarantee a certain nutrition minimum of the approximate equivalent of one ration every four levels,
// with more food on deeper levels since they generally take more turns to complete.
theCategory = FOOD;
if (rogue.depthLevel > AMULET_LEVEL) {
numberOfItems++; // Food isn't at the expense of lumenstones.
}
} else if (rogue.depthLevel > AMULET_LEVEL) {
theCategory = GEM;
} else if (rogue.lifePotionsSpawned * 4 + 3 < rogue.depthLevel + randomDepthOffset) {
theCategory = POTION;
theKind = POTION_LIFE;
}
// Generate the item.
theItem = generateItem(theCategory, theKind);
theItem->originDepth = rogue.depthLevel;
if (theItem->category & FOOD) {
rogue.foodSpawned += foodTable[theItem->kind].strengthRequired;
if (D_MESSAGE_ITEM_GENERATION) printf("\n(:) Depth %i: generated food", rogue.depthLevel);
}
// Choose a placement location not in a hallway.
do {
if ((theItem->category & FOOD) || ((theItem->category & POTION) && theItem->kind == POTION_STRENGTH)) {
randomMatchingLocation(&x, &y, FLOOR, NOTHING, -1); // food and gain strength don't follow the heat map
} else {
getItemSpawnLoc(itemSpawnHeatMap, &x, &y, &totalHeat);
}
} while (passableArcCount(x, y) > 1);
brogueAssert(coordinatesAreInMap(x, y));
// Cool off the item spawning heat map at the chosen location:
coolHeatMapAt(itemSpawnHeatMap, x, y, &totalHeat);
// Regulate the frequency of enchantment scrolls and strength/life potions.
if ((theItem->category & SCROLL) && theItem->kind == SCROLL_ENCHANTING) {
rogue.enchantScrollFrequency -= 50;
if (D_MESSAGE_ITEM_GENERATION) printf("\n(?) Depth %i: generated an enchant scroll at %i frequency", rogue.depthLevel, rogue.enchantScrollFrequency);
} else if (theItem->category & POTION && theItem->kind == POTION_LIFE) {
if (D_MESSAGE_ITEM_GENERATION) printf("\n(!l) Depth %i: generated a life potion at %i frequency", rogue.depthLevel, rogue.lifePotionFrequency);
rogue.lifePotionFrequency -= 150;
rogue.lifePotionsSpawned++;
} else if (theItem->category & POTION && theItem->kind == POTION_STRENGTH) {
if (D_MESSAGE_ITEM_GENERATION) printf("\n(!s) Depth %i: generated a strength potion at %i frequency", rogue.depthLevel, rogue.strengthPotionFrequency);
rogue.strengthPotionFrequency -= 50;
}
// Place the item.
placeItem(theItem, x, y); // Random valid location already obtained according to heat map.
if (D_INSPECT_LEVELGEN) {
short **map = allocGrid();
short i2, j2;
for (i2=0; i2<DCOLS; i2++) {
for (j2=0; j2<DROWS; j2++) {
map[i2][j2] = itemSpawnHeatMap[i2][j2] * -1;
}
}
dumpLevelToScreen();
displayGrid(map);
freeGrid(map);
plotCharWithColor(theItem->displayChar, mapToWindowX(x), mapToWindowY(y), &black, &purple);
temporaryMessage("Added an item.", true);
}
}
// Now generate gold.
for (i=0; i<numberOfGoldPiles; i++) {
theItem = generateItem(GOLD, -1);
getItemSpawnLoc(itemSpawnHeatMap, &x, &y, &totalHeat);
coolHeatMapAt(itemSpawnHeatMap, x, y, &totalHeat);
placeItem(theItem, x, y);
rogue.goldGenerated += theItem->quantity;
}
if (D_INSPECT_LEVELGEN) {
dumpLevelToScreen();
temporaryMessage("Added gold.", true);
}
scrollTable[SCROLL_ENCHANTING].frequency = 0; // No enchant scrolls or strength/life potions can spawn except via initial
potionTable[POTION_STRENGTH].frequency = 0; // item population or blueprints that create them specifically.
potionTable[POTION_LIFE].frequency = 0;
if (D_MESSAGE_ITEM_GENERATION) printf("\n---- Depth %i: %lu gold generated so far.", rogue.depthLevel, rogue.goldGenerated);
}
// Name of this function is a bit misleading -- basically returns true iff the item will stack without consuming an extra slot
// i.e. if it's a throwing weapon with a sibling already in your pack. False for potions and scrolls.
boolean itemWillStackWithPack(item *theItem) {
item *tempItem;
if (!(theItem->quiverNumber)) {
return false;
} else {
for (tempItem = packItems->nextItem;
tempItem != NULL && tempItem->quiverNumber != theItem->quiverNumber;
tempItem = tempItem->nextItem);
return (tempItem ? true : false);
}
}
void removeItemFrom(short x, short y) {
short layer;
pmap[x][y].flags &= ~HAS_ITEM;
if (cellHasTMFlag(x, y, TM_PROMOTES_ON_ITEM_PICKUP)) {
for (layer = 0; layer < NUMBER_TERRAIN_LAYERS; layer++) {
if (tileCatalog[pmap[x][y].layers[layer]].mechFlags & TM_PROMOTES_ON_ITEM_PICKUP) {
promoteTile(x, y, layer, false);
}
}
}
}
// adds the item at (x,y) to the pack
void pickUpItemAt(short x, short y) {
item *theItem;
creature *monst;
char buf[COLS * 3], buf2[COLS * 3];
short guardianX, guardianY;
rogue.disturbed = true;
// find the item
theItem = itemAtLoc(x, y);
if (!theItem) {
message("Error: Expected item; item not found.", true);
return;
}
if ((theItem->flags & ITEM_KIND_AUTO_ID)
&& tableForItemCategory(theItem->category, NULL)
&& !(tableForItemCategory(theItem->category, NULL)[theItem->kind].identified)) {
identifyItemKind(theItem);
}
if ((theItem->category & WAND)
&& wandTable[theItem->kind].identified
&& wandTable[theItem->kind].range.lowerBound == wandTable[theItem->kind].range.upperBound) {
theItem->flags |= ITEM_IDENTIFIED;
}
if (numberOfItemsInPack() < MAX_PACK_ITEMS || (theItem->category & GOLD) || itemWillStackWithPack(theItem)) {
// remove from floor chain
pmap[x][y].flags &= ~ITEM_DETECTED;
if (!removeItemFromChain(theItem, floorItems)) {
brogueAssert(false);
}
if (theItem->category & GOLD) {
rogue.gold += theItem->quantity;
sprintf(buf, "you found %i pieces of gold.", theItem->quantity);
messageWithColor(buf, &itemMessageColor, false);
deleteItem(theItem);
removeItemFrom(x, y); // triggers tiles with T_PROMOTES_ON_ITEM_PICKUP
return;
}
if ((theItem->category & AMULET) && numberOfMatchingPackItems(AMULET, 0, 0, false)) {
message("you already have the Amulet of Yendor.", false);
deleteItem(theItem);
return;
}
theItem = addItemToPack(theItem);
itemName(theItem, buf2, true, true, NULL); // include suffix, article
sprintf(buf, "you now have %s (%c).", buf2, theItem->inventoryLetter);
messageWithColor(buf, &itemMessageColor, false);
removeItemFrom(x, y); // triggers tiles with T_PROMOTES_ON_ITEM_PICKUP
if ((theItem->category & AMULET)
&& !(rogue.yendorWarden)) {
// Identify the amulet guardian, or generate one if there isn't one.
for (monst = monsters->nextCreature; monst != NULL; monst = monst->nextCreature) {
if (monst->info.monsterID == MK_WARDEN_OF_YENDOR) {
rogue.yendorWarden = monst;
break;
}
}
if (!rogue.yendorWarden) {
getRandomMonsterSpawnLocation(&guardianX, &guardianY);
monst = generateMonster(MK_WARDEN_OF_YENDOR, false, false);
monst->xLoc = guardianX;
monst->yLoc = guardianY;
pmap[guardianX][guardianY].flags |= HAS_MONSTER;
rogue.yendorWarden = monst;
}
}
} else {
theItem->flags |= ITEM_PLAYER_AVOIDS; // explore shouldn't try to pick it up more than once.
itemName(theItem, buf2, false, true, NULL); // include article
sprintf(buf, "Your pack is too full to pick up %s.", buf2);
message(buf, false);
}
}
void conflateItemCharacteristics(item *newItem, item *oldItem) {
// let magic detection and other flags propagate to the new stack...
newItem->flags |= (oldItem->flags & (ITEM_MAGIC_DETECTED | ITEM_IDENTIFIED | ITEM_PROTECTED | ITEM_RUNIC
| ITEM_RUNIC_HINTED | ITEM_CAN_BE_IDENTIFIED | ITEM_MAX_CHARGES_KNOWN));
// keep the higher enchantment and lower strength requirement...
if (oldItem->enchant1 > newItem->enchant1) {
newItem->enchant1 = oldItem->enchant1;
}
if (oldItem->strengthRequired < newItem->strengthRequired) {
newItem->strengthRequired = oldItem->strengthRequired;
}
// Copy the inscription.
if (oldItem->inscription && !newItem->inscription) {
strcpy(newItem->inscription, oldItem->inscription);
}
// Keep track of origin depth only if every item in the stack has the same origin depth.
if (oldItem->originDepth <= 0 || newItem->originDepth != oldItem->originDepth) {
newItem->originDepth = 0;
}
}
void stackItems(item *newItem, item *oldItem) {
//Increment the quantity of the old item...
newItem->quantity += oldItem->quantity;
// ...conflate attributes...
conflateItemCharacteristics(newItem, oldItem);
// ...and delete the new item.
deleteItem(oldItem);
}
item *addItemToPack(item *theItem) {
item *previousItem, *tempItem;
char itemLetter;
// Can the item stack with another in the inventory?
if (theItem->category & (FOOD|POTION|SCROLL|GEM)) {
for (tempItem = packItems->nextItem; tempItem != NULL; tempItem = tempItem->nextItem) {
if (theItem->category == tempItem->category && theItem->kind == tempItem->kind) {
// We found a match!
stackItems(tempItem, theItem);
// Pass back the incremented (old) item. No need to add it to the pack since it's already there.
return tempItem;
}
}
} else if (theItem->category & WEAPON && theItem->quiverNumber > 0) {
for (tempItem = packItems->nextItem; tempItem != NULL; tempItem = tempItem->nextItem) {
if (theItem->category == tempItem->category && theItem->kind == tempItem->kind
&& theItem->quiverNumber == tempItem->quiverNumber) {
// We found a match!
stackItems(tempItem, theItem);
// Pass back the incremented (old) item. No need to add it to the pack since it's already there.
return tempItem;
}
}
}
// assign a reference letter to the item
itemLetter = nextAvailableInventoryCharacter();
if (itemLetter) {
theItem->inventoryLetter = itemLetter;
}
// insert at proper place in pack chain
for (previousItem = packItems;
previousItem->nextItem != NULL && previousItem->nextItem->category <= theItem->category;
previousItem = previousItem->nextItem);
theItem->nextItem = previousItem->nextItem;
previousItem->nextItem = theItem;
return theItem;
}
short numberOfItemsInPack() {
short theCount = 0;
item *theItem;
for(theItem = packItems->nextItem; theItem != NULL; theItem = theItem->nextItem) {
theCount += (theItem->category & WEAPON ? 1 : theItem->quantity);
}
return theCount;
}
char nextAvailableInventoryCharacter() {
boolean charTaken[26];
short i;
item *theItem;
char c;
for(i=0; i<26; i++) {
charTaken[i] = false;
}
for (theItem = packItems->nextItem; theItem != NULL; theItem = theItem->nextItem) {
c = theItem->inventoryLetter;
if (c >= 'a' && c <= 'z') {
charTaken[c - 'a'] = true;
}
}
for(i=0; i<26; i++) {
if (!charTaken[i]) {
return ('a' + i);
}
}
return 0;
}
void checkForDisenchantment(item *theItem) {
char buf[COLS], buf2[COLS];
if ((theItem->flags & ITEM_RUNIC)
&& theItem->enchant2 < NUMBER_GOOD_ARMOR_ENCHANT_KINDS
&& theItem->enchant1 <= 0) {
theItem->enchant2 = 0;
theItem->flags &= ~(ITEM_RUNIC | ITEM_RUNIC_HINTED | ITEM_RUNIC_IDENTIFIED);
if (theItem->flags & ITEM_IDENTIFIED) {
identify(theItem);
itemName(theItem, buf2, false, false, NULL);
sprintf(buf, "the runes vanish from your %s!", buf2);
messageWithColor(buf, &itemMessageColor, false);
}
}
}
boolean itemIsSwappable(const item *theItem) {
if ((theItem->category & CAN_BE_SWAPPED)
&& theItem->quiverNumber == 0) {
return true;
} else {
return false;
}
}
void swapItemToEnchantLevel(item *theItem, short newEnchant, boolean enchantmentKnown) {
short x, y, charmPercent;
char buf1[COLS * 3], buf2[COLS * 3];
if ((theItem->category & STAFF) && newEnchant < 2
|| (theItem->category & CHARM) && newEnchant < 1
|| (theItem->category & WAND) && newEnchant < 0) {
itemName(theItem, buf1, false, true, NULL);
sprintf(buf2, "%s shatter%s from the strain!",
buf1,
theItem->quantity == 1 ? "s" : "");
x = theItem->xLoc;
y = theItem->yLoc;
removeItemFromChain(theItem, floorItems);
pmap[x][y].flags &= ~(HAS_ITEM | ITEM_DETECTED);
if (pmap[x][y].flags & (ANY_KIND_OF_VISIBLE | DISCOVERED | ITEM_DETECTED)) {
refreshDungeonCell(x, y);
}
if (playerCanSee(x, y)) {
messageWithColor(buf2, &itemMessageColor, false);
}
} else {
if ((theItem->category & STAFF)
&& theItem->charges > newEnchant) {
theItem->charges = newEnchant;
}
if (theItem->category & CHARM) {
charmPercent = theItem->charges * 100 / charmRechargeDelay(theItem->kind, theItem->enchant1);
theItem->charges = charmPercent * charmRechargeDelay(theItem->kind, newEnchant) / 100;
}
if (enchantmentKnown) {
if (theItem->category & STAFF) {
theItem->flags |= ITEM_MAX_CHARGES_KNOWN;
}
theItem->flags |= ITEM_IDENTIFIED;
} else {
theItem->flags &= ~(ITEM_MAX_CHARGES_KNOWN | ITEM_IDENTIFIED);
theItem->flags |= ITEM_CAN_BE_IDENTIFIED;