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