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()) {
throw(std::runtime_error("Failed to initialize SDL_net"));
}
network.Open(0, sizeof(NetworkPacket));
network.Open(0, PACKET_BUFFER_SIZE);
}
void ClientApplication::Proc() {
+15 -15
View File
@@ -35,21 +35,21 @@ InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNet
clientIndex(*argClientIndex)
{
//setup the utility objects
image.LoadSurface(config["dir.interface"] + "button_menu.bmp");
image.SetClipH(image.GetClipH()/3);
buttonImage.LoadSurface(config["dir.interface"] + "button_menu.bmp");
buttonImage.SetClipH(buttonImage.GetClipH()/3);
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp");
//pass the utility objects
disconnectButton.SetImage(&image);
disconnectButton.SetImage(&buttonImage);
disconnectButton.SetFont(&font);
shutDownButton.SetImage(&image);
shutDownButton.SetImage(&buttonImage);
shutDownButton.SetFont(&font);
//set the button positions
disconnectButton.SetX(50);
disconnectButton.SetY(50 + image.GetClipH() * 0);
disconnectButton.SetY(50 + buttonImage.GetClipH() * 0);
shutDownButton.SetX(50);
shutDownButton.SetY(50 + image.GetClipH() * 1);
shutDownButton.SetY(50 + buttonImage.GetClipH() * 1);
//set the button texts
disconnectButton.SetText("Disconnect");
@@ -65,14 +65,14 @@ InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNet
packet.playerInfo.motion = {0,0};
//send it
char buffer[sizeof(NetworkPacket)];
char buffer[PACKET_BUFFER_SIZE];
serialize(&packet, buffer);
network.Send(Channels::SERVER, buffer, sizeof(NetworkPacket));
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
//request a sync
packet.meta.type = NetworkPacket::Type::SYNCHRONIZE;
serialize(&packet, buffer);
network.Send(Channels::SERVER, buffer, sizeof(NetworkPacket));
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
}
InWorld::~InWorld() {
@@ -290,7 +290,7 @@ void InWorld::HandlePlayerUpdate(NetworkPacket packet) {
void InWorld::SendState() {
NetworkPacket packet;
char buffer[sizeof(NetworkPacket)];
char buffer[PACKET_BUFFER_SIZE];
//pack the packet
packet.meta.type = NetworkPacket::Type::PLAYER_UPDATE;
@@ -302,27 +302,27 @@ void InWorld::SendState() {
packet.playerInfo.motion = localCharacter->GetMotion();
serialize(&packet, buffer);
network.Send(Channels::SERVER, buffer, sizeof(NetworkPacket));
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
}
void InWorld::RequestDisconnect() {
NetworkPacket packet;
char buffer[sizeof(NetworkPacket)];
char buffer[PACKET_BUFFER_SIZE];
//send a disconnect request
packet.meta.type = NetworkPacket::Type::DISCONNECT;
packet.clientInfo.index = clientIndex;
serialize(&packet, buffer);
network.Send(Channels::SERVER, buffer, sizeof(NetworkPacket));
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
}
void InWorld::RequestShutDown() {
NetworkPacket packet;
char buffer[sizeof(NetworkPacket)];
char buffer[PACKET_BUFFER_SIZE];
//send a shutdown request
packet.meta.type = NetworkPacket::Type::SHUTDOWN;
packet.clientInfo.index = clientIndex;
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_
#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 "network_packet.hpp"
#include "serial.hpp"
//graphics
#include "image.hpp"
#include "raster_font.hpp"
#include "button.hpp"
//common
#include "config_utility.hpp"
//client
#include "base_scene.hpp"
#include "player_character.hpp"
//STL
#include <map>
class InWorld : public BaseScene {
@@ -66,16 +78,26 @@ protected:
void RequestDisconnect();
void RequestShutDown();
//global
//globals
ConfigUtility& config;
UDPNetworkUtility& network;
int& clientIndex;
//members
Image image;
//graphics
Image buttonImage;
RasterFont font;
//map
RegionPager<BlankGenerator, DummyFormat> mapPager;
//UI
Button disconnectButton;
Button shutDownButton;
struct {
int x = 0, y = 0;
} camera;
//game
std::map<int, PlayerCharacter> playerCharacters;
PlayerCharacter* localCharacter = nullptr;
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) {
//the vars
NetworkPacket packet;
char buffer[sizeof(NetworkPacket)];
char buffer[PACKET_BUFFER_SIZE];
//broadcast to the network, or a specific server
packet.meta.type = NetworkPacket::Type::BROADCAST_REQUEST;
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
serverInfo.clear();
@@ -142,12 +142,12 @@ void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr) {
//the vars
NetworkPacket packet;
char buffer[sizeof(NetworkPacket)];
char buffer[PACKET_BUFFER_SIZE];
//join the selected server
packet.meta.type = NetworkPacket::Type::JOIN_REQUEST;
serialize(&packet, buffer);
network.Send(&selection->address, buffer, sizeof(NetworkPacket));
network.Send(&selection->address, buffer, PACKET_BUFFER_SIZE);
selection = nullptr;
}
+1
View File
@@ -22,6 +22,7 @@ all: $(OBJ) $(OUT)
ar -crs $(OUT) $(OBJ)
$(MAKE) -C graphics
$(MAKE) -C map
$(MAKE) -C script
$(MAKE) -C network
$(MAKE) -C ui
+51 -2
View File
@@ -21,10 +21,59 @@
*/
#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
}
void MapFileFormat::Save(Region* const ptr) {
void DummyFormat::Save(Region* const ptr) {
//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"
class MapFileFormat {
#include "lua/lua.hpp"
#include <string>
class DummyFormat {
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);
std::string SetSaveDir(std::string s) { return saveDir = s; }
std::string GetSaveDir() { return saveDir; }
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
+40 -2
View File
@@ -21,10 +21,48 @@
*/
#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);
}
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;
}
+22 -1
View File
@@ -24,12 +24,33 @@
#include "region.hpp"
class MapGenerator {
#include "lua/lua.hpp"
class BlankGenerator {
public:
void Create(Region** const, int width, int height, int depth, int x, int y);
void Unload(Region* const);
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
+10 -10
View File
@@ -28,12 +28,12 @@ Region::Region(int argWidth, int argHeight, int argDepth, int argX, int argY):
x(argX),
y(argY)
{
tiles = new int**[width];
for (int i = 0; i < width; ++i) {
tiles[i] = new int*[height];
for (int j = 0; j < height; ++j) {
tiles[i][j] = new int[depth];
for (int k = 0; k < depth; ++k) {
tiles = new type_t**[width];
for (register int i = 0; i < width; ++i) {
tiles[i] = new type_t*[height];
for (register int j = 0; j < height; ++j) {
tiles[i][j] = new type_t[depth];
for (register int k = 0; k < depth; ++k) {
tiles[i][j][k] = 0;
}
}
@@ -41,8 +41,8 @@ Region::Region(int argWidth, int argHeight, int argDepth, int argX, int argY):
}
Region::~Region() {
for (int i = 0; i < width; ++i) {
for (int j = 0; j < height; j++) {
for (register int i = 0; i < width; ++i) {
for (register int j = 0; j < height; j++) {
delete tiles[i][j];
}
delete tiles[i];
@@ -50,10 +50,10 @@ Region::~Region() {
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;
}
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];
}
+15 -8
View File
@@ -22,21 +22,28 @@
#ifndef REGION_HPP_
#define REGION_HPP_
//temporary?
#define REGION_WIDTH 20
#define REGION_HEIGHT 20
#define REGION_DEPTH 3
class Region {
public:
typedef unsigned short type_t;
Region() = delete;
Region(int width, int height, int depth, int x, int y);
~Region();
int SetTile(int x, int y, int z, int v);
int GetTile(int x, int y, int z);
type_t SetTile(int x, int y, int z, type_t v);
type_t GetTile(int x, int y, int z);
//accessors
int GetWidth() { return width; }
int GetHeight() { return height; }
int GetDepth() { return depth; }
int GetX() { return x; }
int GetY() { return y; }
int GetWidth() const { return width; }
int GetHeight() const { return height; }
int GetDepth() const { return depth; }
int GetX() const { return x; }
int GetY() const { return y; }
private:
const int width;
const int height;
@@ -44,7 +51,7 @@ private:
const int x;
const int y;
int*** tiles = nullptr;
type_t*** tiles = nullptr;
};
#endif
+2 -6
View File
@@ -35,12 +35,12 @@ RegionPagerBase::~RegionPagerBase() {
//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);
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);
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;
return CreateRegion(x, y);
}
void RegionPagerBase::Update() {
//TODO
}
+25 -14
View File
@@ -29,14 +29,12 @@
class RegionPagerBase {
public:
RegionPagerBase() = delete;
RegionPagerBase() = default;
RegionPagerBase(int regionWidth, int regionHeight, int regionDepth);
virtual ~RegionPagerBase();
int SetTile(int x, int y, int z, int v);
int GetTile(int x, int y, int z);
void Update();
Region::type_t SetTile(int x, int y, int z, Region::type_t v);
Region::type_t GetTile(int x, int y, int z);
Region* GetRegion(int x, int y);
@@ -47,26 +45,33 @@ public:
virtual void UnloadRegion(int x, int y) = 0;
//accessors
int GetRegionWidth() { return regionWidth; }
int GetRegionHeight() { return regionHeight; }
int GetRegionDepth() { return regionDepth; }
//NOTE: don't change the sizes mid-program, it will cause issues
int SetRegionWidth(int i) { return regionWidth = i; }
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:
const int regionWidth;
const int regionHeight;
const int regionDepth;
int regionWidth;
int regionHeight;
int regionDepth;
std::list<Region*> regionList;
};
template<typename MapGenerator, typename MapFileFormat>
class RegionPager : public RegionPagerBase {
public:
RegionPager() = delete;
RegionPager() = default;
RegionPager(int w, int h, int d):
RegionPagerBase(w, h, d)
{
//EMPTY
}
~RegionPager() = default;
~RegionPager() {
UnloadAll();
}
Region* LoadRegion(int x, int y) {
//snap the coords
@@ -75,7 +80,7 @@ public:
//load the region if possible
Region* ptr = nullptr;
format.Load(&ptr, x, y);
format.Load(&ptr, regionWidth, regionHeight, regionDepth, x, y);
if (ptr) {
regionList.push_back(ptr);
return ptr;
@@ -127,6 +132,12 @@ public:
++it;
}
}
void UnloadAll() {
for (auto& it : regionList) {
generator.Unload(it);
}
regionList.clear();
}
//accessors
MapGenerator* GetGenerator() { return &generator; }
+1 -1
View File
@@ -1,5 +1,5 @@
#config
INCLUDES+=. ..
INCLUDES+=. .. ../map
LIBS+=
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
+13 -3
View File
@@ -22,9 +22,10 @@
#ifndef NETWORKPACKET_HPP_
#define NETWORKPACKET_HPP_
#include "SDL/SDL_net.h"
#include "vector2.hpp"
#include "region.hpp"
#include "SDL/SDL_net.h"
#define PACKET_STRING_SIZE 100
@@ -61,6 +62,10 @@ union NetworkPacket {
PLAYER_NEW = 10,
PLAYER_DELETE = 11,
PLAYER_UPDATE = 12,
//map data
REGION_REQUEST = 13,
REGION_CONTENT = 14,
};
//metadata on the packet itself
@@ -75,6 +80,7 @@ union NetworkPacket {
//TODO: version info
char name[PACKET_STRING_SIZE];
//TODO: player count
//TODO: map format
}serverInfo;
//information about the client
@@ -95,7 +101,11 @@ union NetworkPacket {
}playerInfo;
//map data
//...
struct RegionInformation {
Metadata meta;
int width, height, depth, x, y;
Region* region;
}regionInfo;
//defaults
NetworkPacket() {
+112 -20
View File
@@ -21,36 +21,31 @@
*/
#include "serial.hpp"
#include <cstring>
//#include <iostream>
#include "map_generator.hpp"
//using namespace std;
#include <cstring>
//-------------------------
//internal serialization functions
//-------------------------
void serializeType(NetworkPacket* packet, char* buffer) {
// cout << "serializeType" << endl;
memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type));
}
void serializeServer(NetworkPacket* packet, char* buffer) {
// cout << "serializeServer" << endl;
memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type));
buffer += sizeof(NetworkPacket::Type);
memcpy(buffer, packet->serverInfo.name, PACKET_STRING_SIZE);
}
void serializeClient(NetworkPacket* packet, char* buffer) {
// cout << "serializeClient" << endl;
memcpy(buffer, &packet->meta.type, sizeof(NetworkPacket::Type));
buffer += sizeof(NetworkPacket::Type);
memcpy(buffer, &packet->clientInfo.index, sizeof(int));
}
void serializePlayer(NetworkPacket* packet, char* buffer) {
// cout << "serializePlayer" << endl;
memcpy(buffer, &packet->meta.type, 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));
}
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
//-------------------------
void deserializeType(NetworkPacket* packet, char* buffer) {
// cout << "deserializeType" << endl;
memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type));
}
void deserializeServer(NetworkPacket* packet, char* buffer) {
// cout << "deserializeServer" << endl;
memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type));
buffer += sizeof(NetworkPacket::Type);
memcpy(packet->serverInfo.name, buffer, PACKET_STRING_SIZE);
}
void deserializeClient(NetworkPacket* packet, char* buffer) {
// cout << "deserializeClient" << endl;
memcpy(&packet->meta.type, buffer, sizeof(NetworkPacket::Type));
buffer += sizeof(NetworkPacket::Type);
memcpy(&packet->clientInfo.index, buffer, sizeof(int));
}
void deserializePlayer(NetworkPacket* packet, char* buffer) {
// cout << "deserializePlayer" << endl;
memcpy(&packet->meta.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));
}
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
//-------------------------
@@ -160,11 +242,16 @@ void serialize(NetworkPacket* packet, void* buffer) {
case NetworkPacket::Type::PLAYER_UPDATE:
serializePlayer(packet, reinterpret_cast<char*>(buffer));
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) {
@@ -178,7 +265,7 @@ void deserialize(NetworkPacket* packet, void* buffer) {
case NetworkPacket::Type::BROADCAST_REQUEST:
case NetworkPacket::Type::JOIN_REQUEST:
case NetworkPacket::Type::SYNCHRONIZE:
//
//NOTHING
break;
//Server info
@@ -199,9 +286,14 @@ void deserialize(NetworkPacket* packet, void* buffer) {
case NetworkPacket::Type::PLAYER_UPDATE:
deserializePlayer(packet, reinterpret_cast<char*>(buffer));
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"
/* 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 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):
config(*arg1),
pager(20, 20, 3)
config(*arg1)
{
//create the debugging "window"
debugInfo.CreateSurface(256, 256);
@@ -56,8 +55,13 @@ EditorScene::EditorScene(ConfigUtility* const arg1):
{"Debug", "Debug On", "Debug Off", "Toggle", "Testificate"}
});
//setup the map
pager.SetRegionWidth(REGION_WIDTH);
pager.SetRegionHeight(REGION_HEIGHT);
pager.SetRegionDepth(REGION_DEPTH);
//debug
tsheet.Load("rsc\\graphics\\tilesets\\sand.bmp", 12, 3);
tsheet.Load(config["dir.tilesets"] + "sand.bmp", 12, 3);
}
EditorScene::~EditorScene() {
@@ -85,6 +89,7 @@ void EditorScene::Render(SDL_Surface* const screen) {
for (int i = 0; i < pager.GetRegionWidth()*2; i++) {
for (int j = 0; j < pager.GetRegionHeight()*2; j++) {
for (int k = 0; k < pager.GetRegionDepth(); k++) {
//TODO: skip the out-of-bounds regions
tsheet.DrawTo(
screen,
i*tsheet.GetTileW()-camera.x,
+1 -1
View File
@@ -73,7 +73,7 @@ protected:
int x = 0, y = 0;
} camera;
RegionPager<MapGenerator, MapFileFormat> pager;
RegionPager<BlankGenerator, DummyFormat> pager;
TileSheet tsheet;
};
+1 -1
View File
@@ -1,6 +1,6 @@
#config
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))
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
+5
View File
@@ -17,6 +17,11 @@ dir.tilesets = rsc/graphics/tilesets/
dir.interface = rsc/graphics/interface/
dir.scripts = rsc/scripts/
#map system
map.pager.width = 20
map.pager.height = 20
map.pager.depth = 3
#player options
player.handle = username
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
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
CXXFLAGS+=-std=c++11 -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()) {
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;
//Init SQL
@@ -96,8 +96,25 @@ void ServerApplication::Init(int argc, char** argv) {
}
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
cout << "Startup completed successfully" << endl;
//debugging
//
}
void ServerApplication::Loop() {
@@ -120,6 +137,7 @@ void ServerApplication::Loop() {
void ServerApplication::Quit() {
cout << "Shutting down" << endl;
//empty the members
mapPager.UnloadAll();
//TODO: player manager
//TODO: client manager
@@ -179,9 +197,9 @@ void ServerApplication::HandleBroadcastRequest(NetworkPacket packet) {
//TODO: version info
snprintf(packet.serverInfo.name, PACKET_STRING_SIZE, "%s", config["server.name"].c_str());
//TODO: player count
char buffer[sizeof(NetworkPacket)];
char buffer[PACKET_BUFFER_SIZE];
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) {
@@ -191,25 +209,25 @@ void ServerApplication::HandleJoinRequest(NetworkPacket packet) {
clientMap[clientCounter] = c;
//send the client their info
char buffer[sizeof(NetworkPacket)];
char buffer[PACKET_BUFFER_SIZE];
packet.meta.type = NetworkPacket::Type::JOIN_RESPONSE;
packet.clientInfo.index = clientCounter;
serialize(&packet, buffer);
network.Send(&clientMap[clientCounter].address, buffer, sizeof(NetworkPacket));
network.Send(&clientMap[clientCounter].address, buffer, PACKET_BUFFER_SIZE);
//finished this routine
clientCounter++;
cout << "connect, total: " << clientMap.size() << endl;
cout << "Connect, total: " << clientMap.size() << endl;
}
void ServerApplication::HandleDisconnect(NetworkPacket packet) {
//disconnect the specified client
//TODO: authenticate who is disconnecting/kicking
char buffer[sizeof(NetworkPacket)];
char buffer[PACKET_BUFFER_SIZE];
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);
//delete players from all clients
@@ -228,14 +246,14 @@ void ServerApplication::HandleDisconnect(NetworkPacket packet) {
});
//finished this routine
cout << "disconnect, total: " << clientMap.size() << endl;
cout << "Disconnect, total: " << clientMap.size() << endl;
}
void ServerApplication::HandleSynchronize(NetworkPacket packet) {
//send all the server's data to this client
//TODO: compensate for large distances
NetworkPacket newPacket;
char buffer[sizeof(NetworkPacket)];
char buffer[PACKET_BUFFER_SIZE];
//players
newPacket.meta.type = NetworkPacket::Type::PLAYER_UPDATE;
@@ -246,7 +264,7 @@ void ServerApplication::HandleSynchronize(NetworkPacket packet) {
newPacket.playerInfo.position = it.second.position;
newPacket.playerInfo.motion = it.second.motion;
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);
//finished this routine
cout << "shutting down" << endl;
cout << "Shutdown signal accepted" << endl;
}
void ServerApplication::HandlePlayerNew(NetworkPacket packet) {
@@ -324,9 +342,9 @@ void ServerApplication::HandlePlayerUpdate(NetworkPacket packet) {
void ServerApplication::PumpPacket(NetworkPacket packet) {
//I don't really like this, but it'll do for now
char buffer[sizeof(NetworkPacket)];
char buffer[PACKET_BUFFER_SIZE];
serialize(&packet, buffer);
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_
#define SERVERAPPLICATION_HPP_
//maps
#include "map_generator.hpp"
#include "map_file_format.hpp"
#include "region_pager.hpp"
//networking
#include "network_packet.hpp"
#include "udp_network_utility.hpp"
#include "serial.hpp"
//common
#include "config_utility.hpp"
#include "vector2.hpp"
//APIs
#include "lua/lua.hpp"
#include "sqlite3/sqlite3.h"
#include "SDL/SDL.h"
//common
#include "config_utility.hpp"
#include "vector2.hpp"
//STL
#include <map>
#include <string>
@@ -79,6 +84,9 @@ private:
void PumpPacket(NetworkPacket);
//maps
RegionPager<LuaGenerator, LuaFormat> mapPager;
//networking
UDPNetworkUtility network;