Skip to content

Commit

Permalink
Fix random with 64 bit values. Fix number_to_hex_string with int64 also.
Browse files Browse the repository at this point in the history
  • Loading branch information
m1maker committed Oct 10, 2024
1 parent f7ad6f2 commit c9f02b7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Release/Include/hip.as
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ namespace hip
}

double convert_month_number(const string&in monthname) {
for (int i = 0; i < MONTH_NAMES.length(); i++)
for (uint i = 0; i < MONTH_NAMES.length(); i++)
{
if (monthname == MONTH_NAMES[i])
{
Expand All @@ -196,7 +196,7 @@ namespace hip
}

double convert_weekday_number(const string&in weekdayname) {
for (int i = 0; i < WEEKDAY_NAMES.length(); i++)
for (uint i = 0; i < WEEKDAY_NAMES.length(); i++)
{
if (weekdayname == WEEKDAY_NAMES[i])
{
Expand Down
27 changes: 19 additions & 8 deletions SRC/ngt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,11 @@ void set_library_path(const string& path) {

random_device rd;
mt19937 gen(rd());
long random(long min, long max) {
static_assert(is_integral<long>::value, "Type must be integral");

uniform_int_distribution<long> dis(min, max);

return dis(gen); // Ensure 'gen' is a valid random number generator
int64_t random(int64_t min, int64_t max) {
uniform_int_distribution<int64_t> dis(min, max); // Use int64_t
return dis(gen);
}

double randomDouble(double min, double max) {
static_assert(is_floating_point<double>::value, "Type must be floating point");

Expand Down Expand Up @@ -855,8 +853,8 @@ string hex_to_string(string the_hexadecimal_sequence) {
}


string number_to_hex_string(double the_number) {
return Poco::NumberFormatter::formatHex(static_cast<asUINT>(the_number));
string number_to_hex_string(asINT64 the_number) {
return Poco::NumberFormatter::formatHex(the_number);
}

string string_base64_decode(string base64_string) {
Expand Down Expand Up @@ -951,6 +949,19 @@ int message_box(const std::string& title, const std::string& text, const std::ve
return ret;
}


int message_box_script(const std::string& title, const std::string& text, CScriptArray* buttons, unsigned int flags) {
const unsigned int buttonCount = buttons->GetSize();
std::vector<std::string> v_buttons;
v_buttons.reserve(buttonCount); // Reserve space for efficiency

for (unsigned int i = 0; i < buttonCount; ++i) {
v_buttons.emplace_back(*(std::string*)(buttons->At(i))); // Use emplace_back for better performance
}

return message_box(title, text, v_buttons, flags);
}

bool alert(const string& title, const string& text, const string& button_name)
{
return message_box(title, text, { "`OK" }, 0) == 0;
Expand Down
6 changes: 4 additions & 2 deletions SRC/ngt.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void set_update_window_freq(long);
long get_update_window_freq();
void init_engine();
void set_library_path(const string& path);
long random(long min, long max);
int64_t random(int64_t min, int64_t max);
double randomDouble(double min, double max);
bool random_bool();
int get_last_error();
Expand Down Expand Up @@ -164,7 +164,7 @@ void mail_send(Poco::Net::SMTPClientSession::LoginMethod login_method, asUINT po
string ascii_to_character(int the_ascii_code);
int character_to_ascii(string the_character);
string hex_to_string(string the_hexadecimal_sequence);
string number_to_hex_string(double the_number);
string number_to_hex_string(asINT64 the_number);
string string_base64_decode(string base64_string);
string string_base64_encode(string the_string);
string string_base32_decode(string base32_string);
Expand All @@ -174,6 +174,8 @@ void string_pad(std::string&);
void string_unpad(std::string&);
int message_box(const std::string& title, const std::string& text, const std::vector<std::string>& buttons, unsigned int mb_flags);

int message_box_script(const std::string& title, const std::string& text, CScriptArray* buttons, unsigned int flags);

bool alert(const string& title, const string& text, const string& button_name = "OK");
int question(const string& title, const string& text);
void wait(uint64_t);
Expand Down
12 changes: 10 additions & 2 deletions SRC/ngtreg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ void RegisterFunctions(asIScriptEngine* engine)
engine->RegisterGlobalFunction("uint64 get_time_stamp_seconds()property", asFUNCTION(get_time_stamp_seconds), asCALL_CDECL);
engine->RegisterGlobalFunction("void set_library_path(const string &in)", asFUNCTION(set_library_path), asCALL_CDECL);

engine->RegisterGlobalFunction("long random(long, long)", asFUNCTIONPR(random, (long, long), long), asCALL_CDECL);
engine->RegisterGlobalFunction("long random(long, long)", asFUNCTIONPR(random, (int64_t, int64_t), int64_t), asCALL_CDECL);
engine->RegisterGlobalFunction("double random(double, double)", asFUNCTIONPR(randomDouble, (double, double), double), asCALL_CDECL);
engine->RegisterGlobalFunction("bool random_bool()", asFUNCTION(random_bool), asCALL_CDECL);

Expand Down Expand Up @@ -391,12 +391,20 @@ void RegisterFunctions(asIScriptEngine* engine)
engine->RegisterGlobalFunction("string ascii_to_character(int)", asFUNCTION(ascii_to_character), asCALL_CDECL);
engine->RegisterGlobalFunction("int character_to_ascii(const string &in)", asFUNCTION(character_to_ascii), asCALL_CDECL);
engine->RegisterGlobalFunction("string hex_to_string(const string& in)", asFUNCTION(hex_to_string), asCALL_CDECL);
engine->RegisterGlobalFunction("string number_to_hex_string(double)", asFUNCTION(number_to_hex_string), asCALL_CDECL);
engine->RegisterGlobalFunction("string number_to_hex_string(int64)", asFUNCTION(number_to_hex_string), asCALL_CDECL);
engine->RegisterGlobalFunction("string string_base64_decode(const string &in)", asFUNCTION(string_base64_decode), asCALL_CDECL);
engine->RegisterGlobalFunction("string string_base64_encode(const string &in)", asFUNCTION(string_base64_encode), asCALL_CDECL);
engine->RegisterGlobalFunction("string string_base32_decode(const string &in)", asFUNCTION(string_base32_decode), asCALL_CDECL);
engine->RegisterGlobalFunction("string string_base32_encode(const string &in)", asFUNCTION(string_base32_encode), asCALL_CDECL);
engine->RegisterGlobalFunction("string string_to_hex(const string &in)", asFUNCTION(string_to_hex), asCALL_CDECL);
engine->RegisterEnum(_O("message_box_flags"));
engine->RegisterEnumValue(_O("message_box_flags"), _O("MESSAGE_BOX_ERROR"), SDL_MESSAGEBOX_ERROR);
engine->RegisterEnumValue(_O("message_box_flags"), _O("MESSAGE_BOX_WARNING"), SDL_MESSAGEBOX_WARNING);
engine->RegisterEnumValue(_O("message_box_flags"), _O("MESSAGE_BOX_INFORMATION"), SDL_MESSAGEBOX_INFORMATION);
engine->RegisterEnumValue(_O("message_box_flags"), _O("MESSAGE_BOX_BUTTONS_LEFT_TO_RIGHT"), SDL_MESSAGEBOX_BUTTONS_LEFT_TO_RIGHT);
engine->RegisterEnumValue(_O("message_box_flags"), _O("MESSAGE_BOX_BUTTONS_RIGHT_TO_LEFT"), SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT);
engine->RegisterGlobalFunction("int message_box(const string& in, const string& in, array<string>@, uint = 0)", asFUNCTION(message_box_script), asCALL_CDECL);

engine->RegisterGlobalFunction("bool alert(const string &in, const string &in, const string &in=\"OK\")", asFUNCTION(alert), asCALL_CDECL);
engine->RegisterGlobalFunction("int question(const string &in, const string &in)", asFUNCTION(question), asCALL_CDECL);

Expand Down

0 comments on commit c9f02b7

Please sign in to comment.