]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/sona.c
Update copyright notice
[empserver] / src / lib / commands / sona.c
index 699f81873663eba8207705babaffeb78a4d43099..e470e0397520a7e235315be970cbde6db3e3435e 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
  *  sona.c: Sonar from a sub (or other sonar-equipped ship)
- * 
+ *
  *  Known contributors to this file:
  *     Jim Griffith, 1989
  *     Ken Stevens, 1995
+ *     Markus Armbruster, 2004-2012
  */
 
-#include "misc.h"
-#include "player.h"
-#include "var.h"
-#include "xy.h"
-#include "sect.h"
-#include "nsc.h"
-#include "retreat.h"
-#include "ship.h"
-#include "nat.h"
-#include "path.h"
-#include "file.h"
-#include "queue.h"
-#include "plane.h"
-#include <fcntl.h>
-#include <ctype.h>
+#include <config.h>
+
+#include "chance.h"
 #include "commands.h"
+#include "map.h"
 #include "optlist.h"
+#include "path.h"
+#include "plane.h"
+#include "retreat.h"
+#include "ship.h"
 
-static int blankrow(s_char *);
+static int blankrow(char *);
 
 int
 sona(void)
@@ -64,49 +57,40 @@ sona(void)
     struct mchrstr *tmcp;
     struct nstr_sect ns;
     int range;
-    int pingrange;
+    int visib, pingrange;
     int srange;
     int vrange;
     int dist;
     int x, y;
     int cx, cy;
-    int mines;
     int changed = 0;
     int row;
     /* Where these are used are non-re-entrant, so we keep 'em around */
-    static s_char **rad = (s_char **)0;
-    static s_char *radbuf = (s_char *)0;
-    static s_char **vis = (s_char **)0;
-    static s_char *visbuf = (s_char *)0;
+    static char **rad = NULL;
+    static char *radbuf = NULL;
+    static signed char **vis = NULL;
+    static signed char *visbuf = NULL;
 
-    if (!snxtitem(&ni, EF_SHIP, player->argp[1]))
+    if (!snxtitem(&ni, EF_SHIP, player->argp[1], NULL))
        return RET_SYN;
     if (!radbuf)
-       radbuf =
-           (s_char *)malloc((WORLD_Y * (WORLD_X + 1)) * sizeof(s_char));
+       radbuf = malloc(WORLD_Y * MAPWIDTH(1));
     if (!visbuf)
