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