2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2012, 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-2010
44 static int growfood(struct sctstr *, short *, int, int);
45 static int starve_some(short *, i_type, int);
46 static void trunc_people(struct sctstr *, struct natstr *, short *);
47 static int grow_people(struct sctstr *, int, struct natstr *, int *, int,
49 static int babies(int, int, double, int, int);
52 * feed the individual sector
55 do_feed(struct sctstr *sp, struct natstr *np, short *vec,
64 /* grow people & stuff */
65 sctwork = sp->sct_work;
67 maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
68 work_avail = new_work(sp,
69 total_work(sctwork, etu,
70 vec[I_CIVIL], vec[I_MILIT], vec[I_UW],
73 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 /* Conjure up 1f to make life easier for the player */
83 manna = vec[I_FOOD] = 1;
86 starved = feed_people(vec, etu);
88 if (!player->simulation) {
89 /* don't report POGO starvation */
91 wu(0, sp->sct_own, "%d starved in %s.\n", starved,
92 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
94 nreport(sp->sct_own, N_DIE_FAMINE, 0, 1);
97 sp->sct_loyal += (random() % 8) + 2;
101 if (sp->sct_work < 100)
102 sctwork = sp->sct_work + 8 + (random() % 15);
105 if (!player->simulation)
106 sp->sct_work = sctwork;
107 grow_people(sp, etu, np, &work_avail, sctwork, vec);
109 if (!player->simulation)
110 sp->sct_che = age_people(sp->sct_che, etu);
113 /* Take away food we conjured up */
116 sctwork = sp->sct_work = 100;
117 /* Here is where we truncate extra people, always */
118 trunc_people(sp, np, vec);
125 new_work(struct sctstr *sp, int delta)
127 if (sp->sct_type == sp->sct_newtype)
128 return MIN(rollover_avail_max, sp->sct_avail) + delta;
134 growfood(struct sctstr *sp, short *vec, int work, int etu)
141 food_workers = work * fcrate;
142 food_fertil = etu * sp->sct_fertil * fgrate;
143 food = MIN(food_workers, food_fertil);
144 if (food > ITEM_MAX - vec[I_FOOD])
145 food = ITEM_MAX - vec[I_FOOD];
147 work_used = food / fcrate;
152 * returns the number who starved, if any.
155 feed_people(short *vec, int etu)
157 int to_starve, starved;
162 to_starve = famine_victims(vec, etu);
163 starved = starve_some(vec, I_UW, to_starve);
164 starved += starve_some(vec, I_CIVIL, to_starve - starved);
165 starved += starve_some(vec, I_MILIT, to_starve - starved);
166 vec[I_FOOD] -= roundavg(food_needed(vec, etu));
173 * Return food eaten by people in VEC[] in ETU ETUs.
176 food_needed(short *vec, int etu)
178 int people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
179 double need = etu * eatrate * people;
184 * Return number of famine victims in VEC[] for ETU ETUs.
187 famine_victims(short *vec, int etu)
189 double can_eat = vec[I_FOOD] / (etu * eatrate);
190 int people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
191 if (people <= can_eat)
193 if (can_eat <= people / 2)
195 return (int)(people - can_eat);
199 * Starve up to NUM people of VEC[WHOM].
200 * Return the number of actually starved.
203 starve_some(short *vec, i_type whom, int num)
205 int retval = MIN(num, vec[whom]);
211 * Truncate any extra people that may be around
214 trunc_people(struct sctstr *sp, struct natstr *np, short *vec)
216 int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
218 if (vec[I_CIVIL] > maxpop)
219 vec[I_CIVIL] = maxpop;
220 if (vec[I_UW] > maxpop)
225 * Grow babies, and add to populace.
226 * XXX Might think about dropping in a birth
227 * rate limitation on countries with high tech
228 * production? Maybe with just high education?
231 grow_people(struct sctstr *sp, int etu,
232 struct natstr *np, int *workp, int sctwork,
237 int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
239 newciv = babies(vec[I_CIVIL], etu, obrate, vec[I_FOOD], maxpop);
240 vec[I_CIVIL] += newciv;
241 newuw = babies(vec[I_UW], etu, uwbrate, vec[I_FOOD], maxpop);
244 * subtract the baby eat food (if we are using FOOD) and return
247 if (opt_NOFOOD == 0 && (newciv || newuw))
248 vec[I_FOOD] -= roundavg((newciv + newuw) * babyeat);
249 *workp += total_work(sctwork, etu, newciv, 0, newuw, ITEM_MAX);
250 return newciv + newuw;
254 * Return the number of babies born to ADULTS in ETU ETUs.
255 * BRATE is the birth rate.
256 * FOOD is the food available for growing babies.
257 * MAXPOP is the population limit.
260 babies(int adults, int etu, double brate, int food, int maxpop)
262 int new_birth, new_food, new;
264 if (adults >= maxpop)
267 new_birth = roundavg(brate * etu * adults);
269 new_food = new_birth;
271 new_food = (int)(food / (2.0 * babyeat));
276 if (adults + new > maxpop)
277 new = maxpop - adults;