8 #include "LUAFunctions.h"
18 mutable lua_State* _s;
21 MoveVector executeLUA(
const AIPtr& entity,
float speed)
const {
23 const std::string name =
"__meta_steering_" + _type;
24 lua_getfield(_s, LUA_REGISTRYINDEX, name.c_str());
25 #if AI_LUA_SANTITY > 0
26 if (lua_isnil(_s, -1)) {
27 ai_log_error(
"LUA steering: could not find lua userdata for %s", name.c_str());
32 lua_getmetatable(_s, -1);
33 #if AI_LUA_SANTITY > 0
34 if (!lua_istable(_s, -1)) {
35 ai_log_error(
"LUA steering: userdata for %s doesn't have a metatable assigned", name.c_str());
40 lua_getfield(_s, -1,
"execute");
41 if (!lua_isfunction(_s, -1)) {
42 ai_log_error(
"LUA steering: metatable for %s doesn't have the execute() function assigned", name.c_str());
47 lua_getfield(_s, LUA_REGISTRYINDEX, name.c_str());
50 if (luaAI_pushai(_s, entity) == 0) {
55 lua_pushnumber(_s, speed);
57 #if AI_LUA_SANTITY > 0
58 if (!lua_isfunction(_s, -4)) {
59 ai_log_error(
"LUA steering: expected to find a function on stack -4");
62 if (!lua_isuserdata(_s, -3)) {
63 ai_log_error(
"LUA steering: expected to find the userdata on -3");
66 if (!lua_isuserdata(_s, -2)) {
67 ai_log_error(
"LUA steering: second parameter should be the ai");
70 if (!lua_isnumber(_s, -1)) {
71 ai_log_error(
"LUA steering: first parameter should be the speed");
75 const int error = lua_pcall(_s, 3, 4, 0);
77 ai_log_error(
"LUA steering script: %s", lua_isstring(_s, -1) ? lua_tostring(_s, -1) :
"Unknown Error");
79 lua_pop(_s, lua_gettop(_s));
83 const lua_Number x = luaL_checknumber(_s, -1);
84 const lua_Number y = luaL_checknumber(_s, -2);
85 const lua_Number z = luaL_checknumber(_s, -3);
86 const lua_Number rotation = luaL_checknumber(_s, -4);
89 lua_pop(_s, lua_gettop(_s));
90 return MoveVector(glm::vec3((
float)x, (
float)y, (
float)z), (
float)rotation);
100 _s(s), _type(typeStr) {
103 inline const std::string& type()
const {
108 return std::make_shared<LUASteering>(_s, _type);
112 LUASteering(lua_State* s,
const std::string& type) :
124 return executeLUA(entity, speed);
#define ai_log_error(...)
Logging macro to provide your own loggers.
Definition: Types.h:23
Definition: MoveVector.h:10
Definition: LUASteering.h:94
Defines some basic movement algorithms like Wandering, Seeking and Fleeing.
Definition: AIFactories.h:43
MoveVector execute(const AIPtr &entity, float speed) const override
Calculates the MoveVector.
Definition: LUASteering.h:120
Steering interface.
Definition: Steering.h:47
Definition: AIFactories.h:75
Definition: LUASteering.h:16