]> git.pond.sub.org Git - empserver/blob - src/lib/update/human.c
Update copyright notice
[empserver] / src / lib / update / human.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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             /* age che */
118             if (!player->simulation)
119                 sp->sct_che = age_people(sp->sct_che, etu);
120         }
121         if (manna)
122             /* Take away food we conjured up */
123             vec[I_FOOD] = 0;
124     } else
125         sctwork = sp->sct_work = 100;
126     /* Here is where we truncate extra people, always */
127     trunc_people(sp, np, vec);
128
129     *workp = work_avail;
130     return sctwork;
131 }
132
133 int
134 new_work(struct sctstr *sp, int delta)
135 {
136     if (sp->sct_type == sp->sct_newtype)
137         return MIN(rollover_avail_max, sp->sct_avail) + delta;
138
139     return delta;
140 }
141
142 static int
143 growfood(struct sctstr *sp, short *vec, int work, int etu)
144 {
145     int food_fertil;
146     int food_workers;
147     int food;
148     int work_used;
149
150     food_workers = work * fcrate;
151     food_fertil = etu * sp->sct_fertil * fgrate;
152     food = MIN(food_workers, food_fertil);
153     if (food > ITEM_MAX - vec[I_FOOD])
154         food = ITEM_MAX - vec[I_FOOD];
155     vec[I_FOOD] += food;
156     work_used = food / fcrate;
157     return work_used;
158 }
159
160 /*
161  * returns the number who starved, if any.
162  */
163 int
164 feed_people(short *vec, int etu)
165 {
166     int to_starve, starved;
167
168     if (opt_NOFOOD)
169         return 0;
170
171     to_starve = famine_victims(vec, etu);
172     starved = starve_some(vec, I_UW, to_starve);
173     starved += starve_some(vec, I_CIVIL, to_starve - starved);
174     starved += starve_some(vec, I_MILIT, to_starve - starved);
175     vec[I_FOOD] -= roundavg(food_needed(vec, etu));
176     if (vec[I_FOOD] < 0)
177         vec[I_FOOD] = 0;
178     return starved;
179 }
180
181 /*
182  * Return food eaten by people in VEC[] in ETU ETUs.
183  */
184 double
185 food_needed(short *vec, int etu)
186 {
187     int people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
188     double need = etu * eatrate * people;
189     return need;
190 }
191
192 /*
193  * Return number of famine victims in VEC[] for ETU ETUs.
194  */
195 int
196 famine_victims(short *vec, int etu)
197 {
198     double can_eat = vec[I_FOOD] / (etu * eatrate);
199     int people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
200     if (people <= can_eat)
201         return 0;
202     if (can_eat <= people / 2)
203         return people / 2;
204     return (int)(people - can_eat);
205 }
206
207 /*
208  * Starve up to NUM people of VEC[WHOM].
209  * Return the number of actually starved.
210  */
211 static int
212 starve_some(short *vec, i_type whom, int num)
213 {
214     int retval = MIN(num, vec[whom]);
215     vec[whom] -= retval;
216     return retval;
217 }
218
219 /*
220  * Truncate any extra people that may be around
221  */
222 static void
223 trunc_people(struct sctstr *sp, struct natstr *np,
224              short *vec)
225 {
226     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
227
228     if (vec[I_CIVIL] > maxpop)
229         vec[I_CIVIL] = maxpop;
230     if (vec[I_UW] > maxpop)
231         vec[I_UW] = maxpop;
232 }
233
234 /*
235  * Grow babies, and add to populace.
236  * XXX Might think about dropping in a birth
237  * rate limitation on countries with high tech
238  * production?  Maybe with just high education?
239  */
240 static int
241 grow_people(struct sctstr *sp, int etu,
242             struct natstr *np, int *workp, int sctwork,
243             short *vec)
244 {
245     int newciv;
246     int newuw;
247     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
248
249     newciv = babies(vec[I_CIVIL], etu, obrate, vec[I_FOOD], maxpop);
250     vec[I_CIVIL] += newciv;
251     newuw = babies(vec[I_UW], etu, uwbrate, vec[I_FOOD], maxpop);
252     vec[I_UW] += newuw;
253     /*
254      * subtract the baby eat food (if we are using FOOD) and return
255      * # of births.
256      */
257     if (opt_NOFOOD == 0 && (newciv || newuw))
258         vec[I_FOOD] -= roundavg((newciv + newuw) * babyeat);
259     *workp += total_work(sctwork, etu, newciv, 0, newuw, ITEM_MAX);
260     return newciv + newuw;
261 }
262
263 /*
264  * Return the number of babies born to ADULTS in ETU ETUs.
265  * BRATE is the birth rate.
266  * FOOD is the food available for growing babies.
267  * MAXPOP is the population limit.
268  */
269 static int
270 babies(int adults, int etu, double brate, int food, int maxpop)
271 {
272     int new_birth, new_food, new;
273
274     if (adults >= maxpop)
275         return 0;
276
277     new_birth = roundavg(brate * etu * adults);
278     if (opt_NOFOOD)
279         new_food = new_birth;
280     else
281         new_food = (int)(food / (2.0 * babyeat));
282
283     new = new_birth;
284     if (new > new_food)
285         new = new_food;
286     if (adults + new > maxpop)
287         new = maxpop - adults;
288
289     return new;
290 }