]> git.pond.sub.org Git - empserver/blob - src/lib/update/ship.c
Do not cast to float where usual arithmetic conversions obviously
[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  */
35
36 #include <config.h>
37
38 #include "misc.h"
39 #include "plague.h"
40 #include "sect.h"
41 #include "nat.h"
42 #include "ship.h"
43 #include "news.h"
44 #include "file.h"
45 #include "product.h"
46 #include "land.h"
47 #include "xy.h"
48 #include "nsc.h"
49 #include "optlist.h"
50 #include "player.h"
51 #include "update.h"
52 #include "common.h"
53 #include "subs.h"
54 #include "gen.h"
55 #include "lost.h"
56 #include "budg.h"
57
58 static int shiprepair(struct shpstr *, struct natstr *,
59                       int *, int);
60 static void upd_ship(struct shpstr *, int,
61                      struct natstr *, int *, int);
62
63 int
64 prod_ship(int etus, int natnum, int *bp, int build)
65                 /* build = 1, maintain = 0 */
66 {
67     struct shpstr *sp;
68     struct natstr *np;
69     int n, k = 0;
70     int start_money;
71     int lastx = 9999, lasty = 9999;
72
73     bp_enable_cachepath();
74     for (n = 0; NULL != (sp = getshipp(n)); n++) {
75         if (sp->shp_own == 0)
76             continue;
77         if (sp->shp_own != natnum)
78             continue;
79         np = getnatp(sp->shp_own);
80         start_money = np->nat_money;
81         if (lastx == 9999 || lasty == 9999) {
82             lastx = sp->shp_x;
83             lasty = sp->shp_y;
84         }
85         if (lastx != sp->shp_x || lasty != sp->shp_y) {
86             /* Reset the cache */
87             bp_disable_cachepath();
88             bp_clear_cachepath();
89             bp_enable_cachepath();
90         }
91         upd_ship(sp, etus, np, bp, build);
92         if (build && !player->simulation)       /* make sure to only autonav once */
93             nav_ship(sp);       /* autonav the ship */
94         sea_money[sp->shp_own] += np->nat_money - start_money;
95         if (!build || np->nat_money != start_money)
96             k++;
97         if (player->simulation)
98             np->nat_money = start_money;
99     }
100     bp_disable_cachepath();
101     bp_clear_cachepath();
102
103     if (opt_SAIL) {
104         if (build && !player->simulation)       /* make sure to only sail once */
105             sail_ship(natnum);
106     }
107     return k;
108 }
109
110 static void
111 upd_ship(struct shpstr *sp, int etus,
112          struct natstr *np, int *bp, int build)
113                /* build = 1, maintain = 0 */
114 {
115     struct sctstr *sectp;
116     struct mchrstr *mp;
117     int pstage, ptime;
118     int oil_gained;
119     int max_oil;
120     int max_food;
121     struct pchrstr *product;
122     unsigned char *resource;
123     int dep;
124     int n;
125     int mult;
126     int needed;
127     int cost;
128     int eff;
129
130     mp = &mchr[(int)sp->shp_type];
131     if (build == 1) {
132         if (np->nat_priorities[PRI_SBUILD] == 0 || np->nat_money < 0)
133             return;
134         if (sp->shp_effic < SHIP_MINEFF || !shiprepair(sp, np, bp, etus)) {
135             makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x,
136                      sp->shp_y);
137             sp->shp_own = 0;
138             return;
139         }
140     } else {
141         mult = 1;
142         if (np->nat_level[NAT_TLEV] < sp->shp_tech * 0.85)
143             mult = 2;
144         cost = -(mult * etus * MIN(0.0, money_ship * mp->m_cost));
145         if ((np->nat_priorities[PRI_SMAINT] == 0 || np->nat_money < cost)
146             && !player->simulation) {
147             if ((eff = sp->shp_effic - etus / 5) < SHIP_MINEFF) {
148                 wu(0, sp->shp_own,
149                    "%s lost to lack of maintenance\n", prship(sp));
150                 makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x,
151                          sp->shp_y);
152                 sp->shp_own = 0;
153                 return;
154             }
155             wu(0, sp->shp_own,
156                "%s lost %d%% to lack of maintenance\n",
157                prship(sp), sp->shp_effic - eff);
158             sp->shp_effic = eff;
159         } else {
160             np->nat_money -= cost;
161         }
162
163         if (!player->simulation) {
164             sectp = getsectp(sp->shp_x, sp->shp_y);
165
166             if ((mp->m_flags & M_OIL) && sectp->sct_type == SCT_WATER) {
167                 /*
168                  * take care of oil production
169                  */
170                 product = &pchr[P_OIL];
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                                       * (double)sp->shp_effic / 100.0
177                                       * (double)sectp->sct_oil / 100.0
178                                       * prod_eff(product, 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             if ((mp->m_flags & M_FOOD) && sectp->sct_type == SCT_WATER) {
194                 product = &pchr[P_FOOD];
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(product, sp->shp_tech));
204             }
205             if ((n = feed_ship(sp, etus, &needed, 1)) > 0) {
206                 wu(0, sp->shp_own, "%d starved on %s\n", n, prship(sp));
207                 if (n > 10)
208                     nreport(sp->shp_own, N_DIE_FAMINE, 0, 1);
209             }
210             max_food = mp->m_item[I_FOOD];
211             if (sp->shp_item[I_FOOD] > max_food)
212                 sp->shp_item[I_FOOD] = max_food;
213             /*
214              * do plague stuff.  plague can't break out on ships,
215              * but it can still kill people.
216              */
217             pstage = sp->shp_pstage;
218             ptime = sp->shp_ptime;
219             if (pstage != PLG_HEALTHY) {
220                 n = plague_people(np, sp->shp_item, &pstage, &ptime, etus);
221                 switch (n) {
222                 case PLG_DYING:
223                     wu(0, sp->shp_own,
224                        "PLAGUE deaths reported on %s\n", prship(sp));
225                     nreport(sp->shp_own, N_DIE_PLAGUE, 0, 1);
226                     break;
227                 case PLG_INFECT:
228                     wu(0, sp->shp_own, "%s battling PLAGUE\n", prship(sp));
229                     break;
230                 case PLG_INCUBATE:
231                     /* Are we still incubating? */
232                     if (n == pstage) {
233                         /* Yes. Will it turn "infectious" next time? */
234                         if (ptime <= etus) {
235                             /* Yes.  Report an outbreak. */
236                             wu(0, sp->shp_own,
237                                "Outbreak of PLAGUE on %s!\n", prship(sp));
238                             nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
239                         }
240                     } else {
241                         /* It has already moved on to "infectious" */
242                         wu(0, sp->shp_own,
243                            "%s battling PLAGUE\n", prship(sp));
244                     }
245                     break;
246                 case PLG_EXPOSED:
247                     /* Has the plague moved to "incubation" yet? */
248                     if (n != pstage) {
249                         /* Yes. Will it turn "infectious" next time? */
250                         if (ptime <= etus) {
251                             /* Yes.  Report an outbreak. */
252                             wu(0, sp->shp_own,
253                                "Outbreak of PLAGUE on %s!\n", prship(sp));
254                             nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
255                         }
256                     }
257                     break;
258                 default:
259                     break;
260                 }
261
262                 sp->shp_pstage = pstage;
263                 sp->shp_ptime = ptime;
264             }
265             pops[sp->shp_own] += sp->shp_item[I_CIVIL];
266         }
267     }
268 }
269
270 /*
271  * idea is: a sector full of workers can fix up eight
272  * battleships +8 % eff each etu.  This will cost around
273  * 8 * 8 * $40 = $2560!
274  */
275 static int
276 shiprepair(struct shpstr *ship, struct natstr *np,
277            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 1;
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 1;
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 1;
331     }
332
333     left = 100 - ship->shp_effic;
334     delta = roundavg((double)avail / w_p_eff);
335     if (delta <= 0)
336         return 1;
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
353     get_materials(sp, bp, mvec, 0);
354
355     if (mvec[I_LCM] >= lcm_needed)
356         buildp = leftp;
357     else
358         buildp = ((float)mvec[I_LCM] / (float)mp->m_lcm);
359     if (mvec[I_HCM] < hcm_needed)
360         buildp = MIN(buildp, ((float)mvec[I_HCM] / (float)mp->m_hcm));
361
362     build = ldround(buildp * 100.0, 1);
363     memset(mvec, 0, sizeof(mvec));
364     mvec[I_LCM] = lcm_needed = roundavg(mp->m_lcm * buildp);
365     mvec[I_HCM] = hcm_needed = roundavg(mp->m_hcm * buildp);
366
367     get_materials(sp, bp, mvec, 1);
368
369     if (sp->sct_type != SCT_HARBR)
370         build = delta;
371     wf -= build * w_p_eff;
372     if (wf < 0) {
373         /*
374          * I didn't use roundavg here, because I want to penalize
375          * the player with a large number of ships.
376          */
377         if (!player->simulation)
378             avail = (sp->sct_avail * 100 + wf) / 100;
379         else
380             avail = (gt_bg_nmbr(bp, sp, I_MAX + 1) * 100 + wf) / 100;
381         if (avail < 0)
382             avail = 0;
383         if (!player->simulation)
384             sp->sct_avail = avail;
385         else
386             pt_bg_nmbr(bp, sp, I_MAX + 1, avail);
387     }
388     if (sp->sct_type != SCT_HARBR)
389         if ((build + ship->shp_effic) > 80) {
390             build = 80 - ship->shp_effic;
391             if (build < 0)
392                 build = 0;
393         }
394
395     np->nat_money -= mult * mp->m_cost * build / 100.0;
396     if (!player->simulation)
397         ship->shp_effic += build;
398     return 1;
399 }
400
401 /*
402  * returns the number who starved, if any.
403  */
404 int
405 feed_ship(struct shpstr *sp, int etus, int *needed, int doit)
406 {
407     double food_eaten, land_eaten;
408     int ifood_eaten;
409     int can_eat, need;
410     int total_people;
411     int to_starve;
412     int starved;
413     struct nstr_item ni;
414     struct lndstr *lp;
415
416     if (opt_NOFOOD)
417         return 0;               /* no food no work to do */
418
419     total_people
420         = sp->shp_item[I_CIVIL] + sp->shp_item[I_MILIT] + sp->shp_item[I_UW];
421     food_eaten = etus * eatrate * total_people;
422     ifood_eaten = (int)food_eaten;
423     if (food_eaten - ifood_eaten > 0)
424         ifood_eaten++;
425     starved = 0;
426     *needed = 0;
427     if (!player->simulation && ifood_eaten > sp->shp_item[I_FOOD])
428         sp->shp_item[I_FOOD]
429             += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
430                              I_FOOD, ifood_eaten - sp->shp_item[I_FOOD]);
431
432 /* doit - only steal food from land units during the update */
433     if (ifood_eaten > sp->shp_item[I_FOOD] && sp->shp_nland > 0 && doit) {
434         snxtitem_all(&ni, EF_LAND);
435         while ((lp = nxtitemp(&ni)) && ifood_eaten > sp->shp_item[I_FOOD]) {
436             if (lp->lnd_ship != sp->shp_uid)
437                 continue;
438             need = ifood_eaten - sp->shp_item[I_FOOD];
439             land_eaten = etus * eatrate * lnd_getmil(lp);
440             if (lp->lnd_item[I_FOOD] - need > land_eaten) {
441                 sp->shp_item[I_FOOD] += need;
442                 lp->lnd_item[I_FOOD] -= need;
443             } else if (lp->lnd_item[I_FOOD] - land_eaten > 0) {
444                 sp->shp_item[I_FOOD] += lp->lnd_item[I_FOOD] - land_eaten;
445                 lp->lnd_item[I_FOOD] -= lp->lnd_item[I_FOOD] - land_eaten;
446             }
447         }
448     }
449
450     if (ifood_eaten > sp->shp_item[I_FOOD]) {
451         *needed = ifood_eaten - sp->shp_item[I_FOOD];
452         can_eat = sp->shp_item[I_FOOD] / (etus * eatrate);
453         /* only want to starve off at most 1/2 the populace. */
454         if (can_eat < total_people / 2)
455             can_eat = total_people / 2;
456
457         to_starve = total_people - can_eat;
458         while (to_starve && sp->shp_item[I_UW]) {
459             to_starve--;
460             starved++;
461             sp->shp_item[I_UW]--;
462         }
463         while (to_starve && sp->shp_item[I_CIVIL]) {
464             to_starve--;
465             starved++;
466             sp->shp_item[I_CIVIL]--;
467         }
468         while (to_starve && sp->shp_item[I_MILIT]) {
469             to_starve--;
470             starved++;
471             sp->shp_item[I_MILIT]--;
472         }
473
474         sp->shp_item[I_FOOD] = 0;
475     } else {
476         sp->shp_item[I_FOOD] -= (int)food_eaten;
477     }
478     return starved;
479 }