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