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