]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/mfir.c
Update copyright notice
[empserver] / src / lib / commands / mfir.c
index 788ad2ebcef5b9a6aeaf307c264a9dbda47939b3..a2de86175f1f73c935f9724e16093b752a6cc384 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2018, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
@@ -28,7 +28,7 @@
  *
  *  Known contributors to this file:
  *     Steve McClure, 2000
- *     Markus Armbruster, 2004-2015
+ *     Markus Armbruster, 2004-2016
  */
 
 #include <config.h>
 #include "optlist.h"
 #include "retreat.h"
 
-enum targ_type {
-    targ_land, targ_ship, targ_sub, targ_bogus
+enum targ_type {       /* Targeting... */
+    targ_land,         /* a sector with guns */
+    targ_ship,         /* a ship with guns */
+    targ_sub,          /* a submarine with depth charges */
+    targ_bogus         /* a bogus sector with guns */
 };
 
 struct flist {
@@ -167,8 +170,7 @@ multifire(void)
                pr("Not enough mil on ship #%d\n", item.ship.shp_uid);
                continue;
            }
-           if (mchr[item.ship.shp_type].m_glim == 0
-               && !(mchr[fship.shp_type].m_flags & M_DCH)) {
+           if (mchr[item.ship.shp_type].m_glim == 0) {
                pr("Ships %d cannot fire guns!\n", item.ship.shp_uid);
                continue;
            }
@@ -232,8 +234,7 @@ multifire(void)
                pr("No such ship exists!\n");
                continue;
            }
-           target = (mchr[(int)vship.shp_type].m_flags & M_SUB) ?
-               targ_sub : targ_ship;
+           target = targ_ship; /* targ_ship vs. targ_sub decided below */
            vict = vship.shp_own;
            x = vship.shp_x;
            y = vship.shp_y;
@@ -263,7 +264,7 @@ multifire(void)
        if (type == EF_SHIP) {
            if (!check_ship_ok(&fship))
                return RET_FAIL;
-           if (target == targ_sub || target == targ_ship) {
+           if (target == targ_ship) {
                if (fship.shp_uid == vship.shp_uid) {
                    pr("You can't fire upon yourself!\n");
                    continue;
@@ -271,26 +272,17 @@ multifire(void)
            }
            range = shp_fire_range(&fship);
            range2 = roundrange(range);
-           pr("range is %d.00 (%.2f)\n", range2, range);
-           if (target == targ_sub
-               && trange <= range2
-               && (mchr[(int)fship.shp_type].m_flags & M_DCH)) {
+           /* Use depth charges against subs, but only when in range */
+           if (target == targ_ship && trange <= range2
+               && (mchr[vship.shp_type].m_flags & M_SUB)
+               && (mchr[fship.shp_type].m_flags & M_DCH))
+               target = targ_sub;
+           if (target == targ_sub)
                dam = shp_dchrg(&fship);
-           } else {
-               if (target == targ_sub)
-                   /* Don't tell it's a sub */
-                   range2 = -1;
-               if (fship.shp_item[I_GUN] == 0) {
-                   pr("Insufficient arms.\n");
-                   continue;
-               }
+           else
                dam = shp_fire(&fship);
-           }
+           fship.shp_mission = 0;
            putship(fship.shp_uid, &fship);
-           if (CANT_HAPPEN(dam < 0)) {
-               pr("Klick!     ...\n");
-               continue;
-           }
            if (opt_NOMOBCOST == 0) {
                fship.shp_mobil = MAX(fship.shp_mobil - 15, -100);
                putship(fship.shp_uid, &fship);
@@ -305,21 +297,11 @@ multifire(void)
                    continue;
                }
            }
-
            range = lnd_fire_range(&fland);
            range2 = roundrange(range);
-           pr("range is %d.00 (%.2f)\n", range2, range);
-           if (target == targ_sub) {
-               /* Don't tell it's a sub */
-               range2 = -1;
-           }
-
            dam = lnd_fire(&fland);
+           fland.lnd_mission = 0;
            putland(fland.lnd_uid, &fland);
-           if (CANT_HAPPEN(dam < 0)) {
-               pr("Klick!     ...\n");
-               continue;
-           }
            if (target == targ_ship) {
                if (chance(lnd_acc(&fland) / 100.0))
                    dam = ldround(dam / 2.0, 1);
@@ -336,38 +318,15 @@ multifire(void)
            }
            dam = fort_fire(&fsect);
            putsect(&fsect);
-           if (CANT_HAPPEN(dam < 0)) {
-               pr("Klick!     ...\n");
-               continue;
-           }
            range = fortrange(&fsect);
            range2 = roundrange(range);
-           pr("range is %d.00 (%.2f)\n", range2, range);
-           if (target == targ_sub) {
-               /* Don't tell it's a sub */
-               range2 = -1;
-           }
        }
-       if (trange > range2) {
-           pr("Target out of range.\n");
-           switch (type) {
-           case EF_SECTOR:
-               putsect(&fsect);
-               break;
-           case EF_LAND:
-               fland.lnd_mission = 0;
-               putland(fland.lnd_uid, &fland);
-               break;
-           case EF_SHIP:
-               fship.shp_mission = 0;
-               putship(fship.shp_uid, &fship);
-               break;
-           default:
-               CANT_REACH();
-           }
+
+       if (CANT_HAPPEN(dam < 0)) {
+           pr("Jammed!\n");
            continue;
        }
-
+       pr("range is %d.00 (%.2f)\n", range2, range);
        nfiring++;
        switch (target) {
        case targ_sub:
@@ -379,6 +338,18 @@ multifire(void)
            pr("Kaboom!!!\n");
            break;
        }
+
+       /*
+        * If the player fires guns at a submarine, take care not to
+        * disclose it's a submarine: pretend the target is out of range.
+        */
+       if (target == targ_ship && (mchr[vship.shp_type].m_flags & M_SUB))
+           range2 = -1;
+       if (trange > range2) {
+           pr("Target out of range.\n");
+           continue;
+       }
+
        switch (target) {
        case targ_bogus:
        case targ_land:
@@ -426,18 +397,17 @@ multifire(void)
            shipdamage(&vship, dam);
            if (vship.shp_effic < SHIP_MINEFF)
                pr("%s sunk!\n", prsub(&vship));
-           putship(vship.shp_uid, &vship);
            if (dam && (vship.shp_rflags & RET_INJURED))
                retreat_ship(&vship, vict, 'i');
            else if (target == targ_sub && (vship.shp_rflags & RET_DCHRGED))
                retreat_ship(&vship, vict, 'd');
            else if (totaldefdam == 0 && (vship.shp_rflags & RET_HELPLESS))
                retreat_ship(&vship, vict, 'h');
+           putship(vship.shp_uid, &vship);
            break;
        }
        switch (attgp->ef_type) {
        case EF_SECTOR:
-           putsect(&fsect);
            break;
        case EF_SHIP:
            if ((target == targ_ship) || (target == targ_sub)) {
@@ -445,7 +415,6 @@ multifire(void)
                    shp_missdef(&fship, vict);
                }
            }
-           putship(fship.shp_uid, &fship);
            break;
        default:
            CANT_REACH();
@@ -458,6 +427,7 @@ multifire(void)
     else
        odds = 1.0;
     do_defdam(&fired, odds);
+    free_flist(&fired);
     return RET_OK;
 }
 
@@ -512,7 +482,7 @@ do_defdam(struct emp_qelem *list, double odds)
            if (vict)
                wu(0, vict,
                   "Return fire hit %s in %s for %d damage.\n",
-                  prsub(&ship), xyas(ship.shp_x, ship.shp_y, vict), dam);
+                  prship(&ship), xyas(ship.shp_x, ship.shp_y, vict), dam);
            shipdamage(&ship, dam);
            putship(ship.shp_uid, &ship);
        } else {
@@ -527,8 +497,6 @@ do_defdam(struct emp_qelem *list, double odds)
                wu(0, vict, "Return fire hit sector %s for %d damage.\n",
                   xyas(fp->x, fp->y, vict), dam);
        }
-       emp_remque(&fp->queue);
-       free(fp);
     }
 }
 
@@ -691,13 +659,16 @@ free_flist(struct emp_qelem *list)
 }
 
 static int
-uid_eq(struct emp_qelem *elem, void *key)
+flist_eq(struct emp_qelem *elem, void *key)
 {
-    return ((struct flist *)elem)->uid == ((struct empobj *)key)->uid;
+    struct flist *e = (struct flist *)elem;
+    struct flist *k = key;
+
+    return e->type == k->type && e->uid == k->uid;
 }
 
 static struct flist *
 search_flist(struct emp_qelem *list, struct empobj *gp)
 {
-    return (struct flist *)emp_searchque(list, gp, uid_eq);
+    return (struct flist *)emp_searchque(list, gp, flist_eq);
 }