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