]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/landgun.c
Streamline fortgun()'s efficiency comparison
[empserver] / src / lib / subs / landgun.c
index 737054f6c134f365280eb603b89e772e55188a63..bdb32f65f6f97f079c4748fd48b22dec8a813a1a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
  *
  *  ---
  *
- *  landgun.c: Return values for land and ship gun firing damages
- * 
+ *  landgun.c: Fire weapons
+ *
  *  Known contributors to this file:
- *     Markus Armbruster, 2006
+ *     Markus Armbruster, 2006-2009
  */
 
 #include <config.h>
@@ -42,7 +42,7 @@
 #include "sect.h"
 #include "ship.h"
 
-double
+static double
 fortgun(int effic, int guns)
 {
     double d;
@@ -53,7 +53,7 @@ fortgun(int effic, int guns)
     return d;
 }
 
-double
+static double
 seagun(int effic, int guns)
 {
     double d;
@@ -65,7 +65,7 @@ seagun(int effic, int guns)
     return d;
 }
 
-double
+static double
 landunitgun(int effic, int guns)
 {
     double d;
@@ -86,18 +86,14 @@ int
 fort_fire(struct sctstr *sp)
 {
     int guns = sp->sct_item[I_GUN];
-    int shells;
 
     if (sp->sct_type != SCT_FORTR || sp->sct_effic < FORTEFF)
        return -1;
     if (sp->sct_item[I_MILIT] < 5 || guns == 0)
        return -1;
-    shells = sp->sct_item[I_SHELL];
-    shells += supply_commod(sp->sct_own, sp->sct_x, sp->sct_y,
-                           I_SHELL, 1 - shells);
-    if (shells == 0)
+    if (!sct_supply(sp, I_SHELL, 1))
        return -1;
-    sp->sct_item[I_SHELL] = shells - 1;
+    sp->sct_item[I_SHELL]--;
     return (int)fortgun(sp->sct_effic, guns);
 }
 
@@ -109,7 +105,7 @@ fort_fire(struct sctstr *sp)
 int
 shp_fire(struct shpstr *sp)
 {
-    int guns, shells;
+    int guns;
 
     if (sp->shp_effic < 60)
        return -1;
@@ -117,13 +113,11 @@ shp_fire(struct shpstr *sp)
     guns = MIN(guns, (sp->shp_item[I_MILIT] + 1) / 2);
     if (guns == 0)
        return -1;
-    shells = sp->shp_item[I_SHELL];
-    shells += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
-                           I_SHELL, (guns + 1) / 2 - shells);
-    guns = MIN(guns, shells * 2);
+    shp_supply(sp, I_SHELL, (guns + 1) / 2);
+    guns = MIN(guns, sp->shp_item[I_SHELL] * 2);
     if (guns == 0)
        return -1;
-    sp->shp_item[I_SHELL] = shells - (guns + 1) / 2;
+    sp->shp_item[I_SHELL] -= (guns + 1) / 2;
     return (int)seagun(sp->shp_effic, guns);
 }
 
