Bug Summary

File:src/games/hack/hack.c
Warning:line 375, column 5
Branch condition evaluates to a garbage value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name hack.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/games/hack/obj -resource-dir /usr/local/lib/clang/13.0.0 -I . -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/games/hack/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/games/hack/hack.c
1/* $OpenBSD: hack.c,v 1.11 2016/01/10 15:12:20 mestre Exp $ */
2
3/*
4 * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5 * Amsterdam
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * - Neither the name of the Stichting Centrum voor Wiskunde en
20 * Informatica, nor the names of its contributors may be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37/*
38 * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
39 * All rights reserved.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. The name of the author may not be used to endorse or promote products
50 * derived from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
55 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 */
63
64#include <stdio.h>
65#ifdef QUEST
66#include <stdlib.h>
67#endif
68
69#include "hack.h"
70
71extern char *nomovemsg;
72extern char *exclam();
73
74static void movobj(struct obj *, int, int);
75#ifdef QUEST
76static int rroom(int, int);
77#endif
78static int inv_cnt(void);
79
80/* called on movement:
81 * 1. when throwing ball+chain far away
82 * 2. when teleporting
83 * 3. when walking out of a lit room
84 */
85void
86unsee(void)
87{
88 int x,y;
89 struct rm *lev;
90
91/*
92 if(u.udispl){
93 u.udispl = 0;
94 newsym(u.udisx, u.udisy);
95 }
96*/
97#ifndef QUEST
98 if(seehx){
99 seehx = 0;
100 } else
101#endif /* QUEST */
102 for(x = u.ux-1; x < u.ux+2; x++)
103 for(y = u.uy-1; y < u.uy+2; y++) {
104 if(!isok(x, y)) continue;
105 lev = &levl[x][y];
106 if(!lev->lit && lev->scrsym == '.') {
107 lev->scrsym =' ';
108 lev->new = 1;
109 on_scr(x,y);
110 }
111 }
112}
113
114/* called:
115 * in hack.eat.c: seeoff(0) - blind after eating rotten food
116 * in hack.mon.c: seeoff(0) - blinded by a yellow light
117 * in hack.mon.c: seeoff(1) - swallowed
118 * in hack.do.c: seeoff(0) - blind after drinking potion
119 * in hack.do.c: seeoff(1) - go up or down the stairs
120 * in hack.trap.c:seeoff(1) - fall through trapdoor
121 */
122void
123seeoff(int mode) /* 1 to redo @, 0 to leave them */
124{ /* 1 means misc movement, 0 means blindness */
125 int x,y;
126 struct rm *lev;
127
128 if(u.udispl && mode){
129 u.udispl = 0;
130 levl[(int)u.udisx][(int)u.udisy].scrsym = news0(u.udisx,u.udisy);
131 }
132#ifndef QUEST
133 if(seehx) {
134 seehx = 0;
135 } else
136#endif /* QUEST */
137 if(!mode) {
138 for(x = u.ux-1; x < u.ux+2; x++)
139 for(y = u.uy-1; y < u.uy+2; y++) {
140 if(!isok(x, y)) continue;
141 lev = &levl[x][y];
142 if(!lev->lit && lev->scrsym == '.')
143 lev->seen = 0;
144 }
145 }
146}
147
148void
149domove(void)
150{
151 xchar oldx,oldy;
152 struct monst *mtmp;
153 struct rm *tmpr,*ust;
154 struct trap *trap;
1
'trap' declared without an initial value
155 struct obj *otmp;
156
157 u_wipe_engr(rnd(5));
158
159 if(inv_weight() > 0){
2
Assuming the condition is false
3
Taking false branch
160 pline("You collapse under your load.");
161 nomul(0);
162 return;
163 }
164 if(u.uswallow) {
4
Assuming field 'uswallow' is 0
5
Taking false branch
165 u.dx = u.dy = 0;
166 u.ux = u.ustuck->mx;
167 u.uy = u.ustuck->my;
168 } else {
169 if(Confusionu.uprops[(19 +2)].p_flgs) {
6
Assuming field 'p_flgs' is 0
7
Taking false branch
170 do {
171 confdir();
172 } while(!isok(u.ux+u.dx, u.uy+u.dy) ||
173 IS_ROCK(levl[u.ux+u.dx][u.uy+u.dy].typ)((levl[u.ux+u.dx][u.uy+u.dy].typ) < 6));
174 }
175 if(!isok(u.ux+u.dx, u.uy+u.dy)){
8
Assuming the condition is false
9
Taking false branch
176 nomul(0);
177 return;
178 }
179 }
180
181 ust = &levl[(int)u.ux][(int)u.uy];
182 oldx = u.ux;
183 oldy = u.uy;
184 if(!u.uswallow && (trap = t_at(u.ux+u.dx, u.uy+u.dy)) && trap->tseen)
10
Assuming field 'uswallow' is not equal to 0
185 nomul(0);
186 if(u.ustuck && !u.uswallow && (u.ux+u.dx != u.ustuck->mx ||
11
Assuming field 'ustuck' is null
187 u.uy+u.dy != u.ustuck->my)) {
188 if(dist(u.ustuck->mx, u.ustuck->my) > 2){
189 /* perhaps it fled (or was teleported or ... ) */
190 u.ustuck = 0;
191 } else {
192 if(Blindu.uprops[(19 +7)].p_flgs) pline("You cannot escape from it!");
193 else pline("You cannot escape from %s!",
194 monnam(u.ustuck));
195 nomul(0);
196 return;
197 }
198 }
199 if(u.uswallow
11.1
Field 'uswallow' is not equal to 0
|| (mtmp = m_at(u.ux+u.dx,u.uy+u.dy))) {
200 /* attack monster */
201
202 nomul(0);
203 gethungry();
204 if(multi < 0) return; /* we just fainted */
12
Assuming 'multi' is >= 0
13
Taking false branch
205
206 /* try to attack; note that it might evade */
207 if(attack(u.uswallow ? u.ustuck : mtmp))
14
Assuming field 'uswallow' is not equal to 0
15
'?' condition is true
16
Assuming the condition is false
17
Taking false branch
208 return;
209 }
210 /* not attacking an animal, so we try to move */
211 if(u.utrap) {
18
Assuming field 'utrap' is 0
19
Taking false branch
212 if(u.utraptype == TT_PIT1) {
213 pline("You are still in a pit.");
214 u.utrap--;
215 } else {
216 pline("You are caught in a beartrap.");
217 if((u.dx && u.dy) || !rn2(5)) u.utrap--;
218 }
219 return;
220 }
221 tmpr = &levl[u.ux+u.dx][u.uy+u.dy];
222 if(IS_ROCK(tmpr->typ)((tmpr->typ) < 6) ||
20
Assuming field 'typ' is >= 6
223 (u.dx && u.dy && (tmpr->typ == DOOR7 || ust->typ == DOOR7))){
21
Assuming field 'dx' is 0
224 flags.move = 0;
225 nomul(0);
226 return;
227 }
228 while ((otmp = sobj_at(ENORMOUS_ROCK97, u.ux+u.dx, u.uy+u.dy))) {
22
Loop condition is false. Execution continues on line 287
229 xchar rx = u.ux+2*u.dx, ry = u.uy+2*u.dy;
230 struct trap *ttmp;
231 nomul(0);
232 if (isok(rx,ry) && !IS_ROCK(levl[(int)rx][(int)ry].typ)((levl[(int)rx][(int)ry].typ) < 6) &&
233 (levl[(int)rx][(int)ry].typ != DOOR7 || !(u.dx && u.dy)) &&
234 !sobj_at(ENORMOUS_ROCK97, rx, ry)) {
235 if(m_at(rx,ry)) {
236 pline("You hear a monster behind the rock.");
237 pline("Perhaps that's why you cannot move it.");
238 goto cannot_push;
239 }
240 if ((ttmp = t_at(rx,ry)))
241 switch(ttmp->ttyp) {
242 case PIT5:
243 pline("You push the rock into a pit!");
244 deltrap(ttmp);
245 delobj(otmp);
246 pline("It completely fills the pit!");
247 continue;
248 case TELEP_TRAP4:
249 pline("You push the rock and suddenly it disappears!");
250 delobj(otmp);
251 continue;
252 }
253 if (levl[(int)rx][(int)ry].typ == POOL6) {
254 levl[(int)rx][(int)ry].typ = ROOM9;
255 mnewsym(rx,ry);
256 prl(rx,ry);
257 pline("You push the rock into the water.");
258 pline("Now you can cross the water!");
259 delobj(otmp);
260 continue;
261 }
262 otmp->ox = rx;
263 otmp->oy = ry;
264 /* pobj(otmp); */
265 if(cansee(rx,ry)) atl(rx,ry,otmp->olet);
266 if(Invisible(u.uprops[(19 +3)].p_flgs && !u.uprops[4].p_flgs)) newsym(u.ux+u.dx, u.uy+u.dy);
267
268 { static long lastmovetime;
269 /* note: this var contains garbage initially and
270 after a restore */
271 if(moves > lastmovetime+2 || moves < lastmovetime)
272 pline("With great effort you move the enormous rock.");
273 lastmovetime = moves;
274 }
275 } else {
276 pline("You try to move the enormous rock, but in vain.");
277 cannot_push:
278 if((!invent || inv_weight()+90 <= 0) &&
279 (!u.dx || !u.dy || (IS_ROCK(levl[(int)u.ux][u.uy+u.dy].typ)((levl[(int)u.ux][u.uy+u.dy].typ) < 6)
280 && IS_ROCK(levl[u.ux+u.dx][(int)u.uy].typ)((levl[u.ux+u.dx][(int)u.uy].typ) < 6)))){
281 pline("However, you can squeeze yourself into a small opening.");
282 break;
283 } else
284 return;
285 }
286 }
287 if(u.dx && u.dy && IS_ROCK(levl[(int)u.ux][u.uy+u.dy].typ)((levl[(int)u.ux][u.uy+u.dy].typ) < 6) &&
23
Assuming field 'dx' is 0
288 IS_ROCK(levl[u.ux+u.dx][(int)u.uy].typ)((levl[u.ux+u.dx][(int)u.uy].typ) < 6) &&
289 invent && inv_weight()+40 > 0) {
290 pline("You are carrying too much to get through.");
291 nomul(0);
292 return;
293 }
294 if(Punishedu.uprops[(19 +5)].p_flgs &&
24
Assuming field 'p_flgs' is 0
295 DIST(u.ux+u.dx, u.uy+u.dy, uchain->ox, uchain->oy)(((u.ux+u.dx)-(uchain->ox))*((u.ux+u.dx)-(uchain->ox)) +
((u.uy+u.dy)-(uchain->oy))*((u.uy+u.dy)-(uchain->oy)))
> 2){
296 if(carried(uball)) {
297 movobj(uchain, u.ux, u.uy);
298 goto nodrag;
299 }
300
301 if(DIST(u.ux+u.dx, u.uy+u.dy, uball->ox, uball->oy)(((u.ux+u.dx)-(uball->ox))*((u.ux+u.dx)-(uball->ox)) + (
(u.uy+u.dy)-(uball->oy))*((u.uy+u.dy)-(uball->oy)))
< 3){
302 /* leave ball, move chain under/over ball */
303 movobj(uchain, uball->ox, uball->oy);
304 goto nodrag;
305 }
306
307 if(inv_weight() + (int) uball->owt/2 > 0) {
308 pline("You cannot %sdrag the heavy iron ball.",
309 invent ? "carry all that and also " : "");
310 nomul(0);
311 return;
312 }
313
314 movobj(uball, uchain->ox, uchain->oy);
315 unpobj(uball); /* BAH %% */
316 uchain->ox = u.ux;
317 uchain->oy = u.uy;
318 nomul(-2);
319 nomovemsg = "";
320 nodrag: ;
321 }
322 u.ux += u.dx;
323 u.uy += u.dy;
324 if(flags.run) {
25
Assuming field 'run' is 0
26
Taking false branch
325 if(tmpr->typ == DOOR7 ||
326 (xupstair == u.ux && yupstair == u.uy) ||
327 (xdnstair == u.ux && ydnstair == u.uy))
328 nomul(0);
329 }
330
331 if(tmpr->typ == POOL6 && !Levitationu.uprops[6].p_flgs)
27
Assuming field 'typ' is not equal to POOL
332 drown(); /* not necessarily fatal */
333
334/*
335 if(u.udispl) {
336 u.udispl = 0;
337 newsym(oldx,oldy);
338 }
339*/
340 if(!Blindu.uprops[(19 +7)].p_flgs) {
28
Assuming field 'p_flgs' is not equal to 0
29
Taking false branch
341#ifdef QUEST
342 setsee();
343#else
344 if(ust->lit) {
345 if(tmpr->lit) {
346 if(tmpr->typ == DOOR7)
347 prl1(u.ux+u.dx,u.uy+u.dy);
348 else if(ust->typ == DOOR7)
349 nose1(oldx-u.dx,oldy-u.dy);
350 } else {
351 unsee();
352 prl1(u.ux+u.dx,u.uy+u.dy);
353 }
354 } else {
355 if(tmpr->lit) setsee();
356 else {
357 prl1(u.ux+u.dx,u.uy+u.dy);
358 if(tmpr->typ == DOOR7) {
359 if(u.dy) {
360 prl(u.ux-1,u.uy);
361 prl(u.ux+1,u.uy);
362 } else {
363 prl(u.ux,u.uy-1);
364 prl(u.ux,u.uy+1);
365 }
366 }
367 }
368 nose1(oldx-u.dx,oldy-u.dy);
369 }
370#endif /* QUEST */
371 } else {
372 pru();
373 }
374 if(!flags.nopick) pickup(1);
30
Assuming field 'nopick' is not equal to 0
31
Taking false branch
375 if(trap) dotrap(trap); /* fall into pit, arrow trap, etc. */
32
Branch condition evaluates to a garbage value
376 (void) inshop();
377 if(!Blindu.uprops[(19 +7)].p_flgs) read_engr_at(u.ux,u.uy);
378}
379
380static void
381movobj(struct obj *obj, int ox, int oy)
382{
383 /* Some dirty programming to get display right */
384 freeobj(obj);
385 unpobj(obj);
386 obj->nobj = fobj;
387 fobj = obj;
388 obj->ox = ox;
389 obj->oy = oy;
390}
391
392int
393dopickup(void)
394{
395 if(!g_at(u.ux,u.uy) && !o_at(u.ux,u.uy)) {
396 pline("There is nothing here to pick up.");
397 return(0);
398 }
399 if(Levitationu.uprops[6].p_flgs) {
400 pline("You cannot reach the floor.");
401 return(1);
402 }
403 pickup(0);
404 return(1);
405}
406
407void
408pickup(int all)
409{
410 struct gold *gold;
411 struct obj *obj, *obj2;
412 int wt;
413
414 if (Levitationu.uprops[6].p_flgs)
415 return;
416 while ((gold = g_at(u.ux,u.uy))) {
417 pline("%ld gold piece%s.", gold->amount, plur(gold->amount)(((gold->amount) == 1) ? "" : "s"));
418 u.ugold += gold->amount;
419 flags.botl = 1;
420 freegold(gold);
421 if(flags.run) nomul(0);
422 if(Invisible(u.uprops[(19 +3)].p_flgs && !u.uprops[4].p_flgs)) newsym(u.ux,u.uy);
423 }
424
425 /* check for more than one object */
426 if(!all) {
427 int ct = 0;
428
429 for(obj = fobj; obj; obj = obj->nobj)
430 if(obj->ox == u.ux && obj->oy == u.uy)
431 if(!Punishedu.uprops[(19 +5)].p_flgs || obj != uchain)
432 ct++;
433 if(ct < 2)
434 all++;
435 else
436 pline("There are several objects here.");
437 }
438
439 for(obj = fobj; obj; obj = obj2) {
440 obj2 = obj->nobj; /* perhaps obj will be picked up */
441 if(obj->ox == u.ux && obj->oy == u.uy) {
442 if(flags.run) nomul(0);
443
444 /* do not pick up uchain */
445 if(Punishedu.uprops[(19 +5)].p_flgs && obj == uchain)
446 continue;
447
448 if(!all) {
449 char c;
450
451 pline("Pick up %s ? [ynaq]", doname(obj));
452 while(!strchr("ynaq ", (c = readchar())))
453 hackbell();
454 if(c == 'q') return;
455 if(c == 'n') continue;
456 if(c == 'a') all = 1;
457 }
458
459 if(obj->otyp == DEAD_COCKATRICE47 && !uarmg){
460 pline("Touching the dead cockatrice is a fatal mistake.");
461 pline("You turn to stone.");
462 killer = "cockatrice cadaver";
463 done("died");
464 }
465
466 if(obj->otyp == SCR_SCARE_MONSTER134){
467 if(!obj->spe) obj->spe = 1;
468 else {
469 /* Note: perhaps the 1st pickup failed: you cannot
470 carry anymore, and so we never dropped it -
471 let's assume that treading on it twice also
472 destroys the scroll */
473 pline("The scroll turns to dust as you pick it up.");
474 delobj(obj);
475 continue;
476 }
477 }
478
479 wt = inv_weight() + obj->owt;
480 if(wt > 0) {
481 if(obj->quan > 1) {
482 /* see how many we can lift */
483 int savequan = obj->quan;
484 int iw = inv_weight();
485 int qq;
486 for(qq = 1; qq < savequan; qq++){
487 obj->quan = qq;
488 if(iw + weight(obj) > 0)
489 break;
490 }
491 obj->quan = savequan;
492 qq--;
493 /* we can carry qq of them */
494 if(!qq) goto too_heavy;
495 pline("You can only carry %s of the %s lying here.",
496 (qq == 1) ? "one" : "some",
497 doname(obj));
498 (void) splitobj(obj, qq);
499 /* note: obj2 is set already, so we'll never
500 * encounter the other half; if it should be
501 * otherwise then write
502 * obj2 = splitobj(obj,qq);
503 */
504 goto lift_some;
505 }
506 too_heavy:
507 pline("There %s %s here, but %s.",
508 (obj->quan == 1) ? "is" : "are",
509 doname(obj),
510 !invent ? "it is too heavy for you to lift"
511 : "you cannot carry anymore");
512 break;
513 }
514 lift_some:
515 if(inv_cnt() >= 52) {
516 pline("Your knapsack cannot accommodate anymore items.");
517 break;
518 }
519 if(wt > -5) pline("You have a little trouble lifting");
520 freeobj(obj);
521 if(Invisible(u.uprops[(19 +3)].p_flgs && !u.uprops[4].p_flgs)) newsym(u.ux,u.uy);
522 addtobill(obj); /* sets obj->unpaid if necessary */
523 { int pickquan = obj->quan;
524 int mergquan;
525 if(!Blindu.uprops[(19 +7)].p_flgs) obj->dknown = 1; /* this is done by prinv(),
526 but addinv() needs it already for merging */
527 obj = addinv(obj); /* might merge it with other objects */
528 mergquan = obj->quan;
529 obj->quan = pickquan; /* to fool prinv() */
530 prinv(obj);
531 obj->quan = mergquan;
532 }
533 }
534 }
535}
536
537/* stop running if we see something interesting */
538/* turn around a corner if that is the only way we can proceed */
539/* do not turn left or right twice */
540void
541lookaround(void)
542{
543 int x, y, i, x0, y0, m0, i0 = 9;
544 int corrct = 0, noturn = 0;
545 struct monst *mtmp;
546
547 if (Blindu.uprops[(19 +7)].p_flgs || flags.run == 0) return;
548 if (flags.run == 1 && levl[(int)u.ux][(int)u.uy].typ == ROOM9)
549 return;
550#ifdef QUEST
551 if(u.ux0 == u.ux+u.dx && u.uy0 == u.uy+u.dy) goto stop;
552#endif /* QUEST */
553 for(x = u.ux-1; x <= u.ux+1; x++) for(y = u.uy-1; y <= u.uy+1; y++){
554 if(x == u.ux && y == u.uy) continue;
555 if(!levl[x][y].typ) continue;
556 if((mtmp = m_at(x,y)) && !mtmp->mimic &&
557 (!mtmp->minvis || See_invisibleu.uprops[4].p_flgs)){
558 if(!mtmp->mtame || (x == u.ux+u.dx && y == u.uy+u.dy))
559 goto stop;
560 } else mtmp = 0; /* invisible M cannot influence us */
561 if(x == u.ux-u.dx && y == u.uy-u.dy) continue;
562 switch(levl[x][y].scrsym){
563 case '|':
564 case '-':
565 case '.':
566 case ' ':
567 break;
568 case '+':
569 if(x != u.ux && y != u.uy) break;
570 if(flags.run != 1) goto stop;
571 /* fall into next case */
572 case CORR_SYM'#':
573 corr:
574 if(flags.run == 1 || flags.run == 3) {
575 i = DIST(x,y,u.ux+u.dx,u.uy+u.dy)(((x)-(u.ux+u.dx))*((x)-(u.ux+u.dx)) + ((y)-(u.uy+u.dy))*((y)
-(u.uy+u.dy)))
;
576 if(i > 2) break;
577 if(corrct == 1 && DIST(x,y,x0,y0)(((x)-(x0))*((x)-(x0)) + ((y)-(y0))*((y)-(y0))) != 1)
578 noturn = 1;
579 if(i < i0) {
580 i0 = i;
581 x0 = x;
582 y0 = y;
583 m0 = mtmp ? 1 : 0;
584 }
585 }
586 corrct++;
587 break;
588 case '^':
589 if(flags.run == 1) goto corr; /* if you must */
590 if(x == u.ux+u.dx && y == u.uy+u.dy) goto stop;
591 break;
592 default: /* e.g. objects or trap or stairs */
593 if(flags.run == 1) goto corr;
594 if(mtmp) break; /* d */
595 stop:
596 nomul(0);
597 return;
598 }
599 }
600#ifdef QUEST
601 if(corrct > 0 && (flags.run == 4 || flags.run == 5)) goto stop;
602#endif /* QUEST */
603 if(corrct > 1 && flags.run == 2) goto stop;
604 if((flags.run == 1 || flags.run == 3) && !noturn && !m0 && i0 &&
605 (corrct == 1 || (corrct == 2 && i0 == 1))) {
606 /* make sure that we do not turn too far */
607 if(i0 == 2) {
608 if(u.dx == y0-u.uy && u.dy == u.ux-x0)
609 i = 2; /* straight turn right */
610 else
611 i = -2; /* straight turn left */
612 } else if(u.dx && u.dy) {
613 if((u.dx == u.dy && y0 == u.uy) ||
614 (u.dx != u.dy && y0 != u.uy))
615 i = -1; /* half turn left */
616 else
617 i = 1; /* half turn right */
618 } else {
619 if((x0-u.ux == y0-u.uy && !u.dy) ||
620 (x0-u.ux != y0-u.uy && u.dy))
621 i = 1; /* half turn right */
622 else
623 i = -1; /* half turn left */
624 }
625 i += u.last_str_turn;
626 if(i <= 2 && i >= -2) {
627 u.last_str_turn = i;
628 u.dx = x0-u.ux, u.dy = y0-u.uy;
629 }
630 }
631}
632
633/* something like lookaround, but we are not running */
634/* react only to monsters that might hit us */
635int
636monster_nearby(void)
637{
638 int x,y;
639 struct monst *mtmp;
640
641 if(!Blindu.uprops[(19 +7)].p_flgs)
642 for(x = u.ux-1; x <= u.ux+1; x++) for(y = u.uy-1; y <= u.uy+1; y++){
643 if(x == u.ux && y == u.uy) continue;
644 if((mtmp = m_at(x,y)) && !mtmp->mimic && !mtmp->mtame &&
645 !mtmp->mpeaceful && !strchr("Ea", mtmp->data->mlet) &&
646 !mtmp->mfroz && !mtmp->msleep && /* aplvax!jcn */
647 (!mtmp->minvis || See_invisibleu.uprops[4].p_flgs))
648 return(1);
649 }
650 return(0);
651}
652
653#ifdef QUEST
654int
655cansee(xchar x, xchar y)
656{
657 int dx,dy,adx,ady,sdx,sdy,dmax,d;
658
659 if(Blindu.uprops[(19 +7)].p_flgs) return(0);
660 if(!isok(x,y)) return(0);
661 d = dist(x,y);
662 if(d < 3) return(1);
663 if(d > u.uhorizon*u.uhorizon) return(0);
664 if(!levl[x][y].lit)
665 return(0);
666 dx = x - u.ux; adx = abs(dx); sdx = sgn(dx);
667 dy = y - u.uy; ady = abs(dy); sdy = sgn(dy);
668 if(dx == 0 || dy == 0 || adx == ady){
669 dmax = (dx == 0) ? ady : adx;
670 for(d = 1; d <= dmax; d++)
671 if(!rroom(sdx*d,sdy*d))
672 return(0);
673 return(1);
674 } else if(ady > adx){
675 for(d = 1; d <= ady; d++){
676 if(!rroom(sdx*( (d*adx)/ady ), sdy*d) ||
677 !rroom(sdx*( (d*adx-1)/ady+1 ), sdy*d))
678 return(0);
679 }
680 return(1);
681 } else {
682 for(d = 1; d <= adx; d++){
683 if(!rroom(sdx*d, sdy*( (d*ady)/adx )) ||
684 !rroom(sdx*d, sdy*( (d*ady-1)/adx+1 )))
685 return(0);
686 }
687 return(1);
688 }
689}
690
691static int
692rroom(int x, int y)
693{
694 return(IS_ROOM(levl[u.ux+x][u.uy+y].typ)((levl[u.ux+x][u.uy+y].typ) >= 9));
695}
696
697#else
698
699int
700cansee(xchar x, xchar y)
701{
702 if (Blindu.uprops[(19 +7)].p_flgs || u.uswallow)
703 return(0);
704 if (dist(x,y) < 3)
705 return(1);
706 if (levl[(int)x][(int)y].lit && seelx <= x && x <= seehx &&
707 seely <= y && y <= seehy)
708 return(1);
709 return(0);
710}
711#endif /* QUEST */
712
713int
714sgn(int a)
715{
716 return((a > 0) ? 1 : (a == 0) ? 0 : -1);
717}
718
719#ifdef QUEST
720void
721setsee(void)
722{
723 int x,y;
724
725 if(Blindu.uprops[(19 +7)].p_flgs) {
726 pru();
727 return;
728 }
729 for(y = u.uy-u.uhorizon; y <= u.uy+u.uhorizon; y++)
730 for(x = u.ux-u.uhorizon; x <= u.ux+u.uhorizon; x++) {
731 if(cansee(x,y))
732 prl(x,y);
733 }
734}
735
736#else
737void
738setsee(void)
739{
740 int x,y;
741
742 if (Blindu.uprops[(19 +7)].p_flgs) {
743 pru();
744 return;
745 }
746 if (!levl[(int)u.ux][(int)u.uy].lit) {
747 seelx = u.ux-1;
748 seehx = u.ux+1;
749 seely = u.uy-1;
750 seehy = u.uy+1;
751 } else {
752 for(seelx = u.ux; levl[seelx-1][(int)u.uy].lit; seelx--);
753 for(seehx = u.ux; levl[seehx+1][(int)u.uy].lit; seehx++);
754 for(seely = u.uy; levl[(int)u.ux][seely-1].lit; seely--);
755 for(seehy = u.uy; levl[(int)u.ux][seehy+1].lit; seehy++);
756 }
757 for (y = seely; y <= seehy; y++)
758 for (x = seelx; x <= seehx; x++) {
759 prl(x,y);
760 }
761 if (!levl[(int)u.ux][(int)u.uy].lit)
762 seehx = 0; /* seems necessary elsewhere */
763 else {
764 if(seely == u.uy) for(x = u.ux-1; x <= u.ux+1; x++) prl(x,seely-1);
765 if(seehy == u.uy) for(x = u.ux-1; x <= u.ux+1; x++) prl(x,seehy+1);
766 if(seelx == u.ux) for(y = u.uy-1; y <= u.uy+1; y++) prl(seelx-1,y);
767 if(seehx == u.ux) for(y = u.uy-1; y <= u.uy+1; y++) prl(seehx+1,y);
768 }
769}
770#endif /* QUEST */
771
772void
773nomul(int nval)
774{
775 if(multi < 0) return;
776 multi = nval;
777 flags.mv = flags.run = 0;
778}
779
780int
781abon(void)
782{
783 if(u.ustr == 3) return(-3);
784 else if(u.ustr < 6) return(-2);
785 else if(u.ustr < 8) return(-1);
786 else if(u.ustr < 17) return(0);
787 else if(u.ustr < 69) return(1); /* up to 18/50 */
788 else if(u.ustr < 118) return(2);
789 else return(3);
790}
791
792int
793dbon(void)
794{
795 if(u.ustr < 6) return(-1);
796 else if(u.ustr < 16) return(0);
797 else if(u.ustr < 18) return(1);
798 else if(u.ustr == 18) return(2); /* up to 18 */
799 else if(u.ustr < 94) return(3); /* up to 18/75 */
800 else if(u.ustr < 109) return(4); /* up to 18/90 */
801 else if(u.ustr < 118) return(5); /* up to 18/99 */
802 else return(6);
803}
804
805void
806losestr(int num) /* may kill you; cause may be poison or monster like 'A' */
807{
808 u.ustr -= num;
809 while(u.ustr < 3) {
810 u.ustr++;
811 u.uhp -= 6;
812 u.uhpmax -= 6;
813 }
814 flags.botl = 1;
815}
816
817void
818losehp(int n, char *knam)
819{
820 u.uhp -= n;
821 if(u.uhp > u.uhpmax)
822 u.uhpmax = u.uhp; /* perhaps n was negative */
823 flags.botl = 1;
824 if(u.uhp < 1) {
825 killer = knam; /* the thing that killed you */
826 done("died");
827 }
828}
829
830void
831losehp_m(int n, struct monst *mtmp)
832{
833 u.uhp -= n;
834 flags.botl = 1;
835 if(u.uhp < 1)
836 done_in_by(mtmp);
837}
838
839/* hit by V or W */
840void
841losexp(void)
842{
843 int num;
844
845 if(u.ulevel > 1)
846 pline("Goodbye level %u.", u.ulevel--);
847 else
848 u.uhp = -1;
849 num = rnd(10);
850 u.uhp -= num;
851 u.uhpmax -= num;
852 u.uexp = newuexp();
853 flags.botl = 1;
854}
855
856int
857inv_weight(void)
858{
859 struct obj *otmp = invent;
860 int wt = (u.ugold + 500)/1000;
861 int carrcap;
862
863 if(Levitationu.uprops[6].p_flgs) /* pugh@cornell */
864 carrcap = MAX_CARR_CAP120;
865 else {
866 carrcap = 5*(((u.ustr > 18) ? 20 : u.ustr) + u.ulevel);
867 if(carrcap > MAX_CARR_CAP120) carrcap = MAX_CARR_CAP120;
868 if(Wounded_legsu.uprops[(19 +8)].p_flgs & LEFT_SIDE010000L) carrcap -= 10;
869 if(Wounded_legsu.uprops[(19 +8)].p_flgs & RIGHT_SIDE020000L) carrcap -= 10;
870 }
871 while(otmp){
872 wt += otmp->owt;
873 otmp = otmp->nobj;
874 }
875 return(wt - carrcap);
876}
877
878static int
879inv_cnt(void)
880{
881 struct obj *otmp = invent;
882 int ct = 0;
883
884 while(otmp){
885 ct++;
886 otmp = otmp->nobj;
887 }
888 return(ct);
889}
890
891long
892newuexp(void)
893{
894 return(10*(1L << (u.ulevel-1)));
895}