2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
/** @file script_base.cpp Implementation of ScriptBase. */
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2011-01-22 11:33:16 +01:00
|
|
|
#include "../../stdafx.h"
|
2011-11-30 00:07:38 +01:00
|
|
|
#include "script_base.hpp"
|
2012-09-18 21:29:29 +02:00
|
|
|
#include "script_error.hpp"
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2014-04-23 22:13:33 +02:00
|
|
|
#include "../../safeguards.h"
|
|
|
|
|
2023-02-10 19:55:59 +01:00
|
|
|
/* static */ SQInteger ScriptBase::Rand()
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2023-01-13 23:48:59 +01:00
|
|
|
return ScriptObject::GetRandomizer().Next();
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2023-09-16 22:20:53 +02:00
|
|
|
/* static */ SQInteger ScriptBase::RandItem(SQInteger)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2011-11-30 00:15:35 +01:00
|
|
|
return ScriptBase::Rand();
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2023-02-10 19:55:59 +01:00
|
|
|
/* static */ SQInteger ScriptBase::RandRange(SQInteger max)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2023-02-10 19:55:59 +01:00
|
|
|
max = Clamp<SQInteger>(max, 0, UINT32_MAX);
|
2023-01-13 23:48:59 +01:00
|
|
|
return ScriptObject::GetRandomizer().Next(max);
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2023-09-16 22:20:53 +02:00
|
|
|
/* static */ SQInteger ScriptBase::RandRangeItem(SQInteger, SQInteger max)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2011-11-30 00:15:35 +01:00
|
|
|
return ScriptBase::RandRange(max);
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2023-02-10 19:55:59 +01:00
|
|
|
/* static */ bool ScriptBase::Chance(SQInteger out, SQInteger max)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2023-02-10 19:55:59 +01:00
|
|
|
out = Clamp<SQInteger>(out, 0, UINT32_MAX);
|
|
|
|
max = Clamp<SQInteger>(max, 0, UINT32_MAX);
|
2012-09-18 21:29:29 +02:00
|
|
|
EnforcePrecondition(false, out <= max);
|
2013-04-06 13:59:27 +02:00
|
|
|
return ScriptBase::RandRange(max) < out;
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2023-09-16 22:20:53 +02:00
|
|
|
/* static */ bool ScriptBase::ChanceItem(SQInteger, SQInteger out, SQInteger max)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2011-11-30 00:15:35 +01:00
|
|
|
return ScriptBase::Chance(out, max);
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|