Merge branch 'develop'

This commit is contained in:
Kayne Ruse
2014-09-30 23:31:50 +10:00
10 changed files with 89 additions and 61 deletions
+1 -1
View File
@@ -71,7 +71,7 @@ InWorld::InWorld(
//load the tilesheet
//TODO: add the tilesheet to the map system?
//TODO: Tile size and tile sheet should be loaded elsewhere
tileSheet.Load(config["dir.tilesets"] + "terrain.bmp", 32, 32);
tileSheet.Load(config["dir.tilesets"] + "overworld.bmp", 32, 32);
//send this player's character info
CharacterPacket newPacket;
Binary file not shown.

After

Width:  |  Height:  |  Size: 768 KiB

+37
View File
@@ -0,0 +1,37 @@
local mapMaker = {}
--utility functions
function mapMaker.sqr(x) return x*x end
function mapMaker.dist(x, y, i, j) return math.sqrt(mapMaker.sqr(x - i) + mapMaker.sqr(y - j)) end
--tile macros, mapped to the tilesheet "overworld.bmp"
mapMaker.edges = {}
mapMaker.edges.north = -16
mapMaker.edges.south = 16
mapMaker.edges.east = 1
mapMaker.edges.west = -1
mapMaker.water = 18 + 3 * 0
mapMaker.sand = 18 + 3 * 1
mapMaker.plains = 18 + 3 * 2
mapMaker.grass = 18 + 3 * 3
mapMaker.dirt = 18 + 3 * 4
--custom generation systems here
function mapMaker.debugIsland(region)
for i = 1, Region.GetWidth(region) do
for j = 1, Region.GetHeight(region) do
local dist = mapMaker.dist(0, 0, i + Region.GetX(region) -1, j + Region.GetY(region) -1)
if dist < 10 then
Region.SetTile(region, i, j, 1, mapMaker.plains)
elseif dist < 12 then
Region.SetTile(region, i, j, 1, mapMaker.sand)
else
Region.SetTile(region, i, j, 1, mapMaker.water)
Region.SetSolid(region, i, j, true)
end
end
end
end
return mapMaker
View File
View File
+4 -44
View File
@@ -1,52 +1,12 @@
print("Lua script check")
--uber lazy declarations
function math.sqr(x) return x*x end
function math.dist(x, y, i, j) return math.sqrt(math.sqr(x - i) + math.sqr(y - j)) end
mapMaker = require "map_maker"
mapSaver = require "map_saver"
--tile macros, mapped to the tilesheet
local base = 14
local shift = 36
tiles = {
plains = base + shift * 0,
grass = base + shift * 1,
dirt = base + shift * 2,
sand = base + shift * 3,
water = base + shift * 4
}
--custom generation systems here
function islandGenerator(region)
-- io.write("Generating (", Region.GetX(region), ", ", Region.GetY(region), ")\n")
for i = 1, Region.GetWidth(region) do
for j = 1, Region.GetHeight(region) do
local dist = math.dist(0, 0, i + Region.GetX(region) -1, j + Region.GetY(region) -1)
if dist < 10 then
Region.SetTile(region, i, j, 1, tiles.plains)
elseif dist < 12 then
Region.SetTile(region, i, j, 1, tiles.sand)
else
Region.SetTile(region, i, j, 1, tiles.water)
Region.SetSolid(region, i, j, true)
end
end
end
end
--Get some regions
--BUG: #35 The server fails without at least one room
--TODO: Create rooms with names?
newRoom = RoomManager.CreateRoom()
newRoom = RoomManager.CreateRoom("overworld", "overworld.bmp")
pager = Room.GetPager(newRoom)
RegionPager.SetOnCreate(pager, islandGenerator)
--[[
regionTable = {
RegionPager.GetRegion(pager, Region.GetWidth() * 0, Region.GetHeight() * 0),
RegionPager.GetRegion(pager, Region.GetWidth() *-1, Region.GetHeight() * 0),
RegionPager.GetRegion(pager, Region.GetWidth() * 0, Region.GetHeight() *-1),
RegionPager.GetRegion(pager, Region.GetWidth() *-1, Region.GetHeight() *-1)
}
]]
RegionPager.SetOnCreate(pager, mapMaker.debugIsland)
print("Finished the lua script")
+6
View File
@@ -25,6 +25,8 @@
//map system
#include "region_pager_lua.hpp"
#include <string>
class RoomData {
public:
RoomData() = default;
@@ -33,11 +35,15 @@ public:
//accessors and mutators
RegionPagerLua* GetPager() { return &pager; }
std::string SetTilesetName(std::string s) { return tilesetName = s; }
std::string GetTilesetName() { return tilesetName; }
private:
friend class RoomManager;
//members
RegionPagerLua pager;
std::string tilesetName;
};
#endif
+1
View File
@@ -80,6 +80,7 @@ private:
void HandleSynchronize(ClientPacket* const);
//utility methods
void CheckClientConnections();
//TODO: a function that only sends to characters in a certain proximity
void CleanupLostConnection(int index);
void PumpPacket(SerialPacket* const);
+21 -16
View File
@@ -28,6 +28,7 @@
#include <stdexcept>
#include <chrono>
#include <iostream>
#include <sstream>
#include <string>
//-------------------------
@@ -71,8 +72,27 @@ void ServerApplication::Init(int argc, char* argv[]) {
throw(std::runtime_error("Failed to initialize lua"));
}
luaL_openlibs(luaState);
std::cout << "Initialized lua" << std::endl;
//append config["dir.scripts"] to the module path
if (config["dir.scripts"].size() > 0) {
//get the original path
lua_getglobal(luaState, "package");
lua_getfield(luaState, -1, "path");
//build & push the message
std::ostringstream path;
path << lua_tostring(luaState, -1) << ";" << config["dir.scripts"] << "?.lua";
lua_pushstring(luaState, path.str().c_str());
//set the new path and clean up the stack
lua_setfield(luaState, -3, "path");
lua_pop(luaState, 2);
std::cout << "\tLua script directory appended" << std::endl;
}
//-------------------------
//Setup the objects
//-------------------------
@@ -148,23 +168,8 @@ void ServerApplication::Proc() {
//update the internals
//...
//TODO: This could be checked only every few seconds
//Check connections
for (auto& it : clientMap) {
if (std::chrono::steady_clock::now() - it.second.GetLastBeat() > std::chrono::seconds(3)) {
ServerPacket newPacket;
newPacket.type = SerialPacketType::PING;
network.SendTo(it.second.GetAddress(), &newPacket);
it.second.IncrementAttempts();
}
if (it.second.GetAttempts() > 2) {
CleanupLostConnection(it.first);
//all iterators are invalid, so we can't continue
break;
}
}
CheckClientConnections();
//give the computer a break
SDL_Delay(10);
+19
View File
@@ -21,6 +21,7 @@
*/
#include "server_application.hpp"
#include <chrono>
#include <iostream>
//-------------------------
@@ -159,6 +160,7 @@ void ServerApplication::HandleRegionRequest(RegionPacket* const argPacket) {
newPacket.x = argPacket->x;
newPacket.y = argPacket->y;
//BUG: possibly related to #35
newPacket.region = roomMgr.GetRoom(argPacket->roomIndex)->GetPager()->GetRegion(argPacket->x, argPacket->y);
//send the content
@@ -280,6 +282,23 @@ void ServerApplication::HandleSynchronize(ClientPacket* const argPacket) {
//utility methods
//-------------------------
void ServerApplication::CheckClientConnections() {
for (auto& it : clientMap) {
if (std::chrono::steady_clock::now() - it.second.GetLastBeat() > std::chrono::seconds(3)) {
ServerPacket newPacket;
newPacket.type = SerialPacketType::PING;
network.SendTo(it.second.GetAddress(), &newPacket);
it.second.IncrementAttempts();
}
if (it.second.GetAttempts() > 2) {
CleanupLostConnection(it.first);
//all iterators are invalid, so we can't continue
break;
}
}
}
void ServerApplication::CleanupLostConnection(int clientIndex) {
//NOTE: This assumes each player has only one account and character at a time
//TODO: handle multiple characters (bots, etc.)