]> git.pond.sub.org Git - empserver/blob - src/lib/subs/supply.c
Fix mobility cost of automatic supply from remote sector
[empserver] / src / lib / subs / supply.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  supply.c: Supply subroutines
29  *
30  *  Known contributors to this file:
31  *
32  */
33
34 #include <config.h>
35
36 #include <math.h>
37 #include "file.h"
38 #include "land.h"
39 #include "nat.h"
40 #include "optlist.h"
41 #include "player.h"
42 #include "prototypes.h"
43 #include "sect.h"
44 #include "ship.h"
45
46 static int get_minimum(struct lndstr *, i_type);
47 static int s_commod(int, int, int, i_type, int, int);
48
49 /*
50  * We want to get enough guns to be maxed out, enough shells to
51  *      fire once, one update's worth of food.
52  *
53  * Firts, try to forage in the sector
54  * Second look for a warehouse or headquarters to leech
55  * Third, look for a ship we own in a harbor
56  * Fourth, look for supplies in a supply unit we own
57  *              (one good reason to do this last is that the supply
58  *               unit will then call resupply, taking more time)
59  *
60  * May want to put code to resupply with SAMs here, later --ts
61  */
62
63 void
64 resupply_all(struct lndstr *lp)
65 {
66     if (!opt_NOFOOD)
67         resupply_commod(lp, I_FOOD);
68     resupply_commod(lp, I_SHELL);
69 }
70
71 /*
72  * If the unit has less than it's minimum level of a
73  * certain commodity, fill it, to the best of our abilities.
74  */
75
76 void
77 resupply_commod(struct lndstr *lp, i_type type)
78 {
79     int amt;
80
81     amt = get_minimum(lp, type) - lp->lnd_item[type];
82     if (amt > 0) {
83         lp->lnd_item[type] += supply_commod(lp->lnd_own,
84                                             lp->lnd_x, lp->lnd_y,
85                                             type, amt);
86     }
87 }
88
89 int
90 lnd_in_supply(struct lndstr *lp)
91 {
92     if (!opt_NOFOOD) {
93         if (lp->lnd_item[I_FOOD] < get_minimum(lp, I_FOOD))
94             return 0;
95     }
96     return lp->lnd_item[I_SHELL] >= get_minimum(lp, I_SHELL);
97 }
98
99 /*
100  * Actually get the commod
101  */
102 int
103 supply_commod(int own, int x, int y, i_type type, int total_wanted)
104 {
105     if (total_wanted <= 0)
106         return 0;
107     return s_commod(own, x, y, type, total_wanted, !player->simulation);
108 }
109
110 /*
111  * Just return the number you COULD get, without doing it
112  */
113 static int
114 try_supply_commod(int own, int x, int y, i_type type, int total_wanted)
115 {
116     if (total_wanted <= 0)
117         return 0;
118
119     return s_commod(own, x, y, type, total_wanted, 0);
120 }
121
122 /* Get supplies of a certain type */
123 static int
124 s_commod(int own, int x, int y, i_type type, int total_wanted,
125          int actually_doit)
126 {
127     int wanted = total_wanted;
128     int gotten = 0, lookrange;
129     struct sctstr sect, dest;
130     struct nstr_sect ns;
131     struct nstr_item ni;
132     struct lchrstr *lcp;
133     struct shpstr ship;
134     struct lndstr land;
135     /* leave at least 1 military in sectors/ships */
136     int minimum = (type == I_MILIT ? 1 : 0);
137     int can_move;
138     double move_cost, weight, mobcost;
139     int packing;
140     struct dchrstr *dp;
141     struct ichrstr *ip;
142     char buf[1024];
143
144     /* try to get it from sector we're in */
145     getsect(x, y, &dest);
146     getsect(x, y, &sect);
147     if (sect.sct_own == own) {
148         if (sect.sct_item[type] - wanted >= minimum) {
149             sect.sct_item[type] -= wanted;
150             if (actually_doit)
151                 putsect(&sect);
152             return total_wanted;
153         } else if (sect.sct_item[type] - minimum > 0) {
154             gotten += sect.sct_item[type] - minimum;
155             wanted -= sect.sct_item[type] - minimum;
156             sect.sct_item[type] = minimum;
157             if (actually_doit)
158                 putsect(&sect);
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 (sect.sct_own != own)
166             continue;
167         if ((sect.sct_type != SCT_WAREH) &&
168             (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
169             continue;
170         if ((sect.sct_type == SCT_HEADQ) &&
171             (sect.sct_dist_x == sect.sct_x) &&
172             (sect.sct_dist_y == sect.sct_y))
173             continue;
174         if (sect.sct_effic < 60)
175             continue;
176         if (!BestLandPath(buf, &dest, &sect, &move_cost, MOB_MOVE))
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             /* Don't bother... */
183             continue;
184         }
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
212             if (actually_doit)
213                 putsect(&sect);
214
215             return total_wanted;
216         } else if (can_move > 0) {
217             int n;
218             gotten += can_move;
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
230             if (actually_doit)
231                 putsect(&sect);
232         }
233     }
234
235     /* look for an owned ship in a harbor */
236     snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
237
238     while (nxtitem(&ni, &ship) && wanted) {
239         if (ship.shp_own != own)
240             continue;
241
242         if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
243             continue;
244         getsect(ship.shp_x, ship.shp_y, &sect);
245         if (sect.sct_type != SCT_HARBR)
246             continue;
247         if (sect.sct_effic < 2)
248             continue;
249         if (!BestLandPath(buf, &dest, &sect, &move_cost, MOB_MOVE))
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             /* Don't bother... */
256             continue;
257         }
258         ip = &ichr[type];
259         dp = &dchr[sect.sct_type];
260         packing = ip->i_pkg[dp->d_pkg];
261         if (packing > 1 && sect.sct_effic < 60)
262             packing = 1;
263         weight = (double)ip->i_lbs / packing;
264         mobcost = move_cost * weight;
265         if (mobcost > 0)
266             can_move = (double)sect.sct_mobil / mobcost;
267         else
268             can_move = ship.shp_item[type] - minimum;
269         if (can_move > ship.shp_item[type] - minimum)
270             can_move = ship.shp_item[type] - minimum;
271         if (can_move >= wanted) {
272             int n;
273             ship.shp_item[type] -= wanted;
274
275             n = roundavg(wanted * weight * move_cost);
276             if (n < 0)
277                 n = 0;
278             if (n > sect.sct_mobil)
279                 n = sect.sct_mobil;
280             sect.sct_mobil -= n;
281             if (actually_doit) {
282                 putship(ship.shp_uid, &ship);
283                 putsect(&sect);
284             }
285             return total_wanted;
286         } else if (can_move > 0) {
287             int n;
288             gotten += can_move;
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
299             if (actually_doit) {
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
309     while (nxtitem(&ni, &land) && wanted) {
310         int min;
311
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         getsect(land.lnd_x, land.lnd_y, &sect);
323         if (!BestLandPath(buf, &dest, &sect, &move_cost, MOB_MOVE))
324             continue;
325
326         if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
327             continue;
328
329         if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
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             land.lnd_item[type] =
362                 save.lnd_item[type] + s_commod(own, land.lnd_x, land.lnd_y,
363                                                type, wanted, actually_doit);
364             if (actually_doit)
365                 putland(land.lnd_uid, &land);
366             else
367                 putland(save.lnd_uid, &save);
368         }
369 #endif
370
371         min = get_minimum(&land, type);
372         ip = &ichr[type];
373         weight = ip->i_lbs;
374         mobcost = move_cost * weight;
375         if (mobcost > 0)
376             can_move = (double)land.lnd_mobil / mobcost;
377         else
378             can_move = land.lnd_item[type] - min;
379         if (can_move > land.lnd_item[type] - min)
380             can_move = land.lnd_item[type] - min;
381
382         if (can_move >= wanted) {
383             land.lnd_item[type] -= wanted;
384             land.lnd_mobil -= roundavg(wanted * weight * move_cost);
385
386             if (actually_doit)
387                 putland(land.lnd_uid, &land);
388             return total_wanted;
389         } else if (can_move > 0) {
390             gotten += can_move;
391             wanted -= can_move;
392             land.lnd_item[type] -= can_move;
393
394             land.lnd_mobil -= roundavg(can_move * weight * move_cost);
395
396             if (actually_doit)
397                 putland(land.lnd_uid, &land);
398         }
399     }
400
401     /* We've done the best we could */
402     /* return the number gotten */
403     return gotten;
404 }
405
406
407 /*
408  * We want to get enough shells to fire once,
409  * one update's worth of food.
410  */
411
412 static int
413 get_minimum(struct lndstr *lp, i_type type)
414 {
415     struct lchrstr *lcp;
416     int max, want = 0;
417
418     lcp = &lchr[(int)lp->lnd_type];
419     max = lcp->l_item[type];
420
421     switch (type) {
422     case I_FOOD:
423         if (opt_NOFOOD)
424             return 0;           /* no food reqd, get out */
425         want = (int)ceil(food_needed(lp->lnd_item, etu_per_update));
426         break;
427     case I_SHELL:
428         want = lcp->l_ammo;
429         break;
430     default:
431         return 0;
432     }
433
434     if (want > max)
435         want = max;
436
437     return want;
438 }
439
440 int
441 lnd_could_be_supplied(struct lndstr *lp)
442 {
443     int shells_needed, shells, keepshells;
444     int food, food_needed, keepfood;
445
446     if (!opt_NOFOOD) {
447         food_needed = get_minimum(lp, I_FOOD);
448         food = keepfood = lp->lnd_item[I_FOOD];
449         if (food < food_needed) {
450             lp->lnd_item[I_FOOD] = 0;
451             putland(lp->lnd_uid, lp);
452             food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
453                                       I_FOOD, (food_needed - food));
454             lp->lnd_item[I_FOOD] = keepfood;
455             putland(lp->lnd_uid, lp);
456         }
457         if (food < food_needed)
458             return 0;
459
460     }
461
462     shells_needed = lchr[lp->lnd_type].l_ammo;
463     shells = keepshells = lp->lnd_item[I_SHELL];
464     if (shells < shells_needed) {
465         lp->lnd_item[I_SHELL] = 0;
466         putland(lp->lnd_uid, lp);
467         shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
468                                     I_SHELL, (shells_needed - shells));
469         lp->lnd_item[I_SHELL] = keepshells;
470         putland(lp->lnd_uid, lp);
471     }
472
473     if (shells < shells_needed)
474         return 0;
475
476     return 1;
477 }