]> git.pond.sub.org Git - empserver/blob - src/lib/subs/supply.c
ff084dc2d939aef2a4ebda52964172e3eeaddbd8
[empserver] / src / lib / subs / supply.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2013, 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-2011
31  */
32
33 #include <config.h>
34
35 #include <math.h>
36 #include "chance.h"
37 #include "empobj.h"
38 #include "file.h"
39 #include "land.h"
40 #include "nat.h"
41 #include "optlist.h"
42 #include "path.h"
43 #include "prototypes.h"
44 #include "sect.h"
45 #include "ship.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     /* leave at least 1 military in sectors/ships */
119     int minimum = 0;
120     int can_move;
121     double move_cost, weight, mobcost;
122     int packing;
123     struct dchrstr *dp;
124     struct ichrstr *ip;
125
126     if (wanted > limit)
127         wanted = limit;
128     if (wanted <= vec[type])
129         return 1;
130     wanted -= vec[type];
131
132     /* try to get it from sector we're in */
133     if (sink->ef_type != EF_SECTOR) {
134         getsect(x, y, &sect);
135         if (sect.sct_own == own) {
136             if (!opt_NOFOOD && type == I_FOOD)
137                 minimum = 1 + (int)ceil(food_needed(sect.sct_item,
138                                                     etu_per_update));
139             if (sect.sct_item[type] - wanted >= minimum) {
140                 sect.sct_item[type] -= wanted;
141                 if (actually_doit) {
142                     vec[type] += wanted;
143                     putsect(&sect);
144                     put_empobj(sink->ef_type, sink->uid, sink);
145                 }
146                 return 1;
147             } else if (sect.sct_item[type] - minimum > 0) {
148                 wanted -= sect.sct_item[type] - minimum;
149                 sect.sct_item[type] = minimum;
150                 if (actually_doit) {
151                     vec[type] += sect.sct_item[type] - minimum;
152                     putsect(&sect);
153                 }
154             }
155         }
156     }
157
158     /* look for a headquarters or warehouse */
159     lookrange = tfact(own, 10.0);
160     snxtsct_dist(&ns, x, y, lookrange);
161     while (nxtsct(&ns, &sect) && wanted) {
162         if (ns.curdist == 0)
163             continue;
164         if (sect.sct_own != own)
165             continue;
166         if ((sect.sct_type != SCT_WAREH) &&
167             (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
168             continue;
169         if ((sect.sct_type == SCT_HEADQ) &&
170             (sect.sct_dist_x == sect.sct_x) &&
171             (sect.sct_dist_y == sect.sct_y))
172             continue;
173         if (sect.sct_effic < 60)
174             continue;
175         move_cost = path_find(sect.sct_x, sect.sct_y, x, y, own, MOB_MOVE);
176         if (move_cost < 0)
177             continue;
178         if (!opt_NOFOOD && type == I_FOOD)
179             minimum = 1 + (int)ceil(food_needed(sect.sct_item,
180                                                 etu_per_update));
181         if (sect.sct_item[type] <= minimum)
182             continue;
183         ip = &ichr[type];
184         dp = &dchr[sect.sct_type];
185         packing = ip->i_pkg[dp->d_pkg];
186         if (packing > 1 && sect.sct_effic < 60)
187             packing = 1;
188         weight = (double)ip->i_lbs / packing;
189         mobcost = move_cost * weight;
190         if (mobcost > 0)
191             can_move = (double)sect.sct_mobil / mobcost;
192         else
193             can_move = sect.sct_item[type] - minimum;
194         if (can_move > sect.sct_item[type] - minimum)
195             can_move = sect.sct_item[type] - minimum;
196
197         if (can_move >= wanted) {
198             int n;
199
200             sect.sct_item[type] -= wanted;
201
202             /* take off mobility for delivering sect */
203             n = roundavg(wanted * weight * move_cost);
204             if (n < 0)
205                 n = 0;
206             if (n > sect.sct_mobil)
207                 n = sect.sct_mobil;
208             sect.sct_mobil -= n;
209             if (actually_doit) {
210                 vec[type] += wanted;
211                 putsect(&sect);
212                 put_empobj(sink->ef_type, sink->uid, sink);
213             }
214             return 1;
215         } else if (can_move > 0) {
216             int n;
217             wanted -= can_move;
218             sect.sct_item[type] -= can_move;
219
220             /* take off mobility for delivering sect */
221             n = roundavg(can_move * weight * move_cost);
222             if (n < 0)
223                 n = 0;
224             if (n > sect.sct_mobil)
225                 n = sect.sct_mobil;
226             sect.sct_mobil -= n;
227             if (actually_doit) {
228                 vec[type] += can_move;
229                 putsect(&sect);
230             }
231         }
232     }
233
234     /* look for an owned ship in a harbor */
235     snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
236     while (nxtitem(&ni, &ship) && wanted) {
237         if (sink->ef_type == EF_SHIP && sink->uid == ship.shp_uid)
238             continue;
239         if (ship.shp_own != own)
240             continue;
241         if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
242             continue;
243         getsect(ship.shp_x, ship.shp_y, &sect);
244         if (sect.sct_type != SCT_HARBR)
245             continue;
246         if (sect.sct_effic < 2)
247             continue;
248         move_cost = path_find(sect.sct_x, sect.sct_y, x, y, own, MOB_MOVE);
249         if (move_cost < 0)
250             continue;
251         if (!opt_NOFOOD && type == I_FOOD)
252             minimum = 1 + (int)ceil(food_needed(ship.shp_item,
253                                                 etu_per_update));
254         if (ship.shp_item[type] <= minimum)
255             continue;
256         ip = &ichr[type];
257         dp = &dchr[sect.sct_type];
258         packing = ip->i_pkg[dp->d_pkg];
259         if (packing > 1 && sect.sct_effic < 60)
260             packing = 1;
261         weight = (double)ip->i_lbs / packing;
262         mobcost = move_cost * weight;
263         if (mobcost > 0)
264             can_move = (double)sect.sct_mobil / mobcost;
265         else
266             can_move = ship.shp_item[type] - minimum;
267         if (can_move > ship.shp_item[type] - minimum)
268             can_move = ship.shp_item[type] - minimum;
269         if (can_move >= wanted) {
270             int n;
271             ship.shp_item[type] -= wanted;
272
273             n = roundavg(wanted * weight * move_cost);
274             if (n < 0)
275                 n = 0;
276             if (n > sect.sct_mobil)
277                 n = sect.sct_mobil;
278             sect.sct_mobil -= n;
279             if (actually_doit) {
280                 vec[type] += can_move;
281                 putship(ship.shp_uid, &ship);
282                 putsect(&sect);
283                 put_empobj(sink->ef_type, sink->uid, sink);
284             }
285             return 1;
286         } else if (can_move > 0) {
287             int n;
288             wanted -= can_move;
289             ship.shp_item[type] -= can_move;
290
291             n = roundavg(can_move * weight * move_cost);
292             if (n < 0)
293                 n = 0;
294             if (n > sect.sct_mobil)
295                 n = sect.sct_mobil;
296             sect.sct_mobil -= n;
297             if (actually_doit) {
298                 vec[type] += can_move;
299                 putship(ship.shp_uid, &ship);
300                 putsect(&sect);
301             }
302         }
303     }
304
305     /* look for an owned supply unit */
306     snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
307     while (nxtitem(&ni, &land) && wanted) {
308         int min;
309
310         if (sink->ef_type == EF_LAND && sink->uid == land.lnd_uid)
311             continue;
312         if (land.lnd_own != own)
313             continue;
314
315         lcp = &lchr[(int)land.lnd_type];
316         if (!(lcp->l_flags & L_SUPPLY))
317             continue;
318
319         if (land.lnd_item[type] <= get_minimum(&land, type))
320             continue;
321
322         if (land.lnd_ship >= 0) {
323             getsect(land.lnd_x, land.lnd_y, &sect);
324             if (sect.sct_type != SCT_HARBR || sect.sct_effic < 2)
325                 continue;
326         }
327
328         move_cost = path_find(land.lnd_x, land.lnd_y, x, y, own, MOB_MOVE);
329         if (move_cost < 0)
330             continue;
331
332 #if 0
333         /*
334          * Recursive supply is disabled for now.  It can introduce
335          * cycles into the "resupplies from" relation.  The code below
336          * attempts to break these cycles by temporarily zapping the
337          * commodity being supplied.  That puts the land file in a
338          * funny state temporarily, risking loss of supplies when
339          * something goes wrong on the way.  Worse, it increases
340          * lnd_seqno even when !actually_doit, which can lead to
341          * spurious seqno mismatch oopses in users of
342          * lnd_could_be_supplied().  I can't be bothered to clean up
343          * this mess right now, because recursive resupply is too dumb
344          * to be really useful anyway: each step uses the first source
345          * it finds, without consideration of mobility cost.  If you
346          * re-enable it, don't forget to uncomment its documentation
347          * in supply.t as well.
348          */
349         if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
350             struct lndstr save;
351
352             /*
353              * Temporarily zap this unit's store, so the recursion
354              * avoids it.
355              */
356             save = land;
357             land.lnd_item[type] = 0;
358             putland(land.lnd_uid, &land);
359             save.lnd_seqno = land.lnd_seqno;
360
361             s_commod((struct empobj *)&land, land.lnd_item, type, wanted,
362                      lchr[land.lnd_type].l_item[type] - wanted,
363                      actually_doit);
364             land.lnd_item[type] += save.lnd_item[type];
365
366             if (actually_doit)
367                 putland(land.lnd_uid, &land);
368             else
369                 putland(save.lnd_uid, &save);
370         }
371 #endif
372
373         min = get_minimum(&land, type);
374         ip = &ichr[type];
375         weight = ip->i_lbs;
376         mobcost = move_cost * weight;
377         if (mobcost > 0)
378             can_move = (double)land.lnd_mobil / mobcost;
379         else
380             can_move = land.lnd_item[type] - min;
381         if (can_move > land.lnd_item[type] - min)
382             can_move = land.lnd_item[type] - min;
383
384         if (can_move >= wanted) {
385             land.lnd_item[type] -= wanted;
386             land.lnd_mobil -= roundavg(wanted * weight * move_cost);
387             if (actually_doit) {
388                 vec[type] += wanted;
389                 putland(land.lnd_uid, &land);
390                 put_empobj(sink->ef_type, sink->uid, sink);
391             }
392             return 1;
393         } else if (can_move > 0) {
394             wanted -= can_move;
395             land.lnd_item[type] -= can_move;
396             land.lnd_mobil -= roundavg(can_move * weight * move_cost);
397             if (actually_doit) {
398                 vec[type] += can_move;
399                 putland(land.lnd_uid, &land);
400             }
401         }
402     }
403
404     if (actually_doit)
405         put_empobj(sink->ef_type, sink->uid, sink);
406     return 0;
407 }
408
409 /*
410  * We want to get enough shells to fire once,
411  * one update's worth of food.
412  */
413 static int
414 get_minimum(struct lndstr *lp, i_type type)
415 {
416     struct lchrstr *lcp;
417     int max, want = 0;
418
419     lcp = &lchr[(int)lp->lnd_type];
420     max = lcp->l_item[type];
421
422     switch (type) {
423     case I_FOOD:
424         if (opt_NOFOOD)
425             return 0;           /* no food reqd, get out */
426         want = (int)ceil(food_needed(lp->lnd_item, etu_per_update));
427         break;
428     case I_SHELL:
429         want = lcp->l_ammo;
430         break;
431     default:
432         return 0;
433     }
434
435     if (want > max)
436         want = max;
437
438     return want;
439 }
440
441 int
442 lnd_could_be_supplied(struct lndstr *lp)
443 {
444     int res, food, food_needed, shells_needed, shells;
445
446     if (!opt_NOFOOD) {
447         food_needed = get_minimum(lp, I_FOOD);
448         food = lp->lnd_item[I_FOOD];
449         if (food < food_needed) {
450             res = s_commod((struct empobj *)lp, lp->lnd_item,
451                            I_FOOD, food_needed,
452                            lchr[lp->lnd_type].l_item[I_FOOD], 0);
453             lp->lnd_item[I_FOOD] = food;
454             if (!res)
455                 return 0;
456         }
457     }
458
459     shells_needed = lchr[lp->lnd_type].l_ammo;
460     shells = lp->lnd_item[I_SHELL];
461     if (shells < shells_needed) {
462         res = s_commod((struct empobj *)lp, lp->lnd_item,
463                        I_SHELL, shells_needed,
464                        lchr[lp->lnd_type].l_item[I_SHELL], 0);
465         lp->lnd_item[I_SHELL] = shells;
466         if (!res)
467             return 0;
468     }
469
470     return 1;
471 }