]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/list.c
Update copyright notice
[empserver] / src / lib / subs / list.c
index 8d13262e5da2a4c23a0b68242f319aac9a8dfda9..17a7544042988cfa6a5b3dcf856b9f532e95a4f4 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2021, 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,
@@ -14,8 +14,7 @@
  *  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/>.
  *
  *  ---
  *
  *  ---
  *
  *  list.c: List ships, planes, units at a given x,y
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1986
+ *     Markus Armbruster, 2003-2016
  */
 
 #include <config.h>
 
 #include <ctype.h>
-#include "file.h"
+#include "chance.h"
 #include "land.h"
 #include "misc.h"
 #include "nat.h"
 #include "nsc.h"
+#include "path.h"
 #include "plane.h"
 #include "player.h"
 #include "prototypes.h"
+#include "sect.h"
 #include "ship.h"
 #include "xy.h"
 
+static void
+list_ship(struct shpstr *sp, int first)
+{
+    if (first)
+       pr(" #          owner           eff       type\n");
+    pr("(#%3d) %10.10s  %12.12s  %s\n", sp->shp_uid,
+       cname(sp->shp_own), effadv(sp->shp_effic), prship(sp));
+}
+
 int
-shipsatxy(coord x, coord y, int wantflags, int nowantflags)
+shipsatxy(coord x, coord y, int wantflags, int nowantflags, int only_count)
 {
-    int first;
     int ships;
     struct nstr_item ni;
     struct mchrstr *mp;
     struct shpstr ship;
 
-    first = 1;
     ships = 0;
     snxtitem_xy(&ni, EF_SHIP, x, y);
     while (nxtitem(&ni, &ship)) {
        if (player->owner)
            continue;
-       if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
+       if (!ship.shp_own)
            continue;
        mp = &mchr[(int)ship.shp_type];
        if (wantflags) {
@@ -71,12 +80,8 @@ shipsatxy(coord x, coord y, int wantflags, int nowantflags)
            if (mp->m_flags & nowantflags)
                continue;
        }
-       if (first) {
-           pr(" #          owner           eff       type\n");
-           first = 0;
-       }
-       pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
-          cname(ship.shp_own), effadv(ship.shp_effic), prship(&ship));
+       if (!only_count)
+           list_ship(&ship, !ships);
        ships++;
     }
     return ships;
@@ -85,40 +90,22 @@ shipsatxy(coord x, coord y, int wantflags, int nowantflags)
 /* This one only shows owned or allied ships */
 
 int
-carriersatxy(coord x, coord y, int wantflags, int nowantflags, natid own)
+carriersatxy(coord x, coord y, natid own)
 {
-    int first;
     int ships;
     struct nstr_item ni;
-    struct mchrstr *mp;
     struct shpstr ship;
 
-    first = 1;
     ships = 0;
     snxtitem_xy(&ni, EF_SHIP, x, y);
     while (nxtitem(&ni, &ship)) {
-       if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
+       if (!ship.shp_own)
            continue;
-       if (ship.shp_own != own
-           && getrel(getnatp(ship.shp_own), own) != ALLIED)
+       if (relations_with(ship.shp_own, own) != ALLIED)
            continue;
-       if (ship.shp_effic < SHP_AIROPS_EFF)
+       if ((carrier_planes(&ship, 0) & (P_L | P_K)) == 0)
            continue;
-       mp = &mchr[(int)ship.shp_type];
-       if (wantflags) {
-           if ((mp->m_flags & wantflags) == 0)
-               continue;
-       }
-       if (nowantflags) {
-           if (mp->m_flags & nowantflags)
-               continue;
-       }
-       if (first) {
-           pr(" #          owner           eff       type\n");
-           first = 0;
-       }
-       pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
-          cname(ship.shp_own), effadv(ship.shp_effic), prship(&ship));
+       list_ship(&ship, !ships);
        ships++;
     }
     return ships;
@@ -127,19 +114,16 @@ carriersatxy(coord x, coord y, int wantflags, int nowantflags, natid own)
 int
 unitsatxy(coord x, coord y, int wantflags, int nowantflags)
 {
-    int first;
     int units;
     struct nstr_item ni;
     struct lchrstr *lp;
     struct lndstr land;
 
-    first = 1;
     units = 0;
     snxtitem_xy(&ni, EF_LAND, x, y);
     while (nxtitem(&ni, &land)) {
-       if (land.lnd_effic < LAND_MINEFF || land.lnd_own == 0)
+       if (!land.lnd_own)
            continue;
-       /* Can't bomb units on ships or other units */
        if (land.lnd_ship >= 0 || land.lnd_land >= 0)
            continue;
        lp = &lchr[(int)land.lnd_type];
@@ -153,15 +137,8 @@ unitsatxy(coord x, coord y, int wantflags, int nowantflags)
                continue;
        }
 
-       if (lp->l_flags & L_SPY) {
-           if (!(chance(LND_SPY_DETECT_CHANCE(land.lnd_effic))))
-               continue;
-       }
-
-       if (first) {
+       if (!units)
            pr(" #          owner           eff       type\n");
-           first = 0;
-       }
        pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
           cname(land.lnd_own), effadv(land.lnd_effic), prland(&land));
        units++;
