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