From 2dd47ba2e83ea260b061a69b0e99d05ad890f395 Mon Sep 17 00:00:00 2001 From: Meep-Eep Date: Sat, 4 Oct 2008 13:26:53 +0000 Subject: [PATCH] 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 --- sc2/src/sc2code/uqmdebug.c | 163 +++++++++++++++++++++++++++++++++++-- sc2/src/sc2code/uqmdebug.h | 21 ++++- 2 files changed, 172 insertions(+), 12 deletions(-) diff --git a/sc2/src/sc2code/uqmdebug.c b/sc2/src/sc2code/uqmdebug.c index 1bf8de50f..384fab7ef 100644 --- a/sc2/src/sc2code/uqmdebug.c +++ b/sc2/src/sc2code/uqmdebug.c @@ -50,6 +50,16 @@ static void dumpPlanetCallback (const PLANET_DESC *planet, void *arg); static void dumpMoonCallback (const PLANET_DESC *moon, void *arg); 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, void *arg); @@ -102,6 +112,10 @@ debugKeyPressed (void) // This will cause dumpUniverseToFile to be called from the // main loop. Calling it from here would give threading // problems. +// debugHook = tallyResourcesToFile; + // This will cause tallyResourcesToFile to be called from the + // main loop. Calling it from here would give threading + // problems. // Interactive: // uio_debugInteractive(stdin, stdout, stderr); @@ -579,8 +593,10 @@ UniverseRecurse (UniverseRecurseArg *universeRecurseArg) BOOLEAN clockRunning; ACTIVITY savedActivity; - if (universeRecurseArg->systemFunc == NULL - && universeRecurseArg->planetFunc == NULL + if (universeRecurseArg->systemFuncPre == NULL + && universeRecurseArg->systemFuncPost == NULL + && universeRecurseArg->planetFuncPre == NULL + && universeRecurseArg->planetFuncPost == NULL && universeRecurseArg->moonFunc == NULL) return; @@ -624,19 +640,26 @@ starRecurse (STAR_DESC *star, void *arg) pSolarSysState = &SolarSysState; (*pSolarSysState->GenFunc) (GENERATE_PLANETS); - if (universeRecurseArg->systemFunc != NULL) + if (universeRecurseArg->systemFuncPre != NULL) { - (*universeRecurseArg->systemFunc) ( + (*universeRecurseArg->systemFuncPre) ( star, &SolarSysState, universeRecurseArg->arg); } - if (universeRecurseArg->planetFunc != NULL + if (universeRecurseArg->planetFuncPre != NULL + || universeRecurseArg->planetFuncPost != NULL || universeRecurseArg->moonFunc != NULL) { forAllPlanets (star, &SolarSysState, planetRecurse, (void *) universeRecurseArg); } + if (universeRecurseArg->systemFuncPost != NULL) + { + (*universeRecurseArg->systemFuncPost) ( + star, &SolarSysState, universeRecurseArg->arg); + } + pSolarSysState = oldPSolarSysState; CurStarDescPtr = oldStarDescPtr; TFB_SeedRandom (oldSeed); @@ -654,7 +677,7 @@ planetRecurse (STAR_DESC *star, SOLARSYS_STATE *system, system->pBaseDesc = planet; planet->pPrevDesc = &system->SunDesc[0]; - if (universeRecurseArg->planetFunc != NULL) + if (universeRecurseArg->planetFuncPre != NULL) { system->pOrbitalDesc = planet; DoPlanetaryAnalysis (&system->SysInfo, planet); @@ -662,7 +685,7 @@ planetRecurse (STAR_DESC *star, SOLARSYS_STATE *system, // GENERATE_ORBITAL will also call DoPlanetaryAnalysis, // but with other GenFuncs this is not guaranteed. (*system->GenFunc) (GENERATE_ORBITAL); - (*universeRecurseArg->planetFunc) ( + (*universeRecurseArg->planetFuncPre) ( planet, universeRecurseArg->arg); } @@ -677,6 +700,18 @@ planetRecurse (STAR_DESC *star, SOLARSYS_STATE *system, 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 @@ -719,8 +754,10 @@ dumpUniverse (FILE *out) dumpUniverseArg.out = out; - universeRecurseArg.systemFunc = dumpSystemCallback; - universeRecurseArg.planetFunc = dumpPlanetCallback; + universeRecurseArg.systemFuncPre = dumpSystemCallback; + universeRecurseArg.systemFuncPost = NULL; + universeRecurseArg.planetFuncPre = dumpPlanetCallback; + universeRecurseArg.planetFuncPost = NULL; universeRecurseArg.moonFunc = dumpMoonCallback; 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 forAllPlanetTypes (void (*callback) (int, const PlanetFrame *, void *), void *arg) diff --git a/sc2/src/sc2code/uqmdebug.h b/sc2/src/sc2code/uqmdebug.h index 275fe0298..c164ef199 100644 --- a/sc2/src/sc2code/uqmdebug.h +++ b/sc2/src/sc2code/uqmdebug.h @@ -81,11 +81,20 @@ void forAllMoons (STAR_DESC *star, SOLARSYS_STATE *system, PLANET_DESC *planet, // Argument to UniverseRecurse() typedef struct { - void (*systemFunc) (const STAR_DESC *star, const SOLARSYS_STATE *system, - void *arg); - void (*planetFunc) (const PLANET_DESC *planet, void *arg); + void (*systemFuncPre) (const STAR_DESC *star, + const SOLARSYS_STATE *system, 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); + // Called for each moon. void *arg; + // User data. } UniverseRecurseArg; // Recurse through all systems, planets, and moons in the universe. void UniverseRecurse (UniverseRecurseArg *universeRecurseArg); @@ -122,6 +131,12 @@ COUNT calculateBioValue (const SOLARSYS_STATE *system, void generateBioIndex(const SOLARSYS_STATE *system, 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. void forAllPlanetTypes (void (*callBack) (int, const PlanetFrame *,