2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2018, 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
50 #include "prototypes.h"
55 /* Flags for draw_map() */
56 /* whether to put ships, planes, land units or nukes on the map */
57 #define MAP_SHIP bit(0)
58 #define MAP_PLANE bit(1)
59 #define MAP_LAND bit(2)
60 #define MAP_NUKE bit(3)
61 #define MAP_ALL (MAP_SHIP | MAP_PLANE | MAP_LAND | MAP_NUKE)
62 /* whether to highlight own sectors */
63 #define MAP_HIGH bit(4)
64 /* whether to draw a map or a bmap */
65 #define MAP_BMAP bit(5)
66 /* whether to draw an alternate map: newdes for map, true bmap for bmap */
67 #define MAP_ALT bit(6)
68 /* whether to revert bmap, internal to do_map() */
69 #define MAP_BMAP_REVERT bit(7)
71 static int parse_map_arg(int, char *, struct nstr_sect *, char *);
72 static int parse_map_flags(int, char *);
73 static int revert_bmap(struct nstr_sect *);
74 static int draw_map(char, int, struct nstr_sect *);
75 static int bmnxtsct(struct nstr_sect *);
76 static char map_char(int, natid, int);
77 static int unit_map(int, int, struct nstr_sect *, char *);
78 static void snxtsct_around(struct nstr_sect *, coord, coord);
81 do_map(int bmap, int unit_type, char *arg1, char *arg2)
87 res = parse_map_arg(unit_type, arg1, &ns, &origin);
91 map_flags = parse_map_flags(bmap, arg2);
95 if (map_flags & MAP_BMAP_REVERT)
96 return revert_bmap(&ns);
97 return draw_map(origin, map_flags, &ns);
101 parse_map_arg(int unit_type, char *arg,
102 struct nstr_sect *nsp, char *originp)
104 switch (sarg_type(arg)) {
108 if (!snxtsct(nsp, arg))
113 if (unit_map(unit_type, atoi(arg), nsp, originp) < 0) {
114 pr("No such %s\n", ef_nameof(unit_type));
122 parse_map_flags(int bmap, char *str)
128 default: CANT_REACH();
130 case 'b': map_flags = MAP_BMAP; break;
131 case 'n': map_flags = MAP_ALT; break;
132 case 0: map_flags = 0;
138 /* special case "revert" */
139 if (bmap == 'b' && mineq(str, "revert") != ME_MISMATCH)
140 return MAP_BMAP_REVERT;
142 for (p = str; *p; p++) {
146 map_flags |= MAP_SHIP;
150 map_flags |= MAP_LAND;
154 map_flags |= MAP_PLANE;
158 map_flags |= MAP_NUKE;
162 map_flags |= MAP_HIGH;
165 map_flags |= MAP_ALL;
170 map_flags |= MAP_ALT;
174 pr("Bad flag %c!\n", *p);
183 revert_bmap(struct nstr_sect *nsp)
185 if (!confirm("Are you sure you want to revert your bmap? "))
187 while (bmnxtsct(nsp))
188 player->bmap[nsp->id] = player->map[nsp->id];
189 ef_write(EF_BMAP, player->cnum, player->bmap);
194 draw_map(char origin, int map_flags, struct nstr_sect *nsp)
199 union empobj_storage unit;
202 /* Note this is not re-entrant anyway, so we keep the buffers
204 static unsigned char *bitmap = NULL;
205 static char *wmapbuf = NULL;
206 static char **wmap = NULL;
207 static int ef_mappable[] = { EF_PLANE, EF_SHIP, EF_LAND, EF_NUKE, EF_BAD };
208 static int ef_unit_map[] = { MAP_PLANE, MAP_SHIP, MAP_LAND, MAP_NUKE };
212 wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
214 wmap = malloc(WORLD_Y * sizeof(char *));
215 if (wmap && wmapbuf) {
216 for (i = 0; i < WORLD_Y; i++)
217 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
224 bitmap = malloc((WORLD_SZ() + 7) / 8);
225 if (!wmapbuf || !wmap || !bitmap) {
226 pr("Memory error, tell the deity.\n");
227 logerror("malloc failed in draw_map\n");
231 if (!(player->command->c_flags & C_MOD)) {
232 logerror("%s command needs C_MOD flag set",
233 player->command->c_form);
234 player->command->c_flags |= C_MOD;
236 np = getnatp(player->cnum);
237 /* zap any conditionals */
239 xyrelrange(np, &nsp->range, &range);
240 border(&range, " ", "");
241 blankfill(wmapbuf, &nsp->range, 1);
243 if (map_flags & MAP_BMAP) {
244 char *map = map_flags & MAP_ALT ? player->map : player->bmap;
246 while (bmnxtsct(nsp)) {
248 wmap[nsp->dy][nsp->dx] = map[nsp->id];
256 memset(bitmap, 0, (WORLD_SZ() + 7) / 8);
257 bitinit2(nsp, bitmap, player->cnum);
260 while (nxtsct(nsp, §)) {
261 if (!player->god && !emp_getbit(nsp->x, nsp->y, bitmap))
263 mapch = map_char(map_flags & MAP_ALT
264 ? sect.sct_newtype : sect.sct_type,
265 sect.sct_own, player->owner);
266 wmap[nsp->dy][nsp->dx] = mapch;
267 if (!(map_flags & MAP_ALT))
268 changed |= map_set(player->cnum, nsp->x, nsp->y, mapch, 0);
271 writemap(player->cnum);
275 while (ef_mappable[i] != EF_BAD) {
276 if (map_flags & ef_unit_map[i]) {
277 snxtitem_area(&ni, ef_mappable[i], &nsp->range);
278 while (nxtitem(&ni, &unit)) {
279 if (unit.gen.own == 0)
281 if (unit.gen.own != player->cnum && !player->god)
284 x = deltx(&nsp->range, unit.gen.x);
285 y = delty(&nsp->range, unit.gen.y);
287 if (ef_mappable[i] == EF_NUKE)
290 name = empobj_chr_name(&unit.gen);
291 wmap[y][x] = *name & ~0x20;
297 if (map_flags & MAP_HIGH) {
301 while (nxtsct(nsp, §)) {
302 if (sect.sct_own == player->cnum)
303 wmap[nsp->dy][nsp->dx] |= 0x80;
307 wmap[5][10] = origin & ~0x20;
308 for (y = nsp->range.ly, i = 0; i < nsp->range.height; y++, i++) {
312 wmap[i][nsp->range.width] = '\0';
313 pr("%4d %s %d\n", yval, wmap[i], yval);
317 border(&range, " ", "");
322 * get the next sector in the range
325 bmnxtsct(struct nstr_sect *np)
330 if (np->x >= WORLD_X)
332 if (np->dx >= np->range.width) {
334 np->x = np->range.lx;
336 if (np->dy >= np->range.height)
339 if (np->y >= WORLD_Y)
342 if ((np->y + np->x) & 01)
344 if (np->type == NS_DIST) {
345 np->curdist = mapdist(np->x, np->y, np->cx, np->cy);
346 if (np->curdist > np->dist)
349 np->id = sctoff(np->x, np->y);
355 * Return character to use in maps for sector type @type owned by @own.
356 * If @owner_or_god, the map is for the sector's owner or a deity.
359 map_char(int type, natid own, int owner_or_god)
361 if (CANT_HAPPEN(type > SCT_TYPE_MAX || !dchr[type].d_mnem))
364 || type == SCT_WATER || type == SCT_MOUNT || type == SCT_WASTE
365 || (!own && (type == SCT_RURAL || type == SCT_PLAINS)))
366 return dchr[type].d_mnem;
371 unit_map(int unit_type, int uid, struct nstr_sect *nsp, char *originp)
373 union empobj_storage unit;
376 if (CANT_HAPPEN((ef_flags(unit_type) & (EFF_OWNER | EFF_XY))
377 != (EFF_OWNER | EFF_XY)))
380 if (!get_empobj(unit_type, uid, &unit))
382 if (!player->owner || unit.gen.own == 0)
385 if (unit_type == EF_NUKE)
388 name = empobj_chr_name(&unit.gen);
392 snxtsct_around(nsp, unit.gen.x, unit.gen.y);
397 snxtsct_around(struct nstr_sect *nsp, coord x, coord y)
401 range.lx = xnorm(x - 10);
402 range.hx = xnorm(x + 10);
403 range.ly = ynorm(y - 5);
404 range.hy = ynorm(y + 5);
405 xysize_range(&range);
406 snxtsct_area(nsp, &range);
410 display_region_map(int bmap, int unit_type, coord curx, coord cury,
411 char *arg1, char *arg2)
418 res = parse_map_arg(unit_type, arg1, &ns, &origin);
422 map_flags = parse_map_flags(bmap, arg2);
426 snxtsct_around(&ns, curx, cury);
431 if (map_flags & MAP_BMAP_REVERT)
432 return revert_bmap(&ns);
433 return draw_map(origin, map_flags, &ns);
437 nav_map(int x, int y, int show_designations)
443 /* Note this is not re-entrant anyway, so we keep the buffers
445 static char *wmapbuf = NULL;
446 static char **wmap = NULL;
450 wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
452 wmap = malloc(WORLD_Y * sizeof(*wmap));
453 if (wmap && wmapbuf) {
454 for (i = 0; i < WORLD_Y; i++)
455 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
461 if (!wmapbuf || !wmap) {
462 pr("Memory error, tell the deity.\n");
463 logerror("malloc failed in navi\n");
466 snxtsct_dist(&ns, x, y, 1);
467 blankfill(wmapbuf, &ns.range, 1);
468 while (nxtsct(&ns, §)) {
469 ptr = &wmap[ns.dy][ns.dx];
470 *ptr = dchr[sect.sct_type].d_mnem;
471 if (!show_designations &&
472 sect.sct_own != player->cnum &&
473 sect.sct_type != SCT_WATER &&
474 sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR)
476 changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
478 * We do it this way so that 'x' and 'X'
479 * bdesignations will show up. This can
480 * be used to mark mined sectors. So, the
481 * player will see the current des, UNLESS
482 * they've marked the sector 'x' or 'X',
483 * in which case they'll see that.
486 *ptr = player->bmap[sect.sct_uid];
489 writemap(player->cnum);
490 for (i = 0; i < ns.range.height; i++)
496 bmaps_intersect(natid a, natid b)
498 char *mapa = ef_ptr(EF_MAP, a);
499 char *mapb = ef_ptr(EF_MAP, b);
502 for (i = 0; i < WORLD_SZ(); i++)
503 if (mapa[i] && mapa[i] != ' ' && mapb[i] && mapb[i] != ' ')
508 /* Note that this requires that the BMAP is mapped into memory */
511 share_bmap(natid from, natid to, struct nstr_sect *ns, char des,
514 char *from_bmap = ef_ptr(EF_BMAP, from);
515 char *to_bmap = ef_ptr(EF_BMAP, to);
520 char from_des = *from_name;
525 if (isalpha(from_des))
528 while (nxtsct(ns, §)) {
529 if (!(fromdes = from_bmap[sect.sct_uid]))
531 todes = to_bmap[sect.sct_uid];
534 todes != '.' && todes != ' ' && todes != from_des)
536 if (sect.sct_own == from) {
537 if (fromdes != '=' && fromdes != 'h' && fromdes != des)
540 if (todes == fromdes)
542 n += map_set(to, ns->x, ns->y, fromdes, 1);