-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomap.c
1076 lines (864 loc) · 25 KB
/
automap.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
/* automap.c: main automapping code
Copyright (C) 1999 Evin Robertson
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
The author can be reached at nitfol@deja.com
*/
#include "nitfol.h"
#ifdef DEBUGGING
struct dirinfo {
const char *name;
int deltax, deltay;
char symbol;
char oneway;
};
static const struct dirinfo dirways[] = {
{ "n", 0, -1, '|', '^' },
{ "s", 0, 1, '|', 'v' },
{ "w", -1, 0, '-', '<' },
{ "e", 1, 0, '-', '>' },
{ "nw", -1, -1, '\\', '^' },
{ "se", 1, 1, '\\', 'v' },
{ "ne", 1, -1, '/', '^' },
{ "sw", -1, 1, '/', 'v' },
{ "up", 0, 0, 0, 0 },
{ "down", 0, 0, 0, 0 },
{ "wait", 0, 0, 0, 0 }
};
#define NUM_EXITS (sizeof(dirways) / sizeof(*dirways))
#define REVERSE_DIR(dir) (dir ^ 1)
#define NUM_DIRS (NUM_EXITS - 3)
#define NUM_WALK (NUM_EXITS - 1)
#define DIR_UP (NUM_DIRS + 0)
#define DIR_DOWN (NUM_DIRS + 1)
#define DIR_WAIT (NUM_DIRS + 2)
char *roomsymbol = NULL;
#define ROOM_SYMBOL(is_player, is_up, is_down, is_real) (is_real ? roomsymbol[(is_up != 0) | ((is_down != 0) << 1) | ((is_player != 0) << 2)] : roomsymbol[8])
typedef struct edge edge;
typedef struct loc_node loc_node;
struct edge {
loc_node *dest[2]; /* Two endpoints of passage */
BOOL is_oneway; /* Oneway passages are always created dest[0]--->dest[1] */
BOOL touched;
int min_length;
int guess_length;
};
struct loc_node {
zword number;
BOOL found, real, touched;
edge *outgoing[NUM_DIRS]; /* Drawn map connections */
loc_node *exits[NUM_EXITS]; /* Actual connections */
glui32 dist; /* For automap_find_path */
};
typedef struct edgelist edgelist;
struct edgelist {
edgelist *next;
edge *node;
};
static edgelist *all_edges;
static edge *automap_new_edge(loc_node *src, loc_node *dest, BOOL is_oneway)
{
edgelist newedge;
newedge.node = n_malloc(sizeof(edge));
newedge.node->dest[0] = src;
newedge.node->dest[1] = dest;
newedge.node->is_oneway = is_oneway;
newedge.node->touched = FALSE;
newedge.node->min_length = is_oneway ? 4 : 2;
newedge.node->guess_length = is_oneway ? 4 : 2;
LEadd(all_edges, newedge);
return newedge.node;
}
static void automap_remove_edge(edge *e)
{
unsigned n, i;
edgelist *p, *t;
if(e == NULL)
return;
for(n = 0; n < 2; n++) {
loc_node *thisdest = e->dest[n];
if(thisdest)
for(i = 0; i < NUM_DIRS; i++) {
if(thisdest->outgoing[i] == e)
thisdest->outgoing[i] = NULL;
}
}
LEsearchremove(all_edges, p, t, p->node == e, n_free(p->node));
}
static void automap_edges_untouch(void)
{
edgelist *p;
for(p = all_edges; p; p=p->next) {
p->node->touched = FALSE;
}
}
static void automap_edges_mindist(void)
{
edgelist *p;
for(p = all_edges; p; p=p->next) {
int len = p->node->is_oneway ? 4 : 2;
p->node->min_length = p->node->guess_length = len;
}
}
static hash_table rooms;
static char *loc_exp;
static zwinid automap_win;
void automap_kill(void)
{
mymap_kill();
n_free(loc_exp);
loc_exp = NULL;
LEdestruct(all_edges, n_free(all_edges->node));
n_hash_free_table(&rooms, n_free);
z_kill_window(automap_win);
}
BOOL automap_init(int numobj, const char *location_exp)
{
automap_kill();
if(!roomsymbol)
roomsymbol = n_strdup("*udb@UDB+");
if(location_exp)
loc_exp = n_strdup(location_exp);
n_hash_construct_table(&rooms, numobj / 2);
automap_win = z_split_screen(wintype_TextGrid,
automap_split | winmethod_Fixed,
automap_draw_callback, automap_mouse_callback);
return TRUE;
}
static loc_node *room_find(glui32 location, BOOL is_real)
{
const char *preface = is_real ? "" : "fake";
const char *key = n_static_number(preface, location);
return (loc_node *) n_hash_lookup(key, &rooms);
}
static loc_node *room_find_or_create(glui32 location, BOOL is_real)
{
loc_node *r;
const char *preface = is_real ? "" : "fake";
const char *key = n_static_number(preface, location);
r = (loc_node *) n_hash_lookup(key, &rooms);
if(r == NULL) {
unsigned n;
r = (loc_node *) n_malloc(sizeof(loc_node));
r->number = location;
r->found = FALSE;
r->real = is_real;
r->touched = FALSE;
for(n = 0; n < NUM_EXITS; n++) {
r->exits[n] = NULL;
if(n < NUM_DIRS)
r->outgoing[n] = NULL;
}
n_hash_insert(key, r, &rooms);
}
return r;
}
static void room_remove(loc_node *room)
{
unsigned n;
if(room) {
const char *preface = room->real ? "" : "fake";
for(n = 0; n < NUM_DIRS; n++)
automap_remove_edge(room->outgoing[n]);
n_free(n_hash_del(n_static_number(preface, room->number), &rooms));
}
}
typedef struct automap_path automap_path;
struct automap_path {
automap_path *next;
loc_node *loc; /* A location */
int dir; /* And the direction we're going from it */
};
typedef struct interlist interlist;
struct interlist {
interlist *next;
loc_node *a, *b;
};
static BOOL mymap_plot(int x, int y, char symbol, loc_node *node);
static edge *automap_get_edge(loc_node *location, int dir);
static void automap_calc_location(loc_node *location, loc_node *last,
int x, int y);
static automap_path *automap_find_path(loc_node *location, loc_node *dest,
BOOL by_walking);
static int automap_edge_oneway(loc_node *location, int dir);
static loc_node *automap_edge_follow(loc_node *location, int dir);
static interlist *interferences = NULL;
static void automap_forget_interference(void)
{
LEdestroy(interferences);
}
static void automap_remember_interference(loc_node *a, loc_node *b)
{
/* interlist *p;
LEsearch(interferences, p, (p->a==a && p->b==b) || (p->a==b && p->b==a));
if(!p) {*/
interlist newnode;
newnode.a = a;
newnode.b = b;
LEadd(interferences, newnode);
/* }*/
}
static int automap_find_and_count_interference(loc_node *center)
{
interlist *i;
int count;
automap_cycles_fill_values();
automap_forget_interference();
mymap_reinit();
n_hash_enumerate(&rooms, make_untouched);
automap_edges_untouch();
automap_calc_location(center, NULL, 0, 0);
count = 0;
for(i = interferences; i; i=i->next)
count++;
return count;
}
/* Returns TRUE if it improved any */
static BOOL automap_increase_along_path(automap_path *path, int oldcount,
loc_node *center, int effort)
{
automap_path *p;
int exploring;
int explore_max = effort > 1;
if(!effort)
return FALSE;
/* Takes two passes at trying to improve the situation.
The first time (!exploring), it tries increasing the length of each
edge along the path, observing the results and then undoing the increase.
If it was able to improve anything, it returns with the best improvement.
Otherwise it tries increasing the length of each edge and calling itself;
If its child is able to improve things, then it returns with both
lengthenings in effect. */
for(exploring = 0; exploring <= explore_max; exploring++) {
edge *best_edge = NULL;
int best_count = oldcount;
int smallest_new = 10000;
for(p = path; p; p=p->next) {
int newcount;
edge *e = automap_get_edge(p->loc, p->dir);
int old_min_length = e->min_length;
int old_guess_length = e->guess_length;
if(p->next && p->next->loc != automap_edge_follow(p->loc, p->dir))
n_show_error(E_SYSTEM, "path doesn't follow itself", 0);
e->guess_length += 2;
e->min_length = e->guess_length;
if(!exploring) {
newcount = automap_find_and_count_interference(center);
if(newcount < best_count
|| (newcount == best_count && newcount < oldcount
&& e->min_length < smallest_new)) {
best_edge = e;
best_count = newcount;
smallest_new = e->min_length;
}
} else {
if(automap_increase_along_path(p, oldcount, center, effort-1))
return TRUE;
}
e->min_length = old_min_length;
e->guess_length = old_guess_length;
}
if(!exploring && best_edge) {
best_edge->guess_length += 2;
best_edge->min_length = best_edge->guess_length;
automap_find_and_count_interference(center);
return TRUE;
}
}
return FALSE;
}
/* Returns true if all interferences have been resolved */
static BOOL automap_resolve_interference(loc_node *center, int effort)
{
int skip_interferences = 0;
int n;
while(interferences) {
interlist *oldinter = interferences;
interlist *i;
automap_path *path;
int oldcount;
oldcount = 0;
for(i = oldinter; i; i=i->next)
oldcount++;
if(skip_interferences >= oldcount)
return FALSE;
i = oldinter;
for(n = 0; n < skip_interferences; n++)
i=i->next;
path = automap_find_path(i->a, i->b, FALSE);
if(!path)
return FALSE;
interferences = NULL;
if(!automap_increase_along_path(path, oldcount, center, effort))
skip_interferences++;
LEdestroy(oldinter);
LEdestroy(path);
}
return TRUE;
}
static void automap_set_virtual_connection(loc_node *location, int d,
loc_node *dest, BOOL is_oneway)
{
if(location->outgoing[d]) {
int way = automap_edge_oneway(location, d);
if(dest || way != 2)
automap_remove_edge(location->outgoing[d]);
}
if(dest) {
edge *p = automap_new_edge(location, dest, is_oneway);
location->outgoing[d] = p;
if(dest->outgoing[REVERSE_DIR(d)])
automap_remove_edge(dest->outgoing[REVERSE_DIR(d)]);
dest->outgoing[REVERSE_DIR(d)] = p;
}
}
static void automap_set_connection(int location, int d, int dest, BOOL is_real)
{
loc_node *r, *t;
r = room_find_or_create(location, is_real);
t = room_find_or_create(dest, is_real);
if(t == r)
t = NULL;
r->exits[d] = t;
}
static edge *automap_get_edge(loc_node *location, int dir)
{
return location->outgoing[dir];
}
static loc_node *automap_edge_follow(loc_node *location, int dir)
{
if(location->outgoing[dir] == NULL)
return NULL;
if(location->outgoing[dir]->dest[0] == location)
return location->outgoing[dir]->dest[1];
if(location->outgoing[dir]->dest[1] == location)
return location->outgoing[dir]->dest[0];
n_show_error(E_SYSTEM, "edge isn't connected to what it should be", 0);
return NULL;
}
static int automap_edge_length(loc_node *location, int dir)
{
return location->outgoing[dir]->guess_length;
}
/* Returns 0 if not oneway, 1 if oneway in the given direction, and 2 if
oneway in the other direction */
static int automap_edge_oneway(loc_node *location, int dir)
{
if(location->outgoing[dir] == NULL)
return 0;
if(location->outgoing[dir]->dest[0] == location)
return location->outgoing[dir]->is_oneway;
return (location->outgoing[dir]->is_oneway) << 1;
}
static BOOL automap_draw_edge(loc_node *location, int dir, int *x, int *y)
{
int deltax, deltay;
int s;
int len;
int oneway;
edge *e = automap_get_edge(location, dir);
if(e->touched)
return TRUE;
e->touched = TRUE;
deltax = dirways[dir].deltax;
deltay = dirways[dir].deltay;
len = automap_edge_length(location, dir);
oneway = automap_edge_oneway(location, dir);
*x += deltax;
*y += deltay;
if(oneway)
len--;
if(oneway == 2) {
mymap_plot(*x, *y, dirways[REVERSE_DIR(dir)].oneway, location);
*x += deltax;
*y += deltay;
}
for(s = 1; s < len; s++) {
mymap_plot(*x, *y, dirways[dir].symbol, location);
*x += deltax;
*y += deltay;
}
if(oneway == 1) {
mymap_plot(*x, *y, dirways[dir].oneway, location);
*x += deltax;
*y += deltay;
}
return TRUE;
}
static void automap_adjust_length(loc_node *location, int dir, int newlen)
{
location->outgoing[dir]->min_length = newlen;
}
static int mapwidth;
static int mapheight;
static char *mymap = NULL;
static loc_node **mymapnode = NULL;
static char mymap_read(int x, int y)
{
x += mapwidth / 2; y += mapheight / 2;
if(x < 0 || x >= mapwidth || y < 0 || y >= mapheight)
return ' ';
return mymap[x + y * mapheight];
}
static BOOL mymap_plot(int x, int y, char symbol, loc_node *node)
{
BOOL status = TRUE;
char *dest;
x += mapwidth / 2; y += mapheight / 2;
if(x < 0 || x >= mapwidth || y < 0 || y >= mapheight)
return status;
dest = &mymap[x + y * mapwidth];
if(*dest != ' ') {
if((*dest=='/' && symbol=='\\') || (*dest=='\\' && symbol=='/'))
symbol = 'X';
else if((*dest=='-' && symbol=='|') || (*dest=='|' && symbol=='-'))
symbol = '+';
else
status = FALSE;
} else {
if(mymapnode[x + y * mapwidth])
status = FALSE;
}
if(status) {
*dest = symbol;
mymapnode[x + y * mapwidth] = node;
} else {
loc_node *interfere = mymapnode[x + y * mapwidth];
automap_remember_interference(node, interfere);
}
return status;
}
void mymap_init(int width, int height)
{
int i;
int max;
mapwidth = width * 2;
mapheight = height * 2;
max = mapwidth * mapheight;
n_free(mymap);
n_free(mymapnode);
mymap = (char *) n_malloc(max);
mymapnode = (loc_node **) n_malloc(max * sizeof(*mymapnode));
for(i = 0; i < max; i++) {
mymap[i] = ' ';
mymapnode[i] = NULL;
}
}
int automap_get_height(void)
{
return mapheight / 2;
}
void mymap_reinit(void)
{
mymap_init(mapwidth/2, mapheight/2);
}
void mymap_kill(void)
{
n_free(mymap);
mymap = NULL;
n_free(mymapnode);
mymapnode = NULL;
}
static int xoffset, yoffset;
static void mymap_draw(void)
{
int x, y;
int firsty, firstx, lasty, lastx;
int height, width;
firsty = mapheight; firstx = mapwidth;
lasty = 0; lastx = 0;
for(y = 0; y < mapheight; y++) {
for(x = 0; x < mapwidth; x++)
if(mymap[x + y * mapwidth] != ' ') {
if(y < firsty)
firsty = y;
if(y > lasty)
lasty = y;
if(x < firstx)
firstx = x;
if(x > lastx)
lastx = x;
}
}
height = lasty - firsty; width = lastx - firstx;
xoffset = firstx + (width - mapwidth/2) / 2;
yoffset = firsty + (height - mapheight/2) / 2;
if(yoffset >= mapheight/2)
yoffset = mapheight/2 - 1;
if(yoffset <= 1)
yoffset = 2;
if(xoffset >= mapwidth/2)
xoffset = mapwidth/2 - 1;
if(xoffset <= 1)
xoffset = 2;
for(y = 0; y < mapheight/2; y++) {
for(x = 0; x < mapwidth/2; x++)
glk_put_char(mymap[x+xoffset + (y+yoffset) * mapwidth]);
}
}
static glui32 selected_room_number = 0;
static void automap_write_loc(int x, int y)
{
loc_node *room;
selected_room_number = 0;
x += xoffset; y += yoffset;
if(x < 0 || x >= mapwidth || y < 0 || y >= mapheight)
return;
room = mymapnode[x + y * mapwidth];
if(!room || !room->found || !room->real)
return;
selected_room_number = room->number;
}
glui32 automap_draw_callback(winid_t win, glui32 width, glui32 height)
{
if(win == NULL)
return automap_size;
mymap_init(width, height);
automap_set_locations(automap_location);
glk_stream_set_current(glk_window_get_stream(win));
mymap_draw();
if(selected_room_number) {
offset short_name_off = object_name(selected_room_number);
glk_window_move_cursor(win, 0, 0);
if(short_name_off)
decodezscii(short_name_off, w_glk_put_char);
else
w_glk_put_string("<nameless>");
w_glk_put_string(" (");
g_print_number(selected_room_number);
glk_put_char(')');
}
return automap_size;
}
BOOL automap_mouse_callback(BOOL is_char_event,
winid_t win, glui32 x, glui32 y)
{
automap_write_loc(x, y);
return FALSE;
}
static void automap_calc_location(loc_node *location, loc_node *last,
int x, int y)
{
unsigned i;
char symbol;
loc_node *is_up, *is_down;
if(!location)
return;
if(location->touched)
return;
location->touched = TRUE;
/* Make sure unfound locations are blanked */
if(!location->found) {
mymap_plot(x, y, ' ', location);
return;
}
/* Don't draw up/down exits if there's a normal passage leading that way */
is_up = location->exits[DIR_UP];
is_down = location->exits[DIR_DOWN];
for(i = 0; i < NUM_DIRS; i++) {
loc_node *thisdest = automap_edge_follow(location, i);
if(thisdest && !thisdest->real)
thisdest = location->exits[i];
if(thisdest == is_up)
is_up = 0;
if(thisdest == is_down)
is_down = 0;
}
symbol = ROOM_SYMBOL((x==0 && y==0), is_up, is_down, location->real);
mymap_plot(x, y, symbol, location);
for(i = 0; i < NUM_DIRS; i++) {
loc_node *thisdest = automap_edge_follow(location, i);
if(thisdest && thisdest != last) {
int destx = x;
int desty = y;
automap_draw_edge(location, i, &destx, &desty);
automap_calc_location(thisdest, location, destx, desty);
}
}
}
/* Returns magic cookies to identify fake locations */
static glui32 automap_get_cookie(void) {
/* FIXME: When the glui32 wraps around Bad Things will happen if we return a
cookie still in use. Should reissue cookies to everyone when we wrap
around. */
static glui32 cookie = 0;
return cookie++;
}
static void automap_calc_exits(loc_node *location, int depth)
{
unsigned i, n;
loc_node *proposed[NUM_DIRS]; /* Store proposed edges here */
BOOL is_oneway[NUM_DIRS];
/* Remove fake locations */
for(i = 0; i < NUM_DIRS; i++) {
loc_node *curdest = automap_edge_follow(location, i);
if(curdest && !curdest->real)
room_remove(curdest);
}
/* Default to things going the way they actually do */
for(i = 0; i < NUM_DIRS; i++) {
proposed[i] = location->exits[i];
is_oneway[i] = FALSE;
}
/* Get rid of superfluous exits */
for(i = 0; i < NUM_DIRS; i++) {
if(proposed[i]) {
for(n = i+1; n < NUM_DIRS; n++) {
if(proposed[n] == proposed[i]) {
if(proposed[i]->exits[REVERSE_DIR(n)] == location) {
proposed[i] = NULL;
break;
}
if(proposed[i]->exits[REVERSE_DIR(i)] == location)
proposed[n] = NULL;
}
}
}
}
/* Handle forced movement */
for(i = 0; i < NUM_DIRS; i++) {
if(proposed[i] && proposed[i] == location->exits[DIR_WAIT]) {
if(proposed[i]->exits[REVERSE_DIR(i)] != location)
proposed[i] = NULL;
}
}
/* Check for one way and bent passages */
for(i = 0; i < NUM_DIRS; i++) {
if(proposed[i] && proposed[i]->found
&& proposed[i]->exits[REVERSE_DIR(i)] != location) {
is_oneway[i] = TRUE;
for(n = 0; n < NUM_DIRS; n++) {
if(n != i && proposed[i]->exits[n] == location) {
loc_node *newnode = room_find_or_create(automap_get_cookie(), FALSE);
is_oneway[i] = FALSE;
newnode->found = TRUE;
automap_set_virtual_connection(proposed[i], n, newnode, FALSE);
proposed[i] = newnode;
}
}
}
}
/* If it's a one way passage, but there are up/down exits connecting the two,
ignore the passage */
for(i = 0; i < NUM_DIRS; i++) {
if(is_oneway[i] && proposed[i]
&& ((location->exits[DIR_UP] == proposed[i]
&& proposed[i]->exits[DIR_DOWN] == location)
|| (location->exits[DIR_DOWN] == proposed[i]
&& proposed[i]->exits[DIR_UP] == location))) {
proposed[i] = 0;
is_oneway[i] = FALSE;
}
}
/* Create the proposed passages */
for(i = 0; i < NUM_DIRS; i++)
automap_set_virtual_connection(location, i, proposed[i], is_oneway[i]);
/* Explore neighbors */
if(depth) {
for(i = 0; i < NUM_DIRS; i++)
automap_calc_exits(location->exits[i], depth-1);
}
}
#define INFINITY 1000000L
static void make_distant(const char *unused_key, void *r)
{
loc_node *t = (loc_node *) r;
t->dist = INFINITY;
}
static void automap_calc_distances(loc_node *location, glui32 distance,
BOOL by_walking)
{
unsigned i;
unsigned maxdir = by_walking ? NUM_EXITS : NUM_DIRS;
if(location->dist < distance)
return;
location->dist = distance;
for(i = 0; i < maxdir; i++) {
loc_node *thisdest;
if(by_walking)
thisdest = location->exits[i];
else
thisdest = automap_edge_follow(location, i);
if(thisdest)
automap_calc_distances(thisdest, distance+1, by_walking);
}
}
static automap_path *automap_find_path(loc_node *location, loc_node *dest,
BOOL by_walking)
{
automap_path *path = NULL;
automap_path *rev;
automap_path newnode;
loc_node *p;
/* Find the distances of all nodes from dest */
n_hash_enumerate(&rooms, make_distant);
automap_calc_distances(dest, 0, by_walking);
/* If dest isn't reachable, location's distance will still be infinite */
if(location->dist == INFINITY)
return NULL;
/* At each step, go toward a nearer node 'till we're there */
p = location;
while(p != dest) {
unsigned i;
unsigned best_dir;
glui32 best_dist = INFINITY;
loc_node *best_node = NULL;
unsigned maxdir = by_walking ? NUM_EXITS : NUM_DIRS;
for(i = 0; i < maxdir; i++) {
loc_node *thisdest;
if(by_walking)
thisdest = p->exits[i];
else
thisdest = automap_edge_follow(p, i);
if(thisdest && thisdest->dist < best_dist) {
best_dir = i;
best_dist = thisdest->dist;
best_node = thisdest;
}
}
if(!best_node) {
n_show_error(E_SYSTEM, "couldn't find path there", 0);
return NULL;
}
newnode.loc = p;
newnode.dir = best_dir;
LEadd(path, newnode);
p = best_node;
}
rev = NULL;
while(path) {
LEadd(rev, *path);
LEremove(path);
}
return rev;
}
static void automap_find_cycles(loc_node *location, automap_path *curpath)
{
unsigned i;
location->touched = TRUE;
for(i = 0; i < NUM_DIRS; i++) {
loc_node *thisdest = automap_edge_follow(location, i);
if(thisdest && thisdest->found) {
automap_path newnode;
newnode.dir = i;
newnode.loc = location;
LEadd(curpath, newnode);
if(thisdest->touched) { /* Found a cycle! */
int cyclelength = 0;
automap_path *p;
cycleequation *cycle = NULL;
cycleequation newcycle;
for(p = curpath; p; p=p->next) {
int dir = p->dir;
newcycle.var = &(p->loc->outgoing[dir]->guess_length);
newcycle.min = &(p->loc->outgoing[dir]->min_length);
newcycle.xcoefficient = dirways[dir].deltax;
newcycle.ycoefficient = dirways[dir].deltay;
LEadd(cycle, newcycle);
cyclelength++;
if(p->loc == thisdest) { /* Found the relevant endpoint */
if(cyclelength <= 2) /* Ignore two nodes going to each other */
LEdestroy(cycle);
else
automap_add_cycle(cycle); /* automap_add_cycle gets ownership */
break;
}
}
if(!p) { /* The cycle had already been found */
LEdestroy(cycle);
}
} else {
automap_find_cycles(thisdest, curpath);
}
LEremove(curpath);
}
}
}
static void automap_calc_cycles(loc_node *start)
{
automap_delete_cycles();
automap_find_cycles(start, NULL);
automap_cycle_elimination();
automap_cycles_fill_values();
}
void make_untouched(const char *unused_key, void *r)
{
loc_node *t = (loc_node *) r;
t->touched = FALSE;
}
void automap_set_locations(int center)
{
loc_node *r;
r = room_find(center, TRUE);
n_hash_enumerate(&rooms, make_untouched);
automap_edges_mindist();
automap_calc_cycles(r);
n_hash_enumerate(&rooms, make_untouched);
automap_forget_interference();
mymap_reinit();