]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/revolt.c
Update copyright notice
[empserver] / src / lib / update / revolt.c
index dbf3d60a54c6614f26ffc3b2ca4a0882f6d04960..45768f753bf4333e655c781fda0cb851179be722 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2017, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Steve McClure, 1997-2000
+ *     Markus Armbruster, 2004-2016
  */
 
 #include <config.h>
 
+#include "chance.h"
 #include "land.h"
 #include "lost.h"
+#include "nat.h"
 #include "news.h"
 #include "nsc.h"
+#include "nuke.h"
 #include "path.h"
 #include "plane.h"
+#include "prototypes.h"
+#include "sect.h"
 #include "update.h"
 
-static void take_casualties(struct sctstr *, int);
+static int take_casualties(struct sctstr *, int);
+static int take_casualties_from_lands(struct sctstr *, int, int, int);
 static void lnd_dies_fighting_che(struct lndstr *);
 
 void
@@ -63,7 +70,7 @@ revolt(struct sctstr *sp)
        return;
     che_uw = 0;
     /* che due to civilian unrest */
-    n = 10 - (random() % 20);
+    n = 10 - roll0(20);
     che_civ = 3 + (civ * n / 500);
     if (che_civ < 0)
        che_civ = 0;
@@ -74,7 +81,7 @@ revolt(struct sctstr *sp)
     che += che_civ;
     if (che < CHE_MAX) {
        /* che due to uw unrest */
-       n = 10 + (random() % 30);
+       n = 9 + roll(30);
        che_uw = 5 + (uw * n / 500);
        if (che_uw > uw)
            che_uw = uw;
@@ -122,9 +129,10 @@ guerrilla(struct sctstr *sp)
     struct sctstr *nsp;
     int recruit;
     int move;
-    int ratio;
+    double ratio;
     int che;
     int mil;
+    double security_bonus;
     int cc, mc;
     double odds;
     int civ;
@@ -154,13 +162,14 @@ guerrilla(struct sctstr *sp)
     actor = sp->sct_oldown;
     che = sp->sct_che;
     mil = sp->sct_item[I_MILIT];
+    security_bonus = 0;
 
     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)
+       if (lp->lnd_ship >= 0)
            continue;
 
        mil += lp->lnd_item[I_MILIT];
@@ -173,7 +182,7 @@ guerrilla(struct sctstr *sp)
        if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
            int che_kill, r;
 
-           mil += lp->lnd_item[I_MILIT];
+           security_bonus += 3 * lp->lnd_item[I_MILIT] * lp->lnd_effic / 100.0;
            r = (lp->lnd_item[I_MILIT] * lp->lnd_effic) / 500;
            che_kill = r < 1 ? 0 : roll(r);
            if (che_kill > che)
@@ -199,7 +208,7 @@ guerrilla(struct sctstr *sp)
        return;
     tnat = getnatp(target);
     if (tnat->nat_stat == STAT_UNUSED) {
-       /* target nation has dissolved: che's retire.  */
+       /* target nation has dissolved: che's retire. */
        logerror("%d Che targeted at country %d retiring", che, target);
        sp->sct_che = 0;
        sp->sct_che_target = 0;
@@ -212,8 +221,8 @@ guerrilla(struct sctstr *sp)
        goto domove;
     }
 
