Implemented global EventHandlers in MAPINFO

This commit is contained in:
ZZYZX 2017-01-22 09:58:48 +02:00
parent e33a320544
commit d5a0c29a68
6 changed files with 47 additions and 31 deletions

View file

@ -2,6 +2,7 @@
#include "virtual.h"
#include "r_utility.h"
#include "g_levellocals.h"
#include "gi.h"
#include "v_text.h"
DStaticEventHandler* E_FirstEventHandler = nullptr;
@ -56,6 +57,37 @@ bool E_IsStaticType(PClass* type)
!type->IsDescendantOf(RUNTIME_CLASS(DRenderEventHandler)));
}
static void E_InitStaticHandler(PClass* type, FString typestring, bool map)
{
if (type == nullptr)
{
Printf("%cGWarning: unknown event handler class %s in MAPINFO!", TEXTCOLOR_ESCAPE, typestring.GetChars());
return;
}
if (!E_IsStaticType(type))
{
Printf("%cGWarning: invalid event handler class %s in MAPINFO!\nMAPINFO event handlers should inherit Static* directly!", TEXTCOLOR_ESCAPE, typestring.GetChars());
return;
}
// check if type already exists, don't add twice.
bool typeExists = false;
for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next)
{
if (handler->IsA(type))
{
typeExists = true;
break;
}
}
if (typeExists) return;
DStaticEventHandler* handler = (DStaticEventHandler*)type->CreateNew();
handler->isMapScope = map;
E_RegisterHandler(handler);
}
void E_InitStaticHandlers(bool map)
{
// remove existing
@ -72,39 +104,17 @@ void E_InitStaticHandlers(bool map)
{
FString typestring = level.info->EventHandlers[i];
PClass* type = PClass::FindClass(typestring);
if (type == nullptr)
{
Printf("%cGWarning: unknown event handler class %s in MAPINFO!", TEXTCOLOR_ESCAPE, typestring.GetChars());
continue;
}
if (!E_IsStaticType(type))
{
Printf("%cGWarning: invalid event handler class %s in MAPINFO!\nMAPINFO event handlers should inherit Static* directly!", TEXTCOLOR_ESCAPE, typestring.GetChars());
continue;
}
// check if type already exists, don't add twice.
bool typeExists = false;
for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next)
{
if (handler->IsA(type))
{
typeExists = true;
break;
}
}
if (typeExists) continue;
DStaticEventHandler* handler = (DStaticEventHandler*)type->CreateNew();
handler->isMapScope = true;
E_RegisterHandler(handler);
E_InitStaticHandler(type, typestring, true);
}
}
else
{
for (unsigned int i = 0; i < gameinfo.EventHandlers.Size(); i++)
{
FString typestring = gameinfo.EventHandlers[i];
PClass* type = PClass::FindClass(typestring);
E_InitStaticHandler(type, typestring, false);
}
}
}

View file

@ -1068,6 +1068,8 @@ void G_DoLoadLevel (int position, bool autosave)
StatusBar->AttachToPlayer (&players[consoleplayer]);
// [ZZ] init per-map static handlers
E_InitStaticHandlers(true);
// call map load hook
E_MapLoaded();
P_DoDeferedScripts (); // [RH] Do script actions that were triggered on another map.
if (demoplayback || oldgs == GS_STARTUP || oldgs == GS_TITLELEVEL)

View file

@ -317,6 +317,7 @@ void FMapInfoParser::ParseGameInfo()
GAMEINFOKEY_STRINGARRAY(PrecachedClasses, "precacheclasses", 0, false)
GAMEINFOKEY_STRINGARRAY(PrecachedTextures, "precachetextures", 0, false)
GAMEINFOKEY_STRINGARRAY(PrecachedSounds, "precachesounds", 0, false)
GAMEINFOKEY_STRINGARRAY(EventHandlers, "eventhandlers", 0, false)
GAMEINFOKEY_STRING(PauseSign, "pausesign")
GAMEINFOKEY_STRING(quitSound, "quitSound")
GAMEINFOKEY_STRING(BorderFlat, "borderFlat")

View file

@ -124,6 +124,7 @@ struct gameinfo_t
TArray<FName> PrecachedClasses;
TArray<FString> PrecachedTextures;
TArray<FSoundID> PrecachedSounds;
TArray<FString> EventHandlers;
FString titleMusic;
int titleOrder;

View file

@ -54,6 +54,7 @@
#include "thingdef.h"
#include "d_player.h"
#include "doomerrors.h"
#include "events.h"
extern void LoadActors ();
extern void InitBotStuff();
@ -214,6 +215,9 @@ void PClassActor::StaticInit()
ClearStrifeTypes();
LoadActors ();
InitBotStuff();
// reinit GLOBAL static stuff from gameinfo, once classes are loaded.
E_InitStaticHandlers(false);
}
//==========================================================================

View file

@ -1443,8 +1443,6 @@ void P_SpawnSpecials (void)
break;
}
}
// [ZZ] Loading event hook
E_MapLoaded();
// [RH] Start running any open scripts on this map
FBehavior::StaticStartTypedScripts (SCRIPT_Open, NULL, false);
}