]> git.pond.sub.org Git - empserver/blob - src/lib/commands/sona.c
Update copyright notice
[empserver] / src / lib / commands / sona.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  *  sona.c: Sonar from a sub (or other sonar-equipped ship)
29  *
30  *  Known contributors to this file:
31  *     Jim Griffith, 1989
32  *     Ken Stevens, 1995
33  */
34
35 #include <config.h>
36
37 #include "commands.h"
38 #include "map.h"
39 #include "optlist.h"
40 #include "path.h"
41 #include "plane.h"
42 #include "retreat.h"
43 #include "ship.h"
44
45 static int blankrow(char *);
46
47 int
48 sona(void)
49 {
50     struct nstr_item ni, nit;
51     struct sctstr sect;
52     struct shpstr ship;
53     struct shpstr targ;
54     struct natstr *natp;
55     struct mchrstr *mcp;
56     struct mchrstr *tmcp;
57     struct nstr_sect ns;
58     int range;
59     int visib, pingrange;
60     int srange;
61     int vrange;
62     int dist;
63     int x, y;
64     int cx, cy;
65     int changed = 0;
66     int row;
67     /* Where these are used are non-re-entrant, so we keep 'em around */
68     static char **rad = NULL;
69     static char *radbuf = NULL;
70     static signed char **vis = NULL;
71     static signed char *visbuf = NULL;
72
73     if (!snxtitem(&ni, EF_SHIP, player->argp[1], NULL))
74         return RET_SYN;
75     if (!radbuf)
76         radbuf = malloc(WORLD_Y * MAPWIDTH(1));
77     if (!visbuf)
78         visbuf = malloc(WORLD_Y * MAPWIDTH(1));
79     if (!rad && radbuf) {
80         rad = malloc(WORLD_Y * sizeof(char *));
81         if (rad) {
82             for (x = 0; x < WORLD_Y; x++) {
83                 rad[x] = &radbuf[(WORLD_X + 1) * x];
84             }
85         }
86     }
87     if (!vis && visbuf) {
88         vis = malloc(WORLD_Y * sizeof(signed char *));
89         if (vis) {
90             for (x = 0; x < WORLD_Y; x++) {
91                 vis[x] = &visbuf[(WORLD_X + 1) * x];
92             }
93         }
94     }
95     if (!radbuf || !visbuf || !rad || !vis) {
96         pr("Memory error, tell the deity.\n");
97         logerror("malloc failed in sona\n");
98         return RET_FAIL;
99     }
100     while (nxtitem(&ni, &ship)) {
101         if (!player->owner)
102             continue;
103         mcp = &mchr[(int)ship.shp_type];
104         if (!(mcp->m_flags & M_SONAR))
105             continue;
106         getsect(ship.shp_x, ship.shp_y, &sect);
107         if (sect.sct_type != SCT_WATER)
108             continue;
109         range = (int)techfact(ship.shp_tech, mcp->m_vrnge);
110         srange = MIN(7, 7 * range * ship.shp_effic / 200);
111         pr("%s at %s efficiency %d%%, max range %d\n",
112            prship(&ship),
113            xyas(ship.shp_x, ship.shp_y, player->cnum),
114            ship.shp_effic, srange);
115         snxtsct_dist(&ns, ship.shp_x, ship.shp_y, srange);
116         blankfill(radbuf, &ns.range, 1);
117         while (nxtsct(&ns, &sect)) {
118             if (player->owner || sect.sct_type == SCT_WATER)
119                 rad[ns.dy][ns.dx] = dchr[sect.sct_type].d_mnem;
120             else {
121                 rad[ns.dy][ns.dx] = '?';
122             }
123         }
124         snxtsct_dist(&ns, ship.shp_x, ship.shp_y, srange);
125         cx = deltx(&ns.range, ship.shp_x);
126         cy = delty(&ns.range, ship.shp_y);
127         while (nxtsct(&ns, &sect)) {
128             if (!line_of_sight(rad, cx, cy, ns.dx, ns.dy)) {
129                 rad[ns.dy][ns.dx] = ' ';
130                 continue;
131             }
132             if (ship.shp_tech >= 310 && sect.sct_type == SCT_WATER) {
133                 if (sect.sct_mines) {
134                     pr("Sonar detects %d mines in %s!\n",
135                        sect.sct_mines,
136                        xyas(sect.sct_x, sect.sct_y, player->cnum));
137                     rad[ns.dy][ns.dx] = 'X';
138                 }
139             }
140             changed |= map_set(player->cnum, sect.sct_x, sect.sct_y,
141                                rad[ns.dy][ns.dx], 0);
142
143         }
144         memset(visbuf, 0, (WORLD_Y * (WORLD_X + 1)));
145         snxtitem_dist(&nit, EF_SHIP, ship.shp_x, ship.shp_y, range);
146         while (nxtitem(&nit, &targ)) {
147             if (targ.shp_own == player->cnum || targ.shp_own == 0)
148                 continue;
149             tmcp = &mchr[(int)targ.shp_type];
150             visib = shp_visib(&targ);
151             pingrange = MIN(7, MAX(visib, 10) * range / 10);
152             vrange = pingrange * ship.shp_effic / 200;
153             dist = mapdist(targ.shp_x, targ.shp_y, ship.shp_x, ship.shp_y);
154             pingrange = (MAX(pingrange, 2) * targ.shp_effic) / 100;
155             if (dist > pingrange)
156                 continue;
157             if (tmcp->m_flags & M_SONAR && targ.shp_own) {
158                 natp = getnatp(targ.shp_own);
159                 if (natp->nat_flags & NF_SONAR)
160                     wu(0, targ.shp_own,
161                        "Sonar ping from %s detected by %s!\n",
162                        xyas(ship.shp_x, ship.shp_y,
163                             targ.shp_own), prship(&targ));
164                 if (targ.shp_rflags & RET_SONARED) {
165                     retreat_ship(&targ, 's');
166                     putship(targ.shp_uid, &targ);
167                 }
168             }
169             if (dist > vrange)
170                 continue;
171             x = deltx(&ns.range, (int)targ.shp_x);
172             y = delty(&ns.range, (int)targ.shp_y);
173             if (rad[y][x] != dchr[SCT_WATER].d_mnem && rad[y][x] != 'X')
174                 continue;
175             if (tmcp->m_flags & M_SUB &&
176                 getrel(getnatp(targ.shp_own), player->cnum) < FRIENDLY) {
177                 if (mcp->m_vrnge + visib < 8)
178                     pr("Sonar detects sub #%d @ %s\n",
179                        targ.shp_uid,
180                        xyas(targ.shp_x, targ.shp_y, player->cnum));
181                 else if (mcp->m_vrnge + visib < 10)
182                     pr("Sonar detects %s @ %s\n",
183                        prship(&targ),
184                        xyas(targ.shp_x, targ.shp_y, player->cnum));
185                 else
186                     pr("Sonar detects %s %s @ %s\n",
187                        cname(targ.shp_own), prship(&targ),
188                        xyas(targ.shp_x, targ.shp_y, player->cnum));
189             } else
190                 pr("Sonar detects %s %s @ %s\n",
191                    cname(targ.shp_own), prship(&targ),
192                    xyas(targ.shp_x, targ.shp_y, player->cnum));
193
194             if (visib > vis[y][x]) {
195                 vis[y][x] = visib;
196                 /* &~0x20 makes it a cap letter */
197                 rad[y][x] = (*mchr[(int)targ.shp_type].m_name) & ~0x20;
198             }
199         }
200         if (!player->argp[2]) {
201             rad[cy][cx] = '0';
202             for (row = 0; row < ns.range.height; row++)
203                 if (!blankrow(rad[row]))
204                     pr("%s\n", rad[row]);
205         }
206         pr("\n");
207
208     }
209     if (changed)
210         writemap(player->cnum);
211     return RET_OK;
212 }
213
214 void
215 plane_sona(struct emp_qelem *plane_list, int x, int y,
216            struct shiplist **head)
217 {
218     struct plnstr *pp;
219     struct plchrstr *pcp;
220     struct mchrstr *tmcp;
221     struct shpstr *targ, s;
222     struct natstr *natp;
223     struct emp_qelem *qp;
224     struct emp_qelem *next;
225     struct plist *ip;
226     struct sctstr sect;
227     int found = 0;
228     int range, i, vis;
229     int pingrange;
230     int vrange;
231     int dist;
232
233     getsect(x, y, &sect);
234     if ((sect.sct_type != SCT_WATER) && (sect.sct_type != SCT_HARBR))
235         return;
236     for (qp = plane_list->q_forw; qp != plane_list; qp = next) {
237         next = qp->q_forw;
238         ip = (struct plist *)qp;
239         pp = &ip->plane;
240         pcp = ip->pcp;
241         if (!(pcp->pl_flags & P_A))     /* if it isn't an ASW plane */
242             continue;
243         range = (int)techfact(pp->pln_tech, (100.0 - pln_acc(pp)) / 10.0);
244         for (i = 0; getship(i, &s); i++) {
245             targ = &s;
246             if (targ->shp_own == pp->pln_own || targ->shp_own == 0)
247                 continue;
248             if (on_shiplist(targ->shp_uid, *head))
249                 continue;
250             tmcp = &mchr[(int)targ->shp_type];
251             if (!(tmcp->m_flags & M_SUB))
252                 continue;
253             if (roll(100) > pln_identchance(pp, shp_hardtarget(targ),
254                                             EF_SHIP))
255                 continue;
256             vis = shp_visib(targ);
257             pingrange = MAX(vis, 10) * range / 10;
258             vrange = pingrange * (pp->pln_effic / 200.0);
259             dist = mapdist(targ->shp_x, targ->shp_y, x, y);
260             pingrange = (MAX(pingrange, 2) * targ->shp_effic);
261             pingrange = roundavg(pingrange / 100.0);
262             if (dist > pingrange)
263                 continue;
264             if (tmcp->m_flags & M_SONAR && targ->shp_own) {
265                 natp = getnatp(targ->shp_own);
266                 if (natp->nat_flags & NF_SONAR)
267                     wu(0, targ->shp_own,
268                        "Sonar ping from %s detected by %s!\n",
269                        xyas(x, y, targ->shp_own), prship(targ));
270             }
271             if ((dist > vrange))
272                 continue;
273             add_shiplist(targ->shp_uid, head);
274             if (!found) {
275                 mpr(pp->pln_own,
276                     "\nSonar contact in %s\n", xyas(x, y, pp->pln_own));
277                 found = 1;
278             }
279             if (getrel(getnatp(targ->shp_own), pp->pln_own) < FRIENDLY &&
280                 roll(100) > pln_identchance(pp, shp_hardtarget(targ),
281                                             EF_SHIP))
282                 if (roll(100) > pln_identchance(pp, shp_hardtarget(targ),
283                                                 EF_SHIP))
284                     mpr(pp->pln_own, "sub #%d %s\n", targ->shp_uid,
285                         xyas(targ->shp_x, targ->shp_y, pp->pln_own));
286                 else
287                     mpr(pp->pln_own, "%s %s\n",
288                         prship(targ),
289                         xyas(targ->shp_x, targ->shp_y, pp->pln_own));
290             else
291                 mpr(pp->pln_own, "%s %s @ %s\n",
292                     cname(targ->shp_own), prship(targ),
293                     xyas(targ->shp_x, targ->shp_y, pp->pln_own));
294         }
295     }
296 }
297
298 /*
299  * line_of_sight() - is there a "straight" all water path from (x,y) to (tx,ty)
300  * Ken & Irina Stevens, 1995
301  */
302
303 #define DOT(ax,ay,bx,by) ((ax)*(bx) + (ay)*(by))
304 #define LEN(x,y) ((x)*(x) + (y)*(y))
305 #define DIST(ax,ay,bx,by) LEN(bx - ax, by -ay)
306
307 int
308 line_of_sight(char **rad, int ax, int ay, int bx, int by)
309 {
310     int dxn = XNORM(bx - ax);
311     int dyn = YNORM(by - ay);
312     int dx = dxn > WORLD_X / 2 ? dxn - WORLD_X : dxn;
313     int dy = dyn > WORLD_Y / 2 ? dyn - WORLD_Y : dyn;
314     int dlen = LEN(dx, dy);
315     int n;
316     int cx = 0;
317     int cy = 0;
318     int tx, ty;                 /* test point */
319     double cd_dist = dlen;      /* closest distance from c to vector d */
320     double md_dist;             /* minimum distance from t to vector d */
321     double td_dist;             /* distance from t to vector d */
322     double td_proj;             /* the projection of t onto vector d */
323     int closest;                /* index of closest */
324     int blocked = 0;
325     struct sctstr *sectp;
326
327     while (cd_dist) {
328         if (blocked)
329             return 0;
330         md_dist = 100;          /* will always be <= 2 */
331         closest = -1;
332         for (n = 1; n <= 6; ++n) {      /* Directions */
333             tx = cx + diroff[n][0];
334             ty = cy + diroff[n][1];
335             if (DIST(tx, ty, dx, dy) >= cd_dist)
336                 continue;
337             td_proj = (double)DOT(tx, ty, dx, dy) / dlen;
338             td_dist = DIST(tx, ty, td_proj * dx, td_proj * dy);
339             if (td_dist < md_dist) {
340                 md_dist = td_dist;
341                 closest = n;
342             }
343         }
344         if (CANT_HAPPEN(closest < 0))
345             return 0;
346         cx = cx + diroff[closest][0];
347         cy = cy + diroff[closest][1];
348         if (rad) {
349             blocked = (rad[YNORM(ay + cy)][XNORM(ax + cx)]
350                        != dchr[SCT_WATER].d_mnem);
351         } else {
352             sectp = getsectp((ax + cx), (ay + cy));
353             if (sectp) {
354                 if (sectp->sct_type == SCT_WATER ||
355                     sectp->sct_type == SCT_BSPAN) {
356                     blocked = 0;
357                 } else {
358                     blocked = 1;
359                 }
360             }
361         }
362         cd_dist = DIST(cx, cy, dx, dy);
363     }
364     return 1;
365 }
366
367 static int
368 blankrow(char *s)
369 {
370     while (*s) {
371         if (*s != ' ')
372             return 0;
373         ++s;
374     }
375     return 1;
376 }