Redesign s_commod()
To implement the supply from self avoidance described in the previous commit's message, s_commod() needs to be redesigned along the same principles: take the sink as argument, update and put it. Also take an item limit argument, and return whether supply request was fully met. Update sct_supply(), shp_supply(), lnd_supply() and lnd_could_be_supplied(). The former three become straighforward wrappers. supply_commod() and try_supply_commod() are now unused, remove them.
This commit is contained in:
parent
322f96ecb7
commit
98f24d5cf9
1 changed files with 66 additions and 92 deletions
|
@ -34,6 +34,7 @@
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include "empobj.h"
|
||||||
#include "file.h"
|
#include "file.h"
|
||||||
#include "land.h"
|
#include "land.h"
|
||||||
#include "nat.h"
|
#include "nat.h"
|
||||||
|
@ -43,44 +44,28 @@
|
||||||
#include "sect.h"
|
#include "sect.h"
|
||||||
#include "ship.h"
|
#include "ship.h"
|
||||||
|
|
||||||
static int supply_commod(int, int, int, i_type, int);
|
static int s_commod(struct empobj *, short *, i_type, int, int, int);
|
||||||
static int s_commod(int, int, int, i_type, int, int);
|
|
||||||
static int get_minimum(struct lndstr *, i_type);
|
static int get_minimum(struct lndstr *, i_type);
|
||||||
|
|
||||||
int
|
int
|
||||||
sct_supply(struct sctstr *sp, i_type type, int wanted)
|
sct_supply(struct sctstr *sp, i_type type, int wanted)
|
||||||
{
|
{
|
||||||
if (sp->sct_item[type] < wanted) {
|
return s_commod((struct empobj *)sp, sp->sct_item,
|
||||||
sp->sct_item[type] += supply_commod(sp->sct_own,
|
type, wanted, ITEM_MAX, 1);
|
||||||
sp->sct_x, sp->sct_y, type,
|
|
||||||
wanted - sp->sct_item[type]);
|
|
||||||
putsect(sp);
|
|
||||||
}
|
|
||||||
return sp->sct_item[type] >= wanted;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
shp_supply(struct shpstr *sp, i_type type, int wanted)
|
shp_supply(struct shpstr *sp, i_type type, int wanted)
|
||||||
{
|
{
|
||||||
if (sp->shp_item[type] < wanted) {
|
return s_commod((struct empobj *)sp, sp->shp_item,
|
||||||
sp->shp_item[type] += supply_commod(sp->shp_own,
|
type, wanted, mchr[sp->shp_type].m_item[type], 1);
|
||||||
sp->shp_x, sp->shp_y, type,
|
|
||||||
wanted - sp->shp_item[type]);
|
|
||||||
putship(sp->shp_uid, sp);
|
|
||||||
}
|
|
||||||
return sp->shp_item[type] >= wanted;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
lnd_supply(struct lndstr *lp, i_type type, int wanted)
|
lnd_supply(struct lndstr *lp, i_type type, int wanted)
|
||||||
{
|
{
|
||||||
if (lp->lnd_item[type] < wanted) {
|
return s_commod((struct empobj *)lp, lp->lnd_item,
|
||||||
lp->lnd_item[type] += supply_commod(lp->lnd_own,
|
type, wanted, lchr[lp->lnd_type].l_item[type], 1);
|
||||||
lp->lnd_x, lp->lnd_y, type,
|
|
||||||
wanted - lp->lnd_item[type]);
|
|
||||||
putland(lp->lnd_uid, lp);
|
|
||||||
}
|
|
||||||
return lp->lnd_item[type] >= wanted;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -104,29 +89,6 @@ lnd_supply_all(struct lndstr *lp)
|
||||||
return !fail;
|
return !fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Actually get the commod
|
|
||||||
*/
|
|
||||||
static int
|
|
||||||
supply_commod(int own, int x, int y, i_type type, int total_wanted)
|
|
||||||
{
|
|
||||||
if (total_wanted <= 0)
|
|
||||||
return 0;
|
|
||||||
return s_commod(own, x, y, type, total_wanted, !player->simulation);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Just return the number you COULD get, without doing it
|
|
||||||
*/
|
|
||||||
static int
|
|
||||||
try_supply_commod(int own, int x, int y, i_type type, int total_wanted)
|
|
||||||
{
|
|
||||||
if (total_wanted <= 0)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
return s_commod(own, x, y, type, total_wanted, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Actually get the commod
|
* Actually get the commod
|
||||||
*
|
*
|
||||||
|
@ -140,11 +102,13 @@ try_supply_commod(int own, int x, int y, i_type type, int total_wanted)
|
||||||
* May want to put code to resupply with SAMs here, later --ts
|
* May want to put code to resupply with SAMs here, later --ts
|
||||||
*/
|
*/
|
||||||
static int
|
static int
|
||||||
s_commod(int own, int x, int y, i_type type, int total_wanted,
|
s_commod(struct empobj *sink, short *vec,
|
||||||
int actually_doit)
|
i_type type, int wanted, int limit, int actually_doit)
|
||||||
{
|
{
|
||||||
int wanted = total_wanted;
|
natid own = sink->own;
|
||||||
int gotten = 0, lookrange;
|
coord x = sink->x;
|
||||||
|
coord y = sink->y;
|
||||||
|
int lookrange;
|
||||||
struct sctstr sect, dest;
|
struct sctstr sect, dest;
|
||||||
struct nstr_sect ns;
|
struct nstr_sect ns;
|
||||||
struct nstr_item ni;
|
struct nstr_item ni;
|
||||||
|
@ -160,6 +124,12 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
|
||||||
struct ichrstr *ip;
|
struct ichrstr *ip;
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
|
|
||||||
|
if (wanted > limit)
|
||||||
|
wanted = limit;
|
||||||
|
if (wanted <= vec[type])
|
||||||
|
return 1;
|
||||||
|
wanted -= vec[type];
|
||||||
|
|
||||||
/* try to get it from sector we're in */
|
/* try to get it from sector we're in */
|
||||||
getsect(x, y, &dest);
|
getsect(x, y, &dest);
|
||||||
getsect(x, y, §);
|
getsect(x, y, §);
|
||||||
|
@ -169,17 +139,21 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
|
||||||
etu_per_update));
|
etu_per_update));
|
||||||
if (sect.sct_item[type] - wanted >= minimum) {
|
if (sect.sct_item[type] - wanted >= minimum) {
|
||||||
sect.sct_item[type] -= wanted;
|
sect.sct_item[type] -= wanted;
|
||||||
if (actually_doit)
|
if (actually_doit) {
|
||||||
|
vec[type] += wanted;
|
||||||
putsect(§);
|
putsect(§);
|
||||||
return total_wanted;
|
put_empobj(sink->ef_type, sink->uid, sink);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
} else if (sect.sct_item[type] - minimum > 0) {
|
} else if (sect.sct_item[type] - minimum > 0) {
|
||||||
gotten += sect.sct_item[type] - minimum;
|
|
||||||
wanted -= sect.sct_item[type] - minimum;
|
wanted -= sect.sct_item[type] - minimum;
|
||||||
sect.sct_item[type] = minimum;
|
sect.sct_item[type] = minimum;
|
||||||
if (actually_doit)
|
if (actually_doit) {
|
||||||
|
vec[type] += sect.sct_item[type] - minimum;
|
||||||
putsect(§);
|
putsect(§);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/* look for a headquarters or warehouse */
|
/* look for a headquarters or warehouse */
|
||||||
lookrange = tfact(own, 10.0);
|
lookrange = tfact(own, 10.0);
|
||||||
snxtsct_dist(&ns, x, y, lookrange);
|
snxtsct_dist(&ns, x, y, lookrange);
|
||||||
|
@ -230,14 +204,14 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
|
||||||
if (n > sect.sct_mobil)
|
if (n > sect.sct_mobil)
|
||||||
n = sect.sct_mobil;
|
n = sect.sct_mobil;
|
||||||
sect.sct_mobil -= n;
|
sect.sct_mobil -= n;
|
||||||
|
if (actually_doit) {
|
||||||
if (actually_doit)
|
vec[type] += wanted;
|
||||||
putsect(§);
|
putsect(§);
|
||||||
|
put_empobj(sink->ef_type, sink->uid, sink);
|
||||||
return total_wanted;
|
}
|
||||||
|
return 1;
|
||||||
} else if (can_move > 0) {
|
} else if (can_move > 0) {
|
||||||
int n;
|
int n;
|
||||||
gotten += can_move;
|
|
||||||
wanted -= can_move;
|
wanted -= can_move;
|
||||||
sect.sct_item[type] -= can_move;
|
sect.sct_item[type] -= can_move;
|
||||||
|
|
||||||
|
@ -248,11 +222,12 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
|
||||||
if (n > sect.sct_mobil)
|
if (n > sect.sct_mobil)
|
||||||
n = sect.sct_mobil;
|
n = sect.sct_mobil;
|
||||||
sect.sct_mobil -= n;
|
sect.sct_mobil -= n;
|
||||||
|
if (actually_doit) {
|
||||||
if (actually_doit)
|
vec[type] += can_move;
|
||||||
putsect(§);
|
putsect(§);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* look for an owned ship in a harbor */
|
/* look for an owned ship in a harbor */
|
||||||
snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
|
snxtitem_dist(&ni, EF_SHIP, x, y, lookrange);
|
||||||
|
@ -301,13 +276,14 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
|
||||||
n = sect.sct_mobil;
|
n = sect.sct_mobil;
|
||||||
sect.sct_mobil -= n;
|
sect.sct_mobil -= n;
|
||||||
if (actually_doit) {
|
if (actually_doit) {
|
||||||
|
vec[type] += can_move;
|
||||||
putship(ship.shp_uid, &ship);
|
putship(ship.shp_uid, &ship);
|
||||||
putsect(§);
|
putsect(§);
|
||||||
|
put_empobj(sink->ef_type, sink->uid, sink);
|
||||||
}
|
}
|
||||||
return total_wanted;
|
return 1;
|
||||||
} else if (can_move > 0) {
|
} else if (can_move > 0) {
|
||||||
int n;
|
int n;
|
||||||
gotten += can_move;
|
|
||||||
wanted -= can_move;
|
wanted -= can_move;
|
||||||
ship.shp_item[type] -= can_move;
|
ship.shp_item[type] -= can_move;
|
||||||
|
|
||||||
|
@ -317,8 +293,8 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
|
||||||
if (n > sect.sct_mobil)
|
if (n > sect.sct_mobil)
|
||||||
n = sect.sct_mobil;
|
n = sect.sct_mobil;
|
||||||
sect.sct_mobil -= n;
|
sect.sct_mobil -= n;
|
||||||
|
|
||||||
if (actually_doit) {
|
if (actually_doit) {
|
||||||
|
vec[type] += can_move;
|
||||||
putship(ship.shp_uid, &ship);
|
putship(ship.shp_uid, &ship);
|
||||||
putsect(§);
|
putsect(§);
|
||||||
}
|
}
|
||||||
|
@ -404,33 +380,32 @@ s_commod(int own, int x, int y, i_type type, int total_wanted,
|
||||||
if (can_move >= wanted) {
|
if (can_move >= wanted) {
|
||||||
land.lnd_item[type] -= wanted;
|
land.lnd_item[type] -= wanted;
|
||||||
land.lnd_mobil -= roundavg(wanted * weight * move_cost);
|
land.lnd_mobil -= roundavg(wanted * weight * move_cost);
|
||||||
|
if (actually_doit) {
|
||||||
if (actually_doit)
|
vec[type] += wanted;
|
||||||
putland(land.lnd_uid, &land);
|
putland(land.lnd_uid, &land);
|
||||||
return total_wanted;
|
put_empobj(sink->ef_type, sink->uid, sink);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
} else if (can_move > 0) {
|
} else if (can_move > 0) {
|
||||||
gotten += can_move;
|
|
||||||
wanted -= can_move;
|
wanted -= can_move;
|
||||||
land.lnd_item[type] -= can_move;
|
land.lnd_item[type] -= can_move;
|
||||||
|
|
||||||
land.lnd_mobil -= roundavg(can_move * weight * move_cost);
|
land.lnd_mobil -= roundavg(can_move * weight * move_cost);
|
||||||
|
if (actually_doit) {
|
||||||
if (actually_doit)
|
vec[type] += can_move;
|
||||||
putland(land.lnd_uid, &land);
|
putland(land.lnd_uid, &land);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* We've done the best we could */
|
|
||||||
/* return the number gotten */
|
|
||||||
return gotten;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (actually_doit)
|
||||||
|
put_empobj(sink->ef_type, sink->uid, sink);
|
||||||
|
return wanted <= vec[type];
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We want to get enough shells to fire once,
|
* We want to get enough shells to fire once,
|
||||||
* one update's worth of food.
|
* one update's worth of food.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int
|
static int
|
||||||
get_minimum(struct lndstr *lp, i_type type)
|
get_minimum(struct lndstr *lp, i_type type)
|
||||||
{
|
{
|
||||||
|
@ -462,38 +437,37 @@ get_minimum(struct lndstr *lp, i_type type)
|
||||||
int
|
int
|
||||||
lnd_could_be_supplied(struct lndstr *lp)
|
lnd_could_be_supplied(struct lndstr *lp)
|
||||||
{
|
{
|
||||||
int shells_needed, shells, keepshells;
|
int res, food, food_needed, shells_needed, shells;
|
||||||
int food, food_needed, keepfood;
|
|
||||||
|
|
||||||
if (!opt_NOFOOD) {
|
if (!opt_NOFOOD) {
|
||||||
food_needed = get_minimum(lp, I_FOOD);
|
food_needed = get_minimum(lp, I_FOOD);
|
||||||
food = keepfood = lp->lnd_item[I_FOOD];
|
food = lp->lnd_item[I_FOOD];
|
||||||
if (food < food_needed) {
|
if (food < food_needed) {
|
||||||
lp->lnd_item[I_FOOD] = 0;
|
lp->lnd_item[I_FOOD] = 0;
|
||||||
putland(lp->lnd_uid, lp);
|
putland(lp->lnd_uid, lp);
|
||||||
food += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
|
res = s_commod((struct empobj *)lp, lp->lnd_item,
|
||||||
I_FOOD, (food_needed - food));
|
I_FOOD, food_needed,
|
||||||
lp->lnd_item[I_FOOD] = keepfood;
|
lchr[lp->lnd_type].l_item[I_FOOD], 0);
|
||||||
|
lp->lnd_item[I_FOOD] = food;
|
||||||
putland(lp->lnd_uid, lp);
|
putland(lp->lnd_uid, lp);
|
||||||
}
|
if (!res)
|
||||||
if (food < food_needed)
|
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
shells_needed = lchr[lp->lnd_type].l_ammo;
|
shells_needed = lchr[lp->lnd_type].l_ammo;
|
||||||
shells = keepshells = lp->lnd_item[I_SHELL];
|
shells = lp->lnd_item[I_SHELL];
|
||||||
if (shells < shells_needed) {
|
if (shells < shells_needed) {
|
||||||
lp->lnd_item[I_SHELL] = 0;
|
lp->lnd_item[I_SHELL] = 0;
|
||||||
putland(lp->lnd_uid, lp);
|
putland(lp->lnd_uid, lp);
|
||||||
shells += try_supply_commod(lp->lnd_own, lp->lnd_x, lp->lnd_y,
|
res = s_commod((struct empobj *)lp, lp->lnd_item,
|
||||||
I_SHELL, (shells_needed - shells));
|
I_SHELL, shells_needed,
|
||||||
lp->lnd_item[I_SHELL] = keepshells;
|
lchr[lp->lnd_type].l_item[I_SHELL], 0);
|
||||||
|
lp->lnd_item[I_SHELL] = shells;
|
||||||
putland(lp->lnd_uid, lp);
|
putland(lp->lnd_uid, lp);
|
||||||
}
|
if (!res)
|
||||||
|
|
||||||
if (shells < shells_needed)
|
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue