2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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 files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future 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:
54 static int blankrow(char *);
59 struct nstr_item ni, nit;
76 /* Where these are used are non-re-entrant, so we keep 'em around */
77 static char **rad = NULL;
78 static char *radbuf = NULL;
79 static signed char **vis = NULL;
80 static signed char *visbuf = NULL;
82 if (!snxtitem(&ni, EF_SHIP, player->argp[1]))
85 radbuf = malloc((WORLD_Y * (WORLD_X + 1)));
87 visbuf = malloc((WORLD_Y * (WORLD_X + 1)));
89 rad = malloc(WORLD_Y * sizeof(char *));
91 for (x = 0; x < WORLD_Y; x++) {
92 rad[x] = &radbuf[(WORLD_X + 1) * x];
97 vis = malloc(WORLD_Y * sizeof(signed char *));
99 for (x = 0; x < WORLD_Y; x++) {
100 vis[x] = &visbuf[(WORLD_X + 1) * x];
104 if (!radbuf || !visbuf || !rad || !vis) {
105 pr("Memory error, tell the deity.\n");
106 logerror("malloc failed in sona\n");
109 while (nxtitem(&ni, &ship)) {
112 mcp = &mchr[(int)ship.shp_type];
113 if (!(mcp->m_flags & M_SONAR))
115 getsect(ship.shp_x, ship.shp_y, §);
116 if (sect.sct_type != SCT_WATER)
118 range = (int)techfact(ship.shp_tech, (double)mcp->m_vrnge);
119 srange = MIN(7, 7 * range * ship.shp_effic / 200);
120 pr("%s at %s efficiency %d%%, max range %d\n",
122 xyas(ship.shp_x, ship.shp_y, player->cnum),
123 ship.shp_effic, srange);
124 snxtsct_dist(&ns, ship.shp_x, ship.shp_y, srange);
125 blankfill(radbuf, &ns.range, 1);
126 while (nxtsct(&ns, §)) {
127 if (player->owner || sect.sct_type == SCT_WATER)
128 rad[ns.dy][ns.dx] = dchr[sect.sct_type].d_mnem;
130 rad[ns.dy][ns.dx] = '?';
133 snxtsct_dist(&ns, ship.shp_x, ship.shp_y, srange);
134 cx = deltax(ship.shp_x, ns.range.lx);
135 cy = deltay(ship.shp_y, ns.range.ly);
136 while (nxtsct(&ns, §)) {
137 if (!line_of_sight(rad, cx, cy, ns.dx, ns.dy)) {
138 rad[ns.dy][ns.dx] = ' ';
141 if (ship.shp_tech >= 310 && sect.sct_type == SCT_WATER) {
142 if (sect.sct_mines) {
143 pr("Sonar detects %d mines in %s!\n",
145 xyas(sect.sct_x, sect.sct_y, player->cnum));
146 rad[ns.dy][ns.dx] = 'X';
149 changed |= map_set(player->cnum, sect.sct_x, sect.sct_y,
150 rad[ns.dy][ns.dx], 0);
153 memset(visbuf, 0, (WORLD_Y * (WORLD_X + 1)));
154 snxtitem_dist(&nit, EF_SHIP, ship.shp_x, ship.shp_y, range);
155 while (nxtitem(&nit, &targ)) {
156 if (targ.shp_own == player->cnum || targ.shp_own == 0)
158 tmcp = &mchr[(int)targ.shp_type];
159 pingrange = MIN(7, MAX(targ.shp_visib, 10) * range / 10);
160 vrange = pingrange * ship.shp_effic / 200;
161 dist = mapdist(targ.shp_x, targ.shp_y, ship.shp_x, ship.shp_y);
162 pingrange = (MAX(pingrange, 2) * targ.shp_effic) / 100;
163 if (dist > pingrange)
165 if (tmcp->m_flags & M_SONAR && targ.shp_own) {
166 natp = getnatp(targ.shp_own);
167 if (natp->nat_flags & NF_SONAR)
169 "Sonar ping from %s detected by %s!\n",
170 xyas(ship.shp_x, ship.shp_y,
171 targ.shp_own), prship(&targ));
172 if (targ.shp_rflags & RET_SONARED) {
173 retreat_ship(&targ, 's');
174 putship(targ.shp_uid, &targ);
179 x = deltx(&ns.range, (int)targ.shp_x);
180 y = delty(&ns.range, (int)targ.shp_y);
181 if (rad[y][x] != dchr[SCT_WATER].d_mnem && rad[y][x] != 'X')
183 if (tmcp->m_flags & M_SUB &&
184 getrel(getnatp(targ.shp_own), player->cnum) < FRIENDLY) {
185 if (mcp->m_vrnge + targ.shp_visib < 8)
186 pr("Sonar detects sub #%d @ %s\n",
188 xyas(targ.shp_x, targ.shp_y, player->cnum));
189 else if (mcp->m_vrnge + targ.shp_visib < 10)
190 pr("Sonar detects %s @ %s\n",
192 xyas(targ.shp_x, targ.shp_y, player->cnum));
194 pr("Sonar detects %s %s @ %s\n", cname(targ.shp_own),
196 xyas(targ.shp_x, targ.shp_y, player->cnum));
198 pr("Sonar detects %s %s @ %s\n", cname(targ.shp_own),
200 xyas(targ.shp_x, targ.shp_y, player->cnum));
202 if (targ.shp_visib > vis[y][x]) {
203 vis[y][x] = targ.shp_visib;
204 /* &~0x20 makes it a cap letter */
205 rad[y][x] = (*mchr[(int)targ.shp_type].m_name) & ~0x20;
208 if (!player->argp[2]) {
210 for (row = 0; row < ns.range.height; row++)
211 if (!blankrow(rad[row]))
212 pr("%s\n", rad[row]);
218 writemap(player->cnum);
223 plane_sona(struct emp_qelem *plane_list, int x, int y,
224 struct shiplist **head)
227 struct plchrstr *pcp;
228 struct mchrstr *tmcp;
229 struct shpstr *targ, s;
231 struct emp_qelem *qp;
232 struct emp_qelem *next;
241 getsect(x, y, §);
242 if ((sect.sct_type != SCT_WATER) && (sect.sct_type != SCT_HARBR))
244 for (qp = plane_list->q_forw; qp != plane_list; qp = next) {
246 ip = (struct plist *)qp;
249 if (!(pcp->pl_flags & P_A)) /* if it isn't an ASW plane */
252 (int)techfact(pp->pln_tech,
253 (double)((100 - pp->pln_acc) / 10));
255 for (i=0; targ = getshipp(i); i++) {
257 for (i = 0; getship(i, &s); i++) {
259 if (targ->shp_own == pp->pln_own || targ->shp_own == 0)
261 if (on_shiplist(targ->shp_uid, *head))
263 tmcp = &mchr[(int)targ->shp_type];
264 if (!(tmcp->m_flags & M_SUB))
267 pln_identchance(pp, shp_hardtarget(targ), EF_SHIP))
269 pingrange = MAX(targ->shp_visib, 10) * range / 10;
270 vrange = ((float)pingrange) * ((float)pp->pln_effic / 200.0);
271 dist = mapdist(targ->shp_x, targ->shp_y, x, y);
272 pingrange = (MAX(pingrange, 2) * targ->shp_effic);
273 pingrange = roundavg(pingrange / 100.0);
274 if (dist > pingrange)
276 if (tmcp->m_flags & M_SONAR && targ->shp_own) {
277 natp = getnatp(targ->shp_own);
278 if (natp->nat_flags & NF_SONAR)
280 "Sonar ping from %s detected by %s!\n",
281 xyas(x, y, targ->shp_own), prship(targ));
285 add_shiplist(targ->shp_uid, head);
288 "\nSonar contact in %s\n", xyas(x, y, pp->pln_own));
291 if (getrel(getnatp(targ->shp_own), pp->pln_own) < FRIENDLY &&
292 roll(100) > pln_identchance(pp, shp_hardtarget(targ),
295 pln_identchance(pp, shp_hardtarget(targ), EF_SHIP))
296 mpr(pp->pln_own, "sub #%d %s\n", targ->shp_uid,
297 xyas(targ->shp_x, targ->shp_y, pp->pln_own));
299 mpr(pp->pln_own, "%s %s\n",
301 xyas(targ->shp_x, targ->shp_y, pp->pln_own));
303 mpr(pp->pln_own, "%s %s @ %s\n", cname(targ->shp_own),
305 xyas(targ->shp_x, targ->shp_y, pp->pln_own));
311 * line_of_sight() - is there a "straight" all water path from (x,y) to (tx,ty)
312 * Ken & Irina Stevens, 1995
315 #define DOT(ax,ay,bx,by) ((ax)*(bx) + (ay)*(by))
316 #define LEN(x,y) ((x)*(x) + (y)*(y))
317 #define DIST(ax,ay,bx,by) LEN(bx - ax, by -ay)
320 line_of_sight(char **rad, int ax, int ay, int bx, int by)
322 int dxn = XNORM(bx - ax);
323 int dyn = YNORM(by - ay);
324 int dx = dxn > WORLD_X / 2 ? dxn - WORLD_X : dxn;
325 int dy = dyn > WORLD_Y / 2 ? dyn - WORLD_Y : dyn;
326 int dlen = LEN(dx, dy);
330 int tx, ty; /* test point */
331 double cd_dist = dlen; /* closest distance from c to vector d */
332 double md_dist; /* minimum distance from t to vector d */
333 double td_dist; /* distance from t to vector d */
334 double td_proj; /* the projection of t onto vector d */
335 int closest; /* index of closest */
337 struct sctstr *sectp;
342 md_dist = 100; /* will always be <= 2 */
344 for (n = 1; n <= 6; ++n) { /* Directions */
345 tx = cx + diroff[n][0];
346 ty = cy + diroff[n][1];
347 if (DIST(tx, ty, dx, dy) >= cd_dist)
349 td_proj = (double)DOT(tx, ty, dx, dy) / dlen;
350 td_dist = DIST(tx, ty, td_proj * dx, td_proj * dy);
351 if (td_dist < md_dist) {
356 if (CANT_HAPPEN(closest < 0))
358 cx = cx + diroff[closest][0];
359 cy = cy + diroff[closest][1];
361 blocked = (rad[YNORM(ay + cy)][XNORM(ax + cx)]
362 != dchr[SCT_WATER].d_mnem);
364 sectp = getsectp((ax + cx), (ay + cy));
366 if (sectp->sct_type == SCT_WATER ||
367 sectp->sct_type == SCT_BSPAN) {
374 cd_dist = DIST(cx, cy, dx, dy);