]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/revolt.c
Update copyright notice
[empserver] / src / lib / update / revolt.c
index 822679fc75a499c68343678e17661344fe5b84a6..45768f753bf4333e655c781fda0cb851179be722 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2016, 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
@@ -48,6 +48,7 @@
 #include "update.h"
 
 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
@@ -128,10 +129,10 @@ guerrilla(struct sctstr *sp)
     struct sctstr *nsp;
     int recruit;
     int move;
-    int ratio;
+    double ratio;
     int che;
     int mil;
-    int security_bonus;
+    double security_bonus;
     int cc, mc;
     double odds;
     int civ;
@@ -181,7 +182,7 @@ guerrilla(struct sctstr *sp)
        if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
            int che_kill, r;
 
-           security_bonus += lp->lnd_item[I_MILIT] * 3;
+           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)
@@ -303,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;
@@ -418,8 +417,7 @@ static int
 take_casualties(struct sctstr *sp, int mc)
 {
     int orig_mil, taken;
-    int cantake;
-    int nunits = 0, each, deq, dam;
+    int nunits = 0, each;
     struct lndstr *lp;
     struct nstr_item ni;
 
@@ -445,6 +443,8 @@ take_casualties(struct sctstr *sp, int mc)
            continue;
        if (lp->lnd_ship >= 0)
            continue;
+       if (!lp->lnd_item[I_MILIT])
+           continue;
        nunits++;
     }
 
@@ -455,90 +455,64 @@ take_casualties(struct sctstr *sp, int mc)
     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)
-           continue;
-       if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
-           continue;
-
-       cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
-       cantake = MIN(lp->lnd_item[I_MILIT], cantake);
-       deq = MIN(cantake, MIN(each, mc - taken));
-       if (deq <= 0)
-           continue;
-
-       taken += deq;
-       dam = ((double)deq / lp->lnd_item[I_MILIT]) * 100.0;
-       lp->lnd_effic -= dam;
-       lp->lnd_mobil -= dam / 2;
-       lnd_submil(lp, deq);
-       if (taken >= mc)
-           return taken;
-    }
+    taken += take_casualties_from_lands(sp, MIN(each, mc - taken), 1, 0);
 
     /* 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)
-           continue;
-       if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
-           continue;
-
-       cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
-       cantake = MIN(lp->lnd_item[I_MILIT], cantake);
-       deq = MIN(cantake, MIN(each, mc - taken));
-       if (deq <= 0)
-           continue;
-
-       taken += deq;
-       dam = ((double)deq / lp->lnd_item[I_MILIT]) * 100.0;
-       lp->lnd_effic -= dam;
-       lp->lnd_mobil -= dam / 2;
-       lnd_submil(lp, deq);
-       if (taken >= mc)
-           return taken;
-    }
+    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)
-           continue;
-       if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
-           continue;
-
-       taken += lp->lnd_item[I_MILIT];
-       lnd_dies_fighting_che(lp);
-       if (taken >= mc)
-           return taken;
-    }
+    taken += take_casualties_from_lands(sp, MIN(each, mc - taken), 0, 1);
 
     /* Hmm.. still some left.. kill off units now */
     /* kill some security troops */
+    taken += take_casualties_from_lands(sp, MIN(each, mc - taken), 1, 1);
+
+    CANT_HAPPEN(taken < mc);
+    return taken;
+}
+
+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;
+
+    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)
            continue;
-       if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
+       if (!lp->lnd_item[I_MILIT])
+           continue;
+       if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY) == !!security)
            continue;
 
-       taken += lp->lnd_item[I_MILIT];
-       lnd_dies_fighting_che(lp);
-       if (taken >= mc)
-           return taken;
+       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;
+
+       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);
+       }
     }
 
-    CANT_REACH();
     return taken;
 }