2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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:
54 draw_map(int bmap, s_char origin, int map_flags, struct nstr_sect *nsp, int country)
64 /* Note this is not re-entrant anyway, so we keep the buffers
66 static u_char *bitmap = (u_char *)0;
67 static s_char *wmapbuf = (s_char *)0;
68 static s_char **wmap = (s_char **)0;
71 wmapbuf = (s_char *)malloc((WORLD_Y * MAPWIDTH(1)) * sizeof(s_char));
73 wmap = (s_char **)malloc(WORLD_Y * sizeof(s_char *));
74 if (wmap && wmapbuf) {
75 for (i = 0; i < WORLD_Y; i++)
76 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
83 bitmap = (u_char *)malloc((WORLD_X * WORLD_Y) / 8);
84 if (!wmapbuf || !wmap || !bitmap) {
85 pr("Memory error, tell the deity.\n");
86 logerror("malloc failed in draw_map\n");
90 if (bmap == EF_MAP + EF_BMAP) {
91 if (!confirm("Are you sure you want to revert your bmap? "))
94 if (!player->command->c_flags & C_MOD) {
95 logerror("%s command needs C_MOD flag set",player->command->c_form);
96 player->command->c_flags |= C_MOD;
98 np = getnatp(country);
99 /* zap any conditionals */
101 xyrelrange(np, &nsp->range, &range);
102 border(&range, " ", "");
103 blankfill((s_char *)wmapbuf, &nsp->range, 1);
108 while (bmnxtsct(nsp) && !player->aborted) {
109 if (0 != (c = player->bmap[sctoff(nsp->x, nsp->y)]))
110 wmap[nsp->dy][nsp->dx] = c;
114 while (bmnxtsct(nsp) && !player->aborted) {
115 if (0 != (c = player->map[sctoff(nsp->x, nsp->y)]))
116 wmap[nsp->dy][nsp->dx] = c;
119 case (EF_MAP + EF_BMAP):
120 while (bmnxtsct(nsp) && !player->aborted) {
121 player->bmap[sctoff(nsp->x, nsp->y)] =
122 player->map[sctoff(nsp->x, nsp->y)];
123 if (0 != (c = player->bmap[sctoff(nsp->x, nsp->y)]))
124 wmap[nsp->dy][nsp->dx] = c;
126 ef_write(EF_BMAP, player->cnum, player->bmap);
130 register s_char *ptr;
133 if ((!player->god || country)) {
134 bzero((s_char *)bitmap, (WORLD_X * WORLD_Y) / 8);
135 bitinit2(nsp, bitmap, country);
137 while (nxtsct(nsp, §) && !player->aborted) {
138 if ((!player->god || country) &&
139 !emp_getbit(nsp->x, nsp->y, bitmap)) {
143 ptr = &wmap[nsp->dy][nsp->dx];
144 if (sect.sct_newtype > SCT_MAXDEF) {
147 *ptr = dchr[sect.sct_newtype].d_mnem;
148 switch (sect.sct_newtype) {
156 if (sect.sct_own != country &&
157 (!player->god || country)) {
169 register s_char *ptr;
173 if ((!player->god || country)) {
174 bzero((s_char *)bitmap, (WORLD_X * WORLD_Y) / 8);
175 bitinit2(nsp, bitmap, country);
177 while (nxtsct(nsp, §) && !player->aborted) {
178 if ((!player->god || country) && !emp_getbit(nsp->x, nsp->y, bitmap)) {
182 ptr = &wmap[nsp->dy][nsp->dx];
183 if (sect.sct_type > SCT_MAXDEF) {
186 *ptr = dchr[sect.sct_type].d_mnem;
187 switch (sect.sct_type) {
195 if (sect.sct_own != country &&
196 (!player->god || country)) {
202 changed += map_set(player->cnum, nsp->x, nsp->y, *ptr, 0);
206 writemap(player->cnum);
210 if (map_flags & MAP_PLANE){
211 snxtitem_all(&ni, EF_PLANE);
212 while (nxtitem(&ni, (caddr_t)&plane)) {
213 if (plane.pln_own == 0)
215 if (plane.pln_own != player->cnum && !player->god)
217 if (!xyinrange(plane.pln_x, plane.pln_y, &nsp->range))
220 x = xnorm(plane.pln_x-nsp->range.lx);
221 y = ynorm(plane.pln_y-nsp->range.ly);
222 wmap[y][x] = (*plchr[(int)plane.pln_type].pl_name) & ~0x20;
225 if (map_flags & MAP_SHIP){
226 snxtitem_all(&ni, EF_SHIP);
227 while (nxtitem(&ni, (caddr_t)&ship)) {
228 if (ship.shp_own == 0)
230 if (ship.shp_own != player->cnum && !player->god)
232 if (!xyinrange(ship.shp_x, ship.shp_y, &nsp->range))
235 x = xnorm(ship.shp_x-nsp->range.lx);
236 y = ynorm(ship.shp_y-nsp->range.ly);
237 wmap[y][x] = (*mchr[(int)ship.shp_type].m_name) & ~0x20;
240 if (map_flags & MAP_LAND){
241 snxtitem_all(&ni, EF_LAND);
242 while (nxtitem(&ni, (caddr_t)&land)) {
243 if (land.lnd_own == 0)
245 if (land.lnd_own != player->cnum && !player->god)
247 if (!xyinrange(land.lnd_x, land.lnd_y, &nsp->range))
250 x = xnorm(land.lnd_x-nsp->range.lx);
251 y = ynorm(land.lnd_y-nsp->range.ly);
252 wmap[y][x] = (*lchr[(int)land.lnd_type].l_name) & ~0x20;
256 wmap[5][10] = origin & ~0x20;
257 for (y=nsp->range.ly, i=0; i < nsp->range.height; y++, i++) {
261 wmap[i][nsp->range.width] = '\0';
262 pr("%4d %s %-4d\n", yval, wmap[i], yval);
266 border(&range, " ", "");
271 * get the next sector in the range
274 bmnxtsct(register struct nstr_sect *np)
279 if (np->x >= WORLD_X)
281 if (np->dx >= np->range.width) {
283 np->x = np->range.lx;
285 if (np->dy >= np->range.height)
288 if (np->y >= WORLD_Y)
291 if ((np->y + np->x) & 01)
293 if (np->type == NS_DIST) {
294 np->curdist = mapdist(np->x, np->y, np->cx, np->cy);
295 if (np->curdist > np->dist)
298 np->id = sctoff(np->x, np->y);
305 bitinit2(struct nstr_sect *np, u_char *bitmap, int country)
307 extern int *bitmaps[];
311 while (nxtsct(np, §)) {
312 if (sect.sct_own != country)
314 eff = sect.sct_effic / 20;
317 emp_setbitmap(np->x, np->y, bitmap, bitmaps[eff]);
323 unit_map(int unit_type, int i, struct nstr_sect *nsp, s_char *originp)
331 np = getnatp(player->cnum);
332 if (unit_type == EF_LAND){
333 if (!getland(i, &origl) ||
334 (origl.lnd_own != player->cnum && !player->god) ||
335 (origl.lnd_own == 0))
337 sprintf(what, "%d:%d,%d:%d",xrel(np,origl.lnd_x-10),
338 xrel(np,origl.lnd_x+10),
339 yrel(np,origl.lnd_y-5), yrel(np,origl.lnd_y+5));
340 *originp = *lchr[(int)origl.lnd_type].l_name;
341 } else if (unit_type == EF_PLANE){
342 if (!getplane(i, &origp) ||
343 (origp.pln_own != player->cnum && !player->god) ||
344 (origp.pln_own == 0))
346 sprintf(what, "%d:%d,%d:%d",xrel(np,origp.pln_x-10),
347 xrel(np,origp.pln_x+10),
348 yrel(np,origp.pln_y-5), yrel(np,origp.pln_y+5));
349 *originp = *plchr[(int)origp.pln_type].pl_name;
351 if (!getship(i, &origs) ||
352 (origs.shp_own != player->cnum && !player->god) ||
353 (origs.shp_own == 0))
355 sprintf(what, "%d:%d,%d:%d",xrel(np,origs.shp_x-10),
356 xrel(np,origs.shp_x+10),
357 yrel(np,origs.shp_y-5), yrel(np,origs.shp_y+5));
359 *originp = *mchr[(int)origs.shp_type].m_name;
361 if (!snxtsct(nsp, what))
367 bmaps_intersect(natid a, natid b)
369 s_char *mapa = ef_ptr(EF_MAP, a);
370 s_char *mapb = ef_ptr(EF_MAP, b);
373 for (i = 0; i < WORLD_X*WORLD_Y/2; ++i, ++mapa, ++mapb)
374 if (*mapa && *mapa != ' ' && *mapb && *mapb != ' ')
379 /* Note that this requires that the BMAP is mapped into memory */
382 share_bmap(natid from, natid to, struct nstr_sect *ns, s_char des, s_char *from_name)
384 s_char *from_bmap = ef_ptr(EF_BMAP, from);
385 s_char *to_bmap = ef_ptr(EF_BMAP, to);
390 s_char from_des = *from_name;
392 if (isalpha(from_des))
395 while (nxtsct(ns, §)) {
396 if (!(fromdes = from_bmap[sctoff(ns->x, ns->y)]))
398 todes = to_bmap[sctoff(ns->x, ns->y)];
405 if (sect.sct_own == from) {
406 if (fromdes != '=' &&
411 if (todes == fromdes)
413 n += map_set(to, ns->x, ns->y, fromdes, 1);