]> git.pond.sub.org Git - empserver/blob - src/lib/update/human.c
Update copyright notice.
[empserver] / src / lib / update / human.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  human.c: Food related functions
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Steve McClure, 1996
33  *     Markus Armbruster, 2004-2006
34  */
35
36 #include <config.h>
37
38 #include "budg.h"
39 #include "item.h"
40 #include "news.h"
41 #include "player.h"
42 #include "update.h"
43 #include "xy.h"
44 #include <math.h>
45
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,
50                        short *);
51 static int babies(int, int, double, int, int);
52
53 /*
54  * feed the individual sector
55  */
56 int
57 do_feed(struct sctstr *sp, struct natstr *np, short *vec,
58         int *workp, int *bp, int etu)
59 {
60     int work_avail;
61     int starved, sctwork;
62     int needed;
63     int maxpop;
64
65     /* grow people & stuff */
66     sctwork = sp->sct_work;
67
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],
72                                      maxpop));
73
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 */
81                 pt_bg_nmbr(bp, sp, I_MAX + 1, work_avail);
82                 if (!player->simulation)
83                     sp->sct_avail = work_avail;
84             }
85             if (vec[I_FOOD] < needed && sp->sct_own == sp->sct_oldown) {
86                 /* steal food from warehouses, headquarters,
87                    supply ships in port, or supply units */
88                 vec[I_FOOD] = supply_commod(sp->sct_own,
89                                             sp->sct_x, sp->sct_y,
90                                             I_FOOD, needed);
91             }
92         }
93         starved = feed_people(vec, etu);
94         if (starved > 0) {
95             if (!player->simulation) {
96                 /* don't report POGO starvation */
97                 if (sp->sct_own) {
98                     wu(0, sp->sct_own, "%d starved in %s.\n", starved,
99                        xyas(sp->sct_x, sp->sct_y, sp->sct_own));
100                     if (starved > 25)
101                         nreport(sp->sct_own, N_DIE_FAMINE, 0, 1);
102                 }
103                 sp->sct_work = 0;
104                 sp->sct_loyal += (random() % 8) + 2;
105             }
106             sctwork = 0;
107         } else {
108             if (sp->sct_work < 100)
109                 sctwork = sp->sct_work + 8 + (random() % 15);
110             if (sctwork > 100)
111                 sctwork = 100;
112             if (!player->simulation)
113                 sp->sct_work = sctwork;
114             grow_people(sp, etu, np, &work_avail, sctwork, vec);
115         }
116     } else
117         sctwork = sp->sct_work = 100;
118     /* Here is where we truncate extra people, always */
119     trunc_people(sp, np, vec);
120
121     pt_bg_nmbr(bp, sp, I_CIVIL, vec[I_CIVIL]);
122     pt_bg_nmbr(bp, sp, I_UW, vec[I_UW]);
123     pt_bg_nmbr(bp, sp, I_MILIT, vec[I_MILIT]);
124     *workp = work_avail;
125     return sctwork;
126 }
127
128 int
129 new_work(struct sctstr *sp, int delta)
130 {
131     if (sp->sct_type == sp->sct_newtype)
132         return MIN(rollover_avail_max, sp->sct_avail) + delta;
133
134     return delta;
135 }
136
137 static int
138 growfood(struct sctstr *sp, short *vec, int work, int etu)
139 {
140     int food_fertil;
141     int food_workers;
142     int food;
143     int work_used;
144
145     food_workers = work * fcrate;
146     food_fertil = etu * sp->sct_fertil * fgrate;
147     food = MIN(food_workers, food_fertil);
148     if (food > ITEM_MAX - vec[I_FOOD])
149         food = ITEM_MAX - vec[I_FOOD];
150     /*
151      * Be nice; grow minimum one food unit.
152      * This makes life simpler for the player.
153      */
154     vec[I_FOOD] += food;
155     if (vec[I_FOOD] == 0)
156         vec[I_FOOD] = 1;
157     work_used = food / fcrate;
158     return work_used;
159 }
160
161 /*
162  * returns the number who starved, if any.
163  */
164 int
165 feed_people(short *vec, int etu)
166 {
167     int to_starve, starved;
168
169     if (opt_NOFOOD)
170         return 0;
171
172     to_starve = famine_victims(vec, etu);
173     starved = starve_some(vec, I_UW, to_starve);
174     starved += starve_some(vec, I_CIVIL, to_starve - starved);
175     starved += starve_some(vec, I_MILIT, to_starve - starved);
176     vec[I_FOOD] -= roundavg(food_needed(vec, etu));
177     if (vec[I_FOOD] < 0)
178         vec[I_FOOD] = 0;
179     return starved;
180 }
181
182 /*
183  * Return food eaten by people in VEC[] in ETU ETUs.
184  */
185 double
186 food_needed(short *vec, int etu)
187 {
188     int people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
189     double need = etu * eatrate * people;
190     return need;
191 }
192
193 /*
194  * Return number of famine victims in VEC[] for ETU ETUs.
195  */
196 int
197 famine_victims(short *vec, int etu)
198 {
199     double can_eat = vec[I_FOOD] / (etu * eatrate);
200     int people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
201     if (people < can_eat)
202         return 0;
203     if (can_eat < people / 2)
204         return people / 2;
205     return (int)(people - can_eat);
206 }
207
208 /*
209  * Starve up to NUM people of VEC[WHOM].
210  * Return the number of actually starved.
211  */
212 static int
213 starve_some(short *vec, i_type whom, int num)
214 {
215     int retval = MIN(num, vec[whom]);
216     vec[whom] -= retval;
217     return retval;
218 }
219
220 /*
221  * Truncate any extra people that may be around
222  */
223 static void
224 trunc_people(struct sctstr *sp, struct natstr *np,
225              short *vec)
226 {
227     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
228
229     if (vec[I_CIVIL] > maxpop)
230         vec[I_CIVIL] = maxpop;
231     if (vec[I_UW] > maxpop)
232         vec[I_UW] = maxpop;
233 }
234
235 /*
236  * Grow babies, and add to populace.
237  * XXX Might think about dropping in a birth
238  * rate limitation on countries with high tech
239  * production?  Maybe with just high education?
240  */
241 static int
242 grow_people(struct sctstr *sp, int etu,
243             struct natstr *np, int *workp, int sctwork,
244             short *vec)
245 {
246     int newciv;
247     int newuw;
248     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
249
250     newciv = babies(vec[I_CIVIL], etu, obrate, vec[I_FOOD], maxpop);
251     vec[I_CIVIL] += newciv;
252     newuw = babies(vec[I_UW], etu, uwbrate, vec[I_FOOD], maxpop);
253     vec[I_UW] += newuw;
254     /*
255      * subtract the baby eat food (if we are using FOOD) and return
256      * # of births.
257      */
258     if (opt_NOFOOD == 0 && (newciv || newuw))
259         vec[I_FOOD] -= roundavg((newciv + newuw) * babyeat);
260     *workp += total_work(sctwork, etu, newciv, 0, newuw, ITEM_MAX);
261     return newciv + newuw;
262 }
263
264 /*
265  * Return the number of babies born to ADULTS in ETU ETUs.
266  * BRATE is the birth rate.
267  * FOOD is the food available for growing babies.
268  * MAXPOP is the population limit.
269  */
270 static int
271 babies(int adults, int etu, double brate, int food, int maxpop)
272 {
273     int new_birth, new_food, new;
274
275     if (adults >= maxpop)
276         return 0;
277
278     new_birth = roundavg(brate * etu * adults);
279     if (opt_NOFOOD)
280         new_food = new_birth;
281     else
282         new_food = (int)(0.5 + food / (2.0 * babyeat));
283
284     new = new_birth;
285     if (new > new_food)
286         new = new_food;
287     if (adults + new > maxpop)
288         new = maxpop - adults;
289
290     return new;
291 }