-       visbuf =
-           (s_char *)malloc((WORLD_Y * (WORLD_X + 1)) * sizeof(s_char));
-    if (!rad) {
-       rad = (s_char **)malloc(WORLD_Y * sizeof(s_char *));
-       if (rad && radbuf) {
+       visbuf = malloc(WORLD_Y * MAPWIDTH(1));
+    if (!rad && radbuf) {
+       rad = malloc(WORLD_Y * sizeof(char *));
+       if (rad) {
            for (x = 0; x < WORLD_Y; x++) {
                rad[x] = &radbuf[(WORLD_X + 1) * x];
            }
-       } else if (rad) {
-           free((s_char *)rad);
-           rad = (s_char **)0;
        }
     }
-    if (!vis) {
-       vis = (s_char **)malloc(WORLD_Y * sizeof(s_char *));
-       if (vis && visbuf) {
+    if (!vis && visbuf) {
+       vis = malloc(WORLD_Y * sizeof(signed char *));
+       if (vis) {
            for (x = 0; x < WORLD_Y; x++) {
                vis[x] = &visbuf[(WORLD_X + 1) * x];
            }
-       } else if (vis) {
-           free((s_char *)vis);
-           vis = (s_char **)0;
        }
     }
     if (!radbuf || !visbuf || !rad || !vis) {
@@ -114,7 +98,7 @@ sona(void)
        logerror("malloc failed in sona\n");
        return RET_FAIL;
     }
-    while (nxtitem(&ni, (s_char *)&ship)) {
+    while (nxtitem(&ni, &ship)) {
        if (!player->owner)
            continue;
        mcp = &mchr[(int)ship.shp_type];
@@ -123,14 +107,14 @@ sona(void)
        getsect(ship.shp_x, ship.shp_y, &sect);
        if (sect.sct_type != SCT_WATER)
            continue;
-       range = (int)techfact(ship.shp_tech, (double)mcp->m_vrnge);
-       srange = min(7, 7 * range * ship.shp_effic / 200);
+       range = (int)techfact(ship.shp_tech, mcp->m_vrnge);
+       srange = MIN(7, 7 * range * ship.shp_effic / 200);
        pr("%s at %s efficiency %d%%, max range %d\n",
           prship(&ship),
           xyas(ship.shp_x, ship.shp_y, player->cnum),
           ship.shp_effic, srange);
        snxtsct_dist(&ns, ship.shp_x, ship.shp_y, srange);
-       blankfill((s_char *)radbuf, &ns.range, 1);
+       blankfill(radbuf, &ns.range, 1);
        while (nxtsct(&ns, &sect)) {
            if (player->owner || sect.sct_type == SCT_WATER)
                rad[ns.dy][ns.dx] = dchr[sect.sct_type].d_mnem;
@@ -139,18 +123,18 @@ sona(void)
            }
        }
        snxtsct_dist(&ns, ship.shp_x, ship.shp_y, srange);
-       cx = deltax(ship.shp_x, ns.range.lx);
-       cy = deltay(ship.shp_y, ns.range.ly);
+       cx = deltx(&ns.range, ship.shp_x);
+       cy = delty(&ns.range, ship.shp_y);
        while (nxtsct(&ns, &sect)) {
            if (!line_of_sight(rad, cx, cy, ns.dx, ns.dy)) {
                rad[ns.dy][ns.dx] = ' ';
                continue;
            }
            if (ship.shp_tech >= 310 && sect.sct_type == SCT_WATER) {
-               mines = getvar(V_MINE, (s_char *)&sect, EF_SECTOR);
-               if (mines) {
+               if (sect.sct_mines) {
                    pr("Sonar detects %d mines in %s!\n",
-                      mines, xyas(sect.sct_x, sect.sct_y, player->cnum));
+                      sect.sct_mines,
+                      xyas(sect.sct_x, sect.sct_y, player->cnum));
                    rad[ns.dy][ns.dx] = 'X';
                }
            }
@@ -158,17 +142,17 @@ sona(void)
                               rad[ns.dy][ns.dx], 0);
 
        }
-       bzero((s_char *)visbuf, (WORLD_Y * (WORLD_X + 1)));
+       memset(visbuf, 0, (WORLD_Y * (WORLD_X + 1)));
        snxtitem_dist(&nit, EF_SHIP, ship.shp_x, ship.shp_y, range);
-       while (nxtitem(&nit, (caddr_t)&targ)) {
-
+       while (nxtitem(&nit, &targ)) {
            if (targ.shp_own == player->cnum || targ.shp_own == 0)
                continue;
            tmcp = &mchr[(int)targ.shp_type];
-           pingrange = min(7, max(targ.shp_visib, 10) * range / 10);
+           visib = shp_visib(&targ);
+           pingrange = MIN(7, MAX(visib, 10) * range / 10);
            vrange = pingrange * ship.shp_effic / 200;
            dist = mapdist(targ.shp_x, targ.shp_y, ship.shp_x, ship.shp_y);
-           pingrange = (max(pingrange, 2) * targ.shp_effic) / 100;
+           pingrange = (MAX(pingrange, 2) * targ.shp_effic) / 100;
            if (dist > pingrange)
                continue;
            if (tmcp->m_flags & M_SONAR && targ.shp_own) {
@@ -190,26 +174,26 @@ sona(void)
            if (rad[y][x] != dchr[SCT_WATER].d_mnem && rad[y][x] != 'X')
                continue;
            if (tmcp->m_flags & M_SUB &&
-               getrel(getnatp(targ.shp_own), player->cnum) < FRIENDLY) {
-               if (mcp->m_vrnge + targ.shp_visib < 8)
+               relations_with(targ.shp_own, player->cnum) < FRIENDLY) {
+               if (mcp->m_vrnge + visib < 8)
                    pr("Sonar detects sub #%d @ %s\n",
                       targ.shp_uid,
                       xyas(targ.shp_x, targ.shp_y, player->cnum));
-               else if (mcp->m_vrnge + targ.shp_visib < 10)
+               else if (mcp->m_vrnge + visib < 10)
                    pr("Sonar detects %s @ %s\n",
                       prship(&targ),
                       xyas(targ.shp_x, targ.shp_y, player->cnum));
                else
-                   pr("Sonar detects %s %s @ %s\n", cname(targ.shp_own),
-                      prship(&targ),
+                   pr("Sonar detects %s %s @ %s\n",
+                      cname(targ.shp_own), prship(&targ),
                       xyas(targ.shp_x, targ.shp_y, player->cnum));
            } else
-               pr("Sonar detects %s %s @ %s\n", cname(targ.shp_own),
-                  prship(&targ),
+               pr("Sonar detects %s %s @ %s\n",
+                  cname(targ.shp_own), prship(&targ),
                   xyas(targ.shp_x, targ.shp_y, player->cnum));
 
-           if (targ.shp_visib > vis[y][x]) {
-               vis[y][x] = targ.shp_visib;
+           if (visib > vis[y][x]) {
+               vis[y][x] = visib;
                /* &~0x20 makes it a cap letter */
                rad[y][x] = (*mchr[(int)targ.shp_type].m_name) & ~0x20;
            }
@@ -230,7 +214,7 @@ sona(void)
 
 void
 plane_sona(struct emp_qelem *plane_list, int x, int y,
-          struct shiplook *head)
+          struct shiplist **head)
 {
     struct plnstr *pp;
     struct plchrstr *pcp;
@@ -242,7 +226,7 @@ plane_sona(struct emp_qelem *plane_list, int x, int y,
     struct plist *ip;
     struct sctstr sect;
     int found = 0;
-    int range, i;
+    int range, i, vis;
     int pingrange;
     int vrange;
     int dist;
@@ -257,33 +241,24 @@ plane_sona(struct emp_qelem *plane_list, int x, int y,
        pcp = ip->pcp;
        if (!(pcp->pl_flags & P_A))     /* if it isn't an ASW plane */
            continue;
-       range =
-           (int)techfact(pp->pln_tech,
-                         (double)((100 - pp->pln_acc) / 10));
-/*
-               for (i=0; targ = getshipp(i); i++) {
-*/
+       range = (int)techfact(pp->pln_tech, (100.0 - pln_acc(pp)) / 10.0);
        for (i = 0; getship(i, &s); i++) {
            targ = &s;
            if (targ->shp_own == pp->pln_own || targ->shp_own == 0)
                continue;
-/*
-                       if (have_looked(targ->shp_uid,head))
-                               continue;
-*/
-           if (have_found(targ->shp_uid, head))
+           if (on_shiplist(targ->shp_uid, *head))
                continue;
-           set_have_looked(targ->shp_uid, head);
            tmcp = &mchr[(int)targ->shp_type];
            if (!(tmcp->m_flags & M_SUB))
                continue;
-           if (roll(100) >
-               pln_identchance(pp, shp_hardtarget(targ), EF_SHIP))
+           if (!pct_chance(pln_identchance(pp, shp_hardtarget(targ),
+                                           EF_SHIP)))
                continue;
-           pingrange = max(targ->shp_visib, 10) * range / 10;
-           vrange = ((float)pingrange) * ((float)pp->pln_effic / 200.0);
+           vis = shp_visib(targ);
+           pingrange = MAX(vis, 10) * range / 10;
+           vrange = pingrange * (pp->pln_effic / 200.0);
            dist = mapdist(targ->shp_x, targ->shp_y, x, y);
-           pingrange = (max(pingrange, 2) * targ->shp_effic);
+           pingrange = (MAX(pingrange, 2) * targ->shp_effic);
            pingrange = roundavg(pingrange / 100.0);
            if (dist > pingrange)
                continue;
@@ -296,34 +271,31 @@ plane_sona(struct emp_qelem *plane_list, int x, int y,
            }
            if ((dist > vrange))
                continue;
-           set_have_found(targ->shp_uid, head);
+           add_shiplist(targ->shp_uid, head);
            if (!found) {
-               mpr(pp->pln_own,
-                   "\nSonar contact in %s\n", xyas(x, y, pp->pln_own));
+               pr("Sonar contact in %s\n", xyas(x, y, player->cnum));
                found = 1;
            }
-           if (getrel(getnatp(targ->shp_own), pp->pln_own) < FRIENDLY &&
-               roll(100) > pln_identchance(pp, shp_hardtarget(targ),
-                                           EF_SHIP))
-               if (roll(100) >
-                   pln_identchance(pp, shp_hardtarget(targ), EF_SHIP))
-                   mpr(pp->pln_own, "sub #%d %s\n", targ->shp_uid,
-                       xyas(targ->shp_x, targ->shp_y, pp->pln_own));
+           if (relations_with(targ->shp_own, pp->pln_own) < FRIENDLY &&
+               !pct_chance(pln_identchance(pp, shp_hardtarget(targ),
+                                           EF_SHIP)))
+               if (!pct_chance(pln_identchance(pp, shp_hardtarget(targ),
+                                               EF_SHIP)))
+                   pr("sub #%d %s\n", targ->shp_uid,
+                      xyas(targ->shp_x, targ->shp_y, player->cnum));
                else
-                   mpr(pp->pln_own,
-                       "%s %s\n",
-                       prship(targ),
-                       xyas(targ->shp_x, targ->shp_y, pp->pln_own));
+                   pr("%s %s\n", prship(targ),
+                      xyas(targ->shp_x, targ->shp_y, player->cnum));
            else
-               mpr(pp->pln_own,
-                   "%s %s @ %s\n", cname(targ->shp_own),
-                   prship(targ),
-                   xyas(targ->shp_x, targ->shp_y, pp->pln_own));
+               pr("%s %s @ %s\n", cname(targ->shp_own), prship(targ),
+                  xyas(targ->shp_x, targ->shp_y, player->cnum));
        }
     }
+    if (found)
+       pr("\n");
 }
 
-/* 
+/*
  * line_of_sight() - is there a "straight" all water path from (x,y) to (tx,ty)
  * Ken & Irina Stevens, 1995
  */
@@ -333,10 +305,12 @@ plane_sona(struct emp_qelem *plane_list, int x, int y,
 #define DIST(ax,ay,bx,by) LEN(bx - ax, by -ay)
 
 int
-line_of_sight(s_char **rad, int ax, int ay, int bx, int by)
+line_of_sight(char **rad, int ax, int ay, int bx, int by)
 {
-    int dx = bx - ax;
-    int dy = by - ay;
+    int dxn = XNORM(bx - ax);
+    int dyn = YNORM(by - ay);
+    int dx = dxn > WORLD_X / 2 ? dxn - WORLD_X : dxn;
+    int dy = dyn > WORLD_Y / 2 ? dyn - WORLD_Y : dyn;
     int dlen = LEN(dx, dy);
     int n;
     int cx = 0;
@@ -367,12 +341,13 @@ line_of_sight(s_char **rad, int ax, int ay, int bx, int by)
                closest = n;
            }
        }
-       if (closest < 0)        /* not possible */
+       if (CANT_HAPPEN(closest < 0))
            return 0;
        cx = cx + diroff[closest][0];
        cy = cy + diroff[closest][1];
        if (rad) {
-           blocked = (rad[ay + cy][ax + cx] != dchr[SCT_WATER].d_mnem);
+           blocked = (rad[YNORM(ay + cy)][XNORM(ax + cx)]
+                      != dchr[SCT_WATER].d_mnem);
        } else {
            sectp = getsectp((ax + cx), (ay + cy));
            if (sectp) {
@@ -390,7 +365,7 @@ line_of_sight(s_char **rad, int ax, int ay, int bx, int by)
 }
 
 static int
-blankrow(s_char *s)
+blankrow(char *s)
 {
     while (*s) {
        if (*s != ' ')