Deprecated and deleted some unneeded files
Also merged misc/Notes.md into the GDD. I'll work on this soon.
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
The Game Map
|
||||
|
||||
This section outlines the game’s map system. This system utilizes pagination to create a theoretically infinite game map, as well as supporting multiple tilesets in the same map. The goal of this design is to create a system with as much flexibility as possible, and simply enforcing a more rigid approach higher in the tool chain.
|
||||
|
||||
Tile
|
||||
|
||||
The Tile class is the basic unit of the map system, and is explicitly a POD (plain old data) structure. A tile has these members:
|
||||
|
||||
X Position
|
||||
Y Position
|
||||
Depth
|
||||
Width
|
||||
Height
|
||||
Tile Index
|
||||
|
||||
The tile’s X and Y positions are relative to their container region’s location. A tile’s depth allows multiple tiles to be drawn at the same location, and in the correct order; tiles with lower depths (including below zero) are drawn first. If a new tile has the same X position, Y position and depth as an existing tile, the old tile is overwritten.
|
||||
|
||||
The width and height members indicate the graphical size of the tile (not actually used when drawing), while the tile index is the specific tile for the sheet manager to draw. A negative value here is considered an error message.
|
||||
|
||||
Region
|
||||
|
||||
The region class has these members:
|
||||
|
||||
X Position
|
||||
Y Position
|
||||
Width
|
||||
Height
|
||||
Tile Container
|
||||
|
||||
Each region in a certain map must have the same width and height, and it’s X and Y positions must be multiples of those width and height values, respectfully. The outcome of this restriction is a theoretically infinite grid of region objects.
|
||||
|
||||
Each region holds a set of tiles corresponding to the region’s bounds. The tiles’ X and Y positions are relative to the regions’, so moving the region will move the tiles as well. A region object is created or loaded when a tile is place in it’s bounds; similarly, if a region has no tiles it should be deleted or removed from memory.
|
||||
|
||||
The exact width and height of a region has no significant impact, other than loading or transmission costs. The width and height of a map can be adjusted as needed.
|
||||
|
||||
Region Pager
|
||||
|
||||
The region pager class has these members:
|
||||
|
||||
Region Width
|
||||
Region Height
|
||||
On New Callback
|
||||
On Delete Callback
|
||||
Region Container
|
||||
|
||||
The Region Pager class holds a series of region objects, as well as creating and deleting them as needed. Every region theoretically exists at any time, so if a non-existent region object is requested, it is created and then returned. This class also has the Prune() method, which removes all regions out of bounds from memory, and the DrawTo method, which takes (among other things) the sheet manager for the map.
|
||||
|
||||
The width and height members must be set before the pager is used, and must not be changed while it still has regions loaded. These are used to create region objects as needed.
|
||||
|
||||
Each pager can also have two different callbacks set: “on new” and “on delete”. If either of these are set (that is, not null) then each region object’s address is passed to these after it is created or before it is destroyed, respectfully. The callbacks are intended to be used for domain specific processes, such as loading or saving data, or even requesting data from a remote server.
|
||||
|
||||
Tile Sheet
|
||||
|
||||
A tileset is a series of tile graphics stored in a single file. The tile sheet class loads a tile set into memory, and provides utilities for drawing them to the screen. The tile sheet class has these members:
|
||||
|
||||
Image
|
||||
X Count
|
||||
Y Count
|
||||
Total Count
|
||||
Begin
|
||||
End
|
||||
|
||||
The Image class is utilized heavily here by storing the graphical data and the tile size. Any file loaded into a sheet object must have all tile images arranged in a grid pattern, and they must all have the same width and height. The width and height must be provided when the file is loaded.
|
||||
|
||||
The X and Y counts are the number of tiles along the X and Y axis of the sheet’s image, and the total count is the number of tiles in the whole sheet (which is equal to the product of the X and Y counts).
|
||||
|
||||
Begin is the index of the first tile on the sheet (default is zero), and end is the index after the last tile (defaults to the value of total count). These indicate the range of the tiles, and are mostly used by the sheet manager. They are also used by the InRange() method, which checks to see if a certain tile is in that sheet.
|
||||
|
||||
Tile Sheet Manager
|
||||
|
||||
This class has these members:
|
||||
|
||||
Tile Sheet Container
|
||||
Range End
|
||||
|
||||
This class is a wrapper around a key-value container, using strings as the keys. Given a specific tile index, this class will draw the correct tile from the loaded sheets, or it throws an error.
|
||||
|
||||
Also, this class keeps track of the end of the sheet’s ranges.
|
||||
|
||||
TODO
|
||||
|
||||
Map File Format
|
||||
|
||||
TODO
|
||||
@@ -0,0 +1,3 @@
|
||||
This particular refactoring stage was absolute hell, mostly because I was wrestling with a severe bout of depression too. So, I've added the diffs, and a scary screenshot of the git console.
|
||||
|
||||
There are three diff files because server/server_application.cpp was split into two files: server/server_internals.cpp and server/server_connections.cpp, each with it's own diff.
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 27 KiB |
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,394 @@
|
||||
diff --git a/server/server_connections.cpp b/server/server_connections.cpp
|
||||
index 1a96d5b..2f35566 100644
|
||||
--- a/server/server_connections.cpp
|
||||
+++ b/server/server_connections.cpp
|
||||
@@ -21,212 +21,106 @@
|
||||
*/
|
||||
#include "server_application.hpp"
|
||||
|
||||
-#include "utility.hpp"
|
||||
-
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
-#include <string>
|
||||
-
|
||||
-//-------------------------
|
||||
-//Define the public members
|
||||
-//-------------------------
|
||||
-
|
||||
-void ServerApplication::Init(int argc, char** argv) {
|
||||
- //NOTE: I might need to rearrange the init process so that lua & SQL can interact with the map system as needed.
|
||||
- std::cout << "Beginning startup" << std::endl;
|
||||
-
|
||||
- //initial setup
|
||||
- ClientEntry::uidCounter = 0;
|
||||
- PlayerEntry::uidCounter = 0;
|
||||
- config.Load("rsc\\config.cfg");
|
||||
-
|
||||
- //Init SDL
|
||||
- if (SDL_Init(0)) {
|
||||
- throw(std::runtime_error("Failed to initialize SDL"));
|
||||
- }
|
||||
- std::cout << "Initialized SDL" << std::endl;
|
||||
-
|
||||
- //Init SDL_net
|
||||
- if (SDLNet_Init()) {
|
||||
- throw(std::runtime_error("Failed to initialize SDL_net"));
|
||||
- }
|
||||
- network.Open(config.Int("server.port"), PACKET_BUFFER_SIZE);
|
||||
- std::cout << "Initialized SDL_net" << std::endl;
|
||||
-
|
||||
- //Init SQL
|
||||
- int ret = sqlite3_open_v2(config["server.dbname"].c_str(), &database, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, nullptr);
|
||||
- if (ret != SQLITE_OK || !database) {
|
||||
- throw(std::runtime_error(std::string() + "Failed to initialize SQL: " + sqlite3_errmsg(database) ));
|
||||
- }
|
||||
- std::cout << "Initialized SQL" << std::endl;
|
||||
-
|
||||
- //setup the database
|
||||
- if (runSQLScript(database, config["dir.scripts"] + "setup_server.sql")) {
|
||||
- throw(std::runtime_error("Failed to initialize SQL's setup script"));
|
||||
- }
|
||||
- std::cout << "Initialized SQL's setup script" << std::endl;
|
||||
-
|
||||
- //lua
|
||||
- luaState = luaL_newstate();
|
||||
- if (!luaState) {
|
||||
- throw(std::runtime_error("Failed to initialize lua"));
|
||||
- }
|
||||
- luaL_openlibs(luaState);
|
||||
- std::cout << "Initialized lua" << std::endl;
|
||||
-
|
||||
- //run the startup script
|
||||
- if (luaL_dofile(luaState, (config["dir.scripts"] + "setup_server.lua").c_str())) {
|
||||
- throw(std::runtime_error(std::string() + "Failed to initialize lua's setup script: " + lua_tostring(luaState, -1) ));
|
||||
- }
|
||||
- std::cout << "Initialized lua's setup script" << std::endl;
|
||||
-
|
||||
- //setup the map object
|
||||
- regionPager.GetAllocator()->SetLuaState(luaState);
|
||||
- regionPager.GetFormat()->SetLuaState(luaState);
|
||||
- //TODO: config parameter
|
||||
- regionPager.GetFormat()->SetSaveDir("save/mapname/");
|
||||
-
|
||||
- std::cout << "Initialized the map system" << std::endl;
|
||||
- std::cout << "\tsizeof(SerialPacket): " << sizeof(SerialPacket) << std::endl;
|
||||
- std::cout << "\tPACKET_BUFFER_SIZE: " << PACKET_BUFFER_SIZE << std::endl;
|
||||
-
|
||||
- //finalize the startup
|
||||
- std::cout << "Startup completed successfully" << std::endl;
|
||||
-
|
||||
- //debugging
|
||||
- //
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::Proc() {
|
||||
- SerialPacket packet;
|
||||
- while(running) {
|
||||
- //suck in the waiting packets & process them
|
||||
- while(network.Receive()) {
|
||||
- //get the packet
|
||||
- deserialize(&packet, network.GetInData());
|
||||
- //cache the source address
|
||||
- packet.meta.srcAddress = network.GetInPacket()->address;
|
||||
- //we need to go deeper
|
||||
- HandlePacket(packet);
|
||||
- }
|
||||
- //give the computer a break
|
||||
- SDL_Delay(10);
|
||||
- }
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::Quit() {
|
||||
- std::cout << "Shutting down" << std::endl;
|
||||
- //empty the members
|
||||
- regionPager.UnloadAll();
|
||||
-
|
||||
- //APIs
|
||||
- lua_close(luaState);
|
||||
- sqlite3_close_v2(database);
|
||||
- network.Close();
|
||||
- SDLNet_Quit();
|
||||
- SDL_Quit();
|
||||
- std::cout << "Shutdown finished" << std::endl;
|
||||
-}
|
||||
-
|
||||
-//-------------------------
|
||||
-//Define the uber switch
|
||||
-//-------------------------
|
||||
-
|
||||
-void ServerApplication::HandlePacket(SerialPacket packet) {
|
||||
- switch(packet.meta.type) {
|
||||
- case SerialPacket::Type::BROADCAST_REQUEST:
|
||||
- HandleBroadcastRequest(packet);
|
||||
- break;
|
||||
- case SerialPacket::Type::JOIN_REQUEST:
|
||||
- HandleJoinRequest(packet);
|
||||
- break;
|
||||
- case SerialPacket::Type::DISCONNECT:
|
||||
- HandleDisconnect(packet);
|
||||
- break;
|
||||
- case SerialPacket::Type::SYNCHRONIZE:
|
||||
- HandleSynchronize(packet);
|
||||
- break;
|
||||
- case SerialPacket::Type::SHUTDOWN:
|
||||
- HandleShutdown(packet);
|
||||
- break;
|
||||
- case SerialPacket::Type::PLAYER_NEW:
|
||||
- HandlePlayerNew(packet);
|
||||
- break;
|
||||
- case SerialPacket::Type::PLAYER_DELETE:
|
||||
- HandlePlayerDelete(packet);
|
||||
- break;
|
||||
- case SerialPacket::Type::PLAYER_UPDATE:
|
||||
- HandlePlayerUpdate(packet);
|
||||
- break;
|
||||
- case SerialPacket::Type::REGION_REQUEST:
|
||||
- HandleRegionRequest(packet);
|
||||
- break;
|
||||
- //handle errors
|
||||
- default:
|
||||
- throw(std::runtime_error("Unknown SerialPacket::Type encountered"));
|
||||
- break;
|
||||
- }
|
||||
-}
|
||||
|
||||
//-------------------------
|
||||
//Handle various network input
|
||||
//-------------------------
|
||||
|
||||
void ServerApplication::HandleBroadcastRequest(SerialPacket packet) {
|
||||
- //send back the server's metadata
|
||||
+ //pack the server's data
|
||||
packet.meta.type = SerialPacket::Type::BROADCAST_RESPONSE;
|
||||
-
|
||||
- //pack the data
|
||||
+ packet.serverInfo.networkVersion = NETWORK_VERSION;
|
||||
snprintf(packet.serverInfo.name, PACKET_STRING_SIZE, "%s", config["server.name"].c_str());
|
||||
packet.serverInfo.playerCount = playerMap.size();
|
||||
- packet.serverInfo.regionWidth = REGION_WIDTH;
|
||||
- packet.serverInfo.regionHeight = REGION_HEIGHT;
|
||||
- packet.serverInfo.regionDepth = REGION_DEPTH;
|
||||
|
||||
- //send the data
|
||||
+ //bounce this packet
|
||||
char buffer[PACKET_BUFFER_SIZE];
|
||||
serialize(&packet, buffer);
|
||||
network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
void ServerApplication::HandleJoinRequest(SerialPacket packet) {
|
||||
- //register the new client
|
||||
+ //create the new client
|
||||
ClientEntry newClient;
|
||||
newClient.address = packet.meta.srcAddress;
|
||||
- clientMap[ClientEntry::uidCounter] = newClient;
|
||||
|
||||
- //send the client their index
|
||||
- char buffer[PACKET_BUFFER_SIZE];
|
||||
+ //TODO: move this into the player management code
|
||||
+ //create the new player
|
||||
+ PlayerEntry newPlayer;
|
||||
+ newPlayer.clientIndex = ClientEntry::uidCounter;
|
||||
+ newPlayer.player = packet.clientInfo.player;
|
||||
+ newPlayer.handle = packet.clientInfo.handle;
|
||||
+ newPlayer.avatar = packet.clientInfo.avatar;
|
||||
+
|
||||
+ //send the client their info
|
||||
packet.meta.type = SerialPacket::Type::JOIN_RESPONSE;
|
||||
- packet.clientInfo.index = ClientEntry::uidCounter;
|
||||
- serialize(&packet, buffer);
|
||||
+ packet.clientInfo.clientIndex = ClientEntry::uidCounter;
|
||||
+ packet.clientInfo.playerIndex = PlayerEntry::uidCounter;
|
||||
|
||||
//bounce this packet
|
||||
+ char buffer[PACKET_BUFFER_SIZE];
|
||||
+ serialize(&packet, buffer);
|
||||
network.Send(&newClient.address, buffer, PACKET_BUFFER_SIZE);
|
||||
|
||||
+ //send the new player to all clients
|
||||
+ packet.meta.type = SerialPacket::Type::PLAYER_NEW;
|
||||
+ packet.playerInfo.playerIndex = PlayerEntry::uidCounter;
|
||||
+ strncpy(packet.playerInfo.handle, newPlayer.handle.c_str(), PACKET_STRING_SIZE);
|
||||
+ strncpy(packet.playerInfo.avatar, newPlayer.avatar.c_str(), PACKET_STRING_SIZE);
|
||||
+ packet.playerInfo.position = newPlayer.position;
|
||||
+ packet.playerInfo.motion = newPlayer.motion;
|
||||
+ PumpPacket(packet);
|
||||
+
|
||||
//finished this routine
|
||||
+ clientMap[ClientEntry::uidCounter] = newClient;
|
||||
+ playerMap[PlayerEntry::uidCounter] = newPlayer;
|
||||
ClientEntry::uidCounter++;
|
||||
+ PlayerEntry::uidCounter++;
|
||||
std::cout << "Connect, total: " << clientMap.size() << std::endl;
|
||||
}
|
||||
|
||||
+void ServerApplication::HandleSynchronize(SerialPacket packet) {
|
||||
+ //TODO: compensate for large distances
|
||||
+
|
||||
+ //send all the server's data to this client
|
||||
+ SerialPacket newPacket;
|
||||
+ char buffer[PACKET_BUFFER_SIZE];
|
||||
+
|
||||
+ //players
|
||||
+ newPacket.meta.type = SerialPacket::Type::PLAYER_UPDATE;
|
||||
+ for (auto& it : playerMap) {
|
||||
+ //TODO: update this for the expanded PlayerEntry structure
|
||||
+ newPacket.playerInfo.playerIndex = it.first;
|
||||
+ snprintf(newPacket.playerInfo.handle, PACKET_STRING_SIZE, "%s", it.second.handle.c_str());
|
||||
+ snprintf(newPacket.playerInfo.avatar, PACKET_STRING_SIZE, "%s", it.second.avatar.c_str());
|
||||
+ newPacket.playerInfo.mapIndex = it.second.mapIndex;
|
||||
+ newPacket.playerInfo.position = it.second.position;
|
||||
+ newPacket.playerInfo.motion = it.second.motion;
|
||||
+ serialize(&newPacket, buffer);
|
||||
+ network.Send(&clientMap[packet.clientInfo.clientIndex].address, buffer, PACKET_BUFFER_SIZE);
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
void ServerApplication::HandleDisconnect(SerialPacket packet) {
|
||||
//TODO: authenticate who is disconnecting/kicking
|
||||
+ //TODO: define the difference between unloading and deletng a player
|
||||
|
||||
//disconnect the specified client
|
||||
char buffer[PACKET_BUFFER_SIZE];
|
||||
serialize(&packet, buffer);
|
||||
- network.Send(&clientMap[packet.clientInfo.index].address, buffer, PACKET_BUFFER_SIZE);
|
||||
- clientMap.erase(packet.clientInfo.index);
|
||||
+ network.Send(&clientMap[packet.clientInfo.clientIndex].address, buffer, PACKET_BUFFER_SIZE);
|
||||
+ clientMap.erase(packet.clientInfo.clientIndex);
|
||||
|
||||
//prep the delete packet
|
||||
SerialPacket delPacket;
|
||||
delPacket.meta.type = SerialPacket::Type::PLAYER_DELETE;
|
||||
|
||||
- //TODO: can this use DeletePlayer() instead?
|
||||
//delete server and client side players
|
||||
- erase_if(playerMap, [&](std::pair<unsigned int, PlayerEntry> it) -> bool {
|
||||
+ erase_if(playerMap, [&](std::pair<int, PlayerEntry> it) -> bool {
|
||||
//find the internal players to delete
|
||||
- if (it.second.clientIndex == packet.clientInfo.index) {
|
||||
+ if (it.second.clientIndex == packet.clientInfo.clientIndex) {
|
||||
//send the delete player command to all clients
|
||||
delPacket.playerInfo.playerIndex = it.first;
|
||||
PumpPacket(delPacket);
|
||||
@@ -243,102 +137,23 @@ void ServerApplication::HandleDisconnect(SerialPacket packet) {
|
||||
std::cout << "Disconnect, total: " << clientMap.size() << std::endl;
|
||||
}
|
||||
|
||||
-void ServerApplication::HandleSynchronize(SerialPacket packet) {
|
||||
- //TODO: compensate for large distances
|
||||
-
|
||||
- //send all the server's data to this client
|
||||
- SerialPacket newPacket;
|
||||
- char buffer[PACKET_BUFFER_SIZE];
|
||||
-
|
||||
- //players
|
||||
- newPacket.meta.type = SerialPacket::Type::PLAYER_UPDATE;
|
||||
- for (auto& it : playerMap) {
|
||||
- //TODO: update this for the expanded PlayerEntry structure
|
||||
- newPacket.playerInfo.playerIndex = it.first;
|
||||
- snprintf(newPacket.playerInfo.handle, PACKET_STRING_SIZE, "%s", it.second.handle.c_str());
|
||||
- snprintf(newPacket.playerInfo.avatar, PACKET_STRING_SIZE, "%s", it.second.avatar.c_str());
|
||||
- newPacket.playerInfo.position = it.second.position;
|
||||
- newPacket.playerInfo.motion = it.second.motion;
|
||||
- serialize(&newPacket, buffer);
|
||||
- network.Send(&clientMap[packet.clientInfo.index].address, buffer, PACKET_BUFFER_SIZE);
|
||||
- }
|
||||
-}
|
||||
-
|
||||
void ServerApplication::HandleShutdown(SerialPacket packet) {
|
||||
+ //TODO: authenticate who is shutting the server down
|
||||
+
|
||||
//end the server
|
||||
running = false;
|
||||
|
||||
//disconnect all clients
|
||||
packet.meta.type = SerialPacket::Type::DISCONNECT;
|
||||
+ packet.clientInfo.clientIndex = -1;
|
||||
PumpPacket(packet);
|
||||
|
||||
//finished this routine
|
||||
std::cout << "Shutdown signal accepted" << std::endl;
|
||||
}
|
||||
|
||||
-void ServerApplication::HandlePlayerNew(SerialPacket packet) {
|
||||
- //register the new PlayerEntry
|
||||
- //NOTE: assigning each field one-by-one so adding or moving a field doesn't break this code
|
||||
- PlayerEntry newPlayer;
|
||||
-
|
||||
- //metadata
|
||||
- newPlayer.clientIndex = packet.playerInfo.clientIndex;
|
||||
- newPlayer.handle = packet.playerInfo.handle;
|
||||
- newPlayer.avatar = packet.playerInfo.avatar;
|
||||
-
|
||||
- //position
|
||||
- newPlayer.mapIndex = 0;
|
||||
- newPlayer.position = {0,0};
|
||||
- newPlayer.motion = {0,0};
|
||||
- newPlayer.bbox = {0, 0, 0, 0};
|
||||
-
|
||||
- //TODO: Add the statistic creation code here
|
||||
-
|
||||
- //push this player
|
||||
- playerMap[PlayerEntry::uidCounter] = newPlayer;
|
||||
-
|
||||
- //send the client their info
|
||||
- packet.playerInfo.playerIndex = PlayerEntry::uidCounter;
|
||||
- packet.playerInfo.position = newPlayer.position;
|
||||
- packet.playerInfo.motion = newPlayer.motion;
|
||||
-
|
||||
- //actually send to everyone
|
||||
- PumpPacket(packet);
|
||||
-
|
||||
- //finish this routine
|
||||
- PlayerEntry::uidCounter++;
|
||||
-}
|
||||
-
|
||||
-//TODO: differentiate between delete and unload
|
||||
-void ServerApplication::HandlePlayerDelete(SerialPacket packet) {
|
||||
- //TODO: authenticate who is deleting this player
|
||||
- if (playerMap.find(packet.playerInfo.playerIndex) == playerMap.end()) {
|
||||
- throw(std::runtime_error("Cannot delete a non-existant player"));
|
||||
- }
|
||||
-
|
||||
- //TODO: remove the deleted player from the database?
|
||||
-
|
||||
- //prep the delete packet
|
||||
- SerialPacket delPacket;
|
||||
- delPacket.meta.type = SerialPacket::Type::PLAYER_DELETE;
|
||||
-
|
||||
- //delete the specified playerEntry
|
||||
- erase_if(playerMap, [&](std::pair<unsigned int, PlayerEntry> it) -> bool {
|
||||
- //find the specified PlayerEntry
|
||||
- if (it.first == packet.playerInfo.playerIndex) {
|
||||
- //send to all
|
||||
- delPacket.playerInfo.playerIndex = it.first;
|
||||
- PumpPacket(delPacket);
|
||||
-
|
||||
- //delete this player
|
||||
- return true;
|
||||
- }
|
||||
- //skip this player
|
||||
- return false;
|
||||
- });
|
||||
-}
|
||||
-
|
||||
void ServerApplication::HandlePlayerUpdate(SerialPacket packet) {
|
||||
+ //TODO: this should be moved elsewhere
|
||||
if (playerMap.find(packet.playerInfo.playerIndex) == playerMap.end()) {
|
||||
throw(std::runtime_error("Cannot update a non-existant player"));
|
||||
}
|
||||
@@ -351,9 +166,12 @@ void ServerApplication::HandlePlayerUpdate(SerialPacket packet) {
|
||||
}
|
||||
|
||||
void ServerApplication::HandleRegionRequest(SerialPacket packet) {
|
||||
- char buffer[PACKET_BUFFER_SIZE];
|
||||
+ //TODO: this should be moved elsewhere
|
||||
packet.meta.type = SerialPacket::Type::REGION_CONTENT;
|
||||
packet.regionInfo.region = regionPager.GetRegion(packet.regionInfo.x, packet.regionInfo.y);
|
||||
+
|
||||
+ //send the content
|
||||
+ char buffer[PACKET_BUFFER_SIZE];
|
||||
serialize(&packet, buffer);
|
||||
network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE);
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
diff --git a/server/server_internals.cpp b/server/server_internals.cpp
|
||||
index 1a96d5b..e1a9cb9 100644
|
||||
--- a/server/server_internals.cpp
|
||||
+++ b/server/server_internals.cpp
|
||||
@@ -1,4 +1,4 @@
|
||||
-/* Copyright: (c) Kayne Ruse 2013, 2014
|
||||
+/* 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
|
||||
@@ -21,7 +21,7 @@
|
||||
*/
|
||||
#include "server_application.hpp"
|
||||
|
||||
-#include "utility.hpp"
|
||||
+#include "server_utility.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
@@ -36,8 +36,6 @@ void ServerApplication::Init(int argc, char** argv) {
|
||||
std::cout << "Beginning startup" << std::endl;
|
||||
|
||||
//initial setup
|
||||
- ClientEntry::uidCounter = 0;
|
||||
- PlayerEntry::uidCounter = 0;
|
||||
config.Load("rsc\\config.cfg");
|
||||
|
||||
//Init SDL
|
||||
@@ -109,6 +107,8 @@ void ServerApplication::Proc() {
|
||||
//we need to go deeper
|
||||
HandlePacket(packet);
|
||||
}
|
||||
+ //update the internals
|
||||
+ //TODO: update the internals i.e. player positions
|
||||
//give the computer a break
|
||||
SDL_Delay(10);
|
||||
}
|
||||
@@ -116,6 +116,10 @@ void ServerApplication::Proc() {
|
||||
|
||||
void ServerApplication::Quit() {
|
||||
std::cout << "Shutting down" << std::endl;
|
||||
+
|
||||
+ //save the server state
|
||||
+ //TODO: save the existing players
|
||||
+
|
||||
//empty the members
|
||||
regionPager.UnloadAll();
|
||||
|
||||
@@ -125,6 +129,7 @@ void ServerApplication::Quit() {
|
||||
network.Close();
|
||||
SDLNet_Quit();
|
||||
SDL_Quit();
|
||||
+
|
||||
std::cout << "Shutdown finished" << std::endl;
|
||||
}
|
||||
|
||||
@@ -140,21 +145,15 @@ void ServerApplication::HandlePacket(SerialPacket packet) {
|
||||
case SerialPacket::Type::JOIN_REQUEST:
|
||||
HandleJoinRequest(packet);
|
||||
break;
|
||||
- case SerialPacket::Type::DISCONNECT:
|
||||
- HandleDisconnect(packet);
|
||||
- break;
|
||||
case SerialPacket::Type::SYNCHRONIZE:
|
||||
HandleSynchronize(packet);
|
||||
break;
|
||||
+ case SerialPacket::Type::DISCONNECT:
|
||||
+ HandleDisconnect(packet);
|
||||
+ break;
|
||||
case SerialPacket::Type::SHUTDOWN:
|
||||
HandleShutdown(packet);
|
||||
break;
|
||||
- case SerialPacket::Type::PLAYER_NEW:
|
||||
- HandlePlayerNew(packet);
|
||||
- break;
|
||||
- case SerialPacket::Type::PLAYER_DELETE:
|
||||
- HandlePlayerDelete(packet);
|
||||
- break;
|
||||
case SerialPacket::Type::PLAYER_UPDATE:
|
||||
HandlePlayerUpdate(packet);
|
||||
break;
|
||||
@@ -167,202 +166,3 @@ void ServerApplication::HandlePacket(SerialPacket packet) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
-
|
||||
-//-------------------------
|
||||
-//Handle various network input
|
||||
-//-------------------------
|
||||
-
|
||||
-void ServerApplication::HandleBroadcastRequest(SerialPacket packet) {
|
||||
- //send back the server's metadata
|
||||
- packet.meta.type = SerialPacket::Type::BROADCAST_RESPONSE;
|
||||
-
|
||||
- //pack the data
|
||||
- snprintf(packet.serverInfo.name, PACKET_STRING_SIZE, "%s", config["server.name"].c_str());
|
||||
- packet.serverInfo.playerCount = playerMap.size();
|
||||
- packet.serverInfo.regionWidth = REGION_WIDTH;
|
||||
- packet.serverInfo.regionHeight = REGION_HEIGHT;
|
||||
- packet.serverInfo.regionDepth = REGION_DEPTH;
|
||||
-
|
||||
- //send the data
|
||||
- char buffer[PACKET_BUFFER_SIZE];
|
||||
- serialize(&packet, buffer);
|
||||
- network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE);
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::HandleJoinRequest(SerialPacket packet) {
|
||||
- //register the new client
|
||||
- ClientEntry newClient;
|
||||
- newClient.address = packet.meta.srcAddress;
|
||||
- clientMap[ClientEntry::uidCounter] = newClient;
|
||||
-
|
||||
- //send the client their index
|
||||
- char buffer[PACKET_BUFFER_SIZE];
|
||||
- packet.meta.type = SerialPacket::Type::JOIN_RESPONSE;
|
||||
- packet.clientInfo.index = ClientEntry::uidCounter;
|
||||
- serialize(&packet, buffer);
|
||||
-
|
||||
- //bounce this packet
|
||||
- network.Send(&newClient.address, buffer, PACKET_BUFFER_SIZE);
|
||||
-
|
||||
- //finished this routine
|
||||
- ClientEntry::uidCounter++;
|
||||
- std::cout << "Connect, total: " << clientMap.size() << std::endl;
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::HandleDisconnect(SerialPacket packet) {
|
||||
- //TODO: authenticate who is disconnecting/kicking
|
||||
-
|
||||
- //disconnect the specified client
|
||||
- char buffer[PACKET_BUFFER_SIZE];
|
||||
- serialize(&packet, buffer);
|
||||
- network.Send(&clientMap[packet.clientInfo.index].address, buffer, PACKET_BUFFER_SIZE);
|
||||
- clientMap.erase(packet.clientInfo.index);
|
||||
-
|
||||
- //prep the delete packet
|
||||
- SerialPacket delPacket;
|
||||
- delPacket.meta.type = SerialPacket::Type::PLAYER_DELETE;
|
||||
-
|
||||
- //TODO: can this use DeletePlayer() instead?
|
||||
- //delete server and client side players
|
||||
- erase_if(playerMap, [&](std::pair<unsigned int, PlayerEntry> it) -> bool {
|
||||
- //find the internal players to delete
|
||||
- if (it.second.clientIndex == packet.clientInfo.index) {
|
||||
- //send the delete player command to all clients
|
||||
- delPacket.playerInfo.playerIndex = it.first;
|
||||
- PumpPacket(delPacket);
|
||||
-
|
||||
- //delete this player object
|
||||
- return true;
|
||||
- }
|
||||
-
|
||||
- //don't delete this player object
|
||||
- return false;
|
||||
- });
|
||||
-
|
||||
- //finished this routine
|
||||
- std::cout << "Disconnect, total: " << clientMap.size() << std::endl;
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::HandleSynchronize(SerialPacket packet) {
|
||||
- //TODO: compensate for large distances
|
||||
-
|
||||
- //send all the server's data to this client
|
||||
- SerialPacket newPacket;
|
||||
- char buffer[PACKET_BUFFER_SIZE];
|
||||
-
|
||||
- //players
|
||||
- newPacket.meta.type = SerialPacket::Type::PLAYER_UPDATE;
|
||||
- for (auto& it : playerMap) {
|
||||
- //TODO: update this for the expanded PlayerEntry structure
|
||||
- newPacket.playerInfo.playerIndex = it.first;
|
||||
- snprintf(newPacket.playerInfo.handle, PACKET_STRING_SIZE, "%s", it.second.handle.c_str());
|
||||
- snprintf(newPacket.playerInfo.avatar, PACKET_STRING_SIZE, "%s", it.second.avatar.c_str());
|
||||
- newPacket.playerInfo.position = it.second.position;
|
||||
- newPacket.playerInfo.motion = it.second.motion;
|
||||
- serialize(&newPacket, buffer);
|
||||
- network.Send(&clientMap[packet.clientInfo.index].address, buffer, PACKET_BUFFER_SIZE);
|
||||
- }
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::HandleShutdown(SerialPacket packet) {
|
||||
- //end the server
|
||||
- running = false;
|
||||
-
|
||||
- //disconnect all clients
|
||||
- packet.meta.type = SerialPacket::Type::DISCONNECT;
|
||||
- PumpPacket(packet);
|
||||
-
|
||||
- //finished this routine
|
||||
- std::cout << "Shutdown signal accepted" << std::endl;
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::HandlePlayerNew(SerialPacket packet) {
|
||||
- //register the new PlayerEntry
|
||||
- //NOTE: assigning each field one-by-one so adding or moving a field doesn't break this code
|
||||
- PlayerEntry newPlayer;
|
||||
-
|
||||
- //metadata
|
||||
- newPlayer.clientIndex = packet.playerInfo.clientIndex;
|
||||
- newPlayer.handle = packet.playerInfo.handle;
|
||||
- newPlayer.avatar = packet.playerInfo.avatar;
|
||||
-
|
||||
- //position
|
||||
- newPlayer.mapIndex = 0;
|
||||
- newPlayer.position = {0,0};
|
||||
- newPlayer.motion = {0,0};
|
||||
- newPlayer.bbox = {0, 0, 0, 0};
|
||||
-
|
||||
- //TODO: Add the statistic creation code here
|
||||
-
|
||||
- //push this player
|
||||
- playerMap[PlayerEntry::uidCounter] = newPlayer;
|
||||
-
|
||||
- //send the client their info
|
||||
- packet.playerInfo.playerIndex = PlayerEntry::uidCounter;
|
||||
- packet.playerInfo.position = newPlayer.position;
|
||||
- packet.playerInfo.motion = newPlayer.motion;
|
||||
-
|
||||
- //actually send to everyone
|
||||
- PumpPacket(packet);
|
||||
-
|
||||
- //finish this routine
|
||||
- PlayerEntry::uidCounter++;
|
||||
-}
|
||||
-
|
||||
-//TODO: differentiate between delete and unload
|
||||
-void ServerApplication::HandlePlayerDelete(SerialPacket packet) {
|
||||
- //TODO: authenticate who is deleting this player
|
||||
- if (playerMap.find(packet.playerInfo.playerIndex) == playerMap.end()) {
|
||||
- throw(std::runtime_error("Cannot delete a non-existant player"));
|
||||
- }
|
||||
-
|
||||
- //TODO: remove the deleted player from the database?
|
||||
-
|
||||
- //prep the delete packet
|
||||
- SerialPacket delPacket;
|
||||
- delPacket.meta.type = SerialPacket::Type::PLAYER_DELETE;
|
||||
-
|
||||
- //delete the specified playerEntry
|
||||
- erase_if(playerMap, [&](std::pair<unsigned int, PlayerEntry> it) -> bool {
|
||||
- //find the specified PlayerEntry
|
||||
- if (it.first == packet.playerInfo.playerIndex) {
|
||||
- //send to all
|
||||
- delPacket.playerInfo.playerIndex = it.first;
|
||||
- PumpPacket(delPacket);
|
||||
-
|
||||
- //delete this player
|
||||
- return true;
|
||||
- }
|
||||
- //skip this player
|
||||
- return false;
|
||||
- });
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::HandlePlayerUpdate(SerialPacket packet) {
|
||||
- if (playerMap.find(packet.playerInfo.playerIndex) == playerMap.end()) {
|
||||
- throw(std::runtime_error("Cannot update a non-existant player"));
|
||||
- }
|
||||
-
|
||||
- //TODO: the server needs it's own movement system too
|
||||
- playerMap[packet.playerInfo.playerIndex].position = packet.playerInfo.position;
|
||||
- playerMap[packet.playerInfo.playerIndex].motion = packet.playerInfo.motion;
|
||||
-
|
||||
- PumpPacket(packet);
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::HandleRegionRequest(SerialPacket packet) {
|
||||
- char buffer[PACKET_BUFFER_SIZE];
|
||||
- packet.meta.type = SerialPacket::Type::REGION_CONTENT;
|
||||
- packet.regionInfo.region = regionPager.GetRegion(packet.regionInfo.x, packet.regionInfo.y);
|
||||
- serialize(&packet, buffer);
|
||||
- network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE);
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::PumpPacket(SerialPacket packet) {
|
||||
- //NOTE: I don't really like this, but it'll do for now
|
||||
- char buffer[PACKET_BUFFER_SIZE];
|
||||
- serialize(&packet, buffer);
|
||||
- for (auto& it : clientMap) {
|
||||
- network.Send(&it.second.address, buffer, PACKET_BUFFER_SIZE);
|
||||
- }
|
||||
-}
|
||||
Reference in New Issue
Block a user