]> git.pond.sub.org Git - empserver/blob - src/lib/update/land.c
Update known contributors comments
[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 "ship.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 n;
98     int min = morale_base - (int)np->nat_level[NAT_HLEV];
99     int mult;
100     int cost;
101     int eff;
102
103     if (!player->simulation)
104         if (lp->lnd_retreat < min)
105             lp->lnd_retreat = min;
106
107     lcp = &lchr[(int)lp->lnd_type];
108     if (build == 1) {
109         if (!lp->lnd_off && np->nat_money >= 0)
110             landrepair(lp, np, bp, etus);
111         if (!player->simulation)
112             lp->lnd_off = 0;
113     } else {
114         mult = 1;
115         if (np->nat_level[NAT_TLEV] < lp->lnd_tech * 0.85)
116             mult = 2;
117         if (lcp->l_flags & L_ENGINEER)
118             mult *= 3;
119         cost = -(mult * etus * MIN(0.0, money_land * lcp->l_cost));
120         if (np->nat_money < cost && !player->simulation) {
121             if ((eff = lp->lnd_effic - etus / 5) < LAND_MINEFF) {
122                 wu(0, lp->lnd_own,
123                    "%s lost to lack of maintenance\n", prland(lp));
124                 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid,
125                          lp->lnd_x, lp->lnd_y);
126                 lp->lnd_own = 0;
127                 lp->lnd_ship = lp->lnd_land = -1;
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 (relations_with(sp->sct_own, land->lnd_own) != ALLIED)
227         return;
228
229     if (!player->simulation)
230         avail = sp->sct_avail * 100;
231     else
232         avail = bp_get_avail(bp, sp) * 100;
233
234     w_p_eff = LND_BLD_WORK(lp->l_lcm, lp->l_hcm);
235     delta = roundavg((double)avail / w_p_eff);
236     if (delta <= 0)
237         return;
238     if (delta > (int)((float)etus * land_grow_scale))
239         delta = (int)((float)etus * land_grow_scale);
240     if (delta > 100 - land->lnd_effic)
241         delta = 100 - land->lnd_effic;
242
243     memset(mvec, 0, sizeof(mvec));
244     mvec[I_LCM] = lp->l_lcm;
245     mvec[I_HCM] = lp->l_hcm;
246     build = get_materials(sp, bp, mvec, delta);
247
248     if ((sp->sct_type != SCT_HEADQ) && (sp->sct_type != SCT_FORTR))
249         build /= 3;
250
251     avail -= build * w_p_eff;
252     if (avail < 0)
253         avail = 0;
254     if (!player->simulation)
255         sp->sct_avail = avail / 100;
256     else
257         bp_put_avail(bp, sp, avail / 100);
258
259     if (build < 0)
260         logerror("land unit %d building %d ! \n", land->lnd_uid, build);
261     np->nat_money -= mult * lp->l_cost * build / 100.0;
262     if (!player->simulation) {
263         land->lnd_effic += (signed char)build;
264     }
265 }
266
267 /*
268  * returns the number who starved, if any.
269  */
270 static int
271 feed_land(struct lndstr *lp, int etus)
272 {
273     return feed_people(lp->lnd_item, etus);
274 }