]> git.pond.sub.org Git - empserver/blob - src/lib/subs/supply.c
Update copyright notice
[empserver] / src / lib / subs / supply.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  supply.c: Supply subroutines
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2004-2011
31  */
32
33 #include <config.h>
34
35 #include <math.h>
36 #include "empobj.h"
37 #include "file.h"
38 #include "land.h"
39 #include "nat.h"
40 #include "optlist.h"
41 #include "path.h"
42 #include "prototypes.h"
43 #include "sect.h"
44 #include "ship.h"
45
46 static int s_commod(struct empobj *, short *, i_type, int, int, int);
47 static int get_minimum(struct lndstr *, i_type);
48
49 int
50 sct_supply(struct sctstr *sp, i_type type, int wanted)
51 {
52     return s_commod((struct empobj *)sp, sp->sct_item,
53                     type, wanted, ITEM_MAX, 1);
54 }
55
56 int
57 shp_supply(struct shpstr *sp, i_type type, int wanted)
58 {
59     return s_commod((struct empobj *)sp, sp->shp_item,
60                     type, wanted, mchr[sp->shp_type].m_item[type], 1);
61 }
62
63 int
64 lnd_supply(struct lndstr *lp, i_type type, int wanted)
65 {
66     return s_commod((struct empobj *)lp, lp->lnd_item,
67                     type, wanted, lchr[lp->lnd_type].l_item[type], 1);
68 }
69
70 int
71 lnd_in_supply(struct lndstr *lp)
72 {
73     if (!opt_NOFOOD) {
74         if (lp->lnd_item[I_FOOD] < get_minimum(lp, I_FOOD))
75             return 0;
76     }
77     return lp->lnd_item[I_SHELL] >= get_minimum(lp, I_SHELL);
78 }
79
80 int
81 lnd_supply_all(struct lndstr *lp)
82 {
83     int fail = 0;
84
85     if (!opt_NOFOOD)
86         fail |= !lnd_supply(lp, I_FOOD, get_minimum(lp, I_FOOD));
87     fail |= !lnd_supply(lp, I_SHELL, get_minimum(lp, I_SHELL));
88     return !fail;
89 }
90
91 /*
92  * Actually get the commod
93  *
94  * First, try to forage in the sector
95  * Second look for a warehouse or headquarters to leech
96  * Third, look for a ship we own in a harbor
97  * Fourth, look for supplies in a supply unit we own
98  *              (one good reason to do this last is that the supply
99  *               unit will then call resupply, taking more time)
100  *
101  * May want to put code to resupply with SAMs here, later --ts
102  */
103 static int
104 s_commod(struct empobj *sink, short *vec,
105          i_type type, int wanted, int limit, int actually_doit)
106 {
107     natid own = sink->own;
108     coord x = sink->x;
109     coord y = sink->y;
110     int lookrange;
111     struct sctstr sect;
112     struct nstr_sect ns;
113     struct nstr_item ni;
114     struct lchrstr *lcp;
115     struct shpstr ship;
116     struct lndstr land;
117     /* leave at least 1 military in sectors/ships */
118     int minimum = 0;
119     int can_move;
120     double move_cost, weight, mobcost;
121     int packing;
122     struct dchrstr *dp;
123     struct ichrstr *ip;
124
125     if (wanted > limit)
126         wanted = limit;
127     if (wanted <= vec[type])
128         return 1;
129     wanted -= vec[type];
130
131     /* try to get it from sector we're in */
132     if (sink->ef_type != EF_SECTOR) {
133         getsect(x, y, &sect);
134         if (sect.sct_own == own) {
135             if (!opt_NOFOOD && type == I_FOOD)
136                 minimum = 1 + (int)ceil(food_needed(sect.sct_item,
137                                                     etu_per_update));
138             if (sect.sct_item[type] - wanted >= minimum) {
139                 sect.sct_item[type] -= wanted;
140                 if (actually_doit) {
141                     vec[type] += wanted;
142                     putsect(&sect);
143                     put_empobj(sink->ef_type, sink->uid, sink);
144                 }
145                 return 1;
146             } else if (sect.sct_item[type] - minimum > 0) {
147                 wanted -= sect.sct_item[type] - minimum;
148                 sect.sct_item[type] = minimum;
149                 if (actually_doit) {
150                     vec[type] += sect.sct_item[type] - minimum;
151                     putsect(&sect);
152                 }
153             }
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 (ns.curdist == 0)
162             continue;
163         if (sect.sct_own != own)
164             continue;
165         if ((sect.sct_type != SCT_WAREH) &&
166             (sect.sct_type != SCT_HEADQ) && (sect.sct_type != SCT_HARBR))
167             continue;
168         if ((sect.sct_type == SCT_HEADQ) &&
169             (sect.sct_dist_x == sect.sct_x) &&
170             (sect.sct_dist_y == sect.sct_y))
171             continue;
172         if (sect.sct_effic < 60)
173             continue;
174         move_cost = path_find(sect.sct_x, sect.sct_y, x, y, own, MOB_MOVE);
175         if (move_cost < 0)
176             continue;
177         if (!opt_NOFOOD && type == I_FOOD)
178             minimum = 1 + (int)ceil(food_needed(sect.sct_item,
179                                                 etu_per_update));
180         if (sect.sct_item[type] <= minimum)
181             continue;
182         ip = &ichr[type];
183         dp = &dchr[sect.sct_type];
184         packing = ip->i_pkg[dp->d_pkg];
185         if (packing > 1 && sect.sct_effic < 60)
186             packing = 1;
187         weight = (double)ip->i_lbs / packing;
188         mobcost = move_cost * weight;
189         if (mobcost > 0)
190             can_move = (double)sect.sct_mobil / mobcost;
191         else
192             can_move = sect.sct_item[type] - minimum;
193         if (can_move > sect.sct_item[type] - minimum)
194             can_move = sect.sct_item[type] - minimum;
195
196         if (can_move >= wanted) {
197             int n;
198
199             sect.sct_item[type] -= wanted;
200
201             /* take off mobility for delivering sect */
202             n = roundavg(wanted * weight * move_cost);
203             if (n < 0)
204                 n = 0;
205             if (n > sect.sct_mobil)
206                 n = sect.sct_mobil;
207             sect.sct_mobil -= n;
208             if (actually_doit) {
209                 vec[type] += wanted;
210                 putsect(&sect);
211                 put_empobj(sink->ef_type, sink->uid, sink);
212             }
213             return 1;
214         } else if (can_move > 0) {
215             int n;
216             wanted -= can_move;
217             sect.sct_item[type] -= can_move;
218
219             /* take off mobility for delivering sect */
220             n = roundavg(can_move * weight * move_cost);
221             if (n < 0)
222                 n = 0;
223             if (n > sect.sct_mobil)
224                 n = sect.sct_mobil;
225             sect.sct_mobil -= n;
226             if (actually_doit) {
227                 vec[type] += can_move;
228                 putsect(&sect);
229             }
230         }
231     }
232
233     /* look for an owned ship in a harbor */
234     snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
235     while (nxtitem(&ni, &ship) && wanted) {
236         if (sink->ef_type == EF_SHIP && sink->uid == ship.shp_uid)
237             continue;
238         if (ship.shp_own != own)
239             continue;
240         if (!(mchr[(int)ship.shp_type].m_flags & M_SUPPLY))
241             continue;
242         getsect(ship.shp_x, ship.shp_y, &sect);
243         if (sect.sct_type != SCT_HARBR)
244             continue;
245         if (sect.sct_effic < 2)
246             continue;
247         move_cost = path_find(sect.sct_x, sect.sct_y, x, y, own, MOB_MOVE);
248         if (move_cost < 0)
249             continue;
250         if (!opt_NOFOOD && type == I_FOOD)
251             minimum = 1 + (int)ceil(food_needed(ship.shp_item,
252                                                 etu_per_update));
253         if (ship.shp_item[type] <= minimum)
254             continue;
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     while (nxtitem(&ni, &land) && wanted) {
307         int min;
308
309         if (sink->ef_type == EF_LAND && sink->uid == land.lnd_uid)
310             continue;
311         if (land.lnd_own != own)
312             continue;
313
314         lcp = &lchr[(int)land.lnd_type];
315         if (!(lcp->l_flags & L_SUPPLY))
316             continue;
317
318         if (land.lnd_item[type] <= get_minimum(&land, type))
319             continue;
320
321         if (land.lnd_ship >= 0) {
322             getsect(land.lnd_x, land.lnd_y, &sect);
323             if (sect.sct_type != SCT_HARBR || sect.sct_effic < 2)
324                 continue;
325         }
326
327         move_cost = path_find(land.lnd_x, land.lnd_y, x, y, own, MOB_MOVE);
328         if (move_cost < 0)
329             continue;
330
331 #if 0
332         /*
333          * Recursive supply is disabled for now.  It can introduce
334          * cycles into the "resupplies from" relation.  The code below
335          * attempts to break these cycles by temporarily zapping the
336          * commodity being supplied.  That puts the land file in a
337          * funny state temporarily, risking loss of supplies when
338          * something goes wrong on the way.  Worse, it increases
339          * lnd_seqno even when !actually_doit, which can lead to
340          * spurious seqno mismatch oopses in users of
341          * lnd_could_be_supplied().  I can't be bothered to clean up
342          * this mess right now, because recursive resupply is too dumb
343          * to be really useful anyway: each step uses the first source
344          * it finds, without consideration of mobility cost.  If you
345          * re-enable it, don't forget to uncomment its documentation
346          * in supply.t as well.
347          */
348         if (land.lnd_item[type] - wanted < get_minimum(&land, type)) {
349             struct lndstr save;
350
351             /*
352              * Temporarily zap this unit's store, so the recursion
353              * avoids it.
354              */
355             save = land;
356             land.lnd_item[type] = 0;
357             putland(land.lnd_uid, &land);
358             save.lnd_seqno = land.lnd_seqno;
359
360             s_commod((struct empobj *)&land, land.lnd_item, type, wanted,
361                      lchr[land.lnd_type].l_item[type] - wanted,
362                      actually_doit);
363             land.lnd_item[type] += save.lnd_item[type];
364
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 }