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