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