2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2017, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * sona.c: Sonar from a sub (or other sonar-equipped ship)
29 * Known contributors to this file:
32 * Markus Armbruster, 2004-2015
46 static int blankrow(char *);
51 struct nstr_item ni, nit;
68 /* Where these are used are non-re-entrant, so we keep 'em around */
69 static char **rad = NULL;
70 static char *radbuf = NULL;
71 static signed char **vis = NULL;
72 static signed char *visbuf = NULL;
74 if (!snxtitem(&ni, EF_SHIP, player->argp[1], NULL))
77 radbuf = malloc(WORLD_Y * MAPWIDTH(1));
79 visbuf = malloc(WORLD_Y * MAPWIDTH(1));
81 rad = malloc(WORLD_Y * sizeof(char *));
83 for (x = 0; x < WORLD_Y; x++) {
84 rad[x] = &radbuf[(WORLD_X + 1) * x];
89 vis = malloc(WORLD_Y * sizeof(signed char *));
91 for (x = 0; x < WORLD_Y; x++) {
92 vis[x] = &visbuf[(WORLD_X + 1) * x];
96 if (!radbuf || !visbuf || !rad || !vis) {
97 pr("Memory error, tell the deity.\n");
98 logerror("malloc failed in sona\n");
101 while (nxtitem(&ni, &ship)) {
104 mcp = &mchr[(int)ship.shp_type];
105 if (!(mcp->m_flags & M_SONAR))
107 getsect(ship.shp_x, ship.shp_y, §);
108 if (sect.sct_type != SCT_WATER)
110 range = (int)techfact(ship.shp_tech, mcp->m_vrnge);
111 srange = MIN(7, 7 * range * ship.shp_effic / 200);
112 pr("%s at %s efficiency %d%%, max range %d\n",
114 xyas(ship.shp_x, ship.shp_y, player->cnum),
115 ship.shp_effic, srange);
116 snxtsct_dist(&ns, ship.shp_x, ship.shp_y, srange);
117 blankfill(radbuf, &ns.range, 1);
118 while (nxtsct(&ns, §)) {
119 if (player->owner || sect.sct_type == SCT_WATER)
120 rad[ns.dy][ns.dx] = dchr[sect.sct_type].d_mnem;
122 rad[ns.dy][ns.dx] = '?';
125 snxtsct_dist(&ns, ship.shp_x, ship.shp_y, srange);
126 cx = deltx(&ns.range, ship.shp_x);
127 cy = delty(&ns.range, ship.shp_y);
128 while (nxtsct(&ns, §)) {
129 if (!line_of_sight(rad, cx, cy, ns.dx, ns.dy)) {
130 rad[ns.dy][ns.dx] = ' ';
133 if (ship.shp_tech >= 310 && sect.sct_type == SCT_WATER) {
134 if (sect.sct_mines) {
135 pr("Sonar detects %d mines in %s!\n",
137 xyas(sect.sct_x, sect.sct_y, player->cnum));
138 rad[ns.dy][ns.dx] = 'X';
141 changed |= map_set(player->cnum, sect.sct_x, sect.sct_y,
142 rad[ns.dy][ns.dx], 0);
145 memset(visbuf, 0, (WORLD_Y * (WORLD_X + 1)));
146 snxtitem_dist(&nit, EF_SHIP, ship.shp_x, ship.shp_y, range);
147 while (nxtitem(&nit, &targ)) {
148 if (targ.shp_own == player->cnum || targ.shp_own == 0)
150 tmcp = &mchr[(int)targ.shp_type];
151 visib = shp_visib(&targ);
152 pingrange = MIN(7, MAX(visib, 10) * range / 10);
153 vrange = pingrange * ship.shp_effic / 200;
154 dist = mapdist(targ.shp_x, targ.shp_y, ship.shp_x, ship.shp_y);
155 pingrange = (MAX(pingrange, 2) * targ.shp_effic) / 100;
156 if (dist > pingrange)
158 if (tmcp->m_flags & M_SONAR && targ.shp_own) {
159 natp = getnatp(targ.shp_own);
160 if (natp->nat_flags & NF_SONAR)
162 "Sonar ping from %s detected by %s!\n",
163 xyas(ship.shp_x, ship.shp_y,
164 targ.shp_own), prship(&targ));
165 if (targ.shp_rflags & RET_SONARED)
166 retreat_ship(&targ, targ.shp_own, 's');
170 x = deltx(&ns.range, (int)targ.shp_x);
171 y = delty(&ns.range, (int)targ.shp_y);
172 if (rad[y][x] != dchr[SCT_WATER].d_mnem && rad[y][x] != 'X')
174 if (tmcp->m_flags & M_SUB &&
175 relations_with(targ.shp_own, player->cnum) < FRIENDLY) {
176 if (mcp->m_vrnge + visib < 8)
177 pr("Sonar detects sub #%d @ %s\n",
179 xyas(targ.shp_x, targ.shp_y, player->cnum));
180 else if (mcp->m_vrnge + visib < 10)
181 pr("Sonar detects %s @ %s\n",
183 xyas(targ.shp_x, targ.shp_y, player->cnum));
185 pr("Sonar detects %s %s @ %s\n",
186 cname(targ.shp_own), prship(&targ),
187 xyas(targ.shp_x, targ.shp_y, player->cnum));
189 pr("Sonar detects %s %s @ %s\n",
190 cname(targ.shp_own), prship(&targ),
191 xyas(targ.shp_x, targ.shp_y, player->cnum));
193 if (visib > vis[y][x]) {
195 /* &~0x20 makes it a cap letter */
196 rad[y][x] = (*mchr[(int)targ.shp_type].m_name) & ~0x20;
199 if (!player->argp[2]) {
201 for (row = 0; row < ns.range.height; row++)
202 if (!blankrow(rad[row]))
203 pr("%s\n", rad[row]);
209 writemap(player->cnum);
214 plane_sona(struct emp_qelem *plane_list, int x, int y,
215 struct shiplist **head)
218 struct plchrstr *pcp;
219 struct mchrstr *tmcp;
220 struct shpstr *targ, s;
222 struct emp_qelem *qp;
223 struct emp_qelem *next;
232 getsect(x, y, §);
233 if ((sect.sct_type != SCT_WATER) && (sect.sct_type != SCT_HARBR))
235 for (qp = plane_list->q_forw; qp != plane_list; qp = next) {
237 ip = (struct plist *)qp;
240 if (!(pcp->pl_flags & P_A)) /* if it isn't an ASW plane */
242 range = (int)techfact(pp->pln_tech, (100.0 - pln_acc(pp)) / 10.0);
243 for (i = 0; getship(i, &s); i++) {
245 if (targ->shp_own == pp->pln_own || targ->shp_own == 0)
247 if (on_shiplist(targ->shp_uid, *head))
249 tmcp = &mchr[(int)targ->shp_type];
250 if (!(tmcp->m_flags & M_SUB))
252 if (!pct_chance(pln_identchance(pp, shp_hardtarget(targ),
255 vis = shp_visib(targ);
256 pingrange = MAX(vis, 10) * range / 10;
257 vrange = pingrange * (pp->pln_effic / 200.0);
258 dist = mapdist(targ->shp_x, targ->shp_y, x, y);
259 pingrange = (MAX(pingrange, 2) * targ->shp_effic);
260 pingrange = roundavg(pingrange / 100.0);
261 if (dist > pingrange)
263 if (tmcp->m_flags & M_SONAR && targ->shp_own) {
264 natp = getnatp(targ->shp_own);
265 if (natp->nat_flags & NF_SONAR)
267 "Sonar ping from %s detected by %s!\n",
268 xyas(x, y, targ->shp_own), prship(targ));
272 add_shiplist(targ->shp_uid, head);
274 pr("Sonar contact in %s\n", xyas(x, y, player->cnum));
277 if (relations_with(targ->shp_own, pp->pln_own) < FRIENDLY &&
278 !pct_chance(pln_identchance(pp, shp_hardtarget(targ),
280 if (!pct_chance(pln_identchance(pp, shp_hardtarget(targ),
282 pr("sub #%d %s\n", targ->shp_uid,
283 xyas(targ->shp_x, targ->shp_y, player->cnum));
285 pr("%s %s\n", prship(targ),
286 xyas(targ->shp_x, targ->shp_y, player->cnum));
288 pr("%s %s @ %s\n", cname(targ->shp_own), prship(targ),
289 xyas(targ->shp_x, targ->shp_y, player->cnum));
297 * line_of_sight() - is there a "straight" all water path from (x,y) to (tx,ty)
298 * Ken & Irina Stevens, 1995
301 #define DOT(ax,ay,bx,by) ((ax)*(bx) + (ay)*(by))
302 #define LEN(x,y) ((x)*(x) + (y)*(y))
303 #define DIST(ax,ay,bx,by) LEN(bx - ax, by -ay)
306 line_of_sight(char **rad, int ax, int ay, int bx, int by)
308 int dxn = XNORM(bx - ax);
309 int dyn = YNORM(by - ay);
310 int dx = dxn > WORLD_X / 2 ? dxn - WORLD_X : dxn;
311 int dy = dyn > WORLD_Y / 2 ? dyn - WORLD_Y : dyn;
312 int dlen = LEN(dx, dy);
316 int tx, ty; /* test point */
317 double cd_dist = dlen; /* closest distance from c to vector d */
318 double md_dist; /* minimum distance from t to vector d */
319 double td_dist; /* distance from t to vector d */
320 double td_proj; /* the projection of t onto vector d */
321 int closest; /* index of closest */
323 struct sctstr *sectp;
328 md_dist = 100; /* will always be <= 2 */
330 for (n = 1; n <= 6; ++n) { /* Directions */
331 tx = cx + diroff[n][0];
332 ty = cy + diroff[n][1];
333 if (DIST(tx, ty, dx, dy) >= cd_dist)
335 td_proj = (double)DOT(tx, ty, dx, dy) / dlen;
336 td_dist = DIST(tx, ty, td_proj * dx, td_proj * dy);
337 if (td_dist < md_dist) {
342 if (CANT_HAPPEN(closest < 0))
344 cx = cx + diroff[closest][0];
345 cy = cy + diroff[closest][1];
347 blocked = (rad[YNORM(ay + cy)][XNORM(ax + cx)]
348 != dchr[SCT_WATER].d_mnem);
350 sectp = getsectp((ax + cx), (ay + cy));
352 if (sectp->sct_type == SCT_WATER ||
353 sectp->sct_type == SCT_BSPAN) {
360 cd_dist = DIST(cx, cy, dx, dy);