2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * radmap.c: Do a radar map given an x,y location, effic, and other
29 * Known contributors to this file:
31 * Markus Armbruster, 2004-2010
45 #include "prototypes.h"
50 static int rad_range(int, double, int);
51 static char rad_char(struct sctstr *, int, int, natid);
53 /* More dynamic world sized buffers. We create 'em once, and then
54 * never again. No need to keep creating/tearing apart. We may
55 * want to do this in other places too where it doesn't matter. */
58 static signed char **vis;
59 static signed char *visbuf;
62 * Draw a radar map for radar at CX,CY.
63 * EFF is the radar's efficiency, TLEV its tech level, SPY its power.
64 * Submarines are detected at fraction SEESUB of the range.
67 radmap(int cx, int cy, int eff, double tlev, int spy, double seesub)
78 int range = rad_range(eff, tlev, spy);
82 radbuf = malloc(WORLD_Y * MAPWIDTH(1));
84 visbuf = malloc(WORLD_Y * MAPWIDTH(1));
86 rad = malloc(WORLD_Y * sizeof(char *));
88 for (x = 0; x < WORLD_Y; x++)
89 rad[x] = &radbuf[(WORLD_X + 1) * x];
93 vis = malloc(WORLD_Y * sizeof(signed char *));
95 for (x = 0; x < WORLD_Y; x++)
96 vis[x] = &visbuf[(WORLD_X + 1) * x];
99 if (!radbuf || !visbuf || !rad || !vis) {
100 pr("Memory error in radmap2, tell the deity.\n");
104 memset(visbuf, 0, (WORLD_Y * (WORLD_X + 1)));
105 pr("%s efficiency %d%%, max range %d\n",
106 xyas(cx, cy, player->cnum), eff, range);
107 snxtsct_dist(&ns, cx, cy, range);
108 blankfill(radbuf, &ns.range, 1);
109 while (nxtsct(&ns, §)) {
110 rad[ns.dy][ns.dx] = rad_char(§, ns.curdist, range,
112 changed += map_set(player->cnum, ns.x, ns.y, rad[ns.dy][ns.dx], 0);
115 writemap(player->cnum);
116 snxtitem_dist(&ni, EF_PLANE, cx, cy, range);
117 while (nxtitem(&ni, &plane)) {
118 if (plane.pln_own == 0)
120 /* Used to have 'ghosts' when scanning whole world --ts */
121 x = deltx(&ns.range, (int)plane.pln_x);
122 y = delty(&ns.range, (int)plane.pln_y);
124 if (pln_is_in_orbit(&plane) && plane.pln_own != player->cnum) {
129 snxtitem_dist(&ni, EF_SHIP, cx, cy, range);
130 while (nxtitem(&ni, &ship)) {
131 if (ship.shp_own == 0)
133 /* Used to have 'ghosts' when scanning whole world --ts */
134 x = deltx(&ns.range, (int)ship.shp_x);
135 y = delty(&ns.range, (int)ship.shp_y);
137 visib = shp_visib(&ship);
138 rng = (int)(range * visib / 20.0);
139 if (ni.curdist > rng)
141 if ((mchr[(int)ship.shp_type].m_flags & M_SUB) &&
142 ni.curdist > rng * seesub)
144 if (visib > vis[y][x]) {
146 /* &~0x20 makes it a cap letter */
147 rad[y][x] = (*mchr[(int)ship.shp_type].m_name) & ~0x20;
151 * make the center of the display 0
152 * so ve et al can find it.
154 rad[delty(&ns.range, cy)][deltx(&ns.range, cx)] = '0';
157 for (row = 0; row < n; row++)
158 pr("%s\n", rad[row]);
163 * Return distance from left edge of R to X.
164 * Value is between 0 (inclusive) and WORLD_X (exclusive).
165 * X must be normalized.
168 deltx(struct range *r, coord x)
172 return x + WORLD_X - r->lx;
176 * Return distance from top edge of R to Y.
177 * Value is between 0 (inclusive) and WORLD_Y (exclusive).
178 * Y must be normalized.
181 delty(struct range *r, coord y)
185 return y + WORLD_Y - r->ly;
189 * Update OWNER's bmap for radar at CX,CY.
190 * EFF is the radar's efficiency, TLEV its tech level, SPY its power.
193 rad_map_set(natid owner, int cx, int cy, int eff, double tlev, int spy)
197 int range = rad_range(eff, tlev, spy);
201 snxtsct_dist(&ns, cx, cy, range);
202 while (nxtsct(&ns, §)) {
203 ch = rad_char(§, ns.curdist, range, owner);
204 changed += map_set(owner, ns.x, ns.y, ch, 0);
211 * Range of a radar with EFF efficiency, TLEV tech, and SPY power.
214 rad_range(int eff, double tlev, int spy)
218 range = (int)techfact(tlev, spy);
219 range = (int)(range * (eff / 100.0));
226 * Return character to use in radar maps for sector SP.
227 * DIST is the distance from the radar, RANGE its range.
228 * Country CN is using the radar.
231 rad_char(struct sctstr *sp, int dist, int range, natid cn)
233 if (sp->sct_own == cn
234 || sp->sct_type == SCT_WATER
235 || sp->sct_type == SCT_MOUNT
236 || sp->sct_type == SCT_WASTE
237 || dist <= range / 3)
238 return dchr[sp->sct_type].d_mnem;