]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/navi.c
Update copyright notice
[empserver] / src / lib / commands / navi.c
index 266f9733da769159983e7c6692ae51523034752c..2f409f03f61071fa8bcbc85beec89e53d7e5fe01 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-2013, 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/>.
  *
  *  ---
  *
  *  ---
  *
  *  navi.c: Navigate ships and such
- * 
+ *
  *  Known contributors to this file:
  *     Ken Stevens, 1995 (rewritten)
+ *     Ron Koenderink, 2006-2007
+ *     Markus Armbruster, 2006-2011
  */
 
 #include <config.h>
 
-#include <ctype.h>
-#include "misc.h"
-#include "player.h"
-#include "ship.h"
-#include "sect.h"
-#include "xy.h"
-#include "nsc.h"
-#include "nat.h"
-#include "path.h"
-#include "file.h"
-#include "map.h"
 #include "commands.h"
+#include "map.h"
 #include "optlist.h"
+#include "path.h"
+#include "unit.h"
 
-static int set_flagship(struct emp_qelem *list, struct shpstr **flagshipp);
+static void pr_leader_change(struct empobj *leader);
+static struct empobj *get_leader(struct emp_qelem *list);
+static void switch_leader(struct emp_qelem *list, int uid);
 
 int
 navi(void)
@@ -56,19 +51,8 @@ navi(void)
     struct emp_qelem ship_list;
     double minmob, maxmob;
     int together;
-    char *cp = 0;
-    struct shpstr *shp = 0;    /* flagship */
-    struct nstr_sect ns;
-    char origin;
-    int dir;
-    int stopping = 0;
-    int skip = 0;
-    char buf[1024];
-    char prompt[128];
-    char pathtaken[1024];      /* Doubtful we'll have a path longer than this */
-    char *pt = pathtaken;
 
-    if (!snxtitem(&ni_ship, EF_SHIP, player->argp[1]))
+    if (!snxtitem(&ni_ship, EF_SHIP, player->argp[1], NULL))
        return RET_SYN;
     shp_sel(&ni_ship, &ship_list);
     shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
@@ -76,138 +60,211 @@ navi(void)
        pr("No ships\n");
        return RET_FAIL;
     }
