]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/navi.c
(navi): Reorganize, use parse() to split arguments.
[empserver] / src / lib / commands / navi.c
index 7bfde97e77dc62fbd1c468f964b63b895203e97d..7b55839065a861219881686d9a67030852632ab6 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -19,9 +19,9 @@
  *
  *  ---
  *
- *  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.
  *
  *  ---
  *
  *     Ken Stevens, 1995 (rewritten)
  */
 
+#include <config.h>
+
 #include <ctype.h>
 #include "misc.h"
 #include "player.h"
-#include "var.h"
 #include "ship.h"
 #include "sect.h"
-#include "news.h"
 #include "xy.h"
 #include "nsc.h"
 #include "nat.h"
@@ -56,23 +56,25 @@ navi(void)
     struct emp_qelem ship_list;
     double minmob, maxmob;
     int together;
-    s_char *cp = 0;
-    struct shpstr *shp = 0;    /* flagship */
+    char *cp = NULL;
+    struct shpstr *shp = NULL; /* flagship */
     struct nstr_sect ns;
-    s_char origin;
+    char origin;
     int dir;
     int stopping = 0;
     int skip = 0;
-    s_char buf[1024];
-    s_char prompt[128];
-    s_char pathtaken[1024];    /* Doubtful we'll have a path longer than this */
-    s_char *pt = pathtaken;
+    char buf[1024];
+    char prompt[128];
+    char scanspace[1024];
+    char pathtaken[1024];      /* Doubtful we'll have a path longer than this */
+    char *pt = pathtaken;
+    char bmap_flag;
+    int ac;
 
     if (!snxtitem(&ni_ship, EF_SHIP, player->argp[1]))
        return RET_SYN;
     shp_sel(&ni_ship, &ship_list);
     shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
