]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/mission.c
Update copyright notice
[empserver] / src / lib / subs / mission.c
index bd612cc50c65150f4c2d5fd0baefe2b26e5f8613..ee909b7c94c11efa089dceabc72b963bbe414ae9 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2010, 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/>.
  *
  *  ---
  *
  *  Known contributors to this file:
  *     Ken Stevens, 1995
  *     Steve McClure, 1996-2000
- *     Markus Armbruster, 2003-2009
+ *     Markus Armbruster, 2003-2015
  */
 
 #include <config.h>
 
 #include <stdlib.h>
+#include "chance.h"
 #include "empobj.h"
 #include "file.h"
 #include "item.h"
 #include "misc.h"
 #include "mission.h"
+#include "news.h"
 #include "nsc.h"
 #include "optlist.h"
 #include "path.h"
-#include "player.h"
+#include "plague.h"
 #include "prototypes.h"
 #include "queue.h"
 #include "xy.h"
@@ -106,7 +107,7 @@ ground_interdict(coord x, coord y, natid victim, char *s)
     act[0] = 0;
     for (cn = 1; cn < MAXNOC; cn++) {
        act[cn] = relations_with(cn, victim) <= HOSTILE;
-       emp_initque((struct emp_qelem *)&mi[cn]);
+       emp_initque(&mi[cn].queue);
     }
 
     build_mission_list(mi, act, act, x, y, MI_INTERDICT);
@@ -187,11 +188,11 @@ unit_interdict(coord x, coord y, natid victim, char *s, int hardtarget,
     memset(mi, 0, sizeof(mi));
     other_act[0] = plane_act[0] = 0;
     for (cn = 1; cn < MAXNOC; cn++) {
-       rel = getrel(getnatp(cn), victim);
+       rel = relations_with(cn, victim);
        other_act[cn] = rel <= HOSTILE;
        plane_act[cn] = mission == MI_SINTERDICT
-           ? cn != victim && rel <= NEUTRAL : other_act[cn];
-       emp_initque((struct emp_qelem *)&mi[cn]);
+           ? rel <= NEUTRAL : other_act[cn];
+       emp_initque(&mi[cn].queue);
     }
 
     build_mission_list(mi, other_act, plane_act, x, y, mission);
@@ -232,8 +233,8 @@ def_support(coord x, coord y, natid victim, natid actee)
 }
 
 /*
- * Perform support missions in X,Y against VICTIM for ACTEE.
- * MISSION is either MI_OSUPPORT or MI_DSUPPORT.
+ * Perform support missions in @x,@y against @victim for @actee.
+ * @mission is either MI_OSUPPORT or MI_DSUPPORT.
  * Return total damage.
  */
 static int
@@ -249,7 +250,7 @@ dosupport(coord x, coord y, natid victim, natid actee, int mission)
     act[0] = 0;
     for (cn = 1; cn < MAXNOC; cn++) {
        act[cn] = feels_like_helping(cn, actee, victim);
-       emp_initque((struct emp_qelem *)&mi[cn]);
+       emp_initque(&mi[cn].queue);
     }
 
     build_mission_list(mi, act, act, x, y, MI_SUPPORT);
