]> git.pond.sub.org Git - empserver/blob - src/lib/update/ship.c
782433f1ebd3554d0cc56e27522bdac2da01ce63
[empserver] / src / lib / update / ship.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  ship.c: Do production for ships
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Steve McClure, 1996
33  *     Ron Koenderink, 2004
34  *     Markus Armbruster, 2006-2009
35  */
36
37 #include <config.h>
38
39 #include <math.h>
40 #include "budg.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;
109     int mult;
110     int cost;
111     int eff;
112
113     mp = &mchr[(int)sp->shp_type];
114     if (build == 1) {
115         if (!sp->shp_off && np->nat_money >= 0)
116             shiprepair(sp, np, bp, etus);
117         if (!player->simulation)
118             sp->shp_off = 0;
119     } else {
120         mult = 1;
121         if (np->nat_level[NAT_TLEV] < sp->shp_tech * 0.85)
122             mult = 2;
123         cost = -(mult * etus * MIN(0.0, money_ship * mp->m_cost));
124         if (np->nat_money < cost && !player->simulation) {
125             if ((eff = sp->shp_effic - etus / 5) < SHIP_MINEFF) {
126                 wu(0, sp->shp_own,
127                    "%s lost to lack of maintenance\n", prship(sp));
128                 makelost(EF_SHIP, sp->shp_own, sp->shp_uid,
129                          sp->shp_x, sp->shp_y);
130                 sp->shp_own = 0;
131                 return;
132             }
133             wu(0, sp->shp_own,
134                "%s lost %d%% to lack of maintenance\n",
135                prship(sp), sp->shp_effic - eff);
136             sp->shp_effic = eff;
137         } else {
138             np->nat_money -= cost;
139         }
140
141         if (!player->simulation) {
142             sectp = getsectp(sp->shp_x, sp->shp_y);
143
144             /* produce oil */
145             if (np->nat_money >= 0
146                 && (mp->m_flags & M_OIL) && sectp->sct_type == SCT_WATER) {
147                 product = &pchr[dchr[SCT_OIL].d_prd];
148                 oil_gained = roundavg(total_work(100, etus,
149                                                  sp->shp_item[I_CIVIL],
150                                                  sp->shp_item[I_MILIT],
151                                                  sp->shp_item[I_UW],
152                                                  ITEM_MAX)
153                                       * sp->shp_effic / 100.0
154                                       * sectp->sct_oil / 100.0
155                                       * prod_eff(SCT_OIL, sp->shp_tech));
156                 max_oil = mp->m_item[I_OIL];
157                 if (sp->shp_item[I_OIL] + oil_gained > max_oil)
158                     oil_gained = max_oil - sp->shp_item[I_OIL];
159                 if (product->p_nrdep != 0 && oil_gained > 0) {
160                     resource = (unsigned char *)sectp + product->p_nrndx;
161                     if (*resource * 100 < product->p_nrdep * oil_gained)
162                         oil_gained = *resource * 100 / product->p_nrdep;
163                     dep = roundavg(oil_gained * product->p_nrdep / 100.0);
164                     if (CANT_HAPPEN(dep > *resource))
165                         dep = *resource;
166                     *resource -= dep;
167                 }
168                 sp->shp_item[I_OIL] += oil_gained;
169             }
170             /* produce fish */
171             if (np->nat_money >= 0
172                 && (mp->m_flags & M_FOOD) && sectp->sct_type == SCT_WATER) {
173                 sp->shp_item[I_FOOD]
174                     += roundavg(total_work(100, etus,
175                                            sp->shp_item[I_CIVIL],
176                                            sp->shp_item[I_MILIT],
177                                            sp->shp_item[I_UW],
178                                            ITEM_MAX)
179                                 * sp->shp_effic / 100.0
180                                 * sectp->sct_fertil / 100.0
181                                 * prod_eff(SCT_AGRI, sp->shp_tech));
182             }
183             /* feed */
184             if ((n = feed_ship(sp, etus)) > 0) {
185                 wu(0, sp->shp_own, "%d starved on %s\n", n, prship(sp));
186                 if (n > 10)
187                     nreport(sp->shp_own, N_DIE_FAMINE, 0, 1);
188             }
189             max_food = mp->m_item[I_FOOD];
190             if (sp->shp_item[I_FOOD] > max_food)
191                 sp->shp_item[I_FOOD] = max_food;
192             /*
193              * do plague stuff.  plague can't break out on ships,
194              * but it can still kill people.
195              */
196             pstage = sp->shp_pstage;
197             ptime = sp->shp_ptime;
198             if (pstage != PLG_HEALTHY) {
199                 n = plague_people(np, sp->shp_item, &pstage, &ptime, etus);
200                 switch (n) {
201                 case PLG_DYING:
202                     wu(0, sp->shp_own,
203                        "PLAGUE deaths reported on %s\n", prship(sp));
204                     nreport(sp->shp_own, N_DIE_PLAGUE, 0, 1);
205                     break;
206                 case PLG_INFECT:
207                     wu(0, sp->shp_own, "%s battling PLAGUE\n", prship(sp));
208                     break;
209                 case PLG_INCUBATE:
210                     /* Are we still incubating? */
211                     if (n == pstage) {
212                         /* Yes. Will it turn "infectious" next time? */
213                         if (ptime <= etus) {
214                             /* Yes.  Report an outbreak. */
215                             wu(0, sp->shp_own,
216                                "Outbreak of PLAGUE on %s!\n", prship(sp));
217                             nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
218                         }
219                     } else {
220                         /* It has already moved on to "infectious" */
221                         wu(0, sp->shp_own,
222                            "%s battling PLAGUE\n", prship(sp));
223                     }
224                     break;
225                 case PLG_EXPOSED:
226                     /* Has the plague moved to "incubation" yet? */
227                     if (n != pstage) {
228                         /* Yes. Will it turn "infectious" next time? */
229                         if (ptime <= etus) {
230                             /* Yes.  Report an outbreak. */
231                             wu(0, sp->shp_own,
232                                "Outbreak of PLAGUE on %s!\n", prship(sp));
233                             nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
234                         }
235                     }
236                     break;
237                 default:
238                     break;
239                 }
240
241                 sp->shp_pstage = pstage;
242                 sp->shp_ptime = ptime;
243             }
244             pops[sp->shp_own] += sp->shp_item[I_CIVIL];
245         }
246     }
247 }
248
249 /*
250  * idea is: a sector full of workers can fix up eight
251  * battleships +8 % eff each etu.  This will cost around
252  * 8 * 8 * $40 = $2560!
253  */
254 static void
255 shiprepair(struct shpstr *ship, struct natstr *np, struct bp *bp, int etus)
256 {
257     int delta;
258     struct sctstr *sp;
259     struct mchrstr *mp;
260     int build;
261     int wf;
262     int avail;
263     int w_p_eff;
264     int mult;
265     int mvec[I_MAX + 1];
266     int rel;
267
268     mp = &mchr[(int)ship->shp_type];
269     sp = getsectp(ship->shp_x, ship->shp_y);
270
271     if ((sp->sct_own != ship->shp_own) && (sp->sct_own != 0)) {
272         rel = getrel(getnatp(sp->sct_own), ship->shp_own);
273
274         if (rel < FRIENDLY)
275             return;
276     }
277
278     wf = 0;
279     /* only military can work on a military boat */
280     if (mp->m_glim != 0)
281         wf = etus * ship->shp_item[I_MILIT] / 2;
282     else
283         wf = etus * (ship->shp_item[I_CIVIL] / 2 + ship->shp_item[I_MILIT] / 5);
284
285     if (sp->sct_type != SCT_HARBR) {
286         wf /= 3;
287         avail = wf;
288     } else {
289         if (!player->simulation)
290             avail = wf + sp->sct_avail * 100;
291         else
292             avail = wf + bp_get_avail(bp, sp) * 100;
293     }
294
295     w_p_eff = SHP_BLD_WORK(mp->m_lcm, mp->m_hcm);
296
297     if ((sp->sct_off) && (sp->sct_own == ship->shp_own))
298         return;
299
300     mult = 1;
301     if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
302         mult = 2;
303
304     if (ship->shp_effic == 100) {
305         /* ship is ok; no repairs needed */
306         return;
307     }
308
309     delta = roundavg((double)avail / w_p_eff);
310     if (delta <= 0)
311         return;
312     if (delta > (int)((float)etus * ship_grow_scale))
313         delta = (int)((float)etus * ship_grow_scale);
314     if (delta > 100 - ship->shp_effic)
315         delta = 100 - ship->shp_effic;
316
317     memset(mvec, 0, sizeof(mvec));
318     mvec[I_LCM] = mp->m_lcm;
319     mvec[I_HCM] = mp->m_hcm;
320     build = get_materials(sp, bp, mvec, delta);
321
322     if (sp->sct_type != SCT_HARBR)
323         build = delta;
324
325     wf -= build * w_p_eff;
326     if (wf < 0) {
327         /*
328          * I didn't use roundavg here, because I want to penalize
329          * the player with a large number of ships.
330          */
331         if (!player->simulation)
332             avail = (sp->sct_avail * 100 + wf) / 100;
333         else
334             avail = (bp_get_avail(bp, sp) * 100 + wf) / 100;
335         if (avail < 0)
336             avail = 0;
337         if (!player->simulation)
338             sp->sct_avail = avail;
339         else
340             bp_put_avail(bp, sp, avail);
341     }
342     if (sp->sct_type != SCT_HARBR)
343         if ((build + ship->shp_effic) > 80) {
344             build = 80 - ship->shp_effic;
345             if (build < 0)
346                 build = 0;
347         }
348
349     np->nat_money -= mult * mp->m_cost * build / 100.0;
350     if (!player->simulation)
351         ship->shp_effic += (signed char)build;
352 }
353
354 /*
355  * returns the number who starved, if any.
356  */
357 static int
358 feed_ship(struct shpstr *sp, int etus)
359 {
360     int needed, take;
361     double give;
362     struct nstr_item ni;
363     struct lndstr *lp;
364
365     if (opt_NOFOOD)
366         return 0;
367
368     needed = (int)ceil(food_needed(sp->shp_item, etus));
369
370     /* scrounge */
371     if (needed > sp->shp_item[I_FOOD]) {
372         /* take from embarked land units, but don't starve them */
373         snxtitem_cargo(&ni, EF_LAND, EF_SHIP, sp->shp_uid);
374         while ((lp = nxtitemp(&ni)) && needed > sp->shp_item[I_FOOD]) {
375             give = lp->lnd_item[I_FOOD] - food_needed(lp->lnd_item, etus);
376             if (give < 1.0)
377                 continue;
378             take = MIN((int)give, needed - sp->shp_item[I_FOOD]);
379             sp->shp_item[I_FOOD] += take;
380             lp->lnd_item[I_FOOD] -= take;
381         }
382     }
383
384     return feed_people(sp->shp_item, etus);
385 }