]> 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-2007, 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 (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, int *bp, int etus)
270 {
271     int delta;
272     struct sctstr *sp;
273     struct mchrstr *mp;
274     float leftp, buildp;
275     int left, build;
276     int lcm_needed, hcm_needed;
277     int wf;
278     int avail;
279     int w_p_eff;
280     int mult;
281     int mvec[I_MAX + 1];
282     int rel;
283
284     mp = &mchr[(int)ship->shp_type];
285     sp = getsectp(ship->shp_x, ship->shp_y);
286
287     if ((sp->sct_own != ship->shp_own) && (sp->sct_own != 0)) {
288         rel = getrel(getnatp(sp->sct_own), ship->shp_own);
289
290         if (rel < FRIENDLY)
291             return;
292     }
293
294     wf = 0;
295     /* only military can work on a military boat */
296     if (ship->shp_glim > 0)
297         wf = etus * ship->shp_item[I_MILIT] / 2;
298     else
299         wf = etus * (ship->shp_item[I_CIVIL] / 2 + ship->shp_item[I_MILIT] / 5);
300
301     if (sp->sct_type != SCT_HARBR) {
302         wf /= 3;
303         avail = wf;
304     } else {
305         if (!player->simulation)
306             avail = wf + sp->sct_avail * 100;
307         else
308             avail = wf + gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
309     }
310
311     w_p_eff = SHP_BLD_WORK(mp->m_lcm, mp->m_hcm);
312
313     if ((sp->sct_off) && (sp->sct_own == ship->shp_own))
314         return;
315
316     mult = 1;
317     if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
318         mult = 2;
319
320     if (ship->shp_effic == 100) {
321         /* ship is ok; no repairs needed */
322         return;
323     }
324
325     left = 100 - ship->shp_effic;
326     delta = roundavg((double)avail / w_p_eff);
327     if (delta <= 0)
328         return;
329     if (delta > (int)((float)etus * ship_grow_scale))
330         delta = (int)((float)etus * ship_grow_scale);
331     if (delta > left)
332         delta = left;
333
334     /* delta is the max amount we can grow */
335
336     left = 100 - ship->shp_effic;
337     if (left > delta)
338         left = delta;
339
340     leftp = left / 100.0;
341     memset(mvec, 0, sizeof(mvec));
342     mvec[I_LCM] = lcm_needed = ldround(mp->m_lcm * leftp, 1);
343     mvec[I_HCM] = hcm_needed = ldround(mp->m_hcm * leftp, 1);
344     get_materials(sp, bp, mvec, 0);
345
346     buildp = leftp;
347     if (mvec[I_LCM] < lcm_needed)
348         buildp = MIN(buildp, (float)mvec[I_LCM] / (float)mp->m_lcm);
349     if (mvec[I_HCM] < hcm_needed)
350         buildp = MIN(buildp, (float)mvec[I_HCM] / (float)mp->m_hcm);
351
352     build = ldround(buildp * 100.0, 1);
353     memset(mvec, 0, sizeof(mvec));
354     mvec[I_LCM] = roundavg(mp->m_lcm * buildp);
355     mvec[I_HCM] = roundavg(mp->m_hcm * buildp);
356     get_materials(sp, bp, mvec, 1);
357
358     if (sp->sct_type != SCT_HARBR)
359         build = delta;
360     wf -= build * w_p_eff;
361     if (wf < 0) {
362         /*
363          * I didn't use roundavg here, because I want to penalize
364          * the player with a large number of ships.
365          */
366         if (!player->simulation)
367             avail = (sp->sct_avail * 100 + wf) / 100;
368         else
369             avail = (gt_bg_nmbr(bp, sp, I_MAX + 1) * 100 + wf) / 100;
370         if (avail < 0)
371             avail = 0;
372         if (!player->simulation)
373             sp->sct_avail = avail;
374         else
375             pt_bg_nmbr(bp, sp, I_MAX + 1, avail);
376     }
377     if (sp->sct_type != SCT_HARBR)
378         if ((build + ship->shp_effic) > 80) {
379             build = 80 - ship->shp_effic;
380             if (build < 0)
381                 build = 0;
382         }
383
384     np->nat_money -= mult * mp->m_cost * build / 100.0;
385     if (!player->simulation)
386         ship->shp_effic += (signed char)build;
387 }
388
389 /*
390  * returns the number who starved, if any.
391  */
392 static int
393 feed_ship(struct shpstr *sp, int etus)
394 {
395     int needed, take;
396     double give;
397     struct nstr_item ni;
398     struct lndstr *lp;
399
400     if (opt_NOFOOD)
401         return 0;
402
403     needed = (int)ceil(food_needed(sp->shp_item, etus));
404
405     /* scrounge */
406     if (needed > sp->shp_item[I_FOOD])
407         sp->shp_item[I_FOOD] += supply_commod(sp->shp_own,
408                                         sp->shp_x, sp->shp_y, I_FOOD,
409                                         needed - sp->shp_item[I_FOOD]);
410     if (needed > sp->shp_item[I_FOOD]) {
411         /* take from embarked land units, but don't starve them */
412         snxtitem_all(&ni, EF_LAND);
413         while ((lp = nxtitemp(&ni)) && needed > sp->shp_item[I_FOOD]) {
414             if (lp->lnd_ship != sp->shp_uid)
415                 continue;
416             give = lp->lnd_item[I_FOOD] - food_needed(lp->lnd_item, etus);
417             if (give < 1.0)
418                 continue;
419             take = MIN((int)give, needed - sp->shp_item[I_FOOD]);
420             sp->shp_item[I_FOOD] += take;
421             lp->lnd_item[I_FOOD] -= take;
422         }
423     }
424
425     return feed_people(sp->shp_item, etus);
426 }