-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.cpp
More file actions
3479 lines (3067 loc) · 71.3 KB
/
Copy pathbuild.cpp
File metadata and controls
3479 lines (3067 loc) · 71.3 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: build.cpp ( POWDER Library, C++ )
*
* COMMENTS:
* build.cpp contains all the routines used to build dungeon levels.
* These are as many and varied as are the types of levels that can
* be built.
*
* The major hook is MAP::build, which is where the level layout
* is determined and used for determining which generator should
* be applied.
*/
#include <stdio.h>
#include <math.h>
#include "mygba.h"
#include "map.h"
#include "msg.h"
#include "rand.h"
#include "assert.h"
#include "creature.h"
#include "item.h"
#include "victory.h"
#include "rooms/allrooms.h"
void
MAP::clear()
{
memset(mySquares, 0, MAP_WIDTH * MAP_HEIGHT);
memset(myFlags, 0, MAP_WIDTH * MAP_HEIGHT);
//memset(myFlags, SQUAREFLAG_LIT, MAP_WIDTH * MAP_HEIGHT);
//memset(myFlags, SQUAREFLAG_MAPPED, MAP_WIDTH * MAP_HEIGHT);
MOB *mob, *mnext;
ITEM *item, *inext;
for (mob = myMobHead; mob; mob = mnext)
{
mnext = mob->getNext();
mob->setNext(0);
delete mob;
}
myMobHead = 0;
myMobCount = 0;
for (item = myItemHead; item; item = inext)
{
inext = item->getNext();
item->setNext(0);
delete item;
}
myItemHead = 0;
myItemCount = 0;
}
void
MAP::build(bool alreadysavedseed)
{
// Save our random seed used...
if (!alreadysavedseed)
mySeed = rand_getseed();
// Empty the map...
clear();
// Clear out our wall map.
memset(ourWallMap, 0, MAP_WIDTH * MAP_HEIGHT);
// Decide what sort of map to build. If the current level is 10
// we do the big room.
// Rough map:
// 0: Surface World.
// 1-5: Roguelikes.
// 6-7: Lit QIX.
// 8-9: Unlit QIX.
// 10: Big Room
// 11-14: Mazes
// 15: Cretan Minotaur
// 16-17: Lit cavern
// 18: Dark cavern
// 19-20: Dark water cavern
// 21: Dark cavern, Belweir
// 22: Quizar
// 23: Hruth
// 24: Klaskov
// 25: Circles of Hell.
if (0)
{
// Stress test all room generators.
static int ct = 0;
ct++;
switch (ct % 9)
{
case 0:
buildRoguelike();
break;
case 1:
buildDrunkenCavern(true, false, true, true);
break;
case 2:
{
static int ct2 = 0;
ct2++;
switch (ct2 % 5)
{
case 0:
// We really don't want a crash here!
buildSurfaceWorld();
break;
case 1:
buildQuest(GOD_ROGUE);
break;
case 2:
buildQuest(GOD_BARB);
break;
case 3:
buildQuest(GOD_FIGHTER);
break;
case 4:
buildTutorial();
break;
}
break;
}
case 3:
buildQix(true);
break;
case 4:
// This breaks, I eat my shoe.
buildBigRoom();
break;
case 5:
buildMaze(rand_choice(4)+1);
break;
case 6:
buildRiverRoom();
break;
case 7:
buildHello();
break;
case 8:
buildSpaceShip();
break;
}
if (!(ct & 127))
printf("Current room count %d\n", ct);
}
else if (glbStressTest && !glbMapStats)
{
// Pick a random depth.
myDepth = rand_range(0, 25);
// Pick a random branch... Currently only tridude supported.
if (!rand_choice(15))
myBranch = BRANCH_TRIDUDE;
else
myBranch = BRANCH_MAIN;
}
if (glbTutorial)
{
buildTutorial();
return;
}
if (branchName() == BRANCH_TRIDUDE)
{
// Simple enough.
buildSpaceShip();
return;
}
if (myDepth == 0)
{
buildSurfaceWorld();
}
else if (myDepth <= 5)
{
//buildQix(true);
//buildQuest(GOD_ROGUE);
buildRoguelike();
}
else if (myDepth >= 6 && myDepth <= 7)
{
buildQix(true);
}
else if (myDepth >= 8 && myDepth <= 9)
{
buildQix(false);
}
else if (myDepth == 10)
{
buildBigRoom();
}
else if (myDepth >= 11 && myDepth <= 15)
{
buildMaze(rand_choice(4)+1);
}
else if (myDepth >= 16 && myDepth <= 17)
{
buildDrunkenCavern(true, false, true, true);
}
else if (myDepth >= 18 && myDepth <= 18)
{
buildDrunkenCavern(false, false, true, true);
}
else if (myDepth >= 19 && myDepth <= 20)
{
buildDrunkenCavern(false, true, true, true);
// If depth is 20, we want hard floors to prevent cheating!
myGlobalFlags &= ~MAPFLAG_DIG;
}
else if (myDepth == 21)
{
// Belweir's library is here!
buildDrunkenCavern(false, false, true, true);
}
else if (myDepth == 22)
{
buildQuest(GOD_ROGUE);
}
else if (myDepth == 23)
{
buildQuest(GOD_BARB);
}
else if (myDepth == 24)
{
buildQuest(GOD_FIGHTER);
}
else if (myDepth == 25)
{
buildHello();
}
else
{
// This should not happen.
buildRoguelike();
}
// Build some random traps.
int numtraps, i;
numtraps = rand_choice(myDepth);
for (i = 0; i < numtraps; i++)
{
int tries = 10;
int x, y;
while (tries--)
{
if (!findRandomLoc(x, y, MOVE_WALK,
true, false, false, false, false, false))
{
tries = 0;
break;
}
if (createTrap(x, y, 0))
break;
}
// Failed to find after 10 tries? Give up on all traps!
if (!tries)
break;
}
// Verify we have appropriate up and down stairs
#ifdef USEASSERT
if (myDepth)
{
int x, y;
UT_ASSERT(findTile(SQUARE_LADDERUP, x, y));
}
if (myDepth < 25)
{
int x, y;
UT_ASSERT(findTile(SQUARE_LADDERDOWN, x, y));
}
#endif
}
void
MAP::roomToMapCoords(int &x, int &y, const ROOM &room, int rx, int ry)
{
int tmp;
if (room.rotation & ROOM_FLIP_X)
x = room.definition->size.x - rx - 1;
else
x = rx;
if (room.rotation & ROOM_FLIP_Y)
y = room.definition->size.y - ry - 1;
else
y = ry;
if (room.rotation & ROOM_FLOP)
{
tmp = x;
x = y;
y = tmp;
}
x += room.l.x;
y += room.l.y;
}
void
MAP::mapToRoomCoords(int &rx, int &ry, const ROOM &room, int x, int y)
{
int tmp;
x -= room.l.x;
y -= room.l.y;
if (room.rotation & ROOM_FLOP)
{
tmp = x;
x = y;
y = tmp;
}
if (room.rotation & ROOM_FLIP_Y)
ry = room.definition->size.y - y - 1;
else
ry = y;
if (room.rotation & ROOM_FLIP_X)
rx = room.definition->size.x - x - 1;
else
rx = x;
}
// Draws a walled room using the inclusive rectangle specified.
void
MAP::drawRoom(const ROOM &room, bool lit, bool mazetype, bool caverntype)
{
int x, y;
if (room.definition)
{
int squarenum;
int rx, ry, i;
squarenum = 0;
for (ry = 0; ry < room.definition->size.y; ry++)
{
for (rx = 0; rx < room.definition->size.x; rx++)
{
roomToMapCoords(x, y, room, rx, ry);
ourWallMap[y * MAP_WIDTH + x] = 1;
SQUARE_NAMES square;
square = room.definition->squarelist[squarenum];
if (caverntype && square == SQUARE_WALL)
square = SQUARE_EMPTY;
if (caverntype && square == SQUARE_FLOOR)
square = SQUARE_CORRIDOR;
if (mazetype && square == SQUARE_CORRIDOR)
square = SQUARE_FLOOR;
if (mazetype && square == SQUARE_EMPTY)
square = SQUARE_WALL;
setTile(x, y, square);
squarenum++;
}
}
for (i = 0; room.definition->squareflaglist[i].data != SQUAREFLAG_NONE;
i++)
{
rx = room.definition->squareflaglist[i].x;
ry = room.definition->squareflaglist[i].y;
roomToMapCoords(x, y, room, rx, ry);
setFlag(x, y, room.definition->squareflaglist[i].data);
}
return;
}
for (y = room.l.y; y <= room.h.y; y++)
{
for (x = room.l.x; x <= room.h.x; x++)
{
// Mark the entire room in the wall map.
ourWallMap[y * MAP_WIDTH + x] = 1;
if (x == room.l.x || x == room.h.x ||
y == room.l.y || y == room.h.y)
{
// Draw a wall.
setTile(x, y, caverntype ? SQUARE_EMPTY : SQUARE_WALL);
setFlag(x, y, SQUAREFLAG_LIT, lit);
}
else
{
// Draw the floor.
setTile(x, y, caverntype ? SQUARE_CORRIDOR : SQUARE_FLOOR);
setFlag(x, y, SQUAREFLAG_LIT, lit);
}
}
}
}
bool
MAP::isMapExit(int x, int y) const
{
UT_ASSERT(x >= 0 && x <= MAP_WIDTH);
UT_ASSERT(y >= 0 && y <= MAP_HEIGHT);
switch (getTile(x, y))
{
case SQUARE_DOOR:
case SQUARE_BLOCKEDDOOR:
case SQUARE_SECRETDOOR:
case SQUARE_CORRIDOR:
case SQUARE_FLOOR:
case SQUARE_OPENDOOR:
return true;
default:
break;
}
return false;
}
bool
MAP::isRoomExit(const ROOM &room, int x, int y) const
{
UT_ASSERT(x >= 0 && x < room.definition->size.x);
UT_ASSERT(y >= 0 && y < room.definition->size.y);
UT_ASSERT(room.definition != 0);
switch (room.definition->squarelist[x + y * room.definition->size.x])
{
case SQUARE_DOOR:
case SQUARE_BLOCKEDDOOR:
case SQUARE_SECRETDOOR:
case SQUARE_CORRIDOR:
case SQUARE_FLOOR:
case SQUARE_OPENDOOR:
return true;
case SQUARE_WATER:
case SQUARE_LAVA:
case SQUARE_ACID:
default:
return false;
}
}
bool
MAP::isMapPathway(int x, int y) const
{
if (x < 0 || x >= MAP_WIDTH)
return false;
if (y < 0 || y >= MAP_HEIGHT)
return false;
switch (getTile(x, y))
{
case SQUARE_CORRIDOR:
case SQUARE_FLOOR:
case SQUARE_WATER:
case SQUARE_LAVA:
case SQUARE_ACID:
return true;
default:
break;
}
return false;
}
bool
MAP::isTileWall(int tile) const
{
switch (tile)
{
case SQUARE_WALL:
case SQUARE_DOOR:
case SQUARE_BLOCKEDDOOR:
case SQUARE_SECRETDOOR:
case SQUARE_OPENDOOR:
return true;
default:
break;
}
return false;
}
bool
MAP::isMapWall(int x, int y) const
{
// The -1 for width & hieght here is to intentionally leave a 1 space
// border on the edges so we can scroll past the edges.
if (x < 0 || x >= MAP_WIDTH-1)
return true;
if (y < 0 || y >= MAP_HEIGHT-1)
return true;
if (ourWallMap[y * MAP_WIDTH + x])
return true;
return false;
}
//
// Logic for setting a tile on a path...
//
void
MAP::setPathTile(int x, int y, bool lit, bool usefloor)
{
SQUARE_NAMES oldtile, newtile;
oldtile = getTile(x, y);
newtile = oldtile; // Do nothing by default.
switch (oldtile)
{
case SQUARE_EMPTY:
if (usefloor)
newtile = SQUARE_FLOOR;
else
newtile = SQUARE_CORRIDOR;
break;
case SQUARE_FLOOR:
newtile = SQUARE_FLOOR;
break;
case SQUARE_CORRIDOR:
if (usefloor)
newtile = SQUARE_FLOOR;
else
newtile = SQUARE_CORRIDOR;
break;
case SQUARE_BLOCKEDDOOR:
// We only have two choices here. We don't want to
// turn into a corridor or normal door or we allow creatures
// to escape. Secret door is acceptable as creatures don't
// yet search by default
switch (rand_choice(6))
{
case 0:
case 1:
case 2:
case 3:
case 4:
newtile = SQUARE_BLOCKEDDOOR;
lit = true;
break;
case 5:
newtile = SQUARE_SECRETDOOR;
// Inherit existing lit flag.
lit = getFlag(x, y, SQUAREFLAG_LIT);
break;
}
break;
case SQUARE_DOOR:
case SQUARE_WALL:
case SQUARE_SECRETDOOR:
case SQUARE_OPENDOOR:
// We have several choices:
// 1) Secret door
// 2) Door
// 3) No door.
switch (rand_choice(6))
{
case 0:
case 1:
case 2:
newtile = SQUARE_DOOR;
lit = true;
break;
case 3:
newtile = SQUARE_OPENDOOR;
lit = true;
break;
case 4:
if (usefloor)
newtile = SQUARE_FLOOR;
else
newtile = SQUARE_CORRIDOR;
break;
case 5:
newtile = SQUARE_SECRETDOOR;
// Inherit existing lit flag.
lit = getFlag(x, y, SQUAREFLAG_LIT);
break;
}
break;
// Error checking:
default:
newtile = SQUARE_WATER;
lit = true;
break;
}
// Set the tile.
setTile(x, y, newtile);
setFlag(x, y, SQUAREFLAG_LIT, lit);
}
//
// This initializes ourDistanceMap with the distance to go, including
// tunnelling as allowed. Thus, if isMapWall, we can't go through it.
// If we must tunnel, distance is 2.
// We return false if there is no path (should not happen!)
//
#define STACK_SIZE 2048
bool
MAP::buildMoveDistance(int px, int py, MOVE_NAMES move)
{
s8 stackx[STACK_SIZE], stacky[STACK_SIZE];
int stackdepth = 0, stackbot = 0, stacktop = 0;
int dist, nextdist;
int dx, dy, x, y;
// Initialize to infinity.
memset(ourDistanceMap, 0xff, MAP_WIDTH * MAP_HEIGHT * sizeof(u16));
// Zero distance to self.
ourDistanceMap[py * MAP_WIDTH + px] = 0;
// Add to our distance stack...
stackx[stacktop] = px;
stacky[stacktop] = py;
stacktop++;
stackdepth++;
stacktop &= STACK_SIZE-1;
// And recurse...
while (stackdepth)
{
// Pull top guy off the stack...
stackdepth--;
px = stackx[stackbot];
py = stacky[stackbot];
stackbot++;
// Now, see if anyone will have a shorter distance if they came
// from here...
dist = ourDistanceMap[py * MAP_WIDTH + px];
FORALL_4DIR(dx, dy)
{
y = py + dy;
if (y < 0)
continue;
if (y > MAP_HEIGHT-1)
continue;
x = px + dx;
if (x < 0)
continue;
if (x > MAP_WIDTH-1)
continue;
// See if we can go there...
if (// canMove(x, y, move, true, true, true) ||
(glb_squaredefs[getTile(x, y)].movetype & move) ||
getTile(x, y) == SQUARE_SECRETDOOR ||
getTile(x, y) == SQUARE_DOOR ||
getTile(x, y) == SQUARE_BLOCKEDDOOR)
{
nextdist = 1;
nextdist += dist;
// Check if it is strictly faster...
if (nextdist < ourDistanceMap[y * MAP_WIDTH + x])
{
// Store it.
ourDistanceMap[y * MAP_WIDTH + x] = nextdist;
// Add to our stack.
stackx[stacktop] = x;
stacky[stacktop] = y;
stackdepth++;
stacktop++;
stacktop &= STACK_SIZE-1;
UT_ASSERT(stackdepth < STACK_SIZE-2);
}
}
}
}
return true;
}
bool
MAP::buildDistance(int px, int py)
{
s8 stackx[STACK_SIZE], stacky[STACK_SIZE];
int stackdepth = 0;
u16 dist, nextdist;
int dx, dy, x, y;
// Initialize to infinity.
memset(ourDistanceMap, 0xff, MAP_WIDTH * MAP_HEIGHT * sizeof(u16));
// Zero distance to self.
ourDistanceMap[py * MAP_WIDTH + py] = 0;
// Add to our distance stack...
stackx[stackdepth] = px;
stacky[stackdepth] = py;
stackdepth++;
// And recurse...
while (stackdepth)
{
// Pull top guy off the stack...
stackdepth--;
px = stackx[stackdepth];
py = stacky[stackdepth];
// Now, see if anyone will have a shorter distance if they came
// from here...
dist = ourDistanceMap[py * MAP_WIDTH + px];
for (dy = -1; dy <= 1; dy++)
{
y = py + dy;
if (y < 0)
continue;
if (y >= MAP_HEIGHT-1)
break;
for (dx = -1; dx <= 1; dx++)
{
if (!dx && !dy)
continue;
x = px + dx;
if (x < 0)
continue;
if (x >= MAP_WIDTH-1)
break;
// See if we can go there...
if (!isMapWall(x, y))
{
nextdist = (getTile(x, y) == SQUARE_EMPTY) ? 2 : 1;
//nextdist = 1;
nextdist += dist;
// Check if it is strictly faster...
if (nextdist < ourDistanceMap[y * MAP_WIDTH + x])
{
// Store it.
ourDistanceMap[y * MAP_WIDTH + x] = nextdist;
// Add to our stack.
stackx[stackdepth] = x;
stacky[stackdepth] = y;
stackdepth++;
UT_ASSERT(stackdepth < STACK_SIZE);
}
}
}
}
}
return true;
}
//
// Draw path using a distance function.
//
bool
MAP::drawPathStraight(int sx, int sy, int ex, int ey, bool lit)
{
u16 dist, dp, dn;
int d[2];
int dir;
int s[2], e[2];
int rev = 0;
buildDistance(ex, ey);
UT_ASSERT(!"Got to draw...");
// Check to see if a path is possible....
if (ourDistanceMap[sy * MAP_WIDTH + sx] == 0xffff)
{
UT_ASSERT(!"Impossibly map!");
return false;
}
s[0] = sx;
s[1] = sy;
e[0] = ex;
e[1] = ey;
// Initial random direction.
dir = rand_choice(2);
setPathTile(s[0], s[1], lit, false);
while (s[0] != e[0] || s[1] != e[1])
{
// Determine which way is down in dir....
dist = ourDistanceMap[s[1] * MAP_WIDTH + s[0]];
d[!dir] = 0;
dp = 0xffff;
dn = 0xffff;
if ((s[dir] + 1) < MAP_WIDTH)
{
d[dir] = 1;
dp = ourDistanceMap[(s[1] + d[1]) * MAP_WIDTH + s[0] + d[0]];
}
if ((s[dir] - 1) >= 0)
{
d[dir] = -1;
dn = ourDistanceMap[(s[1] + d[1]) * MAP_WIDTH + s[0] + d[0]];
}
if (dp >= dist && dn >= dist)
{
// No one has a faster distance! Change direction.
UT_ASSERT(!rev);
rev++;
dir = !dir;
continue;
}
if (dp < dist && dn < dist)
{
// Both are shorter, this is a cusp which by definition
// cannot last. Pick a random direction.
d[dir] = rand_sign();
}
else if (dp < dist)
{
d[dir] = 1;
}
else
{
UT_ASSERT(dn < dist);
// Dn is smallest.
d[dir] = -1;
}
rev = 0;
// Go in this direction until it is unprofitable...
while (s[0] != e[0] || s[1] != e[1])
{
setPathTile(s[0], s[1], lit, false);
s[0] += d[0];
s[1] += d[1];
// Check if next tile is good...
dist = ourDistanceMap[s[1] * MAP_WIDTH + s[0]];
if (s[dir] + d[dir] < 0)
break;
if (s[dir] + d[dir] >= MAP_WIDTH)
break;
if (ourDistanceMap[(s[1]+d[1]) * MAP_WIDTH + s[0] + d[0]] >= dist)
{
// No longer profitable, stop moving this way!
break;
}
}
}
setPathTile(s[0], s[1], lit, false);
return true;
}
//
// Draw a path from the given location to the given location.
// This will avoid walls.
// It will return if it failed. It shouldn't, but shit happens.
//
bool
MAP::drawPath(int sx, int sy, int ex, int ey, bool lit)
{
int d[2];
int i, dir, wallhit;
int s[2], e[2], n[2];
int len = 0;
int ncnt = 0;
s[0] = sx;
s[1] = sy;
e[0] = ex;
e[1] = ey;
// Initial random direction.
dir = rand_choice(2);
setPathTile(s[0], s[1], lit, false);
while (s[0] != e[0] || s[1] != e[1])
{
// Find our current direction to go...
for (i = 0; i < 2; i++)
d[i] = SIGN(e[i] - s[i]);
// Switch our direction.
dir = !dir;
// If we are aligned in that direction, try the opposite.
if (!d[dir])
dir = !dir;
// Move in the direction dir until a random chance fails,
// or we get aligned with a destination.
wallhit = 0;
while (1)
{
if (rand_chance(40))
{
// Stop digging!
break;
}
// Alignment... This only counts for straight moves,
// if bounced off a wall we don't want to trigger this.
if (!wallhit && s[dir] == e[dir])
break;
// Calculate the next pos...
n[0] = s[0];
n[1] = s[1];
n[dir] += d[dir];
// Check if our current dig direction is valid...
ncnt = 0;
while (isMapWall(n[0], n[1]))
{
// Not a valid dig direction! Rotate 90 degrees
// and go in a random direction...
// As we never build walls adjacent and they are square,
// this guarantees a non-wall.
// Except it doesn't. WTF?
dir = !dir;
if (!d[dir])
d[dir] = rand_sign();
n[0] = s[0];
n[1] = s[1];
n[dir] += d[dir];
wallhit = 1;
ncnt++;
if (ncnt > 100)
{
// THis fails when we hit the bottom most wall,
// turn to the left,
// and then hit the left wall prior to deciding to reset
// NOte that alignment will never trigger as wallhit
// is true, and we could have been going the wrong
// way anyways.
// Our abort here is reasonable enough.
return false;
}
}
// Move in the desired direction...
s[dir] += d[dir];
// Write out the path tile...
setPathTile(s[0], s[1], lit, false);
len++;
if (len > 100)
return false;
}
}
return true;
}
void
MAP::drawPath_old(int sx, int sy, int ex, int ey, bool lit)
{
int dx = 0, dy = 0, dir, dirlen;
dir = 0; // 1 is y, -1 is x.
while (sx != ex || sy != ey)
{
// Choose our direction...
dx = rand_sign();
if (sx < ex)
dx = 1;
else if (sx > ex)
dx = -1;
dy = rand_sign();
if (sy < ey)
dy = 1;
else if (sy > ey)
dy = -1;
// Every so often the builder is drunk...
if (0) // rand_chance(30))
{
dx *= -1;
dy *= -1;
}
if (dir != 0)
dir *= -1;
else
{
if (dx && dy)
{
dir = rand_sign();
}
else if (dx)
dir = -1;
else