]> git.pond.sub.org Git - empserver/commitdiff
load: Factor out plane_loadable(), land_loadable()
authorMarkus Armbruster <armbru@pond.sub.org>
Sun, 3 Sep 2017 15:58:42 +0000 (17:58 +0200)
committerMarkus Armbruster <armbru@pond.sub.org>
Tue, 5 Jan 2021 06:25:18 +0000 (07:25 +0100)
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>
src/lib/commands/load.c
tests/load-tend/journal.log

index 9095d3279cce842e438ad37dd0e370cb78666356..ba1fa841652db8ac31c953a9c8442ed19694e2e7 100644 (file)
@@ -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 */
index 6ad2ee83076fc3a94dae8d13d0edc54d80e0f589..a50a23dd0e66526dbc015023a3a54e7791acde82 100644 (file)
     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
     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