]> git.pond.sub.org Git - empserver/blob - src/lib/update/ship.c
update budget: Fix civ counting for happiness and education
[empserver] / src / lib / update / ship.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  ship.c: Do production for ships
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Steve McClure, 1996
32  *     Ron Koenderink, 2004
33  *     Markus Armbruster, 2006-2016
34  */
35
36 #include <config.h>
37
38 #include <math.h>
39 #include "chance.h"
40 #include "file.h"
41 #include "land.h"
42 #include "lost.h"
43 #include "nat.h"
44 #include "news.h"
45 #include "optlist.h"
46 #include "plague.h"
47 #include "player.h"
48 #include "product.h"
49 #include "prototypes.h"
50 #include "ship.h"
51 #include "update.h"
52
53 static void upd_ship(struct shpstr *, int, struct bp *, int);
54 static void shiprepair(struct shpstr *, struct natstr *, struct bp *,
55                        int, struct budget *);
56 static int feed_ship(struct shpstr *, int);
57
58 void
59 prod_ship(int etus, int natnum, struct bp *bp, int build)
60                 /* build = 1, maintain = 0 */
61 {
62     struct shpstr *sp;
63     int i;
64
65     for (i = 0; (sp = getshipp(i)); i++) {
66         if (sp->shp_own == 0)
67             continue;
68         if (sp->shp_own != natnum)
69             continue;
70         if (sp->shp_effic < SHIP_MINEFF) {
71             makelost(EF_SHIP, sp->shp_own, sp->shp_uid,
72                      sp->shp_x, sp->shp_y);
73             sp->shp_own = 0;
74             continue;
75         }
76
77         upd_ship(sp, etus, bp, build);
78     }
79 }
80
81 static void
82 upd_ship(struct shpstr *sp, int etus, struct bp *bp, int build)
83                /* build = 1, maintain = 0 */
84 {
85     struct budget *budget = &nat_budget[sp->shp_own];
86     struct mchrstr *mp = &mchr[sp->shp_type];
87     struct natstr *np = getnatp(sp->shp_own);
88     struct sctstr *sectp;
89     int pstage, ptime;
90     int oil_gained;
91     int max_oil;
92     int max_food;
93     struct pchrstr *product;
94     unsigned char *resource;
95     int dep;
96     int n, mult, eff_lost;
97     double cost;
98
99     if (build == 1) {
100         if (!sp->shp_off && budget->money >= 0)
101             shiprepair(sp, np, bp, etus, budget);
102         if (!player->simulation)
103             sp->shp_off = 0;
104     } else {
105         budget->oldowned_civs += sp->shp_item[I_CIVIL];
106         mult = 1;
107         if (np->nat_level[NAT_TLEV] < sp->shp_tech * 0.85)
108             mult = 2;
109         budget->bm[BUDG_SHP_MAINT].count++;
110         cost = mult * etus * -money_ship * mp->m_cost;
111         if (budget->money < cost && !player->simulation) {
112             eff_lost = etus / 5;
113             if (sp->shp_effic - eff_lost < SHIP_MINEFF)
114                 eff_lost = sp->shp_effic - SHIP_MINEFF;
115             if (eff_lost > 0) {
116                 wu(0, sp->shp_own, "%s lost %d%% to lack of maintenance\n",
117                    prship(sp), eff_lost);
118                 sp->shp_effic -= eff_lost;
119             }
120         } else {
121             budget->bm[BUDG_SHP_MAINT].money -= cost;
122             budget->money -= cost;
123         }
124
125         if (!player->simulation) {
126             sectp = getsectp(sp->shp_x, sp->shp_y);
127
128             /* produce oil */
129             if (budget->money >= 0
130                 && (mp->m_flags & M_OIL) && sectp->sct_type == SCT_WATER) {
131                 product = &pchr[dchr[SCT_OIL].d_prd];
132                 oil_gained = roundavg(total_work(100, etus,
133                                                  sp->shp_item[I_CIVIL],
134                                                  sp->shp_item[I_MILIT],
135                                                  sp->shp_item[I_UW],
136                                                  ITEM_MAX)
137                                       * sp->shp_effic / 100.0
138                                       * sectp->sct_oil / 100.0
139                                       * prod_eff(SCT_OIL, sp->shp_tech));
140                 max_oil = mp->m_item[I_OIL];
141                 if (sp->shp_item[I_OIL] + oil_gained > max_oil)
142                     oil_gained = max_oil - sp->shp_item[I_OIL];
143                 if (product->p_nrdep != 0 && oil_gained > 0) {
144                     resource = (unsigned char *)sectp + product->p_nrndx;
145                     if (*resource * 100 < product->p_nrdep * oil_gained)
146                         oil_gained = *resource * 100 / product->p_nrdep;
147                     dep = roundavg(oil_gained * product->p_nrdep / 100.0);
148                     if (CANT_HAPPEN(dep > *resource))
149                         dep = *resource;
150                     *resource -= dep;
151                 }
152                 sp->shp_item[I_OIL] += oil_gained;
153             }
154             /* produce fish */
155             if (budget->money >= 0
156                 && (mp->m_flags & M_FOOD) && sectp->sct_type == SCT_WATER) {
157                 sp->shp_item[I_FOOD]
158                     += roundavg(total_work(100, etus,
159                                            sp->shp_item[I_CIVIL],
160                                            sp->shp_item[I_MILIT],
161                                            sp->shp_item[I_UW],
162                                            ITEM_MAX)
163                                 * sp->shp_effic / 100.0
164                                 * sectp->sct_fertil / 100.0
165                                 * prod_eff(SCT_AGRI, sp->shp_tech));
166             }
167             /* feed */
168             if ((n = feed_ship(sp, etus)) > 0) {
169                 wu(0, sp->shp_own, "%d starved on %s\n", n, prship(sp));
170                 if (n > 10)
171                     nreport(sp->shp_own, N_DIE_FAMINE, 0, 1);
172             }
173             max_food = mp->m_item[I_FOOD];
174             if (sp->shp_item[I_FOOD] > max_food)
175                 sp->shp_item[I_FOOD] = max_food;
176             /*
177              * do plague stuff.  plague can't break out on ships,
178              * but it can still kill people.
179              */
180             pstage = sp->shp_pstage;
181             ptime = sp->shp_ptime;
182             if (pstage != PLG_HEALTHY) {
183                 n = plague_people(np, sp->shp_item, &pstage, &ptime, etus);
184                 switch (n) {
185                 case PLG_DYING:
186                     wu(0, sp->shp_own,
187                        "PLAGUE deaths reported on %s\n", prship(sp));
188                     nreport(sp->shp_own, N_DIE_PLAGUE, 0, 1);
189                     break;
190                 case PLG_INFECT:
191                     wu(0, sp->shp_own, "%s battling PLAGUE\n", prship(sp));
192                     break;
193                 case PLG_INCUBATE:
194                     /* Are we still incubating? */
195                     if (n == pstage) {
196                         /* Yes. Will it turn "infectious" next time? */
197                         if (ptime <= etus) {
198                             /* Yes.  Report an outbreak. */
199                             wu(0, sp->shp_own,
200                                "Outbreak of PLAGUE on %s!\n", prship(sp));
201                             nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
202                         }
203                     } else {
204                         /* It has already moved on to "infectious" */
205                         wu(0, sp->shp_own,
206                            "%s battling PLAGUE\n", prship(sp));
207                     }
208                     break;
209                 case PLG_EXPOSED:
210                     /* Has the plague moved to "incubation" yet? */
211                     if (n != pstage) {
212                         /* Yes. Will it turn "infectious" next time? */
213                         if (ptime <= etus) {
214                             /* Yes.  Report an outbreak. */
215                             wu(0, sp->shp_own,
216                                "Outbreak of PLAGUE on %s!\n", prship(sp));
217                             nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
218                         }
219                     }
220                     break;
221                 default:
222                     break;
223                 }
224
225                 sp->shp_pstage = pstage;
226                 sp->shp_ptime = ptime;
227             }
228         }
229     }
230 }
231
232 /*
233  * idea is: a sector full of workers can fix up eight
234  * battleships +8 % eff each etu.  This will cost around
235  * 8 * 8 * $40 = $2560!
236  */
237 static void
238 shiprepair(struct shpstr *ship, struct natstr *np, struct bp *bp, int etus,
239            struct budget *budget)
240 {
241     struct mchrstr *mp = &mchr[(int)ship->shp_type];
242     int delta;
243     struct sctstr *sp, scratch_sect;
244     int build;
245     int wf;
246     int avail;
247     int mult;
248     double cost;
249
250     if (ship->shp_effic == 100)
251         return;
252
253     sp = getsectp(ship->shp_x, ship->shp_y);
254     if (sp->sct_off)
255         return;
256
257     if (sp->sct_own != 0
258         && relations_with(sp->sct_own, ship->shp_own) < FRIENDLY)
259         return;
260
261     if (player->simulation) {
262         scratch_sect = *sp;
263         bp_to_sect(bp, &scratch_sect);
264         sp = &scratch_sect;
265     }
266
267     mult = 1;
268     if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
269         mult = 2;
270
271     /* only military can work on a military boat */
272     if (mp->m_glim != 0)
273         wf = etus * ship->shp_item[I_MILIT] / 2;
274     else
275         wf = etus * (ship->shp_item[I_CIVIL] / 2 + ship->shp_item[I_MILIT] / 5);
276
277     if (sp->sct_type != SCT_HARBR) {
278         wf /= 3;
279         avail = wf;
280     } else
281         avail = wf + sp->sct_avail * 100;
282
283     delta = avail / mp->m_bwork;
284     if (delta <= 0)
285         return;
286     if (delta > (int)((float)etus * ship_grow_scale))
287         delta = (int)((float)etus * ship_grow_scale);
288     if (delta > 100 - ship->shp_effic)
289         delta = 100 - ship->shp_effic;
290
291     build = get_materials(sp, mp->m_mat, delta);
292
293     if (sp->sct_type != SCT_HARBR)
294         build = delta;
295
296     wf -= build * mp->m_bwork;
297     if (wf < 0) {
298         avail = roundavg((sp->sct_avail * 100 + wf) / 100.0);
299         if (avail < 0)
300             avail = 0;
301         sp->sct_avail = avail;
302     }
303     if (sp->sct_type != SCT_HARBR)
304         if ((build + ship->shp_effic) > 80) {
305             build = 80 - ship->shp_effic;
306             if (build < 0)
307                 build = 0;
308         }
309
310     bp_set_from_sect(bp, sp);
311     cost = mult * mp->m_cost * build / 100.0;
312     budget->bm[BUDG_SHP_BUILD].count += !!build;
313     budget->bm[BUDG_SHP_BUILD].money -= cost;
314     budget->money -= cost;
315     if (!player->simulation)
316         ship->shp_effic += (signed char)build;
317 }
318
319 /*
320  * returns the number who starved, if any.
321  */
322 static int
323 feed_ship(struct shpstr *sp, int etus)
324 {
325     int needed, take;
326     double give;
327     struct nstr_item ni;
328     struct lndstr *lp;
329
330     if (opt_NOFOOD)
331         return 0;
332
333     needed = (int)ceil(food_needed(sp->shp_item, etus));
334
335     /* scrounge */
336     if (needed > sp->shp_item[I_FOOD]) {
337         /* take from embarked land units, but don't starve them */
338         snxtitem_cargo(&ni, EF_LAND, EF_SHIP, sp->shp_uid);
339         while ((lp = nxtitemp(&ni)) && needed > sp->shp_item[I_FOOD]) {
340             give = lp->lnd_item[I_FOOD] - food_needed(lp->lnd_item, etus);
341             if (give < 1.0)
342                 continue;
343             take = MIN((int)give, needed - sp->shp_item[I_FOOD]);
344             sp->shp_item[I_FOOD] += take;
345             lp->lnd_item[I_FOOD] -= take;
346         }
347     }
348
349     return feed_people(sp->shp_item, etus);
350 }