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; }
void pumpPacketProximity(SerialPacket* const argPacket, int roomIndex, Vector2 position, int distance) { RoomData* roomData = RoomManager::GetSingleton().Get(roomIndex); if (!roomData) { throw(std::runtime_error("Failed to pump to a non-existant room")); } AccountData* accountData = nullptr; ClientData* clientData = nullptr; for (auto& characterIt : *roomData->GetCharacterList()) { if (distance == -1 || (characterIt->GetOrigin() - position).Length() <= distance) { accountData = AccountManager::GetSingleton().Get(characterIt->GetOwner()); clientData = ClientManager::GetSingleton().Get(accountData->GetClientIndex()); UDPNetworkUtility::GetSingleton().SendTo(clientData->GetAddress(), argPacket); } } }