2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * maps.c: Map routines
30 * Known contributors to this file:
54 draw_map(int bmap, s_char origin, int map_flags, struct nstr_sect *nsp,
65 /* Note this is not re-entrant anyway, so we keep the buffers
67 static u_char *bitmap = (u_char *)0;
68 static s_char *wmapbuf = (s_char *)0;
69 static s_char **wmap = (s_char **)0;
73 (s_char *)malloc((WORLD_Y * MAPWIDTH(1)) * sizeof(s_char));
75 wmap = (s_char **)malloc(WORLD_Y * sizeof(s_char *));
76 if (wmap && wmapbuf) {
77 for (i = 0; i < WORLD_Y; i++)
78 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
85 bitmap = (u_char *)malloc((WORLD_X * WORLD_Y) / 8);
86 if (!wmapbuf || !wmap || !bitmap) {
87 pr("Memory error, tell the deity.\n");
88 logerror("malloc failed in draw_map\n");
92 if (bmap == EF_MAP + EF_BMAP) {
93 if (!confirm("Are you sure you want to revert your bmap? "))
96 if (!player->command->c_flags & C_MOD) {
97 logerror("%s command needs C_MOD flag set",
98 player->command->c_form);
99 player->command->c_flags |= C_MOD;
101 np = getnatp(country);
102 /* zap any conditionals */
104 xyrelrange(np, &nsp->range, &range);
105 border(&range, " ", "");
106 blankfill((s_char *)wmapbuf, &nsp->range, 1);
111 while (bmnxtsct(nsp) && !player->aborted) {
112 if (0 != (c = player->bmap[sctoff(nsp->x, nsp->y)]))
113 wmap[nsp->dy][nsp->dx] = c;
117 while (bmnxtsct(nsp) && !player->aborted) {
118 if (0 != (c = player->map[sctoff(nsp->x, nsp->y)]))
119 wmap[nsp->dy][nsp->dx] = c;
122 case (EF_MAP + EF_BMAP):
123 while (bmnxtsct(nsp) && !player->aborted) {
124 player->bmap[sctoff(nsp->x, nsp->y)] =
125 player->map[sctoff(nsp->x, nsp->y)];
126 if (0 != (c = player->bmap[sctoff(nsp->x, nsp->y)]))
127 wmap[nsp->dy][nsp->dx] = c;
129 ef_write(EF_BMAP, player->cnum, player->bmap);
133 register s_char *ptr;
136 if ((!player->god || country)) {
137 bzero((s_char *)bitmap, (WORLD_X * WORLD_Y) / 8);
138 bitinit2(nsp, bitmap, country);
140 while (nxtsct(nsp, §) && !player->aborted) {
141 if ((!player->god || country) &&
142 !emp_getbit(nsp->x, nsp->y, bitmap)) {
146 ptr = &wmap[nsp->dy][nsp->dx];
147 if (sect.sct_newtype > SCT_MAXDEF) {
150 *ptr = dchr[sect.sct_newtype].d_mnem;
151 switch (sect.sct_newtype) {
159 if (sect.sct_own != country &&
160 (!player->god || country)) {
172 register s_char *ptr;
176 if ((!player->god || country)) {
177 bzero((s_char *)bitmap, (WORLD_X * WORLD_Y) / 8);
178 bitinit2(nsp, bitmap, country);
180 while (nxtsct(nsp, §) && !player->aborted) {
181 if ((!player->god || country)
182 && !emp_getbit(nsp->x, nsp->y, bitmap)) {
186 ptr = &wmap[nsp->dy][nsp->dx];
187 if (sect.sct_type > SCT_MAXDEF) {
190 *ptr = dchr[sect.sct_type].d_mnem;
191 switch (sect.sct_type) {
199 if (sect.sct_own != country &&
200 (!player->god || country)) {
206 changed += map_set(player->cnum, nsp->x, nsp->y, *ptr, 0);
210 writemap(player->cnum);
214 if (map_flags & MAP_PLANE) {
215 snxtitem_all(&ni, EF_PLANE);
216 while (nxtitem(&ni, (caddr_t)&plane)) {
217 if (plane.pln_own == 0)
219 if (plane.pln_own != player->cnum && !player->god)
221 if (!xyinrange(plane.pln_x, plane.pln_y, &nsp->range))
224 x = xnorm(plane.pln_x - nsp->range.lx);
225 y = ynorm(plane.pln_y - nsp->range.ly);
226 wmap[y][x] = (*plchr[(int)plane.pln_type].pl_name) & ~0x20;
229 if (map_flags & MAP_SHIP) {
230 snxtitem_all(&ni, EF_SHIP);
231 while (nxtitem(&ni, (caddr_t)&ship)) {
232 if (ship.shp_own == 0)
234 if (ship.shp_own != player->cnum && !player->god)
236 if (!xyinrange(ship.shp_x, ship.shp_y, &nsp->range))
239 x = xnorm(ship.shp_x - nsp->range.lx);
240 y = ynorm(ship.shp_y - nsp->range.ly);
241 wmap[y][x] = (*mchr[(int)ship.shp_type].m_name) & ~0x20;
244 if (map_flags & MAP_LAND) {
245 snxtitem_all(&ni, EF_LAND);
246 while (nxtitem(&ni, (caddr_t)&land)) {
247 if (land.lnd_own == 0)
249 if (land.lnd_own != player->cnum && !player->god)
251 if (!xyinrange(land.lnd_x, land.lnd_y, &nsp->range))
254 x = xnorm(land.lnd_x - nsp->range.lx);
255 y = ynorm(land.lnd_y - nsp->range.ly);
256 wmap[y][x] = (*lchr[(int)land.lnd_type].l_name) & ~0x20;
260 wmap[5][10] = origin & ~0x20;
261 for (y = nsp->range.ly, i = 0; i < nsp->range.height; y++, i++) {
265 wmap[i][nsp->range.width] = '\0';
266 pr("%4d %s %-4d\n", yval, wmap[i], yval);
270 border(&range, " ", "");
275 * get the next sector in the range
278 bmnxtsct(register struct nstr_sect *np)
283 if (np->x >= WORLD_X)
285 if (np->dx >= np->range.width) {
287 np->x = np->range.lx;
289 if (np->dy >= np->range.height)
292 if (np->y >= WORLD_Y)
295 if ((np->y + np->x) & 01)
297 if (np->type == NS_DIST) {
298 np->curdist = mapdist(np->x, np->y, np->cx, np->cy);
299 if (np->curdist > np->dist)
302 np->id = sctoff(np->x, np->y);
309 bitinit2(struct nstr_sect *np, u_char *bitmap, int country)
311 extern int *bitmaps[];
315 while (nxtsct(np, §)) {
316 if (sect.sct_own != country)
318 eff = sect.sct_effic / 20;
321 emp_setbitmap(np->x, np->y, bitmap, bitmaps[eff]);
327 unit_map(int unit_type, int i, struct nstr_sect *nsp, s_char *originp)
335 np = getnatp(player->cnum);
336 if (unit_type == EF_LAND) {
337 if (!getland(i, &origl) ||
338 (origl.lnd_own != player->cnum && !player->god) ||
339 (origl.lnd_own == 0))
341 sprintf(what, "%d:%d,%d:%d", xrel(np, origl.lnd_x - 10),
342 xrel(np, origl.lnd_x + 10),
343 yrel(np, origl.lnd_y - 5), yrel(np, origl.lnd_y + 5));
344 *originp = *lchr[(int)origl.lnd_type].l_name;
345 } else if (unit_type == EF_PLANE) {
346 if (!getplane(i, &origp) ||
347 (origp.pln_own != player->cnum && !player->god) ||
348 (origp.pln_own == 0))
350 sprintf(what, "%d:%d,%d:%d", xrel(np, origp.pln_x - 10),
351 xrel(np, origp.pln_x + 10),
352 yrel(np, origp.pln_y - 5), yrel(np, origp.pln_y + 5));
353 *originp = *plchr[(int)origp.pln_type].pl_name;
355 if (!getship(i, &origs) ||
356 (origs.shp_own != player->cnum && !player->god) ||
357 (origs.shp_own == 0))
359 sprintf(what, "%d:%d,%d:%d", xrel(np, origs.shp_x - 10),
360 xrel(np, origs.shp_x + 10),
361 yrel(np, origs.shp_y - 5), yrel(np, origs.shp_y + 5));
363 *originp = *mchr[(int)origs.shp_type].m_name;
365 if (!snxtsct(nsp, what))
371 bmaps_intersect(natid a, natid b)
373 s_char *mapa = ef_ptr(EF_MAP, a);
374 s_char *mapb = ef_ptr(EF_MAP, b);
377 for (i = 0; i < WORLD_X * WORLD_Y / 2; ++i, ++mapa, ++mapb)
378 if (*mapa && *mapa != ' ' && *mapb && *mapb != ' ')
383 /* Note that this requires that the BMAP is mapped into memory */
386 share_bmap(natid from, natid to, struct nstr_sect *ns, s_char des,
389 s_char *from_bmap = ef_ptr(EF_BMAP, from);
390 s_char *to_bmap = ef_ptr(EF_BMAP, to);
395 s_char from_des = *from_name;
397 if (isalpha(from_des))
400 while (nxtsct(ns, §)) {
401 if (!(fromdes = from_bmap[sctoff(ns->x, ns->y)]))
403 todes = to_bmap[sctoff(ns->x, ns->y)];
406 todes != '.' && todes != ' ' && todes != from_des)
408 if (sect.sct_own == from) {
409 if (fromdes != '=' && fromdes != 'h' && fromdes != des)
412 if (todes == fromdes)
414 n += map_set(to, ns->x, ns->y, fromdes, 1);