2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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:
52 static int blankrow(s_char *);
57 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 if (sect.sct_mines) {
149 pr("Sonar detects %d mines in %s!\n",
151 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 memset(visbuf, 0, (WORLD_Y * (WORLD_X + 1)));
160 snxtitem_dist(&nit, EF_SHIP, ship.shp_x, ship.shp_y, range);
161 while (nxtitem(&nit, &targ)) {
162 if (targ.shp_own == player->cnum || targ.shp_own == 0)
164 tmcp = &mchr[(int)targ.shp_type];
165 pingrange = min(7, max(targ.shp_visib, 10) * range / 10);
166 vrange = pingrange * ship.shp_effic / 200;
167 dist = mapdist(targ.shp_x, targ.shp_y, ship.shp_x, ship.shp_y);
168 pingrange = (max(pingrange, 2) * targ.shp_effic) / 100;
169 if (dist > pingrange)
171 if (tmcp->m_flags & M_SONAR && targ.shp_own) {
172 natp = getnatp(targ.shp_own);
173 if (natp->nat_flags & NF_SONAR)
175 "Sonar ping from %s detected by %s!\n",
176 xyas(ship.shp_x, ship.shp_y,
177 targ.shp_own), prship(&targ));
178 if (targ.shp_rflags & RET_SONARED) {
179 retreat_ship(&targ, 's');
180 putship(targ.shp_uid, &targ);
185 x = deltx(&ns.range, (int)targ.shp_x);
186 y = delty(&ns.range, (int)targ.shp_y);
187 if (rad[y][x] != dchr[SCT_WATER].d_mnem && rad[y][x] != 'X')
189 if (tmcp->m_flags & M_SUB &&
190 getrel(getnatp(targ.shp_own), player->cnum) < FRIENDLY) {
191 if (mcp->m_vrnge + targ.shp_visib < 8)
192 pr("Sonar detects sub #%d @ %s\n",
194 xyas(targ.shp_x, targ.shp_y, player->cnum));
195 else if (mcp->m_vrnge + targ.shp_visib < 10)
196 pr("Sonar detects %s @ %s\n",
198 xyas(targ.shp_x, targ.shp_y, player->cnum));
200 pr("Sonar detects %s %s @ %s\n", cname(targ.shp_own),
202 xyas(targ.shp_x, targ.shp_y, player->cnum));
204 pr("Sonar detects %s %s @ %s\n", cname(targ.shp_own),
206 xyas(targ.shp_x, targ.shp_y, player->cnum));
208 if (targ.shp_visib > vis[y][x]) {
209 vis[y][x] = targ.shp_visib;
210 /* &~0x20 makes it a cap letter */
211 rad[y][x] = (*mchr[(int)targ.shp_type].m_name) & ~0x20;
214 if (!player->argp[2]) {
216 for (row = 0; row < ns.range.height; row++)
217 if (!blankrow(rad[row]))
218 pr("%s\n", rad[row]);
224 writemap(player->cnum);
229 plane_sona(struct emp_qelem *plane_list, int x, int y,
230 struct shiplist **head)
233 struct plchrstr *pcp;
234 struct mchrstr *tmcp;
235 struct shpstr *targ, s;
237 struct emp_qelem *qp;
238 struct emp_qelem *next;
247 getsect(x, y, §);
248 if ((sect.sct_type != SCT_WATER) && (sect.sct_type != SCT_HARBR))
250 for (qp = plane_list->q_forw; qp != plane_list; qp = next) {
252 ip = (struct plist *)qp;
255 if (!(pcp->pl_flags & P_A)) /* if it isn't an ASW plane */
258 (int)techfact(pp->pln_tech,
259 (double)((100 - pp->pln_acc) / 10));
261 for (i=0; targ = getshipp(i); i++) {
263 for (i = 0; getship(i, &s); i++) {
265 if (targ->shp_own == pp->pln_own || targ->shp_own == 0)
267 if (on_shiplist(targ->shp_uid, *head))
269 tmcp = &mchr[(int)targ->shp_type];
270 if (!(tmcp->m_flags & M_SUB))
273 pln_identchance(pp, shp_hardtarget(targ), EF_SHIP))
275 pingrange = max(targ->shp_visib, 10) * range / 10;
276 vrange = ((float)pingrange) * ((float)pp->pln_effic / 200.0);
277 dist = mapdist(targ->shp_x, targ->shp_y, x, y);
278 pingrange = (max(pingrange, 2) * targ->shp_effic);
279 pingrange = roundavg(pingrange / 100.0);
280 if (dist > pingrange)
282 if (tmcp->m_flags & M_SONAR && targ->shp_own) {
283 natp = getnatp(targ->shp_own);
284 if (natp->nat_flags & NF_SONAR)
286 "Sonar ping from %s detected by %s!\n",
287 xyas(x, y, targ->shp_own), prship(targ));
291 add_shiplist(targ->shp_uid, head);
294 "\nSonar contact in %s\n", xyas(x, y, pp->pln_own));
297 if (getrel(getnatp(targ->shp_own), pp->pln_own) < FRIENDLY &&
298 roll(100) > pln_identchance(pp, shp_hardtarget(targ),
301 pln_identchance(pp, shp_hardtarget(targ), EF_SHIP))
302 mpr(pp->pln_own, "sub #%d %s\n", targ->shp_uid,
303 xyas(targ->shp_x, targ->shp_y, pp->pln_own));
308 xyas(targ->shp_x, targ->shp_y, pp->pln_own));
311 "%s %s @ %s\n", cname(targ->shp_own),
313 xyas(targ->shp_x, targ->shp_y, pp->pln_own));
319 * line_of_sight() - is there a "straight" all water path from (x,y) to (tx,ty)
320 * Ken & Irina Stevens, 1995
323 #define DOT(ax,ay,bx,by) ((ax)*(bx) + (ay)*(by))
324 #define LEN(x,y) ((x)*(x) + (y)*(y))
325 #define DIST(ax,ay,bx,by) LEN(bx - ax, by -ay)
328 line_of_sight(char **rad, int ax, int ay, int bx, int by)
330 int dxn = XNORM(bx - ax);
331 int dyn = YNORM(by - ay);
332 int dx = dxn > WORLD_X / 2 ? dxn - WORLD_X : dxn;
333 int dy = dyn > WORLD_Y / 2 ? dyn - WORLD_Y : dyn;
334 int dlen = LEN(dx, dy);
338 int tx, ty; /* test point */
339 double cd_dist = dlen; /* closest distance from c to vector d */
340 double md_dist; /* minimum distance from t to vector d */
341 double td_dist; /* distance from t to vector d */
342 double td_proj; /* the projection of t onto vector d */
343 int closest; /* index of closest */
345 struct sctstr *sectp;
350 md_dist = 100; /* will always be <= 2 */
352 for (n = 1; n <= 6; ++n) { /* Directions */
353 tx = cx + diroff[n][0];
354 ty = cy + diroff[n][1];
355 if (DIST(tx, ty, dx, dy) >= cd_dist)
357 td_proj = (double)DOT(tx, ty, dx, dy) / dlen;
358 td_dist = DIST(tx, ty, td_proj * dx, td_proj * dy);
359 if (td_dist < md_dist) {
364 if (CANT_HAPPEN(closest < 0))
366 cx = cx + diroff[closest][0];
367 cy = cy + diroff[closest][1];
369 blocked = (rad[YNORM(ay + cy)][XNORM(ax + cx)]
370 != dchr[SCT_WATER].d_mnem);
372 sectp = getsectp((ax + cx), (ay + cy));
374 if (sectp->sct_type == SCT_WATER ||
375 sectp->sct_type == SCT_BSPAN) {
382 cd_dist = DIST(cx, cy, dx, dy);