-    ratio = mil / che;
-    odds = (double)che / (mil + che);
+    ratio = (mil + security_bonus) / che;
+    odds = (double)che / (mil + security_bonus + che);
     odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
     if (mil == 0) {
        wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
@@ -226,18 +235,15 @@ guerrilla(struct sctstr *sp)
         * If loyalty bad enough, then take the sector over,
         * and enlist 5% of civ as military force.
         */
-       while (che > 0 && mil > 0) {
-           if (chance(odds)) {
+       while (che > cc && mil > mc) {
+           if (chance(odds))
                mc++;
-               mil--;
-           } else {
+           else
                cc++;
-               che--;
-           }
        }
-       if (mil > 0) {
+       if (mil > mc) {
            /* military won.  */
-           n = sp->sct_loyal - (random() % 15);
+           n = sp->sct_loyal - roll0(15);
            if (n < 0)
                n = 0;
            sp->sct_loyal = n;
@@ -245,13 +251,15 @@ guerrilla(struct sctstr *sp)
            convert++;
            recruit++;
        }
-       take_casualties(sp, mc);
+       mc = take_casualties(sp, mc);
+       che -= cc;
+       mil -= mc;
     } else if (ratio < 5) {
        /*
         * guerrillas have to resort to blowing things up.
         * Note this disrupts work in the sector.
         */
-       n = (random() % 10) + (random() % che);
+       n = roll0(10) + roll0(che);
        if (n > 100)
            n = 100;
        tmp = sp->sct_work - n;
@@ -275,18 +283,17 @@ guerrilla(struct sctstr *sp)
         */
        if (chance(ratio * 0.10)) {
            n = (mil / 5) + 1;
-           odds = (double)che / (n + che);
+           odds = (double)che / (n + security_bonus / 5 + che);
            odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
-           while (che > 0 && n > 0) {
-               if (chance(odds)) {
+           while (che > cc && n > mc) {
+               if (chance(odds))
                    mc++;
-                   n--;
-               } else {
+               else
                    cc++;
-                   che--;
-               }
            }
-           take_casualties(sp, mc);
+           mc = take_casualties(sp, mc);
+           che -= cc;
+           mil -= mc;
            recruit = 0;
        }
     }
@@ -297,11 +304,9 @@ guerrilla(struct sctstr *sp)
        /* che won, and sector converts. */
        if (sp->sct_own == sp->sct_oldown)
            sp->sct_oldown = 0;
-       else {
-           lost_and_found(EF_SECTOR, sp->sct_own, sp->sct_oldown,
-                          0, sp->sct_x, sp->sct_y);
-           takeover(sp, sp->sct_oldown);
-       }
+       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;
@@ -324,14 +329,14 @@ guerrilla(struct sctstr *sp)
        /* loyalty drops during recruitment efforts */
        n = sp->sct_loyal;
        if (n < 30)
-           n += (random() % 5) + 1;
+           n += roll(5);
        else if (n < 70)
-           n += (random() % 10) + 4;
+           n += roll(10) + 3;
        if (n > 127)
            n = 127;
        sp->sct_loyal = n;
        if (sp->sct_oldown != sp->sct_own || n > 100) {
-           n = civ * (random() % 3) / 200;
+           n = civ * roll0(3) / 200;
            n /= hap_fact(tnat, getnatp(sp->sct_oldown));
            if (n + che > CHE_MAX)
                n = CHE_MAX - che;
@@ -339,7 +344,7 @@ guerrilla(struct sctstr *sp)
            civ -= n;
            sp->sct_item[I_CIVIL] = civ;
        }
-       n = uw * (random() % 3) / 200;
+       n = uw * roll0(3) / 200;
        if (n + che > CHE_MAX)
            n = CHE_MAX - che;
        che += n;
@@ -354,6 +359,7 @@ domove:
        else
            min_mil = mil;
        /* search adjacent sectors for a nice one */
+       /* TODO consider land units in addition to mil */
        for (n = 1; n <= 6; n++) {
            nsp = getsectp(sp->sct_x + diroff[n][0],
                           sp->sct_y + diroff[n][1]);
@@ -372,7 +378,7 @@ domove:
            val = roundintby(val, 10);
            /* inject a modicum of indeterminism; also
             * avoids che preferring certain directions */
-           val += random() % 10 - 5;
+           val += roll(10) - 6;
            if (val >= min_mil)
                continue;
            nicest_sp = nsp;
@@ -407,12 +413,11 @@ domove:
           xyas(sp->sct_x, sp->sct_y, victim));
 }
 
-static void
+static int
 take_casualties(struct sctstr *sp, int mc)
 {
-    int orig_mil;
-    int cantake;
-    int nunits = 0, each, deq;
+    int orig_mil, taken;
+    int nunits = 0, each;
     struct lndstr *lp;
     struct nstr_item ni;
 
@@ -421,137 +426,149 @@ take_casualties(struct sctstr *sp, int mc)
 
     if (mc <= orig_mil) {
        sp->sct_item[I_MILIT] = orig_mil - mc;
-       return;
+       return mc;
     }
     sp->sct_item[I_MILIT] = 0;
 
-    /* remaining casualites */
-    mc -= orig_mil;
-
     /*
      * Need to take total_casualties and divide
      * them amongst the land units in the sector
      * Do security troops first, then others.
      * Try not to kill any unit.
+     * TODO Spread proportionally to mil instead of evenly
      */
     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)
+       if (lp->lnd_ship >= 0)
+           continue;
+       if (!lp->lnd_item[I_MILIT])
            continue;
        nunits++;
-       if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
-           nunits++;
     }
 
-    if (nunits == 0)
-       return;
+    if (CANT_HAPPEN(!nunits))
+       return orig_mil;
 
-    each = (mc / nunits) + 2;
+    taken = orig_mil;
+    each = (mc - taken) / nunits + 2;
 
     /* 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) * lp->lnd_item[I_MILIT];
-
-       if (cantake >= each) {
-           deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
-           mc -= 2 * each;
-       } else if (cantake > 0) {
-           deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
-           mc -= 2 * cantake;
-       } else
-           deq = 0;
+    taken += take_casualties_from_lands(sp, MIN(each, mc - taken), 1, 0);
 
-       lp->lnd_effic -= deq;
-       lp->lnd_mobil -= deq / 2;
-       deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
-       lnd_submil(lp, deq);
-       if (mc <= 0)
-           return;
-    }
+    /* kill some normal troops */
+    taken += take_casualties_from_lands(sp, MIN(each, mc - taken), 0, 0);
 
+    /* Hmm.. still some left.. kill off units now */
     /* 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;
+    taken += take_casualties_from_lands(sp, MIN(each, mc - taken), 0, 1);
 
-       cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
+    /* Hmm.. still some left.. kill off units now */
+    /* kill some security troops */
+    taken += take_casualties_from_lands(sp, MIN(each, mc - taken), 1, 1);
 
-       if (cantake >= each) {
-           deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
-           mc -= each;
-       } else if (cantake > 0) {
-           deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
-           mc -= cantake;
-       } else
-           deq = 0;
+    CANT_HAPPEN(taken < mc);
+    return taken;
+}
 
