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