/*
* 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;
- double eff_per_cas;
/* casualties come out of mil first */
orig_mil = sp->sct_item[I_MILIT];
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 (!lp->lnd_item[I_MILIT])
- continue;
- if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
- continue;
-
- eff_per_cas = 100.0 / lchr[lp->lnd_type].l_item[I_MILIT];
- cantake = MIN(lp->lnd_item[I_MILIT],
- (int)((lp->lnd_effic - 40) / eff_per_cas));
- deq = MIN(cantake, MIN(each, mc - taken));
- if (deq <= 0)
- continue;
+ taken += take_casualties_from_lands(sp, MIN(each, mc - taken), 1, 0);
- taken += deq;
- lp->lnd_effic -= deq * eff_per_cas;
- lp->lnd_mobil -= deq * eff_per_cas / 2;
- lnd_submil(lp, deq);
- if (taken >= mc)
- return taken;
- }
+ /* 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 */
+ 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 (!lp->lnd_item[I_MILIT])
continue;
- if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
+ 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 = MIN(lp->lnd_item[I_MILIT],
- (int)((lp->lnd_effic - 40) / eff_per_cas));
- deq = MIN(cantake, MIN(each, mc - taken));
+ 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;
lp->lnd_effic -= deq * eff_per_cas;
lp->lnd_mobil -= deq * eff_per_cas / 2;
lnd_submil(lp, deq);
- if (taken >= mc)
- return taken;
- }
-
- /* 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 (!lp->lnd_item[I_MILIT])
- 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;
- }
-
- /* 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)
- continue;
- if (lp->lnd_ship >= 0)
- continue;
- if (!lp->lnd_item[I_MILIT])
- 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;
+ 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;
}