]> git.pond.sub.org Git - empserver/blob - src/lib/subs/maps.c
Update copyright notice
[empserver] / src / lib / subs / maps.c
1 /*
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
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-2011
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
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
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
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
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
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
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
359 map_char(int type, natid own, int owner_or_god)
360 {
361     if (CANT_HAPPEN(type > SCT_TYPE_MAX || !dchr[type].d_mnem))
362         return '?';
363     if (owner_or_god
364         || type == SCT_WATER || type == SCT_MOUNT || type == SCT_WASTE
365         || (!own && (type == SCT_RURAL || type == SCT_PLAINS)))
366         return dchr[type].d_mnem;
367     return '?';
368 }
369
370 static int
371 unit_map(int unit_type, int uid, struct nstr_sect *nsp, char *originp)
372 {
373     union empobj_storage unit;
374     char *name;
375
376     if (CANT_HAPPEN((ef_flags(unit_type) & (EFF_OWNER | EFF_XY))
377                     != (EFF_OWNER | EFF_XY)))
378         return -1;
379
380     if (!get_empobj(unit_type, uid, &unit))
381         return -1;
382     if (!player->owner || unit.gen.own == 0)
383         return -1;
384
385     if (unit_type == EF_NUKE)
386         *originp = 'n';
387     else {
388         name = empobj_chr_name(&unit.gen);
389         *originp = *name;
390     }
391
392     snxtsct_around(nsp, unit.gen.x, unit.gen.y);
393     return 0;
394 }
395
396 static void
397 snxtsct_around(struct nstr_sect *nsp, coord x, coord y)
398 {
399     struct range range;
400
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);
407 }
408
409 int
410 display_region_map(int bmap, int unit_type, coord curx, coord cury,
411                    char *arg1, char *arg2)
412 {
413     struct nstr_sect ns;
414     char origin;
415     int res, map_flags;
416
417     if (arg1 && *arg1) {
418         res = parse_map_arg(unit_type, arg1, &ns, &origin);
419         if (res != RET_OK)
420             return res;
421
422         map_flags = parse_map_flags(bmap, arg2);
423         if (map_flags < 0)
424             return RET_SYN;
425     } else {
426         snxtsct_around(&ns, curx, cury);
427         map_flags = 0;
428         origin = 0;
429     }
430
431     if (map_flags & MAP_BMAP_REVERT)
432         return revert_bmap(&ns);
433     return draw_map(origin, map_flags, &ns);
434 }
435
436 int
437 nav_map(int x, int y, int show_designations)
438 {
439     char *ptr;
440     struct nstr_sect ns;
441     struct sctstr sect;
442     int i;
443     /* Note this is not re-entrant anyway, so we keep the buffers
444        around */
445     static char *wmapbuf = NULL;
446     static char **wmap = NULL;
447     int changed = 0;
448
449     if (!wmapbuf)
450         wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
451     if (!wmap) {
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];
456         } else if (wmap) {
457             free(wmap);
458             wmap = NULL;
459         }
460     }
461     if (!wmapbuf || !wmap) {
462         pr("Memory error, tell the deity.\n");
463         logerror("malloc failed in navi\n");
464         return RET_FAIL;
465     }
466     snxtsct_dist(&ns, x, y, 1);
467     blankfill(wmapbuf, &ns.range, 1);
468     while (nxtsct(&ns, &sect)) {
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)
475             *ptr = '?';
476         changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
477         /*
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.
484          * --ts
485          */
486         *ptr = player->bmap[sect.sct_uid];
487     }
488     if (changed)
489         writemap(player->cnum);
490     for (i = 0; i < ns.range.height; i++)
491         pr("%s\n", wmap[i]);
492     return RET_OK;
493 }
494
495 int
496 bmaps_intersect(natid a, natid b)
497 {
498     char *mapa = ef_ptr(EF_MAP, a);
499     char *mapb = ef_ptr(EF_MAP, b);
500     int i;
501
502     for (i = 0; i < WORLD_SZ(); i++)
503         if (mapa[i] && mapa[i] != ' ' && mapb[i] && mapb[i] != ' ')
504             return 1;
505     return 0;
506 }
507
508 /* Note that this requires that the BMAP is mapped into memory */
509
510 int
511 share_bmap(natid from, natid to, struct nstr_sect *ns, char des,
512            char *from_name)
513 {
514     char *from_bmap = ef_ptr(EF_BMAP, from);
515     char *to_bmap = ef_ptr(EF_BMAP, to);
516     int n = 0;
517     struct sctstr sect;
518     char fromdes;
519     char todes;
520     char from_des = *from_name;
521
522     if (from == to)
523         return 0;
524
525     if (isalpha(from_des))
526         from_des &= ~0x20;
527
528     while (nxtsct(ns, &sect)) {
529         if (!(fromdes = from_bmap[sect.sct_uid]))
530             continue;
531         todes = to_bmap[sect.sct_uid];
532         if (todes &&
533             todes != '?' &&
534             todes != '.' && todes != ' ' && todes != from_des)
535             continue;
536         if (sect.sct_own == from) {
537             if (fromdes != '=' && fromdes != 'h' && fromdes != des)
538                 fromdes = from_des;
539         }
540         if (todes == fromdes)
541             continue;
542         n += map_set(to, ns->x, ns->y, fromdes, 1);
543     }
544
545     if (n)
546         writebmap(to);
547     return n;
548 }