]> git.pond.sub.org Git - empserver/blob - src/lib/update/ship.c
update: Factor out common plague reporting
[empserver] / src / lib / update / ship.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  *  ship.c: Do production for ships
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Steve McClure, 1996
32  *     Ron Koenderink, 2004
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 "product.h"
49 #include "prototypes.h"
50 #include "ship.h"
51 #include "update.h"
52
53 static void upd_ship(struct shpstr *, int, struct bp *, int);
54 static void shiprepair(struct shpstr *, struct natstr *, struct bp *,
55                        int, struct budget *);
56 static void ship_produce(struct shpstr *, int, struct budget *);
57 static int feed_ship(struct shpstr *, int);
58
59 void
60 prod_ship(int etus, int natnum, struct bp *bp, int build)
61                 /* build = 1, maintain = 0 */
62 {
63     struct shpstr *sp;
64     int i;
65
66     for (i = 0; (sp = getshipp(i)); i++) {
67         if (sp->shp_own == 0)
68             continue;
69         if (sp->shp_own != natnum)
70             continue;
71         if (sp->shp_effic < SHIP_MINEFF) {
72             makelost(EF_SHIP, sp->shp_own, sp->shp_uid,
73                      sp->shp_x, sp->shp_y);
74             sp->shp_own = 0;
75             continue;
76         }
77
78         upd_ship(sp, etus, bp, build);
79     }
80 }
81
82 static void
83 upd_ship(struct shpstr *sp, int etus, struct bp *bp, int build)
84                /* build = 1, maintain = 0 */
85 {
86     struct budget *budget = &nat_budget[sp->shp_own];
87     struct mchrstr *mp = &mchr[sp->shp_type];
88     struct natstr *np = getnatp(sp->shp_own);
89     int pstage, ptime;
90     int n, mult, eff_lost;
91     double cost;
92
93     if (build == 1) {
94         if (!sp->shp_off && budget->money >= 0)
95             shiprepair(sp, np, bp, etus, budget);
96         ship_produce(sp, etus, budget);
97         if (!player->simulation)
98             sp->shp_off = 0;
99     } else {
100         budget->oldowned_civs += sp->shp_item[I_CIVIL];
101         mult = 1;
102         if (np->nat_level[NAT_TLEV] < sp->shp_tech * 0.85)
103             mult = 2;
104         budget->bm[BUDG_SHP_MAINT].count++;
105         cost = mult * etus * -money_ship * mp->m_cost;
106         if (budget->money < cost && !player->simulation) {
107             eff_lost = etus / 5;
108             if (sp->shp_effic - eff_lost < SHIP_MINEFF)
109                 eff_lost = sp->shp_effic - SHIP_MINEFF;
110             if (eff_lost > 0) {
111                 wu(0, sp->shp_own, "%s lost %d%% to lack of maintenance\n",
112                    prship(sp), eff_lost);
113                 sp->shp_effic -= eff_lost;
114             }
115         } else {
116             budget->bm[BUDG_SHP_MAINT].money -= cost;
117             budget->money -= cost;
118         }
119
120         if (!player->simulation) {
121             /* feed */
122             if ((n = feed_ship(sp, etus)) > 0) {
123                 wu(0, sp->shp_own, "%d starved on %s\n", n, prship(sp));
124                 if (n > 10)
125                     nreport(sp->shp_own, N_DIE_FAMINE, 0, 1);
126             }
127             /*
128              * do plague stuff.  plague can't break out on ships,
129              * but it can still kill people.
130              */
131             pstage = sp->shp_pstage;
132             ptime = sp->shp_ptime;
133             if (pstage != PLG_HEALTHY) {
134                 n = plague_people(np, sp->shp_item, &pstage, &ptime, etus);
135                 if (n != PLG_HEALTHY)
136                     plague_report(sp->shp_own, n, pstage, ptime, etus,
137                                   "on", prship(sp));
138                 sp->shp_pstage = pstage;
139                 sp->shp_ptime = ptime;
140             }
141         }
142     }
143 }
144
145 /*
146  * idea is: a sector full of workers can fix up eight
147  * battleships +8 % eff each etu.  This will cost around
148  * 8 * 8 * $40 = $2560!
149  */
150 static void
151 shiprepair(struct shpstr *ship, struct natstr *np, struct bp *bp, int etus,
152            struct budget *budget)
153 {
154     struct mchrstr *mp = &mchr[(int)ship->shp_type];
155     int delta;
156     struct sctstr *sp, scratch_sect;
157     int build;
158     int wf;
159     int avail;
160     int mult;
161     double cost;
162
163     if (ship->shp_effic == 100)
164         return;
165
166     sp = getsectp(ship->shp_x, ship->shp_y);
167     if (sp->sct_off)
168         return;
169
170     if (sp->sct_own != 0
171         && relations_with(sp->sct_own, ship->shp_own) < FRIENDLY)
172         return;
173
174     if (player->simulation) {
175         scratch_sect = *sp;
176         bp_to_sect(bp, &scratch_sect);
177         sp = &scratch_sect;
178     }
179
180     mult = 1;
181     if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
182         mult = 2;
183
184     /* only military can work on a military boat */
185     if (mp->m_glim != 0)
186         wf = etus * ship->shp_item[I_MILIT] / 2;
187     else
188         wf = etus * (ship->shp_item[I_CIVIL] / 2 + ship->shp_item[I_MILIT] / 5);
189
190     if (sp->sct_type != SCT_HARBR) {
191         wf /= 3;
192         avail = wf;
193     } else
194         avail = wf + sp->sct_avail * 100;
195
196     delta = avail / mp->m_bwork;
197     if (delta <= 0)
198         return;
199     if (delta > (int)((float)etus * ship_grow_scale))
200         delta = (int)((float)etus * ship_grow_scale);
201     if (delta > 100 - ship->shp_effic)
202         delta = 100 - ship->shp_effic;
203
204     build = get_materials(sp, mp->m_mat, delta);
205
206     if (sp->sct_type != SCT_HARBR)
207         build = delta;
208
209     wf -= build * mp->m_bwork;
210     if (wf < 0) {
211         avail = roundavg((sp->sct_avail * 100 + wf) / 100.0);
212         if (avail < 0)
213             avail = 0;
214         sp->sct_avail = avail;
215     }
216     if (sp->sct_type != SCT_HARBR)
217         if ((build + ship->shp_effic) > 80) {
218             build = 80 - ship->shp_effic;
219             if (build < 0)
220                 build = 0;
221         }
222
223     bp_set_from_sect(bp, sp);
224     cost = mult * mp->m_cost * build / 100.0;
225     budget->bm[BUDG_SHP_BUILD].count += !!build;
226     budget->bm[BUDG_SHP_BUILD].money -= cost;
227     budget->money -= cost;
228     if (!player->simulation)
229         ship->shp_effic += (signed char)build;
230 }
231
232 static void
233 ship_produce(struct shpstr *sp, int etus, struct budget *budget)
234 {
235     struct mchrstr *mp = &mchr[sp->shp_type];
236     struct sctstr *sectp = getsectp(sp->shp_x, sp->shp_y);
237     int oil_gained;
238     int max_oil;
239     int max_food;
240     struct pchrstr *product;
241     unsigned char *resource;
242     int dep;
243
244     if (player->simulation)
245         return;
246
247     /* produce oil */
248     if (!sp->shp_off && budget->money >= 0
249         && (mp->m_flags & M_OIL) && sectp->sct_type == SCT_WATER) {
250         product = &pchr[dchr[SCT_OIL].d_prd];
251         oil_gained = roundavg(total_work(100, etus,
252                                          sp->shp_item[I_CIVIL],
253                                          sp->shp_item[I_MILIT],
254                                          sp->shp_item[I_UW],
255                                          ITEM_MAX)
256                               * sp->shp_effic / 100.0
257                               * sectp->sct_oil / 100.0
258                               * prod_eff(SCT_OIL, sp->shp_tech));
259         max_oil = mp->m_item[I_OIL];
260         if (sp->shp_item[I_OIL] + oil_gained > max_oil)
261             oil_gained = max_oil - sp->shp_item[I_OIL];
262         if (product->p_nrdep != 0 && oil_gained > 0) {
263             resource = (unsigned char *)sectp + product->p_nrndx;
264             if (*resource * 100 < product->p_nrdep * oil_gained)
265                 oil_gained = *resource * 100 / product->p_nrdep;
266             dep = roundavg(oil_gained * product->p_nrdep / 100.0);
267             if (CANT_HAPPEN(dep > *resource))
268                 dep = *resource;
269             *resource -= dep;
270         }
271         sp->shp_item[I_OIL] += oil_gained;
272     }
273     /* produce fish */
274     if (!sp->shp_off && budget->money >= 0
275         && (mp->m_flags & M_FOOD) && sectp->sct_type == SCT_WATER) {
276         sp->shp_item[I_FOOD]
277             += roundavg(total_work(100, etus,
278                                    sp->shp_item[I_CIVIL],
279                                    sp->shp_item[I_MILIT],
280                                    sp->shp_item[I_UW],
281                                    ITEM_MAX)
282                         * sp->shp_effic / 100.0
283                         * sectp->sct_fertil / 100.0
284                         * prod_eff(SCT_AGRI, sp->shp_tech));
285     }
286     max_food = mp->m_item[I_FOOD];
287     if (sp->shp_item[I_FOOD] > max_food)
288         sp->shp_item[I_FOOD] = max_food;
289 }
290
291 /*
292  * returns the number who starved, if any.
293  */
294 static int
295 feed_ship(struct shpstr *sp, int etus)
296 {
297     int needed, take;
298     double give;
299     struct nstr_item ni;
300     struct lndstr *lp;
301
302     if (opt_NOFOOD)
303         return 0;
304
305     needed = (int)ceil(food_needed(sp->shp_item, etus));
306
307     /* scrounge */
308     if (needed > sp->shp_item[I_FOOD]) {
309         /* take from embarked land units, but don't starve them */
310         snxtitem_cargo(&ni, EF_LAND, EF_SHIP, sp->shp_uid);
311         while ((lp = nxtitemp(&ni)) && needed > sp->shp_item[I_FOOD]) {
312             give = lp->lnd_item[I_FOOD] - food_needed(lp->lnd_item, etus);
313             if (give < 1.0)
314                 continue;
315             take = MIN((int)give, needed - sp->shp_item[I_FOOD]);
316             sp->shp_item[I_FOOD] += take;
317             lp->lnd_item[I_FOOD] -= take;
318         }
319     }
320
321     return feed_people(sp->shp_item, etus);
322 }