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