]> git.pond.sub.org Git - empserver/blob - src/lib/subs/maps.c
Move map flags from map.h to maps.c, and reorder
[empserver] / src / lib / subs / maps.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, 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 "file.h"
42 #include "land.h"
43 #include "map.h"
44 #include "misc.h"
45 #include "nat.h"
46 #include "nsc.h"
47 #include "nuke.h"
48 #include "optlist.h"
49 #include "plane.h"
50 #include "player.h"
51 #include "prototypes.h"
52 #include "sect.h"
53 #include "ship.h"
54 #include "xy.h"
55
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
66 static int draw_map(int, char, int, struct nstr_sect *);
67 static int bmnxtsct(struct nstr_sect *);
68 static char map_char(int, natid, int);
69 static int unit_map(int, int, struct nstr_sect *, char *);
70
71 int
72 do_map(int bmap, int unit_type, char *arg, char *map_flags_arg)
73 {
74     struct nstr_sect ns;
75     char origin = '\0';
76     char *b;
77     int map_flags = 0;
78
79     if (!snxtsct(&ns, arg)) {
80         if (unit_map(unit_type, atoi(arg), &ns, &origin))
81             return RET_FAIL;
82     }
83     for (b = map_flags_arg; b && *b; b++) {
84         switch (*b) {
85         case 's':
86         case 'S':
87             map_flags |= MAP_SHIP;
88             break;
89         case 'l':
90         case 'L':
91             map_flags |= MAP_LAND;
92             break;
93         case 'p':
94         case 'P':
95             map_flags |= MAP_PLANE;
96             break;
97         case 'n':
98         case 'N':
99             map_flags |= MAP_NUKE;
100             break;
101         case 'h':
102         case 'H':
103             map_flags |= MAP_HIGH;
104             break;
105         case '*':
106             map_flags |= MAP_ALL;
107             break;
108         case 't':
109             if (bmap != 'b')
110                 goto bad_flag;
111             bmap = 't';
112             *(b + 1) = 0;
113             break;
114         case 'r':
115             if (bmap != 'b')
116                 goto bad_flag;
117             bmap = 'r';
118             *(b + 1) = 0;
119             break;
120         default:
121         bad_flag:
122             pr("Bad flag %c!\n", *b);
123             break;
124         }
125     }
126     return draw_map(bmap, origin, map_flags, &ns);
127 }
128
129 static int
130 draw_map(int bmap, char origin, int map_flags, struct nstr_sect *nsp)
131 {
132     struct natstr *np;
133     struct range range;
134     struct nstr_item ni;
135     union empobj_storage unit;
136     coord x, y;
137     int i;
138     /* Note this is not re-entrant anyway, so we keep the buffers
139        around */
140     static unsigned char *bitmap = NULL;
141     static char *wmapbuf = NULL;
142     static char **wmap = NULL;
143     static int ef_mappable[] = { EF_PLANE, EF_SHIP, EF_LAND, EF_NUKE, EF_BAD };
144     static int ef_unit_map[] = { MAP_PLANE, MAP_SHIP, MAP_LAND, MAP_NUKE };
145     char *name;
146
147     if (!wmapbuf)
148         wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
149     if (!wmap) {
150         wmap = malloc(WORLD_Y * sizeof(char *));
151         if (wmap && wmapbuf) {
152             for (i = 0; i < WORLD_Y; i++)
153                 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
154         } else if (wmap) {
155             free(wmap);
156             wmap = NULL;
157         }
158     }
159     if (!bitmap)
160         bitmap = malloc((WORLD_SZ() + 7) / 8);
161     if (!wmapbuf || !wmap || !bitmap) {
162         pr("Memory error, tell the deity.\n");
163         logerror("malloc failed in draw_map\n");
164         return RET_FAIL;
165     }
166
167     if (bmap == 'r') {
168         if (!confirm("Are you sure you want to revert your bmap? "))
169             return RET_OK;
170     }
171     if (!(player->command->c_flags & C_MOD)) {
172         logerror("%s command needs C_MOD flag set",
173                  player->command->c_form);
174         player->command->c_flags |= C_MOD;
175     }
176     np = getnatp(player->cnum);
177     /* zap any conditionals */
178     nsp->ncond = 0;
179     xyrelrange(np, &nsp->range, &range);
180     border(&range, "     ", "");
181     blankfill(wmapbuf, &nsp->range, 1);
182     if (bmap) {
183         int c;
184         switch (bmap) {
185         default:
186             CANT_REACH();
187             /* fall through */
188         case 'b':
189             while (bmnxtsct(nsp) && !player->aborted) {
190                 if (0 != (c = player->bmap[nsp->id]))
191                     wmap[nsp->dy][nsp->dx] = c;
192             }
193             break;
194         case 't':
195             while (bmnxtsct(nsp) && !player->aborted) {
196                 if (0 != (c = player->map[nsp->id]))
197                     wmap[nsp->dy][nsp->dx] = c;
198             }
199             break;
200         case 'r':
201             while (bmnxtsct(nsp) && !player->aborted) {
202                 player->bmap[nsp->id] =
203                     player->map[nsp->id];
204                 if (0 != (c = player->bmap[nsp->id]))
205                     wmap[nsp->dy][nsp->dx] = c;
206             }
207             ef_write(EF_BMAP, player->cnum, player->bmap);
208             break;
209         case 'n':
210             {
211                 struct sctstr sect;
212
213                 if (!player->god) {
214                     memset(bitmap, 0, (WORLD_SZ() + 7) / 8);
215                     bitinit2(nsp, bitmap, player->cnum);
216                 }
217                 while (nxtsct(nsp, &sect) && !player->aborted) {
218                     if (!player->god && !emp_getbit(nsp->x, nsp->y, bitmap))
219                         continue;
220                     wmap[nsp->dy][nsp->dx]
221                         = map_char(sect.sct_newtype, sect.sct_own,
222                                    player->owner);
223                 }
224                 break;
225             }
226         }
227     } else {
228         struct sctstr sect;
229         char mapch;
230         int changed = 0;
231
232         if (!player->god) {
233             memset(bitmap, 0, (WORLD_SZ() + 7) / 8);
234             bitinit2(nsp, bitmap, player->cnum);
235         }
236         while (nxtsct(nsp, &sect) && !player->aborted) {
237             if (!player->god && !emp_getbit(nsp->x, nsp->y, bitmap))
238                 continue;
239             mapch = map_char(sect.sct_type, sect.sct_own, player->owner);
240             wmap[nsp->dy][nsp->dx] = mapch;
241             changed |= map_set(player->cnum, nsp->x, nsp->y, mapch, 0);
242         }
243         if (changed)
244             writemap(player->cnum);
245     }
246     if (player->aborted)
247         return RET_OK;
248
249     i = 0;
250     while (ef_mappable[i] != EF_BAD) {
251         if (map_flags & ef_unit_map[i]) {
252             snxtitem_area(&ni, ef_mappable[i], &nsp->range);
253             while (nxtitem(&ni, &unit)) {
254                 if (unit.gen.own == 0)
255                     continue;
256                 if (unit.gen.own != player->cnum && !player->god)
257                     continue;
258
259                 x = deltx(&nsp->range, unit.gen.x);
260                 y = delty(&nsp->range, unit.gen.y);
261
262                 if (ef_mappable[i] == EF_NUKE)
263                     wmap[y][x] = 'N';
264                 else {
265                     name = empobj_chr_name(&unit.gen);
266                     wmap[y][x] = *name & ~0x20;
267                 }
268             }
269         }
270         i++;
271     }
272     if (map_flags & MAP_HIGH) {
273         struct sctstr sect;
274
275         snxtsct_rewind(nsp);
276         while (nxtsct(nsp, &sect) && !player->aborted) {
277             if (sect.sct_own == player->cnum)
278                  wmap[nsp->dy][nsp->dx] |= 0x80;
279         }
280     }
281     if (origin)
282         wmap[5][10] = origin & ~0x20;
283     for (y = nsp->range.ly, i = 0; i < nsp->range.height; y++, i++) {
284         int yval;
285
286         yval = yrel(np, y);
287         wmap[i][nsp->range.width] = '\0';
288         pr("%4d %s %-4d\n", yval, wmap[i], yval);
289         if (y >= WORLD_Y)
290             y -= WORLD_Y;
291     }
292     border(&range, "     ", "");
293     return RET_OK;
294 }
295
296 /*
297  * get the next sector in the range
298  */
299 static int
300 bmnxtsct(struct nstr_sect *np)
301 {
302     while (1) {
303         np->dx++;
304         np->x++;
305         if (np->x >= WORLD_X)
306             np->x = 0;
307         if (np->dx >= np->range.width) {
308             np->dx = 0;
309             np->x = np->range.lx;
310             np->dy++;
311             if (np->dy >= np->range.height)
312                 return 0;
313             np->y++;
314             if (np->y >= WORLD_Y)
315                 np->y = 0;
316         }
317         if ((np->y + np->x) & 01)
318             continue;
319         if (np->type == NS_DIST) {
320             np->curdist = mapdist(np->x, np->y, np->cx, np->cy);
321             if (np->curdist > np->dist)
322                 continue;
323         }
324         np->id = sctoff(np->x, np->y);
325         return 1;
326     }
327     /*NOTREACHED*/
328 }
329
330 /*
331  * Return character to use in maps for sector type TYPE owned by OWN.
332  * If OWNER_OR_GOD, the map is for the sector's owner or a deity.
333  */
334 static char
335 map_char(int type, natid own, int owner_or_god)
336 {
337     if (CANT_HAPPEN(type > SCT_TYPE_MAX || !dchr[type].d_mnem))
338         return '?';
339     if (owner_or_god
340         || type == SCT_WATER || type == SCT_MOUNT || type == SCT_WASTE
341         || (!own && (type == SCT_RURAL || type == SCT_PLAINS)))
342         return dchr[type].d_mnem;
343     return '?';
344 }
345
346 static int
347 unit_map(int unit_type, int uid, struct nstr_sect *nsp, char *originp)
348 {
349     union empobj_storage unit;
350     struct range range;
351     char *name;
352
353     if (CANT_HAPPEN((ef_flags(unit_type) & (EFF_OWNER | EFF_XY))
354                     != (EFF_OWNER | EFF_XY)))
355         return RET_FAIL;
356
357     if (!get_empobj(unit_type, uid, &unit))
358         return RET_FAIL;
359     if (!player->owner || unit.gen.own == 0)
360         return RET_FAIL;
361
362     if (unit_type == EF_NUKE)
363         *originp = 'n';
364     else {
365         name = empobj_chr_name(&unit.gen);
366         *originp = *name;
367     }
368
369     range.lx = xnorm(unit.gen.x - 10);
370     range.hx = xnorm(unit.gen.x + 10);
371     range.ly = ynorm(unit.gen.y - 5);
372     range.hy = ynorm(unit.gen.y + 5);
373     xysize_range(&range);
374     snxtsct_area(nsp, &range);
375     return RET_OK;
376 }
377
378 int
379 display_region_map(int bmap, int unit_type, coord curx, coord cury,
380                    char *arg)
381 {
382     char coordinates[80];
383     char *map_flag_arg;
384
385     if (!arg || !*arg) {
386         struct natstr *np;
387
388         np = getnatp(player->cnum);
389         sprintf(coordinates, "%d:%d,%d:%d",
390             xrel(np, curx - 10), xrel(np, curx + 10),
391             yrel(np, cury - 5), yrel(np, cury + 5));
392         arg = coordinates;
393         map_flag_arg = NULL;
394     } else {
395         map_flag_arg = strchr(arg, ' ');
396         if (map_flag_arg != NULL) {
397             *map_flag_arg++  = '\0';
398             while (isspace(*map_flag_arg)) map_flag_arg++;
399         }
400     }
401     player->condarg = NULL;
402     return do_map(bmap, unit_type, arg, map_flag_arg);
403 }
404
405 int
406 bmaps_intersect(natid a, natid b)
407 {
408     char *mapa = ef_ptr(EF_MAP, a);
409     char *mapb = ef_ptr(EF_MAP, b);
410     int i;
411
412     for (i = 0; i < WORLD_SZ(); i++)
413         if (mapa[i] && mapa[i] != ' ' && mapb[i] && mapb[i] != ' ')
414             return 1;
415     return 0;
416 }
417
418 /* Note that this requires that the BMAP is mapped into memory */
419
420 int
421 share_bmap(natid from, natid to, struct nstr_sect *ns, char des,
422            char *from_name)
423 {
424     char *from_bmap = ef_ptr(EF_BMAP, from);
425     char *to_bmap = ef_ptr(EF_BMAP, to);
426     int n = 0;
427     struct sctstr sect;
428     char fromdes;
429     char todes;
430     char from_des = *from_name;
431
432     if (from == to)
433         return 0;
434
435     if (isalpha(from_des))
436         from_des &= ~0x20;
437
438     while (nxtsct(ns, &sect)) {
439         if (!(fromdes = from_bmap[sect.sct_uid]))
440             continue;
441         todes = to_bmap[sect.sct_uid];
442         if (todes &&
443             todes != '?' &&
444             todes != '.' && todes != ' ' && todes != from_des)
445             continue;
446         if (sect.sct_own == from) {
447             if (fromdes != '=' && fromdes != 'h' && fromdes != des)
448                 fromdes = from_des;
449         }
450         if (todes == fromdes)
451             continue;
452         n += map_set(to, ns->x, ns->y, fromdes, 1);
453     }
454
455     if (n)
456         writebmap(to);
457     return n;
458 }