]> git.pond.sub.org Git - empserver/blob - src/lib/update/land.c
Import of Empire 4.2.12
[empserver] / src / lib / update / land.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  *  land.c: Do production for land units
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Thomas Ruschak, 1992
33  *     Steve McClure, 1996
34  */
35
36 #include "misc.h"
37 #include "var.h"
38 #include "sect.h"
39 #include "nat.h"
40 #include "land.h"
41 #include "ship.h"
42 #include "var.h"
43 #include "news.h"
44 #include "file.h"
45 #include "product.h"
46 #include "optlist.h"
47 #include "budg.h"
48 #include "player.h"
49 #include "update.h"
50 #include "lost.h"
51 #include "common.h"
52 #include "subs.h"
53 #include "common.h"
54 #include "gen.h"
55
56 int mil_dbl_pay;
57
58 #ifndef MIN
59 #define MIN(x,y)       ((x) > (y) ? (y) : (x))
60 #endif
61
62 void upd_land(register struct lndstr *lp, int landno, register int etus, struct natstr *np, int *bp, int build);
63
64 int
65 prod_land(int etus, int natnum, int *bp, int build)
66                 /* build = 1, maintain = 0 */
67 {
68         register struct lndstr *lp;
69         struct  sctstr *sp;
70         struct  natstr *np;
71         int     n, k=0;
72         extern  long lnd_money[MAXNOC];
73         int     start_money;
74         int     lastx = 9999, lasty = 9999;
75
76         bp_enable_cachepath();
77         for (n=0; NULL != (lp = getlandp(n)); n++) {
78                 if (lp->lnd_own == 0)
79                         continue;
80                 if (lp->lnd_own != natnum)
81                         continue;
82
83                 sp = getsectp(lp->lnd_x,lp->lnd_y);
84                 if (sp->sct_type == SCT_SANCT)
85                         continue;
86                 if (lastx == 9999 || lasty == 9999) {
87                   lastx = lp->lnd_x;
88                   lasty = lp->lnd_y;
89                 }
90                 if (lastx != lp->lnd_x || lasty != lp->lnd_y) {
91                   /* Reset the cache */
92                   bp_disable_cachepath();
93                   bp_clear_cachepath();
94                   bp_enable_cachepath();
95                 }
96                 np = getnatp(lp->lnd_own);
97                 start_money = np->nat_money;
98                 upd_land(lp, n, etus, np, bp, build);
99                 lnd_money[lp->lnd_own] += np->nat_money - start_money;
100                 if ((build && (np->nat_money != start_money)) || (!build))
101                     k++;
102                 if (player->simulation)
103                   np->nat_money = start_money;
104         }
105         bp_disable_cachepath();
106         bp_clear_cachepath();
107
108         return k;
109 }
110
111 void
112 upd_land(register struct lndstr *lp, int landno, register int etus, struct natstr *np, int *bp, int build)
113                /* build = 1, maintain = 0 */
114 {
115         extern  int morale_base;
116         struct  lchrstr *lcp;
117         int     vec[I_MAX+1];
118         int     cvec[I_MAX+1];
119         int     n;
120         double  techfact(int, double);
121         int     min = morale_base-(int)np->nat_level[NAT_HLEV];
122         int     mult;
123         extern  double money_land;
124         int     needed;
125         int     cost;
126         int     eff;
127
128         if (!player->simulation)
129           if (lp->lnd_retreat < min)
130             lp->lnd_retreat = min;
131
132         lcp = &lchr[(int)lp->lnd_type];
133         getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
134         if (build == 1){
135                 if (np->nat_priorities[PRI_LBUILD] == 0 ||
136                     np->nat_money < 0)
137                         return;
138                 if (lp->lnd_effic < LAND_MINEFF ||
139                         !(landrepair(lp,vec,np,bp,etus))){
140                     makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
141                     lp->lnd_own = 0;
142                     return;
143                 }
144         }else{
145                 mult = 1;
146                 if (np->nat_level[NAT_TLEV] < lp->lnd_tech * 0.85)
147                         mult = 2;
148                 if (lcp->l_flags & L_ENGINEER)
149                         mult *= 3;
150 /*              cost = -(mult * etus * dmin(0.0, money_land * LND_COST(lcp->l_cost, lp->lnd_tech - lcp->l_tech)));*/
151                 cost = -(mult * etus * dmin(0.0, money_land * lcp->l_cost));
152                 if ((np->nat_priorities[PRI_LMAINT] == 0 ||
153                     np->nat_money < cost) && !player->simulation) {
154                         if ((eff = lp->lnd_effic - etus/5) < LAND_MINEFF) {
155                                 wu(0, lp->lnd_own,
156                                    "%s lost to lack of maintenance\n",
157                                    prland(lp));
158                                 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
159                                 lp->lnd_own = 0;
160                                 return;
161                         }
162                         wu(0, lp->lnd_own,
163                            "%s lost %d%% to lack of maintenance\n",
164                            prland(lp), lp->lnd_effic - eff);
165                         lp->lnd_effic = eff;
166                 } else {
167                         np->nat_money -= cost;
168                 }
169 /* Mil costs are now part of regular mil costs, not maint costs */
170 /*              np->nat_money += (int) (money_mil * etus * lnd_getmil(lp));*/
171
172                 /* Grab more stuff */
173                 if ((opt_NOFOOD == 0) && !player->simulation)
174                         resupply_commod(lp,I_FOOD);
175
176                 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
177                 if (!player->simulation) {
178                    if ((n = feed_land(lp, vec, etus, &needed, 1)) > 0) {
179                         wu(0, lp->lnd_own, "%d starved in %s%s\n",
180                                 n, prland(lp),
181                                 (lp->lnd_effic < LAND_MINEFF ?
182                                 ", killing it" : ""));
183                         if (n > 10)
184                                 nreport(lp->lnd_own, N_DIE_FAMINE, 0, 1);
185                    }
186                    /*
187                     * do plague stuff.  plague can't break out on land units,
188                     * but it can still kill people on them.
189                     */
190                    getvec(VT_COND, cvec, (s_char *)lp, EF_LAND);
191                    if (cvec[C_PSTAGE] > 0) {
192                         n = plague_people(np, vec, cvec, etus);
193                         switch (n) {
194                         case PLG_DYING:
195                             wu(0, lp->lnd_own,
196                                "PLAGUE deaths reported on %s\n",
197                                prland(lp));
198                             nreport(lp->lnd_own, N_DIE_PLAGUE, 0, 1);
199                             break;
200                         case PLG_INFECT:
201                             wu(0, lp->lnd_own, "%s battling PLAGUE\n",
202                                prland(lp));
203                             break;
204                         case PLG_INCUBATE:
205                             /* Are we still incubating? */
206                             if (n == cvec[C_PSTAGE]) {
207                                 /* Yes. Will it turn "infectious" next time? */
208                                 if (cvec[C_PTIME] <= etus) {
209                                     /* Yes.  Report an outbreak. */
210                                     wu(0, lp->lnd_own,
211                                        "Outbreak of PLAGUE on %s!\n",
212                                        prland(lp));
213                                     nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
214                                 }
215                             } else {
216                                 /* It has already moved on to "infectious" */
217                                 wu(0, lp->lnd_own,
218                                    "%s battling PLAGUE\n", prland(lp));
219                             }
220                             break;
221                         case PLG_EXPOSED:
222                             /* Has the plague moved to "incubation" yet? */
223                             if (n != cvec[C_PSTAGE]) {
224                                 /* Yes. Will it turn "infectious" next time? */
225                                 if (cvec[C_PTIME] <= etus) {
226                                     /* Yes.  Report an outbreak. */
227                                     wu(0, lp->lnd_own,
228                                        "Outbreak of PLAGUE on %s!\n",
229                                        prland(lp));
230                                     nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
231                                 }
232                             }
233                             break;
234                         default:
235                             break;
236                         }
237                         putvec(VT_COND, cvec, (s_char *)lp, EF_LAND);
238                    }
239                    putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
240                 } /* end !player->simulation */
241         }
242 }
243
244 /*ARGSUSED*/
245 int
246 landrepair(register struct lndstr *land, int *vec, struct natstr *np, int *bp, int etus)
247 {
248         extern  int mil_dbl_pay;
249         extern  int land_grow_scale;
250         register int delta;
251         struct  sctstr *sp;
252         struct  lchrstr *lp;
253         float   leftp, buildp;
254         int     left, build;
255         int     mil_needed, lcm_needed, hcm_needed, gun_needed, shell_needed;
256         int     avail;
257         int     w_p_eff;
258         int     mult;
259         int     svec[I_MAX+1];
260         int     mvec[I_MAX+1];
261
262         lp = &lchr[(int)land->lnd_type];
263         sp = getsectp(land->lnd_x, land->lnd_y);
264         if (sp->sct_off)
265                 return 1;
266         getvec(VT_ITEM, svec, (s_char *)sp, EF_SECTOR);
267         mult = 1;
268         if (np->nat_level[NAT_TLEV] < land->lnd_tech * 0.85)
269                 mult = 2;
270
271         if (land->lnd_effic == 100) {
272                 /* land is ok; no repairs needed */
273                 return 1;
274         }
275         if (sp->sct_own != land->lnd_own)
276                 return 1;
277
278         if (!player->simulation)
279            avail = sp->sct_avail * 100;
280         else
281            avail = gt_bg_nmbr(bp, sp, I_MAX+1) * 100;
282         
283         w_p_eff = 20 + (lp->l_lcm + 2 * lp->l_hcm);
284         delta = roundavg((double)avail/w_p_eff);
285         if (delta <= 0)
286                 return 1;
287         if (delta > etus*land_grow_scale)
288                 delta = etus*land_grow_scale;
289
290         /* delta is the max amount we can grow */
291
292         left = 100 - land->lnd_effic;
293         if (left > delta)
294                 left = delta;
295
296         leftp = ((float)left/100.0);
297
298         bzero((s_char *)mvec, sizeof(mvec));
299         mvec[I_LCM] = lcm_needed = ldround((double)(lp->l_lcm * leftp),1);
300         mvec[I_HCM] = hcm_needed = ldround((double)(lp->l_hcm * leftp),1);
301 /*
302         mvec[I_GUN] = gun_needed = ldround((double)(lp->l_gun * leftp),1);
303         mvec[I_MILIT] = mil_needed = ldround((double)(lp->l_mil * leftp),1);
304         mvec[I_SHELL] = shell_needed = ldround((double)(lp->l_shell *leftp),1);
305  */
306                 mvec[I_GUN] = gun_needed = 0;
307         mvec[I_MILIT] = mil_needed = 0;
308         mvec[I_SHELL] = shell_needed = 0;
309
310         get_materials(sp, bp, mvec, 0);
311
312         if (mvec[I_MILIT]>=mil_needed)
313                 buildp=leftp;
314         else
315                 buildp=((float)mvec[I_MILIT]/(float)lp->l_mil);
316         if (mvec[I_LCM] < lcm_needed)
317                 buildp = MIN(buildp,((float)mvec[I_LCM]/(float)lp->l_lcm));
318         if (mvec[I_HCM] < hcm_needed)
319                 buildp = MIN(buildp,((float)mvec[I_HCM]/(float)lp->l_hcm));
320         if (mvec[I_GUN] < gun_needed)
321                 buildp = MIN(buildp,((float)mvec[I_GUN]/(float)lp->l_gun));
322         if (mvec[I_SHELL] < shell_needed)
323                 buildp = MIN(buildp,((float)mvec[I_SHELL]/(float)lp->l_shell));
324
325         build=ldround((double)(buildp*100.0),1);
326
327         bzero((s_char *)mvec, sizeof(mvec));
328         mvec[I_LCM] = lcm_needed = roundavg((double)(lp->l_lcm * buildp));
329         mvec[I_HCM] = hcm_needed = roundavg((double)(lp->l_hcm * buildp));
330 /*
331         mvec[I_GUN] = gun_needed = roundavg((double)(lp->l_gun * buildp));
332         mvec[I_MILIT] = mil_needed = roundavg((double)(lp->l_mil * buildp));
333         mvec[I_SHELL] = shell_needed = roundavg((double)(lp->l_shell *buildp));
334  */
335                 mvec[I_GUN] = gun_needed = 0;
336         mvec[I_MILIT] = mil_needed = 0;
337         mvec[I_SHELL] = shell_needed = 0;
338         mil_dbl_pay += mil_needed;
339
340         get_materials(sp, bp, mvec, 1);
341
342         if ((sp->sct_type != SCT_HEADQ) && (sp->sct_type != SCT_FORTR))
343                 build /= 3;
344
345         avail -= build * w_p_eff;
346         if (!player->simulation) {
347            sp->sct_avail = avail/100;
348            if (sp->sct_avail < 0)
349               sp->sct_avail = 0;
350         } else {
351            pt_bg_nmbr(bp, sp, I_MAX+1, avail/100);
352            if (gt_bg_nmbr(bp, sp, I_MAX+1) < 0)
353                 pt_bg_nmbr(bp, sp, I_MAX+1, 0);
354         }
355
356 if (build<0)
357         logerror("land unit %d building %d ! \n",land->lnd_uid,build);
358         np->nat_money -= mult * lp->l_cost * build / 100.0;
359         if (!player->simulation) {
360            land->lnd_effic += (s_char)build;
361
362            putsect(sp);
363         }
364         return 1;
365 }
366
367 /*
368  * returns the number who starved, if any.
369  */
370 int
371 feed_land(struct lndstr *lp, register int *vec, int etus, int *needed, int doit)
372 {
373         extern  double eatrate;
374         double  food_eaten, ship_eaten;
375         double  people_left;
376         int     can_eat, need, svec[I_MAX+1];
377         int     total_people;
378         int     starved;
379         struct  lchrstr *lcp;
380         struct  shpstr *sp;
381
382         if (opt_NOFOOD) return 0; /* no food no work to be done */
383
384         lcp = &lchr[(int)lp->lnd_type];
385
386         food_eaten = (etus * eatrate) * total_mil(lp);
387         starved = 0;
388         *needed = 0;
389         /*
390          * If we're on a ship, and we don't have enough food,
391          * get some food off the carrying ship. (Don't starve
392          * the ship, tho...
393          */
394 /* doit - Only try to take food off the ship during the update */
395         if ((food_eaten > vec[I_FOOD]) && (lp->lnd_ship >= 0) && doit){
396                 need = (int)food_eaten - vec[I_FOOD];
397                 sp = getshipp(lp->lnd_ship);
398                 getvec(VT_ITEM, svec, (s_char *)sp, EF_SHIP);
399                 ship_eaten = (etus * eatrate) *
400                         (svec[I_CIVIL]+svec[I_MILIT]+svec[I_UW]);
401                 if ((svec[I_FOOD]-need) > ship_eaten){
402                         vec[I_FOOD] += need;
403                         svec[I_FOOD] -= need;
404                 }
405                 else if ((svec[I_FOOD]-ship_eaten) > 0){
406                         vec[I_FOOD] += (svec[I_FOOD] - ship_eaten);
407                         svec[I_FOOD] -= (svec[I_FOOD] - ship_eaten);
408                 }
409                 putvec(VT_ITEM, svec, (s_char *)sp, EF_SHIP);
410         }
411
412         if (food_eaten > vec[I_FOOD]) {
413                 *needed = food_eaten - vec[I_FOOD];
414                 if (*needed < (food_eaten - vec[I_FOOD]))
415                         (*needed)++;
416                 can_eat = (vec[I_FOOD] / (etus * eatrate));
417                 total_people = total_mil(lp);
418                 /* only want to starve off at most 1/2 the populace. */
419                 if (can_eat < (total_people/2))
420                         can_eat = total_people/2;
421
422                 people_left = (vec[I_FOOD] + 0.01) / (food_eaten + 0.01);
423                 /* only want to starve off at most 1/2 the populace. */
424                 if (people_left < 0.5)
425                         people_left = 0.5;
426 /*              lp->lnd_effic *= people_left;*/
427                 starved = vec[I_MILIT] - (vec[I_MILIT] * people_left);
428 /*              if (!player->simulation)
429                         wu(0, lp->lnd_own, "%d mil starved on unit %s.\n",
430                            starved,
431                            prland(lp));*/
432                 vec[I_MILIT] -= starved;
433                 vec[I_FOOD] = 0;
434         } else {
435                 vec[I_FOOD] -= (int)food_eaten;
436         }
437         return starved;
438 }
439