Added character iteration to the rooms

This commit is contained in:
Kayne Ruse
2015-03-02 04:26:34 +11:00
parent b8806cc209
commit bd68af5875
2 changed files with 29 additions and 2 deletions
+28 -1
View File
@@ -23,6 +23,9 @@
#include "room_data.hpp"
#include <sstream>
#include <stdexcept>
static int setRoomName(lua_State* L) {
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
room->SetName(lua_tostring(L, 2));
@@ -67,7 +70,22 @@ static int getTriggerMgr(lua_State* L) {
//TODO: character list
//TODO: (1) forEachCharacter
static int forEachCharacter(lua_State* L) {
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
//pass each character to the given function
for (auto& it : *room->GetCharacterList()) {
lua_pushvalue(L, -1);
lua_pushlightuserdata(L, static_cast<void*>(it));
//call each iteration, throwing an exception if something happened
if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
std::ostringstream os;
os << "Lua error: ";
os << lua_tostring(L, -1);
throw(std::runtime_error(os.str()));
}
}
return 0;
}
static int setOnTick(lua_State* L) {
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
@@ -76,6 +94,12 @@ static int setOnTick(lua_State* L) {
return 0;
}
static int getOnTick(lua_State* L) {
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
lua_rawgeti(lua, LUA_REGISTRYINDEX, room->GetTickReference());
return 1;
}
static int initialize(lua_State* L) {
RoomData* room = static_cast<RoomData*>(lua_touserdata(L, 1));
@@ -99,7 +123,10 @@ static const luaL_Reg roomLib[] = {
{"GetMonsterMgr",getMonsterMgr},
{"GetTriggerMgr",getTriggerMgr},
{"ForEachCharacter", forEachCharacter},
{"SetOnTick", setOnTick},
{"GetOnTick", getOnTick},
{"Initialize", initialize},
{nullptr, nullptr}