]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/fortdef.c
Update copyright notice
[empserver] / src / lib / subs / fortdef.c
index 4b77e239f9e48e17c7d3ef8236d2b1232d0e1e40..752d2290b0b25f27f2e054092bbec3a6a551bcec 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2018, 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/>.
  *
  *  ---
  *
@@ -26,9 +25,9 @@
  *  ---
  *
  *  fortdef.c: Fort defends an area.
- * 
+ *
  *  Known contributors to this file:
- *    
+ *     Markus Armbruster, 2006-2011
  */
 
 /*
@@ -39,7 +38,6 @@
 
 #include <config.h>
 
-#include "file.h"
 #include "nat.h"
 #include "news.h"
 #include "optlist.h"
@@ -47,7 +45,7 @@
 #include "sect.h"
 #include "ship.h"
 
-#define        NOISY   1
+#define NOISY  1
 
 static int sb(natid, natid, struct sctstr *, coord, coord, int, int);
 
@@ -67,13 +65,11 @@ int
 sd(natid att, natid own, coord x, coord y, int noisy, int defending,
    int usesubs)
 {
-    int nshot;
     int range;
     double eff;
     struct shpstr ship;
     struct nstr_item ni;
-    int shell;
-    int dam, rel, rel2;
+    int dam;
 
     if (own == 0)
        return 0;
@@ -82,43 +78,26 @@ sd(natid att, natid own, coord x, coord y, int noisy, int defending,
     eff = 1.0;
     snxtitem_dist(&ni, EF_SHIP, x, y, 8);
     while (nxtitem(&ni, &ship) && eff > 0.30) {
-       if (ship.shp_own == att)
-           continue;
-       if (ship.shp_own == 0)
+       if (!feels_like_helping(ship.shp_own, own, att))
            continue;
 
-       rel = getrel(getnatp(ship.shp_own), own);
-       rel2 = getrel(getnatp(ship.shp_own), att);
-       if ((ship.shp_own != own) && ((rel != ALLIED) || (rel2 != AT_WAR)))
-           continue;
-       if (ship.shp_effic < 60)
-           continue;
        if ((mchr[(int)ship.shp_type].m_flags & M_SUB) && !usesubs)
            continue;
-       range = roundrange(effrange(ship.shp_frnge, ship.shp_tech));
+       range = roundrange(shp_fire_range(&ship));
        if (range < ni.curdist)
            continue;
-       /* must have gun, shell, and milit to fire */
-       shell = ship.shp_item[I_SHELL];
-       if (shell < ship.shp_glim)
-           shell += supply_commod(ship.shp_own, ship.shp_x, ship.shp_y,
-                                  I_SHELL, shell - ship.shp_glim);
-       nshot = MIN(MIN(ship.shp_item[I_GUN], shell), ship.shp_item[I_MILIT]);
-       nshot = MIN(nshot, ship.shp_glim);
-       if (nshot <= 0)
-           continue;
-       ship.shp_item[I_SHELL] = shell - nshot;
+       dam = shp_fire(&ship);
        putship(ship.shp_uid, &ship);
+       if (dam < 0)
+           continue;
        if (defending)
            nreport(ship.shp_own, N_FIRE_BACK, att, 1);
        else
            nreport(ship.shp_own, N_FIRE_S_ATTACK, att, 1);
-       dam = seagun(ship.shp_effic, nshot);
        eff *= (1.0 - (0.01 * dam));
        if (noisy) {
            pr_beep();
-           pr("Incoming shell%s %d damage!\n",
-              nshot == 1 ? " does" : "s do", dam);
+           pr("Incoming shells do %d damage!\n", dam);
        }
        if (noisy || (ship.shp_own != own)) {
            if (ship.shp_own == own)
@@ -144,7 +123,7 @@ sd(natid att, natid own, coord x, coord y, int noisy, int defending,
 int
 dd(natid att, natid def_own, coord ax, coord ay, int noisy, int defending)
 {
-    int dam, rel, rel2;
+    int dam;
     struct sctstr firing;
     struct nstr_sect ns;
 
@@ -157,13 +136,7 @@ dd(natid att, natid def_own, coord ax, coord ay, int noisy, int defending)
     dam = 0;
     snxtsct_dist(&ns, ax, ay, 8);
     while (nxtsct(&ns, &firing) && dam < 80) {
-       if (firing.sct_own == att)
-           continue;
-       if (firing.sct_own == 0)
-           continue;
-       rel = getrel(getnatp(firing.sct_own), def_own);
-       rel2 = getrel(getnatp(firing.sct_own), att);
-       if (firing.sct_own != def_own && (rel != ALLIED || rel2 != AT_WAR))
+       if (!feels_like_helping(firing.sct_own, def_own, att))
            continue;
        /* XXX defdef damage is additive, but ship or land unit damage isn't */
        dam += sb(att, def_own, &firing, ax, ay, noisy, defending);
@@ -171,6 +144,23 @@ dd(natid att, natid def_own, coord ax, coord ay, int noisy, int defending)
     return dam;
 }
 
+/*
+ * Shall @cn attempt to help @friend against @foe?
+ */
+int
+feels_like_helping(natid cn, natid friend, natid foe)
+{
+    if (cn == 0)
+       return 0;               /* never helps anybody */
+    if (cn == foe)
+       return 0;               /* don't help anybody against self */
+    if (cn == friend)
+       return 1;               /* help self against anybody else */
+    /* third party helps ally if at war with foe: */
+    return relations_with(cn, friend) == ALLIED
+       && relations_with(cn, foe) == AT_WAR;
+}
+
 /* Shoot back
  *
  * See if the sector being fired at will defend itself.
@@ -181,9 +171,7 @@ sb(natid att, natid def, struct sctstr *sp, coord tx, coord ty, int noisy,
 {
     int damage;
     natid own;
-    int shell;
-    int range;
-    int range2, gun;
+    int range, range2;
 
     own = sp->sct_own;
     if (own == 0)
@@ -194,18 +182,10 @@ sb(natid att, natid def, struct sctstr *sp, coord tx, coord ty, int noisy,
     range2 = mapdist(sp->sct_x, sp->sct_y, tx, ty);
     if (range < range2)
        return 0;
-    gun = sp->sct_item[I_GUN];
-    if (gun == 0)
-       return 0;
-    shell = sp->sct_item[I_SHELL];
-    if (shell <= 0)
-       shell += supply_commod(sp->sct_own, sp->sct_x, sp->sct_y,
-                              I_SHELL, 1);
-    if (shell <= 0)
-       return 0;
-    sp->sct_item[I_SHELL] = shell - 1;
+    damage = fort_fire(sp);
     putsect(sp);
-    damage = landgun((int)sp->sct_effic, gun);
+    if (damage < 0)
+       return 0;
     if (sp->sct_own != def)
        wu(0, sp->sct_own,
           "%s fired on %s in %s in defense of %s, doing %d damage!\n",