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