]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/landgun.c
Update copyright notice
[empserver] / src / lib / subs / landgun.c
index 00c71438e8813875da19989e6de62c2c57b9d0d5..8e0c3f1580848ba26ebdb754083744f2a98ab3a8 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
  *
  *  ---
  *
- *  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-2013
  */
 
 #include <config.h>
 
+#include "chance.h"
 #include "damage.h"
 #include "file.h"
 #include "land.h"
 #include "sect.h"
 #include "ship.h"
 
-double
+static double
 fortgun(int effic, int guns)
 {
     double d;
     double g = MIN(guns, 7);
 
-    d = (random() % 30 + 20.0) * (g / 7.0);
+    d = (roll(30) + 19.0) * (g / 7.0);
     d *= effic / 100.0;
     return d;
 }
 
-double
+static double
 seagun(int effic, int guns)
 {
     double d;
 
     d = 0.0;
     while (guns--)
-       d += 10.0 + random() % 6;
+       d += 9.0 + roll(6);
     d *= effic * 0.01;
     return d;
 }
 
-double
+static double
 landunitgun(int effic, int guns)
 {
     double d;
 
     d = 0.0;
     while (guns--)
-       d += 5.0 + random() % 6;
+       d += 4.0 + roll(6);
     d *= effic * 0.01;
     return d;
 }
 
 /*
- * Fire from fortress SP.
+ * Fire from fortress @sp.
  * Use ammo, resupply if necessary.
  * Return damage if the fortress fires, else -1.
  */
@@ -86,30 +86,26 @@ 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);
 }
 
 /*
- * Fire from ship SP.
+ * Fire from ship @sp.
  * Use ammo, resupply if necessary.
  * Return damage if the ship fires, else -1.
  */
 int
 shp_fire(struct shpstr *sp)
 {
-    int guns, shells;
+    int guns;
 
     if (sp->shp_effic < 60)
        return -1;
@@ -117,80 +113,69 @@ 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);
 }
 
 /*
- * Drop depth-charges from ship SP.
+ * Drop depth-charges from ship @sp.
  * Use ammo, resupply if necessary.
  * Return damage if the ship drops depth-charges, else -1.
  */
 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)
+    if (sp->shp_item[I_MILIT] == 0 || sp->shp_item[I_GUN] == 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);
 }
 
 /*
- * Fire torpedo from ship SP.
- * Use ammo and mobility, resupply if necessary.
+ * Fire torpedo from ship @sp.
+ * Use ammo, resupply if necessary.
+ * Use mobility if @usemob is non-zero.
  * Return damage if the ship fires, else -1.
  */
 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();
 }
 
 /*
- * Fire from land unit LP.
+ * Fire from land unit @lp.
  * Use ammo, resupply if necessary.
  * Return damage if the land unit fires, else -1.
  */
 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;
@@ -202,9 +187,11 @@ lnd_fire(struct lndstr *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,12 +199,37 @@ 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;
 }
 
 /*
- * Return number of guns ship SP can fire.
+ * Sabotage with land unit @lp, target has @item[] commodities.
+ * 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, roll0(item[I_SHELL] / 10));
+    if (item[I_PETROL] > 100)
+       dam += seagun(lp->lnd_effic, roll0(item[I_PETROL] / 50));
+    return dam;
+}
+
+/*
+ * Return number of guns ship @sp can fire.
  */
 int
 shp_usable_guns(struct shpstr *sp)
@@ -226,7 +238,7 @@ shp_usable_guns(struct shpstr *sp)
 }
 
 /*
- * Return effective firing range for range factor RNG at tech TLEV.
+ * Return effective firing range for range factor @rng at tech @tlev.
  */
 static double
 effrange(int rng, double tlev)
@@ -236,7 +248,7 @@ effrange(int rng, double tlev)
 }
 
 /*
- * Return firing range for sector SP.
+ * Return firing range for sector @sp.
  */
 double
 fortrange(struct sctstr *sp)
@@ -248,13 +260,13 @@ 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;
 }
 
 /*
- * Return firing range for ship SP.
+ * Return firing range for ship @sp.
  */
 double
 shp_fire_range(struct shpstr *sp)
@@ -263,7 +275,7 @@ shp_fire_range(struct shpstr *sp)
 }
 
 /*
- * Return torpedo range for ship SP.
+ * Return torpedo range for ship @sp.
  */
 double
 torprange(struct shpstr *sp)
@@ -273,7 +285,7 @@ torprange(struct shpstr *sp)
 }
 
 /*
- * Return hit chance for torpedo from ship SP at range RANGE.
+ * Return hit chance for torpedo from ship @sp at range @range.
  */
 double
 shp_torp_hitchance(struct shpstr *sp, int range)
@@ -282,7 +294,7 @@ shp_torp_hitchance(struct shpstr *sp, int range)
 }
 
 /*
- * Return firing range for land unit SP.
+ * Return firing range for land unit @lp.
  */
 double
 lnd_fire_range(struct lndstr *lp)