]> 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-2010, 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-2009
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         }
88         starved = feed_people(vec, etu);
89         if (starved > 0) {
90             if (!player->simulation) {
91                 /* don't report POGO starvation */
92                 if (sp->sct_own) {
93                     wu(0, sp->sct_own, "%d starved in %s.\n", starved,
94                        xyas(sp->sct_x, sp->sct_y, sp->sct_own));
95                     if (starved > 25)
96                         nreport(sp->sct_own, N_DIE_FAMINE, 0, 1);
97                 }
98                 sp->sct_work = 0;
99                 sp->sct_loyal += (random() % 8) + 2;
100             }
101             sctwork = 0;
102         } else {
103             if (sp->sct_work < 100)
104                 sctwork = sp->sct_work + 8 + (random() % 15);
105             if (sctwork > 100)
106                 sctwork = 100;
107             if (!player->simulation)
108                 sp->sct_work = sctwork;
109             grow_people(sp, etu, np, &work_avail, sctwork, vec);
110             /* age che */
111             if (!player->simulation)
112                 sp->sct_che = age_people(sp->sct_che, etu);
113         }
114         if (manna)
115             /* Take away food we conjured up */
116             vec[I_FOOD] = 0;
117     } else
118         sctwork = sp->sct_work = 100;
119     /* Here is where we truncate extra people, always */
120     trunc_people(sp, np, vec);
121
122     *workp = work_avail;
123     return sctwork;
124 }
125
126 int
127 new_work(struct sctstr *sp, int delta)
128 {
129     if (sp->sct_type == sp->sct_newtype)
130         return MIN(rollover_avail_max, sp->sct_avail) + delta;
131
132     return delta;
133 }
134
135 static int
136 growfood(struct sctstr *sp, short *vec, int work, int etu)
137 {
138     int food_fertil;
139     int food_workers;
140     int food;
141     int work_used;
142
143     food_workers = work * fcrate;
144     food_fertil = etu * sp->sct_fertil * fgrate;
145     food = MIN(food_workers, food_fertil);
146     if (food > ITEM_MAX - vec[I_FOOD])
147         food = ITEM_MAX - vec[I_FOOD];
148     vec[I_FOOD] += food;
149     work_used = food / fcrate;
150     return work_used;
151 }
152
153 /*
154  * returns the number who starved, if any.
155  */
156 int
157 feed_people(short *vec, int etu)
158 {
159     int to_starve, starved;
160
161     if (opt_NOFOOD)
162         return 0;
163
164     to_starve = famine_victims(vec, etu);
165     starved = starve_some(vec, I_UW, to_starve);
166     starved += starve_some(vec, I_CIVIL, to_starve - starved);
167     starved += starve_some(vec, I_MILIT, to_starve - starved);
168     vec[I_FOOD] -= roundavg(food_needed(vec, etu));
169     if (vec[I_FOOD] < 0)
170         vec[I_FOOD] = 0;
171     return starved;
172 }
173
174 /*
175  * Return food eaten by people in VEC[] in ETU ETUs.
176  */
177 double
178 food_needed(short *vec, int etu)
179 {
180     int people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
181     double need = etu * eatrate * people;
182     return need;
183 }
184
185 /*
186  * Return number of famine victims in VEC[] for ETU ETUs.
187  */
188 int
189 famine_victims(short *vec, int etu)
190 {
191     double can_eat = vec[I_FOOD] / (etu * eatrate);
192     int people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
193     if (people <= can_eat)
194         return 0;
195     if (can_eat <= people / 2)
196         return people / 2;
197     return (int)(people - can_eat);
198 }
199
200 /*
201  * Starve up to NUM people of VEC[WHOM].
202  * Return the number of actually starved.
203  */
204 static int
205 starve_some(short *vec, i_type whom, int num)
206 {
207     int retval = MIN(num, vec[whom]);
208     vec[whom] -= retval;
209     return retval;
210 }
211
212 /*
213  * Truncate any extra people that may be around
214  */
215 static void
216 trunc_people(struct sctstr *sp, struct natstr *np,
217              short *vec)
218 {
219     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
220
221     if (vec[I_CIVIL] > maxpop)
222         vec[I_CIVIL] = maxpop;
223     if (vec[I_UW] > maxpop)
224         vec[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,
235             struct natstr *np, int *workp, int sctwork,
236             short *vec)
237 {
238     int newciv;
239     int newuw;
240     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
241
242     newciv = babies(vec[I_CIVIL], etu, obrate, vec[I_FOOD], maxpop);
243     vec[I_CIVIL] += newciv;
244     newuw = babies(vec[I_UW], etu, uwbrate, vec[I_FOOD], maxpop);
245     vec[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         vec[I_FOOD] -= roundavg((newciv + newuw) * babyeat);
252     *workp += total_work(sctwork, etu, newciv, 0, newuw, ITEM_MAX);
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  */
262 static int
263 babies(int adults, int etu, double brate, int food, int maxpop)
264 {
265     int new_birth, new_food, new;
266
267     if (adults >= maxpop)
268         return 0;
269
270     new_birth = roundavg(brate * etu * adults);
271     if (opt_NOFOOD)
272         new_food = new_birth;
273     else
274         new_food = (int)(food / (2.0 * babyeat));
275
276     new = new_birth;
277     if (new > new_food)
278         new = new_food;
279     if (adults + new > maxpop)
280         new = maxpop - adults;
281
282     return new;
283 }