]> git.pond.sub.org Git - empserver/blob - src/lib/subs/supply.c
4c0dba491a45c369b0b2b2ffd2eea3fb77f1866a
[empserver] / src / lib / subs / supply.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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 "path.h"
43 #include "player.h"
44 #include "prototypes.h"
45 #include "sect.h"
46 #include "ship.h"
47
48 static int s_commod(struct empobj *, short *, i_type, int, int, int);
49 static int get_minimum(struct lndstr *, i_type);
50
51 int
52 sct_supply(struct sctstr *sp, i_type type, int wanted)
53 {
54     return s_commod((struct empobj *)sp, sp->sct_item,
55                     type, wanted, ITEM_MAX, 1);
56 }
57
58 int
59 shp_supply(struct shpstr *sp, i_type type, int wanted)
60 {
61     return s_commod((struct empobj *)sp, sp->shp_item,
62                     type, wanted, mchr[sp->shp_type].m_item[type], 1);
63 }
64
65 int
66 lnd_supply(struct lndstr *lp, i_type type, int wanted)
67 {
68     return s_commod((struct empobj *)lp, lp->lnd_item,
69                     type, wanted, lchr[lp->lnd_type].l_item[type], 1);
70 }
71
72 int
73 lnd_in_supply(struct lndstr *lp)
74 {
75     if (!opt_NOFOOD) {
76         if (lp->lnd_item[I_FOOD] < get_minimum(lp, I_FOOD))
77             return 0;
78     }
79     return lp->lnd_item[I_SHELL] >= get_minimum(lp, I_SHELL);
80 }
81
82 int
83 lnd_supply_all(struct lndstr *lp)
84 {
85     int fail = 0;
86
87     if (!opt_NOFOOD)
88         fail |= !lnd_supply(lp, I_FOOD, get_minimum(lp, I_FOOD));
89     fail |= !lnd_supply(lp, I_SHELL, get_minimum(lp, I_SHELL));
90     return !fail;
91 }
92
93 /*
94  * Actually get the commod
95  *
96  * First, try to forage in the sector
97  * Second look for a warehouse or headquarters to leech
98  * Third, look for a ship we own in a harbor
99  * Fourth, look for supplies in a supply unit we own
100  *              (one good reason to do this last is that the supply
101  *               unit will then call resupply, taking more time)
102  *
103  * May want to put code to resupply with SAMs here, later --ts
104  */
105 static int
106 s_commod(struct empobj *sink, short *vec,
107          i_type type, int wanted, int limit, int actually_doit)
108 {
109     natid own = sink->own;
110     coord x = sink->x;
111     coord y = sink->y;
112     int lookrange;
113     struct sctstr sect, dest;
114     struct nstr_sect ns;
115     struct nstr_item ni;
116     struct lchrstr *lcp;
117     struct shpstr ship;
118     struct lndstr land;
119     /* leave at least 1 military in sectors/ships */
120     int minimum = 0;
121     int can_move;
122     double move_cost, weight, mobcost;
123     int packing;
124     struct dchrstr *dp;
125     struct ichrstr *ip;
126     char buf[1024];
127
128     if (wanted > limit)
129         wanted = limit;
130     if (wanted <= vec[type])
131         return 1;
132     wanted -= vec[type];
133
134     getsect(x, y, &dest);
135
136     /* try to get it from sector we're in */
137     if (sink->ef_type != EF_SECTOR) {
138         getsect(x, y, &sect);
139         if (sect.sct_own == own) {
140             if (!opt_NOFOOD && type == I_FOOD)
141                 minimum = 1 + (int)ceil(food_needed(sect.sct_item,
142                                                     etu_per_update));
143             if (sect.sct_item[type] - wanted >= minimum) {
144                 sect.sct_item[type] -= wanted;
145                 if (actually_doit) {
146                     vec[type] += wanted;
147                     putsect(&sect);
148                     put_empobj(sink->ef_type, sink->uid, sink);
149                 }
150                 return 1;
151             } else if (sect.sct_item[type] - minimum > 0) {
152                 wanted -= sect.sct_item[type] - minimum;
153                 sect.sct_item[type] = minimum;
154                 if (actually_doit) {
155                     vec[type] += sect.sct_item[type] - minimum;
156                     putsect(&sect);
157                 }
158             }
159         }
160     }
161
162     /* look for a headquarters or warehouse */
163     lookrange = tfact(own, 10.0);
164     snxtsct_dist(&ns, x, y, lookrange);
165     while (nxtsct(&ns, &sect) && wanted) {
166         if (ns.curdist == 0)
167             continue;
168         if (sect.sct_own != own)
169             continue;
170         if ((sect.sct_type != SCT_WAREH) &&
171             (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
172             continue;
173         if ((sect.sct_type == SCT_HEADQ) &&
174             (sect.sct_dist_x == sect.sct_x) &&
175             (sect.sct_dist_y == sect.sct_y))
176             continue;
177         if (sect.sct_effic < 60)
178             continue;
179         if (!BestLandPath(buf, &dest, &sect, &move_cost, MOB_MOVE))
180             continue;
181         if (!opt_NOFOOD && type == I_FOOD)
182             minimum = 1 + (int)ceil(food_needed(sect.sct_item,
183                                                 etu_per_update));
184         if (sect.sct_item[type] <= minimum)
185             continue;
186         ip = &ichr[type];
187         dp = &dchr[sect.sct_type];
188         packing = ip->i_pkg[dp->d_pkg];
189         if (packing > 1 && sect.sct_effic < 60)
190             packing = 1;
191         weight = (double)ip->i_lbs / packing;
192         mobcost = move_cost * weight;
193         if (mobcost > 0)
194             can_move = (double)sect.sct_mobil / mobcost;
195         else
196             can_move = sect.sct_item[type] - minimum;
197         if (can_move > sect.sct_item[type] - minimum)
198             can_move = sect.sct_item[type] - minimum;
199
200         if (can_move >= wanted) {
201             int n;
202
203             sect.sct_item[type] -= wanted;
204
205             /* take off mobility for delivering sect */
206             n = roundavg(wanted * weight * move_cost);
207             if (n < 0)
208                 n = 0;
209             if (n > sect.sct_mobil)
210                 n = sect.sct_mobil;
211             sect.sct_mobil -= n;
212             if (actually_doit) {
213                 vec[type] += wanted;
214                 putsect(&sect);
215                 put_empobj(sink->ef_type, sink->uid, sink);
216             }
217             return 1;
218         } else if (can_move > 0) {
219             int n;
220             wanted -= can_move;
221             sect.sct_item[type] -= can_move;
222
223             /* take off mobility for delivering sect */
224             n = roundavg(can_move * weight * move_cost);
225             if (n < 0)
226                 n = 0;
227             if (n > sect.sct_mobil)
228                 n = sect.sct_mobil;
229             sect.sct_mobil -= n;
230             if (actually_doit) {
231                 vec[type] += can_move;
232                 putsect(&sect);
233             }
234         }
235     }
236
237     /* look for an owned ship in a harbor */
238     snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
239     while (nxtitem(&ni, &ship) && wanted) {
240         if (sink->ef_type == EF_SHIP && sink->uid == ship.shp_uid)
241             continue;
242         if (ship.shp_own != own)
243             continue;
244         if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
245             continue;
246         getsect(ship.shp_x, ship.shp_y, &sect);
247         if (sect.sct_type != SCT_HARBR)
248             continue;
249         if (sect.sct_effic < 2)
250             continue;
251         if (!BestLandPath(buf, &dest, &sect, &move_cost, MOB_MOVE))
252             continue;
253         if (!opt_NOFOOD && type == I_FOOD)
254             minimum = 1 + (int)ceil(food_needed(ship.shp_item,
255                                                 etu_per_update));
256         if (ship.shp_item[type] <= minimum)
257             continue;
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                 vec[type] += can_move;
283                 putship(ship.shp_uid, &ship);
284                 putsect(&sect);
285                 put_empobj(sink->ef_type, sink->uid, sink);
286             }
287             return 1;
288         } else if (can_move > 0) {
289             int n;
290             wanted -= can_move;
291             ship.shp_item[type] -= can_move;
292
293             n = roundavg(can_move * weight * move_cost);
294             if (n < 0)
295                 n = 0;
296             if (n > sect.sct_mobil)
297                 n = sect.sct_mobil;
298             sect.sct_mobil -= n;
299             if (actually_doit) {
300                 vec[type] += can_move;
301                 putship(ship.shp_uid, &ship);
302                 putsect(&sect);
303             }
304         }
305     }
306
307     /* look for an owned supply unit */
308     snxtitem_dist(&ni, EF_LAND, x, y, lookrange);
309     while (nxtitem(&ni, &land) && wanted) {
310         int min;
311
312         if (sink->ef_type == EF_LAND && sink->uid == land.lnd_uid)
313             continue;
314         if (land.lnd_own != own)
315             continue;
316
317         lcp = &lchr[(int)land.lnd_type];
318         if (!(lcp->l_flags & L_SUPPLY))
319             continue;
320
321         if (land.lnd_item[type] <= get_minimum(&land, type))
322             continue;
323
324         getsect(land.lnd_x, land.lnd_y, &sect);
325         if (!BestLandPath(buf, &dest, &sect, &move_cost, MOB_MOVE))
326             continue;
327
328         if ((land.lnd_ship >= 0) && (sect.sct_type != SCT_HARBR))
329             continue;
330
331         if ((land.lnd_ship >= 0) && (sect.sct_effic < 2))
332             continue;
333
334 #if 0
335         /*
336          * Recursive supply is disabled for now.  It can introduce
337          * cycles into the "resupplies from" relation.  The code below
338          * attempts to break these cycles by temporarily zapping the
339          * commodity being supplied.  That puts the land file in a
340          * funny state temporarily, risking loss of supplies when
341          * something goes wrong on the way.  Worse, it increases
342          * lnd_seqno even when !actually_doit, which can lead to
343          * spurious seqno mismatch oopses in users of
344          * lnd_could_be_supplied().  I can't be bothered to clean up
345          * this mess right now, because recursive resupply is too dumb
346          * to be really useful anyway: each step uses the first source
347          * it finds, without consideration of mobility cost.  If you
348          * re-enable it, don't forget to uncomment its documentation
349          * in supply.t as well.
350          */
351         if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
352             struct lndstr save;
353
354             /*
355              * Temporarily zap this unit's store, so the recursion
356              * avoids it.
357              */
358             save = land;
359             land.lnd_item[type] = 0;
360             putland(land.lnd_uid, &land);
361             save.lnd_seqno = land.lnd_seqno;
362
363             s_commod((struct empobj *)&land, land.lnd_item, type, wanted,
364                      lchr[land.lnd_type].l_item[type] - wanted,
365                      actually_doit);
366             land.lnd_item[type] += save.lnd_item[type];
367
368             if (actually_doit)
369                 putland(land.lnd_uid, &land);
370             else
371                 putland(save.lnd_uid, &save);
372         }
373 #endif
374
375         min = get_minimum(&land, type);
376         ip = &ichr[type];
377         weight = ip->i_lbs;
378         mobcost = move_cost * weight;
379         if (mobcost > 0)
380             can_move = (double)land.lnd_mobil / mobcost;
381         else
382             can_move = land.lnd_item[type] - min;
383         if (can_move > land.lnd_item[type] - min)
384             can_move = land.lnd_item[type] - min;
385
386         if (can_move >= wanted) {
387             land.lnd_item[type] -= wanted;
388             land.lnd_mobil -= roundavg(wanted * weight * move_cost);
389             if (actually_doit) {
390                 vec[type] += wanted;
391                 putland(land.lnd_uid, &land);
392                 put_empobj(sink->ef_type, sink->uid, sink);
393             }
394             return 1;
395         } else if (can_move > 0) {
396             wanted -= can_move;
397             land.lnd_item[type] -= can_move;
398             land.lnd_mobil -= roundavg(can_move * weight * move_cost);
399             if (actually_doit) {
400                 vec[type] += can_move;
401                 putland(land.lnd_uid, &land);
402             }
403         }
404     }
405
406     if (actually_doit)
407         put_empobj(sink->ef_type, sink->uid, sink);
408     return 0;
409 }
410
411 /*
412  * We want to get enough shells to fire once,
413  * one update's worth of food.
414  */
415 static int
416 get_minimum(struct lndstr *lp, i_type type)
417 {
418     struct lchrstr *lcp;
419     int max, want = 0;
420
421     lcp = &lchr[(int)lp->lnd_type];
422     max = lcp->l_item[type];
423
424     switch (type) {
425     case I_FOOD:
426         if (opt_NOFOOD)
427             return 0;           /* no food reqd, get out */
428         want = (int)ceil(food_needed(lp->lnd_item, etu_per_update));
429         break;
430     case I_SHELL:
431         want = lcp->l_ammo;
432         break;
433     default:
434         return 0;
435     }
436
437     if (want > max)
438         want = max;
439
440     return want;
441 }
442
443 int
444 lnd_could_be_supplied(struct lndstr *lp)
445 {
446     int res, food, food_needed, shells_needed, shells;
447
448     if (!opt_NOFOOD) {
449         food_needed = get_minimum(lp, I_FOOD);
450         food = lp->lnd_item[I_FOOD];
451         if (food < food_needed) {
452             res = s_commod((struct empobj *)lp, lp->lnd_item,
453                            I_FOOD, food_needed,
454                            lchr[lp->lnd_type].l_item[I_FOOD], 0);
455             lp->lnd_item[I_FOOD] = food;
456             if (!res)
457                 return 0;
458         }
459     }
460
461     shells_needed = lchr[lp->lnd_type].l_ammo;
462     shells = lp->lnd_item[I_SHELL];
463     if (shells < shells_needed) {
464         res = s_commod((struct empobj *)lp, lp->lnd_item,
465                        I_SHELL, shells_needed,
466                        lchr[lp->lnd_type].l_item[I_SHELL], 0);
467         lp->lnd_item[I_SHELL] = shells;
468         if (!res)
469             return 0;
470     }
471
472     return 1;
473 }