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
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.
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.
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
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.
28 * sona.c: Sonar from a sub (or other sonar-equipped ship)
30 * Known contributors to this file:
53 static int blankrow(s_char *);
58 struct nstr_item ni, nit;
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;
81 if (!snxtitem(&ni, EF_SHIP, player->argp[1]))
85 (s_char *)malloc((WORLD_Y * (WORLD_X + 1)) * sizeof(s_char));
88 (s_char *)malloc((WORLD_Y * (WORLD_X + 1)) * sizeof(s_char));
90 rad = (s_char **)malloc(WORLD_Y * sizeof(s_char *));
92 for (x = 0; x < WORLD_Y; x++) {
93 rad[x] = &radbuf[(WORLD_X + 1) * x];
101 vis = (s_char **)malloc(WORLD_Y * sizeof(s_char *));
103 for (x = 0; x < WORLD_Y; x++) {
104 vis[x] = &visbuf[(WORLD_X + 1) * x];
111 if (!radbuf || !visbuf || !rad || !vis) {
112 pr("Memory error, tell the deity.\n");
113 logerror("malloc failed in sona\n");
116 while (nxtitem(&ni, (s_char *)&ship)) {
119 mcp = &mchr[(int)ship.shp_type];
120 if (!(mcp->m_flags & M_SONAR))
122 getsect(ship.shp_x, ship.shp_y, §);
123 if (sect.sct_type != SCT_WATER)
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",
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, §)) {
134 if (player->owner || sect.sct_type == SCT_WATER)
135 rad[ns.dy][ns.dx] = dchr[sect.sct_type].d_mnem;
137 rad[ns.dy][ns.dx] = '?';
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, §)) {
144 if (!line_of_sight(rad, cx, cy, ns.dx, ns.dy)) {
145 rad[ns.dy][ns.dx] = ' ';
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",
152 xyas(sect.sct_x, sect.sct_y, player->cnum));
153 rad[ns.dy][ns.dx] = 'X';
156 changed |= map_set(player->cnum, sect.sct_x, sect.sct_y,
157 rad[ns.dy][ns.dx], 0);
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)) {
164 if (targ.shp_own == player->cnum || targ.shp_own == 0)
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)
173 if (tmcp->m_flags & M_SONAR && targ.shp_own) {
174 natp = getnatp(targ.shp_own);
175 if (natp->nat_flags & NF_SONAR)
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);
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')
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",
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",
200 xyas(targ.shp_x, targ.shp_y, player->cnum));
202 pr("Sonar detects %s %s @ %s\n", cname(targ.shp_own),
204 xyas(targ.shp_x, targ.shp_y, player->cnum));
206 pr("Sonar detects %s %s @ %s\n", cname(targ.shp_own),
208 xyas(targ.shp_x, targ.shp_y, player->cnum));
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;
216 if (!player->argp[2]) {
218 for (row = 0; row < ns.range.height; row++)
219 if (!blankrow(rad[row]))
220 pr("%s\n", rad[row]);
226 writemap(player->cnum);
231 plane_sona(struct emp_qelem *plane_list, int x, int y,
232 struct shiplook *head)
235 struct plchrstr *pcp;
236 struct mchrstr *tmcp;
237 struct shpstr *targ, s;
239 struct emp_qelem *qp;
240 struct emp_qelem *next;
249 getsect(x, y, §);
250 if ((sect.sct_type != SCT_WATER) && (sect.sct_type != SCT_HARBR))
252 for (qp = plane_list->q_forw; qp != plane_list; qp = next) {
254 ip = (struct plist *)qp;
257 if (!(pcp->pl_flags & P_A)) /* if it isn't an ASW plane */
260 (int)techfact(pp->pln_tech,
261 (double)((100 - pp->pln_acc) / 10));
263 for (i=0; targ = getshipp(i); i++) {
265 for (i = 0; getship(i, &s); i++) {
267 if (targ->shp_own == pp->pln_own || targ->shp_own == 0)
270 if (have_looked(targ->shp_uid,head))
273 if (have_found(targ->shp_uid, head))
275 set_have_looked(targ->shp_uid, head);
276 tmcp = &mchr[(int)targ->shp_type];
277 if (!(tmcp->m_flags & M_SUB))
280 pln_identchance(pp, shp_hardtarget(targ), EF_SHIP))
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)
289 if (tmcp->m_flags & M_SONAR && targ->shp_own) {
290 natp = getnatp(targ->shp_own);
291 if (natp->nat_flags & NF_SONAR)
293 "Sonar ping from %s detected by %s!\n",
294 xyas(x, y, targ->shp_own), prship(targ));
298 set_have_found(targ->shp_uid, head);
301 "\nSonar contact in %s\n", xyas(x, y, pp->pln_own));
304 if (getrel(getnatp(targ->shp_own), pp->pln_own) < FRIENDLY &&
305 roll(100) > pln_identchance(pp, shp_hardtarget(targ),
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));
315 xyas(targ->shp_x, targ->shp_y, pp->pln_own));
318 "%s %s @ %s\n", cname(targ->shp_own),
320 xyas(targ->shp_x, targ->shp_y, pp->pln_own));
326 * line_of_sight() - is there a "straight" all water path from (x,y) to (tx,ty)
327 * Ken & Irina Stevens, 1995
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)
335 line_of_sight(char **rad, int ax, int ay, int bx, int by)
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);
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 */
352 struct sctstr *sectp;
357 md_dist = 100; /* will always be <= 2 */
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)
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) {
371 if (CANT_HAPPEN(closest < 0))
373 cx = cx + diroff[closest][0];
374 cy = cy + diroff[closest][1];
376 blocked = (rad[YNORM(ay + cy)][XNORM(ax + cx)]
377 != dchr[SCT_WATER].d_mnem);
379 sectp = getsectp((ax + cx), (ay + cy));
381 if (sectp->sct_type == SCT_WATER ||
382 sectp->sct_type == SCT_BSPAN) {
389 cd_dist = DIST(cx, cy, dx, dy);