]> git.pond.sub.org Git - empserver/blob - src/lib/update/ship.c
(upd_ship, upd_land): Simplify. No functional changes.
[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     int vec[I_MAX + 1];
120     u_short pstage, ptime;
121     int oil_gained;
122     int max_oil;
123     int max_food;
124     struct pchrstr *product;
125     s_char *resource;
126     int n;
127     int mult;
128     int needed;
129     int cost;
130     int eff;
131
132     mp = &mchr[(int)sp->shp_type];
133     if (build == 1) {
134         if (np->nat_priorities[PRI_SBUILD] == 0 || np->nat_money < 0)
135             return;
136         if (sp->shp_effic < SHIP_MINEFF || !shiprepair(sp, np, bp, etus)) {
137             makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x,
138                      sp->shp_y);
139             sp->shp_own = 0;
140             return;
141         }
142     } else {
143         mult = 1;
144         if (np->nat_level[NAT_TLEV] < sp->shp_tech * 0.85)
145             mult = 2;
146         cost = -(mult * etus * dmin(0.0, money_ship * mp->m_cost));
147         if ((np->nat_priorities[PRI_SMAINT] == 0 || np->nat_money < cost)
148             && !player->simulation) {
149             if ((eff = sp->shp_effic - etus / 5) < SHIP_MINEFF) {
150                 wu(0, sp->shp_own,
151                    "%s lost to lack of maintenance\n", prship(sp));
152                 makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x,
153                          sp->shp_y);
154                 sp->shp_own = 0;
155                 return;
156             }
157             wu(0, sp->shp_own,
158                "%s lost %d%% to lack of maintenance\n",
159                prship(sp), sp->shp_effic - eff);
160             sp->shp_effic = eff;
161         } else {
162             np->nat_money -= cost;
163         }
164
165         if (!player->simulation) {
166             getvec(VT_ITEM, vec, (s_char *)sp, EF_SHIP);
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                 oil_gained = roundavg((vec[I_CIVIL] * etus / 10000.0)
174                                       * sectp->sct_oil);
175                 vec[I_OIL] += oil_gained;
176                 max_oil = vl_find(V_OIL, mp->m_vtype, mp->m_vamt, mp->m_nv);
177                 if (vec[I_OIL] > max_oil)
178                     vec[I_OIL] = max_oil;
179                 product = &pchr[P_OIL];
180                 if (product->p_nrdep != 0 && oil_gained > 0) {
181                     resource = ((s_char *)sectp) + product->p_nrndx;
182                     *resource -= roundavg(oil_gained *
183                                           product->p_nrdep / 100.0);
184                 }
185             }
186             if ((mp->m_flags & M_FOOD) && sectp->sct_type == SCT_WATER) {
187                 vec[I_FOOD] += ((vec[I_CIVIL] * etus) / 1000.0)
188                     * sectp->sct_fertil;
189             }
190             if ((n = feed_ship(sp, vec, etus, &needed, 1)) > 0) {
191                 wu(0, sp->shp_own, "%d starved on %s\n", n, prship(sp));
192                 if (n > 10)
193                     nreport(sp->shp_own, N_DIE_FAMINE, 0, 1);
194             }
195             max_food = vl_find(V_FOOD, mp->m_vtype, mp->m_vamt, mp->m_nv);
196             if (vec[I_FOOD] > max_food)
197                 vec[I_FOOD] = max_food;
198             /*
199              * do plague stuff.  plague can't break out on ships,
200              * but it can still kill people.
201              */
202             pstage = sp->shp_pstage;
203             ptime = sp->shp_ptime;
204             if (pstage != PLG_HEALTHY) {
205                 n = plague_people(np, vec, &pstage, &ptime, etus);
206                 switch (n) {
207                 case PLG_DYING:
208                     wu(0, sp->shp_own,
209                        "PLAGUE deaths reported on %s\n", prship(sp));
210                     nreport(sp->shp_own, N_DIE_PLAGUE, 0, 1);
211                     break;
212                 case PLG_INFECT:
213                     wu(0, sp->shp_own, "%s battling PLAGUE\n", prship(sp));
214                     break;
215                 case PLG_INCUBATE:
216                     /* Are we still incubating? */
217                     if (n == pstage) {
218                         /* Yes. Will it turn "infectious" next time? */
219                         if (ptime <= etus) {
220                             /* Yes.  Report an outbreak. */
221                             wu(0, sp->shp_own,
222                                "Outbreak of PLAGUE on %s!\n", prship(sp));
223                             nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
224                         }
225                     } else {
226                         /* It has already moved on to "infectious" */
227                         wu(0, sp->shp_own,
228                            "%s battling PLAGUE\n", prship(sp));
229                     }
230                     break;
231                 case PLG_EXPOSED:
232                     /* Has the plague moved to "incubation" yet? */
233                     if (n != pstage) {
234                         /* Yes. Will it turn "infectious" next time? */
235                         if (ptime <= etus) {
236                             /* Yes.  Report an outbreak. */
237                             wu(0, sp->shp_own,
238                                "Outbreak of PLAGUE on %s!\n", prship(sp));
239                             nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
240                         }
241                     }
242                     break;
243                 default:
244                     break;
245                 }
246
247                 sp->shp_pstage = pstage;
248                 sp->shp_ptime = ptime;
249             }
250             putvec(VT_ITEM, vec, (s_char *)sp, EF_SHIP);
251             pops[sp->shp_own] += vec[I_CIVIL];
252         }
253     }
254 }
255
256 /*
257  * idea is: a sector full of workers can fix up eight
258  * battleships +8 % eff each etu.  This will cost around
259  * 8 * 8 * $40 = $2560!
260  */
261 static int
262 shiprepair(register struct shpstr *ship, struct natstr *np,
263            int *bp, int etus)
264 {
265     register int delta;
266     struct sctstr *sp;
267     struct mchrstr *mp;
268     float leftp, buildp;
269     int left, build;
270     int lcm_needed, hcm_needed;
271     int wf;
272     int avail;
273     int w_p_eff;
274     int mult;
275     int mvec[I_MAX + 1];
276     int rel;
277
278     mp = &mchr[(int)ship->shp_type];
279     sp = getsectp(ship->shp_x, ship->shp_y);
280
281     if ((sp->sct_own != ship->shp_own) && (sp->sct_own != 0)) {
282         rel = getrel(getnatp(sp->sct_own), ship->shp_own);
283
284         if (rel < FRIENDLY)
285             return 1;
286     }
287
288     wf = 0;
289     /* only military can work on a military boat */
290     if (ship->shp_glim > 0)
291         wf = etus * ship->shp_item[I_MILIT] / 2;
292     else
293         wf = etus * (ship->shp_item[I_CIVIL] / 2 + ship->shp_item[I_MILIT] / 5);
294
295     if (sp->sct_type != SCT_HARBR) {
296         wf /= 3;
297         avail = wf;
298     } else {
299         if (!player->simulation)
300             avail = wf + sp->sct_avail * 100;
301         else
302             avail = wf + gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
303     }
304
305     w_p_eff = 20 + (mp->m_lcm + 2 * mp->m_hcm);
306
307     if (sp->sct_type != SCT_HARBR) {
308         int abs_max, amt;
309
310         if (ship->shp_glim > 0) {
311             abs_max = vl_find(V_MILIT, mp->m_vtype,
312                               mp->m_vamt, (int)mp->m_nv);
313             amt = ship->shp_item[I_MILIT];
314         } else {
315             abs_max = vl_find(V_CIVIL, mp->m_vtype,
316                               mp->m_vamt, (int)mp->m_nv);
317             amt = ship->shp_item[I_CIVIL];
318             if (abs_max == 0) {
319                 abs_max = vl_find(V_MILIT, mp->m_vtype, mp->m_vamt,
320                                   (int)mp->m_nv);
321                 amt = ship->shp_item[I_MILIT];
322             }
323         }
324
325         if (abs_max == 0) {
326             logerror("Abs max of 0 for ship %d\n", ship->shp_uid);
327             abs_max = 1;
328         }
329         avail -= (etus * (100 - ((amt * 100) / abs_max))) / 7;
330         /* think of it as entropy in action */
331     }
332
333     if (avail <= 0) {
334         if (!player->simulation) {
335             if (opt_SHIP_DECAY) {
336                 ship->shp_effic += avail / w_p_eff;
337             }
338             return 1;
339         }
340     }
341
342     if ((sp->sct_off) && (sp->sct_own == ship->shp_own))
343         return 1;
344
345     mult = 1;
346     if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
347         mult = 2;
348
349     if (ship->shp_effic == 100) {
350         /* ship is ok; no repairs needed */
351         return 1;
352     }
353
354     left = 100 - ship->shp_effic;
355     delta = roundavg((double)avail / w_p_eff);
356     if (delta <= 0)
357         return 1;
358     if (delta > etus * ship_grow_scale)
359         delta = etus * ship_grow_scale;
360     if (delta > left)
361         delta = left;
362
363     /* delta is the max amount we can grow */
364
365     left = 100 - ship->shp_effic;
366     if (left > delta)
367         left = delta;
368
369     leftp = ((float)left / 100.0);
370     memset(mvec, 0, sizeof(mvec));
371     mvec[I_LCM] = lcm_needed = ldround((double)(mp->m_lcm * leftp), 1);
372     mvec[I_HCM] = hcm_needed = ldround((double)(mp->m_hcm * leftp), 1);
373
374     get_materials(sp, bp, mvec, 0);
375
376     if (mvec[I_LCM] >= lcm_needed)
377         buildp = leftp;
378     else
379         buildp = ((float)mvec[I_LCM] / (float)mp->m_lcm);
380     if (mvec[I_HCM] < hcm_needed)
381         buildp = MIN(buildp, ((float)mvec[I_HCM] / (float)mp->m_hcm));
382
383     build = ldround((double)(buildp * 100.0), 1);
384     memset(mvec, 0, sizeof(mvec));
385     mvec[I_LCM] = lcm_needed = roundavg((double)(mp->m_lcm * buildp));
386     mvec[I_HCM] = hcm_needed = roundavg((double)(mp->m_hcm * buildp));
387
388     get_materials(sp, bp, mvec, 1);
389
390     if (sp->sct_type != SCT_HARBR)
391         build = delta;
392     wf -= build * w_p_eff;
393     if (wf < 0) {
394         /*
395          * I didn't use roundavg here, because I want to penalize
396          * the player with a large number of ships.
397          */
398         if (!player->simulation)
399             avail = (sp->sct_avail * 100 + wf) / 100;
400         else
401             avail = (gt_bg_nmbr(bp, sp, I_MAX + 1) * 100 + wf) / 100;
402         if (avail < 0)
403             avail = 0;
404         if (!player->simulation)
405             sp->sct_avail = avail;
406         else
407             pt_bg_nmbr(bp, sp, I_MAX + 1, avail);
408     }
409     if (sp->sct_type != SCT_HARBR)
410         if ((build + ship->shp_effic) > 80) {
411             build = 80 - ship->shp_effic;
412             if (build < 0)
413                 build = 0;
414         }
415
416     np->nat_money -= mult * mp->m_cost * build / 100.0;
417     if (!player->simulation)
418         ship->shp_effic += (s_char)build;
419     return 1;
420 }
421
422 /*
423  * returns the number who starved, if any.
424  */
425 int
426 feed_ship(struct shpstr *sp, register int *vec, int etus, int *needed,
427           int doit)
428 {
429     double food_eaten, land_eaten;
430     int ifood_eaten;
431     int can_eat, need;
432     int total_people;
433     int to_starve;
434     int starved;
435     struct nstr_item ni;
436     struct lndstr *lp;
437
438     if (opt_NOFOOD)
439         return 0;               /* no food no work to do */
440
441     food_eaten =
442         (etus * eatrate) * (vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW]);
443     ifood_eaten = (int)food_eaten;
444     if ((food_eaten - ifood_eaten) > 0)
445         ifood_eaten++;
446     starved = 0;
447     *needed = 0;
448     if (!player->simulation && food_eaten > vec[I_FOOD])
449         vec[I_FOOD] += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
450                                      I_FOOD, (ifood_eaten - vec[I_FOOD]));
451
452     if (food_eaten > vec[I_FOOD]) {
453 /* doit - only steal food from land units during the update */
454         if (sp->shp_nland > 0 && doit) {
455             snxtitem_all(&ni, EF_LAND);
456             while ((lp = (struct lndstr *)nxtitemp(&ni, 0)) &&
457                    (food_eaten > vec[I_FOOD])) {
458                 if (lp->lnd_ship != sp->shp_uid)
459                     continue;
460                 need = ifood_eaten - vec[I_FOOD];
461                 land_eaten = (etus * eatrate) * (double)lnd_getmil(lp);
462                 if (lp->lnd_item[I_FOOD] - need > land_eaten) {
463                     vec[I_FOOD] += need;
464                     lp->lnd_item[I_FOOD] -= need;
465                 } else if (lp->lnd_item[I_FOOD] - land_eaten > 0) {
466                     vec[I_FOOD] += lp->lnd_item[I_FOOD] - land_eaten;
467                     lp->lnd_item[I_FOOD] -= lp->lnd_item[I_FOOD] - land_eaten;
468                 }
469             }
470         }
471     }
472
473     if (food_eaten > vec[I_FOOD]) {
474         *needed = food_eaten - vec[I_FOOD];
475         if (*needed < (food_eaten - vec[I_FOOD]))
476             (*needed)++;
477         can_eat = (vec[I_FOOD] / (etus * eatrate));
478         total_people = vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW];
479
480         /* only want to starve off at most 1/2 the populace. */
481         if (can_eat < (total_people / 2))
482             can_eat = total_people / 2;
483
484         to_starve = total_people - can_eat;
485         while (to_starve && vec[I_UW]) {
486             to_starve--;
487             starved++;
488             vec[I_UW]--;
489         }
490         while (to_starve && vec[I_CIVIL]) {
491             to_starve--;
492             starved++;
493             vec[I_CIVIL]--;
494         }
495         while (to_starve && vec[I_MILIT]) {
496             to_starve--;
497             starved++;
498             vec[I_MILIT]--;
499         }
500
501         vec[I_FOOD] = 0;
502     } else {
503         vec[I_FOOD] -= (int)food_eaten;
504     }
505     return starved;
506 }