rand / srand functions

git-svn-id: http://svn.berlios.de/svnroot/repos/sems/trunk@1490 8eb893ce-cfd4-0310-b710-fb5ebe64c474
sayer/1.4-spce2.6
Stefan Sayer 17 years ago
parent 857363dbf5
commit bc584d6f8c

@ -28,6 +28,8 @@
#include "log.h"
#include "AmUtils.h"
#include <math.h>
#include <stdlib.h>
#include <time.h>
#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);

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

Loading…
Cancel
Save