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