xref: /openbsd-src/games/hack/hack.search.c (revision aed906e4b20d9afbda31247cdb6acf6f29da8819)
1*aed906e4Smestre /*	$OpenBSD: hack.search.c,v 1.6 2016/01/09 18:33:15 mestre Exp $	*/
2d0b779f3Sniklas 
3df930be7Sderaadt /*
4d25013f2Scamield  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5d25013f2Scamield  * Amsterdam
6d25013f2Scamield  * All rights reserved.
7d25013f2Scamield  *
8d25013f2Scamield  * Redistribution and use in source and binary forms, with or without
9d25013f2Scamield  * modification, are permitted provided that the following conditions are
10d25013f2Scamield  * met:
11d25013f2Scamield  *
12d25013f2Scamield  * - Redistributions of source code must retain the above copyright notice,
13d25013f2Scamield  * this list of conditions and the following disclaimer.
14d25013f2Scamield  *
15d25013f2Scamield  * - Redistributions in binary form must reproduce the above copyright
16d25013f2Scamield  * notice, this list of conditions and the following disclaimer in the
17d25013f2Scamield  * documentation and/or other materials provided with the distribution.
18d25013f2Scamield  *
19d25013f2Scamield  * - Neither the name of the Stichting Centrum voor Wiskunde en
20d25013f2Scamield  * Informatica, nor the names of its contributors may be used to endorse or
21d25013f2Scamield  * promote products derived from this software without specific prior
22d25013f2Scamield  * written permission.
23d25013f2Scamield  *
24d25013f2Scamield  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25d25013f2Scamield  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26d25013f2Scamield  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27d25013f2Scamield  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28d25013f2Scamield  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29d25013f2Scamield  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30d25013f2Scamield  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31d25013f2Scamield  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32d25013f2Scamield  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33d25013f2Scamield  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34d25013f2Scamield  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35d25013f2Scamield  */
36d25013f2Scamield 
37d25013f2Scamield /*
38d25013f2Scamield  * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
39d25013f2Scamield  * All rights reserved.
40d25013f2Scamield  *
41d25013f2Scamield  * Redistribution and use in source and binary forms, with or without
42d25013f2Scamield  * modification, are permitted provided that the following conditions
43d25013f2Scamield  * are met:
44d25013f2Scamield  * 1. Redistributions of source code must retain the above copyright
45d25013f2Scamield  *    notice, this list of conditions and the following disclaimer.
46d25013f2Scamield  * 2. Redistributions in binary form must reproduce the above copyright
47d25013f2Scamield  *    notice, this list of conditions and the following disclaimer in the
48d25013f2Scamield  *    documentation and/or other materials provided with the distribution.
49d25013f2Scamield  * 3. The name of the author may not be used to endorse or promote products
50d25013f2Scamield  *    derived from this software without specific prior written permission.
51d25013f2Scamield  *
52d25013f2Scamield  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53d25013f2Scamield  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54d25013f2Scamield  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
55d25013f2Scamield  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56d25013f2Scamield  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57d25013f2Scamield  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58d25013f2Scamield  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59d25013f2Scamield  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60d25013f2Scamield  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61d25013f2Scamield  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62df930be7Sderaadt  */
63df930be7Sderaadt 
64df930be7Sderaadt #include "hack.h"
65df930be7Sderaadt 
66*aed906e4Smestre /* returns number of things found */
674a5fbbc4Spjanzen int
findit(void)68*aed906e4Smestre findit(void)
69df930be7Sderaadt {
70df930be7Sderaadt 	int num;
714a5fbbc4Spjanzen 	xchar zx,zy;
724a5fbbc4Spjanzen 	struct trap *ttmp;
734a5fbbc4Spjanzen 	struct monst *mtmp;
74df930be7Sderaadt 	xchar lx,hx,ly,hy;
75df930be7Sderaadt 
76df930be7Sderaadt 	if(u.uswallow) return(0);
774a5fbbc4Spjanzen 	for(lx = u.ux; (num = levl[lx-1][(int)u.uy].typ) && num != CORR; lx--) ;
784a5fbbc4Spjanzen 	for(hx = u.ux; (num = levl[hx+1][(int)u.uy].typ) && num != CORR; hx++) ;
794a5fbbc4Spjanzen 	for(ly = u.uy; (num = levl[(int)u.ux][ly-1].typ) && num != CORR; ly--) ;
804a5fbbc4Spjanzen 	for(hy = u.uy; (num = levl[(int)u.ux][hy+1].typ) && num != CORR; hy++) ;
81df930be7Sderaadt 	num = 0;
82df930be7Sderaadt 	for(zy = ly; zy <= hy; zy++)
83df930be7Sderaadt 		for(zx = lx; zx <= hx; zx++) {
844a5fbbc4Spjanzen 			if (levl[(int)zx][(int)zy].typ == SDOOR) {
854a5fbbc4Spjanzen 				levl[(int)zx][(int)zy].typ = DOOR;
86df930be7Sderaadt 				atl(zx, zy, '+');
87df930be7Sderaadt 				num++;
884a5fbbc4Spjanzen 			} else if (levl[(int)zx][(int)zy].typ == SCORR) {
894a5fbbc4Spjanzen 				levl[(int)zx][(int)zy].typ = CORR;
90df930be7Sderaadt 				atl(zx, zy, CORR_SYM);
91df930be7Sderaadt 				num++;
924a5fbbc4Spjanzen 			} else if ((ttmp = t_at(zx, zy))) {
93df930be7Sderaadt 				if(ttmp->ttyp == PIERC){
94df930be7Sderaadt 					(void) makemon(PM_PIERCER, zx, zy);
95df930be7Sderaadt 					num++;
96df930be7Sderaadt 					deltrap(ttmp);
97df930be7Sderaadt 				} else if (!ttmp->tseen) {
98df930be7Sderaadt 					ttmp->tseen = 1;
99df930be7Sderaadt 					if(!vism_at(zx, zy))
100df930be7Sderaadt 						atl(zx,zy,'^');
101df930be7Sderaadt 					num++;
102df930be7Sderaadt 				}
1034a5fbbc4Spjanzen 			} else if ((mtmp = m_at(zx,zy)) && (mtmp->mimic)) {
104df930be7Sderaadt 				seemimic(mtmp);
105df930be7Sderaadt 				num++;
106df930be7Sderaadt 			}
107df930be7Sderaadt 		}
108df930be7Sderaadt 	return(num);
109df930be7Sderaadt }
110df930be7Sderaadt 
1114a5fbbc4Spjanzen int
dosearch(void)112*aed906e4Smestre dosearch(void)
113df930be7Sderaadt {
1144a5fbbc4Spjanzen 	xchar x,y;
1154a5fbbc4Spjanzen 	struct trap *trap;
1164a5fbbc4Spjanzen 	struct monst *mtmp;
117df930be7Sderaadt 
118df930be7Sderaadt 	if(u.uswallow)
119df930be7Sderaadt 		pline("What are you looking for? The exit?");
120df930be7Sderaadt 	else
121df930be7Sderaadt 	for(x = u.ux-1; x < u.ux+2; x++)
122df930be7Sderaadt 	for(y = u.uy-1; y < u.uy+2; y++) if(x != u.ux || y != u.uy) {
1234a5fbbc4Spjanzen 		if(levl[(int)x][(int)y].typ == SDOOR) {
124df930be7Sderaadt 			if(rn2(7)) continue;
1254a5fbbc4Spjanzen 			levl[(int)x][(int)y].typ = DOOR;
1264a5fbbc4Spjanzen 			levl[(int)x][(int)y].seen = 0;	/* force prl */
127df930be7Sderaadt 			prl(x,y);
128df930be7Sderaadt 			nomul(0);
1294a5fbbc4Spjanzen 		} else if(levl[(int)x][(int)y].typ == SCORR) {
130df930be7Sderaadt 			if(rn2(7)) continue;
1314a5fbbc4Spjanzen 			levl[(int)x][(int)y].typ = CORR;
1324a5fbbc4Spjanzen 			levl[(int)x][(int)y].seen = 0;	/* force prl */
133df930be7Sderaadt 			prl(x,y);
134df930be7Sderaadt 			nomul(0);
135df930be7Sderaadt 		} else {
136df930be7Sderaadt 		/* Be careful not to find anything in an SCORR or SDOOR */
1374a5fbbc4Spjanzen 			if ((mtmp = m_at(x,y)))
1384a5fbbc4Spjanzen 				if(mtmp->mimic){
139df930be7Sderaadt 					seemimic(mtmp);
140df930be7Sderaadt 					pline("You find a mimic.");
141df930be7Sderaadt 					return(1);
142df930be7Sderaadt 				}
143df930be7Sderaadt 			for(trap = ftrap; trap; trap = trap->ntrap)
144df930be7Sderaadt 			if(trap->tx == x && trap->ty == y &&
145df930be7Sderaadt 			   !trap->tseen && !rn2(8)) {
146df930be7Sderaadt 				nomul(0);
147df930be7Sderaadt 				pline("You find a%s.", traps[trap->ttyp]);
148df930be7Sderaadt 				if(trap->ttyp == PIERC) {
149df930be7Sderaadt 					deltrap(trap);
150df930be7Sderaadt 					(void) makemon(PM_PIERCER,x,y);
151df930be7Sderaadt 					return(1);
152df930be7Sderaadt 				}
153df930be7Sderaadt 				trap->tseen = 1;
154df930be7Sderaadt 				if(!vism_at(x,y)) atl(x,y,'^');
155df930be7Sderaadt 			}
156df930be7Sderaadt 		}
157df930be7Sderaadt 	}
158df930be7Sderaadt 	return(1);
159df930be7Sderaadt }
160df930be7Sderaadt 
1614a5fbbc4Spjanzen int
doidtrap(void)162*aed906e4Smestre doidtrap(void)
1634a5fbbc4Spjanzen {
1644a5fbbc4Spjanzen 	struct trap *trap;
1654a5fbbc4Spjanzen 	int x,y;
1664a5fbbc4Spjanzen 
167df930be7Sderaadt 	if(!getdir(1)) return(0);
168df930be7Sderaadt 	x = u.ux + u.dx;
169df930be7Sderaadt 	y = u.uy + u.dy;
170df930be7Sderaadt 	for(trap = ftrap; trap; trap = trap->ntrap)
171df930be7Sderaadt 		if(trap->tx == x && trap->ty == y && trap->tseen) {
172df930be7Sderaadt 		    if(u.dz)
173df930be7Sderaadt 			if((u.dz < 0) != (!xdnstair && trap->ttyp == TRAPDOOR))
174df930be7Sderaadt 			    continue;
175df930be7Sderaadt 		    pline("That is a%s.", traps[trap->ttyp]);
176df930be7Sderaadt 		    return(0);
177df930be7Sderaadt 		}
178df930be7Sderaadt 	pline("I can't see a trap there.");
179df930be7Sderaadt 	return(0);
180df930be7Sderaadt }
181df930be7Sderaadt 
1824a5fbbc4Spjanzen void
wakeup(struct monst * mtmp)1834a5fbbc4Spjanzen wakeup(struct monst *mtmp)
184df930be7Sderaadt {
185df930be7Sderaadt 	mtmp->msleep = 0;
186df930be7Sderaadt 	setmangry(mtmp);
187df930be7Sderaadt 	if(mtmp->mimic) seemimic(mtmp);
188df930be7Sderaadt }
189df930be7Sderaadt 
190df930be7Sderaadt /* NOTE: we must check if(mtmp->mimic) before calling this routine */
1914a5fbbc4Spjanzen void
seemimic(struct monst * mtmp)1924a5fbbc4Spjanzen seemimic(struct monst *mtmp)
193df930be7Sderaadt {
194df930be7Sderaadt 		mtmp->mimic = 0;
195df930be7Sderaadt 		mtmp->mappearance = 0;
196df930be7Sderaadt 		unpmon(mtmp);
197df930be7Sderaadt 		pmon(mtmp);
198df930be7Sderaadt }
199