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