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