]> git.pond.sub.org Git - empserver/blob - src/lib/subs/supply.c
subs/attsub: Drop a stale comment
[empserver] / src / lib / subs / supply.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  supply.c: Supply subroutines
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2004-2013
31  */
32
33 #include <config.h>
34
35 #include <math.h>
36 #include "chance.h"
37 #include "empobj.h"
38 #include "land.h"
39 #include "nat.h"
40 #include "optlist.h"
41 #include "path.h"
42 #include "prototypes.h"
43 #include "sect.h"
44 #include "ship.h"
45 #include "update.h"
46
47 static int s_commod(struct empobj *, short *, i_type, int, int, int);
48 static int get_minimum(struct lndstr *, i_type);
49
50 int
51 sct_supply(struct sctstr *sp, i_type type, int wanted)
52 {
53     return s_commod((struct empobj *)sp, sp->sct_item,
54                     type, wanted, ITEM_MAX, 1);
55 }
56
57 int
58 shp_supply(struct shpstr *sp, i_type type, int wanted)
59 {
60     return s_commod((struct empobj *)sp, sp->shp_item,
61                     type, wanted, mchr[sp->shp_type].m_item[type], 1);
62 }
63
64 int
65 lnd_supply(struct lndstr *lp, i_type type, int wanted)
66 {
67     return s_commod((struct empobj *)lp, lp->lnd_item,
68                     type, wanted, lchr[lp->lnd_type].l_item[type], 1);
69 }
70
71 int
72 lnd_in_supply(struct lndstr *lp)
73 {
74     if (!opt_NOFOOD) {
75         if (lp->lnd_item[I_FOOD] < get_minimum(lp, I_FOOD))
76             return 0;
77     }
78     return lp->lnd_item[I_SHELL] >= get_minimum(lp, I_SHELL);
79 }
80
81 int
82 lnd_supply_all(struct lndstr *lp)
83 {
84     int fail = 0;
85
86     if (!opt_NOFOOD)
87         fail |= !lnd_supply(lp, I_FOOD, get_minimum(lp, I_FOOD));
88     fail |= !lnd_supply(lp, I_SHELL, get_minimum(lp, I_SHELL));
89     return !fail;
90 }
91
92 /*
93  * Actually get the commod
94  *
95  * First, try to forage in the sector
96  * Second look for a warehouse or headquarters to leech
97  * Third, look for a ship we own in a harbor
98  * Fourth, look for supplies in a supply unit we own
99  *              (one good reason to do this last is that the supply
100  *               unit will then call resupply, taking more time)
101  *
102  * May want to put code to resupply with SAMs here, later --ts
103  */
104 static int
105 s_commod(struct empobj *sink, short *vec,
106          i_type type, int wanted, int limit, int actually_doit)
107 {
108     natid own = sink->own;
109     coord x = sink->x;
110     coord y = sink->y;
111     int lookrange;
112     struct sctstr sect;
113     struct nstr_sect ns;
114     struct nstr_item ni;
115     struct lchrstr *lcp;
116     struct shpstr ship;
117     struct lndstr land;
118     int minimum = 0;
119     int can_move;
120     double move_cost, weight, mobcost;
121     int packing;
122     struct dchrstr *dp;
123     struct ichrstr *ip;
124
125     if (wanted > limit)
126         wanted = limit;
127     if (wanted <= vec[type])
128         return 1;
129     wanted -= vec[type];
130
131     /* try to get it from sector we're in */
132     if (sink->ef_type != EF_SECTOR) {
133         getsect(x, y, &sect);
134         if (sect.sct_own == own) {
135             if (!opt_NOFOOD && type == I_FOOD)
136                 minimum = 1 + (int)ceil(food_needed(sect.sct_item,
137                                                     etu_per_update));
138             if (sect.sct_item[type] - wanted >= minimum) {
139                 sect.sct_item[type] -= wanted;
140                 if (actually_doit) {
141                     vec[type] += wanted;
142                     putsect(&sect);
143                     put_empobj(sink->ef_type, sink->uid, sink);
144                 }
145                 return 1;
146             } else if (sect.sct_item[type] - minimum > 0) {
147                 wanted -= sect.sct_item[type] - minimum;
148                 sect.sct_item[type] = minimum;
149                 if (actually_doit) {
150                     vec[type] += sect.sct_item[type] - minimum;
151                     putsect(&sect);
152                 }
153             }
154         }
155     }
156
157     /* look for a headquarters or warehouse */
158     lookrange = tfact(own, 10.0);
159     snxtsct_dist(&ns, x, y, lookrange);
160     while (nxtsct(&ns, &sect) && wanted) {
161         if (ns.curdist == 0)
162             continue;
163         if (sect.sct_own != own)
164             continue;
165         if ((sect.sct_type != SCT_WAREH) &&
166             (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
167             continue;
168         if ((sect.sct_type == SCT_HEADQ) &&
169             (sect.sct_dist_x == sect.sct_x) &&
170             (sect.sct_dist_y == sect.sct_y))
171             continue;
172         if (sect.sct_effic < 60)
173             continue;
174         move_cost = path_find(sect.sct_x, sect.sct_y, x, y, own, MOB_MOVE);
175         if (move_cost < 0)
176             continue;
177         if (!opt_NOFOOD && type == I_FOOD)
178             minimum = 1 + (int)ceil(food_needed(sect.sct_item,
179                                                 etu_per_update));
180         if (sect.sct_item[type] <= minimum)
181             continue;
182         ip = &ichr[type];
183         dp = &dchr[sect.sct_type];
184         packing = ip->i_pkg[dp->d_pkg];
185         if (packing > 1 && sect.sct_effic < 60)
186             packing = 1;
187         weight = (double)ip->i_lbs / packing;
188         mobcost = move_cost * weight;
189         if (mobcost > 0)
190             can_move = (double)sect.sct_mobil / mobcost;
191         else
192             can_move = sect.sct_item[type] - minimum;
193         if (can_move > sect.sct_item[type] - minimum)
194             can_move = sect.sct_item[type] - minimum;
195
196         if (can_move >= wanted) {
197             int n;
198
199             sect.sct_item[type] -= wanted;
200
201             /* take off mobility for delivering sect */
202             n = roundavg(wanted * weight * move_cost);
203             sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil);
204             if (actually_doit) {
205                 vec[type] += wanted;
206                 putsect(&sect);
207                 put_empobj(sink->ef_type, sink->uid, sink);
208             }
209             return 1;
210         } else if (can_move > 0) {
211             int n;
212             wanted -= can_move;
213             sect.sct_item[type] -= can_move;
214
215             /* take off mobility for delivering sect */
216             n = roundavg(can_move * weight * move_cost);
217             sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil);
218             if (actually_doit) {
219                 vec[type] += can_move;
220                 putsect(&sect);
221             }
222         }
223     }
224
225     /* look for an owned ship in a harbor */
226     snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
227     while (nxtitem(&ni, &ship) && wanted) {
228         if (sink->ef_type == EF_SHIP && sink->uid == ship.shp_uid)
229             continue;
230         if (ship.shp_own != own)
231             continue;
232         if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
233             continue;
234         getsect(ship.shp_x, ship.shp_y, &sect);
235         if (sect.sct_type != SCT_HARBR)
236             continue;
237         if (sect.sct_effic < 2)
238             continue;
239         move_cost = path_find(sect.sct_x, sect.sct_y, x, y, own, MOB_MOVE);
240         if (move_cost < 0)
241             continue;
242         if (!opt_NOFOOD && type == I_FOOD)
243             minimum = 1 + (int)ceil(food_needed(ship.shp_item,
244                                                 etu_per_update));
245         if (ship.shp_item[type] <= minimum)
246             continue;
247         ip = &ichr[type];
248         dp = &dchr[sect.sct_type];
249         packing = ip->i_pkg[dp->d_pkg];
250         if (packing > 1 && sect.sct_effic < 60)
251             packing = 1;
252         weight = (double)ip->i_lbs / packing;
253         mobcost = move_cost * weight;
254         if (mobcost > 0)
255             can_move = (double)sect.sct_mobil / mobcost;
256         else
257             can_move = ship.shp_item[type] - minimum;
258         if (can_move > ship.shp_item[type] - minimum)
259             can_move = ship.shp_item[type] - minimum;
260         if (can_move >= wanted) {
261             int n;
262             ship.shp_item[type] -= wanted;
263
264             n = roundavg(wanted * weight * move_cost);
265             sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil);
266             if (actually_doit) {
267                 vec[type] += can_move;
268                 putship(ship.shp_uid, &ship);
269                 if (n)
270                     putsect(&sect);
271                 put_empobj(sink->ef_type, sink->uid, sink);
272             }
273             return 1;
274         } else if (can_move > 0) {
275             int n;
276             wanted -= can_move;
277             ship.shp_item[type] -= can_move;
278
279             n = roundavg(can_move * weight * move_cost);
280             sect.sct_mobil -= LIMIT_TO(n, 0, sect.sct_mobil);
281             if (actually_doit) {
282                 vec[type] += can_move;
283                 putship(ship.shp_uid, &ship);
284                 if (n)
285                     putsect(&sect);
286             }
287         }
288     }
289
290     /* look for an owned supply unit */
291     snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
292     while (nxtitem(&ni, &land) && wanted) {
293         int min;
294
295         if (sink->ef_type == EF_LAND && sink->uid == land.lnd_uid)
296             continue;
297         if (land.lnd_own != own)
298             continue;
299
300         lcp = &lchr[(int)land.lnd_type];
301         if (!(lcp->l_flags & L_SUPPLY))
302             continue;
303
304         if (land.lnd_item[type] <= get_minimum(&land, type))
305             continue;
306
307         if (land.lnd_ship >= 0) {
308             getsect(land.lnd_x, land.lnd_y, &sect);
309             if (sect.sct_type != SCT_HARBR || sect.sct_effic < 2)
310                 continue;
311         }
312
313         move_cost = path_find(land.lnd_x, land.lnd_y, x, y, own, MOB_MOVE);
314         if (move_cost < 0)
315             continue;
316
317 #if 0
318         /*
319          * Recursive supply is disabled for now.  It can introduce
320          * cycles into the "resupplies from" relation.  The code below
321          * attempts to break these cycles by temporarily zapping the
322          * commodity being supplied.  That puts the land file in a
323          * funny state temporarily, risking loss of supplies when
324          * something goes wrong on the way.  Worse, it increases
325          * lnd_seqno even when !actually_doit, which can lead to
326          * spurious seqno mismatch oopses in users of
327          * lnd_could_be_supplied().  I can't be bothered to clean up
328          * this mess right now, because recursive resupply is too dumb
329          * to be really useful anyway: each step uses the first source
330          * it finds, without consideration of mobility cost.  If you
331          * re-enable it, don't forget to uncomment its documentation
332          * in supply.t as well.
333          */
334         if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
335             struct lndstr save;
336
337             /*
338              * Temporarily zap this unit's store, so the recursion
339              * avoids it.
340              */
341             save = land;
342             land.lnd_item[type] = 0;
343             putland(land.lnd_uid, &land);
344             save.lnd_seqno = land.lnd_seqno;
345
346             s_commod((struct empobj *)&land, land.lnd_item, type, wanted,
347                      lchr[land.lnd_type].l_item[type] - wanted,
348                      actually_doit);
349             land.lnd_item[type] += save.lnd_item[type];
350
351             if (actually_doit)
352                 putland(land.lnd_uid, &land);
353             else
354                 putland(save.lnd_uid, &save);
355         }
356 #endif
357
358         min = get_minimum(&land, type);
359         ip = &ichr[type];
360         weight = ip->i_lbs;
361         mobcost = move_cost * weight;
362         if (mobcost > 0)
363             can_move = (double)land.lnd_mobil / mobcost;
364         else
365             can_move = land.lnd_item[type] - min;
366         if (can_move > land.lnd_item[type] - min)
367             can_move = land.lnd_item[type] - min;
368
369         if (can_move >= wanted) {
370             land.lnd_item[type] -= wanted;
371             land.lnd_mobil -= roundavg(wanted * weight * move_cost);
372             if (actually_doit) {
373                 vec[type] += wanted;
374                 putland(land.lnd_uid, &land);
375                 put_empobj(sink->ef_type, sink->uid, sink);
376             }
377             return 1;
378         } else if (can_move > 0) {
379             wanted -= can_move;
380             land.lnd_item[type] -= can_move;
381             land.lnd_mobil -= roundavg(can_move * weight * move_cost);
382             if (actually_doit) {
383                 vec[type] += can_move;
384                 putland(land.lnd_uid, &land);
385             }
386         }
387     }
388
389     if (actually_doit)
390         put_empobj(sink->ef_type, sink->uid, sink);
391     return 0;
392 }
393
394 /*
395  * We want to get enough shells to fire once,
396  * one update's worth of food.
397  */
398 static int
399 get_minimum(struct lndstr *lp, i_type type)
400 {
401     struct lchrstr *lcp;
402     int max, want = 0;
403
404     lcp = &lchr[(int)lp->lnd_type];
405     max = lcp->l_item[type];
406
407     switch (type) {
408     case I_FOOD:
409         if (opt_NOFOOD)
410             return 0;           /* no food reqd, get out */
411         want = (int)ceil(food_needed(lp->lnd_item, etu_per_update));
412         break;
413     case I_SHELL:
414         want = lcp->l_ammo;
415         break;
416     default:
417         return 0;
418     }
419
420     if (want > max)
421         want = max;
422
423     return want;
424 }
425
426 int
427 lnd_could_be_supplied(struct lndstr *lp)
428 {
429     int res, food, food_needed, shells_needed, shells;
430
431     if (!opt_NOFOOD) {
432         food_needed = get_minimum(lp, I_FOOD);
433         food = lp->lnd_item[I_FOOD];
434         if (food < food_needed) {
435             res = s_commod((struct empobj *)lp, lp->lnd_item,
436                            I_FOOD, food_needed,
437                            lchr[lp->lnd_type].l_item[I_FOOD], 0);
438             lp->lnd_item[I_FOOD] = food;
439             if (!res)
440                 return 0;
441         }
442     }
443
444     shells_needed = lchr[lp->lnd_type].l_ammo;
445     shells = lp->lnd_item[I_SHELL];
446     if (shells < shells_needed) {
447         res = s_commod((struct empobj *)lp, lp->lnd_item,
448                        I_SHELL, shells_needed,
449                        lchr[lp->lnd_type].l_item[I_SHELL], 0);
450         lp->lnd_item[I_SHELL] = shells;
451         if (!res)
452             return 0;
453     }
454
455     return 1;
456 }