]> git.pond.sub.org Git - empserver/blob - src/lib/update/human.c
a06d34ef43f69cec5b4ad18b65ee20c0ca656f8b
[empserver] / src / lib / update / human.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, 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 growfood(struct sctstr *, short *, int, int);
53 static void starvation(struct sctstr *);
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, dummy;
69     int civvies, uws;
70     int mil;
71     int maxpop;
72
73     /* grow people & stuff */
74     sctwork = sp->sct_work;
75
76     maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
77     civvies = (vec[I_CIVIL] > maxpop) ? maxpop : vec[I_CIVIL];
78     uws = (vec[I_UW] > maxpop) ? maxpop : vec[I_UW];
79     mil = (vec[I_MILIT] > maxpop) ? maxpop : vec[I_MILIT];
80     work_avail = new_work(sp, total_work(sctwork, etu, civvies, mil, uws));
81
82     people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
83     if (sp->sct_type != SCT_SANCT) {
84         if (opt_NOFOOD == 0) {
85             if (vec[I_FOOD] < 1 + etu * people * eatrate) {
86                 /* need to grow "emergency rations" */
87                 work_avail -= (2 *
88                                growfood(sp, vec, (int)(work_avail / 2),
89                                         etu));
90                 /* It's twice as hard to grow those than norm */
91                 pt_bg_nmbr(bp, sp, I_MAX + 1, work_avail);
92                 if (!player->simulation)
93                     sp->sct_avail = work_avail;
94             }
95             if ((vec[I_FOOD] < 1 + etu * people * eatrate) &&
96                 (sp->sct_own == sp->sct_oldown)) {
97
98                 /* steal food from warehouses, headquarters,
99                    supply ships in port, or supply units */
100                 int needed;
101
102                 needed = ldround((double)(1 + etu * people * eatrate), 1);
103
104                 /* Now, find some food */
105                 vec[I_FOOD] = supply_commod(sp->sct_own, sp->sct_x,
106                                             sp->sct_y, I_FOOD, needed);
107
108             }
109         }
110         starved = feed_people(vec, etu, &needed);
111         if ((starved > 0 && sp->sct_own) && (!player->simulation)) {
112             /* don't report POGO starvation */
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         if (starved > 0) {
119             if (!player->simulation)
120                 starvation(sp);
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             dummy = 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     /* 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 = min(food_workers, food_fertil);
168     /*
169      * be nice; grow minimum one food unit.
170      * This makes life simpler for the player.
171      */
172     if (food > ITEM_MAX - vec[I_FOOD])
173         food = ITEM_MAX - vec[I_FOOD];
174     vec[I_FOOD] += food;
175     if (vec[I_FOOD] == 0)
176         vec[I_FOOD] = 1;
177     work_used = food / fcrate;
178     return work_used;
179 }
180
181 /*
182  * returns the number who starved, if any.
183  */
184 int
185 feed_people(short *vec, int etu, int *needed)
186 {
187     double food_eaten;
188     int ifood_eaten;
189     int can_eat;
190     int total_people;
191     int to_starve;
192     int starved;
193
194     if (opt_NOFOOD)
195         return 0;
196
197     total_people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
198     food_eaten = etu * eatrate * total_people;
199     ifood_eaten = (int)food_eaten;
200     if (food_eaten - ifood_eaten > 0)
201         ifood_eaten++;
202     if (ifood_eaten <= 1)
203         return 0;
204     starved = 0;
205     *needed = 0;
206     if (ifood_eaten > vec[I_FOOD]) {
207         *needed = ifood_eaten - vec[I_FOOD];
208         can_eat = vec[I_FOOD] / (etu * eatrate);
209         /* only want to starve off at most 1/2 the populace. */
210         if (can_eat < total_people / 2)
211             can_eat = total_people / 2;
212
213         to_starve = total_people - can_eat;
214         while (to_starve && vec[I_UW]) {
215             to_starve--;
216             starved++;
217             vec[I_UW]--;
218         }
219         while (to_starve && vec[I_CIVIL]) {
220             to_starve--;
221             starved++;
222             vec[I_CIVIL]--;
223         }
224         while (to_starve && vec[I_MILIT]) {
225             to_starve--;
226             starved++;
227             vec[I_MILIT]--;
228         }
229
230         vec[I_FOOD] = 0;
231     } else {
232         vec[I_FOOD] -= roundavg(food_eaten);
233     }
234     return starved;
235 }
236
237 /*
238  * Truncate any extra people that may be around
239  */
240 static void
241 trunc_people(struct sctstr *sp, struct natstr *np,
242              short *vec)
243 {
244     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
245
246     if (vec[I_CIVIL] > maxpop)
247         vec[I_CIVIL] = maxpop;
248     if (vec[I_UW] > maxpop)
249         vec[I_UW] = maxpop;
250 }
251
252 /*
253  * Grow babies, and add to populace.
254  * XXX Might think about dropping in a birth
255  * rate limitation on countries with high tech
256  * production?  Maybe with just high education?
257  */
258 static int
259 grow_people(struct sctstr *sp, int etu,
260             struct natstr *np, int *workp, int sctwork,
261             short *vec)
262 {
263     int newciv;
264     int newuw;
265     int new_birth;
266     int new_food;
267     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
268
269     newciv = 0;
270     newuw = 0;
271     if (vec[I_CIVIL] < maxpop) {
272         new_birth = (int)roundavg(obrate * (double)(etu * vec[I_CIVIL]));
273         if (opt_NOFOOD)
274             new_food = (int)(0.5 + maxpop / (2.0 * babyeat));
275         else                    /* we are using food */
276             new_food = (int)(0.5 + vec[I_FOOD] / (2.0 * babyeat));
277
278         newciv = new_birth;
279         if (newciv > new_food)
280             newciv = new_food;
281         /* Now, check max pops */
282         if ((vec[I_CIVIL] + newciv) > maxpop)
283             newciv = maxpop - vec[I_CIVIL];
284         vec[I_CIVIL] += newciv;
285     }
286     if (vec[I_UW] < maxpop) {
287         /*
288          * now grow uw's
289          */
290         new_birth = (int)roundavg(uwbrate * (double)(etu * vec[I_UW]));
291         if (opt_NOFOOD)
292             new_food = (int)(0.5 + maxpop / (2.0 * babyeat));
293         else                    /* food is important */
294             new_food = (int)(0.5 + vec[I_FOOD] / (2.0 * babyeat));
295
296         newuw = new_birth;
297         if (newuw > new_food)
298             newuw = new_food;
299         /* Now, check max pops */
300         if ((vec[I_UW] + newuw) > maxpop)
301             newuw = maxpop - vec[I_UW];
302         vec[I_UW] += newuw;
303     }
304     /*
305      * subtract the baby eat food (if we are using FOOD) and return
306      * # of births.
307      */
308     if (opt_NOFOOD == 0 && (newciv || newuw))
309         vec[I_FOOD] -= roundavg((newciv + newuw) * babyeat);
310     *workp += total_work(sctwork, etu, newciv, 0, newuw);
311     return newciv + newuw;
312 }
313
314 /*
315  * percentage of people who starved
316  */
317 static void
318 starvation(struct sctstr *sp)
319 {
320     sp->sct_work = 0;
321     sp->sct_loyal += (random() % 8) + 2;
322 }