2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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:
49 static int grow_people(struct sctstr *, int,
50 struct natstr *, int *, int,
52 static int growfood(struct sctstr *, short *, int, int);
53 static void starvation(struct sctstr *);
54 static void trunc_people(struct sctstr *, struct natstr *,
58 * feed the individual sector
62 do_feed(struct sctstr *sp, struct natstr *np, short *vec,
63 int *workp, int *bp, int etu)
71 /* grow people & stuff */
72 sctwork = sp->sct_work;
74 maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
75 work_avail = new_work(sp,
76 total_work(sctwork, etu,
77 vec[I_CIVIL], vec[I_MILIT], vec[I_UW],
80 people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
81 if (sp->sct_type != SCT_SANCT) {
82 if (opt_NOFOOD == 0) {
83 if (vec[I_FOOD] < 1 + etu * people * eatrate) {
84 /* need to grow "emergency rations" */
86 growfood(sp, vec, (int)(work_avail / 2),
88 /* It's twice as hard to grow those than norm */
89 pt_bg_nmbr(bp, sp, I_MAX + 1, work_avail);
90 if (!player->simulation)
91 sp->sct_avail = work_avail;
93 if ((vec[I_FOOD] < 1 + etu * people * eatrate) &&
94 (sp->sct_own == sp->sct_oldown)) {
96 /* steal food from warehouses, headquarters,
97 supply ships in port, or supply units */
100 needed = ldround((double)(1 + etu * people * eatrate), 1);
102 /* Now, find some food */
103 vec[I_FOOD] = supply_commod(sp->sct_own, sp->sct_x,
104 sp->sct_y, I_FOOD, needed);
108 starved = feed_people(vec, etu, &needed);
109 if ((starved > 0 && sp->sct_own) && (!player->simulation)) {
110 /* don't report POGO starvation */
111 wu(0, sp->sct_own, "%d starved in %s.\n", starved,
112 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
114 nreport(sp->sct_own, N_DIE_FAMINE, 0, 1);
117 if (!player->simulation)
121 if (sp->sct_work < 100)
122 sctwork = sp->sct_work + 8 + (random() % 15);
125 if (!player->simulation)
126 sp->sct_work = sctwork;
127 grow_people(sp, etu, np, &work_avail, sctwork, vec);
130 sctwork = sp->sct_work = 100;
131 /* Here is where we truncate extra people, always */
132 trunc_people(sp, np, vec);
134 pt_bg_nmbr(bp, sp, I_CIVIL, vec[I_CIVIL]);
135 pt_bg_nmbr(bp, sp, I_UW, vec[I_UW]);
136 pt_bg_nmbr(bp, sp, I_MILIT, vec[I_MILIT]);
142 new_work(struct sctstr *sp, int delta)
144 if (sp->sct_type == sp->sct_newtype)
145 return min(rollover_avail_max, sp->sct_avail) + delta;
151 growfood(struct sctstr *sp, short *vec, int work, int etu)
158 /* I'm being very nice and commenting out this so players
159 * won't whine about starvation
160 if (sp->sct_fertil == 0 || work == 0)
163 food_workers = work * fcrate;
164 food_fertil = etu * sp->sct_fertil * fgrate;
165 food = min(food_workers, food_fertil);
167 * be nice; grow minimum one food unit.
168 * This makes life simpler for the player.
170 if (food > ITEM_MAX - vec[I_FOOD])
171 food = ITEM_MAX - vec[I_FOOD];
173 if (vec[I_FOOD] == 0)
175 work_used = food / fcrate;
180 * returns the number who starved, if any.
183 feed_people(short *vec, int etu, int *needed)
195 total_people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
196 food_eaten = etu * eatrate * total_people;
197 ifood_eaten = (int)food_eaten;
198 if (food_eaten - ifood_eaten > 0)
200 if (ifood_eaten <= 1)
204 if (ifood_eaten > vec[I_FOOD]) {
205 *needed = ifood_eaten - vec[I_FOOD];
206 can_eat = vec[I_FOOD] / (etu * eatrate);
207 /* only want to starve off at most 1/2 the populace. */
208 if (can_eat < total_people / 2)
209 can_eat = total_people / 2;
211 to_starve = total_people - can_eat;
212 while (to_starve && vec[I_UW]) {
217 while (to_starve && vec[I_CIVIL]) {
222 while (to_starve && vec[I_MILIT]) {
230 vec[I_FOOD] -= roundavg(food_eaten);
236 * Truncate any extra people that may be around
239 trunc_people(struct sctstr *sp, struct natstr *np,
242 int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
244 if (vec[I_CIVIL] > maxpop)
245 vec[I_CIVIL] = maxpop;
246 if (vec[I_UW] > maxpop)
251 * Grow babies, and add to populace.
252 * XXX Might think about dropping in a birth
253 * rate limitation on countries with high tech
254 * production? Maybe with just high education?
257 grow_people(struct sctstr *sp, int etu,
258 struct natstr *np, int *workp, int sctwork,
265 int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
269 if (vec[I_CIVIL] < maxpop) {
270 new_birth = (int)roundavg(obrate * (double)(etu * vec[I_CIVIL]));
272 new_food = (int)(0.5 + maxpop / (2.0 * babyeat));
273 else /* we are using food */
274 new_food = (int)(0.5 + vec[I_FOOD] / (2.0 * babyeat));
277 if (newciv > new_food)
279 /* Now, check max pops */
280 if ((vec[I_CIVIL] + newciv) > maxpop)
281 newciv = maxpop - vec[I_CIVIL];
282 vec[I_CIVIL] += newciv;
284 if (vec[I_UW] < maxpop) {
288 new_birth = (int)roundavg(uwbrate * (double)(etu * vec[I_UW]));
290 new_food = (int)(0.5 + maxpop / (2.0 * babyeat));
291 else /* food is important */
292 new_food = (int)(0.5 + vec[I_FOOD] / (2.0 * babyeat));
295 if (newuw > new_food)
297 /* Now, check max pops */
298 if ((vec[I_UW] + newuw) > maxpop)
299 newuw = maxpop - vec[I_UW];
303 * subtract the baby eat food (if we are using FOOD) and return
306 if (opt_NOFOOD == 0 && (newciv || newuw))
307 vec[I_FOOD] -= roundavg((newciv + newuw) * babyeat);
308 *workp += total_work(sctwork, etu, newciv, 0, newuw, ITEM_MAX);
309 return newciv + newuw;
313 * percentage of people who starved
316 starvation(struct sctstr *sp)
319 sp->sct_loyal += (random() % 8) + 2;