From 31256510ccf936ddea20d4d0fb6c2ccd95b1d58e Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Sun, 3 Sep 2017 17:58:42 +0200 Subject: [PATCH] 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 --- src/lib/commands/load.c | 119 ++++++++++++++++-------------------- tests/load-tend/journal.log | 4 +- 2 files changed, 54 insertions(+), 69 deletions(-) diff --git a/src/lib/commands/load.c b/src/lib/commands/load.c index 9095d3279..ba1fa8416 100644 --- a/src/lib/commands/load.c +++ b/src/lib/commands/load.c @@ -365,6 +365,54 @@ still_ok_land(struct sctstr *sectp, struct lndstr *landp) 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 load_plane_ship(struct sctstr *sectp, struct shpstr *sp, int noisy, 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"); continue; } - if (loading && pln.pln_ship > -1) { - if (noisy) - pr("%s is already on ship #%d!\n", - prplane(&pln), pln.pln_ship); + if (loading && !plane_loadable(&pln, noisy)) 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 (noisy) 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; 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); + if (!land_loadable(&land, noisy)) 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 (noisy) 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"); continue; } - - if (loading && pln.pln_ship > -1) { - if (noisy) - pr("%s is already on ship #%d!\n", - prplane(&pln), pln.pln_ship); - continue; - } - if (loading && pln.pln_land > -1) { - if (noisy) - pr("%s is already on unit #%d!\n", - prplane(&pln), pln.pln_land); + if (loading && !plane_loadable(&pln, noisy)) continue; - } if (pln.pln_harden != 0) { if (noisy) 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; 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 (noisy) pr("%s can't be loaded onto itself!\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)); + if (!land_loadable(&land, noisy)) continue; - } } /* Unit sanity done */ diff --git a/tests/load-tend/journal.log b/tests/load-tend/journal.log index 6ad2ee830..a50a23dd0 100644 --- a/tests/load-tend/journal.log +++ b/tests/load-tend/journal.log @@ -1513,7 +1513,7 @@ 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 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 2 units loaded Play#1 output Play#1 6 0 483 @@ -1560,7 +1560,7 @@ Play#1 input lload plane 100 57/58 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 #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 6 0 474 Play#1 input lload plane 100 59 -- 2.43.0