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