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