]> git.pond.sub.org Git - empserver/blob - src/lib/subs/radmap.c
Compute radar range in one place, rad_range()
[empserver] / src / lib / subs / radmap.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  *  radmap.c: Do a radar map given an x,y location, effic, and other
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  */
33
34 #include <config.h>
35
36 #include <stdlib.h>
37 #include "file.h"
38 #include "map.h"
39 #include "misc.h"
40 #include "nat.h"
41 #include "nsc.h"
42 #include "optlist.h"
43 #include "plane.h"
44 #include "player.h"
45 #include "prototypes.h"
46 #include "sect.h"
47 #include "ship.h"
48 #include "xy.h"
49
50 static int rad_range(int, double, int);
51
52 /* More dynamic world sized buffers.  We create 'em once, and then
53  * never again.  No need to keep creating/tearing apart.  We may
54  * want to do this in other places too where it doesn't matter. */
55 static char **rad;
56 static char *radbuf;
57 static signed char **vis;
58 static signed char *visbuf;
59
60 /*
61  * Draw a radar map for radar at CX,CY.
62  * EFF is the radar's efficiency, TLEV its tech level, SPY its power.
63  * Submarines are detected at fraction SEESUB of the range.
64  */
65 void
66 radmap(int cx, int cy, int eff, double tlev, int spy, double seesub)
67 {
68     int visib, rng;
69     struct sctstr sect;
70     struct shpstr ship;
71     struct plnstr plane;
72     struct nstr_sect ns;
73     struct nstr_item ni;
74     int x, y;
75     int row;
76     int n;
77     int range = rad_range(eff, tlev, spy);
78     int changed = 0;
79
80     if (!radbuf)
81         radbuf = malloc(WORLD_Y * MAPWIDTH(1));
82     if (!visbuf)
83         visbuf = malloc(WORLD_Y * MAPWIDTH(1));
84     if (!rad) {
85         rad = malloc(WORLD_Y * sizeof(char *));
86         if (rad && radbuf) {
87             for (x = 0; x < WORLD_Y; x++)
88                 rad[x] = &radbuf[(WORLD_X + 1) * x];
89         }
90     }
91     if (!vis) {
92         vis = malloc(WORLD_Y * sizeof(signed char *));
93         if (vis && visbuf) {
94             for (x = 0; x < WORLD_Y; x++)
95                 vis[x] = &visbuf[(WORLD_X + 1) * x];
96         }
97     }
98     if (!radbuf || !visbuf || !rad || !vis) {
99         pr("Memory error in radmap2, tell the deity.\n");
100         return;
101     }
102
103     memset(visbuf, 0, (WORLD_Y * (WORLD_X + 1)));
104     pr("%s efficiency %d%%, max range %d\n",
105        xyas(cx, cy, player->cnum), eff, range);
106     snxtsct_dist(&ns, cx, cy, range);
107     blankfill(radbuf, &ns.range, 1);
108     while (nxtsct(&ns, &sect)) {
109         if (sect.sct_own == player->cnum
110             || sect.sct_type == SCT_WATER
111             || sect.sct_type == SCT_MOUNT
112             || sect.sct_type == SCT_WASTE
113             || ns.curdist <= range / 3)
114             rad[ns.dy][ns.dx] = dchr[sect.sct_type].d_mnem;
115         else
116             rad[ns.dy][ns.dx] = '?';
117         changed += map_set(player->cnum, ns.x, ns.y, rad[ns.dy][ns.dx], 0);
118     }
119     if (changed)
120         writemap(player->cnum);
121     snxtitem_dist(&ni, EF_PLANE, cx, cy, range);
122     while (nxtitem(&ni, &plane)) {
123         if (plane.pln_own == 0)
124             continue;
125         /* Used to have 'ghosts' when scanning whole world --ts */
126         x = deltx(&ns.range, (int)plane.pln_x);
127         y = delty(&ns.range, (int)plane.pln_y);
128
129         if (pln_is_in_orbit(&plane) && plane.pln_own != player->cnum) {
130             vis[y][x] = 100;
131             rad[y][x] = '$';
132         }
133     }
134     snxtitem_dist(&ni, EF_SHIP, cx, cy, range);
135     while (nxtitem(&ni, &ship)) {
136         if (ship.shp_own == 0)
137             continue;
138         /* Used to have 'ghosts' when scanning whole world --ts */
139         x = deltx(&ns.range, (int)ship.shp_x);
140         y = delty(&ns.range, (int)ship.shp_y);
141
142         visib = shp_visib(&ship);
143         rng = (int)(range * visib / 20.0);
144         if (ni.curdist > rng)
145             continue;
146         if ((mchr[(int)ship.shp_type].m_flags & M_SUB) &&
147             ni.curdist > rng * seesub)
148             continue;
149         if (visib > vis[y][x]) {
150             vis[y][x] = visib;
151             /* &~0x20 makes it a cap letter */
152             rad[y][x] = (*mchr[(int)ship.shp_type].m_name) & ~0x20;
153         }
154     }
155     /*
156      * make the center of the display 0
157      * so ve et al can find it.
158      */
159     rad[delty(&ns.range, cy)][deltx(&ns.range, cx)] = '0';
160
161     n = ns.range.height;
162     for (row = 0; row < n; row++)
163         pr("%s\n", rad[row]);
164     pr("\n");
165 }
166
167 /*
168  * Return distance from left edge of R to X.
169  * Value is between 0 (inclusive) and WORLD_X (exclusive).
170  * X must be normalized.
171  */
172 int
173 deltx(struct range *r, coord x)
174 {
175     if (x >= r->lx)
176         return x - r->lx;
177     return x + WORLD_X - r->lx;
178 }
179
180 /*
181  * Return distance from top edge of R to Y.
182  * Value is between 0 (inclusive) and WORLD_Y (exclusive).
183  * Y must be normalized.
184  */
185 int
186 delty(struct range *r, coord y)
187 {
188     if (y >= r->ly)
189         return y - r->ly;
190     return y + WORLD_Y - r->ly;
191 }
192
193 /*
194  * Update OWNER's bmap for radar at CX,CY.
195  * EFF is the radar's efficiency, TLEV its tech level, SPY its power.
196  */
197 void
198 rad_map_set(natid owner, int cx, int cy, int eff, double tlev, int spy)
199 {
200     struct nstr_sect ns;
201     struct sctstr sect;
202     int range = rad_range(eff, tlev, spy);
203     int changed = 0;
204     char ch;
205
206     snxtsct_dist(&ns, cx, cy, range);
207     while (nxtsct(&ns, &sect)) {
208         if (sect.sct_own == owner
209             || sect.sct_type == SCT_WATER
210             || sect.sct_type == SCT_MOUNT
211             || sect.sct_type == SCT_WASTE
212             || ns.curdist <= range / 3)
213             ch = dchr[sect.sct_type].d_mnem;
214         else
215             ch = '?';
216         changed += map_set(owner, ns.x, ns.y, ch, 0);
217     }
218     if (changed)
219         writemap(owner);
220 }
221
222 /*
223  * Range of a radar with EFF efficiency, TLEV tech, and SPY power.
224  */
225 static int
226 rad_range(int eff, double tlev, int spy)
227 {
228     int range;
229
230     range = (int)techfact(tlev, spy);
231     range = (int)(range * (eff / 100.0));
232     if (range < 1)
233         range = 1;
234     return range;
235 }