(feed_ship, feed_land): Use it.
(do_feed): Use it. Estimate of food needed was one too large for
integer food needs. Used to round fractional food need to nearest
instead of up for supply_commod(), which could cause starvation.
(s_commod, get_minimum): Use it. Estimate of food needed was one too
large for integer food needs. s_commod() used to reserve one more
than get_minimum() would have returned; it's now the same.
(famine_victims): New.
(feed_people): Use it. This rounds victim fractions down instead of
up. It also dosn't flush needs <=1 to zero. Doesn't change
starvation, as do_feed() always produces at least one emergency food.
Does change food consumption.
(starve_some): New.
(feed_people): Use it.
(feed_ship): Use feed_people(). This rounds victim fractions down
instead of up.
(feed_land): Use feed_people(). Rounding of victim fractions
unchanged. Feeds all people not just mil; closes #913997.
(starv_people): New.
(starv_sects, starv_ships, starv_units): Use it. Fixes starve land to
talk about people instead of mil.
(starv_sects): Use famine_victims() rather than feed_people(). Take
emergency food into account, because feed_people() doesn't. Don't aim
for one extra food, for consistency with starv_ships() and
starv_units().
(feed_people): Remove useless parameter. Simplify.
(starv_ships, starv_ships): Use famine_victims() rather than
feed_ship() and feed_land().
(feed_ship, feed_land): Remove useless parameters. Internal linkage.
Simplify.
(feed_land): Call resupply_commod() only if there's a food shortage.
Don't scrounge lnd_ship for food, resupply_commod() already does.
extern int new_work(struct sctstr *, int);
extern int do_feed(struct sctstr *, struct natstr *,
short *, int *, int *, int);
-extern int feed_people(short *, int, int *);
+extern int feed_people(short *, int);
+extern double food_needed(short *, int);
+extern int famine_victims(short *, int);
/* land.c */
extern int prod_land(int, int, int *, int);
-extern int feed_land(struct lndstr *, int, int *, int);
/* main.c */
/* in server.h */
/* material.c */
long (*p_sect)[2], int sector_type);
/* ship.c */
extern int prod_ship(int, int, int *, int);
-extern int feed_ship(struct shpstr *, int, int *, int);
/*
* src/server
#include <config.h>
+#include <math.h>
#include "misc.h"
#include "player.h"
#include "xy.h"
return RET_OK;
}
+static void
+starv_people(short *vec, int victims)
+{
+ pr(" will starve %d people. %.0f more food needed\n", victims,
+ ceil(food_needed(vec, etu_per_update) - vec[I_FOOD]));
+}
+
static void
starv_sects(char *range)
{
struct nstr_sect nstr;
struct sctstr sect;
int nsect = 0;
- int s, needed;
+ int s;
if (!snxtsct(&nstr, range))
return;
if (sect.sct_type == SCT_SANCT)
continue;
-/* This next 2 lines were added to overcompensate for the needy */
- if (sect.sct_item[I_FOOD])
- sect.sct_item[I_FOOD]--;
- s = feed_people(sect.sct_item, etu_per_update, &needed);
+ if (sect.sct_item[I_FOOD] == 0)
+ sect.sct_item[I_FOOD] = 1; /* see growfood() */
+ s = famine_victims(sect.sct_item, etu_per_update);
if (s == 0)
continue;
else
pr(" ");
pr("%4d%%", sect.sct_effic);
- pr(" will starve %d people. %d more food needed\n", s, needed);
+ starv_people(sect.sct_item, s);
}
if (nsect == 0) {
if (player->argp[1])
struct nstr_item ni;
struct shpstr ship;
int nship = 0;
- int s, needed;
+ int s;
if (!snxtitem(&ni, EF_SHIP, range))
return;
if (!player->owner || !ship.shp_own)
continue;
- s = feed_ship(&ship, etu_per_update, &needed, 0);
+ s = famine_victims(ship.shp_item, etu_per_update);
if (s == 0)
continue;
if (nship++ == 0)
pr("%3d ", ship.shp_own);
pr("%5d ", ship.shp_uid);
pr("%-16.16s ", mchr[(int)ship.shp_type].m_name);
- pr(" will starve %d people. %d more food needed\n", s, needed);
+ starv_people(ship.shp_item, s);
}
if (nship == 0) {
if (range)
if (!player->owner || !land.lnd_own)
continue;
- s = feed_land(&land, etu_per_update, &needed, 0);
+ s = famine_victims(land.lnd_item, etu_per_update);
if (s == 0)
continue;
if (nunit++ == 0)
pr("%3d ", land.lnd_own);
pr("%5d ", land.lnd_uid);
pr("%-16.16s ", lchr[(int)land.lnd_type].l_name);
- pr(" will starve %d mil. %d more food needed\n", s, needed);
+ starv_people(land.lnd_item, s);
}
if (nunit == 0) {
if (range)
#include <config.h>
+#include <math.h>
#include "misc.h"
#include "nat.h"
#include "ship.h"
continue;
if (!BestLandPath(buf, &dest, §, &move_cost, MOB_ROAD))
continue;
- if (!opt_NOFOOD && type == I_FOOD) {
- minimum = 2 + ((etu_per_update * eatrate)
- * (sect.sct_item[I_CIVIL] + sect.sct_item[I_MILIT]
- + sect.sct_item[I_UW]));
- }
+ if (!opt_NOFOOD && type == I_FOOD)
+ minimum = 1 + (int)ceil(food_needed(sect.sct_item,
+ etu_per_update));
if (sect.sct_item[type] <= minimum) {
/* Don't bother... */
continue;
if (!BestLandPath(buf, &dest, §, &move_cost, MOB_ROAD))
continue;
if (!opt_NOFOOD && type == I_FOOD)
- minimum = 2 + ((etu_per_update * eatrate)
- * (ship.shp_item[I_CIVIL] + ship.shp_item[I_MILIT]
- + ship.shp_item[I_UW]));
+ minimum = 1 + (int)ceil(food_needed(ship.shp_item,
+ etu_per_update));
if (ship.shp_item[type] <= minimum) {
/* Don't bother... */
continue;
case I_FOOD:
if (opt_NOFOOD)
return 0; /* no food reqd, get out */
- want = (double)etu_per_update * eatrate * lp->lnd_item[I_MILIT] + 1;
+ want = (int)ceil(food_needed(lp->lnd_item, etu_per_update));
break;
case I_SHELL:
want = lp->lnd_ammo;
#include <config.h>
+#include <math.h>
#include "misc.h"
#include "sect.h"
#include "nat.h"
#include "gen.h"
#include "subs.h"
-static int grow_people(struct sctstr *, int,
- struct natstr *, int *, int,
+static int growfood(struct sctstr *, short *, int, int);
+static int starve_some(short *, i_type, int);
+static void trunc_people(struct sctstr *, struct natstr *, short *);
+static int grow_people(struct sctstr *, int, struct natstr *, int *, int,
short *);
static int babies(int, int, double, int, int);
-static int growfood(struct sctstr *, short *, int, int);
-static void trunc_people(struct sctstr *, struct natstr *,
- short *);
/*
* feed the individual sector
- *
*/
int
do_feed(struct sctstr *sp, struct natstr *np, short *vec,
int *workp, int *bp, int etu)
{
- int people;
int work_avail;
int starved, sctwork;
int needed;
vec[I_CIVIL], vec[I_MILIT], vec[I_UW],
maxpop));
- people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
if (sp->sct_type != SCT_SANCT) {
if (opt_NOFOOD == 0) {
- if (vec[I_FOOD] < 1 + etu * people * eatrate) {
+ needed = (int)ceil(food_needed(vec, etu));
+ if (vec[I_FOOD] < needed) {
/* need to grow "emergency rations" */
- work_avail -= (2 *
- growfood(sp, vec, (int)(work_avail / 2),
- etu));
+ work_avail -= 2 * growfood(sp, vec, work_avail / 2, etu);
/* It's twice as hard to grow those than norm */
pt_bg_nmbr(bp, sp, I_MAX + 1, work_avail);
if (!player->simulation)
sp->sct_avail = work_avail;
}
- if ((vec[I_FOOD] < 1 + etu * people * eatrate) &&
- (sp->sct_own == sp->sct_oldown)) {
-
+ if (vec[I_FOOD] < needed && sp->sct_own == sp->sct_oldown) {
/* steal food from warehouses, headquarters,
supply ships in port, or supply units */
- int needed;
-
- needed = ldround(1.0 + etu * people * eatrate, 1);
-
- /* Now, find some food */
vec[I_FOOD] = supply_commod(sp->sct_own,
sp->sct_x, sp->sct_y,
I_FOOD, needed);
}
}
- starved = feed_people(vec, etu, &needed);
+ starved = feed_people(vec, etu);
if (starved > 0) {
if (!player->simulation) {
/* don't report POGO starvation */
* returns the number who starved, if any.
*/
int
-feed_people(short *vec, int etu, int *needed)
+feed_people(short *vec, int etu)
{
- double food_eaten;
- int ifood_eaten;
- int can_eat;
- int total_people;
- int to_starve;
- int starved;
+ int to_starve, starved;
if (opt_NOFOOD)
return 0;
- total_people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
- food_eaten = etu * eatrate * total_people;
- ifood_eaten = (int)food_eaten;
- if (food_eaten - ifood_eaten > 0)
- ifood_eaten++;
- if (ifood_eaten <= 1)
- return 0;
- starved = 0;
- *needed = 0;
- if (ifood_eaten > vec[I_FOOD]) {
- *needed = ifood_eaten - vec[I_FOOD];
- can_eat = vec[I_FOOD] / (etu * eatrate);
- /* only want to starve off at most 1/2 the populace. */
- if (can_eat < total_people / 2)
- can_eat = total_people / 2;
-
- to_starve = total_people - can_eat;
- while (to_starve && vec[I_UW]) {
- to_starve--;
- starved++;
- vec[I_UW]--;
- }
- while (to_starve && vec[I_CIVIL]) {
- to_starve--;
- starved++;
- vec[I_CIVIL]--;
- }
- while (to_starve && vec[I_MILIT]) {
- to_starve--;
- starved++;
- vec[I_MILIT]--;
- }
-
+ to_starve = famine_victims(vec, etu);
+ starved = starve_some(vec, I_UW, to_starve);
+ starved += starve_some(vec, I_CIVIL, to_starve - starved);
+ starved += starve_some(vec, I_MILIT, to_starve - starved);
+ vec[I_FOOD] -= roundavg(food_needed(vec, etu));
+ if (vec[I_FOOD] < 0)
vec[I_FOOD] = 0;
- } else {
- vec[I_FOOD] -= roundavg(food_eaten);
- }
return starved;
}
+/*
+ * Return food eaten by people in VEC[] in ETU ETUs.
+ */
+double
+food_needed(short *vec, int etu)
+{
+ int people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
+ double need = etu * eatrate * people;
+ return need;
+}
+
+/*
+ * Return number of famine victims in VEC[] for ETU ETUs.
+ */
+int
+famine_victims(short *vec, int etu)
+{
+ double can_eat = vec[I_FOOD] / (etu * eatrate);
+ int people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
+ if (people < can_eat)
+ return 0;
+ if (can_eat < people / 2)
+ return people / 2;
+ return (int)(people - can_eat);
+}
+
+/*
+ * Starve up to NUM people of VEC[WHOM].
+ * Return the number of actually starved.
+ */
+static int
+starve_some(short *vec, i_type whom, int num)
+{
+ int retval = MIN(num, vec[whom]);
+ vec[whom] -= retval;
+ return retval;
+}
+
/*
* Truncate any extra people that may be around
*/
#include <config.h>
+#include <math.h>
#include "misc.h"
#include "plague.h"
#include "sect.h"
static void landrepair(struct lndstr *, struct natstr *, int *, int);
static void upd_land(struct lndstr *, int, struct natstr *, int *, int);
+static int feed_land(struct lndstr *, int);
int
prod_land(int etus, int natnum, int *bp, int build)
int n;
int min = morale_base - (int)np->nat_level[NAT_HLEV];
int mult;
- int needed;
int cost;
int eff;
if (!player->simulation) {
/* feed */
- if ((n = feed_land(lp, etus, &needed, 1)) > 0) {
+ if ((n = feed_land(lp, etus)) > 0) {
wu(0, lp->lnd_own, "%d starved in %s%s\n",
n, prland(lp),
(lp->lnd_effic < LAND_MINEFF ? ", killing it" : ""));
/*
* returns the number who starved, if any.
*/
-int
-feed_land(struct lndstr *lp, int etus, int *needed, int doit)
+static int
+feed_land(struct lndstr *lp, int etus)
{
- double food_eaten, ship_eaten;
- int ifood_eaten;
- double people_left;
- int need;
- int total_people;
- int starved;
+ int needed, give, take;
struct shpstr *sp;
if (opt_NOFOOD)
- return 0; /* no food no work to be done */
+ return 0;
- total_people = lp->lnd_item[I_MILIT];
- food_eaten = etus * eatrate * total_people;
- ifood_eaten = (int)food_eaten;
- if (food_eaten - ifood_eaten > 0)
- ifood_eaten++;
- starved = 0;
- *needed = 0;
+ needed = (int)ceil(food_needed(lp->lnd_item, etus));
- if (doit)
+ /* scrounge */
+ if (needed > lp->lnd_item[I_FOOD])
resupply_commod(lp, I_FOOD);
- /*
- * If we're on a ship, and we don't have enough food,
- * get some food off the carrying ship. (Don't starve
- * the ship, tho...
- */
-/* doit - Only try to take food off the ship during the update */
- if (ifood_eaten > lp->lnd_item[I_FOOD] && lp->lnd_ship >= 0 && doit) {
- need = ifood_eaten - lp->lnd_item[I_FOOD];
- sp = getshipp(lp->lnd_ship);
- ship_eaten = etus * eatrate * (sp->shp_item[I_CIVIL]
- + sp->shp_item[I_MILIT]
- + sp->shp_item[I_UW]);
- if (sp->shp_item[I_FOOD] - need > ship_eaten) {
- lp->lnd_item[I_FOOD] += need;
- sp->shp_item[I_FOOD] -= need;
- } else if (sp->shp_item[I_FOOD] - ship_eaten > 0) {
- lp->lnd_item[I_FOOD] += sp->shp_item[I_FOOD] - ship_eaten;
- sp->shp_item[I_FOOD] -= sp->shp_item[I_FOOD] - ship_eaten;
- }
- }
- if (ifood_eaten > lp->lnd_item[I_FOOD]) {
- *needed = ifood_eaten - lp->lnd_item[I_FOOD];
- people_left = (lp->lnd_item[I_FOOD] + 0.01) / (food_eaten + 0.01);
- /* only want to starve off at most 1/2 the populace. */
- if (people_left < 0.5)
- people_left = 0.5;
- starved = total_people * (1 - people_left);
- lp->lnd_item[I_MILIT] -= starved;
- lp->lnd_item[I_FOOD] = 0;
- } else {
- lp->lnd_item[I_FOOD] -= (int)food_eaten;
- }
- return starved;
+ return feed_people(lp->lnd_item, etus);
}
#include <config.h>
+#include <math.h>
#include "misc.h"
#include "plague.h"
#include "sect.h"
static void shiprepair(struct shpstr *, struct natstr *, int *, int);
static void upd_ship(struct shpstr *, int, struct natstr *, int *, int);
+static int feed_ship(struct shpstr *, int);
int
prod_ship(int etus, int natnum, int *bp, int build)
int dep;
int n;
int mult;
- int needed;
int cost;
int eff;
* prod_eff(product, sp->shp_tech));
}
/* feed */
- if ((n = feed_ship(sp, etus, &needed, 1)) > 0) {
+ if ((n = feed_ship(sp, etus)) > 0) {
wu(0, sp->shp_own, "%d starved on %s\n", n, prship(sp));
if (n > 10)
nreport(sp->shp_own, N_DIE_FAMINE, 0, 1);
/*
* returns the number who starved, if any.
*/
-int
-feed_ship(struct shpstr *sp, int etus, int *needed, int doit)
+static int
+feed_ship(struct shpstr *sp, int etus)
{
- double food_eaten, land_eaten;
- int ifood_eaten;
- int can_eat, need;
- int total_people;
- int to_starve;
- int starved;
+ int needed, take;
+ double give;
struct nstr_item ni;
struct lndstr *lp;
if (opt_NOFOOD)
- return 0; /* no food no work to do */
-
- total_people
- = sp->shp_item[I_CIVIL] + sp->shp_item[I_MILIT] + sp->shp_item[I_UW];
- food_eaten = etus * eatrate * total_people;
- ifood_eaten = (int)food_eaten;
- if (food_eaten - ifood_eaten > 0)
- ifood_eaten++;
- starved = 0;
- *needed = 0;
- if (!player->simulation && ifood_eaten > sp->shp_item[I_FOOD])
- sp->shp_item[I_FOOD]
- += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
- I_FOOD, ifood_eaten - sp->shp_item[I_FOOD]);
-
-/* doit - only steal food from land units during the update */
- if (ifood_eaten > sp->shp_item[I_FOOD] && sp->shp_nland > 0 && doit) {
+ return 0;
+
+ needed = (int)ceil(food_needed(sp->shp_item, etus));
+
+ /* scrounge */
+ if (needed > sp->shp_item[I_FOOD])
+ sp->shp_item[I_FOOD] += supply_commod(sp->shp_own,
+ sp->shp_x, sp->shp_y, I_FOOD,
+ needed - sp->shp_item[I_FOOD]);
+ if (needed > sp->shp_item[I_FOOD]) {
+ /* take from embarked land units, but don't starve them */
snxtitem_all(&ni, EF_LAND);
- while ((lp = nxtitemp(&ni)) && ifood_eaten > sp->shp_item[I_FOOD]) {
+ while ((lp = nxtitemp(&ni)) && needed > sp->shp_item[I_FOOD]) {
if (lp->lnd_ship != sp->shp_uid)
continue;
- need = ifood_eaten - sp->shp_item[I_FOOD];
- land_eaten = etus * eatrate * lp->lnd_item[I_MILIT];
- if (lp->lnd_item[I_FOOD] - need > land_eaten) {
- sp->shp_item[I_FOOD] += need;
- lp->lnd_item[I_FOOD] -= need;
- } else if (lp->lnd_item[I_FOOD] - land_eaten > 0) {
- sp->shp_item[I_FOOD] += lp->lnd_item[I_FOOD] - land_eaten;
- lp->lnd_item[I_FOOD] -= lp->lnd_item[I_FOOD] - land_eaten;
- }
+ give = lp->lnd_item[I_FOOD] - food_needed(lp->lnd_item, etus);
+ if (give < 1.0)
+ continue;
+ take = MIN((int)give, needed - sp->shp_item[I_FOOD]);
+ sp->shp_item[I_FOOD] += take;
+ lp->lnd_item[I_FOOD] -= take;
}
}
- if (ifood_eaten > sp->shp_item[I_FOOD]) {
- *needed = ifood_eaten - sp->shp_item[I_FOOD];
- can_eat = sp->shp_item[I_FOOD] / (etus * eatrate);
- /* only want to starve off at most 1/2 the populace. */
- if (can_eat < total_people / 2)
- can_eat = total_people / 2;
-
- to_starve = total_people - can_eat;
- while (to_starve && sp->shp_item[I_UW]) {
- to_starve--;
- starved++;
- sp->shp_item[I_UW]--;
- }
- while (to_starve && sp->shp_item[I_CIVIL]) {
- to_starve--;
- starved++;
- sp->shp_item[I_CIVIL]--;
- }
- while (to_starve && sp->shp_item[I_MILIT]) {
- to_starve--;
- starved++;
- sp->shp_item[I_MILIT]--;
- }
-
- sp->shp_item[I_FOOD] = 0;
- } else {
- sp->shp_item[I_FOOD] -= (int)food_eaten;
- }
- return starved;
+ return feed_people(sp->shp_item, etus);
}