/*
* 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
#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
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;
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)
/* 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;
take_casualties(struct sctstr *sp, int mc)
{
int orig_mil, taken;
- int cantake;
- int nunits = 0, each, deq;
+ int nunits = 0, each;
struct lndstr *lp;
struct nstr_item ni;
}
sp->sct_item[I_MILIT] = 0;
- /* remaining casualites */
- mc -= orig_mil;
- taken = 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))) {
continue;
if (lp->lnd_ship >= 0)
continue;
+ if (!lp->lnd_item[I_MILIT])
+ continue;
nunits++;
}
- if (nunits == 0)
- return taken;
+ 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)
- 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 -= each;
- } else if (cantake > 0) {
- 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_item[I_MILIT] * (deq / 100.0);
- taken += MIN(deq, lp->lnd_item[I_MILIT]);
- lnd_submil(lp, deq);
- if (mc <= 0)
- 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];
-
- 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;
-
- lp->lnd_effic -= deq;
- lp->lnd_mobil -= deq / 2;
- deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
- taken += MIN(deq, lp->lnd_item[I_MILIT]);
- lnd_submil(lp, deq);
- if (mc <= 0)
- 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;
-
- mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT];
- taken += lp->lnd_item[I_MILIT];
- lnd_dies_fighting_che(lp);
- if (mc <= 0)
- 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;
+
+ 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];
- taken += lp->lnd_item[I_MILIT];
- lnd_dies_fighting_che(lp);
- if (mc <= 0)
- return taken;
+ 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;
}