in_world.cpp is building
This commit is contained in:
+93
-85
@@ -22,6 +22,7 @@
|
|||||||
#include "in_world.hpp"
|
#include "in_world.hpp"
|
||||||
|
|
||||||
#include "channels.hpp"
|
#include "channels.hpp"
|
||||||
|
#include "utility.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
@@ -93,11 +94,10 @@ void InWorld::FrameStart() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void InWorld::Update(double delta) {
|
void InWorld::Update(double delta) {
|
||||||
SerialPacket packet;
|
//suck in and process all waiting packets
|
||||||
|
char packetBuffer[MAX_PACKET_SIZE];
|
||||||
//suck in all waiting packets
|
while(network.Receive(reinterpret_cast<SerialPacket*>(packetBuffer))) {
|
||||||
while(network.Receive(&packet)) {
|
HandlePacket(reinterpret_cast<SerialPacket*>(packetBuffer));
|
||||||
HandlePacket(packet);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//update the characters
|
//update the characters
|
||||||
@@ -248,76 +248,55 @@ void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
|
|||||||
//Network handlers
|
//Network handlers
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
void InWorld::HandlePacket(SerialPacket packet) {
|
void InWorld::HandlePacket(SerialPacket* const argPacket) {
|
||||||
switch(packet.meta.type) {
|
switch(argPacket->type) {
|
||||||
case SerialPacket::Type::DISCONNECT:
|
case SerialPacketType::DISCONNECT:
|
||||||
HandleDisconnect(packet);
|
HandleDisconnect(argPacket);
|
||||||
break;
|
break;
|
||||||
case SerialPacket::Type::REGION_CONTENT:
|
case SerialPacketType::CHARACTER_NEW:
|
||||||
HandleRegionContent(packet);
|
HandleCharacterNew(dynamic_cast<CharacterPacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
case SerialPacket::Type::CHARACTER_UPDATE:
|
case SerialPacketType::CHARACTER_DELETE:
|
||||||
HandleCharacterUpdate(packet);
|
HandleCharacterDelete(dynamic_cast<CharacterPacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
case SerialPacket::Type::CHARACTER_NEW:
|
case SerialPacketType::CHARACTER_UPDATE:
|
||||||
HandleCharacterNew(packet);
|
HandleCharacterUpdate(dynamic_cast<CharacterPacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
case SerialPacket::Type::CHARACTER_DELETE:
|
case SerialPacketType::REGION_CONTENT:
|
||||||
HandleCharacterDelete(packet);
|
HandleRegionContent(dynamic_cast<RegionPacket*>(argPacket));
|
||||||
break;
|
break;
|
||||||
//handle errors
|
//handle errors
|
||||||
default:
|
default:
|
||||||
throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in InWorld: " + to_string_custom(int(packet.meta.type))));
|
throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in InWorld: " + to_string_custom(int(argPacket->type)) ));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void InWorld::HandleDisconnect(SerialPacket packet) {
|
void InWorld::HandleDisconnect(SerialPacket* const argPacket) {
|
||||||
SetNextScene(SceneList::RESTART);
|
SetNextScene(SceneList::RESTART);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InWorld::HandleRegionContent(SerialPacket packet) {
|
void InWorld::HandleCharacterNew(CharacterPacket* const argPacket) {
|
||||||
//replace existing regions
|
if (characterMap.find(argPacket->characterIndex) != characterMap.end()) {
|
||||||
regionPager.UnloadRegion(packet.regionInfo.x, packet.regionInfo.y);
|
|
||||||
regionPager.PushRegion(packet.regionInfo.region);
|
|
||||||
packet.regionInfo.region = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::HandleCharacterUpdate(SerialPacket packet) {
|
|
||||||
if (characterMap.find(packet.characterInfo.characterIndex) == characterMap.end()) {
|
|
||||||
HandleCharacterNew(packet);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//update only if the message didn't originate from here
|
|
||||||
if (packet.characterInfo.clientIndex != clientIndex) {
|
|
||||||
characterMap[packet.characterInfo.characterIndex].origin = packet.characterInfo.origin;
|
|
||||||
characterMap[packet.characterInfo.characterIndex].motion = packet.characterInfo.motion;
|
|
||||||
}
|
|
||||||
characterMap[packet.characterInfo.characterIndex].CorrectSprite();
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::HandleCharacterNew(SerialPacket packet) {
|
|
||||||
if (characterMap.find(packet.characterInfo.characterIndex) != characterMap.end()) {
|
|
||||||
throw(std::runtime_error("Cannot create duplicate characters"));
|
throw(std::runtime_error("Cannot create duplicate characters"));
|
||||||
}
|
}
|
||||||
|
|
||||||
//create the character object
|
//create the character object
|
||||||
CharacterData& character = characterMap[packet.characterInfo.characterIndex];
|
CharacterData& character = characterMap[argPacket->characterIndex];
|
||||||
|
|
||||||
//set the members
|
//set the members
|
||||||
character.handle = packet.characterInfo.handle;
|
character.handle = argPacket->handle;
|
||||||
character.avatar = packet.characterInfo.avatar;
|
character.avatar = argPacket->avatar;
|
||||||
character.sprite.LoadSurface(config["dir.sprites"] + character.avatar, 4, 4);
|
character.sprite.LoadSurface(config["dir.sprites"] + character.avatar, 4, 4);
|
||||||
character.mapIndex = packet.characterInfo.mapIndex;
|
character.roomIndex = argPacket->roomIndex;
|
||||||
character.origin = packet.characterInfo.origin;
|
character.origin = argPacket->origin;
|
||||||
character.motion = packet.characterInfo.motion;
|
character.motion = argPacket->motion;
|
||||||
character.stats = packet.characterInfo.stats;
|
character.stats = argPacket->stats;
|
||||||
|
|
||||||
character.CorrectSprite();
|
character.CorrectSprite();
|
||||||
|
|
||||||
//catch this client's player object
|
//catch this client's player object
|
||||||
if (packet.characterInfo.characterIndex == characterIndex && !localCharacter) {
|
if (argPacket->characterIndex == characterIndex && !localCharacter) {
|
||||||
localCharacter = &character;
|
localCharacter = &character;
|
||||||
|
|
||||||
//setup the camera
|
//setup the camera
|
||||||
@@ -331,15 +310,39 @@ void InWorld::HandleCharacterNew(SerialPacket packet) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void InWorld::HandleCharacterDelete(SerialPacket packet) {
|
void InWorld::HandleCharacterDelete(CharacterPacket* const argPacket) {
|
||||||
//TODO: authenticate when own character is being deleted (linked to a TODO in the server)
|
//TODO: authenticate when own character is being deleted (linked to a TODO in the server)
|
||||||
//catch this client's player object
|
//catch this client's player object
|
||||||
if (packet.characterInfo.characterIndex == characterIndex) {
|
if (argPacket->characterIndex == characterIndex) {
|
||||||
characterIndex = -1;
|
characterIndex = -1;
|
||||||
localCharacter = nullptr;
|
localCharacter = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
characterMap.erase(packet.characterInfo.characterIndex);
|
characterMap.erase(argPacket->characterIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InWorld::HandleCharacterUpdate(CharacterPacket* const argPacket) {
|
||||||
|
if (characterMap.find(argPacket->characterIndex) == characterMap.end()) {
|
||||||
|
HandleCharacterNew(argPacket);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
CharacterData& character = characterMap[argPacket->characterIndex];
|
||||||
|
|
||||||
|
//TODO: review this
|
||||||
|
if (argPacket->characterIndex != characterIndex) {
|
||||||
|
character.roomIndex = argPacket->roomIndex;
|
||||||
|
character.origin = argPacket->origin;
|
||||||
|
character.motion = argPacket->motion;
|
||||||
|
character.CorrectSprite();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void InWorld::HandleRegionContent(RegionPacket* const argPacket) {
|
||||||
|
//replace existing regions
|
||||||
|
regionPager.UnloadRegion(argPacket->x, argPacket->y);
|
||||||
|
regionPager.PushRegion(argPacket->region);
|
||||||
|
argPacket->region = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
@@ -347,63 +350,68 @@ void InWorld::HandleCharacterDelete(SerialPacket packet) {
|
|||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
void InWorld::RequestSynchronize() {
|
void InWorld::RequestSynchronize() {
|
||||||
SerialPacket packet;
|
ClientPacket newPacket;
|
||||||
|
|
||||||
//request a sync
|
//request a sync
|
||||||
packet.meta.type = SerialPacket::Type::SYNCHRONIZE;
|
newPacket.type = SerialPacketType::SYNCHRONIZE;
|
||||||
packet.clientInfo.clientIndex = clientIndex;
|
newPacket.clientIndex = clientIndex;
|
||||||
packet.clientInfo.accountIndex = accountIndex;
|
newPacket.accountIndex = accountIndex;
|
||||||
packet.clientInfo.characterIndex = characterIndex;
|
|
||||||
|
|
||||||
network.SendTo(Channels::SERVER, &packet);
|
network.SendTo(Channels::SERVER, &newPacket);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InWorld::SendPlayerUpdate() {
|
void InWorld::SendPlayerUpdate() {
|
||||||
SerialPacket packet;
|
CharacterPacket newPacket;
|
||||||
|
|
||||||
//pack the packet
|
//pack the packet
|
||||||
packet.meta.type = SerialPacket::Type::CHARACTER_UPDATE;
|
newPacket.type = SerialPacketType::CHARACTER_UPDATE;
|
||||||
packet.characterInfo.clientIndex = clientIndex;
|
|
||||||
packet.characterInfo.accountIndex = accountIndex;
|
|
||||||
packet.characterInfo.characterIndex = characterIndex;
|
|
||||||
packet.characterInfo.origin = localCharacter->origin;
|
|
||||||
packet.characterInfo.motion = localCharacter->motion;
|
|
||||||
|
|
||||||
network.SendTo(Channels::SERVER, &packet);
|
newPacket.characterIndex = characterIndex;
|
||||||
|
//handle, avatar
|
||||||
|
newPacket.accountIndex = accountIndex;
|
||||||
|
newPacket.roomIndex = localCharacter->roomIndex;
|
||||||
|
newPacket.origin = localCharacter->origin;
|
||||||
|
newPacket.motion = localCharacter->motion;
|
||||||
|
newPacket.stats = localCharacter->stats;
|
||||||
|
|
||||||
|
//TODO: equipment
|
||||||
|
//TODO: items
|
||||||
|
//TODO: buffs
|
||||||
|
//TODO: debuffs
|
||||||
|
|
||||||
|
network.SendTo(Channels::SERVER, &newPacket);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InWorld::RequestDisconnect() {
|
void InWorld::RequestDisconnect() {
|
||||||
SerialPacket packet;
|
ClientPacket newPacket;
|
||||||
|
|
||||||
//send a disconnect request
|
//send a disconnect request
|
||||||
packet.meta.type = SerialPacket::Type::DISCONNECT;
|
newPacket.type = SerialPacketType::DISCONNECT;
|
||||||
packet.clientInfo.clientIndex = clientIndex;
|
newPacket.clientIndex = clientIndex;
|
||||||
packet.clientInfo.accountIndex = accountIndex;
|
newPacket.accountIndex = accountIndex;
|
||||||
packet.clientInfo.characterIndex = characterIndex;
|
|
||||||
|
|
||||||
network.SendTo(Channels::SERVER, &packet);
|
network.SendTo(Channels::SERVER, &newPacket);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InWorld::RequestShutDown() {
|
void InWorld::RequestShutDown() {
|
||||||
SerialPacket packet;
|
ClientPacket newPacket;
|
||||||
|
|
||||||
//send a shutdown request
|
//send a shutdown request
|
||||||
packet.meta.type = SerialPacket::Type::SHUTDOWN;
|
newPacket.type = SerialPacketType::SHUTDOWN;
|
||||||
packet.clientInfo.clientIndex = clientIndex;
|
newPacket.clientIndex = clientIndex;
|
||||||
packet.clientInfo.accountIndex = accountIndex;
|
newPacket.accountIndex = accountIndex;
|
||||||
packet.clientInfo.characterIndex = characterIndex;
|
|
||||||
|
|
||||||
network.SendTo(Channels::SERVER, &packet);
|
network.SendTo(Channels::SERVER, &newPacket);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InWorld::RequestRegion(int mapIndex, int x, int y) {
|
void InWorld::RequestRegion(int roomIndex, int x, int y) {
|
||||||
SerialPacket packet;
|
RegionPacket packet;
|
||||||
|
|
||||||
//pack the region's data
|
//pack the region's data
|
||||||
packet.meta.type = SerialPacket::Type::REGION_REQUEST;
|
packet.type = SerialPacketType::REGION_REQUEST;
|
||||||
packet.regionInfo.mapIndex = mapIndex;
|
packet.roomIndex = roomIndex;
|
||||||
packet.regionInfo.x = x;
|
packet.x = x;
|
||||||
packet.regionInfo.y = y;
|
packet.y = y;
|
||||||
|
|
||||||
network.SendTo(Channels::SERVER, &packet);
|
network.SendTo(Channels::SERVER, &packet);
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-7
@@ -78,19 +78,19 @@ protected:
|
|||||||
void KeyUp(SDL_KeyboardEvent const&);
|
void KeyUp(SDL_KeyboardEvent const&);
|
||||||
|
|
||||||
//Network handlers
|
//Network handlers
|
||||||
void HandlePacket(SerialPacket);
|
void HandlePacket(SerialPacket* const);
|
||||||
void HandleDisconnect(SerialPacket);
|
void HandleDisconnect(SerialPacket* const);
|
||||||
void HandleCharacterNew(SerialPacket);
|
void HandleCharacterNew(CharacterPacket* const);
|
||||||
void HandleCharacterDelete(SerialPacket);
|
void HandleCharacterDelete(CharacterPacket* const);
|
||||||
void HandleCharacterUpdate(SerialPacket);
|
void HandleCharacterUpdate(CharacterPacket* const);
|
||||||
void HandleRegionContent(SerialPacket);
|
void HandleRegionContent(RegionPacket* const);
|
||||||
|
|
||||||
//Server control
|
//Server control
|
||||||
void RequestSynchronize();
|
void RequestSynchronize();
|
||||||
void SendPlayerUpdate();
|
void SendPlayerUpdate();
|
||||||
void RequestDisconnect();
|
void RequestDisconnect();
|
||||||
void RequestShutDown();
|
void RequestShutDown();
|
||||||
void RequestRegion(int mapIndex, int x, int y);
|
void RequestRegion(int roomIndex, int x, int y);
|
||||||
|
|
||||||
//utilities
|
//utilities
|
||||||
void UpdateMap();
|
void UpdateMap();
|
||||||
|
|||||||
@@ -63,6 +63,19 @@ Region* RegionPager::FindRegion(int x, int y) {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Region* RegionPager::PushRegion(Region* const region) {
|
||||||
|
if (
|
||||||
|
region->GetX() != snapToBase(REGION_WIDTH, region->GetX()) ||
|
||||||
|
region->GetY() != snapToBase(REGION_HEIGHT, region->GetY())
|
||||||
|
)
|
||||||
|
{
|
||||||
|
throw(std::runtime_error("Pushed region does not conform to the region grid"));
|
||||||
|
}
|
||||||
|
|
||||||
|
regionList.push_front(region);
|
||||||
|
return regionList.front();
|
||||||
|
}
|
||||||
|
|
||||||
Region* RegionPager::LoadRegion(int x, int y) {
|
Region* RegionPager::LoadRegion(int x, int y) {
|
||||||
//only work if using lua
|
//only work if using lua
|
||||||
if (!luaState) {
|
if (!luaState) {
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ public:
|
|||||||
//region manipulation
|
//region manipulation
|
||||||
Region* GetRegion(int x, int y);
|
Region* GetRegion(int x, int y);
|
||||||
Region* FindRegion(int x, int y);
|
Region* FindRegion(int x, int y);
|
||||||
|
Region* PushRegion(Region* const);
|
||||||
|
|
||||||
Region* LoadRegion(int x, int y);
|
Region* LoadRegion(int x, int y);
|
||||||
Region* SaveRegion(int x, int y);
|
Region* SaveRegion(int x, int y);
|
||||||
|
|||||||
Reference in New Issue
Block a user