]> git.pond.sub.org Git - empserver/blob - src/lib/update/human.c
Do not include var.h where no longer needed. Clean up register keywords in these...
[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 "sect.h"
38 #include "nat.h"
39 #include "item.h"
40 #include "news.h"
41 #include "file.h"
42 #include "xy.h"
43 #include "optlist.h"
44 #include "budg.h"
45 #include "player.h"
46 #include "update.h"
47 #include "common.h"
48 #include "gen.h"
49 #include "subs.h"
50
51 static int grow_people(struct sctstr *, int,
52                        struct natstr *, int *, int,
53                        short *);
54 static int growfood(struct sctstr *, short *, int, int);
55 static void starvation(struct sctstr *);
56 static void trunc_people(struct sctstr *, struct natstr *,
57                          short *);
58
59 /*
60  * feed the individual sector
61  *
62  */
63 int
64 do_feed(struct sctstr *sp, struct natstr *np, short *vec,
65         int *workp, int *bp, int etu)
66 {
67     int people;
68     int work_avail;
69     int starved, sctwork;
70     int needed, dummy;
71     int civvies, uws;
72     int mil;
73     int maxpop;
74
75     /* grow people & stuff */
76     sctwork = sp->sct_work;
77
78     maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
79     civvies = (vec[I_CIVIL] > maxpop) ? maxpop : vec[I_CIVIL];
80     uws = (vec[I_UW] > maxpop) ? maxpop : vec[I_UW];
81     mil = (vec[I_MILIT] > maxpop) ? maxpop : vec[I_MILIT];
82     work_avail = new_work(sp, total_work(sctwork, etu, civvies, mil, uws));
83
84     people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
85     if (sp->sct_type != SCT_SANCT) {
86         if (opt_NOFOOD == 0) {
87             if (vec[I_FOOD] < 1 + etu * people * eatrate) {
88                 /* need to grow "emergency rations" */
89                 work_avail -= (2 *
90                                growfood(sp, vec, (int)(work_avail / 2),
91                                         etu));
92                 /* It's twice as hard to grow those than norm */
93                 pt_bg_nmbr(bp, sp, I_MAX + 1, work_avail);
94                 if (!player->simulation)
95                     sp->sct_avail = work_avail;
96             }
97             if ((vec[I_FOOD] < 1 + etu * people * eatrate) &&
98                 (sp->sct_own == sp->sct_oldown)) {
99
100                 /* steal food from warehouses, headquarters,
101                    supply ships in port, or supply units */
102                 int needed;
103
104                 needed = ldround((double)(1 + etu * people * eatrate), 1);
105
106                 /* Now, find some food */
107                 vec[I_FOOD] = supply_commod(sp->sct_own, sp->sct_x,
108                                             sp->sct_y, I_FOOD, needed);
109
110             }
111         }
112         starved = feed_people(vec, etu, &needed);
113         if ((starved > 0 && sp->sct_own) && (!player->simulation)) {
114             /* don't report POGO starvation */
115             wu(0, sp->sct_own, "%d starved in %s.\n", starved,
116                xyas(sp->sct_x, sp->sct_y, sp->sct_own));
117             if (starved > 25)
118                 nreport(sp->sct_own, N_DIE_FAMINE, 0, 1);
119         }
120         if (starved > 0) {
121             if (!player->simulation)
122                 starvation(sp);
123             sctwork = 0;
124         } else {
125             if (sp->sct_work < 100)
126                 sctwork = sp->sct_work + 8 + (random() % 15);
127             if (sctwork > 100)
128                 sctwork = 100;
129             if (!player->simulation)
130                 sp->sct_work = sctwork;
131             dummy = grow_people(sp, etu, np, &work_avail, sctwork, vec);
132         }
133     } else
134         sctwork = sp->sct_work = 100;
135     /* Here is where we truncate extra people, always */
136     trunc_people(sp, np, vec);
137
138     pt_bg_nmbr(bp, sp, I_CIVIL, vec[I_CIVIL]);
139     pt_bg_nmbr(bp, sp, I_UW, vec[I_UW]);
140     pt_bg_nmbr(bp, sp, I_MILIT, vec[I_MILIT]);
141     *workp = work_avail;
142     return sctwork;
143 }
144
145 int
146 new_work(struct sctstr *sp, int delta)
147 {
148     if (sp->sct_type == sp->sct_newtype)
149         return min(rollover_avail_max, sp->sct_avail) + delta;
150
151     return delta;
152 }
153
154 static int
155 growfood(struct sctstr *sp, short *vec, int work, int etu)
156 {
157     int food_fertil;
158     int food_workers;
159     int food;
160     int work_used;
161
162     /* I'm being very nice and commenting out this so players
163      * won't whine about starvation
164      if (sp->sct_fertil == 0 || work == 0)
165      return 0;
166      */
167     food_workers = work * fcrate;
168     food_fertil = etu * sp->sct_fertil * fgrate;
169     food = min(food_workers, food_fertil);
170     /*
171      * be nice; grow minimum one food unit.
172      * This makes life simpler for the player.
173      */
174     if (food > ITEM_MAX - vec[I_FOOD])
175         food = ITEM_MAX - vec[I_FOOD];
176     vec[I_FOOD] += food;
177     if (vec[I_FOOD] == 0)
178         vec[I_FOOD] = 1;
179     work_used = food / fcrate;
180     return work_used;
181 }
182
183 /*
184  * returns the number who starved, if any.
185  */
186 int
187 feed_people(short *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, struct natstr *np,
244              short *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, int etu,
262             struct natstr *np, int *workp, int sctwork,
263             short *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 }