@@ -170,31 +147,25 @@ unitsatxy(coord x, coord y, int wantflags, int nowantflags)
 }
 
 int
-planesatxy(coord x, coord y, int wantflags, int nowantflags,
-          struct emp_qelem *list)
+planesatxy(coord x, coord y, int wantflags, int nowantflags)
 {
-    int first;
     int planes;
     struct plnstr plane;
     struct nstr_item ni;
     struct plchrstr *plp;
 
     planes = 0;
-    first = 1;
     snxtitem_xy(&ni, EF_PLANE, x, y);
     while (nxtitem(&ni, &plane)) {
-       if (plane.pln_effic < PLANE_MINEFF || plane.pln_own == 0)
+       if (!plane.pln_own)
            continue;
-       if (plane.pln_flags & PLN_LAUNCHED)
+       if (plane.pln_ship >= 0 || plane.pln_land >= 0)
            continue;
-       /* Is this plane one of the ones flying somewhere? */
-       if (ac_isflying(&plane, list))
+       if (plane.pln_flags & PLN_LAUNCHED)
            continue;
        plp = &plchr[(int)plane.pln_type];
-       if (first) {
+       if (!planes)
            pr(" #          owner           eff       type\n");
-           first = 0;
-       }
        if (wantflags) {
            if ((plp->pl_flags & wantflags) == 0)
                continue;
@@ -214,19 +185,17 @@ int
 asw_shipsatxy(coord x, coord y, int wantflags, int nowantflags,
              struct plnstr *pp, struct shiplist **head)
 {
-    int first;
     int ships;
     struct nstr_item ni;
     struct mchrstr *mp;
     struct shpstr ship;
 
-    first = 1;
     ships = 0;
     snxtitem_xy(&ni, EF_SHIP, x, y);
     while (nxtitem(&ni, &ship)) {
        if (player->owner)
            continue;
-       if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
+       if (!ship.shp_own)
            continue;
        mp = &mchr[(int)ship.shp_type];
        if (wantflags) {
@@ -238,59 +207,55 @@ asw_shipsatxy(coord x, coord y, int wantflags, int nowantflags,
                continue;
        }
        if (mp->m_flags & M_SUB) {
-           if (roll(100) > pln_hitchance(pp,
-                                         shp_hardtarget(&ship), EF_SHIP))
+           if (!pct_chance(pln_hitchance(pp,
+                                         shp_hardtarget(&ship), EF_SHIP)))
                continue;
        }
        add_shiplist(ship.shp_uid, head);
-       if (first) {
-           pr(" #          owner           eff       type\n");
-           first = 0;
-       }
-       pr("(#%3d) %10.10s  %12.12s  %s\n", ni.cur,
-          cname(ship.shp_own), effadv(ship.shp_effic), prship(&ship));
+       list_ship(&ship, !ships);
        ships++;
     }
     return ships;
 }
 
-int
-num_shipsatxy(coord x, coord y, int wantflags, int nowantflags)
+void
+print_shiplist(struct shiplist *head)
 {
-    int ships;
-    struct nstr_item ni;
-    struct mchrstr *mp;
+    struct shiplist *s;
     struct shpstr ship;
 
-    ships = 0;
-    snxtitem_xy(&ni, EF_SHIP, x, y);
-    while (nxtitem(&ni, &ship)) {
-       if (ship.shp_effic < SHIP_MINEFF || ship.shp_own == 0)
-           continue;
-       mp = &mchr[(int)ship.shp_type];
-       if (wantflags) {
-           if ((mp->m_flags & wantflags) == 0)
-               continue;
-       }
-       if (nowantflags) {
-           if (mp->m_flags & nowantflags)
-               continue;
-       }
-       ships++;
+    for (s = head; s; s = s->next) {
+       getship(s->uid, &ship);
+       list_ship(&ship, s == head);
     }
-    return ships;
 }
 
-/*
- * is p a list of ships/planes/units?
- */
+int
+adj_units(coord x, coord y, natid own)
+{
+    int i;
+    struct sctstr sect;
+
+    for (i = DIR_FIRST; i <= DIR_LAST; i++) {
+       getsect(x + diroff[i][0], y + diroff[i][1], &sect);
+       if (has_units(sect.sct_x, sect.sct_y, own))
+           return 1;
+    }
+    return 0;
+}
 
 int
-islist(char *p)
+has_units(coord x, coord y, natid cn)
 {
-    for (; *p; p++) {
-       if (!isdigit(*p) && *p != '/')
-           return 0;
+    int n;
+    struct lndstr land;
+
+    for (n = 0; ef_read(EF_LAND, n, &land); n++) {
+       if (land.lnd_x != x || land.lnd_y != y)
+           continue;
+       if (land.lnd_own == cn)
+           return 1;
     }
-    return 1;
+
+    return 0;
 }