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