]> 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-2008, 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 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]))
74         return RET_SYN;
75     if (!radbuf)
76         radbuf = malloc((WORLD_Y * (WORLD_X + 1)));
77     if (!visbuf)
78         visbuf = malloc((WORLD_Y * (WORLD_X + 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 = deltax(ship.shp_x, ns.range.lx);
126         cy = deltay(ship.shp_y, ns.range.ly);
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             pingrange = MIN(7, MAX(targ.shp_visib, 10) * range / 10);
151             vrange = pingrange * ship.shp_effic / 200;
152             dist = mapdist(targ.shp_x, targ.shp_y, ship.shp_x, ship.shp_y);
153             pingrange = (MAX(pingrange, 2) * targ.shp_effic) / 100;
154             if (dist > pingrange)
155                 continue;
156             if (tmcp->m_flags & M_SONAR && targ.shp_own) {
157                 natp = getnatp(targ.shp_own);
158                 if (natp->nat_flags & NF_SONAR)
159                     wu(0, targ.shp_own,
160                        "Sonar ping from %s detected by %s!\n",
161                        xyas(ship.shp_x, ship.shp_y,
162                             targ.shp_own), prship(&targ));
163                 if (targ.shp_rflags & RET_SONARED) {
164                     retreat_ship(&targ, 's');
165                     putship(targ.shp_uid, &targ);
166                 }
167             }
168             if (dist > vrange)
169                 continue;
170             x = deltx(&ns.range, (int)targ.shp_x);
171             y = delty(&ns.range, (int)targ.shp_y);
172             if (rad[y][x] != dchr[SCT_WATER].d_mnem && rad[y][x] != 'X')
173                 continue;
174             if (tmcp->m_flags & M_SUB &&
175                 getrel(getnatp(targ.shp_own), player->cnum) < FRIENDLY) {
176                 if (mcp->m_vrnge + targ.shp_visib < 8)
177                     pr("Sonar detects sub #%d @ %s\n",
178                        targ.shp_uid,
179                        xyas(targ.shp_x, targ.shp_y, player->cnum));
180                 else if (mcp->m_vrnge + targ.shp_visib < 10)
181                     pr("Sonar detects %s @ %s\n",
182                        prship(&targ),
183                        xyas(targ.shp_x, targ.shp_y, player->cnum));
184                 else
185                     pr("Sonar detects %s %s @ %s\n",
186                        cname(targ.shp_own), prship(&targ),
187                        xyas(targ.shp_x, targ.shp_y, player->cnum));
188             } else
189                 pr("Sonar detects %s %s @ %s\n",
190                    cname(targ.shp_own), prship(&targ),
191                    xyas(targ.shp_x, targ.shp_y, player->cnum));
192
193             if (targ.shp_visib > vis[y][x]) {
194                 vis[y][x] = targ.shp_visib;
195                 /* &~0x20 makes it a cap letter */
196                 rad[y][x] = (*mchr[(int)targ.shp_type].m_name) & ~0x20;
197             }
198         }
199         if (!player->argp[2]) {
200             rad[cy][cx] = '0';
201             for (row = 0; row < ns.range.height; row++)
202                 if (!blankrow(rad[row]))
203                     pr("%s\n", rad[row]);
204         }
205         pr("\n");
206
207     }
208     if (changed)
209         writemap(player->cnum);
210     return RET_OK;
211 }
212
213 void
214 plane_sona(struct emp_qelem *plane_list, int x, int y,
215            struct shiplist **head)
216 {
217     struct plnstr *pp;
218     struct plchrstr *pcp;
219     struct mchrstr *tmcp;
220     struct shpstr *targ, s;
221     struct natstr *natp;
222     struct emp_qelem *qp;
223     struct emp_qelem *next;
224     struct plist *ip;
225     struct sctstr sect;
226     int found = 0;
227     int range, i;
228     int pingrange;
229     int vrange;
230     int dist;
231
232     getsect(x, y, &sect);
233     if ((sect.sct_type != SCT_WATER) && (sect.sct_type != SCT_HARBR))
234         return;
235     for (qp = plane_list->q_forw; qp != plane_list; qp = next) {
236         next = qp->q_forw;
237         ip = (struct plist *)qp;
238         pp = &ip->plane;
239         pcp = ip->pcp;
240         if (!(pcp->pl_flags & P_A))     /* if it isn't an ASW plane */
241             continue;
242         range = (int)techfact(pp->pln_tech, (100.0 - pp->pln_acc) / 10.0);
243         for (i = 0; getship(i, &s); i++) {
244             targ = &s;
245             if (targ->shp_own == pp->pln_own || targ->shp_own == 0)
246                 continue;
247             if (on_shiplist(targ->shp_uid, *head))
248                 continue;
249             tmcp = &mchr[(int)targ->shp_type];
250             if (!(tmcp->m_flags & M_SUB))
251                 continue;
252             if (roll(100) > pln_identchance(pp, shp_hardtarget(targ),
253                                             EF_SHIP))
254                 continue;
255             pingrange = MAX(targ->shp_visib, 10) * range / 10;
256             vrange = pingrange * (pp->pln_effic / 200.0);
257             dist = mapdist(targ->shp_x, targ->shp_y, x, y);
258             pingrange = (MAX(pingrange, 2) * targ->shp_effic);
259             pingrange = roundavg(pingrange / 100.0);
260             if (dist > pingrange)
261                 continue;
262             if (tmcp->m_flags & M_SONAR && targ->shp_own) {
263                 natp = getnatp(targ->shp_own);
264                 if (natp->nat_flags & NF_SONAR)
265                     wu(0, targ->shp_own,
266                        "Sonar ping from %s detected by %s!\n",
267                        xyas(x, y, targ->shp_own), prship(targ));
268             }
269             if ((dist > vrange))
270                 continue;
271             add_shiplist(targ->shp_uid, head);
272             if (!found) {
273                 mpr(pp->pln_own,
274                     "\nSonar contact in %s\n", xyas(x, y, pp->pln_own));
275                 found = 1;
276             }
277             if (getrel(getnatp(targ->shp_own), pp->pln_own) < FRIENDLY &&
278                 roll(100) > pln_identchance(pp, shp_hardtarget(targ),
279                                             EF_SHIP))
280                 if (roll(100) > pln_identchance(pp, shp_hardtarget(targ),
281                                                 EF_SHIP))
282                     mpr(pp->pln_own, "sub #%d %s\n", targ->shp_uid,
283                         xyas(targ->shp_x, targ->shp_y, pp->pln_own));
284                 else
285                     mpr(pp->pln_own, "%s %s\n",
286                         prship(targ),
287                         xyas(targ->shp_x, targ->shp_y, pp->pln_own));
288             else
289                 mpr(pp->pln_own, "%s %s @ %s\n",
290                     cname(targ->shp_own), prship(targ),
291                     xyas(targ->shp_x, targ->shp_y, pp->pln_own));
292         }
293     }
294 }
295
296 /* 
297  * line_of_sight() - is there a "straight" all water path from (x,y) to (tx,ty)
298  * Ken & Irina Stevens, 1995
299  */
300
301 #define DOT(ax,ay,bx,by) ((ax)*(bx) + (ay)*(by))
302 #define LEN(x,y) ((x)*(x) + (y)*(y))
303 #define DIST(ax,ay,bx,by) LEN(bx - ax, by -ay)
304
305 int
306 line_of_sight(char **rad, int ax, int ay, int bx, int by)
307 {
308     int dxn = XNORM(bx - ax);
309     int dyn = YNORM(by - ay);
310     int dx = dxn > WORLD_X / 2 ? dxn - WORLD_X : dxn;
311     int dy = dyn > WORLD_Y / 2 ? dyn - WORLD_Y : dyn;
312     int dlen = LEN(dx, dy);
313     int n;
314     int cx = 0;
315     int cy = 0;
316     int tx, ty;                 /* test point */
317     double cd_dist = dlen;      /* closest distance from c to vector d */
318     double md_dist;             /* minimum distance from t to vector d */
319     double td_dist;             /* distance from t to vector d */
320     double td_proj;             /* the projection of t onto vector d */
321     int closest;                /* index of closest */
322     int blocked = 0;
323     struct sctstr *sectp;
324
325     while (cd_dist) {
326         if (blocked)
327             return 0;
328         md_dist = 100;          /* will always be <= 2 */
329         closest = -1;
330         for (n = 1; n <= 6; ++n) {      /* Directions */
331             tx = cx + diroff[n][0];
332             ty = cy + diroff[n][1];
333             if (DIST(tx, ty, dx, dy) >= cd_dist)
334                 continue;
335             td_proj = (double)DOT(tx, ty, dx, dy) / dlen;
336             td_dist = DIST(tx, ty, td_proj * dx, td_proj * dy);
337             if (td_dist < md_dist) {
338                 md_dist = td_dist;
339                 closest = n;
340             }
341         }
342         if (CANT_HAPPEN(closest < 0))
343             return 0;
344         cx = cx + diroff[closest][0];
345         cy = cy + diroff[closest][1];
346         if (rad) {
347             blocked = (rad[YNORM(ay + cy)][XNORM(ax + cx)]
348                        != dchr[SCT_WATER].d_mnem);
349         } else {
350             sectp = getsectp((ax + cx), (ay + cy));
351             if (sectp) {
352                 if (sectp->sct_type == SCT_WATER ||
353                     sectp->sct_type == SCT_BSPAN) {
354                     blocked = 0;
355                 } else {
356                     blocked = 1;
357                 }
358             }
359         }
360         cd_dist = DIST(cx, cy, dx, dy);
361     }
362     return 1;
363 }
364
365 static int
366 blankrow(char *s)
367 {
368     while (*s) {
369         if (*s != ' ')
370             return 0;
371         ++s;
372     }
373     return 1;
374 }