Skip to content

Instantly share code, notes, and snippets.

@louisdx
Created April 14, 2011 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louisdx/919303 to your computer and use it in GitHub Desktop.
Save louisdx/919303 to your computer and use it in GitHub Desktop.
Plugin system
/// Current plug-ing system, schematically:
std::vector<Block*> BlockCB; // Dirt, Stone, Stairs, Torch, ...
PacketHandler::player_does_something()
{
int16_t block_type = figureOutAffectedBlockType();
for ( each Block* blockcb in BlockCB)
{
if (blockcb != NULL && blockcb->affectedBlock(block_type))
{
blockcb->onRelevantEvent();
// ...
}
}
// repeated many times
}
/***********************/
/// Observation: affectedBlock() ONLY returns "true" for at most 2 blocks: The actual affected block, and the default block. Yet each time the entire BlockCB vector has to be traversed. Here's how this could be accelerated:
clever_map_type<int8_t, Block*> BLOCK_ACTIONS;
PacketHandler::player_does_something()
{
int16_t block_type = figureOutAffectedBlockType();
if (BLOCK_ACTIONS[block_type])
{
BLOCK_ACTIONS[block_type]->onRelevantEvent();
}
BLOCK_ACTIONS["default"]->onRelevantEvent();
}
/// The assumption is that the default handler is called AFTER the specific one. One could split this up into PRE and POST default actions if desired.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment