Skip to content

Commit

Permalink
[MRG] Overhaul Atmos's locales creation logic
Browse files Browse the repository at this point in the history
Implement Atmos locales as graphs using Lemon library (https://lemon.cs.elte.hu/).

Locales are dynamically created and updated on building/demolishing and airtightness changes like window rotating.

pull request #75
  • Loading branch information
Insineer authored Dec 3, 2019
2 parents a87b809 + 08dbe24 commit a4f1ae0
Show file tree
Hide file tree
Showing 30 changed files with 528 additions and 192 deletions.
1 change: 1 addition & 0 deletions CI/appveyor/appveyor-debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ install:

- vcpkg install python3:x64-windows-static
- vcpkg install sfml:x64-windows-static
- vcpkg install liblemon:x64-windows-static
- vcpkg install gtest:x64-windows-static

cache: c:\tools\vcpkg\installed
1 change: 1 addition & 0 deletions CI/appveyor/appveyor-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ install:

- vcpkg install "python3:%platform%-windows"
- vcpkg install "python3:%platform%-windows-static"
- vcpkg install "liblemon:&platform%-windows-static"
- vcpkg install "sfml:%platform%-windows-static"
- vcpkg install "gtest:%platform%-windows-static"

Expand Down
10 changes: 10 additions & 0 deletions CI/travis/install_dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ if [ ! -d /home/travis/vcpkg/.git ]; then
./vcpkg integrate install
./vcpkg install gtest
fi

cd ~
wget http://lemon.cs.elte.hu/pub/sources/lemon-1.3.1.tar.gz
tar zxf lemon-1.3.1.tar.gz
cd lemon-1.3.1
mkdir build
cd build
cmake ..
make
sudo make install
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.6)
project(OSS13)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -pedantic")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -pedantic -Wno-unknown-pragmas")

include_directories(External/plog/include)
include_directories(External/sfml-imgui)
Expand Down
3 changes: 2 additions & 1 deletion GameLogic/Objects/Turfs/Window.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from Engine.Geometry import Direction, DirectionSet
from Engine.Geometry import Direction, DirectionSet, DirectionSetFractional
from Engine.World import Object

from Objects.Turf import Turf
Expand All @@ -12,3 +12,4 @@ def __init__(self):
super().__init__()
self.layer = 80
self.solidity = DirectionSet([Direction.SOUTH])
self.airtightness = DirectionSetFractional([(Direction.SOUTH, 0)])
4 changes: 4 additions & 0 deletions OSS13 Server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ find_package(SFML REQUIRED system window graphics network) #audio

target_compile_options(${EXECUTABLE_NAME} PRIVATE -fvisibility=hidden)
target_link_libraries(${EXECUTABLE_NAME} sfml-system sfml-window sfml-graphics sfml-network)

find_package(LEMON REQUIRED)
include_directories(${LEMON_INCLUDE_DIRS})
target_link_libraries(${EXECUTABLE_NAME} ${LEMON_LIBRARIES})
171 changes: 171 additions & 0 deletions OSS13 Server/Include/Algorithms/Graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#pragma once

#include <map>
#include <vector>

#pragma warning(push, 0)
#include <lemon/list_graph.h>
#include <lemon/maps.h>
#include <lemon/connectivity.h>
#pragma warning(pop)

