]> git.pond.sub.org Git - empserver/blob - src/lib/subs/supply.c
1a10d19a85023b801e026674656386f40653feac
[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 /*
90  * Actually get the commod
91  */
92 int
93 supply_commod(int own, int x, int y, i_type type, int total_wanted)
94 {
95     if (total_wanted <= 0)
96         return 0;
97     return s_commod(own, x, y, type, total_wanted, !player->simulation);
98 }
99
100 /*
101  * Just return the number you COULD get, without doing it
102  */
103 static int
104 try_supply_commod(int own, int x, int y, i_type type, int total_wanted)
105 {
106     if (total_wanted <= 0)
107         return 0;
108
109     return s_commod(own, x, y, type, total_wanted, 0);
110 }
111
112 /* Get supplies of a certain type */
113 static int
114 s_commod(int own, int x, int y, i_type type, int total_wanted,
115          int actually_doit)
116 {
117     int wanted = total_wanted;
118     int gotten = 0, lookrange;
119     struct sctstr sect, dest;
120     struct nstr_sect ns;
121     struct nstr_item ni;
122     struct lchrstr *lcp;
123     struct shpstr ship;
124     struct lndstr land;
125     /* leave at least 1 military in sectors/ships */
126     int minimum = (type == I_MILIT ? 1 : 0);
127     int can_move;
128     double move_cost, weight, mobcost;
129     int packing;
130     struct dchrstr *dp;
131     struct ichrstr *ip;
132     char buf[1024];
133
134     /* try to get it from sector we're in */
135     getsect(x, y, &dest);
136     getsect(x, y, &sect);
137     if (sect.sct_own == own) {
138         if (sect.sct_item[type] - wanted >= minimum) {
139             sect.sct_item[type] -= wanted;
140             if (actually_doit)
141                 putsect(&sect);
142             return total_wanted;
143         } else if (sect.sct_item[type] - minimum > 0) {
144             gotten += sect.sct_item[type] - minimum;
145             wanted -= sect.sct_item[type] - minimum;
146             sect.sct_item[type] = minimum;
147             if (actually_doit)
148                 putsect(&sect);
149         }
150     }
151     /* look for a headquarters or warehouse */
152     lookrange = tfact(own, 10.0);
153     snxtsct_dist(&ns, x, y, lookrange);
154     while (nxtsct(&ns, &sect) && wanted) {
155         if (sect.sct_own != own)
156             continue;
157         if ((sect.sct_type != SCT_WAREH) &&
158             (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
159             continue;
160         if ((sect.sct_type == SCT_HEADQ) &&
161             (sect.sct_dist_x == sect.sct_x) &&
162             (sect.sct_dist_y == sect.sct_y))
163             continue;
164         if (sect.sct_effic < 60)
165             continue;
166         if (!BestLandPath(buf, &dest, &sect, &move_cost, MOB_MOVE))
167             continue;
168         if (!opt_NOFOOD && type == I_FOOD)
169             minimum = 1 + (int)ceil(food_needed(sect.sct_item,
170                                                 etu_per_update));
171         if (sect.sct_item[type] <= minimum) {
172             /* Don't bother... */
173             continue;
174         }
175         ip = &ichr[type];
176         dp = &dchr[sect.sct_type];
177         packing = ip->i_pkg[dp->d_pkg];
178         if (packing > 1 && sect.sct_effic < 60)
179             packing = 1;
180         weight = (double)ip->i_lbs / packing;
181         mobcost = move_cost * weight;
182         if (mobcost > 0)
183             can_move = (double)sect.sct_mobil / mobcost;
184         else
185             can_move = sect.sct_item[type] - minimum;
186         if (can_move > sect.sct_item[type] - minimum)
187             can_move = sect.sct_item[type] - minimum;
188
189         if (can_move >= wanted) {
190             int n;
191
192             sect.sct_item[type] -= wanted;
193
194             /* take off mobility for delivering sect */
195             n = roundavg(total_wanted * weight * move_cost);
196             if (n < 0)
197                 n = 0;
198             if (n > sect.sct_mobil)
199                 n = sect.sct_mobil;
200             sect.sct_mobil -= n;
201
202             if (actually_doit)
203                 putsect(&sect);
204
205             return total_wanted;
206         } else if (can_move > 0) {
207             int n;
208             gotten += can_move;
209             wanted -= can_move;
210             sect.sct_item[type] -= can_move;
211
212             /* take off mobility for delivering sect */
213             n = roundavg(can_move * weight * move_cost);
214             if (n < 0)
215                 n = 0;
216             if (n > sect.sct_mobil)
217                 n = sect.sct_mobil;
218             sect.sct_mobil -= n;
219
220             if (actually_doit)
221                 putsect(&sect);
222         }
223     }
224
225     /* look for an owned ship in a harbor */
226     snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
227
228     while (nxtitem(&ni, &ship) && wanted) {
229         if (ship.shp_own != own)
230             continue;
231
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         if (!BestLandPath(buf, &dest, &sect, &move_cost, MOB_MOVE))
240             continue;
241         if (!opt_NOFOOD && type == I_FOOD)
242             minimum = 1 + (int)ceil(food_needed(ship.shp_item,
243                                                 etu_per_update));
244         if (ship.shp_item[type] <= minimum) {
245             /* Don't bother... */
246             continue;
247         }
248         ip = &ichr[type];
249         dp = &dchr[sect.sct_type];
250         packing = ip->i_pkg[dp->d_pkg];
251         if (packing > 1 && sect.sct_effic < 60)
252             packing = 1;
253         weight = (double)ip->i_lbs / packing;
254         mobcost = move_cost * weight;
255         if (mobcost > 0)
256             can_move = (double)sect.sct_mobil / mobcost;
257         else
258             can_move = ship.shp_item[type] - minimum;
259         if (can_move > ship.shp_item[type] - minimum)
260             can_move = ship.shp_item[type] - minimum;
261         if (can_move >= wanted) {
262             int n;
263             ship.shp_item[type] -= wanted;
264
265             n = roundavg(wanted * weight * move_cost);
266             if (n < 0)
267                 n = 0;
268             if (n > sect.sct_mobil)
269                 n = sect.sct_mobil;
270             sect.sct_mobil -= n;
271             if (actually_doit) {
272                 putship(ship.shp_uid, &ship);
273                 putsect(&sect);
274             }
275             return total_wanted;
276         } else if (can_move > 0) {
277             int n;
278             gotten += can_move;
279             wanted -= can_move;
280             ship.shp_item[type] -= can_move;
281
282             n = roundavg(can_move * weight * move_cost);
283             if (n < 0)
284                 n = 0;
285             if (n > sect.sct_mobil)
286                 n = sect.sct_mobil;
287             sect.sct_mobil -= n;
288
289             if (actually_doit) {
290                 putship(ship.shp_uid, &ship);
291                 putsect(&sect);
292             }
293         }
294     }
295
296     /* look for an owned supply unit */
297     snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
298
299     while (nxtitem(&ni, &land) && wanted) {
300         int min;
301
302         if (land.lnd_own != own)
303             continue;
304
305         lcp = &lchr[(int)land.lnd_type];
306         if (!(lcp->l_flags & L_SUPPLY))
307             continue;
308
309         if (land.lnd_item[type] <= get_minimum(&land, type))
310             continue;
311
312         getsect(land.lnd_x, land.lnd_y, &sect);
313         if (!BestLandPath(buf, &dest, &sect, &move_cost, MOB_MOVE))
314             continue;
315
316         if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
317             continue;
318
319         if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
320             continue;
321
322         if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
323             struct lndstr save;
324
325             /*
326              * Temporarily zap this unit's store, so the recursion
327              * avoids it.
328              */
329             save = land;
330             land.lnd_item[type] = 0;
331             putland(land.lnd_uid, &land);
332             save.lnd_seqno = land.lnd_seqno;
333
334             land.lnd_item[type] =
335                 save.lnd_item[type] + s_commod(own, land.lnd_x, land.lnd_y,
336                                                type, wanted, actually_doit);
337             if (actually_doit)
338                 putland(land.lnd_uid, &land);
339             else
340                 putland(save.lnd_uid, &save);
341         }
342
343         min = get_minimum(&land, type);
344         ip = &ichr[type];
345         weight = ip->i_lbs;
346         mobcost = move_cost * weight;
347         if (mobcost > 0)
348             can_move = (double)land.lnd_mobil / mobcost;
349         else
350             can_move = land.lnd_item[type] - min;
351         if (can_move > land.lnd_item[type] - min)
352             can_move = land.lnd_item[type] - min;
353
354         if (can_move >= wanted) {
355             land.lnd_item[type] -= wanted;
356
357             /* resupply the supply unit */
358             resupply_commod(&land, type);
359
360             land.lnd_mobil -= roundavg(wanted * weight * move_cost);
361
362             if (actually_doit)
363                 putland(land.lnd_uid, &land);
364             return total_wanted;
365         } else if (can_move > 0) {
366             gotten += can_move;
367             wanted -= can_move;
368             land.lnd_item[type] -= can_move;
369
370             land.lnd_mobil -= roundavg(can_move * weight * move_cost);
371
372             if (actually_doit)
373                 putland(land.lnd_uid, &land);
374         }
375     }
376
377     /* We've done the best we could */
378     /* return the number gotten */
379     return gotten;
380 }
381
382
383 /*
384  * We want to get enough shells to fire once,
385  * one update's worth of food.
386  */
387
388 static int
389 get_minimum(struct lndstr *lp, i_type type)
390 {
391     struct lchrstr *lcp;
392     int max, want = 0;
393
394     lcp = &lchr[(int)lp->lnd_type];
395     max = lcp->l_item[type];
396
397     switch (type) {
398     case I_FOOD:
399         if (opt_NOFOOD)
400             return 0;           /* no food reqd, get out */
401         want = (int)ceil(food_needed(lp->lnd_item, etu_per_update));
402         break;
403     case I_SHELL:
404         want = lcp->l_ammo;
405         break;
406     default:
407         return 0;
408     }
409
410     if (want > max)
411         want = max;
412
413     return want;
414 }
415
416 int
417 has_supply(struct lndstr *lp)
418 {
419     int shells_needed, shells, keepshells;
420     int food, food_needed, keepfood;
421
422     if (!opt_NOFOOD) {
423         food_needed = get_minimum(lp, I_FOOD);
424         food = keepfood = lp->lnd_item[I_FOOD];
425         if (food < food_needed) {
426             lp->lnd_item[I_FOOD] = 0;
427             putland(lp->lnd_uid, lp);
428             food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
429                                       I_FOOD, (food_needed - food));
430             lp->lnd_item[I_FOOD] = keepfood;
431             putland(lp->lnd_uid, lp);
432         }
433         if (food < food_needed)
434             return 0;
435
436     }
437
438     shells_needed = lchr[lp->lnd_type].l_ammo;
439     shells = keepshells = lp->lnd_item[I_SHELL];
440     if (shells < shells_needed) {
441         lp->lnd_item[I_SHELL] = 0;
442         putland(lp->lnd_uid, lp);
443         shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
444                                     I_SHELL, (shells_needed - shells));
445         lp->lnd_item[I_SHELL] = keepshells;
446         putland(lp->lnd_uid, lp);
447     }
448
449     if (shells < shells_needed)
450         return 0;
451
452     return 1;
453 }