@@ -288,7 +289,7 @@ build_mission_list_type(struct genlist mi[], unsigned char act[],
 
     snxtitem_all(&ni, type);
     while (nxtitem(&ni, &item)) {
-       gp = (struct empobj *)&item;
+       gp = &item.gen;
 
        if (!act[gp->own])
            continue;
@@ -330,7 +331,8 @@ find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
        if (!in_oparea((struct empobj *)&plane, x, y))
            continue;
        plp = malloc(sizeof(struct plist));
-       memset(plp, 0, sizeof(struct plist));
+       plp->load = 0;
+       plp->pstage = PLG_HEALTHY;
        plp->pcp = &plchr[(int)plane.pln_type];
        plp->plane = plane;
        emp_insque(&plp->queue, escorts);
@@ -372,8 +374,8 @@ perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
            else {
                /* save planes for later */
                plp = malloc(sizeof(struct plist));
-
-               memset(plp, 0, sizeof(struct plist));
+               plp->load = 0;
+               plp->pstage = PLG_HEALTHY;
                plp->pcp = pcp;
                memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
                if (plp->pcp->pl_flags & P_M)
@@ -485,8 +487,7 @@ perform_mission_ship(int dam, struct shpstr *sp, coord x, coord y,
        wu(0, sp->shp_own,
           "\tEffective torpedo range is %d.0\n", range);
        wu(0, sp->shp_own,
-          "\tWhooosh... Hitchance = %d%%\n",
-          (int)(hitchance * 100));
+          "\tWhooosh... Hitchance = %.0f%%\n", hitchance * 100);
 
        if (!chance(hitchance)) {
            wu(0, sp->shp_own, "\tMissed\n");
@@ -559,7 +560,7 @@ perform_mission_msl(int dam, struct emp_qelem *missiles, coord x, coord y,
            if (!msl_hit(&plp->plane, SECT_HARDTARGET, EF_SECTOR,
                         N_SCT_MISS, N_SCT_SMISS, sublaunch, victim))
                CANT_REACH();
-           dam2 = pln_damage(&plp->plane, 'p', 1);
+           dam2 = pln_damage(&plp->plane, 'p', "");
            air_dam += dam2;
        use_up_msl:
            plp->plane.pln_effic = 0;
@@ -579,7 +580,8 @@ perform_mission_bomb(int dam, struct emp_qelem *bombers, coord x, coord y,
 {
     struct emp_qelem *qp, *newqp, escorts, airp, b, e;
     struct plist *plp;
-    int plane_owner, performed, air_dam, md;
+    int plane_owner, performed, air_dam;
+    size_t md;
 
     emp_initque(&escorts);
     emp_initque(&airp);
@@ -614,11 +616,9 @@ perform_mission_bomb(int dam, struct emp_qelem *bombers, coord x, coord y,
     performed = air_dam = 0;
     for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
        struct airport *air;
-       char buf[512];
-       char *pp;
+       char buf[1024];
 
        air = (struct airport *)qp;
-       md = mapdist(x, y, air->x, air->y);
 
        emp_initque(&b);
        emp_initque(&e);
@@ -629,16 +629,17 @@ perform_mission_bomb(int dam, struct emp_qelem *bombers, coord x, coord y,
        /* Split off the escorts at this base into e */
        divide(&escorts, &e, air->x, air->y);
 
-       mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', NULL);
+       if (path_find(air->x, air->y, x, y, plane_owner, MOB_FLY) < 0)
+           continue;
+       md = path_find_route(buf, sizeof(buf), air->x, air->y, x, y);
+       if (md >= sizeof(buf))
+           continue;
 
+       mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', NULL);
        if (QEMPTY(&b))
            continue;
-
        mission_pln_arm(&e, air->x, air->y, 2 * md, 'e', NULL);
 
-       pp = BestAirPath(buf, air->x, air->y, x, y);
-       if (CANT_HAPPEN(!pp))
-           continue;
        performed = 1;
        wu(0, plane_owner, "Flying %s mission from %s to %s\n",
           mission_name(mission),
@@ -651,7 +652,7 @@ perform_mission_bomb(int dam, struct emp_qelem *bombers, coord x, coord y,
               xyas(x, y, air->own));
        }
 
-       ac_encounter(&b, &e, air->x, air->y, pp, 0);
+       ac_encounter(&b, &e, air->x, air->y, buf, 0);
 
        if (!QEMPTY(&b))
            air_dam +=
@@ -746,7 +747,7 @@ mission_name(int mission)
 }
 
 /*
- * Maximum distance GP can perform its mission.
+ * Maximum distance @gp can perform its mission.
  * Note: this has nothing to do with the radius of the op-area.
  * oprange() governs where the unit *can* strike, the op-area governs
  * where the player wants it to strike.
@@ -772,7 +773,7 @@ oprange(struct empobj *gp)
 }
 
 /*
- * Does GP's mission op area cover X,Y?
+ * Does @gp's mission op area cover @x,@y?
  */
 int
 in_oparea(struct empobj *gp, coord x, coord y)
@@ -833,7 +834,7 @@ mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
        }
 
        if (pcp->pl_flags & P_A) {
-           if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
+           if (!pct_chance(pln_identchance(pp, hardtarget, EF_SHIP))) {
                emp_remque(qp);
                free(qp);
                continue;
@@ -895,12 +896,15 @@ mission_pln_equip(struct plist *plp, struct ichrstr *ip, char mission)
     pcp = plp->pcp;
     if (pp->pln_ship >= 0) {
        getship(pp->pln_ship, &ship);
+       plp->pstage = ship.shp_pstage;
        item = ship.shp_item;
     } else if (pp->pln_land >= 0) {
        getland(pp->pln_land, &land);
+       plp->pstage = land.lnd_pstage;
        item = land.lnd_item;
     } else {
        getsect(pp->pln_x, pp->pln_y, &sect);
+       plp->pstage = sect.sct_pstage;
        item = sect.sct_item;
     }
     if (pcp->pl_fuel > item[I_PETROL]) {
@@ -987,7 +991,7 @@ add_airport(struct emp_qelem *airp, coord x, coord y)
     getsect(x, y, &sect);
     a->own = sect.sct_own;
 
-    emp_insque((struct emp_qelem *)a, airp);
+    emp_insque(&a->queue, airp);
 }
 
 /*
@@ -1022,6 +1026,7 @@ air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
     struct plist *plp;
     struct plnstr *pp;
     int newdam, dam = 0;
+    char buf[32];
     int hitchance;
 
     for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
@@ -1035,7 +1040,7 @@ air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
            continue;
 
        if (plp->pcp->pl_flags & P_A) {
-           if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
+           if (!pct_chance(pln_identchance(pp, hardtarget, EF_SHIP))) {
                wu(0, pp->pln_own,
                   "\t%s detects sub movement in %s\n",
                   prplane(pp), xyas(x, y, pp->pln_own));
@@ -1055,20 +1060,25 @@ air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
               "\t%s pinbombing %s %s in %s\n",
               prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
        }
-       hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
+
+       buf[0] = 0;
        if (nuk_on_plane(&plp->plane) >= 0)
            hitchance = 100;
-       else if (hardtarget != SECT_HARDTARGET)
-           wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
-       if (roll(100) <= hitchance) {
-           newdam = pln_damage(&plp->plane, 'p', 1);
+       else {
+           hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
+           if (hardtarget != SECT_HARDTARGET)
+               snprintf(buf, sizeof(buf), "\t\t%d%% hitchance...",
+                        hitchance);
+       }
+       if (pct_chance(hitchance)) {
+           newdam = pln_damage(&plp->plane, 'p', buf);
            wu(0, pp->pln_own,
               "\t\thit %s %s for %d damage\n",
               cname(victim), s, newdam);
            dam += newdam;
        } else {
-           newdam = pln_damage(&plp->plane, 'p', 0);
-           wu(0, pp->pln_own, "missed\n");
+           newdam = pln_damage(&plp->plane, 'p', NULL);
+           wu(0, pp->pln_own, "%smissed\n", buf);
            if (mission == MI_SINTERDICT) {
                mpr(victim,
                    "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",