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