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