2015-06-30 12:46:07 +03:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2015-09-16 23:19:21 +03:00
|
|
|
// STL uses exceptions, but we are not compiling with them - ignore warning
|
|
|
|
#pragma warning(disable : 4530)
|
|
|
|
|
|
|
|
// long STL names get truncated in browse info.
|
|
|
|
#pragma warning(disable : 4786)
|
|
|
|
|
|
|
|
#undef min
|
|
|
|
#undef max
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif // _WIN32
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
PlaceDirectory placeDirectory;
|
2015-06-30 12:46:07 +03:00
|
|
|
|
2016-02-04 03:18:26 +03:00
|
|
|
void PlaceDirectory::Reset()
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2015-08-20 13:35:01 +03:00
|
|
|
m_directory.clear();
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2015-08-20 13:35:01 +03:00
|
|
|
bool PlaceDirectory::IsKnown(Place place) const
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2017-01-29 02:56:29 +03:00
|
|
|
auto it = std::find(m_directory.begin(), m_directory.end(), place);
|
2015-08-20 13:35:01 +03:00
|
|
|
return (it != m_directory.end());
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2015-08-20 13:35:01 +03:00
|
|
|
PlaceDirectory::EntryType PlaceDirectory::GetEntry(Place place) const
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2015-08-20 13:35:01 +03:00
|
|
|
if (place == UNDEFINED_PLACE)
|
|
|
|
return 0;
|
2015-06-30 12:46:07 +03:00
|
|
|
|
2017-01-29 02:56:29 +03:00
|
|
|
auto it = std::find(m_directory.begin(), m_directory.end(), place);
|
2015-08-20 13:35:01 +03:00
|
|
|
if (it == m_directory.end())
|
|
|
|
{
|
|
|
|
assert(false && "PlaceDirectory::GetEntry failure");
|
|
|
|
return 0;
|
|
|
|
}
|
2015-09-16 23:19:21 +03:00
|
|
|
|
2015-08-20 13:35:01 +03:00
|
|
|
return 1 + (it - m_directory.begin());
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2015-08-20 13:35:01 +03:00
|
|
|
void PlaceDirectory::AddPlace(Place place)
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2015-08-20 13:35:01 +03:00
|
|
|
if (place == UNDEFINED_PLACE)
|
|
|
|
return;
|
2015-06-30 12:46:07 +03:00
|
|
|
|
2015-08-20 13:35:01 +03:00
|
|
|
assert(place < 1000);
|
2015-06-30 12:46:07 +03:00
|
|
|
|
2015-08-20 13:35:01 +03:00
|
|
|
if (IsKnown(place))
|
|
|
|
return;
|
2015-06-30 12:46:07 +03:00
|
|
|
|
2015-08-20 13:35:01 +03:00
|
|
|
m_directory.push_back(place);
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2015-08-20 13:35:01 +03:00
|
|
|
Place PlaceDirectory::EntryToPlace(EntryType entry) const
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2017-10-12 17:50:56 +03:00
|
|
|
if (entry == 0 || !m_directory.size())
|
2015-08-20 13:35:01 +03:00
|
|
|
return UNDEFINED_PLACE;
|
2015-06-30 12:46:07 +03:00
|
|
|
|
2015-08-20 13:35:01 +03:00
|
|
|
unsigned int i = entry - 1;
|
|
|
|
if (i > m_directory.size())
|
|
|
|
{
|
|
|
|
assert(false && "PlaceDirectory::EntryToPlace: Invalid entry");
|
|
|
|
return UNDEFINED_PLACE;
|
|
|
|
}
|
2015-06-30 12:46:07 +03:00
|
|
|
|
2017-10-12 17:50:56 +03:00
|
|
|
return m_directory[i];
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2015-08-20 13:35:01 +03:00
|
|
|
void PlaceDirectory::Save(int fd)
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
|
|
|
// store number of entries in directory
|
2017-01-29 02:56:29 +03:00
|
|
|
EntryType count = (EntryType)m_directory.size();
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &count, sizeof(EntryType));
|
2015-06-30 12:46:07 +03:00
|
|
|
|
2015-09-16 23:19:21 +03:00
|
|
|
// store entries
|
2017-01-29 02:56:29 +03:00
|
|
|
for (auto id : m_directory)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
2017-01-29 02:56:29 +03:00
|
|
|
auto placeName = TheBotPhrases->IDToName(id);
|
2015-06-30 12:46:07 +03:00
|
|
|
|
|
|
|
// store string length followed by string itself
|
2017-01-29 02:56:29 +03:00
|
|
|
unsigned short len = (unsigned short)Q_strlen(placeName) + 1;
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &len, sizeof(unsigned short));
|
|
|
|
_write(fd, placeName, len);
|
2015-08-20 13:35:01 +03:00
|
|
|
}
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2015-08-20 13:35:01 +03:00
|
|
|
void PlaceDirectory::Load(SteamFile *file)
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
|
|
|
// read number of entries
|
2015-08-20 13:35:01 +03:00
|
|
|
EntryType count;
|
|
|
|
file->Read(&count, sizeof(EntryType));
|
2015-06-30 12:46:07 +03:00
|
|
|
|
2015-08-20 13:35:01 +03:00
|
|
|
m_directory.reserve(count);
|
2015-06-30 12:46:07 +03:00
|
|
|
|
|
|
|
// read each entry
|
2015-08-20 13:35:01 +03:00
|
|
|
char placeName[256];
|
|
|
|
unsigned short len;
|
2017-10-12 17:50:56 +03:00
|
|
|
for (int i = 0; i < count; i++)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
file->Read(&len, sizeof(unsigned short));
|
|
|
|
file->Read(placeName, len);
|
2015-06-30 12:46:07 +03:00
|
|
|
|
2015-08-20 13:35:01 +03:00
|
|
|
AddPlace(TheBotPhrases->NameToID(placeName));
|
|
|
|
}
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2017-01-29 02:56:29 +03:00
|
|
|
char *GetBspFilename()
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2015-08-20 13:35:01 +03:00
|
|
|
static char bspFilename[256];
|
2017-01-29 02:56:29 +03:00
|
|
|
Q_snprintf(bspFilename, sizeof(bspFilename), "maps\\%s.bsp", STRING(gpGlobals->mapname));
|
2015-08-20 13:35:01 +03:00
|
|
|
return bspFilename;
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2015-08-20 13:35:01 +03:00
|
|
|
void CNavArea::Save(FILE *fp) const
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2017-10-12 17:50:56 +03:00
|
|
|
fprintf(fp, "v %f %f %f\n", m_extent.lo.x, m_extent.lo.y, m_extent.lo.z);
|
|
|
|
fprintf(fp, "v %f %f %f\n", m_extent.hi.x, m_extent.lo.y, m_neZ);
|
|
|
|
fprintf(fp, "v %f %f %f\n", m_extent.hi.x, m_extent.hi.y, m_extent.hi.z);
|
|
|
|
fprintf(fp, "v %f %f %f\n", m_extent.lo.x, m_extent.hi.y, m_swZ);
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
static int base = 1;
|
2017-10-12 17:50:56 +03:00
|
|
|
fprintf(fp, "\n\ng %04dArea%s%s%s%s\n", m_id,
|
2015-12-09 01:39:54 +03:00
|
|
|
(GetAttributes() & NAV_CROUCH) ? "CROUCH" : "", (GetAttributes() & NAV_JUMP) ? "JUMP" : "",
|
|
|
|
(GetAttributes() & NAV_PRECISE) ? "PRECISE" : "", (GetAttributes() & NAV_NO_JUMP) ? "NO_JUMP" : "");
|
2015-08-20 13:35:01 +03:00
|
|
|
|
2017-10-12 17:50:56 +03:00
|
|
|
fprintf(fp, "f %d %d %d %d\n\n", base, base + 1, base + 2, base + 3);
|
2015-08-20 13:35:01 +03:00
|
|
|
base += 4;
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2015-12-22 21:07:49 +03:00
|
|
|
void CNavArea::Save(int fd, unsigned int version)
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2015-08-20 13:35:01 +03:00
|
|
|
// save ID
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &m_id, sizeof(unsigned int));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
// save attribute flags
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &m_attributeFlags, sizeof(unsigned char));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
// save extent of area
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &m_extent, 6 * sizeof(float));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
// save heights of implicit corners
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &m_neZ, sizeof(float));
|
|
|
|
_write(fd, &m_swZ, sizeof(float));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
// save connections to adjacent areas
|
|
|
|
// in the enum order NORTH, EAST, SOUTH, WEST
|
2017-10-12 17:50:56 +03:00
|
|
|
for (int d = 0; d < NUM_DIRECTIONS; d++)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
// save number of connections for this direction
|
2017-01-29 02:56:29 +03:00
|
|
|
size_t count = m_connect[d].size();
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &count, sizeof(size_t));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
2017-01-29 02:56:29 +03:00
|
|
|
for (auto connect : m_connect[d]) {
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &connect.area->m_id, sizeof(unsigned int));
|
2015-08-20 13:35:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store hiding spots for this area
|
|
|
|
unsigned char count;
|
|
|
|
if (m_hidingSpotList.size() > 255)
|
|
|
|
{
|
|
|
|
count = 255;
|
|
|
|
CONSOLE_ECHO("Warning: NavArea #%d: Truncated hiding spot list to 255\n", m_id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-29 02:56:29 +03:00
|
|
|
count = (unsigned char)m_hidingSpotList.size();
|
2015-08-20 13:35:01 +03:00
|
|
|
}
|
|
|
|
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &count, sizeof(unsigned char));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
// store HidingSpot objects
|
|
|
|
unsigned int saveCount = 0;
|
2017-01-29 02:56:29 +03:00
|
|
|
for (auto spot : m_hidingSpotList)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
spot->Save(fd, version);
|
|
|
|
|
|
|
|
// overflow check
|
|
|
|
if (++saveCount == count)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the approach areas for this area
|
|
|
|
// save number of approach areas
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &m_approachCount, sizeof(unsigned char));
|
2015-12-22 21:07:49 +03:00
|
|
|
|
2015-08-20 13:35:01 +03:00
|
|
|
if (cv_bot_debug.value > 0.0f)
|
2015-12-22 21:07:49 +03:00
|
|
|
{
|
2015-08-20 13:35:01 +03:00
|
|
|
CONSOLE_ECHO(" m_approachCount = %d\n", m_approachCount);
|
2015-12-22 21:07:49 +03:00
|
|
|
}
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
// save approach area info
|
|
|
|
unsigned char type;
|
|
|
|
unsigned int zero = 0;
|
2017-10-12 17:50:56 +03:00
|
|
|
for (int a = 0; a < m_approachCount; a++)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
if (m_approach[a].here.area)
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &m_approach[a].here.area->m_id, sizeof(unsigned int));
|
2015-08-20 13:35:01 +03:00
|
|
|
else
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &zero, sizeof(unsigned int));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
if (m_approach[a].prev.area)
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &m_approach[a].prev.area->m_id, sizeof(unsigned int));
|
2015-08-20 13:35:01 +03:00
|
|
|
else
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &zero, sizeof(unsigned int));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
type = (unsigned char)m_approach[a].prevToHereHow;
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &type, sizeof(unsigned char));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
if (m_approach[a].next.area)
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &m_approach[a].next.area->m_id, sizeof(unsigned int));
|
2015-08-20 13:35:01 +03:00
|
|
|
else
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &zero, sizeof(unsigned int));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
type = (unsigned char)m_approach[a].hereToNextHow;
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &type, sizeof(unsigned char));
|
2015-08-20 13:35:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save encounter spots for this area
|
|
|
|
{
|
|
|
|
// save number of encounter paths for this area
|
|
|
|
unsigned int count = m_spotEncounterList.size();
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &count, sizeof(unsigned int));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
if (cv_bot_debug.value > 0.0f)
|
|
|
|
CONSOLE_ECHO(" m_spotEncounterList.size() = %d\n", count);
|
|
|
|
|
2017-01-29 02:56:29 +03:00
|
|
|
for (auto spote : m_spotEncounterList)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
2017-01-29 02:56:29 +03:00
|
|
|
if (spote.from.area)
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &spote.from.area->m_id, sizeof(unsigned int));
|
2015-08-20 13:35:01 +03:00
|
|
|
else
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &zero, sizeof(unsigned int));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
2017-01-29 02:56:29 +03:00
|
|
|
unsigned char dir = spote.fromDir;
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &dir, sizeof(unsigned char));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
2017-01-29 02:56:29 +03:00
|
|
|
if (spote.to.area)
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &spote.to.area->m_id, sizeof(unsigned int));
|
2015-08-20 13:35:01 +03:00
|
|
|
else
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &zero, sizeof(unsigned int));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
2017-01-29 02:56:29 +03:00
|
|
|
dir = spote.toDir;
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &dir, sizeof(unsigned char));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
// write list of spots along this path
|
|
|
|
unsigned char spotCount;
|
2017-01-29 02:56:29 +03:00
|
|
|
if (spote.spotList.size() > 255)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
spotCount = 255;
|
|
|
|
CONSOLE_ECHO("Warning: NavArea #%d: Truncated encounter spot list to 255\n", m_id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-29 02:56:29 +03:00
|
|
|
spotCount = (unsigned char)spote.spotList.size();
|
2015-08-20 13:35:01 +03:00
|
|
|
}
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &spotCount, sizeof(unsigned char));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
saveCount = 0;
|
2017-01-29 02:56:29 +03:00
|
|
|
for (auto order : spote.spotList)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
// order->spot may be NULL if we've loaded a nav mesh that has been edited but not re-analyzed
|
2017-01-29 02:56:29 +03:00
|
|
|
unsigned int id = (order.spot) ? order.spot->GetID() : 0;
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &id, sizeof(unsigned int));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
2017-01-29 02:56:29 +03:00
|
|
|
unsigned char t = 255 * order.t;
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &t, sizeof(unsigned char));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
// overflow check
|
|
|
|
if (++saveCount == spotCount)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// store place dictionary entry
|
|
|
|
PlaceDirectory::EntryType entry = placeDirectory.GetEntry(GetPlace());
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &entry, sizeof(entry));
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2015-12-22 21:07:49 +03:00
|
|
|
void CNavArea::Load(SteamFile *file, unsigned int version)
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2015-08-20 13:35:01 +03:00
|
|
|
// load ID
|
|
|
|
file->Read(&m_id, sizeof(unsigned int));
|
|
|
|
|
|
|
|
// update nextID to avoid collisions
|
2015-12-09 01:39:54 +03:00
|
|
|
if (m_id >= IMPL(m_nextID))
|
|
|
|
IMPL(m_nextID) = m_id + 1;
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
// load attribute flags
|
|
|
|
file->Read(&m_attributeFlags, sizeof(unsigned char));
|
|
|
|
|
|
|
|
// load extent of area
|
|
|
|
file->Read(&m_extent, 6 * sizeof(float));
|
|
|
|
|
|
|
|
m_center.x = (m_extent.lo.x + m_extent.hi.x) / 2.0f;
|
|
|
|
m_center.y = (m_extent.lo.y + m_extent.hi.y) / 2.0f;
|
|
|
|
m_center.z = (m_extent.lo.z + m_extent.hi.z) / 2.0f;
|
|
|
|
|
|
|
|
// load heights of implicit corners
|
|
|
|
file->Read(&m_neZ, sizeof(float));
|
|
|
|
file->Read(&m_swZ, sizeof(float));
|
|
|
|
|
|
|
|
// load connections (IDs) to adjacent areas
|
|
|
|
// in the enum order NORTH, EAST, SOUTH, WEST
|
2017-10-12 17:50:56 +03:00
|
|
|
for (int d = 0; d < NUM_DIRECTIONS; d++)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
// load number of connections for this direction
|
|
|
|
unsigned int count;
|
|
|
|
file->Read(&count, sizeof(unsigned int));
|
|
|
|
|
2017-10-12 17:50:56 +03:00
|
|
|
for (unsigned int i = 0; i < count; i++)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
NavConnect connect;
|
|
|
|
file->Read(&connect.id, sizeof(unsigned int));
|
|
|
|
|
|
|
|
m_connect[d].push_back(connect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load hiding spots
|
|
|
|
// load number of hiding spots
|
|
|
|
unsigned char hidingSpotCount;
|
|
|
|
file->Read(&hidingSpotCount, sizeof(unsigned char));
|
|
|
|
|
|
|
|
if (version == 1)
|
|
|
|
{
|
|
|
|
// load simple vector array
|
|
|
|
Vector pos;
|
2017-10-12 17:50:56 +03:00
|
|
|
for (int h = 0; h < hidingSpotCount; h++)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
file->Read(&pos, 3 * sizeof(float));
|
|
|
|
|
|
|
|
// create new hiding spot and put on master list
|
|
|
|
HidingSpot *spot = new HidingSpot(&pos, HidingSpot::IN_COVER);
|
|
|
|
|
|
|
|
m_hidingSpotList.push_back(spot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// load HidingSpot objects for this area
|
2017-10-12 17:50:56 +03:00
|
|
|
for (int h = 0; h < hidingSpotCount; h++)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
// create new hiding spot and put on master list
|
|
|
|
HidingSpot *spot = new HidingSpot;
|
|
|
|
|
|
|
|
spot->Load(file, version);
|
2015-09-16 23:19:21 +03:00
|
|
|
|
2015-08-20 13:35:01 +03:00
|
|
|
m_hidingSpotList.push_back(spot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load number of approach areas
|
|
|
|
file->Read(&m_approachCount, sizeof(unsigned char));
|
|
|
|
|
|
|
|
// load approach area info (IDs)
|
|
|
|
unsigned char type;
|
2017-10-12 17:50:56 +03:00
|
|
|
for (int a = 0; a < m_approachCount; a++)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
file->Read(&m_approach[a].here.id, sizeof(unsigned int));
|
|
|
|
|
|
|
|
file->Read(&m_approach[a].prev.id, sizeof(unsigned int));
|
|
|
|
file->Read(&type, sizeof(unsigned char) );
|
|
|
|
m_approach[a].prevToHereHow = (NavTraverseType)type;
|
|
|
|
|
|
|
|
file->Read(&m_approach[a].next.id, sizeof(unsigned int));
|
|
|
|
file->Read(&type, sizeof(unsigned char));
|
|
|
|
m_approach[a].hereToNextHow = (NavTraverseType)type;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load encounter paths for this area
|
|
|
|
unsigned int count;
|
|
|
|
file->Read(&count, sizeof(unsigned int));
|
|
|
|
|
|
|
|
if (version < 3)
|
|
|
|
{
|
|
|
|
// old data, read and discard
|
2017-10-12 17:50:56 +03:00
|
|
|
for (unsigned int e = 0; e < count; e++)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
SpotEncounter encounter;
|
|
|
|
|
|
|
|
file->Read(&encounter.from.id, sizeof(unsigned int));
|
|
|
|
file->Read(&encounter.to.id, sizeof(unsigned int));
|
|
|
|
|
|
|
|
file->Read(&encounter.path.from.x, 3 * sizeof(float));
|
|
|
|
file->Read(&encounter.path.to.x, 3 * sizeof(float));
|
|
|
|
|
|
|
|
// read list of spots along this path
|
|
|
|
unsigned char spotCount;
|
|
|
|
file->Read(&spotCount, sizeof(unsigned char));
|
2015-09-16 23:19:21 +03:00
|
|
|
|
2017-10-12 17:50:56 +03:00
|
|
|
for (int s = 0; s < spotCount; s++)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
Vector pos;
|
|
|
|
file->Read(&pos, 3 * sizeof(float));
|
|
|
|
file->Read(&pos, sizeof(float));
|
|
|
|
}
|
|
|
|
}
|
2016-04-07 23:20:45 +03:00
|
|
|
|
2015-08-20 13:35:01 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-12 17:50:56 +03:00
|
|
|
for (unsigned int e = 0; e < count; e++)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
SpotEncounter encounter;
|
|
|
|
|
|
|
|
file->Read(&encounter.from.id, sizeof(unsigned int));
|
|
|
|
|
|
|
|
unsigned char dir;
|
|
|
|
file->Read(&dir, sizeof(unsigned char));
|
|
|
|
encounter.fromDir = static_cast<NavDirType>(dir);
|
|
|
|
|
|
|
|
file->Read(&encounter.to.id, sizeof(unsigned int));
|
|
|
|
|
|
|
|
file->Read(&dir, sizeof(unsigned char));
|
|
|
|
encounter.toDir = static_cast<NavDirType>(dir);
|
|
|
|
|
|
|
|
// read list of spots along this path
|
|
|
|
unsigned char spotCount;
|
|
|
|
file->Read(&spotCount, sizeof(unsigned char));
|
2015-09-16 23:19:21 +03:00
|
|
|
|
2015-08-20 13:35:01 +03:00
|
|
|
SpotOrder order;
|
2017-10-12 17:50:56 +03:00
|
|
|
for (int s = 0; s < spotCount; s++)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
file->Read(&order.id, sizeof(unsigned int));
|
|
|
|
|
|
|
|
unsigned char t;
|
|
|
|
file->Read(&t, sizeof(unsigned char));
|
|
|
|
|
2016-02-23 02:13:52 +03:00
|
|
|
order.t = float(t) / 255.0f;
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
encounter.spotList.push_back(order);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_spotEncounterList.push_back(encounter);
|
|
|
|
}
|
|
|
|
|
2017-01-29 02:56:29 +03:00
|
|
|
if (version >= NAV_VERSION)
|
|
|
|
{
|
|
|
|
// Load Place data
|
|
|
|
PlaceDirectory::EntryType entry;
|
|
|
|
file->Read(&entry, sizeof(entry));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
2017-01-29 02:56:29 +03:00
|
|
|
// convert entry to actual Place
|
|
|
|
SetPlace(placeDirectory.EntryToPlace(entry));
|
|
|
|
}
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-04 03:18:26 +03:00
|
|
|
NavErrorType CNavArea::PostLoad()
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2015-08-20 13:35:01 +03:00
|
|
|
NavErrorType error = NAV_OK;
|
|
|
|
|
|
|
|
// connect areas together
|
2017-10-12 17:50:56 +03:00
|
|
|
for (int d = 0; d < NUM_DIRECTIONS; d++)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
2017-02-02 19:56:35 +03:00
|
|
|
for (auto& connect : m_connect[d])
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
2017-02-01 18:24:32 +03:00
|
|
|
auto id = connect.id;
|
|
|
|
connect.area = TheNavAreaGrid.GetNavAreaByID(id);
|
2017-10-12 17:50:56 +03:00
|
|
|
if (id && !connect.area)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
CONSOLE_ECHO("ERROR: Corrupt navigation data. Cannot connect Navigation Areas.\n");
|
|
|
|
error = NAV_CORRUPT_DATA;
|
|
|
|
}
|
2015-09-16 23:19:21 +03:00
|
|
|
}
|
2015-08-20 13:35:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// resolve approach area IDs
|
2017-10-12 17:50:56 +03:00
|
|
|
for (int a = 0; a < m_approachCount; a++)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
m_approach[a].here.area = TheNavAreaGrid.GetNavAreaByID(m_approach[a].here.id);
|
2017-10-12 17:50:56 +03:00
|
|
|
if (m_approach[a].here.id && !m_approach[a].here.area)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
CONSOLE_ECHO("ERROR: Corrupt navigation data. Missing Approach Area (here).\n");
|
|
|
|
error = NAV_CORRUPT_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_approach[a].prev.area = TheNavAreaGrid.GetNavAreaByID(m_approach[a].prev.id);
|
2017-10-12 17:50:56 +03:00
|
|
|
if (m_approach[a].prev.id && !m_approach[a].prev.area)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
CONSOLE_ECHO("ERROR: Corrupt navigation data. Missing Approach Area (prev).\n");
|
|
|
|
error = NAV_CORRUPT_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_approach[a].next.area = TheNavAreaGrid.GetNavAreaByID(m_approach[a].next.id);
|
2017-10-12 17:50:56 +03:00
|
|
|
if (m_approach[a].next.id && !m_approach[a].next.area)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
CONSOLE_ECHO("ERROR: Corrupt navigation data. Missing Approach Area (next).\n");
|
|
|
|
error = NAV_CORRUPT_DATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve spot encounter IDs
|
2017-02-02 19:56:35 +03:00
|
|
|
for (auto& spote : m_spotEncounterList)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
2017-01-29 02:56:29 +03:00
|
|
|
spote.from.area = TheNavAreaGrid.GetNavAreaByID(spote.from.id);
|
2017-10-12 17:50:56 +03:00
|
|
|
if (!spote.from.area)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
CONSOLE_ECHO("ERROR: Corrupt navigation data. Missing \"from\" Navigation Area for Encounter Spot.\n");
|
|
|
|
error = NAV_CORRUPT_DATA;
|
|
|
|
}
|
|
|
|
|
2017-01-29 02:56:29 +03:00
|
|
|
spote.to.area = TheNavAreaGrid.GetNavAreaByID(spote.to.id);
|
2017-10-12 17:50:56 +03:00
|
|
|
if (!spote.to.area)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
CONSOLE_ECHO("ERROR: Corrupt navigation data. Missing \"to\" Navigation Area for Encounter Spot.\n");
|
|
|
|
error = NAV_CORRUPT_DATA;
|
|
|
|
}
|
|
|
|
|
2017-01-29 02:56:29 +03:00
|
|
|
if (spote.from.area && spote.to.area)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
// compute path
|
|
|
|
float halfWidth;
|
2017-01-29 02:56:29 +03:00
|
|
|
ComputePortal(spote.to.area, spote.toDir, &spote.path.to, &halfWidth);
|
|
|
|
ComputePortal(spote.from.area, spote.fromDir, &spote.path.from, &halfWidth);
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
const float eyeHeight = HalfHumanHeight;
|
2017-01-29 02:56:29 +03:00
|
|
|
spote.path.from.z = spote.from.area->GetZ(&spote.path.from) + eyeHeight;
|
|
|
|
spote.path.to.z = spote.to.area->GetZ(&spote.path.to) + eyeHeight;
|
2015-08-20 13:35:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// resolve HidingSpot IDs
|
2017-02-02 19:56:35 +03:00
|
|
|
for (auto& order : spote.spotList)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
2017-01-29 02:56:29 +03:00
|
|
|
order.spot = GetHidingSpotByID(order.id);
|
2017-10-12 17:50:56 +03:00
|
|
|
if (!order.spot)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
CONSOLE_ECHO("ERROR: Corrupt navigation data. Missing Hiding Spot\n");
|
|
|
|
error = NAV_CORRUPT_DATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// build overlap list
|
2015-12-09 01:39:54 +03:00
|
|
|
// TODO: Optimize this
|
2017-01-29 02:56:29 +03:00
|
|
|
for (auto area : TheNavAreaList)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
if (area == this)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (IsOverlapping(area))
|
|
|
|
m_overlapList.push_back(area);
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2015-12-22 21:07:49 +03:00
|
|
|
// Store AI navigation data to a file
|
|
|
|
bool SaveNavigationMap(const char *filename)
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2017-10-12 17:50:56 +03:00
|
|
|
if (!filename)
|
2015-08-20 13:35:01 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Store the NAV file
|
2017-10-12 17:50:56 +03:00
|
|
|
Q_FixSlashes(const_cast<char *>(filename));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
int fd = _open(filename, _O_BINARY | _O_CREAT | _O_WRONLY, _S_IREAD | _S_IWRITE);
|
|
|
|
#else
|
|
|
|
int fd = creat(filename, S_IRUSR | S_IWUSR | S_IRGRP);
|
|
|
|
#endif // WIN32
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// store "magic number" to help identify this kind of file
|
|
|
|
unsigned int magic = NAV_MAGIC_NUMBER;
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &magic, sizeof(unsigned int));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
// store version number of file
|
2017-01-29 02:56:29 +03:00
|
|
|
unsigned int version = NAV_VERSION;
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &version, sizeof(unsigned int));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
// get size of source bsp file and store it in the nav file
|
|
|
|
// so we can test if the bsp changed since the nav file was made
|
2017-01-29 02:56:29 +03:00
|
|
|
char *bspFilename = GetBspFilename();
|
2017-10-12 17:50:56 +03:00
|
|
|
if (!bspFilename)
|
2015-08-20 13:35:01 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned int bspSize = (unsigned int)GET_FILE_SIZE(bspFilename);
|
|
|
|
CONSOLE_ECHO("Size of bsp file '%s' is %u bytes.\n", bspFilename, bspSize);
|
|
|
|
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &bspSize, sizeof(unsigned int));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
// Build a directory of the Places in this map
|
|
|
|
placeDirectory.Reset();
|
|
|
|
|
2017-01-29 02:56:29 +03:00
|
|
|
for (auto area : TheNavAreaList)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
Place place = area->GetPlace();
|
2017-01-29 02:56:29 +03:00
|
|
|
if (place) {
|
2015-08-20 13:35:01 +03:00
|
|
|
placeDirectory.AddPlace(place);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
placeDirectory.Save(fd);
|
|
|
|
|
|
|
|
// Store navigation areas
|
|
|
|
// store number of areas
|
|
|
|
unsigned int count = TheNavAreaList.size();
|
2017-10-12 17:50:56 +03:00
|
|
|
_write(fd, &count, sizeof(unsigned int));
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
// store each area
|
2017-01-29 02:56:29 +03:00
|
|
|
for (auto area : TheNavAreaList) {
|
2015-08-20 13:35:01 +03:00
|
|
|
area->Save(fd, version);
|
|
|
|
}
|
|
|
|
|
2017-10-12 17:50:56 +03:00
|
|
|
_close(fd);
|
2015-08-20 13:35:01 +03:00
|
|
|
return true;
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2015-12-22 21:07:49 +03:00
|
|
|
// Load place map
|
|
|
|
// This is legacy code - Places are stored directly in the nav file now
|
|
|
|
void LoadLocationFile(const char *filename)
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2015-08-20 13:35:01 +03:00
|
|
|
char locFilename[256];
|
|
|
|
Q_strcpy(locFilename, filename);
|
|
|
|
|
|
|
|
char *dot = Q_strchr(locFilename, '.');
|
2017-01-20 17:52:37 +03:00
|
|
|
if (dot)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
Q_strcpy(dot, ".loc");
|
|
|
|
|
|
|
|
int locDataLength;
|
|
|
|
char *locDataFile = (char *)LOAD_FILE_FOR_ME(const_cast<char *>(locFilename), &locDataLength);
|
|
|
|
char *locData = locDataFile;
|
|
|
|
|
2017-01-20 17:52:37 +03:00
|
|
|
if (locData)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
CONSOLE_ECHO("Loading legacy 'location file' '%s'\n", locFilename);
|
|
|
|
|
|
|
|
// read directory
|
2017-10-12 17:50:56 +03:00
|
|
|
locData = SharedParse(locData);
|
|
|
|
int dirSize = Q_atoi(SharedGetToken());
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
if (dirSize)
|
|
|
|
{
|
|
|
|
std::vector<unsigned int> directory;
|
|
|
|
directory.reserve(dirSize);
|
|
|
|
|
2017-10-12 17:50:56 +03:00
|
|
|
for (int i = 0; i < dirSize; i++)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
2017-10-12 17:50:56 +03:00
|
|
|
locData = SharedParse(locData);
|
|
|
|
directory.push_back(TheBotPhrases->NameToID(SharedGetToken()));
|
2015-08-20 13:35:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// read places for each nav area
|
|
|
|
unsigned int areaID, locDirIndex;
|
|
|
|
while (true)
|
|
|
|
{
|
2017-10-12 17:50:56 +03:00
|
|
|
locData = SharedParse(locData);
|
|
|
|
if (!locData)
|
2015-08-20 13:35:01 +03:00
|
|
|
break;
|
|
|
|
|
2017-10-12 17:50:56 +03:00
|
|
|
areaID = Q_atoi(SharedGetToken());
|
2015-08-20 13:35:01 +03:00
|
|
|
|
2017-10-12 17:50:56 +03:00
|
|
|
locData = SharedParse(locData);
|
|
|
|
locDirIndex = Q_atoi(SharedGetToken());
|
2015-08-20 13:35:01 +03:00
|
|
|
|
|
|
|
CNavArea *area = TheNavAreaGrid.GetNavAreaByID(areaID);
|
|
|
|
unsigned int place = (locDirIndex > 0) ? directory[locDirIndex - 1] : UNDEFINED_PLACE;
|
2015-09-16 23:19:21 +03:00
|
|
|
|
2017-01-20 17:52:37 +03:00
|
|
|
if (area)
|
2015-08-20 13:35:01 +03:00
|
|
|
area->SetPlace(place);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FREE_FILE(locDataFile);
|
|
|
|
}
|
|
|
|
}
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2015-12-22 21:07:49 +03:00
|
|
|
// Performs a lightweight sanity-check of the specified map's nav mesh
|
|
|
|
void SanityCheckNavigationMap(const char *mapName)
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2015-08-20 13:35:01 +03:00
|
|
|
if (!mapName)
|
|
|
|
{
|
|
|
|
CONSOLE_ECHO("ERROR: navigation file not specified.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nav filename is derived from map filename
|
|
|
|
const int BufLen = 4096;
|
|
|
|
char bspFilename[BufLen];
|
|
|
|
char navFilename[BufLen];
|
|
|
|
|
|
|
|
Q_snprintf(bspFilename, BufLen, "maps\\%s.bsp", mapName);
|
|
|
|
Q_snprintf(navFilename, BufLen, "maps\\%s.nav", mapName);
|
|
|
|
|
|
|
|
SteamFile navFile(navFilename);
|
|
|
|
|
|
|
|
if (!navFile.IsValid())
|
|
|
|
{
|
|
|
|
CONSOLE_ECHO("ERROR: navigation file %s does not exist.\n", navFilename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check magic number
|
|
|
|
bool result;
|
|
|
|
unsigned int magic;
|
|
|
|
result = navFile.Read(&magic, sizeof(unsigned int));
|
|
|
|
if (!result || magic != NAV_MAGIC_NUMBER)
|
|
|
|
{
|
|
|
|
CONSOLE_ECHO("ERROR: Invalid navigation file '%s'.\n", navFilename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read file version number
|
|
|
|
unsigned int version;
|
|
|
|
result = navFile.Read(&version, sizeof(unsigned int));
|
2017-01-29 02:56:29 +03:00
|
|
|
if (!result || version > NAV_VERSION)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
CONSOLE_ECHO("ERROR: Unknown version in navigation file %s.\n", navFilename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (version >= 4)
|
|
|
|
{
|
|
|
|
// get size of source bsp file and verify that the bsp hasn't changed
|
|
|
|
unsigned int saveBspSize;
|
|
|
|
navFile.Read(&saveBspSize, sizeof(unsigned int));
|
|
|
|
|
|
|
|
// verify size
|
2017-10-12 17:50:56 +03:00
|
|
|
if (!bspFilename)
|
2015-08-20 13:35:01 +03:00
|
|
|
{
|
|
|
|
CONSOLE_ECHO("ERROR: No map corresponds to navigation file %s.\n", navFilename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int bspSize = (unsigned int)GET_FILE_SIZE(bspFilename);
|
|
|
|
if (bspSize != saveBspSize)
|
|
|
|
{
|
|
|
|
// this nav file is out of date for this bsp file
|
|
|
|
CONSOLE_ECHO("ERROR: Out-of-date navigation data in navigation file %s.\n", navFilename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-12-22 21:07:49 +03:00
|
|
|
|
2015-08-20 13:35:01 +03:00
|
|
|
CONSOLE_ECHO("navigation file %s passes the sanity check.\n", navFilename);
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-04 03:18:26 +03:00
|
|
|
NavErrorType LoadNavigationMap()
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2015-12-05 22:40:30 +03:00
|
|
|
// since the navigation map is destroyed on map change,
|
|
|
|
// if it exists it has already been loaded for this map
|
|
|
|
if (!TheNavAreaList.empty())
|
|
|
|
return NAV_OK;
|
|
|
|
|
|
|
|
// nav filename is derived from map filename
|
|
|
|
char filename[256];
|
|
|
|
Q_sprintf(filename, "maps\\%s.nav", STRING(gpGlobals->mapname));
|
|
|
|
|
|
|
|
// free previous navigation map data
|
|
|
|
DestroyNavigationMap();
|
|
|
|
placeDirectory.Reset();
|
|
|
|
|
2015-12-09 01:39:54 +03:00
|
|
|
IMPL_CLASS(CNavArea, m_nextID) = 1;
|
2015-12-05 22:40:30 +03:00
|
|
|
|
|
|
|
SteamFile navFile(filename);
|
|
|
|
|
|
|
|
if (!navFile.IsValid())
|
|
|
|
return NAV_CANT_ACCESS_FILE;
|
|
|
|
|
|
|
|
// check magic number
|
|
|
|
bool result;
|
|
|
|
unsigned int magic;
|
|
|
|
result = navFile.Read(&magic, sizeof(unsigned int));
|
|
|
|
if (!result || magic != NAV_MAGIC_NUMBER)
|
|
|
|
{
|
|
|
|
CONSOLE_ECHO("ERROR: Invalid navigation file '%s'.\n", filename);
|
|
|
|
return NAV_INVALID_FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read file version number
|
|
|
|
unsigned int version;
|
|
|
|
result = navFile.Read(&version, sizeof(unsigned int));
|
2017-01-29 02:56:29 +03:00
|
|
|
if (!result || version > NAV_VERSION)
|
2015-12-05 22:40:30 +03:00
|
|
|
{
|
|
|
|
CONSOLE_ECHO("ERROR: Unknown navigation file version.\n");
|
|
|
|
return NAV_BAD_FILE_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (version >= 4)
|
|
|
|
{
|
|
|
|
// get size of source bsp file and verify that the bsp hasn't changed
|
|
|
|
unsigned int saveBspSize;
|
|
|
|
navFile.Read(&saveBspSize, sizeof(unsigned int));
|
|
|
|
|
|
|
|
// verify size
|
2017-01-29 02:56:29 +03:00
|
|
|
char *bspFilename = GetBspFilename();
|
2017-10-12 17:50:56 +03:00
|
|
|
if (!bspFilename)
|
2015-12-05 22:40:30 +03:00
|
|
|
return NAV_INVALID_FILE;
|
|
|
|
|
|
|
|
unsigned int bspSize = (unsigned int)GET_FILE_SIZE(bspFilename);
|
|
|
|
if (bspSize != saveBspSize)
|
|
|
|
{
|
|
|
|
// this nav file is out of date for this bsp file
|
|
|
|
char *msg = "*** WARNING ***\nThe AI navigation data is from a different version of this map.\nThe CPU players will likely not perform well.\n";
|
|
|
|
HintMessageToAllPlayers(msg);
|
|
|
|
CONSOLE_ECHO("\n-----------------\n");
|
|
|
|
CONSOLE_ECHO(msg);
|
|
|
|
CONSOLE_ECHO("-----------------\n\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// load Place directory
|
2017-01-29 02:56:29 +03:00
|
|
|
if (version >= NAV_VERSION)
|
2015-12-05 22:40:30 +03:00
|
|
|
{
|
|
|
|
placeDirectory.Load(&navFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get number of areas
|
|
|
|
unsigned int count;
|
|
|
|
result = navFile.Read(&count, sizeof(unsigned int));
|
|
|
|
|
|
|
|
Extent extent;
|
|
|
|
extent.lo.x = 9999999999.9f;
|
|
|
|
extent.lo.y = 9999999999.9f;
|
|
|
|
extent.hi.x = -9999999999.9f;
|
|
|
|
extent.hi.y = -9999999999.9f;
|
|
|
|
|
|
|
|
// load the areas and compute total extent
|
2017-10-12 17:50:56 +03:00
|
|
|
for (unsigned int i = 0; i < count; i++)
|
2015-12-05 22:40:30 +03:00
|
|
|
{
|
|
|
|
CNavArea *area = new CNavArea;
|
|
|
|
area->Load(&navFile, version);
|
|
|
|
TheNavAreaList.push_back(area);
|
|
|
|
|
|
|
|
const Extent *areaExtent = area->GetExtent();
|
|
|
|
|
|
|
|
// check validity of nav area
|
2017-01-29 02:56:29 +03:00
|
|
|
if (areaExtent->lo.x >= areaExtent->hi.x || areaExtent->lo.y >= areaExtent->hi.y) {
|
|
|
|
CONSOLE_ECHO("WARNING: Degenerate Navigation Area #%d at ( %g, %g, %g )\n", area->GetID(), area->m_center.x, area->m_center.y, area->m_center.z);
|
|
|
|
}
|
2015-12-05 22:40:30 +03:00
|
|
|
|
|
|
|
if (areaExtent->lo.x < extent.lo.x)
|
|
|
|
extent.lo.x = areaExtent->lo.x;
|
|
|
|
|
|
|
|
if (areaExtent->lo.y < extent.lo.y)
|
|
|
|
extent.lo.y = areaExtent->lo.y;
|
|
|
|
|
|
|
|
if (areaExtent->hi.x > extent.hi.x)
|
|
|
|
extent.hi.x = areaExtent->hi.x;
|
|
|
|
|
|
|
|
if (areaExtent->hi.y > extent.hi.y)
|
|
|
|
extent.hi.y = areaExtent->hi.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the areas to the grid
|
|
|
|
TheNavAreaGrid.Initialize(extent.lo.x, extent.hi.x, extent.lo.y, extent.hi.y);
|
|
|
|
|
2017-01-29 02:56:29 +03:00
|
|
|
for (auto area : TheNavAreaList) {
|
|
|
|
TheNavAreaGrid.AddNavArea(area);
|
|
|
|
}
|
2015-12-05 22:40:30 +03:00
|
|
|
|
|
|
|
// allow areas to connect to each other, etc
|
2017-01-29 02:56:29 +03:00
|
|
|
for (auto area : TheNavAreaList) {
|
2015-12-05 22:40:30 +03:00
|
|
|
area->PostLoad();
|
|
|
|
}
|
|
|
|
|
|
|
|
// load legacy location file (Places)
|
2017-01-29 02:56:29 +03:00
|
|
|
if (version < NAV_VERSION)
|
2015-12-05 22:40:30 +03:00
|
|
|
{
|
|
|
|
LoadLocationFile(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up all the ladders
|
|
|
|
BuildLadders();
|
|
|
|
return NAV_OK;
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|