Add script animation type

This commit is contained in:
Federico Cecchetto 2022-06-19 20:56:15 +02:00
parent 16f181bda4
commit 6d3f032ae8
8 changed files with 230 additions and 3 deletions

View File

@ -0,0 +1,17 @@
#include <std_include.hpp>
#include "array.hpp"
#include "script_value.hpp"
#include "execution.hpp"
namespace scripting
{
animation::animation(unsigned int value)
: value_(value)
{
}
uint64_t animation::get_value() const
{
return this->value_;
}
}

View File

@ -0,0 +1,16 @@
#pragma once
#include "game/game.hpp"
#include "script_value.hpp"
namespace scripting
{
class animation final
{
public:
animation(unsigned int value);
uint64_t get_value() const;
private:
uint64_t value_;
};
}

View File

@ -2,6 +2,7 @@
#include "game/game.hpp"
#include "entity.hpp"
#include "array.hpp"
#include "animation.hpp"
#include "script_value.hpp"
namespace scripting

View File

@ -190,6 +190,130 @@ namespace scripting::lua
state["level"] = entity{*::game::levelEntityId};
state["player"] = call("getentbynum", {0}).as<entity>();
auto animation_type = state.new_usertype<animation>("animation");
auto array_type = state.new_usertype<array>("array", sol::constructors<array()>());
array_type["erase"] = [](const array& array, const sol::this_state s,
const sol::lua_value& key)
{
if (key.is<int>())
{
const auto index = key.as<int>() - 1;
array.erase(index);
}
else if (key.is<std::string>())
{
const auto _key = key.as<std::string>();
array.erase(_key);
}
};
array_type["push"] = [](const array& array, const sol::this_state s,
const sol::lua_value& value)
{
const auto _value = convert(value);
array.push(_value);
};
array_type["pop"] = [](const array& array, const sol::this_state s)
{
return convert(s, array.pop());
};
array_type["get"] = [](const array& array, const sol::this_state s,
const sol::lua_value& key)
{
if (key.is<int>())
{
const auto index = key.as<int>() - 1;
return convert(s, array.get(index));
}
else if (key.is<std::string>())
{
const auto _key = key.as<std::string>();
return convert(s, array.get(_key));
}
return sol::lua_value{s, sol::lua_nil};
};
array_type["set"] = [](const array& array, const sol::this_state s,
const sol::lua_value& key, const sol::lua_value& value)
{
const auto _value = convert(value);
const auto nil = _value.get_raw().type == 0;
if (key.is<int>())
{
const auto index = key.as<int>() - 1;
nil ? array.erase(index) : array.set(index, _value);
}
else if (key.is<std::string>())
{
const auto _key = key.as<std::string>();
nil ? array.erase(_key) : array.set(_key, _value);
}
};
array_type["size"] = [](const array& array, const sol::this_state s)
{
return array.size();
};
array_type[sol::meta_function::length] = [](const array& array, const sol::this_state s)
{
return array.size();
};
array_type[sol::meta_function::index] = [](const array& array, const sol::this_state s,
const sol::lua_value& key)
{
if (key.is<int>())
{
const auto index = key.as<int>() - 1;
return convert(s, array.get(index));
}
else if (key.is<std::string>())
{
const auto _key = key.as<std::string>();
return convert(s, array.get(_key));
}
return sol::lua_value{s, sol::lua_nil};
};
array_type[sol::meta_function::new_index] = [](const array& array, const sol::this_state s,
const sol::lua_value& key, const sol::lua_value& value)
{
const auto _value = convert(value);
const auto nil = _value.get_raw().type == 0;
if (key.is<int>())
{
const auto index = key.as<int>() - 1;
nil ? array.erase(index) : array.set(index, _value);
}
else if (key.is<std::string>())
{
const auto _key = key.as<std::string>();
nil ? array.erase(_key) : array.set(_key, _value);
}
};
array_type["getkeys"] = [](const array& array, const sol::this_state s)
{
std::vector<sol::lua_value> keys;
const auto keys_ = array.get_keys();
for (const auto& key : keys_)
{
keys.push_back(convert(s, key));
}
return keys;
};
auto entity_type = state.new_usertype<entity>("entity");
for (const auto& func : method_map)
@ -592,6 +716,28 @@ namespace scripting::lua
return sol::lua_value{s, sol::lua_nil};
}
};
game_type["getvarusage"] = [](const game&)
{
auto count = 0;
for (auto i = 0; i < 56320; i++)
{
const auto value = ::game::scr_VarGlob->objectVariableValue[i];
count += value.w.type != 24;
}
return count;
};
game_type["getchildvarusage"] = [](const game&)
{
auto count = 0;
for (auto i = 0; i < 384000; i++)
{
const auto value = ::game::scr_VarGlob->childVariableValue[i];
count += value.type != 24;
}
return count;
};
}
}
@ -657,7 +803,10 @@ namespace scripting::lua
{
};
setup_io(this->state_);
setup_vector_type(this->state_);
setup_entity_type(this->state_, this->event_handler_, this->scheduler_);
setup_game_type(this->state_, this->event_handler_, this->scheduler_);
}
std::string context::load(const std::string& code)