template<typename LogicNode>
class Graph : public lemon::ListGraph
{
public:
Graph() :
rawNodesToLogic(*this),
edgeToConnection(*this)
{ }

Graph(Graph &&) = default;
Graph &operator=(Graph &&) = default;

class Connection {
public:
Connection() = default;
Connection(LogicNode first, LogicNode second) {
if (first > second)
std::swap(first, second);
logicNodes = { first, second };
}

const std::array<LogicNode, 2> &GetTiles() const {
return logicNodes;
}

bool operator<(const Connection &other) const {
if (logicNodes[0] < other.logicNodes[0])
return true;
if (logicNodes[0] == other.logicNodes[0] && logicNodes[1] < other.logicNodes[1])
return true;
return false;
}

private:
std::array<LogicNode, 2> logicNodes;
};

void AddNode(LogicNode node) {
auto rawNode = ListGraph::addNode();

rawNodesToLogic[rawNode] = node;
logicNodesToRaw[node] = rawNode;
}

void AddConnection(LogicNode first, LogicNode second) {
auto edge = ListGraph::addEdge(logicNodesToRaw[first], logicNodesToRaw[second]);
auto connection = Connection(first, second);

edgeToConnection[edge] = connection;
connectionToEdge[connection] = edge;
}

void CopyIn(const Graph &other) {
for (auto &[logicNode, rawNode] : other.logicNodesToRaw)
AddNode(logicNode);

for (auto &[connection, edge] : other.connectionToEdge)
AddConnection(connection.GetTiles()[0], connection.GetTiles()[1]);
}

auto GetConnectedComponents() const {
ListGraph::NodeMap<int> componentsMap(*this);
auto componentsCount = lemon::connectedComponents(*this, componentsMap);

std::vector<std::unique_ptr<Graph<LogicNode>>> components;
for (int i = 0; i < componentsCount; i++)
components.push_back(std::make_unique<Graph<LogicNode>>());

for (auto &[logicNode, rawNode] : logicNodesToRaw) {
auto index = componentsMap[rawNode];
components[index]->AddNode(logicNode);
}

for (auto &[connection, edge] : connectionToEdge) {
lemon::ListGraph::Node rawNode = logicNodesToRaw.at(connection.GetTiles()[0]);
auto index = componentsMap[rawNode];
components[index]->AddConnection(connection.GetTiles()[0], connection.GetTiles()[1]);
}

return components;
}

void EraseNode(LogicNode node) {
for (auto iter = connectionToEdge.begin(); iter != connectionToEdge.end();) {
auto &connection = iter->first;

if (connection.GetTiles()[0] == node || connection.GetTiles()[1] == node)
iter = connectionToEdge.erase(iter);
else
iter++;
}

ListGraph::erase(logicNodesToRaw[node]);
logicNodesToRaw.erase(node);
}

void EraseConnection(LogicNode first, LogicNode second) {
Connection connection{first, second};

ListGraph::erase(connectionToEdge[connection]);
connectionToEdge.erase(connection);
}

void Clear() {
ListGraph::clear();
logicNodesToRaw.clear();
connectionToEdge.clear();
}

bool IsContain(LogicNode node) const {
return logicNodesToRaw.find(node) != logicNodesToRaw.end();
}

size_t Count() const {
return logicNodesToRaw.size();
}

size_t CountConnections() const {
return connectionToEdge.size();
}

bool IsEmpty() const {
return !lemon::countNodes(*this);
}


class const_iterator {
public:
const_iterator(typename std::map<LogicNode, lemon::ListGraph::Node>::iterator iter)
: iter(iter)
{ }
const_iterator& operator++() { iter++; return *this; };
LogicNode operator*() const { return iter->first; }
bool operator!=(const const_iterator& it) const { return iter != it.iter; }
protected:
typename std::map<LogicNode, ListGraph::Node>::iterator iter;
};

class iterator : public const_iterator {
public:
using const_iterator::const_iterator;

iterator& operator++() { const_iterator::iter++; return *this; };
LogicNode &operator*() const { return const_cast<LogicNode &>(const_iterator::iter->first); }
bool operator!=(const iterator& it) const { return const_iterator::iter != it.iter; }
};


iterator begin() { return iterator(logicNodesToRaw.begin()); }
iterator end() { return iterator(logicNodesToRaw.end()); }
const_iterator cbegin() const { return const_iterator(logicNodesToRaw.begin()); }
const_iterator cend() const { return const_iterator(logicNodesToRaw.end()); }

private:
ListGraph::NodeMap<LogicNode> rawNodesToLogic;
std::map<LogicNode, lemon::ListGraph::Node> logicNodesToRaw;

ListGraph::EdgeMap<Connection> edgeToConnection;
std::map<Connection, ListGraph::Edge> connectionToEdge;
};
11 changes: 11 additions & 0 deletions OSS13 Server/Include/World/Subsystems/Atmos/IAtmos.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <World/ISubsystem.h>

