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