]> git.pond.sub.org Git - empserver/blob - src/lib/update/human.c
Fix trailing whitespace
[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
65     /* grow people & stuff */
66     sctwork = sp->sct_work;
67
68     maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
69     work_avail = new_work(sp,
70                           total_work(sctwork, etu,
71                                      vec[I_CIVIL], vec[I_MILIT], vec[I_UW],
72                                      maxpop));
73
74     if (sp->sct_type != SCT_SANCT) {
75         if (opt_NOFOOD == 0) {
76             needed = (int)ceil(food_needed(vec, etu));
77             if (vec[I_FOOD] < needed) {
78                 /* need to grow "emergency rations" */
79                 work_avail -= 2 * growfood(sp, vec, work_avail / 2, etu);
80                 /* It's twice as hard to grow those than norm */
81             }
82             if (vec[I_FOOD] < needed && sp->sct_own == sp->sct_oldown) {
83                 /* steal food from warehouses, headquarters,
84                    supply ships in port, or supply units */
85                 vec[I_FOOD] = supply_commod(sp->sct_own,
86                                             sp->sct_x, sp->sct_y,
87                                             I_FOOD, needed);
88             }
89         }
90         starved = feed_people(vec, etu);
91         if (starved > 0) {
92             if (!player->simulation) {
93                 /* don't report POGO starvation */
94                 if (sp->sct_own) {
95                     wu(0, sp->sct_own, "%d starved in %s.\n", starved,
96                        xyas(sp->sct_x, sp->sct_y, sp->sct_own));
97                     if (starved > 25)
98                         nreport(sp->sct_own, N_DIE_FAMINE, 0, 1);
99                 }
100                 sp->sct_work = 0;
101                 sp->sct_loyal += (random() % 8) + 2;
102             }
103             sctwork = 0;
104         } else {
105             if (sp->sct_work < 100)
106                 sctwork = sp->sct_work + 8 + (random() % 15);
107             if (sctwork > 100)
108                 sctwork = 100;
109             if (!player->simulation)
110                 sp->sct_work = sctwork;
111             grow_people(sp, etu, np, &work_avail, sctwork, vec);
112         }
113     } else
114         sctwork = sp->sct_work = 100;
115     /* Here is where we truncate extra people, always */
116     trunc_people(sp, np, vec);
117
118     *workp = work_avail;
119     return sctwork;
120 }
121
122 int
123 new_work(struct sctstr *sp, int delta)
124 {
125     if (sp->sct_type == sp->sct_newtype)
126         return MIN(rollover_avail_max, sp->sct_avail) + delta;
127
128     return delta;
129 }
130
131 static int
132 growfood(struct sctstr *sp, short *vec, int work, int etu)
133 {
134     int food_fertil;
135     int food_workers;
136     int food;
137     int work_used;
138
139     food_workers = work * fcrate;
140     food_fertil = etu * sp->sct_fertil * fgrate;
141     food = MIN(food_workers, food_fertil);
142     if (food > ITEM_MAX - vec[I_FOOD])
143         food = ITEM_MAX - vec[I_FOOD];
144     /*
145      * Be nice; grow minimum one food unit.
146      * This makes life simpler for the player.
147      */
148     vec[I_FOOD] += food;
149     if (vec[I_FOOD] == 0)
150         vec[I_FOOD] = 1;
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              short *vec)
220 {
221     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
222
223     if (vec[I_CIVIL] > maxpop)
224         vec[I_CIVIL] = maxpop;
225     if (vec[I_UW] > maxpop)
226         vec[I_UW] = maxpop;
227 }
228
229 /*
230  * Grow babies, and add to populace.
231  * XXX Might think about dropping in a birth
232  * rate limitation on countries with high tech
233  * production?  Maybe with just high education?
234  */
235 static int
236 grow_people(struct sctstr *sp, int etu,
237             struct natstr *np, int *workp, int sctwork,
238             short *vec)
239 {
240     int newciv;
241     int newuw;
242     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
243
244     newciv = babies(vec[I_CIVIL], etu, obrate, vec[I_FOOD], maxpop);
245     vec[I_CIVIL] += newciv;
246     newuw = babies(vec[I_UW], etu, uwbrate, vec[I_FOOD], maxpop);
247     vec[I_UW] += newuw;
248     /*
249      * subtract the baby eat food (if we are using FOOD) and return
250      * # of births.
251      */
252     if (opt_NOFOOD == 0 && (newciv || newuw))
253         vec[I_FOOD] -= roundavg((newciv + newuw) * babyeat);
254     *workp += total_work(sctwork, etu, newciv, 0, newuw, ITEM_MAX);
255     return newciv + newuw;
256 }
257
258 /*
259  * Return the number of babies born to ADULTS in ETU ETUs.
260  * BRATE is the birth rate.
261  * FOOD is the food available for growing babies.
262  * MAXPOP is the population limit.
263  */
264 static int
265 babies(int adults, int etu, double brate, int food, int maxpop)
266 {
267     int new_birth, new_food, new;
268
269     if (adults >= maxpop)
270         return 0;
271
272     new_birth = roundavg(brate * etu * adults);
273     if (opt_NOFOOD)
274         new_food = new_birth;
275     else
276         new_food = (int)(food / (2.0 * babyeat));
277
278     new = new_birth;
279     if (new > new_food)
280         new = new_food;
281     if (adults + new > maxpop)
282         new = maxpop - adults;
283
284     return new;
285 }