1*86d7f5d3SJohn Marino /*-
2*86d7f5d3SJohn Marino * Copyright (c) 1983-2003, Regents of the University of California.
3*86d7f5d3SJohn Marino * All rights reserved.
4*86d7f5d3SJohn Marino *
5*86d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
6*86d7f5d3SJohn Marino * modification, are permitted provided that the following conditions are
7*86d7f5d3SJohn Marino * met:
8*86d7f5d3SJohn Marino *
9*86d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
10*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
11*86d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
12*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the
13*86d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution.
14*86d7f5d3SJohn Marino * 3. Neither the name of the University of California, San Francisco nor
15*86d7f5d3SJohn Marino * the names of its contributors may be used to endorse or promote
16*86d7f5d3SJohn Marino * products derived from this software without specific prior written
17*86d7f5d3SJohn Marino * permission.
18*86d7f5d3SJohn Marino *
19*86d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20*86d7f5d3SJohn Marino * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*86d7f5d3SJohn Marino * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22*86d7f5d3SJohn Marino * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23*86d7f5d3SJohn Marino * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24*86d7f5d3SJohn Marino * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25*86d7f5d3SJohn Marino * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26*86d7f5d3SJohn Marino * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27*86d7f5d3SJohn Marino * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28*86d7f5d3SJohn Marino * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29*86d7f5d3SJohn Marino * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30*86d7f5d3SJohn Marino *
31*86d7f5d3SJohn Marino * $OpenBSD: expl.c,v 1.9 2007/09/04 22:39:31 hshoexer Exp $
32*86d7f5d3SJohn Marino * $NetBSD: expl.c,v 1.2 1997/10/10 16:33:18 lukem Exp $
33*86d7f5d3SJohn Marino * $DragonFly: src/games/hunt/huntd/expl.c,v 1.2 2008/09/04 16:12:51 swildner Exp $
34*86d7f5d3SJohn Marino */
35*86d7f5d3SJohn Marino
36*86d7f5d3SJohn Marino #include <stdlib.h>
37*86d7f5d3SJohn Marino #include <syslog.h>
38*86d7f5d3SJohn Marino #include <string.h>
39*86d7f5d3SJohn Marino #include "hunt.h"
40*86d7f5d3SJohn Marino #include "server.h"
41*86d7f5d3SJohn Marino #include "conf.h"
42*86d7f5d3SJohn Marino
43*86d7f5d3SJohn Marino static void remove_wall(int, int);
44*86d7f5d3SJohn Marino static void init_removed(void);
45*86d7f5d3SJohn Marino
46*86d7f5d3SJohn Marino
47*86d7f5d3SJohn Marino /*
48*86d7f5d3SJohn Marino * showexpl:
49*86d7f5d3SJohn Marino * Show the explosions as they currently are
50*86d7f5d3SJohn Marino */
51*86d7f5d3SJohn Marino void
showexpl(int y,int x,char type)52*86d7f5d3SJohn Marino showexpl(int y, int x, char type)
53*86d7f5d3SJohn Marino {
54*86d7f5d3SJohn Marino PLAYER *pp;
55*86d7f5d3SJohn Marino EXPL *ep;
56*86d7f5d3SJohn Marino
57*86d7f5d3SJohn Marino if (y < 0 || y >= HEIGHT)
58*86d7f5d3SJohn Marino return;
59*86d7f5d3SJohn Marino if (x < 0 || x >= WIDTH)
60*86d7f5d3SJohn Marino return;
61*86d7f5d3SJohn Marino ep = (EXPL *) malloc(sizeof (EXPL)); /* NOSTRICT */
62*86d7f5d3SJohn Marino if (ep == NULL) {
63*86d7f5d3SJohn Marino logit(LOG_ERR, "malloc");
64*86d7f5d3SJohn Marino return;
65*86d7f5d3SJohn Marino }
66*86d7f5d3SJohn Marino ep->e_y = y;
67*86d7f5d3SJohn Marino ep->e_x = x;
68*86d7f5d3SJohn Marino ep->e_char = type;
69*86d7f5d3SJohn Marino ep->e_next = NULL;
70*86d7f5d3SJohn Marino if (Last_expl == NULL)
71*86d7f5d3SJohn Marino Expl[0] = ep;
72*86d7f5d3SJohn Marino else
73*86d7f5d3SJohn Marino Last_expl->e_next = ep;
74*86d7f5d3SJohn Marino Last_expl = ep;
75*86d7f5d3SJohn Marino for (pp = Player; pp < End_player; pp++) {
76*86d7f5d3SJohn Marino if (pp->p_maze[y][x] == type)
77*86d7f5d3SJohn Marino continue;
78*86d7f5d3SJohn Marino pp->p_maze[y][x] = type;
79*86d7f5d3SJohn Marino cgoto(pp, y, x);
80*86d7f5d3SJohn Marino outch(pp, type);
81*86d7f5d3SJohn Marino }
82*86d7f5d3SJohn Marino for (pp = Monitor; pp < End_monitor; pp++) {
83*86d7f5d3SJohn Marino if (pp->p_maze[y][x] == type)
84*86d7f5d3SJohn Marino continue;
85*86d7f5d3SJohn Marino pp->p_maze[y][x] = type;
86*86d7f5d3SJohn Marino cgoto(pp, y, x);
87*86d7f5d3SJohn Marino outch(pp, type);
88*86d7f5d3SJohn Marino }
89*86d7f5d3SJohn Marino switch (Maze[y][x]) {
90*86d7f5d3SJohn Marino case WALL1:
91*86d7f5d3SJohn Marino case WALL2:
92*86d7f5d3SJohn Marino case WALL3:
93*86d7f5d3SJohn Marino case DOOR:
94*86d7f5d3SJohn Marino case WALL4:
95*86d7f5d3SJohn Marino case WALL5:
96*86d7f5d3SJohn Marino if (y >= UBOUND && y < DBOUND && x >= LBOUND && x < RBOUND)
97*86d7f5d3SJohn Marino remove_wall(y, x);
98*86d7f5d3SJohn Marino break;
99*86d7f5d3SJohn Marino }
100*86d7f5d3SJohn Marino }
101*86d7f5d3SJohn Marino
102*86d7f5d3SJohn Marino /*
103*86d7f5d3SJohn Marino * rollexpl:
104*86d7f5d3SJohn Marino * Roll the explosions over, so the next one in the list is at the
105*86d7f5d3SJohn Marino * top
106*86d7f5d3SJohn Marino */
107*86d7f5d3SJohn Marino void
rollexpl(void)108*86d7f5d3SJohn Marino rollexpl(void)
109*86d7f5d3SJohn Marino {
110*86d7f5d3SJohn Marino EXPL *ep;
111*86d7f5d3SJohn Marino PLAYER *pp;
112*86d7f5d3SJohn Marino int y, x;
113*86d7f5d3SJohn Marino char c;
114*86d7f5d3SJohn Marino EXPL *nextep;
115*86d7f5d3SJohn Marino
116*86d7f5d3SJohn Marino for (ep = Expl[EXPLEN - 1]; ep != NULL; ep = nextep) {
117*86d7f5d3SJohn Marino nextep = ep->e_next;
118*86d7f5d3SJohn Marino y = ep->e_y;
119*86d7f5d3SJohn Marino x = ep->e_x;
120*86d7f5d3SJohn Marino if (y < UBOUND || y >= DBOUND || x < LBOUND || x >= RBOUND)
121*86d7f5d3SJohn Marino c = Maze[y][x];
122*86d7f5d3SJohn Marino else
123*86d7f5d3SJohn Marino c = SPACE;
124*86d7f5d3SJohn Marino for (pp = Player; pp < End_player; pp++)
125*86d7f5d3SJohn Marino if (pp->p_maze[y][x] == ep->e_char) {
126*86d7f5d3SJohn Marino pp->p_maze[y][x] = c;
127*86d7f5d3SJohn Marino cgoto(pp, y, x);
128*86d7f5d3SJohn Marino outch(pp, c);
129*86d7f5d3SJohn Marino }
130*86d7f5d3SJohn Marino for (pp = Monitor; pp < End_monitor; pp++)
131*86d7f5d3SJohn Marino check(pp, y, x);
132*86d7f5d3SJohn Marino free((char *) ep);
133*86d7f5d3SJohn Marino }
134*86d7f5d3SJohn Marino memmove(&Expl[1], &Expl[0], (EXPLEN - 1) * sizeof Expl[0]);
135*86d7f5d3SJohn Marino /* for (x = EXPLEN - 1; x > 0; x--)
136*86d7f5d3SJohn Marino Expl[x] = Expl[x - 1]; */
137*86d7f5d3SJohn Marino Last_expl = Expl[0] = NULL;
138*86d7f5d3SJohn Marino }
139*86d7f5d3SJohn Marino
140*86d7f5d3SJohn Marino int
can_rollexpl(void)141*86d7f5d3SJohn Marino can_rollexpl(void)
142*86d7f5d3SJohn Marino {
143*86d7f5d3SJohn Marino int i;
144*86d7f5d3SJohn Marino
145*86d7f5d3SJohn Marino for (i = EXPLEN - 1; i >= 0; i--)
146*86d7f5d3SJohn Marino if (Expl[i] != NULL)
147*86d7f5d3SJohn Marino return 1;
148*86d7f5d3SJohn Marino return 0;
149*86d7f5d3SJohn Marino }
150*86d7f5d3SJohn Marino
151*86d7f5d3SJohn Marino static REGEN *removed = NULL;
152*86d7f5d3SJohn Marino static REGEN *rem_index = NULL;
153*86d7f5d3SJohn Marino
154*86d7f5d3SJohn Marino static void
init_removed(void)155*86d7f5d3SJohn Marino init_removed(void)
156*86d7f5d3SJohn Marino {
157*86d7f5d3SJohn Marino rem_index = removed = calloc(conf_maxremove, sizeof(REGEN));
158*86d7f5d3SJohn Marino if (rem_index == NULL) {
159*86d7f5d3SJohn Marino logit(LOG_ERR, "malloc");
160*86d7f5d3SJohn Marino cleanup(1);
161*86d7f5d3SJohn Marino }
162*86d7f5d3SJohn Marino }
163*86d7f5d3SJohn Marino
164*86d7f5d3SJohn Marino /*
165*86d7f5d3SJohn Marino * remove_wall - add a location where the wall was blown away.
166*86d7f5d3SJohn Marino * if there is no space left over, put the a wall at
167*86d7f5d3SJohn Marino * the location currently pointed at.
168*86d7f5d3SJohn Marino */
169*86d7f5d3SJohn Marino static void
remove_wall(int y,int x)170*86d7f5d3SJohn Marino remove_wall(int y, int x)
171*86d7f5d3SJohn Marino {
172*86d7f5d3SJohn Marino REGEN *r;
173*86d7f5d3SJohn Marino PLAYER *pp;
174*86d7f5d3SJohn Marino char save_char = 0;
175*86d7f5d3SJohn Marino
176*86d7f5d3SJohn Marino if (removed == NULL)
177*86d7f5d3SJohn Marino clearwalls();
178*86d7f5d3SJohn Marino
179*86d7f5d3SJohn Marino r = rem_index;
180*86d7f5d3SJohn Marino while (r->r_y != 0) {
181*86d7f5d3SJohn Marino switch (Maze[r->r_y][r->r_x]) {
182*86d7f5d3SJohn Marino case SPACE:
183*86d7f5d3SJohn Marino case LEFTS:
184*86d7f5d3SJohn Marino case RIGHT:
185*86d7f5d3SJohn Marino case ABOVE:
186*86d7f5d3SJohn Marino case BELOW:
187*86d7f5d3SJohn Marino case FLYER:
188*86d7f5d3SJohn Marino save_char = Maze[r->r_y][r->r_x];
189*86d7f5d3SJohn Marino goto found;
190*86d7f5d3SJohn Marino }
191*86d7f5d3SJohn Marino if (++r >= removed + conf_maxremove)
192*86d7f5d3SJohn Marino r = removed;
193*86d7f5d3SJohn Marino }
194*86d7f5d3SJohn Marino
195*86d7f5d3SJohn Marino found:
196*86d7f5d3SJohn Marino if (r->r_y != 0) {
197*86d7f5d3SJohn Marino /* Slot being used, put back this wall */
198*86d7f5d3SJohn Marino if (save_char == SPACE)
199*86d7f5d3SJohn Marino Maze[r->r_y][r->r_x] = Orig_maze[r->r_y][r->r_x];
200*86d7f5d3SJohn Marino else {
201*86d7f5d3SJohn Marino /* We throw the player off the wall: */
202*86d7f5d3SJohn Marino pp = play_at(r->r_y, r->r_x);
203*86d7f5d3SJohn Marino if (pp->p_flying >= 0)
204*86d7f5d3SJohn Marino pp->p_flying += rand_num(conf_flytime / 2);
205*86d7f5d3SJohn Marino else {
206*86d7f5d3SJohn Marino pp->p_flying = rand_num(conf_flytime);
207*86d7f5d3SJohn Marino pp->p_flyx = 2 * rand_num(conf_flystep + 1) -
208*86d7f5d3SJohn Marino conf_flystep;
209*86d7f5d3SJohn Marino pp->p_flyy = 2 * rand_num(conf_flystep + 1) -
210*86d7f5d3SJohn Marino conf_flystep;
211*86d7f5d3SJohn Marino }
212*86d7f5d3SJohn Marino pp->p_over = Orig_maze[r->r_y][r->r_x];
213*86d7f5d3SJohn Marino pp->p_face = FLYER;
214*86d7f5d3SJohn Marino Maze[r->r_y][r->r_x] = FLYER;
215*86d7f5d3SJohn Marino showexpl(r->r_y, r->r_x, FLYER);
216*86d7f5d3SJohn Marino }
217*86d7f5d3SJohn Marino Maze[r->r_y][r->r_x] = Orig_maze[r->r_y][r->r_x];
218*86d7f5d3SJohn Marino if (conf_random && rand_num(100) < conf_prandom)
219*86d7f5d3SJohn Marino Maze[r->r_y][r->r_x] = DOOR;
220*86d7f5d3SJohn Marino if (conf_reflect && rand_num(100) == conf_preflect)
221*86d7f5d3SJohn Marino Maze[r->r_y][r->r_x] = WALL4;
222*86d7f5d3SJohn Marino for (pp = Monitor; pp < End_monitor; pp++)
223*86d7f5d3SJohn Marino check(pp, r->r_y, r->r_x);
224*86d7f5d3SJohn Marino }
225*86d7f5d3SJohn Marino
226*86d7f5d3SJohn Marino r->r_y = y;
227*86d7f5d3SJohn Marino r->r_x = x;
228*86d7f5d3SJohn Marino if (++r >= removed + conf_maxremove)
229*86d7f5d3SJohn Marino rem_index = removed;
230*86d7f5d3SJohn Marino else
231*86d7f5d3SJohn Marino rem_index = r;
232*86d7f5d3SJohn Marino
233*86d7f5d3SJohn Marino Maze[y][x] = SPACE;
234*86d7f5d3SJohn Marino for (pp = Monitor; pp < End_monitor; pp++)
235*86d7f5d3SJohn Marino check(pp, y, x);
236*86d7f5d3SJohn Marino }
237*86d7f5d3SJohn Marino
238*86d7f5d3SJohn Marino /*
239*86d7f5d3SJohn Marino * clearwalls:
240*86d7f5d3SJohn Marino * Clear out the walls array
241*86d7f5d3SJohn Marino */
242*86d7f5d3SJohn Marino void
clearwalls(void)243*86d7f5d3SJohn Marino clearwalls(void)
244*86d7f5d3SJohn Marino {
245*86d7f5d3SJohn Marino REGEN *rp;
246*86d7f5d3SJohn Marino
247*86d7f5d3SJohn Marino if (removed == NULL)
248*86d7f5d3SJohn Marino init_removed();
249*86d7f5d3SJohn Marino for (rp = removed; rp < removed + conf_maxremove; rp++)
250*86d7f5d3SJohn Marino rp->r_y = 0;
251*86d7f5d3SJohn Marino rem_index = removed;
252*86d7f5d3SJohn Marino }
253