-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmap.cpp
More file actions
6250 lines (5420 loc) · 126 KB
/
map.cpp
File metadata and controls
6250 lines (5420 loc) · 126 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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: map.cpp ( POWDER Library, C++ )
*
* COMMENTS:
* Routines to handle the map data structure.
*
* Note that code relevant to the creation of levels is in build.cpp.
*/
#include <stdio.h>
#include <math.h>
#include "mygba.h"
#include "map.h"
#include "msg.h"
#include "gfxengine.h"
#include "rand.h"
#include "assert.h"
#include "creature.h"
#include "item.h"
#include "itemstack.h"
#include "sramstream.h"
#include "piety.h"
#include "victory.h"
#include "ptrlist.h"
MAP *glbCurLevel = 0;
int glbMapCount = 0;
u16 *MAP::ourItemMap = 0;
u16 *MAP::ourMobMap = 0;
// Temporary map used to mark if stuff are walls or not.
u8 *MAP::ourWallMap = 0;
// Used while building our levels.
u16 *MAP::ourDistanceMap = 0;
int *MAP::ourCells = 0;
int *MAP::ourCutOuts = 0;
// Used for smelling..
u16 *MAP::ourAvatarSmell = 0;
u16 MAP::ourAvatarSmellWatermark = 64;
MOBREF *MAP::ourMobRefMap = 0;
ITEM **MAP::ourItemPtrMap = 0;
MOBREF *MAP::ourMobGuiltyMap = 0;
bool MAP::ourIsLoading = false;
s8 MAP::ourWindDX = 0;
s8 MAP::ourWindDY = 0;
int MAP::ourWindCounter = 0;
bool MAP::ourMapDirty = true;
// Marks if we have a pending level change.
MAP *MAP::ourDelayedLevelChange = 0;
u32 glbBlockingTiles[32] =
{ 0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0 };
u32 glbWalkTiles1[32] =
{ 0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0 };
u32 glbWalkTiles2[32] =
{ 0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0 };
static int
sign(int d)
{
if (d == 0)
return 0;
else if (d < 0)
return -1;
else
return 1;
}
MAP::MAP(int level, BRANCH_NAMES branch)
{
glbMapCount++;
mySquares = new u8[MAP_WIDTH * MAP_HEIGHT];
myFlags = new u8[MAP_WIDTH * MAP_HEIGHT];
// Default abilities for all levels.
myGlobalFlags = 0;
myGlobalFlags |= MAPFLAG_DIG;
myGlobalFlags |= MAPFLAG_NEWMOBS;
if (!ourItemMap)
ourItemMap = new u16[MAP_WIDTH * MAP_HEIGHT];
if (!ourMobMap)
ourMobMap = new u16[MAP_WIDTH * MAP_HEIGHT];
if (!ourWallMap)
ourWallMap = new u8[MAP_WIDTH * MAP_HEIGHT];
if (!ourDistanceMap)
ourDistanceMap = new u16[MAP_WIDTH * MAP_HEIGHT];
if (!ourCells)
ourCells = new int[1024];
if (!ourCutOuts)
ourCutOuts = new int[512];
if (!ourAvatarSmell)
ourAvatarSmell = new u16[MAP_WIDTH * MAP_HEIGHT];
if (!ourMobRefMap)
ourMobRefMap = new MOBREF[MAP_WIDTH * MAP_HEIGHT];
if (!ourItemPtrMap)
ourItemPtrMap = new ITEM*[MAP_WIDTH * MAP_HEIGHT];
if (!ourMobGuiltyMap)
ourMobGuiltyMap = new MOBREF[MAP_WIDTH * MAP_HEIGHT];
myMobCount = 0;
myItemCount = 0;
myMapDown = 0;
myMapUp = 0;
myMapBranch = 0;
myItemHead = 0;
myMobHead = 0;
myMobSafeNext = 0;
mySignList = 0;
clear();
myDepth = level;
myBranch = branch;
myFOVX = myFOVY = -1;
}
MAP::~MAP()
{
delete [] mySquares;
delete [] myFlags;
delete mySignList;
glbMapCount--;
}
void
MAP::deleteAllMaps()
{
MOB *mob;
ITEM *item;
// Clear out our mob refs.
if (this == glbCurLevel)
{
int x, y;
for (y = 0; y < MAP_HEIGHT; y++)
for (x = 0; x < MAP_WIDTH; x++)
{
ourMobRefMap[(y) * MAP_WIDTH + x].setMob(0);
ourMobGuiltyMap[(y) * MAP_WIDTH + x].setMob(0);
}
}
for (mob = getMobHead(); mob; mob = getMobHead())
{
myMobHead = mob->getNext();
mob->setNext(0);
delete mob;
}
for (item = getItemHead(); item; item = getItemHead())
{
myItemHead = item->getNext();
item->setNext(0);
delete item;
}
if (myMapUp)
{
myMapUp->myMapDown = 0;
myMapUp->deleteAllMaps();
}
if (myMapDown)
{
myMapDown->myMapUp = 0;
myMapDown->deleteAllMaps();
}
if (myMapBranch)
{
// Break the branch.
myMapBranch->myMapBranch = 0;
myMapBranch->deleteAllMaps();
}
myMapUp = myMapDown = myMapBranch = 0;
delete this;
changeCurrentLevel(0);
}
bool
MAP::findPath(int sx, int sy,
int ex, int ey,
MOVE_NAMES movetype,
bool allowmob,
bool allowdoor,
int &dx, int &dy)
{
int x, y, iterations;
u32 mask, val, endmask, diff;
u32 *curtile, *lasttile, *tmp;
UT_ASSERT(sx >= 0 && sx < 32);
UT_ASSERT(sy >= 0 && sy < 32);
UT_ASSERT(ex >= 0 && ex < 32);
UT_ASSERT(ey >= 0 && ey < 32);
if (sx == ex && sy == ey)
{
// Already there, trivial.
dx = dy = 0;
return true;
}
// Initialize the blocking tiles.
for (y = 0; y < 32; y++)
{
val = 0;
mask = 1;
for (x = 0; x < 32; x++)
{
if (canMove(x, y, movetype, allowmob, allowdoor))
val |= mask;
mask <<= 1;
}
glbBlockingTiles[y] = val;
}
// Initialize the current array to the destination location.
memset(glbWalkTiles1, 0, sizeof(u32) * 32);
glbWalkTiles1[ey] = (1 << ex);
// Now, walk until we get a match...
curtile = glbWalkTiles1;
lasttile = glbWalkTiles2;
endmask = (1 << sx);
// We explicitly allow walking to the end square. If allowmob
// is false, for example, we may falsely report we can't get to the
// mob.
glbBlockingTiles[sy] |= endmask;
iterations = 0;
while (1)
{
// Write from curtile into lasttile the effect of one step.
for (y = 0; y < 32; y++)
{
// Include effects of stepping left and right.
lasttile[y] = curtile[y] | (curtile[y] << 1) | (curtile[y] >> 1);
}
// Now, merge in vertical steps.
for (y = 1; y < 31; y++)
{
lasttile[y] |= curtile[y-1] | curtile[y+1];
}
// Special cases.
lasttile[0] |= curtile[1];
lasttile[31] |= curtile[30];
// Now, clear out the relevant mask. At the same time,
// we track if we have any differences. If there are no
// differences, we can't reach our destination.
diff = 0;
for (y = 0; y < 32; y++)
{
lasttile[y] &= glbBlockingTiles[y];
diff |= lasttile[y] ^ curtile[y];
}
if (!diff)
{
#if 0
BUF buf;
buf.sprintf(buf, "Same effect at %d. ", iterations);
msg_report(buf);
#endif
return false;
}
// Swap last & cur.
tmp = curtile;
curtile = lasttile;
lasttile = tmp;
// If the curtile now has the end bit set, we are done.
if (curtile[sy] & endmask)
break;
iterations++;
}
if (0)
{
BUF buf;
buf.sprintf("Found end at %d. ", iterations);
msg_report(buf);
}
// We have successfully built a path to the start. To determine
// the best path, we check the previous step. All the neighbouring
// squares of the previous step are, by definition, closer. Further,
// at least one must be set.
int numdir, dir, dirlist[4];
numdir = 0;
for (dir = 0; dir < 4; dir++)
{
getDirection(dir, dx, dy);
x = sx + dx;
y = sy + dy;
if (x >= 0 && x < 32 && y >= 0 && y < 32)
{
if (lasttile[y] & (1 << x))
{
dirlist[numdir++] = dir;
}
}
}
if (!numdir)
{
UT_ASSERT(!"No direction found?");
return false;
}
// Pick a direction at random...
dir = rand_choice(numdir);
dir = dirlist[dir];
getDirection(dir, dx, dy);
return true;
}
void
MAP::updateAvatarSmell(int x, int y, int smell)
{
int cursmell;
UT_ASSERT(x >= 0 && x < MAP_WIDTH);
UT_ASSERT(y >= 0 && y < MAP_HEIGHT);
// Move to top of current watermark.
smell += ourAvatarSmellWatermark;
// Get current smell at this square.
cursmell = ourAvatarSmell[y * MAP_WIDTH + x];
// Update if better...
if (cursmell < smell || cursmell > ourAvatarSmellWatermark)
ourAvatarSmell[y * MAP_WIDTH + x] = smell;
}
int
MAP::getAvatarSmell(int x, int y) const
{
u16 usmell; // You smell
int ismell; // I smell
// char wiismell; // We smell.
usmell = ourAvatarSmell[y * MAP_WIDTH + x];
// If we are farther than the watermark, it is zero.
if (usmell > ourAvatarSmellWatermark)
return 0;
// Find negative smell. Lower is less smelly, but is centered
// with 0 being closest.
ismell = (int) usmell - (int)ourAvatarSmellWatermark;
// Normalize.
ismell += 65535;
return ismell;
}
bool
MAP::getAvatarSmellGradient(MOB *mob, int x, int y, int &dx, int &dy) const
{
int maxsmell = 0, smell;
int ddx, ddy;
FORALL_8DIR(ddx, ddy)
{
if (mob && mob->canMove(x+ddx, y+ddy, true))
{
// Possible smelly square to move to.
smell = getAvatarSmell(x+ddx, y+ddy);
if (smell > maxsmell)
{
dx = ddx;
dy = ddy;
maxsmell = smell;
}
}
}
// See if we got a good direction...
if (maxsmell)
return true;
return false;
}
void
MAP::populate()
{
int desiredcount;
MOB *mob;
int x, y;
int fails;
int i, n;
desiredcount = getDesiredMobCount();
// desiredcount = 100;
fails = 0;
while (myMobCount < desiredcount && (fails < 20))
{
// No worry about FOV here.
if (!createAndPlaceNPC(false))
fails++;
}
// Create a dimensional portal on the correct level
if (myDepth == 20 && branchName() == BRANCH_MAIN)
{
int tries = 20;
while (tries--)
{
if (findRandomLoc(x, y, MOVE_STD_SWIM, true, false, false, false, false, false))
{
// Make sure this isn't an invulnerable square.
SQUARE_NAMES square;
square = getTile(x, y);
if (!glb_squaredefs[square].invulnerable)
{
setTile(x, y, SQUARE_DIMDOOR);
// Dimensional door glows!
setFlag(x, y, SQUAREFLAG_LIT);
break;
}
}
}
UT_ASSERT(tries);
}
// Check if we are a special level..
if (myDepth == 25 && branchName() == BRANCH_MAIN)
{
// Add the daemon...
mob = MOB::create(MOB_BAEZLBUB);
if (findRandomLoc(x, y, mob->getMoveType(), false,
mob->getSize() >= SIZE_GARGANTUAN,
false, false, true, true))
{
if (!mob->move(x, y, true))
{
UT_ASSERT(!"COOL MOB INSTA KILLED");
}
else
{
registerMob(mob);
// We want this to be a global broadcast.
msg_report("You sense your doom approaching! ");
}
}
else
{
UT_ASSERT(!"Couldn't place cool mob!");
delete mob;
}
}
// Populate the floor of the dungeon.
n = 10 + rand_choice(myDepth);
// Do not put anything on the wilderness map.
if (!allowItemGeneration())
n = 0;
for (i = 0; i < n; i++)
{
ITEM *item;
// Don't generate items in nomob locations such as star fields.
if (findRandomLoc(x, y, MOVE_WALK, true, false, false, false, false, true))
{
item = ITEM::createRandom();
#if 0
item = ITEM::create(ITEM_BOULDER);
#endif
acquireItem(item, x, y, 0);
}
}
}
bool
MAP::createAndPlaceNPC(bool avoidfov)
{
MOB *mob;
int x, y;
bool avoidfire = true, avoidacid = true;
mob = MOB::createNPC(getThreatLevel());
if (rand_chance(1))
mob->makeUnique();
if (mob->hasIntrinsic(INTRINSIC_RESISTFIRE) &&
!mob->hasIntrinsic(INTRINSIC_VULNFIRE))
avoidfire = false;
if ((mob->getMoveType() & MOVE_FLY))
avoidacid = false;
if (mob->hasIntrinsic(INTRINSIC_RESISTACID) &&
!mob->hasIntrinsic(INTRINSIC_VULNACID))
avoidacid = false;
// We don't want the new dude to show up in the FOV.
if (findRandomLoc(x, y, mob->getMoveType(), false,
mob->getSize() >= SIZE_GARGANTUAN,
avoidfov, avoidfire, avoidacid, true))
{
// Do an interlevel move to skip trap checking.
// Still check for move death.
if (mob->move(x, y, true))
{
registerMob(mob);
return true;
}
}
// Failed to place the mob, ignore.
delete mob;
return false;
}
void
MAP::refresh(bool preserve_ray)
{
int x, y, tile, mobtile;
MOB *mob;
ITEM *item;
bool avatarblind = false;
bool avatarinvis = false;
bool avatarinpit = false;
bool avatarintree = false;
bool avatarsubmerged = false;
bool forcelit;
memset(ourItemMap, TILE_VOID, sizeof(u16) * MAP_HEIGHT * MAP_WIDTH);
memset(ourMobMap, TILE_VOID, sizeof(u16) * MAP_HEIGHT * MAP_WIDTH);
// Cycle through all MOBs...
MOB *avatar = MOB::getAvatar();
int ax = 0, ay = 0;
if (avatar)
{
avatarblind = avatar->hasIntrinsic(INTRINSIC_BLIND);
avatarinpit = avatar->hasIntrinsic(INTRINSIC_INPIT);
avatarintree = avatar->hasIntrinsic(INTRINSIC_INTREE);
avatarsubmerged = avatar->hasIntrinsic(INTRINSIC_SUBMERGED);
ax = avatar->getX();
ay = avatar->getY();
}
// Cycle through all items...
for (item = getItemHead(); item; item = item->getNext())
{
int ix, iy;
ix = item->getX();
iy = item->getY();
// Items that are below grade (in pits, or under water)
// an only be seen by avatars that are below grade,
// in pits or underwater.
if (item->isBelowGrade())
{
if (avatar)
{
// One's in a pit are only seen by avatars on that
// square.
if (avatarinpit)
{
if (ix != ax || iy != ay)
{
// Can't see, not in our own pit.
continue;
}
}
// If the avatar is underwater, can see all below
// grade stuff (if it has LOS, of course :>)
else if (!avatarsubmerged)
{
// Not in a pit, not underwater.
continue;
}
}
}
else
{
if (avatarinpit || avatarsubmerged)
{
// Can't see anything above ground if we are
// underground.
continue;
}
// Can't see anything under tree canopy when in trees.
if (avatarintree && glb_squaredefs[getTile(ix,iy)].isforest)
continue;
}
// Figure out rejection criteria from expensive to cheap...
if (item->isMapped())
{
// Trivially true.
ourItemMap[iy * MAP_WIDTH + ix] = item->getTile();
continue;
}
if (!getFlag(ix, iy, SQUAREFLAG_MAPPED))
{
// Trivially false...
continue;
}
// Now, check if ix, iy is visible to us. Our square is always
// visible, even if blind. Otherwise, if we can see, we use
// LOS.
if (ix == ax && iy == ay)
{
item->markMapped();
ourItemMap[iy * MAP_WIDTH + ix] = item->getTile();
continue;
}
// Check if avatar is blind, that is trivial no see.
if (avatarblind)
continue;
// Check if square is lit. If not, can't see.
// We can always feel around to neighbouring squares if it
// is merely dark.
forcelit = false;
if ((ix - ax >= -1) && (ix - ax <= 1) &&
(iy - ay >= -1) && (iy - ay <= 1))
forcelit = true;
if (!forcelit && !isLit(ix, iy))
continue;
// Finally, check LOS. Have LOS, can see.
UT_ASSERT(ix >= 0 && ix < MAP_WIDTH);
UT_ASSERT(iy >= 0 && iy < MAP_HEIGHT);
if (hasLOS(ax, ay, ix, iy))
{
item->markMapped();
ourItemMap[iy * MAP_WIDTH + ix] = item->getTile();
}
}
for (mob = getMobHead(); mob; mob = mob->getNext())
{
if (avatar ? avatar->canSense(mob)
: true)
{
if (avatar && avatar->getSenseType(mob) == SENSE_WARN)
{
// MOB tile gets changed to a ?
ourMobMap[mob->getY() * MAP_WIDTH + mob->getX()] =
TILE_UNKNOWN;
if (mob->getSize() >= SIZE_GARGANTUAN
&& mob->getX() < MAP_WIDTH - 1
&& mob->getY() < MAP_HEIGHT - 1)
{
ourMobMap[(mob->getY()+1) * MAP_WIDTH + mob->getX()] =
TILE_UNKNOWN;
ourMobMap[(mob->getY()+1) * MAP_WIDTH + mob->getX()+1] =
TILE_UNKNOWN;
ourMobMap[mob->getY() * MAP_WIDTH + mob->getX()+1] =
TILE_UNKNOWN;
}
}
else
{
ourMobMap[mob->getY() * MAP_WIDTH + mob->getX()] =
mob->getTile();
if (mob->getSize() >= SIZE_GARGANTUAN
&& mob->getX() < MAP_WIDTH - 1
&& mob->getY() < MAP_HEIGHT - 1)
{
ourMobMap[(mob->getY()+1) * MAP_WIDTH + mob->getX()] =
mob->getTileLL();
ourMobMap[(mob->getY()+1) * MAP_WIDTH + mob->getX()+1] =
mob->getTileLR();
ourMobMap[mob->getY() * MAP_WIDTH + mob->getX()+1] =
mob->getTileUR();
}
}
}
else if (mob->isAvatar())
{
// Can't see oneself!
avatarinvis = true;
}
}
for (y = 0; y < MAP_HEIGHT; y++)
{
for (x = 0; x < MAP_WIDTH; x++)
{
if (getFlag(x, y, SQUAREFLAG_MAPPED))
tile = glb_squaredefs[mySquares[y * MAP_WIDTH + x]].tile;
else
tile = TILE_VOID;
mobtile = TILE_VOID;
if (ourItemMap[y * MAP_WIDTH + x] != TILE_VOID)
mobtile = ourItemMap[y * MAP_WIDTH + x];
if (ourMobMap[y * MAP_WIDTH + x] != TILE_VOID)
mobtile = ourMobMap[y * MAP_WIDTH + x];
// Clear out underneath the mob if opaque tiles are on.
if (mobtile != TILE_VOID && glbOpaqueTiles)
tile = TILE_VOID;
gfx_settile(x, y, tile);
gfx_setmoblayer(x, y, mobtile);
// See if we force the tile as lit due to avatar being
// dead or this being next to avatar.
forcelit = false;
if (!avatar)
forcelit = true;
else if (!avatarblind)
{
int diffx, diffy;
diffx = ax - x;
diffy = ay - y;
if (diffx >= -1 && diffx <= 1 &&
diffy >= -1 && diffy <= 1)
{
forcelit = true;
}
}
// If the avatar is blind, everything is shadow.
if (!avatarblind && hasFOV(x, y) && (forcelit || isLit(x, y)))
{
// It isn't shadow. It might be a smokey square though.
if (getFlag(x, y, SQUAREFLAG_SMOKE))
{
SMOKE_NAMES smoke;
smoke = getSmoke(x, y);
tile = glb_smokedefs[smoke].tile;
}
else
tile = TILE_VOID;
}
else
tile = TILE_SHADOW;
// Exception: If this is a mob, we want to reveal how
// we saw the mob.
// We have to apply this to all mobs. If a mob is in a lit
// area but we only sense with hearing, we want to put
// the hearing overlay on.
if ((tile == TILE_SHADOW || tile == TILE_VOID) &&
ourMobMap[y * MAP_WIDTH + x] != TILE_VOID)
{
if (avatar)
{
switch (avatar->getSenseType(getMob(x, y)))
{
case SENSE_SIGHT:
{
MOB *mob = getMob(x, y);
tile = TILE_VOID;
if (mob)
{
// If the creature is burning, draw special
if (mob->hasIntrinsic(INTRINSIC_AFLAME))
{
tile = TILE_AFLAME;
}
else if (mob->hasIntrinsic(INTRINSIC_TAME) &&
mob->isSlave(MOB::getAvatar()))
{
tile = TILE_TAMEMOB;
}
else if (mob->hasIntrinsic(INTRINSIC_UNIQUE))
{
tile = TILE_UNIQUEMOB;
}
else if (mob->hasIntrinsic(INTRINSIC_ASLEEP))
{
tile = TILE_ASLEEP;
}
}
break;
}
case SENSE_NONE:
// Should not happen.
// Maybe black tile?
tile = TILE_SHADOW;
break;
case SENSE_HEAR:
tile = TILE_SHADOW_HEAR;
break;
case SENSE_WARN:
tile = TILE_SHADOW_ESP;
break;
case SENSE_ESP:
tile = TILE_SHADOW_ESP;
break;
case NUM_SENSES:
UT_ASSERT(!"Invalid sense!");
break;
}
}
}
// Exception: If this is the avatar's location, and the
// avatar isn't displayed due to being invisible, draw
// the invisible avatar overlay tile.
if (avatarinvis)
{
if (x == ax && y == ay)
{
tile = TILE_INVISIBLEAVATAR;
}
}
// Exception: If there is no mob here, but there is
// an item which is an artifact, flag as such.
if (tile == TILE_VOID && mobtile != TILE_VOID &&
ourMobMap[y * MAP_WIDTH + x] == TILE_VOID)
{
// Returns topmost item quickly.
item = getItem(x, y);
if (item && item->isArtifact())
{
// Okay, a bit misnamed now.
tile = TILE_UNIQUEMOB;
}
}
// If preserve ray is turned on, we check the current
// overlay tile and see if we have manually updated
// it into a ray tile. If so, we don't want to
// overwrite it.
// Alternatively, we may want to preserve anything that
// is *not* one of the standard shadow tiles?
if (preserve_ray)
{
int raytile;
raytile = gfx_getoverlay(x, y);
switch (raytile)
{
case TILE_RAYSLASH:
case TILE_RAYPIPE:
case TILE_RAYDASH:
case TILE_RAYBACKSLASH:
// Preseve the tile
tile = raytile;
break;
default:
// Leave tile unchanged.
break;
}
}
gfx_setoverlay(x, y, tile);
}
}
}
MOB *
MAP::getGuiltyMob(int x, int y) const
{
if (this != glbCurLevel)
return 0;
return ourMobGuiltyMap[y*MAP_WIDTH+x].getMob();
}
void
MAP::setGuiltyMob(int x, int y, MOB *mob)
{
if (this != glbCurLevel)
return;
ourMobGuiltyMap[y*MAP_WIDTH+x].setMob(mob);
}
void
MAP::moveMob(MOB *mob, int ox, int oy, int nx, int ny)
{
int dx, dy;
// Note that the mob may not be in its old position, it may
// have already been overwritten.
// Note also that the old position may be invalid.
if (this != glbCurLevel)
return;
if (ox >= 0 && ox < MAP_WIDTH &&
oy >= 0 && oy < MAP_HEIGHT)
{
// Clear old spot.
if (ourMobRefMap[oy * MAP_WIDTH + ox].getMob() == mob)
ourMobRefMap[oy * MAP_WIDTH + ox].setMob(0);
if (mob->getSize() >= SIZE_GARGANTUAN)
{
for (dx = 0; dx < 2; dx++)
{
for (dy = 0; dy < 2; dy++)
{
if (ourMobRefMap[(oy + dy) * MAP_WIDTH + (ox+dx)].getMob() == mob)
ourMobRefMap[(oy + dy) * MAP_WIDTH + (ox+dx)].setMob(0);
}
}
}
}
if (nx >= 0 && nx < MAP_WIDTH &&
ny >= 0 && ny < MAP_HEIGHT)
{
// Mark new spot.
ourMobRefMap[ny * MAP_WIDTH + nx].setMob(mob);
if (mob->getSize() >= SIZE_GARGANTUAN)
{
for (dx = 0; dx < 2; dx++)
{
for (dy = 0; dy < 2; dy++)
{
ourMobRefMap[(ny + dy) * MAP_WIDTH + (nx+dx)].setMob(mob);
}
}
}
}
}