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