]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/shpsub.c
Update copyright notice
[empserver] / src / lib / subs / shpsub.c
index 8291c46dadf7e75905527516e841cf665a4c2731..2182a4a25fcbb5526392467a966e9030573c9e8a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
@@ -58,6 +58,20 @@ static int shp_check_one_mines(struct ulist *);
 static int shp_hit_mine(struct shpstr *);
 static void shp_stays(natid, char *, struct ulist *);
 
+static struct ulist *
+shp_find_capable(struct emp_qelem *list, int flags)
+{
+    struct emp_qelem *qp;
+    struct ulist *mlp;
+
+    for (qp = list->q_back; qp != list; qp = qp->q_back) {
+       mlp = (struct ulist *)qp;
+       if (mchr[mlp->unit.ship.shp_type].m_flags & flags)
+           return mlp;
+    }
+    return NULL;
+}
+
 void
 shp_sel(struct nstr_item *ni, struct emp_qelem *list)
 {
@@ -96,7 +110,6 @@ shp_insque(struct shpstr *sp, struct emp_qelem *list)
 {
     struct ulist *mlp = malloc(sizeof(struct ulist));
 
-    mlp->chrp = (struct empobj_chr *)&mchr[sp->shp_type];
     mlp->unit.ship = *sp;
     mlp->mobil = sp->shp_mobil;
     emp_insque(&mlp->queue, list);
@@ -193,8 +206,17 @@ shp_nav_put_one(struct ulist *mlp)
     free(mlp);
 }
 
+/*
+ * Sweep seamines with engineers in SHIP_LIST for ACTOR.
+ * All ships in SHIP_LIST must be in the same sector.
+ * If EXPLICIT is non-zero, this is for an explicit sweep command from
+ * a player.  Else it's an automatic "on the move" sweep.
+ * If TAKEMOB is non-zero, require and charge mobility.
+ * Return non-zero when the ships should stop.
+ */
 int
-shp_sweep(struct emp_qelem *ship_list, int verbose, int takemob, natid actor)
+shp_sweep(struct emp_qelem *ship_list, int explicit, int takemob,
+         natid actor)
 {
     struct emp_qelem *qp;
     struct emp_qelem *next;
@@ -202,41 +224,52 @@ shp_sweep(struct emp_qelem *ship_list, int verbose, int takemob, natid actor)
     struct sctstr sect;
     int mines, m, max, shells;
     int changed = 0;
-    int stopping = 0;
+    int stopping = 0, first = 1;
+
+    mlp = shp_find_capable(ship_list, M_SWEEP);
+    if (!mlp) {
+       if (explicit)
+           mpr(actor, "No minesweepers!\n");
+       return 0;
+    }
+
+    getsect(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y, &sect);
+    if (sect.sct_type != SCT_WATER) {
+       if (explicit)
+           mpr(actor, "%s is a %s.  No seamines there!\n",
+              xyas(sect.sct_x, sect.sct_y, actor),
+              dchr[sect.sct_type].d_name);
+       return 0;
+    }
 
     for (qp = ship_list->q_back; qp != ship_list; qp = next) {
        next = qp->q_back;
        mlp = (struct ulist *)qp;
-       if (!(((struct mchrstr *)mlp->chrp)->m_flags & M_SWEEP)) {
-           if (verbose)
-               mpr(actor, "%s doesn't have minesweeping capability!\n",
-                   prship(&mlp->unit.ship));
-           continue;
-       }
-       if (takemob && mlp->mobil <= 0.0) {
-           if (verbose)
-               mpr(actor, "%s is out of mobility!\n",
-                   prship(&mlp->unit.ship));
+       if (!(mchr[mlp->unit.ship.shp_type].m_flags & M_SWEEP))
            continue;
-       }
-       getsect(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y, &sect);
-       if (sect.sct_type != SCT_WATER) {
-           if (verbose)
-               mpr(actor, "%s is not at sea.  No mines there!\n",
-                   prship(&mlp->unit.ship));
-           continue;
-       }
        if (takemob) {
+           if (mlp->mobil <= 0.0) {
+               if (explicit)
+                   mpr(actor, "%s is out of mobility!\n",
+                       prship(&mlp->unit.ship));
+               continue;
+           }
            mlp->mobil -= shp_mobcost(&mlp->unit.ship);
            mlp->unit.ship.shp_mobil = (int)mlp->mobil;
        }
        putship(mlp->unit.ship.shp_uid, &mlp->unit.ship);
+       getsect(mlp->unit.ship.shp_x, mlp->unit.ship.shp_y, &sect);
        if (!(mines = sect.sct_mines))
            continue;
-       max = ((struct mchrstr *)mlp->chrp)->m_item[I_SHELL];
+       max = mchr[mlp->unit.ship.shp_type].m_item[I_SHELL];
        shells = mlp->unit.ship.shp_item[I_SHELL];
        for (m = 0; mines > 0 && m < 5; m++) {
            if (chance(0.66)) {
+               if (first) {
+                   mpr(actor, "Approaching minefield at %s...\n",
+                       xyas(sect.sct_x, sect.sct_y, actor));
+                   first = 0;
+               }
                mpr(actor, "Sweep...\n");
                mines--;
                shells = MIN(max, shells + 1);
@@ -371,10 +404,10 @@ shp_count(struct emp_qelem *list, int wantflags, int nowantflags,
        if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
            continue;
        if (wantflags &&
-           (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
+           (mchr[mlp->unit.ship.shp_type].m_flags & wantflags) != wantflags)
            continue;
        if (nowantflags &&
-           ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
+           mchr[mlp->unit.ship.shp_type].m_flags & nowantflags)
            continue;
        ++count;
     }
@@ -414,10 +447,10 @@ shp_damage(struct emp_qelem *list, int totdam, int wantflags,
        if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
            continue;
        if (wantflags &&
-           (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
+           (mchr[mlp->unit.ship.shp_type].m_flags & wantflags) != wantflags)
            continue;
        if (nowantflags &&
-           ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
+           mchr[mlp->unit.ship.shp_type].m_flags & nowantflags)
            continue;
        shp_damage_one(mlp, dam);
     }
@@ -439,10 +472,10 @@ shp_contains(struct emp_qelem *list, int newx, int newy, int wantflags,
        if (newx != mlp->unit.ship.shp_x || newy != mlp->unit.ship.shp_y)
            continue;
        if (wantflags &&
-           (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
+           (mchr[mlp->unit.ship.shp_type].m_flags & wantflags) != wantflags)
            continue;
        if (nowantflags &&
-           ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
+           mchr[mlp->unit.ship.shp_type].m_flags & nowantflags)
            continue;
        return 1;
     }
@@ -462,20 +495,20 @@ most_valuable_ship(struct emp_qelem *list, coord x, coord y)
        mlp = (struct ulist *)qp;
        if (mlp->unit.ship.shp_x != x || mlp->unit.ship.shp_y != y)
            continue;
-       if (((struct mchrstr *)mlp->chrp)->m_flags & M_SUB)
+       if (mchr[mlp->unit.ship.shp_type].m_flags & M_SUB)
            continue;
-       if (!((struct mchrstr *)mlp->chrp)->m_nxlight &&
-           !((struct mchrstr *)mlp->chrp)->m_nchoppers &&
-           ((struct mchrstr *)mlp->chrp)->m_cost < 1000 &&
-           !((struct mchrstr *)mlp->chrp)->m_nplanes &&
-           !((struct mchrstr *)mlp->chrp)->m_nland)
+       if (!mchr[mlp->unit.ship.shp_type].m_nxlight &&
+           !mchr[mlp->unit.ship.shp_type].m_nchoppers &&
+           mchr[mlp->unit.ship.shp_type].m_cost < 1000 &&
+           !mchr[mlp->unit.ship.shp_type].m_nplanes &&
+           !mchr[mlp->unit.ship.shp_type].m_nland)
            continue;
        if (!mvs) {
            mvs = mlp;
            continue;
        }
-       if (((struct mchrstr *)mlp->chrp)->m_cost * mlp->unit.ship.shp_effic >
-           ((struct mchrstr *)mvs->chrp)->m_cost * mvs->unit.ship.shp_effic)
+       if (mchr[mlp->unit.ship.shp_type].m_cost * mlp->unit.ship.shp_effic >
+           mchr[mvs->unit.ship.shp_type].m_cost * mvs->unit.ship.shp_effic)
            mvs = mlp;
     }
     return mvs;
@@ -495,10 +528,10 @@ shp_easiest_target(struct emp_qelem *list, int wantflags, int nowantflags)
        next = qp->q_back;
        mlp = (struct ulist *)qp;
        if (wantflags &&
-           (((struct mchrstr *)mlp->chrp)->m_flags & wantflags) != wantflags)
+           (mchr[mlp->unit.ship.shp_type].m_flags & wantflags) != wantflags)
            continue;
        if (nowantflags &&
-           ((struct mchrstr *)mlp->chrp)->m_flags & nowantflags)
+           mchr[mlp->unit.ship.shp_type].m_flags & nowantflags)
            continue;
        hard = shp_hardtarget(&mlp->unit.ship);
        if (hard < easiest)
@@ -584,7 +617,7 @@ notify_coastguard(struct emp_qelem *list, int trange, struct sctstr *sectp)
     for (qp = list->q_back; qp != list; qp = next) {
        next = qp->q_back;
        mlp = (struct ulist *)qp;
-       if (((struct mchrstr *)mlp->chrp)->m_flags & M_SUB)
+       if (mchr[mlp->unit.ship.shp_type].m_flags & M_SUB)
            continue;
        if (natp->nat_flags & NF_COASTWATCH)
            wu(0, sectp->sct_own,
@@ -840,7 +873,7 @@ shp_nav_one_sector(struct emp_qelem *list, int dir, natid actor)
        rad_map_set(mlp->unit.ship.shp_own,
                    mlp->unit.ship.shp_x, mlp->unit.ship.shp_y,
                    mlp->unit.ship.shp_effic, mlp->unit.ship.shp_tech,
-                   ((struct mchrstr *)mlp->chrp)->m_vrnge);
+                   mchr[mlp->unit.ship.shp_type].m_vrnge);
     }
     if (QEMPTY(list))
        return stopping;