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