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