]> git.pond.sub.org Git - empserver/blob - src/lib/update/ship.c
update: Use a scratch sctstr for unit repair simulation
[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     struct mchrstr *mp = &mchr[(int)ship->shp_type];
245     int delta;
246     struct sctstr *sp, scratch_sect;
247     int build;
248     int wf;
249     int avail;
250     int mult;
251
252     if (ship->shp_effic == 100)
253         return;
254
255     sp = getsectp(ship->shp_x, ship->shp_y);
256     if (sp->sct_off)
257         return;
258
259     if (sp->sct_own != 0
260         && relations_with(sp->sct_own, ship->shp_own) < FRIENDLY)
261         return;
262
263     if (player->simulation) {
264         scratch_sect = *sp;
265         bp_to_sect(bp, &scratch_sect);
266         sp = &scratch_sect;
267     }
268
269     mult = 1;
270     if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
271         mult = 2;
272
273     /* only military can work on a military boat */
274     if (mp->m_glim != 0)
275         wf = etus * ship->shp_item[I_MILIT] / 2;
276     else
277         wf = etus * (ship->shp_item[I_CIVIL] / 2 + ship->shp_item[I_MILIT] / 5);
278
279     if (sp->sct_type != SCT_HARBR) {
280         wf /= 3;
281         avail = wf;
282     } else
283         avail = wf + sp->sct_avail * 100;
284
285     delta = roundavg((double)avail / mp->m_bwork);
286     if (delta <= 0)
287         return;
288     if (delta > (int)((float)etus * ship_grow_scale))
289         delta = (int)((float)etus * ship_grow_scale);
290     if (delta > 100 - ship->shp_effic)
291         delta = 100 - ship->shp_effic;
292
293     build = get_materials(sp, mp->m_mat, delta);
294
295     if (sp->sct_type != SCT_HARBR)
296         build = delta;
297
298     wf -= build * mp->m_bwork;
299     if (wf < 0) {
300         /*
301          * I didn't use roundavg here, because I want to penalize
302          * the player with a large number of ships.
303          */
304         avail = (sp->sct_avail * 100 + wf) / 100;
305         if (avail < 0)
306             avail = 0;
307         sp->sct_avail = avail;
308     }
309     if (sp->sct_type != SCT_HARBR)
310         if ((build + ship->shp_effic) > 80) {
311             build = 80 - ship->shp_effic;
312             if (build < 0)
313                 build = 0;
314         }
315
316     bp_set_from_sect(bp, sp);
317     np->nat_money -= mult * mp->m_cost * build / 100.0;
318     if (!player->simulation)
319         ship->shp_effic += (signed char)build;
320 }
321
322 /*
323  * returns the number who starved, if any.
324  */
325 static int
326 feed_ship(struct shpstr *sp, int etus)
327 {
328     int needed, take;
329     double give;
330     struct nstr_item ni;
331     struct lndstr *lp;
332
333     if (opt_NOFOOD)
334         return 0;
335
336     needed = (int)ceil(food_needed(sp->shp_item, etus));
337
338     /* scrounge */
339     if (needed > sp->shp_item[I_FOOD]) {
340         /* take from embarked land units, but don't starve them */
341         snxtitem_cargo(&ni, EF_LAND, EF_SHIP, sp->shp_uid);
342         while ((lp = nxtitemp(&ni)) && needed > sp->shp_item[I_FOOD]) {
343             give = lp->lnd_item[I_FOOD] - food_needed(lp->lnd_item, etus);
344             if (give < 1.0)
345                 continue;
346             take = MIN((int)give, needed - sp->shp_item[I_FOOD]);
347             sp->shp_item[I_FOOD] += take;
348             lp->lnd_item[I_FOOD] -= take;
349         }
350     }
351
352     return feed_people(sp->shp_item, etus);
353 }