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