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