From 4630b7e4039b510ecdc204f6c978191930ce3e87 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 27 Feb 2015 02:39:27 +1100 Subject: [PATCH] PumpCharacterUpdate() works from lua to client, read more There was a bug in the entity API, where getMotion() was pushing the origin instead of the motion. This has been corrected. Since this is an important bug, and because the features for this leg is finished, I'll merge this to master. --- client/gameplay_scenes/world.hpp | 1 + client/gameplay_scenes/world_characters.cpp | 18 ++++++++++ client/gameplay_scenes/world_logic.cpp | 3 ++ rsc/scripts/setup_server.lua | 12 +++++-- server/entities/entity_api.cpp | 4 +-- server/network_api.cpp | 39 ++++++++++++++++++++- server/server_character_methods.cpp | 9 +++-- 7 files changed, 79 insertions(+), 7 deletions(-) diff --git a/client/gameplay_scenes/world.hpp b/client/gameplay_scenes/world.hpp index bb57f75..a2af0a2 100644 --- a/client/gameplay_scenes/world.hpp +++ b/client/gameplay_scenes/world.hpp @@ -96,6 +96,7 @@ protected: void UpdateMap(); //character management + void hCharacterUpdate(CharacterPacket* const); void hCharacterCreate(CharacterPacket* const); void hCharacterDelete(CharacterPacket* const); void hQueryCharacterExists(CharacterPacket* const); diff --git a/client/gameplay_scenes/world_characters.cpp b/client/gameplay_scenes/world_characters.cpp index 4091824..85a4dc2 100644 --- a/client/gameplay_scenes/world_characters.cpp +++ b/client/gameplay_scenes/world_characters.cpp @@ -35,6 +35,24 @@ //DOCS: new characters will result in create messages //DOCS: this client's character will exist in both (skipped) +void World::hCharacterUpdate(CharacterPacket* const argPacket) { + //TODO: (1) Authentication + //NOTE: applies to the local character too + + //check that this character exists + std::map::iterator characterIt = characterMap.find(argPacket->characterIndex); + if (characterIt != characterMap.end()) { + //update the origin and motion, if there's a difference + if (characterIt->second.GetOrigin() != argPacket->origin) { + characterIt->second.SetOrigin(argPacket->origin); + } + if (characterIt->second.GetMotion() != argPacket->motion) { + characterIt->second.SetMotion(argPacket->motion); + characterIt->second.CorrectSprite(); //only correct the sprite if the motion changes + } + } +} + void World::hCharacterCreate(CharacterPacket* const argPacket) { //prevent double message if (characterMap.find(argPacket->characterIndex) != characterMap.end()) { diff --git a/client/gameplay_scenes/world_logic.cpp b/client/gameplay_scenes/world_logic.cpp index 0a09c12..b766140 100644 --- a/client/gameplay_scenes/world_logic.cpp +++ b/client/gameplay_scenes/world_logic.cpp @@ -335,6 +335,9 @@ void World::HandlePacket(SerialPacket* const argPacket) { break; //character management + case SerialPacketType::CHARACTER_UPDATE: + hCharacterUpdate(static_cast(argPacket)); + break; case SerialPacketType::CHARACTER_CREATE: hCharacterCreate(static_cast(argPacket)); break; diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index bdb0974..f01924f 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -4,6 +4,7 @@ mapMaker = require("map_maker") mapSaver = require("map_saver") roomSystem = require("room_system") characterSystem = require("character_system") +networkSystem = require("network") local function dumpTable(t) print(t) @@ -18,12 +19,19 @@ roomSystem.RoomManager.SetOnCreate(function(room, index) --called ~60 times per second roomSystem.Room.SetOnTick(room, function(room) + --[[ local character = characterSystem.CharacterManager.GetCharacter("handle") if character ~= nil then --debugging - local x, y = characterSystem.Character.GetOrigin(character) - print("character.Origin(x, y): ", x, y) + local originX, originY = characterSystem.Character.GetOrigin(character) + local motionX, motionY = characterSystem.Character.GetMotion(character) + if motionY < 0 then + characterSystem.Character.SetMotion(character, motionX, 0) + networkSystem.PumpCharacterUpdate(character) + print("Sending: ", motionX, motionY) + end end + --]] end) end) diff --git a/server/entities/entity_api.cpp b/server/entities/entity_api.cpp index a7f71bd..8669417 100644 --- a/server/entities/entity_api.cpp +++ b/server/entities/entity_api.cpp @@ -56,8 +56,8 @@ static int getOrigin(lua_State* L) { static int getMotion(lua_State* L) { Entity* entity = static_cast(lua_touserdata(L, 1)); - lua_pushnumber(L, entity->GetOrigin().x); - lua_pushnumber(L, entity->GetOrigin().y); + lua_pushnumber(L, entity->GetMotion().x); + lua_pushnumber(L, entity->GetMotion().y); return 2; } diff --git a/server/network_api.cpp b/server/network_api.cpp index db385e4..9975cd0 100644 --- a/server/network_api.cpp +++ b/server/network_api.cpp @@ -21,8 +21,45 @@ */ #include "network_api.hpp" +#include "character_data.hpp" +#include "character_manager.hpp" +#include "server_utilities.hpp" + static int pumpCharacterUpdate(lua_State* L) { - return 0; + CharacterData* characterData = static_cast(lua_touserdata(L, 1)); + + //determine the character's index + int index = -1; + for (auto const& it : *CharacterManager::GetSingleton().GetContainer()) { + if(characterData == &it.second) { + index = it.first; + break; + } + } + + //signal an error + if (index == -1) { + lua_pushboolean(L, false); + return 1; + } + + //fill the packet with all of this character's data + CharacterPacket newPacket; + newPacket.type = SerialPacketType::CHARACTER_UPDATE; + newPacket.characterIndex = index; + strncpy(newPacket.handle, characterData->GetHandle().c_str(), PACKET_STRING_SIZE); + strncpy(newPacket.avatar, characterData->GetAvatar().c_str(), PACKET_STRING_SIZE); + newPacket.accountIndex = characterData->GetOwner(); + newPacket.roomIndex = characterData->GetRoomIndex(); + newPacket.origin = characterData->GetOrigin(); + newPacket.motion = characterData->GetMotion(); + + //pump to the room + pumpPacketProximity(&newPacket, characterData->GetRoomIndex()); + + //signal success + lua_pushboolean(L, true); + return 1; } static const luaL_Reg networkLib[] = { diff --git a/server/server_character_methods.cpp b/server/server_character_methods.cpp index 022095b..2d701cc 100644 --- a/server/server_character_methods.cpp +++ b/server/server_character_methods.cpp @@ -183,10 +183,15 @@ void ServerApplication::hCharacterUnload(CharacterPacket* const argPacket) { void ServerApplication::hCharacterMovement(CharacterPacket* const argPacket) { //get the specified objects AccountData* accountData = accountMgr.Get(argPacket->accountIndex); + + if (!accountData) { + throw(std::runtime_error("Failed to move a character, missing account")); + } + CharacterData* characterData = characterMgr.Get(argPacket->characterIndex); - if (!accountData || !characterData) { - throw(std::runtime_error("Failed to move a character, missing data")); + if (!characterData) { + throw(std::runtime_error("Failed to move a character, missing character")); } //get this account's client