(get_materials): Redesign.

(shiprepair, planerepair, landrepair): Use it.  Behavioral change:
ship repairs outside harbors and plane repairs by a carrier can use
fewer materials.  Before, such repairs consumed each required
commodity as far as available.  Now, they consume the same fraction of
the real cost of each commodity, i.e. commodity use is limited by the
most scarce commodity.  Neither old nor new behavior make much sense,
but the new code is simpler.
This commit is contained in:
Markus Armbruster 2007-01-10 07:15:45 +00:00
parent 63bdc89835
commit ac9cdf5bb9
5 changed files with 51 additions and 127 deletions

View file

@ -42,7 +42,7 @@
void fill_update_array(int *bp, struct sctstr *sp);
int gt_bg_nmbr(int *bp, struct sctstr *sp, i_type comm);
void pt_bg_nmbr(int *bp, struct sctstr *sp, i_type comm, int amount);
void get_materials(struct sctstr *sp, int *bp, int *mvec, int check);
int get_materials(struct sctstr *, int *, int *, int);
extern long money[MAXNOC];
extern long pops[MAXNOC];

View file

@ -213,16 +213,13 @@ upd_land(struct lndstr *lp, int etus,
}
}
/*ARGSUSED*/
static void
landrepair(struct lndstr *land, struct natstr *np, int *bp, int etus)
{
int delta;
struct sctstr *sp;
struct lchrstr *lp;
float leftp, buildp;
int left, build;
int mil_needed, lcm_needed, hcm_needed, gun_needed, shell_needed;
int build;
int avail;
int w_p_eff;
int mult;
@ -255,53 +252,13 @@ landrepair(struct lndstr *land, struct natstr *np, int *bp, int etus)
return;
if (delta > (int)((float)etus * land_grow_scale))
delta = (int)((float)etus * land_grow_scale);
/* delta is the max amount we can grow */
left = 100 - land->lnd_effic;
if (left > delta)
left = delta;
leftp = left / 100.0;
if (delta > 100 - land->lnd_effic)
delta = 100 - land->lnd_effic;
memset(mvec, 0, sizeof(mvec));
mvec[I_LCM] = lcm_needed = ldround(lp->l_lcm * leftp, 1);
mvec[I_HCM] = hcm_needed = ldround(lp->l_hcm * leftp, 1);
/*
mvec[I_GUN] = gun_needed = ldround(lp->l_gun * leftp, 1);
mvec[I_MILIT] = mil_needed = ldround(lp->l_mil * leftp, 1);
mvec[I_SHELL] = shell_needed = ldround(lp->l_shell *leftp, 1);
*/
mvec[I_GUN] = gun_needed = 0;
mvec[I_MILIT] = mil_needed = 0;
mvec[I_SHELL] = shell_needed = 0;
get_materials(sp, bp, mvec, 0);
buildp = leftp;
if (mvec[I_MILIT] < mil_needed)
buildp = MIN(buildp, (float)mvec[I_MILIT] / (float)lp->l_mil);
if (mvec[I_LCM] < lcm_needed)
buildp = MIN(buildp, (float)mvec[I_LCM] / (float)lp->l_lcm);
if (mvec[I_HCM] < hcm_needed)
buildp = MIN(buildp, (float)mvec[I_HCM] / (float)lp->l_hcm);
if (mvec[I_GUN] < gun_needed)
buildp = MIN(buildp, (float)mvec[I_GUN] / (float)lp->l_gun);
if (mvec[I_SHELL] < shell_needed)
buildp = MIN(buildp, (float)mvec[I_SHELL] / (float)lp->l_shell);
build = ldround(buildp * 100.0, 1);
memset(mvec, 0, sizeof(mvec));
mvec[I_LCM] = roundavg(lp->l_lcm * buildp);
mvec[I_HCM] = roundavg(lp->l_hcm * buildp);
/*
mvec[I_GUN] = roundavg(lp->l_gun * buildp);
mvec[I_MILIT] = roundavg(lp->l_mil * buildp);
mvec[I_SHELL] = roundavg(lp->l_shell *buildp);
*/
mvec[I_GUN] = 0;
mvec[I_MILIT] = 0;
mvec[I_SHELL] = 0;
get_materials(sp, bp, mvec, 1);
mvec[I_LCM] = lp->l_lcm;
mvec[I_HCM] = lp->l_hcm;
build = get_materials(sp, bp, mvec, delta);
if ((sp->sct_type != SCT_HEADQ) && (sp->sct_type != SCT_FORTR))
build /= 3;

View file

@ -29,6 +29,7 @@
*
* Known contributors to this file:
* Ville Virrankoski, 1996
* Markus Armbruster, 2007
*/
#include <config.h>
@ -37,30 +38,39 @@
#include "player.h"
#include "update.h"
void
get_materials(struct sctstr *sp, int *bp, int *mvec, int check)
/* only check if found=0, remove them=1 */
/*
* Get build materials from sector SP.
* BP is the sector's build pointer.
* MVEC[] defines the materials needed to build 100%.
* PCT is the percentage to build.
* Adjust build percentage downwards so that available materials
* suffice. Remove the materials.
* Return adjusted build percentage.
*/
int
get_materials(struct sctstr *sp, int *bp, int *mvec, int pct)
{
i_type i;
int still_left;
int i, amt;
for (i = I_NONE + 1; i <= I_MAX; i++) {
if (mvec[i] == 0)
continue;
amt = gt_bg_nmbr(bp, sp, i);
if (amt * 100 < mvec[i] * pct)
pct = amt * 100 / mvec[i];
}
if (check) {
still_left = gt_bg_nmbr(bp, sp, i);
if ((still_left - mvec[i]) < 0)
still_left = 0;
else
still_left -= mvec[i];
pt_bg_nmbr(bp, sp, i, still_left);
for (i = I_NONE + 1; i <= I_MAX; i++) {
if (mvec[i] == 0)
continue;
amt = gt_bg_nmbr(bp, sp, i);
amt -= roundavg(mvec[i] * pct / 100.0);
if (CANT_HAPPEN(amt < 0))
amt = 0;
pt_bg_nmbr(bp, sp, i, amt);
if (!player->simulation)
sp->sct_item[i] = still_left;
sp->sct_item[i] = amt;
}
} else {
still_left = gt_bg_nmbr(bp, sp, i);
mvec[i] = MIN(mvec[i], still_left);
}
}
return pct;
}

