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
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 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.
28 * radmap.c: Do a radar map given an x,y location, effic, and other
30 * Known contributors to this file:
45 #include "prototypes.h"
50 static void radmap2(int, int, int, int, int, double, int);
53 radmap(int cx, int cy, int eff, int range, double seesub)
55 radmap2(player->cnum, cx, cy, eff, range, seesub, 1);
59 radmapupd(int own, int cx, int cy, int eff, int range, double seesub)
61 radmap2(own, cx, cy, eff, range, seesub, 0);
64 /* More dynamic world sized buffers. We create 'em once, and then
65 * never again. No need to keep creating/tearing apart. We may
66 * want to do this in other places too where it doesn't matter. */
69 static signed char **vis;
70 static signed char *visbuf;
74 int cx, int cy, int eff, int range, double seesub, int pr_flag)
88 radbuf = malloc(WORLD_Y * MAPWIDTH(1));
90 visbuf = malloc(WORLD_Y * MAPWIDTH(1));
92 rad = malloc(WORLD_Y * sizeof(char *));
94 for (x = 0; x < WORLD_Y; x++)
95 rad[x] = &radbuf[(WORLD_X + 1) * x];
99 vis = malloc(WORLD_Y * sizeof(signed char *));
101 for (x = 0; x < WORLD_Y; x++)
102 vis[x] = &visbuf[(WORLD_X + 1) * x];
105 if (!radbuf || !visbuf || !rad || !vis) {
106 pr("Memory error in radmap2, tell the deity.\n");
110 memset(visbuf, 0, (WORLD_Y * (WORLD_X + 1)));
111 range = (int)(range * (eff / 100.0));
115 pr("%s efficiency %d%%, max range %d\n",
116 xyas(cx, cy, owner), eff, range);
117 snxtsct_dist(&ns, cx, cy, range);
118 blankfill(radbuf, &ns.range, 1);
119 while (nxtsct(&ns, §)) {
120 if (sect.sct_own == owner
121 || sect.sct_type == SCT_WATER
122 || sect.sct_type == SCT_MOUNT
123 || sect.sct_type == SCT_WASTE
124 || ns.curdist <= range / 3)
125 rad[ns.dy][ns.dx] = dchr[sect.sct_type].d_mnem;
127 rad[ns.dy][ns.dx] = '?';
128 changed += map_set(owner, ns.x, ns.y, rad[ns.dy][ns.dx], 0);
134 snxtitem_dist(&ni, EF_PLANE, cx, cy, range);
135 while (nxtitem(&ni, &plane)) {
136 if (plane.pln_own == 0)
138 /* Used to have 'ghosts' when scanning whole world --ts */
139 x = deltx(&ns.range, (int)plane.pln_x);
140 y = delty(&ns.range, (int)plane.pln_y);
142 if (pln_is_in_orbit(&plane) && plane.pln_own != owner) {
147 snxtitem_dist(&ni, EF_SHIP, cx, cy, range);
148 while (nxtitem(&ni, &ship)) {
149 if (ship.shp_own == 0)
151 /* Used to have 'ghosts' when scanning whole world --ts */
152 x = deltx(&ns.range, (int)ship.shp_x);
153 y = delty(&ns.range, (int)ship.shp_y);
155 visib = shp_visib(&ship);
156 rng = (int)(range * visib / 20.0);
157 if (ni.curdist > rng)
159 if ((mchr[(int)ship.shp_type].m_flags & M_SUB) &&
160 ni.curdist > rng * seesub)
162 if (visib > vis[y][x]) {
164 /* &~0x20 makes it a cap letter */
165 rad[y][x] = (*mchr[(int)ship.shp_type].m_name) & ~0x20;
169 * make the center of the display 0
170 * so ve et al can find it.
172 rad[delty(&ns.range, cy)][deltx(&ns.range, cx)] = '0';
175 for (row = 0; row < n; row++)
176 pr("%s\n", rad[row]);
181 * Return distance from left edge of R to X.
182 * Value is between 0 (inclusive) and WORLD_X (exclusive).
183 * X must be normalized.
186 deltx(struct range *r, coord x)
190 return x + WORLD_X - r->lx;
194 * Return distance from top edge of R to Y.
195 * Value is between 0 (inclusive) and WORLD_Y (exclusive).
196 * Y must be normalized.
199 delty(struct range *r, coord y)
203 return y + WORLD_Y - r->ly;