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