-       lp->lnd_effic -= deq;
-       lp->lnd_mobil -= deq / 2;
-       deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
-       lnd_submil(lp, deq);
-       if (mc <= 0)
-           return;
-    }
+int
+take_casualties_from_lands(struct sctstr *sp, int cas,
+                          int security, int may_kill)
+{
+    struct nstr_item ni;
+    struct lndstr *lp;
+    double eff_per_cas;
+    int cantake, deq, taken;
 
-    /* Hmm.. still some left.. kill off units now */
-    /* kill some normal troops */
+    taken = 0;
     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
-    while (NULL != (lp = nxtitemp(&ni))) {
+    while (taken < cas && (lp = nxtitemp(&ni))) {
        if (lp->lnd_own != sp->sct_own)
            continue;
-       if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
+       if (lp->lnd_ship >= 0)
            continue;
-       if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
-           continue;
-
-       mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT];
-       lnd_dies_fighting_che(lp);
-       if (mc <= 0)
-           return;
-    }
-
-    /* Hmm.. still some left.. kill off units now */
-    /* 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)
+       if (!lp->lnd_item[I_MILIT])
            continue;
-       if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
+       if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY) == !!security)
            continue;
-       if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
+
+       eff_per_cas = 100.0 / lchr[lp->lnd_type].l_item[I_MILIT];
+       cantake = lp->lnd_item[I_MILIT];
+       if (!may_kill)
+           cantake = MIN(cantake,
+                         (int)((lp->lnd_effic - 40) / eff_per_cas));
+       deq = MIN(cantake, cas - taken);
+       if (deq <= 0)
            continue;
 
-       mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT] * 2.0;
-       lnd_dies_fighting_che(lp);
-       if (mc <= 0)
-           return;
+       taken += deq;
+       lp->lnd_effic -= deq * eff_per_cas;
+       lp->lnd_mobil -= deq * eff_per_cas / 2;
+       lnd_submil(lp, deq);
+       if (lp->lnd_effic < LAND_MINEFF) {
+           CANT_HAPPEN(!may_kill);
+           taken += lp->lnd_item[I_MILIT];
+           lnd_dies_fighting_che(lp);
+       }
     }
 
-    /* Hmm.. everyone dead.. too bad */
+    return taken;
 }
 
 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;
+
+    /* Take dead lp off its carrier */
+    if (lp->lnd_land >= 0) {
+       lnd_carrier_change(lp, EF_LAND, lp->lnd_land, -1);
+       lp->lnd_land = -1;
+    }
+
+    /* 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;
+       }
+    }
 }