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