update: Use a scratch sctstr for unit repair simulation
If player->simulation, shiprepair(), planerepair(), landrepair() must use the bp map, and must not change game state. Copy the sector to a scratch buffer, update it from the bp map, work on the sector normally, then write back to the bp map. This is simpler and safer. Since get_materials() loses its connection to the bp map, move its declaration out of budg.h. While there, drop an ancient debugging logerror() from landrepair(). Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
This commit is contained in:
parent
66edd2baca
commit
199b1498c8
7 changed files with 56 additions and 52 deletions
|
@ -48,8 +48,7 @@ void bp_put_item(struct bp *, struct sctstr *, i_type, int);
|
||||||
void bp_put_items(struct bp *, struct sctstr *);
|
void bp_put_items(struct bp *, struct sctstr *);
|
||||||
int bp_get_avail(struct bp *, struct sctstr *);
|
int bp_get_avail(struct bp *, struct sctstr *);
|
||||||
void bp_put_avail(struct bp *, struct sctstr *, int);
|
void bp_put_avail(struct bp *, struct sctstr *, int);
|
||||||
|
void bp_to_sect(struct bp *, struct sctstr *);
|
||||||
int get_materials(struct sctstr *, struct bp *, short[], int);
|
|
||||||
|
|
||||||
extern int money[MAXNOC];
|
extern int money[MAXNOC];
|
||||||
extern int pops[MAXNOC];
|
extern int pops[MAXNOC];
|
||||||
|
|
|
@ -672,7 +672,7 @@ extern int prod_land(int, int, struct bp *, int);
|
||||||
/* main.c */
|
/* main.c */
|
||||||
/* in server.h */
|
/* in server.h */
|
||||||
/* material.c */
|
/* material.c */
|
||||||
/* in budg.h */
|
extern int get_materials(struct sctstr *, short[], int);
|
||||||
/* mobility.c */
|
/* mobility.c */
|
||||||
extern void mob_sect(void);
|
extern void mob_sect(void);
|
||||||
extern void mob_ship(void);
|
extern void mob_ship(void);
|
||||||
|
|
|
@ -132,6 +132,21 @@ bp_set_from_sect(struct bp *bp, struct sctstr *sp)
|
||||||
bp_put_avail(bp, sp, sp->sct_avail);
|
bp_put_avail(bp, sp, sp->sct_avail);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Copy the values tracked in @bp for sector @sp back to @sp. */
|
||||||
|
void
|
||||||
|
bp_to_sect(struct bp *bp, struct sctstr *sp)
|
||||||
|
{
|
||||||
|
i_type i;
|
||||||
|
enum bp_item_idx idx;
|
||||||
|
|
||||||
|
for (i = I_NONE + 1; i <= I_MAX; i++) {
|
||||||
|
idx = bud_key[i];
|
||||||
|
if (idx >= 0)
|
||||||
|
sp->sct_item[i] = bp[sp->sct_uid].bp_item[idx];
|
||||||
|
}
|
||||||
|
sp->sct_avail = bp[sp->sct_uid].bp_avail;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Return a new bp map.
|
* Return a new bp map.
|
||||||
* Caller should pass it to free() when done with it.
|
* Caller should pass it to free() when done with it.
|
||||||
|
|
|
@ -196,7 +196,7 @@ landrepair(struct lndstr *land, struct natstr *np, struct bp *bp, int etus)
|
||||||
{
|
{
|
||||||
struct lchrstr *lp = &lchr[(int)land->lnd_type];
|
struct lchrstr *lp = &lchr[(int)land->lnd_type];
|
||||||
int delta;
|
int delta;
|
||||||
struct sctstr *sp;
|
struct sctstr *sp, scratch_sect;
|
||||||
int build;
|
int build;
|
||||||
int avail;
|
int avail;
|
||||||
int mult;
|
int mult;
|
||||||
|
@ -211,14 +211,17 @@ landrepair(struct lndstr *land, struct natstr *np, struct bp *bp, int etus)
|
||||||
if (relations_with(sp->sct_own, land->lnd_own) != ALLIED)
|
if (relations_with(sp->sct_own, land->lnd_own) != ALLIED)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (player->simulation) {
|
||||||
|
scratch_sect = *sp;
|
||||||
|
bp_to_sect(bp, &scratch_sect);
|
||||||
|
sp = &scratch_sect;
|
||||||
|
}
|
||||||
|
|
||||||
mult = 1;
|
mult = 1;
|
||||||
if (np->nat_level[NAT_TLEV] < land->lnd_tech * 0.85)
|
if (np->nat_level[NAT_TLEV] < land->lnd_tech * 0.85)
|
||||||
mult = 2;
|
mult = 2;
|
||||||
|
|
||||||
if (!player->simulation)
|
avail = sp->sct_avail * 100;
|
||||||
avail = sp->sct_avail * 100;
|
|
||||||
else
|
|
||||||
avail = bp_get_avail(bp, sp) * 100;
|
|
||||||
|
|
||||||
delta = roundavg((double)avail / lp->l_bwork);
|
delta = roundavg((double)avail / lp->l_bwork);
|
||||||
if (delta <= 0)
|
if (delta <= 0)
|
||||||
|
@ -228,7 +231,7 @@ landrepair(struct lndstr *land, struct natstr *np, struct bp *bp, int etus)
|
||||||
if (delta > 100 - land->lnd_effic)
|
if (delta > 100 - land->lnd_effic)
|
||||||
delta = 100 - land->lnd_effic;
|
delta = 100 - land->lnd_effic;
|
||||||
|
|
||||||
build = get_materials(sp, bp, lp->l_mat, delta);
|
build = get_materials(sp, lp->l_mat, delta);
|
||||||
|
|
||||||
if ((sp->sct_type != SCT_HEADQ) && (sp->sct_type != SCT_FORTR))
|
if ((sp->sct_type != SCT_HEADQ) && (sp->sct_type != SCT_FORTR))
|
||||||
build /= 3;
|
build /= 3;
|
||||||
|
@ -236,13 +239,9 @@ landrepair(struct lndstr *land, struct natstr *np, struct bp *bp, int etus)
|
||||||
avail -= build * lp->l_bwork;
|
avail -= build * lp->l_bwork;
|
||||||
if (avail < 0)
|
if (avail < 0)
|
||||||
avail = 0;
|
avail = 0;
|
||||||
if (!player->simulation)
|
sp->sct_avail = avail / 100;
|
||||||
sp->sct_avail = avail / 100;
|
|
||||||
else
|
|
||||||
bp_put_avail(bp, sp, avail / 100);
|
|
||||||
|
|
||||||
if (build < 0)
|
bp_set_from_sect(bp, sp);
|
||||||
logerror("land unit %d building %d ! \n", land->lnd_uid, build);
|
|
||||||
np->nat_money -= mult * lp->l_cost * build / 100.0;
|
np->nat_money -= mult * lp->l_cost * build / 100.0;
|
||||||
if (!player->simulation) {
|
if (!player->simulation) {
|
||||||
land->lnd_effic += (signed char)build;
|
land->lnd_effic += (signed char)build;
|
||||||
|
|
|
@ -33,14 +33,11 @@
|
||||||
|
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
|
||||||
#include "budg.h"
|
|
||||||
#include "chance.h"
|
#include "chance.h"
|
||||||
#include "player.h"
|
|
||||||
#include "update.h"
|
#include "update.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Get build materials from sector @sp.
|
* Get build materials from sector @sp.
|
||||||
* @bp is the sector's build pointer.
|
|
||||||
* Array @mvec[ITEM_MAX+1] defines the materials needed to build 100%.
|
* Array @mvec[ITEM_MAX+1] defines the materials needed to build 100%.
|
||||||
* @pct is the percentage to build.
|
* @pct is the percentage to build.
|
||||||
* Adjust build percentage downwards so that available materials
|
* Adjust build percentage downwards so that available materials
|
||||||
|
@ -48,14 +45,14 @@
|
||||||
* Return adjusted build percentage.
|
* Return adjusted build percentage.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
get_materials(struct sctstr *sp, struct bp *bp, short mvec[], int pct)
|
get_materials(struct sctstr *sp, short mvec[], int pct)
|
||||||
{
|
{
|
||||||
int i, amt;
|
int i, amt;
|
||||||
|
|
||||||
for (i = I_NONE + 1; i <= I_MAX; i++) {
|
for (i = I_NONE + 1; i <= I_MAX; i++) {
|
||||||
if (mvec[i] == 0)
|
if (mvec[i] == 0)
|
||||||
continue;
|
continue;
|
||||||
amt = bp_get_item(bp, sp, i);
|
amt = sp->sct_item[i];
|
||||||
if (amt * 100 < mvec[i] * pct)
|
if (amt * 100 < mvec[i] * pct)
|
||||||
pct = amt * 100 / mvec[i];
|
pct = amt * 100 / mvec[i];
|
||||||
}
|
}
|
||||||
|
@ -63,13 +60,11 @@ get_materials(struct sctstr *sp, struct bp *bp, short mvec[], int pct)
|
||||||
for (i = I_NONE + 1; i <= I_MAX; i++) {
|
for (i = I_NONE + 1; i <= I_MAX; i++) {
|
||||||
if (mvec[i] == 0)
|
if (mvec[i] == 0)
|
||||||
continue;
|
continue;
|
||||||
amt = bp_get_item(bp, sp, i);
|
amt = sp->sct_item[i];
|
||||||
amt -= roundavg(mvec[i] * pct / 100.0);
|
amt -= roundavg(mvec[i] * pct / 100.0);
|
||||||
if (CANT_HAPPEN(amt < 0))
|
if (CANT_HAPPEN(amt < 0))
|
||||||
amt = 0;
|
amt = 0;
|
||||||
bp_put_item(bp, sp, i, amt);
|
sp->sct_item[i] = amt;
|
||||||
if (!player->simulation)
|
|
||||||
sp->sct_item[i] = amt;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return pct;
|
return pct;
|
||||||
|
|
|
@ -126,7 +126,7 @@ planerepair(struct plnstr *pp, struct natstr *np, struct bp *bp, int etus)
|
||||||
struct plchrstr *pcp = &plchr[(int)pp->pln_type];
|
struct plchrstr *pcp = &plchr[(int)pp->pln_type];
|
||||||
int build;
|
int build;
|
||||||
struct shpstr *carrier;
|
struct shpstr *carrier;
|
||||||
struct sctstr *sp;
|
struct sctstr *sp, scratch_sect;
|
||||||
int delta;
|
int delta;
|
||||||
int mult;
|
int mult;
|
||||||
int avail;
|
int avail;
|
||||||
|
@ -155,14 +155,17 @@ planerepair(struct plnstr *pp, struct natstr *np, struct bp *bp, int etus)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (player->simulation) {
|
||||||
|
scratch_sect = *sp;
|
||||||
|
bp_to_sect(bp, &scratch_sect);
|
||||||
|
sp = &scratch_sect;
|
||||||
|
}
|
||||||
|
|
||||||
mult = 1;
|
mult = 1;
|
||||||
if (np->nat_level[NAT_TLEV] < pp->pln_tech * 0.85)
|
if (np->nat_level[NAT_TLEV] < pp->pln_tech * 0.85)
|
||||||
mult = 2;
|
mult = 2;
|
||||||
|
|
||||||
if (!player->simulation)
|
avail = sp->sct_avail * 100;
|
||||||
avail = sp->sct_avail * 100;
|
|
||||||
else
|
|
||||||
avail = bp_get_avail(bp, sp) * 100;
|
|
||||||
if (carrier)
|
if (carrier)
|
||||||
avail += etus * carrier->shp_item[I_MILIT] / 2;
|
avail += etus * carrier->shp_item[I_MILIT] / 2;
|
||||||
|
|
||||||
|
@ -174,7 +177,7 @@ planerepair(struct plnstr *pp, struct natstr *np, struct bp *bp, int etus)
|
||||||
if (delta > 100 - pp->pln_effic)
|
if (delta > 100 - pp->pln_effic)
|
||||||
delta = 100 - pp->pln_effic;
|
delta = 100 - pp->pln_effic;
|
||||||
|
|
||||||
build = get_materials(sp, bp, pcp->pl_mat, delta);
|
build = get_materials(sp, pcp->pl_mat, delta);
|
||||||
|
|
||||||
if (carrier)
|
if (carrier)
|
||||||
build = delta;
|
build = delta;
|
||||||
|
@ -184,17 +187,10 @@ planerepair(struct plnstr *pp, struct natstr *np, struct bp *bp, int etus)
|
||||||
* I didn't use roundavg here, because I want to
|
* I didn't use roundavg here, because I want to
|
||||||
* penalize the player with a large number of planes.
|
* penalize the player with a large number of planes.
|
||||||
*/
|
*/
|
||||||
if (!player->simulation)
|
avail = (sp->sct_avail * 100 - used) / 100;
|
||||||
avail = (sp->sct_avail * 100 - used) / 100;
|
|
||||||
else
|
|
||||||
avail = (bp_get_avail(bp, sp) * 100 - used) / 100;
|
|
||||||
|
|
||||||
if (avail < 0)
|
if (avail < 0)
|
||||||
avail = 0;
|
avail = 0;
|
||||||
if (!player->simulation)
|
sp->sct_avail = avail;
|
||||||
sp->sct_avail = avail;
|
|
||||||
else
|
|
||||||
bp_put_avail(bp, sp, avail);
|
|
||||||
|
|
||||||
if (sp->sct_type != SCT_AIRPT)
|
if (sp->sct_type != SCT_AIRPT)
|
||||||
build /= 3;
|
build /= 3;
|
||||||
|
@ -203,6 +199,7 @@ planerepair(struct plnstr *pp, struct natstr *np, struct bp *bp, int etus)
|
||||||
build = 80 - pp->pln_effic;
|
build = 80 - pp->pln_effic;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bp_set_from_sect(bp, sp);
|
||||||
np->nat_money -= mult * build * pcp->pl_cost / 100.0;
|
np->nat_money -= mult * build * pcp->pl_cost / 100.0;
|
||||||
|
|
||||||
if (!player->simulation)
|
if (!player->simulation)
|
||||||
|
|
|
@ -243,7 +243,7 @@ shiprepair(struct shpstr *ship, struct natstr *np, struct bp *bp, int etus)
|
||||||
{
|
{
|
||||||
struct mchrstr *mp = &mchr[(int)ship->shp_type];
|
struct mchrstr *mp = &mchr[(int)ship->shp_type];
|
||||||
int delta;
|
int delta;
|
||||||
struct sctstr *sp;
|
struct sctstr *sp, scratch_sect;
|
||||||
int build;
|
int build;
|
||||||
int wf;
|
int wf;
|
||||||
int avail;
|
int avail;
|
||||||
|
@ -260,6 +260,12 @@ shiprepair(struct shpstr *ship, struct natstr *np, struct bp *bp, int etus)
|
||||||
&& relations_with(sp->sct_own, ship->shp_own) < FRIENDLY)
|
&& relations_with(sp->sct_own, ship->shp_own) < FRIENDLY)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (player->simulation) {
|
||||||
|
scratch_sect = *sp;
|
||||||
|
bp_to_sect(bp, &scratch_sect);
|
||||||
|
sp = &scratch_sect;
|
||||||
|
}
|
||||||
|
|
||||||
mult = 1;
|
mult = 1;
|
||||||
if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
|
if (np->nat_level[NAT_TLEV] < ship->shp_tech * 0.85)
|
||||||
mult = 2;
|
mult = 2;
|
||||||
|
@ -273,10 +279,8 @@ shiprepair(struct shpstr *ship, struct natstr *np, struct bp *bp, int etus)
|
||||||
if (sp->sct_type != SCT_HARBR) {
|
if (sp->sct_type != SCT_HARBR) {
|
||||||
wf /= 3;
|
wf /= 3;
|
||||||
avail = wf;
|
avail = wf;
|
||||||
} else if (!player->simulation)
|
} else
|
||||||
avail = wf + sp->sct_avail * 100;
|
avail = wf + sp->sct_avail * 100;
|
||||||
else
|
|
||||||
avail = wf + bp_get_avail(bp, sp) * 100;
|
|
||||||
|
|
||||||
delta = roundavg((double)avail / mp->m_bwork);
|
delta = roundavg((double)avail / mp->m_bwork);
|
||||||
if (delta <= 0)
|
if (delta <= 0)
|
||||||
|
@ -286,7 +290,7 @@ shiprepair(struct shpstr *ship, struct natstr *np, struct bp *bp, int etus)
|
||||||
if (delta > 100 - ship->shp_effic)
|
if (delta > 100 - ship->shp_effic)
|
||||||
delta = 100 - ship->shp_effic;
|
delta = 100 - ship->shp_effic;
|
||||||
|
|
||||||
build = get_materials(sp, bp, mp->m_mat, delta);
|
build = get_materials(sp, mp->m_mat, delta);
|
||||||
|
|
||||||
if (sp->sct_type != SCT_HARBR)
|
if (sp->sct_type != SCT_HARBR)
|
||||||
build = delta;
|
build = delta;
|
||||||
|
@ -297,16 +301,10 @@ shiprepair(struct shpstr *ship, struct natstr *np, struct bp *bp, int etus)
|
||||||
* I didn't use roundavg here, because I want to penalize
|
* I didn't use roundavg here, because I want to penalize
|
||||||
* the player with a large number of ships.
|
* the player with a large number of ships.
|
||||||
*/
|
*/
|
||||||
if (!player->simulation)
|
avail = (sp->sct_avail * 100 + wf) / 100;
|
||||||
avail = (sp->sct_avail * 100 + wf) / 100;
|
|
||||||
else
|
|
||||||
avail = (bp_get_avail(bp, sp) * 100 + wf) / 100;
|
|
||||||
if (avail < 0)
|
if (avail < 0)
|
||||||
avail = 0;
|
avail = 0;
|
||||||
if (!player->simulation)
|
sp->sct_avail = avail;
|
||||||
sp->sct_avail = avail;
|
|
||||||
else
|
|
||||||
bp_put_avail(bp, sp, avail);
|
|
||||||
}
|
}
|
||||||
if (sp->sct_type != SCT_HARBR)
|
if (sp->sct_type != SCT_HARBR)
|
||||||
if ((build + ship->shp_effic) > 80) {
|
if ((build + ship->shp_effic) > 80) {
|
||||||
|
@ -315,6 +313,7 @@ shiprepair(struct shpstr *ship, struct natstr *np, struct bp *bp, int etus)
|
||||||
build = 0;
|
build = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bp_set_from_sect(bp, sp);
|
||||||
np->nat_money -= mult * mp->m_cost * build / 100.0;
|
np->nat_money -= mult * mp->m_cost * build / 100.0;
|
||||||
if (!player->simulation)
|
if (!player->simulation)
|
||||||
ship->shp_effic += (signed char)build;
|
ship->shp_effic += (signed char)build;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue