2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2015, 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 /* Flags for draw_map() */
57 /* whether to put ships, planes, land units or nukes on the map */
58 #define MAP_SHIP bit(0)
59 #define MAP_PLANE bit(1)
60 #define MAP_LAND bit(2)
61 #define MAP_NUKE bit(3)
62 #define MAP_ALL (MAP_SHIP | MAP_PLANE | MAP_LAND | MAP_NUKE)
63 /* whether to highlight own sectors */
64 #define MAP_HIGH bit(4)
65 /* whether to draw a map or a bmap */
66 #define MAP_BMAP bit(5)
67 /* whether to draw an alternate map: newdes for map, true bmap for bmap */
68 #define MAP_ALT bit(6)
69 /* whether to revert bmap, internal to do_map() */
70 #define MAP_BMAP_REVERT bit(7)
72 static int parse_map_arg(int, char *, struct nstr_sect *, char *);
73 static int parse_map_flags(int, char *);
74 static int revert_bmap(struct nstr_sect *);
75 static int draw_map(char, int, struct nstr_sect *);
76 static int bmnxtsct(struct nstr_sect *);
77 static char map_char(int, natid, int);
78 static int unit_map(int, int, struct nstr_sect *, char *);
79 static void snxtsct_around(struct nstr_sect *, coord, coord);
82 do_map(int bmap, int unit_type, char *arg1, char *arg2)
88 res = parse_map_arg(unit_type, arg1, &ns, &origin);
92 map_flags = parse_map_flags(bmap, arg2);
96 if (map_flags & MAP_BMAP_REVERT)
97 return revert_bmap(&ns);
98 return draw_map(origin, map_flags, &ns);
102 parse_map_arg(int unit_type, char *arg,
103 struct nstr_sect *nsp, char *originp)
105 switch (sarg_type(arg)) {
109 if (!snxtsct(nsp, arg))
114 if (unit_map(unit_type, atoi(arg), nsp, originp) < 0) {
115 pr("No such %s\n", ef_nameof(unit_type));
123 parse_map_flags(int bmap, char *str)
129 default: CANT_REACH();
131 case 'b': map_flags = MAP_BMAP; break;
132 case 'n': map_flags = MAP_ALT; break;
133 case 0: map_flags = 0;
139 /* special case "revert" */
140 if (bmap == 'b' && mineq(str, "revert") != ME_MISMATCH)
141 return MAP_BMAP_REVERT;
143 for (p = str; *p; p++) {
147 map_flags |= MAP_SHIP;
151 map_flags |= MAP_LAND;
155 map_flags |= MAP_PLANE;
159 map_flags |= MAP_NUKE;
163 map_flags |= MAP_HIGH;
166 map_flags |= MAP_ALL;
171 map_flags |= MAP_ALT;
175 pr("Bad flag %c!\n", *p);
184 revert_bmap(struct nstr_sect *nsp)
186 if (!confirm("Are you sure you want to revert your bmap? "))
188 while (bmnxtsct(nsp))
189 player->bmap[nsp->id] = player->map[nsp->id];
190 ef_write(EF_BMAP, player->cnum, player->bmap);
195 draw_map(char origin, int map_flags, struct nstr_sect *nsp)
200 union empobj_storage unit;
203 /* Note this is not re-entrant anyway, so we keep the buffers
205 static unsigned char *bitmap = NULL;
206 static char *wmapbuf = NULL;
207 static char **wmap = NULL;
208 static int ef_mappable[] = { EF_PLANE, EF_SHIP, EF_LAND, EF_NUKE, EF_BAD };
209 static int ef_unit_map[] = { MAP_PLANE, MAP_SHIP, MAP_LAND, MAP_NUKE };
213 wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
215 wmap = malloc(WORLD_Y * sizeof(char *));
216 if (wmap && wmapbuf) {
217 for (i = 0; i < WORLD_Y; i++)
218 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
225 bitmap = malloc((WORLD_SZ() + 7) / 8);
226 if (!wmapbuf || !wmap || !bitmap) {
227 pr("Memory error, tell the deity.\n");
228 logerror("malloc failed in draw_map\n");
232 if (!(player->command->c_flags & C_MOD)) {
233 logerror("%s command needs C_MOD flag set",
234 player->command->c_form);
235 player->command->c_flags |= C_MOD;
237 np = getnatp(player->cnum);
238 /* zap any conditionals */
240 xyrelrange(np, &nsp->range, &range);
241 border(&range, " ", "");
242 blankfill(wmapbuf, &nsp->range, 1);
244 if (map_flags & MAP_BMAP) {
245 char *map = map_flags & MAP_ALT ? player->map : player->bmap;
247 while (bmnxtsct(nsp)) {
249 wmap[nsp->dy][nsp->dx] = map[nsp->id];
257 memset(bitmap, 0, (WORLD_SZ() + 7) / 8);
258 bitinit2(nsp, bitmap, player->cnum);
261 while (nxtsct(nsp, §)) {
262 if (!player->god && !emp_getbit(nsp->x, nsp->y, bitmap))
264 mapch = map_char(map_flags & MAP_ALT
265 ? sect.sct_newtype : sect.sct_type,
266 sect.sct_own, player->owner);
267 wmap[nsp->dy][nsp->dx] = mapch;
268 if (!(map_flags & MAP_ALT))
269 changed |= map_set(player->cnum, nsp->x, nsp->y, mapch, 0);
272 writemap(player->cnum);
276 while (ef_mappable[i] != EF_BAD) {
277 if (map_flags & ef_unit_map[i]) {
278 snxtitem_area(&ni, ef_mappable[i], &nsp->range);
279 while (nxtitem(&ni, &unit)) {
280 if (unit.gen.own == 0)
282 if (unit.gen.own != player->cnum && !player->god)
285 x = deltx(&nsp->range, unit.gen.x);
286 y = delty(&nsp->range, unit.gen.y);
288 if (ef_mappable[i] == EF_NUKE)
291 name = empobj_chr_name(&unit.gen);
292 wmap[y][x] = *name & ~0x20;
298 if (map_flags & MAP_HIGH) {
302 while (nxtsct(nsp, §)) {
303 if (sect.sct_own == player->cnum)
304 wmap[nsp->dy][nsp->dx] |= 0x80;
308 wmap[5][10] = origin & ~0x20;
309 for (y = nsp->range.ly, i = 0; i < nsp->range.height; y++, i++) {
313 wmap[i][nsp->range.width] = '\0';
314 pr("%4d %s %d\n", yval, wmap[i], yval);
318 border(&range, " ", "");
323 * get the next sector in the range
326 bmnxtsct(struct nstr_sect *np)
331 if (np->x >= WORLD_X)
333 if (np->dx >= np->range.width) {
335 np->x = np->range.lx;
337 if (np->dy >= np->range.height)
340 if (np->y >= WORLD_Y)
343 if ((np->y + np->x) & 01)
345 if (np->type == NS_DIST) {
346 np->curdist = mapdist(np->x, np->y, np->cx, np->cy);
347 if (np->curdist > np->dist)
350 np->id = sctoff(np->x, np->y);
356 * Return character to use in maps for sector type @type owned by @own.
357 * If @owner_or_god, the map is for the sector's owner or a deity.
360 map_char(int type, natid own, int owner_or_god)
362 if (CANT_HAPPEN(type > SCT_TYPE_MAX || !dchr[type].d_mnem))
365 || type == SCT_WATER || type == SCT_MOUNT || type == SCT_WASTE
366 || (!own && (type == SCT_RURAL || type == SCT_PLAINS)))
367 return dchr[type].d_mnem;
372 unit_map(int unit_type, int uid, struct nstr_sect *nsp, char *originp)
374 union empobj_storage unit;
377 if (CANT_HAPPEN((ef_flags(unit_type) & (EFF_OWNER | EFF_XY))
378 != (EFF_OWNER | EFF_XY)))
381 if (!get_empobj(unit_type, uid, &unit))
383 if (!player->owner || unit.gen.own == 0)
386 if (unit_type == EF_NUKE)
389 name = empobj_chr_name(&unit.gen);
393 snxtsct_around(nsp, unit.gen.x, unit.gen.y);
398 snxtsct_around(struct nstr_sect *nsp, coord x, coord y)
402 range.lx = xnorm(x - 10);
403 range.hx = xnorm(x + 10);
404 range.ly = ynorm(y - 5);
405 range.hy = ynorm(y + 5);
406 xysize_range(&range);
407 snxtsct_area(nsp, &range);
411 display_region_map(int bmap, int unit_type, coord curx, coord cury,
412 char *arg1, char *arg2)
419 res = parse_map_arg(unit_type, arg1, &ns, &origin);
423 map_flags = parse_map_flags(bmap, arg2);
427 snxtsct_around(&ns, curx, cury);
432 if (map_flags & MAP_BMAP_REVERT)
433 return revert_bmap(&ns);
434 return draw_map(origin, map_flags, &ns);
438 nav_map(int x, int y, int show_designations)
444 /* Note this is not re-entrant anyway, so we keep the buffers
446 static char *wmapbuf = NULL;
447 static char **wmap = NULL;
451 wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
453 wmap = malloc(WORLD_Y * sizeof(*wmap));
454 if (wmap && wmapbuf) {
455 for (i = 0; i < WORLD_Y; i++)
456 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
462 if (!wmapbuf || !wmap) {
463 pr("Memory error, tell the deity.\n");
464 logerror("malloc failed in navi\n");
467 snxtsct_dist(&ns, x, y, 1);
468 blankfill(wmapbuf, &ns.range, 1);
469 while (nxtsct(&ns, §)) {
470 ptr = &wmap[ns.dy][ns.dx];
471 *ptr = dchr[sect.sct_type].d_mnem;
472 if (!show_designations &&
473 sect.sct_own != player->cnum &&
474 sect.sct_type != SCT_WATER &&
475 sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR)
477 changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
479 * We do it this way so that 'x' and 'X'
480 * bdesignations will show up. This can
481 * be used to mark mined sectors. So, the
482 * player will see the current des, UNLESS
483 * they've marked the sector 'x' or 'X',
484 * in which case they'll see that.
487 *ptr = player->bmap[sect.sct_uid];
490 writemap(player->cnum);
491 for (i = 0; i < ns.range.height; i++)
497 bmaps_intersect(natid a, natid b)
499 char *mapa = ef_ptr(EF_MAP, a);
500 char *mapb = ef_ptr(EF_MAP, b);
503 for (i = 0; i < WORLD_SZ(); i++)
504 if (mapa[i] && mapa[i] != ' ' && mapb[i] && mapb[i] != ' ')
509 /* Note that this requires that the BMAP is mapped into memory */
512 share_bmap(natid from, natid to, struct nstr_sect *ns, char des,
515 char *from_bmap = ef_ptr(EF_BMAP, from);
516 char *to_bmap = ef_ptr(EF_BMAP, to);
521 char from_des = *from_name;
526 if (isalpha(from_des))
529 while (nxtsct(ns, §)) {
530 if (!(fromdes = from_bmap[sect.sct_uid]))
532 todes = to_bmap[sect.sct_uid];
535 todes != '.' && todes != ' ' && todes != from_des)
537 if (sect.sct_own == from) {
538 if (fromdes != '=' && fromdes != 'h' && fromdes != des)
541 if (todes == fromdes)
543 n += map_set(to, ns->x, ns->y, fromdes, 1);