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