2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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 files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * maps.c: Map routines
30 * Known contributors to this file:
50 #include "prototypes.h"
55 static int bmnxtsct(struct nstr_sect *);
56 static char map_char(unsigned char type, natid own, int owner_or_god);
59 draw_map(int bmap, char origin, int map_flags, struct nstr_sect *nsp)
70 /* Note this is not re-entrant anyway, so we keep the buffers
72 static unsigned char *bitmap = NULL;
73 static char *wmapbuf = NULL;
74 static char **wmap = NULL;
77 wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
79 wmap = malloc(WORLD_Y * sizeof(char *));
80 if (wmap && wmapbuf) {
81 for (i = 0; i < WORLD_Y; i++)
82 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
89 bitmap = malloc((WORLD_X * WORLD_Y) / 8);
90 if (!wmapbuf || !wmap || !bitmap) {
91 pr("Memory error, tell the deity.\n");
92 logerror("malloc failed in draw_map\n");
97 if (!confirm("Are you sure you want to revert your bmap? "))
100 if (!(player->command->c_flags & C_MOD)) {
101 logerror("%s command needs C_MOD flag set",
102 player->command->c_form);
103 player->command->c_flags |= C_MOD;
105 np = getnatp(player->cnum);
106 /* zap any conditionals */
108 xyrelrange(np, &nsp->range, &range);
109 border(&range, " ", "");
110 blankfill(wmapbuf, &nsp->range, 1);
119 while (bmnxtsct(nsp) && !player->aborted) {
120 if (0 != (c = player->bmap[sctoff(nsp->x, nsp->y)]))
121 wmap[nsp->dy][nsp->dx] = c;
125 while (bmnxtsct(nsp) && !player->aborted) {
126 if (0 != (c = player->map[sctoff(nsp->x, nsp->y)]))
127 wmap[nsp->dy][nsp->dx] = c;
131 while (bmnxtsct(nsp) && !player->aborted) {
132 player->bmap[sctoff(nsp->x, nsp->y)] =
133 player->map[sctoff(nsp->x, nsp->y)];
134 if (0 != (c = player->bmap[sctoff(nsp->x, nsp->y)]))
135 wmap[nsp->dy][nsp->dx] = c;
137 ef_write(EF_BMAP, player->cnum, player->bmap);
144 memset(bitmap, 0, (WORLD_X * WORLD_Y) / 8);
145 bitinit2(nsp, bitmap, player->cnum);
147 while (nxtsct(nsp, §) && !player->aborted) {
148 if (!player->god && !emp_getbit(nsp->x, nsp->y, bitmap))
150 wmap[nsp->dy][nsp->dx]
151 = map_char(sect.sct_newtype, sect.sct_own,
163 memset(bitmap, 0, (WORLD_X * WORLD_Y) / 8);
164 bitinit2(nsp, bitmap, player->cnum);
166 while (nxtsct(nsp, §) && !player->aborted) {
167 if (!player->god && !emp_getbit(nsp->x, nsp->y, bitmap))
169 mapch = map_char(sect.sct_type, sect.sct_own, player->owner);
170 wmap[nsp->dy][nsp->dx] = mapch;
171 changed |= map_set(player->cnum, nsp->x, nsp->y, mapch, 0);
174 writemap(player->cnum);
178 if (map_flags & MAP_PLANE) {
179 snxtitem_all(&ni, EF_PLANE);
180 while (nxtitem(&ni, &plane)) {
181 if (plane.pln_own == 0)
183 if (plane.pln_own != player->cnum && !player->god)
185 if (!xyinrange(plane.pln_x, plane.pln_y, &nsp->range))
188 x = xnorm(plane.pln_x - nsp->range.lx);
189 y = ynorm(plane.pln_y - nsp->range.ly);
190 wmap[y][x] = (*plchr[(int)plane.pln_type].pl_name) & ~0x20;
193 if (map_flags & MAP_SHIP) {
194 snxtitem_all(&ni, EF_SHIP);
195 while (nxtitem(&ni, &ship)) {
196 if (ship.shp_own == 0)
198 if (ship.shp_own != player->cnum && !player->god)
200 if (!xyinrange(ship.shp_x, ship.shp_y, &nsp->range))
203 x = xnorm(ship.shp_x - nsp->range.lx);
204 y = ynorm(ship.shp_y - nsp->range.ly);
205 wmap[y][x] = (*mchr[(int)ship.shp_type].m_name) & ~0x20;
208 if (map_flags & MAP_LAND) {
209 snxtitem_all(&ni, EF_LAND);
210 while (nxtitem(&ni, &land)) {
211 if (land.lnd_own == 0)
213 if (land.lnd_own != player->cnum && !player->god)
215 if (!xyinrange(land.lnd_x, land.lnd_y, &nsp->range))
218 x = xnorm(land.lnd_x - nsp->range.lx);
219 y = ynorm(land.lnd_y - nsp->range.ly);
220 wmap[y][x] = (*lchr[(int)land.lnd_type].l_name) & ~0x20;
223 if (map_flags & MAP_NUKE) {
224 snxtitem_all(&ni, EF_NUKE);
225 while (nxtitem(&ni, &nuke)) {
226 if (nuke.nuk_own == 0)
228 if (nuke.nuk_own != player->cnum && !player->god)
230 if (!xyinrange(nuke.nuk_x, nuke.nuk_y, &nsp->range))
233 x = xnorm(nuke.nuk_x - nsp->range.lx);
234 y = ynorm(nuke.nuk_y - nsp->range.ly);
238 if (map_flags & MAP_HIGH) {
244 memset(bitmap, 0, (WORLD_X * WORLD_Y) / 8);
245 bitinit2(nsp, bitmap, player->cnum);
247 while (nxtsct(nsp, §) && !player->aborted) {
248 if (!player->god && !emp_getbit(nsp->x, nsp->y, bitmap))
250 ptr = &wmap[nsp->dy][nsp->dx];
251 if (sect.sct_own == player->cnum)
256 wmap[5][10] = origin & ~0x20;
257 for (y = nsp->range.ly, i = 0; i < nsp->range.height; y++, i++) {
261 wmap[i][nsp->range.width] = '\0';
262 pr("%4d %s %-4d\n", yval, wmap[i], yval);
266 border(&range, " ", "");
271 * get the next sector in the range
274 bmnxtsct(struct nstr_sect *np)
279 if (np->x >= WORLD_X)
281 if (np->dx >= np->range.width) {
283 np->x = np->range.lx;
285 if (np->dy >= np->range.height)
288 if (np->y >= WORLD_Y)
291 if ((np->y + np->x) & 01)
293 if (np->type == NS_DIST) {
294 np->curdist = mapdist(np->x, np->y, np->cx, np->cy);
295 if (np->curdist > np->dist)
298 np->id = sctoff(np->x, np->y);
305 * Return character to use in maps for sector type TYPE owned by OWN.
306 * If OWNER_OR_GOD, the map is for the sector's owner or a deity.
309 map_char(unsigned char type, natid own, int owner_or_god)
311 if (CANT_HAPPEN(type > SCT_TYPE_MAX || !dchr[type].d_mnem))
314 || type == SCT_WATER || type == SCT_MOUNT || type == SCT_WASTE
315 || (!own && (type == SCT_RURAL || type == SCT_PLAINS)))
316 return dchr[type].d_mnem;
321 unit_map(int unit_type, int uid, struct nstr_sect *nsp, char *originp)
330 if (unit_type == EF_LAND) {
331 if (!getland(uid, &origl) || !player->owner || origl.lnd_own == 0)
333 gp = (struct empobj *)&origl;
334 *originp = *lchr[(int)origl.lnd_type].l_name;
335 } else if (unit_type == EF_PLANE) {
336 if (!getplane(uid, &origp) || !player->owner || origp.pln_own == 0)
338 gp = (struct empobj *)&origp;
339 *originp = *plchr[(int)origp.pln_type].pl_name;
340 } else if (unit_type == EF_NUKE) {
341 if (!getnuke(uid, &orign) || !player->owner || orign.nuk_own == 0)
343 gp = (struct empobj *)&orign;
346 if (!getship(uid, &origs) || !player->owner || origs.shp_own == 0)
348 gp = (struct empobj *)&origs;
349 *originp = *mchr[(int)origs.shp_type].m_name;
352 range.lx = xnorm(gp->x - 10);
353 range.hx = xnorm(gp->x + 11);
354 range.ly = ynorm(gp->y - 5);
355 range.hy = ynorm(gp->y + 6);
356 xysize_range(&range);
357 snxtsct_area(nsp, &range);
362 display_region_map(char *cmd, coord curx, coord cury, char *arg)
364 char coordinates[80], *cp;
366 player->argp[0] = cmd;
370 np = getnatp(player->cnum);
371 sprintf(coordinates, "%d:%d,%d:%d",
372 xrel(np, curx - 10), xrel(np, curx + 11),
373 yrel(np, cury - 5), yrel(np, cury + 6));
374 player->argp[1] = coordinates;
375 player->argp[2] = NULL;
377 player->argp[1] = arg;
378 cp = strchr(arg, ' ');
381 while (isspace(*cp)) cp++;
382 player->argp[2] = cp;
384 player->argp[2] = NULL;
386 player->condarg = NULL;
391 bmaps_intersect(natid a, natid b)
393 char *mapa = ef_ptr(EF_MAP, a);
394 char *mapb = ef_ptr(EF_MAP, b);
397 for (i = 0; i < WORLD_X * WORLD_Y / 2; ++i, ++mapa, ++mapb)
398 if (*mapa && *mapa != ' ' && *mapb && *mapb != ' ')
403 /* Note that this requires that the BMAP is mapped into memory */
406 share_bmap(natid from, natid to, struct nstr_sect *ns, char des,
409 char *from_bmap = ef_ptr(EF_BMAP, from);
410 char *to_bmap = ef_ptr(EF_BMAP, to);
415 char from_des = *from_name;
417 if (isalpha(from_des))
420 while (nxtsct(ns, §)) {
421 if (!(fromdes = from_bmap[sctoff(ns->x, ns->y)]))
423 todes = to_bmap[sctoff(ns->x, ns->y)];
426 todes != '.' && todes != ' ' && todes != from_des)
428 if (sect.sct_own == from) {
429 if (fromdes != '=' && fromdes != 'h' && fromdes != des)
432 if (todes == fromdes)
434 n += map_set(to, ns->x, ns->y, fromdes, 1);