1
0
forked from 0ad/0ad
0ad/source/simulation/TerritoryManager.h
janwas c0ed950657 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 18:48:32 +00:00

66 lines
1.8 KiB
C++

// TerritoryManager.h
//
// Calculates territory boundaries and maintains territory data.
//
// Usage:
#ifndef INCLUDED_TERRITORYMANAGER
#define INCLUDED_TERRITORYMANAGER
#include "ps/Vector2D.h"
#include "maths/Vector3D.h"
#include "EntityHandles.h"
class CUnit;
class CPlayer;
const float TERRITORY_PRECISION_STEP = 1.0f;
class CTerritory
{
public:
CPlayer* owner; // owner of the territory, or Gaia for none
HEntity centre; // centre object of this territory
std::vector<CVector2D> boundary; // boundary polygon, in map coordinates
private:
// cached coordinates for the polygon's edge segments (conformed to the terrain)
std::vector<std::vector<CVector3D> > edgeCoords;
public:
CTerritory(CPlayer* owner_, HEntity centre_, std::vector<CVector2D> boundary_)
: owner(owner_), centre(centre_), boundary(boundary_) {}
const std::vector<CVector3D>& GetEdgeCoords(size_t edge);
void ClearEdgeCache();
};
class CTerritoryManager
{
std::vector<CTerritory*> m_Territories;
CTerritory*** m_TerritoryMatrix; // m_TerritoryMatrix[x][z] points to the territory for tile (x, z)
size_t m_TilesPerSide;
bool m_DelayedRecalculate;
public:
CTerritoryManager();
~CTerritoryManager();
void Initialize(); // initialize, called after the game is fully loaded
void Recalculate(); // recalculate the territory boundaries
void DelayedRecalculate(); // recalculate the territory boundaries when next rendered
void RenderTerritories();
CTerritory* GetTerritory(int x, int z); // get the territory to which the given tile belongs
CTerritory* GetTerritory(float x, float z); // get the territory to which the given world-space point belongs
std::vector<CTerritory*>& GetTerritories() { return m_Territories; }
private:
void CalculateBoundary( std::vector<CEntity*>& centres, size_t index, std::vector<CVector2D>& boundary );
};
#endif