Skip to content

Commit

Permalink
Добавлена поддержка спавн файлов со встроенным гейм графом
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyLoaderr committed Aug 16, 2022
1 parent bcae676 commit c756706
Show file tree
Hide file tree
Showing 17 changed files with 316 additions and 115 deletions.
12 changes: 9 additions & 3 deletions ogsr_engine/COMMON_AI/GRAPH/game_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,24 @@ class CGameGraph {

private:
CHeader m_header;
IReader *m_reader;
CVertex *m_nodes;
IReader* m_reader;
CVertex* m_nodes;
bool m_separated_graphs;
mutable ENABLED m_enabled;
_GRAPH_ID m_current_level_some_vertex_id;

private:
u32* m_cross_tables;
CGameLevelCrossTable* m_current_level_cross_table;

public:

IC CGameGraph ();
IC CGameGraph (IReader* stream, bool separatedGraphs);

public:
IC virtual ~CGameGraph ();
IC const CHeader &header () const;
IC const CGameLevelCrossTable& cross_table () const;
IC bool mask (const svector<_LOCATION_ID,GameGraph::LOCATION_TYPE_COUNT> &M, const _LOCATION_ID E[GameGraph::LOCATION_TYPE_COUNT]) const;
IC bool mask (const _LOCATION_ID M[GameGraph::LOCATION_TYPE_COUNT], const _LOCATION_ID E[GameGraph::LOCATION_TYPE_COUNT]) const;
IC float distance (const _GRAPH_ID tGraphID0, const _GRAPH_ID tGraphID1) const;
Expand Down
78 changes: 68 additions & 10 deletions ogsr_engine/COMMON_AI/GRAPH/game_graph_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,36 @@

#pragma once

IC CGameGraph::CGameGraph()
IC CGameGraph::CGameGraph(IReader* stream, bool separatedGraphs)
{
string_path file_name;
FS.update_path (file_name,"$game_data$",GRAPH_NAME);

m_reader = FS.r_open(file_name);
m_reader = stream;
m_separated_graphs = separatedGraphs;
VERIFY (m_reader);
m_header.load (m_reader);
R_ASSERT2 (header().version() == XRAI_CURRENT_VERSION,"Graph version mismatch!");
m_nodes = (CVertex*)m_reader->pointer();
m_current_level_some_vertex_id = _GRAPH_ID(-1);
m_enabled.assign (header().vertex_count(),true);

if (m_separated_graphs)
{
m_cross_tables = nullptr;
m_current_level_cross_table = nullptr;
}
else
{
u8* temp = (u8*)(m_nodes + header().vertex_count());
temp += header().edge_count() * sizeof(CGameGraph::CEdge);
m_cross_tables = (u32*)(((CLevelPoint*)temp) + header().death_point_count());
m_current_level_cross_table = nullptr;
}
}

IC CGameGraph::~CGameGraph ()
IC CGameGraph::~CGameGraph()
{
FS.r_close (m_reader);
xr_delete (m_current_level_cross_table);
if (m_separated_graphs)
FS.r_close (m_reader);
}

IC const CGameGraph::CHeader &CGameGraph::header () const
Expand Down Expand Up @@ -136,11 +149,25 @@ IC const u32 &GameGraph::CHeader::death_point_count () const
return (m_death_point_count);
}


IC const GameGraph::LEVEL_MAP &GameGraph::CHeader::levels () const
{
return (m_levels);
}

IC bool GameGraph::CHeader::level_exist (const _LEVEL_ID& id) const
{
return levels().find(id) != levels().end();
}

IC bool GameGraph::CHeader::level_exist (pcstr level_name) const
{
for (const auto& levelPair : levels())
if (xr_strcmp(levelPair.second.name(), level_name) == 0)
return true;
return false;
}

