1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  maps.c: Map routines
28  *
29  *  Known contributors to this file:
30  *     Ken Stevens, 1995
31  *     Steve McClure, 1998
32  *     Markus Armbruster, 2004-2020
33  *     Ron Koenderink, 2006
34  */
35 
36 #include <config.h>
37 
38 #include <ctype.h>
39 #include "com.h"
40 #include "empobj.h"
41 #include "land.h"
42 #include "map.h"
43 #include "match.h"
44 #include "misc.h"
45 #include "nat.h"
46 #include "nsc.h"
47 #include "optlist.h"
48 #include "plane.h"
49 #include "player.h"
50 #include "prototypes.h"
51 #include "sect.h"
52 #include "ship.h"
53 #include "xy.h"
54 
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)
70 
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);
79 
80 int
do_map(int bmap,int unit_type,char * arg1,char * arg2)81 do_map(int bmap, int unit_type, char *arg1, char *arg2)
82 {
83     struct nstr_sect ns;
84     char origin;
85     int res, map_flags;
86 
87     res = parse_map_arg(unit_type, arg1, &ns, &origin);
88     if (res != RET_OK)
89 	return res;
90 
91     map_flags = parse_map_flags(bmap, arg2);
92     if (map_flags < 0)
93 	return RET_SYN;
94 
95     if (map_flags & MAP_BMAP_REVERT)
96 	return revert_bmap(&ns);
97     return draw_map(origin, map_flags, &ns);
98 }
99 
100 static int
parse_map_arg(int unit_type,char * arg,struct nstr_sect * nsp,char * originp)101 parse_map_arg(int unit_type, char *arg,
102 	      struct nstr_sect *nsp, char *originp)
103 {
104     switch (sarg_type(arg)) {
105     case NS_DIST:
106     case NS_AREA:
107     case NS_ALL:
108 	if (!snxtsct(nsp, arg))
109 	    return RET_SYN;
110 	*originp = 0;
111 	break;
112     default:
113 	if (unit_map(unit_type, atoi(arg), nsp, originp) < 0) {
114 	    pr("No such %s\n", ef_nameof(unit_type));
115 	    return RET_FAIL;
116 	}
117     }
118     return RET_OK;
119 }
120 
121 static int
parse_map_flags(int bmap,char * str)122 parse_map_flags(int bmap, char *str)
123 {
124     int map_flags;
125     char *p;
126 
127     switch (bmap) {
128     default: CANT_REACH();
129 	/* fall through */
130     case 'b': map_flags = MAP_BMAP; break;
131     case 'n': map_flags = MAP_ALT; break;
132     case 0:   map_flags = 0;
133     }
134 
135     if (!str || !*str)
136 	return map_flags;
137 
138     /* special case "revert" */
139     if (bmap == 'b' && mineq(str, "revert") != ME_MISMATCH)
140 	return MAP_BMAP_REVERT;
141 
142     for (p = str; *p; p++) {
143 	switch (*p) {
144 	case 's':
145 	case 'S':
146 	    map_flags |= MAP_SHIP;
147 	    break;
148 	case 'l':
149 	case 'L':
150 	    map_flags |= MAP_LAND;
151 	    break;
152 	case 'p':
153 	case 'P':
154 	    map_flags |= MAP_PLANE;
155 	    break;
156 	case 'n':
157 	case 'N':
158 	    map_flags |= MAP_NUKE;
159 	    break;
160 	case 'h':
161 	case 'H':
162 	    map_flags |= MAP_HIGH;
163 	    break;
164 	case '*':
165 	    map_flags |= MAP_ALL;
166 	    break;
167 	case 't':
168 	    if (bmap != 'b')
169 		goto bad_flag;
170 	    map_flags |= MAP_ALT;
171 	    break;
172 	default:
173 	bad_flag:
174 	    pr("Bad flag %c!\n", *p);
175 	    return -1;
176 	}
177     }
178 
179     return map_flags;
180 }
181 
182 static int
revert_bmap(struct nstr_sect * nsp)183 revert_bmap(struct nstr_sect *nsp)
184 {
185     if (!confirm("Are you sure you want to revert your bmap? "))
186 	return RET_FAIL;
187     while (bmnxtsct(nsp))
188 	player->bmap[nsp->id] = player->map[nsp->id];
189     ef_write(EF_BMAP, player->cnum, player->bmap);
190     return RET_OK;
191 }
192 
193 static int
draw_map(char origin,int map_flags,struct nstr_sect * nsp)194 draw_map(char origin, int map_flags, struct nstr_sect *nsp)
195 {
196     struct natstr *np;
197     struct range range;
198     struct nstr_item ni;
199     union empobj_storage unit;
200     coord x, y;
201     int i;
202     /* Note this is not re-entrant anyway, so we keep the buffers
203        around */
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 };
209     char *name;
210 
211     if (!wmapbuf)
212 	wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
213     if (!wmap) {
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];
218 	} else if (wmap) {
219 	    free(wmap);
220 	    wmap = NULL;
221 	}
222     }
223     if (!bitmap)
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");
228 	return RET_FAIL;
229     }
230 
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;
235     }
236     np = getnatp(player->cnum);
237     /* zap any conditionals */
238     nsp->ncond = 0;
239     xyrelrange(np, &nsp->range, &range);
240     border(&range, "     ", "");
241     blankfill(wmapbuf, &nsp->range, 1);
242 
243     if (map_flags & MAP_BMAP) {
244 	char *map = map_flags & MAP_ALT ? player->map : player->bmap;
245 
246 	while (bmnxtsct(nsp)) {
247 	    if (map[nsp->id])
248 		wmap[nsp->dy][nsp->dx] = map[nsp->id];
249 	}
250     } else {
251 	struct sctstr sect;
252 	char mapch;
253 	int changed = 0;
254 
255 	if (!player->god) {
256 	    memset(bitmap, 0, (WORLD_SZ() + 7) / 8);
257 	    bitinit2(nsp, bitmap, player->cnum);
258 	}
259 
260 	while (nxtsct(nsp, &sect)) {
261 	    if (!player->god && !emp_getbit(nsp->x, nsp->y, bitmap))
262 		continue;
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);
269 	}
270 	if (changed)
271 	    writemap(player->cnum);
272     }
273 
274     i = 0;
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)
280 		    continue;
281 		if (unit.gen.own != player->cnum && !player->god)
282 		    continue;
283 
284 		x = deltx(&nsp->range, unit.gen.x);
285 		y = delty(&nsp->range, unit.gen.y);
286 
287 		if (ef_mappable[i] == EF_NUKE)
288 		    wmap[y][x] = 'N';
289 		else {
290 		    name = empobj_chr_name(&unit.gen);
291 		    wmap[y][x] = *name & ~0x20;
292 		}
293 	    }
294 	}
295 	i++;
296     }
297     if (map_flags & MAP_HIGH) {
298 	struct sctstr sect;
299 
300 	snxtsct_rewind(nsp);
301 	while (nxtsct(nsp, &sect)) {
302 	    if (sect.sct_own == player->cnum)
303 		 wmap[nsp->dy][nsp->dx] |= 0x80;
304 	}
305     }
306     if (origin)
307 	wmap[5][10] = origin & ~0x20;
308     for (y = nsp->range.ly, i = 0; i < nsp->range.height; y++, i++) {
309 	int yval;
310 
311 	yval = yrel(np, y);
312 	wmap[i][nsp->range.width] = '\0';
313 	pr("%4d %s %d\n", yval, wmap[i], yval);
314 	if (y >= WORLD_Y)
315 	    y -= WORLD_Y;
316     }
317     border(&range, "     ", "");
318     return RET_OK;
319 }
320 
321 /*
322  * get the next sector in the range
323  */
324 static int
bmnxtsct(struct nstr_sect * np)325 bmnxtsct(struct nstr_sect *np)
326 {
327     while (1) {
328 	np->dx++;
329 	np->x++;
330 	if (np->x >= WORLD_X)
331 	    np->x = 0;
332 	if (np->dx >= np->range.width) {
333 	    np->dx = 0;
334 	    np->x = np->range.lx;
335 	    np->dy++;
336 	    if (np->dy >= np->range.height)
337 		return 0;
338 	    np->y++;
339 	    if (np->y >= WORLD_Y)
340 		np->y = 0;
341 	}
342 	if ((np->y + np->x) & 01)
343 	    continue;
344 	if (np->type == NS_DIST) {
345 	    np->curdist = mapdist(np->x, np->y, np->cx, np->cy);
346 	    if (np->curdist > np->dist)
347 		continue;
348 	}
349 	np->id = sctoff(np->x, np->y);
350 	return 1;
351     }
352 }
353 
354 /*
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.
357  */
358 static char
map_char(int type,natid own,int owner_or_god)359 map_char(int type, natid own, int owner_or_god)
360 {
361     if (CANT_HAPPEN((unsigned)type >= ARRAY_SIZE(dchr) - 1
362 		    || !dchr[type].d_mnem))
363 	return '?';
364     if (owner_or_god
365 	|| type == SCT_WATER || type == SCT_MOUNT || type == SCT_WASTE
366 	|| (!own && (type == SCT_RURAL || type == SCT_PLAINS)))
367 	return dchr[type].d_mnem;
368     return '?';
369 }
370 
371 static int
unit_map(int unit_type,int uid,struct nstr_sect * nsp,char * originp)372 unit_map(int unit_type, int uid, struct nstr_sect *nsp, char *originp)
373 {
374     union empobj_storage unit;
375     char *name;
376 
377     if (CANT_HAPPEN((ef_flags(unit_type) & (EFF_OWNER | EFF_XY))
378 		    != (EFF_OWNER | EFF_XY)))
379 	return -1;
380 
381     if (!get_empobj(unit_type, uid, &unit))
382 	return -1;
383     if (!player->owner || unit.gen.own == 0)
384 	return -1;
385 
386     if (unit_type == EF_NUKE)
387 	*originp = 'n';
388     else {
389 	name = empobj_chr_name(&unit.gen);
390 	*originp = *name;
391     }
392 
393     snxtsct_around(nsp, unit.gen.x, unit.gen.y);
394     return 0;
395 }
396 
397 static void
snxtsct_around(struct nstr_sect * nsp,coord x,coord y)398 snxtsct_around(struct nstr_sect *nsp, coord x, coord y)
399 {
400     struct range range;
401 
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);
408 }
409 
410 int
display_region_map(int bmap,int unit_type,coord curx,coord cury,char * arg1,char * arg2)411 display_region_map(int bmap, int unit_type, coord curx, coord cury,
412 		   char *arg1, char *arg2)
413 {
414     struct nstr_sect ns;
415     char origin;
416     int res, map_flags;
417 
418     if (arg1 && *arg1) {
419 	res = parse_map_arg(unit_type, arg1, &ns, &origin);
420 	if (res != RET_OK)
421 	    return res;
422 
423 	map_flags = parse_map_flags(bmap, arg2);
424 	if (map_flags < 0)
425 	    return RET_SYN;
426     } else {
427 	snxtsct_around(&ns, curx, cury);
428 	map_flags = 0;
429 	origin = 0;
430     }
431 
432     if (map_flags & MAP_BMAP_REVERT)
433 	return revert_bmap(&ns);
434     return draw_map(origin, map_flags, &ns);
435 }
436 
437 int
nav_map(int x,int y,int show_designations)438 nav_map(int x, int y, int show_designations)
439 {
440     char *ptr;
441     struct nstr_sect ns;
442     struct sctstr sect;
443     int i;
444     /* Note this is not re-entrant anyway, so we keep the buffers
445        around */
446     static char *wmapbuf = NULL;
447     static char **wmap = NULL;
448     int changed = 0;
449 
450     if (!wmapbuf)
451 	wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
452     if (!wmap) {
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];
457 	} else if (wmap) {
458 	    free(wmap);
459 	    wmap = NULL;
460 	}
461     }
462     if (!wmapbuf || !wmap) {
463 	pr("Memory error, tell the deity.\n");
464 	logerror("malloc failed in navi\n");
465 	return RET_FAIL;
466     }
467     snxtsct_dist(&ns, x, y, 1);
468     blankfill(wmapbuf, &ns.range, 1);
469     while (nxtsct(&ns, &sect)) {
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)
476 	    *ptr = '?';
477 	changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
478 	/*
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.
485 	 * --ts
486 	 */
487 	*ptr = player->bmap[sect.sct_uid];
488     }
489     if (changed)
490 	writemap(player->cnum);
491     for (i = 0; i < ns.range.height; i++)
492 	pr("%s\n", wmap[i]);
493     return RET_OK;
494 }
495 
496 int
bmaps_intersect(natid a,natid b)497 bmaps_intersect(natid a, natid b)
498 {
499     char *mapa = ef_ptr(EF_MAP, a);
500     char *mapb = ef_ptr(EF_MAP, b);
501     int i;
502 
503     for (i = 0; i < WORLD_SZ(); i++)
504 	if (mapa[i] && mapa[i] != ' ' && mapb[i] && mapb[i] != ' ')
505 	    return 1;
506     return 0;
507 }
508 
509 /* Note that this requires that the BMAP is mapped into memory */
510 
511 int
share_bmap(natid from,natid to,struct nstr_sect * ns,char des,char * from_name)512 share_bmap(natid from, natid to, struct nstr_sect *ns, char des,
513 	   char *from_name)
514 {
515     char *from_bmap = ef_ptr(EF_BMAP, from);
516     char *to_bmap = ef_ptr(EF_BMAP, to);
517     int n = 0;
518     struct sctstr sect;
519     char fromdes;
520     char todes;
521     char from_des = *from_name;
522 
523     if (from == to)
524 	return 0;
525 
526     if (isalpha(from_des))
527 	from_des &= ~0x20;
528 
529     while (nxtsct(ns, &sect)) {
530 	if (!(fromdes = from_bmap[sect.sct_uid]))
531 	    continue;
532 	todes = to_bmap[sect.sct_uid];
533 	if (todes &&
534 	    todes != '?' &&
535 	    todes != '.' && todes != ' ' && todes != from_des)
536 	    continue;
537 	if (sect.sct_own == from) {
538 	    if (fromdes != '=' && fromdes != 'h' && fromdes != des)
539 		fromdes = from_des;
540 	}
541 	if (todes == fromdes)
542 	    continue;
543 	n += map_set(to, ns->x, ns->y, fromdes, 1);
544     }
545 
546     if (n)
547 	writebmap(to);
548     return n;
549 }
550