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