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