]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/revolt.c
License upgrade to GPL version 3 or later
[empserver] / src / lib / update / revolt.c
index a76db9cc8cebdcb458710340c384d28f0f1400d2..95acd20e5ec8d8d32c885fb378aa874571e07757 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2006, 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/>.
  *
  *  ---
  *
@@ -26,7 +25,7 @@
  *  ---
  *
  *  revolt.c: Have disloyal populace revolt!
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Steve McClure, 1997-2000
 
 #include <config.h>
 
-#include "misc.h"
-#include "sect.h"
-#include "nat.h"
-#include "news.h"
-#include "file.h"
-#include "path.h"
-#include "xy.h"
 #include "land.h"
+#include "lost.h"
+#include "news.h"
 #include "nsc.h"
+#include "path.h"
 #include "plane.h"
 #include "update.h"
-#include "common.h"
-#include "gen.h"
-#include "lost.h"
-#include "subs.h"
 
 static void take_casualties(struct sctstr *, int);
 
@@ -70,7 +61,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);
@@ -100,10 +90,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
     }
 }
 
@@ -173,16 +159,21 @@ 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 += lnd_getmil(lp);
+       mil += lp->lnd_item[I_MILIT];
 
-       /* Security troops can now kill up to 1/2 their complement each
+       if (sp->sct_che_target != sp->sct_own)
+           continue;
+
+       /* Security troops can now kill up to 1/5 their complement each
           update, before doing anything else. */
        if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
            int che_kill, r;
 
-           mil += lnd_getmil(lp);
-           r = (lnd_getmil(lp) * lp->lnd_effic) / 500;
+           mil += lp->lnd_item[I_MILIT];
+           r = (lp->lnd_item[I_MILIT] * lp->lnd_effic) / 500;
            che_kill = r < 1 ? 0 : roll(r);
            if (che_kill > che)
                che_kill = che;
@@ -203,10 +194,8 @@ guerrilla(struct sctstr *sp)
     }
 
     target = sp->sct_che_target;
-    if (target == 0) {
-       /* the deity can't be a target! */
+    if (CANT_HAPPEN(target == 0))
        return;
-    }
     tnat = getnatp(target);
     if (tnat->nat_stat == STAT_UNUSED) {
        /* target nation has dissolved: che's retire.  */
@@ -218,7 +207,6 @@ guerrilla(struct sctstr *sp)
     }
 
     if (sp->sct_own != target) {
-       /*logerror("own %d != target %d", sp->sct_own, target); */
        move++;
        goto domove;
     }
@@ -232,7 +220,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,
@@ -253,13 +240,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) {
@@ -267,7 +250,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;
@@ -278,14 +260,10 @@ guerrilla(struct sctstr *sp)
        wu(0, sp->sct_own,
           "Production %s disrupted by terrorists in %s\n",
           effadv(n), ownxy(sp));
-       sect_damage(sp, n / 10, 0);
-       /*logerror("(#%d) che blew up %s for %d", sp->sct_own, */
-       /*ownxy(sp), n); */
+       sect_damage(sp, n / 10);
        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) {
@@ -296,11 +274,6 @@ guerrilla(struct sctstr *sp)
         */
        if (chance(ratio * 0.10)) {
            n = (mil / 5) + 1;
-           if ((n + che) == 0) {
-               logerror("n=%d che=%d\n", n, che);
-               if (che == 0)
-                   return;
-           }
            odds = (double)che / (n + che);
            odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
            while (che > 0 && n > 0) {
@@ -314,7 +287,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) {
@@ -324,8 +296,11 @@ guerrilla(struct sctstr *sp)
        /* che won, and sector converts. */
        if (sp->sct_own == sp->sct_oldown)
            sp->sct_oldown = 0;
-       else
+       else {
+           lost_and_found(EF_SECTOR, sp->sct_own, sp->sct_oldown,
+                          0, sp->sct_x, sp->sct_y);
            takeover(sp, sp->sct_oldown);
+       }
        sp->sct_mobil = oldmob;
        civ += uw;
        uw = 0;
@@ -370,9 +345,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
@@ -381,7 +356,7 @@ guerrilla(struct sctstr *sp)
        for (n = 1; n <= 6; n++) {
            nsp = getsectp(sp->sct_x + diroff[n][0],
                           sp->sct_y + diroff[n][1]);
-           if (dchr[nsp->sct_type].d_mcst == 0)
+           if (dchr[nsp->sct_type].d_mob0 < 0)
                continue;
            if (nsp->sct_own != target)
                continue;
@@ -403,7 +378,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;
@@ -423,8 +398,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)
@@ -461,6 +435,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++;
@@ -474,23 +452,27 @@ 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;
 
-       cantake = ((lp->lnd_effic - 40) / 100.0) * lnd_getmil(lp) * 2.0;
+       cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
 
        if (cantake >= each) {
-           deq = ((double)each / (lnd_getmil(lp) * 2.0)) * 100.0;
-           mc -= each;
+           deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
+           mc -= 2 * each;
        } else if (cantake > 0) {
-           deq = ((double)cantake / (lnd_getmil(lp) * 2.0)) * 100.0;
-           mc -= (deq / 100.0) * lnd_getmil(lp) * 2.0;
+           deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
+           mc -= 2 * cantake;
        } else
            deq = 0;
 
        lp->lnd_effic -= deq;
        lp->lnd_mobil -= deq / 2;
-       deq = lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
+       deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
        lnd_submil(lp, deq);
        if (mc <= 0)
            return;
@@ -499,23 +481,27 @@ 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;
 
-       cantake = ((lp->lnd_effic - 40) / 100.0) * lnd_getmil(lp);
+       cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
 
        if (cantake >= each) {
-           deq = ((double)each / (lnd_getmil(lp) * 2.0)) * 100.0;
+           deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
            mc -= each;
        } else if (cantake > 0) {
-           deq = ((double)cantake / lnd_getmil(lp)) * 100.0;
-           mc -= (deq / 100.0) * lnd_getmil(lp);
+           deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
+           mc -= cantake;
        } else
            deq = 0;
 
        lp->lnd_effic -= deq;
        lp->lnd_mobil -= deq / 2;
-       deq = lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
+       deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
        lnd_submil(lp, deq);
        if (mc <= 0)
            return;
@@ -525,10 +511,14 @@ 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) * lnd_getmil(lp);
+       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",
@@ -543,10 +533,14 @@ 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) * lnd_getmil(lp) * 2.0;
+       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",