]> git.pond.sub.org Git - empserver/blob - src/lib/update/human.c
9206edf11c390c224978baa1fd54aad60f588cd6
[empserver] / src / lib / update / human.c
1 /*
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
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-2008
34  */
35
36 #include <config.h>
37
38 #include <math.h>
39 #include "budg.h"
40 #include "item.h"
41 #include "news.h"
42 #include "player.h"
43 #include "update.h"
44 #include "xy.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 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             }
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,
86                                             sp->sct_x, sp->sct_y,
87                                             I_FOOD, needed);
88             }
89         }
90         starved = feed_people(vec, etu);
91         if (starved > 0) {
92             if (!player->simulation) {
93                 /* don't report POGO starvation */
94                 if (sp->sct_own) {
95                     wu(0, sp->sct_own, "%d starved in %s.\n", starved,
96                        xyas(sp->sct_x, sp->sct_y, sp->sct_own));
97                     if (starved > 25)
98                         nreport(sp->sct_own, N_DIE_FAMINE, 0, 1);
99                 }
100                 sp->sct_work = 0;
101                 sp->sct_loyal += (random() % 8) + 2;
102             }
103             sctwork = 0;
104         } else {
105             if (sp->sct_work < 100)
106                 sctwork = sp->sct_work + 8 + (random() % 15);
107             if (sctwork > 100)
108                 sctwork = 100;
109             if (!player->simulation)
110                 sp->sct_work = sctwork;
111             grow_people(sp, etu, np, &work_avail, sctwork, vec);
112             /* age che */
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 */
118                     sp->sct_che /= 2;
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);
122             }
123         }
124     } else
125         sctwork = sp->sct_work = 100;
126     /* Here is where we truncate extra people, always */
127     trunc_people(sp, np, vec);
128
129     *workp = work_avail;
130     return sctwork;
131 }
132
133 int
134 new_work(struct sctstr *sp, int delta)
135 {
136     if (sp->sct_type == sp->sct_newtype)
137         return MIN(rollover_avail_max, sp->sct_avail) + delta;
138
139     return delta;
140 }
141
142 static int
143 growfood(struct sctstr *sp, short *vec, int work, int etu)
144 {
145     int food_fertil;
146     int food_workers;
147     int food;
148     int work_used;
149
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];
155     /*
156      * Be nice; grow minimum one food unit.
157      * This makes life simpler for the player.
158      */
159     vec[I_FOOD] += food;
160     if (vec[I_FOOD] == 0)
161         vec[I_FOOD] = 1;
162     work_used = food / fcrate;
163     return work_used;
164 }
165
166 /*
167  * returns the number who starved, if any.
168  */
169 int
170 feed_people(short *vec, int etu)
171 {
172     int to_starve, starved;
173
174     if (opt_NOFOOD)
175         return 0;
176
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));
182     if (vec[I_FOOD] < 0)
183         vec[I_FOOD] = 0;
184     return starved;
185 }
186
187 /*
188  * Return food eaten by people in VEC[] in ETU ETUs.
189  */
190 double
191 food_needed(short *vec, int etu)
192 {
193     int people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
194     double need = etu * eatrate * people;
195     return need;
196 }
197
198 /*
199  * Return number of famine victims in VEC[] for ETU ETUs.
200  */
201 int
202 famine_victims(short *vec, int etu)
203 {
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)
207         return 0;
208     if (can_eat < people / 2)
209         return people / 2;
210     return (int)(people - can_eat);
211 }
212
213 /*
214  * Starve up to NUM people of VEC[WHOM].
215  * Return the number of actually starved.
216  */
217 static int
218 starve_some(short *vec, i_type whom, int num)
219 {
220     int retval = MIN(num, vec[whom]);
221     vec[whom] -= retval;
222     return retval;
223 }
224
225 /*
226  * Truncate any extra people that may be around
227  */
228 static void
229 trunc_people(struct sctstr *sp, struct natstr *np,
230              short *vec)
231 {
232     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
233
234     if (vec[I_CIVIL] > maxpop)
235         vec[I_CIVIL] = maxpop;
236     if (vec[I_UW] > maxpop)
237         vec[I_UW] = maxpop;
238 }
239
240 /*
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?
245  */
246 static int
247 grow_people(struct sctstr *sp, int etu,
248             struct natstr *np, int *workp, int sctwork,
249             short *vec)
250 {
251     int newciv;
252     int newuw;
253     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
254
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);
258     vec[I_UW] += newuw;
259     /*
260      * subtract the baby eat food (if we are using FOOD) and return
261      * # of births.
262      */
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;
267 }
268
269 /*
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.
274  */
275 static int
276 babies(int adults, int etu, double brate, int food, int maxpop)
277 {
278     int new_birth, new_food, new;
279
280     if (adults >= maxpop)
281         return 0;
282
283     new_birth = roundavg(brate * etu * adults);
284     if (opt_NOFOOD)
285         new_food = new_birth;
286     else
287         new_food = (int)(food / (2.0 * babyeat));
288
289     new = new_birth;
290     if (new > new_food)
291         new = new_food;
292     if (adults + new > maxpop)
293         new = maxpop - adults;
294
295     return new;
296 }