]> git.pond.sub.org Git - empserver/blob - src/lib/update/human.c
(babies): Factored out of grow_people(). Identical, except it doesn't
[empserver] / src / lib / update / human.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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  */
34
35 #include "misc.h"
36 #include "sect.h"
37 #include "nat.h"
38 #include "item.h"
39 #include "news.h"
40 #include "xy.h"
41 #include "optlist.h"
42 #include "budg.h"
43 #include "player.h"
44 #include "update.h"
45 #include "common.h"
46 #include "gen.h"
47 #include "subs.h"
48
49 static int grow_people(struct sctstr *, int,
50                        struct natstr *, int *, int,
51                        short *);
52 static int babies(int, int, double, int, int);
53 static int growfood(struct sctstr *, short *, int, int);
54 static void trunc_people(struct sctstr *, struct natstr *,
55                          short *);
56
57 /*
58  * feed the individual sector
59  *
60  */
61 int
62 do_feed(struct sctstr *sp, struct natstr *np, short *vec,
63         int *workp, int *bp, int etu)
64 {
65     int people;
66     int work_avail;
67     int starved, sctwork;
68     int needed;
69     int maxpop;
70
71     /* grow people & stuff */
72     sctwork = sp->sct_work;
73
74     maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
75     work_avail = new_work(sp,
76                           total_work(sctwork, etu,
77                                      vec[I_CIVIL], vec[I_MILIT], vec[I_UW],
78                                      maxpop));
79
80     people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
81     if (sp->sct_type != SCT_SANCT) {
82         if (opt_NOFOOD == 0) {
83             if (vec[I_FOOD] < 1 + etu * people * eatrate) {
84                 /* need to grow "emergency rations" */
85                 work_avail -= (2 *
86                                growfood(sp, vec, (int)(work_avail / 2),
87                                         etu));
88                 /* It's twice as hard to grow those than norm */
89                 pt_bg_nmbr(bp, sp, I_MAX + 1, work_avail);
90                 if (!player->simulation)
91                     sp->sct_avail = work_avail;
92             }
93             if ((vec[I_FOOD] < 1 + etu * people * eatrate) &&
94                 (sp->sct_own == sp->sct_oldown)) {
95
96                 /* steal food from warehouses, headquarters,
97                    supply ships in port, or supply units */
98                 int needed;
99
100                 needed = ldround((double)(1 + etu * people * eatrate), 1);
101
102                 /* Now, find some food */
103                 vec[I_FOOD] = supply_commod(sp->sct_own, sp->sct_x,
104                                             sp->sct_y, I_FOOD, needed);
105
106             }
107         }
108         starved = feed_people(vec, etu, &needed);
109         if (starved > 0) {
110             if (!player->simulation) {
111                 /* don't report POGO starvation */
112                 if (sp->sct_own) {
113                     wu(0, sp->sct_own, "%d starved in %s.\n", starved,
114                        xyas(sp->sct_x, sp->sct_y, sp->sct_own));
115                     if (starved > 25)
116                         nreport(sp->sct_own, N_DIE_FAMINE, 0, 1);
117                 }
118                 sp->sct_work = 0;
119                 sp->sct_loyal += (random() % 8) + 2;
120             }
121             sctwork = 0;
122         } else {
123             if (sp->sct_work < 100)
124                 sctwork = sp->sct_work + 8 + (random() % 15);
125             if (sctwork > 100)
126                 sctwork = 100;
127             if (!player->simulation)
128                 sp->sct_work = sctwork;
129             grow_people(sp, etu, np, &work_avail, sctwork, vec);
130         }
131     } else
132         sctwork = sp->sct_work = 100;
133     /* Here is where we truncate extra people, always */
134     trunc_people(sp, np, vec);
135
136     pt_bg_nmbr(bp, sp, I_CIVIL, vec[I_CIVIL]);
137     pt_bg_nmbr(bp, sp, I_UW, vec[I_UW]);
138     pt_bg_nmbr(bp, sp, I_MILIT, vec[I_MILIT]);
139     *workp = work_avail;
140     return sctwork;
141 }
142
143 int
144 new_work(struct sctstr *sp, int delta)
145 {
146     if (sp->sct_type == sp->sct_newtype)
147         return min(rollover_avail_max, sp->sct_avail) + delta;
148
149     return delta;
150 }
151
152 static int
153 growfood(struct sctstr *sp, short *vec, int work, int etu)
154 {
155     int food_fertil;
156     int food_workers;
157     int food;
158     int work_used;
159
160     food_workers = work * fcrate;
161     food_fertil = etu * sp->sct_fertil * fgrate;
162     food = min(food_workers, food_fertil);
163     if (food > ITEM_MAX - vec[I_FOOD])
164         food = ITEM_MAX - vec[I_FOOD];
165     /*
166      * Be nice; grow minimum one food unit.
167      * This makes life simpler for the player.
168      */
169     vec[I_FOOD] += food;
170     if (vec[I_FOOD] == 0)
171         vec[I_FOOD] = 1;
172     work_used = food / fcrate;
173     return work_used;
174 }
175
176 /*
177  * returns the number who starved, if any.
178  */
179 int
180 feed_people(short *vec, int etu, int *needed)
181 {
182     double food_eaten;
183     int ifood_eaten;
184     int can_eat;
185     int total_people;
186     int to_starve;
187     int starved;
188
189     if (opt_NOFOOD)
190         return 0;
191
192     total_people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
193     food_eaten = etu * eatrate * total_people;
194     ifood_eaten = (int)food_eaten;
195     if (food_eaten - ifood_eaten > 0)
196         ifood_eaten++;
197     if (ifood_eaten <= 1)
198         return 0;
199     starved = 0;
200     *needed = 0;
201     if (ifood_eaten > vec[I_FOOD]) {
202         *needed = ifood_eaten - vec[I_FOOD];
203         can_eat = vec[I_FOOD] / (etu * eatrate);
204         /* only want to starve off at most 1/2 the populace. */
205         if (can_eat < total_people / 2)
206             can_eat = total_people / 2;
207
208         to_starve = total_people - can_eat;
209         while (to_starve && vec[I_UW]) {
210             to_starve--;
211             starved++;
212             vec[I_UW]--;
213         }
214         while (to_starve && vec[I_CIVIL]) {
215             to_starve--;
216             starved++;
217             vec[I_CIVIL]--;
218         }
219         while (to_starve && vec[I_MILIT]) {
220             to_starve--;
221             starved++;
222             vec[I_MILIT]--;
223         }
224
225         vec[I_FOOD] = 0;
226     } else {
227         vec[I_FOOD] -= roundavg(food_eaten);
228     }
229     return starved;
230 }
231
232 /*
233  * Truncate any extra people that may be around
234  */
235 static void
236 trunc_people(struct sctstr *sp, struct natstr *np,
237              short *vec)
238 {
239     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
240
241     if (vec[I_CIVIL] > maxpop)
242         vec[I_CIVIL] = maxpop;
243     if (vec[I_UW] > maxpop)
244         vec[I_UW] = maxpop;
245 }
246
247 /*
248  * Grow babies, and add to populace.
249  * XXX Might think about dropping in a birth
250  * rate limitation on countries with high tech
251  * production?  Maybe with just high education?
252  */
253 static int
254 grow_people(struct sctstr *sp, int etu,
255             struct natstr *np, int *workp, int sctwork,
256             short *vec)
257 {
258     int newciv;
259     int newuw;
260     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
261
262     newciv = babies(vec[I_CIVIL], etu, obrate, vec[I_FOOD], maxpop);
263     vec[I_CIVIL] += newciv;
264     newuw = babies(vec[I_UW], etu, uwbrate, vec[I_FOOD], maxpop);
265     vec[I_UW] += newuw;
266     /*
267      * subtract the baby eat food (if we are using FOOD) and return
268      * # of births.
269      */
270     if (opt_NOFOOD == 0 && (newciv || newuw))
271         vec[I_FOOD] -= roundavg((newciv + newuw) * babyeat);
272     *workp += total_work(sctwork, etu, newciv, 0, newuw, ITEM_MAX);
273     return newciv + newuw;
274 }
275
276 /*
277  * Return the number of babies born to ADULTS in ETU ETUs.
278  * BRATE is the birth rate.
279  * FOOD is the food available for growing babies.
280  * MAXPOP is the population limit.
281  */
282 static int
283 babies(int adults, int etu, double brate, int food, int maxpop)
284 {
285     int new_birth, new_food, new;
286
287     new_birth = roundavg(brate * etu * adults);
288     if (opt_NOFOOD)
289         new_food = new_birth;
290     else
291         new_food = (int)(0.5 + food / (2.0 * babyeat));
292
293     new = new_birth;
294     if (new > new_food)
295         new = new_food;
296     if (adults + new > maxpop)
297         new = maxpop - adults;
298
299     return new;
300 }