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