forked from rogueforge/rogomatic14
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsurvival.c
More file actions
323 lines (266 loc) · 8.14 KB
/
Copy pathsurvival.c
File metadata and controls
323 lines (266 loc) · 8.14 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
/*
* Rog-O-Matic
* Automatically exploring the dungeons of doom.
*
* Copyright (C) 2008 by Anthony Molinaro
* Copyright (C) 1985 by Appel, Jacobson, Hamey, and Mauldin.
*
* This file is part of Rog-O-Matic.
*
* Rog-O-Matic 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 3 of the License, or
* (at your option) any later version.
*
* Rog-O-Matic 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 Rog-O-Matic. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* survival.c:
*
* This file contains all of the "Run Away" code.
* Well, almost all of the run away code.
* At least I think it has something to do with running away.
*
* 7-Mar-87 Michael Mauldin (mlm) at Carnegie-Mellon University
* Modified markcycles() to check for choke points in corridors,
* as possible RUNOK squares, in addition to doors. This fixes the
* bug where a cycle through corridors was not noticed because
* there was no door there.
*/
# include <stdio.h>
# include <ctype.h>
# include <setjmp.h>
# include <string.h>
# include "modern_curses.h"
# include "types.h"
# include "config.h"
# include "globals.h"
# define SO 1
# define SE 0
# define highlight(rowcol,stand) \
if (print || debug (D_SCREEN)) { \
at((rowcol)/C,(rowcol)%C); \
if (stand) standout (); \
printw("%c",screen[0][rowcol]); \
if (stand) standend (); \
if (!quiet) { \
refresh (); \
} \
}
/* static declarations */
static void markchokepts (void);
/*
* markcycles: evokes fond memories of an earlier time, when Andrew
* was a hacker who just patched things together until they worked,
* and used the SHIFT-STOP key to compile things, rather than thinking.
*
* markcycles does a depth-first-search of the squares to find loops in
* the rooms. All doors on loops are marked RUNOK, and are used by the
* runaway code.
*/
int
markcycles (int print)
{
short mark[R_C+1]; /* +1 for paranoia */
struct {short where,door,dirs;} st[SM_BUF+1+1]; /* +1 for fencepost, +1 for paranoia */
int sp,newsquare; int *Scr; int whichdir; int D;
if (!new_mark) return (0);
Scr=scrmap[0];
markchokepts ();
memset (mark, 0, sizeof(mark)); /* paranoia */
memset (st, 0, sizeof(st)); /* paranoia */
sp=1; st[1].where=atrow*C+atcol; st[1].dirs=1; st[1].door=0;
for (D = 0; D < 8; D += 2) {
newsquare = st[1].where + deltrc[dirmask(D^4)];
if (valr_c(newsquare) && (Scr[newsquare] & CANGO)) {
if (mark[newsquare]) {
int stop, i;
if (mark[newsquare] < sp) {
for (stop=st[mark[newsquare]].door,
i=(Scr[st[sp].where] & CHOKE) ? sp : st[sp].door;
i!=stop;
i=st[i].door) {
if (valr_c(st[i].where)) {
Scr[st[i].where] |= RUNOK;
highlight (st[i].where, SO);
}
}
}
} else if ((sp > 1) && (sp < SM_BUF)) {
sp++; mark[newsquare] = sp;
highlight (newsquare, SO);
st[sp].where = newsquare;
st[sp].dirs = 1; st[1].dirs= -1;
st[sp].door = (valr_c(st[sp-1].where) && (Scr[st[sp-1].where] & CHOKE)) ? sp-1 : st[sp-1].door;
}
}
while ((sp > 1) && (sp < SM_BUF)) {
whichdir = ((st[sp].dirs++) << 1);
if (whichdir < 8) {
/* whichdir is 6,2, or 4. */
newsquare = st[sp].where + deltrc[dirmask(whichdir+D)];
if (valr_c(newsquare) && (Scr[newsquare] & CANGO)) {
if (mark[newsquare]) {
int stop, i;
if (mark[newsquare] < sp) {
for (stop=st[mark[newsquare]].door,
i=(Scr[st[sp].where]&CHOKE)?sp:st[sp].door;
i!=stop;
i=st[i].door) {
if (valr_c(st[i].where)) {
Scr[st[i].where] |= RUNOK;
highlight (st[i].where, SO);
}
}
}
}
else if ((sp < SM_BUF) && valr_c(newsquare)) {
sp++; mark[newsquare] = sp;
highlight (newsquare, SO);
st[sp].where = newsquare;
st[sp].dirs = 1; D += whichdir+4;
st[sp].door = (Scr[st[sp-1].where] & CHOKE) ? sp-1 : st[sp-1].door;
}
}
}
else {
if (! (Scr[st[sp].where] & RUNOK)) highlight (st[sp].where, SE);
sp--;
D -= 4+((st[sp].dirs-1)<<1);
}
}
}
highlight (st[1].where, SE);
new_mark = false;
return (1);
}
/*
* markchokepts: Mark places to check for cycles. A choke point
* is any door or any hall with only two neighbors.
*
* Added: 3/7/87 by mlm
*/
static void
markchokepts (void)
{
int *Scr, *ScrEnd;
for (Scr = scrmap[0], ScrEnd = &Scr[1920]; Scr<ScrEnd; Scr++) {
if (*Scr & DOOR) *Scr |= CHOKE;
else if (*Scr & HALL) {
int nbrs = 0, k;
for (k=0; k<8; k++)
{ if (Scr[deltrc[k]] & CANGO) nbrs++; }
if (nbrs < 4 ||
! (Scr[ 1] & Scr[-(C-1)] & Scr[-C] & CANGO ||
Scr[-C] & Scr[-(C+1)] & Scr[-1] & CANGO ||
Scr[-1] & Scr[ C-1 ] & Scr[ C] & CANGO ||
Scr[ C] & Scr[ C+1 ] & Scr[ 1] & CANGO)) {
*Scr |= CHOKE;
if (debug (D_SCREEN)) {
int rowcol = Scr - scrmap[0];
standout ();
mvprintw (rowcol/C, rowcol%C, "C");
standend ();
}
}
}
}
}
/*
* Runaway: Panic!
*/
int
runaway (void)
{
if (on (SCAREM)) {
dwait (D_BATTLE, __func__, "Not running, on scare monster scroll");
return (0);
}
dwait (D_BATTLE | D_SEARCH, __func__, "Run away!!!!");
if (on (STAIRS) && !floating) /* Go up or down */
return (goupstairs (RUNNING) || godownstairs (RUNNING));
if (canrun ()) /* If canrun finds a move, use it */
return (followmap (RUNAWAY));
return (0); /* Cant run away */
}
/*
* Canrun: set up a move which will get us away from danger.
*/
int
canrun (void)
{
int result, oldcomp = compression;
if (on (STAIRS)) return (1); /* Can run down stairs */
compression = false; /* Be tense when fleeing */
result = (findmove (RUNAWAY, runinit, runvalue, REEVAL) ||
findmove (EXPLORERUN, expruninit, exprunvalue, REEVAL));
compression = oldcomp;
return (result);
}
/*
* unpin:
*
* "When you're stuck and wriggling on a pin,
* When you're pinned and wriggling on a wall..."
*
* "The Love Song of J. Alfred Prufrock", T.S. Eliot
*/
int
unpin (void)
{
int result, oldcomp = compression;
if (on (SCAREM)) {
dwait (D_BATTLE, __func__, "Not unpinning, on scare monster scroll");
return (0);
}
if (on (STAIRS) && !floating) {
if (!goupstairs (RUNNING)) godownstairs (RUNNING);
return (1);
}
dwait (D_BATTLE, __func__, "Pinned");
/* currentrectangle (); // always done after each move of the rogue // */
compression = false; /* Be tense when fleeing */
result = (makemove (UNPIN, unpininit, runvalue, REEVAL) ||
makemove (UNPINEXP, expunpininit, expunpinvalue, REEVAL));
compression = oldcomp;
return (result);
}
/*
* backtodoor: Useful when being ganged up on. Back into the nearest
* door.
*/
int
backtodoor (int dist)
{
static int lastcall= -10, stillcount=0, notmoving=0, closest=99;
/*
* Keep track of the opponents distance. If they stop advancing on us,
* disable the rule for 10 turns.
*/
if (turns-lastcall > 20)
{ notmoving=0; closest=99; stillcount=0; }
else if (dist < closest)
{ closest=dist; stillcount=0; }
else if (++stillcount > 5)
{ notmoving++; }
lastcall = turns;
/*
* Now check whether we try to move back to the door
*/
if (notmoving) {
dwait (D_BATTLE, __func__, "monsters not moving");
} else if (on (SCAREM)) {
dwait (D_BATTLE, __func__, "Not backing up, on scare monster scroll");
} else if (dist > 0 && (on (DOOR) || nextto (DOOR, atrow, atcol))) {
dwait (D_BATTLE, __func__, "next to door, have time");
} else if (makemove (RUNTODOOR, rundoorinit, rundoorvalue, REEVAL)) {
dwait (D_BATTLE, __func__, "Back to the door"); return (1);
}
return (0);
}