Separate calbacks for systems and planets to be called before and after recursing for their respective planets and moons.

Functions for tallying of resources.



git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@3079 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
Meep-Eep
2008-10-04 13:26:53 +00:00
parent c5c8445bc4
commit 2dd47ba2e8
2 changed files with 172 additions and 12 deletions
+154 -9
View File
@@ -50,6 +50,16 @@ static void dumpPlanetCallback (const PLANET_DESC *planet, void *arg);
static void dumpMoonCallback (const PLANET_DESC *moon, void *arg); static void dumpMoonCallback (const PLANET_DESC *moon, void *arg);
static void dumpWorld (FILE *out, const PLANET_DESC *world); static void dumpWorld (FILE *out, const PLANET_DESC *world);
typedef struct TallyResourcesArg TallyResourcesArg;
static void tallySystemPreCallback (const STAR_DESC *star, const
SOLARSYS_STATE *system, void *arg);
static void tallySystemPostCallback (const STAR_DESC *star, const
SOLARSYS_STATE *system, void *arg);
static void tallyPlanetCallback (const PLANET_DESC *planet, void *arg);
static void tallyMoonCallback (const PLANET_DESC *moon, void *arg);
static void tallyResourcesWorld (TallyResourcesArg *arg,
const PLANET_DESC *world);
static void dumpPlanetTypeCallback (int index, const PlanetFrame *planet, static void dumpPlanetTypeCallback (int index, const PlanetFrame *planet,
void *arg); void *arg);
@@ -102,6 +112,10 @@ debugKeyPressed (void)
// This will cause dumpUniverseToFile to be called from the // This will cause dumpUniverseToFile to be called from the
// main loop. Calling it from here would give threading // main loop. Calling it from here would give threading
// problems. // problems.
// debugHook = tallyResourcesToFile;
// This will cause tallyResourcesToFile to be called from the
// main loop. Calling it from here would give threading
// problems.
// Interactive: // Interactive:
// uio_debugInteractive(stdin, stdout, stderr); // uio_debugInteractive(stdin, stdout, stderr);
@@ -579,8 +593,10 @@ UniverseRecurse (UniverseRecurseArg *universeRecurseArg)
BOOLEAN clockRunning; BOOLEAN clockRunning;
ACTIVITY savedActivity; ACTIVITY savedActivity;
if (universeRecurseArg->systemFunc == NULL if (universeRecurseArg->systemFuncPre == NULL
&& universeRecurseArg->planetFunc == NULL && universeRecurseArg->systemFuncPost == NULL
&& universeRecurseArg->planetFuncPre == NULL
&& universeRecurseArg->planetFuncPost == NULL
&& universeRecurseArg->moonFunc == NULL) && universeRecurseArg->moonFunc == NULL)
return; return;
@@ -624,19 +640,26 @@ starRecurse (STAR_DESC *star, void *arg)
pSolarSysState = &SolarSysState; pSolarSysState = &SolarSysState;
(*pSolarSysState->GenFunc) (GENERATE_PLANETS); (*pSolarSysState->GenFunc) (GENERATE_PLANETS);
if (universeRecurseArg->systemFunc != NULL) if (universeRecurseArg->systemFuncPre != NULL)
{ {
(*universeRecurseArg->systemFunc) ( (*universeRecurseArg->systemFuncPre) (
star, &SolarSysState, universeRecurseArg->arg); star, &SolarSysState, universeRecurseArg->arg);
} }
if (universeRecurseArg->planetFunc != NULL if (universeRecurseArg->planetFuncPre != NULL
|| universeRecurseArg->planetFuncPost != NULL
|| universeRecurseArg->moonFunc != NULL) || universeRecurseArg->moonFunc != NULL)
{ {
forAllPlanets (star, &SolarSysState, planetRecurse, forAllPlanets (star, &SolarSysState, planetRecurse,
(void *) universeRecurseArg); (void *) universeRecurseArg);
} }
if (universeRecurseArg->systemFuncPost != NULL)
{
(*universeRecurseArg->systemFuncPost) (
star, &SolarSysState, universeRecurseArg->arg);
}
pSolarSysState = oldPSolarSysState; pSolarSysState = oldPSolarSysState;
CurStarDescPtr = oldStarDescPtr; CurStarDescPtr = oldStarDescPtr;
TFB_SeedRandom (oldSeed); TFB_SeedRandom (oldSeed);
@@ -654,7 +677,7 @@ planetRecurse (STAR_DESC *star, SOLARSYS_STATE *system,
system->pBaseDesc = planet; system->pBaseDesc = planet;
planet->pPrevDesc = &system->SunDesc[0]; planet->pPrevDesc = &system->SunDesc[0];
if (universeRecurseArg->planetFunc != NULL) if (universeRecurseArg->planetFuncPre != NULL)
{ {
system->pOrbitalDesc = planet; system->pOrbitalDesc = planet;
DoPlanetaryAnalysis (&system->SysInfo, planet); DoPlanetaryAnalysis (&system->SysInfo, planet);
@@ -662,7 +685,7 @@ planetRecurse (STAR_DESC *star, SOLARSYS_STATE *system,
// GENERATE_ORBITAL will also call DoPlanetaryAnalysis, // GENERATE_ORBITAL will also call DoPlanetaryAnalysis,
// but with other GenFuncs this is not guaranteed. // but with other GenFuncs this is not guaranteed.
(*system->GenFunc) (GENERATE_ORBITAL); (*system->GenFunc) (GENERATE_ORBITAL);
(*universeRecurseArg->planetFunc) ( (*universeRecurseArg->planetFuncPre) (
planet, universeRecurseArg->arg); planet, universeRecurseArg->arg);
} }
@@ -677,6 +700,18 @@ planetRecurse (STAR_DESC *star, SOLARSYS_STATE *system,
TFB_SeedRandom (oldSeed); TFB_SeedRandom (oldSeed);
} }
if (universeRecurseArg->planetFuncPost != NULL)
{
system->pOrbitalDesc = planet;
DoPlanetaryAnalysis (&system->SysInfo, planet);
// When GenerateRandomIP is used as GenFunc,
// GENERATE_ORBITAL will also call DoPlanetaryAnalysis,
// but with other GenFuncs this is not guaranteed.
(*system->GenFunc) (GENERATE_ORBITAL);
(*universeRecurseArg->planetFuncPost) (
planet, universeRecurseArg->arg);
}
} }
static void static void
@@ -719,8 +754,10 @@ dumpUniverse (FILE *out)
dumpUniverseArg.out = out; dumpUniverseArg.out = out;
universeRecurseArg.systemFunc = dumpSystemCallback; universeRecurseArg.systemFuncPre = dumpSystemCallback;
universeRecurseArg.planetFunc = dumpPlanetCallback; universeRecurseArg.systemFuncPost = NULL;
universeRecurseArg.planetFuncPre = dumpPlanetCallback;
universeRecurseArg.planetFuncPost = NULL;
universeRecurseArg.moonFunc = dumpMoonCallback; universeRecurseArg.moonFunc = dumpMoonCallback;
universeRecurseArg.arg = (void *) &dumpUniverseArg; universeRecurseArg.arg = (void *) &dumpUniverseArg;
@@ -1111,6 +1148,114 @@ generateMineralIndex(const SOLARSYS_STATE *system,
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
struct TallyResourcesArg
{
FILE *out;
COUNT mineralCount;
COUNT bioCount;
};
void
tallyResources (FILE *out)
{
TallyResourcesArg tallyResourcesArg;
UniverseRecurseArg universeRecurseArg;
tallyResourcesArg.out = out;
universeRecurseArg.systemFuncPre = tallySystemPreCallback;
universeRecurseArg.systemFuncPost = tallySystemPostCallback;
universeRecurseArg.planetFuncPre = tallyPlanetCallback;
universeRecurseArg.planetFuncPost = NULL;
universeRecurseArg.moonFunc = tallyMoonCallback;
universeRecurseArg.arg = (void *) &tallyResourcesArg;
UniverseRecurse (&universeRecurseArg);
}
void
tallyResourcesToFile (void)
{
FILE *out;
# define RESOURCE_TALLY_FILE "ResourceTally"
out = fopen(RESOURCE_TALLY_FILE, "w");
if (out == NULL)
{
fprintf(stderr, "Error: Could not open file '%s' for "
"writing: %s\n", RESOURCE_TALLY_FILE, strerror(errno));
return;
}
tallyResources (out);
fclose(out);
fprintf(stdout, "*** Resource tally complete. The game may be in an "
"undefined state.\n");
// Data generation may have changed the game state,
// in particular for special planet generation.
}
static void
tallySystemPreCallback (const STAR_DESC *star, const SOLARSYS_STATE *system,
void *arg)
{
TallyResourcesArg *tallyResourcesArg = (TallyResourcesArg *) arg;
tallyResourcesArg->mineralCount = 0;
tallyResourcesArg->bioCount = 0;
(void) star; /* satisfy compiler */
(void) system; /* satisfy compiler */
}
static void
tallySystemPostCallback (const STAR_DESC *star, const SOLARSYS_STATE *system,
void *arg)
{
UNICODE name[256];
TallyResourcesArg *tallyResourcesArg = (TallyResourcesArg *) arg;
FILE *out = tallyResourcesArg->out;
GetClusterName (star, name);
fprintf (out, "%s\t%d\t%d\n", name, tallyResourcesArg->mineralCount,
tallyResourcesArg->bioCount);
(void) star; /* satisfy compiler */
(void) system; /* satisfy compiler */
}
static void
tallyPlanetCallback (const PLANET_DESC *planet, void *arg)
{
tallyResourcesWorld ((TallyResourcesArg *) arg, planet);
}
static void
tallyMoonCallback (const PLANET_DESC *moon, void *arg)
{
tallyResourcesWorld ((TallyResourcesArg *) arg, moon);
}
static void
tallyResourcesWorld (TallyResourcesArg *arg, const PLANET_DESC *world)
{
if (world->data_index == (BYTE) ~0) {
// StarBase
return;
}
if (world->data_index == (BYTE)(~0 - 1)) {
// Sa-Matra
return;
}
arg->bioCount += calculateBioValue (pSolarSysState, world),
arg->mineralCount += calculateMineralValue (pSolarSysState, world);
}
////////////////////////////////////////////////////////////////////////////
void void
forAllPlanetTypes (void (*callback) (int, const PlanetFrame *, void *), forAllPlanetTypes (void (*callback) (int, const PlanetFrame *, void *),
void *arg) void *arg)
+18 -3
View File
@@ -81,11 +81,20 @@ void forAllMoons (STAR_DESC *star, SOLARSYS_STATE *system, PLANET_DESC *planet,
// Argument to UniverseRecurse() // Argument to UniverseRecurse()
typedef struct typedef struct
{ {
void (*systemFunc) (const STAR_DESC *star, const SOLARSYS_STATE *system, void (*systemFuncPre) (const STAR_DESC *star,
void *arg); const SOLARSYS_STATE *system, void *arg);
void (*planetFunc) (const PLANET_DESC *planet, void *arg); // Called for each system prior to recursing to its planets.
void (*systemFuncPost) (const STAR_DESC *star,
const SOLARSYS_STATE *system, void *arg);
// Called for each system after recursing to its planets.
void (*planetFuncPre) (const PLANET_DESC *planet, void *arg);
// Called for each planet prior to recursing to its moons.
void (*planetFuncPost) (const PLANET_DESC *planet, void *arg);
// Called for each planet after recursing to its moons.
void (*moonFunc) (const PLANET_DESC *moon, void *arg); void (*moonFunc) (const PLANET_DESC *moon, void *arg);
// Called for each moon.
void *arg; void *arg;
// User data.
} UniverseRecurseArg; } UniverseRecurseArg;
// Recurse through all systems, planets, and moons in the universe. // Recurse through all systems, planets, and moons in the universe.
void UniverseRecurse (UniverseRecurseArg *universeRecurseArg); void UniverseRecurse (UniverseRecurseArg *universeRecurseArg);
@@ -122,6 +131,12 @@ COUNT calculateBioValue (const SOLARSYS_STATE *system,
void generateBioIndex(const SOLARSYS_STATE *system, void generateBioIndex(const SOLARSYS_STATE *system,
const PLANET_DESC *world, COUNT bio[]); const PLANET_DESC *world, COUNT bio[]);
// Tally the resources for each star system.
void tallyResources (FILE *out);
// Tally the resources for each star system, output to a file
// "./ResourceTally".
void tallyResourcesToFile (void);
// Call a function for all planet types. // Call a function for all planet types.
void forAllPlanetTypes (void (*callBack) (int, const PlanetFrame *, void forAllPlanetTypes (void (*callBack) (int, const PlanetFrame *,