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