Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 51 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,61 @@ std::random_device rd;
std::mt19937 gen;

int randomNumber(lua_State* state) {
if (LUA->GetType(1) != GarrysMod::Lua::Type::NIL) {
LUA->CheckType(1, GarrysMod::Lua::Type::NUMBER);
LUA->CheckType(2, GarrysMod::Lua::Type::NUMBER);
if (LUA->GetType(1) != GarrysMod::Lua::Type::NIL) { //Check if we have the first argument
LUA->CheckType(1, GarrysMod::Lua::Type::NUMBER); //Verify the first argument is a number

std::uniform_real_distribution<double> dist(LUA->GetNumber(1), LUA->GetNumber(2));
LUA->PushNumber(dist(gen));
if (LUA->GetType(2) != GarrysMod::Lua::Type::NIL) { //Check if we have the second argument
LUA->CheckType(2, GarrysMod::Lua::Type::NUMBER); //Verify the second argument is a number

//Push our random number between the first and second argument
std::uniform_real_distribution<double> dist(LUA->GetNumber(1), LUA->GetNumber(2));
LUA->PushNumber(dist(gen));
} else {
//Added in by The Owl Cafe
//Purpose: To make it behave fully like the math.random function in glua, effectively allowing it to fully replace math.random

//We only have the first argument
//We return a number between 1 and the second argument
std::uniform_real_distribution<double> dist( 1.0, LUA->GetNumber(1));
LUA->PushNumber(dist(gen));
}
} else {
//Return a random number between 0 and 1
std::uniform_real_distribution<double> dist;
LUA->PushNumber(dist(gen));
}

return 1;
}

int randomWholeNumber(lua_State* state) {
if (LUA->GetType(1) != GarrysMod::Lua::Type::NIL) { //Check if we have the first argument
LUA->CheckType(1, GarrysMod::Lua::Type::NUMBER); //Verify the first argument is a number

if (LUA->GetType(2) != GarrysMod::Lua::Type::NIL) { //Check if we have the second argument
LUA->CheckType(2, GarrysMod::Lua::Type::NUMBER); //Verify the second argument is a number

//Push our random number between the first and second argument
std::uniform_int_distribution<int> dist(LUA->GetNumber(1), LUA->GetNumber(2));
LUA->PushNumber(dist(gen));
} else {
//Added in by The Owl Cafe
//Purpose: To make it behave fully like the math.random function in glua, effectively allowing it to fully replace math.random

//We only have the first argument
//We return a number between 1 and the second argument
std::uniform_int_distribution<int> dist( 1, LUA->GetNumber(1));
LUA->PushNumber(dist(gen));
}
} else {
//Return a random number between 0 and 1
std::uniform_int_distribution<int> dist;
LUA->PushNumber(dist(gen));
}

return 1;
}

int randomBytes(lua_State* state) {
LUA->CheckType(1, GarrysMod::Lua::Type::NUMBER);
const double requestedSize = LUA->GetNumber(1);
Expand Down Expand Up @@ -51,6 +92,10 @@ GMOD_MODULE_OPEN() {
LUA->CreateTable();
LUA->PushCFunction(randomNumber);
LUA->SetField(-2, "Number");

//Same as random.Number, but should provide a whole number instead of a decimal
LUA->PushCFunction(randomWholeNumber);
LUA->SetField(-2, "WholeNumber");

LUA->PushCFunction(randomBytes);
LUA->SetField(-2, "Bytes");
Expand All @@ -61,4 +106,4 @@ GMOD_MODULE_OPEN() {

GMOD_MODULE_CLOSE() {
return 0;
}
}