feat: Implement math and dice rolling script routines

This commit is contained in:
Vsevolod Kremianskii 2020-11-15 17:57:41 +07:00
parent 28168f9a23
commit 2b89855ed6
4 changed files with 235 additions and 40 deletions

View file

@ -135,6 +135,36 @@ private:
script::Variable showPartySelectionGUI(const std::vector<script::Variable> &args, script::ExecutionContext &ctx); script::Variable showPartySelectionGUI(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
// END Party // END Party
// Math
script::Variable fabs(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
script::Variable cos(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
script::Variable sin(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
script::Variable tan(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
script::Variable acos(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
script::Variable asin(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
script::Variable atan(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
script::Variable log(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
script::Variable pow(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
script::Variable sqrt(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
script::Variable abs(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
// END Math
// Dice
script::Variable d2(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
script::Variable d3(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
script::Variable d4(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
script::Variable d6(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
script::Variable d8(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
script::Variable d10(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
script::Variable d12(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
script::Variable d20(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
script::Variable d100(const std::vector<script::Variable> &args, script::ExecutionContext &ctx);
// END Dice
}; };
} // namespace game } // namespace game

View file

@ -478,6 +478,171 @@ Variable Routines::showPartySelectionGUI(const vector<Variable> &args, Execution
return Variable(); return Variable();
} }
Variable Routines::fabs(const vector<Variable> &args, ExecutionContext &ctx) {
float value = args[0].floatValue;
return glm::abs(value);
}
Variable Routines::cos(const vector<Variable> &args, ExecutionContext &ctx) {
float value = args[0].floatValue;
return glm::cos(value);
}
Variable Routines::sin(const vector<Variable> &args, ExecutionContext &ctx) {
float value = args[0].floatValue;
return glm::sin(value);
}
Variable Routines::tan(const vector<Variable> &args, ExecutionContext &ctx) {
float value = args[0].floatValue;
return glm::tan(value);
}
Variable Routines::acos(const vector<Variable> &args, ExecutionContext &ctx) {
float value = args[0].floatValue;
if (value > 1 || value < -1) return 0.0f;
return glm::acos(value);
}
Variable Routines::asin(const vector<Variable> &args, ExecutionContext &ctx) {
float value = args[0].floatValue;
if (value > 1 || value < -1) return 0.0f;
return glm::asin(value);
}
Variable Routines::atan(const vector<Variable> &args, ExecutionContext &ctx) {
float value = args[0].floatValue;
return glm::atan(value);
}
Variable Routines::log(const vector<Variable> &args, ExecutionContext &ctx) {
float value = args[0].floatValue;
if (value <= 0.0f) return 0.0f;
return glm::log(value);
}
Variable Routines::pow(const vector<Variable> &args, ExecutionContext &ctx) {
float value = args[0].floatValue;
float exponent = args[1].floatValue;
if (value == 0.0f && exponent < 0.0f) return 0.0f;
return glm::pow(value, exponent);
}
Variable Routines::sqrt(const vector<Variable> &args, ExecutionContext &ctx) {
float value = args[0].floatValue;
if (value < 0.0f) return 0.0f;
return glm::sqrt(value);
}
Variable Routines::abs(const vector<Variable> &args, ExecutionContext &ctx) {
int value = args[0].intValue;
return glm::abs(value);
}
Variable Routines::d2(const vector<Variable> &args, ExecutionContext &ctx) {
int numDice = args.size() > 0 ? args[0].intValue : 1;
int result = 0;
for (int i = 0; i < numDice; ++i) {
result += reone::random(1, 2);
}
return result;
}
Variable Routines::d3(const vector<Variable> &args, ExecutionContext &ctx) {
int numDice = args.size() > 0 ? args[0].intValue : 1;
int result = 0;
for (int i = 0; i < numDice; ++i) {
result += reone::random(1, 3);
}
return result;
}
Variable Routines::d4(const vector<Variable> &args, ExecutionContext &ctx) {
int numDice = args.size() > 0 ? args[0].intValue : 1;
int result = 0;
for (int i = 0; i < numDice; ++i) {
result += reone::random(1, 4);
}
return result;
}
Variable Routines::d6(const vector<Variable> &args, ExecutionContext &ctx) {
int numDice = args.size() > 0 ? args[0].intValue : 1;
int result = 0;
for (int i = 0; i < numDice; ++i) {
result += reone::random(1, 6);
}
return result;
}
Variable Routines::d8(const vector<Variable> &args, ExecutionContext &ctx) {
int numDice = args.size() > 0 ? args[0].intValue : 1;
int result = 0;
for (int i = 0; i < numDice; ++i) {
result += reone::random(1, 8);
}
return result;
}
Variable Routines::d10(const vector<Variable> &args, ExecutionContext &ctx) {
int numDice = args.size() > 0 ? args[0].intValue : 1;
int result = 0;
for (int i = 0; i < numDice; ++i) {
result += reone::random(1, 10);
}
return result;
}
Variable Routines::d12(const vector<Variable> &args, ExecutionContext &ctx) {
int numDice = args.size() > 0 ? args[0].intValue : 1;
int result = 0;
for (int i = 0; i < numDice; ++i) {
result += reone::random(1, 12);
}
return result;
}
Variable Routines::d20(const vector<Variable> &args, ExecutionContext &ctx) {
int numDice = args.size() > 0 ? args[0].intValue : 1;
int result = 0;
for (int i = 0; i < numDice; ++i) {
result += reone::random(1, 20);
}
return result;
}
Variable Routines::d100(const vector<Variable> &args, ExecutionContext &ctx) {
int numDice = args.size() > 0 ? args[0].intValue : 1;
int result = 0;
for (int i = 0; i < numDice; ++i) {
result += reone::random(1, 100);
}
return result;
}
} // namespace game } // namespace game
} // namespace reone } // namespace reone

View file

@ -106,17 +106,17 @@ void Routines::addKotorRoutines() {
add("InsertString", String, { String, String, Int }); add("InsertString", String, { String, String, Int });
add("GetSubString", String, { String, Int, Int }); add("GetSubString", String, { String, Int, Int });
add("FindSubString", Int, { String, String }); add("FindSubString", Int, { String, String });
add("fabs", Float, { Float }); add("fabs", Float, { Float }, bind(&Routines::fabs, this, _1, _2));
add("cos", Float, { Float }); add("cos", Float, { Float }, bind(&Routines::cos, this, _1, _2));
add("sin", Float, { Float }); add("sin", Float, { Float }, bind(&Routines::sin, this, _1, _2));
add("tan", Float, { Float }); add("tan", Float, { Float }, bind(&Routines::tan, this, _1, _2));
add("acos", Float, { Float }); add("acos", Float, { Float }, bind(&Routines::acos, this, _1, _2));
add("asin", Float, { Float }); add("asin", Float, { Float }, bind(&Routines::asin, this, _1, _2));
add("atan", Float, { Float }); add("atan", Float, { Float }, bind(&Routines::atan, this, _1, _2));
add("log", Float, { Float }); add("log", Float, { Float }, bind(&Routines::log, this, _1, _2));
add("pow", Float, { Float, Float }); add("pow", Float, { Float, Float }, bind(&Routines::pow, this, _1, _2));
add("sqrt", Float, { Float }); add("sqrt", Float, { Float }, bind(&Routines::sqrt, this, _1, _2));
add("abs", Int, { Int }); add("abs", Int, { Int }, bind(&Routines::abs, this, _1, _2));
add("EffectHeal", Effect, { Int }); add("EffectHeal", Effect, { Int });
add("EffectDamage", Effect, { Int, Int, Int }); add("EffectDamage", Effect, { Int, Int, Int });
add("EffectAbilityIncrease", Effect, { Int, Int }); add("EffectAbilityIncrease", Effect, { Int, Int });
@ -134,15 +134,15 @@ void Routines::addKotorRoutines() {
add("IntToString", String, { Int }); add("IntToString", String, { Int });
add("GetFirstObjectInArea", Object, { Object, Int }); add("GetFirstObjectInArea", Object, { Object, Int });
add("GetNextObjectInArea", Object, { Object, Int }); add("GetNextObjectInArea", Object, { Object, Int });
add("d2", Int, { Int }); add("d2", Int, { Int }, bind(&Routines::d2, this, _1, _2));
add("d3", Int, { Int }); add("d3", Int, { Int }, bind(&Routines::d3, this, _1, _2));
add("d4", Int, { Int }); add("d4", Int, { Int }, bind(&Routines::d4, this, _1, _2));
add("d6", Int, { Int }); add("d6", Int, { Int }, bind(&Routines::d6, this, _1, _2));
add("d8", Int, { Int }); add("d8", Int, { Int }, bind(&Routines::d8, this, _1, _2));
add("d", Int, { Int }); add("d10", Int, { Int }, bind(&Routines::d10, this, _1, _2));
add("d12", Int, { Int }); add("d12", Int, { Int }, bind(&Routines::d12, this, _1, _2));
add("d", Int, { Int }); add("d20", Int, { Int }, bind(&Routines::d20, this, _1, _2));
add("d0", Int, { Int }); add("d100", Int, { Int }, bind(&Routines::d100, this, _1, _2));
add("VectorMagnitude", Float, { Vector }); add("VectorMagnitude", Float, { Vector });
add("GetMetaMagicFeat", Int, { }); add("GetMetaMagicFeat", Int, { });
add("GetObjectType", Int, { Object }); add("GetObjectType", Int, { Object });

View file

@ -106,17 +106,17 @@ void Routines::addTslRoutines() {
add("InsertString", String, { String, String, Int }); add("InsertString", String, { String, String, Int });
add("GetSubString", String, { String, Int, Int }); add("GetSubString", String, { String, Int, Int });
add("FindSubString", Int, { String, String }); add("FindSubString", Int, { String, String });
add("fabs", Float, { Float }); add("fabs", Float, { Float }, bind(&Routines::fabs, this, _1, _2));
add("cos", Float, { Float }); add("cos", Float, { Float }, bind(&Routines::cos, this, _1, _2));
add("sin", Float, { Float }); add("sin", Float, { Float }, bind(&Routines::sin, this, _1, _2));
add("tan", Float, { Float }); add("tan", Float, { Float }, bind(&Routines::tan, this, _1, _2));
add("acos", Float, { Float }); add("acos", Float, { Float }, bind(&Routines::acos, this, _1, _2));
add("asin", Float, { Float }); add("asin", Float, { Float }, bind(&Routines::asin, this, _1, _2));
add("atan", Float, { Float }); add("atan", Float, { Float }, bind(&Routines::atan, this, _1, _2));
add("log", Float, { Float }); add("log", Float, { Float }, bind(&Routines::log, this, _1, _2));
add("pow", Float, { Float, Float }); add("pow", Float, { Float, Float }, bind(&Routines::pow, this, _1, _2));
add("sqrt", Float, { Float }); add("sqrt", Float, { Float }, bind(&Routines::sqrt, this, _1, _2));
add("abs", Int, { Int }); add("abs", Int, { Int }, bind(&Routines::abs, this, _1, _2));
add("EffectHeal", Effect, { Int }); add("EffectHeal", Effect, { Int });
add("EffectDamage", Effect, { Int, Int, Int }); add("EffectDamage", Effect, { Int, Int, Int });
add("EffectAbilityIncrease", Effect, { Int, Int }); add("EffectAbilityIncrease", Effect, { Int, Int });
@ -134,15 +134,15 @@ void Routines::addTslRoutines() {
add("IntToString", String, { Int }); add("IntToString", String, { Int });
add("GetFirstObjectInArea", Object, { Object, Int }); add("GetFirstObjectInArea", Object, { Object, Int });
add("GetNextObjectInArea", Object, { Object, Int }); add("GetNextObjectInArea", Object, { Object, Int });
add("d2", Int, { Int }); add("d2", Int, { Int }, bind(&Routines::d2, this, _1, _2));
add("d3", Int, { Int }); add("d3", Int, { Int }, bind(&Routines::d3, this, _1, _2));
add("d4", Int, { Int }); add("d4", Int, { Int }, bind(&Routines::d4, this, _1, _2));
add("d6", Int, { Int }); add("d6", Int, { Int }, bind(&Routines::d6, this, _1, _2));
add("d8", Int, { Int }); add("d8", Int, { Int }, bind(&Routines::d8, this, _1, _2));
add("d10", Int, { Int }); add("d10", Int, { Int }, bind(&Routines::d10, this, _1, _2));
add("d12", Int, { Int }); add("d12", Int, { Int }, bind(&Routines::d12, this, _1, _2));
add("d20", Int, { Int }); add("d20", Int, { Int }, bind(&Routines::d20, this, _1, _2));
add("d100", Int, { Int }); add("d100", Int, { Int }, bind(&Routines::d100, this, _1, _2));
add("VectorMagnitude", Float, { Vector }); add("VectorMagnitude", Float, { Vector });
add("GetMetaMagicFeat", Int, { }); add("GetMetaMagicFeat", Int, { });
add("GetObjectType", Int, { Object }); add("GetObjectType", Int, { Object });