9 #include "LUAFunctions.h"
20 bool evaluateLUA(
const AIPtr& entity) {
22 const std::string name =
"__meta_condition_" + _name;
23 lua_getfield(_s, LUA_REGISTRYINDEX, name.c_str());
24 #if AI_LUA_SANTITY > 0
25 if (lua_isnil(_s, -1)) {
26 ai_log_error(
"LUA condition: could not find lua userdata for %s", _name.c_str());
31 lua_getmetatable(_s, -1);
32 #if AI_LUA_SANTITY > 0
33 if (!lua_istable(_s, -1)) {
34 ai_log_error(
"LUA condition: userdata for %s doesn't have a metatable assigned", _name.c_str());
39 lua_getfield(_s, -1,
"evaluate");
40 if (!lua_isfunction(_s, -1)) {
41 ai_log_error(
"LUA condition: metatable for %s doesn't have the evaluate() function assigned", _name.c_str());
46 lua_getfield(_s, LUA_REGISTRYINDEX, name.c_str());
49 if (luaAI_pushai(_s, entity) == 0) {
53 #if AI_LUA_SANTITY > 0
54 if (!lua_isfunction(_s, -3)) {
55 ai_log_error(
"LUA condition: expected to find a function on stack -3");
58 if (!lua_isuserdata(_s, -2)) {
59 ai_log_error(
"LUA condition: expected to find the userdata on -2");
62 if (!lua_isuserdata(_s, -1)) {
63 ai_log_error(
"LUA condition: second parameter should be the ai");
67 const int error = lua_pcall(_s, 2, 1, 0);
69 ai_log_error(
"LUA condition script: %s", lua_isstring(_s, -1) ? lua_tostring(_s, -1) :
"Unknown Error");
71 lua_pop(_s, lua_gettop(_s));
74 const int state = lua_toboolean(_s, -1);
75 if (state != 0 && state != 1) {
76 ai_log_error(
"LUA condition: illegal evaluate() value returned: %i", state);
81 lua_pop(_s, lua_gettop(_s));
92 _s(s), _type(typeStr) {
95 inline const std::string& type()
const {
100 return std::make_shared<LUACondition>(_type, ctx->parameters, _s);
104 LUACondition(
const std::string& name,
const std::string& parameters, lua_State* s) :
115 return evaluateLUA(entity);
118 ai_log_error(
"Exception while evaluating lua condition");
Definition: LUACondition.h:86
bool evaluate(const AIPtr &entity) override
Checks whether the condition evaluates to true for the given entity.
Definition: LUACondition.h:111
#define ai_log_error(...)
Logging macro to provide your own loggers.
Definition: Types.h:23
A condition can be placed on a TreeNode to decide which node is going to get executed. In general they are stateless. If they are not, it should explicitly get noted.
Definition: ICondition.h:124
Definition: AIFactories.h:51
Definition: LUACondition.h:16
Definition: AIFactories.h:96