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