]> git.pond.sub.org Git - empserver/blob - src/lib/subs/maps.c
Clean up dead stores
[empserver] / src / lib / subs / maps.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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             /* fall through */
179         case 'b':
180             while (bmnxtsct(nsp) && !player->aborted) {
181                 if (0 != (c = player->bmap[nsp->id]))
182                     wmap[nsp->dy][nsp->dx] = c;
183             }
184             break;
185         case 't':
186             while (bmnxtsct(nsp) && !player->aborted) {
187                 if (0 != (c = player->map[nsp->id]))
188                     wmap[nsp->dy][nsp->dx] = c;
189             }
190             break;
191         case 'r':
192             while (bmnxtsct(nsp) && !player->aborted) {
193                 player->bmap[nsp->id] =
194                     player->map[nsp->id];
195                 if (0 != (c = player->bmap[nsp->id]))
196                     wmap[nsp->dy][nsp->dx] = c;
197             }
198             ef_write(EF_BMAP, player->cnum, player->bmap);
199             break;
200         case 'n':
201             {
202                 struct sctstr sect;
203
204                 if (!player->god) {
205                     memset(bitmap, 0, WORLD_SZ() / 8);
206                     bitinit2(nsp, bitmap, player->cnum);
207                 }
208                 while (nxtsct(nsp, &sect) && !player->aborted) {
209                     if (!player->god && !emp_getbit(nsp->x, nsp->y, bitmap))
210                         continue;
211                     wmap[nsp->dy][nsp->dx]
212                         = map_char(sect.sct_newtype, sect.sct_own,
213                                    player->owner);
214                 }
215                 break;
216             }
217         }
218     } else {
219         struct sctstr sect;
220         char mapch;
221         int changed = 0;
222
223         if (!player->god) {
224             memset(bitmap, 0, WORLD_SZ() / 8);
225             bitinit2(nsp, bitmap, player->cnum);
226         }
227         while (nxtsct(nsp, &sect) && !player->aborted) {
228             if (!player->god && !emp_getbit(nsp->x, nsp->y, bitmap))
229                 continue;
230             mapch = map_char(sect.sct_type, sect.sct_own, player->owner);
231             wmap[nsp->dy][nsp->dx] = mapch;
232             changed |= map_set(player->cnum, nsp->x, nsp->y, mapch, 0);
233         }
234         if (changed)
235             writemap(player->cnum);
236     }
237     if (player->aborted)
238         return RET_OK;
239
240     i = 0;
241     while (ef_mappable[i] != EF_BAD) {
242         if (map_flags & ef_unit_map[i]) {
243             snxtitem_area(&ni, ef_mappable[i], &nsp->range);
244             while (nxtitem(&ni, &unit)) {
245                 if (unit.gen.own == 0)
246                     continue;
247                 if (unit.gen.own != player->cnum && !player->god)
248                     continue;
249
250                 x = deltx(&nsp->range, unit.gen.x);
251                 y = delty(&nsp->range, unit.gen.y);
252
253                 if (ef_mappable[i] == EF_NUKE)
254                     wmap[y][x] = 'N';
255                 else {
256                     name = empobj_chr_name(&unit.gen);
257                     wmap[y][x] = *name & ~0x20;
258                 }
259             }
260         }
261         i++;
262     }
263     if (map_flags & MAP_HIGH) {
264         struct sctstr sect;
265
266         snxtsct_rewind(nsp);
267         while (nxtsct(nsp, &sect) && !player->aborted) {
268             if (sect.sct_own == player->cnum)
269                  wmap[nsp->dy][nsp->dx] |= 0x80;
270         }
271     }
272     if (origin)
273         wmap[5][10] = origin & ~0x20;
274     for (y = nsp->range.ly, i = 0; i < nsp->range.height; y++, i++) {
275         int yval;
276
277         yval = yrel(np, y);
278         wmap[i][nsp->range.width] = '\0';
279         pr("%4d %s %-4d\n", yval, wmap[i], yval);
280         if (y >= WORLD_Y)
281             y -= WORLD_Y;
282     }
283     border(&range, "     ", "");
284     return RET_OK;
285 }
286
287 /*
288  * get the next sector in the range
289  */
290 static int
291 bmnxtsct(struct nstr_sect *np)
292 {
293     while (1) {
294         np->dx++;
295         np->x++;
296         if (np->x >= WORLD_X)
297             np->x = 0;
298         if (np->dx >= np->range.width) {
299             np->dx = 0;
300             np->x = np->range.lx;
301             np->dy++;
302             if (np->dy >= np->range.height)
303                 return 0;
304             np->y++;
305             if (np->y >= WORLD_Y)
306                 np->y = 0;
307         }
308         if ((np->y + np->x) & 01)
309             continue;
310         if (np->type == NS_DIST) {
311             np->curdist = mapdist(np->x, np->y, np->cx, np->cy);
312             if (np->curdist > np->dist)
313                 continue;
314         }
315         np->id = sctoff(np->x, np->y);
316         return 1;
317     }
318     /*NOTREACHED*/
319 }
320
321 /*
322  * Return character to use in maps for sector type TYPE owned by OWN.
323  * If OWNER_OR_GOD, the map is for the sector's owner or a deity.
324  */
325 static char
326 map_char(int type, natid own, int owner_or_god)
327 {
328     if (CANT_HAPPEN(type > SCT_TYPE_MAX || !dchr[type].d_mnem))
329         return '?';
330     if (owner_or_god
331         || type == SCT_WATER || type == SCT_MOUNT || type == SCT_WASTE
332         || (!own && (type == SCT_RURAL || type == SCT_PLAINS)))
333         return dchr[type].d_mnem;
334     return '?';
335 }
336
337 static int
338 unit_map(int unit_type, int uid, struct nstr_sect *nsp, char *originp)
339 {
340     union empobj_storage unit;
341     struct range range;
342     char *name;
343
344     if (CANT_HAPPEN((ef_flags(unit_type) & (EFF_OWNER | EFF_XY))
345                     != (EFF_OWNER | EFF_XY)))
346         return RET_FAIL;
347
348     if (!get_empobj(unit_type, uid, &unit))
349         return RET_FAIL;
350     if (!player->owner || unit.gen.own == 0)
351         return RET_FAIL;
352
353     if (unit_type == EF_NUKE)
354         *originp = 'n';
355     else {
356         name = empobj_chr_name(&unit.gen);
357         *originp = *name;
358     }
359
360     range.lx = xnorm(unit.gen.x - 10);
361     range.hx = xnorm(unit.gen.x + 10);
362     range.ly = ynorm(unit.gen.y - 5);
363     range.hy = ynorm(unit.gen.y + 5);
364     xysize_range(&range);
365     snxtsct_area(nsp, &range);
366     return RET_OK;
367 }
368
369 int
370 display_region_map(int bmap, int unit_type, coord curx, coord cury,
371                    char *arg)
372 {
373     char coordinates[80];
374     char *map_flag_arg;
375
376     if (!arg || !*arg) {
377         struct natstr *np;
378
379         np = getnatp(player->cnum);
380         sprintf(coordinates, "%d:%d,%d:%d",
381             xrel(np, curx - 10), xrel(np, curx + 10),
382             yrel(np, cury - 5), yrel(np, cury + 5));
383         arg = coordinates;
384         map_flag_arg = NULL;
385     } else {
386         map_flag_arg = strchr(arg, ' ');
387         if (map_flag_arg != NULL) {
388             *map_flag_arg++  = '\0';
389             while (isspace(*map_flag_arg)) map_flag_arg++;
390         }
391     }
392     player->condarg = NULL;
393     return do_map(bmap, unit_type, arg, map_flag_arg);
394 }
395
396 int
397 bmaps_intersect(natid a, natid b)
398 {
399     char *mapa = ef_ptr(EF_MAP, a);
400     char *mapb = ef_ptr(EF_MAP, b);
401     int i;
402
403     for (i = 0; i < WORLD_SZ(); i++)
404         if (mapa[i] && mapa[i] != ' ' && mapb[i] && mapb[i] != ' ')
405             return 1;
406     return 0;
407 }
408
409 /* Note that this requires that the BMAP is mapped into memory */
410
411 int
412 share_bmap(natid from, natid to, struct nstr_sect *ns, char des,
413            char *from_name)
414 {
415     char *from_bmap = ef_ptr(EF_BMAP, from);
416     char *to_bmap = ef_ptr(EF_BMAP, to);
417     int n = 0;
418     struct sctstr sect;
419     char fromdes;
420     char todes;
421     char from_des = *from_name;
422
423     if (isalpha(from_des))
424         from_des &= ~0x20;
425
426     while (nxtsct(ns, &sect)) {
427         if (!(fromdes = from_bmap[sect.sct_uid]))
428             continue;
429         todes = to_bmap[sect.sct_uid];
430         if (todes &&
431             todes != '?' &&
432             todes != '.' && todes != ' ' && todes != from_des)
433             continue;
434         if (sect.sct_own == from) {
435             if (fromdes != '=' && fromdes != 'h' && fromdes != des)
436                 fromdes = from_des;
437         }
438         if (todes == fromdes)
439             continue;
440         n += map_set(to, ns->x, ns->y, fromdes, 1);
441     }
442
443     if (n)
444         writebmap(to);
445     return n;
446 }