namespace subsystem {

struct IAtmos : public ISubsystem {

};

} // namespace subsystem
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <World/ITile.h>
#include <World/Subsystems/IAtmos.h>
#include <World/Subsystems/Atmos/ILocale.h>

namespace subsystem {
namespace atmos {
Expand Down
12 changes: 12 additions & 0 deletions OSS13 Server/Include/World/Subsystems/Atmos/ILocale.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <Shared/IFaces/IFace.h>
#include <Shared/IFaces/IHasRepeatableID.h>

namespace subsystem::atmos {

struct ILocale : public IFace, public IHasRepeatableID {

};

} // namespace subsystem::atmos
22 changes: 0 additions & 22 deletions OSS13 Server/Include/World/Subsystems/IAtmos.h

This file was deleted.

17 changes: 11 additions & 6 deletions OSS13 Server/OSS13 Server.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
</Link>
<ClCompile>
<AdditionalIncludeDirectories>Include;Sources;$(SolutionDir)/SharedLibrary/Sources;$(SolutionDir)/External/plog/include;$(SolutionDir)/External/sfml-imgui;$(VcpkgRoot)/include/python3.7;$(SolutionDir)/External/pybind11/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>SFML_STATIC;HAVE_SNPRINTF;_MBCS;_USE_MATH_DEFINES;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>SFML_STATIC;HAVE_SNPRINTF;_MBCS;_USE_MATH_DEFINES;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand All @@ -96,7 +96,7 @@
</Link>
<ClCompile>
<AdditionalIncludeDirectories>Include;Sources;$(SolutionDir)/SharedLibrary/Sources;$(SolutionDir)/External/plog/include;$(SolutionDir)/External/sfml-imgui;$(VcpkgRoot)/include/python3.7;$(SolutionDir)/External/pybind11/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>SFML_STATIC;HAVE_SNPRINTF;_MBCS;_USE_MATH_DEFINES;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>SFML_STATIC;HAVE_SNPRINTF;_MBCS;_USE_MATH_DEFINES;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand All @@ -109,7 +109,7 @@
</Link>
<ClCompile>
<AdditionalIncludeDirectories>Include;Sources;$(SolutionDir)/SharedLibrary/Sources;$(SolutionDir)/External/plog/include;$(SolutionDir)/External/sfml-imgui;$(VcpkgRoot)/include/python3.7;$(SolutionDir)/External/pybind11/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>SFML_STATIC;HAVE_SNPRINTF;_MBCS;_USE_MATH_DEFINES;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>SFML_STATIC;HAVE_SNPRINTF;_MBCS;_USE_MATH_DEFINES;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -122,7 +122,7 @@
</Link>
<ClCompile>
<AdditionalIncludeDirectories>Include;Sources;$(SolutionDir)/SharedLibrary/Sources;$(SolutionDir)/External/plog/include;$(SolutionDir)/External/sfml-imgui;$(VcpkgRoot)/include/python3.7;$(SolutionDir)/External/pybind11/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>SFML_STATIC;HAVE_SNPRINTF;_MBCS;_USE_MATH_DEFINES;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>SFML_STATIC;HAVE_SNPRINTF;_MBCS;_USE_MATH_DEFINES;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down Expand Up @@ -159,6 +159,7 @@
<ClCompile Include="Sources\World\Objects\Object.cpp" />
<ClCompile Include="Sources\World\Objects\ObjectHolder.cpp" />
<ClCompile Include="Sources\World\Subsystems\Atmos\AtmosTile.cpp" />
<ClCompile Include="Sources\World\Subsystems\Atmos\Locale.cpp" />
<ClCompile Include="Sources\World\Tile.cpp" />
<ClCompile Include="Sources\World\World.cpp" />
</ItemGroup>
Expand All @@ -169,8 +170,10 @@
<ClInclude Include="Include\IVerbsHolder.h" />
<ClInclude Include="Include\World\ISubsystem.h" />
<ClInclude Include="Include\World\ITile.h" />
<ClInclude Include="Include\World\Subsystems\IAtmosTile.h" />
<ClInclude Include="Include\World\Subsystems\IAtmos.h" />
<ClInclude Include="Include\World\Subsystems\Atmos\IAtmosTile.h" />
<ClInclude Include="Include\World\Subsystems\Atmos\IAtmos.h" />
<ClInclude Include="Include\World\Subsystems\Atmos\ILocale.h" />
<ClInclude Include="Sources\World\Subsystems\Atmos\ILocaleFactory.h" />
<ClInclude Include="Sources\Chat.h" />
<ClInclude Include="Sources\ClientUI\WelcomeWindowSink.h" />
<ClInclude Include="Sources\Database\UsersDB.hpp" />
Expand Down Expand Up @@ -206,6 +209,8 @@
<ClInclude Include="Sources\World\Objects\CreateObject.h" />
<ClInclude Include="Sources\World\Objects\Object.hpp" />
<ClInclude Include="Sources\World\Objects\ObjectHolder.h" />
<ClInclude Include="Include\Algorithms\Graph.h" />
<ClInclude Include="Sources\World\Subsystems\Atmos\Locale.h" />
<ClInclude Include="Sources\World\Tile.hpp" />
<ClInclude Include="Sources\World\World.hpp" />
</ItemGroup>
Expand Down
23 changes: 19 additions & 4 deletions OSS13 Server/OSS13 Server.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@
<ClCompile Include="Sources\World\Subsystems\Atmos\AtmosTile.cpp">
<Filter>Файлы исходного кода</Filter>
</ClCompile>
<ClCompile Include="Sources\World\Subsystems\Atmos\Locale.cpp">
<Filter>Файлы исходного кода</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Sources\Database\UsersDB.hpp">
Expand Down Expand Up @@ -215,9 +218,6 @@
<ClInclude Include="Sources\Player\Player.h">
<Filter>Заголовочные файлы</Filter>
</ClInclude>
<ClInclude Include="Include\World\Subsystems\IAtmos.h">
<Filter>Заголовочные файлы</Filter>
</ClInclude>
<ClInclude Include="Sources\World\Subsystems\Atmos\AtmosCameraOverlay.h">
<Filter>Заголовочные файлы</Filter>
</ClInclude>
Expand All @@ -239,7 +239,22 @@
<ClInclude Include="Include\World\ITile.h">
<Filter>Заголовочные файлы</Filter>
</ClInclude>
<ClInclude Include="Include\World\Subsystems\IAtmosTile.h">
<ClInclude Include="Sources\World\Subsystems\Atmos\Locale.h">
<Filter>Заголовочные файлы</Filter>
</ClInclude>
<ClInclude Include="Sources\World\Subsystems\Atmos\ILocaleFactory.h">
<Filter>Заголовочные файлы</Filter>
</ClInclude>
<ClInclude Include="Include\World\Subsystems\Atmos\ILocale.h">
<Filter>Заголовочные файлы</Filter>
</ClInclude>
<ClInclude Include="Include\World\Subsystems\Atmos\IAtmos.h">
<Filter>Заголовочные файлы</Filter>
</ClInclude>
<ClInclude Include="Include\World\Subsystems\Atmos\IAtmosTile.h">
<Filter>Заголовочные файлы</Filter>
</ClInclude>
<ClInclude Include="Include\Algorithms\Graph.h">
<Filter>Заголовочные файлы</Filter>
</ClInclude>
</ItemGroup>
Expand Down
Loading

0 comments on commit a4f1ae0

Please sign in to comment.