]> 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-2008, 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-2007
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     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, struct bp *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 (np->nat_money >= 0
160                 && (mp->m_flags & M_OIL) && sectp->sct_type == SCT_WATER) {
161                 product = &pchr[dchr[SCT_OIL].d_prd];
162                 oil_gained = roundavg(total_work(100, etus,
163                                                  sp->shp_item[I_CIVIL],
164                                                  sp->shp_item[I_MILIT],
165                                                  sp->shp_item[I_UW],
166                                                  ITEM_MAX)
167                                       * sp->shp_effic / 100.0
168                                       * sectp->sct_oil / 100.0
169                                       * prod_eff(SCT_OIL, sp->shp_tech));
170                 max_oil = mp->m_item[I_OIL];
171                 if (sp->shp_item[I_OIL] + oil_gained > max_oil)
172                     oil_gained = max_oil - sp->shp_item[I_OIL];
173                 if (product->p_nrdep != 0 && oil_gained > 0) {
174                     resource = (unsigned char *)sectp + product->p_nrndx;
175                     if (*resource * 100 < product->p_nrdep * oil_gained)
176                         oil_gained = *resource * 100 / product->p_nrdep;
177                     dep = roundavg(oil_gained * product->p_nrdep / 100.0);
178                     if (CANT_HAPPEN(dep > *resource))
179                         dep = *resource;
180                     *resource -= dep;
181                 }
182                 sp->shp_item[I_OIL] += oil_gained;
183             }
184             /* produce fish */
185             if (np->nat_money >= 0
186                 && (mp->m_flags & M_FOOD) && sectp->sct_type == SCT_WATER) {
187                 sp->shp_item[I_FOOD]
188                     += roundavg(total_work(100, etus,
189                                            sp->shp_item[I_CIVIL],
190                                            sp->shp_item[I_MILIT],
191                                            sp->shp_item[I_UW],
192                                            ITEM_MAX)
193                                 * sp->shp_effic / 100.0
194                                 * sectp->sct_fertil / 100.0
195                                 * prod_eff(SCT_AGRI, sp->shp_tech));
196             }
197             /* feed */
198             if ((n = feed_ship(sp, etus)) > 0) {
199                 wu(0, sp->shp_own, "%d starved on %s\n", n, prship(sp));
200                 if (n > 10)
201                     nreport(sp->shp_own, N_DIE_FAMINE, 0, 1);
202             }
203             max_food = mp->m_item[I_FOOD];
204             if (sp->shp_item[I_FOOD] > max_food)
205                 sp->shp_item[I_FOOD] = max_food;
206             /*
207              * do plague stuff.  plague can't break out on ships,
208              * but it can still kill people.
209              */
210             pstage = sp->shp_pstage;
211             ptime = sp->shp_ptime;
212             if (pstage != PLG_HEALTHY) {
213                 n = plague_people(np, sp->shp_item, &pstage, &ptime, etus);
214                 switch (n) {
215                 case PLG_DYING:
216                     wu(0, sp->shp_own,
217                        "PLAGUE deaths reported on %s\n", prship(sp));
218                     nreport(sp->shp_own, N_DIE_PLAGUE, 0, 1);
219                     break;
220                 case PLG_INFECT:
221                     wu(0, sp->shp_own, "%s battling PLAGUE\n", prship(sp));
222                     break;
223                 case PLG_INCUBATE:
224                     /* Are we still incubating? */
225                     if (n == pstage) {
226                         /* Yes. Will it turn "infectious" next time? */
227                         if (ptime <= etus) {
228                             /* Yes.  Report an outbreak. */
229                             wu(0, sp->shp_own,
230                                "Outbreak of PLAGUE on %s!\n", prship(sp));
231                             nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
232                         }
233                     } else {
234                         /* It has already moved on to "infectious" */
235                         wu(0, sp->shp_own,
236                            "%s battling PLAGUE\n", prship(sp));
237                     }
238                     break;
239                 case PLG_EXPOSED:
240                     /* Has the plague moved to "incubation" yet? */
241                     if (n != pstage) {
242                         /* Yes. Will it turn "infectious" next time? */
243                         if (ptime <= etus) {
244                             /* Yes.  Report an outbreak. */
245                             wu(0, sp->shp_own,
246                                "Outbreak of PLAGUE on %s!\n", prship(sp));
247                             nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
248                         }
249                     }
250                     break;
251                 default:
252                     break;
253                 }
254
255                 sp->shp_pstage = pstage;
256                 sp->shp_ptime = ptime;
257             }
258             pops[sp->shp_own] += sp->shp_item[I_CIVIL];
259         }
260     }
261 }
262
263 /*
264  * idea is: a sector full of workers can fix up eight
265  * battleships +8 % eff each etu.  This will cost around
266  * 8 * 8 * $40 = $2560!
267  */
268 static void
269 shiprepair(struct shpstr *ship, struct natstr *np, struct bp *bp, int etus)
270 {
271     int delta;
272     struct sctstr *sp;
273     struct mchrstr *mp;
274     int build;
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 + bp_get_avail(bp, sp) * 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     delta = roundavg((double)avail / w_p_eff);
324     if (delta <= 0)
325         return;
326     if (delta > (int)((float)etus * ship_grow_scale))
327         delta = (int)((float)etus * ship_grow_scale);
328     if (delta > 100 - ship->shp_effic)
329         delta = 100 - ship->shp_effic;
330
331     memset(mvec, 0, sizeof(mvec));
332     mvec[I_LCM] = mp->m_lcm;
333     mvec[I_HCM] = mp->m_hcm;
334     build = get_materials(sp, bp, mvec, delta);
335
336     if (sp->sct_type != SCT_HARBR)
337         build = delta;
338
339     wf -= build * w_p_eff;
340     if (wf < 0) {
341         /*
342          * I didn't use roundavg here, because I want to penalize
343          * the player with a large number of ships.
344          */
345         if (!player->simulation)
346             avail = (sp->sct_avail * 100 + wf) / 100;
347         else
348             avail = (bp_get_avail(bp, sp) * 100 + wf) / 100;
349         if (avail < 0)
350             avail = 0;
351         if (!player->simulation)
352             sp->sct_avail = avail;
353         else
354             bp_put_avail(bp, sp, avail);
355     }
356     if (sp->sct_type != SCT_HARBR)
357         if ((build + ship->shp_effic) > 80) {
358             build = 80 - ship->shp_effic;
359             if (build < 0)
360                 build = 0;
361         }
362
363     np->nat_money -= mult * mp->m_cost * build / 100.0;
364     if (!player->simulation)
365         ship->shp_effic += (signed char)build;
366 }
367
368 /*
369  * returns the number who starved, if any.
370  */
371 static int
372 feed_ship(struct shpstr *sp, int etus)
373 {
374     int needed, take;
375     double give;
376     struct nstr_item ni;
377     struct lndstr *lp;
378
379     if (opt_NOFOOD)
380         return 0;
381
382     needed = (int)ceil(food_needed(sp->shp_item, etus));
383
384     /* scrounge */
385     if (needed > sp->shp_item[I_FOOD])
386         sp->shp_item[I_FOOD] += supply_commod(sp->shp_own,
387                                         sp->shp_x, sp->shp_y, I_FOOD,
388                                         needed - sp->shp_item[I_FOOD]);
389     if (needed > sp->shp_item[I_FOOD]) {
390         /* take from embarked land units, but don't starve them */
391         snxtitem_all(&ni, EF_LAND);
392         while ((lp = nxtitemp(&ni)) && needed > sp->shp_item[I_FOOD]) {
393             if (lp->lnd_ship != sp->shp_uid)
394                 continue;
395             give = lp->lnd_item[I_FOOD] - food_needed(lp->lnd_item, etus);
396             if (give < 1.0)
397                 continue;
398             take = MIN((int)give, needed - sp->shp_item[I_FOOD]);
399             sp->shp_item[I_FOOD] += take;
400             lp->lnd_item[I_FOOD] -= take;
401         }
402     }
403
404     return feed_people(sp->shp_item, etus);
405 }