* 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
This commit is contained in:
False.Genesis 2007-02-08 17:41:52 +00:00
parent 4aa6911563
commit bd103935d0
10 changed files with 204 additions and 8 deletions

View File

@ -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
// ...

View File

@ -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}

View File

@ -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<std::string,DefScript*>::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

View File

@ -20,6 +20,7 @@ typedef __int64_t def_int64;
#include <map>
#include <deque>
#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<std::string,DefScript*> Script;
std::map<std::string,unsigned char> 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);

View File

@ -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;
}

View File

@ -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);
}
}
}

View File

@ -0,0 +1,28 @@
#ifndef _DEF_DYNAMICEVENT_H
#define _DEF_DYNAMICEVENT_H
#include <time.h>
#include <list>
#include <string>
struct DefScript_DynamicEvent;
class DefScript;
class DefScriptPackage;
typedef std::list<DefScript_DynamicEvent*> 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

View File

@ -206,6 +206,7 @@ void PseuInstance::Update()
this->Sleep(1000); // wait 1 sec before reconnecting
return;
}
GetScripts()->GetEventMgr()->Update();
this->Sleep(GetConf()->networksleeptime);
}

View File

@ -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))
{

View File

@ -240,6 +240,12 @@
<File
RelativePath=".\Client\DefScript\DefScriptFunctions.cpp">
</File>
<File
RelativePath=".\Client\DefScript\DynamicEvent.cpp">
</File>
<File
RelativePath=".\Client\DefScript\DynamicEvent.h">
</File>
<File
RelativePath=".\Client\DefScript\VarSet.cpp">
</File>