Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BasicTypes overload operators #916

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
70 changes: 70 additions & 0 deletions src/reverse/BasicTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,41 @@ bool Vector3::operator==(const Vector3& acRhs) const noexcept
return x == acRhs.x && y == acRhs.y && z == acRhs.z;
}

bool Vector3::operator<(const Vector3& acRhs) const noexcept
{
return x < acRhs.x && y < acRhs.y && z < acRhs.z;
}

bool Vector3::operator<=(const Vector3& acRhs) const noexcept
{
return x <= acRhs.x && y <= acRhs.y && z <= acRhs.z;
}

Vector3 Vector3::operator+(const Vector3& acRhs) const noexcept
{
return Vector3(x + acRhs.x, y + acRhs.y, z + acRhs.z);
}

Vector3 Vector3::operator-(const Vector3& acRhs) const noexcept
{
return Vector3(x - acRhs.x, y - acRhs.y, z - acRhs.z);
}

Vector3 Vector3::operator*(const Vector3& acRhs) const noexcept
{
return Vector3(x * acRhs.x, y * acRhs.y, z * acRhs.z);
}

Vector3 Vector3::operator/(const Vector3& acRhs) const noexcept
{
if (acRhs.x == 0 || acRhs.y == 0 || acRhs.z == 0)
{
CET::Get().GetVM().ThrowLuaError("Divide by zero exception");
return acRhs;
}
return Vector3(x / acRhs.x, y / acRhs.y, z / acRhs.z);
}

std::string Vector4::ToString() const noexcept
{
return fmt::format("ToVector4{{ x = {}, y = {}, z = {}, w = {} }}", x, y, z, w);
Expand All @@ -26,6 +61,41 @@ bool Vector4::operator==(const Vector4& acRhs) const noexcept
return x == acRhs.x && y == acRhs.y && z == acRhs.z && w == acRhs.w;
}

bool Vector4::operator<(const Vector4& acRhs) const noexcept
{
return x < acRhs.x && y < acRhs.y && z < acRhs.z && w < acRhs.w;
}

bool Vector4::operator<=(const Vector4& acRhs) const noexcept
{
return x <= acRhs.x && y <= acRhs.y && z <= acRhs.z && w <= acRhs.w;
}

Vector4 Vector4::operator+(const Vector4& acRhs) const noexcept
{
return Vector4(x + acRhs.x, y + acRhs.y, z + acRhs.z, w + acRhs.w);
}

Vector4 Vector4::operator-(const Vector4& acRhs) const noexcept
{
return Vector4(x - acRhs.x, y - acRhs.y, z - acRhs.z, w - acRhs.w);
}

Vector4 Vector4::operator*(const Vector4& acRhs) const noexcept
{
return Vector4(x * acRhs.x, y * acRhs.y, z * acRhs.z, w * acRhs.w);
}

Vector4 Vector4::operator/(const Vector4& acRhs) const noexcept
{
if (acRhs.x == 0 || acRhs.y == 0 || acRhs.z == 0 || acRhs.w == 0)
{
CET::Get().GetVM().ThrowLuaError("Divide by zero exception");
return acRhs;
}
return Vector4(x / acRhs.x, y / acRhs.y, z / acRhs.z, w / acRhs.w);
}

std::string EulerAngles::ToString() const noexcept
{
return fmt::format("ToEulerAngles{{ roll = {}, pitch = {}, yaw = {} }}", roll, pitch, yaw);
Expand Down
12 changes: 12 additions & 0 deletions src/reverse/BasicTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ struct Vector3
std::string ToString() const noexcept;

bool operator==(const Vector3& acRhs) const noexcept;
bool operator<(const Vector3& acRhs) const noexcept;
bool operator<=(const Vector3& acRhs) const noexcept;
Vector3 operator+(const Vector3& acRhs) const noexcept;
Vector3 operator-(const Vector3& acRhs) const noexcept;
Vector3 operator*(const Vector3& acRhs) const noexcept;
Vector3 operator/(const Vector3& acRhs) const noexcept;
};

struct Vector4
Expand All @@ -36,6 +42,12 @@ struct Vector4
std::string ToString() const noexcept;

