]> git.pond.sub.org Git - empserver/commitdiff
Factor ship_can_carry() out of could_be_on_ship()
authorMarkus Armbruster <armbru@pond.sub.org>
Sat, 23 Jun 2012 14:20:20 +0000 (16:20 +0200)
committerMarkus Armbruster <armbru@pond.sub.org>
Sun, 24 Jun 2012 06:49:57 +0000 (08:49 +0200)
src/lib/subs/plnsub.c

index 9cf9c4718df4c677d2f0bcf6ea7a1af9893c5f25..400dc8a23b63979eb2e67ac1c82842196ab49507 100644 (file)
@@ -30,7 +30,7 @@
  *     Dave Pare, 1986
  *     Ken Stevens, 1995
  *     Steve McClure, 1998-2000
- *     Markus Armbruster, 2004-2010
+ *     Markus Armbruster, 2004-2012
  */
 
 #include <config.h>
@@ -813,6 +813,29 @@ pln_put1(struct plist *plp)
     free(plp);
 }
 
+/*
+ * Can a carrier of SP's type carry this load of planes?
+ * The load consists of N planes, of which NCH are choppers, NXL
+ * xlight, NMSL light missiles, and the rest are light fixed-wing
+ * planes.
+ */
+static int
+ship_can_carry(struct shpstr *sp, int n, int nch, int nxl, int nmsl)
+{
+    struct mchrstr *mcp = &mchr[sp->shp_type];
+    int nfw = n - nch - nxl - nmsl;
+
+    if (nch > mcp->m_nchoppers) /* overflow into fixed-wing slots */
+       nfw += nch - mcp->m_nchoppers;
+    if (nxl > mcp->m_nxlight)  /* overflow into missile slots */
+       nmsl += nxl - mcp->m_nxlight;
+    if (nmsl && !(mcp->m_flags & (M_MSL | M_FLY)))
+       return 0;               /* missile slots wanted */
+    if (nfw && !(mcp->m_flags & M_FLY))
+       return 0;               /* fixed-wing slots wanted */
+    return nfw + nmsl <= mcp->m_nplanes;
+}
+
 /*
  * Can PP be loaded on a ship of SP's type?
  * Assume that it already carries N planes, of which NCH are choppers,
@@ -824,8 +847,6 @@ could_be_on_ship(struct plnstr *pp, struct shpstr *sp,
                 int n, int nch, int nxl, int nmsl)
 {
     struct plchrstr *pcp = &plchr[pp->pln_type];
-    struct mchrstr *mcp = &mchr[sp->shp_type];
-    int nfw;
 
     if (pcp->pl_flags & P_K)
        nch++;
@@ -835,18 +856,8 @@ could_be_on_ship(struct plnstr *pp, struct shpstr *sp,
        return 0;
     else if (pcp->pl_flags & P_M)
        nmsl++;
-    n++;
-    nfw = n - nch - nxl - nmsl;
 
-    if (nch > mcp->m_nchoppers) /* overflow into fixed-wing slots */
-       nfw += nch - mcp->m_nchoppers;
-    if (nxl > mcp->m_nxlight)  /* overflow into missile slots */
-       nmsl += nxl - mcp->m_nxlight;
-    if (nmsl && !(mcp->m_flags & (M_MSL | M_FLY)))
-       return 0;               /* missile slots wanted */
-    if (nfw && !(mcp->m_flags & M_FLY))
-       return 0;               /* fixed-wing slots wanted */
-    return nfw + nmsl <= mcp->m_nplanes;
+    return ship_can_carry(sp, n + 1, nch, nxl, nmsl);
 }
 
 /*