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