Scripting changes + SP support
This commit is contained in:
@ -145,47 +145,6 @@ namespace scripting
|
||||
return exec_ent_thread(entity, pos, arguments);
|
||||
}
|
||||
|
||||
static std::unordered_map<unsigned int, std::unordered_map<std::string, script_value>> custom_fields;
|
||||
|
||||
script_value get_custom_field(const entity& entity, const std::string& field)
|
||||
{
|
||||
auto& fields = custom_fields[entity.get_entity_id()];
|
||||
const auto _field = fields.find(field);
|
||||
if (_field != fields.end())
|
||||
{
|
||||
return _field->second;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void set_custom_field(const entity& entity, const std::string& field, const script_value& value)
|
||||
{
|
||||
const auto id = entity.get_entity_id();
|
||||
|
||||
if (custom_fields[id].find(field) != custom_fields[id].end())
|
||||
{
|
||||
custom_fields[id][field] = value;
|
||||
return;
|
||||
}
|
||||
|
||||
custom_fields[id].insert(std::make_pair(field, value));
|
||||
}
|
||||
|
||||
void clear_entity_fields(const entity& entity)
|
||||
{
|
||||
const auto id = entity.get_entity_id();
|
||||
|
||||
if (custom_fields.find(id) != custom_fields.end())
|
||||
{
|
||||
custom_fields[id].clear();
|
||||
}
|
||||
}
|
||||
|
||||
void clear_custom_fields()
|
||||
{
|
||||
custom_fields.clear();
|
||||
}
|
||||
|
||||
void set_entity_field(const entity& entity, const std::string& field, const script_value& value)
|
||||
{
|
||||
const auto entref = entity.get_entity_reference();
|
||||
@ -206,8 +165,7 @@ namespace scripting
|
||||
}
|
||||
else
|
||||
{
|
||||
// Read custom fields
|
||||
set_custom_field(entity, field, value);
|
||||
set_object_variable(entity.get_entity_id(), field, value);
|
||||
}
|
||||
}
|
||||
|
||||
@ -234,8 +192,7 @@ namespace scripting
|
||||
return value;
|
||||
}
|
||||
|
||||
// Add custom fields
|
||||
return get_custom_field(entity, field);
|
||||
return get_object_variable(entity.get_entity_id(), field);
|
||||
}
|
||||
|
||||
unsigned int make_array()
|
||||
@ -248,4 +205,47 @@ namespace scripting
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
void set_object_variable(const unsigned int parent_id, const unsigned int id, const script_value& value)
|
||||
{
|
||||
const auto offset = 0xFA00 * (parent_id & 3);
|
||||
const auto variable_id = game::GetVariable(parent_id, id);
|
||||
const auto variable = &game::scr_VarGlob->childVariableValue[variable_id + offset];
|
||||
const auto& raw_value = value.get_raw();
|
||||
|
||||
game::AddRefToValue(raw_value.type, raw_value.u);
|
||||
game::RemoveRefToValue(variable->type, variable->u.u);
|
||||
|
||||
variable->type = static_cast<char>(raw_value.type);
|
||||
variable->u.u = raw_value.u;
|
||||
}
|
||||
|
||||
void set_object_variable(const unsigned int parent_id, const std::string& name, const script_value& value)
|
||||
{
|
||||
const auto id = scripting::find_token_id(name);
|
||||
set_object_variable(parent_id, id, value);
|
||||
}
|
||||
|
||||
script_value get_object_variable(const unsigned int parent_id, const unsigned int id)
|
||||
{
|
||||
const auto offset = 0xFA00 * (parent_id & 3);
|
||||
const auto variable_id = game::FindVariable(parent_id, id);
|
||||
if (!variable_id)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
const auto variable = &game::scr_VarGlob->childVariableValue[variable_id + offset];
|
||||
game::VariableValue value{};
|
||||
value.type = static_cast<int>(variable->type);
|
||||
value.u = variable->u.u;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
script_value get_object_variable(const unsigned int parent_id, const std::string& name)
|
||||
{
|
||||
const auto id = scripting::find_token_id(name);
|
||||
return get_object_variable(parent_id, id);
|
||||
}
|
||||
}
|
||||
|
@ -27,13 +27,16 @@ namespace scripting
|
||||
script_value call_script_function(const entity& entity, const std::string& filename,
|
||||
const std::string& function, const std::vector<script_value>& arguments);
|
||||
|
||||
void clear_entity_fields(const entity& entity);
|
||||
void clear_custom_fields();
|
||||
|
||||
void set_entity_field(const entity& entity, const std::string& field, const script_value& value);
|
||||
script_value get_entity_field(const entity& entity, const std::string& field);
|
||||
|
||||
void notify(const entity& entity, const std::string& event, const std::vector<script_value>& arguments);
|
||||
|
||||
unsigned int make_array();
|
||||
|
||||
script_value get_object_variable(const unsigned int parent_id, const unsigned int id);
|
||||
script_value get_object_variable(const unsigned int parent_id, const std::string& name);
|
||||
|
||||
void set_object_variable(const unsigned int parent_id, const std::string& name, const script_value& value);
|
||||
void set_object_variable(const unsigned int parent_id, const unsigned int id, const script_value& value);
|
||||
}
|
||||
|
@ -69,19 +69,40 @@ namespace scripting
|
||||
|
||||
return reinterpret_cast<script_function*>(method_table)[index - 0x8000];
|
||||
}
|
||||
|
||||
unsigned int parse_token_id(const std::string& name)
|
||||
{
|
||||
if (name.starts_with("_ID"))
|
||||
{
|
||||
return static_cast<unsigned int>(std::strtol(name.substr(3).data(), nullptr, 10));
|
||||
}
|
||||
|
||||
if (name.starts_with("_id_"))
|
||||
{
|
||||
return static_cast<unsigned int>(std::strtol(name.substr(4).data(), nullptr, 16));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
std::string find_token(unsigned int id)
|
||||
std::vector<std::string> find_token(unsigned int id)
|
||||
{
|
||||
std::vector<std::string> results;
|
||||
|
||||
results.push_back(utils::string::va("_id_%X", id));
|
||||
results.push_back(utils::string::va("_ID%i", id));
|
||||
|
||||
for (const auto& token : token_map)
|
||||
{
|
||||
if (token.second == id)
|
||||
{
|
||||
return token.first;
|
||||
results.push_back(token.first);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return utils::string::va("_ID%i", id);
|
||||
return results;
|
||||
}
|
||||
|
||||
unsigned int find_token_id(const std::string& name)
|
||||
@ -93,7 +114,13 @@ namespace scripting
|
||||
return result->second;
|
||||
}
|
||||
|
||||
return 0;
|
||||
const auto parsed_id = parse_token_id(name);
|
||||
if (parsed_id)
|
||||
{
|
||||
return parsed_id;
|
||||
}
|
||||
|
||||
return game::SL_GetCanonicalString(name.data());
|
||||
}
|
||||
|
||||
script_function find_function(const std::string& name, const bool prefer_global)
|
||||
|
@ -9,7 +9,7 @@ namespace scripting
|
||||
|
||||
using script_function = void(*)(game::scr_entref_t);
|
||||
|
||||
std::string find_token(unsigned int id);
|
||||
std::vector<std::string> find_token(unsigned int id);
|
||||
unsigned int find_token_id(const std::string& name);
|
||||
|
||||
script_function find_function(const std::string& name, const bool prefer_global);
|
||||
|
@ -17,7 +17,6 @@ namespace scripting::lua
|
||||
{
|
||||
namespace
|
||||
{
|
||||
|
||||
vector normalize_vector(const vector& vec)
|
||||
{
|
||||
const auto length = sqrt(
|
||||
@ -155,12 +154,52 @@ namespace scripting::lua
|
||||
{
|
||||
return normalize_vector(a);
|
||||
};
|
||||
|
||||
vector_type["normalize"] = [](const vector& a)
|
||||
{
|
||||
return normalize_vector(a);
|
||||
};
|
||||
|
||||
vector_type["toangles"] = [](const vector& a)
|
||||
{
|
||||
return call("vectortoangles", {a}).as<vector>();
|
||||
};
|
||||
|
||||
vector_type["toyaw"] = [](const vector& a)
|
||||
{
|
||||
return call("vectortoyaw", {a}).as<vector>();
|
||||
};
|
||||
|
||||
vector_type["tolerp"] = [](const vector& a)
|
||||
{
|
||||
return call("vectortolerp", {a}).as<vector>();
|
||||
};
|
||||
|
||||
vector_type["toup"] = [](const vector& a)
|
||||
{
|
||||
return call("anglestoup", {a}).as<vector>();
|
||||
};
|
||||
|
||||
vector_type["toright"] = [](const vector& a)
|
||||
{
|
||||
return call("anglestoright", {a}).as<vector>();
|
||||
};
|
||||
|
||||
vector_type["toforward"] = [](const vector& a)
|
||||
{
|
||||
return call("anglestoforward", {a}).as<vector>();
|
||||
};
|
||||
}
|
||||
|
||||
void setup_entity_type(sol::state& state, event_handler& handler, scheduler& scheduler)
|
||||
{
|
||||
state["level"] = entity{*game::levelEntityId};
|
||||
|
||||
if (game::environment::is_sp())
|
||||
{
|
||||
state["player"] = call("getentbynum", {0}).as<entity>();
|
||||
}
|
||||
|
||||
auto entity_type = state.new_usertype<entity>("entity");
|
||||
|
||||
for (const auto& func : method_map)
|
||||
|
@ -11,6 +11,8 @@ namespace scripting::lua::engine
|
||||
{
|
||||
namespace
|
||||
{
|
||||
bool running = false;
|
||||
|
||||
auto& get_scripts()
|
||||
{
|
||||
static std::vector<std::unique_ptr<context>> scripts{};
|
||||
@ -38,21 +40,30 @@ namespace scripting::lua::engine
|
||||
|
||||
void stop()
|
||||
{
|
||||
running = false;
|
||||
logfile::clear_callbacks();
|
||||
get_scripts().clear();
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
// No SP until there is a concept
|
||||
if (game::environment::is_sp())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
stop();
|
||||
|
||||
load_scripts("h1-mod/scripts/");
|
||||
load_scripts("data/scripts/");
|
||||
|
||||
if (game::environment::is_sp())
|
||||
{
|
||||
load_scripts("h1-mod/scripts/sp/");
|
||||
load_scripts("data/scripts/sp/");
|
||||
}
|
||||
else
|
||||
{
|
||||
load_scripts("h1-mod/scripts/mp/");
|
||||
load_scripts("data/scripts/mp/");
|
||||
}
|
||||
|
||||
running = true;
|
||||
}
|
||||
|
||||
void notify(const event& e)
|
||||
@ -70,4 +81,9 @@ namespace scripting::lua::engine
|
||||
script->run_frame();
|
||||
}
|
||||
}
|
||||
|
||||
bool is_running()
|
||||
{
|
||||
return running;
|
||||
}
|
||||
}
|
||||
|
@ -8,4 +8,5 @@ namespace scripting::lua::engine
|
||||
void stop();
|
||||
void notify(const event& e);
|
||||
void run_frame();
|
||||
bool is_running();
|
||||
}
|
||||
|
@ -165,56 +165,33 @@ namespace scripting::lua
|
||||
auto table = sol::table::create(state);
|
||||
auto metatable = sol::table::create(state);
|
||||
|
||||
const auto offset = 64000 * (parent_id & 3);
|
||||
|
||||
metatable[sol::meta_function::new_index] = [offset, parent_id](const sol::table t, const sol::this_state s,
|
||||
metatable[sol::meta_function::new_index] = [parent_id](const sol::table t, const sol::this_state s,
|
||||
const sol::lua_value& field, const sol::lua_value& value)
|
||||
{
|
||||
const auto id = field.is<std::string>()
|
||||
? scripting::find_token_id(field.as<std::string>())
|
||||
: field.as<int>();
|
||||
|
||||
if (!id)
|
||||
const auto new_variable = convert({s, value});
|
||||
if (field.is<unsigned int>())
|
||||
{
|
||||
return;
|
||||
scripting::set_object_variable(parent_id, field.as<unsigned int>(), new_variable);
|
||||
}
|
||||
else if (field.is<std::string>())
|
||||
{
|
||||
scripting::set_object_variable(parent_id, field.as<std::string>(), new_variable);
|
||||
}
|
||||
|
||||
const auto variable_id = game::GetVariable(parent_id, id);
|
||||
const auto variable = &game::scr_VarGlob->childVariableValue[variable_id + offset];
|
||||
const auto new_variable = convert({s, value}).get_raw();
|
||||
|
||||
game::AddRefToValue(new_variable.type, new_variable.u);
|
||||
game::RemoveRefToValue(variable->type, variable->u.u);
|
||||
|
||||
variable->type = (char)new_variable.type;
|
||||
variable->u.u = new_variable.u;
|
||||
};
|
||||
|
||||
metatable[sol::meta_function::index] = [offset, parent_id](const sol::table t, const sol::this_state s,
|
||||
metatable[sol::meta_function::index] = [parent_id](const sol::table t, const sol::this_state s,
|
||||
const sol::lua_value& field)
|
||||
{
|
||||
const auto id = field.is<std::string>()
|
||||
? scripting::find_token_id(field.as<std::string>())
|
||||
: field.as<int>();
|
||||
|
||||
if (!id)
|
||||
if (field.is<unsigned int>())
|
||||
{
|
||||
return sol::lua_value{s, sol::lua_nil};
|
||||
return convert(s, scripting::get_object_variable(parent_id, field.as<unsigned int>()));
|
||||
}
|
||||
else if (field.is<std::string>())
|
||||
{
|
||||
return convert(s, scripting::get_object_variable(parent_id, field.as<std::string>()));
|
||||
}
|
||||
|
||||
const auto variable_id = game::FindVariable(parent_id, id);
|
||||
if (!variable_id)
|
||||
{
|
||||
return sol::lua_value{s, sol::lua_nil};
|
||||
}
|
||||
|
||||
const auto variable = game::scr_VarGlob->childVariableValue[variable_id + offset];
|
||||
|
||||
game::VariableValue result{};
|
||||
result.u = variable.u.u;
|
||||
result.type = (game::scriptType_e)variable.type;
|
||||
|
||||
return convert(s, result);
|
||||
return sol::lua_value{s, sol::lua_nil};
|
||||
};
|
||||
|
||||
table[sol::metatable_key] = metatable;
|
||||
|
Reference in New Issue
Block a user