]> 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-2010, 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-2009
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                 lp->lnd_ship = lp->lnd_land = -1;
129                 return;
130             }
131             wu(0, lp->lnd_own,
132                "%s lost %d%% to lack of maintenance\n",
133                prland(lp), lp->lnd_effic - eff);
134             lp->lnd_effic = eff;
135         } else {
136             np->nat_money -= cost;
137         }
138
139         if (!player->simulation) {
140             /* feed */
141             if ((n = feed_land(lp, etus)) > 0) {
142                 wu(0, lp->lnd_own, "%d starved in %s%s\n",
143                    n, prland(lp),
144                    (lp->lnd_effic < LAND_MINEFF ? ", killing it" : ""));
145                 if (n > 10)
146                     nreport(lp->lnd_own, N_DIE_FAMINE, 0, 1);
147             }
148             /*
149              * do plague stuff.  plague can't break out on land units,
150              * but it can still kill people on them.
151              */
152             pstage = lp->lnd_pstage;
153             ptime = lp->lnd_ptime;
154             if (pstage != PLG_HEALTHY) {
155                 n = plague_people(np, lp->lnd_item, &pstage, &ptime, etus);
156                 switch (n) {
157                 case PLG_DYING:
158                     wu(0, lp->lnd_own,
159                        "PLAGUE deaths reported on %s\n", prland(lp));
160                     nreport(lp->lnd_own, N_DIE_PLAGUE, 0, 1);
161                     break;
162                 case PLG_INFECT:
163                     wu(0, lp->lnd_own, "%s battling PLAGUE\n", prland(lp));
164                     break;
165                 case PLG_INCUBATE:
166                     /* Are we still incubating? */
167                     if (n == pstage) {
168                         /* Yes. Will it turn "infectious" next time? */
169                         if (ptime <= etus) {
170                             /* Yes.  Report an outbreak. */
171                             wu(0, lp->lnd_own,
172                                "Outbreak of PLAGUE on %s!\n", prland(lp));
173                             nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
174                         }
175                     } else {
176                         /* It has already moved on to "infectious" */
177                         wu(0, lp->lnd_own,
178                            "%s battling PLAGUE\n", prland(lp));
179                     }
180                     break;
181                 case PLG_EXPOSED:
182                     /* Has the plague moved to "incubation" yet? */
183                     if (n != pstage) {
184                         /* Yes. Will it turn "infectious" next time? */
185                         if (ptime <= etus) {
186                             /* Yes.  Report an outbreak. */
187                             wu(0, lp->lnd_own,
188                                "Outbreak of PLAGUE on %s!\n", prland(lp));
189                             nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
190                         }
191                     }
192                     break;
193                 default:
194                     break;
195                 }
196                 lp->lnd_pstage = pstage;
197                 lp->lnd_ptime = ptime;
198             }
199         }                       /* end !player->simulation */
200     }
201 }
202
203 static void
204 landrepair(struct lndstr *land, struct natstr *np, struct bp *bp, int etus)
205 {
206     int delta;
207     struct sctstr *sp;
208     struct lchrstr *lp;
209     int build;
210     int avail;
211     int w_p_eff;
212     int mult;
213     int mvec[I_MAX + 1];
214
215     lp = &lchr[(int)land->lnd_type];
216     sp = getsectp(land->lnd_x, land->lnd_y);
217     if (sp->sct_off)
218         return;
219     mult = 1;
220     if (np->nat_level[NAT_TLEV] < land->lnd_tech * 0.85)
221         mult = 2;
222
223     if (land->lnd_effic == 100) {
224         /* land is ok; no repairs needed */
225         return;
226     }
227     if ((sp->sct_own != land->lnd_own) &&
228         (getrel(getnatp(sp->sct_own), land->lnd_own) != ALLIED))
229         return;
230
231     if (!player->simulation)
232         avail = sp->sct_avail * 100;
233     else
234         avail = bp_get_avail(bp, sp) * 100;
235
236     w_p_eff = LND_BLD_WORK(lp->l_lcm, lp->l_hcm);
237     delta = roundavg((double)avail / w_p_eff);
238     if (delta <= 0)
239         return;
240     if (delta > (int)((float)etus * land_grow_scale))
241         delta = (int)((float)etus * land_grow_scale);
242     if (delta > 100 - land->lnd_effic)
243         delta = 100 - land->lnd_effic;
244
245     memset(mvec, 0, sizeof(mvec));
246     mvec[I_LCM] = lp->l_lcm;
247     mvec[I_HCM] = lp->l_hcm;
248     build = get_materials(sp, bp, mvec, delta);
249
250     if ((sp->sct_type != SCT_HEADQ) && (sp->sct_type != SCT_FORTR))
251         build /= 3;
252
253     avail -= build * w_p_eff;
254     if (avail < 0)
255         avail = 0;
256     if (!player->simulation)
257         sp->sct_avail = avail / 100;
258     else
259         bp_put_avail(bp, sp, avail / 100);
260
261     if (build < 0)
262         logerror("land unit %d building %d ! \n", land->lnd_uid, build);
263     np->nat_money -= mult * lp->l_cost * build / 100.0;
264     if (!player->simulation) {
265         land->lnd_effic += (signed char)build;
266     }
267 }
268
269 /*
270  * returns the number who starved, if any.
271  */
272 static int
273 feed_land(struct lndstr *lp, int etus)
274 {
275     return feed_people(lp->lnd_item, etus);
276 }