Changed: Random command has now private roll.

--HG--
branch : develop
This commit is contained in:
inky 2019-01-07 00:14:14 +01:00
parent ef1a51d0e4
commit ed31302163
3 changed files with 31 additions and 7 deletions

View file

@ -475,14 +475,17 @@ bool randomFromString(std::string const& str, sint16& val, sint16 min = -32768,
return false;
}
NLMISC_COMMAND(random, "Roll a dice and say the result around","[<min>] <max>")
NLMISC_COMMAND(random, "Roll a dice and say the result around","[<min>] <max> [h|ide]")
{
// Check parameters.
if (args.size()<1 || args.size()>2)
if (args.size() < 1 || args.size() > 3)
return false;
sint16 min = 1;
sint16 max;
bool hide = args[args.size()-1][0] == 'h';
if (!randomFromString(args[0], max))
{
CInterfaceManager *pIM = CInterfaceManager::getInstance();
@ -491,13 +494,13 @@ NLMISC_COMMAND(random, "Roll a dice and say the result around","[<min>] <max>")
pIM->displaySystemInfo(msg);
return false;
}
if (args.size()==2)
if (args.size() > 1 && args[1][0] != 'h')
{
if (!randomFromString(args[1], min))
{
CInterfaceManager *pIM = CInterfaceManager::getInstance();
ucstring msg = CI18N::get("uiRandomBadParameter");
strFindReplace(msg, "%s", args[0] );
strFindReplace(msg, "%s", args[1] );
pIM->displaySystemInfo(msg);
return false;
}
@ -506,7 +509,7 @@ NLMISC_COMMAND(random, "Roll a dice and say the result around","[<min>] <max>")
std::swap(min, max);
if (UserEntity != NULL)
UserEntity->rollDice(min, max);
UserEntity->rollDice(min, max, hide);
return true;
}

View file

@ -3091,8 +3091,29 @@ void CUserEntity::setAFK(bool b, string afkTxt)
//-----------------------------------------------
// rollDice
//-----------------------------------------------
void CUserEntity::rollDice(sint16 min, sint16 max)
void CUserEntity::rollDice(sint16 min, sint16 max, bool local)
{
if (local)
{
// no need to broadcast over network here
static NLMISC::CRandom* dice = (NLMISC::CRandom*)NULL;
if (!dice)
{
dice = new NLMISC::CRandom;
dice->srand(CTickEventHandler::getGameCycle());
}
sint16 roll = min + (sint16)dice->rand(max-min);
ucstring msg = CI18N::get("msgRollDiceLocal");
strFindReplace(msg, "%min", std::to_string(min));
strFindReplace(msg, "%max", std::to_string(max));
strFindReplace(msg, "%roll", std::to_string(roll));
CInterfaceManager *pIM= CInterfaceManager::getInstance();
pIM->displaySystemInfo(msg, getStringCategory(msg, msg));
return;
}
const string msgName = "COMMAND:RANDOM";
CBitMemStream out;
if (GenericMsgHeaderMngr.pushNameToStream(msgName, out))

View file

@ -225,7 +225,7 @@ public:
void setAFK(bool b, std::string afkTxt="");
/// Roll a dice and tell the result around
void rollDice(sint16 min, sint16 max);
void rollDice(sint16 min, sint16 max, bool local);
/// return true if user can engage melee combat, else return false and display system msg
bool canEngageCombat();