]> 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-2004, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "misc.h"
35 #include "player.h"
36 #include "xy.h"
37 #include "nat.h"
38 #include "sect.h"
39 #include "ship.h"
40 #include "plane.h"
41 #include "file.h"
42 #include "nsc.h"
43 #include "prototypes.h"
44 #include "optlist.h"
45
46 static void radmap2(int, int, int, int, int, double, int);
47
48 void
49 radmap(int cx, int cy, int eff, int range, double seesub)
50 {
51     radmap2(player->cnum, cx, cy, eff, range, seesub, 1);
52 }
53
54 void
55 radmapnopr(int cx, int cy, int eff, int range, double seesub)
56 {
57     radmap2(player->cnum, cx, cy, eff, range, seesub, 0);
58 }
59
60 void
61 radmapupd(int own, int cx, int cy, int eff, int range, double seesub)
62 {
63     radmap2(own, cx, cy, eff, range, seesub, 0);
64 }
65
66 /* More dynamic world sized buffers.  We create 'em once, and then
67  * never again.  No need to keep creating/tearing apart.  We may
68  * want to do this in other places too where it doesn't matter. */
69 static s_char **rad;
70 static s_char *radbuf;
71 static s_char **vis;
72 static s_char *visbuf;
73
74 static void
75 radmap2(int owner,
76         int cx, int cy, int eff, int range, double seesub, int pr_flag)
77 {
78     int rng;
79     struct sctstr sect;
80     struct shpstr ship;
81     struct plnstr plane;
82     struct nstr_sect ns;
83     struct nstr_item ni;
84     int x, y;
85     int row;
86     int n;
87     int changed = 0;
88
89     if (!radbuf)
90         radbuf = (s_char *)malloc((WORLD_Y * (WORLD_X + 1)) *
91                                   sizeof(s_char));
92     if (!visbuf)
93         visbuf = (s_char *)malloc((WORLD_Y * (WORLD_X + 1)) *
94                                   sizeof(s_char));
95     if (!rad) {
96         rad = (s_char **)malloc(WORLD_Y * sizeof(s_char *));
97         if (rad && radbuf) {
98             for (x = 0; x < WORLD_Y; x++)
99                 rad[x] = &radbuf[(WORLD_X + 1) * x];
100         }
101     }
102     if (!vis) {
103         vis = (s_char **)malloc(WORLD_Y * sizeof(s_char *));
104         if (vis && visbuf) {
105             for (x = 0; x < WORLD_Y; x++)
106                 vis[x] = &visbuf[(WORLD_X + 1) * x];
107         }
108     }
109     if (!radbuf || !visbuf || !rad || !vis) {
110         pr("Memory error in radmap2, tell the deity.\n");
111         return;
112     }
113
114     memset(visbuf, 0, (WORLD_Y * (WORLD_X + 1)));
115     range = (int)(range * (eff / 100.0));
116     if (range < 1)
117         range = 1;
118     if (pr_flag)
119         pr("%s efficiency %d%%, max range %d\n",
120            xyas(cx, cy, owner), eff, range);
121     snxtsct_dist(&ns, cx, cy, range);
122     blankfill((s_char *)radbuf, &ns.range, 1);
123     while (nxtsct(&ns, &sect)) {
124         if (sect.sct_own == owner
125             || sect.sct_type == SCT_WATER
126             || sect.sct_type == SCT_MOUNT
127             || sect.sct_type == SCT_WASTE
128             || ns.curdist <= range / 3)
129             rad[ns.dy][ns.dx] = dchr[sect.sct_type].d_mnem;
130         else
131             rad[ns.dy][ns.dx] = '?';
132         changed += map_set(owner, ns.x, ns.y, rad[ns.dy][ns.dx], 0);
133     }
134     if (changed)
135         writemap(owner);
136     if (!pr_flag)
137         return;
138     snxtitem_dist(&ni, EF_PLANE, cx, cy, range);
139     while (nxtitem(&ni, &plane)) {
140         if (plane.pln_own == 0)
141             continue;
142         /* Used to have 'ghosts' when scanning whole world --ts */
143         x = deltx(&ns.range, (int)plane.pln_x);
144         y = delty(&ns.range, (int)plane.pln_y);
145
146         if ((plane.pln_flags & PLN_LAUNCHED) && plane.pln_own != owner) {
147             vis[y][x] = (s_char)100;
148             rad[y][x] = '$';
149         }
150     }
151     snxtitem_dist(&ni, EF_SHIP, cx, cy, range);
152     while (nxtitem(&ni, &ship)) {
153         if (ship.shp_own == 0)
154             continue;
155         /* Used to have 'ghosts' when scanning whole world --ts */
156         x = deltx(&ns.range, (int)ship.shp_x);
157         y = delty(&ns.range, (int)ship.shp_y);
158
159         rng = (int)(range * ship.shp_visib / 20.0);
160         if (ni.curdist > rng)
161             continue;
162         if ((mchr[(int)ship.shp_type].m_flags & M_SUB) &&
163             ni.curdist > rng * seesub)
164             continue;
165         if (ship.shp_visib > vis[y][x]) {
166             vis[y][x] = ship.shp_visib;
167             /* &~0x20 makes it a cap letter */
168             rad[y][x] = (*mchr[(int)ship.shp_type].m_name) & ~0x20;
169         }
170     }
171     /* 
172      * make the center of the display 0
173      * so ve et al can find it.
174      */
175     rad[deltay(cy, ns.range.ly)][deltax(cx, ns.range.lx)] = '0';
176     /* won't work for radar maps > WORLD_Y/2 */
177 #ifdef HAY
178     /* This is not correct for small, hitech worlds. */
179     n = deltay(ns.range.hy, ns.range.ly);
180 #else
181     /* This is already available, so why not use it. */
182     n = ns.range.height;
183 #endif
184     for (row = 0; row < n; row++)
185         pr("%s\n", rad[row]);
186     pr("\n");
187 }
188
189 int
190 deltx(struct range *r, coord x)
191 {
192     if (r->lx < r->hx)
193         return x - r->lx;
194
195     if (x >= r->lx)
196         return x - r->lx;
197
198     return x + WORLD_X - r->lx;
199 }
200
201 int
202 delty(struct range *r, coord y)
203 {
204     if (r->ly < r->hy)
205         return y - r->ly;
206
207     if (y >= r->ly)
208         return y - r->ly;
209
210     return y + WORLD_Y - r->ly;
211 }