From bd103935d0be39cdb8e9561596481ed0eb88f233 Mon Sep 17 00:00:00 2001 From: "False.Genesis" Date: Thu, 8 Feb 2007 17:41:52 +0000 Subject: [PATCH] * Added support for timed scripts (=event timers) -> 2 new script commands: "addevent,$eventname,$interval $script" and "removeevent $eventname". note that you cannot register 2 events under the same name! * Fixed a minor bug with ${@myname} when called in an #onload block * Added an example script file autobroadcast.def --- bin/scripts/_enterworld.def | 3 + bin/scripts/autobroadcast.def | 56 +++++++++++++++ src/Client/DefScript/DefScript.cpp | 14 +++- src/Client/DefScript/DefScript.h | 9 ++- src/Client/DefScript/DefScriptFunctions.cpp | 12 ++++ src/Client/DefScript/DynamicEvent.cpp | 77 +++++++++++++++++++++ src/Client/DefScript/DynamicEvent.h | 28 ++++++++ src/Client/PseuWoW.cpp | 1 + src/Client/World/UpdateData.cpp | 6 +- src/PseuWoW.vcproj | 6 ++ 10 files changed, 204 insertions(+), 8 deletions(-) create mode 100644 bin/scripts/autobroadcast.def create mode 100644 src/Client/DefScript/DynamicEvent.cpp create mode 100644 src/Client/DefScript/DynamicEvent.h diff --git a/bin/scripts/_enterworld.def b/bin/scripts/_enterworld.def index 0b430a8..f0ae4bc 100644 --- a/bin/scripts/_enterworld.def +++ b/bin/scripts/_enterworld.def @@ -29,6 +29,9 @@ JOINCHANNEL help JOINCHANNEL tradee // ... +// uncomment the following line to enable the autobroadcast script (first broadcast will happen after the timer (check the script) has expired) +// LOADDEF autobroadcast + // add your own stuff here // ... diff --git a/bin/scripts/autobroadcast.def b/bin/scripts/autobroadcast.def new file mode 100644 index 0000000..cb4bf6f --- /dev/null +++ b/bin/scripts/autobroadcast.def @@ -0,0 +1,56 @@ + +// load this file to register an autobroadcast +// run this file to broadcast immediately (the timer will not change if you do!) + +#permission=255 + +#onload + + // --- CONFIG --- + + // your command you use to broadcast + set,cmd .announce + + // set the broadcast interval (in seconds!) + // here we set it to 20 mins + set,secs 1200 + + // configure the amount of texts we have + set,textcount 3 + // configure the texts to broadcast (must begin with text1) + set,text1 This is PseuWoW autobroadcast #1 + set,text2 And another one! Autobroadcast #2 + set,text3 The world is round, and so am I! + //... Add more texts here... + + // --- END CONFIG -- + + // convert secs into msecs + set,timer ${secs} + mul,timer 1000 + + // we need this variable later as "array-index" + set,x 0 + + // register the timer. + // script name is "autobroadcast", so the event name will be "event_autobroadcast" and the script command to call will be "autobroadcast" + // so we basically register ourselves + addevent,event_{${@myname}},${timer} ${@myname} + + // yay we are loaded :) + log ** AutoBroadcast loaded. ${textcount} Texts. + +#endonload + +// --- begin of the script body --- + +// every call the value of x gets increased by 1 +add,x 1 + +// broadcast the text number x +say ${cmd} ${text${x}} + +// if x = textcount: reset text counter x to 0 (using the modulo operator) +mod,x ${textcount} + + diff --git a/src/Client/DefScript/DefScript.cpp b/src/Client/DefScript/DefScript.cpp index 9eac128..a7fc7bd 100644 --- a/src/Client/DefScript/DefScript.cpp +++ b/src/Client/DefScript/DefScript.cpp @@ -11,11 +11,13 @@ DefScriptPackage::DefScriptPackage() { functionTable=_GetFunctionTable(); + _eventmgr=new DefScript_DynamicEventMgr(this); } DefScriptPackage::~DefScriptPackage() { - Clear(); + delete _eventmgr; + Clear(); } void DefScriptPackage::SetParentMethod(void *p) @@ -57,7 +59,8 @@ DefScriptFunctionTable *DefScriptPackage::_GetFunctionTable(void) const {"bitor",&DefScriptPackage::func_bitor}, {"bitand",&DefScriptPackage::func_bitand}, {"bitxor",&DefScriptPackage::func_bitxor}, - + {"addevent",&DefScriptPackage::func_addevent}, + {"removeevent",&DefScriptPackage::func_removeevent}, // user functions: {"pause",&DefScriptPackage::SCpause}, @@ -104,6 +107,11 @@ unsigned int DefScriptPackage::GetScripts(void){ return Script.size(); } +DefScript_DynamicEventMgr *DefScriptPackage::GetEventMgr(void) +{ + return _eventmgr; +} + bool DefScriptPackage::ScriptExists(std::string name) { for (std::map::iterator i = Script.begin();i != Script.end();i++) @@ -319,7 +327,7 @@ bool DefScriptPackage::RunSingleLine(std::string line){ bool DefScriptPackage::RunSingleLineFromScript(std::string line, DefScript *pScript){ CmdSet Set(pScript); - + Set.myname=pScript->GetName(); // temp fix, this needs to be cleaned up later DefXChgResult final=ReplaceVars(line,&Set,false); CmdSet curSet=SplitLine(final.str); curSet.myname=pScript->GetName(); // temp fix, this needs to be cleaned up later diff --git a/src/Client/DefScript/DefScript.h b/src/Client/DefScript/DefScript.h index 0fac547..bfa9811 100644 --- a/src/Client/DefScript/DefScript.h +++ b/src/Client/DefScript/DefScript.h @@ -20,6 +20,7 @@ typedef __int64_t def_int64; #include #include #include "VarSet.h" +#include "DynamicEvent.h" class DefScriptPackage; class DefScript; @@ -97,7 +98,9 @@ public: bool LoadByName(std::string); void SetFunctionTable(DefScriptFunctionTable*); std::string _NormalizeVarName(std::string, std::string); - + bool RunSingleLineFromScript(std::string line, DefScript *pScript); + DefScript_DynamicEventMgr *GetEventMgr(void); + std::string scPath; // Own executor functions @@ -110,11 +113,11 @@ private: bool Interpret(CmdSet); CmdSet RemoveBrackets(CmdSet); std::string RemoveBracketsFromString(std::string); - bool RunSingleLineFromScript(std::string line, DefScript *pScript); DefScriptFunctionTable *_GetFunctionTable(void) const; DefScriptFunctionTable *functionTable; unsigned int functions; void *parentMethod; + DefScript_DynamicEventMgr *_eventmgr; std::map Script; std::map scriptPermissionMap; @@ -138,6 +141,8 @@ private: bool func_bitor(CmdSet); bool func_bitand(CmdSet); bool func_bitxor(CmdSet); + bool func_addevent(CmdSet); + bool func_removeevent(CmdSet); // Useable own internal functions: bool SCpause(CmdSet); diff --git a/src/Client/DefScript/DefScriptFunctions.cpp b/src/Client/DefScript/DefScriptFunctions.cpp index c227dde..1bc73e3 100644 --- a/src/Client/DefScript/DefScriptFunctions.cpp +++ b/src/Client/DefScript/DefScriptFunctions.cpp @@ -297,3 +297,15 @@ bool DefScriptPackage::func_bitxor(CmdSet Set) variables.Set(vname,ss.str()); return true; } + +bool DefScriptPackage::func_addevent(CmdSet Set) +{ + GetEventMgr()->Add(Set.arg[0],Set.defaultarg,atoi(Set.arg[1].c_str()),Set.myname.c_str()); + return true; +} + +bool DefScriptPackage::func_removeevent(CmdSet Set) +{ + GetEventMgr()->Remove(Set.defaultarg); + return true; +} diff --git a/src/Client/DefScript/DynamicEvent.cpp b/src/Client/DefScript/DynamicEvent.cpp new file mode 100644 index 0000000..3129ccb --- /dev/null +++ b/src/Client/DefScript/DynamicEvent.cpp @@ -0,0 +1,77 @@ +#include "DefScript.h" +#include "DynamicEvent.h" + +struct DefScript_DynamicEvent +{ + std::string name, cmd, parent; + clock_t counter, interval; +}; + +DefScript_DynamicEventMgr::DefScript_DynamicEventMgr(DefScriptPackage *pack) +{ + _pack = pack; + _lastclock = clock(); +} + +DefScript_DynamicEventMgr::~DefScript_DynamicEventMgr() +{ + for(DefDynamicEventList::iterator i = _storage.begin(); i != _storage.end(); i++) + { + delete *i; + } +} + +void DefScript_DynamicEventMgr::Add(std::string name, std::string script, clock_t interval, const char *parent) +{ + _DEFSC_DEBUG( printf("DEFSCRIPT: Add Event %s, interval=%u, parent=%s\n",name.c_str(),interval,parent?parent:""); printf("DEFSCRIPT: EventRun='%s'\n",script.c_str()); ) + if(name.empty() || script.empty() || interval==0) + return; + for(DefDynamicEventList::iterator i = _storage.begin(); i != _storage.end(); i++) + if((*i)->name == name) + return; // event with that name is already registered + DefScript_DynamicEvent *e = new DefScript_DynamicEvent; + e->name = name; + e->cmd = script; + e->interval = interval; + e->parent = parent?parent:""; + e->counter = 0; + _storage.push_back(e); +} + +void DefScript_DynamicEventMgr::Remove(std::string name) +{ + if(name.empty()) + return; + for(DefDynamicEventList::iterator i = _storage.begin(); i != _storage.end(); i++) + { + delete *i; + _storage.erase(i); + break; + } + return; +} + +void DefScript_DynamicEventMgr::Update(void) +{ + clock_t diff = clock() - _lastclock; + _lastclock = clock(); + DefScript *sc; + for(DefDynamicEventList::iterator i = _storage.begin(); i != _storage.end(); i++) + { + sc = NULL; + (*i)->counter += diff; + if((*i)->counter >= (*i)->interval) + { + (*i)->counter %= (*i)->interval; + + if(!(*i)->parent.empty()) + sc = _pack->GetScript((*i)->parent); + + if(sc) + _pack->RunSingleLineFromScript((*i)->cmd,sc); + else + _pack->RunSingleLine((*i)->cmd); + } + } +} + \ No newline at end of file diff --git a/src/Client/DefScript/DynamicEvent.h b/src/Client/DefScript/DynamicEvent.h new file mode 100644 index 0000000..8cfab41 --- /dev/null +++ b/src/Client/DefScript/DynamicEvent.h @@ -0,0 +1,28 @@ +#ifndef _DEF_DYNAMICEVENT_H +#define _DEF_DYNAMICEVENT_H + +#include +#include +#include + +struct DefScript_DynamicEvent; +class DefScript; +class DefScriptPackage; +typedef std::list DefDynamicEventList; + +class DefScript_DynamicEventMgr +{ +public: + DefScript_DynamicEventMgr(DefScriptPackage *pack); + ~DefScript_DynamicEventMgr(); + void Add(std::string name, std::string script, clock_t interval, const char *parent); + void Remove(std::string name); + void Update(void); + +private: + DefDynamicEventList _storage; + clock_t _lastclock; + DefScriptPackage *_pack; +}; + +#endif diff --git a/src/Client/PseuWoW.cpp b/src/Client/PseuWoW.cpp index 1beb41f..11328cd 100644 --- a/src/Client/PseuWoW.cpp +++ b/src/Client/PseuWoW.cpp @@ -206,6 +206,7 @@ void PseuInstance::Update() this->Sleep(1000); // wait 1 sec before reconnecting return; } + GetScripts()->GetEventMgr()->Update(); this->Sleep(GetConf()->networksleeptime); } diff --git a/src/Client/World/UpdateData.cpp b/src/Client/World/UpdateData.cpp index f0b227e..82d386b 100644 --- a/src/Client/World/UpdateData.cpp +++ b/src/Client/World/UpdateData.cpp @@ -48,8 +48,8 @@ void WorldSession::_HandleUpdateObjectOpcode(WorldPacket& recvPacket) { case UPDATETYPE_VALUES: { - uint8 blockcount, masksize, valuesCount = 1500; - uint32 value; + uint8 blockcount, masksize; + uint32 value, valuesCount = 1500; uguid = recvPacket.GetPackedGuid(); recvPacket >> blockcount; masksize = blockcount * 4; @@ -61,7 +61,7 @@ void WorldSession::_HandleUpdateObjectOpcode(WorldPacket& recvPacket) recvPacket.read((uint8*)updateMask, masksize); umask.SetMask(updateMask); - for (int i = 0; i < valuesCount; i++) // How do i get valuesCount? + for (uint32 i = 0; i < valuesCount; i++) // How do i get valuesCount? { if (umask.GetBit(i)) { diff --git a/src/PseuWoW.vcproj b/src/PseuWoW.vcproj index 69f0ff2..9b9e401 100644 --- a/src/PseuWoW.vcproj +++ b/src/PseuWoW.vcproj @@ -240,6 +240,12 @@ + + + +