]> git.pond.sub.org Git - empserver/blob - src/lib/update/land.c
Don't use automatic supply to avoid starvation at the update
[empserver] / src / lib / update / land.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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  *     Markus Armbruster, 2006-2008
35  */
36
37 #include <config.h>
38
39 #include <math.h>
40 #include "budg.h"
41 #include "land.h"
42 #include "lost.h"
43 #include "news.h"
44 #include "plague.h"
45 #include "player.h"
46 #include "ship.h"
47 #include "update.h"
48
49 static void landrepair(struct lndstr *, struct natstr *, struct bp *, int);
50 static void upd_land(struct lndstr *, int, struct natstr *, struct bp *, int);
51 static int feed_land(struct lndstr *, int);
52
53 int
54 prod_land(int etus, int natnum, struct bp *bp, int build)
55                 /* build = 1, maintain = 0 */
56 {
57     struct lndstr *lp;
58     struct sctstr *sp;
59     struct natstr *np;
60     int n, k = 0;
61     int start_money;
62
63     for (n = 0; NULL != (lp = getlandp(n)); n++) {
64         if (lp->lnd_own == 0)
65             continue;
66         if (lp->lnd_own != natnum)
67             continue;
68         if (lp->lnd_effic < LAND_MINEFF) {
69             makelost(EF_LAND, lp->lnd_own, lp->lnd_uid,
70                      lp->lnd_x, lp->lnd_y);
71             lp->lnd_own = 0;
72             continue;
73         }
74
75         sp = getsectp(lp->lnd_x, lp->lnd_y);
76         if (sp->sct_type == SCT_SANCT)
77             continue;
78         np = getnatp(lp->lnd_own);
79         start_money = np->nat_money;
80         upd_land(lp, etus, np, bp, build);
81         lnd_money[lp->lnd_own] += np->nat_money - start_money;
82         if (!build || np->nat_money != start_money)
83             k++;
84         if (player->simulation)
85             np->nat_money = start_money;
86     }
87
88     return k;
89 }
90
91 static void
92 upd_land(struct lndstr *lp, int etus,
93          struct natstr *np, struct bp *bp, int build)
94                /* build = 1, maintain = 0 */
95 {
96     struct lchrstr *lcp;
97     int pstage, ptime;
98     int n;
99     int min = morale_base - (int)np->nat_level[NAT_HLEV];
100     int mult;
101     int cost;
102     int eff;
103
104     if (!player->simulation)
105         if (lp->lnd_retreat < min)
106             lp->lnd_retreat = min;
107
108     lcp = &lchr[(int)lp->lnd_type];
109     if (build == 1) {
110         if (!lp->lnd_off && np->nat_money >= 0)
111             landrepair(lp, np, bp, etus);
112         if (!player->simulation)
113             lp->lnd_off = 0;
114     } else {
115         mult = 1;
116         if (np->nat_level[NAT_TLEV] < lp->lnd_tech * 0.85)
117             mult = 2;
118         if (lcp->l_flags & L_ENGINEER)
119             mult *= 3;
120         cost = -(mult * etus * MIN(0.0, money_land * lcp->l_cost));
121         if (np->nat_money < cost && !player->simulation) {
122             if ((eff = lp->lnd_effic - etus / 5) < LAND_MINEFF) {
123                 wu(0, lp->lnd_own,
124                    "%s lost to lack of maintenance\n", prland(lp));
125                 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid,
126                          lp->lnd_x, lp->lnd_y);
127                 lp->lnd_own = 0;
128                 return;
129             }
130             wu(0, lp->lnd_own,
131                "%s lost %d%% to lack of maintenance\n",
132                prland(lp), lp->lnd_effic - eff);
133             lp->lnd_effic = eff;
134         } else {
135             np->nat_money -= cost;
136         }
137
138         if (!player->simulation) {
139             /* feed */
140             if ((n = feed_land(lp, etus)) > 0) {
141                 wu(0, lp->lnd_own, "%d starved in %s%s\n",
142                    n, prland(lp),
143                    (lp->lnd_effic < LAND_MINEFF ? ", killing it" : ""));
144                 if (n > 10)
145                     nreport(lp->lnd_own, N_DIE_FAMINE, 0, 1);
146             }
147             /*
148              * do plague stuff.  plague can't break out on land units,
149              * but it can still kill people on them.
150              */
151             pstage = lp->lnd_pstage;
152             ptime = lp->lnd_ptime;
153             if (pstage != PLG_HEALTHY) {
154                 n = plague_people(np, lp->lnd_item, &pstage, &ptime, etus);
155                 switch (n) {
156                 case PLG_DYING:
157                     wu(0, lp->lnd_own,
158                        "PLAGUE deaths reported on %s\n", prland(lp));
159                     nreport(lp->lnd_own, N_DIE_PLAGUE, 0, 1);
160                     break;
161                 case PLG_INFECT:
162                     wu(0, lp->lnd_own, "%s battling PLAGUE\n", prland(lp));
163                     break;
164                 case PLG_INCUBATE:
165                     /* Are we still incubating? */
166                     if (n == pstage) {
167                         /* Yes. Will it turn "infectious" next time? */
168                         if (ptime <= etus) {
169                             /* Yes.  Report an outbreak. */
170                             wu(0, lp->lnd_own,
171                                "Outbreak of PLAGUE on %s!\n", prland(lp));
172                             nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
173                         }
174                     } else {
175                         /* It has already moved on to "infectious" */
176                         wu(0, lp->lnd_own,
177                            "%s battling PLAGUE\n", prland(lp));
178                     }
179                     break;
180                 case PLG_EXPOSED:
181                     /* Has the plague moved to "incubation" yet? */
182                     if (n != pstage) {
183                         /* Yes. Will it turn "infectious" next time? */
184                         if (ptime <= etus) {
185                             /* Yes.  Report an outbreak. */
186                             wu(0, lp->lnd_own,
187                                "Outbreak of PLAGUE on %s!\n", prland(lp));
188                             nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
189                         }
190                     }
191                     break;
192                 default:
193                     break;
194                 }
195                 lp->lnd_pstage = pstage;
196                 lp->lnd_ptime = ptime;
197             }
198         }                       /* end !player->simulation */
199     }
200 }
201
202 static void
203 landrepair(struct lndstr *land, struct natstr *np, struct bp *bp, int etus)
204 {
205     int delta;
206     struct sctstr *sp;
207     struct lchrstr *lp;
208     int build;
209     int avail;
210     int w_p_eff;
211     int mult;
212     int mvec[I_MAX + 1];
213
214     lp = &lchr[(int)land->lnd_type];
215     sp = getsectp(land->lnd_x, land->lnd_y);
216     if (sp->sct_off)
217         return;
218     mult = 1;
219     if (np->nat_level[NAT_TLEV] < land->lnd_tech * 0.85)
220         mult = 2;
221
222     if (land->lnd_effic == 100) {
223         /* land is ok; no repairs needed */
224         return;
225     }
226     if ((sp->sct_own != land->lnd_own) &&
227         (getrel(getnatp(sp->sct_own), land->lnd_own) != ALLIED))
228         return;
229
230     if (!player->simulation)
231         avail = sp->sct_avail * 100;
232     else
233         avail = bp_get_avail(bp, sp) * 100;
234
235     w_p_eff = LND_BLD_WORK(lp->l_lcm, lp->l_hcm);
236     delta = roundavg((double)avail / w_p_eff);
237     if (delta <= 0)
238         return;
239     if (delta > (int)((float)etus * land_grow_scale))
240         delta = (int)((float)etus * land_grow_scale);
241     if (delta > 100 - land->lnd_effic)
242         delta = 100 - land->lnd_effic;
243
244     memset(mvec, 0, sizeof(mvec));
245     mvec[I_LCM] = lp->l_lcm;
246     mvec[I_HCM] = lp->l_hcm;
247     build = get_materials(sp, bp, mvec, delta);
248
249     if ((sp->sct_type != SCT_HEADQ) && (sp->sct_type != SCT_FORTR))
250         build /= 3;
251
252     avail -= build * w_p_eff;
253     if (avail < 0)
254         avail = 0;
255     if (!player->simulation)
256         sp->sct_avail = avail / 100;
257     else
258         bp_put_avail(bp, sp, avail / 100);
259
260     if (build < 0)
261         logerror("land unit %d building %d ! \n", land->lnd_uid, build);
262     np->nat_money -= mult * lp->l_cost * build / 100.0;
263     if (!player->simulation) {
264         land->lnd_effic += (signed char)build;
265     }
266 }
267
268 /*
269  * returns the number who starved, if any.
270  */
271 static int
272 feed_land(struct lndstr *lp, int etus)
273 {
274     return feed_people(lp->lnd_item, etus);
275 }