Decouple the generator and the starmap defs.
I changed it to use string lookups. This permits hardcoded strings in the table, which defeats a bit of static checking -- but the goal is to permit those strings also at runtime so folks can define their own generators as plugins. Further, the decoupling means that there's less tight coupling between how the generator works and how the map is defined -- fewer forward decls and constants for each race, etc.
This commit is contained in:
+3
-16
@@ -50,24 +50,18 @@ extern GenerateFunctions generateYehatFunctions;
|
|||||||
extern GenerateFunctions generateZoqFotPikFunctions;
|
extern GenerateFunctions generateZoqFotPikFunctions;
|
||||||
extern GenerateFunctions generateZoqFotPikScoutFunctions;
|
extern GenerateFunctions generateZoqFotPikScoutFunctions;
|
||||||
|
|
||||||
extern GenerateFunctions *getGenerateCxxFunctions( void );
|
|
||||||
|
|
||||||
|
|
||||||
extern struct GeneratorBase cxxDefaultPlanetGen;
|
|
||||||
|
|
||||||
|
|
||||||
const GenerateFunctions *
|
const GenerateFunctions *
|
||||||
getGenerateFunctions (STAR_DESC *const star, void **sup, const char **supArg )
|
getGenerateFunctions (STAR_DESC *const star)
|
||||||
{
|
{
|
||||||
*sup= star->supplement;
|
|
||||||
*supArg= 0;
|
|
||||||
|
|
||||||
switch (star->Index)
|
switch (star->Index)
|
||||||
{
|
{
|
||||||
// Legacy ones are disabled...
|
// Legacy ones are disabled...
|
||||||
case SHOFIXTI_DEFINED:
|
case SHOFIXTI_DEFINED:
|
||||||
case SLYLANDRO_DEFINED:
|
case SLYLANDRO_DEFINED:
|
||||||
case START_COLONY_DEFINED:
|
case START_COLONY_DEFINED:
|
||||||
|
case CXX_DEFINED:
|
||||||
|
default:
|
||||||
abort();
|
abort();
|
||||||
|
|
||||||
case SOL_DEFINED:
|
case SOL_DEFINED:
|
||||||
@@ -137,13 +131,6 @@ getGenerateFunctions (STAR_DESC *const star, void **sup, const char **supArg )
|
|||||||
return &generateRainbowWorldFunctions;
|
return &generateRainbowWorldFunctions;
|
||||||
case ILWRATH_DEFINED:
|
case ILWRATH_DEFINED:
|
||||||
return &generateIlwrathFunctions;
|
return &generateIlwrathFunctions;
|
||||||
default:
|
|
||||||
*sup= &cxxDefaultPlanetGen;
|
|
||||||
// FALLTHROUGH
|
|
||||||
|
|
||||||
// Eventually everything should go thru this.
|
|
||||||
case CXX_DEFINED:
|
|
||||||
return getGenerateCxxFunctions();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
const GenerateFunctions *getGenerateFunctions (STAR_DESC *star, void **sup, const char **arg );
|
const GenerateFunctions *getGenerateFunctions (STAR_DESC *star);
|
||||||
|
|
||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -21,10 +21,6 @@
|
|||||||
#include "planets/planets.h"
|
#include "planets/planets.h"
|
||||||
#include "planets/elemdata.h"
|
#include "planets/elemdata.h"
|
||||||
|
|
||||||
extern struct GeneratorBase slyDef;
|
|
||||||
extern struct GeneratorBase shofDef;
|
|
||||||
extern struct GeneratorBase colonyDef;
|
|
||||||
|
|
||||||
// This array has to be sorted in Y coordinate order then X coordinate order.
|
// This array has to be sorted in Y coordinate order then X coordinate order.
|
||||||
// Items out of order can cause crashes (better, actually) or "ghost"
|
// Items out of order can cause crashes (better, actually) or "ghost"
|
||||||
// stars in the map that cannot be entered.
|
// stars in the map that cannot be entered.
|
||||||
@@ -542,8 +538,8 @@ STAR_DESC starmap_array[] =
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
//{{2908, 269}, MAKE_STAR (DWARF_STAR, BLUE_BODY, -1), SHOFIXTI_DEFINED, 4, 82},
|
//{{2908, 269}, MAKE_STAR (DWARF_STAR, BLUE_BODY, -1), SHOFIXTI_DEFINED, 4, 82},
|
||||||
{{1004, 1083}, MAKE_STAR (DWARF_STAR, GREEN_BODY, -1), CXX_DEFINED, 2, 27, &slyDef},
|
{{1004, 1083}, MAKE_STAR (DWARF_STAR, GREEN_BODY, -1), 0, 2, 27, "Slylandro"},
|
||||||
{{1776, 1395}, MAKE_STAR (DWARF_STAR, YELLOW_BODY, -1), CXX_DEFINED, 0, 98, &colonyDef},
|
{{1776, 1395}, MAKE_STAR (DWARF_STAR, YELLOW_BODY, -1), 0, 0, 98, "Colony"},
|
||||||
|
|
||||||
{{1777, 1405}, MAKE_STAR (DWARF_STAR, YELLOW_BODY, -1), 0, 0, 99},
|
{{1777, 1405}, MAKE_STAR (DWARF_STAR, YELLOW_BODY, -1), 0, 0, 99},
|
||||||
|
|
||||||
@@ -552,7 +548,7 @@ STAR_DESC starmap_array[] =
|
|||||||
{{1752, 1450}, MAKE_STAR (DWARF_STAR, YELLOW_BODY, -1), SOL_DEFINED, 0, 129},
|
{{1752, 1450}, MAKE_STAR (DWARF_STAR, YELLOW_BODY, -1), SOL_DEFINED, 0, 129},
|
||||||
|
|
||||||
|
|
||||||
{{1848, 1469}, MAKE_STAR (DWARF_STAR, BLUE_BODY, -1), CXX_DEFINED, 4, 82, &shofDef},
|
{{1848, 1469}, MAKE_STAR (DWARF_STAR, BLUE_BODY, -1), 0, 4, 82, "Shofixti"},
|
||||||
|
|
||||||
{{1751, 1479}, MAKE_STAR (GIANT_STAR, GREEN_BODY, -1), URQUAN_WRECK_DEFINED, 1, 59},
|
{{1751, 1479}, MAKE_STAR (GIANT_STAR, GREEN_BODY, -1), URQUAN_WRECK_DEFINED, 1, 59},
|
||||||
{{1707, 1501}, MAKE_STAR (DWARF_STAR, GREEN_BODY, -1), 0, 0, 52},
|
{{1707, 1501}, MAKE_STAR (DWARF_STAR, GREEN_BODY, -1), 0, 0, 52},
|
||||||
|
|||||||
@@ -16,9 +16,16 @@
|
|||||||
|
|
||||||
#include "uqm/planets/generate.h"
|
#include "uqm/planets/generate.h"
|
||||||
|
|
||||||
|
#include <map>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <exception>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "uqm/planets/generate/gendefault.h"
|
#include "uqm/planets/generate/gendefault.h"
|
||||||
|
#include "uqm/gendef.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* XXX: Note that the way that defaults get called via C++ is a bit
|
* XXX: Note that the way that defaults get called via C++ is a bit
|
||||||
@@ -171,18 +178,69 @@ namespace Planets ::detail:: Generate_m
|
|||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
class RealDefaultGenerator
|
auto &
|
||||||
: public DefaultGenerator
|
registry()
|
||||||
{
|
{
|
||||||
public:
|
static std::map< std::string, std::unique_ptr< Generator > > r;
|
||||||
~RealDefaultGenerator() override= default;
|
return r;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
auto init= []
|
||||||
|
{
|
||||||
|
class RealDefaultGenerator
|
||||||
|
: public DefaultGenerator
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
~RealDefaultGenerator() override= default;
|
||||||
|
};
|
||||||
|
Generator::add( ".default", std::make_unique< RealDefaultGenerator >() );
|
||||||
|
return 0xDEADBEEF;
|
||||||
|
}();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
Generator::add( const std::string &name, std::unique_ptr< Generator > gen )
|
||||||
|
{
|
||||||
|
registry()[ name ]= std::move( gen );
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
Planets::detail::Generate_m::RealDefaultGenerator cxxDefaultPlanetGen;
|
using Planets::SolarSystem;
|
||||||
|
using Planets::detail::Generate_m::registry;
|
||||||
|
|
||||||
|
extern GenerateFunctions *getGenerateCxxFunctions( void );
|
||||||
|
|
||||||
|
void
|
||||||
|
SetupGeneratorForSolarSystem( STAR_DESC *const desc, SolarSystem *const solarSys )
|
||||||
|
{
|
||||||
|
// Must either have a legacy constructor or a named lookup,
|
||||||
|
// but not both.
|
||||||
|
|
||||||
|
// If there's a legacy builder, we must have no supplement.
|
||||||
|
// We just go to the old way of doing things...
|
||||||
|
if( desc->Index )
|
||||||
|
{
|
||||||
|
assert( desc->supplement == nullptr );
|
||||||
|
solarSys->genFuncs= getGenerateFunctions( desc );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we've a supplement, there should be no legacy.
|
||||||
|
|
||||||
|
solarSys->genFuncs= getGenerateCxxFunctions(); // By default we pull up into C++
|
||||||
|
|
||||||
|
const std::string name= desc->supplement == nullptr ? ".default" : desc->supplement;
|
||||||
|
|
||||||
|
if( not registry().count( name ) )
|
||||||
|
{
|
||||||
|
throw std::runtime_error{ "Solar System generator " + name + " is missing" };
|
||||||
|
}
|
||||||
|
|
||||||
|
solarSys->genSupplement= registry().at( name ).get();
|
||||||
|
|
||||||
|
solarSys->supArgument= desc->supArg;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,9 +116,14 @@ struct GenerateFunctions {
|
|||||||
PickupLifeFunction pickupLife;
|
PickupLifeFunction pickupLife;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void SetupGeneratorForSolarSystem( STAR_DESC *desc, SOLARSYS_STATE *state );
|
||||||
|
|
||||||
#if defined(__cplusplus)
|
#if defined(__cplusplus)
|
||||||
} // extern "C"
|
} // extern "C"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace Planets ::detail:: Generate_m
|
namespace Planets ::detail:: Generate_m
|
||||||
{
|
{
|
||||||
inline namespace exports
|
inline namespace exports
|
||||||
@@ -133,6 +138,8 @@ namespace Planets ::detail:: Generate_m
|
|||||||
: public GeneratorBase
|
: public GeneratorBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
static void add( const std::string &name, std::unique_ptr< Generator > );
|
||||||
|
|
||||||
virtual ~Generator()= 0;
|
virtual ~Generator()= 0;
|
||||||
|
|
||||||
virtual void initNpcs( SolarSystem *solarSys )= 0;
|
virtual void initNpcs( SolarSystem *solarSys )= 0;
|
||||||
|
|||||||
@@ -33,16 +33,18 @@ namespace
|
|||||||
: public DefaultGenerator
|
: public DefaultGenerator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void initNpcs( SolarSystem *solarSys) override;
|
void initNpcs( SolarSystem *solarSys ) override;
|
||||||
bool generatePlanets( SolarSystem *solarSys) override;
|
bool generatePlanets( SolarSystem *solarSys ) override;
|
||||||
bool generateOrbital( SolarSystem *solarSys,
|
bool generateOrbital( SolarSystem *solarSys,
|
||||||
PLANET_DESC *world) override;
|
PLANET_DESC *world ) override;
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
extern "C"
|
auto init= []
|
||||||
{
|
{
|
||||||
GenerateColony colonyDef;
|
Generator::add( "Colony", std::make_unique< GenerateColony >() );
|
||||||
|
|
||||||
|
return 0xDEADBEEF;
|
||||||
|
}();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -39,13 +39,14 @@ namespace
|
|||||||
|
|
||||||
static void check_old_shofixti ();
|
static void check_old_shofixti ();
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
extern "C"
|
auto init= []
|
||||||
{
|
{
|
||||||
GenerateShofixti shofDef;
|
Generator::add( "Shofixti", std::make_unique< GenerateShofixti >() );
|
||||||
}
|
|
||||||
|
|
||||||
|
return 0xDEADBEEF;
|
||||||
|
}();
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
GenerateShofixti::initNpcs( SolarSystem *const solarSys )
|
GenerateShofixti::initNpcs( SolarSystem *const solarSys )
|
||||||
|
|||||||
@@ -58,9 +58,12 @@ namespace
|
|||||||
return DefaultGenerator::generateOrbital( solarSys, world );
|
return DefaultGenerator::generateOrbital( solarSys, world );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
auto init= []
|
||||||
|
{
|
||||||
|
Generator::add( "Slylandro", std::make_unique< GenerateSlylandro >() );
|
||||||
|
|
||||||
|
return 0xDEADBEEF;
|
||||||
|
}();
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C"
|
|
||||||
{
|
|
||||||
GenerateSlylandro slyDef;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -142,8 +142,8 @@ struct star_desc
|
|||||||
BYTE Prefix;
|
BYTE Prefix;
|
||||||
BYTE Postfix;
|
BYTE Postfix;
|
||||||
|
|
||||||
void *supplement; // The CXX builder's pointer gets put here.
|
const char *supplement; // The CXX builder's name gets put here.
|
||||||
void *supArg; // Extra info for the CXX builder goes here.
|
const char *supArg; // Extra info for the CXX builder goes here.
|
||||||
};
|
};
|
||||||
|
|
||||||
struct node_info
|
struct node_info
|
||||||
|
|||||||
@@ -1735,8 +1735,7 @@ ExploreSolarSys (void)
|
|||||||
|
|
||||||
memset (pSolarSysState, 0, sizeof (*pSolarSysState));
|
memset (pSolarSysState, 0, sizeof (*pSolarSysState));
|
||||||
|
|
||||||
SolarSysState.genFuncs = getGenerateFunctions (CurStarDescPtr, &SolarSysState.genSupplement,
|
SetupGeneratorForSolarSystem( CurStarDescPtr, &SolarSysState );
|
||||||
&SolarSysState.supArgument);
|
|
||||||
|
|
||||||
InitSolarSys ();
|
InitSolarSys ();
|
||||||
SetMenuSounds (MENU_SOUND_NONE, MENU_SOUND_NONE);
|
SetMenuSounds (MENU_SOUND_NONE, MENU_SOUND_NONE);
|
||||||
|
|||||||
@@ -645,8 +645,7 @@ starRecurse (STAR_DESC *star, void *arg)
|
|||||||
SolarSysState.SunDesc[0].location.x = 0;
|
SolarSysState.SunDesc[0].location.x = 0;
|
||||||
SolarSysState.SunDesc[0].location.y = 0;
|
SolarSysState.SunDesc[0].location.y = 0;
|
||||||
//SolarSysState.SunDesc[0].radius = MIN_ZOOM_RADIUS;
|
//SolarSysState.SunDesc[0].radius = MIN_ZOOM_RADIUS;
|
||||||
SolarSysState.genFuncs = getGenerateFunctions (star, &SolarSysState.genSupplement,
|
SetupGeneratorForSolarSystem( star, &SolarSysState );
|
||||||
&SolarSysState.supArgument);
|
|
||||||
|
|
||||||
pSolarSysState = &SolarSysState;
|
pSolarSysState = &SolarSysState;
|
||||||
(*SolarSysState.genFuncs->generatePlanets) (&SolarSysState);
|
(*SolarSysState.genFuncs->generatePlanets) (&SolarSysState);
|
||||||
|
|||||||
Reference in New Issue
Block a user