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