]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/mfir.c
Remove option TREATIES
[empserver] / src / lib / commands / mfir.c
index e3b59e5183a3093ae0945e6c5ec0a8f35ac63e5b..87b308aa875564f4845a608667afb6b43c4d1dee 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-2014, 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/>.
  *
  *  ---
  *
  *  ---
  *
  *  multifire.c: Fire at other sectors/ships
- * 
+ *
  *  Known contributors to this file:
  *     Steve McClure, 2000
+ *     Markus Armbruster, 2004-2014
  */
 
 #include <config.h>
 
+#include "chance.h"
 #include "commands.h"
 #include "empobj.h"
+#include "news.h"
 #include "optlist.h"
 #include "retreat.h"
 
@@ -45,7 +47,7 @@ enum targ_type {
 struct flist {
     struct emp_qelem queue;    /* list of fired things */
     short type;                        /* EF_SECTOR, EF_SHIP or EF_LAND */
-    short uid;
+    int uid;
     coord x, y;
     int defdam;                        /* damage defenders did */
     natid victim;
@@ -71,20 +73,17 @@ multifire(void)
     coord fy;
     coord x;
     coord y;
-    int mil;
     int dam;
     int totaldefdam = 0;
     int vshipno;
-    double prb;
     natid vict;
     struct shpstr fship;
     struct lndstr fland;
     struct sctstr fsect;
+    char *sep = "";
     struct shpstr vship;
     struct sctstr vsect;
     enum targ_type target;
-    int rel;
-    struct natstr *natp;
     struct nstr_item nbst;
     int type;
     struct empobj *attgp;
@@ -98,9 +97,9 @@ multifire(void)
 
     emp_initque(&fired);
     emp_initque(&defended);
-    if (!(p = getstarg(player->argp[1],
-                      "Firing from ship(s), sect(s), or land unit(s)? ",
-                      buf)))
+    p = getstarg(player->argp[1],
+                "Firing from ship(s), sect(s), or land unit(s)? ", buf);
+    if (!p)
        return RET_SYN;
     type = ef_byname_from(p, ef_with_guns);
     if (opt_NO_FORT_FIRE && type == EF_SECTOR) {
@@ -111,17 +110,9 @@ multifire(void)
        pr("Ships, land units or sectors only!\n");
        return RET_SYN;
     }
-    if ((ptr = getstarg(player->argp[2], "Firing from? ", buf)) == 0
-       || *ptr == '\0')
-       return RET_SYN;
-
-    if (!snxtitem(&nbst, type, ptr))
+    if (!snxtitem(&nbst, type, player->argp[2], "Firing from? "))
        return RET_SYN;
 
-    if (player->aborted) {
-       pr("Fire aborted.\n");
-       return RET_OK;
-    }
     while (nxtitem(&nbst, &item)) {
        if (type == EF_LAND) {
            if (!getland(item.land.lnd_uid, &fland))
@@ -155,10 +146,16 @@ multifire(void)
                   fland.lnd_uid, LAND_MINFIREEFF);
                continue;
            }
+           if (fland.lnd_item[I_GUN] == 0) {
+               pr("%s -- not enough guns\n", prland(&fland));
+               continue;
+           }
+
            if (fland.lnd_item[I_SHELL] == 0) {
                pr("%s -- not enough shells\n", prland(&fland));
                continue;
            }
+           pr("%s%s ready to fire\n", sep, prland(&fland));
            fx = fland.lnd_x;
            fy = fland.lnd_y;
        } else if (type == EF_SHIP) {
@@ -170,7 +167,8 @@ multifire(void)
                pr("Not enough mil on ship #%d\n", item.ship.shp_uid);
                continue;
            }
-           if (mchr[item.ship.shp_type].m_glim == 0) {
+           if (mchr[item.ship.shp_type].m_glim == 0
+               && !(mchr[fship.shp_type].m_flags & M_DCH)) {
                pr("Ships %d cannot fire guns!\n", item.ship.shp_uid);
                continue;
            }
@@ -186,9 +184,10 @@ multifire(void)
                pr("Ship #%d is crippled!\n", item.ship.shp_uid);
                continue;
            }
+           pr("%s%s ready to fire\n", sep, prship(&fship));
            fx = fship.shp_x;
            fy = fship.shp_y;
-       } else if (type == EF_SECTOR) {
+       } else {
            if (!getsect(item.sect.sct_x, item.sect.sct_y, &fsect))
                continue;
            if (item.sect.sct_own != player->cnum)
@@ -214,19 +213,18 @@ multifire(void)
                   xyas(item.sect.sct_x, item.sect.sct_y, player->cnum));
                continue;
            }
