]> git.pond.sub.org Git - empserver/blob - src/lib/update/human.c
Don't produce food without work
[empserver] / src / lib / update / human.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  human.c: Food related functions
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Steve McClure, 1996
33  *     Markus Armbruster, 2004-2008
34  */
35
36 #include <config.h>
37
38 #include <math.h>
39 #include "budg.h"
40 #include "item.h"
41 #include "news.h"
42 #include "player.h"
43 #include "update.h"
44 #include "xy.h"
45
46 static int growfood(struct sctstr *, short *, int, int);
47 static int starve_some(short *, i_type, int);
48 static void trunc_people(struct sctstr *, struct natstr *, short *);
49 static int grow_people(struct sctstr *, int, struct natstr *, int *, int,
50                        short *);
51 static int babies(int, int, double, int, int);
52
53 /*
54  * feed the individual sector
55  */
56 int
57 do_feed(struct sctstr *sp, struct natstr *np, short *vec,
58         int *workp, int etu)
59 {
60     int work_avail;
61     int starved, sctwork;
62     int needed;
63     int maxpop;
64     int manna;
65
66     /* grow people & stuff */
67     sctwork = sp->sct_work;
68
69     maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
70     work_avail = new_work(sp,
71                           total_work(sctwork, etu,
72                                      vec[I_CIVIL], vec[I_MILIT], vec[I_UW],
73                                      maxpop));
74
75     if (sp->sct_type != SCT_SANCT) {
76         manna = 0;
77         if (opt_NOFOOD == 0) {
78             needed = (int)ceil(food_needed(vec, etu));
79             if (vec[I_FOOD] < needed) {
80                 /* need to grow "emergency rations" */
81                 work_avail -= 2 * growfood(sp, vec, work_avail / 2, etu);
82                 /* It's twice as hard to grow those than norm */
83                 if (vec[I_FOOD] == 0)
84                     /* Conjure up 1f to make life easier for the player */
85                     manna = vec[I_FOOD] = 1;
86             }
87             if (vec[I_FOOD] < needed && sp->sct_own == sp->sct_oldown) {
88                 /* steal food from warehouses, headquarters,
89                    supply ships in port, or supply units */
90                 vec[I_FOOD] = supply_commod(sp->sct_own,
91                                             sp->sct_x, sp->sct_y,
92                                             I_FOOD, needed);
93             }
94         }
95         starved = feed_people(vec, etu);
96         if (starved > 0) {
97             if (!player->simulation) {
98                 /* don't report POGO starvation */
99                 if (sp->sct_own) {
100                     wu(0, sp->sct_own, "%d starved in %s.\n", starved,
101                        xyas(sp->sct_x, sp->sct_y, sp->sct_own));
102                     if (starved > 25)
103                         nreport(sp->sct_own, N_DIE_FAMINE, 0, 1);
104                 }
105                 sp->sct_work = 0;
106                 sp->sct_loyal += (random() % 8) + 2;
107             }
108             sctwork = 0;
109         } else {
110             if (sp->sct_work < 100)
111                 sctwork = sp->sct_work + 8 + (random() % 15);
112             if (sctwork > 100)
113                 sctwork = 100;
114             if (!player->simulation)
115                 sp->sct_work = sctwork;
116             grow_people(sp, etu, np, &work_avail, sctwork, vec);
117         }
118         if (manna)
119             /* Take away food we conjured up */
120             vec[I_FOOD] = 0;
121     } else
122         sctwork = sp->sct_work = 100;
123     /* Here is where we truncate extra people, always */
124     trunc_people(sp, np, vec);
125
126     *workp = work_avail;
127     return sctwork;
128 }
129
130 int
131 new_work(struct sctstr *sp, int delta)
132 {
133     if (sp->sct_type == sp->sct_newtype)
134         return MIN(rollover_avail_max, sp->sct_avail) + delta;
135
136     return delta;
137 }
138
139 static int
140 growfood(struct sctstr *sp, short *vec, int work, int etu)
141 {
142     int food_fertil;
143     int food_workers;
144     int food;
145     int work_used;
146
147     food_workers = work * fcrate;
148     food_fertil = etu * sp->sct_fertil * fgrate;
149     food = MIN(food_workers, food_fertil);
150     if (food > ITEM_MAX - vec[I_FOOD])
151         food = ITEM_MAX - vec[I_FOOD];
152     vec[I_FOOD] += food;
153     work_used = food / fcrate;
154     return work_used;
155 }
156
157 /*
158  * returns the number who starved, if any.
159  */
160 int
161 feed_people(short *vec, int etu)
162 {
163     int to_starve, starved;
164
165     if (opt_NOFOOD)
166         return 0;
167
168     to_starve = famine_victims(vec, etu);
169     starved = starve_some(vec, I_UW, to_starve);
170     starved += starve_some(vec, I_CIVIL, to_starve - starved);
171     starved += starve_some(vec, I_MILIT, to_starve - starved);
172     vec[I_FOOD] -= roundavg(food_needed(vec, etu));
173     if (vec[I_FOOD] < 0)
174         vec[I_FOOD] = 0;
175     return starved;
176 }
177
178 /*
179  * Return food eaten by people in VEC[] in ETU ETUs.
180  */
181 double
182 food_needed(short *vec, int etu)
183 {
184     int people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
185     double need = etu * eatrate * people;
186     return need;
187 }
188
189 /*
190  * Return number of famine victims in VEC[] for ETU ETUs.
191  */
192 int
193 famine_victims(short *vec, int etu)
194 {
195     double can_eat = vec[I_FOOD] / (etu * eatrate);
196     int people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
197     if (people <= can_eat)
198         return 0;
199     if (can_eat <= people / 2)
200         return people / 2;
201     return (int)(people - can_eat);
202 }
203
204 /*
205  * Starve up to NUM people of VEC[WHOM].
206  * Return the number of actually starved.
207  */
208 static int
209 starve_some(short *vec, i_type whom, int num)
210 {
211     int retval = MIN(num, vec[whom]);
212     vec[whom] -= retval;
213     return retval;
214 }
215
216 /*
217  * Truncate any extra people that may be around
218  */
219 static void
220 trunc_people(struct sctstr *sp, struct natstr *np,
221              short *vec)
222 {
223     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
224
225     if (vec[I_CIVIL] > maxpop)
226         vec[I_CIVIL] = maxpop;
227     if (vec[I_UW] > maxpop)
228         vec[I_UW] = maxpop;
229 }
230
231 /*
232  * Grow babies, and add to populace.
233  * XXX Might think about dropping in a birth
234  * rate limitation on countries with high tech
235  * production?  Maybe with just high education?
236  */
237 static int
238 grow_people(struct sctstr *sp, int etu,
239             struct natstr *np, int *workp, int sctwork,
240             short *vec)
241 {
242     int newciv;
243     int newuw;
244     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
245
246     newciv = babies(vec[I_CIVIL], etu, obrate, vec[I_FOOD], maxpop);
247     vec[I_CIVIL] += newciv;
248     newuw = babies(vec[I_UW], etu, uwbrate, vec[I_FOOD], maxpop);
249     vec[I_UW] += newuw;
250     /*
251      * subtract the baby eat food (if we are using FOOD) and return
252      * # of births.
253      */
254     if (opt_NOFOOD == 0 && (newciv || newuw))
255         vec[I_FOOD] -= roundavg((newciv + newuw) * babyeat);
256     *workp += total_work(sctwork, etu, newciv, 0, newuw, ITEM_MAX);
257     return newciv + newuw;
258 }
259
260 /*
261  * Return the number of babies born to ADULTS in ETU ETUs.
262  * BRATE is the birth rate.
263  * FOOD is the food available for growing babies.
264  * MAXPOP is the population limit.
265  */
266 static int
267 babies(int adults, int etu, double brate, int food, int maxpop)
268 {
269     int new_birth, new_food, new;
270
271     if (adults >= maxpop)
272         return 0;
273
274     new_birth = roundavg(brate * etu * adults);
275     if (opt_NOFOOD)
276         new_food = new_birth;
277     else
278         new_food = (int)(food / (2.0 * babyeat));
279
280     new = new_birth;
281     if (new > new_food)
282         new = new_food;
283     if (adults + new > maxpop)
284         new = maxpop - adults;
285
286     return new;
287 }