2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * human.c: Food related functions
30 * Known contributors to this file:
52 static int grow_people(struct sctstr *, register int,
53 register struct natstr *, int *, int,
55 static int growfood(struct sctstr *, short *, int, int);
56 static void starvation(struct sctstr *);
57 static void trunc_people(struct sctstr *, register struct natstr *,
61 * feed the individual sector
65 do_feed(struct sctstr *sp, struct natstr *np, short *vec,
66 int *workp, int *bp, int etu)
76 /* grow people & stuff */
77 sctwork = sp->sct_work;
79 maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
80 civvies = (vec[I_CIVIL] > maxpop) ? maxpop : vec[I_CIVIL];
81 uws = (vec[I_UW] > maxpop) ? maxpop : vec[I_UW];
82 mil = (vec[I_MILIT] > maxpop) ? maxpop : vec[I_MILIT];
83 work_avail = new_work(sp, total_work(sctwork, etu, civvies, mil, uws));
85 people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
86 if (sp->sct_type != SCT_SANCT) {
87 if (opt_NOFOOD == 0) {
88 if (vec[I_FOOD] < 1 + etu * people * eatrate) {
89 /* need to grow "emergency rations" */
91 growfood(sp, vec, (int)(work_avail / 2),
93 /* It's twice as hard to grow those than norm */
94 pt_bg_nmbr(bp, sp, I_MAX + 1, work_avail);
95 if (!player->simulation)
96 sp->sct_avail = work_avail;
98 if ((vec[I_FOOD] < 1 + etu * people * eatrate) &&
99 (sp->sct_own == sp->sct_oldown)) {
101 /* steal food from warehouses, headquarters,
102 supply ships in port, or supply units */
105 needed = ldround((double)(1 + etu * people * eatrate), 1);
107 /* Now, find some food */
108 vec[I_FOOD] = supply_commod(sp->sct_own, sp->sct_x,
109 sp->sct_y, I_FOOD, needed);
113 starved = feed_people(vec, etu, &needed);
114 if ((starved > 0 && sp->sct_own) && (!player->simulation)) {
115 /* don't report POGO starvation */
116 wu(0, sp->sct_own, "%d starved in %s.\n", starved,
117 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
119 nreport(sp->sct_own, N_DIE_FAMINE, 0, 1);
122 if (!player->simulation)
126 if (sp->sct_work < 100)
127 sctwork = sp->sct_work + 8 + (random() % 15);
130 if (!player->simulation)
131 sp->sct_work = sctwork;
132 dummy = grow_people(sp, etu, np, &work_avail, sctwork, vec);
135 sctwork = sp->sct_work = 100;
136 /* Here is where we truncate extra people, always */
137 trunc_people(sp, np, vec);
139 pt_bg_nmbr(bp, sp, I_CIVIL, vec[I_CIVIL]);
140 pt_bg_nmbr(bp, sp, I_UW, vec[I_UW]);
141 pt_bg_nmbr(bp, sp, I_MILIT, vec[I_MILIT]);
147 new_work(struct sctstr *sp, int delta)
149 if (sp->sct_type == sp->sct_newtype)
150 return min(rollover_avail_max, sp->sct_avail) + delta;
156 growfood(struct sctstr *sp, short *vec, int work, int etu)
163 /* I'm being very nice and commenting out this so players
164 * won't whine about starvation
165 if (sp->sct_fertil == 0 || work == 0)
168 food_workers = work * fcrate;
169 food_fertil = etu * sp->sct_fertil * fgrate;
170 food = min(food_workers, food_fertil);
172 * be nice; grow minimum one food unit.
173 * This makes life simpler for the player.
175 if (food > ITEM_MAX - vec[I_FOOD])
176 food = ITEM_MAX - vec[I_FOOD];
178 if (vec[I_FOOD] == 0)
180 work_used = food / fcrate;
185 * returns the number who starved, if any.
188 feed_people(short *vec, int etu, int *needed)
200 total_people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
201 food_eaten = etu * eatrate * total_people;
202 ifood_eaten = (int)food_eaten;
203 if (food_eaten - ifood_eaten > 0)
205 if (ifood_eaten <= 1)
209 if (ifood_eaten > vec[I_FOOD]) {
210 *needed = ifood_eaten - vec[I_FOOD];
211 can_eat = vec[I_FOOD] / (etu * eatrate);
212 /* only want to starve off at most 1/2 the populace. */
213 if (can_eat < total_people / 2)
214 can_eat = total_people / 2;
216 to_starve = total_people - can_eat;
217 while (to_starve && vec[I_UW]) {
222 while (to_starve && vec[I_CIVIL]) {
227 while (to_starve && vec[I_MILIT]) {
235 vec[I_FOOD] -= roundavg(food_eaten);
241 * Truncate any extra people that may be around
244 trunc_people(struct sctstr *sp, register struct natstr *np,
247 int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
249 if (vec[I_CIVIL] > maxpop)
250 vec[I_CIVIL] = maxpop;
251 if (vec[I_UW] > maxpop)
256 * Grow babies, and add to populace.
257 * XXX Might think about dropping in a birth
258 * rate limitation on countries with high tech
259 * production? Maybe with just high education?
262 grow_people(struct sctstr *sp, register int etu,
263 register struct natstr *np, int *workp, int sctwork,
270 int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
274 if (vec[I_CIVIL] < maxpop) {
275 new_birth = (int)roundavg(obrate * (double)(etu * vec[I_CIVIL]));
277 new_food = (int)(0.5 + maxpop / (2.0 * babyeat));
278 else /* we are using food */
279 new_food = (int)(0.5 + vec[I_FOOD] / (2.0 * babyeat));
282 if (newciv > new_food)
284 /* Now, check max pops */
285 if ((vec[I_CIVIL] + newciv) > maxpop)
286 newciv = maxpop - vec[I_CIVIL];
287 vec[I_CIVIL] += newciv;
289 if (vec[I_UW] < maxpop) {
293 new_birth = (int)roundavg(uwbrate * (double)(etu * vec[I_UW]));
295 new_food = (int)(0.5 + maxpop / (2.0 * babyeat));
296 else /* food is important */
297 new_food = (int)(0.5 + vec[I_FOOD] / (2.0 * babyeat));
300 if (newuw > new_food)
302 /* Now, check max pops */
303 if ((vec[I_UW] + newuw) > maxpop)
304 newuw = maxpop - vec[I_UW];
308 * subtract the baby eat food (if we are using FOOD) and return
311 if (opt_NOFOOD == 0 && (newciv || newuw))
312 vec[I_FOOD] -= roundavg((newciv + newuw) * babyeat);
313 *workp += total_work(sctwork, etu, newciv, 0, newuw);
314 return newciv + newuw;
318 * percentage of people who starved
321 starvation(struct sctstr *sp)
324 sp->sct_loyal += (random() % 8) + 2;