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