]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/load.c
Update copyright notice
[empserver] / src / lib / commands / load.c
index 912582e5ec213b1459d026abb70ca158a8c23de6..0cae81bc70a29eaf46c195c717e97119b7980ce6 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2018, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
@@ -113,8 +113,6 @@ load(void)
 
        if (!getsect(ship.shp_x, ship.shp_y, &sect))    /* XXX */
            continue;
-       if (!sect.sct_own)
-           continue;
        if (!player->owner) {
            if (ship.shp_own != player->cnum)
                continue;
@@ -151,7 +149,8 @@ load(void)
 
        if (opt_MARKET) {
            if (ontradingblock(EF_SHIP, &ship)) {
-               pr("You cannot load/unload an item on the trading block!\n");
+               if (noisy)
+                   pr("%s is on the trading block\n", prship(&ship));
                continue;
            }
        }
@@ -252,15 +251,17 @@ lload(void)
                continue;
            }
            if (relations_with(sect.sct_own, player->cnum) != ALLIED) {
-               pr("Sector %s is not yours.\n",
-                  xyas(sect.sct_x, sect.sct_y, player->cnum));
+               if (noisy)
+                   pr("Sector %s is not yours.\n",
+                      xyas(sect.sct_x, sect.sct_y, player->cnum));
                continue;
            }
        }
 
        if (opt_MARKET) {
            if (ontradingblock(EF_LAND, &land)) {
-               pr("You cannot load/unload an item on the trading block!\n");
+               if (noisy)
+                   pr("%s is on the trading block\n", prland(&land));
                continue;
            }
        }
@@ -364,6 +365,65 @@ 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;
+    }
+    if (pp->pln_harden) {
+       if (noisy)
+           pr("%s has been hardened and can't be loaded\n",
+              prplane(pp));
+       return 0;
+    }
+    if (pln_is_in_orbit(pp)) {
+       if (noisy)
+           pr("%s is in space\n", prplane(pp));
+       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)
@@ -384,8 +444,7 @@ load_plane_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
     if (loading &&
        shp_nplane(sp, NULL, NULL, NULL)
                >= mcp->m_nchoppers + mcp->m_nxlight + mcp->m_nplanes) {
-       if (noisy)
-           pr("%s doesn't have room for any more planes\n", prship(sp));
+       pr("%s doesn't have room for any more planes\n", prship(sp));
        return 0;
     }
     sprintf(prompt, "Plane(s) to %s %s? ",
@@ -399,8 +458,7 @@ load_plane_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
     if (!still_ok_ship(sectp, sp))
        return RET_SYN;
 
-    if (noisy)
-       noisy = ni.sel == NS_LIST;
+    noisy = ni.sel == NS_LIST;
 
     while (nxtitem(&ni, &pln)) {
        if (!player->owner)
@@ -414,24 +472,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);
-           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",
-                  prplane(&pln));
+       if (loading && !plane_loadable(&pln, noisy))
            continue;
