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