]> git.pond.sub.org Git - empserver/blob - src/lib/subs/radmap.c
Update copyright notice
[empserver] / src / lib / subs / radmap.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, 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  *     Markus Armbruster, 2004-2010
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 static char rad_char(struct sctstr *, int, int, natid);
52
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. */
56 static char **rad;
57 static char *radbuf;
58 static signed char **vis;
59 static signed char *visbuf;
60
61 /*
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.
65  */
66 void
67 radmap(int cx, int cy, int eff, double tlev, int spy, double seesub)
68 {
69     int visib, rng;
70     struct sctstr sect;
71     struct shpstr ship;
72     struct plnstr plane;
73     struct nstr_sect ns;
74     struct nstr_item ni;
75     int x, y;
76     int row;
77     int n;
78     int range = rad_range(eff, tlev, spy);
79     int changed = 0;
80
81     if (!radbuf)
82         radbuf = malloc(WORLD_Y * MAPWIDTH(1));
83     if (!visbuf)
84         visbuf = malloc(WORLD_Y * MAPWIDTH(1));
85     if (!rad) {
86         rad = malloc(WORLD_Y * sizeof(char *));
87         if (rad && radbuf) {
88             for (x = 0; x < WORLD_Y; x++)
89                 rad[x] = &radbuf[(WORLD_X + 1) * x];
90         }
91     }
92     if (!vis) {
93         vis = malloc(WORLD_Y * sizeof(signed char *));
94         if (vis && visbuf) {
95             for (x = 0; x < WORLD_Y; x++)
96                 vis[x] = &visbuf[(WORLD_X + 1) * x];
97         }
98     }
99     if (!radbuf || !visbuf || !rad || !vis) {
100         pr("Memory error in radmap2, tell the deity.\n");
101         return;
102     }
103
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, &sect)) {
110         rad[ns.dy][ns.dx] = rad_char(&sect, ns.curdist, range,
111                                      player->cnum);
112         changed += map_set(player->cnum, ns.x, ns.y, rad[ns.dy][ns.dx], 0);
113     }
114     if (changed)
115         writemap(player->cnum);
116     snxtitem_dist(&ni, EF_PLANE, cx, cy, range);
117     while (nxtitem(&ni, &plane)) {
118         if (plane.pln_own == 0)
119             continue;
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);
123
124         if (pln_is_in_orbit(&plane) && plane.pln_own != player->cnum) {
125             vis[y][x] = 100;
126             rad[y][x] = '$';
127         }
128     }
129     snxtitem_dist(&ni, EF_SHIP, cx, cy, range);
130     while (nxtitem(&ni, &ship)) {
131         if (ship.shp_own == 0)
132             continue;
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);
136
137         visib = shp_visib(&ship);
138         rng = (int)(range * visib / 20.0);
139         if (ni.curdist > rng)
140             continue;
141         if ((mchr[(int)ship.shp_type].m_flags & M_SUB) &&
142             ni.curdist > rng * seesub)
143             continue;
144         if (visib > vis[y][x]) {
145             vis[y][x] = visib;
146             /* &~0x20 makes it a cap letter */
147             rad[y][x] = (*mchr[(int)ship.shp_type].m_name) & ~0x20;
148         }
149     }
150     /*
151      * make the center of the display 0
152      * so ve et al can find it.
153      */
154     rad[delty(&ns.range, cy)][deltx(&ns.range, cx)] = '0';
155
156     n = ns.range.height;
157     for (row = 0; row < n; row++)
158         pr("%s\n", rad[row]);
159     pr("\n");
160 }
161
162 /*
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.
166  */
167 int
168 deltx(struct range *r, coord x)
169 {
170     if (x >= r->lx)
171         return x - r->lx;
172     return x + WORLD_X - r->lx;
173 }
174
175 /*
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.
179  */
180 int
181 delty(struct range *r, coord y)
182 {
183     if (y >= r->ly)
184         return y - r->ly;
185     return y + WORLD_Y - r->ly;
186 }
187
188 /*
189  * Update @owner's bmap for radar at @cx,@cy.
190  * @eff is the radar's efficiency, @tlev its tech level, @spy its power.
191  */
192 void
193 rad_map_set(natid owner, int cx, int cy, int eff, double tlev, int spy)
194 {
195     struct nstr_sect ns;
196     struct sctstr sect;
197     int range = rad_range(eff, tlev, spy);
198     int changed = 0;
199     char ch;
200
201     snxtsct_dist(&ns, cx, cy, range);
202     while (nxtsct(&ns, &sect)) {
203         ch = rad_char(&sect, ns.curdist, range, owner);
204         changed += map_set(owner, ns.x, ns.y, ch, 0);
205     }
206     if (changed)
207         writemap(owner);
208 }
209
210 /*
211  * Range of a radar with @eff efficiency, @tlev tech, and @spy power.
212  */
213 static int
214 rad_range(int eff, double tlev, int spy)
215 {
216     int range;
217
218     range = (int)techfact(tlev, spy);
219     range = (int)(range * (eff / 100.0));
220     if (range < 1)
221         range = 1;
222     return range;
223 }
224
225 /*
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.
229  */
230 static char
231 rad_char(struct sctstr *sp, int dist, int range, natid cn)
232 {
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;
239     return '?';
240 }