]> git.pond.sub.org Git - empserver/blob - src/lib/subs/supply.c
Fix return value of s_commod() when it can't draw enough
[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, 2004-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     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, &dest, &sect, &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, &dest, &sect, &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, &dest, &sect, &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             land.lnd_item[type] =
363                 save.lnd_item[type] + s_commod(own, land.lnd_x, land.lnd_y,
364                                                type, wanted, actually_doit);
365             if (actually_doit)
366                 putland(land.lnd_uid, &land);
367             else
368                 putland(save.lnd_uid, &save);
369         }
370 #endif
371
372         min = get_minimum(&land, type);
373         ip = &ichr[type];
374         weight = ip->i_lbs;
375         mobcost = move_cost * weight;
376         if (mobcost > 0)
377             can_move = (double)land.lnd_mobil / mobcost;
378         else
379             can_move = land.lnd_item[type] - min;
380         if (can_move > land.lnd_item[type] - min)
381             can_move = land.lnd_item[type] - min;
382
383         if (can_move >= wanted) {
384             land.lnd_item[type] -= wanted;
385             land.lnd_mobil -= roundavg(wanted * weight * move_cost);
386             if (actually_doit) {
387                 vec[type] += wanted;
388                 putland(land.lnd_uid, &land);
389                 put_empobj(sink->ef_type, sink->uid, sink);
390             }
391             return 1;
392         } else if (can_move > 0) {
393             wanted -= can_move;
394             land.lnd_item[type] -= can_move;
395             land.lnd_mobil -= roundavg(can_move * weight * move_cost);
396             if (actually_doit) {
397                 vec[type] += can_move;
398                 putland(land.lnd_uid, &land);
399             }
400         }
401     }
402
403     if (actually_doit)
404         put_empobj(sink->ef_type, sink->uid, sink);
405     return 0;
406 }
407
408 /*
409  * We want to get enough shells to fire once,
410  * one update's worth of food.
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 res, food, food_needed, shells_needed, shells;
444
445     if (!opt_NOFOOD) {
446         food_needed = get_minimum(lp, I_FOOD);
447         food = lp->lnd_item[I_FOOD];
448         if (food < food_needed) {
449             res = s_commod((struct empobj *)lp, lp->lnd_item,
450                            I_FOOD, food_needed,
451                            lchr[lp->lnd_type].l_item[I_FOOD], 0);
452             lp->lnd_item[I_FOOD] = food;
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         res = s_commod((struct empobj *)lp, lp->lnd_item,
462                        I_SHELL, shells_needed,
463                        lchr[lp->lnd_type].l_item[I_SHELL], 0);
464         lp->lnd_item[I_SHELL] = shells;
465         if (!res)
466             return 0;
467     }
468
469     return 1;
470 }