xref: /openbsd-src/games/hack/hack.search.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: hack.search.c,v 1.4 2003/05/19 06:30:56 pjanzen 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 #ifndef lint
65 static const char rcsid[] = "$OpenBSD: hack.search.c,v 1.4 2003/05/19 06:30:56 pjanzen Exp $";
66 #endif /* not lint */
67 
68 #include "hack.h"
69 
70 int
71 findit()	/* returns number of things found */
72 {
73 	int num;
74 	xchar zx,zy;
75 	struct trap *ttmp;
76 	struct monst *mtmp;
77 	xchar lx,hx,ly,hy;
78 
79 	if(u.uswallow) return(0);
80 	for(lx = u.ux; (num = levl[lx-1][(int)u.uy].typ) && num != CORR; lx--) ;
81 	for(hx = u.ux; (num = levl[hx+1][(int)u.uy].typ) && num != CORR; hx++) ;
82 	for(ly = u.uy; (num = levl[(int)u.ux][ly-1].typ) && num != CORR; ly--) ;
83 	for(hy = u.uy; (num = levl[(int)u.ux][hy+1].typ) && num != CORR; hy++) ;
84 	num = 0;
85 	for(zy = ly; zy <= hy; zy++)
86 		for(zx = lx; zx <= hx; zx++) {
87 			if (levl[(int)zx][(int)zy].typ == SDOOR) {
88 				levl[(int)zx][(int)zy].typ = DOOR;
89 				atl(zx, zy, '+');
90 				num++;
91 			} else if (levl[(int)zx][(int)zy].typ == SCORR) {
92 				levl[(int)zx][(int)zy].typ = CORR;
93 				atl(zx, zy, CORR_SYM);
94 				num++;
95 			} else if ((ttmp = t_at(zx, zy))) {
96 				if(ttmp->ttyp == PIERC){
97 					(void) makemon(PM_PIERCER, zx, zy);
98 					num++;
99 					deltrap(ttmp);
100 				} else if (!ttmp->tseen) {
101 					ttmp->tseen = 1;
102 					if(!vism_at(zx, zy))
103 						atl(zx,zy,'^');
104 					num++;
105 				}
106 			} else if ((mtmp = m_at(zx,zy)) && (mtmp->mimic)) {
107 				seemimic(mtmp);
108 				num++;
109 			}
110 		}
111 	return(num);
112 }
113 
114 int
115 dosearch()
116 {
117 	xchar x,y;
118 	struct trap *trap;
119 	struct monst *mtmp;
120 
121 	if(u.uswallow)
122 		pline("What are you looking for? The exit?");
123 	else
124 	for(x = u.ux-1; x < u.ux+2; x++)
125 	for(y = u.uy-1; y < u.uy+2; y++) if(x != u.ux || y != u.uy) {
126 		if(levl[(int)x][(int)y].typ == SDOOR) {
127 			if(rn2(7)) continue;
128 			levl[(int)x][(int)y].typ = DOOR;
129 			levl[(int)x][(int)y].seen = 0;	/* force prl */
130 			prl(x,y);
131 			nomul(0);
132 		} else if(levl[(int)x][(int)y].typ == SCORR) {
133 			if(rn2(7)) continue;
134 			levl[(int)x][(int)y].typ = CORR;
135 			levl[(int)x][(int)y].seen = 0;	/* force prl */
136 			prl(x,y);
137 			nomul(0);
138 		} else {
139 		/* Be careful not to find anything in an SCORR or SDOOR */
140 			if ((mtmp = m_at(x,y)))
141 				if(mtmp->mimic){
142 					seemimic(mtmp);
143 					pline("You find a mimic.");
144 					return(1);
145 				}
146 			for(trap = ftrap; trap; trap = trap->ntrap)
147 			if(trap->tx == x && trap->ty == y &&
148 			   !trap->tseen && !rn2(8)) {
149 				nomul(0);
150 				pline("You find a%s.", traps[trap->ttyp]);
151 				if(trap->ttyp == PIERC) {
152 					deltrap(trap);
153 					(void) makemon(PM_PIERCER,x,y);
154 					return(1);
155 				}
156 				trap->tseen = 1;
157 				if(!vism_at(x,y)) atl(x,y,'^');
158 			}
159 		}
160 	}
161 	return(1);
162 }
163 
164 int
165 doidtrap()
166 {
167 	struct trap *trap;
168 	int x,y;
169 
170 	if(!getdir(1)) return(0);
171 	x = u.ux + u.dx;
172 	y = u.uy + u.dy;
173 	for(trap = ftrap; trap; trap = trap->ntrap)
174 		if(trap->tx == x && trap->ty == y && trap->tseen) {
175 		    if(u.dz)
176 			if((u.dz < 0) != (!xdnstair && trap->ttyp == TRAPDOOR))
177 			    continue;
178 		    pline("That is a%s.", traps[trap->ttyp]);
179 		    return(0);
180 		}
181 	pline("I can't see a trap there.");
182 	return(0);
183 }
184 
185 void
186 wakeup(struct monst *mtmp)
187 {
188 	mtmp->msleep = 0;
189 	setmangry(mtmp);
190 	if(mtmp->mimic) seemimic(mtmp);
191 }
192 
193 /* NOTE: we must check if(mtmp->mimic) before calling this routine */
194 void
195 seemimic(struct monst *mtmp)
196 {
197 		mtmp->mimic = 0;
198 		mtmp->mappearance = 0;
199 		unpmon(mtmp);
200 		pmon(mtmp);
201 }
202