2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2017, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * human.c: Food related functions
29 * Known contributors to this file:
32 * Markus Armbruster, 2004-2016
44 #include "prototypes.h"
49 static int new_work(struct sctstr *, int);
50 static int growfood(struct sctstr *, int, int);
51 static int starve_some(short *, i_type, int);
52 static void trunc_people(struct sctstr *, struct natstr *);
53 static int grow_people(struct sctstr *, int, struct natstr *, int);
54 static int babies(int, int, double, int, int, int);
57 * feed the individual sector
60 do_feed(struct sctstr *sp, struct natstr *np, int etu,
61 int round_babies_down)
69 maxworkers = max_workers(np->nat_level[NAT_RLEV], sp);
70 work_avail = new_work(sp,
71 total_work(sp->sct_work, etu,
72 sp->sct_item[I_CIVIL],
73 sp->sct_item[I_MILIT],
77 if (sp->sct_type != SCT_WATER && sp->sct_type != SCT_SANCT) {
79 if (opt_NOFOOD == 0) {
80 needed = (int)ceil(food_needed(sp->sct_item, etu));
81 if (sp->sct_item[I_FOOD] < needed) {
82 /* need to grow "emergency rations" */
83 work_avail -= 2 * growfood(sp, work_avail / 2, etu);
84 /* It's twice as hard to grow those than norm */
85 if (sp->sct_item[I_FOOD] == 0)
86 /* Conjure up 1f to make life easier for the player */
87 manna = sp->sct_item[I_FOOD] = 1;
90 starved = feed_people(sp->sct_item, etu);
92 if (!player->simulation) {
93 /* don't report POGO starvation */
95 wu(0, sp->sct_own, "%d starved in %s.\n", starved,
96 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
98 nreport(sp->sct_own, N_DIE_FAMINE, 0, 1);
101 sp->sct_loyal += roll(8) + 1;
104 sctwork = sp->sct_work;
106 sctwork += 7 + roll(15);
109 grow_people(sp, etu, np, round_babies_down);
110 work_avail = new_work(sp,
111 total_work(sp->sct_work, etu,
112 sp->sct_item[I_CIVIL],
113 sp->sct_item[I_MILIT],
116 /* FIXME restores work charged for growfood() */
118 sp->sct_che = age_people(sp->sct_che, etu);
121 /* Take away food we conjured up */
122 sp->sct_item[I_FOOD] = 0;
126 /* Here is where we truncate extra people, always */
127 trunc_people(sp, np);
129 sp->sct_work = sctwork;
130 sp->sct_avail = work_avail;
134 new_work(struct sctstr *sp, int delta)
136 int rollover = sp->sct_avail;
138 if (sp->sct_type != sp->sct_newtype)
140 if (rollover > rollover_avail_max)
141 rollover = rollover_avail_max;
142 if (rollover > delta / 2 + 1)
143 rollover = delta / 2 + 1;
145 return rollover + delta;
149 growfood(struct sctstr *sp, int work, int etu)
156 food_workers = work * fcrate;
157 food_fertil = etu * sp->sct_fertil * fgrate;
158 food = MIN(food_workers, food_fertil);
159 if (food > ITEM_MAX - sp->sct_item[I_FOOD])
160 food = ITEM_MAX - sp->sct_item[I_FOOD];
161 sp->sct_item[I_FOOD] += food;
162 work_used = food / fcrate;
167 * returns the number who starved, if any.
170 feed_people(short *vec, int etu)
172 int to_starve, starved;
177 to_starve = famine_victims(vec, etu);
178 starved = starve_some(vec, I_UW, to_starve);
179 starved += starve_some(vec, I_CIVIL, to_starve - starved);
180 starved += starve_some(vec, I_MILIT, to_starve - starved);
181 vec[I_FOOD] -= roundavg(food_needed(vec, etu));
188 * Return food eaten by people in @vec[] in @etu ETUs.
191 food_needed(short *vec, int etu)
193 int people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
194 double need = etu * eatrate * people;
199 * Return number of famine victims in @vec[] for @etu ETUs.
202 famine_victims(short *vec, int etu)
204 double can_eat = vec[I_FOOD] / (etu * eatrate);
205 int people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
206 if (people <= can_eat)
208 if (can_eat <= people / 2)
210 return (int)(people - can_eat);
214 * Starve up to @num people of @vec[@whom].
215 * Return the number of actually starved.
218 starve_some(short *vec, i_type whom, int num)
220 int retval = MIN(num, vec[whom]);
226 * Truncate any extra people that may be around
229 trunc_people(struct sctstr *sp, struct natstr *np)
231 int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
233 if (sp->sct_item[I_CIVIL] > maxpop)
234 sp->sct_item[I_CIVIL] = maxpop;
235 if (sp->sct_item[I_UW] > maxpop)
236 sp->sct_item[I_UW] = maxpop;
240 * Grow babies, and add to populace.
241 * XXX Might think about dropping in a birth
242 * rate limitation on countries with high tech
243 * production? Maybe with just high education?
246 grow_people(struct sctstr *sp, int etu, struct natstr *np, int round_down)
250 int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
252 newciv = babies(sp->sct_item[I_CIVIL], etu, obrate,
253 sp->sct_item[I_FOOD], maxpop, round_down);
254 sp->sct_item[I_CIVIL] += newciv;
255 newuw = babies(sp->sct_item[I_UW], etu, uwbrate,
256 sp->sct_item[I_FOOD], maxpop, round_down);
257 sp->sct_item[I_UW] += newuw;
259 * subtract the baby eat food (if we are using FOOD) and return
262 if (opt_NOFOOD == 0 && (newciv || newuw))
263 sp->sct_item[I_FOOD] -= roundavg((newciv + newuw) * babyeat);
264 return newciv + newuw;
268 * Return the number of babies born to @adults in @etu ETUs.
269 * @brate is the birth rate.
270 * @food is the food available for growing babies.
271 * @maxpop is the population limit.
272 * If @round_down, discard fractions instead of rounding them
276 babies(int adults, int etu, double brate, int food, int maxpop,
282 if (adults >= maxpop)
285 new_birth = brate * etu * adults;
286 new = round_down ? (int)new_birth : roundavg(new_birth);
289 new_food = (int)(food / (2.0 * babyeat));
294 if (adults + new > maxpop)
295 new = maxpop - adults;