2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * human.c: Food related functions
30 * Known contributors to this file:
33 * Markus Armbruster, 2004-2008
46 static int growfood(struct sctstr *, short *, int, int);
47 static int starve_some(short *, i_type, int);
48 static void trunc_people(struct sctstr *, struct natstr *, short *);
49 static int grow_people(struct sctstr *, int, struct natstr *, int *, int,
51 static int babies(int, int, double, int, int);
54 * feed the individual sector
57 do_feed(struct sctstr *sp, struct natstr *np, short *vec,
65 /* grow people & stuff */
66 sctwork = sp->sct_work;
68 maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
69 work_avail = new_work(sp,
70 total_work(sctwork, etu,
71 vec[I_CIVIL], vec[I_MILIT], vec[I_UW],
74 if (sp->sct_type != SCT_SANCT) {
75 if (opt_NOFOOD == 0) {
76 needed = (int)ceil(food_needed(vec, etu));
77 if (vec[I_FOOD] < needed) {
78 /* need to grow "emergency rations" */
79 work_avail -= 2 * growfood(sp, vec, work_avail / 2, etu);
80 /* It's twice as hard to grow those than norm */
82 if (vec[I_FOOD] < needed && sp->sct_own == sp->sct_oldown) {
83 /* steal food from warehouses, headquarters,
84 supply ships in port, or supply units */
85 vec[I_FOOD] = supply_commod(sp->sct_own,
90 starved = feed_people(vec, 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 += (random() % 8) + 2;
105 if (sp->sct_work < 100)
106 sctwork = sp->sct_work + 8 + (random() % 15);
109 if (!player->simulation)
110 sp->sct_work = sctwork;
111 grow_people(sp, etu, np, &work_avail, sctwork, vec);
113 if (!player->simulation) {
114 int oldche = sp->sct_che;
115 sp->sct_che = age_people(sp->sct_che, etu);
116 if (sp->sct_che_target == sp->sct_own && sp->sct_loyal < 40)
117 /* make them fade away eventually, for playability */
119 if (sp->sct_che != oldche)
120 logerror("che in %d,%d aged from %d to %d",
121 sp->sct_x, sp->sct_y, oldche, sp->sct_che);
125 sctwork = sp->sct_work = 100;
126 /* Here is where we truncate extra people, always */
127 trunc_people(sp, np, vec);
134 new_work(struct sctstr *sp, int delta)
136 if (sp->sct_type == sp->sct_newtype)
137 return MIN(rollover_avail_max, sp->sct_avail) + delta;
143 growfood(struct sctstr *sp, short *vec, int work, int etu)
150 food_workers = work * fcrate;
151 food_fertil = etu * sp->sct_fertil * fgrate;
152 food = MIN(food_workers, food_fertil);
153 if (food > ITEM_MAX - vec[I_FOOD])
154 food = ITEM_MAX - vec[I_FOOD];
156 * Be nice; grow minimum one food unit.
157 * This makes life simpler for the player.
160 if (vec[I_FOOD] == 0)
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,
232 int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
234 if (vec[I_CIVIL] > maxpop)
235 vec[I_CIVIL] = maxpop;
236 if (vec[I_UW] > maxpop)
241 * Grow babies, and add to populace.
242 * XXX Might think about dropping in a birth
243 * rate limitation on countries with high tech
244 * production? Maybe with just high education?
247 grow_people(struct sctstr *sp, int etu,
248 struct natstr *np, int *workp, int sctwork,
253 int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
255 newciv = babies(vec[I_CIVIL], etu, obrate, vec[I_FOOD], maxpop);
256 vec[I_CIVIL] += newciv;
257 newuw = babies(vec[I_UW], etu, uwbrate, vec[I_FOOD], maxpop);
260 * subtract the baby eat food (if we are using FOOD) and return
263 if (opt_NOFOOD == 0 && (newciv || newuw))
264 vec[I_FOOD] -= roundavg((newciv + newuw) * babyeat);
265 *workp += total_work(sctwork, etu, newciv, 0, newuw, ITEM_MAX);
266 return newciv + newuw;
270 * Return the number of babies born to ADULTS in ETU ETUs.
271 * BRATE is the birth rate.
272 * FOOD is the food available for growing babies.
273 * MAXPOP is the population limit.
276 babies(int adults, int etu, double brate, int food, int maxpop)
278 int new_birth, new_food, new;
280 if (adults >= maxpop)
283 new_birth = roundavg(brate * etu * adults);
285 new_food = new_birth;
287 new_food = (int)(food / (2.0 * babyeat));
292 if (adults + new > maxpop)
293 new = maxpop - adults;