Merge branch 'develop', read more

This merge covers mostly back-end refactoring, including splitting
Character into Renderable and BaseCharacter, as well as creating the
manager interface.
This commit is contained in:
Kayne Ruse
2014-10-18 23:45:57 +11:00
39 changed files with 535 additions and 346 deletions
+5 -9
View File
@@ -38,8 +38,7 @@
#include "options_menu.hpp" #include "options_menu.hpp"
#include "lobby_menu.hpp" #include "lobby_menu.hpp"
#include "in_world.hpp" #include "in_world.hpp"
//#include "in_combat.hpp" #include "disconnected_screen.hpp"
#include "clean_up.hpp"
//------------------------- //-------------------------
//Public access members //Public access members
@@ -179,16 +178,13 @@ void ClientApplication::LoadScene(SceneList sceneIndex) {
activeScene = new OptionsMenu(); activeScene = new OptionsMenu();
break; break;
case SceneList::LOBBYMENU: case SceneList::LOBBYMENU:
activeScene = new LobbyMenu(&clientIndex, &accountIndex); activeScene = new LobbyMenu(&clientIndex, &accountIndex); //TODO: can I use the ConfigUtility for these parameters?
break; break;
case SceneList::INWORLD: case SceneList::INWORLD:
activeScene = new InWorld(&clientIndex, &accountIndex, &characterIndex, &characterMap); activeScene = new InWorld(&clientIndex, &accountIndex);
break; break;
// case SceneList::INCOMBAT: case SceneList::DISCONNECTEDSCREEN:
// activeScene = new InCombat(&clientIndex, &accountIndex, &characterIndex, &characterMap); activeScene = new DisconnectedScreen();
// break;
case SceneList::CLEANUP:
activeScene = new CleanUp(&clientIndex, &accountIndex, &characterIndex, &characterMap);
break; break;
default: default:
throw(std::logic_error("Failed to recognize the scene index")); throw(std::logic_error("Failed to recognize the scene index"));
-4
View File
@@ -26,7 +26,6 @@
#include "base_scene.hpp" #include "base_scene.hpp"
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
#include "character.hpp"
#include "singleton.hpp" #include "singleton.hpp"
@@ -54,9 +53,6 @@ private:
//shared parameters //shared parameters
int clientIndex = -1; int clientIndex = -1;
int accountIndex = -1; int accountIndex = -1;
int characterIndex = -1;
CharacterMap characterMap;
}; };
#endif #endif
+1 -1
View File
@@ -12,7 +12,7 @@ OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
#output #output
OUTDIR=.. OUTDIR=..
OUT=$(addprefix $(OUTDIR)/,server.a) OUT=$(addprefix $(OUTDIR)/,client.a)
#targets #targets
all: $(OBJ) $(OUT) all: $(OBJ) $(OUT)
+2 -1
View File
@@ -1,5 +1,5 @@
#config #config
INCLUDES+=. client_utilities scenes ../common/debugging ../common/gameplay ../common/graphics ../common/map ../common/network ../common/network/packet_types ../common/ui ../common/utilities INCLUDES+=. client_utilities renderable scenes ../common/debugging ../common/gameplay ../common/graphics ../common/map ../common/network ../common/network/packet_types ../common/ui ../common/utilities
LIBS+=client.a ../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua LIBS+=client.a ../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
@@ -18,6 +18,7 @@ OUT=$(addprefix $(OUTDIR)/,client)
all: $(OBJ) $(OUT) all: $(OBJ) $(OUT)
$(MAKE) -C client_utilities $(MAKE) -C client_utilities
$(MAKE) -C scenes $(MAKE) -C scenes
$(MAKE) -C renderable
$(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIBS) $(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIBS)
$(OBJ): | $(OBJDIR) $(OBJ): | $(OBJDIR)
@@ -19,23 +19,9 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#include "character.hpp" #include "base_character.hpp"
void Character::Update() { void BaseCharacter::CorrectSprite() {
if (motion.x && motion.y) {
origin += motion * CHARACTER_WALKING_MOD;
}
else if (motion != 0) {
origin += motion;
}
sprite.Update(0.016);
}
void Character::DrawTo(SDL_Surface* const dest, int camX, int camY) {
sprite.DrawTo(dest, origin.x - camX, origin.y - camY);
}
void Character::CorrectSprite() {
//NOTE: These must correspond to the sprite sheet in use //NOTE: These must correspond to the sprite sheet in use
if (motion.y > 0) { if (motion.y > 0) {
sprite.SetYIndex(0); sprite.SetYIndex(0);
@@ -19,38 +19,23 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#ifndef CHARACTER_HPP_ #ifndef BASECHARACTER_HPP_
#define CHARACTER_HPP_ #define BASECHARACTER_HPP_
//components //components
#include "character_defines.hpp" #include "character_defines.hpp"
#include "vector2.hpp" #include "renderable.hpp"
#include "bounding_box.hpp"
#include "statistics.hpp"
//graphics
#include "sprite_sheet.hpp"
//std namespace //std namespace
#include <string> #include <string>
#include <cmath>
class Character { class BaseCharacter : public Renderable {
public: public:
Character() = default; BaseCharacter() = default;
~Character() = default; virtual ~BaseCharacter() = default;
void Update();
//graphics //graphics
void DrawTo(SDL_Surface* const, int camX, int camY);
void CorrectSprite(); void CorrectSprite();
SpriteSheet* GetSprite() { return &sprite; }
//gameplay
Statistics* GetStats() { return &stats; }
//accessors and mutators
//metadata //metadata
int SetOwner(int i) { return owner = i; } int SetOwner(int i) { return owner = i; }
@@ -60,36 +45,11 @@ public:
std::string SetAvatar(std::string s) { return avatar = s; } std::string SetAvatar(std::string s) { return avatar = s; }
std::string GetAvatar() const { return avatar; } std::string GetAvatar() const { return avatar; }
//position
Vector2 SetOrigin(Vector2 v) { return origin = v; }
Vector2 GetOrigin() const { return origin; }
Vector2 SetMotion(Vector2 v) { return motion = v; }
Vector2 GetMotion() const { return motion; }
BoundingBox SetBounds(BoundingBox b) { return bounds = b; }
BoundingBox GetBounds() { return bounds; }
private: private:
//graphics
SpriteSheet sprite;
//base statistics
Statistics stats;
//gameplay components: equipment, items, buffs, debuffs...
//metadata //metadata
int owner; int owner;
std::string handle; std::string handle;
std::string avatar; std::string avatar;
//position
Vector2 origin = {0.0,0.0};
Vector2 motion = {0.0,0.0};
BoundingBox bounds;
}; };
//tmp
#include <map>
typedef std::map<int, Character> CharacterMap;
#endif #endif
+23
View File
@@ -0,0 +1,23 @@
/* Copyright: (c) Kayne Ruse 2014
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#include "base_monster.hpp"
@@ -19,16 +19,18 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#ifndef COMBATDEFINES_HPP_ #ifndef BASEMONSTER_HPP_
#define COMBATDEFINES_HPP_ #define BASEMONSTER_HPP_
#define COMBAT_MAX_CHARACTERS 16 #include "renderable.hpp"
#define COMBAT_MAX_ENEMIES 16
enum class TerrainType { class BaseMonster {
NONE = 0, public:
GRASSLANDS, BaseMonster();
//etc. virtual ~BaseMonster();
private:
//
}; };
#endif #endif
+23
View File
@@ -0,0 +1,23 @@
/* Copyright: (c) Kayne Ruse 2014
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#include "local_character.hpp"
+40
View File
@@ -0,0 +1,40 @@
/* Copyright: (c) Kayne Ruse 2014
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#ifndef LOCALCHARACTER_HPP_
#define LOCALCHARACTER_HPP_
#include "base_character.hpp"
#include "statistics.hpp"
class LocalCharacter : public BaseCharacter {
public:
LocalCharacter() = default;
~LocalCharacter() = default;
Statistics* GetBaseStats() { return &baseStats; }
private:
Statistics baseStats;
//TODO: weapons, armour, buffs, debuffs, etc.
};
#endif
+37
View File
@@ -0,0 +1,37 @@
#config
INCLUDES+=. .. ../../common/gameplay ../../common/graphics ../../common/utilities
LIBS+=
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
#source
CXXSRC=$(wildcard *.cpp)
#objects
OBJDIR=obj
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
#output
OUTDIR=..
OUT=$(addprefix $(OUTDIR)/,client.a)
#targets
all: $(OBJ) $(OUT)
ar -crs $(OUT) $(OBJ)
$(OBJ): | $(OBJDIR)
$(OUT): | $(OUTDIR)
$(OBJDIR):
mkdir $(OBJDIR)
$(OUTDIR):
mkdir $(OUTDIR)
$(OBJDIR)/%.o: %.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<
clean:
$(RM) *.o *.a *.exe
rebuild: clean all
+31
View File
@@ -0,0 +1,31 @@
/* Copyright: (c) Kayne Ruse 2014
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#include "renderable.hpp"
void Renderable::Update() {
origin += motion;
sprite.Update(0.016);
}
void Renderable::DrawTo(SDL_Surface* const dest, int camX, int camY) {
sprite.DrawTo(dest, origin.x - camX, origin.y - camY);
}
+56
View File
@@ -0,0 +1,56 @@
/* Copyright: (c) Kayne Ruse 2014
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#ifndef RENDERABLE_HPP_
#define RENDERABLE_HPP_
#include "bounding_box.hpp"
#include "sprite_sheet.hpp"
#include "vector2.hpp"
class Renderable {
public:
Renderable() = default;
virtual ~Renderable() = default;
virtual void Update();
virtual void DrawTo(SDL_Surface* const, int camX, int camY);
SpriteSheet* GetSprite() { return &sprite; }
//position
Vector2 SetOrigin(Vector2 v) { return origin = v; }
Vector2 GetOrigin() const { return origin; }
Vector2 SetMotion(Vector2 v) { return motion = v; }
Vector2 GetMotion() const { return motion; }
//collision
BoundingBox SetBounds(BoundingBox b) { return bounds = b; }
BoundingBox GetBounds() { return bounds; }
protected: //TODO: should be private
SpriteSheet sprite;
Vector2 origin = {0, 0};
Vector2 motion = {0, 0};
BoundingBox bounds;
};
#endif
+1 -2
View File
@@ -34,8 +34,7 @@ enum class SceneList {
OPTIONSMENU, OPTIONSMENU,
LOBBYMENU, LOBBYMENU,
INWORLD, INWORLD,
INCOMBAT, DISCONNECTEDSCREEN,
CLEANUP,
}; };
#endif #endif
@@ -19,10 +19,11 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#include "clean_up.hpp" #include "disconnected_screen.hpp"
#include "channels.hpp" #include "channels.hpp"
#include "config_utility.hpp" #include "config_utility.hpp"
#include "udp_network_utility.hpp"
#include <stdexcept> #include <stdexcept>
@@ -30,17 +31,7 @@
//Public access members //Public access members
//------------------------- //-------------------------
CleanUp::CleanUp( DisconnectedScreen::DisconnectedScreen() {
int* const argClientIndex,
int* const argAccountIndex,
int* const argCharacterIndex,
CharacterMap* argCharacterMap
):
clientIndex(*argClientIndex),
accountIndex(*argAccountIndex),
characterIndex(*argCharacterIndex),
characterMap(*argCharacterMap)
{
ConfigUtility& config = ConfigUtility::GetSingleton(); ConfigUtility& config = ConfigUtility::GetSingleton();
//setup the utility objects //setup the utility objects
@@ -60,19 +51,13 @@ CleanUp::CleanUp(
backButton.SetText("Back"); backButton.SetText("Back");
//full reset //full reset
network.Unbind(Channels::SERVER); UDPNetworkUtility::GetSingleton().Unbind(Channels::SERVER);
clientIndex = -1;
accountIndex = -1;
characterIndex = -1;
// combatMap.clear();
characterMap.clear();
// enemyMap.clear();
//auto return //auto return
startTick = std::chrono::steady_clock::now(); startTick = std::chrono::steady_clock::now();
} }
CleanUp::~CleanUp() { DisconnectedScreen::~DisconnectedScreen() {
// //
} }
@@ -80,16 +65,16 @@ CleanUp::~CleanUp() {
//Frame loop //Frame loop
//------------------------- //-------------------------
void CleanUp::Update() { void DisconnectedScreen::Update() {
if (std::chrono::steady_clock::now() - startTick > std::chrono::duration<int>(10)) { if (std::chrono::steady_clock::now() - startTick > std::chrono::duration<int>(10)) {
SetNextScene(SceneList::MAINMENU); SetNextScene(SceneList::MAINMENU);
} }
//Eat incoming packets //Eat incoming packets
while(network.Receive()); while(UDPNetworkUtility::GetSingleton().Receive());
} }
void CleanUp::Render(SDL_Surface* const screen) { void DisconnectedScreen::Render(SDL_Surface* const screen) {
ConfigUtility& config = ConfigUtility::GetSingleton(); ConfigUtility& config = ConfigUtility::GetSingleton();
backButton.DrawTo(screen); backButton.DrawTo(screen);
@@ -100,25 +85,25 @@ void CleanUp::Render(SDL_Surface* const screen) {
//Event handlers //Event handlers
//------------------------- //-------------------------
void CleanUp::QuitEvent() { void DisconnectedScreen::QuitEvent() {
SetNextScene(SceneList::QUIT); SetNextScene(SceneList::QUIT);
} }
void CleanUp::MouseMotion(SDL_MouseMotionEvent const& motion) { void DisconnectedScreen::MouseMotion(SDL_MouseMotionEvent const& motion) {
backButton.MouseMotion(motion); backButton.MouseMotion(motion);
} }
void CleanUp::MouseButtonDown(SDL_MouseButtonEvent const& button) { void DisconnectedScreen::MouseButtonDown(SDL_MouseButtonEvent const& button) {
backButton.MouseButtonDown(button); backButton.MouseButtonDown(button);
} }
void CleanUp::MouseButtonUp(SDL_MouseButtonEvent const& button) { void DisconnectedScreen::MouseButtonUp(SDL_MouseButtonEvent const& button) {
if (backButton.MouseButtonUp(button) == Button::State::HOVER) { if (backButton.MouseButtonUp(button) == Button::State::HOVER) {
SetNextScene(SceneList::MAINMENU); SetNextScene(SceneList::MAINMENU);
} }
} }
void CleanUp::KeyDown(SDL_KeyboardEvent const& key) { void DisconnectedScreen::KeyDown(SDL_KeyboardEvent const& key) {
switch(key.keysym.sym) { switch(key.keysym.sym) {
case SDLK_ESCAPE: case SDLK_ESCAPE:
SetNextScene(SceneList::MAINMENU); SetNextScene(SceneList::MAINMENU);
@@ -126,6 +111,6 @@ void CleanUp::KeyDown(SDL_KeyboardEvent const& key) {
} }
} }
void CleanUp::KeyUp(SDL_KeyboardEvent const& key) { void DisconnectedScreen::KeyUp(SDL_KeyboardEvent const& key) {
// //
} }
@@ -19,11 +19,8 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#ifndef CLEANUP_HPP_ #ifndef DISCONNECTEDSCREEN_HPP_
#define CLEANUP_HPP_ #define DISCONNECTEDSCREEN_HPP_
//network
#include "udp_network_utility.hpp"
//graphics //graphics
#include "image.hpp" #include "image.hpp"
@@ -31,22 +28,16 @@
#include "button.hpp" #include "button.hpp"
//client //client
#include "character.hpp"
#include "base_scene.hpp" #include "base_scene.hpp"
//std namespace //std namespace
#include <chrono> #include <chrono>
class CleanUp : public BaseScene { class DisconnectedScreen : public BaseScene {
public: public:
//Public access members //Public access members
CleanUp( DisconnectedScreen();
int* const argClientIndex, ~DisconnectedScreen();
int* const argAccountIndex,
int* const argCharacterIndex,
CharacterMap* argCharacterMap
);
~CleanUp();
protected: protected:
//Frame loop //Frame loop
@@ -61,13 +52,6 @@ protected:
void KeyDown(SDL_KeyboardEvent const&); void KeyDown(SDL_KeyboardEvent const&);
void KeyUp(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&);
//shared parameters
UDPNetworkUtility& network = UDPNetworkUtility::GetSingleton();
int& clientIndex;
int& accountIndex;
int& characterIndex;
CharacterMap& characterMap;
//graphics //graphics
Image image; Image image;
RasterFont font; RasterFont font;
+9 -16
View File
@@ -34,16 +34,9 @@
//Public access members //Public access members
//------------------------- //-------------------------
InWorld::InWorld( InWorld::InWorld(int* const argClientIndex, int* const argAccountIndex):
int* const argClientIndex,
int* const argAccountIndex,
int* const argCharacterIndex,
CharacterMap* argCharacterMap
):
clientIndex(*argClientIndex), clientIndex(*argClientIndex),
accountIndex(*argAccountIndex), accountIndex(*argAccountIndex)
characterIndex(*argCharacterIndex),
characterMap(*argCharacterMap)
{ {
ConfigUtility& config = ConfigUtility::GetSingleton(); ConfigUtility& config = ConfigUtility::GetSingleton();
@@ -153,7 +146,7 @@ void InWorld::Update() {
if (Clock::now() - lastBeat > std::chrono::seconds(3)) { if (Clock::now() - lastBeat > std::chrono::seconds(3)) {
if (attemptedBeats > 2) { if (attemptedBeats > 2) {
RequestDisconnect(); RequestDisconnect();
SetNextScene(SceneList::CLEANUP); SetNextScene(SceneList::DISCONNECTEDSCREEN);
ConfigUtility::GetSingleton()["client.disconnectMessage"] = "Error: Lost connection to the server"; ConfigUtility::GetSingleton()["client.disconnectMessage"] = "Error: Lost connection to the server";
} }
@@ -342,7 +335,7 @@ void InWorld::HandlePong(ServerPacket* const argPacket) {
void InWorld::HandleDisconnect(ClientPacket* const argPacket) { void InWorld::HandleDisconnect(ClientPacket* const argPacket) {
//TODO: More needed in the disconnection //TODO: More needed in the disconnection
SetNextScene(SceneList::CLEANUP); SetNextScene(SceneList::DISCONNECTEDSCREEN);
ConfigUtility::GetSingleton()["client.disconnectMessage"] = "You have been disconnected"; ConfigUtility::GetSingleton()["client.disconnectMessage"] = "You have been disconnected";
} }
@@ -352,7 +345,7 @@ void InWorld::HandleCharacterNew(CharacterPacket* const argPacket) {
} }
//create the character object //create the character object
Character& newCharacter = characterMap[argPacket->characterIndex]; BaseCharacter& newCharacter = characterMap[argPacket->characterIndex];
//fill out the character's members //fill out the character's members
newCharacter.SetHandle(argPacket->handle); newCharacter.SetHandle(argPacket->handle);
@@ -369,7 +362,7 @@ void InWorld::HandleCharacterNew(CharacterPacket* const argPacket) {
CHARACTER_BOUNDS_HEIGHT CHARACTER_BOUNDS_HEIGHT
}); });
(*newCharacter.GetStats()) = argPacket->stats; // (*newCharacter.GetBaseStats()) = argPacket->stats;
//bookkeeping code //bookkeeping code
newCharacter.CorrectSprite(); newCharacter.CorrectSprite();
@@ -407,7 +400,7 @@ void InWorld::HandleCharacterUpdate(CharacterPacket* const argPacket) {
return; return;
} }
Character& character = characterMap[argPacket->characterIndex]; BaseCharacter& character = characterMap[argPacket->characterIndex];
//other characters moving //other characters moving
if (argPacket->characterIndex != characterIndex) { if (argPacket->characterIndex != characterIndex) {
@@ -419,7 +412,7 @@ void InWorld::HandleCharacterUpdate(CharacterPacket* const argPacket) {
void InWorld::HandleCharacterRejection(TextPacket* const argPacket) { void InWorld::HandleCharacterRejection(TextPacket* const argPacket) {
RequestDisconnect(); RequestDisconnect();
SetNextScene(SceneList::CLEANUP); SetNextScene(SceneList::DISCONNECTEDSCREEN);
ConfigUtility& config = ConfigUtility::GetSingleton(); ConfigUtility& config = ConfigUtility::GetSingleton();
config["client.disconnectMessage"] = "Error: "; config["client.disconnectMessage"] = "Error: ";
config["client.disconnectMessage"] += argPacket->text; config["client.disconnectMessage"] += argPacket->text;
@@ -464,7 +457,7 @@ void InWorld::SendPlayerUpdate() {
newPacket.roomIndex = 0; //TODO: room index newPacket.roomIndex = 0; //TODO: room index
newPacket.origin = localCharacter->GetOrigin(); newPacket.origin = localCharacter->GetOrigin();
newPacket.motion = localCharacter->GetMotion(); newPacket.motion = localCharacter->GetMotion();
newPacket.stats = *localCharacter->GetStats(); // newPacket.stats = *localCharacter->GetBaseStats();
//TODO: gameplay components: equipment, items, buffs, debuffs //TODO: gameplay components: equipment, items, buffs, debuffs
+6 -10
View File
@@ -38,7 +38,8 @@
//common //common
#include "frame_rate.hpp" #include "frame_rate.hpp"
#include "character.hpp" #include "base_character.hpp"
#include "local_character.hpp"
//client //client
#include "base_scene.hpp" #include "base_scene.hpp"
@@ -51,12 +52,7 @@
class InWorld : public BaseScene { class InWorld : public BaseScene {
public: public:
//Public access members //Public access members
InWorld( InWorld(int* const argClientIndex, int* const argAccountIndex);
int* const argClientIndex,
int* const argAccountIndex,
int* const argCharacterIndex,
CharacterMap* argCharacterMap
);
~InWorld(); ~InWorld();
protected: protected:
@@ -100,8 +96,8 @@ protected:
UDPNetworkUtility& network = UDPNetworkUtility::GetSingleton(); UDPNetworkUtility& network = UDPNetworkUtility::GetSingleton();
int& clientIndex; int& clientIndex;
int& accountIndex; int& accountIndex;
int& characterIndex; int characterIndex = -1;
CharacterMap& characterMap; std::map<int, BaseCharacter> characterMap;
//graphics //graphics
Image buttonImage; Image buttonImage;
@@ -124,7 +120,7 @@ protected:
FrameRate fps; FrameRate fps;
//game //game
Character* localCharacter = nullptr; BaseCharacter* localCharacter = nullptr;
//connections //connections
//TODO: This needs it's own utility, for both InWorld and InCombat //TODO: This needs it's own utility, for both InWorld and InCombat
+4
View File
@@ -34,6 +34,10 @@ LobbyMenu::LobbyMenu(int* const argClientIndex, int* const argAccountIndex):
clientIndex(*argClientIndex), clientIndex(*argClientIndex),
accountIndex(*argAccountIndex) accountIndex(*argAccountIndex)
{ {
//preemptive reset
clientIndex = -1;
accountIndex = -1;
//setup the utility objects //setup the utility objects
image.LoadSurface(config["dir.interface"] + "button_menu.bmp"); image.LoadSurface(config["dir.interface"] + "button_menu.bmp");
image.SetClipH(image.GetClipH()/3); image.SetClipH(image.GetClipH()/3);
+1 -1
View File
@@ -1,5 +1,5 @@
#config #config
INCLUDES+=. .. ../../common/gameplay ../../common/graphics ../../common/map ../../common/network ../../common/network/packet_types ../../common/ui ../../common/utilities INCLUDES+=. .. ../renderable ../../common/gameplay ../../common/graphics ../../common/map ../../common/network ../../common/network/packet_types ../../common/ui ../../common/utilities
LIBS+= LIBS+=
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
+1
View File
@@ -27,6 +27,7 @@
//the speeds that the characters move //the speeds that the characters move
constexpr double CHARACTER_WALKING_SPEED = 2.24; constexpr double CHARACTER_WALKING_SPEED = 2.24;
constexpr double CHARACTER_WALKING_MOD = 1.0/sqrt(2.0); constexpr double CHARACTER_WALKING_MOD = 1.0/sqrt(2.0);
constexpr double CHARACTER_WALKING_NEGATIVE_MOD = 1.0 - CHARACTER_WALKING_MOD;
//the bounds for the character objects, mapped to the default sprites //the bounds for the character objects, mapped to the default sprites
constexpr int CHARACTER_BOUNDS_X = 0; constexpr int CHARACTER_BOUNDS_X = 0;
+3 -3
View File
@@ -53,15 +53,15 @@ void ConfigUtility::Load(std::string fname, int argc, char* argv[]) {
} }
//set some specific values //set some specific values
if (!strncmp(argv[i], "-C", 2)) { if (!strncmp(argv[i], "-", 1)) {
//wipe the variables //wipe the variables
memset(key, 0, 256); memset(key, 0, 256);
memset(key, 0, 256); memset(key, 0, 256);
//read the key-value pair //read the key-value pair
if (sscanf(argv[i], "-C%[^=]=%[^\0]", key, val) != 2) { if (sscanf(argv[i], "-%[^=]=%[^\0]", key, val) != 2) {
std::ostringstream os; std::ostringstream os;
os << "Failed to read a command line config argument (expected -C%s=%s):" << std::endl; os << "Failed to read a command line config argument (expected -%s=%s):" << std::endl;
os << "\targv[" << i << "]: " << argv[i] << std::endl; os << "\targv[" << i << "]: " << argv[i] << std::endl;
os << "\tkey: " << key << std::endl; os << "\tkey: " << key << std::endl;
os << "\tval: " << val << std::endl; os << "\tval: " << val << std::endl;
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 784 B

+19 -17
View File
@@ -87,13 +87,13 @@ int AccountManager::Load(std::string username, int clientIndex) {
int uid = sqlite3_column_int(statement, 0); int uid = sqlite3_column_int(statement, 0);
//check to see if this account is already loaded //check to see if this account is already loaded
if (accountMap.find(uid) != accountMap.end()) { if (elementMap.find(uid) != elementMap.end()) {
sqlite3_finalize(statement); sqlite3_finalize(statement);
return -1; return -1;
} }
//extract the data into memory //extract the data into memory
AccountData& newAccount = accountMap[uid]; AccountData& newAccount = elementMap[uid];
newAccount.username = reinterpret_cast<const char*>(sqlite3_column_text(statement, 1)); newAccount.username = reinterpret_cast<const char*>(sqlite3_column_text(statement, 1));
newAccount.blackListed = sqlite3_column_int(statement, 2); newAccount.blackListed = sqlite3_column_int(statement, 2);
newAccount.whiteListed = sqlite3_column_int(statement, 3); newAccount.whiteListed = sqlite3_column_int(statement, 3);
@@ -121,11 +121,11 @@ int AccountManager::Save(int uid) {
//DOCS: To use this method, change the in-memory copy, and then call this function using that object's UID. //DOCS: To use this method, change the in-memory copy, and then call this function using that object's UID.
//this method fails if this account is not loaded //this method fails if this account is not loaded
if (accountMap.find(uid) == accountMap.end()) { if (elementMap.find(uid) == elementMap.end()) {
return -1; return -1;
} }
AccountData& account = accountMap[uid]; AccountData& account = elementMap[uid];
sqlite3_stmt* statement = nullptr; sqlite3_stmt* statement = nullptr;
//prep //prep
@@ -163,7 +163,7 @@ void AccountManager::Unload(int uid) {
//save this user account, and then unload it //save this user account, and then unload it
//NOTE: the associated characters are unloaded externally //NOTE: the associated characters are unloaded externally
Save(uid); Save(uid);
accountMap.erase(uid); elementMap.erase(uid);
} }
void AccountManager::Delete(int uid) { void AccountManager::Delete(int uid) {
@@ -190,25 +190,27 @@ void AccountManager::Delete(int uid) {
//finish the routine //finish the routine
sqlite3_finalize(statement); sqlite3_finalize(statement);
accountMap.erase(uid); elementMap.erase(uid);
} }
void AccountManager::UnloadAll() { void AccountManager::UnloadAll() {
for (auto& it : accountMap) { for (auto& it : elementMap) {
Save(it.first); Save(it.first);
} }
accountMap.clear(); elementMap.clear();
} }
void AccountManager::UnloadIf(std::function<bool(std::pair<int, AccountData>)> fn) { void AccountManager::UnloadIf(std::function<bool(std::pair<const int, AccountData>)> fn) {
//replicate std::remove_if, using custom code //replicate std::remove_if, using custom code
for (std::map<int, AccountData>::iterator it = accountMap.begin(); it != accountMap.end(); /* empty */) { std::map<int, AccountData>::iterator it = elementMap.begin();
while (it != elementMap.end()) {
if (fn(*it)) { if (fn(*it)) {
Save(it->first); Save(it->first);
it = accountMap.erase(it); it = elementMap.erase(it);
continue; }
else {
++it;
} }
++it;
} }
} }
@@ -218,9 +220,9 @@ void AccountManager::UnloadIf(std::function<bool(std::pair<int, AccountData>)> f
AccountData* AccountManager::Get(int uid) { AccountData* AccountManager::Get(int uid) {
//TODO: could this load an account first? //TODO: could this load an account first?
std::map<int, AccountData>::iterator it = accountMap.find(uid); std::map<int, AccountData>::iterator it = elementMap.find(uid);
if (it == accountMap.end()) { if (it == elementMap.end()) {
return nullptr; return nullptr;
} }
@@ -228,7 +230,7 @@ AccountData* AccountManager::Get(int uid) {
} }
int AccountManager::GetLoadedCount() { int AccountManager::GetLoadedCount() {
return accountMap.size(); return elementMap.size();
} }
int AccountManager::GetTotalCount() { int AccountManager::GetTotalCount() {
@@ -250,7 +252,7 @@ int AccountManager::GetTotalCount() {
} }
std::map<int, AccountData>* AccountManager::GetContainer() { std::map<int, AccountData>* AccountManager::GetContainer() {
return &accountMap; return &elementMap;
} }
sqlite3* AccountManager::SetDatabase(sqlite3* db) { sqlite3* AccountManager::SetDatabase(sqlite3* db) {
+17 -14
View File
@@ -24,29 +24,33 @@
#include "account_data.hpp" #include "account_data.hpp"
#include "singleton.hpp" #include "singleton.hpp"
#include "manager_interface.hpp"
#include "sqlite3/sqlite3.h" #include "sqlite3/sqlite3.h"
#include <functional> #include <functional>
#include <map> #include <map>
class AccountManager : public Singleton<AccountManager> { class AccountManager:
public Singleton<AccountManager>,
public ManagerInterface<AccountData, std::string, int>
{
public: public:
//public access methods //common public methods
int Create(std::string username, int clientIndex); int Create(std::string username, int clientIndex) override;
int Load(std::string username, int clientIndex); int Load(std::string username, int clientIndex) override;
int Save(int uid); int Save(int uid) override;
void Unload(int uid); void Unload(int uid) override;
void Delete(int uid); void Delete(int uid) override;
void UnloadAll(); void UnloadAll() override;
void UnloadIf(std::function<bool(std::pair<int, AccountData>)> fn); void UnloadIf(std::function<bool(std::pair<const int, AccountData>)> fn) override;
//accessors and mutators //accessors and mutators
AccountData* Get(int uid); AccountData* Get(int uid) override;
int GetLoadedCount(); int GetLoadedCount() override;
int GetTotalCount(); int GetTotalCount() override;
std::map<int, AccountData>* GetContainer(); std::map<int, AccountData>* GetContainer() override;
sqlite3* SetDatabase(sqlite3* db); sqlite3* SetDatabase(sqlite3* db);
sqlite3* GetDatabase(); sqlite3* GetDatabase();
@@ -57,7 +61,6 @@ private:
AccountManager() = default; AccountManager() = default;
~AccountManager() = default; ~AccountManager() = default;
std::map<int, AccountData> accountMap;
sqlite3* database = nullptr; sqlite3* database = nullptr;
}; };
+1 -1
View File
@@ -1,5 +1,5 @@
#config #config
INCLUDES+=. ../../common/utilities INCLUDES+=. ../server_utilities ../../common/utilities
LIBS+= LIBS+=
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
+19 -18
View File
@@ -98,7 +98,7 @@ int CharacterManager::Load(int owner, std::string handle, std::string avatar) {
int uid = sqlite3_column_int(statement, 0); int uid = sqlite3_column_int(statement, 0);
//check to see if this character is already loaded //check to see if this character is already loaded
if (characterMap.find(uid) != characterMap.end()) { if (elementMap.find(uid) != elementMap.end()) {
sqlite3_finalize(statement); sqlite3_finalize(statement);
return -1; return -1;
} }
@@ -110,7 +110,7 @@ int CharacterManager::Load(int owner, std::string handle, std::string avatar) {
} }
//extract the data into memory //extract the data into memory
CharacterData& newChar = characterMap[uid]; CharacterData& newChar = elementMap[uid];
//metadata //metadata
newChar.owner = owner; newChar.owner = owner;
@@ -145,11 +145,11 @@ int CharacterManager::Save(int uid) {
//DOCS: To use this method, change the in-memory copy, and then call this function using that object's UID. //DOCS: To use this method, change the in-memory copy, and then call this function using that object's UID.
//this method fails if this character is not loaded //this method fails if this character is not loaded
if (characterMap.find(uid) == characterMap.end()) { if (elementMap.find(uid) == elementMap.end()) {
return -1; return -1;
} }
CharacterData& character = characterMap[uid]; CharacterData& character = elementMap[uid];
sqlite3_stmt* statement = nullptr; sqlite3_stmt* statement = nullptr;
//prep //prep
@@ -187,7 +187,7 @@ int CharacterManager::Save(int uid) {
void CharacterManager::Unload(int uid) { void CharacterManager::Unload(int uid) {
//save this character, then unload it //save this character, then unload it
Save(uid); Save(uid);
characterMap.erase(uid); elementMap.erase(uid);
} }
void CharacterManager::Delete(int uid) { void CharacterManager::Delete(int uid) {
@@ -213,25 +213,26 @@ void CharacterManager::Delete(int uid) {
//finish the routine //finish the routine
sqlite3_finalize(statement); sqlite3_finalize(statement);
characterMap.erase(uid); elementMap.erase(uid);
} }
void CharacterManager::UnloadAll() { void CharacterManager::UnloadAll() {
for (auto& it : characterMap) { for (auto& it : elementMap) {
Save(it.first); Save(it.first);
} }
characterMap.clear(); elementMap.clear();
} }
void CharacterManager::UnloadIf(std::function<bool(std::pair<int, CharacterData>)> fn) { void CharacterManager::UnloadIf(std::function<bool(std::pair<const int, CharacterData>)> fn) {
//replicate std::remove_if, using custom code std::map<int, CharacterData>::iterator it = elementMap.begin();
for (std::map<int, CharacterData>::iterator it = characterMap.begin(); it != characterMap.end(); /* empty */) { while (it != elementMap.end()) {
if (fn(*it)) { if (fn(*it)) {
Save(it->first); Save(it->first);
it = characterMap.erase(it); it = elementMap.erase(it);
continue; }
else {
++it;
} }
++it;
} }
} }
@@ -240,9 +241,9 @@ void CharacterManager::UnloadIf(std::function<bool(std::pair<int, CharacterData>
//------------------------- //-------------------------
CharacterData* CharacterManager::Get(int uid) { CharacterData* CharacterManager::Get(int uid) {
std::map<int, CharacterData>::iterator it = characterMap.find(uid); std::map<int, CharacterData>::iterator it = elementMap.find(uid);
if (it == characterMap.end()) { if (it == elementMap.end()) {
return nullptr; return nullptr;
} }
@@ -250,7 +251,7 @@ CharacterData* CharacterManager::Get(int uid) {
} }
int CharacterManager::GetLoadedCount() { int CharacterManager::GetLoadedCount() {
return characterMap.size(); return elementMap.size();
} }
int CharacterManager::GetTotalCount() { int CharacterManager::GetTotalCount() {
@@ -272,7 +273,7 @@ int CharacterManager::GetTotalCount() {
} }
std::map<int, CharacterData>* CharacterManager::GetContainer() { std::map<int, CharacterData>* CharacterManager::GetContainer() {
return &characterMap; return &elementMap;
} }
sqlite3* CharacterManager::SetDatabase(sqlite3* db) { sqlite3* CharacterManager::SetDatabase(sqlite3* db) {
+17 -14
View File
@@ -24,29 +24,33 @@
#include "character_data.hpp" #include "character_data.hpp"
#include "singleton.hpp" #include "singleton.hpp"
#include "manager_interface.hpp"
#include "sqlite3/sqlite3.h" #include "sqlite3/sqlite3.h"
#include <functional> #include <functional>
#include <map> #include <map>
class CharacterManager : public Singleton<CharacterManager> { class CharacterManager:
public Singleton<CharacterManager>,
public ManagerInterface<CharacterData, int, std::string, std::string>
{
public: public:
//public access methods //common public methods
int Create(int owner, std::string handle, std::string avatar); int Create(int owner, std::string handle, std::string avatar) override;
int Load(int owner, std::string handle, std::string avatar); int Load(int owner, std::string handle, std::string avatar) override;
int Save(int uid); int Save(int uid) override;
void Unload(int uid); void Unload(int uid) override;
void Delete(int uid); void Delete(int uid) override;
void UnloadAll(); void UnloadAll() override;
void UnloadIf(std::function<bool(std::pair<int, CharacterData>)> fn); void UnloadIf(std::function<bool(std::pair<const int, CharacterData>)> fn) override;
//accessors and mutators //accessors and mutators
CharacterData* Get(int uid); CharacterData* Get(int uid) override;
int GetLoadedCount(); int GetLoadedCount() override;
int GetTotalCount(); int GetTotalCount() override;
std::map<int, CharacterData>* GetContainer(); std::map<int, CharacterData>* GetContainer() override;
sqlite3* SetDatabase(sqlite3* db); sqlite3* SetDatabase(sqlite3* db);
sqlite3* GetDatabase(); sqlite3* GetDatabase();
@@ -57,7 +61,6 @@ private:
CharacterManager() = default; CharacterManager() = default;
~CharacterManager() = default; ~CharacterManager() = default;
std::map<int, CharacterData> characterMap;
sqlite3* database = nullptr; sqlite3* database = nullptr;
}; };
+1 -1
View File
@@ -1,5 +1,5 @@
#config #config
INCLUDES+=. ../../common/gameplay ../../common/utilities INCLUDES+=. ../server_utilities ../../common/gameplay ../../common/utilities
LIBS+= LIBS+=
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
+1 -1
View File
@@ -1,5 +1,5 @@
#config #config
INCLUDES+=. ../../common/map ../../common/utilities INCLUDES+=. ../server_utilities ../../common/map ../../common/utilities
LIBS+= LIBS+=
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
+22 -7
View File
@@ -29,21 +29,36 @@ static int getPager(lua_State* L) {
return 1; return 1;
} }
static int create(lua_State* L) { static int setRoomName(lua_State* L) {
//EMPTY RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
//NOTE: This can be used to set defaults for the pager room->SetRoomName(lua_tostring(L, 2));
return 0; return 0;
} }
static int unload(lua_State* L) { static int getRoomName(lua_State* L) {
//EMPTY RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
lua_pushstring(L, room->GetRoomName().c_str());
return 1;
}
static int setTilesetName(lua_State* L) {
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
room->SetTilesetName(lua_tostring(L, 2));
return 0; return 0;
} }
static int getTilesetName(lua_State* L) {
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
lua_pushstring(L, room->GetTilesetName().c_str());
return 1;
}
static const luaL_Reg roomLib[] = { static const luaL_Reg roomLib[] = {
{"GetPager",getPager}, {"GetPager",getPager},
{"Create", create}, {"SetRoomName", setRoomName},
{"Unload", unload}, {"GetRoomName", getRoomName},
{"SetTileset", setTilesetName},
{"GetTileset", getTilesetName},
{nullptr, nullptr} {nullptr, nullptr}
}; };
+6
View File
@@ -35,6 +35,9 @@ public:
//accessors and mutators //accessors and mutators
RegionPagerLua* GetPager() { return &pager; } RegionPagerLua* GetPager() { return &pager; }
std::string SetRoomName(std::string s) { return roomName = s; }
std::string GetRoomName() { return roomName; }
std::string SetTilesetName(std::string s) { return tilesetName = s; } std::string SetTilesetName(std::string s) { return tilesetName = s; }
std::string GetTilesetName() { return tilesetName; } std::string GetTilesetName() { return tilesetName; }
@@ -43,7 +46,10 @@ private:
//members //members
RegionPagerLua pager; RegionPagerLua pager;
std::string roomName;
std::string tilesetName; std::string tilesetName;
//TODO: pass the room name & tileset name to the clients
//TODO: lua references i.e. create, unload, etc.
}; };
#endif #endif
+53 -55
View File
@@ -29,78 +29,76 @@
//public access methods //public access methods
//------------------------- //-------------------------
int RoomManager::CreateRoom() { int RoomManager::Create() {
//create the room //create the room
RoomData* newRoom = new RoomData(); RoomData* newRoom = &elementMap[counter]; //implicitly constructs the element
newRoom->pager.SetLuaState(luaState); newRoom->pager.SetLuaState(lua);
//register the room
roomMap[counter] = newRoom;
//API hook
lua_getglobal(luaState, TORTUGA_ROOM_NAME);
lua_getfield(luaState, -1, "Create");
lua_pushlightuserdata(luaState, newRoom);
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
}
lua_pop(luaState, 1);
//finish the routine //finish the routine
return counter++; return counter++;
} }
void RoomManager::UnloadRoom(int uid) { int RoomManager::Load() {
//TODO: RoomManager::Load()
return -1;
}
int RoomManager::Save(int uid) {
//TODO: RoomManager::Save(uid)
return -1;
}
void RoomManager::Unload(int uid) {
//find the room //find the room
RoomData* room = FindRoom(uid); std::map<int, RoomData>::iterator it = elementMap.find(uid);
if (!room) { if (it == elementMap.end()) {
return; return;
} }
//API hook
lua_getglobal(luaState, TORTUGA_ROOM_NAME);
lua_getfield(luaState, -1, "Unload");
lua_pushlightuserdata(luaState, room);
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
}
lua_pop(luaState, 1);
//free the memory //free the memory
delete room; elementMap.erase(uid);
roomMap.erase(uid);
} }
RoomData* RoomManager::GetRoom(int uid) { void RoomManager::Delete(int uid) {
return FindRoom(uid); //TODO: RoomManager::Delete(int uid)
//TODO: expand this to auto-create the room //NOTE: aliased to RoomManager::Unload(int uid)
} Unload(uid);
RoomData* RoomManager::FindRoom(int uid) {
std::map<int, RoomData*>::iterator it = roomMap.find(uid);
if (it == roomMap.end()) {
return nullptr;
}
return it->second;
}
int RoomManager::PushRoom(RoomData* room) {
roomMap[counter++] = room;
return counter;
} }
void RoomManager::UnloadAll() { void RoomManager::UnloadAll() {
lua_getglobal(luaState, TORTUGA_ROOM_NAME); elementMap.clear();
}
for (auto& it : roomMap) { void RoomManager::UnloadIf(std::function<bool(std::pair<const int,RoomData>)> fn) {
//API hook std::map<int, RoomData>::iterator it = elementMap.begin();
lua_getfield(luaState, -1, "Unload"); while (it != elementMap.end()) {
lua_pushlightuserdata(luaState, it.second); if (fn(*it)) {
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) { it = elementMap.erase(it);
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) )); }
else {
++it;
} }
} }
}
lua_pop(luaState, 1); RoomData* RoomManager::Get(int uid) {
roomMap.clear(); std::map<int, RoomData>::iterator it = elementMap.find(uid);
}
if (it == elementMap.end()) {
return nullptr;
}
return &it->second;
}
int RoomManager::GetLoadedCount() {
return elementMap.size();
}
int RoomManager::GetTotalCount() {
return elementMap.size();
}
std::map<int, RoomData>* RoomManager::GetContainer() {
return &elementMap;
}
+21 -16
View File
@@ -24,28 +24,34 @@
#include "room_data.hpp" #include "room_data.hpp"
#include "singleton.hpp" #include "singleton.hpp"
#include "manager_interface.hpp"
#include "lua/lua.hpp" #include "lua/lua.hpp"
#include <map> class RoomManager:
public Singleton<RoomManager>,
class RoomManager : public Singleton<RoomManager> { public ManagerInterface<RoomData>
{
public: public:
//public access methods //common public methods
int CreateRoom(); int Create() override;
void UnloadRoom(int uid); int Load() override;
int Save(int uid) override;
void Unload(int uid) override;
void Delete(int uid) override;
RoomData* GetRoom(int uid); void UnloadAll() override;
RoomData* FindRoom(int uid); void UnloadIf(std::function<bool(std::pair<const int,RoomData>)> fn) override;
int PushRoom(RoomData*);
void UnloadAll();
//accessors and mutators //accessors and mutators
std::map<int, RoomData*>* GetContainer() { return &roomMap; } RoomData* Get(int uid) override;
int GetLoadedCount() override;
int GetTotalCount() override;
std::map<int, RoomData>* GetContainer() override;
lua_State* SetLuaState(lua_State* L) { return luaState = L; } //hooks
lua_State* GetLuaState() { return luaState; } lua_State* SetLuaState(lua_State* L) { return lua = L; }
lua_State* GetLuaState() { return lua; }
private: private:
friend Singleton<RoomManager>; friend Singleton<RoomManager>;
@@ -53,8 +59,7 @@ private:
RoomManager() = default; RoomManager() = default;
~RoomManager() = default; ~RoomManager() = default;
std::map<int, RoomData*> roomMap; lua_State* lua = nullptr;
lua_State* luaState = nullptr;
int counter = 0; int counter = 0;
}; };
+21 -21
View File
@@ -23,34 +23,34 @@
#include "room_manager.hpp" #include "room_manager.hpp"
#include <string> int createRoom(lua_State* L) {
//create & get the room
RoomManager& roomMgr = RoomManager::GetSingleton();
int uid = roomMgr.Create();
RoomData* room = roomMgr.Get(uid);
static int getRoom(lua_State* L) { //setup the room
//find, push and return the room //TODO: room parameters only set via lua, fix this
RoomData* room = RoomManager::GetSingleton().GetRoom(lua_tointeger(L, -2)); room->SetRoomName(lua_tostring(L, 1));
lua_pushlightuserdata(L, reinterpret_cast<void*>(room)); room->SetTilesetName(lua_tostring(L, 2));
return 1;
//return room, uid
lua_pushlightuserdata(L, static_cast<void*>(room));
lua_pushinteger(L, uid);
return 2;
} }
static int createRoom(lua_State* L) { int unloadRoom(lua_State* L) {
//TODO: check parameter count for the glue functions //TODO: check authorization for room deletion
RoomManager& roomMgr = RoomManager::GetSingleton();
//create, find and return the room roomMgr.Unload(lua_tointeger(L, 1));
int uid = RoomManager::GetSingleton().CreateRoom();
lua_pushlightuserdata(L, RoomManager::GetSingleton().FindRoom(uid));
return 1;
}
static int unloadRoom(lua_State* L) {
//unload the specified room
RoomManager::GetSingleton().UnloadRoom(lua_tointeger(L, -2));
return 0; return 0;
} }
static const luaL_Reg roomManagerLib[] = { static const luaL_Reg roomManagerLib[] = {
{"GetRoom",getRoom}, {"CreateRoom", createRoom},
{"CreateRoom",createRoom}, {"UnloadRoom", unloadRoom},
{"UnloadRoom",unloadRoom},
{nullptr, nullptr} {nullptr, nullptr}
}; };
+1 -1
View File
@@ -161,7 +161,7 @@ void ServerApplication::HandleRegionRequest(RegionPacket* const argPacket) {
newPacket.y = argPacket->y; newPacket.y = argPacket->y;
//BUG: possibly related to #35 //BUG: possibly related to #35
newPacket.region = roomMgr.GetRoom(argPacket->roomIndex)->GetPager()->GetRegion(argPacket->x, argPacket->y); newPacket.region = roomMgr.Get(argPacket->roomIndex)->GetPager()->GetRegion(argPacket->x, argPacket->y);
//send the content //send the content
network.SendTo(argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket)); network.SendTo(argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
@@ -0,0 +1,55 @@
/* Copyright: (c) Kayne Ruse 2014
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#ifndef MANAGERINTERFACE_HPP_
#define MANAGERINTERFACE_HPP_
#include <functional>
#include <map>
template<typename T, typename... Arguments>
class ManagerInterface {
public:
//common public methods
virtual int Create(Arguments... parameters) = 0;
virtual int Load(Arguments... parameters) = 0;
virtual int Save(int uid) = 0;
virtual void Unload(int uid) = 0;
virtual void Delete(int uid) = 0;
virtual void UnloadAll() = 0;
virtual void UnloadIf(std::function<bool(std::pair<const int, T>)> fn) = 0;
//accessors & mutators
virtual T* Get(int uid) = 0;
virtual int GetLoadedCount() = 0;
virtual int GetTotalCount() = 0; //can be an alias of GetLoadedCount()
virtual std::map<int, T>* GetContainer() = 0;
protected:
ManagerInterface() = default;
~ManagerInterface() = default;
//members
std::map<int, T> elementMap;
};
#endif
-12
View File
@@ -1,12 +0,0 @@
TODO: Get the rooms working, even if only via hotkeys
TODO: Fix shoddy movement
TODO: Remove the big "Shut Down" button
TODO: Make a way for the server owner to control the server directly
TODO: Move the map system into it's own namespace?
TODO: The TileSheet class should implement the surface itself
TODO: make the whole thing more fault tolerant
TODO: Authentication
TODO: Time delay for requesting region packets
TODO: A proper logging system