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