-    set_flagship(&ship_list, &shp);
+
+    return do_unit_move(&ship_list, &together, &minmob, &maxmob);
+}
+
+int
+do_unit_move(struct emp_qelem *ulist, int *together,
+            double *minmob, double *maxmob)
+{
+    char *cp = NULL;
+    int leader_uid;
+    struct empobj *leader;
+    int dir;
+    int stopping = 0;
+    int skip = 0;
+    char buf[1024];
+    char prompt[128];
+    char pathtaken[1024];  /* Doubtful we'll have a path longer than this */
+    char *pt = pathtaken;
+    char bmap_flag;
+    int ac;
+    int type;
+
+    leader = get_leader(ulist);
+    leader_uid = leader->uid;
+    type = leader->ef_type;
+    pr("%s is %s\n",
+       type == EF_SHIP ? "Flagship" : "Leader",
+       unit_nameof(leader));
+
     if (player->argp[2]) {
        strcpy(buf, player->argp[2]);
-       if (!(cp = shp_path(together, shp, buf)))
-           cp = player->argp[2];
+       cp = unit_path(*together, leader, buf, sizeof(buf));
     }
 
     *pt = '\0';
-    while (!QEMPTY(&ship_list)) {
-       char *bp, dp[80];
+    while (!QEMPTY(ulist)) {
+       char dp[80];
 
-       if (cp == 0 || *cp == '\0' || stopping) {
+       if (cp == NULL || *cp == '\0' || stopping) {
            stopping = 0;
-           shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
-           if (QEMPTY(&ship_list)) {
-               pr("No ships left\n");
-               if (strlen(pathtaken) > 0) {
+           if (type == EF_SHIP)
+               shp_nav(ulist, minmob, maxmob, together, player->cnum);
+           else
+               lnd_mar(ulist, minmob, maxmob, together, player->cnum);
+           if (QEMPTY(ulist)) {
+               pr("No %s left\n", type == EF_SHIP ? "ships" : "lands");
+               if (type == EF_SHIP && strlen(pathtaken) > 1) {
                    pathtaken[strlen(pathtaken) - 1] = '\0';
-                   if (strlen(pathtaken) > 0)
-                       pr("Path taken: %s\n", pathtaken);
+                   pr("Path taken: %s\n", pathtaken);
                }
                return RET_OK;
            }
-           if (set_flagship(&ship_list, &shp)) {
+           leader = get_leader(ulist);
+           if (leader->uid != leader_uid) {
+               leader_uid = leader->uid;
+               pr_leader_change(leader);
                stopping = 1;
                continue;
            }
            if (!skip)
-               nav_map(shp->shp_x, shp->shp_y,
-                       !(mchr[(int)shp->shp_type].m_flags & M_SUB));
+               nav_map(leader->x, leader->y,
+                       type == EF_SHIP
+                       ? !(mchr[(int)leader->type].m_flags & M_SUB) : 1);
            else
                skip = 0;
-           sprintf(prompt, "<%.1f:%.1f: %s> ", maxmob,
-                   minmob, xyas(shp->shp_x, shp->shp_y, player->cnum));
+           sprintf(prompt, "<%.1f:%.1f: %s> ", *maxmob,
+                   *minmob, xyas(leader->x, leader->y, player->cnum));
            cp = getstring(prompt, buf);
-           /* Just in case any of our ships were shelled while we were at the
-            * prompt, we call shp_nav() again.
+           /* Just in case any of our units were shelled while we were
+            * at the prompt, we call shp_nav() or lnd_mar() again.
             */
-           shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
-           if (QEMPTY(&ship_list)) {
-               pr("No ships left\n");
-               if (strlen(pathtaken) > 0) {
+           if (type == EF_SHIP)
+               shp_nav(ulist, minmob, maxmob, together, player->cnum);
+           else
+               lnd_mar(ulist, minmob, maxmob, together, player->cnum);
+           if (QEMPTY(ulist)) {
+               pr("No %s left\n", type == EF_SHIP ? "ships" : "lands");
+               if (type == EF_SHIP && strlen(pathtaken) > 1) {
                    pathtaken[strlen(pathtaken) - 1] = '\0';
-                   if (strlen(pathtaken) > 0)
-                       pr("Path taken: %s\n", pathtaken);
+                   pr("Path taken: %s\n", pathtaken);
                }
                return RET_OK;
            }
-           if (set_flagship(&ship_list, &shp)) {
+           leader = get_leader(ulist);
+           if (leader->uid != leader_uid) {
+               leader_uid = leader->uid;
+               pr_leader_change(leader);
                stopping = 1;
                continue;
            }
+           if (cp)
+               cp = unit_path(*together, leader, buf, sizeof(buf));
        }
-       radmapnopr(shp->shp_x, shp->shp_y, (int)shp->shp_effic,
-                  (int)techfact(shp->shp_tech,
-                                mchr[(int)shp->shp_type].m_vrnge),
-                  (mchr[(int)shp->shp_type].m_flags & M_SONAR)
-                  ? techfact(shp->shp_tech, 1.0) : 0.0);
-       if (cp == 0 || *cp == '\0')
+       if (type == EF_SHIP) {
+           rad_map_set(player->cnum, leader->x, leader->y, leader->effic,
+                       leader->tech, mchr[leader->type].m_vrnge);
+       }
+       if (cp == NULL || *cp == '\0')
            cp = &dirch[DIR_STOP];
-       if (*cp == 'M' ||
-           *cp == 'B' || *cp == 'f' || *cp == 'i' || *cp == 'm') {
-           ++cp;
-           if (cp[-1] == 'M') {
-               unit_map(EF_SHIP, shp->shp_uid, &ns, &origin);
-               draw_map(0, origin, MAP_SHIP, &ns);
-               skip = 1;
-           } else if (cp[-1] == 'B') {
-               unit_map(EF_SHIP, shp->shp_uid, &ns, &origin);
-               draw_map('b', origin, MAP_SHIP, &ns);
-               skip = 1;
-           } else if (cp[-1] == 'f') {
-               struct emp_qelem *qp;
-               qp = ship_list.q_back;
-               emp_remque(ship_list.q_back);
-               emp_insque(qp, &ship_list);
-               set_flagship(&ship_list, &shp);
-           } else if (cp[-1] == 'i') {
-               shp_list(&ship_list);
-           } else {
-               stopping |= shp_sweep(&ship_list, 1, player->cnum);
-           }
+       dir = chkdir(*cp, DIR_STOP, DIR_LAST);
+       if (dir >= 0) {
+           if (type == EF_SHIP) {
+               stopping |= shp_nav_one_sector(ulist, dir,
+                                              player->cnum, *together);
+               if (stopping != 2) {
+                   *pt++ = dirch[dir];
+                   *pt = '\0';
+               }
+           } else
+               stopping |=
+                   lnd_mar_one_sector(ulist, dir, player->cnum,
+                                      *together);
+           cp++;
            continue;
-       } else if (*cp == 'r' || *cp == 'l' || *cp == 's') {
-           for (bp = cp + 1; *bp && !isspace(*bp); bp++) ;
-           for (; *bp && isspace(*bp); bp++) ;
-           if (*bp)
-               player->argp[1] = bp;
-           else {
-               sprintf(dp, "%d", shp->shp_uid);
-               player->argp[1] = dp;
+       }
+       ac = parse(cp, player->argbuf, player->argp, NULL, NULL, NULL);
+       if (ac <= 0) {
+           player->argp[0] = "";
+           cp = NULL;
+       } else if (ac == 1) {
+           sprintf(dp, "%d", leader->uid);
+           player->argp[1] = dp;
+           cp++;
+       } else
+           cp = NULL;
+       bmap_flag = 0;
+       switch (*player->argp[0]) {
+       case 'B':
+           bmap_flag = 'b';
+           /*
+            * fall through
+            */
+       case 'M':
+           display_region_map(bmap_flag, type, leader->x, leader->y,
+                              player->argp[1], player->argp[2]);
+           skip = 1;
+           continue;
+       case 'f':
+           if (ac <= 1)
+               switch_leader(ulist, -1);
+           else
+               switch_leader(ulist, atoi(player->argp[1]));
+           leader = get_leader(ulist);
+           if (leader->uid != leader_uid) {
+               leader_uid = leader->uid;
+               pr_leader_change(leader);
            }
-           if (*cp++ == 'r') {
-               rada();
-               skip = 1;
-           } else if (cp[-1] == 'l')
-               look();
+           continue;
+       case 'i':
+           unit_list(ulist);
+           continue;
+       case 'm':
+           if (type == EF_SHIP)
+               stopping |= shp_sweep(ulist, 1, 1, player->cnum);
            else {
-               player->argp[2] = 0;
-               sona();
-               skip = 1;
+               lnd_sweep(ulist, 1, 1, player->cnum);
+               stopping |= lnd_check_mines(ulist);
            }
-           *cp = 0;
+           continue;
+       case 'r':
+           radar(leader->ef_type);
+           skip = 1;
            player->btused++;
            continue;
-       } else {
-           dir = chkdir(*cp++, DIR_STOP, DIR_VIEW);
-           if (dir < 0) {
-               if (NULL != (cp = shp_path(together, shp, buf)))
-                   continue;
-               direrr("`%c' to stop", ", `%c' to view, ", 0);
-               pr("`i' to list ships, `f' to change flagship,\n");
-               pr("`r' to radar, `s' to sonar, `l' to look, `M' to map, `B' to bmap,\n");
-               pr("and `m' to minesweep\n");
-               stopping = 1;
-               continue;
-           } else if (dir == DIR_VIEW) {
-               shp_view(&ship_list);
-               continue;
+       case 'l':
+           do_look(type);
+           player->btused++;
+           continue;
+       case 's':
+           if (leader->ef_type != EF_SHIP)
+               break;
+           sona();
+           player->btused++;
+           skip = 1;
+           continue;
+       case 'd':
+           if (ac < 3) {
+               player->argp[2] = ac < 2 ? "1" : player->argp[1];
+               sprintf(dp, "%d", leader->uid);
+               player->argp[1] = dp;
            }
+           if (type == EF_SHIP)
+               mine();
+           else
+               landmine();
+           stopping = 1;
+           skip = 1;
+           player->btused++;
+           continue;
+       case 'v':
+           unit_view(ulist);
+           continue;
        }
-       stopping |=
-           shp_nav_one_sector(&ship_list, dir, player->cnum, together);
-       if (stopping != 2) {
-           *pt++ = dirch[dir];
-           *pt = '\0';
-       }
+       direrr("`%c' to stop", ", `%c' to view", NULL);
+       pr(", `i' to list %s, `f' to change %s,\n",
+           type == EF_SHIP ? "ships" : "units",
+           type == EF_SHIP ? "flagship" : "leader");
+       pr("`r' to radar, %s`l' to look, `M' to map, `B' to bmap,\n",
+           type == EF_SHIP ? "`s' to sonar, " : "");
+       pr("`d' to drop mines, and `m' to minesweep\n");
+       stopping = 1;
     }
-    if (strlen(pathtaken) > 0) {
+    if (type == EF_SHIP && strlen(pathtaken) > 1) {
        pathtaken[strlen(pathtaken) - 1] = '\0';
-       if (strlen(pathtaken) > 0)
-           pr("Path taken: %s\n", pathtaken);
+       pr("Path taken: %s\n", pathtaken);
     }
     return RET_OK;
 }
@@ -217,13 +274,10 @@ nav_map(int x, int y, int show_designations)
 {
     char *ptr;
     struct nstr_sect ns;
-    struct natstr *np;
     struct sctstr sect;
-    struct range range;
     int i;
     /* Note this is not re-entrant anyway, so we keep the buffers
        around */
-    static unsigned char *bitmap = NULL;
     static char *wmapbuf = NULL;
     static char **wmap = NULL;
     int changed = 0;
@@ -240,17 +294,12 @@ nav_map(int x, int y, int show_designations)
            wmap = NULL;
        }
     }
-    if (!bitmap)
-       bitmap = malloc((WORLD_X * WORLD_Y) / 8);
-    if (!wmapbuf || !wmap || !bitmap) {
+    if (!wmapbuf || !wmap) {
        pr("Memory error, tell the deity.\n");
        logerror("malloc failed in navi\n");
        return RET_FAIL;
     }
-    memset(bitmap, 0, (WORLD_X * WORLD_Y) / 8);
     snxtsct_dist(&ns, x, y, 1);
-    np = getnatp(player->cnum);
-    xyrelrange(np, &ns.range, &range);
     blankfill(wmapbuf, &ns.range, 1);
     while (nxtsct(&ns, &sect)) {
        ptr = &wmap[ns.dy][ns.dx];
@@ -270,7 +319,7 @@ nav_map(int x, int y, int show_designations)
         * in which case they'll see that.
         * --ts
         */
-       *ptr = player->bmap[sctoff(sect.sct_x, sect.sct_y)];
+       *ptr = player->bmap[sect.sct_uid];
     }
     if (changed)
        writemap(player->cnum);
@@ -279,18 +328,36 @@ nav_map(int x, int y, int show_designations)
     return RET_OK;
 }
 
-static int
-set_flagship(struct emp_qelem *list, struct shpstr **flagshipp)
+static void
+pr_leader_change(struct empobj *leader)
 {
-    struct mlist *mlp = (struct mlist *)(list->q_back);
+    pr("Changing %s to %s\n",
+       leader->ef_type == EF_SHIP ? "flagship" : "leader",
+       unit_nameof(leader));
+}
+
+static struct empobj *
+get_leader(struct emp_qelem *list)
+{
+    return &((struct ulist *)(list->q_back))->unit.gen;
+}
+
+static void
+switch_leader(struct emp_qelem *list, int uid)
+{
+    struct emp_qelem *qp, *save;
+    struct ulist *ulp;
+
+    if (QEMPTY(list))
+       return;
 
-    if (!*flagshipp)
-       pr("Flagship is ");
-    else if ((*flagshipp)->shp_uid != mlp->ship.shp_uid)
-       pr("Changing flagship to ");
-    else
-       return 0;
-    *flagshipp = &mlp->ship;
-    pr("%s\n", prship(&mlp->ship));
-    return 1;
+    save = qp = list->q_back;
+    do {
+       emp_remque(qp);
+       emp_insque(qp, list);
+       qp = list->q_back;
+       ulp = (struct ulist *)qp;
+       if (ulp->unit.gen.uid == uid || uid == -1)
+           break;
+    } while (list->q_back != save);
 }