bool operator==(const Vector4& acRhs) const noexcept;
bool operator<(const Vector4& acRhs) const noexcept;
bool operator<=(const Vector4& acRhs) const noexcept;
Vector4 operator+(const Vector4& acRhs) const noexcept;
Vector4 operator-(const Vector4& acRhs) const noexcept;
Vector4 operator*(const Vector4& acRhs) const noexcept;
Vector4 operator/(const Vector4& acRhs) const noexcept;
};

struct EulerAngles
Expand Down
7 changes: 7 additions & 0 deletions src/scripting/LuaVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ bool LuaVM::ExecuteLua(const std::string& acCommand) const
return m_scripting.ExecuteLua(acCommand);
}

int LuaVM::ThrowLuaError(const std::string& acMessage) const
{
lua_State* state = m_scripting.GetLockedState().Get().lua_state();

return luaL_error(state, "%s", acMessage.c_str());
}

void LuaVM::Update(float aDeltaTime)
{
if (!m_initialized)
Expand Down
1 change: 1 addition & 0 deletions src/scripting/LuaVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct LuaVM
[[nodiscard]] const TiltedPhoques::Map<std::string, std::reference_wrapper<const TiltedPhoques::Vector<VKBind>>>& GetAllBinds() const;

bool ExecuteLua(const std::string& acCommand) const;
int ThrowLuaError(const std::string& acMessage) const;

void Update(float aDeltaTime);
void Draw() const;
Expand Down
46 changes: 41 additions & 5 deletions src/scripting/Scripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,26 @@ void Scripting::PostInitializeScripting()
};

luaVm.new_usertype<Vector3>(
"Vector3", sol::constructors<Vector3(float, float, float), Vector3(float, float), Vector3(float), Vector3(const Vector3&), Vector3()>(), sol::meta_function::to_string,
&Vector3::ToString, sol::meta_function::equal_to, &Vector3::operator==, "x", &Vector3::x, "y", &Vector3::y, "z", &Vector3::z);
"Vector3",
sol::constructors<
Vector3(float, float, float),
Vector3(float, float),
Vector3(float),
Vector3(const Vector3&),
Vector3()
>(),
sol::meta_function::to_string, &Vector3::ToString,
sol::meta_function::equal_to, &Vector3::operator==,
sol::meta_function::less_than, &Vector3::operator<,
sol::meta_function::less_than_or_equal_to, &Vector3::operator<=,
sol::meta_function::addition, &Vector3::operator+,
sol::meta_function::subtraction, &Vector3::operator-,
sol::meta_function::multiplication, &Vector3::operator*,
sol::meta_function::division, &Vector3::operator/,
"x", &Vector3::x,
"y", &Vector3::y,
"z", &Vector3::z
);
globals["Vector3"] = luaVm["Vector3"];

globals["ToVector3"] = [](sol::table table) -> Vector3
Expand All @@ -242,9 +260,27 @@ void Scripting::PostInitializeScripting()

luaVm.new_usertype<Vector4>(
"Vector4",
sol::constructors<Vector4(float, float, float, float), Vector4(float, float, float), Vector4(float, float), Vector4(float), Vector4(const Vector4&), Vector4()>(),
sol::meta_function::to_string, &Vector4::ToString, sol::meta_function::equal_to, &Vector4::operator==, "x", &Vector4::x, "y", &Vector4::y, "z", &Vector4::z, "w",
&Vector4::w);
sol::constructors<
Vector4(float, float, float, float),
Vector4(float, float, float),
Vector4(float, float),
Vector4(float),
Vector4(const Vector4&),
Vector4()
>(),
sol::meta_function::to_string, &Vector4::ToString,
sol::meta_function::equal_to, &Vector4::operator==,
sol::meta_function::less_than, &Vector4::operator<,
sol::meta_function::less_than_or_equal_to, &Vector4::operator<=,
sol::meta_function::addition, &Vector4::operator+,
sol::meta_function::subtraction, &Vector4::operator-,
sol::meta_function::multiplication, &Vector4::operator*,
sol::meta_function::division, &Vector4::operator/,
"x", &Vector4::x,
"y", &Vector4::y,
"z", &Vector4::z,
"w", &Vector4::w
);
globals["Vector4"] = luaVm["Vector4"];

globals["ToVector4"] = [](sol::table table) -> Vector4
Expand Down
Loading