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
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:
56 struct nstr_item ni, nit;
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;
80 if (!snxtitem(&ni, EF_SHIP, player->argp[1]))
84 (s_char *)malloc((WORLD_Y * (WORLD_X + 1)) * sizeof(s_char));
87 (s_char *)malloc((WORLD_Y * (WORLD_X + 1)) * sizeof(s_char));
89 rad = (s_char **)malloc(WORLD_Y * sizeof(s_char *));
91 for (x = 0; x < WORLD_Y; x++) {
92 rad[x] = &radbuf[(WORLD_X + 1) * x];
100 vis = (s_char **)malloc(WORLD_Y * sizeof(s_char *));
102 for (x = 0; x < WORLD_Y; x++) {
103 vis[x] = &visbuf[(WORLD_X + 1) * x];
110 if (!radbuf || !visbuf || !rad || !vis) {
111 pr("Memory error, tell the deity.\n");
112 logerror("malloc failed in sona\n");
115 while (nxtitem(&ni, (s_char *)&ship)) {
118 mcp = &mchr[(int)ship.shp_type];
119 if (!(mcp->m_flags & M_SONAR))
121 getsect(ship.shp_x, ship.shp_y, §);
122 if (sect.sct_type != SCT_WATER)
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",
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, §)) {
133 if (player->owner || sect.sct_type == SCT_WATER)
134 rad[ns.dy][ns.dx] = dchr[sect.sct_type].d_mnem;
136 rad[ns.dy][ns.dx] = '?';
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, §)) {
143 if (!line_of_sight(rad, cx, cy, ns.dx, ns.dy)) {
144 rad[ns.dy][ns.dx] = ' ';
147 if (ship.shp_tech >= 310 && sect.sct_type == SCT_WATER) {
148 mines = getvar(V_MINE, (s_char *)§, EF_SECTOR);
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';
155 changed |= map_set(player->cnum, sect.sct_x, sect.sct_y,
156 rad[ns.dy][ns.dx], 0);
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)) {
163 if (targ.shp_own == player->cnum || targ.shp_own == 0)
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)
172 if (tmcp->m_flags & M_SONAR && targ.shp_own) {
173 natp = getnatp(targ.shp_own);
174 if (natp->nat_flags & NF_SONAR)
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);
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')
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",
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",
199 xyas(targ.shp_x, targ.shp_y, player->cnum));
201 pr("Sonar detects %s %s @ %s\n", cname(targ.shp_own),
203 xyas(targ.shp_x, targ.shp_y, player->cnum));
205 pr("Sonar detects %s %s @ %s\n", cname(targ.shp_own),
207 xyas(targ.shp_x, targ.shp_y, player->cnum));
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;
215 if (!player->argp[2]) {
217 for (row = 0; row < ns.range.height; row++)
218 if (!blankrow(rad[row]))
219 pr("%s\n", rad[row]);
225 writemap(player->cnum);
230 plane_sona(struct emp_qelem *plane_list, int x, int y,
231 struct shiplook *head)
234 struct plchrstr *pcp;
235 struct mchrstr *tmcp;
236 struct shpstr *targ, s;
238 struct emp_qelem *qp;
239 struct emp_qelem *next;
248 getsect(x, y, §);
249 if ((sect.sct_type != SCT_WATER) && (sect.sct_type != SCT_HARBR))
251 for (qp = plane_list->q_forw; qp != plane_list; qp = next) {
253 ip = (struct plist *)qp;
256 if (!(pcp->pl_flags & P_A)) /* if it isn't an ASW plane */
259 (int)techfact(pp->pln_tech,
260 (double)((100 - pp->pln_acc) / 10));
262 for (i=0; targ = getshipp(i); i++) {
264 for (i = 0; getship(i, &s); i++) {
266 if (targ->shp_own == pp->pln_own || targ->shp_own == 0)
269 if (have_looked(targ->shp_uid,head))
272 if (have_found(targ->shp_uid, head))
274 set_have_looked(targ->shp_uid, head);
275 tmcp = &mchr[(int)targ->shp_type];
276 if (!(tmcp->m_flags & M_SUB))
279 pln_identchance(pp, shp_hardtarget(targ), EF_SHIP))
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)
288 if (tmcp->m_flags & M_SONAR && targ->shp_own) {
289 natp = getnatp(targ->shp_own);
290 if (natp->nat_flags & NF_SONAR)
292 "Sonar ping from %s detected by %s!\n",
293 xyas(x, y, targ->shp_own), prship(targ));
297 set_have_found(targ->shp_uid, head);
300 "\nSonar contact in %s\n", xyas(x, y, pp->pln_own));
303 if (getrel(getnatp(targ->shp_own), pp->pln_own) < FRIENDLY &&
304 roll(100) > pln_identchance(pp, shp_hardtarget(targ),
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));
314 xyas(targ->shp_x, targ->shp_y, pp->pln_own));
317 "%s %s @ %s\n", cname(targ->shp_own),
319 xyas(targ->shp_x, targ->shp_y, pp->pln_own));
325 * line_of_sight() - is there a "straight" all water path from (x,y) to (tx,ty)
326 * Ken & Irina Stevens, 1995
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)
334 line_of_sight(s_char **rad, int ax, int ay, int bx, int by)
338 int dlen = LEN(dx, dy);
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 */
349 struct sctstr *sectp;
354 md_dist = 100; /* will always be <= 2 */
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)
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) {
368 if (closest < 0) /* not possible */
370 cx = cx + diroff[closest][0];
371 cy = cy + diroff[closest][1];
373 blocked = (rad[ay + cy][ax + cx] != dchr[SCT_WATER].d_mnem);
375 sectp = getsectp((ax + cx), (ay + cy));
377 if (sectp->sct_type == SCT_WATER ||
378 sectp->sct_type == SCT_BSPAN) {
385 cd_dist = DIST(cx, cy, dx, dy);