diff --git a/apps/dsm/mods/mod_utils/ModUtils.cpp b/apps/dsm/mods/mod_utils/ModUtils.cpp index 198f34fe..4025a0d9 100644 --- a/apps/dsm/mods/mod_utils/ModUtils.cpp +++ b/apps/dsm/mods/mod_utils/ModUtils.cpp @@ -28,6 +28,8 @@ #include "log.h" #include "AmUtils.h" #include +#include +#include #include "DSMSession.h" #include "AmSession.h" @@ -59,6 +61,8 @@ DSMAction* SCUtilsModule::getAction(const string& from_str) { DEF_CMD("utils.playCountLeft", SCUPlayCountLeftAction); DEF_CMD("utils.getNewId", SCGetNewIdAction); DEF_CMD("utils.spell", SCUSpellAction); + DEF_CMD("utils.rand", SCURandomAction); + DEF_CMD("utils.srand", SCUSRandomAction); return NULL; } @@ -145,6 +149,27 @@ EXEC_ACTION_START(SCGetNewIdAction) { sc_sess->var[d]=AmSession::getNewId(); } EXEC_ACTION_END; +CONST_ACTION_2P(SCURandomAction, ',', true); +EXEC_ACTION_START(SCURandomAction) { + string varname = resolveVars(par1, sess, sc_sess, event_params); + string modulo_str = resolveVars(par2, sess, sc_sess, event_params); + + unsigned int modulo = 0; + if (modulo_str.length()) + str2i(modulo_str, modulo); + + if (modulo) + sc_sess->var[varname]=int2str(rand()%modulo); + else + sc_sess->var[varname]=int2str(rand()); + + DBG("Generated random $%s=%s\n", varname.c_str(), sc_sess->var[varname].c_str()); +} EXEC_ACTION_END; + +EXEC_ACTION_START(SCUSRandomAction) { + srand(time(0)); +} EXEC_ACTION_END; + CONST_ACTION_2P(SCUSpellAction, ',', true); EXEC_ACTION_START(SCUSpellAction) { string basedir = resolveVars(par2, sess, sc_sess, event_params); diff --git a/apps/dsm/mods/mod_utils/ModUtils.h b/apps/dsm/mods/mod_utils/ModUtils.h index af8e8e7d..b6c90f92 100644 --- a/apps/dsm/mods/mod_utils/ModUtils.h +++ b/apps/dsm/mods/mod_utils/ModUtils.h @@ -43,4 +43,7 @@ DEF_ACTION_2P(SCUPlayCountRightAction); DEF_ACTION_2P(SCUPlayCountLeftAction); DEF_ACTION_1P(SCGetNewIdAction); DEF_ACTION_2P(SCUSpellAction); +DEF_ACTION_2P(SCURandomAction); +DEF_ACTION_1P(SCUSRandomAction); + #endif