]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/revolt.c
Don't leave cargo stuck on land unit killed by che
[empserver] / src / lib / update / revolt.c
index d6fe613da5e018e4261820aa652b0f41f4efbb5a..a5bed8f5a4ce21a34362abfe13f7c27e0735f8fb 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-2011, 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/>.
  *
  *  ---
  *
@@ -30,6 +29,7 @@
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Steve McClure, 1997-2000
+ *     Markus Armbruster, 2004-2011
  */
 
 #include <config.h>
 #include "lost.h"
 #include "news.h"
 #include "nsc.h"
+#include "nuke.h"
 #include "path.h"
 #include "plane.h"
 #include "update.h"
 
 static void take_casualties(struct sctstr *, int);
+static void lnd_dies_fighting_che(struct lndstr *);
 
 void
 revolt(struct sctstr *sp)
@@ -62,7 +64,6 @@ revolt(struct sctstr *sp)
     if (che > (civ + uw) * 3)
        return;
     che_uw = 0;
-    che_civ = 0;
     /* che due to civilian unrest */
     n = 10 - (random() % 20);
     che_civ = 3 + (civ * n / 500);
@@ -92,10 +93,6 @@ revolt(struct sctstr *sp)
            sp->sct_item[I_CIVIL] = civ;
        if (che_uw > 0)
            sp->sct_item[I_UW] = uw;
-#ifdef DEBUG
-       logerror("(#%d) %d che fired up in %s",
-                sp->sct_own, che, ownxy(sp));
-#endif
     }
 }
 
@@ -165,6 +162,8 @@ guerrilla(struct sctstr *sp)
     while (NULL != (lp = nxtitemp(&ni))) {
        if (lp->lnd_own != sp->sct_own)
            continue;
+       if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
+           continue;
 
        mil += lp->lnd_item[I_MILIT];
 
@@ -211,7 +210,6 @@ guerrilla(struct sctstr *sp)
     }
 
     if (sp->sct_own != target) {
-       /*logerror("own %d != target %d", sp->sct_own, target); */
        move++;
        goto domove;
     }
@@ -225,7 +223,6 @@ guerrilla(struct sctstr *sp)
        recruit++;
        convert++;
     } else if (che > mil && mil > 0) {
-       /*logerror("guerrilla shootout with military"); */
        /*
         * shoot it out with the military, and kill them off.
         * If loyalty bad enough, then take the sector over,
@@ -246,13 +243,9 @@ guerrilla(struct sctstr *sp)
            if (n < 0)
                n = 0;
            sp->sct_loyal = n;
-           /*logerror("(#%d) mil beat che in %s", sp->sct_own, */
-           /*ownxy(sp)); */
        } else {
            convert++;
            recruit++;
-           /*logerror("(#%d) che beat mil in %s", sp->sct_own, */
-           /*ownxy(sp)); */
        }
        take_casualties(sp, mc);
     } else if (ratio < 5) {
@@ -260,7 +253,6 @@ guerrilla(struct sctstr *sp)
         * guerrillas have to resort to blowing things up.
         * Note this disrupts work in the sector.
         */
-       n = 0;
        n = (random() % 10) + (random() % che);
        if (n > 100)
            n = 100;
@@ -272,13 +264,9 @@ guerrilla(struct sctstr *sp)
           "Production %s disrupted by terrorists in %s\n",
           effadv(n), ownxy(sp));
        sect_damage(sp, n / 10);
-       /*logerror("(#%d) che blew up %s for %d", sp->sct_own, */
-       /*ownxy(sp), n); */
        recruit++;
     } else {
        /* ratio >= 5 */
-       /*logerror("(#%d) %d che fleeing %d mil in %s", sp->sct_own, */
-       /*che, mil, ownxy(sp)); */
        move++;
     }
     if (mil > 0 && che > 0) {
@@ -302,7 +290,6 @@ guerrilla(struct sctstr *sp)
            }
            take_casualties(sp, mc);
            recruit = 0;
-           /*logerror("Caught che; mc: %d, cc: %d", cc, mc); */
        }
     }
     if (convert && sp->sct_loyal >= 50) {
@@ -361,9 +348,9 @@ guerrilla(struct sctstr *sp)
        uw -= n;
        sp->sct_item[I_UW] = uw;
     }
-  domove:
+domove:
     if (move && che > 0) {
-       struct sctstr *nicest_sp = 0;
+       struct sctstr *nicest_sp = NULL;
        if (convert)
            min_mil = 999;
        else
@@ -394,7 +381,7 @@ guerrilla(struct sctstr *sp)
            min_mil = val;
        }
        /* if we found a nice sector, go there */