View file

@ -127,9 +127,7 @@ upd_plane(struct plnstr *pp, int etus,
static void
planerepair(struct plnstr *pp, struct natstr *np, int *bp, int etus)
{
float leftp, buildp;
int left, build;
int mil_needed, lcm_needed, hcm_needed;
int build;
int mvec[I_MAX + 1];
struct shpstr *carrier;
struct plchrstr *pcp = &plchr[(int)pp->pln_type];
@ -180,34 +178,14 @@ planerepair(struct plnstr *pp, struct natstr *np, int *bp, int etus)
return;
if (delta > (int)((float)etus * plane_grow_scale))
delta = (int)((float)etus * plane_grow_scale);
if (delta > 100 - pp->pln_effic)
delta = 100 - pp->pln_effic;
/* delta is the max amount we can grow */
left = 100 - pp->pln_effic;
if (left > delta)
left = delta;
leftp = left / 100.0;
memset(mvec, 0, sizeof(mvec));
mvec[I_MILIT] = mil_needed = ldround(pcp->pl_crew * leftp, 1);
mvec[I_LCM] = lcm_needed = ldround(pcp->pl_lcm * leftp, 1);
mvec[I_HCM] = hcm_needed = ldround(pcp->pl_hcm * leftp, 1);
get_materials(sp, bp, mvec, 0);
buildp = leftp;
if (mvec[I_MILIT] < mil_needed)
buildp = MIN(buildp, (float)mvec[I_MILIT] / (float)pcp->pl_crew);
if (mvec[I_LCM] < lcm_needed)
buildp = MIN(buildp, (float)mvec[I_LCM] / (float)pcp->pl_lcm);
if (mvec[I_HCM] < hcm_needed)
buildp = MIN(buildp, (float)mvec[I_HCM] / (float)pcp->pl_hcm);
build = ldround(buildp * 100.0, 1);
memset(mvec, 0, sizeof(mvec));
mvec[I_MILIT] = roundavg(pcp->pl_crew * buildp);
mvec[I_LCM] = roundavg(pcp->pl_lcm * buildp);
mvec[I_HCM] = roundavg(pcp->pl_hcm * buildp);
get_materials(sp, bp, mvec, 1);
mvec[I_MILIT] = pcp->pl_crew;
mvec[I_LCM] = pcp->pl_lcm;
mvec[I_HCM] = pcp->pl_hcm;
build = get_materials(sp, bp, mvec, delta);
if (carrier)
build = delta;

View file

@ -271,9 +271,7 @@ shiprepair(struct shpstr *ship, struct natstr *np, int *bp, int etus)
int delta;
struct sctstr *sp;
struct mchrstr *mp;
float leftp, buildp;
int left, build;
int lcm_needed, hcm_needed;
int build;
int wf;
int avail;
int w_p_eff;
@ -322,41 +320,22 @@ shiprepair(struct shpstr *ship, struct natstr *np, int *bp, int etus)
return;
}
left = 100 - ship->shp_effic;
delta = roundavg((double)avail / w_p_eff);
if (delta <= 0)
return;
if (delta > (int)((float)etus * ship_grow_scale))
delta = (int)((float)etus * ship_grow_scale);
if (delta > left)
delta = left;
if (delta > 100 - ship->shp_effic)
delta = 100 - ship->shp_effic;
/* delta is the max amount we can grow */
left = 100 - ship->shp_effic;
if (left > delta)
left = delta;
leftp = left / 100.0;
memset(mvec, 0, sizeof(mvec));
mvec[I_LCM] = lcm_needed = ldround(mp->m_lcm * leftp, 1);
mvec[I_HCM] = hcm_needed = ldround(mp->m_hcm * leftp, 1);
get_materials(sp, bp, mvec, 0);
buildp = leftp;
if (mvec[I_LCM] < lcm_needed)
buildp = MIN(buildp, (float)mvec[I_LCM] / (float)mp->m_lcm);
if (mvec[I_HCM] < hcm_needed)
buildp = MIN(buildp, (float)mvec[I_HCM] / (float)mp->m_hcm);
build = ldround(buildp * 100.0, 1);
memset(mvec, 0, sizeof(mvec));
mvec[I_LCM] = roundavg(mp->m_lcm * buildp);
mvec[I_HCM] = roundavg(mp->m_hcm * buildp);
get_materials(sp, bp, mvec, 1);
mvec[I_LCM] = mp->m_lcm;
mvec[I_HCM] = mp->m_hcm;
build = get_materials(sp, bp, mvec, delta);
if (sp->sct_type != SCT_HARBR)
build = delta;
wf -= build * w_p_eff;
if (wf < 0) {
/*