2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, 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:
33 * Markus Armbruster, 2004-2008
34 * Ron Koenderink, 2006
52 #include "prototypes.h"
57 static int draw_map(int, char, int, struct nstr_sect *);
58 static int bmnxtsct(struct nstr_sect *);
59 static char map_char(int, natid, int);
60 static int unit_map(int, int, struct nstr_sect *, char *);
63 do_map(int bmap, int unit_type, char *arg, char *map_flags_arg)
70 if (!snxtsct(&ns, arg)) {
71 if (unit_map(unit_type, atoi(arg), &ns, &origin))
74 for (b = map_flags_arg; b && *b; b++) {
78 map_flags |= MAP_SHIP;
82 map_flags |= MAP_LAND;
86 map_flags |= MAP_PLANE;
90 map_flags |= MAP_NUKE;
94 map_flags |= MAP_HIGH;
113 pr("Bad flag %c!\n", *b);
117 return draw_map(bmap, origin, map_flags, &ns);
121 draw_map(int bmap, char origin, int map_flags, struct nstr_sect *nsp)
126 union empobj_storage unit;
129 /* Note this is not re-entrant anyway, so we keep the buffers
131 static unsigned char *bitmap = NULL;
132 static char *wmapbuf = NULL;
133 static char **wmap = NULL;
134 static int ef_mappable[] = { EF_PLANE, EF_SHIP, EF_LAND, EF_NUKE, EF_BAD };
135 static int ef_unit_map[] = { MAP_PLANE, MAP_SHIP, MAP_LAND, MAP_NUKE };
139 wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
141 wmap = malloc(WORLD_Y * sizeof(char *));
142 if (wmap && wmapbuf) {
143 for (i = 0; i < WORLD_Y; i++)
144 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
151 bitmap = malloc(WORLD_SZ() / 8);
152 if (!wmapbuf || !wmap || !bitmap) {
153 pr("Memory error, tell the deity.\n");
154 logerror("malloc failed in draw_map\n");
159 if (!confirm("Are you sure you want to revert your bmap? "))
162 if (!(player->command->c_flags & C_MOD)) {
163 logerror("%s command needs C_MOD flag set",
164 player->command->c_form);
165 player->command->c_flags |= C_MOD;
167 np = getnatp(player->cnum);
168 /* zap any conditionals */
170 xyrelrange(np, &nsp->range, &range);
171 border(&range, " ", "");
172 blankfill(wmapbuf, &nsp->range, 1);
181 while (bmnxtsct(nsp) && !player->aborted) {
182 if (0 != (c = player->bmap[nsp->id]))
183 wmap[nsp->dy][nsp->dx] = c;
187 while (bmnxtsct(nsp) && !player->aborted) {
188 if (0 != (c = player->map[nsp->id]))
189 wmap[nsp->dy][nsp->dx] = c;
193 while (bmnxtsct(nsp) && !player->aborted) {
194 player->bmap[nsp->id] =
195 player->map[nsp->id];
196 if (0 != (c = player->bmap[nsp->id]))
197 wmap[nsp->dy][nsp->dx] = c;
199 ef_write(EF_BMAP, player->cnum, player->bmap);
206 memset(bitmap, 0, WORLD_SZ() / 8);
207 bitinit2(nsp, bitmap, player->cnum);
209 while (nxtsct(nsp, §) && !player->aborted) {
210 if (!player->god && !emp_getbit(nsp->x, nsp->y, bitmap))
212 wmap[nsp->dy][nsp->dx]
213 = map_char(sect.sct_newtype, sect.sct_own,
225 memset(bitmap, 0, WORLD_SZ() / 8);
226 bitinit2(nsp, bitmap, player->cnum);
228 while (nxtsct(nsp, §) && !player->aborted) {
229 if (!player->god && !emp_getbit(nsp->x, nsp->y, bitmap))
231 mapch = map_char(sect.sct_type, sect.sct_own, player->owner);
232 wmap[nsp->dy][nsp->dx] = mapch;
233 changed |= map_set(player->cnum, nsp->x, nsp->y, mapch, 0);
236 writemap(player->cnum);
242 while (ef_mappable[i] != EF_BAD) {
243 if (map_flags & ef_unit_map[i]) {
244 snxtitem_area(&ni, ef_mappable[i], &nsp->range);
245 while (nxtitem(&ni, &unit)) {
246 if (unit.gen.own == 0)
248 if (unit.gen.own != player->cnum && !player->god)
251 x = deltx(&nsp->range, unit.gen.x);
252 y = delty(&nsp->range, unit.gen.y);
254 if (ef_mappable[i] == EF_NUKE)
257 name = empobj_chr_name(&unit.gen);
258 wmap[y][x] = *name & ~0x20;
264 if (map_flags & MAP_HIGH) {
268 while (nxtsct(nsp, §) && !player->aborted) {
269 if (sect.sct_own == player->cnum)
270 wmap[nsp->dy][nsp->dx] |= 0x80;
274 wmap[5][10] = origin & ~0x20;
275 for (y = nsp->range.ly, i = 0; i < nsp->range.height; y++, i++) {
279 wmap[i][nsp->range.width] = '\0';
280 pr("%4d %s %-4d\n", yval, wmap[i], yval);
284 border(&range, " ", "");
289 * get the next sector in the range
292 bmnxtsct(struct nstr_sect *np)
297 if (np->x >= WORLD_X)
299 if (np->dx >= np->range.width) {
301 np->x = np->range.lx;
303 if (np->dy >= np->range.height)
306 if (np->y >= WORLD_Y)
309 if ((np->y + np->x) & 01)
311 if (np->type == NS_DIST) {
312 np->curdist = mapdist(np->x, np->y, np->cx, np->cy);
313 if (np->curdist > np->dist)
316 np->id = sctoff(np->x, np->y);
323 * Return character to use in maps for sector type TYPE owned by OWN.
324 * If OWNER_OR_GOD, the map is for the sector's owner or a deity.
327 map_char(int type, natid own, int owner_or_god)
329 if (CANT_HAPPEN(type > SCT_TYPE_MAX || !dchr[type].d_mnem))
332 || type == SCT_WATER || type == SCT_MOUNT || type == SCT_WASTE
333 || (!own && (type == SCT_RURAL || type == SCT_PLAINS)))
334 return dchr[type].d_mnem;
339 unit_map(int unit_type, int uid, struct nstr_sect *nsp, char *originp)
341 union empobj_storage unit;
345 if (CANT_HAPPEN((ef_flags(unit_type) & (EFF_OWNER | EFF_XY))
346 != (EFF_OWNER | EFF_XY)))
349 if (!get_empobj(unit_type, uid, &unit))
351 if (!player->owner || unit.gen.own == 0)
354 if (unit_type == EF_NUKE)
357 name = empobj_chr_name(&unit.gen);
361 range.lx = xnorm(unit.gen.x - 10);
362 range.hx = xnorm(unit.gen.x + 10);
363 range.ly = ynorm(unit.gen.y - 5);
364 range.hy = ynorm(unit.gen.y + 5);
365 xysize_range(&range);
366 snxtsct_area(nsp, &range);
371 display_region_map(int bmap, int unit_type, coord curx, coord cury,
374 char coordinates[80];
380 np = getnatp(player->cnum);
381 sprintf(coordinates, "%d:%d,%d:%d",
382 xrel(np, curx - 10), xrel(np, curx + 10),
383 yrel(np, cury - 5), yrel(np, cury + 5));
387 map_flag_arg = strchr(arg, ' ');
388 if (map_flag_arg != NULL) {
389 *map_flag_arg++ = '\0';
390 while (isspace(*map_flag_arg)) map_flag_arg++;
393 player->condarg = NULL;
394 return do_map(bmap, unit_type, arg, map_flag_arg);
398 bmaps_intersect(natid a, natid b)
400 char *mapa = ef_ptr(EF_MAP, a);
401 char *mapb = ef_ptr(EF_MAP, b);
404 for (i = 0; i < WORLD_SZ(); i++)
405 if (mapa[i] && mapa[i] != ' ' && mapb[i] && mapb[i] != ' ')
410 /* Note that this requires that the BMAP is mapped into memory */
413 share_bmap(natid from, natid to, struct nstr_sect *ns, char des,
416 char *from_bmap = ef_ptr(EF_BMAP, from);
417 char *to_bmap = ef_ptr(EF_BMAP, to);
422 char from_des = *from_name;
424 if (isalpha(from_des))
427 while (nxtsct(ns, §)) {
428 if (!(fromdes = from_bmap[sect.sct_uid]))
430 todes = to_bmap[sect.sct_uid];
433 todes != '.' && todes != ' ' && todes != from_des)
435 if (sect.sct_own == from) {
436 if (fromdes != '=' && fromdes != 'h' && fromdes != des)
439 if (todes == fromdes)
441 n += map_set(to, ns->x, ns->y, fromdes, 1);