-           pr("\nSector %s firing\n",
+           pr("%sSector %s ready to fire\n", sep,
               xyas(item.sect.sct_x, item.sect.sct_y, player->cnum));
            fx = fsect.sct_x;
            fy = fsect.sct_y;
        }
+       sep = "\n";
 
-       if ((ptr = getstarg(player->argp[3], "Firing at? ", buf)) == 0
-           || *ptr == '\0')
+       ptr = getstarg(player->argp[3], "Firing at? ", buf);
+       if (!ptr)
+           return RET_FAIL;
+       if (!*ptr)
            continue;
-       if (player->aborted) {
-           pr("Fire aborted.\n");
-           continue;
-       }
        if (!issector(ptr)) {
            vshipno = atoi(ptr);
            if (vshipno < 0 || !getship(vshipno, &vship) ||
@@ -265,25 +263,12 @@ multifire(void)
        if (type == EF_SHIP) {
            if (!check_ship_ok(&fship))
                return RET_FAIL;
-           if (fship.shp_own != player->cnum) {
-               pr("Not your ship!\n");
-               continue;
-           }
            if (target == targ_sub || target == targ_ship) {
                if (fship.shp_uid == vship.shp_uid) {
                    pr("You can't fire upon yourself!\n");
                    continue;
                }
            }
-           if ((mil = fship.shp_item[I_MILIT]) < 1) {
-               pr("Not enough military for firing crew.\n");
-               continue;
-           }
-           if (fship.shp_effic < 60) {
-               pr("Ship #%d is crippled (%d%%)\n",
-                  fship.shp_uid, fship.shp_effic);
-               continue;
-           }
            range = shp_fire_range(&fship);
            range2 = roundrange(range);
            pr("range is %d.00 (%.2f)\n", range2, range);
@@ -302,7 +287,7 @@ multifire(void)
                dam = shp_fire(&fship);
            }
            putship(fship.shp_uid, &fship);
-           if (dam <= 0) {
+           if (CANT_HAPPEN(dam < 0)) {
                pr("Klick!     ...\n");
                continue;
            }
@@ -313,11 +298,6 @@ multifire(void)
        } else if (type == EF_LAND) {
            if (!check_land_ok(&fland))
                return RET_FAIL;
-           if (fland.lnd_own != player->cnum) {
-               pr("Not your unit!\n");
-               continue;
-           }
-
            if (target == targ_land) {
                if (fland.lnd_x == vsect.sct_x
                    && fland.lnd_y == vsect.sct_y) {
@@ -326,15 +306,6 @@ multifire(void)
                }
            }
 
-           if (lchr[fland.lnd_type].l_dam == 0) {
-               pr("Unit %d cannot fire!\n", fland.lnd_uid);
-               continue;
-           }
-           if (fland.lnd_item[I_GUN] == 0) {
-               pr("%s -- not enough guns\n", prland(&fland));
-               continue;
-           }
-
            range = lnd_fire_range(&fland);
            range2 = roundrange(range);
            pr("range is %d.00 (%.2f)\n", range2, range);
@@ -345,7 +316,7 @@ multifire(void)
 
            dam = lnd_fire(&fland);
            putland(fland.lnd_uid, &fland);
-           if (dam < 0) {
+           if (CANT_HAPPEN(dam < 0)) {
                pr("Klick!     ...\n");
                continue;
            }
@@ -356,12 +327,6 @@ multifire(void)
        } else {
            if (!check_sect_ok(&fsect))
                return RET_FAIL;
-           if (fsect.sct_own != player->cnum ||
-               fsect.sct_type != SCT_FORTR) {
-               pr("No fortress at %s\n",
-                  xyas(fsect.sct_x, fsect.sct_y, player->cnum));
-               continue;
-           }
            if (target == targ_land) {
                if (fsect.sct_x == vsect.sct_x
                    && fsect.sct_y == vsect.sct_y) {
@@ -369,17 +334,9 @@ multifire(void)
                    continue;
                }
            }
-           if (fsect.sct_item[I_GUN] == 0) {
-               pr("Insufficient arms.\n");
-               continue;
-           }
-           if (fsect.sct_item[I_MILIT] < 5) {
-               pr("Not enough military for firing crew.\n");
-               continue;
-           }
            dam = fort_fire(&fsect);
            putsect(&fsect);
-           if (dam < 0) {
+           if (CANT_HAPPEN(dam < 0)) {
                pr("Klick!     ...\n");
                continue;
            }
@@ -410,54 +367,16 @@ multifire(void)
            }
            continue;
        }
-       switch (target) {
-       case targ_ship:
-           if (!trechk(player->cnum, vict, SEAFIR))
-               continue;
-           break;
-       case targ_sub:
-           if (!trechk(player->cnum, vict, SUBFIR))
-               continue;
-           break;
-       case targ_land:
-           if (!trechk(player->cnum, vict, LANFIR))
-               continue;
-           break;
-       default:
-           break;
-       }
 
-       if (opt_SLOW_WAR) {
-           if (target == targ_land) {
-               natp = getnatp(player->cnum);
-               rel = getrel(natp, vict);
-               if ((rel != AT_WAR) && (player->cnum != vict) &&
-                   (vict) && (vsect.sct_oldown != player->cnum)) {
-                   pr("You're not at war with them!\n");
-                   continue;
-               }
-           }
-       }
        nfiring++;
        switch (target) {
        case targ_sub:
            pr_beep();
            pr("Kawhomp!!!\n");
-           if (vship.shp_rflags & RET_DCHRGED)
-               retreat_ship(&vship, 'd');
            break;
        default:
            pr_beep();
            pr("Kaboom!!!\n");
-           prb = range2 ? (double)trange / range2 : 1.0;
-           prb *= prb;
-           if (chance(prb)) {
-               pr("Wind deflects shells.\n");
-/*                     dam = (int)(dam / 2.0);*/
-               dam *= (90 - (random() % 11)) / 100.0;
-               if (dam < 0)
-                   dam = 0;
-           }
            break;
        }
        switch (target) {
@@ -470,19 +389,12 @@ multifire(void)
                   player->cnum, xyas(x, y, vict), dam);
            pr("Shells hit sector %s for %d damage.\n",
               xyas(x, y, player->cnum), dam);
-           if (target != targ_bogus)
-               sectdamage(&vsect, dam);
            break;
        case targ_ship:
            nreport(player->cnum, N_SHP_SHELL, vict, 1);
            /* fall through */
        default:
-           if ((target != targ_sub) ||
-               ((vship.shp_rflags & RET_DCHRGED) == 0))
-               check_retreat_and_do_shipdamage(&vship, dam);
-           else
-               shipdamage(&vship, dam);
-           if (vict) {
+           if (vict && vict != player->cnum) {
                wu(0, vict,
                   "Country #%d shelled %s in %s for %d damage.\n",
                   player->cnum, prship(&vship),
@@ -491,10 +403,6 @@ multifire(void)
            pr("Shells hit %s in %s for %d damage.\n",
               prsub(&vship),
               xyas(vship.shp_x, vship.shp_y, player->cnum), dam);
-
-           if (vship.shp_effic < SHIP_MINEFF)
-               pr("%s sunk!\n", prsub(&vship));
-
            break;
        }
        /*  Ok, now, check if we had a bogus target.  If so,
@@ -509,15 +417,26 @@ multifire(void)
        totaldefdam = defend(&fired, &defended, attgp, vict, &ndefending);
        switch (target) {
        case targ_land:
+           getsect(x, y, &vsect);
+           sectdamage(&vsect, dam);
            putsect(&vsect);
            break;
        default:
+           getship(vshipno, &vship);
+           check_retreat_and_do_shipdamage(&vship, dam);
+           if (vship.shp_effic < SHIP_MINEFF)
+               pr("%s sunk!\n", prsub(&vship));
+           else if (target == targ_sub
+               && (vship.shp_rflags & RET_DCHRGED)
+               && !(vship.shp_rflags & RET_INJURED))
+               retreat_ship(&vship, 'd');
+           else if (totaldefdam == 0
+                    && (vship.shp_rflags & RET_HELPLESS)
+                    && !(vship.shp_rflags & RET_INJURED))
+               retreat_ship(&vship, 'h');
            putship(vship.shp_uid, &vship);
            break;
        }
-       if ((totaldefdam == 0) && (target == targ_ship))
-           if (vship.shp_rflags & RET_HELPLESS)
-               retreat_ship(&vship, 'h');
        switch (attgp->ef_type) {
        case EF_SECTOR:
            putsect(&fsect);
@@ -623,7 +542,7 @@ quiet_bigdef(int type, struct emp_qelem *list, natid own, natid aown,
     struct shpstr ship;
     struct lndstr land;
     struct nstr_item ni;
-    int dam, dam2, rel, rel2;
+    int dam, dam2;
     struct sctstr firing;
     struct nstr_sect ns;
     struct flist *fp;
@@ -633,19 +552,12 @@ quiet_bigdef(int type, struct emp_qelem *list, natid own, natid aown,
     dam = 0;
     snxtitem_dist(&ni, EF_SHIP, ax, ay, 8);
     while (nxtitem(&ni, &ship)) {
-       if (ship.shp_own == 0)
+       if (!feels_like_helping(ship.shp_own, own, aown))
            continue;
 
        if ((mchr[ship.shp_type].m_flags & M_SUB) && type != EF_SHIP)
            continue;
 
-       rel = getrel(getnatp(ship.shp_own), own);
-       rel2 = getrel(getnatp(ship.shp_own), aown);
-       if ((ship.shp_own != own) && ((rel != ALLIED) || (rel2 != AT_WAR)))
-           continue;
-       /* Don't shoot yourself */
-       if (ship.shp_own == aown)
-           continue;
        if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
            erange = torprange(&ship);
            if (roundrange(erange) < ni.curdist)
@@ -685,16 +597,7 @@ quiet_bigdef(int type, struct emp_qelem *list, natid own, natid aown,
     }
     snxtitem_dist(&ni, EF_LAND, ax, ay, 8);
     while (nxtitem(&ni, &land)) {
-       if (land.lnd_own == 0)
-           continue;
-       /* Don't shoot yourself */
-       if (land.lnd_own == aown)
-           continue;
-
-       rel = getrel(getnatp(land.lnd_own), own);
-       rel2 = getrel(getnatp(land.lnd_own), aown);
-
-       if ((land.lnd_own != own) && ((rel != ALLIED) || (rel2 != AT_WAR)))
+       if (!feels_like_helping(land.lnd_own, own, aown))
            continue;
 
        erange = lnd_fire_range(&land);
@@ -715,6 +618,10 @@ quiet_bigdef(int type, struct emp_qelem *list, natid own, natid aown,
        if (!fp)
            add_to_flist(list, (struct empobj *)&land, dam2, 0);
        nreport(land.lnd_own, N_FIRE_BACK, player->cnum, 1);
+       if (type == EF_SHIP) {
+           if (chance(lnd_acc(&land) / 100.0))
+               dam2 = ldround(dam2 / 2.0, 1);
+       }
        dam += dam2;
     }
 
@@ -728,17 +635,9 @@ quiet_bigdef(int type, struct emp_qelem *list, natid own, natid aown,
     if (!opt_NO_FORT_FIRE) {
        snxtsct_dist(&ns, ax, ay, 8);
        while (nxtsct(&ns, &firing)) {
-           if (firing.sct_own == 0)
+           if (!feels_like_helping(firing.sct_own, own, aown))
                continue;
-           rel = getrel(getnatp(firing.sct_own), own);
-           rel2 = getrel(getnatp(firing.sct_own), aown);
 
-           if ((firing.sct_own != own) &&
-               ((rel != ALLIED) || (rel2 != AT_WAR)))
-               continue;
-           /* Don't shoot yourself */
-           if (firing.sct_own == aown)
-               continue;
            erange = fortrange(&firing);
            if (roundrange(erange) < ns.curdist)
                continue;