-       if (nicest_sp != 0) {
+       if (nicest_sp) {
            nicest_sp->sct_che += che;
            nicest_sp->sct_che_target = target;
            che = 0;
@@ -414,8 +401,7 @@ guerrilla(struct sctstr *sp)
        if (sp->sct_own == target)
            wu(0, target, "  body count: troops: %d, rebels: %d\n", mc, cc);
        else
-           wu(0, target,
-              "  rebels murder %d military\n", mc);
+           wu(0, target, "  rebels murder %d military\n", mc);
        nreport(actor, N_FREEDOM_FIGHT, victim, 1);
     }
     if (sp->sct_own != victim)
@@ -452,6 +438,10 @@ take_casualties(struct sctstr *sp, int mc)
      */
     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
     while (NULL != (lp = nxtitemp(&ni))) {
+       if (lp->lnd_own != sp->sct_own)
+           continue;
+       if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
+           continue;
        nunits++;
        if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
            nunits++;
@@ -465,6 +455,10 @@ take_casualties(struct sctstr *sp, int mc)
     /* kill some security troops */
     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
     while (NULL != (lp = nxtitemp(&ni))) {
+       if (lp->lnd_own != sp->sct_own)
+           continue;
+       if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
+           continue;
        if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
            continue;
 
@@ -490,6 +484,10 @@ take_casualties(struct sctstr *sp, int mc)
     /* kill some normal troops */
     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
     while (NULL != (lp = nxtitemp(&ni))) {
+       if (lp->lnd_own != sp->sct_own)
+           continue;
+       if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
+           continue;
        if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
            continue;
 
@@ -516,16 +514,15 @@ take_casualties(struct sctstr *sp, int mc)
     /* kill some normal troops */
     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
     while (NULL != (lp = nxtitemp(&ni))) {
+       if (lp->lnd_own != sp->sct_own)
+           continue;
+       if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
+           continue;
        if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
            continue;
 
        mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT];
-       lp->lnd_effic = 0;
-       lnd_submil(lp, 1000);   /* Remove 'em all */
-       wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
-          prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
-       makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
-       lp->lnd_own = 0;
+       lnd_dies_fighting_che(lp);
        if (mc <= 0)
            return;
     }
@@ -534,19 +531,69 @@ take_casualties(struct sctstr *sp, int mc)
     /* kill some security troops */
     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
     while (NULL != (lp = nxtitemp(&ni))) {
+       if (lp->lnd_own != sp->sct_own)
+           continue;
+       if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
+           continue;
        if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
            continue;
 
        mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT] * 2.0;
-       lp->lnd_effic = 0;
-       lnd_submil(lp, 1000);   /* Kill 'em all */
-       wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
-          prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
-       makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
-       lp->lnd_own = 0;
+       lnd_dies_fighting_che(lp);
        if (mc <= 0)
            return;
     }
 
     /* Hmm.. everyone dead.. too bad */
 }
+
+static void
+lnd_dies_fighting_che(struct lndstr *lp)
+{
+    int i, j;
+    struct lndstr *clp;
+    struct plnstr *cpp;
+    struct nukstr *cnp;
+
+    lp->lnd_effic = 0;
+    lnd_submil(lp, 1000);      /* Remove 'em all */
+    wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
+       prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
+    makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
+    lp->lnd_own = 0;
+
+    /* Unload lp's land unit cargo */
+    for (i = lnd_first_on_land(lp); i >= 0; i = lnd_next_on_unit(i)) {
+       clp = getlandp(i);
+       if (CANT_HAPPEN(!clp))
+           continue;
+       lnd_carrier_change(clp, EF_LAND, clp->lnd_land, -1);
+       clp->lnd_land = -1;
+    }
+
+    /* Destroy lp's plane cargo */
+    for (i = pln_first_on_land(lp); i >= 0; i = pln_next_on_unit(i)) {
+       cpp = getplanep(i);
+       if (CANT_HAPPEN(!cpp))
+           continue;
+       pln_carrier_change(cpp, EF_LAND, cpp->pln_land, -1);
+       makelost(EF_PLANE, cpp->pln_own, i, cpp->pln_x, cpp->pln_y);
+       wu(0, cpp->pln_own, "%s lost!\n", prplane(cpp));
+       cpp->pln_own = 0;
+       cpp->pln_effic = 0;
+       cpp->pln_land = -1;
+
+       j = nuk_on_plane(cpp);
+       if (j >= 0) {
+           cnp = getnukep(j);
+           if (CANT_HAPPEN(!cnp))
+               continue;
+           nuk_carrier_change(cnp, EF_PLANE, cnp->nuk_plane, -1);
+           makelost(EF_NUKE, cnp->nuk_own, j, cnp->nuk_x, cnp->nuk_y);
+           wu(0, cnp->nuk_own, "%s lost!\n", prnuke(cnp));
+           cnp->nuk_own = 0;
+           cnp->nuk_effic = 0;
+           cnp->nuk_plane = -1;
+       }
+    }
+}