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