]> git.pond.sub.org Git - empserver/commitdiff
Replace pln_onewaymission() by pln_where_to_land()
authorMarkus Armbruster <armbru@pond.sub.org>
Sat, 23 Jun 2012 18:36:48 +0000 (20:36 +0200)
committerMarkus Armbruster <armbru@pond.sub.org>
Fri, 13 Jul 2012 18:14:10 +0000 (20:14 +0200)
New function reads and returns target sector/ship.  Avoids reading the
target sector unnecessarily.  Callers receive the target ship, not
just its number.  Next commit will put it to use.

include/prototypes.h
src/lib/commands/fly.c
src/lib/commands/reco.c
src/lib/subs/plnsub.c

index 0971a477378bb9d347785401545cda6b28c00af5..554f6b411ffd198e64b5b5cf3234de92a7a1f1bb 100644 (file)
@@ -504,7 +504,7 @@ extern void pln_prewrite(int, void *, void *);
 extern int get_planes(struct nstr_item *, struct nstr_item *,
                      char *, char *);
 extern struct sctstr *get_assembly_point(char *, struct sctstr *, char *);
-extern int pln_onewaymission(struct sctstr *, int *, int *);
+extern int pln_where_to_land(coord, coord, union empobj_storage *, int *);
 extern int pln_oneway_to_carrier_ok(struct emp_qelem *,
                                    struct emp_qelem *, int);
 extern void pln_newlanding(struct emp_qelem *, coord, coord, int);
index 89b88d79da8471133134f95b5f3ed2de237faf3c..57d298a6d56bb06f376573db275a301d5892a03d 100644 (file)
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Steve McClure, 2000
- *     Markus Armbruster, 2004-2011
+ *     Markus Armbruster, 2004-2012
  */
 
 #include <config.h>
 
 #include "commands.h"
+#include "empobj.h"
 #include "item.h"
 #include "path.h"
 #include "plane.h"
@@ -50,7 +51,7 @@ fly(void)
     int cno;
     struct nstr_item ni_bomb;
     struct nstr_item ni_esc;
-    struct sctstr target;
+    union empobj_storage target;
     struct emp_qelem bomb_list;
     struct emp_qelem esc_list;
     int wantflags;
@@ -74,14 +75,14 @@ fly(void)
     ip = whatitem(player->argp[5], "transport what? ");
     if (player->aborted)
        return RET_SYN;
-    getsect(tx, ty, &target);
 
-    cno = -1;
-    if (pln_onewaymission(&target, &cno, &wantflags) < 0)
+    if (pln_where_to_land(tx, ty, &target, &wantflags) < 0)
        return RET_SYN;
+    cno = target.gen.ef_type == EF_SHIP ? target.gen.uid : -1;
 
     if (ip && ip->i_uid == I_CIVIL
-       && cno < 0 && target.sct_own != target.sct_oldown) {
+       && target.gen.ef_type == EF_SECTOR
+       && target.sect.sct_own != target.sect.sct_oldown) {
        pr("Can't fly civilians into occupied sectors.\n");
        return RET_FAIL;
     }
index 9c1edb687dee3edace578272dc7253be42c44e41..e27031cfe929cfed366565e531618220d7583ba3 100644 (file)
  *
  *  Known contributors to this file:
  *     Dave Pare, 1986
- *     Markus Armbruster, 2004-2011
+ *     Markus Armbruster, 2004-2012
  */
 
 #include <config.h>
 
 #include "commands.h"
+#include "empobj.h"
 #include "path.h"
 #include "plane.h"
 
@@ -48,7 +49,7 @@ reco(void)
     int cno;
     struct nstr_item ni_bomb;
     struct nstr_item ni_esc;
-    struct sctstr target;
+    union empobj_storage target;
     struct emp_qelem bomb_list;
     struct emp_qelem esc_list;
     int wantflags;
@@ -69,10 +70,11 @@ reco(void)
     ty = ay;
     (void)pathtoxy(flightpath, &tx, &ty, fcost);
     pr("target is %s\n", xyas(tx, ty, player->cnum));
-    getsect(tx, ty, &target);
-    cno = -1;
-    if (pln_onewaymission(&target, &cno, &wantflags) < 0)
+
+    if (pln_where_to_land(tx, ty, &target, &wantflags) < 0)
        return RET_SYN;
