2009-04-18 19:00:33 +02:00
|
|
|
/* Copyright (C) 2009 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2006-07-09 00:40:01 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "TerritoryManager.h"
|
|
|
|
|
2007-01-08 02:56:46 +01:00
|
|
|
#include "graphics/Frustum.h"
|
|
|
|
#include "graphics/Camera.h"
|
2006-08-25 08:04:33 +02:00
|
|
|
#include "graphics/GameView.h"
|
2006-07-09 00:40:01 +02:00
|
|
|
#include "graphics/Model.h"
|
2007-01-08 02:56:46 +01:00
|
|
|
#include "graphics/Terrain.h"
|
|
|
|
#include "graphics/Unit.h"
|
2008-01-03 23:07:18 +01:00
|
|
|
#include "lib/allocators/allocators.h" // matrix_alloc
|
2006-08-25 08:04:33 +02:00
|
|
|
#include "lib/ogl.h"
|
2007-01-08 02:56:46 +01:00
|
|
|
#include "lib/timer.h"
|
|
|
|
#include "maths/Bound.h"
|
2007-01-24 21:17:28 +01:00
|
|
|
#include "maths/MathUtil.h"
|
2007-01-08 02:56:46 +01:00
|
|
|
#include "ps/Game.h"
|
|
|
|
#include "ps/Player.h"
|
|
|
|
#include "ps/Profile.h"
|
|
|
|
#include "simulation/Entity.h"
|
|
|
|
#include "simulation/EntityManager.h"
|
|
|
|
#include "simulation/EntityManager.h"
|
|
|
|
#include "simulation/EntityTemplate.h"
|
|
|
|
#include "simulation/LOSManager.h"
|
2006-07-09 00:40:01 +02:00
|
|
|
|
|
|
|
CTerritoryManager::CTerritoryManager()
|
|
|
|
{
|
|
|
|
m_TerritoryMatrix = 0;
|
# Added tool for viewing models and animations outside the game.
Atlas: Added ActorViewer. Moved GL canvas into separate class for shared
use. Disabled message-handling callback while blocked on the game, and
stopped creating dialog boxes inside the game thread in order to avoid
deadlocks (hopefully). Support multiple Views (for independent sets of
camera/update/render code). Recalculate territory boundaries when
necessary. Changed default list of animations to match those currently
used by actors.
# Tidied up more code.
Moved some more #includes out of .h files, to minimise unnecessary
compilation.
MathUtil: Deleted unused/unuseful macros (M_PI (use PI instead), M_PI_2
(use PI/2), MAX3, ABS (use abs)).
ObjectManager: Removed some ScEd-specific things.
Unit: Moved creation out of UnitManager, so units can be created without
adding to the manager. Changed CStr8 to the more conventional CStr.
app_hooks: Removed warning for setting multiple times.
win: Restored SEH catcher.
GameSetup, GameView: Removed RenderNoCull, because it doesn't seem to do
what it says it does ("force renderer to load everything") since we're
loading-on-demand most stuff and it doesn't seem especially useful since
we'd prefer to minimise loading times (but feel free to correct me if
I'm wrong). (And because it crashes when things need to be initialised
in a different order, so it's easier to remove than to understand and
fix it.)
PatchRData, Renderer: Work sensibly when there's no game (hence no LOS
manager, water, etc).
LOSManager: Use entity position instead of actor position when possible.
TerritoryManager: Allow delayed recalculations (so Atlas can issue lots
of move+recalculate commands per frame).
Cinematic: Non-pointer wxTimer, so it doesn't leak and doesn't have to
be deleted manually.
This was SVN commit r4261.
2006-08-28 19:36:42 +02:00
|
|
|
m_DelayedRecalculate = false;
|
2006-07-09 00:40:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CTerritoryManager::~CTerritoryManager()
|
|
|
|
{
|
|
|
|
if(m_TerritoryMatrix)
|
|
|
|
{
|
|
|
|
matrix_free( (void**) m_TerritoryMatrix );
|
|
|
|
m_TerritoryMatrix = 0;
|
|
|
|
}
|
2006-07-12 16:49:10 +02:00
|
|
|
|
|
|
|
for( size_t i=0; i<m_Territories.size(); i++)
|
|
|
|
delete m_Territories[i];
|
|
|
|
m_Territories.clear();
|
2006-07-09 00:40:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTerritoryManager::Initialize()
|
|
|
|
{
|
|
|
|
CTerrain* terrain = g_Game->GetWorld()->GetTerrain();
|
|
|
|
m_TilesPerSide = terrain->GetVerticesPerSide() - 1;
|
|
|
|
|
|
|
|
m_TerritoryMatrix = (CTerritory***) matrix_alloc( m_TilesPerSide, m_TilesPerSide, sizeof(CTerritory*) );
|
|
|
|
Recalculate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTerritoryManager::Recalculate()
|
|
|
|
{
|
|
|
|
// Delete any territories created last time we called Recalculate()
|
|
|
|
|
|
|
|
for( size_t i=0; i<m_Territories.size(); i++)
|
|
|
|
{
|
|
|
|
if( m_Territories[i]->centre )
|
|
|
|
m_Territories[i]->centre->m_associatedTerritory = 0;
|
|
|
|
delete m_Territories[i];
|
|
|
|
}
|
|
|
|
m_Territories.clear();
|
|
|
|
|
|
|
|
// First, find all the units that are territory centres
|
|
|
|
std::vector<CEntity*> centres;
|
|
|
|
std::vector<CEntity*> entities;
|
2010-05-20 20:09:23 +02:00
|
|
|
// g_EntityManager.GetExtant(entities);
|
2006-07-09 00:40:01 +02:00
|
|
|
for( size_t i=0; i<entities.size(); i++ )
|
|
|
|
{
|
2006-08-29 01:05:15 +02:00
|
|
|
if( !entities[i]->entf_get(ENTF_DESTROYED) && entities[i]->m_base->m_isTerritoryCentre )
|
2006-07-09 00:40:01 +02:00
|
|
|
centres.push_back(entities[i]);
|
|
|
|
}
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
const size_t mapSize = m_TilesPerSide * CELL_SIZE;
|
2006-07-09 00:40:01 +02:00
|
|
|
|
|
|
|
// If there aren't any centre objects, create one big Gaia territory which spans the whole map
|
2006-07-12 16:49:10 +02:00
|
|
|
if( centres.empty() )
|
2006-07-09 00:40:01 +02:00
|
|
|
{
|
|
|
|
std::vector<CVector2D> boundary;
|
|
|
|
boundary.push_back( CVector2D(0, 0) );
|
|
|
|
boundary.push_back( CVector2D(0, mapSize) );
|
|
|
|
boundary.push_back( CVector2D(mapSize, mapSize) );
|
|
|
|
boundary.push_back( CVector2D(mapSize, 0) );
|
|
|
|
|
2006-07-10 01:12:37 +02:00
|
|
|
CTerritory* ter = new CTerritory( g_Game->GetPlayer(0), HEntity(), boundary );
|
2006-07-09 00:40:01 +02:00
|
|
|
|
|
|
|
m_Territories.push_back(ter);
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
for( size_t x=0; x<m_TilesPerSide; x++ )
|
2006-07-09 00:40:01 +02:00
|
|
|
{
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
for( size_t z=0; z<m_TilesPerSide; z++ )
|
2006-07-09 00:40:01 +02:00
|
|
|
{
|
|
|
|
m_TerritoryMatrix[x][z] = ter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// For each centre object, create a territory
|
|
|
|
for( size_t i=0; i<centres.size(); i++ )
|
|
|
|
{
|
|
|
|
std::vector<CVector2D> boundary;
|
|
|
|
CalculateBoundary( centres, i, boundary );
|
|
|
|
|
2006-07-10 01:12:37 +02:00
|
|
|
CTerritory* ter = new CTerritory( centres[i]->GetPlayer(), centres[i]->me, boundary );
|
2006-07-09 00:40:01 +02:00
|
|
|
|
|
|
|
centres[i]->m_associatedTerritory = ter;
|
|
|
|
m_Territories.push_back(ter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// For each tile, match it to the closest centre object to it.
|
|
|
|
|
|
|
|
// TODO: Optimize this, for example by intersecting scanlines with the Voronoi polygons.
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
for( size_t x=0; x<m_TilesPerSide; x++ )
|
2006-07-09 00:40:01 +02:00
|
|
|
{
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
for( size_t z=0; z<m_TilesPerSide; z++ )
|
2006-07-09 00:40:01 +02:00
|
|
|
{
|
|
|
|
CVector2D tileLoc( (x+0.5f) * CELL_SIZE, (z+0.5f) * CELL_SIZE );
|
|
|
|
float bestSquareDist = 1e20f;
|
|
|
|
for( size_t i=0; i<centres.size(); i++ )
|
|
|
|
{
|
|
|
|
CVector2D centreLoc( centres[i]->m_position.X, centres[i]->m_position.Z );
|
|
|
|
float squareDist = (centreLoc - tileLoc).length2();
|
|
|
|
if( squareDist < bestSquareDist )
|
|
|
|
{
|
|
|
|
bestSquareDist = squareDist;
|
|
|
|
m_TerritoryMatrix[x][z] = m_Territories[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
# Added tool for viewing models and animations outside the game.
Atlas: Added ActorViewer. Moved GL canvas into separate class for shared
use. Disabled message-handling callback while blocked on the game, and
stopped creating dialog boxes inside the game thread in order to avoid
deadlocks (hopefully). Support multiple Views (for independent sets of
camera/update/render code). Recalculate territory boundaries when
necessary. Changed default list of animations to match those currently
used by actors.
# Tidied up more code.
Moved some more #includes out of .h files, to minimise unnecessary
compilation.
MathUtil: Deleted unused/unuseful macros (M_PI (use PI instead), M_PI_2
(use PI/2), MAX3, ABS (use abs)).
ObjectManager: Removed some ScEd-specific things.
Unit: Moved creation out of UnitManager, so units can be created without
adding to the manager. Changed CStr8 to the more conventional CStr.
app_hooks: Removed warning for setting multiple times.
win: Restored SEH catcher.
GameSetup, GameView: Removed RenderNoCull, because it doesn't seem to do
what it says it does ("force renderer to load everything") since we're
loading-on-demand most stuff and it doesn't seem especially useful since
we'd prefer to minimise loading times (but feel free to correct me if
I'm wrong). (And because it crashes when things need to be initialised
in a different order, so it's easier to remove than to understand and
fix it.)
PatchRData, Renderer: Work sensibly when there's no game (hence no LOS
manager, water, etc).
LOSManager: Use entity position instead of actor position when possible.
TerritoryManager: Allow delayed recalculations (so Atlas can issue lots
of move+recalculate commands per frame).
Cinematic: Non-pointer wxTimer, so it doesn't leak and doesn't have to
be deleted manually.
This was SVN commit r4261.
2006-08-28 19:36:42 +02:00
|
|
|
void CTerritoryManager::DelayedRecalculate()
|
|
|
|
{
|
|
|
|
// This is useful particularly for Atlas, which wants to recalculate
|
|
|
|
// the boundaries as you move an object around but which doesn't want
|
|
|
|
// to waste time recalculating multiple times per frame
|
|
|
|
m_DelayedRecalculate = true;
|
|
|
|
}
|
|
|
|
|
2006-07-09 00:40:01 +02:00
|
|
|
CTerritory* CTerritoryManager::GetTerritory(int x, int z)
|
|
|
|
{
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
debug_assert( (size_t) x < m_TilesPerSide && (size_t) z < m_TilesPerSide );
|
2006-07-09 00:40:01 +02:00
|
|
|
return m_TerritoryMatrix[x][z];
|
|
|
|
}
|
|
|
|
|
|
|
|
CTerritory* CTerritoryManager::GetTerritory(float x, float z)
|
|
|
|
{
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
ssize_t ix, iz;
|
2006-07-09 00:40:01 +02:00
|
|
|
CTerrain::CalcFromPosition(x, z, ix, iz);
|
2008-07-04 13:15:47 +02:00
|
|
|
return GetTerritory((int)ix, (int)iz);
|
2006-07-09 00:40:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the boundary points of a given territory into the given vector
|
|
|
|
void CTerritoryManager::CalculateBoundary( std::vector<CEntity*>& centres, size_t myIndex, std::vector<CVector2D>& boundary )
|
|
|
|
{
|
|
|
|
// Start with a boundary equal to the whole map
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
const size_t mapSize = m_TilesPerSide * CELL_SIZE;
|
2006-07-09 00:40:01 +02:00
|
|
|
boundary.push_back( CVector2D(0, 0) );
|
|
|
|
boundary.push_back( CVector2D(0, mapSize) );
|
|
|
|
boundary.push_back( CVector2D(mapSize, mapSize) );
|
|
|
|
boundary.push_back( CVector2D(mapSize, 0) );
|
|
|
|
|
|
|
|
// Clip this polygon against the perpendicular bisector between this centre and each other territory centre
|
|
|
|
CVector2D myPos( centres[myIndex]->m_position.X, centres[myIndex]->m_position.Z );
|
|
|
|
|
|
|
|
for( size_t i=0; i<centres.size(); i++ )
|
|
|
|
{
|
|
|
|
if( i != myIndex )
|
|
|
|
{
|
|
|
|
CVector2D itsPos( centres[i]->m_position.X, centres[i]->m_position.Z );
|
|
|
|
CVector2D midpoint = (myPos + itsPos) / 2.0f;
|
|
|
|
CVector2D normal = itsPos - myPos;
|
|
|
|
|
|
|
|
// Clip our polygon to the negative side of the half-space with normal "normal"
|
|
|
|
// containing point "midpoint", i.e. the side of the perpendicular bisector
|
|
|
|
// between myPos and itsPos that contains myPos. We do this by tracing around
|
|
|
|
// the polygon looking at each vertex to decide which ones to add as follows:
|
|
|
|
// - If a vertex is inside the half-space, take it.
|
|
|
|
// - If a vertex is inside but the next one is outside, also take the
|
|
|
|
// intersection of that edge with the perpendicular bisector.
|
|
|
|
// - If a vertex is outside but the next one is inside, take the
|
|
|
|
// intersection of that edge with the perpendicular bisector.
|
|
|
|
|
|
|
|
std::vector<CVector2D> newBoundary;
|
|
|
|
for( size_t j=0; j<boundary.size(); j++ )
|
|
|
|
{
|
|
|
|
CVector2D& pos = boundary[j];
|
2007-05-02 14:07:08 +02:00
|
|
|
float dot = (pos - midpoint).Dot(normal);
|
2006-07-09 00:40:01 +02:00
|
|
|
bool inside = dot < 0.0f;
|
|
|
|
|
|
|
|
size_t nextJ = (j+1) % boundary.size(); // index of next point
|
|
|
|
CVector2D& nextPos = boundary[nextJ];
|
2007-05-02 14:07:08 +02:00
|
|
|
float nextDot = (nextPos - midpoint).Dot(normal);
|
2006-07-09 00:40:01 +02:00
|
|
|
bool nextInside = nextDot < 0.0f;
|
|
|
|
|
|
|
|
if( inside )
|
|
|
|
{
|
|
|
|
newBoundary.push_back( pos );
|
|
|
|
|
|
|
|
if( !nextInside )
|
|
|
|
{
|
|
|
|
// Also add intersection of this line segment and the bisector
|
|
|
|
float t = nextDot / (-dot + nextDot);
|
|
|
|
newBoundary.push_back( pos * t + nextPos * (1.0f - t) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( nextInside )
|
|
|
|
{
|
|
|
|
// Add intersection of this line segment and the bisector
|
|
|
|
float t = nextDot / (-dot + nextDot);
|
|
|
|
newBoundary.push_back( pos * t + nextPos * (1.0f - t) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
boundary = newBoundary;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-05-02 14:07:08 +02:00
|
|
|
void CTerritoryManager::RenderTerritories()
|
# Added tool for viewing models and animations outside the game.
Atlas: Added ActorViewer. Moved GL canvas into separate class for shared
use. Disabled message-handling callback while blocked on the game, and
stopped creating dialog boxes inside the game thread in order to avoid
deadlocks (hopefully). Support multiple Views (for independent sets of
camera/update/render code). Recalculate territory boundaries when
necessary. Changed default list of animations to match those currently
used by actors.
# Tidied up more code.
Moved some more #includes out of .h files, to minimise unnecessary
compilation.
MathUtil: Deleted unused/unuseful macros (M_PI (use PI instead), M_PI_2
(use PI/2), MAX3, ABS (use abs)).
ObjectManager: Removed some ScEd-specific things.
Unit: Moved creation out of UnitManager, so units can be created without
adding to the manager. Changed CStr8 to the more conventional CStr.
app_hooks: Removed warning for setting multiple times.
win: Restored SEH catcher.
GameSetup, GameView: Removed RenderNoCull, because it doesn't seem to do
what it says it does ("force renderer to load everything") since we're
loading-on-demand most stuff and it doesn't seem especially useful since
we'd prefer to minimise loading times (but feel free to correct me if
I'm wrong). (And because it crashes when things need to be initialised
in a different order, so it's easier to remove than to understand and
fix it.)
PatchRData, Renderer: Work sensibly when there's no game (hence no LOS
manager, water, etc).
LOSManager: Use entity position instead of actor position when possible.
TerritoryManager: Allow delayed recalculations (so Atlas can issue lots
of move+recalculate commands per frame).
Cinematic: Non-pointer wxTimer, so it doesn't leak and doesn't have to
be deleted manually.
This was SVN commit r4261.
2006-08-28 19:36:42 +02:00
|
|
|
{
|
2006-09-07 00:26:27 +02:00
|
|
|
PROFILE( "render territories" );
|
|
|
|
|
# Added tool for viewing models and animations outside the game.
Atlas: Added ActorViewer. Moved GL canvas into separate class for shared
use. Disabled message-handling callback while blocked on the game, and
stopped creating dialog boxes inside the game thread in order to avoid
deadlocks (hopefully). Support multiple Views (for independent sets of
camera/update/render code). Recalculate territory boundaries when
necessary. Changed default list of animations to match those currently
used by actors.
# Tidied up more code.
Moved some more #includes out of .h files, to minimise unnecessary
compilation.
MathUtil: Deleted unused/unuseful macros (M_PI (use PI instead), M_PI_2
(use PI/2), MAX3, ABS (use abs)).
ObjectManager: Removed some ScEd-specific things.
Unit: Moved creation out of UnitManager, so units can be created without
adding to the manager. Changed CStr8 to the more conventional CStr.
app_hooks: Removed warning for setting multiple times.
win: Restored SEH catcher.
GameSetup, GameView: Removed RenderNoCull, because it doesn't seem to do
what it says it does ("force renderer to load everything") since we're
loading-on-demand most stuff and it doesn't seem especially useful since
we'd prefer to minimise loading times (but feel free to correct me if
I'm wrong). (And because it crashes when things need to be initialised
in a different order, so it's easier to remove than to understand and
fix it.)
PatchRData, Renderer: Work sensibly when there's no game (hence no LOS
manager, water, etc).
LOSManager: Use entity position instead of actor position when possible.
TerritoryManager: Allow delayed recalculations (so Atlas can issue lots
of move+recalculate commands per frame).
Cinematic: Non-pointer wxTimer, so it doesn't leak and doesn't have to
be deleted manually.
This was SVN commit r4261.
2006-08-28 19:36:42 +02:00
|
|
|
if (m_DelayedRecalculate)
|
|
|
|
{
|
|
|
|
Recalculate();
|
|
|
|
m_DelayedRecalculate = false;
|
|
|
|
}
|
|
|
|
|
2006-09-07 00:26:27 +02:00
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
2006-08-29 01:01:16 +02:00
|
|
|
glEnable(GL_LINE_SMOOTH);
|
|
|
|
glLineWidth(1.5f);
|
|
|
|
|
2006-09-01 01:56:12 +02:00
|
|
|
CLOSManager* losMgr = g_Game->GetWorld()->GetLOSManager();
|
2006-08-25 08:04:33 +02:00
|
|
|
CFrustum frustum = g_Game->GetView()->GetCamera()->GetFrustum();
|
|
|
|
std::vector<CTerritory*>::iterator terr=m_Territories.begin();
|
|
|
|
|
|
|
|
for ( ; terr != m_Territories.end(); ++terr )
|
|
|
|
{
|
2006-09-01 01:56:12 +02:00
|
|
|
float r, g, b;
|
|
|
|
|
2006-08-29 01:01:16 +02:00
|
|
|
if ( (*terr)->owner->GetPlayerID() == 0 )
|
|
|
|
{
|
|
|
|
// Use a dark gray for Gaia territories since white looks a bit weird
|
2006-09-01 01:56:12 +02:00
|
|
|
//glColor3f( 0.65f, 0.65f, 0.65f );
|
|
|
|
r = g = b = 0.65f;
|
2006-08-29 01:01:16 +02:00
|
|
|
}
|
|
|
|
else
|
2006-08-25 08:04:33 +02:00
|
|
|
{
|
2006-08-29 01:01:16 +02:00
|
|
|
// Use the player's colour
|
|
|
|
const SPlayerColour& col = (*terr)->owner->GetColour();
|
2006-09-01 01:56:12 +02:00
|
|
|
//glColor3f(col.r, col.g, col.b);
|
|
|
|
r = col.r;
|
|
|
|
g = col.g;
|
|
|
|
b = col.b;
|
2006-08-29 01:01:16 +02:00
|
|
|
}
|
|
|
|
|
2006-09-08 18:03:18 +02:00
|
|
|
for ( size_t edge=0; edge < (*terr)->boundary.size(); edge++ )
|
2006-08-29 01:01:16 +02:00
|
|
|
{
|
2006-09-08 18:03:18 +02:00
|
|
|
const std::vector<CVector3D>& coords = (*terr)->GetEdgeCoords(edge);
|
|
|
|
CVector3D start = coords[0];
|
|
|
|
CVector3D end = coords[coords.size() - 1];
|
2006-08-29 01:01:16 +02:00
|
|
|
|
|
|
|
if ( !frustum.DoesSegmentIntersect(start, end) )
|
2006-08-25 08:04:33 +02:00
|
|
|
continue;
|
|
|
|
|
2006-09-08 18:03:18 +02:00
|
|
|
glBegin( GL_LINE_STRIP );
|
2006-09-01 01:56:12 +02:00
|
|
|
|
2006-09-08 18:03:18 +02:00
|
|
|
for( size_t i=0; i<coords.size(); i++ )
|
2006-08-25 08:04:33 +02:00
|
|
|
{
|
2006-09-08 18:03:18 +02:00
|
|
|
float losScale = 0.0f;
|
|
|
|
ELOSStatus los = losMgr->GetStatus(coords[i].X, coords[i].Z, g_Game->GetLocalPlayer());
|
|
|
|
if( los & LOS_VISIBLE ) losScale = 1.0f;
|
|
|
|
else if( los & LOS_EXPLORED ) losScale = 0.7f;
|
|
|
|
glColor3f( r*losScale, g*losScale, b*losScale );
|
|
|
|
glVertex3f( coords[i].X, coords[i].Y, coords[i].Z );
|
2006-08-25 08:04:33 +02:00
|
|
|
}
|
2006-09-01 01:56:12 +02:00
|
|
|
|
2006-08-25 21:31:01 +02:00
|
|
|
glEnd();
|
2006-08-25 08:04:33 +02:00
|
|
|
}
|
|
|
|
}
|
2006-08-29 01:01:16 +02:00
|
|
|
|
2006-09-07 00:26:27 +02:00
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
glDisable(GL_BLEND);
|
2006-08-25 08:04:33 +02:00
|
|
|
glDisable(GL_LINE_SMOOTH);
|
2006-08-25 21:31:01 +02:00
|
|
|
glLineWidth(1.0f);
|
2006-09-07 00:26:27 +02:00
|
|
|
glColor4f(1,1,1,1);
|
2006-09-08 18:03:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<CVector3D>& CTerritory::GetEdgeCoords(size_t edge)
|
|
|
|
{
|
|
|
|
if ( edgeCoords.size() == 0 )
|
|
|
|
{
|
|
|
|
// Edge coords have not been calculated - calculate them now
|
|
|
|
edgeCoords.resize( boundary.size() );
|
|
|
|
|
|
|
|
const CTerrain* pTerrain = g_Game->GetWorld()->GetTerrain();
|
|
|
|
|
|
|
|
// Tweak the boundary to shift all edges "inwards" by 0.3 units towards the territory's centre,
|
|
|
|
// so that boundaries for adjacent territories don't overlap
|
|
|
|
std::vector<CVector2D> tweakedBoundary = boundary;
|
|
|
|
for ( size_t i=0; i<boundary.size(); i++ )
|
|
|
|
{
|
|
|
|
size_t prevI = (i+boundary.size()-1) % boundary.size();
|
|
|
|
size_t nextI = (i+1) % boundary.size();
|
|
|
|
|
|
|
|
// Figure out the direction perpendicular to each of the two edges that meet at this point.
|
2007-05-02 14:07:08 +02:00
|
|
|
CVector2D dir1 = (boundary[i]-boundary[prevI]).beta().Normalize();
|
|
|
|
CVector2D dir2 = (boundary[nextI]-boundary[i]).beta().Normalize();
|
2006-09-08 18:03:18 +02:00
|
|
|
|
|
|
|
// If you draw a picture of what our point looks like and what the two lines 0.3 units
|
|
|
|
// away from it look like, and draw a line between our point and that one as well as
|
|
|
|
// drop perpendicular lines from it to the original edges, you get this formula for the
|
|
|
|
// length and direction we have to be moved.
|
2007-05-02 14:07:08 +02:00
|
|
|
float angle = acosf(dir1.Dot(dir2));
|
|
|
|
tweakedBoundary[i] += (dir1 + dir2).Normalize() * 0.3f / cosf(angle/2);
|
2006-09-08 18:03:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the heights at points TERRITORY_PRECISION_STEP apart on our edges
|
|
|
|
// and store the final vertices in edgeCoords.
|
|
|
|
for ( size_t e=0; e<boundary.size(); e++ )
|
|
|
|
{
|
|
|
|
std::vector<CVector3D>& coords = edgeCoords[e];
|
|
|
|
|
|
|
|
CVector2D start = tweakedBoundary[e];
|
|
|
|
CVector2D end = tweakedBoundary[(e+1) % boundary.size()];
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
float iterf = (end - start).Length() / TERRITORY_PRECISION_STEP;
|
2006-09-08 18:03:18 +02:00
|
|
|
for ( float i=0; i < iterf; i += TERRITORY_PRECISION_STEP )
|
|
|
|
{
|
|
|
|
CVector2D pos = Interpolate( start, end, i/iterf );
|
2007-05-02 14:07:08 +02:00
|
|
|
coords.push_back( CVector3D( pos.x, pTerrain->GetExactGroundLevel(pos)+0.25f, pos.y ) );
|
2006-09-08 18:03:18 +02:00
|
|
|
}
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
coords.push_back( CVector3D( end.x, pTerrain->GetExactGroundLevel(end)+0.25f, end.y ) );
|
2006-09-08 18:03:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return edgeCoords[edge];
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTerritory::ClearEdgeCache()
|
|
|
|
{
|
|
|
|
edgeCoords.clear();
|
|
|
|
edgeCoords.resize( boundary.size() );
|
|
|
|
}
|