Merge branch 'network-map'

This commit is contained in:
Kayne Ruse
2014-03-31 21:54:22 +11:00
29 changed files with 708 additions and 120 deletions
+1 -1
View File
@@ -68,7 +68,7 @@ void ClientApplication::Init() {
if (SDLNet_Init()) { if (SDLNet_Init()) {
throw(std::runtime_error("Failed to initialize SDL_net")); throw(std::runtime_error("Failed to initialize SDL_net"));
} }
network.Open(0, sizeof(NetworkPacket)); network.Open(0, PACKET_BUFFER_SIZE);
} }
void ClientApplication::Proc() { void ClientApplication::Proc() {
+15 -15
View File
@@ -35,21 +35,21 @@ InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNet
clientIndex(*argClientIndex) clientIndex(*argClientIndex)
{ {
//setup the utility objects //setup the utility objects
image.LoadSurface(config["dir.interface"] + "button_menu.bmp"); buttonImage.LoadSurface(config["dir.interface"] + "button_menu.bmp");
image.SetClipH(image.GetClipH()/3); buttonImage.SetClipH(buttonImage.GetClipH()/3);
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp"); font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp");
//pass the utility objects //pass the utility objects
disconnectButton.SetImage(&image); disconnectButton.SetImage(&buttonImage);
disconnectButton.SetFont(&font); disconnectButton.SetFont(&font);
shutDownButton.SetImage(&image); shutDownButton.SetImage(&buttonImage);
shutDownButton.SetFont(&font); shutDownButton.SetFont(&font);
//set the button positions //set the button positions
disconnectButton.SetX(50); disconnectButton.SetX(50);
disconnectButton.SetY(50 + image.GetClipH() * 0); disconnectButton.SetY(50 + buttonImage.GetClipH() * 0);
shutDownButton.SetX(50); shutDownButton.SetX(50);
shutDownButton.SetY(50 + image.GetClipH() * 1); shutDownButton.SetY(50 + buttonImage.GetClipH() * 1);
//set the button texts //set the button texts
disconnectButton.SetText("Disconnect"); disconnectButton.SetText("Disconnect");
@@ -65,14 +65,14 @@ InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNet
packet.playerInfo.motion = {0,0}; packet.playerInfo.motion = {0,0};
//send it //send it
char buffer[sizeof(NetworkPacket)]; char buffer[PACKET_BUFFER_SIZE];
serialize(&packet, buffer); serialize(&packet, buffer);
network.Send(Channels::SERVER, buffer, sizeof(NetworkPacket)); network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
//request a sync //request a sync
packet.meta.type = NetworkPacket::Type::SYNCHRONIZE; packet.meta.type = NetworkPacket::Type::SYNCHRONIZE;
serialize(&packet, buffer); serialize(&packet, buffer);
network.Send(Channels::SERVER, buffer, sizeof(NetworkPacket)); network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
} }
InWorld::~InWorld() { InWorld::~InWorld() {
@@ -290,7 +290,7 @@ void InWorld::HandlePlayerUpdate(NetworkPacket packet) {
void InWorld::SendState() { void InWorld::SendState() {
NetworkPacket packet; NetworkPacket packet;
char buffer[sizeof(NetworkPacket)]; char buffer[PACKET_BUFFER_SIZE];
//pack the packet //pack the packet
packet.meta.type = NetworkPacket::Type::PLAYER_UPDATE; packet.meta.type = NetworkPacket::Type::PLAYER_UPDATE;
@@ -302,27 +302,27 @@ void InWorld::SendState() {
packet.playerInfo.motion = localCharacter->GetMotion(); packet.playerInfo.motion = localCharacter->GetMotion();
serialize(&packet, buffer); serialize(&packet, buffer);
network.Send(Channels::SERVER, buffer, sizeof(NetworkPacket)); network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
} }
void InWorld::RequestDisconnect() { void InWorld::RequestDisconnect() {
NetworkPacket packet; NetworkPacket packet;
char buffer[sizeof(NetworkPacket)]; char buffer[PACKET_BUFFER_SIZE];
//send a disconnect request //send a disconnect request
packet.meta.type = NetworkPacket::Type::DISCONNECT; packet.meta.type = NetworkPacket::Type::DISCONNECT;
packet.clientInfo.index = clientIndex; packet.clientInfo.index = clientIndex;
serialize(&packet, buffer); serialize(&packet, buffer);
network.Send(Channels::SERVER, buffer, sizeof(NetworkPacket)); network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
} }
void InWorld::RequestShutDown() { void InWorld::RequestShutDown() {
NetworkPacket packet; NetworkPacket packet;
char buffer[sizeof(NetworkPacket)]; char buffer[PACKET_BUFFER_SIZE];
//send a shutdown request //send a shutdown request
packet.meta.type = NetworkPacket::Type::SHUTDOWN; packet.meta.type = NetworkPacket::Type::SHUTDOWN;
packet.clientInfo.index = clientIndex; packet.clientInfo.index = clientIndex;
serialize(&packet, buffer); serialize(&packet, buffer);
network.Send(Channels::SERVER, buffer, sizeof(NetworkPacket)); network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
} }
+27 -5
View File
@@ -22,17 +22,29 @@
#ifndef INWORLD_HPP_ #ifndef INWORLD_HPP_
#define INWORLD_HPP_ #define INWORLD_HPP_
#include "base_scene.hpp" //maps
#include "map_generator.hpp"
#include "map_file_format.hpp"
#include "region_pager.hpp"
#include "config_utility.hpp" //networking
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
#include "network_packet.hpp" #include "network_packet.hpp"
#include "serial.hpp" #include "serial.hpp"
//graphics
#include "image.hpp" #include "image.hpp"
#include "raster_font.hpp" #include "raster_font.hpp"
#include "button.hpp" #include "button.hpp"
//common
#include "config_utility.hpp"
//client
#include "base_scene.hpp"
#include "player_character.hpp" #include "player_character.hpp"
//STL
#include <map> #include <map>
class InWorld : public BaseScene { class InWorld : public BaseScene {
@@ -66,16 +78,26 @@ protected:
void RequestDisconnect(); void RequestDisconnect();
void RequestShutDown(); void RequestShutDown();
//global //globals
ConfigUtility& config; ConfigUtility& config;
UDPNetworkUtility& network; UDPNetworkUtility& network;
int& clientIndex; int& clientIndex;
//members //graphics
Image image; Image buttonImage;
RasterFont font; RasterFont font;
//map
RegionPager<BlankGenerator, DummyFormat> mapPager;
//UI
Button disconnectButton; Button disconnectButton;
Button shutDownButton; Button shutDownButton;
struct {
int x = 0, y = 0;
} camera;
//game
std::map<int, PlayerCharacter> playerCharacters; std::map<int, PlayerCharacter> playerCharacters;
PlayerCharacter* localCharacter = nullptr; PlayerCharacter* localCharacter = nullptr;
int playerIndex = -1; int playerIndex = -1;
+4 -4
View File
@@ -127,12 +127,12 @@ void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
if (search.MouseButtonUp(button) == Button::State::HOVER) { if (search.MouseButtonUp(button) == Button::State::HOVER) {
//the vars //the vars
NetworkPacket packet; NetworkPacket packet;
char buffer[sizeof(NetworkPacket)]; char buffer[PACKET_BUFFER_SIZE];
//broadcast to the network, or a specific server //broadcast to the network, or a specific server
packet.meta.type = NetworkPacket::Type::BROADCAST_REQUEST; packet.meta.type = NetworkPacket::Type::BROADCAST_REQUEST;
serialize(&packet, buffer); serialize(&packet, buffer);
network.Send(config["server.host"].c_str(), config.Int("server.port"), buffer, sizeof(NetworkPacket)); network.Send(config["server.host"].c_str(), config.Int("server.port"), buffer, PACKET_BUFFER_SIZE);
//reset the server list //reset the server list
serverInfo.clear(); serverInfo.clear();
@@ -142,12 +142,12 @@ void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr) { else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr) {
//the vars //the vars
NetworkPacket packet; NetworkPacket packet;
char buffer[sizeof(NetworkPacket)]; char buffer[PACKET_BUFFER_SIZE];
//join the selected server //join the selected server
packet.meta.type = NetworkPacket::Type::JOIN_REQUEST; packet.meta.type = NetworkPacket::Type::JOIN_REQUEST;
serialize(&packet, buffer); serialize(&packet, buffer);
network.Send(&selection->address, buffer, sizeof(NetworkPacket)); network.Send(&selection->address, buffer, PACKET_BUFFER_SIZE);
selection = nullptr; selection = nullptr;
} }
+1
View File
@@ -22,6 +22,7 @@ all: $(OBJ) $(OUT)
ar -crs $(OUT) $(OBJ) ar -crs $(OUT) $(OBJ)
$(MAKE) -C graphics $(MAKE) -C graphics
$(MAKE) -C map $(MAKE) -C map
$(MAKE) -C script
$(MAKE) -C network $(MAKE) -C network
$(MAKE) -C ui $(MAKE) -C ui
+51 -2
View File
@@ -21,10 +21,59 @@
*/ */
#include "map_file_format.hpp" #include "map_file_format.hpp"
void MapFileFormat::Load(Region** const ptr, int x, int y) { #include <stdexcept>
void DummyFormat::Load(Region** const ptr, int width, int height, int depth, int x, int y) {
//TODO //TODO
} }
void MapFileFormat::Save(Region* const ptr) { void DummyFormat::Save(Region* const ptr) {
//TODO //TODO
} }
/*
void VerboseFormat::Load(Region** const ptr, int x, int y) {
//TODO
}
void VerboseFormat::Save(Region* const ptr) {
//TODO
}
void CompactFormat::Load(Region** const ptr, int x, int y) {
//TODO
}
void CompactFormat::Save(Region* const ptr) {
//TODO
}
*/
void LuaFormat::Load(Region** const ptr, int width, int height, int depth, int x, int y) {
//something to load into
(*ptr) = new Region(width, height, depth, x, y);
//API hook
lua_getglobal(state, "Region");
lua_getfield(state, -1, "Load");
lua_pushlightuserdata(state, *ptr);
lua_pushstring(state, saveDir.c_str());
if (lua_pcall(state, 2, 1, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
}
if (lua_toboolean(state, -1) == false) {
delete (*ptr);
(*ptr) = nullptr;
}
lua_pop(state, 2);
}
void LuaFormat::Save(Region* const ptr) {
//API hook
lua_getglobal(state, "Region");
lua_getfield(state, -1, "Save");
lua_pushlightuserdata(state, ptr);
lua_pushstring(state, saveDir.c_str());
if (lua_pcall(state, 2, 0, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
}
lua_pop(state, 1);
}
+47 -3
View File
@@ -24,12 +24,56 @@
#include "region.hpp" #include "region.hpp"
class MapFileFormat { #include "lua/lua.hpp"
#include <string>
class DummyFormat {
public: public:
void Load(Region** const, int x, int y); void Load(Region** const, int width, int height, int depth, int x, int y);
void Save(Region* const); void Save(Region* const);
std::string SetSaveDir(std::string s) { return saveDir = s; }
std::string GetSaveDir() { return saveDir; }
private: private:
// std::string saveDir;
};
/*
class VerboseFormat {
public:
void Load(Region** const, int width, int height, int depth, int x, int y);
void Save(Region* const);
std::string SetSaveDir(std::string s) { return saveDir = s; }
std::string GetSaveDir() { return saveDir; }
private:
std::string saveDir;
};
class CompactFormat {
public:
void Load(Region** const, int width, int height, int depth, int x, int y);
void Save(Region* const);
std::string SetSaveDir(std::string s) { return saveDir = s; }
std::string GetSaveDir() { return saveDir; }
private:
std::string saveDir;
};
*/
class LuaFormat {
public:
void Load(Region** const, int width, int height, int depth, int x, int y);
void Save(Region* const);
std::string SetSaveDir(std::string s) { return saveDir = s; }
std::string GetSaveDir() { return saveDir; }
lua_State* SetLuaState(lua_State* L) { return state = L; }
lua_State* GetLuaState() { return state; }
private:
std::string saveDir;
lua_State* state = nullptr;
}; };
#endif #endif
+40 -2
View File
@@ -21,10 +21,48 @@
*/ */
#include "map_generator.hpp" #include "map_generator.hpp"
void MapGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) { #include <stdexcept>
void BlankGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) {
(*ptr) = new Region(width, height, depth, x, y); (*ptr) = new Region(width, height, depth, x, y);
} }
void MapGenerator::Unload(Region* const ptr) { void BlankGenerator::Unload(Region* const ptr) {
delete ptr;
}
/*
void PerlinGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) {
(*ptr) = new Region(width, height, depth, x, y);
}
void PerlinGenerator::Unload(Region* const ptr) {
delete ptr;
}
*/
void LuaGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) {
//something to work on
(*ptr) = new Region(width, height, depth, x, y);
//API hook
lua_getglobal(state, "Region");
lua_getfield(state, -1, "Create");
lua_pushlightuserdata(state, *ptr);
if (lua_pcall(state, 1, 0, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
}
lua_pop(state, 1);
}
void LuaGenerator::Unload(Region* const ptr) {
//API hook
lua_getglobal(state, "Region");
lua_getfield(state, -1, "Unload");
lua_pushlightuserdata(state, ptr);
if (lua_pcall(state, 1, 0, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
}
lua_pop(state, 1);
//clean up the memory
delete ptr; delete ptr;
} }
+22 -1
View File
@@ -24,12 +24,33 @@
#include "region.hpp" #include "region.hpp"
class MapGenerator { #include "lua/lua.hpp"
class BlankGenerator {
public: public:
void Create(Region** const, int width, int height, int depth, int x, int y); void Create(Region** const, int width, int height, int depth, int x, int y);
void Unload(Region* const); void Unload(Region* const);
private: private:
// //
}; };
/*
class PerlinGenerator {
public:
void Create(Region** const, int width, int height, int depth, int x, int y);
void Unload(Region* const);
private:
//
};
*/
class LuaGenerator {
public:
void Create(Region** const, int width, int height, int depth, int x, int y);
void Unload(Region* const);
lua_State* SetLuaState(lua_State* L) { return state = L; }
lua_State* GetLuaState() { return state; }
private:
lua_State* state = nullptr;
};
#endif #endif
+10 -10
View File
@@ -28,12 +28,12 @@ Region::Region(int argWidth, int argHeight, int argDepth, int argX, int argY):
x(argX), x(argX),
y(argY) y(argY)
{ {
tiles = new int**[width]; tiles = new type_t**[width];
for (int i = 0; i < width; ++i) { for (register int i = 0; i < width; ++i) {
tiles[i] = new int*[height]; tiles[i] = new type_t*[height];
for (int j = 0; j < height; ++j) { for (register int j = 0; j < height; ++j) {
tiles[i][j] = new int[depth]; tiles[i][j] = new type_t[depth];
for (int k = 0; k < depth; ++k) { for (register int k = 0; k < depth; ++k) {
tiles[i][j][k] = 0; tiles[i][j][k] = 0;
} }
} }
@@ -41,8 +41,8 @@ Region::Region(int argWidth, int argHeight, int argDepth, int argX, int argY):
} }
Region::~Region() { Region::~Region() {
for (int i = 0; i < width; ++i) { for (register int i = 0; i < width; ++i) {
for (int j = 0; j < height; j++) { for (register int j = 0; j < height; j++) {
delete tiles[i][j]; delete tiles[i][j];
} }
delete tiles[i]; delete tiles[i];
@@ -50,10 +50,10 @@ Region::~Region() {
delete tiles; delete tiles;
} }
int Region::SetTile(int x, int y, int z, int v) { Region::type_t Region::SetTile(int x, int y, int z, type_t v) {
return tiles[x][y][z] = v; return tiles[x][y][z] = v;
} }
int Region::GetTile(int x, int y, int z) { Region::type_t Region::GetTile(int x, int y, int z) {
return tiles[x][y][z]; return tiles[x][y][z];
} }
+15 -8
View File
@@ -22,21 +22,28 @@
#ifndef REGION_HPP_ #ifndef REGION_HPP_
#define REGION_HPP_ #define REGION_HPP_
//temporary?
#define REGION_WIDTH 20
#define REGION_HEIGHT 20
#define REGION_DEPTH 3
class Region { class Region {
public: public:
typedef unsigned short type_t;
Region() = delete; Region() = delete;
Region(int width, int height, int depth, int x, int y); Region(int width, int height, int depth, int x, int y);
~Region(); ~Region();
int SetTile(int x, int y, int z, int v); type_t SetTile(int x, int y, int z, type_t v);
int GetTile(int x, int y, int z); type_t GetTile(int x, int y, int z);
//accessors //accessors
int GetWidth() { return width; } int GetWidth() const { return width; }
int GetHeight() { return height; } int GetHeight() const { return height; }
int GetDepth() { return depth; } int GetDepth() const { return depth; }
int GetX() { return x; } int GetX() const { return x; }
int GetY() { return y; } int GetY() const { return y; }
private: private:
const int width; const int width;
const int height; const int height;
@@ -44,7 +51,7 @@ private:
const int x; const int x;
const int y; const int y;
int*** tiles = nullptr; type_t*** tiles = nullptr;
}; };
#endif #endif
+2 -6
View File
@@ -35,12 +35,12 @@ RegionPagerBase::~RegionPagerBase() {
//EMPTY //EMPTY
} }
int RegionPagerBase::SetTile(int x, int y, int z, int v) { Region::type_t RegionPagerBase::SetTile(int x, int y, int z, Region::type_t v) {
Region* ptr = GetRegion(x, y); Region* ptr = GetRegion(x, y);
return ptr->SetTile(x - ptr->GetX(), y - ptr->GetY(), z, v); return ptr->SetTile(x - ptr->GetX(), y - ptr->GetY(), z, v);
} }
int RegionPagerBase::GetTile(int x, int y, int z) { Region::type_t RegionPagerBase::GetTile(int x, int y, int z) {
Region* ptr = GetRegion(x, y); Region* ptr = GetRegion(x, y);
return ptr->GetTile(x - ptr->GetX(), y - ptr->GetY(), z); return ptr->GetTile(x - ptr->GetX(), y - ptr->GetY(), z);
} }
@@ -62,7 +62,3 @@ Region* RegionPagerBase::GetRegion(int x, int y) {
if (ptr) return ptr; if (ptr) return ptr;
return CreateRegion(x, y); return CreateRegion(x, y);
} }
void RegionPagerBase::Update() {
//TODO
}
+25 -14
View File
@@ -29,14 +29,12 @@
class RegionPagerBase { class RegionPagerBase {
public: public:
RegionPagerBase() = delete; RegionPagerBase() = default;
RegionPagerBase(int regionWidth, int regionHeight, int regionDepth); RegionPagerBase(int regionWidth, int regionHeight, int regionDepth);
virtual ~RegionPagerBase(); virtual ~RegionPagerBase();
int SetTile(int x, int y, int z, int v); Region::type_t SetTile(int x, int y, int z, Region::type_t v);
int GetTile(int x, int y, int z); Region::type_t GetTile(int x, int y, int z);
void Update();
Region* GetRegion(int x, int y); Region* GetRegion(int x, int y);
@@ -47,26 +45,33 @@ public:
virtual void UnloadRegion(int x, int y) = 0; virtual void UnloadRegion(int x, int y) = 0;
//accessors //accessors
int GetRegionWidth() { return regionWidth; } //NOTE: don't change the sizes mid-program, it will cause issues
int GetRegionHeight() { return regionHeight; } int SetRegionWidth(int i) { return regionWidth = i; }
int GetRegionDepth() { return regionDepth; } int SetRegionHeight(int i) { return regionHeight = i; }
int SetRegionDepth(int i) { return regionDepth = i; }
int GetRegionWidth() const { return regionWidth; }
int GetRegionHeight() const { return regionHeight; }
int GetRegionDepth() const { return regionDepth; }
protected: protected:
const int regionWidth; int regionWidth;
const int regionHeight; int regionHeight;
const int regionDepth; int regionDepth;
std::list<Region*> regionList; std::list<Region*> regionList;
}; };
template<typename MapGenerator, typename MapFileFormat> template<typename MapGenerator, typename MapFileFormat>
class RegionPager : public RegionPagerBase { class RegionPager : public RegionPagerBase {
public: public:
RegionPager() = delete; RegionPager() = default;
RegionPager(int w, int h, int d): RegionPager(int w, int h, int d):
RegionPagerBase(w, h, d) RegionPagerBase(w, h, d)
{ {
//EMPTY //EMPTY
} }
~RegionPager() = default; ~RegionPager() {
UnloadAll();
}
Region* LoadRegion(int x, int y) { Region* LoadRegion(int x, int y) {
//snap the coords //snap the coords
@@ -75,7 +80,7 @@ public:
//load the region if possible //load the region if possible
Region* ptr = nullptr; Region* ptr = nullptr;
format.Load(&ptr, x, y); format.Load(&ptr, regionWidth, regionHeight, regionDepth, x, y);
if (ptr) { if (ptr) {
regionList.push_back(ptr); regionList.push_back(ptr);
return ptr; return ptr;
@@ -127,6 +132,12 @@ public:
++it; ++it;
} }
} }
void UnloadAll() {
for (auto& it : regionList) {
generator.Unload(it);
}
regionList.clear();
}
//accessors //accessors
MapGenerator* GetGenerator() { return &generator; } MapGenerator* GetGenerator() { return &generator; }
+1 -1
View File
@@ -1,5 +1,5 @@
#config #config
INCLUDES+=. .. INCLUDES+=. .. ../map
LIBS+= LIBS+=
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
+13 -3
View File
@@ -22,9 +22,10 @@
#ifndef NETWORKPACKET_HPP_ #ifndef NETWORKPACKET_HPP_
#define NETWORKPACKET_HPP_ #define NETWORKPACKET_HPP_
#include "SDL/SDL_net.h"
#include "vector2.hpp" #include "vector2.hpp"
#include "region.hpp"
#include "SDL/SDL_net.h"
#define PACKET_STRING_SIZE 100 #define PACKET_STRING_SIZE 100
@@ -61,6 +62,10 @@ union NetworkPacket {
PLAYER_NEW = 10, PLAYER_NEW = 10,
PLAYER_DELETE = 11, PLAYER_DELETE = 11,
PLAYER_UPDATE = 12, PLAYER_UPDATE = 12,
//map data
REGION_REQUEST = 13,
REGION_CONTENT = 14,
}; };
//metadata on the packet itself //metadata on the packet itself
@@ -75,6 +80,7 @@ union NetworkPacket {
//TODO: version info //TODO: version info
char name[PACKET_STRING_SIZE]; char name[PACKET_STRING_SIZE];
//TODO: player count //TODO: player count
//TODO: map format
}serverInfo; }serverInfo;
//information about the client //information about the client
@@ -95,7 +101,11 @@ union NetworkPacket {
}playerInfo; }playerInfo;
//map data //map data
//... struct RegionInformation {
Metadata meta;
int width, height, depth, x, y;
Region* region;
}regionInfo;
//defaults //defaults
NetworkPacket() { NetworkPacket() {
+112 -20
View File
@@ -21,36 +21,31 @@
*/ */
#include "serial.hpp" #include "serial.hpp"
#include <cstring> #include "map_generator.hpp"
//#include <iostream>
//using namespace std; #include <cstring>
//------------------------- //-------------------------
//internal serialization functions //internal serialization functions
//------------------------- //-------------------------
void serializeType(NetworkPacket* packet, char* buffer) { void serializeType(NetworkPacket* packet, char* buffer) {
// cout << "serializeType" << endl;
memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type)); memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type));
} }
void serializeServer(NetworkPacket* packet, char* buffer) { void serializeServer(NetworkPacket* packet, char* buffer) {
// cout << "serializeServer" << endl;
memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type)); memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type));
buffer += sizeof(NetworkPacket::Type); buffer += sizeof(NetworkPacket::Type);
memcpy(buffer, packet->serverInfo.name, PACKET_STRING_SIZE); memcpy(buffer, packet->serverInfo.name, PACKET_STRING_SIZE);
} }
void serializeClient(NetworkPacket* packet, char* buffer) { void serializeClient(NetworkPacket* packet, char* buffer) {
// cout << "serializeClient" << endl;
memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type)); memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type));
buffer += sizeof(NetworkPacket::Type); buffer += sizeof(NetworkPacket::Type);
memcpy(buffer, &packet->clientInfo.index, sizeof(int)); memcpy(buffer, &packet->clientInfo.index, sizeof(int));
} }
void serializePlayer(NetworkPacket* packet, char* buffer) { void serializePlayer(NetworkPacket* packet, char* buffer) {
// cout << "serializePlayer" << endl;
memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type)); memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type));
buffer += sizeof(NetworkPacket::Type); buffer += sizeof(NetworkPacket::Type);
@@ -76,31 +71,75 @@ void serializePlayer(NetworkPacket* packet, char* buffer) {
memcpy(buffer, &packet->playerInfo.motion.y, sizeof(double)); memcpy(buffer, &packet->playerInfo.motion.y, sizeof(double));
} }
void serializeRegionFormat(NetworkPacket* packet, char* buffer) {
memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type));
buffer += sizeof(NetworkPacket::Type);
//size
memcpy(buffer, &packet->regionInfo.width, sizeof(int));
buffer += sizeof(int);
memcpy(buffer, &packet->regionInfo.height, sizeof(int));
buffer += sizeof(int);
memcpy(buffer, &packet->regionInfo.depth, sizeof(int));
buffer += sizeof(int);
//x & y
memcpy(buffer, &packet->regionInfo.x, sizeof(int));
buffer += sizeof(int);
memcpy(buffer, &packet->regionInfo.y, sizeof(int));
}
void serializeRegionContent(NetworkPacket* packet, char* buffer) {
//format
memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type));
buffer += sizeof(NetworkPacket::Type);
//size
*reinterpret_cast<int*>(buffer) = packet->regionInfo.region->GetWidth();
buffer += sizeof(int);
*reinterpret_cast<int*>(buffer) = packet->regionInfo.region->GetHeight();
buffer += sizeof(int);
*reinterpret_cast<int*>(buffer) = packet->regionInfo.region->GetDepth();
buffer += sizeof(int);
//x & y
*reinterpret_cast<int*>(buffer) = packet->regionInfo.region->GetX();
buffer += sizeof(int);
*reinterpret_cast<int*>(buffer) = packet->regionInfo.region->GetY();
buffer += sizeof(int);
//content
for (register int i = 0; i < packet->regionInfo.region->GetWidth(); i++) {
for (register int j = 0; j < packet->regionInfo.region->GetHeight(); j++) {
for (register int k = 0; k < packet->regionInfo.region->GetDepth(); k++) {
*reinterpret_cast<Region::type_t*>(buffer) = packet->regionInfo.region->GetTile(i, j, k);
buffer += sizeof(Region::type_t);
}
}
}
}
//------------------------- //-------------------------
//internal deserialization functions //internal deserialization functions
//------------------------- //-------------------------
void deserializeType(NetworkPacket* packet, char* buffer) { void deserializeType(NetworkPacket* packet, char* buffer) {
// cout << "deserializeType" << endl;
memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type)); memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type));
} }
void deserializeServer(NetworkPacket* packet, char* buffer) { void deserializeServer(NetworkPacket* packet, char* buffer) {
// cout << "deserializeServer" << endl;
memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type)); memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type));
buffer += sizeof(NetworkPacket::Type); buffer += sizeof(NetworkPacket::Type);
memcpy(packet->serverInfo.name, buffer, PACKET_STRING_SIZE); memcpy(packet->serverInfo.name, buffer, PACKET_STRING_SIZE);
} }
void deserializeClient(NetworkPacket* packet, char* buffer) { void deserializeClient(NetworkPacket* packet, char* buffer) {
// cout << "deserializeClient" << endl;
memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type)); memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type));
buffer += sizeof(NetworkPacket::Type); buffer += sizeof(NetworkPacket::Type);
memcpy(&packet->clientInfo.index, buffer, sizeof(int)); memcpy(&packet->clientInfo.index, buffer, sizeof(int));
} }
void deserializePlayer(NetworkPacket* packet, char* buffer) { void deserializePlayer(NetworkPacket* packet, char* buffer) {
// cout << "deserializePlayer" << endl;
memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type)); memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type));
buffer += sizeof(NetworkPacket::Type); buffer += sizeof(NetworkPacket::Type);
@@ -126,6 +165,49 @@ void deserializePlayer(NetworkPacket* packet, char* buffer) {
memcpy(&packet->playerInfo.motion.y, buffer, sizeof(double)); memcpy(&packet->playerInfo.motion.y, buffer, sizeof(double));
} }
void deserializeRegionFormat(NetworkPacket* packet, char* buffer) {
memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type));
buffer += sizeof(NetworkPacket::Type);
//size
memcpy(&packet->regionInfo.width, buffer, sizeof(int));
buffer += sizeof(int);
memcpy(&packet->regionInfo.height, buffer, sizeof(int));
buffer += sizeof(int);
memcpy(&packet->regionInfo.depth, buffer, sizeof(int));
buffer += sizeof(int);
//x & y
memcpy(&packet->regionInfo.x, buffer, sizeof(int));
buffer += sizeof(int);
memcpy(&packet->regionInfo.y, buffer, sizeof(int));
}
void deserializeRegionContent(NetworkPacket* packet, char* buffer) {
//format
deserializeRegionFormat(packet, buffer);
buffer += sizeof(int) * 5 + sizeof(NetworkPacket::Type);
//content
BlankGenerator().Create(
&packet->regionInfo.region,
packet->regionInfo.width,
packet->regionInfo.height,
packet->regionInfo.depth,
packet->regionInfo.x,
packet->regionInfo.y
);
for (register int i = 0; i < packet->regionInfo.region->GetWidth(); i++) {
for (register int j = 0; j < packet->regionInfo.region->GetHeight(); j++) {
for (register int k = 0; k < packet->regionInfo.region->GetDepth(); k++) {
packet->regionInfo.region->SetTile(i, j, k, *reinterpret_cast<Region::type_t*>(buffer));
buffer += sizeof(Region::type_t);
}
}
}
}
//------------------------- //-------------------------
//the interface functions //the interface functions
//------------------------- //-------------------------
@@ -160,11 +242,16 @@ void serialize(NetworkPacket* packet, void* buffer) {
case NetworkPacket::Type::PLAYER_UPDATE: case NetworkPacket::Type::PLAYER_UPDATE:
serializePlayer(packet, reinterpret_cast<char*>(buffer)); serializePlayer(packet, reinterpret_cast<char*>(buffer));
break; break;
//region info
case NetworkPacket::Type::REGION_REQUEST:
serializeRegionFormat(packet, reinterpret_cast<char*>(buffer));
break;
case NetworkPacket::Type::REGION_CONTENT:
serializeRegionContent(packet, reinterpret_cast<char*>(buffer));
break;
} }
// for (int i = 0; i < sizeof(NetworkPacket); i++) {
// cout << ((char*)(buffer))[i];
// }
// cout << endl;
} }
void deserialize(NetworkPacket* packet, void* buffer) { void deserialize(NetworkPacket* packet, void* buffer) {
@@ -178,7 +265,7 @@ void deserialize(NetworkPacket* packet, void* buffer) {
case NetworkPacket::Type::BROADCAST_REQUEST: case NetworkPacket::Type::BROADCAST_REQUEST:
case NetworkPacket::Type::JOIN_REQUEST: case NetworkPacket::Type::JOIN_REQUEST:
case NetworkPacket::Type::SYNCHRONIZE: case NetworkPacket::Type::SYNCHRONIZE:
// //NOTHING
break; break;
//Server info //Server info
@@ -199,9 +286,14 @@ void deserialize(NetworkPacket* packet, void* buffer) {
case NetworkPacket::Type::PLAYER_UPDATE: case NetworkPacket::Type::PLAYER_UPDATE:
deserializePlayer(packet, reinterpret_cast<char*>(buffer)); deserializePlayer(packet, reinterpret_cast<char*>(buffer));
break; break;
//region info
case NetworkPacket::Type::REGION_REQUEST:
deserializeRegionFormat(packet, reinterpret_cast<char*>(buffer));
break;
case NetworkPacket::Type::REGION_CONTENT:
deserializeRegionContent(packet, reinterpret_cast<char*>(buffer));
break;
} }
// for (int i = 0; i < sizeof(NetworkPacket); i++) {
// cout << ((char*)(buffer))[i];
// }
// cout << endl;
} }
+7
View File
@@ -24,6 +24,13 @@
#include "network_packet.hpp" #include "network_packet.hpp"
/* Sending regions are the largest type of packet
* content: width * height * depth * sizoeof(type)
* map format: sizeof(int) * 5
* metadata: sizeof(metadata)
*/
#define PACKET_BUFFER_SIZE REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + sizeof(int) * 5 + sizeof(NetworkPacket::Metadata)
void serialize(NetworkPacket* const, void*); void serialize(NetworkPacket* const, void*);
void deserialize(NetworkPacket* const, void*); void deserialize(NetworkPacket* const, void*);
+72
View File
@@ -0,0 +1,72 @@
/*
** $Id: linit.c,v 1.32 2011/04/08 19:17:36 roberto Exp $
** Initialization of libraries for lua.c and other clients
** See Copyright Notice in lua.h
*/
/* Modified for use in Tortuga, renamed to linit.cpp
*/
/*
** If you embed Lua in your program and need to open the standard
** libraries, call luaL_openlibs in your program. If you need a
** different set of libraries, copy this file to your project and edit
** it to suit your needs.
*/
#define linit_c
#define LUA_LIB
#include "lua/lua.hpp"
#include "region_api.hpp"
/*
** these libs are loaded by lua.c and are readily available to any Lua
** program
*/
static const luaL_Reg loadedlibs[] = {
/* Standard libs */
{"_G", luaopen_base},
{LUA_LOADLIBNAME, luaopen_package},
{LUA_COLIBNAME, luaopen_coroutine},
{LUA_TABLIBNAME, luaopen_table},
{LUA_IOLIBNAME, luaopen_io},
{LUA_OSLIBNAME, luaopen_os},
{LUA_STRLIBNAME, luaopen_string},
{LUA_BITLIBNAME, luaopen_bit32},
{LUA_MATHLIBNAME, luaopen_math},
{LUA_DBLIBNAME, luaopen_debug},
/* custom libs */
{LUA_REGIONLIBNAME, luaopen_regionapi},
{NULL, NULL}
};
/*
** these libs are preloaded and must be required before used
*/
static const luaL_Reg preloadedlibs[] = {
{NULL, NULL}
};
LUALIB_API void luaL_openlibs (lua_State *L) {
const luaL_Reg *lib;
/* call open functions from 'loadedlibs' and set results to global table */
for (lib = loadedlibs; lib->func; lib++) {
luaL_requiref(L, lib->name, lib->func, 1);
lua_pop(L, 1); /* remove lib */
}
/* add open functions from 'preloadedlibs' into 'package.preload' table */
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
for (lib = preloadedlibs; lib->func; lib++) {
lua_pushcfunction(L, lib->func);
lua_setfield(L, -2, lib->name);
}
lua_pop(L, 1); /* remove _PRELOAD table */
}
+43
View File
@@ -0,0 +1,43 @@
#config
INCLUDES+=. .. ../map
LIBS+=
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
#source
CXXSRC=$(wildcard *.cpp)
CSRC=$(wildcard *.c)
#objects
OBJDIR=obj
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o))
#output
OUTDIR=../..
OUT=$(addprefix $(OUTDIR)/,libcommon.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 $@ $<
$(OBJDIR)/%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
clean:
$(RM) *.o *.a *.exe
rebuild: clean all
+91
View File
@@ -0,0 +1,91 @@
/* 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 "region_api.hpp"
#include "region.hpp"
static int setTile(lua_State* L) {
Region* ptr = (Region*)lua_touserdata(L, 1);
ptr->SetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1, lua_tointeger(L, 5));
return 0;
}
static int getTile(lua_State* L) {
Region* ptr = (Region*)lua_touserdata(L, 1);
int ret = ptr->GetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1);
lua_pushnumber(L, ret);
return 1;
}
static int getWidth(lua_State* L) {
Region* ptr = (Region*)lua_touserdata(L, 1);
lua_pushinteger(L, ptr->GetWidth());
return 1;
}
static int getHeight(lua_State* L) {
Region* ptr = (Region*)lua_touserdata(L, 1);
lua_pushinteger(L, ptr->GetHeight());
return 1;
}
static int getDepth(lua_State* L) {
Region* ptr = (Region*)lua_touserdata(L, 1);
lua_pushinteger(L, ptr->GetDepth());
return 1;
}
static int getX(lua_State* L) {
Region* ptr = (Region*)lua_touserdata(L, 1);
lua_pushinteger(L, ptr->GetX());
return 1;
}
static int getY(lua_State* L) {
Region* ptr = (Region*)lua_touserdata(L, 1);
lua_pushinteger(L, ptr->GetY());
return 1;
}
static int dummy(lua_State* L) {
return 0;
}
static const luaL_Reg regionlib[] = {
{"SetTile",setTile},
{"GetTile",getTile},
{"GetWidth",getWidth},
{"GetHeight",getHeight},
{"GetDepth",getDepth},
{"GetX",getX},
{"GetY",getY},
{"Create", dummy},
{"Unload", dummy},
{"Load", dummy},
{"Save", dummy},
{nullptr, nullptr}
};
LUAMOD_API int luaopen_regionapi(lua_State* L) {
luaL_newlib(L, regionlib);
return 1;
}
+30
View File
@@ -0,0 +1,30 @@
/* 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 REGIONAPI_HPP_
#define REGIONAPI_HPP_
#include "lua/lua.hpp"
#define LUA_REGIONLIBNAME "Region"
LUAMOD_API int luaopen_regionapi(lua_State* L);
#endif
+8 -3
View File
@@ -34,8 +34,7 @@ using namespace std;
//------------------------- //-------------------------
EditorScene::EditorScene(ConfigUtility* const arg1): EditorScene::EditorScene(ConfigUtility* const arg1):
config(*arg1), config(*arg1)
pager(20, 20, 3)
{ {
//create the debugging "window" //create the debugging "window"
debugInfo.CreateSurface(256, 256); debugInfo.CreateSurface(256, 256);
@@ -56,8 +55,13 @@ EditorScene::EditorScene(ConfigUtility* const arg1):
{"Debug", "Debug On", "Debug Off", "Toggle", "Testificate"} {"Debug", "Debug On", "Debug Off", "Toggle", "Testificate"}
}); });
//setup the map
pager.SetRegionWidth(REGION_WIDTH);
pager.SetRegionHeight(REGION_HEIGHT);
pager.SetRegionDepth(REGION_DEPTH);
//debug //debug
tsheet.Load("rsc\\graphics\\tilesets\\sand.bmp", 12, 3); tsheet.Load(config["dir.tilesets"] + "sand.bmp", 12, 3);
} }
EditorScene::~EditorScene() { EditorScene::~EditorScene() {
@@ -85,6 +89,7 @@ void EditorScene::Render(SDL_Surface* const screen) {
for (int i = 0; i < pager.GetRegionWidth()*2; i++) { for (int i = 0; i < pager.GetRegionWidth()*2; i++) {
for (int j = 0; j < pager.GetRegionHeight()*2; j++) { for (int j = 0; j < pager.GetRegionHeight()*2; j++) {
for (int k = 0; k < pager.GetRegionDepth(); k++) { for (int k = 0; k < pager.GetRegionDepth(); k++) {
//TODO: skip the out-of-bounds regions
tsheet.DrawTo( tsheet.DrawTo(
screen, screen,
i*tsheet.GetTileW()-camera.x, i*tsheet.GetTileW()-camera.x,
+1 -1
View File
@@ -73,7 +73,7 @@ protected:
int x = 0, y = 0; int x = 0, y = 0;
} camera; } camera;
RegionPager<MapGenerator, MapFileFormat> pager; RegionPager<BlankGenerator, DummyFormat> pager;
TileSheet tsheet; TileSheet tsheet;
}; };
+1 -1
View File
@@ -1,6 +1,6 @@
#config #config
INCLUDES+=../common ../common/graphics ../common/map ../common/ui INCLUDES+=../common ../common/graphics ../common/map ../common/ui
LIBS+=../libcommon.a -lmingw32 -lSDLmain -lSDL LIBS+=../libcommon.a -lmingw32 -lSDLmain -lSDL -llua
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
+5
View File
@@ -17,6 +17,11 @@ dir.tilesets = rsc/graphics/tilesets/
dir.interface = rsc/graphics/interface/ dir.interface = rsc/graphics/interface/
dir.scripts = rsc/scripts/ dir.scripts = rsc/scripts/
#map system
map.pager.width = 20
map.pager.height = 20
map.pager.depth = 3
#player options #player options
player.handle = username player.handle = username
player.avatar = elliot2.bmp player.avatar = elliot2.bmp
+19 -1
View File
@@ -1 +1,19 @@
print("Lua script check OK") print("Lua script check OK (./rsc)")
function Region.Create(r)
print("Region:Create(r", Region.GetX(r), Region.GetY(r), ")")
end
function Region.Unload(r)
print("Region:Unload(r", Region.GetX(r), Region.GetY(r), ")")
end
--return true if file loaded, otherwise return false
function Region.Load(r, saveDir)
print("Region:Load(r,", saveDir, Region.GetX(r), Region.GetY(r), ")")
return false
end
function Region.Save(r, saveDir)
print("Region:Save(r,", saveDir, Region.GetX(r), Region.GetY(r), ")")
end
+1 -1
View File
@@ -1,5 +1,5 @@
#config #config
INCLUDES+=. ../common ../common/map ../common/network INCLUDES+=. ../common ../common/map ../common/script ../common/network
LIBS+=../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua -lsqlite3 LIBS+=../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua -lsqlite3
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
+32 -14
View File
@@ -66,7 +66,7 @@ void ServerApplication::Init(int argc, char** argv) {
if (SDLNet_Init()) { if (SDLNet_Init()) {
throw(runtime_error("Failed to initialize SDL_net")); throw(runtime_error("Failed to initialize SDL_net"));
} }
network.Open(config.Int("server.port"), sizeof(NetworkPacket)); network.Open(config.Int("server.port"), PACKET_BUFFER_SIZE);
cout << "Initialized SDL_net" << endl; cout << "Initialized SDL_net" << endl;
//Init SQL //Init SQL
@@ -96,8 +96,25 @@ void ServerApplication::Init(int argc, char** argv) {
} }
cout << "Initialized lua's setup script" << endl; cout << "Initialized lua's setup script" << endl;
//setup the map object
mapPager.SetRegionWidth(REGION_WIDTH);
mapPager.SetRegionHeight(REGION_HEIGHT);
mapPager.SetRegionDepth(REGION_DEPTH);
mapPager.GetGenerator()->SetLuaState(luaState);
mapPager.GetFormat()->SetLuaState(luaState);
mapPager.GetFormat()->SetSaveDir("save/mapname/");
//TODO: pass args to the generator & format as needed
//NOTE: I might need to rearrange the init process so that lua & SQL can interact
// with the map system as needed.
cout << "Initialized the map system" << endl;
cout << "\tsizeof(NetworkPacket): " << sizeof(NetworkPacket) << endl;
cout << "\tPACKET_BUFFER_SIZE: " << PACKET_BUFFER_SIZE << endl;
//finalize the startup //finalize the startup
cout << "Startup completed successfully" << endl; cout << "Startup completed successfully" << endl;
//debugging
//
} }
void ServerApplication::Loop() { void ServerApplication::Loop() {
@@ -120,6 +137,7 @@ void ServerApplication::Loop() {
void ServerApplication::Quit() { void ServerApplication::Quit() {
cout << "Shutting down" << endl; cout << "Shutting down" << endl;
//empty the members //empty the members
mapPager.UnloadAll();
//TODO: player manager //TODO: player manager
//TODO: client manager //TODO: client manager
@@ -179,9 +197,9 @@ void ServerApplication::HandleBroadcastRequest(NetworkPacket packet) {
//TODO: version info //TODO: version info
snprintf(packet.serverInfo.name, PACKET_STRING_SIZE, "%s", config["server.name"].c_str()); snprintf(packet.serverInfo.name, PACKET_STRING_SIZE, "%s", config["server.name"].c_str());
//TODO: player count //TODO: player count
char buffer[sizeof(NetworkPacket)]; char buffer[PACKET_BUFFER_SIZE];
serialize(&packet, buffer); serialize(&packet, buffer);
network.Send(&packet.meta.srcAddress, buffer, sizeof(NetworkPacket)); network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE);
} }
void ServerApplication::HandleJoinRequest(NetworkPacket packet) { void ServerApplication::HandleJoinRequest(NetworkPacket packet) {
@@ -191,25 +209,25 @@ void ServerApplication::HandleJoinRequest(NetworkPacket packet) {
clientMap[clientCounter] = c; clientMap[clientCounter] = c;
//send the client their info //send the client their info
char buffer[sizeof(NetworkPacket)]; char buffer[PACKET_BUFFER_SIZE];
packet.meta.type = NetworkPacket::Type::JOIN_RESPONSE; packet.meta.type = NetworkPacket::Type::JOIN_RESPONSE;
packet.clientInfo.index = clientCounter; packet.clientInfo.index = clientCounter;
serialize(&packet, buffer); serialize(&packet, buffer);
network.Send(&clientMap[clientCounter].address, buffer, sizeof(NetworkPacket)); network.Send(&clientMap[clientCounter].address, buffer, PACKET_BUFFER_SIZE);
//finished this routine //finished this routine
clientCounter++; clientCounter++;
cout << "connect, total: " << clientMap.size() << endl; cout << "Connect, total: " << clientMap.size() << endl;
} }
void ServerApplication::HandleDisconnect(NetworkPacket packet) { void ServerApplication::HandleDisconnect(NetworkPacket packet) {
//disconnect the specified client //disconnect the specified client
//TODO: authenticate who is disconnecting/kicking //TODO: authenticate who is disconnecting/kicking
char buffer[sizeof(NetworkPacket)]; char buffer[PACKET_BUFFER_SIZE];
serialize(&packet, buffer); serialize(&packet, buffer);
network.Send(&clientMap[packet.clientInfo.index].address, buffer, sizeof(NetworkPacket)); network.Send(&clientMap[packet.clientInfo.index].address, buffer, PACKET_BUFFER_SIZE);
clientMap.erase(packet.clientInfo.index); clientMap.erase(packet.clientInfo.index);
//delete players from all clients //delete players from all clients
@@ -228,14 +246,14 @@ void ServerApplication::HandleDisconnect(NetworkPacket packet) {
}); });
//finished this routine //finished this routine
cout << "disconnect, total: " << clientMap.size() << endl; cout << "Disconnect, total: " << clientMap.size() << endl;
} }
void ServerApplication::HandleSynchronize(NetworkPacket packet) { void ServerApplication::HandleSynchronize(NetworkPacket packet) {
//send all the server's data to this client //send all the server's data to this client
//TODO: compensate for large distances //TODO: compensate for large distances
NetworkPacket newPacket; NetworkPacket newPacket;
char buffer[sizeof(NetworkPacket)]; char buffer[PACKET_BUFFER_SIZE];
//players //players
newPacket.meta.type = NetworkPacket::Type::PLAYER_UPDATE; newPacket.meta.type = NetworkPacket::Type::PLAYER_UPDATE;
@@ -246,7 +264,7 @@ void ServerApplication::HandleSynchronize(NetworkPacket packet) {
newPacket.playerInfo.position = it.second.position; newPacket.playerInfo.position = it.second.position;
newPacket.playerInfo.motion = it.second.motion; newPacket.playerInfo.motion = it.second.motion;
serialize(&newPacket, buffer); serialize(&newPacket, buffer);
network.Send(&clientMap[packet.clientInfo.index].address, buffer, sizeof(NetworkPacket)); network.Send(&clientMap[packet.clientInfo.index].address, buffer, PACKET_BUFFER_SIZE);
} }
} }
@@ -259,7 +277,7 @@ void ServerApplication::HandleShutdown(NetworkPacket packet) {
PumpPacket(packet); PumpPacket(packet);
//finished this routine //finished this routine
cout << "shutting down" << endl; cout << "Shutdown signal accepted" << endl;
} }
void ServerApplication::HandlePlayerNew(NetworkPacket packet) { void ServerApplication::HandlePlayerNew(NetworkPacket packet) {
@@ -324,9 +342,9 @@ void ServerApplication::HandlePlayerUpdate(NetworkPacket packet) {
void ServerApplication::PumpPacket(NetworkPacket packet) { void ServerApplication::PumpPacket(NetworkPacket packet) {
//I don't really like this, but it'll do for now //I don't really like this, but it'll do for now
char buffer[sizeof(NetworkPacket)]; char buffer[PACKET_BUFFER_SIZE];
serialize(&packet, buffer); serialize(&packet, buffer);
for (auto& it : clientMap) { for (auto& it : clientMap) {
network.Send(&it.second.address, buffer, sizeof(NetworkPacket)); network.Send(&it.second.address, buffer, PACKET_BUFFER_SIZE);
} }
} }
+12 -4
View File
@@ -22,20 +22,25 @@
#ifndef SERVERAPPLICATION_HPP_ #ifndef SERVERAPPLICATION_HPP_
#define SERVERAPPLICATION_HPP_ #define SERVERAPPLICATION_HPP_
//maps
#include "map_generator.hpp"
#include "map_file_format.hpp"
#include "region_pager.hpp"
//networking //networking
#include "network_packet.hpp" #include "network_packet.hpp"
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
#include "serial.hpp" #include "serial.hpp"
//common
#include "config_utility.hpp"
#include "vector2.hpp"
//APIs //APIs
#include "lua/lua.hpp" #include "lua/lua.hpp"
#include "sqlite3/sqlite3.h" #include "sqlite3/sqlite3.h"
#include "SDL/SDL.h" #include "SDL/SDL.h"
//common
#include "config_utility.hpp"
#include "vector2.hpp"
//STL //STL
#include <map> #include <map>
#include <string> #include <string>
@@ -79,6 +84,9 @@ private:
void PumpPacket(NetworkPacket); void PumpPacket(NetworkPacket);
//maps
RegionPager<LuaGenerator, LuaFormat> mapPager;
//networking //networking
UDPNetworkUtility network; UDPNetworkUtility network;