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