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