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