From d9a915a05b9a1d855538a5394574ba259916b132 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Sat, 23 Jun 2012 20:36:48 +0200 Subject: [PATCH] Replace pln_onewaymission() by pln_where_to_land() 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 | 2 +- src/lib/commands/fly.c | 13 +++++----- src/lib/commands/reco.c | 12 +++++---- src/lib/subs/plnsub.c | 54 ++++++++++++++++++++--------------------- 4 files changed, 41 insertions(+), 40 deletions(-) diff --git a/include/prototypes.h b/include/prototypes.h index 0971a477..554f6b41 100644 --- a/include/prototypes.h +++ b/include/prototypes.h @@ -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); diff --git a/src/lib/commands/fly.c b/src/lib/commands/fly.c index 89b88d79..57d298a6 100644 --- a/src/lib/commands/fly.c +++ b/src/lib/commands/fly.c @@ -29,12 +29,13 @@ * Known contributors to this file: * Dave Pare, 1986 * Steve McClure, 2000 - * Markus Armbruster, 2004-2011 + * Markus Armbruster, 2004-2012 */ #include #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; } diff --git a/src/lib/commands/reco.c b/src/lib/commands/reco.c index 9c1edb68..e27031cf 100644 --- a/src/lib/commands/reco.c +++ b/src/lib/commands/reco.c @@ -28,12 +28,13 @@ * * Known contributors to this file: * Dave Pare, 1986 - * Markus Armbruster, 2004-2011 + * Markus Armbruster, 2004-2012 */ #include #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--; diff --git a/src/lib/subs/plnsub.c b/src/lib/subs/plnsub.c index 8ee93a84..7c6f49cb 100644 --- a/src/lib/subs/plnsub.c +++ b/src/lib/subs/plnsub.c @@ -35,6 +35,7 @@ #include +#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; }