]> git.pond.sub.org Git - empserver/blob - src/lib/commands/sona.c
Indented with src/scripts/indent-emp.
[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 int
54 sona(void)
55 {
56     struct nstr_item ni, nit;
57     struct sctstr sect;
58     struct shpstr ship;
59     struct shpstr targ;
60     struct natstr *natp;
61     struct mchrstr *mcp;
62     struct mchrstr *tmcp;
63     struct nstr_sect ns;
64     int range;
65     int pingrange;
66     int srange;
67     int vrange;
68     int dist;
69     int x, y;
70     int cx, cy;
71     int mines;
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                 mines = getvar(V_MINE, (s_char *)&sect, EF_SECTOR);
149                 if (mines) {
150                     pr("Sonar detects %d mines in %s!\n",
151                        mines, 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         bzero((s_char *)visbuf, (WORLD_Y * (WORLD_X + 1)));
160         snxtitem_dist(&nit, EF_SHIP, ship.shp_x, ship.shp_y, range);
161         while (nxtitem(&nit, (caddr_t)&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 shiplook *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 /*
269                         if (have_looked(targ->shp_uid,head))
270                                 continue;
271 */
272             if (have_found(targ->shp_uid, head))
273                 continue;
274             set_have_looked(targ->shp_uid, head);
275             tmcp = &mchr[(int)targ->shp_type];
276             if (!(tmcp->m_flags & M_SUB))
277                 continue;
278             if (roll(100) >
279                 pln_identchance(pp, shp_hardtarget(targ), EF_SHIP))
280                 continue;
281             pingrange = max(targ->shp_visib, 10) * range / 10;
282             vrange = ((float)pingrange) * ((float)pp->pln_effic / 200.0);
283             dist = mapdist(targ->shp_x, targ->shp_y, x, y);
284             pingrange = (max(pingrange, 2) * targ->shp_effic);
285             pingrange = roundavg(pingrange / 100.0);
286             if (dist > pingrange)
287                 continue;
288             if (tmcp->m_flags & M_SONAR && targ->shp_own) {
289                 natp = getnatp(targ->shp_own);
290                 if (natp->nat_flags & NF_SONAR)
291                     wu(0, targ->shp_own,
292                        "Sonar ping from %s detected by %s!\n",
293                        xyas(x, y, targ->shp_own), prship(targ));
294             }
295             if ((dist > vrange))
296                 continue;
297             set_have_found(targ->shp_uid, head);
298             if (!found) {
299                 mpr(pp->pln_own,
300                     "\nSonar contact in %s\n", xyas(x, y, pp->pln_own));
301                 found = 1;
302             }
303             if (getrel(getnatp(targ->shp_own), pp->pln_own) < FRIENDLY &&
304                 roll(100) > pln_identchance(pp, shp_hardtarget(targ),
305                                             EF_SHIP))
306                 if (roll(100) >
307                     pln_identchance(pp, shp_hardtarget(targ), EF_SHIP))
308                     mpr(pp->pln_own, "sub #%d %s\n", targ->shp_uid,
309                         xyas(targ->shp_x, targ->shp_y, pp->pln_own));
310                 else
311                     mpr(pp->pln_own,
312                         "%s %s\n",
313                         prship(targ),
314                         xyas(targ->shp_x, targ->shp_y, pp->pln_own));
315             else
316                 mpr(pp->pln_own,
317                     "%s %s @ %s\n", cname(targ->shp_own),
318                     prship(targ),
319                     xyas(targ->shp_x, targ->shp_y, pp->pln_own));
320         }
321     }
322 }
323
324 /* 
325  * line_of_sight() - is there a "straight" all water path from (x,y) to (tx,ty)
326  * Ken & Irina Stevens, 1995
327  */
328
329 #define DOT(ax,ay,bx,by) ((ax)*(bx) + (ay)*(by))
330 #define LEN(x,y) ((x)*(x) + (y)*(y))
331 #define DIST(ax,ay,bx,by) LEN(bx - ax, by -ay)
332
333 int
334 line_of_sight(s_char **rad, int ax, int ay, int bx, int by)
335 {
336     int dx = bx - ax;
337     int dy = by - ay;
338     int dlen = LEN(dx, dy);
339     int n;
340     int cx = 0;
341     int cy = 0;
342     int tx, ty;                 /* test point */
343     double cd_dist = dlen;      /* closest distance from c to vector d */
344     double md_dist;             /* minimum distance from t to vector d */
345     double td_dist;             /* distance from t to vector d */
346     double td_proj;             /* the projection of t onto vector d */
347     int closest;                /* index of closest */
348     int blocked = 0;
349     struct sctstr *sectp;
350
351     while (cd_dist) {
352         if (blocked)
353             return 0;
354         md_dist = 100;          /* will always be <= 2 */
355         closest = -1;
356         for (n = 1; n <= 6; ++n) {      /* Directions */
357             tx = cx + diroff[n][0];
358             ty = cy + diroff[n][1];
359             if (DIST(tx, ty, dx, dy) >= cd_dist)
360                 continue;
361             td_proj = (double)DOT(tx, ty, dx, dy) / dlen;
362             td_dist = DIST(tx, ty, td_proj * dx, td_proj * dy);
363             if (td_dist < md_dist) {
364                 md_dist = td_dist;
365                 closest = n;
366             }
367         }
368         if (closest < 0)        /* not possible */
369             return 0;
370         cx = cx + diroff[closest][0];
371         cy = cy + diroff[closest][1];
372         if (rad) {
373             blocked = (rad[ay + cy][ax + cx] != dchr[SCT_WATER].d_mnem);
374         } else {
375             sectp = getsectp((ax + cx), (ay + cy));
376             if (sectp) {
377                 if (sectp->sct_type == SCT_WATER ||
378                     sectp->sct_type == SCT_BSPAN) {
379                     blocked = 0;
380                 } else {
381                     blocked = 1;
382                 }
383             }
384         }
385         cd_dist = DIST(cx, cy, dx, dy);
386     }
387     return 1;
388 }
389
390 int
391 blankrow(s_char *s)
392 {
393     while (*s) {
394         if (*s != ' ')
395             return 0;
396         ++s;
397     }
398     return 1;
399 }