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