load: Factor out plane_loadable(), land_loadable()

load_plane_ship() and load_plane_land() duplicate code to check
whether a plane can be loaded, except for the phrasing of one message.
Factor out into plane_loadable().  tran_plane() has equivalent code,
but wants different messages, which makes de-duplication unattractive.

load_land_ship() and load_land_land() duplicate code to check whether
a land unit can be loaded.  Factor out into land_loadable().

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
This commit is contained in:
Markus Armbruster 2017-09-03 17:58:42 +02:00
parent c931758307
commit 31256510cc
2 changed files with 54 additions and 69 deletions

View file

@ -365,6 +365,54 @@ still_ok_land(struct sctstr *sectp, struct lndstr *landp)
return 1; return 1;
} }
static int
plane_loadable(struct plnstr *pp, int noisy)
{
if (pp->pln_ship >= 0) {
if (noisy)
pr("%s is already on ship #%d!\n",
prplane(pp), pp->pln_ship);
return 0;
}
if (pp->pln_land >= 0) {
if (noisy)
pr("%s is already on land unit #%d!\n",
prplane(pp), pp->pln_land);
return 0;
}
return 1;
}
static int
land_loadable(struct lndstr *lp, int noisy)
{
if (lp->lnd_ship >= 0) {
if (noisy)
pr("%s is already on ship #%d!\n",
prland(lp), lp->lnd_ship);
return 0;
}
if (lp->lnd_land >= 0) {
if (noisy)
pr("%s is already on land #%d!\n",
prland(lp), lp->lnd_land);
return 0;
}
if (lnd_first_on_land(lp) >= 0) {
/* Outlawed to prevent arbitrarily deep recursion */
if (noisy)
pr("%s cannot be loaded since it is carrying units\n",
prland(lp));
return 0;
}
if (lchr[lp->lnd_type].l_flags & L_HEAVY) {
if (noisy)
pr("%s is too heavy to load.\n", prland(lp));
return 0;
}
return 1;
}
static int static int
load_plane_ship(struct sctstr *sectp, struct shpstr *sp, int noisy, load_plane_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
int loading, int *nshipsp) int loading, int *nshipsp)
@ -413,18 +461,8 @@ load_plane_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
pr("You can only load light planes, helos, xtra-light, or missiles onto ships.\n"); pr("You can only load light planes, helos, xtra-light, or missiles onto ships.\n");
continue; continue;
} }
if (loading && pln.pln_ship > -1) { if (loading && !plane_loadable(&pln, noisy))
if (noisy)
pr("%s is already on ship #%d!\n",
prplane(&pln), pln.pln_ship);
continue; continue;
}
if (loading && pln.pln_land > -1) {
if (noisy)
pr("%s is already on land unit #%d!\n",
prplane(&pln), pln.pln_land);
continue;
}
if (pln.pln_harden != 0) { if (pln.pln_harden != 0) {
if (noisy) if (noisy)
pr("%s has been hardened and can't be loaded\n", pr("%s has been hardened and can't be loaded\n",
@ -532,29 +570,8 @@ load_land_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
continue; continue;
if (loading) { if (loading) {
if (land.lnd_ship > -1) { if (!land_loadable(&land, noisy))
if (noisy)
pr("%s is already on ship #%d!\n",
prland(&land), land.lnd_ship);
continue; continue;
}
if (land.lnd_land > -1) {
if (noisy)
pr("%s is already on land #%d!\n",
prland(&land), land.lnd_land);
continue;
}
if (lnd_first_on_land(&land) >= 0) {
if (noisy)
pr("%s cannot be loaded since it is carrying units\n",
prland(&land));
continue;
}
if (lchr[(int)land.lnd_type].l_flags & L_HEAVY) {
if (noisy)
pr("%s is too heavy to load.\n", prland(&land));
continue;
}
if (load_spy && !(lchr[(int)land.lnd_type].l_flags & L_SPY)) { if (load_spy && !(lchr[(int)land.lnd_type].l_flags & L_SPY)) {
if (noisy) if (noisy)
pr("Subs can only carry spy units.\n"); pr("Subs can only carry spy units.\n");
@ -737,19 +754,8 @@ load_plane_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
pr("You can only load xlight planes onto units.\n"); pr("You can only load xlight planes onto units.\n");
continue; continue;
} }
if (loading && !plane_loadable(&pln, noisy))
if (loading && pln.pln_ship > -1) {
if (noisy)
pr("%s is already on ship #%d!\n",
prplane(&pln), pln.pln_ship);
continue; continue;
}
if (loading && pln.pln_land > -1) {
if (noisy)
pr("%s is already on unit #%d!\n",
prplane(&pln), pln.pln_land);
continue;
}
if (pln.pln_harden != 0) { if (pln.pln_harden != 0) {
if (noisy) if (noisy)
pr("%s has been hardened and can't be loaded\n", pr("%s has been hardened and can't be loaded\n",
@ -889,34 +895,13 @@ load_land_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
continue; continue;
if (loading) { if (loading) {
if (land.lnd_ship > -1) {
if (noisy)
pr("%s is already on ship #%d!\n",
prland(&land), land.lnd_ship);
continue;
}
if (land.lnd_land > -1) {
if (noisy)
pr("%s is already on land #%d!\n",
prland(&land), land.lnd_land);
continue;
}
if (lnd_first_on_land(&land) >= 0) {
if (noisy)
pr("%s cannot be loaded since it is carrying units\n",
prland(&land));
continue;
}
if (land.lnd_uid == lp->lnd_uid) { if (land.lnd_uid == lp->lnd_uid) {
if (noisy) if (noisy)
pr("%s can't be loaded onto itself!\n", prland(&land)); pr("%s can't be loaded onto itself!\n", prland(&land));
continue; continue;
} }
if (lchr[(int)land.lnd_type].l_flags & L_HEAVY) { if (!land_loadable(&land, noisy))
if (noisy)
pr("%s is too heavy to load.\n", prland(&land));
continue; continue;
}
} }
/* Unit sanity done */ /* Unit sanity done */

View file

@ -1513,7 +1513,7 @@
Play#1 command lload Play#1 command lload
Play#1 output Play#1 1 sam Sea Sparrow #231 loaded onto rad radar unit #121 at -2,0. Play#1 output Play#1 1 sam Sea Sparrow #231 loaded onto rad radar unit #121 at -2,0.
Play#1 output Play#1 1 Can't put plane 233 on this unit! Play#1 output Play#1 1 Can't put plane 233 on this unit!
Play#1 output Play#1 1 sam Sea Sparrow #231 is already on unit #121! Play#1 output Play#1 1 sam Sea Sparrow #231 is already on land unit #121!
Play#1 output Play#1 1 sam Sea Sparrow #233 loaded onto rad radar unit #123 at -2,0. Play#1 output Play#1 1 sam Sea Sparrow #233 loaded onto rad radar unit #123 at -2,0.
Play#1 output Play#1 1 2 units loaded Play#1 output Play#1 1 2 units loaded
Play#1 output Play#1 6 0 483 Play#1 output Play#1 6 0 483
@ -1560,7 +1560,7 @@
Play#1 input lload plane 100 57/58 Play#1 input lload plane 100 57/58
Play#1 command lload Play#1 command lload
Play#1 output Play#1 1 sam Sea Sparrow #57 is already on ship #105! Play#1 output Play#1 1 sam Sea Sparrow #57 is already on ship #105!
Play#1 output Play#1 1 sam Sea Sparrow #58 is already on unit #105! Play#1 output Play#1 1 sam Sea Sparrow #58 is already on land unit #105!
Play#1 output Play#1 1 No units affected Play#1 output Play#1 1 No units affected
Play#1 output Play#1 6 0 474 Play#1 output Play#1 6 0 474
Play#1 input lload plane 100 59 Play#1 input lload plane 100 59