16 ICharacterPtr character;
19 static inline const char *luaAI_metaai() {
23 static inline const char *luaAI_metazone() {
27 static inline const char* luaAI_metaaggromgr() {
28 return "__meta_aggromgr";
31 static inline const char* luaAI_metaregistry() {
32 return "__meta_registry";
35 static inline const char* luaAI_metagroupmgr() {
36 return "__meta_groupmgr";
39 static inline const char* luaAI_metacharacter() {
40 return "__meta_character";
43 static inline const char* luaAI_metavec() {
47 static void luaAI_registerfuncs(lua_State* s,
const luaL_Reg* funcs,
const char *name) {
48 luaL_newmetatable(s, name);
51 lua_setfield(s, -2,
"__index");
52 luaL_setfuncs(s, funcs, 0);
55 static void luaAI_setupmetatable(lua_State* s,
const std::string& type,
const luaL_Reg *funcs,
const std::string& name) {
56 const std::string& metaFull =
"__meta_" + name +
"_" + type;
58 lua_setfield(s, LUA_REGISTRYINDEX, metaFull.c_str());
60 lua_getfield(s, LUA_REGISTRYINDEX, metaFull.c_str());
65 lua_createtable(s, 0, 2);
67 lua_setfield(s, -2,
"__index");
68 lua_pushstring(s, name.c_str());
69 lua_setfield(s, -2,
"__name");
70 lua_pushstring(s, type.c_str());
71 lua_setfield(s, -2,
"type");
72 luaL_setfuncs(s, funcs, 0);
73 lua_setmetatable(s, -2);
76 static int luaAI_newindex(lua_State* s) {
78 lua_getmetatable(s, -3);
80 const char *field = luaL_checkstring(s, -3);
84 lua_setfield(s, -2, field);
90 static inline T luaAI_getudata(lua_State* s,
int n,
const char *name) {
91 void* dataVoid = luaL_checkudata(s, n, name);
92 if (dataVoid ==
nullptr) {
93 std::string error(name);
94 error.append(
" userdata must not be null");
95 luaL_argcheck(s, dataVoid !=
nullptr, n, error.c_str());
101 static inline T* luaAI_newuserdata(lua_State* s,
const T& data) {
102 T* udata = (T*) lua_newuserdata(s,
sizeof(T));
107 static void luaAI_globalpointer(lua_State* s,
void* pointer,
const char *name) {
108 lua_pushlightuserdata(s, pointer);
109 lua_setglobal(s, name);
112 static int luaAI_assignmetatable(lua_State* s,
const char *name) {
113 luaL_getmetatable(s, name);
115 if (!lua_istable(s, -1)) {
116 ai_log_error(
"LUA: metatable for %s doesn't exist", name);
120 lua_setmetatable(s, -2);
125 static inline int luaAI_pushudata(lua_State* s,
const T& data,
const char *name) {
126 luaAI_newuserdata<T>(s, data);
127 return luaAI_assignmetatable(s, name);
131 static T* luaAI_getlightuserdata(lua_State *s,
const char *name) {
132 lua_getglobal(s, name);
133 if (lua_isnil(s, -1)) {
136 T* data = (T*) lua_touserdata(s, -1);
141 static luaAI_AI* luaAI_toai(lua_State *s,
int n) {
142 luaAI_AI* ai = luaAI_getudata<luaAI_AI*>(s, n, luaAI_metaai());
144 luaL_error(s,
"AI is already destroyed");
149 static luaAI_ICharacter* luaAI_tocharacter(lua_State *s,
int n) {
150 luaAI_ICharacter* chr = luaAI_getudata<luaAI_ICharacter*>(s, n, luaAI_metacharacter());
151 if (!chr->character) {
152 luaL_error(s,
"ICharacter is already destroyed");
157 static Zone* luaAI_tozone(lua_State *s,
int n) {
158 return *(Zone**)luaAI_getudata<Zone*>(s, n, luaAI_metazone());
161 static AggroMgr* luaAI_toaggromgr(lua_State *s,
int n) {
162 return *(AggroMgr**)luaAI_getudata<AggroMgr*>(s, n, luaAI_metaaggromgr());
165 static GroupMgr* luaAI_togroupmgr(lua_State *s,
int n) {
166 return *(GroupMgr**)luaAI_getudata<GroupMgr*>(s, n, luaAI_metagroupmgr());
169 static glm::vec3* luaAI_tovec(lua_State *s,
int n) {
170 return luaAI_getudata<glm::vec3*>(s, n, luaAI_metavec());
173 static int luaAI_pushzone(lua_State* s, Zone* zone) {
174 if (zone ==
nullptr) {
178 return luaAI_pushudata<Zone*>(s, zone, luaAI_metazone());
181 static int luaAI_pushaggromgr(lua_State* s, AggroMgr* aggroMgr) {
182 return luaAI_pushudata<AggroMgr*>(s, aggroMgr, luaAI_metaaggromgr());
185 static int luaAI_pushgroupmgr(lua_State* s, GroupMgr* groupMgr) {
186 return luaAI_pushudata<GroupMgr*>(s, groupMgr, luaAI_metagroupmgr());
189 static int luaAI_pushcharacter(lua_State* s,
const ICharacterPtr& character) {
190 luaAI_ICharacter* raw = (luaAI_ICharacter*) lua_newuserdata(s,
sizeof(luaAI_ICharacter));
191 luaAI_ICharacter* udata =
new (raw)luaAI_ICharacter();
192 udata->character = character;
193 return luaAI_assignmetatable(s, luaAI_metacharacter());
196 static int luaAI_pushai(lua_State* s,
const AIPtr& ai) {
197 luaAI_AI* raw = (luaAI_AI*) lua_newuserdata(s,
sizeof(luaAI_AI));
198 luaAI_AI* udata =
new (raw)luaAI_AI();
200 return luaAI_assignmetatable(s, luaAI_metaai());
203 static int luaAI_pushvec(lua_State* s,
const glm::vec3& v) {
204 return luaAI_pushudata<glm::vec3>(s, v, luaAI_metavec());
213 static int luaAI_groupmgrposition(lua_State* s) {
214 const GroupMgr* groupMgr = luaAI_togroupmgr(s, 1);
215 const GroupId groupId = (GroupId)luaL_checkinteger(s, 2);
216 return luaAI_pushvec(s, groupMgr->getPosition(groupId));
226 static int luaAI_groupmgradd(lua_State* s) {
227 GroupMgr* groupMgr = luaAI_togroupmgr(s, 1);
228 const GroupId groupId = (GroupId)luaL_checkinteger(s, 2);
229 luaAI_AI* ai = luaAI_toai(s, 3);
230 const bool state = groupMgr->add(groupId, ai->ai);
231 lua_pushboolean(s, state);
242 static int luaAI_groupmgrremove(lua_State* s) {
243 GroupMgr* groupMgr = luaAI_togroupmgr(s, 1);
244 const GroupId groupId = (GroupId)luaL_checkinteger(s, 2);
245 luaAI_AI* ai = luaAI_toai(s, 3);
246 const bool state = groupMgr->remove(groupId, ai->ai);
247 lua_pushboolean(s, state);
258 static int luaAI_groupmgrisleader(lua_State* s) {
259 const GroupMgr* groupMgr = luaAI_togroupmgr(s, 1);
260 const GroupId groupId = (GroupId)luaL_checkinteger(s, 2);
261 luaAI_AI* ai = luaAI_toai(s, 3);
262 const bool state = groupMgr->isGroupLeader(groupId, ai->ai);
263 lua_pushboolean(s, state);
274 static int luaAI_groupmgrisingroup(lua_State* s) {
275 const GroupMgr* groupMgr = luaAI_togroupmgr(s, 1);
276 const GroupId groupId = (GroupId)luaL_checkinteger(s, 2);
277 luaAI_AI* ai = luaAI_toai(s, 3);
278 const bool state = groupMgr->isInGroup(groupId, ai->ai);
279 lua_pushboolean(s, state);
289 static int luaAI_groupmgrisinanygroup(lua_State* s) {
290 const GroupMgr* groupMgr = luaAI_togroupmgr(s, 1);
291 luaAI_AI* ai = luaAI_toai(s, 2);
292 const bool state = groupMgr->isInAnyGroup(ai->ai);
293 lua_pushboolean(s, state);
303 static int luaAI_groupmgrsize(lua_State* s) {
304 const GroupMgr* groupMgr = luaAI_togroupmgr(s, 1);
305 const GroupId groupId = (GroupId)luaL_checkinteger(s, 2);
306 lua_pushinteger(s, groupMgr->getGroupSize(groupId));
316 static int luaAI_groupmgrleader(lua_State* s) {
317 const GroupMgr* groupMgr = luaAI_togroupmgr(s, 1);
318 const GroupId groupId = (GroupId)luaL_checkinteger(s, 2);
319 const AIPtr& ai = groupMgr->getLeader(groupId);
328 static int luaAI_groupmgrtostring(lua_State* s) {
329 const GroupMgr* groupMgr = luaAI_togroupmgr(s, 1);
330 lua_pushfstring(s,
"groupmgr: %p", groupMgr);
339 static int luaAI_zoneexecute(lua_State* s) {
340 Zone* zone = luaAI_tozone(s, 1);
341 luaL_checktype(s, 2, LUA_TFUNCTION);
342 const int topIndex = lua_gettop(s);
343 zone->execute([=] (
const AIPtr& ai) {
344 if (luaAI_pushai(s, ai) <= 0) {
347 lua_pcall(s, 1, 0, 0);
348 const int stackDelta = lua_gettop(s) - topIndex;
349 if (stackDelta > 0) {
350 lua_pop(s, stackDelta);
361 static int luaAI_zonegroupmgr(lua_State* s) {
362 Zone* zone = luaAI_tozone(s, 1);
363 return luaAI_pushgroupmgr(s, &zone->getGroupMgr());
366 static int luaAI_zonetostring(lua_State* s) {
367 const Zone* zone = luaAI_tozone(s, 1);
368 lua_pushfstring(s,
"zone: %s", zone->getName().c_str());
377 static int luaAI_zonename(lua_State* s) {
378 const Zone* zone = luaAI_tozone(s, 1);
379 lua_pushstring(s, zone->getName().c_str());
390 static int luaAI_zoneai(lua_State* s) {
391 Zone* zone = luaAI_tozone(s, 1);
392 const CharacterId
id = (CharacterId)luaL_checkinteger(s, 2);
393 const AIPtr& ai = zone->getAI(
id);
407 static int luaAI_zonesize(lua_State* s) {
408 const Zone* zone = luaAI_tozone(s, 1);
409 lua_pushinteger(s, zone->size());
419 static int luaAI_aggromgrhighestentry(lua_State* s) {
420 AggroMgr* aggroMgr = luaAI_toaggromgr(s, 1);
421 EntryPtr entry = aggroMgr->getHighestEntry();
422 if (entry ==
nullptr) {
426 lua_pushinteger(s, entry->getCharacterId());
427 lua_pushnumber(s, entry->getAggro());
437 static int luaAI_aggromgrentries(lua_State* s) {
438 AggroMgr* aggroMgr = luaAI_toaggromgr(s, 1);
439 const AggroMgr::Entries& entries = aggroMgr->getEntries();
441 const int top = lua_gettop(s);
442 for (
auto it = entries.begin(); it != entries.end(); ++it) {
443 lua_pushinteger(s, it->getCharacterId());
444 lua_pushnumber(s, it->getAggro());
445 lua_settable(s, top);
456 static int luaAI_aggromgrsetreducebyratio(lua_State* s) {
457 AggroMgr* aggroMgr = luaAI_toaggromgr(s, 1);
458 const lua_Number reduceRatioSecond = luaL_checknumber(s, 2);
459 const lua_Number minAggro = luaL_checknumber(s, 3);
460 aggroMgr->setReduceByRatio((
float)reduceRatioSecond, (
float)minAggro);
469 static int luaAI_aggromgrsetreducebyvalue(lua_State* s) {
470 AggroMgr* aggroMgr = luaAI_toaggromgr(s, 1);
471 const lua_Number reduceValueSecond = luaL_checknumber(s, 2);
472 aggroMgr->setReduceByValue((
float)reduceValueSecond);
480 static int luaAI_aggromgrresetreducevalue(lua_State* s) {
481 AggroMgr* aggroMgr = luaAI_toaggromgr(s, 1);
482 aggroMgr->resetReduceValue();
493 static int luaAI_aggromgraddaggro(lua_State* s) {
494 AggroMgr* aggroMgr = luaAI_toaggromgr(s, 1);
495 const CharacterId chrId = (CharacterId)luaL_checkinteger(s, 2);
496 const lua_Number amount = luaL_checknumber(s, 3);
497 const EntryPtr& entry = aggroMgr->addAggro(chrId, (
float)amount);
498 lua_pushnumber(s, entry->getAggro());
502 static int luaAI_aggromgrtostring(lua_State* s) {
503 lua_pushliteral(s,
"aggroMgr");
512 static int luaAI_characterid(lua_State* s) {
513 const luaAI_ICharacter* chr = luaAI_tocharacter(s, 1);
514 lua_pushinteger(s, chr->character->getId());
523 static int luaAI_characterposition(lua_State* s) {
524 const luaAI_ICharacter* chr = luaAI_tocharacter(s, 1);
525 return luaAI_pushvec(s, chr->character->getPosition());
533 static int luaAI_charactersetposition(lua_State* s) {
534 luaAI_ICharacter* chr = luaAI_tocharacter(s, 1);
535 const glm::vec3* v = luaAI_tovec(s, 2);
536 chr->character->setPosition(*v);
545 static int luaAI_characterspeed(lua_State* s) {
546 const luaAI_ICharacter* chr = luaAI_tocharacter(s, 1);
547 lua_pushnumber(s, chr->character->getSpeed());
556 static int luaAI_characterorientation(lua_State* s) {
557 const luaAI_ICharacter* chr = luaAI_tocharacter(s, 1);
558 lua_pushnumber(s, chr->character->getOrientation());
567 static int luaAI_charactersetspeed(lua_State* s) {
568 luaAI_ICharacter* chr = luaAI_tocharacter(s, 1);
569 const lua_Number value = luaL_checknumber(s, 2);
570 chr->character->setSpeed((
float)value);
579 static int luaAI_charactersetorientation(lua_State* s) {
580 luaAI_ICharacter* chr = luaAI_tocharacter(s, 1);
581 const lua_Number value = luaL_checknumber(s, 2);
582 chr->character->setOrientation((
float)value);
591 static int luaAI_charactereq(lua_State* s) {
592 const luaAI_ICharacter* a = luaAI_tocharacter(s, 1);
593 const luaAI_ICharacter* b = luaAI_tocharacter(s, 2);
594 const bool e = *a->character == *b->character;
595 lua_pushboolean(s, e);
603 static int luaAI_charactergc(lua_State* s) {
604 luaAI_ICharacter* chr = luaAI_tocharacter(s, -1);
605 chr->character = ICharacterPtr();
614 static int luaAI_characterattributes(lua_State* s) {
615 const luaAI_ICharacter* chr = luaAI_tocharacter(s, 1);
616 const CharacterAttributes& attributes = chr->character->getAttributes();
618 const int top = lua_gettop(s);
619 for (
auto it = attributes.begin(); it != attributes.end(); ++it) {
620 const std::string& key = it->first;
621 const std::string& value = it->second;
622 lua_pushlstring(s, key.c_str(), key.size());
623 lua_pushlstring(s, value.c_str(), value.size());
624 lua_settable(s, top);
635 static int luaAI_charactersetattribute(lua_State* s) {
636 luaAI_ICharacter* chr = luaAI_tocharacter(s, 1);
637 const char* key = luaL_checkstring(s, 2);
638 const char* value = luaL_checkstring(s, 3);
639 chr->character->setAttribute(key, value);
643 static int luaAI_charactertostring(lua_State* s) {
644 luaAI_ICharacter* character = luaAI_tocharacter(s, 1);
645 lua_pushfstring(s,
"Character: %d", (lua_Integer)character->character->getId());
654 static int luaAI_aiid(lua_State* s) {
655 const luaAI_AI* ai = luaAI_toai(s, 1);
656 lua_pushinteger(s, ai->ai->getId());
665 static int luaAI_aitime(lua_State* s) {
666 const luaAI_AI* ai = luaAI_toai(s, 1);
667 lua_pushinteger(s, ai->ai->getTime());
676 static int luaAI_aifilteredentities(lua_State* s) {
677 const luaAI_AI* ai = luaAI_toai(s, 1);
678 const FilteredEntities& filteredEntities = ai->ai->getFilteredEntities();
680 const int top = lua_gettop(s);
682 for (
auto it = filteredEntities.begin(); it != filteredEntities.end(); ++it) {
683 lua_pushinteger(s, ++i);
684 lua_pushinteger(s, *it);
685 lua_settable(s, top);
695 static int luaAI_aigetzone(lua_State* s) {
696 const luaAI_AI* ai = luaAI_toai(s, 1);
697 return luaAI_pushzone(s, ai->ai->getZone());
705 static int luaAI_aigetaggromgr(lua_State* s) {
706 luaAI_AI* ai = luaAI_toai(s, 1);
707 return luaAI_pushaggromgr(s, &ai->ai->getAggroMgr());
715 static int luaAI_aigetcharacter(lua_State* s) {
716 const luaAI_AI* ai = luaAI_toai(s, 1);
717 return luaAI_pushcharacter(s, ai->ai->getCharacter());
725 static int luaAI_aihaszone(lua_State* s) {
726 const luaAI_AI* ai = luaAI_toai(s, 1);
727 lua_pushboolean(s, ai->ai->hasZone() ? 1 : 0);
736 static int luaAI_aieq(lua_State* s) {
737 const luaAI_AI* a = luaAI_toai(s, 1);
738 const luaAI_AI* b = luaAI_toai(s, 2);
739 const bool e = a->ai->getId() == b->ai->getId();
740 lua_pushboolean(s, e);
748 static int luaAI_aigc(lua_State* s) {
749 luaAI_AI* ai = luaAI_toai(s, -1);
754 static int luaAI_aitostring(lua_State* s) {
755 const luaAI_AI* ai = luaAI_toai(s, 1);
756 TreeNodePtr treeNode = ai->ai->getBehaviour();
758 lua_pushfstring(s,
"ai: %s", treeNode->getName().c_str());
760 lua_pushstring(s,
"ai: no behaviour tree set");
772 static int luaAI_vecadd(lua_State* s) {
773 const glm::vec3* a = luaAI_tovec(s, 1);
774 const glm::vec3* b = luaAI_tovec(s, 2);
775 const glm::vec3& c = *a + *b;
776 return luaAI_pushvec(s, c);
786 static int luaAI_vecdot(lua_State* s) {
787 const glm::vec3* a = luaAI_tovec(s, 1);
788 const glm::vec3* b = luaAI_tovec(s, 2);
789 const float c = glm::dot(*a, *b);
790 lua_pushnumber(s, c);
801 static int luaAI_vecdiv(lua_State* s) {
802 const glm::vec3* a = luaAI_tovec(s, 1);
803 const glm::vec3* b = luaAI_tovec(s, 2);
804 const glm::vec3& c = *a / *b;
809 static int luaAI_veclen(lua_State* s) {
810 const glm::vec3* a = luaAI_tovec(s, 1);
811 const float c = glm::length(*a);
812 lua_pushnumber(s, c);
816 static int luaAI_veceq(lua_State* s) {
817 const glm::vec3* a = luaAI_tovec(s, 1);
818 const glm::vec3* b = luaAI_tovec(s, 2);
819 const bool e = glm::all(glm::epsilonEqual(*a, *b, 0.0001f));
820 lua_pushboolean(s, e);
831 static int luaAI_vecsub(lua_State* s) {
832 const glm::vec3* a = luaAI_tovec(s, 1);
833 const glm::vec3* b = luaAI_tovec(s, 2);
834 const glm::vec3& c = *a - *b;
845 static int luaAI_vecnegate(lua_State* s) {
846 const glm::vec3* a = luaAI_tovec(s, 1);
847 luaAI_pushvec(s, -(*a));
851 static int luaAI_vectostring(lua_State* s) {
852 const glm::vec3* a = luaAI_tovec(s, 1);
853 lua_pushfstring(s,
"vec: %f:%f:%f", a->x, a->y, a->z);
857 static int luaAI_vecindex(lua_State *s) {
858 const glm::vec3* v = luaAI_tovec(s, 1);
859 const char* i = luaL_checkstring(s, 2);
865 lua_pushnumber(s, v->x);
870 lua_pushnumber(s, v->y);
875 lua_pushnumber(s, v->z);
885 static int luaAI_vecnewindex(lua_State *s) {
886 glm::vec3* v = luaAI_tovec(s, 1);
887 const char *i = luaL_checkstring(s, 2);
888 const lua_Number t = luaL_checknumber(s, 3);
Definition: LUAFunctions.h:11
#define ai_log_error(...)
Logging macro to provide your own loggers.
Definition: Types.h:23
Definition: LUAFunctions.h:15