IC const GameGraph::SLevel &GameGraph::CHeader::level (const _LEVEL_ID &id) const
{
LEVEL_MAP::const_iterator I = levels().find(id);
Expand Down Expand Up @@ -316,14 +343,45 @@ IC void GameGraph::CHeader::save (IWriter *writer)

IC void CGameGraph::set_current_level (const u32 &level_id)
{
xr_delete (m_current_level_cross_table);
if (m_cross_tables)
{
u32* current_cross_table = m_cross_tables;
for (const auto& levelPair : header().levels())
{
if (level_id != levelPair.first)
{
current_cross_table = (u32*)((u8*)current_cross_table + *current_cross_table);
continue;
}

m_current_level_cross_table = xr_new<CGameLevelCrossTable>(current_cross_table + 1, *current_cross_table);
break;
}
}
else
{
string_path fName;
FS.update_path (fName, "$level$", CROSS_TABLE_NAME);
m_current_level_cross_table = xr_new<CGameLevelCrossTable>(fName);
}
VERIFY (m_current_level_cross_table);

m_current_level_some_vertex_id = _GRAPH_ID(-1);
for (_GRAPH_ID i=0, n = header().vertex_count(); i<n; ++i) {
for (_GRAPH_ID i = 0, n = header().vertex_count(); i < n; ++i)
{
if (level_id != vertex(i)->level_id())
continue;

m_current_level_some_vertex_id = i;
m_current_level_some_vertex_id = i;
break;
}

VERIFY (valid_vertex_id(m_current_level_some_vertex_id));
VERIFY (valid_vertex_id(m_current_level_some_vertex_id));
}

IC const CGameLevelCrossTable& CGameGraph::cross_table () const
{
VERIFY (m_current_level_cross_table);
return (*m_current_level_cross_table);
}
2 changes: 2 additions & 0 deletions ogsr_engine/COMMON_AI/GRAPH/game_graph_space.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ namespace GameGraph {
IC const u32 &death_point_count () const;
IC const xrGUID &guid () const;
IC const LEVEL_MAP &levels () const;
IC bool level_exist (const _LEVEL_ID& id) const;
IC bool level_exist (pcstr level_name) const;
IC const SLevel &level (const _LEVEL_ID &id) const;
IC const SLevel &level (LPCSTR level_name) const;
IC const SLevel *level (LPCSTR level_name, bool) const;
Expand Down
9 changes: 5 additions & 4 deletions ogsr_engine/COMMON_AI/GRAPH/game_level_cross_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ class CGameLevelCrossTable {

private:
CHeader m_tCrossTableHeader;
CCell *m_tpaCrossTable;
CCell* m_tpaCrossTable;

private:
IReader *m_tpCrossTableVFS;
IReader *m_chunk;
IReader* m_tpCrossTableVFS;
IReader* m_chunk;
public:
IC CGameLevelCrossTable ();
IC CGameLevelCrossTable (const void* buffer, const u32& buffer_size);
IC CGameLevelCrossTable (LPCSTR fName);

public:
IC virtual ~CGameLevelCrossTable ();
Expand Down
45 changes: 26 additions & 19 deletions ogsr_engine/COMMON_AI/GRAPH/game_level_cross_table_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,41 @@

#pragma once

IC CGameLevelCrossTable::CGameLevelCrossTable ()
IC CGameLevelCrossTable::CGameLevelCrossTable(LPCSTR fName)
{
string_path fName;
FS.update_path (fName,"$level$",CROSS_TABLE_NAME);

m_tpCrossTableVFS = FS.r_open(fName);
R_ASSERT2 (m_tpCrossTableVFS,"Can't open cross table!");
IReader *chunk = m_tpCrossTableVFS->open_chunk(CROSS_TABLE_CHUNK_VERSION);
R_ASSERT2 (chunk,"Cross table is corrupted!");
chunk->r (&m_tCrossTableHeader,sizeof(m_tCrossTableHeader));
R_ASSERT2 (m_tpCrossTableVFS, "Can't open cross table!");

IReader* chunk = m_tpCrossTableVFS->open_chunk(CROSS_TABLE_CHUNK_VERSION);
R_ASSERT2 (chunk, "Cross table is corrupted!");
chunk->r (&m_tCrossTableHeader, sizeof(m_tCrossTableHeader));
chunk->close ();
R_ASSERT2 (m_tCrossTableHeader.version() == XRAI_CURRENT_VERSION,"Cross table version mismatch!");

R_ASSERT2 (m_tCrossTableHeader.version() == XRAI_CURRENT_VERSION, "Cross table version mismatch!");

m_chunk = m_tpCrossTableVFS->open_chunk(CROSS_TABLE_CHUNK_DATA);
R_ASSERT2 (m_chunk,"Cross table is corrupted!");
R_ASSERT2 (m_chunk, "Cross table is corrupted!");
m_tpaCrossTable = (CCell*)m_chunk->pointer();
};
}

IC CGameLevelCrossTable::CGameLevelCrossTable(const void* buffer, const u32& /*buffer_size*/)
{
memcpy (&m_tCrossTableHeader, buffer, sizeof(m_tCrossTableHeader));
buffer = (const u8*)buffer + sizeof(m_tCrossTableHeader);

R_ASSERT2 (m_tCrossTableHeader.version() == XRAI_CURRENT_VERSION, "Cross table version mismatch!");

IC CGameLevelCrossTable::~CGameLevelCrossTable ()
m_tpaCrossTable = (CCell*)buffer;
m_chunk = nullptr;
m_tpCrossTableVFS = nullptr;
}

IC CGameLevelCrossTable::~CGameLevelCrossTable()
{
VERIFY (m_chunk);
m_chunk->close ();

VERIFY (m_tpCrossTableVFS);
if (m_chunk)
m_chunk->close ();
FS.r_close (m_tpCrossTableVFS);
};
}

IC const CGameLevelCrossTable::CCell &CGameLevelCrossTable::vertex(u32 level_vertex_id) const
{
Expand Down
73 changes: 35 additions & 38 deletions ogsr_engine/xrGame/ai_space.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,23 @@ CAI_Space *g_ai_space = 0;

CAI_Space::CAI_Space ()
{
m_ef_storage = 0;
m_game_graph = 0;
m_graph_engine = 0;
m_cover_manager = 0;
m_level_graph = 0;
m_cross_table = 0;
m_alife_simulator = 0;
m_patrol_path_storage = 0;
m_script_engine = 0;
m_ef_storage = nullptr;
m_game_graph = nullptr;
m_graph_engine = nullptr;
m_cover_manager = nullptr;
m_level_graph = nullptr;
m_alife_simulator = nullptr;
m_patrol_path_storage = nullptr;
m_script_engine = nullptr;
}

void CAI_Space::init ()
{
VERIFY (!m_ef_storage);
m_ef_storage = xr_new<CEF_Storage>();

VERIFY (!m_game_graph);
m_game_graph = xr_new<CGameGraph>();

VERIFY (!m_graph_engine);
m_graph_engine = xr_new<CGraphEngine>(game_graph().header().vertex_count());
m_graph_engine = xr_new<CGraphEngine>(1024);

VERIFY (!m_cover_manager);
m_cover_manager = xr_new<CCoverManager>();
Expand All @@ -62,8 +58,9 @@ CAI_Space::~CAI_Space ()

xr_delete (m_patrol_path_storage);
xr_delete (m_ef_storage);
xr_delete (m_graph_engine);

xr_delete (m_game_graph);
VERIFY (!m_game_graph);

try {
xr_delete (m_script_engine);
Expand All @@ -72,11 +69,12 @@ CAI_Space::~CAI_Space ()
}

xr_delete (m_cover_manager);
xr_delete (m_graph_engine);
}

void CAI_Space::load (LPCSTR level_name)
{
VERIFY (m_game_graph);

unload (true);

#ifdef DEBUG
Expand All @@ -86,28 +84,25 @@ void CAI_Space::load (LPCSTR level_name)
timer.Start ();
#endif

const CGameGraph::SLevel &current_level = game_graph().header().level(level_name);

const CGameGraph::SLevel& currentLevel = game_graph().header().level(level_name);
m_level_graph = xr_new<CLevelGraph>();
m_cross_table = xr_new<CGameLevelCrossTable>();
R_ASSERT2 (cross_table().header().level_guid() == level_graph().header().guid(), "cross_table doesn't correspond to the AI-map");
R_ASSERT2 (cross_table().header().game_guid() == game_graph().header().guid(), "graph doesn't correspond to the cross table");
m_graph_engine = xr_new<CGraphEngine>(
_max(
game_graph().header().vertex_count(),
level_graph().header().vertex_count()
)
);

R_ASSERT2 (current_level.guid() == level_graph().header().guid(), "graph doesn't correspond to the AI-map");

game_graph().set_current_level(currentLevel.id());
auto& crossHeader = cross_table().header();
auto& levelHeader = level_graph().header();
auto& gameHeader = game_graph().header();
R_ASSERT2 (crossHeader.level_guid() == levelHeader.guid(), "cross_table doesn't correspond to the AI-map");
R_ASSERT2 (crossHeader.game_guid() == gameHeader.guid(), "graph doesn't correspond to the cross table");

u32 vertexCount = _max(gameHeader.vertex_count(), levelHeader.vertex_count());
m_graph_engine = xr_new<CGraphEngine>(vertexCount);
R_ASSERT2 (currentLevel.guid() == levelHeader.guid(), "graph doesn't correspond to the AI-map");

#ifdef DEBUG
if (!xr_strcmp(current_level.name(),level_name))
validate (current_level.id());
if (!xr_strcmp(currentLevel.name(), level_name))
validate(currentLevel.id());
#endif

level_graph().level_id (current_level.id());
game_graph().set_current_level(current_level.id());
level_graph().level_id(currentLevel.id());

m_cover_manager->compute_static_cover ();

Expand All @@ -121,10 +116,7 @@ void CAI_Space::unload (bool reload)
script_engine().unload ();
xr_delete (m_graph_engine);
xr_delete (m_level_graph);
xr_delete (m_cross_table);
if (
!reload
)
if (!reload && m_game_graph)
m_graph_engine = xr_new<CGraphEngine>(game_graph().header().vertex_count());
}

Expand Down Expand Up @@ -183,4 +175,9 @@ void CAI_Space::set_alife (CALifeSimulator *alife_simulator)
{
VERIFY ((!m_alife_simulator && alife_simulator) || (m_alife_simulator && !alife_simulator));
m_alife_simulator = alife_simulator;
}

VERIFY (!alife_simulator || !m_game_graph);
if (alife_simulator)
return;
set_game_graph (nullptr);
}
4 changes: 2 additions & 2 deletions ogsr_engine/xrGame/ai_space.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class CAI_Space {

private:
CGameGraph *m_game_graph;
CGameLevelCrossTable *m_cross_table;
CLevelGraph *m_level_graph;
CGraphEngine *m_graph_engine;
CEF_Storage *m_ef_storage;
Expand All @@ -51,12 +50,13 @@ class CAI_Space {
void init ();
IC CGameGraph &game_graph () const;
IC CGameGraph *get_game_graph () const;
IC void set_game_graph (CGameGraph* graph);
IC CLevelGraph &level_graph () const;
IC const CLevelGraph *get_level_graph () const;
IC const CGameLevelCrossTable &cross_table () const;
IC const CGameLevelCrossTable *get_cross_table () const;
IC const CPatrolPathStorage &patrol_paths () const;
IC CPatrolPathStorage &patrol_paths_raw();
IC CPatrolPathStorage &patrol_paths_raw ();
IC CEF_Storage &ef_storage () const;
IC CGraphEngine &graph_engine () const;
IC const CALifeSimulator &alife () const;
Expand Down
Loading

0 comments on commit c756706

Please sign in to comment.