]> git.pond.sub.org Git - empserver/blob - src/lib/update/ship.c
Remove budget priorities:
[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 (np->nat_money >= 0)
140             shiprepair(sp, np, bp, etus);
141     } else {
142         mult = 1;
143         if (np->nat_level[NAT_TLEV] < sp->shp_tech * 0.85)
144             mult = 2;
145         cost = -(mult * etus * MIN(0.0, money_ship * mp->m_cost));
146         if (np->nat_money < cost && !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,
151                          sp->shp_x, 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             /* produce oil */
167             if ((mp->m_flags & M_OIL) && sectp->sct_type == SCT_WATER) {
168                 product = &pchr[P_OIL];
169                 oil_gained = roundavg(total_work(100, etus,
170                                                  sp->shp_item[I_CIVIL],
171                                                  sp->shp_item[I_MILIT],
172                                                  sp->shp_item[I_UW],
173                                                  ITEM_MAX)
174                                       * sp->shp_effic / 100.0
175                                       * sectp->sct_oil / 100.0
176                                       * prod_eff(product, sp->shp_tech));
177                 max_oil = mp->m_item[I_OIL];
178                 if (sp->shp_item[I_OIL] + oil_gained > max_oil)
179                     oil_gained = max_oil - sp->shp_item[I_OIL];
180                 if (product->p_nrdep != 0 && oil_gained > 0) {
181                     resource = (unsigned char *)sectp + product->p_nrndx;
182                     if (*resource * 100 < product->p_nrdep * oil_gained)
183                         oil_gained = *resource * 100 / product->p_nrdep;
184                     dep = roundavg(oil_gained * product->p_nrdep / 100.0);
185                     if (CANT_HAPPEN(dep > *resource))
186                         dep = *resource;
187                     *resource -= dep;
188                 }
189                 sp->shp_item[I_OIL] += oil_gained;
190             }
191             /* produce fish */
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             /* feed */
205             if ((n = feed_ship(sp, etus)) > 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 void
276 shiprepair(struct shpstr *ship, struct natstr *np, 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;
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;
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;
330     }
331
332     left = 100 - ship->shp_effic;
333     delta = roundavg((double)avail / w_p_eff);
334     if (delta <= 0)
335         return;
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 = left / 100.0;
348     memset(mvec, 0, sizeof(mvec));
349     mvec[I_LCM] = lcm_needed = ldround(mp->m_lcm * leftp, 1);
350     mvec[I_HCM] = hcm_needed = ldround(mp->m_hcm * leftp, 1);
351     get_materials(sp, bp, mvec, 0);
352
353     buildp = leftp;
354     if (mvec[I_LCM] < lcm_needed)
355         buildp = MIN(buildp, (float)mvec[I_LCM] / (float)mp->m_lcm);
356     if (mvec[I_HCM] < hcm_needed)
357         buildp = MIN(buildp, (float)mvec[I_HCM] / (float)mp->m_hcm);
358
359     build = ldround(buildp * 100.0, 1);
360     memset(mvec, 0, sizeof(mvec));
361     mvec[I_LCM] = roundavg(mp->m_lcm * buildp);
362     mvec[I_HCM] = roundavg(mp->m_hcm * buildp);
363     get_materials(sp, bp, mvec, 1);
364
365     if (sp->sct_type != SCT_HARBR)
366         build = delta;
367     wf -= build * w_p_eff;
368     if (wf < 0) {
369         /*
370          * I didn't use roundavg here, because I want to penalize
371          * the player with a large number of ships.
372          */
373         if (!player->simulation)
374             avail = (sp->sct_avail * 100 + wf) / 100;
375         else
376             avail = (gt_bg_nmbr(bp, sp, I_MAX + 1) * 100 + wf) / 100;
377         if (avail < 0)
378             avail = 0;
379         if (!player->simulation)
380             sp->sct_avail = avail;
381         else
382             pt_bg_nmbr(bp, sp, I_MAX + 1, avail);
383     }
384     if (sp->sct_type != SCT_HARBR)
385         if ((build + ship->shp_effic) > 80) {
386             build = 80 - ship->shp_effic;
387             if (build < 0)
388                 build = 0;
389         }
390
391     np->nat_money -= mult * mp->m_cost * build / 100.0;
392     if (!player->simulation)
393         ship->shp_effic += (signed char)build;
394 }
395
396 /*
397  * returns the number who starved, if any.
398  */
399 static int
400 feed_ship(struct shpstr *sp, int etus)
401 {
402     int needed, take;
403     double give;
404     struct nstr_item ni;
405     struct lndstr *lp;
406
407     if (opt_NOFOOD)
408         return 0;
409
410     needed = (int)ceil(food_needed(sp->shp_item, etus));
411
412     /* scrounge */
413     if (needed > sp->shp_item[I_FOOD])
414         sp->shp_item[I_FOOD] += supply_commod(sp->shp_own,
415                                         sp->shp_x, sp->shp_y, I_FOOD,
416                                         needed - sp->shp_item[I_FOOD]);
417     if (needed > sp->shp_item[I_FOOD]) {
418         /* take from embarked land units, but don't starve them */
419         snxtitem_all(&ni, EF_LAND);
420         while ((lp = nxtitemp(&ni)) && needed > sp->shp_item[I_FOOD]) {
421             if (lp->lnd_ship != sp->shp_uid)
422                 continue;
423             give = lp->lnd_item[I_FOOD] - food_needed(lp->lnd_item, etus);
424             if (give < 1.0)
425                 continue;
426             take = MIN((int)give, needed - sp->shp_item[I_FOOD]);
427             sp->shp_item[I_FOOD] += take;
428             lp->lnd_item[I_FOOD] -= take;
429         }
430     }
431
432     return feed_people(sp->shp_item, etus);
433 }