]> 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-2020, 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 "optlist.h"
41 #include "nat.h"
42 #include "news.h"
43 #include "player.h"
44 #include "prototypes.h"
45 #include "sect.h"
46 #include "update.h"
47 #include "xy.h"
48
49 static int new_work(struct sctstr *, int);
50 static int growfood(struct sctstr *, int, int);
51 static int starve_some(short *, i_type, int);
52 static void trunc_people(struct sctstr *, struct natstr *);
53 static int grow_people(struct sctstr *, int, struct natstr *, int);
54 static int babies(int, int, double, int, int, int);
55
56 /*
57  * feed the individual sector
58  */
59 void
60 do_feed(struct sctstr *sp, struct natstr *np, int etu,
61         int round_babies_down)
62 {
63     int work_avail;
64     int starved, sctwork;
65     int needed;
66     int maxworkers;
67     int manna;
68
69     maxworkers = max_workers(np->nat_level[NAT_RLEV], sp);
70     work_avail = new_work(sp,
71                           total_work(sp->sct_work, etu,
72                                      sp->sct_item[I_CIVIL],
73                                      sp->sct_item[I_MILIT],
74                                      sp->sct_item[I_UW],
75                                      maxworkers));
76
77     if (sp->sct_type != SCT_WATER && sp->sct_type != SCT_SANCT) {
78         manna = 0;
79         if (opt_NOFOOD == 0) {
80             needed = (int)ceil(food_needed(sp->sct_item, etu));
81             if (sp->sct_item[I_FOOD] < needed) {
82                 /* need to grow "emergency rations" */
83                 work_avail -= 2 * growfood(sp, work_avail / 2, etu);
84                 /* It's twice as hard to grow those than norm */
85                 if (sp->sct_item[I_FOOD] == 0)
86                     /* Conjure up 1f to make life easier for the player */
87                     manna = sp->sct_item[I_FOOD] = 1;
88             }
89         }
90         starved = feed_people(sp->sct_item, 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             }
101             sp->sct_loyal += roll(8) + 1;
102             sctwork = 0;
103         } else {
104             sctwork = sp->sct_work;
105             if (sctwork < 100)
106                 sctwork += 7 + roll(15);
107             if (sctwork > 100)
108                 sctwork = 100;
109             grow_people(sp, etu, np, round_babies_down);
110             work_avail = new_work(sp,
111                                   total_work(sp->sct_work, etu,
112                                              sp->sct_item[I_CIVIL],
113                                              sp->sct_item[I_MILIT],
114                                              sp->sct_item[I_UW],
115                                              maxworkers));
116             /* FIXME restores work charged for growfood() */
117             /* age che */
118             sp->sct_che = age_people(sp->sct_che, etu);
119         }
120         if (manna)
121             /* Take away food we conjured up */
122             sp->sct_item[I_FOOD] = 0;
123     } else
124         sctwork = 100;
125
126     /* Here is where we truncate extra people, always */
127     trunc_people(sp, np);
128
129     sp->sct_work = sctwork;
130     sp->sct_avail = work_avail;
131 }
132
133 static int
134 new_work(struct sctstr *sp, int delta)
135 {
136     int rollover = sp->sct_avail;
137
138     if (sp->sct_type != sp->sct_newtype)
139         rollover = 0;
140     if (rollover > rollover_avail_max)
141         rollover = rollover_avail_max;
142     if (rollover > delta / 2 + 1)
143         rollover = delta / 2 + 1;
144
145     return rollover + delta;
146 }
147
148 static int
149 growfood(struct sctstr *sp, int work, int etu)
150 {
151     int food_fertil;
152     int food_workers;
153     int food;
154     int work_used;
155
156     food_workers = work * fcrate;
157     food_fertil = etu * sp->sct_fertil * fgrate;
158     food = MIN(food_workers, food_fertil);
159     if (food > ITEM_MAX - sp->sct_item[I_FOOD])
160         food = ITEM_MAX - sp->sct_item[I_FOOD];
161     sp->sct_item[I_FOOD] += food;
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 {
231     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
232
233     if (sp->sct_item[I_CIVIL] > maxpop)
234         sp->sct_item[I_CIVIL] = maxpop;
235     if (sp->sct_item[I_UW] > maxpop)
236         sp->sct_item[I_UW] = maxpop;
237 }
238
239 /*
240  * Grow babies, and add to populace.
241  * XXX Might think about dropping in a birth
242  * rate limitation on countries with high tech
243  * production?  Maybe with just high education?
244  */
245 static int
246 grow_people(struct sctstr *sp, int etu, struct natstr *np, int round_down)
247 {
248     int newciv;
249     int newuw;
250     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
251
252     newciv = babies(sp->sct_item[I_CIVIL], etu, obrate,
253                     sp->sct_item[I_FOOD], maxpop, round_down);
254     sp->sct_item[I_CIVIL] += newciv;
255     newuw = babies(sp->sct_item[I_UW], etu, uwbrate,
256                    sp->sct_item[I_FOOD], maxpop, round_down);
257     sp->sct_item[I_UW] += newuw;
258     /*
259      * subtract the baby eat food (if we are using FOOD) and return
260      * # of births.
261      */
262     if (opt_NOFOOD == 0 && (newciv || newuw))
263         sp->sct_item[I_FOOD] -= roundavg((newciv + newuw) * babyeat);
264     return newciv + newuw;
265 }
266
267 /*
268  * Return the number of babies born to @adults in @etu ETUs.
269  * @brate is the birth rate.
270  * @food is the food available for growing babies.
271  * @maxpop is the population limit.
272  * If @round_down, discard fractions instead of rounding them
273  * randomly.
274  */
275 static int
276 babies(int adults, int etu, double brate, int food, int maxpop,
277        int round_down)
278 {
279     double new_birth;
280     int new_food, new;
281
282     if (adults >= maxpop)
283         return 0;
284
285     new_birth = brate * etu * adults;
286     new = round_down ? (int)new_birth : roundavg(new_birth);
287
288     if (!opt_NOFOOD) {
289         new_food = (int)(food / (2.0 * babyeat));
290         if (new > new_food)
291             new = new_food;
292     }
293
294     if (adults + new > maxpop)
295         new = maxpop - adults;
296
297     return new;
298 }