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