]> git.pond.sub.org Git - empserver/blob - src/lib/update/land.c
update: Treat sanctuaries more consistently
[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 plague_land(struct lndstr *, int);
53 static void landrepair(struct lndstr *, struct natstr *, struct bp *,
54                        int, struct budget *);
55 static int feed_land(struct lndstr *, int);
56
57 void prep_lands(int etus, natid natnum)
58 {
59     int mil, i;
60     double mil_pay;
61     struct lndstr *lp;
62
63     for (i = 0; (lp = getlandp(i)); i++) {
64         if (lp->lnd_own == 0)
65             continue;
66         if (lp->lnd_own != natnum)
67             continue;
68         if (CANT_HAPPEN(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         mil = lp->lnd_item[I_MILIT];
76         mil_pay = mil * etus * money_mil;
77         nat_budget[natnum].mil.count += mil;
78         nat_budget[natnum].mil.money += mil_pay;
79         nat_budget[natnum].money += mil_pay;
80     }
81 }
82
83 void
84 prod_land(int etus, int natnum, struct bp *bp, int build)
85                 /* build = 1, maintain = 0 */
86 {
87     struct lndstr *lp;
88     int i;
89
90     for (i = 0; (lp = getlandp(i)); i++) {
91         if (lp->lnd_own == 0)
92             continue;
93         if (lp->lnd_own != natnum)
94             continue;
95         upd_land(lp, etus, bp, build);
96     }
97 }
98
99 static void
100 upd_land(struct lndstr *lp, int etus, struct bp *bp, int build)
101                /* build = 1, maintain = 0 */
102 {
103     struct budget *budget = &nat_budget[lp->lnd_own];
104     struct lchrstr *lcp = &lchr[lp->lnd_type];
105     struct natstr *np = getnatp(lp->lnd_own);
106     int min = morale_base - (int)np->nat_level[NAT_HLEV];
107     int n, mult, eff_lost;
108     double cost;
109
110     if (!player->simulation)
111         if (lp->lnd_retreat < min)
112             lp->lnd_retreat = min;
113
114     if (build == 1) {
115         if (!lp->lnd_off && budget->money >= 0)
116             landrepair(lp, np, bp, etus, budget);
117         if (!player->simulation)
118             lp->lnd_off = 0;
119     } else {
120         budget->oldowned_civs += lp->lnd_item[I_CIVIL];
121         mult = 1;
122         if (np->nat_level[NAT_TLEV] < lp->lnd_tech * 0.85)
123             mult = 2;
124         if (lcp->l_flags & L_ENGINEER)
125             mult *= 3;
126         budget->bm[BUDG_LND_MAINT].count++;
127         cost = mult * etus * -money_land * lcp->l_cost;
128         if (budget->money < cost && !player->simulation) {
129             eff_lost = etus / 5;
130             if (lp->lnd_effic - eff_lost < LAND_MINEFF)
131                 eff_lost = lp->lnd_effic - LAND_MINEFF;
132             if (eff_lost > 0) {
133                 wu(0, lp->lnd_own, "%s lost %d%% to lack of maintenance\n",
134                    prland(lp), eff_lost);
135                 lp->lnd_effic -= eff_lost;
136             }
137         } else {
138             budget->bm[BUDG_LND_MAINT].money -= cost;
139             budget->money -= cost;
140         }
141
142         if (!player->simulation) {
143             /* feed */
144             if ((n = feed_land(lp, etus)) > 0) {
145                 wu(0, lp->lnd_own, "%d starved in %s\n", n, prland(lp));
146                 if (n > 10)
147                     nreport(lp->lnd_own, N_DIE_FAMINE, 0, 1);
148             }
149             plague_land(lp, etus);
150         }                       /* end !player->simulation */
151     }
152 }
153
154 void
155 plague_land(struct lndstr *lp, int etus)
156 {
157     struct natstr *np = getnatp(lp->lnd_own);
158     int pstage, ptime;
159     int n;
160
161     /* Plague can't break out on land units, but it can still kill people */
162     pstage = lp->lnd_pstage;
163     ptime = lp->lnd_ptime;
164     if (pstage != PLG_HEALTHY) {
165         n = plague_people(np, lp->lnd_item, &pstage, &ptime, etus);
166         if (n != PLG_HEALTHY)
167             plague_report(lp->lnd_own, n, pstage, ptime, etus,
168                           "on", prland(lp));
169         lp->lnd_pstage = pstage;
170         lp->lnd_ptime = ptime;
171     }
172 }
173
174 static void
175 landrepair(struct lndstr *land, struct natstr *np, struct bp *bp, int etus,
176            struct budget *budget)
177 {
178     struct lchrstr *lp = &lchr[(int)land->lnd_type];
179     int delta;
180     struct sctstr *sp, scratch_sect;
181     int build;
182     int avail;
183     int mult;
184     double cost;
185
186     if (land->lnd_effic == 100)
187         return;
188
189     sp = getsectp(land->lnd_x, land->lnd_y);
190     if (sp->sct_off)
191         return;
192
193     if (relations_with(sp->sct_own, land->lnd_own) != ALLIED)
194         return;
195
196     if (player->simulation) {
197         scratch_sect = *sp;
198         bp_to_sect(bp, &scratch_sect);
199         sp = &scratch_sect;
200     }
201
202     mult = 1;
203     if (np->nat_level[NAT_TLEV] < land->lnd_tech * 0.85)
204         mult = 2;
205
206     avail = sp->sct_avail * 100;
207
208     delta = avail / lp->l_bwork;
209     if (delta <= 0)
210         return;
211     if (delta > (int)((float)etus * land_grow_scale))
212         delta = (int)((float)etus * land_grow_scale);
213     if (delta > 100 - land->lnd_effic)
214         delta = 100 - land->lnd_effic;
215
216     build = get_materials(sp, lp->l_mat, delta);
217
218     if ((sp->sct_type != SCT_HEADQ) && (sp->sct_type != SCT_FORTR))
219         build /= 3;
220
221     avail = roundavg((avail - build * lp->l_bwork) / 100.0);
222     if (avail < 0)
223         avail = 0;
224     sp->sct_avail = avail;
225
226     bp_set_from_sect(bp, sp);
227     cost = mult * lp->l_cost * build / 100.0;
228     budget->bm[BUDG_LND_BUILD].count += !!build;
229     budget->bm[BUDG_LND_BUILD].money -= cost;
230     budget->money -= cost;
231     if (!player->simulation)
232         land->lnd_effic += (signed char)build;
233 }
234
235 /*
236  * returns the number who starved, if any.
237  */
238 static int
239 feed_land(struct lndstr *lp, int etus)
240 {
241     return feed_people(lp->lnd_item, etus);
242 }