]> git.pond.sub.org Git - empserver/blob - src/lib/subs/radmap.c
License upgrade to GPL version 3 or later
[empserver] / src / lib / subs / radmap.c
1 /*
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
5  *
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.
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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  radmap.c: Do a radar map given an x,y location, effic, and other
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1989
31  */
32
33 #include <config.h>
34
35 #include <stdlib.h>
36 #include "file.h"
37 #include "map.h"
38 #include "misc.h"
39 #include "nat.h"
40 #include "nsc.h"
41 #include "optlist.h"
42 #include "plane.h"
43 #include "player.h"
44 #include "prototypes.h"
45 #include "sect.h"
46 #include "ship.h"
47 #include "xy.h"
48
49 static int rad_range(int, double, int);
50 static char rad_char(struct sctstr *, int, int, natid);
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         rad[ns.dy][ns.dx] = rad_char(&sect, ns.curdist, range,
110                                      player->cnum);
111         changed += map_set(player->cnum, ns.x, ns.y, rad[ns.dy][ns.dx], 0);
112     }
113     if (changed)
114         writemap(player->cnum);
115     snxtitem_dist(&ni, EF_PLANE, cx, cy, range);
116     while (nxtitem(&ni, &plane)) {
117         if (plane.pln_own == 0)
118             continue;
119         /* Used to have 'ghosts' when scanning whole world --ts */
120         x = deltx(&ns.range, (int)plane.pln_x);
121         y = delty(&ns.range, (int)plane.pln_y);
122
123         if (pln_is_in_orbit(&plane) && plane.pln_own != player->cnum) {
124             vis[y][x] = 100;
125             rad[y][x] = '$';
126         }
127     }
128     snxtitem_dist(&ni, EF_SHIP, cx, cy, range);
129     while (nxtitem(&ni, &ship)) {
130         if (ship.shp_own == 0)
131             continue;
132         /* Used to have 'ghosts' when scanning whole world --ts */
133         x = deltx(&ns.range, (int)ship.shp_x);
134         y = delty(&ns.range, (int)ship.shp_y);
135
136         visib = shp_visib(&ship);
137         rng = (int)(range * visib / 20.0);
138         if (ni.curdist > rng)
139             continue;
140         if ((mchr[(int)ship.shp_type].m_flags & M_SUB) &&
141             ni.curdist > rng * seesub)
142             continue;
143         if (visib > vis[y][x]) {
144             vis[y][x] = visib;
145             /* &~0x20 makes it a cap letter */
146             rad[y][x] = (*mchr[(int)ship.shp_type].m_name) & ~0x20;
147         }
148     }
149     /*
150      * make the center of the display 0
151      * so ve et al can find it.
152      */
153     rad[delty(&ns.range, cy)][deltx(&ns.range, cx)] = '0';
154
155     n = ns.range.height;
156     for (row = 0; row < n; row++)
157         pr("%s\n", rad[row]);
158     pr("\n");
159 }
160
161 /*
162  * Return distance from left edge of R to X.
163  * Value is between 0 (inclusive) and WORLD_X (exclusive).
164  * X must be normalized.
165  */
166 int
167 deltx(struct range *r, coord x)
168 {
169     if (x >= r->lx)
170         return x - r->lx;
171     return x + WORLD_X - r->lx;
172 }
173
174 /*
175  * Return distance from top edge of R to Y.
176  * Value is between 0 (inclusive) and WORLD_Y (exclusive).
177  * Y must be normalized.
178  */
179 int
180 delty(struct range *r, coord y)
181 {
182     if (y >= r->ly)
183         return y - r->ly;
184     return y + WORLD_Y - r->ly;
185 }
186
187 /*
188  * Update OWNER's bmap for radar at CX,CY.
189  * EFF is the radar's efficiency, TLEV its tech level, SPY its power.
190  */
191 void
192 rad_map_set(natid owner, int cx, int cy, int eff, double tlev, int spy)
193 {
194     struct nstr_sect ns;
195     struct sctstr sect;
196     int range = rad_range(eff, tlev, spy);
197     int changed = 0;
198     char ch;
199
200     snxtsct_dist(&ns, cx, cy, range);
201     while (nxtsct(&ns, &sect)) {
202         ch = rad_char(&sect, ns.curdist, range, owner);
203         changed += map_set(owner, ns.x, ns.y, ch, 0);
204     }
205     if (changed)
206         writemap(owner);
207 }
208
209 /*
210  * Range of a radar with EFF efficiency, TLEV tech, and SPY power.
211  */
212 static int
213 rad_range(int eff, double tlev, int spy)
214 {
215     int range;
216
217     range = (int)techfact(tlev, spy);
218     range = (int)(range * (eff / 100.0));
219     if (range < 1)
220         range = 1;
221     return range;
222 }
223
224 /*
225  * Return character to use in radar maps for sector SP.
226  * DIST is the distance from the radar, RANGE its range.
227  * Country CN is using the radar.
228  */
229 static char
230 rad_char(struct sctstr *sp, int dist, int range, natid cn)
231 {
232     if (sp->sct_own == cn
233         || sp->sct_type == SCT_WATER
234         || sp->sct_type == SCT_MOUNT
235         || sp->sct_type == SCT_WASTE
236         || dist <= range / 3)
237         return dchr[sp->sct_type].d_mnem;
238     return '?';
239 }