-    player->condarg = 0;       /* conditions don't apply to nav_map() */
     if (QEMPTY(&ship_list)) {
        pr("No ships\n");
        return RET_FAIL;
@@ -86,9 +88,9 @@ navi(void)
 
     *pt = '\0';
     while (!QEMPTY(&ship_list)) {
-       s_char *bp, dp[80];
+       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)) {
@@ -112,8 +114,8 @@ navi(void)
            sprintf(prompt, "<%.1f:%.1f: %s> ", maxmob,
                    minmob, xyas(shp->shp_x, shp->shp_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 ships were shelled while we were
+            * at the prompt, we call shp_nav() again.
             */
            shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
            if (QEMPTY(&ship_list)) {
@@ -129,83 +131,87 @@ navi(void)
                stopping = 1;
                continue;
            }
+           if (cp && !(cp = shp_path(together, shp, buf)))
+               cp = buf;
        }
        radmapnopr(shp->shp_x, shp->shp_y, (int)shp->shp_effic,
                   (int)techfact(shp->shp_tech,
-                                (double)mchr[(int)shp->shp_type].m_vrnge),
-                  (double)((mchr[(int)shp->shp_type].m_flags & M_SONAR)
-                           ? techfact(shp->shp_tech, 1.0) : 0.0));
-       if (cp == 0 || *cp == '\0')
+                                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 == 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, player->cnum);
-               skip = 1;
-           } else if (cp[-1] == 'B') {
-               unit_map(EF_SHIP, shp->shp_uid, &ns, &origin);
-               draw_map(EF_BMAP, origin, MAP_SHIP, &ns, player->cnum);
-               skip = 1;
-           } else if (cp[-1] == 'f') {
+       }
+       dir = chkdir(*cp, DIR_STOP, DIR_VIEW);
+       if (dir >= 0) {
+           if (dir == DIR_VIEW)
+               shp_view(&ship_list);
+           else {
+               stopping |= shp_nav_one_sector(&ship_list, dir, player->cnum, together);
+               if (stopping != 2) {
+                   *pt++ = dirch[dir];
+                   *pt = '\0';
+               }
+           }
+           cp++;
+           continue;
+       }
+       ac = parse(cp, player->argp, NULL, scanspace, NULL);
+       if (ac <= 1) {
+           sprintf(dp, "%d", shp->shp_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':
+           unit_map(EF_SHIP, shp->shp_uid, &ns, &origin);
+           draw_map(bmap_flag, origin, MAP_SHIP, &ns);
+           skip = 1;
+           break;
+       case '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);
            }
-           continue;
-       } else if (*cp == 'r' || *cp == 'l' || *cp == 's') {
-           bp = ++cp;
-           while ((*bp != ' ') && (*bp))
-               bp++;
-           while ((*bp == ' ') && (*bp))
-               bp++;
-           if ((bp != (s_char *)0) && (*bp))
-               player->argp[1] = bp;
-           else {
-               sprintf(dp, "%d", shp->shp_uid);
-               player->argp[1] = dp;
-           }
-           if (cp[-1] == 'r') {
-               rada();
-               skip = 1;
-           } else if (cp[-1] == 'l')
-               look();
-           else {
-               player->argp[2] = 0;
-               sona();
-               skip = 1;
-           }
-           *cp = 0;
+           break;
+       case 'i':
+           shp_list(&ship_list);
+           skip = 1;
+           break;
+       case 'm':
+           stopping |= shp_sweep(&ship_list, 1, 0, player->cnum);
+           break;
+       case 'r':
+           rada();
+           skip = 1;
            player->btused++;
-           continue;
-       } else {
-           dir = chkdir(*cp++, DIR_STOP, DIR_VIEW);
-           if (dir == -1) {
-               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;
-           }
-       }
-       stopping |=
-           shp_nav_one_sector(&ship_list, dir, player->cnum, together);
-       if (stopping != 2) {
-           *pt++ = dirch[dir];
-           *pt = '\0';
+           break;
+       case 'l':
+           look();
+           player->btused++;
+           break;
+       case 's':
+           sona();
+           player->btused++;
+           skip = 1;
+           break;
+       default:
+           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;
        }
     }
     if (strlen(pathtaken) > 0) {
@@ -219,7 +225,7 @@ navi(void)
 int
 nav_map(int x, int y, int show_designations)
 {
-    register s_char *ptr;
+    char *ptr;
     struct nstr_sect ns;
     struct natstr *np;
     struct sctstr sect;
@@ -227,42 +233,35 @@ nav_map(int x, int y, int show_designations)
     int i;
     /* Note this is not re-entrant anyway, so we keep the buffers
        around */
-    static u_char *bitmap = (u_char *)0;
-    static s_char *wmapbuf = (s_char *)0;
-    static s_char **wmap = (s_char **)0;
-    s_char what[64];
+    static unsigned char *bitmap = NULL;
+    static char *wmapbuf = NULL;
+    static char **wmap = NULL;
     int changed = 0;
 
-    np = getnatp(player->cnum);
-    sprintf(what, "%d:%d,%d:%d", xrel(np, x - 2), xrel(np, x + 2),
-           yrel(np, y - 1), yrel(np, y + 1));
-    if (!snxtsct(&ns, what))
-       return RET_FAIL;
     if (!wmapbuf)
-       wmapbuf =
-           (s_char *)malloc((WORLD_Y * MAPWIDTH(1)) * sizeof(s_char));
+       wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
     if (!wmap) {
-       wmap = (s_char **)malloc(WORLD_Y * sizeof(s_char *));
+       wmap = malloc(WORLD_Y * sizeof(*wmap));
        if (wmap && wmapbuf) {
            for (i = 0; i < WORLD_Y; i++)
                wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
        } else if (wmap) {
-           free((s_char *)wmap);
-           wmap = (s_char **)0;
+           free(wmap);
+           wmap = NULL;
        }
     }
     if (!bitmap)
-       bitmap = (u_char *)malloc((WORLD_X * WORLD_Y) / 8);
+       bitmap = malloc((WORLD_X * WORLD_Y) / 8);
     if (!wmapbuf || !wmap || !bitmap) {
        pr("Memory error, tell the deity.\n");
        logerror("malloc failed in navi\n");
        return RET_FAIL;
     }
     memset(bitmap, 0, (WORLD_X * WORLD_Y) / 8);
-    /* zap any conditionals */
-    ns.ncond = 0;
+    snxtsct_dist(&ns, x, y, 1);
+    np = getnatp(player->cnum);
     xyrelrange(np, &ns.range, &range);
-    blankfill((s_char *)wmapbuf, &ns.range, 1);
+    blankfill(wmapbuf, &ns.range, 1);
     while (nxtsct(&ns, &sect)) {
        ptr = &wmap[ns.dy][ns.dx];
        *ptr = dchr[sect.sct_type].d_mnem;