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