-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathitem.cpp
4802 lines (4098 loc) · 98.8 KB
/
item.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
/*
* PROPRIETARY INFORMATION. This software is proprietary to POWDER
* Development, and is not to be reproduced, transmitted, or disclosed
* in any way without written permission.
*
* Produced by: Jeff Lait
*
* POWDER Development
*
* NAME: item.cpp ( POWDER Library, C++ )
*
* COMMENTS:
* Implementation of the item manipulation routines
*/
#include <mygba.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "assert.h"
#include "artifact.h"
#include "item.h"
#include "itemstack.h"
#include "creature.h"
#include "gfxengine.h"
#include "msg.h"
#include "map.h"
#include "sramstream.h"
#include "mobref.h"
#include "victory.h"
#include "piety.h"
#include "encyc_support.h"
//
// ITEM defintions
//
// Global data:
// This is a list of what true magic item each mundane item will correspond
// to. What type it refers to varies depending on the item's type, ie:
// potions will refer to POTION_.
u8 glb_magicitem[NUM_ITEMS];
// This tracks if each given item type has been ided. This is different from
// a specic item has been ided. A generically ided item will properly
// index its glb_magicitem class to give its type (acid potion rather than
// purple potion), a specific id will do this and its own state (bless vs curse)
bool glb_itemid[NUM_ITEMS];
// This is the overloaded name someone has given this dude.
// It has been strduped so should be freed.
char *glb_itemnames[NUM_ITEMS];
int glbItemCount = 0;
// This is for the callback to track who did the zapping.
MOBREF ITEM::ourZapper;
void
item_init()
{
memset(glb_itemnames, 0, sizeof(char *) * NUM_ITEMS);
}
ITEM::ITEM()
{
myNext = 0;
myName.setName(0);
glbItemCount++;
}
ITEM::~ITEM()
{
MOB *mob;
mob = myCorpseMob.getMob();
if (mob)
{
// This MUST be safe, as we own it!
// We want to punish the owner if we are a familiar.
// This will catch the case of corpses decaying...
// (Or being eaten!)
// One problem is I think double jeapordy for smashing a statue
// of your familiar. But such evil scum that do that deserve
// to be punished.
if (mob->hasIntrinsic(INTRINSIC_FAMILIAR))
{
MOB *master;
master = mob->getMaster();
if (master)
{
master->systemshock();
master->formatAndReport("%U <feel> a great loss.");
}
}
delete mob;
}
glbItemCount--;
}
void
item_randomizetype(MAGICTYPE_NAMES magictype, int num)
{
int i, j;
u8 *lut;
lut = new u8[num];
for (i = 0; i < num; i++)
{
lut[i] = i;
}
// Now, mix randomly...
rand_shuffle(lut, num);
// Now, run through our items and assign the relevant type for each
// potion, asserting we have enough!
j = 0;
for (i = 0; i < NUM_ITEMS; i++)
{
if (glb_itemdefs[i].magictype == magictype)
{
glb_magicitem[i] = lut[j++];
UT_ASSERT(j <= num);
}
}
delete [] lut;
// Verify we gave away each item exactly once.
UT_ASSERT(j == num);
}
void
ITEM::init()
{
int i;
// Clear the item names.
for (i = 0; i < NUM_ITEMS; i++)
{
if (glb_itemnames[i])
free(glb_itemnames[i]);
glb_itemnames[i] = 0;
}
// Build all the magic lookup tables...
memset(glb_magicitem, 0, NUM_ITEMS);
// Clear out the id flags.
memset(glb_itemid, 0, NUM_ITEMS);
// Now, for each of the special types, enumerate them...
item_randomizetype(MAGICTYPE_POTION, NUM_POTIONS);
item_randomizetype(MAGICTYPE_SCROLL, NUM_SCROLLS);
item_randomizetype(MAGICTYPE_RING, NUM_RINGS);
item_randomizetype(MAGICTYPE_HELM, NUM_HELMS);
item_randomizetype(MAGICTYPE_BOOTS, NUM_BOOTSS);
item_randomizetype(MAGICTYPE_AMULET, NUM_AMULETS);
item_randomizetype(MAGICTYPE_WAND, NUM_WANDS);
item_randomizetype(MAGICTYPE_SPELLBOOK, NUM_SPELLBOOKS);
item_randomizetype(MAGICTYPE_STAFF, NUM_STAFFS);
}
ITEM *
ITEM::createClone() const
{
ITEM *clone;
clone = ITEM::create((ITEM_NAMES) myDefinition);
clone->myStackCount = myStackCount;
clone->myX = myX;
clone->myY = myY;
clone->myName = myName;
clone->myEnchantment = myEnchantment;
clone->myCharges = myCharges;
clone->myPoison = myPoison;
clone->myPoisonCharges = myPoisonCharges;
clone->myFlag1 = myFlag1;
clone->myCorpseMob.setMob(0);
return clone;
}
bool
ITEM::petrify(MAP *map, MOB *petrifier)
{
// Mounds of flesh turn into boulders.
// Things made of flesh become stones.
if (myDefinition == ITEM_MOUNDFLESH)
{
myDefinition = ITEM_BOULDER;
myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 | ITEMFLAG1_ISBLOCKING);
formatAndReport("The mound of flesh hardens into a boulder.");
return true;
}
if (getMaterial() == MATERIAL_FLESH)
{
if (defn().isquest)
{
// No petrification of quest items!
formatAndReport("%U <resist> the petrification!");
return false;
}
// This is made of flesh. Should turn into a rock.
formatAndReport("%U <harden> into a rock.");
myDefinition = ITEM_ROCK;
return true;
}
return false;
}
bool
ITEM::unpetrify(MAP *map, MOB *unpetrifier)
{
if (myDefinition == ITEM_STATUE)
{
// This is at least possible
MOB *mob;
mob = myCorpseMob.getMob();
if (!mob)
{
// Mobless statue.
formatAndReport("%U, being but a work of art, <collapse> into a meatball.");
myDefinition = ITEM_MEATBALL;
return true;
}
// Bring back to life
mob->clearIntrinsic(INTRINSIC_DEAD);
// Register on the map.
mob->move(getX(), getY(), true);
map->registerMob(mob);
// We may have somehow been looted in the meantime.
mob->rebuildAppearance();
mob->rebuildWornIntrinsic();
mob->formatAndReport("The statue of %U comes to life!");
// Important so we don't delete ourselves :>
myCorpseMob.setMob(0);
map->dropItem(this);
delete this;
return true;
}
if (myDefinition == ITEM_BOULDER)
{
// Becomes a mound of flesh.
formatAndReport("%U <collapse> into a mound of flesh.");
myDefinition = ITEM_MOUNDFLESH;
myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 & ~ITEMFLAG1_ISBLOCKING);
return true;
}
if (getMaterial() == MATERIAL_STONE)
{
if (defn().isquest)
{
// No petrification of quest items!
formatAndReport("%U <remain> stony!");
return false;
}
// Transforms into a meatball.
formatAndReport("%U <collapse>...");
myDefinition = ITEM_MEATBALL;
formatAndReport("into %U.");
return true;
}
return false;
}
ITEM *
ITEM::polymorph(MOB *polyer)
{
// Flavour text...
formatAndReport(polyer, "A shimmering cloud engulfs %U.");
// Check for poly-immune items.
if (defn().isquest || hasIntrinsic(INTRINSIC_UNCHANGING) || isArtifact())
{
formatAndReport("%U <shudder>.");
return 0;
}
// Create a new item of the same type.
ITEMTYPE_NAMES itemtype;
itemtype = getItemType();
// Note artifacts can never arise from polying.
ITEM *newitem;
newitem = ITEM::createRandomType(itemtype, false);
newitem->formatAndReport(polyer, "The cloud dissipates revealing %U.");
return newitem;
}
bool
ITEM::resurrect(MAP *map, MOB *resser, int x, int y)
{
if (myDefinition == ITEM_BONES || myDefinition == ITEM_CORPSE)
{
// This is at least possible
MOB *mob;
mob = myCorpseMob.getMob();
if (!mob)
{
// This likely should be an assert: Why does a corpse/bones
// lack a mob?
resser->reportMessage("The spirit has travelled too far.");
return false;
}
// See if we can find room in which to resurrect the mob.
// The closest valid square is used.
// We or in STD_SWIM here as we want creatures brought back
// inside water to stay in the water.
if (!map->findCloseTile(x, y, (MOVE_NAMES) (mob->getMoveType() | MOVE_STD_SWIM)))
{
formatAndReport("%U <twitch> feebily.");
return false;
}
// Bring back to the verge of death.
mob->setMinimalHP();
if (mob->canResurrectFromCorpse())
{
// Trolls get full hit points.
mob->setMaximalHP();
}
mob->clearIntrinsic(INTRINSIC_DEAD);
// Register on the map.
mob->move(x, y, true);
map->registerMob(mob);
// The mob may have been looted on death. Thus, we rebuild
// it's worn intrinisics.
mob->rebuildWornIntrinsic();
mob->rebuildAppearance();
mob->formatAndReport("%U <return> from the dead!");
// After being resurrected, one is, of course, thankful.
mob->makeSlaveOf(resser);
// Important so we don't delete ourselves :>
myCorpseMob.setMob(0);
// Drop back into whatever liquid we were in. This is a bit
// mean as pets that die in pits will insta die on resurrecting
// as they will fall back into the pit. However, you can easily
// fix this by carrying the corpse out.
map->dropMobs(mob->getX(), mob->getY(), true, resser);
return true;
}
return false;
}
bool
ITEM::raiseZombie(MAP *map, MOB *raiser)
{
if (myDefinition == ITEM_CORPSE)
{
// This is at least possible
MOB *mob, *zombie;
// It doesn't matter if the mob is there, it just allows
// us to get extra hits.
mob = myCorpseMob.getMob();
// Note we don't clear out myCorpseMob which means that
// if the mob is a familiar we will be punished by zombifying
// it.
// On reflection, I think this is appropriate.
zombie = MOB::create(MOB_ZOMBIE);
zombie->destroyInventory();
if (mob)
{
zombie->forceHP(mob->getMaxHP());
zombie->setOrigDefinition(mob->getDefinition());
}
// Register on the map.
zombie->move(getX(), getY(), true);
map->registerMob(zombie);
zombie->formatAndReport("%U <rise> from the dead!");
// The zombie is marked as tame...
zombie->makeSlaveOf(raiser);
return true;
}
return false;
}
bool
ITEM::raiseSkeleton(MAP *map, MOB *raiser)
{
if (myDefinition == ITEM_BONES)
{
// This is at least possible
MOB *mob, *skeleton;
// It doesn't matter if the mob is there, it just allows
// us to get extra hits.
mob = myCorpseMob.getMob();
skeleton = MOB::create(MOB_SKELETON);
skeleton->destroyInventory();
if (mob)
{
skeleton->forceHP(mob->getMaxHP());
skeleton->setOrigDefinition(mob->getDefinition());
}
// Register on the map.
skeleton->move(getX(), getY(), true);
map->registerMob(skeleton);
skeleton->formatAndReport("%U <rise> from the dead!");
// The skeleton is marked as tame...
skeleton->makeSlaveOf(raiser);
return true;
}
return false;
}
bool
ITEM::doBreak(int dc, MOB *breaker, MOB *owner, MAP *map, int x, int y)
{
bool shouldbreak = false;
bool deleteself = false;
// Determine if this DC is enough to break the item...
// For now, potions break...
if (getItemType() == ITEMTYPE_POTION)
{
shouldbreak = true;
}
if (getDefinition() == ITEM_BOTTLE)
shouldbreak = true;
if (getDefinition() == ITEM_MINDACIDHAND)
{
deleteself = true;
shouldbreak = true;
}
// TODO: Build break chance into item properties,
// can have Glass Swords then.
if (getDefinition() == ITEM_ARROW || getDefinition() == ITEM_FIREARROW)
{
// Blessed arrows much less likely to break than cursed.
int chance;
chance = 10;
if (isCursed())
chance = 50;
if (isBlessed())
chance = 1;
// A bit more likely to break...
if (getDefinition() == ITEM_FIREARROW)
chance *= 2;
// If the breaker is a ranger, we don't break!
if (breaker && breaker->hasIntrinsic(INTRINSIC_DRESSED_RANGER))
chance = 0;
if (rand_chance(chance))
{
deleteself = true;
shouldbreak = true;
}
}
// Exit early if there is no breakage.
if (!shouldbreak)
return false;
// Stash this before piety as piety may kill us.
ourZapper.setMob(breaker);
// Provide the breaker with piety...
if (breaker)
breaker->pietyBreak(this);
// Switch on type & hence effect.
switch (getItemType())
{
case ITEMTYPE_MISC:
{
if (getDefinition() == ITEM_BOTTLE)
{
// Empty bottles grenade...
// FALL THROUGH
}
else
break;
}
case ITEMTYPE_POTION:
{
// Potions are always fully destroyed
deleteself = true;
// Give the appropriate dialog...
formatAndReport("%U <shatter>.");
if (map)
{
map->fireBall(x, y, 1 + isBlessed() - isCursed(), true,
grenadeCallbackStatic,
this);
}
break;
}
case ITEMTYPE_AMULET:
case ITEMTYPE_WEAPON:
case ITEMTYPE_ARMOUR:
case ITEMTYPE_RING:
case ITEMTYPE_SCROLL:
case ITEMTYPE_SPELLBOOK:
case ITEMTYPE_WAND:
case ITEMTYPE_STAFF:
case NUM_ITEMTYPES:
case ITEMTYPE_NONE:
case ITEMTYPE_ANY:
case ITEMTYPE_ARTIFACT:
break;
}
// Destroy our self if required.
if (deleteself)
{
// If we are stacked, we can escape actually breaking...
if (getStackCount() > 1)
{
ITEM *top;
// This is very round about. The theory goes we may
// do something zany in splitStack or delete...
top = splitStack();
delete top;
// Well, it broke, but the ITEM * is still valid...
return false;
}
if (owner)
{
owner->dropItem(getX(), getY());
owner->rebuildAppearance();
owner->rebuildWornIntrinsic();
}
delete this;
}
// Breakage occurred!
return true;
}
void
ITEM::renameType(const char *newname)
{
if (glb_itemnames[myDefinition])
free(glb_itemnames[myDefinition]);
if (newname && newname[0])
{
glb_itemnames[myDefinition] = strdup(newname);
}
else
{
glb_itemnames[myDefinition] = 0;
}
}
void
ITEM::rename(const char *newname)
{
myName.setName(newname);
}
void
ITEM::rename(BUF buf)
{
rename(buf.buffer());
}
const char *
ITEM::getPersonalName() const
{
return myName.getName();
}
ITEM *
ITEM::create(ITEM_NAMES definition, bool allowartifact, bool nocurse)
{
ITEM *item;
item = new ITEM();
item->myDefinition = definition;
item->myStackCount = rand_dice(glb_itemdefs[item->myDefinition].stacksize);
item->myX = item->myY = 0;
item->myFlag1 = (ITEMFLAG1_NAMES)glb_itemdefs[item->myDefinition].flag1;
item->myNext = 0;
item->myEnchantment = 0;
item->myCharges = 0;
item->myPoison = POISON_NONE;
item->myPoisonCharges = 0;
// Determine cursed state...
CURSECHANCE_NAMES cursechance;
cursechance = (CURSECHANCE_NAMES)
glb_itemdefs[item->myDefinition].cursechance;
// Do indirection on special magic types...
switch ((MAGICTYPE_NAMES) glb_itemdefs[item->myDefinition].magictype)
{
case MAGICTYPE_POTION:
cursechance = (CURSECHANCE_NAMES) glb_potiondefs[glb_magicitem[item->myDefinition]].cursechance;
break;
case MAGICTYPE_SCROLL:
cursechance = (CURSECHANCE_NAMES) glb_scrolldefs[glb_magicitem[item->myDefinition]].cursechance;
break;
case MAGICTYPE_RING:
cursechance = (CURSECHANCE_NAMES) glb_ringdefs[glb_magicitem[item->myDefinition]].cursechance;
break;
case MAGICTYPE_HELM:
cursechance = (CURSECHANCE_NAMES) glb_helmdefs[glb_magicitem[item->myDefinition]].cursechance;
break;
case MAGICTYPE_BOOTS:
cursechance = (CURSECHANCE_NAMES) glb_bootsdefs[glb_magicitem[item->myDefinition]].cursechance;
break;
case MAGICTYPE_AMULET:
cursechance = (CURSECHANCE_NAMES) glb_amuletdefs[glb_magicitem[item->myDefinition]].cursechance;
break;
case MAGICTYPE_WAND:
cursechance = (CURSECHANCE_NAMES) glb_wanddefs[glb_magicitem[item->myDefinition]].cursechance;
break;
case MAGICTYPE_SPELLBOOK:
cursechance = (CURSECHANCE_NAMES) glb_spellbookdefs[glb_magicitem[item->myDefinition]].cursechance;
break;
case MAGICTYPE_STAFF:
cursechance = (CURSECHANCE_NAMES) glb_staffdefs[glb_magicitem[item->myDefinition]].cursechance;
break;
case MAGICTYPE_NONE:
break;
case NUM_MAGICTYPES:
UT_ASSERT(!"Unknown magic type!");
break;
}
// Probabilities of each.
int curse, normal, bless;
curse = glb_cursechancedefs[cursechance].curse;
normal = glb_cursechancedefs[cursechance].normal;
bless = glb_cursechancedefs[cursechance].bless;
UT_ASSERT(curse + normal + bless == 100);
int percent;
percent = rand_choice(100);
if (!nocurse)
{
if (percent < curse)
item->myFlag1 = (ITEMFLAG1_NAMES) (item->myFlag1 | ITEMFLAG1_ISCURSED);
else if (percent < curse + bless)
item->myFlag1 = (ITEMFLAG1_NAMES) (item->myFlag1 | ITEMFLAG1_ISBLESSED);
}
// Determine enchantment. Only certain types of items will
// have a default enchantment.
// You can enchant anything, however.
if (item->isBoots() || item->isHelmet() || item->isShield() ||
item->isJacket() || item->isWeapon())
{
// Armour enchant or weapon
// We have a 10% chance of +/-, 50-50 for each.
// This gives 1% for +2 or -2, 18% chance for +1 and 18% chance for -1.
// Bah, not very good. This makes 40% of items enchanted...
// Thus, we hard code:
// 1% +2
// 9% +1
// 9% -1
// 1% -2
// This is also silly, as we get limitted to +/-2.
// Instead, just use a power distribution and have 10% less chance
// for every bonus.
// The major flaw with this is that we go from a 20% enchanted
// to a 10% enchanted. Thus, we encompsass the first test
// with a 20% check.
// Randomly determine if we will increase or decrease the enchantment.
int enchantdir;
if (rand_choice(2))
enchantdir = 1;
else
enchantdir = -1;
item->myEnchantment = 0;
if (rand_chance(20))
{
item->enchant(enchantdir);
// Each additional level of enchantment has 10% chance of going
// through.
// This loop will terminate. I hope.
// Call me paranoid...
int maxenchant = 50;
while (rand_chance(10))
{
item->enchant(enchantdir);
maxenchant--;
if (!maxenchant)
{
UT_ASSERT(!"Excessive enchantment!");
break;
}
}
}
// If the enchantment was negative, we get an extra 30% chance
// of it being (more) cursed.
if (item->myEnchantment < 0 && rand_chance(30))
item->makeCursed();
// Otherwise, if it is positive, there is a chance for blessed.
if (item->myEnchantment > 0 && rand_chance(30))
item->makeBlessed();
}
// Add in charges...
if (item->getMagicType() == MAGICTYPE_WAND)
{
item->myCharges = rand_dice(glb_wanddefs[item->getMagicClass()].charges);
}
if (item->getMagicType() == MAGICTYPE_SPELLBOOK)
{
item->myCharges = rand_dice(glb_spellbookdefs[item->getMagicClass()].charges);
}
// One in a hundred mundanes is a real artifact! Woot!
if (allowartifact && rand_chance(1))
item->makeArtifact();
return item;
}
ITEM_NAMES
ITEM::lookupMagicItem(MAGICTYPE_NAMES magictype, int magicclass)
{
int itemtype;
for (itemtype = 0; itemtype < NUM_ITEMS; itemtype++)
{
if (glb_itemdefs[itemtype].magictype == magictype)
{
if (glb_magicitem[itemtype] == magicclass)
{
return (ITEM_NAMES) itemtype;
}
}
}
return ITEM_NONE;
}
ITEM *
ITEM::createMagicItem(MAGICTYPE_NAMES magictype, int magicclass,
bool allowartifact,
bool nocurse)
{
ITEM_NAMES item;
item = lookupMagicItem(magictype, magicclass);
if (item != ITEM_NONE)
return ITEM::create(item, allowartifact, nocurse);
return 0;
}
ITEM *
ITEM::createRandom(bool allowartifact)
{
int table[NUM_ITEMS];
int i, max;
int prob;
max = 0;
for (i = 0; i < NUM_ITEMS; i++)
{
if (glb_itemdefs[i].magictype == MAGICTYPE_SCROLL)
prob = glb_scrolldefs[glb_magicitem[i]].rarity;
else
prob = glb_itemdefs[i].rarity;
prob *= ((int)glb_itemtypedefs[glb_itemdefs[i].itemtype].rarity);
table[i] = prob;
max += prob;
}
return createRandomFromTable(table, max, allowartifact);
}
ITEM *
ITEM::createRandomType(ITEMTYPE_NAMES type, bool allowartifact)
{
int table[NUM_ITEMS];
int i, max;
int prob;
// Special case item types.
if (type == ITEMTYPE_ANY)
return ITEM::createRandom(allowartifact);
if (type == ITEMTYPE_ARTIFACT)
{
ITEM *item;
item = ITEM::createRandom(false);
item->makeArtifact();
return item;
}
max = 0;
for (i = 0; i < NUM_ITEMS; i++)
{
if (glb_itemdefs[i].itemtype == type)
{
if (glb_itemdefs[i].magictype == MAGICTYPE_SCROLL)
prob = glb_scrolldefs[glb_magicitem[i]].rarity;
else
prob = glb_itemdefs[i].rarity;
}
else
prob = 0;
table[i] = prob;
max += prob;
}
return createRandomFromTable(table, max, allowartifact);
}
ITEM *
ITEM::createRandomFromTable(int *table, int max_prob, bool allowartifact)
{
int item, i;
if (!max_prob)
{
UT_ASSERT(!"No such items.");
return 0;
}
item = rand_choice(max_prob);
for (i = 0; i < NUM_ITEMS; i++)
{
item -= table[i];
if (item < 0)
return create((ITEM_NAMES) i, allowartifact);
}
UT_ASSERT(!"Could not find item!");
return 0;
}
ITEM *
ITEM::createCorpse(MOB *mob)
{
ITEM *item;
item = ITEM::create(ITEM_CORPSE, false);
item->myCorpseMob.setMob(mob);
item->myCharges = 20;
// Boneless critters get extra time as corpses as they have
// no skeleton state.
if (mob->isBoneless())
item->myCharges += 20;
return item;
}
ITEM *
ITEM::createStatue(MOB *mob)
{
ITEM *item;
item = ITEM::create(ITEM_STATUE, false);
item->myCorpseMob.setMob(mob);
return item;
}
void
ITEM::setPos(int x, int y)
{
myX = x;
myY = y;
}
void
ITEM::enchant(int delta)
{
int newval;
newval = myEnchantment + delta;
if (newval < -99)
newval = -99;
if (newval > 99)
newval = 99;
myEnchantment = newval;
}
void
ITEM::makeArtifact(const char *name)
{
myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 | ITEMFLAG1_ISARTIFACT);
if (!name)
rename(artifact_buildname(getDefinition()));
else
rename(name);
}
void
ITEM::makeVanilla()
{
// Ensure it isn't artifact.
myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 & ~ITEMFLAG1_ISARTIFACT);
rename(0);
makePoisoned(POISON_NONE, 0);
myEnchantment = 0;
myCharges = 0;
// Ensure neither bless nor cursed.
myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 & ~ITEMFLAG1_ISBLESSED);
myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 & ~ITEMFLAG1_ISCURSED);
}
bool
ITEM::doHeartbeat(MAP *map, MOB *owner)
{
// Determine if we are in a proper phase.
if (!speed_isheartbeatphase())
return true;
// Run mind acid decay...
if (myDefinition == ITEM_MINDACIDHAND)
{
if (myCharges)
myCharges--;
else
{
// Has fully decayed...
// The owner is confused.
if (owner)
{