Skip to content

Commit

Permalink
Added pick_random_X opcodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
MiranDMC committed Nov 24, 2024
1 parent ad99b56 commit 388895f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
- new opcode **2702 ([clear_bit](https://library.sannybuilder.com/#/sa/math/2702))**
- new opcode **2703 ([toggle_bit](https://library.sannybuilder.com/#/sa/math/2703))**
- new opcode **2704 ([is_truthy](https://library.sannybuilder.com/#/sa/math/2704))**
- new opcode **2705 ([pick_random_int](https://library.sannybuilder.com/#/sa/math/2705))**
- new opcode **2706 ([pick_random_float](https://library.sannybuilder.com/#/sa/math/2706))**
- new opcode **2707 ([pick_random_text](https://library.sannybuilder.com/#/sa/math/2707))**
- new [MemoryOperations](https://github.com/cleolibrary/CLEO5/tree/master/cleo_plugins/MemoryOperations) plugin
- memory related opcodes moved from CLEO core into separated plugin
- validation of input and output parameters for all opcodes
Expand Down
81 changes: 81 additions & 0 deletions cleo_plugins/Math/Math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class Math
CLEO_RegisterOpcode(0x2702, opcode_2702); // clear_bit
CLEO_RegisterOpcode(0x2703, opcode_2703); // toggle_bit
CLEO_RegisterOpcode(0x2704, opcode_2704); // is_truthy
CLEO_RegisterOpcode(0x2705, opcode_2705); // pick_random_int
CLEO_RegisterOpcode(0x2706, opcode_2706); // pick_random_float
CLEO_RegisterOpcode(0x2707, opcode_2707); // pick_random_text
}

//0098=1,generate_random_float
static OpcodeResult WINAPI opcode_0098(CRunningScript* thread)
Expand Down Expand Up @@ -495,4 +499,81 @@ class Math
OPCODE_CONDITION_RESULT(value != 0);
return OR_CONTINUE;
}

//2705=-1,pick_random_int values %d% store_to %d%
static OpcodeResult WINAPI opcode_2705(CScriptThread* thread)
{
auto valueCount = CLEO_GetVarArgCount(thread);

if (valueCount < 2) // value + result
{
SHOW_ERROR("Insufficient number of arguments in script %s\nScript suspended.", CLEO::ScriptInfoStr(thread).c_str());
return thread->Suspend();
}
valueCount -= 1; // output param

uniform_int_distribution<size_t> dist(0, valueCount - 1); // 0 to last index
auto idx = dist(Instance.randomGenerator);

// read n-th param
OPCODE_SKIP_PARAMS(idx);
auto value = OPCODE_READ_PARAM_INT();

OPCODE_SKIP_PARAMS(valueCount - (idx + 1)); // seek to output param
OPCODE_WRITE_PARAM_INT(value);

CLEO_SkipUnusedVarArgs(thread); // var args terminator
return OR_CONTINUE;
}

//2706=-1,pick_random_float values %d% store_to %d%
static OpcodeResult WINAPI opcode_2706(CScriptThread* thread)
{
auto valueCount = CLEO_GetVarArgCount(thread);

if (valueCount < 2) // value + result
{
SHOW_ERROR("Insufficient number of arguments in script %s\nScript suspended.", CLEO::ScriptInfoStr(thread).c_str());
return thread->Suspend();
}
valueCount -= 1; // output param

uniform_int_distribution<size_t> dist(0, valueCount - 1); // 0 to last index
auto idx = dist(Instance.randomGenerator);

// read n-th param
OPCODE_SKIP_PARAMS(idx);
auto value = OPCODE_READ_PARAM_FLOAT();

OPCODE_SKIP_PARAMS(valueCount - (idx + 1)); // seek to output param
OPCODE_WRITE_PARAM_FLOAT(value);

return OR_CONTINUE;
}

//2707=-1,pick_random_text values %d% store_to %d%
static OpcodeResult WINAPI opcode_2707(CScriptThread* thread)
{
auto valueCount = CLEO_GetVarArgCount(thread);

if (valueCount < 2) // value + result
{
SHOW_ERROR("Insufficient number of arguments in script %s\nScript suspended.", CLEO::ScriptInfoStr(thread).c_str());
return thread->Suspend();
}
valueCount -= 1; // output param

uniform_int_distribution<size_t> dist(0, valueCount - 1); // 0 to last index
auto idx = dist(Instance.randomGenerator);

// read n-th param
OPCODE_SKIP_PARAMS(idx);
OPCODE_READ_PARAM_STRING(value);

OPCODE_SKIP_PARAMS(valueCount - (idx + 1)); // seek to output param
OPCODE_WRITE_PARAM_STRING(value);

CLEO_SkipUnusedVarArgs(thread); // var args terminator
return OR_CONTINUE;
}
} Instance;

0 comments on commit 388895f

Please sign in to comment.