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/>.
|
|
|
|
*/
|
|
|
|
|
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/Projectile.h"
|
|
|
|
#include "simulation/Scheduler.h"
|
|
|
|
#include "simulation/Simulation.h"
|
2006-07-09 00:40:01 +02:00
|
|
|
#include "simulation/TerritoryManager.h"
|
2007-01-24 21:17:28 +01:00
|
|
|
#include "simulation/TurnManager.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()),
|
2004-08-16 17:19:17 +02:00
|
|
|
m_pTurnManager((g_SinglePlayerTurnManager=new CSinglePlayerTurnManager())),
|
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()
|
|
|
|
{
|
2008-06-16 20:19:35 +02:00
|
|
|
delete g_SinglePlayerTurnManager;
|
2004-08-16 17:19:17 +02:00
|
|
|
g_SinglePlayerTurnManager=NULL;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2004-08-16 17:19:17 +02:00
|
|
|
m_pTurnManager->Initialize(m_pGame->GetNumPlayers());
|
|
|
|
|
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]
|
2006-10-08 19:39:46 +02:00
|
|
|
g_EntityManager.m_screenshotMode = pAttribs->m_ScreenshotMode;
|
2004-11-11 08:09:32 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-01-24 21:17:28 +01:00
|
|
|
bool CSimulation::Update(double frameTime)
|
2004-07-27 23:00:53 +02:00
|
|
|
{
|
2007-01-24 21:17:28 +01:00
|
|
|
bool ok = true;
|
|
|
|
|
2004-07-27 23:00:53 +02:00
|
|
|
m_DeltaTime += frameTime;
|
2004-08-16 17:19:17 +02:00
|
|
|
|
2007-01-24 21:17:28 +01:00
|
|
|
if (m_DeltaTime >= 0.0)
|
2004-07-27 23:00:53 +02:00
|
|
|
{
|
|
|
|
// A new simulation frame is required.
|
2007-11-18 10:09:06 +01:00
|
|
|
if (m_pTurnManager->NewTurnReady())
|
|
|
|
{
|
|
|
|
PROFILE( "simulation turn" );
|
|
|
|
Simulate();
|
|
|
|
double turnLength = m_pTurnManager->GetTurnLength() / 1000.0;
|
|
|
|
m_DeltaTime -= turnLength;
|
|
|
|
if (m_DeltaTime >= 0.0)
|
|
|
|
{
|
|
|
|
// The desired sim frame rate can't be achieved - we're being called
|
|
|
|
// with average(frameTime) > turnLength.
|
|
|
|
// Let the caller know we can't go fast enough - they should try
|
2008-06-16 20:19:35 +02:00
|
|
|
// cutting down on Interpolate and rendering, and call us a few times
|
2007-11-18 10:09:06 +01:00
|
|
|
// with frameTime == 0 to give us a chance to catch up.
|
|
|
|
ok = false;
|
2009-11-03 22:46:35 +01:00
|
|
|
debug_printf(L"WARNING: missing a simulation turn due to low FPS\n");
|
2007-11-18 10:09:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2004-07-27 23:00:53 +02:00
|
|
|
{
|
2007-11-18 10:09:06 +01:00
|
|
|
// The network is lagging behind the simulation rate.
|
|
|
|
// Set delta time back to zero so we don't jump into the middle
|
|
|
|
// of the next simulation frame when we get the next turn.
|
|
|
|
// This creates "lag" on the client rather than just jumpiness.
|
|
|
|
m_DeltaTime = 0;
|
2004-07-27 23:00:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-24 21:17:28 +01:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2007-02-10 04:09:52 +01:00
|
|
|
void CSimulation::DiscardMissedUpdates()
|
|
|
|
{
|
|
|
|
if (m_DeltaTime > 0.0)
|
|
|
|
m_DeltaTime = 0.0;
|
|
|
|
}
|
|
|
|
|
2007-01-24 21:17:28 +01:00
|
|
|
void CSimulation::Interpolate(double frameTime)
|
|
|
|
{
|
|
|
|
double turnLength = m_pTurnManager->GetTurnLength()/1000.0;
|
|
|
|
|
|
|
|
// 'offset' should be how far we are between the previous and next
|
|
|
|
// simulation frames.
|
|
|
|
// m_DeltaTime/turnLength will usually be between -1 and 0, indicating
|
|
|
|
// the time until the next frame, so we can use that easily.
|
|
|
|
// If the simulation is going too slowly and hasn't been giving a chance
|
2007-02-10 04:09:52 +01:00
|
|
|
// to catch up before Interpolate is called, then m_DeltaTime > 0, so we'll
|
|
|
|
// just clamp it to offset=1, which is alright.
|
|
|
|
Interpolate(frameTime, clamp(m_DeltaTime / turnLength + 1.0, 0.0, 1.0));
|
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
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
g_EntityManager.InterpolateAll(offset);
|
2007-02-10 04:09:52 +01:00
|
|
|
m_pWorld->GetProjectileManager().InterpolateAll(offset);
|
2006-01-22 00:27:42 +01:00
|
|
|
g_Renderer.GetWaterManager()->m_WaterTexTimer += frameTime;
|
2004-07-27 23:00:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSimulation::Simulate()
|
|
|
|
{
|
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
|
|
|
int time = m_pTurnManager->GetTurnLength();
|
2007-10-23 08:52:23 +02:00
|
|
|
|
|
|
|
m_Time += time / 1000.0f;
|
2008-06-16 20:19:35 +02:00
|
|
|
#if defined(DEBUG_SYNCHRONIZATION)
|
2009-11-03 22:46:35 +01:00
|
|
|
debug_printf(L"Simulation turn: %.3lf\n", m_Time);
|
2007-11-18 10:09:06 +01:00
|
|
|
#endif
|
2007-02-10 04:09:52 +01:00
|
|
|
|
2005-03-30 18:14:19 +02:00
|
|
|
PROFILE_START( "scheduler tick" );
|
2007-05-02 14:07:08 +02:00
|
|
|
g_Scheduler.Update(time);
|
2005-03-30 18:14:19 +02:00
|
|
|
PROFILE_END( "scheduler tick" );
|
|
|
|
PROFILE_START( "entity updates" );
|
2007-05-02 14:07:08 +02:00
|
|
|
g_EntityManager.UpdateAll(time);
|
2005-03-30 18:14:19 +02:00
|
|
|
PROFILE_END( "entity updates" );
|
2004-08-16 17:19:17 +02:00
|
|
|
|
2005-05-10 09:13:25 +02:00
|
|
|
PROFILE_START( "projectile updates" );
|
2007-02-10 04:09:52 +01:00
|
|
|
m_pWorld->GetProjectileManager().UpdateAll(time);
|
2005-05-10 09:13:25 +02:00
|
|
|
PROFILE_END( "projectile updates" );
|
|
|
|
|
2005-10-09 05:43:03 +02:00
|
|
|
PROFILE_START( "los update" );
|
|
|
|
m_pWorld->GetLOSManager()->Update();
|
|
|
|
PROFILE_END( "los update" );
|
|
|
|
|
2007-01-29 23:21:25 +01:00
|
|
|
PROFILE_START("trigger update");
|
2007-02-10 04:09:52 +01:00
|
|
|
g_TriggerManager.Update(time);
|
2007-01-29 23:21:25 +01:00
|
|
|
PROFILE_END("trigger udpate");
|
|
|
|
|
2005-03-30 18:14:19 +02:00
|
|
|
PROFILE_START( "turn manager update" );
|
2004-08-16 17:19:17 +02:00
|
|
|
m_pTurnManager->NewTurn();
|
|
|
|
m_pTurnManager->IterateBatch(0, TranslateMessage, this);
|
2005-03-30 18:14:19 +02:00
|
|
|
PROFILE_END( "turn manager update" );
|
2007-11-18 10:09:06 +01:00
|
|
|
|
2008-06-16 20:19:35 +02:00
|
|
|
#if defined(DEBUG_SYNCHRONIZATION)
|
2009-11-03 22:46:35 +01:00
|
|
|
debug_printf(L"End turn\n", m_Time);
|
2007-11-18 10:09:06 +01:00
|
|
|
#endif
|
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
|
|
|
{
|
|
|
|
CEntityOrder order;
|
2006-08-25 06:24:06 +02:00
|
|
|
bool isQueued = true;
|
2005-05-18 07:32:09 +02:00
|
|
|
|
2007-01-27 03:36:33 +01:00
|
|
|
#define ENTITY_POSITION(_msg, _order) \
|
|
|
|
do { \
|
2005-05-18 07:32:09 +02:00
|
|
|
_msg *msg=(_msg *)pMsg; \
|
2006-08-25 06:24:06 +02:00
|
|
|
isQueued = msg->m_IsQueued != 0; \
|
2005-05-18 07:32:09 +02:00
|
|
|
order.m_type=CEntityOrder::_order; \
|
2006-12-21 15:57:13 +01:00
|
|
|
order.m_target_location.x=(float)msg->m_TargetX; \
|
|
|
|
order.m_target_location.y=(float)msg->m_TargetY; \
|
2006-08-25 06:24:06 +02:00
|
|
|
RandomizeLocations(order, msg->m_Entities, isQueued); \
|
2005-05-18 07:32:09 +02:00
|
|
|
} while(0)
|
2007-01-27 03:36:33 +01:00
|
|
|
#define ENTITY_POSITION_FORM(_msg, _order) \
|
|
|
|
do { \
|
2006-03-31 05:30:34 +02:00
|
|
|
_msg *msg=(_msg *)pMsg; \
|
2006-08-25 06:24:06 +02:00
|
|
|
isQueued = msg->m_IsQueued != 0; \
|
2006-03-31 05:30:34 +02:00
|
|
|
order.m_type=CEntityOrder::_order; \
|
2006-12-21 15:57:13 +01:00
|
|
|
order.m_target_location.x=(float)msg->m_TargetX; \
|
|
|
|
order.m_target_location.y=(float)msg->m_TargetY; \
|
2006-08-25 06:24:06 +02:00
|
|
|
FormationLocations(order, msg->m_Entities, isQueued); \
|
2006-03-31 05:30:34 +02:00
|
|
|
} while(0)
|
2007-01-27 03:36:33 +01:00
|
|
|
#define ENTITY_ENTITY_INT(_msg, _order) \
|
|
|
|
do { \
|
2005-12-29 09:42:44 +01:00
|
|
|
_msg *msg=(_msg *)pMsg; \
|
2006-08-25 06:24:06 +02:00
|
|
|
isQueued = msg->m_IsQueued != 0; \
|
2005-12-29 09:42:44 +01:00
|
|
|
order.m_type=CEntityOrder::_order; \
|
2006-12-21 15:57:13 +01:00
|
|
|
order.m_target_entity=msg->m_Target; \
|
|
|
|
order.m_action=msg->m_Action; \
|
2006-08-25 06:24:06 +02:00
|
|
|
QueueOrder(order, msg->m_Entities, isQueued); \
|
2005-12-29 09:42:44 +01:00
|
|
|
} while(0)
|
2008-07-13 08:24:50 +02:00
|
|
|
#define ENTITY_ENTITY_INT_BOOL(_msg, _order) \
|
|
|
|
do { \
|
|
|
|
_msg *msg=(_msg *)pMsg; \
|
|
|
|
isQueued = msg->m_IsQueued != 0; \
|
|
|
|
order.m_type=CEntityOrder::_order; \
|
|
|
|
order.m_target_entity=msg->m_Target; \
|
|
|
|
order.m_action=msg->m_Action; \
|
|
|
|
order.m_run=msg->m_Run != 0; \
|
|
|
|
QueueOrder(order, msg->m_Entities, isQueued); \
|
|
|
|
} while(0)
|
2007-01-27 03:36:33 +01:00
|
|
|
#define ENTITY_INT_STRING(_msg, _order) \
|
|
|
|
do { \
|
2006-02-22 23:45:16 +01:00
|
|
|
_msg *msg=(_msg *)pMsg; \
|
2006-08-25 06:24:06 +02:00
|
|
|
isQueued = msg->m_IsQueued != 0; \
|
2006-02-22 23:45:16 +01:00
|
|
|
order.m_type=CEntityOrder::_order; \
|
2008-07-24 07:25:14 +02:00
|
|
|
order.m_name=msg->m_Name; \
|
2006-12-21 15:57:13 +01:00
|
|
|
order.m_produce_type=msg->m_Type; \
|
2006-08-25 06:24:06 +02:00
|
|
|
QueueOrder(order, msg->m_Entities, isQueued); \
|
2006-02-22 23:45:16 +01:00
|
|
|
} while(0)
|
2008-07-24 07:25:14 +02:00
|
|
|
#define ENTITY_STRING(_msg, _order) \
|
|
|
|
do { \
|
|
|
|
_msg *msg=(_msg *)pMsg; \
|
|
|
|
isQueued = msg->m_IsQueued != 0; \
|
|
|
|
order.m_type=CEntityOrder::_order; \
|
|
|
|
order.m_name=msg->m_Stance; \
|
|
|
|
QueueOrder(order, msg->m_Entities, isQueued); \
|
|
|
|
} while(0)
|
2005-05-18 07:32:09 +02:00
|
|
|
|
2004-08-16 17:19:17 +02:00
|
|
|
switch (pMsg->GetType())
|
|
|
|
{
|
2008-06-16 20:19:35 +02:00
|
|
|
case NMT_ADD_WAYPOINT:
|
2005-05-18 07:32:09 +02:00
|
|
|
{
|
2008-06-16 20:19:35 +02:00
|
|
|
CAddWaypointMessage *msg=(CAddWaypointMessage *)pMsg;
|
2006-08-25 06:24:06 +02:00
|
|
|
isQueued = msg->m_IsQueued != 0;
|
2005-05-18 07:32:09 +02:00
|
|
|
order.m_type=CEntityOrder::ORDER_LAST;
|
2006-12-21 15:57:13 +01:00
|
|
|
order.m_target_location.x=(float)msg->m_TargetX;
|
|
|
|
order.m_target_location.y=(float)msg->m_TargetY;
|
|
|
|
for(CEntityIt it = msg->m_Entities.begin(); it != msg->m_Entities.end(); ++it)
|
2005-05-18 07:32:09 +02:00
|
|
|
{
|
2006-12-21 15:57:13 +01:00
|
|
|
HEntity& hentity = *it;
|
|
|
|
|
2006-12-21 16:37:45 +01:00
|
|
|
const CEntityOrders& order_queue = hentity->m_orderQueue;
|
2006-12-21 15:57:13 +01:00
|
|
|
for(CEntityOrderCRIt ord_it = order_queue.rbegin(); ord_it != order_queue.rend(); ++ord_it)
|
2005-05-18 07:32:09 +02:00
|
|
|
{
|
|
|
|
if (ord_it->m_type == CEntityOrder::ORDER_PATH_END_MARKER)
|
|
|
|
{
|
|
|
|
order.m_type = CEntityOrder::ORDER_GOTO;
|
2007-05-02 14:07:08 +02:00
|
|
|
hentity->PushOrder(order);
|
2005-05-18 07:32:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ord_it->m_type == CEntityOrder::ORDER_PATROL)
|
|
|
|
{
|
|
|
|
order.m_type = ord_it->m_type;
|
2007-05-02 14:07:08 +02:00
|
|
|
hentity->PushOrder(order);
|
2005-05-18 07:32:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (order.m_type == CEntityOrder::ORDER_LAST)
|
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
LOG(CLogger::Error, LOG_CATEGORY, L"Got an AddWaypoint message for an entity that isn't moving.");
|
2005-05-18 07:32:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-06-16 20:19:35 +02:00
|
|
|
case NMT_GOTO:
|
|
|
|
ENTITY_POSITION(CGotoMessage, ORDER_GOTO);
|
2005-05-18 07:32:09 +02:00
|
|
|
break;
|
2008-06-16 20:19:35 +02:00
|
|
|
case NMT_RUN:
|
|
|
|
ENTITY_POSITION(CRunMessage, ORDER_RUN);
|
2006-01-16 11:56:47 +01:00
|
|
|
break;
|
2008-06-16 20:19:35 +02:00
|
|
|
case NMT_PATROL:
|
|
|
|
ENTITY_POSITION(CPatrolMessage, ORDER_PATROL);
|
2005-05-18 07:32:09 +02:00
|
|
|
break;
|
2008-07-13 19:37:45 +02:00
|
|
|
case NMT_SET_RALLY_POINT:
|
|
|
|
ENTITY_POSITION(CSetRallyPointMessage, ORDER_SET_RALLY_POINT);
|
|
|
|
break;
|
2008-07-24 07:25:14 +02:00
|
|
|
case NMT_SET_STANCE:
|
|
|
|
ENTITY_STRING(CSetStanceMessage, ORDER_SET_STANCE);
|
|
|
|
break;
|
2008-06-16 20:19:35 +02:00
|
|
|
case NMT_FORMATION_GOTO:
|
|
|
|
ENTITY_POSITION_FORM(CFormationGotoMessage, ORDER_GOTO);
|
2006-12-21 15:57:13 +01:00
|
|
|
break;
|
|
|
|
//TODO: make formation move to within range of target and then attack normally
|
2008-07-29 10:22:56 +02:00
|
|
|
case NMT_CONTACT_ACTION:
|
|
|
|
ENTITY_ENTITY_INT_BOOL(CContactActionMessage, ORDER_CONTACT_ACTION);
|
2005-12-29 09:42:44 +01:00
|
|
|
break;
|
2008-07-29 10:22:56 +02:00
|
|
|
case NMT_FORMATION_CONTACT_ACTION:
|
|
|
|
ENTITY_ENTITY_INT(CFormationContactActionMessage, ORDER_CONTACT_ACTION);
|
2006-02-22 23:45:16 +01:00
|
|
|
break;
|
2008-06-16 20:19:35 +02:00
|
|
|
case NMT_NOTIFY_REQUEST:
|
|
|
|
ENTITY_ENTITY_INT(CNotifyRequestMessage, ORDER_NOTIFY_REQUEST);
|
2006-02-13 04:32:15 +01:00
|
|
|
break;
|
2008-06-16 20:19:35 +02:00
|
|
|
case NMT_PRODUCE:
|
|
|
|
ENTITY_INT_STRING(CProduceMessage, ORDER_PRODUCE);
|
2006-12-21 15:57:13 +01:00
|
|
|
break;
|
2008-06-16 20:19:35 +02:00
|
|
|
case NMT_PLACE_OBJECT:
|
2006-05-04 09:40:31 +02:00
|
|
|
{
|
2008-06-16 20:19:35 +02:00
|
|
|
CPlaceObjectMessage *msg = (CPlaceObjectMessage *) pMsg;
|
2006-08-25 06:24:06 +02:00
|
|
|
isQueued = msg->m_IsQueued != 0;
|
2006-05-04 09:40:31 +02:00
|
|
|
|
2006-05-14 00:11:46 +02:00
|
|
|
// Figure out the player
|
|
|
|
CPlayer* player = 0;
|
2006-05-04 09:40:31 +02:00
|
|
|
if(msg->m_Entities.size() > 0)
|
2006-05-14 00:11:46 +02:00
|
|
|
player = msg->m_Entities[0]->GetPlayer();
|
2006-05-04 09:40:31 +02:00
|
|
|
else
|
2006-05-14 00:11:46 +02:00
|
|
|
player = g_Game->GetLocalPlayer();
|
|
|
|
|
|
|
|
// Create the object
|
|
|
|
CVector3D pos(msg->m_X/1000.0f, msg->m_Y/1000.0f, msg->m_Z/1000.0f);
|
2007-05-02 14:07:08 +02:00
|
|
|
HEntity newObj = g_EntityManager.CreateFoundation( msg->m_Template, player, pos, msg->m_Angle/1000.0f );
|
2006-09-02 23:20:25 +02:00
|
|
|
newObj->m_actor->SetPlayerID(player->GetPlayerID());
|
2006-06-10 05:05:16 +02:00
|
|
|
if( newObj->Initialize() )
|
|
|
|
{
|
|
|
|
// Order all the selected units to work on the new object using the given action
|
|
|
|
order.m_type = CEntityOrder::ORDER_START_CONSTRUCTION;
|
2006-12-21 15:57:13 +01:00
|
|
|
order.m_new_obj = newObj;
|
2006-10-06 16:21:16 +02:00
|
|
|
QueueOrder(order, msg->m_Entities, isQueued);
|
2006-06-10 05:05:16 +02:00
|
|
|
}
|
2007-01-27 03:36:33 +01:00
|
|
|
}
|
2006-05-04 09:40:31 +02:00
|
|
|
break;
|
2006-03-31 05:30:34 +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)
|
|
|
|
{
|
|
|
|
m_pTurnManager->QueueLocalCommand(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
|
|
|
}
|