-       }
 
        if (!loading) {
            if (pln.pln_ship != sp->shp_uid)
@@ -456,8 +498,7 @@ load_plane_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
        /* Fit plane on ship */
        if (loading) {
            if (!put_plane_on_ship(&pln, sp)) {
-               if (noisy)
-                   pr("Can't put plane %d on this ship!\n", pln.pln_uid);
+               pr("Can't put plane %d on this ship!\n", pln.pln_uid);
                continue;
            }
            sprintf(buf, "loaded on your %s at %s",
@@ -511,10 +552,8 @@ load_land_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
            load_spy = 1;
        }
        if (!load_spy && shp_nland(sp) >= mchr[sp->shp_type].m_nland) {
-           if (noisy) {
-               pr("%s doesn't have room for any more land units!\n",
-                  prship(sp));
-           }
+           pr("%s doesn't have room for any more land units!\n",
+              prship(sp));
            return 0;
        }
     }
@@ -529,37 +568,15 @@ load_land_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
     if (!still_ok_ship(sectp, sp))
        return RET_SYN;
 
-    if (noisy)
-       noisy = ni.sel == NS_LIST;
+    noisy = ni.sel == NS_LIST;
 
     while (nxtitem(&ni, &land)) {
        if (!player->owner)
            continue;
 
        if (loading) {
-           if (land.lnd_ship > -1) {
-               if (noisy)
-                   pr("%s is already on ship #%d!\n",
-                      prland(&land), land.lnd_ship);
+           if (!land_loadable(&land, noisy))
                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 (noisy)
                    pr("Subs can only carry spy units.\n");
@@ -589,21 +606,17 @@ load_land_ship(struct sctstr *sectp, struct shpstr *sp, int noisy,
        }
        /* Fit unit on ship */
        if (loading) {
-           /* We have to check again, since it may have changed */
-           if ((mchr[(int)sp->shp_type].m_flags & M_SUB) &&
-               (mchr[(int)sp->shp_type].m_nland == 0)) {
+           if (load_spy) {
                if (shp_nland(sp) >= 2) {
                    pr("Non-land unit carrying subs can only carry up to two spy units.\n");
                    return 0;
                }
-               /* Eh, let 'em load a spy only */
-               load_spy = 1;
-           }
-           if (!load_spy && shp_nland(sp) >= mchr[sp->shp_type].m_nland) {
-               if (noisy)
+           } else {
+               if (shp_nland(sp) >= mchr[sp->shp_type].m_nland) {
                    pr("%s doesn't have room for any more land units!\n",
                       prship(sp));
-               return 0;
+                   return 0;
+               }
            }
            sprintf(buf, "loaded on your %s at %s",
                    prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
@@ -720,9 +733,8 @@ load_plane_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
        return 0;
     }
     if (loading && lnd_nxlight(lp) >= lcp->l_nxlight) {
-       if (noisy)
-           pr("%s doesn't have room for any more extra-light planes\n",
-              prland(lp));
+       pr("%s doesn't have room for any more extra-light planes\n",
+          prland(lp));
        return 0;
     }
     sprintf(prompt, "Plane(s) to %s %s? ",
@@ -736,8 +748,7 @@ load_plane_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
     if (!still_ok_land(sectp, lp))
        return RET_SYN;
 
-    if (noisy)
-       noisy = ni.sel == NS_LIST;
+    noisy = ni.sel == NS_LIST;
 
     while (nxtitem(&ni, &pln)) {
        if (!player->owner)
@@ -748,25 +759,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",
-                  prplane(&pln));
-           continue;
-       }
 
        /* Plane sanity done */
        /* Find the right unit */
@@ -779,8 +773,7 @@ load_plane_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
        /* Fit plane on unit */
        if (loading) {
            if (!put_plane_on_land(&pln, lp)) {
-               if (noisy)
-                   pr("Can't put plane %d on this unit!\n", pln.pln_uid);
+               pr("Can't put plane %d on this unit!\n", pln.pln_uid);
                continue;
            }
            sprintf(buf, "loaded on %s at %s",
@@ -879,9 +872,8 @@ load_land_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
        return 0;
     }
     if (loading && lnd_nland(lp) >= lchr[lp->lnd_type].l_nland) {
-       if (noisy)
-           pr("%s doesn't have room for any more land units!\n",
-              prland(lp));
+       pr("%s doesn't have room for any more land units!\n",
+          prland(lp));
        return 0;
     }
     sprintf(prompt, "Land unit(s) to %s %s? ",
@@ -895,42 +887,20 @@ load_land_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
     if (!still_ok_land(sectp, lp))
        return RET_SYN;
 
-    if (noisy)
-       noisy = ni.sel == NS_LIST;
+    noisy = ni.sel == NS_LIST;
 
     while (nxtitem(&ni, &land)) {
        if (!player->owner)
            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 | L_TRAIN)) {
-               if (noisy)
-                   pr("%s is too heavy to load.\n", prland(&land));
+           if (!land_loadable(&land, noisy))
                continue;
-           }
        }
 
        /* Unit sanity done */
@@ -946,9 +916,8 @@ load_land_land(struct sctstr *sectp, struct lndstr *lp, int noisy,
        /* Fit unit on ship */
        if (loading) {
            if (lnd_nland(lp) >= lchr[lp->lnd_type].l_nland) {
-               if (noisy)
-                   pr("%s doesn't have room for any more land units!\n",
-                      prland(lp));
+               pr("%s doesn't have room for any more land units!\n",
+                  prland(lp));
                break;
            }
            sprintf(buf, "loaded on your %s at %s",