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