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