]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/landgun.c
Update known contributors comments
[empserver] / src / lib / subs / landgun.c
index 6bc37cc4fed5b3c0d6d316f1a5773f0e6718f9ca..cd4298cdb44a760e0e8f10f9953282a99105b482 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>
 
+#include "damage.h"
 #include "file.h"
+#include "land.h"
 #include "nat.h"
 #include "optlist.h"
 #include "prototypes.h"
@@ -41,7 +43,7 @@
 #include "ship.h"
 
 double
-landgun(int effic, int guns)
+fortgun(int effic, int guns)
 {
     double d;
     double g = MIN(guns, 7);
@@ -64,37 +66,159 @@ seagun(int effic, int guns)
 }
 
 double
-landunitgun(int effic, int shots, int guns, int ammo, int shells)
+landunitgun(int effic, int guns)
 {
-    double d = 0.0;
+    double d;
 
-    shots = MIN(shots, guns);
-    while (shots-- > 0)
+    d = 0.0;
+    while (guns--)
        d += 5.0 + random() % 6;
     d *= effic * 0.01;
-    if (shells < ammo && ammo != 0)
+    return d;
+}
+
+/*
+ * Fire from fortress SP.
+ * Use ammo, resupply if necessary.
+ * Return damage if the fortress fires, else -1.
+ */
+int
+fort_fire(struct sctstr *sp)
+{
+    int guns = sp->sct_item[I_GUN];
+
+    if (sp->sct_type != SCT_FORTR || sp->sct_effic < FORTEFF)
+       return -1;
+    if (sp->sct_item[I_MILIT] < 5 || guns == 0)
+       return -1;
+    if (!sct_supply(sp, I_SHELL, 1))
+       return -1;
+    sp->sct_item[I_SHELL]--;
+    return (int)fortgun(sp->sct_effic, guns);
+}
+
+/*
+ * 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;
+
+    if (sp->shp_effic < 60)
+       return -1;
+    guns = shp_usable_guns(sp);
+    guns = MIN(guns, (sp->shp_item[I_MILIT] + 1) / 2);
+    if (guns == 0)
+       return -1;
+    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] -= (guns + 1) / 2;
+    return (int)seagun(sp->shp_effic, guns);
+}
+
+/*
+ * 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 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;
+    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.
+ * Return damage if the ship fires, else -1.
+ */
+int
+shp_torp(struct shpstr *sp, int usemob)
+{
+    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;
+    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.
+ * Use ammo, resupply if necessary.
+ * Return damage if the land unit fires, else -1.
+ */
+int
+lnd_fire(struct lndstr *lp)
+{
+    int guns, ammo, shells;
+    double d;
+
+    if (lp->lnd_effic < LAND_MINFIREEFF)
+       return -1;
+    if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
+       return -1;
+    if (lp->lnd_item[I_MILIT] == 0)
+       return -1;
+    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];
+    if (shells == 0)
+       return -1;
+    d = landunitgun(lp->lnd_effic, guns);
+    if (shells < ammo) {
        d *= (double)shells / (double)ammo;
+       ammo = shells;
+    }
+    lp->lnd_item[I_SHELL] -= ammo;
     return d;
 }
 
 /*
- * Return effective firing range for range factor RNG at tech TLEV.
+ * Return number of guns ship SP can fire.
  */
-double
-effrange(int rng, double tlev)
+int
+shp_usable_guns(struct shpstr *sp)
 {
-    /* FIXME don't truncate TLEV */
-    return techfact((int)tlev, rng / 2.0);
+    return MIN(shp_glim(sp), sp->shp_item[I_GUN]);
 }
 
 /*
- * Return torpedo range for ship SP.
+ * Return effective firing range for range factor RNG at tech TLEV.
  */
-double
-torprange(struct shpstr *sp)
+static double
+effrange(int rng, double tlev)
 {
-    return effrange(sp->shp_frnge * 2, sp->shp_tech)
-       * sp->shp_effic / 100.0;
+    /* FIXME don't truncate TLEV */
+    return techfact((int)tlev, rng / 2.0);
 }
 
 /*
@@ -115,6 +239,43 @@ fortrange(struct sctstr *sp)
     return rng;
 }
 
+/*
+ * Return firing range for ship SP.
+ */
+double
+shp_fire_range(struct shpstr *sp)
+{
+    return effrange(shp_frnge(sp), sp->shp_tech);
+}
+
+/*
+ * Return torpedo range for ship SP.
+ */
+double
+torprange(struct shpstr *sp)
+{
+    return effrange(shp_frnge(sp) * 2, sp->shp_tech)
+       * sp->shp_effic / 100.0;
+}
+
+/*
+ * Return hit chance for torpedo from ship SP at range RANGE.
+ */
+double
+shp_torp_hitchance(struct shpstr *sp, int range)
+{
+    return DTORP_HITCHANCE(range, shp_visib(sp));
+}
+
+/*
+ * Return firing range for land unit SP.
+ */
+double
+lnd_fire_range(struct lndstr *lp)
+{
+    return effrange(lnd_frg(lp), lp->lnd_tech);
+}
+
 int
 roundrange(double r)
 {