Implemented networked room chainging function

This commit is contained in:
Kayne Ruse
2015-03-09 21:28:28 +11:00
parent c3c6d42a80
commit 81b3769188
4 changed files with 41 additions and 21 deletions
+2 -6
View File
@@ -22,18 +22,14 @@
#include "character_api.hpp"
#include "character_data.hpp"
#include "entity_api.hpp"
#include "room_manager.hpp"
#include "server_utilities.hpp"
static int setRoomIndex(lua_State* L) {
//NOTE: type-dependant calls to various API functions, see bug #43
CharacterData* character = static_cast<CharacterData*>(lua_touserdata(L, 1));
RoomManager::GetSingleton().PopCharacter(character);
character->SetRoomIndex(lua_tointeger(L, 2));
RoomManager::GetSingleton().PushCharacter(character);
//TODO: (0) send character room change messages
pumpAndChangeRooms(character, lua_tointeger(L, 2), -1); //TODO: (0) undefined behavior without character index
return 0;
}
+1 -15
View File
@@ -212,21 +212,7 @@ void ServerApplication::hCharacterMovement(CharacterPacket* const argPacket) {
//check if moving rooms
if (characterData->GetRoomIndex() != argPacket->roomIndex) {
//delete from the old room
CharacterPacket newPacket;
copyCharacterToPacket(&newPacket, argPacket->characterIndex);
newPacket.type = SerialPacketType::CHARACTER_DELETE;
pumpPacketProximity(&newPacket, characterData->GetRoomIndex());
//move the character between rooms
roomMgr.PopCharacter(characterData);
characterData->SetRoomIndex(argPacket->roomIndex);
roomMgr.PushCharacter(characterData);
//create in the new room
copyCharacterToPacket(&newPacket, argPacket->characterIndex);
newPacket.type = SerialPacketType::CHARACTER_CREATE;
pumpPacketProximity(&newPacket, characterData->GetRoomIndex());
pumpAndChangeRooms(characterData, argPacket->roomIndex, argPacket->characterIndex);
}
//if not moving between rooms
else {
@@ -140,6 +140,10 @@ void copyCharacterToPacket(CharacterPacket* const packet, int characterIndex) {
throw(std::runtime_error("Failed to copy a character to a packet"));
}
copyCharacterToPacket(packet, characterData, characterIndex);
}
void copyCharacterToPacket(CharacterPacket* const packet, CharacterData* const characterData, int characterIndex) {
//NOTE: keep this up to date when the character changes
packet->characterIndex = characterIndex;
strncpy(packet->handle, characterData->GetHandle().c_str(), PACKET_STRING_SIZE);
@@ -150,3 +154,31 @@ void copyCharacterToPacket(CharacterPacket* const packet, int characterIndex) {
packet->motion = characterData->GetMotion();
packet->bounds = characterData->GetBounds();
}
void pumpAndChangeRooms(int characterIndex, int newRoomIndex) {
//BUG: three redundant lookups
//get the character object
CharacterData* character = CharacterManager::GetSingleton().Get(characterIndex);
//pass ownwards
pumpAndChangeRooms(character, newRoomIndex, characterIndex);
}
void pumpAndChangeRooms(CharacterData* const characterData, int newRoomIndex, int characterIndex) {
//delete from the old room
CharacterPacket newPacket;
copyCharacterToPacket(&newPacket, characterData, characterIndex);
newPacket.type = SerialPacketType::CHARACTER_DELETE;
pumpPacketProximity(&newPacket, characterData->GetRoomIndex());
//move the character between rooms
RoomManager::GetSingleton().PopCharacter(characterData);
characterData->SetRoomIndex(newRoomIndex);
RoomManager::GetSingleton().PushCharacter(characterData);
//create in the new room
copyCharacterToPacket(&newPacket, characterData, characterIndex);
newPacket.type = SerialPacketType::CHARACTER_CREATE;
pumpPacketProximity(&newPacket, characterData->GetRoomIndex());
}
@@ -22,14 +22,20 @@
#ifndef SERVERUTILITIES_HPP_
#define SERVERUTILITIES_HPP_
#include "character_data.hpp"
#include "serial_packet.hpp"
#include "vector2.hpp"
void fullClientUnload(int index);
void fullAccountUnload(int index);
void fullCharacterUnload(int index);
void pumpPacket(SerialPacket* const argPacket);
void pumpPacketProximity(SerialPacket* const argPacket, int roomIndex, Vector2 position = {0, 0}, int distance = -1);
void copyCharacterToPacket(CharacterPacket* const packet, int characterIndex);
void copyCharacterToPacket(CharacterPacket* const packet, CharacterData* const characterData, int characterIndex);
void pumpAndChangeRooms(int characterIndex, int newRoomIndex);
void pumpAndChangeRooms(CharacterData* const characterData, int newRoomIndex, int characterIndex);
#endif