]> git.pond.sub.org Git - empserver/blob - src/lib/update/ship.c
(upd_ship): Remove carriage return.
[empserver] / src / lib / update / ship.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, 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 "plague.h"
37 #include "sect.h"
38 #include "nat.h"
39 #include "ship.h"
40 #include "news.h"
41 #include "file.h"
42 #include "product.h"
43 #include "land.h"
44 #include "xy.h"
45 #include "nsc.h"
46 #include "optlist.h"
47 #include "player.h"
48 #include "update.h"
49 #include "common.h"
50 #include "subs.h"
51 #include "gen.h"
52 #include "lost.h"
53 #include "budg.h"
54
55 #ifndef MIN
56 #define MIN(x,y)        ((x) > (y) ? (y) : (x))
57 #endif
58
59 static int shiprepair(struct shpstr *, struct natstr *,
60                       int *, int);
61 static void upd_ship(struct shpstr *, 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     struct shpstr *sp;
69     struct natstr *np;
70     int n, k = 0;
71     int start_money;
72     int lastx = 9999, lasty = 9999;
73
74     bp_enable_cachepath();
75     for (n = 0; NULL != (sp = getshipp(n)); n++) {
76         if (sp->shp_own == 0)
77             continue;
78         if (sp->shp_own != natnum)
79             continue;
80         np = getnatp(sp->shp_own);
81         start_money = np->nat_money;
82         if (lastx == 9999 || lasty == 9999) {
83             lastx = sp->shp_x;
84             lasty = sp->shp_y;
85         }
86         if (lastx != sp->shp_x || lasty != sp->shp_y) {
87             /* Reset the cache */
88             bp_disable_cachepath();
89             bp_clear_cachepath();
90             bp_enable_cachepath();
91         }
92         upd_ship(sp, etus, np, bp, build);
93         if (build && !player->simulation)       /* make sure to only autonav once */
94             nav_ship(sp);       /* autonav the ship */
95         sea_money[sp->shp_own] += np->nat_money - start_money;
96         if ((build && (np->nat_money != start_money)) || (!build))
97             k++;
98         if (player->simulation)
99             np->nat_money = start_money;
100     }
101     bp_disable_cachepath();
102     bp_clear_cachepath();
103
104     if (opt_SAIL) {
105         if (build && !player->simulation)       /* make sure to only sail once */
106             sail_ship(natnum);
107     }
108     return k;
109 }
110
111 static void
112 upd_ship(struct shpstr *sp, int etus,
113          struct natstr *np, int *bp, int build)
114                /* build = 1, maintain = 0 */
115 {
116     struct sctstr *sectp;
117     struct mchrstr *mp;
118     int pstage, ptime;
119     int oil_gained;
120     int max_oil;
121     int max_food;
122     struct pchrstr *product;
123     s_char *resource;
124     int n;
125     int mult;
126     int needed;
127     int cost;
128     int eff;
129
130     mp = &mchr[(int)sp->shp_type];
131     if (build == 1) {
132         if (np->nat_priorities[PRI_SBUILD] == 0 || np->nat_money < 0)
133             return;
134         if (sp->shp_effic < SHIP_MINEFF || !shiprepair(sp, np, bp, etus)) {
135             makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x,
136                      sp->shp_y);
137             sp->shp_own = 0;
138             return;
139         }
140     } else {
141         mult = 1;
142         if (np->nat_level[NAT_TLEV] < sp->shp_tech * 0.85)
143             mult = 2;
144         cost = -(mult * etus * dmin(0.0, money_ship * mp->m_cost));
145         if ((np->nat_priorities[PRI_SMAINT] == 0 || np->nat_money < cost)
146             && !player->simulation) {
147             if ((eff = sp->shp_effic - etus / 5) < SHIP_MINEFF) {
148                 wu(0, sp->shp_own,
149                    "%s lost to lack of maintenance\n", prship(sp));
150                 makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x,
151                          sp->shp_y);
152                 sp->shp_own = 0;
153                 return;
154             }
155             wu(0, sp->shp_own,
156                "%s lost %d%% to lack of maintenance\n",
157                prship(sp), sp->shp_effic - eff);
158             sp->shp_effic = eff;
159         } else {
160             np->nat_money -= cost;
161         }
162
163         if (!player->simulation) {
164             sectp = getsectp(sp->shp_x, sp->shp_y);
165
166             if ((mp->m_flags & M_OIL) && sectp->sct_type == SCT_WATER) {
167                 /*
168                  * take care of oil production
169                  */
170                 product = &pchr[P_OIL];
171                 oil_gained = roundavg(total_work(100, etus,
172                                                  sp->shp_item[I_CIVIL],
173                                                  sp->shp_item[I_MILIT],
174                                                  sp->shp_item[I_UW])
175                                       * (double)sp->shp_effic / 100.0
176                                       * (double)sectp->sct_oil / 100.0
177                                       * prod_eff(product, sp->shp_tech));
178                 max_oil = mp->m_item[I_OIL];
179                 if (sp->shp_item[I_OIL] + oil_gained > max_oil)
180                     oil_gained = max_oil - sp->shp_item[I_OIL];
181                 sp->shp_item[I_OIL] += oil_gained;
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                 product = &pchr[P_FOOD];
190                 sp->shp_item[I_FOOD] += roundavg(total_work(100, etus,
191                                                             sp->shp_item[I_CIVIL],
192                                                             sp->shp_item[I_MILIT],
193                                                             sp->shp_item[I_UW])
194                                                  * (double)sp->shp_effic / 100.0
195                                                  * (double)sectp->sct_fertil / 100.0
196                                                  * prod_eff(product, sp->shp_tech));
197             }
198             if ((n = feed_ship(sp, etus, &needed, 1)) > 0) {
199                 wu(0, sp->shp_own, "%d starved on %s\n", n, prship(sp));
200                 if (n > 10)
201                     nreport(sp->shp_own, N_DIE_FAMINE, 0, 1);
202             }
203             max_food = mp->m_item[I_FOOD];
204             if (sp->shp_item[I_FOOD] > max_food)
205                 sp->shp_item[I_FOOD] = max_food;
206             /*
207              * do plague stuff.  plague can't break out on ships,
208              * but it can still kill people.
209              */
210             pstage = sp->shp_pstage;
211             ptime = sp->shp_ptime;
212             if (pstage != PLG_HEALTHY) {
213                 n = plague_people(np, sp->shp_item, &pstage, &ptime, etus);
214                 switch (n) {
215                 case PLG_DYING:
216                     wu(0, sp->shp_own,
217                        "PLAGUE deaths reported on %s\n", prship(sp));
218                     nreport(sp->shp_own, N_DIE_PLAGUE, 0, 1);
219                     break;
220                 case PLG_INFECT:
221                     wu(0, sp->shp_own, "%s battling PLAGUE\n", prship(sp));
222                     break;
223                 case PLG_INCUBATE:
224                     /* Are we still incubating? */
225                     if (n == pstage) {
226                         /* Yes. Will it turn "infectious" next time? */
227                         if (ptime <= etus) {
228                             /* Yes.  Report an outbreak. */
229                             wu(0, sp->shp_own,
230                                "Outbreak of PLAGUE on %s!\n", prship(sp));
231                             nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
232                         }
233                     } else {
234                         /* It has already moved on to "infectious" */
235                         wu(0, sp->shp_own,
236                            "%s battling PLAGUE\n", prship(sp));
237                     }
238                     break;
239                 case PLG_EXPOSED:
240                     /* Has the plague moved to "incubation" yet? */
241                     if (n != pstage) {
242                         /* Yes. Will it turn "infectious" next time? */
243                         if (ptime <= etus) {
244                             /* Yes.  Report an outbreak. */
245                             wu(0, sp->shp_own,
246                                "Outbreak of PLAGUE on %s!\n", prship(sp));
247                             nreport(sp->shp_own, N_OUT_PLAGUE, 0, 1);
248                         }
249                     }
250                     break;
251                 default:
252                     break;
253                 }
254
255                 sp->shp_pstage = pstage;
256                 sp->shp_ptime = ptime;
257             }
258             pops[sp->shp_own] += sp->shp_item[I_CIVIL];
259         }
260     }
261 }
262
263 /*
264  * idea is: a sector full of workers can fix up eight
265  * battleships +8 % eff each etu.  This will cost around
266  * 8 * 8 * $40 = $2560!
267  */
268 static int
269 shiprepair(struct shpstr *ship, struct natstr *np,
270            int *bp, int etus)
271 {
272     int delta;
273     struct sctstr *sp;
274     struct mchrstr *mp;
275     float leftp, buildp;
276     int left, build;
277     int lcm_needed, hcm_needed;
278     int wf;
279     int avail;
280     int w_p_eff;
281     int mult;
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 * ship->shp_item[I_MILIT] / 2;
299     else
300         wf = etus * (ship->shp_item[I_CIVIL] / 2 + ship->shp_item[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 = SHP_BLD_WORK(mp->m_lcm, mp->m_hcm);
313
314     if ((sp->sct_off) && (sp->sct_own == ship->shp_own))
315         return 1;
316
317     mult = 1;
318     if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
319         mult = 2;
320
321     if (ship->shp_effic == 100) {
322         /* ship is ok; no repairs needed */
323         return 1;
324     }
325
326     left = 100 - ship->shp_effic;
327     delta = roundavg((double)avail / w_p_eff);
328     if (delta <= 0)
329         return 1;
330     if (delta > etus * ship_grow_scale)
331         delta = etus * ship_grow_scale;
332     if (delta > left)
333         delta = left;
334
335     /* delta is the max amount we can grow */
336
337     left = 100 - ship->shp_effic;
338     if (left > delta)
339         left = delta;
340
341     leftp = ((float)left / 100.0);
342     memset(mvec, 0, sizeof(mvec));
343     mvec[I_LCM] = lcm_needed = ldround((double)(mp->m_lcm * leftp), 1);
344     mvec[I_HCM] = hcm_needed = ldround((double)(mp->m_hcm * leftp), 1);
345
346     get_materials(sp, bp, mvec, 0);
347
348     if (mvec[I_LCM] >= lcm_needed)
349         buildp = leftp;
350     else
351         buildp = ((float)mvec[I_LCM] / (float)mp->m_lcm);
352     if (mvec[I_HCM] < hcm_needed)
353         buildp = MIN(buildp, ((float)mvec[I_HCM] / (float)mp->m_hcm));
354
355     build = ldround((double)(buildp * 100.0), 1);
356     memset(mvec, 0, sizeof(mvec));
357     mvec[I_LCM] = lcm_needed = roundavg((double)(mp->m_lcm * buildp));
358     mvec[I_HCM] = hcm_needed = roundavg((double)(mp->m_hcm * buildp));
359
360     get_materials(sp, bp, mvec, 1);
361
362     if (sp->sct_type != SCT_HARBR)
363         build = delta;
364     wf -= build * w_p_eff;
365     if (wf < 0) {
366         /*
367          * I didn't use roundavg here, because I want to penalize
368          * the player with a large number of ships.
369          */
370         if (!player->simulation)
371             avail = (sp->sct_avail * 100 + wf) / 100;
372         else
373             avail = (gt_bg_nmbr(bp, sp, I_MAX + 1) * 100 + wf) / 100;
374         if (avail < 0)
375             avail = 0;
376         if (!player->simulation)
377             sp->sct_avail = avail;
378         else
379             pt_bg_nmbr(bp, sp, I_MAX + 1, avail);
380     }
381     if (sp->sct_type != SCT_HARBR)
382         if ((build + ship->shp_effic) > 80) {
383             build = 80 - ship->shp_effic;
384             if (build < 0)
385                 build = 0;
386         }
387
388     np->nat_money -= mult * mp->m_cost * build / 100.0;
389     if (!player->simulation)
390         ship->shp_effic += (s_char)build;
391     return 1;
392 }
393
394 /*
395  * returns the number who starved, if any.
396  */
397 int
398 feed_ship(struct shpstr *sp, int etus, int *needed, int doit)
399 {
400     double food_eaten, land_eaten;
401     int ifood_eaten;
402     int can_eat, need;
403     int total_people;
404     int to_starve;
405     int starved;
406     struct nstr_item ni;
407     struct lndstr *lp;
408
409     if (opt_NOFOOD)
410         return 0;               /* no food no work to do */
411
412     total_people
413         = sp->shp_item[I_CIVIL] + sp->shp_item[I_MILIT] + sp->shp_item[I_UW];
414     food_eaten = etus * eatrate * total_people;
415     ifood_eaten = (int)food_eaten;
416     if (food_eaten - ifood_eaten > 0)
417         ifood_eaten++;
418     starved = 0;
419     *needed = 0;
420     if (!player->simulation && ifood_eaten > sp->shp_item[I_FOOD])
421         sp->shp_item[I_FOOD]
422             += supply_commod(sp->shp_own, sp->shp_x, sp->shp_y,
423                              I_FOOD, ifood_eaten - sp->shp_item[I_FOOD]);
424
425 /* doit - only steal food from land units during the update */
426     if (ifood_eaten > sp->shp_item[I_FOOD] && sp->shp_nland > 0 && doit) {
427         snxtitem_all(&ni, EF_LAND);
428         while ((lp = (struct lndstr *)nxtitemp(&ni, 0)) &&
429                ifood_eaten > sp->shp_item[I_FOOD]) {
430             if (lp->lnd_ship != sp->shp_uid)
431                 continue;
432             need = ifood_eaten - sp->shp_item[I_FOOD];
433             land_eaten = etus * eatrate * lnd_getmil(lp);
434             if (lp->lnd_item[I_FOOD] - need > land_eaten) {
435                 sp->shp_item[I_FOOD] += need;
436                 lp->lnd_item[I_FOOD] -= need;
437             } else if (lp->lnd_item[I_FOOD] - land_eaten > 0) {
438                 sp->shp_item[I_FOOD] += lp->lnd_item[I_FOOD] - land_eaten;
439                 lp->lnd_item[I_FOOD] -= lp->lnd_item[I_FOOD] - land_eaten;
440             }
441         }
442     }
443
444     if (ifood_eaten > sp->shp_item[I_FOOD]) {
445         *needed = ifood_eaten - sp->shp_item[I_FOOD];
446         can_eat = sp->shp_item[I_FOOD] / (etus * eatrate);
447         /* only want to starve off at most 1/2 the populace. */
448         if (can_eat < total_people / 2)
449             can_eat = total_people / 2;
450
451         to_starve = total_people - can_eat;
452         while (to_starve && sp->shp_item[I_UW]) {
453             to_starve--;
454             starved++;
455             sp->shp_item[I_UW]--;
456         }
457         while (to_starve && sp->shp_item[I_CIVIL]) {
458             to_starve--;
459             starved++;
460             sp->shp_item[I_CIVIL]--;
461         }
462         while (to_starve && sp->shp_item[I_MILIT]) {
463             to_starve--;
464             starved++;
465             sp->shp_item[I_MILIT]--;
466         }
467
468         sp->shp_item[I_FOOD] = 0;
469     } else {
470         sp->shp_item[I_FOOD] -= (int)food_eaten;
471     }
472     return starved;
473 }