View File

@ -198,7 +198,7 @@ namespace scripting::lua
const auto variable = &game::scr_VarGlob->childVariableValue[variable_id + offset];
const auto new_variable = convert({ s, value }).get_raw();
const auto new_variable = convert({s, value}).get_raw();
game::AddRefToValue(new_variable.type, new_variable.u);
game::RemoveRefToValue(variable->type, variable->u.u);
@ -276,6 +276,11 @@ namespace scripting::lua
return {value.as<std::string>()};
}
if (value.is<array>())
{
return {value.as<array>()};
}
if (value.is<entity>())
{
return {value.as<entity>()};
@ -286,6 +291,11 @@ namespace scripting::lua
return {value.as<vector>()};
}
if (value.is<animation>())
{
return {value.as<animation>()};
}
return {};
}
@ -311,9 +321,9 @@ namespace scripting::lua
return entity_to_struct(state, value.get_raw().u.uintValue);
}
if (value.is<std::vector<script_value>>())
if (value.is<array>())
{
return entity_to_array(state, value.get_raw().u.uintValue);
return {state, value.as<array>()};
}
if (value.is<std::function<void()>>())
@ -331,6 +341,11 @@ namespace scripting::lua
return {state, value.as<vector>()};
}
if (value.is<animation>())
{
return {state, value.as<animation>()};
}
return {state, sol::lua_nil};
}
}

View File

@ -104,6 +104,15 @@ namespace scripting
this->value_ = variable;
}
script_value::script_value(const animation& value)
{
game::VariableValue variable{};
variable.type = 13;
variable.u.value = value.get_value();
this->value_ = variable;
}
/***************************************************************
* Integer
**************************************************************/
@ -302,6 +311,22 @@ namespace scripting
return this->get_raw().u.vectorValue;
}
/***************************************************************
* Animation
**************************************************************/
template <>
bool script_value::is<animation>() const
{
return this->get_raw().type == 13;
}
template <>
animation script_value::get() const
{
return this->get_raw().u.uintValue;
}
/***************************************************************
*
**************************************************************/

View File

@ -2,6 +2,7 @@
#include "game/game.hpp"
#include "variable_value.hpp"
#include "vector.hpp"
#include "animation.hpp"
namespace scripting
{
@ -29,6 +30,8 @@ namespace scripting
script_value(const vector& value);
script_value(const animation& value);
template <typename T>
bool is() const;

View File

@ -859,6 +859,7 @@ namespace game
unsigned int pointerValue;
VariableStackBuffer* stackValue;
unsigned int entityOffset;
uint64_t value;
};
struct VariableValue