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