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