2010-05-20 19:58:37 +02:00
|
|
|
/* Copyright (C) 2010 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2004-07-27 23:00:53 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2005-05-18 07:32:09 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "graphics/Model.h"
|
2006-06-09 18:44:16 +02:00
|
|
|
#include "graphics/Terrain.h"
|
|
|
|
#include "graphics/Unit.h"
|
|
|
|
#include "graphics/UnitManager.h"
|
2007-01-24 21:17:28 +01:00
|
|
|
#include "maths/MathUtil.h"
|
|
|
|
#include "network/NetMessage.h"
|
2006-06-09 18:44:16 +02:00
|
|
|
#include "ps/CLogger.h"
|
|
|
|
#include "ps/Game.h"
|
|
|
|
#include "ps/GameAttributes.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "ps/Loader.h"
|
|
|
|
#include "ps/LoaderThunks.h"
|
2006-06-09 18:44:16 +02:00
|
|
|
#include "ps/Profile.h"
|
2006-01-22 00:27:42 +01:00
|
|
|
#include "renderer/Renderer.h"
|
|
|
|
#include "renderer/WaterManager.h"
|
2006-06-09 18:44:16 +02:00
|
|
|
#include "simulation/Entity.h"
|
2007-01-24 21:17:28 +01:00
|
|
|
#include "simulation/EntityFormation.h"
|
|
|
|
#include "simulation/EntityManager.h"
|
|
|
|
#include "simulation/EntityTemplateCollection.h"
|
2006-07-09 00:40:01 +02:00
|
|
|
#include "simulation/LOSManager.h"
|
2007-01-24 21:17:28 +01:00
|
|
|
#include "simulation/Scheduler.h"
|
|
|
|
#include "simulation/Simulation.h"
|
2006-07-09 00:40:01 +02:00
|
|
|
#include "simulation/TerritoryManager.h"
|
2007-01-29 23:21:25 +01:00
|
|
|
#include "simulation/TriggerManager.h"
|
2004-07-27 23:00:53 +02:00
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
#define LOG_CATEGORY L"simulation"
|
|
|
|
|
2006-08-07 22:38:23 +02:00
|
|
|
CSimulation::CSimulation(CGame *pGame):
|
2004-07-27 23:00:53 +02:00
|
|
|
m_pGame(pGame),
|
|
|
|
m_pWorld(pGame->GetWorld()),
|
2007-10-23 08:52:23 +02:00
|
|
|
m_DeltaTime(0),
|
|
|
|
m_Time(0)
|
2006-07-30 02:24:04 +02:00
|
|
|
{
|
|
|
|
}
|
2004-07-27 23:00:53 +02:00
|
|
|
|
2004-08-16 17:19:17 +02:00
|
|
|
CSimulation::~CSimulation()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-10-10 05:35:32 +02:00
|
|
|
int CSimulation::Initialize(CGameAttributes* pAttribs)
|
2004-07-31 17:57:18 +02:00
|
|
|
{
|
2006-08-07 22:38:23 +02:00
|
|
|
m_Random.seed(0); // TODO: Store a random seed in CGameAttributes and synchronize it accross the network
|
|
|
|
|
2006-06-10 05:05:16 +02:00
|
|
|
// Call the game startup script
|
|
|
|
// TODO: Maybe don't do this if we're in Atlas
|
2006-06-30 00:52:50 +02:00
|
|
|
// [2006-06-26 20ms]
|
2009-11-03 22:46:35 +01:00
|
|
|
g_ScriptingHost.RunScript( L"scripts/game_startup.js" );
|
2006-06-10 05:05:16 +02:00
|
|
|
|
2006-06-30 00:52:50 +02:00
|
|
|
// [2006-06-26 3647ms]
|
2010-05-20 20:09:23 +02:00
|
|
|
// g_EntityManager.m_screenshotMode = pAttribs->m_ScreenshotMode;
|
|
|
|
// g_EntityManager.InitializeAll();
|
2005-10-09 05:43:03 +02:00
|
|
|
|
2006-06-30 00:52:50 +02:00
|
|
|
// [2006-06-26: 61ms]
|
2008-07-19 02:36:42 +02:00
|
|
|
m_pWorld->GetLOSManager()->Initialize((ELOSSetting)pAttribs->m_LOSSetting, pAttribs->m_FogOfWar);
|
2005-10-09 05:43:03 +02:00
|
|
|
|
2006-07-09 00:40:01 +02:00
|
|
|
m_pWorld->GetTerritoryManager()->Initialize();
|
|
|
|
|
2005-05-03 23:36:57 +02:00
|
|
|
return 0;
|
2004-07-31 17:57:18 +02:00
|
|
|
}
|
2004-07-27 23:00:53 +02:00
|
|
|
|
2005-03-22 03:17:55 +01:00
|
|
|
|
|
|
|
void CSimulation::RegisterInit(CGameAttributes *pAttribs)
|
|
|
|
{
|
2006-03-18 22:25:03 +01:00
|
|
|
RegMemFun1(this, &CSimulation::Initialize, pAttribs, L"CSimulation", 3900);
|
2005-03-22 03:17:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-05-20 19:58:37 +02:00
|
|
|
bool CSimulation::Update(double UNUSED(frameTime))
|
2004-07-27 23:00:53 +02:00
|
|
|
{
|
2007-01-24 21:17:28 +01:00
|
|
|
bool ok = true;
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2007-02-10 04:09:52 +01:00
|
|
|
void CSimulation::DiscardMissedUpdates()
|
|
|
|
{
|
|
|
|
if (m_DeltaTime > 0.0)
|
|
|
|
m_DeltaTime = 0.0;
|
|
|
|
}
|
|
|
|
|
2010-05-20 19:58:37 +02:00
|
|
|
void CSimulation::Interpolate(double UNUSED(frameTime))
|
2007-01-24 21:17:28 +01:00
|
|
|
{
|
2004-07-27 23:00:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSimulation::Interpolate(double frameTime, double offset)
|
|
|
|
{
|
2007-01-24 21:17:28 +01:00
|
|
|
PROFILE( "simulation interpolation" );
|
|
|
|
|
|
|
|
const std::vector<CUnit*>& units = m_pWorld->GetUnitManager().GetUnits();
|
|
|
|
for (size_t i = 0; i < units.size(); ++i)
|
2007-02-10 04:09:52 +01:00
|
|
|
units[i]->UpdateModel((float)frameTime);
|
2004-07-27 23:00:53 +02:00
|
|
|
|
2010-05-20 20:09:23 +02:00
|
|
|
// g_EntityManager.InterpolateAll(offset);
|
2004-07-27 23:00:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSimulation::Simulate()
|
|
|
|
{
|
2004-08-16 17:19:17 +02:00
|
|
|
}
|
|
|
|
|
2005-05-18 07:32:09 +02:00
|
|
|
// Location randomizer, for group orders...
|
|
|
|
// Having the group turn up at the destination with /some/ sort of cohesion is
|
|
|
|
// good but tasking them all to the exact same point will leave them brawling
|
|
|
|
// for it at the other end (it shouldn't, but the PASAP pathfinder is too
|
|
|
|
// simplistic)
|
|
|
|
|
|
|
|
// Task them all to a point within a radius of the target, radius depends upon
|
|
|
|
// the number of units in the group.
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
void RandomizeLocations(const CEntityOrder& order, const std::vector<HEntity> &entities, bool isQueued)
|
2004-08-16 17:19:17 +02:00
|
|
|
{
|
2005-05-18 07:32:09 +02:00
|
|
|
float radius = 2.0f * sqrt( (float)entities.size() - 1 );
|
|
|
|
|
2008-11-15 23:38:04 +01:00
|
|
|
CSimulation* sim = g_Game->GetSimulation();
|
|
|
|
|
2008-11-22 17:50:03 +01:00
|
|
|
for (std::vector<HEntity>::const_iterator it = entities.begin(); it < entities.end(); it++)
|
2005-05-18 07:32:09 +02:00
|
|
|
{
|
2008-11-22 17:50:03 +01:00
|
|
|
if(!*it)
|
|
|
|
continue;
|
|
|
|
|
2005-05-18 07:32:09 +02:00
|
|
|
float _x, _y;
|
|
|
|
CEntityOrder randomizedOrder = order;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2006-08-17 23:09:46 +02:00
|
|
|
_x = sim->RandFloat() * 2.0f - 1.0f;
|
|
|
|
_y = sim->RandFloat() * 2.0f - 1.0f;
|
2005-05-18 07:32:09 +02:00
|
|
|
}
|
|
|
|
while( ( _x * _x ) + ( _y * _y ) > 1.0f );
|
|
|
|
|
2006-12-21 15:57:13 +01:00
|
|
|
randomizedOrder.m_target_location.x += _x * radius;
|
|
|
|
randomizedOrder.m_target_location.y += _y * radius;
|
2005-05-18 07:32:09 +02:00
|
|
|
|
|
|
|
// Clamp it to within the map, just in case.
|
|
|
|
float mapsize = (float)g_Game->GetWorld()->GetTerrain()->GetVerticesPerSide() * CELL_SIZE;
|
2006-12-21 15:57:13 +01:00
|
|
|
randomizedOrder.m_target_location.x = clamp(randomizedOrder.m_target_location.x, 0.0f, mapsize);
|
|
|
|
randomizedOrder.m_target_location.y = clamp(randomizedOrder.m_target_location.y, 0.0f, mapsize);
|
2005-05-18 07:32:09 +02:00
|
|
|
|
2006-08-25 06:24:06 +02:00
|
|
|
if( !isQueued )
|
2007-05-02 14:07:08 +02:00
|
|
|
(*it)->ClearOrders();
|
2005-05-18 07:32:09 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
(*it)->PushOrder( randomizedOrder );
|
2005-05-18 07:32:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
void FormationLocations(const CEntityOrder& order, const std::vector<HEntity> &entities, bool isQueued)
|
2006-03-31 05:30:34 +02:00
|
|
|
{
|
2008-11-22 17:50:03 +01:00
|
|
|
const CVector2D upvec(0.0f, 1.0f);
|
|
|
|
const CEntityFormation* formation = entities.front()->GetFormation();
|
2006-03-31 05:30:34 +02:00
|
|
|
|
2008-11-22 17:50:03 +01:00
|
|
|
for (std::vector<HEntity>::const_iterator it = entities.begin(); it != entities.end(); it++)
|
2006-03-31 05:30:34 +02:00
|
|
|
{
|
2008-11-22 17:50:03 +01:00
|
|
|
if(!*it)
|
|
|
|
continue;
|
|
|
|
|
2006-03-31 05:30:34 +02:00
|
|
|
CEntityOrder orderCopy = order;
|
2006-12-21 15:57:13 +01:00
|
|
|
CVector2D posDelta = orderCopy.m_target_location - formation->GetPosition();
|
2006-03-31 05:30:34 +02:00
|
|
|
CVector2D formDelta = formation->GetSlotPosition( (*it)->m_formationSlot );
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
posDelta = posDelta.Normalize();
|
2006-03-31 05:30:34 +02:00
|
|
|
//Rotate the slot position's offset vector according to the rotation of posDelta.
|
|
|
|
CVector2D rotDelta;
|
2007-05-02 14:07:08 +02:00
|
|
|
float deltaCos = posDelta.Dot(upvec);
|
2006-03-31 05:30:34 +02:00
|
|
|
float deltaSin = sinf( acosf(deltaCos) );
|
|
|
|
rotDelta.x = formDelta.x * deltaCos - formDelta.y * deltaSin;
|
|
|
|
rotDelta.y = formDelta.x * deltaSin + formDelta.y * deltaCos;
|
|
|
|
|
2006-12-21 15:57:13 +01:00
|
|
|
orderCopy.m_target_location += rotDelta;
|
2006-03-31 05:30:34 +02:00
|
|
|
|
|
|
|
// Clamp it to within the map, just in case.
|
|
|
|
float mapsize = (float)g_Game->GetWorld()->GetTerrain()->GetVerticesPerSide() * CELL_SIZE;
|
2006-12-21 15:57:13 +01:00
|
|
|
orderCopy.m_target_location.x = clamp(orderCopy.m_target_location.x, 0.0f, mapsize);
|
|
|
|
orderCopy.m_target_location.y = clamp(orderCopy.m_target_location.y, 0.0f, mapsize);
|
2006-03-31 05:30:34 +02:00
|
|
|
|
2006-08-25 06:24:06 +02:00
|
|
|
if( !isQueued )
|
2007-05-02 14:07:08 +02:00
|
|
|
(*it)->ClearOrders();
|
2006-03-31 05:30:34 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
(*it)->PushOrder( orderCopy );
|
2006-03-31 05:30:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
void QueueOrder(const CEntityOrder& order, const std::vector<HEntity> &entities, bool isQueued)
|
2005-05-18 07:32:09 +02:00
|
|
|
{
|
2008-11-22 17:50:03 +01:00
|
|
|
for (std::vector<HEntity>::const_iterator it = entities.begin(); it < entities.end(); it++)
|
2005-05-18 07:32:09 +02:00
|
|
|
{
|
2008-11-22 17:50:03 +01:00
|
|
|
if (!*it)
|
|
|
|
continue;
|
2005-05-18 07:32:09 +02:00
|
|
|
|
2008-11-22 17:50:03 +01:00
|
|
|
if( !isQueued )
|
|
|
|
(*it)->ClearOrders();
|
|
|
|
|
|
|
|
(*it)->PushOrder( order );
|
2005-05-18 07:32:09 +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
|
|
|
size_t CSimulation::TranslateMessage(CNetMessage* pMsg, size_t clientMask, void* UNUSED(userdata))
|
2005-05-18 07:32:09 +02:00
|
|
|
{
|
2004-08-16 17:19:17 +02:00
|
|
|
return clientMask;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
size_t CSimulation::GetMessageMask(CNetMessage* UNUSED(pMsg), size_t UNUSED(oldMask), void* UNUSED(userdata))
|
2004-08-16 17:19:17 +02:00
|
|
|
{
|
2005-08-09 17:55:44 +02:00
|
|
|
//CSimulation *pSimulation=(CSimulation *)userdata;
|
|
|
|
|
2004-08-16 17:19:17 +02:00
|
|
|
// Pending a complete visibility/minimal-update implementation, we'll
|
|
|
|
// simply select the first 32 connected clients ;-)
|
2007-01-01 22:25:47 +01:00
|
|
|
return 0xFFFFFFFF;
|
2004-08-16 17:19:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSimulation::QueueLocalCommand(CNetMessage *pMsg)
|
|
|
|
{
|
2004-07-27 23:00:53 +02:00
|
|
|
}
|
2006-08-07 22:38:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Get a random integer between 0 and maxVal-1 from the simulation's random number generator
|
|
|
|
int CSimulation::RandInt(int maxVal)
|
|
|
|
{
|
|
|
|
boost::uniform_smallint<int> distr(0, maxVal-1);
|
|
|
|
return distr(m_Random);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a random float in [0, 1) from the simulation's random number generator
|
|
|
|
float CSimulation::RandFloat()
|
|
|
|
{
|
2006-08-17 23:09:46 +02:00
|
|
|
// Cannot use uniform_01 here because it is not a real distribution, but rather an
|
|
|
|
// utility class that makes a copy of the generator, and therefore it would repeatedly
|
|
|
|
// return the same values because it never modifies our copy of the generator.
|
|
|
|
boost::uniform_real<float> distr(0.0f, 1.0f);
|
|
|
|
return distr(m_Random);
|
2006-08-07 22:38:23 +02:00
|
|
|
}
|