+    cno = target.gen.ef_type == EF_SHIP ? target.gen.uid : -1;
+
     ap_to_target = strlen(flightpath);
     if (flightpath[ap_to_target - 1] == 'h')
        ap_to_target--;
index 8ee93a84aa564fc16d908d621027cd53da4da939..7c6f49cb49b3b94a09104bffd40e0aab2afc56de 100644 (file)
@@ -35,6 +35,7 @@
 
 #include <config.h>
 
+#include "empobj.h"
 #include "file.h"
 #include "item.h"
 #include "land.h"
@@ -116,20 +117,25 @@ get_assembly_point(char *input, struct sctstr *ap_sect, char *buf)
     return NULL;
 }
 
+/*
+ * Find out whether planes can fly one-way to X,Y.
+ * Offer the player any carriers there.  If he chooses one, read it
+ * into TARGET->ship.  Else read the target sector into TARGET->sect.
+ * If planes can land there, set required plane flags in *FLAGSP, and
+ * return 0.  Else return -1.
+ */
 int
-pln_onewaymission(struct sctstr *target, int *shipno, int *flagp)
+pln_where_to_land(coord x, coord y,
+                 union empobj_storage *target, int *flagsp)
 {
     int nships;
     int cno;
-    int flags, fl;
-    struct shpstr ship;
+    int fl;
     char buf[1024];
     char *p;
 
-    flags = *flagp;
-
     /* offer carriers */
-    nships = carriersatxy(target->sct_x, target->sct_y, player->cnum);
+    nships = carriersatxy(x, y, player->cnum);
     if (nships) {
        for (;;) {
            p = getstring("Carrier #? ", buf);
@@ -138,50 +144,42 @@ pln_onewaymission(struct sctstr *target, int *shipno, int *flagp)
            if (!*p)
                break;
            cno = atoi(p);
-           if (cno < 0
-               || !getship(cno, &ship)
+           if (!getship(cno, &target->ship)
                || (!player->owner
-                   && (relations_with(ship.shp_own, player->cnum)
+                   && (relations_with(target->ship.shp_own, player->cnum)
                        != ALLIED))) {
                pr("Not yours\n");
                continue;
            }
-           if (ship.shp_x != target->sct_x || ship.shp_y != target->sct_y) {
-               pr("Ship #%d not in %s\n", cno,
-                  xyas(target->sct_x, target->sct_y, player->cnum));
+           if (target->ship.shp_x != x || target->ship.shp_y != y) {
+               pr("Ship #%d not in %s\n", cno, xyas(x, y, player->cnum));
                continue;
            }
-           fl = carrier_planes(&ship, 0);
+           fl = carrier_planes(&target->ship, 0);
            if (fl == 0) {
-               pr("Can't land on %s.\n", prship(&ship));
+               pr("Can't land on %s.\n", prship(&target->ship));
                continue;
            }
            /* clear to land on ship#CNO */
            pr("landing on carrier %d\n", cno);
-           flags |= fl;
-           *shipno = cno;
-           *flagp = flags;
+           *flagsp |= fl;
            return 0;
        }
     }
 
     /* try to land at sector */
-    if (relations_with(target->sct_own, player->cnum) != ALLIED) {
-       pr("Nowhere to land at sector %s!\n",
-          xyas(target->sct_x, target->sct_y, player->cnum));
+    getsect(x, y, &target->sect);
+    if (relations_with(target->sect.sct_own, player->cnum) != ALLIED) {
+       pr("Nowhere to land at sector %s!\n", xyas(x, y, player->cnum));
        return -1;
     }
-    if (target->sct_type == SCT_MOUNT) {
-       pr("Nowhere to land at sector %s!\n",
-          xyas(target->sct_x, target->sct_y, player->cnum));
+    if (target->sect.sct_type == SCT_MOUNT) {
+       pr("Nowhere to land at sector %s!\n", xyas(x, y, player->cnum));
        return -1;
     }
-    if (target->sct_type != SCT_AIRPT || target->sct_effic < 60)
-       flags |= P_V;
-
     /* clear to land at sector */
-    *shipno = -1;
-    *flagp = flags;
+    if (target->sect.sct_type != SCT_AIRPT || target->sect.sct_effic < 60)
+       *flagsp |= P_V;
     return 0;
 }