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