]> git.pond.sub.org Git - empserver/blob - src/lib/update/human.c
(ship_bomb, deli, do_demo, fuel, grin, look_ship)
[empserver] / src / lib / update / human.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 <config.h>
36
37 #include "misc.h"
38 #include "sect.h"
39 #include "nat.h"
40 #include "item.h"
41 #include "news.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 babies(int, int, double, int, int);
55 static int growfood(struct sctstr *, short *, int, int);
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;
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     work_avail = new_work(sp,
78                           total_work(sctwork, etu,
79                                      vec[I_CIVIL], vec[I_MILIT], vec[I_UW],
80                                      maxpop));
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) {
112             if (!player->simulation) {
113                 /* don't report POGO starvation */
114                 if (sp->sct_own) {
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                 sp->sct_work = 0;
121                 sp->sct_loyal += (random() % 8) + 2;
122             }
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             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     food_workers = work * fcrate;
163     food_fertil = etu * sp->sct_fertil * fgrate;
164     food = MIN(food_workers, food_fertil);
165     if (food > ITEM_MAX - vec[I_FOOD])
166         food = ITEM_MAX - vec[I_FOOD];
167     /*
168      * Be nice; grow minimum one food unit.
169      * This makes life simpler for the player.
170      */
171     vec[I_FOOD] += food;
172     if (vec[I_FOOD] == 0)
173         vec[I_FOOD] = 1;
174     work_used = food / fcrate;
175     return work_used;
176 }
177
178 /*
179  * returns the number who starved, if any.
180  */
181 int
182 feed_people(short *vec, int etu, int *needed)
183 {
184     double food_eaten;
185     int ifood_eaten;
186     int can_eat;
187     int total_people;
188     int to_starve;
189     int starved;
190
191     if (opt_NOFOOD)
192         return 0;
193
194     total_people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
195     food_eaten = etu * eatrate * total_people;
196     ifood_eaten = (int)food_eaten;
197     if (food_eaten - ifood_eaten > 0)
198         ifood_eaten++;
199     if (ifood_eaten <= 1)
200         return 0;
201     starved = 0;
202     *needed = 0;
203     if (ifood_eaten > vec[I_FOOD]) {
204         *needed = ifood_eaten - vec[I_FOOD];
205         can_eat = vec[I_FOOD] / (etu * eatrate);
206         /* only want to starve off at most 1/2 the populace. */
207         if (can_eat < total_people / 2)
208             can_eat = total_people / 2;
209
210         to_starve = total_people - can_eat;
211         while (to_starve && vec[I_UW]) {
212             to_starve--;
213             starved++;
214             vec[I_UW]--;
215         }
216         while (to_starve && vec[I_CIVIL]) {
217             to_starve--;
218             starved++;
219             vec[I_CIVIL]--;
220         }
221         while (to_starve && vec[I_MILIT]) {
222             to_starve--;
223             starved++;
224             vec[I_MILIT]--;
225         }
226
227         vec[I_FOOD] = 0;
228     } else {
229         vec[I_FOOD] -= roundavg(food_eaten);
230     }
231     return starved;
232 }
233
234 /*
235  * Truncate any extra people that may be around
236  */
237 static void
238 trunc_people(struct sctstr *sp, struct natstr *np,
239              short *vec)
240 {
241     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
242
243     if (vec[I_CIVIL] > maxpop)
244         vec[I_CIVIL] = maxpop;
245     if (vec[I_UW] > maxpop)
246         vec[I_UW] = maxpop;
247 }
248
249 /*
250  * Grow babies, and add to populace.
251  * XXX Might think about dropping in a birth
252  * rate limitation on countries with high tech
253  * production?  Maybe with just high education?
254  */
255 static int
256 grow_people(struct sctstr *sp, int etu,
257             struct natstr *np, int *workp, int sctwork,
258             short *vec)
259 {
260     int newciv;
261     int newuw;
262     int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
263
264     newciv = babies(vec[I_CIVIL], etu, obrate, vec[I_FOOD], maxpop);
265     vec[I_CIVIL] += newciv;
266     newuw = babies(vec[I_UW], etu, uwbrate, vec[I_FOOD], maxpop);
267     vec[I_UW] += newuw;
268     /*
269      * subtract the baby eat food (if we are using FOOD) and return
270      * # of births.
271      */
272     if (opt_NOFOOD == 0 && (newciv || newuw))
273         vec[I_FOOD] -= roundavg((newciv + newuw) * babyeat);
274     *workp += total_work(sctwork, etu, newciv, 0, newuw, ITEM_MAX);
275     return newciv + newuw;
276 }
277
278 /*
279  * Return the number of babies born to ADULTS in ETU ETUs.
280  * BRATE is the birth rate.
281  * FOOD is the food available for growing babies.
282  * MAXPOP is the population limit.
283  */
284 static int
285 babies(int adults, int etu, double brate, int food, int maxpop)
286 {
287     int new_birth, new_food, new;
288
289     if (adults >= maxpop)
290         return 0;
291
292     new_birth = roundavg(brate * etu * adults);
293     if (opt_NOFOOD)
294         new_food = new_birth;
295     else
296         new_food = (int)(0.5 + food / (2.0 * babyeat));
297
298     new = new_birth;
299     if (new > new_food)
300         new = new_food;
301     if (adults + new > maxpop)
302         new = maxpop - adults;
303
304     return new;
305 }