]> git.pond.sub.org Git - empserver/blob - src/lib/commands/sona.c
retreat: Fix group retreat after failed board sinks ship
[empserver] / src / lib / commands / sona.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  sona.c: Sonar from a sub (or other sonar-equipped ship)
28  *
29  *  Known contributors to this file:
30  *     Jim Griffith, 1989
31  *     Ken Stevens, 1995
32  *     Markus Armbruster, 2004-2012
33  */
34
35 #include <config.h>
36
37 #include "chance.h"
38 #include "commands.h"
39 #include "map.h"
40 #include "optlist.h"
41 #include "path.h"
42 #include "plane.h"
43 #include "retreat.h"
44 #include "ship.h"
45
46 static int blankrow(char *);
47
48 int
49 sona(void)
50 {
51     struct nstr_item ni, nit;
52     struct sctstr sect;
53     struct shpstr ship;
54     struct shpstr targ;
55     struct natstr *natp;
56     struct mchrstr *mcp;
57     struct mchrstr *tmcp;
58     struct nstr_sect ns;
59     int range;
60     int visib, pingrange;
61     int srange;
62     int vrange;
63     int dist;
64     int x, y;
65     int cx, cy;
66     int changed = 0;
67     int row;
68     /* Where these are used are non-re-entrant, so we keep 'em around */
69     static char **rad = NULL;
70     static char *radbuf = NULL;
71     static signed char **vis = NULL;
72     static signed char *visbuf = NULL;
73
74     if (!snxtitem(&ni, EF_SHIP, player->argp[1], NULL))
75         return RET_SYN;
76     if (!radbuf)
77         radbuf = malloc(WORLD_Y * MAPWIDTH(1));
78     if (!visbuf)
79         visbuf = malloc(WORLD_Y * MAPWIDTH(1));
80     if (!rad && radbuf) {
81         rad = malloc(WORLD_Y * sizeof(char *));
82         if (rad) {
83             for (x = 0; x < WORLD_Y; x++) {
84                 rad[x] = &radbuf[(WORLD_X + 1) * x];
85             }
86         }
87     }
88     if (!vis && visbuf) {
89         vis = malloc(WORLD_Y * sizeof(signed char *));
90         if (vis) {
91             for (x = 0; x < WORLD_Y; x++) {
92                 vis[x] = &visbuf[(WORLD_X + 1) * x];
93             }
94         }
95     }
96     if (!radbuf || !visbuf || !rad || !vis) {
97         pr("Memory error, tell the deity.\n");
98         logerror("malloc failed in sona\n");
99         return RET_FAIL;
100     }
101     while (nxtitem(&ni, &ship)) {
102         if (!player->owner)
103             continue;
104         mcp = &mchr[(int)ship.shp_type];
105         if (!(mcp->m_flags & M_SONAR))
106             continue;
107         getsect(ship.shp_x, ship.shp_y, &sect);
108         if (sect.sct_type != SCT_WATER)
109             continue;
110         range = (int)techfact(ship.shp_tech, mcp->m_vrnge);
111         srange = MIN(7, 7 * range * ship.shp_effic / 200);
112         pr("%s at %s efficiency %d%%, max range %d\n",
113            prship(&ship),
114            xyas(ship.shp_x, ship.shp_y, player->cnum),
115            ship.shp_effic, srange);
116         snxtsct_dist(&ns, ship.shp_x, ship.shp_y, srange);
117         blankfill(radbuf, &ns.range, 1);
118         while (nxtsct(&ns, &sect)) {
119             if (player->owner || sect.sct_type == SCT_WATER)
120                 rad[ns.dy][ns.dx] = dchr[sect.sct_type].d_mnem;
121             else {
122                 rad[ns.dy][ns.dx] = '?';
123             }
124         }
125         snxtsct_dist(&ns, ship.shp_x, ship.shp_y, srange);
126         cx = deltx(&ns.range, ship.shp_x);
127         cy = delty(&ns.range, ship.shp_y);
128         while (nxtsct(&ns, &sect)) {
129             if (!line_of_sight(rad, cx, cy, ns.dx, ns.dy)) {
130                 rad[ns.dy][ns.dx] = ' ';
131                 continue;
132             }
133             if (ship.shp_tech >= 310 && sect.sct_type == SCT_WATER) {
134                 if (sect.sct_mines) {
135                     pr("Sonar detects %d mines in %s!\n",
136                        sect.sct_mines,
137                        xyas(sect.sct_x, sect.sct_y, player->cnum));
138                     rad[ns.dy][ns.dx] = 'X';
139                 }
140             }
141             changed |= map_set(player->cnum, sect.sct_x, sect.sct_y,
142                                rad[ns.dy][ns.dx], 0);
143
144         }
145         memset(visbuf, 0, (WORLD_Y * (WORLD_X + 1)));
146         snxtitem_dist(&nit, EF_SHIP, ship.shp_x, ship.shp_y, range);
147         while (nxtitem(&nit, &targ)) {
148             if (targ.shp_own == player->cnum || targ.shp_own == 0)
149                 continue;
150             tmcp = &mchr[(int)targ.shp_type];
151             visib = shp_visib(&targ);
152             pingrange = MIN(7, MAX(visib, 10) * range / 10);
153             vrange = pingrange * ship.shp_effic / 200;
154             dist = mapdist(targ.shp_x, targ.shp_y, ship.shp_x, ship.shp_y);
155             pingrange = (MAX(pingrange, 2) * targ.shp_effic) / 100;
156             if (dist > pingrange)
157                 continue;
158             if (tmcp->m_flags & M_SONAR && targ.shp_own) {
159                 natp = getnatp(targ.shp_own);
160                 if (natp->nat_flags & NF_SONAR)
161                     wu(0, targ.shp_own,
162                        "Sonar ping from %s detected by %s!\n",
163                        xyas(ship.shp_x, ship.shp_y,
164                             targ.shp_own), prship(&targ));
165                 if (targ.shp_rflags & RET_SONARED) {
166                     retreat_ship(&targ, targ.shp_own, 's');
167                     putship(targ.shp_uid, &targ);
168                 }
169             }
170             if (dist > vrange)
171                 continue;
172             x = deltx(&ns.range, (int)targ.shp_x);
173             y = delty(&ns.range, (int)targ.shp_y);
174             if (rad[y][x] != dchr[SCT_WATER].d_mnem && rad[y][x] != 'X')
175                 continue;
176             if (tmcp->m_flags & M_SUB &&
177                 relations_with(targ.shp_own, player->cnum) < FRIENDLY) {
178                 if (mcp->m_vrnge + visib < 8)
179                     pr("Sonar detects sub #%d @ %s\n",
180                        targ.shp_uid,
181                        xyas(targ.shp_x, targ.shp_y, player->cnum));
182                 else if (mcp->m_vrnge + visib < 10)
183                     pr("Sonar detects %s @ %s\n",
184                        prship(&targ),
185                        xyas(targ.shp_x, targ.shp_y, player->cnum));
186                 else
187                     pr("Sonar detects %s %s @ %s\n",
188                        cname(targ.shp_own), prship(&targ),
189                        xyas(targ.shp_x, targ.shp_y, player->cnum));
190             } else
191                 pr("Sonar detects %s %s @ %s\n",
192                    cname(targ.shp_own), prship(&targ),
193                    xyas(targ.shp_x, targ.shp_y, player->cnum));
194
195             if (visib > vis[y][x]) {
196                 vis[y][x] = visib;
197                 /* &~0x20 makes it a cap letter */
198                 rad[y][x] = (*mchr[(int)targ.shp_type].m_name) & ~0x20;
199             }
200         }
201         if (!player->argp[2]) {
202             rad[cy][cx] = '0';
203             for (row = 0; row < ns.range.height; row++)
204                 if (!blankrow(rad[row]))
205                     pr("%s\n", rad[row]);
206         }
207         pr("\n");
208
209     }
210     if (changed)
211         writemap(player->cnum);
212     return RET_OK;
213 }
214
215 void
216 plane_sona(struct emp_qelem *plane_list, int x, int y,
217            struct shiplist **head)
218 {
219     struct plnstr *pp;
220     struct plchrstr *pcp;
221     struct mchrstr *tmcp;
222     struct shpstr *targ, s;
223     struct natstr *natp;
224     struct emp_qelem *qp;
225     struct emp_qelem *next;
226     struct plist *ip;
227     struct sctstr sect;
228     int found = 0;
229     int range, i, vis;
230     int pingrange;
231     int vrange;
232     int dist;
233
234     getsect(x, y, &sect);
235     if ((sect.sct_type != SCT_WATER) && (sect.sct_type != SCT_HARBR))
236         return;
237     for (qp = plane_list->q_forw; qp != plane_list; qp = next) {
238         next = qp->q_forw;
239         ip = (struct plist *)qp;
240         pp = &ip->plane;
241         pcp = ip->pcp;
242         if (!(pcp->pl_flags & P_A))     /* if it isn't an ASW plane */
243             continue;
244         range = (int)techfact(pp->pln_tech, (100.0 - pln_acc(pp)) / 10.0);
245         for (i = 0; getship(i, &s); i++) {
246             targ = &s;
247             if (targ->shp_own == pp->pln_own || targ->shp_own == 0)
248                 continue;
249             if (on_shiplist(targ->shp_uid, *head))
250                 continue;
251             tmcp = &mchr[(int)targ->shp_type];
252             if (!(tmcp->m_flags & M_SUB))
253                 continue;
254             if (!pct_chance(pln_identchance(pp, shp_hardtarget(targ),
255                                             EF_SHIP)))
256                 continue;
257             vis = shp_visib(targ);
258             pingrange = MAX(vis, 10) * range / 10;
259             vrange = pingrange * (pp->pln_effic / 200.0);
260             dist = mapdist(targ->shp_x, targ->shp_y, x, y);
261             pingrange = (MAX(pingrange, 2) * targ->shp_effic);
262             pingrange = roundavg(pingrange / 100.0);
263             if (dist > pingrange)
264                 continue;
265             if (tmcp->m_flags & M_SONAR && targ->shp_own) {
266                 natp = getnatp(targ->shp_own);
267                 if (natp->nat_flags & NF_SONAR)
268                     wu(0, targ->shp_own,
269                        "Sonar ping from %s detected by %s!\n",
270                        xyas(x, y, targ->shp_own), prship(targ));
271             }
272             if ((dist > vrange))
273                 continue;
274             add_shiplist(targ->shp_uid, head);
275             if (!found) {
276                 pr("Sonar contact in %s\n", xyas(x, y, player->cnum));
277                 found = 1;
278             }
279             if (relations_with(targ->shp_own, pp->pln_own) < FRIENDLY &&
280                 !pct_chance(pln_identchance(pp, shp_hardtarget(targ),
281                                             EF_SHIP)))
282                 if (!pct_chance(pln_identchance(pp, shp_hardtarget(targ),
283                                                 EF_SHIP)))
284                     pr("sub #%d %s\n", targ->shp_uid,
285                        xyas(targ->shp_x, targ->shp_y, player->cnum));
286                 else
287                     pr("%s %s\n", prship(targ),
288                        xyas(targ->shp_x, targ->shp_y, player->cnum));
289             else
290                 pr("%s %s @ %s\n", cname(targ->shp_own), prship(targ),
291                    xyas(targ->shp_x, targ->shp_y, player->cnum));
292         }
293     }
294     if (found)
295         pr("\n");
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 }