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]))
83 radbuf = (s_char *)malloc((WORLD_Y*(WORLD_X+1)) * sizeof(s_char));
85 visbuf = (s_char *)malloc((WORLD_Y*(WORLD_X+1)) * sizeof(s_char));
87 rad = (s_char **)malloc(WORLD_Y * sizeof(s_char *));
89 for (x = 0; x < WORLD_Y; x++) {
90 rad[x] = &radbuf[(WORLD_X + 1) * x];
98 vis = (s_char **)malloc(WORLD_Y * sizeof(s_char *));
100 for (x = 0; x < WORLD_Y; x++) {
101 vis[x] = &visbuf[(WORLD_X + 1) * x];
108 if (!radbuf || !visbuf || !rad || !vis) {
109 pr("Memory error, tell the deity.\n");
110 logerror("malloc failed in sona\n");
113 while (nxtitem(&ni, (s_char *)&ship)) {
116 mcp = &mchr[(int)ship.shp_type];
117 if (!(mcp->m_flags & M_SONAR))
119 getsect(ship.shp_x, ship.shp_y, §);
120 if (sect.sct_type != SCT_WATER)
122 range = (int) techfact(ship.shp_tech,
123 (double) mcp->m_vrnge);
124 srange = min(7, 7 * range * ship.shp_effic / 200);
125 pr("%s at %s efficiency %d%%, max range %d\n",
127 xyas(ship.shp_x, ship.shp_y, player->cnum),
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 &&
148 sect.sct_type == SCT_WATER) {
149 mines=getvar(V_MINE,(s_char *)§,EF_SECTOR);
151 pr("Sonar detects %d mines in %s!\n",
153 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 bzero((s_char *)visbuf, (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,
171 ship.shp_x, ship.shp_y);
172 pingrange = (max(pingrange, 2) * targ.shp_effic)/100;
173 if (dist > pingrange)
175 if (tmcp->m_flags & M_SONAR && targ.shp_own) {
176 natp = getnatp(targ.shp_own);
177 if (natp->nat_flags & NF_SONAR)
179 "Sonar ping from %s detected by %s!\n",
181 xyas(ship.shp_x, ship.shp_y,
184 if (targ.shp_rflags & RET_SONARED){
185 retreat_ship(&targ, 's');
186 putship(targ.shp_uid,&targ);
191 x = deltx(&ns.range,(int)targ.shp_x);
192 y = delty(&ns.range,(int)targ.shp_y);
193 if (rad[y][x] != dchr[SCT_WATER].d_mnem &&
196 if (tmcp->m_flags & M_SUB &&
197 getrel(getnatp(targ.shp_own), player->cnum) < FRIENDLY) {
198 if (mcp->m_vrnge + targ.shp_visib < 8)
199 pr("Sonar detects sub #%d @ %s\n",
201 xyas(targ.shp_x, targ.shp_y, player->cnum));
202 else if (mcp->m_vrnge + targ.shp_visib < 10)
203 pr("Sonar detects %s @ %s\n",
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 pr("Sonar detects %s %s @ %s\n", cname(targ.shp_own),
213 xyas(targ.shp_x, targ.shp_y, player->cnum));
215 if (targ.shp_visib > vis[y][x]) {
216 vis[y][x] = targ.shp_visib;
217 /* &~0x20 makes it a cap letter */
218 rad[y][x] = (*mchr[(int)targ.shp_type].m_name) & ~0x20;
221 if (!player->argp[2]) {
223 for (row=0; row < ns.range.height; row++)
224 if (!blankrow(rad[row]))
225 pr("%s\n", rad[row]);
231 writemap(player->cnum);
236 plane_sona(struct emp_qelem *plane_list, int x, int y, struct shiplook *head)
239 struct plchrstr *pcp;
240 struct mchrstr *tmcp;
241 struct shpstr *targ,s;
243 struct emp_qelem *qp;
244 struct emp_qelem *next;
254 if ((sect.sct_type != SCT_WATER) && (sect.sct_type != SCT_HARBR))
256 for (qp = plane_list->q_forw; qp != plane_list; qp = next) {
258 ip = (struct plist *) qp;
261 if (!(pcp->pl_flags & P_A)) /* if it isn't an ASW plane */
263 range = (int) techfact(pp->pln_tech, (double) ((100-pp->pln_acc)/10));
265 for (i=0; targ = getshipp(i); i++) {
267 for (i=0; getship(i,&s) ; i++) {
269 if (targ->shp_own == pp->pln_own || targ->shp_own == 0)
272 if (have_looked(targ->shp_uid,head))
275 if (have_found(targ->shp_uid,head))
277 set_have_looked(targ->shp_uid,head);
278 tmcp = &mchr[(int)targ->shp_type];
279 if (!(tmcp->m_flags & M_SUB))
281 if (roll(100) > 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),
300 set_have_found(targ->shp_uid,head);
303 "\nSonar contact in %s\n",xyas(x,y,pp->pln_own));
306 if (getrel(getnatp(targ->shp_own), pp->pln_own) < FRIENDLY &&
307 roll(100) > pln_identchance(pp, shp_hardtarget(targ), EF_SHIP))
308 if (roll(100) > pln_identchance(pp, shp_hardtarget(targ), EF_SHIP))
312 xyas(targ->shp_x, targ->shp_y, pp->pln_own));
317 xyas(targ->shp_x, targ->shp_y, pp->pln_own));
320 "%s %s @ %s\n", cname(targ->shp_own),
322 xyas(targ->shp_x, targ->shp_y, pp->pln_own));
328 * line_of_sight() - is there a "straight" all water path from (x,y) to (tx,ty)
329 * Ken & Irina Stevens, 1995
332 #define DOT(ax,ay,bx,by) ((ax)*(bx) + (ay)*(by))
333 #define LEN(x,y) ((x)*(x) + (y)*(y))
334 #define DIST(ax,ay,bx,by) LEN(bx - ax, by -ay)
337 line_of_sight(s_char **rad, int ax, int ay, int bx, int by)
341 int dlen = LEN(dx, dy);
345 int tx, ty; /* test point */
346 double cd_dist = dlen; /* closest distance from c to vector d */
347 double md_dist; /* minimum distance from t to vector d */
348 double td_dist; /* distance from t to vector d */
349 double td_proj; /* the projection of t onto vector d */
350 int closest; /* index of closest */
352 struct sctstr *sectp;
357 md_dist = 100; /* will always be <= 2 */
359 for (n = 1; n <= 6; ++n) { /* Directions */
360 tx = cx + diroff[n][0];
361 ty = cy + diroff[n][1];
362 if (DIST(tx,ty,dx,dy) >= cd_dist)
364 td_proj = (double)DOT(tx,ty,dx,dy) / dlen;
365 td_dist = DIST(tx,ty,td_proj*dx,td_proj*dy);
366 if (td_dist < md_dist) {
371 if (closest < 0) /* not possible */
373 cx = cx + diroff[closest][0];
374 cy = cy + diroff[closest][1];
376 blocked = (rad[ay+cy][ax+cx] != dchr[SCT_WATER].d_mnem);
378 sectp = getsectp((ax + cx), (ay + cy));
380 if (sectp->sct_type == SCT_WATER ||
381 sectp->sct_type == SCT_BSPAN) {
388 cd_dist = DIST(cx,cy,dx,dy);