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:
53 static int blankrow(s_char *);
58 struct nstr_item ni, nit;
76 /* Where these are used are non-re-entrant, so we keep 'em around */
77 static s_char **rad = (s_char **)0;
78 static s_char *radbuf = (s_char *)0;
79 static s_char **vis = (s_char **)0;
80 static s_char *visbuf = (s_char *)0;
82 if (!snxtitem(&ni, EF_SHIP, player->argp[1]))
86 (s_char *)malloc((WORLD_Y * (WORLD_X + 1)) * sizeof(s_char));
89 (s_char *)malloc((WORLD_Y * (WORLD_X + 1)) * sizeof(s_char));
91 rad = (s_char **)malloc(WORLD_Y * sizeof(s_char *));
93 for (x = 0; x < WORLD_Y; x++) {
94 rad[x] = &radbuf[(WORLD_X + 1) * x];
102 vis = (s_char **)malloc(WORLD_Y * sizeof(s_char *));
104 for (x = 0; x < WORLD_Y; x++) {
105 vis[x] = &visbuf[(WORLD_X + 1) * x];
112 if (!radbuf || !visbuf || !rad || !vis) {
113 pr("Memory error, tell the deity.\n");
114 logerror("malloc failed in sona\n");
117 while (nxtitem(&ni, (s_char *)&ship)) {
120 mcp = &mchr[(int)ship.shp_type];
121 if (!(mcp->m_flags & M_SONAR))
123 getsect(ship.shp_x, ship.shp_y, §);
124 if (sect.sct_type != SCT_WATER)
126 range = (int)techfact(ship.shp_tech, (double)mcp->m_vrnge);
127 srange = min(7, 7 * range * ship.shp_effic / 200);
128 pr("%s at %s efficiency %d%%, max range %d\n",
130 xyas(ship.shp_x, ship.shp_y, player->cnum),
131 ship.shp_effic, srange);
132 snxtsct_dist(&ns, ship.shp_x, ship.shp_y, srange);
133 blankfill((s_char *)radbuf, &ns.range, 1);
134 while (nxtsct(&ns, §)) {
135 if (player->owner || sect.sct_type == SCT_WATER)
136 rad[ns.dy][ns.dx] = dchr[sect.sct_type].d_mnem;
138 rad[ns.dy][ns.dx] = '?';
141 snxtsct_dist(&ns, ship.shp_x, ship.shp_y, srange);
142 cx = deltax(ship.shp_x, ns.range.lx);
143 cy = deltay(ship.shp_y, ns.range.ly);
144 while (nxtsct(&ns, §)) {
145 if (!line_of_sight(rad, cx, cy, ns.dx, ns.dy)) {
146 rad[ns.dy][ns.dx] = ' ';
149 if (ship.shp_tech >= 310 && sect.sct_type == SCT_WATER) {
150 mines = getvar(V_MINE, (s_char *)§, EF_SECTOR);
152 pr("Sonar detects %d mines in %s!\n",
153 mines, xyas(sect.sct_x, sect.sct_y, player->cnum));
154 rad[ns.dy][ns.dx] = 'X';
157 changed |= map_set(player->cnum, sect.sct_x, sect.sct_y,
158 rad[ns.dy][ns.dx], 0);
161 memset(visbuf, 0, (WORLD_Y * (WORLD_X + 1)));
162 snxtitem_dist(&nit, EF_SHIP, ship.shp_x, ship.shp_y, range);
163 while (nxtitem(&nit, (caddr_t)&targ)) {
165 if (targ.shp_own == player->cnum || targ.shp_own == 0)
167 tmcp = &mchr[(int)targ.shp_type];
168 pingrange = min(7, max(targ.shp_visib, 10) * range / 10);
169 vrange = pingrange * ship.shp_effic / 200;
170 dist = mapdist(targ.shp_x, targ.shp_y, ship.shp_x, ship.shp_y);
171 pingrange = (max(pingrange, 2) * targ.shp_effic) / 100;
172 if (dist > pingrange)
174 if (tmcp->m_flags & M_SONAR && targ.shp_own) {
175 natp = getnatp(targ.shp_own);
176 if (natp->nat_flags & NF_SONAR)
178 "Sonar ping from %s detected by %s!\n",
179 xyas(ship.shp_x, ship.shp_y,
180 targ.shp_own), prship(&targ));
181 if (targ.shp_rflags & RET_SONARED) {
182 retreat_ship(&targ, 's');
183 putship(targ.shp_uid, &targ);
188 x = deltx(&ns.range, (int)targ.shp_x);
189 y = delty(&ns.range, (int)targ.shp_y);
190 if (rad[y][x] != dchr[SCT_WATER].d_mnem && rad[y][x] != 'X')
192 if (tmcp->m_flags & M_SUB &&
193 getrel(getnatp(targ.shp_own), player->cnum) < FRIENDLY) {
194 if (mcp->m_vrnge + targ.shp_visib < 8)
195 pr("Sonar detects sub #%d @ %s\n",
197 xyas(targ.shp_x, targ.shp_y, player->cnum));
198 else if (mcp->m_vrnge + targ.shp_visib < 10)
199 pr("Sonar detects %s @ %s\n",
201 xyas(targ.shp_x, targ.shp_y, player->cnum));
203 pr("Sonar detects %s %s @ %s\n", cname(targ.shp_own),
205 xyas(targ.shp_x, targ.shp_y, player->cnum));
207 pr("Sonar detects %s %s @ %s\n", cname(targ.shp_own),
209 xyas(targ.shp_x, targ.shp_y, player->cnum));
211 if (targ.shp_visib > vis[y][x]) {
212 vis[y][x] = targ.shp_visib;
213 /* &~0x20 makes it a cap letter */
214 rad[y][x] = (*mchr[(int)targ.shp_type].m_name) & ~0x20;
217 if (!player->argp[2]) {
219 for (row = 0; row < ns.range.height; row++)
220 if (!blankrow(rad[row]))
221 pr("%s\n", rad[row]);
227 writemap(player->cnum);
232 plane_sona(struct emp_qelem *plane_list, int x, int y,
233 struct shiplook *head)
236 struct plchrstr *pcp;
237 struct mchrstr *tmcp;
238 struct shpstr *targ, s;
240 struct emp_qelem *qp;
241 struct emp_qelem *next;
250 getsect(x, y, §);
251 if ((sect.sct_type != SCT_WATER) && (sect.sct_type != SCT_HARBR))
253 for (qp = plane_list->q_forw; qp != plane_list; qp = next) {
255 ip = (struct plist *)qp;
258 if (!(pcp->pl_flags & P_A)) /* if it isn't an ASW plane */
261 (int)techfact(pp->pln_tech,
262 (double)((100 - pp->pln_acc) / 10));
264 for (i=0; targ = getshipp(i); i++) {
266 for (i = 0; getship(i, &s); i++) {
268 if (targ->shp_own == pp->pln_own || targ->shp_own == 0)
271 if (have_looked(targ->shp_uid,head))
274 if (have_found(targ->shp_uid, head))
276 set_have_looked(targ->shp_uid, head);
277 tmcp = &mchr[(int)targ->shp_type];
278 if (!(tmcp->m_flags & M_SUB))
281 pln_identchance(pp, shp_hardtarget(targ), EF_SHIP))
283 pingrange = max(targ->shp_visib, 10) * range / 10;
284 vrange = ((float)pingrange) * ((float)pp->pln_effic / 200.0);
285 dist = mapdist(targ->shp_x, targ->shp_y, x, y);
286 pingrange = (max(pingrange, 2) * targ->shp_effic);
287 pingrange = roundavg(pingrange / 100.0);
288 if (dist > pingrange)
290 if (tmcp->m_flags & M_SONAR && targ->shp_own) {
291 natp = getnatp(targ->shp_own);
292 if (natp->nat_flags & NF_SONAR)
294 "Sonar ping from %s detected by %s!\n",
295 xyas(x, y, targ->shp_own), prship(targ));
299 set_have_found(targ->shp_uid, head);
302 "\nSonar contact in %s\n", xyas(x, y, pp->pln_own));
305 if (getrel(getnatp(targ->shp_own), pp->pln_own) < FRIENDLY &&
306 roll(100) > pln_identchance(pp, shp_hardtarget(targ),
309 pln_identchance(pp, shp_hardtarget(targ), EF_SHIP))
310 mpr(pp->pln_own, "sub #%d %s\n", targ->shp_uid,
311 xyas(targ->shp_x, targ->shp_y, pp->pln_own));
316 xyas(targ->shp_x, targ->shp_y, pp->pln_own));
319 "%s %s @ %s\n", cname(targ->shp_own),
321 xyas(targ->shp_x, targ->shp_y, pp->pln_own));
327 * line_of_sight() - is there a "straight" all water path from (x,y) to (tx,ty)
328 * Ken & Irina Stevens, 1995
331 #define DOT(ax,ay,bx,by) ((ax)*(bx) + (ay)*(by))
332 #define LEN(x,y) ((x)*(x) + (y)*(y))
333 #define DIST(ax,ay,bx,by) LEN(bx - ax, by -ay)
336 line_of_sight(s_char **rad, int ax, int ay, int bx, int by)
340 int dlen = LEN(dx, dy);
344 int tx, ty; /* test point */
345 double cd_dist = dlen; /* closest distance from c to vector d */
346 double md_dist; /* minimum distance from t to vector d */
347 double td_dist; /* distance from t to vector d */
348 double td_proj; /* the projection of t onto vector d */
349 int closest; /* index of closest */
351 struct sctstr *sectp;
356 md_dist = 100; /* will always be <= 2 */
358 for (n = 1; n <= 6; ++n) { /* Directions */
359 tx = cx + diroff[n][0];
360 ty = cy + diroff[n][1];
361 if (DIST(tx, ty, dx, dy) >= cd_dist)
363 td_proj = (double)DOT(tx, ty, dx, dy) / dlen;
364 td_dist = DIST(tx, ty, td_proj * dx, td_proj * dy);
365 if (td_dist < md_dist) {
370 if (closest < 0) /* not possible */
372 cx = cx + diroff[closest][0];
373 cy = cy + diroff[closest][1];
375 blocked = (rad[ay + cy][ax + cx] != dchr[SCT_WATER].d_mnem);
377 sectp = getsectp((ax + cx), (ay + cy));
379 if (sectp->sct_type == SCT_WATER ||
380 sectp->sct_type == SCT_BSPAN) {
387 cd_dist = DIST(cx, cy, dx, dy);