2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * maps.c: Map routines
29 * Known contributors to this file:
32 * Markus Armbruster, 2004-2011
33 * Ron Koenderink, 2006
51 #include "prototypes.h"
56 static int draw_map(int, char, int, struct nstr_sect *);
57 static int bmnxtsct(struct nstr_sect *);
58 static char map_char(int, natid, int);
59 static int unit_map(int, int, struct nstr_sect *, char *);
62 do_map(int bmap, int unit_type, char *arg, char *map_flags_arg)
69 if (!snxtsct(&ns, arg)) {
70 if (unit_map(unit_type, atoi(arg), &ns, &origin))
73 for (b = map_flags_arg; b && *b; b++) {
77 map_flags |= MAP_SHIP;
81 map_flags |= MAP_LAND;
85 map_flags |= MAP_PLANE;
89 map_flags |= MAP_NUKE;
93 map_flags |= MAP_HIGH;
112 pr("Bad flag %c!\n", *b);
116 return draw_map(bmap, origin, map_flags, &ns);
120 draw_map(int bmap, char origin, int map_flags, struct nstr_sect *nsp)
125 union empobj_storage unit;
128 /* Note this is not re-entrant anyway, so we keep the buffers
130 static unsigned char *bitmap = NULL;
131 static char *wmapbuf = NULL;
132 static char **wmap = NULL;
133 static int ef_mappable[] = { EF_PLANE, EF_SHIP, EF_LAND, EF_NUKE, EF_BAD };
134 static int ef_unit_map[] = { MAP_PLANE, MAP_SHIP, MAP_LAND, MAP_NUKE };
138 wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
140 wmap = malloc(WORLD_Y * sizeof(char *));
141 if (wmap && wmapbuf) {
142 for (i = 0; i < WORLD_Y; i++)
143 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
150 bitmap = malloc((WORLD_SZ() + 7) / 8);
151 if (!wmapbuf || !wmap || !bitmap) {
152 pr("Memory error, tell the deity.\n");
153 logerror("malloc failed in draw_map\n");
158 if (!confirm("Are you sure you want to revert your bmap? "))
161 if (!(player->command->c_flags & C_MOD)) {
162 logerror("%s command needs C_MOD flag set",
163 player->command->c_form);
164 player->command->c_flags |= C_MOD;
166 np = getnatp(player->cnum);
167 /* zap any conditionals */
169 xyrelrange(np, &nsp->range, &range);
170 border(&range, " ", "");
171 blankfill(wmapbuf, &nsp->range, 1);
179 while (bmnxtsct(nsp) && !player->aborted) {
180 if (0 != (c = player->bmap[nsp->id]))
181 wmap[nsp->dy][nsp->dx] = c;
185 while (bmnxtsct(nsp) && !player->aborted) {
186 if (0 != (c = player->map[nsp->id]))
187 wmap[nsp->dy][nsp->dx] = c;
191 while (bmnxtsct(nsp) && !player->aborted) {
192 player->bmap[nsp->id] =
193 player->map[nsp->id];
194 if (0 != (c = player->bmap[nsp->id]))
195 wmap[nsp->dy][nsp->dx] = c;
197 ef_write(EF_BMAP, player->cnum, player->bmap);
204 memset(bitmap, 0, (WORLD_SZ() + 7) / 8);
205 bitinit2(nsp, bitmap, player->cnum);
207 while (nxtsct(nsp, §) && !player->aborted) {
208 if (!player->god && !emp_getbit(nsp->x, nsp->y, bitmap))
210 wmap[nsp->dy][nsp->dx]
211 = map_char(sect.sct_newtype, sect.sct_own,
223 memset(bitmap, 0, (WORLD_SZ() + 7) / 8);
224 bitinit2(nsp, bitmap, player->cnum);
226 while (nxtsct(nsp, §) && !player->aborted) {
227 if (!player->god && !emp_getbit(nsp->x, nsp->y, bitmap))
229 mapch = map_char(sect.sct_type, sect.sct_own, player->owner);
230 wmap[nsp->dy][nsp->dx] = mapch;
231 changed |= map_set(player->cnum, nsp->x, nsp->y, mapch, 0);
234 writemap(player->cnum);
240 while (ef_mappable[i] != EF_BAD) {
241 if (map_flags & ef_unit_map[i]) {
242 snxtitem_area(&ni, ef_mappable[i], &nsp->range);
243 while (nxtitem(&ni, &unit)) {
244 if (unit.gen.own == 0)
246 if (unit.gen.own != player->cnum && !player->god)
249 x = deltx(&nsp->range, unit.gen.x);
250 y = delty(&nsp->range, unit.gen.y);
252 if (ef_mappable[i] == EF_NUKE)
255 name = empobj_chr_name(&unit.gen);
256 wmap[y][x] = *name & ~0x20;
262 if (map_flags & MAP_HIGH) {
266 while (nxtsct(nsp, §) && !player->aborted) {
267 if (sect.sct_own == player->cnum)
268 wmap[nsp->dy][nsp->dx] |= 0x80;
272 wmap[5][10] = origin & ~0x20;
273 for (y = nsp->range.ly, i = 0; i < nsp->range.height; y++, i++) {
277 wmap[i][nsp->range.width] = '\0';
278 pr("%4d %s %-4d\n", yval, wmap[i], yval);
282 border(&range, " ", "");
287 * get the next sector in the range
290 bmnxtsct(struct nstr_sect *np)
295 if (np->x >= WORLD_X)
297 if (np->dx >= np->range.width) {
299 np->x = np->range.lx;
301 if (np->dy >= np->range.height)
304 if (np->y >= WORLD_Y)
307 if ((np->y + np->x) & 01)
309 if (np->type == NS_DIST) {
310 np->curdist = mapdist(np->x, np->y, np->cx, np->cy);
311 if (np->curdist > np->dist)
314 np->id = sctoff(np->x, np->y);
321 * Return character to use in maps for sector type TYPE owned by OWN.
322 * If OWNER_OR_GOD, the map is for the sector's owner or a deity.
325 map_char(int type, natid own, int owner_or_god)
327 if (CANT_HAPPEN(type > SCT_TYPE_MAX || !dchr[type].d_mnem))
330 || type == SCT_WATER || type == SCT_MOUNT || type == SCT_WASTE
331 || (!own && (type == SCT_RURAL || type == SCT_PLAINS)))
332 return dchr[type].d_mnem;
337 unit_map(int unit_type, int uid, struct nstr_sect *nsp, char *originp)
339 union empobj_storage unit;
343 if (CANT_HAPPEN((ef_flags(unit_type) & (EFF_OWNER | EFF_XY))
344 != (EFF_OWNER | EFF_XY)))
347 if (!get_empobj(unit_type, uid, &unit))
349 if (!player->owner || unit.gen.own == 0)
352 if (unit_type == EF_NUKE)
355 name = empobj_chr_name(&unit.gen);
359 range.lx = xnorm(unit.gen.x - 10);
360 range.hx = xnorm(unit.gen.x + 10);
361 range.ly = ynorm(unit.gen.y - 5);
362 range.hy = ynorm(unit.gen.y + 5);
363 xysize_range(&range);
364 snxtsct_area(nsp, &range);
369 display_region_map(int bmap, int unit_type, coord curx, coord cury,
372 char coordinates[80];
378 np = getnatp(player->cnum);
379 sprintf(coordinates, "%d:%d,%d:%d",
380 xrel(np, curx - 10), xrel(np, curx + 10),
381 yrel(np, cury - 5), yrel(np, cury + 5));
385 map_flag_arg = strchr(arg, ' ');
386 if (map_flag_arg != NULL) {
387 *map_flag_arg++ = '\0';
388 while (isspace(*map_flag_arg)) map_flag_arg++;
391 player->condarg = NULL;
392 return do_map(bmap, unit_type, arg, map_flag_arg);
396 bmaps_intersect(natid a, natid b)
398 char *mapa = ef_ptr(EF_MAP, a);
399 char *mapb = ef_ptr(EF_MAP, b);
402 for (i = 0; i < WORLD_SZ(); i++)
403 if (mapa[i] && mapa[i] != ' ' && mapb[i] && mapb[i] != ' ')
408 /* Note that this requires that the BMAP is mapped into memory */
411 share_bmap(natid from, natid to, struct nstr_sect *ns, char des,
414 char *from_bmap = ef_ptr(EF_BMAP, from);
415 char *to_bmap = ef_ptr(EF_BMAP, to);
420 char from_des = *from_name;
425 if (isalpha(from_des))
428 while (nxtsct(ns, §)) {
429 if (!(fromdes = from_bmap[sect.sct_uid]))
431 todes = to_bmap[sect.sct_uid];
434 todes != '.' && todes != ' ' && todes != from_des)
436 if (sect.sct_own == from) {
437 if (fromdes != '=' && fromdes != 'h' && fromdes != des)
440 if (todes == fromdes)
442 n += map_set(to, ns->x, ns->y, fromdes, 1);