@@ -135,19 +129,18 @@ shp_fire(struct shpstr *sp)
 int
 shp_dchrg(struct shpstr *sp)
 {
-    int shells;
+    int dchrgs;
 
     if (sp->shp_effic < 60 || (mchr[sp->shp_type].m_flags & M_DCH) == 0)
        return -1;
     if (sp->shp_item[I_MILIT] == 0)
        return -1;
-    shells = sp->shp_item[I_SHELL];
-    shells += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
-                           I_SHELL, 2 - shells);
-    if (shells < 2)
-       return -1;
-    sp->shp_item[I_SHELL] = shells - 2;
-    return (int)seagun(sp->shp_effic, 3);
+    shp_supply(sp, I_SHELL, 2);
+    dchrgs = MIN(2, sp->shp_item[I_SHELL]);
+    if (dchrgs == 0)
+       return -1;
+    sp->shp_item[I_SHELL] -= dchrgs;
+    return (int)seagun(sp->shp_effic, 2 * dchrgs - 1);
 }
 
 /*
@@ -158,20 +151,15 @@ shp_dchrg(struct shpstr *sp)
 int
 shp_torp(struct shpstr *sp, int usemob)
 {
-    int shells;
-
     if (sp->shp_effic < 60 || (mchr[sp->shp_type].m_flags & M_TORP) == 0)
        return -1;
     if (sp->shp_item[I_MILIT] == 0 || sp->shp_item[I_GUN] == 0)
        return -1;
     if (usemob && sp->shp_mobil <= 0)
        return -1;
-    shells = sp->shp_item[I_SHELL];
-    shells += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
-                           I_SHELL, SHP_TORP_SHELLS - shells);
-    if (shells < SHP_TORP_SHELLS)
-       return -1;
-    sp->shp_item[I_SHELL] = shells - SHP_TORP_SHELLS;
+    if (!shp_supply(sp, I_SHELL, SHP_TORP_SHELLS))
+       return -1;
+    sp->shp_item[I_SHELL] -= SHP_TORP_SHELLS;
     if (usemob)
        sp->shp_mobil -= (int)shp_mobcost(sp) / 2.0;
     return TORP_DAMAGE();
@@ -185,12 +173,8 @@ shp_torp(struct shpstr *sp, int usemob)
 int
 lnd_fire(struct lndstr *lp)
 {
-    int guns, shells;
+    int guns, ammo, shells;
     double d;
-    int ammo = lchr[lp->lnd_type].l_ammo;
-
-    if (CANT_HAPPEN(ammo == 0))
-       ammo = 1;
 
     if (lp->lnd_effic < LAND_MINFIREEFF)
        return -1;
@@ -198,13 +182,15 @@ lnd_fire(struct lndstr *lp)
        return -1;
     if (lp->lnd_item[I_MILIT] == 0)
        return -1;
-    guns = lp->lnd_dam;
+    guns = lnd_dam(lp);
     guns = MIN(guns, lp->lnd_item[I_GUN]);
     if (guns == 0)
        return -1;
+    ammo = lchr[lp->lnd_type].l_ammo;
+    if (CANT_HAPPEN(ammo == 0))
+       ammo = 1;
+    lnd_supply(lp, I_SHELL, ammo);
     shells = lp->lnd_item[I_SHELL];
-    shells += supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
-                           I_SHELL, ammo - shells);
     if (shells == 0)
        return -1;
     d = landunitgun(lp->lnd_effic, guns);
@@ -212,10 +198,35 @@ lnd_fire(struct lndstr *lp)
        d *= (double)shells / (double)ammo;
        ammo = shells;
     }
-    lp->lnd_item[I_SHELL] = shells - ammo;
+    lp->lnd_item[I_SHELL] -= ammo;
     return d;
 }
 
+/*
+ * Sabotage with land unit LP.
+ * Use ammo.
+ * Return damage if the land unit sabotages, else -1.
+ */
+int
+lnd_sabo(struct lndstr *lp, short item[])
+{
+    int dam;
+
+    if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
+       return -1;
+    if (!(lchr[lp->lnd_type].l_flags & L_SPY))
+       return -1;
+    if (!lp->lnd_item[I_SHELL])
+       return -1;
+    lp->lnd_item[I_SHELL]--;
+    dam = fortgun(3 * lp->lnd_effic, 7);
+    if (item[I_SHELL] > 20)
+       dam += seagun(lp->lnd_effic, random() % (item[I_SHELL] / 10));
+    if (item[I_PETROL] > 100)
+       dam += seagun(lp->lnd_effic, random() % (item[I_PETROL] / 50));
+    return dam;
+}
+
 /*
  * Return number of guns ship SP can fire.
  */
@@ -248,7 +259,7 @@ fortrange(struct sctstr *sp)
        return -1.0;
 
     rng = effrange(14.0 * fire_range_factor, np->nat_level[NAT_TLEV]);
-    if (sp->sct_effic > 59)
+    if (sp->sct_effic >= 60)
        rng++;
     return rng;
 }
@@ -287,7 +298,7 @@ shp_torp_hitchance(struct shpstr *sp, int range)
 double
 lnd_fire_range(struct lndstr *lp)
 {
-    return effrange(lp->lnd_frg, lp->lnd_tech);
+    return effrange(lnd_frg(lp), lp->lnd_tech);
 }
 
 int