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