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