2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * maps.c: Map routines
30 * Known contributors to this file:
53 static int bmnxtsct(struct nstr_sect *);
54 static s_char map_char(u_char type, natid own, int owner_or_god);
57 draw_map(int bmap, s_char origin, int map_flags, struct nstr_sect *nsp)
67 /* Note this is not re-entrant anyway, so we keep the buffers
69 static u_char *bitmap = (u_char *)0;
70 static s_char *wmapbuf = (s_char *)0;
71 static s_char **wmap = (s_char **)0;
74 wmapbuf = malloc((WORLD_Y * MAPWIDTH(1)) * sizeof(s_char));
76 wmap = malloc(WORLD_Y * sizeof(s_char *));
77 if (wmap && wmapbuf) {
78 for (i = 0; i < WORLD_Y; i++)
79 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
86 bitmap = malloc((WORLD_X * WORLD_Y) / 8);
87 if (!wmapbuf || !wmap || !bitmap) {
88 pr("Memory error, tell the deity.\n");
89 logerror("malloc failed in draw_map\n");
94 if (!confirm("Are you sure you want to revert your bmap? "))
97 if (!(player->command->c_flags & C_MOD)) {
98 logerror("%s command needs C_MOD flag set",
99 player->command->c_form);
100 player->command->c_flags |= C_MOD;
102 np = getnatp(player->cnum);
103 /* zap any conditionals */
105 xyrelrange(np, &nsp->range, &range);
106 border(&range, " ", "");
107 blankfill((s_char *)wmapbuf, &nsp->range, 1);
112 CANT_HAPPEN("bad BMAP");
116 while (bmnxtsct(nsp) && !player->aborted) {
117 if (0 != (c = player->bmap[sctoff(nsp->x, nsp->y)]))
118 wmap[nsp->dy][nsp->dx] = c;
122 while (bmnxtsct(nsp) && !player->aborted) {
123 if (0 != (c = player->map[sctoff(nsp->x, nsp->y)]))
124 wmap[nsp->dy][nsp->dx] = c;
128 while (bmnxtsct(nsp) && !player->aborted) {
129 player->bmap[sctoff(nsp->x, nsp->y)] =
130 player->map[sctoff(nsp->x, nsp->y)];
131 if (0 != (c = player->bmap[sctoff(nsp->x, nsp->y)]))
132 wmap[nsp->dy][nsp->dx] = c;
134 ef_write(EF_BMAP, player->cnum, player->bmap);
141 memset(bitmap, 0, (WORLD_X * WORLD_Y) / 8);
142 bitinit2(nsp, bitmap, player->cnum);
144 while (nxtsct(nsp, §) && !player->aborted) {
145 if (!player->god && !emp_getbit(nsp->x, nsp->y, bitmap))
147 wmap[nsp->dy][nsp->dx]
148 = map_char(sect.sct_newtype, sect.sct_own,
160 memset(bitmap, 0, (WORLD_X * WORLD_Y) / 8);
161 bitinit2(nsp, bitmap, player->cnum);
163 while (nxtsct(nsp, §) && !player->aborted) {
164 if (!player->god && !emp_getbit(nsp->x, nsp->y, bitmap))
166 mapch = map_char(sect.sct_type, sect.sct_own, player->owner);
167 wmap[nsp->dy][nsp->dx] = mapch;
168 changed |= map_set(player->cnum, nsp->x, nsp->y, mapch, 0);
171 writemap(player->cnum);
175 if (map_flags & MAP_PLANE) {
176 snxtitem_all(&ni, EF_PLANE);
177 while (nxtitem(&ni, &plane)) {
178 if (plane.pln_own == 0)
180 if (plane.pln_own != player->cnum && !player->god)
182 if (!xyinrange(plane.pln_x, plane.pln_y, &nsp->range))
185 x = xnorm(plane.pln_x - nsp->range.lx);
186 y = ynorm(plane.pln_y - nsp->range.ly);
187 wmap[y][x] = (*plchr[(int)plane.pln_type].pl_name) & ~0x20;
190 if (map_flags & MAP_SHIP) {
191 snxtitem_all(&ni, EF_SHIP);
192 while (nxtitem(&ni, &ship)) {
193 if (ship.shp_own == 0)
195 if (ship.shp_own != player->cnum && !player->god)
197 if (!xyinrange(ship.shp_x, ship.shp_y, &nsp->range))
200 x = xnorm(ship.shp_x - nsp->range.lx);
201 y = ynorm(ship.shp_y - nsp->range.ly);
202 wmap[y][x] = (*mchr[(int)ship.shp_type].m_name) & ~0x20;
205 if (map_flags & MAP_LAND) {
206 snxtitem_all(&ni, EF_LAND);
207 while (nxtitem(&ni, &land)) {
208 if (land.lnd_own == 0)
210 if (land.lnd_own != player->cnum && !player->god)
212 if (!xyinrange(land.lnd_x, land.lnd_y, &nsp->range))
215 x = xnorm(land.lnd_x - nsp->range.lx);
216 y = ynorm(land.lnd_y - nsp->range.ly);
217 wmap[y][x] = (*lchr[(int)land.lnd_type].l_name) & ~0x20;
220 if (map_flags & MAP_HIGH) {
226 memset(bitmap, 0, (WORLD_X * WORLD_Y) / 8);
227 bitinit2(nsp, bitmap, player->cnum);
229 while (nxtsct(nsp, §) && !player->aborted) {
230 if (!player->god && !emp_getbit(nsp->x, nsp->y, bitmap))
232 ptr = &wmap[nsp->dy][nsp->dx];
233 if (sect.sct_own == player->cnum)
238 wmap[5][10] = origin & ~0x20;
239 for (y = nsp->range.ly, i = 0; i < nsp->range.height; y++, i++) {
243 wmap[i][nsp->range.width] = '\0';
244 pr("%4d %s %-4d\n", yval, wmap[i], yval);
248 border(&range, " ", "");
253 * get the next sector in the range
256 bmnxtsct(struct nstr_sect *np)
261 if (np->x >= WORLD_X)
263 if (np->dx >= np->range.width) {
265 np->x = np->range.lx;
267 if (np->dy >= np->range.height)
270 if (np->y >= WORLD_Y)
273 if ((np->y + np->x) & 01)
275 if (np->type == NS_DIST) {
276 np->curdist = mapdist(np->x, np->y, np->cx, np->cy);
277 if (np->curdist > np->dist)
280 np->id = sctoff(np->x, np->y);
287 * Return character to use in maps for sector type TYPE owned by OWN.
288 * If OWNER_OR_GOD, the map is for the sector's owner or a deity.
291 map_char(u_char type, natid own, int owner_or_god)
293 if (type > SCT_MAXDEF) {
294 logerror("bad sector type %d\n", type);
298 || type == SCT_WATER || type == SCT_MOUNT || type == SCT_WASTE
299 || (!own && (type == SCT_RURAL || type == SCT_PLAINS)))
300 return dchr[type].d_mnem;
305 unit_map(int unit_type, int i, struct nstr_sect *nsp, s_char *originp)
313 np = getnatp(player->cnum);
314 if (unit_type == EF_LAND) {
315 if (!getland(i, &origl) ||
316 (origl.lnd_own != player->cnum && !player->god) ||
317 (origl.lnd_own == 0))
319 sprintf(what, "%d:%d,%d:%d", xrel(np, origl.lnd_x - 10),
320 xrel(np, origl.lnd_x + 10),
321 yrel(np, origl.lnd_y - 5), yrel(np, origl.lnd_y + 5));
322 *originp = *lchr[(int)origl.lnd_type].l_name;
323 } else if (unit_type == EF_PLANE) {
324 if (!getplane(i, &origp) ||
325 (origp.pln_own != player->cnum && !player->god) ||
326 (origp.pln_own == 0))
328 sprintf(what, "%d:%d,%d:%d", xrel(np, origp.pln_x - 10),
329 xrel(np, origp.pln_x + 10),
330 yrel(np, origp.pln_y - 5), yrel(np, origp.pln_y + 5));
331 *originp = *plchr[(int)origp.pln_type].pl_name;
333 if (!getship(i, &origs) ||
334 (origs.shp_own != player->cnum && !player->god) ||
335 (origs.shp_own == 0))
337 sprintf(what, "%d:%d,%d:%d", xrel(np, origs.shp_x - 10),
338 xrel(np, origs.shp_x + 10),
339 yrel(np, origs.shp_y - 5), yrel(np, origs.shp_y + 5));
341 *originp = *mchr[(int)origs.shp_type].m_name;
343 if (!snxtsct(nsp, what))
349 bmaps_intersect(natid a, natid b)
351 s_char *mapa = ef_ptr(EF_MAP, a);
352 s_char *mapb = ef_ptr(EF_MAP, b);
355 for (i = 0; i < WORLD_X * WORLD_Y / 2; ++i, ++mapa, ++mapb)
356 if (*mapa && *mapa != ' ' && *mapb && *mapb != ' ')
361 /* Note that this requires that the BMAP is mapped into memory */
364 share_bmap(natid from, natid to, struct nstr_sect *ns, s_char des,
367 s_char *from_bmap = ef_ptr(EF_BMAP, from);
368 s_char *to_bmap = ef_ptr(EF_BMAP, to);
373 s_char from_des = *from_name;
375 if (isalpha(from_des))
378 while (nxtsct(ns, §)) {
379 if (!(fromdes = from_bmap[sctoff(ns->x, ns->y)]))
381 todes = to_bmap[sctoff(ns->x, ns->y)];
384 todes != '.' && todes != ' ' && todes != from_des)
386 if (sect.sct_own == from) {
387 if (fromdes != '=' && fromdes != 'h' && fromdes != des)
390 if (todes == fromdes)
392 n += map_set(to, ns->x, ns->y, fromdes, 1);