2004-05-28 04:57:50 +02:00
|
|
|
// Entity state-machine processing code.
|
|
|
|
|
2004-06-03 20:38:14 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2004-05-28 04:57:50 +02:00
|
|
|
#include "Entity.h"
|
2006-07-13 05:29:33 +02:00
|
|
|
#include "EntityTemplate.h"
|
# 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
|
|
|
#include "EventHandlers.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "graphics/Model.h"
|
|
|
|
#include "graphics/ObjectEntry.h"
|
|
|
|
#include "graphics/SkeletonAnim.h"
|
|
|
|
#include "graphics/SkeletonAnimDef.h" // Animation duration
|
|
|
|
#include "graphics/Unit.h"
|
2006-02-22 23:45:16 +01:00
|
|
|
#include "ProductionQueue.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "maths/MathUtil.h"
|
2004-05-28 04:57:50 +02:00
|
|
|
#include "Collision.h"
|
2004-05-29 05:32:33 +02:00
|
|
|
#include "PathfindEngine.h"
|
2006-09-01 02:24:26 +02:00
|
|
|
#include "LOSManager.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "graphics/Terrain.h"
|
2006-09-09 02:00:23 +02:00
|
|
|
#include "Stance.h"
|
2008-06-16 20:19:35 +02:00
|
|
|
#include "sound/SoundGroupMgr.h"
|
2004-07-23 12:56:52 +02:00
|
|
|
|
2006-03-21 21:55:45 +01:00
|
|
|
#include "ps/Game.h"
|
|
|
|
#include "ps/World.h"
|
2004-05-28 04:57:50 +02:00
|
|
|
|
2007-05-18 02:14:26 +02:00
|
|
|
#include "lib/rand.h"
|
|
|
|
|
2007-10-10 21:35:23 +02:00
|
|
|
#include "ps/GameSetup/Config.h"
|
|
|
|
|
2004-11-11 08:09:32 +01:00
|
|
|
enum EGotoSituation
|
|
|
|
{
|
|
|
|
NORMAL = 0,
|
|
|
|
ALREADY_AT_DESTINATION,
|
|
|
|
REACHED_DESTINATION,
|
|
|
|
COLLISION_WITH_DESTINATION,
|
|
|
|
COLLISION_NEAR_DESTINATION,
|
|
|
|
COLLISION_OVERLAPPING_OBJECTS,
|
|
|
|
COLLISION_OTHER,
|
2006-09-09 02:00:23 +02:00
|
|
|
WOULD_LEAVE_MAP,
|
|
|
|
STANCE_DISALLOWS
|
2004-11-11 08:09:32 +01:00
|
|
|
};
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
bool CEntity::ShouldRun(float distance)
|
2006-07-24 03:33:26 +02:00
|
|
|
{
|
2007-02-10 04:09:52 +01:00
|
|
|
if( !entf_get(ENTF_SHOULD_RUN) )
|
2006-07-24 03:33:26 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// tired
|
2007-02-10 04:09:52 +01:00
|
|
|
if( m_staminaCurr <= 0 )
|
2006-07-24 03:33:26 +02:00
|
|
|
return false;
|
|
|
|
|
2007-02-10 04:09:52 +01:00
|
|
|
if( distance >= m_runMaxRange )
|
2006-07-24 03:33:26 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// don't start running if less than minimum
|
|
|
|
if( distance <= m_runMinRange && !entf_get(ENTF_IS_RUNNING) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
float CEntity::ChooseMovementSpeed( float distance )
|
2004-05-28 04:57:50 +02:00
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
bool should_run = ShouldRun(distance);
|
2006-07-24 03:33:26 +02:00
|
|
|
|
2006-09-16 22:13:40 +02:00
|
|
|
float speed = should_run? m_runSpeed : m_speed;
|
2006-07-24 03:33:26 +02:00
|
|
|
const char* anim_name = should_run? "run" : "walk";
|
2006-03-17 04:59:49 +01:00
|
|
|
|
2006-09-16 22:13:40 +02:00
|
|
|
// Modify the speed based on the slope of the terrain in our direction (obtained from our x orientation)
|
|
|
|
float angle = m_orientation_unclamped.x;
|
2006-09-30 17:46:40 +02:00
|
|
|
int sector = rintf( angle / (PI/2) * m_base->m_pitchDivs );
|
2006-09-16 22:13:40 +02:00
|
|
|
speed -= sector * m_base->m_pitchValue;
|
|
|
|
|
2007-02-10 04:09:52 +01:00
|
|
|
entf_set_to(ENTF_IS_RUNNING, should_run);
|
2006-07-24 03:33:26 +02:00
|
|
|
|
|
|
|
if ( m_actor )
|
2006-01-10 05:39:11 +01:00
|
|
|
{
|
2006-07-24 03:33:26 +02:00
|
|
|
if ( !m_actor->IsPlayingAnimation( anim_name ) )
|
2006-01-10 05:39:11 +01:00
|
|
|
{
|
2007-02-10 04:09:52 +01:00
|
|
|
m_actor->SetAnimationState( anim_name, false, speed );
|
2006-03-17 04:59:49 +01:00
|
|
|
|
2006-07-24 03:33:26 +02:00
|
|
|
// Animation desync
|
|
|
|
m_actor->GetModel()->Update( rand( 0, 1000 ) / 1000.0f );
|
2006-01-16 11:56:47 +01:00
|
|
|
}
|
2006-01-10 05:39:11 +01:00
|
|
|
}
|
2006-07-24 03:33:26 +02:00
|
|
|
|
|
|
|
return speed;
|
2006-03-17 04:59:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Does all the shared processing for line-of-sight gotos
|
|
|
|
|
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 CEntity::ProcessGotoHelper( CEntityOrder* current, int timestep_millis, HEntity& collide, float& timeLeft )
|
2006-03-17 04:59:49 +01:00
|
|
|
{
|
|
|
|
float timestep=timestep_millis/1000.0f;
|
|
|
|
|
|
|
|
CVector2D delta;
|
2006-12-21 15:57:13 +01:00
|
|
|
delta.x = (float)current->m_target_location.x - m_position.X;
|
|
|
|
delta.y = (float)current->m_target_location.y - m_position.Z;
|
2006-03-17 04:59:49 +01:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
float len = delta.Length();
|
2006-03-17 04:59:49 +01:00
|
|
|
|
2006-08-25 06:24:06 +02:00
|
|
|
if( len < 0.01f )
|
2006-03-17 04:59:49 +01:00
|
|
|
return( ALREADY_AT_DESTINATION );
|
|
|
|
|
|
|
|
// Curve smoothing.
|
|
|
|
// Here there be trig.
|
|
|
|
|
2007-07-05 09:33:43 +02:00
|
|
|
float speed = ChooseMovementSpeed( len );
|
|
|
|
float scale = speed * timestep;
|
2006-01-10 05:39:11 +01:00
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
// Note: Easy optimization: flag somewhere that this unit
|
2006-01-16 11:56:47 +01:00
|
|
|
// is already pointing the way, and don't do this
|
|
|
|
// trig every time.right
|
2004-08-03 01:14:54 +02:00
|
|
|
|
|
|
|
m_targetorientation = atan2( delta.x, delta.y );
|
2006-05-04 06:14:48 +02:00
|
|
|
float deltatheta = m_targetorientation - (float)m_orientation.Y;
|
2004-08-03 01:14:54 +02:00
|
|
|
while( deltatheta > PI ) deltatheta -= 2 * PI;
|
|
|
|
while( deltatheta < -PI ) deltatheta += 2 * PI;
|
|
|
|
|
|
|
|
if( fabs( deltatheta ) > 0.01f )
|
2004-06-03 04:20:48 +02:00
|
|
|
{
|
2006-01-22 02:17:58 +01:00
|
|
|
if ( m_turningRadius != 0 )
|
2004-08-03 01:14:54 +02:00
|
|
|
{
|
2007-07-05 09:33:43 +02:00
|
|
|
float maxTurningSpeed = ( speed / m_turningRadius ) * timestep;
|
2006-01-22 00:27:42 +01:00
|
|
|
deltatheta = clamp( deltatheta, -maxTurningSpeed, maxTurningSpeed );
|
2004-08-03 01:14:54 +02:00
|
|
|
}
|
2006-05-04 06:14:48 +02:00
|
|
|
m_orientation.Y = m_orientation.Y + deltatheta;
|
2004-08-03 01:14:54 +02:00
|
|
|
|
2006-05-04 06:14:48 +02:00
|
|
|
m_ahead.x = sin( m_orientation.Y );
|
|
|
|
m_ahead.y = cos( m_orientation.Y );
|
2004-08-03 01:14:54 +02:00
|
|
|
|
2004-06-03 04:20:48 +02:00
|
|
|
// We can only really attempt to smooth paths the pathfinder
|
|
|
|
// has flagged for us. If the turning-radius calculations are
|
2006-01-22 00:27:42 +01:00
|
|
|
// applied to other types of waypoint, weirdness happens.
|
2004-06-03 04:20:48 +02:00
|
|
|
// Things like an entity trying to walk to a point inside
|
|
|
|
// his turning radius (which he can't do directly, so he'll
|
2006-01-22 00:27:42 +01:00
|
|
|
// orbit the point indefinitely), or just massive deviations
|
2004-06-03 04:20:48 +02:00
|
|
|
// making the paths we calculate useless.
|
|
|
|
// It's also painful trying to watch two entities resolve their
|
|
|
|
// collision when they're both bound by turning constraints.
|
2004-08-03 01:14:54 +02:00
|
|
|
|
|
|
|
// So, as a compromise for the look of the thing, we'll just turn in
|
|
|
|
// place until we're looking the right way. At least, that's what
|
|
|
|
// seems logical. But in most cases that looks worse. So actually,
|
|
|
|
// let's not.
|
2004-11-11 08:09:32 +01:00
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
if( current->m_type != CEntityOrder::ORDER_GOTO_SMOOTHED )
|
2006-05-04 06:14:48 +02:00
|
|
|
m_orientation.Y = m_targetorientation;
|
2004-08-03 01:14:54 +02:00
|
|
|
|
2004-06-03 04:20:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
m_ahead = delta / len;
|
2006-05-04 06:14:48 +02:00
|
|
|
m_orientation.Y = m_targetorientation;
|
2004-06-03 04:20:48 +02:00
|
|
|
}
|
2006-04-22 07:52:23 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
UpdateXZOrientation();
|
2004-06-03 04:20:48 +02:00
|
|
|
|
2005-03-30 00:04:38 +02:00
|
|
|
if( m_bounds && m_bounds->m_type == CBoundingObject::BOUND_OABB )
|
2007-07-05 09:33:43 +02:00
|
|
|
((CBoundingBox*) m_bounds)->SetOrientation( m_ahead );
|
2004-05-28 04:57:50 +02:00
|
|
|
|
2004-11-11 08:09:32 +01:00
|
|
|
EGotoSituation rc = NORMAL;
|
|
|
|
|
2004-05-28 04:57:50 +02:00
|
|
|
if( scale > len )
|
2004-11-11 08:09:32 +01:00
|
|
|
{
|
2007-07-05 09:33:43 +02:00
|
|
|
// Reached destination. Calculate how much time we have left for the next order.
|
2004-05-28 04:57:50 +02:00
|
|
|
scale = len;
|
2007-07-05 09:33:43 +02:00
|
|
|
timeLeft = timestep - (len / speed);
|
2004-11-11 08:09:32 +01:00
|
|
|
rc = REACHED_DESTINATION;
|
|
|
|
}
|
2004-05-28 04:57:50 +02:00
|
|
|
|
|
|
|
delta = m_ahead * scale;
|
|
|
|
|
2004-07-23 12:56:52 +02:00
|
|
|
// What would happen if we moved forward a little?
|
|
|
|
|
2004-05-28 04:57:50 +02:00
|
|
|
m_position.X += delta.x;
|
|
|
|
m_position.Z += delta.y;
|
2004-07-23 12:56:52 +02:00
|
|
|
|
2005-03-30 00:04:38 +02:00
|
|
|
if( m_bounds )
|
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
m_bounds->SetPosition( m_position.X, m_position.Z );
|
2004-05-28 04:57:50 +02:00
|
|
|
|
2006-09-16 22:13:40 +02:00
|
|
|
// For now, ignore passThroughAllies for low-level movement (but leave it on for long-range
|
|
|
|
// pathfinding); ideally we will enable pass-through-allies only for the long-range pathing
|
|
|
|
// and when the unit is moving to assume its place in a formation, since it looks bad to have
|
|
|
|
// units stand on each other otherwise.
|
2007-05-02 14:07:08 +02:00
|
|
|
collide = GetCollisionObject( this, false );
|
2005-03-30 00:04:38 +02:00
|
|
|
|
|
|
|
if( collide )
|
|
|
|
{
|
|
|
|
// We'd hit something. Let's not.
|
|
|
|
m_position.X -= delta.x;
|
|
|
|
m_position.Z -= delta.y;
|
|
|
|
m_bounds->m_pos -= delta;
|
2004-05-28 04:57:50 +02:00
|
|
|
|
2005-03-30 00:04:38 +02:00
|
|
|
// Is it too late to avoid the collision?
|
2004-05-28 04:57:50 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
if( collide->m_bounds->Intersects( m_bounds ) )
|
2005-03-30 00:04:38 +02:00
|
|
|
{
|
|
|
|
// Yes. Oh dear. That can't be good.
|
|
|
|
// This really shouldn't happen in the current build.
|
2004-07-22 18:18:12 +02:00
|
|
|
|
2006-10-18 00:34:47 +02:00
|
|
|
//debug_assert( false && "Overlapping objects" );
|
2004-07-23 12:56:52 +02:00
|
|
|
|
2005-03-30 00:04:38 +02:00
|
|
|
// Erm... do nothing?
|
|
|
|
|
|
|
|
return( COLLISION_OVERLAPPING_OBJECTS );
|
|
|
|
}
|
2004-07-23 12:56:52 +02:00
|
|
|
|
2005-03-30 00:04:38 +02:00
|
|
|
// No. Is our destination within the obstacle?
|
2007-05-02 14:07:08 +02:00
|
|
|
if( collide->m_bounds->Contains( current->m_target_location ) )
|
2005-03-30 00:04:38 +02:00
|
|
|
return( COLLISION_WITH_DESTINATION );
|
2004-11-11 08:09:32 +01:00
|
|
|
|
2005-03-30 00:04:38 +02:00
|
|
|
// No. Are we nearing our destination, do we wish to stop there, and is it obstructed?
|
2004-07-23 12:56:52 +02:00
|
|
|
|
2005-03-30 00:04:38 +02:00
|
|
|
if( ( m_orderQueue.size() == 1 ) && ( len <= 10.0f ) )
|
2004-07-23 12:56:52 +02:00
|
|
|
{
|
2006-12-21 15:57:13 +01:00
|
|
|
CBoundingCircle destinationObs( current->m_target_location.x, current->m_target_location.y, m_bounds->m_radius, 0.0f );
|
2007-05-02 14:07:08 +02:00
|
|
|
if( GetCollisionObject( &destinationObs ) )
|
2005-03-30 00:04:38 +02:00
|
|
|
{
|
|
|
|
// Yes. (Chances are a bunch of units were tasked to the same destination)
|
|
|
|
return( COLLISION_NEAR_DESTINATION );
|
|
|
|
}
|
2004-07-23 12:56:52 +02:00
|
|
|
}
|
|
|
|
|
2005-03-30 00:04:38 +02:00
|
|
|
// No?
|
|
|
|
return( COLLISION_OTHER );
|
|
|
|
}
|
2004-11-11 08:09:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Will we step off the map?
|
2007-05-02 14:07:08 +02:00
|
|
|
if( !g_Game->GetWorld()->GetTerrain()->IsOnMap( m_position.X, m_position.Z ) )
|
2004-11-11 08:09:32 +01:00
|
|
|
{
|
|
|
|
// Yes. That's not a particularly good idea, either.
|
|
|
|
|
|
|
|
m_position.X -= delta.x;
|
|
|
|
m_position.Z -= delta.y;
|
2005-03-30 00:04:38 +02:00
|
|
|
if( m_bounds )
|
2007-05-02 14:07:08 +02:00
|
|
|
m_bounds->SetPosition( m_position.X, m_position.Z );
|
2004-11-11 08:09:32 +01:00
|
|
|
|
|
|
|
// All things being equal, we should only get here while on a collision path
|
|
|
|
// (No destination should be off the map)
|
|
|
|
|
|
|
|
return( WOULD_LEAVE_MAP );
|
|
|
|
}
|
|
|
|
|
2006-09-09 02:00:23 +02:00
|
|
|
// Does our stance not allow us to go there?
|
2007-05-02 14:07:08 +02:00
|
|
|
if( current->m_source==CEntityOrder::SOURCE_UNIT_AI && !m_stance->CheckMovement( m_position ) )
|
2006-09-09 02:00:23 +02:00
|
|
|
{
|
|
|
|
m_position.X -= delta.x;
|
|
|
|
m_position.Z -= delta.y;
|
|
|
|
if( m_bounds )
|
2007-05-02 14:07:08 +02:00
|
|
|
m_bounds->SetPosition( m_position.X, m_position.Z );
|
2006-09-09 02:00:23 +02:00
|
|
|
|
|
|
|
return( STANCE_DISALLOWS );
|
|
|
|
}
|
|
|
|
|
2004-11-11 08:09:32 +01:00
|
|
|
// No. I suppose it's OK to go there, then. *disappointed*
|
|
|
|
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
bool CEntity::ProcessGotoNoPathing( CEntityOrder* current, int timestep_millis )
|
2004-11-11 08:09:32 +01:00
|
|
|
{
|
|
|
|
HEntity collide;
|
2007-07-05 09:33:43 +02:00
|
|
|
float timeLeft;
|
|
|
|
switch( ProcessGotoHelper( current, timestep_millis, collide, timeLeft ) )
|
|
|
|
{
|
|
|
|
case ALREADY_AT_DESTINATION:
|
2004-11-11 08:09:32 +01:00
|
|
|
{
|
|
|
|
// If on a collision path; decide where to go next. Otherwise, proceed to the next waypoint.
|
|
|
|
if( current->m_type == CEntityOrder::ORDER_GOTO_COLLISION )
|
2007-05-02 14:07:08 +02:00
|
|
|
Repath();
|
2004-11-11 08:09:32 +01:00
|
|
|
else
|
|
|
|
m_orderQueue.pop_front();
|
2007-07-05 09:33:43 +02:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
case REACHED_DESTINATION:
|
|
|
|
{
|
|
|
|
// Start along the next segment of the path, if one exists
|
|
|
|
m_orderQueue.pop_front();
|
|
|
|
if( !m_orderQueue.empty() )
|
|
|
|
{
|
|
|
|
CEntityOrder* newOrder = &m_orderQueue.front();
|
|
|
|
switch( newOrder->m_type )
|
|
|
|
{
|
|
|
|
case CEntityOrder::ORDER_GOTO_NOPATHING:
|
|
|
|
case CEntityOrder::ORDER_GOTO_COLLISION:
|
|
|
|
case CEntityOrder::ORDER_GOTO_SMOOTHED:
|
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 newTimestep = int(timeLeft * 1000.0f);
|
2008-02-04 09:24:17 +01:00
|
|
|
return( ProcessGotoNoPathing( newOrder, newTimestep ) );
|
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
|
|
|
}
|
2007-07-05 09:33:43 +02:00
|
|
|
}
|
2006-01-10 05:39:11 +01:00
|
|
|
}
|
2004-11-11 08:09:32 +01:00
|
|
|
return( false );
|
2007-07-05 09:33:43 +02:00
|
|
|
}
|
2004-11-11 08:09:32 +01:00
|
|
|
case COLLISION_OVERLAPPING_OBJECTS:
|
2007-07-05 09:33:43 +02:00
|
|
|
{
|
2004-11-11 08:09:32 +01:00
|
|
|
return( false );
|
2007-07-05 09:33:43 +02:00
|
|
|
}
|
2004-11-11 08:09:32 +01:00
|
|
|
case COLLISION_WITH_DESTINATION:
|
2007-07-05 09:33:43 +02:00
|
|
|
{
|
2006-08-25 06:24:06 +02:00
|
|
|
// We're as close as we can get...
|
2004-11-11 08:09:32 +01:00
|
|
|
m_orderQueue.pop_front();
|
|
|
|
return( false );
|
2007-07-05 09:33:43 +02:00
|
|
|
}
|
2004-11-11 08:09:32 +01:00
|
|
|
case COLLISION_NEAR_DESTINATION:
|
|
|
|
{
|
2006-01-22 00:27:42 +01:00
|
|
|
// Here's a weird idea: (I hope it works)
|
2004-11-11 08:09:32 +01:00
|
|
|
// Spiral round the destination until a free point is found.
|
2006-12-21 15:57:13 +01:00
|
|
|
CBoundingCircle destinationObs( current->m_target_location.x, current->m_target_location.y, m_bounds->m_radius, 0.0f );
|
2004-11-11 08:09:32 +01:00
|
|
|
|
|
|
|
float interval = destinationObs.m_radius;
|
|
|
|
float r = interval, theta = 0.0f, delta;
|
2006-12-21 15:57:13 +01:00
|
|
|
float _x = current->m_target_location.x, _y = current->m_target_location.y;
|
2004-11-11 08:09:32 +01:00
|
|
|
|
|
|
|
while( true )
|
|
|
|
{
|
|
|
|
delta = interval / r;
|
|
|
|
theta += delta;
|
|
|
|
r += ( interval * delta ) / ( 2 * PI );
|
2007-05-02 14:07:08 +02:00
|
|
|
destinationObs.SetPosition( _x + r * cosf( theta ), _y + r * sinf( theta ) );
|
|
|
|
if( !GetCollisionObject( &destinationObs ) ) break;
|
2004-11-11 08:09:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset our destination
|
2006-12-21 15:57:13 +01:00
|
|
|
current->m_target_location.x = _x + r * cosf( theta );
|
|
|
|
current->m_target_location.y = _y + r * sinf( theta );
|
2004-11-11 08:09:32 +01:00
|
|
|
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
case COLLISION_OTHER:
|
|
|
|
{
|
|
|
|
// Path around it.
|
2004-05-28 04:57:50 +02:00
|
|
|
|
2004-07-20 21:30:35 +02:00
|
|
|
CEntityOrder avoidance;
|
|
|
|
avoidance.m_type = CEntityOrder::ORDER_GOTO_COLLISION;
|
|
|
|
CVector2D right;
|
|
|
|
right.x = m_ahead.y; right.y = -m_ahead.x;
|
|
|
|
CVector2D avoidancePosition;
|
|
|
|
|
2004-07-23 12:56:52 +02:00
|
|
|
// Which is the shortest diversion, going left or right?
|
|
|
|
// (Weight a little towards the right, to stop both units dodging the same way)
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
if( ( collide->m_bounds->m_pos - m_bounds->m_pos ).Dot( right ) < 1 )
|
2004-07-20 21:30:35 +02:00
|
|
|
{
|
|
|
|
// Turn right.
|
|
|
|
avoidancePosition = collide->m_bounds->m_pos + right * ( collide->m_bounds->m_radius + m_bounds->m_radius * 2.5f );
|
2004-05-28 04:57:50 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-07-20 21:30:35 +02:00
|
|
|
// Turn left.
|
|
|
|
avoidancePosition = collide->m_bounds->m_pos - right * ( collide->m_bounds->m_radius + m_bounds->m_radius * 2.5f );
|
|
|
|
}
|
2004-06-03 04:20:48 +02:00
|
|
|
|
2004-07-23 12:56:52 +02:00
|
|
|
// Create a short path representing this detour
|
|
|
|
|
2006-12-21 15:57:13 +01:00
|
|
|
avoidance.m_target_location = avoidancePosition;
|
2004-07-20 21:30:35 +02:00
|
|
|
if( current->m_type == CEntityOrder::ORDER_GOTO_COLLISION )
|
|
|
|
m_orderQueue.pop_front();
|
|
|
|
m_orderQueue.push_front( avoidance );
|
|
|
|
return( false );
|
2004-11-11 08:09:32 +01:00
|
|
|
}
|
|
|
|
case WOULD_LEAVE_MAP:
|
2007-07-05 09:33:43 +02:00
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
// Just stop here, Repath if necessary.
|
2004-11-11 08:09:32 +01:00
|
|
|
m_orderQueue.pop_front();
|
|
|
|
return( false );
|
2007-07-05 09:33:43 +02:00
|
|
|
}
|
2006-09-09 02:00:23 +02:00
|
|
|
case STANCE_DISALLOWS:
|
2008-02-04 09:24:17 +01:00
|
|
|
{
|
2006-09-09 02:00:23 +02:00
|
|
|
return( false ); // The stance will have cleared our order queue already
|
2008-02-04 09:24:17 +01:00
|
|
|
}
|
2007-07-05 09:33:43 +02:00
|
|
|
default:
|
2004-11-11 08:09:32 +01:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-15 06:23:33 +02:00
|
|
|
// Handles processing common to (at the moment) gather and melee attack actions
|
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
|
|
|
bool CEntity::ProcessContactAction( CEntityOrder* current, int UNUSED(timestep_millis), CEntityOrder::EOrderType transition, SEntityAction* action )
|
2004-11-11 08:09:32 +01:00
|
|
|
{
|
2006-12-21 15:57:13 +01:00
|
|
|
HEntity target = current->m_target_entity;
|
2006-09-01 02:24:26 +02:00
|
|
|
|
|
|
|
if( !target || !target->m_extant )
|
2006-10-09 06:17:15 +02:00
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
PopOrder();
|
|
|
|
if( m_orderQueue.empty() && target.IsValid() )
|
2006-10-09 06:17:15 +02:00
|
|
|
{
|
|
|
|
CEventTargetExhausted evt( target, action->m_Id );
|
|
|
|
DispatchEvent( &evt );
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2006-09-01 02:24:26 +02:00
|
|
|
|
2007-01-29 23:21:25 +01:00
|
|
|
if( current->m_source != CEntityOrder::SOURCE_TRIGGERS &&
|
|
|
|
g_Game->GetWorld()->GetLOSManager()->GetUnitStatus( target, m_player ) == UNIT_HIDDEN )
|
2006-10-09 06:17:15 +02:00
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
PopOrder();
|
2006-09-01 02:24:26 +02:00
|
|
|
return false;
|
2006-10-09 06:17:15 +02:00
|
|
|
}
|
2006-09-01 02:24:26 +02:00
|
|
|
|
2006-12-21 15:57:13 +01:00
|
|
|
current->m_target_location = target->m_position;
|
2007-05-02 14:07:08 +02:00
|
|
|
float Distance = Distance2D(current->m_target_location);
|
2006-01-22 09:53:35 +01:00
|
|
|
|
2006-01-05 07:13:31 +01:00
|
|
|
if( Distance < action->m_MaxRange )
|
2004-11-11 08:09:32 +01:00
|
|
|
{
|
2006-09-09 02:00:23 +02:00
|
|
|
current->m_type = transition;
|
2006-07-12 18:18:31 +02:00
|
|
|
entf_clear(ENTF_IS_RUNNING);
|
2006-09-09 02:00:23 +02:00
|
|
|
return true;
|
2004-05-28 04:57:50 +02:00
|
|
|
}
|
2006-09-09 02:00:23 +02:00
|
|
|
else
|
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
if( current->m_source == CEntityOrder::SOURCE_UNIT_AI && !m_stance->AllowsMovement() )
|
2006-09-09 02:00:23 +02:00
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
PopOrder();
|
2006-09-09 02:00:23 +02:00
|
|
|
return false; // We're not allowed to move at all by the current stance
|
|
|
|
}
|
2004-07-23 12:56:52 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
ChooseMovementSpeed( Distance );
|
2004-11-11 08:09:32 +01:00
|
|
|
|
2006-09-09 02:00:23 +02:00
|
|
|
// The pathfinder will push its result back into this unit's queue and
|
|
|
|
// add back the current order at the end with the transition type.
|
|
|
|
current->m_type = transition;
|
2007-05-02 14:07:08 +02:00
|
|
|
g_Pathfinder.RequestContactPath( me, current, action->m_MaxRange );
|
2006-09-09 02:00:23 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2004-11-11 08:09:32 +01:00
|
|
|
}
|
2007-02-10 04:09:52 +01: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
|
|
|
bool CEntity::ProcessContactActionNoPathing( CEntityOrder* current, int timestep_millis, const CStr& animation, CScriptEvent* contactEvent, SEntityAction* action )
|
2004-11-11 08:09:32 +01:00
|
|
|
{
|
2006-12-21 15:57:13 +01:00
|
|
|
HEntity target = current->m_target_entity;
|
2006-09-01 02:24:26 +02:00
|
|
|
|
2005-05-01 21:09:13 +02:00
|
|
|
if( m_fsm_cyclepos != NOT_IN_CYCLE )
|
2004-11-11 08:09:32 +01: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
|
|
|
int nextpos = m_fsm_cyclepos + timestep_millis * 2;
|
2007-02-10 04:09:52 +01:00
|
|
|
|
2005-05-01 21:09:13 +02:00
|
|
|
if( ( m_fsm_cyclepos <= action->m_Speed ) && ( nextpos > action->m_Speed ) )
|
|
|
|
{
|
2008-06-16 20:19:35 +02:00
|
|
|
const size_t soundGroupIndex = m_base->m_SoundGroupTable[animation];
|
2008-06-21 22:06:30 +02:00
|
|
|
g_soundGroupMgr->PlayNext(soundGroupIndex, m_position);
|
2007-01-13 21:14:03 +01:00
|
|
|
|
2006-02-22 23:45:16 +01:00
|
|
|
if(!DispatchEvent( contactEvent ))
|
|
|
|
{
|
|
|
|
// Cancel current order
|
2006-07-12 18:18:31 +02:00
|
|
|
entf_clear(ENTF_IS_RUNNING);
|
|
|
|
entf_clear(ENTF_SHOULD_RUN);
|
2007-02-10 04:09:52 +01:00
|
|
|
m_actor->SetAnimationState( "idle" );
|
2007-05-02 14:07:08 +02:00
|
|
|
PopOrder();
|
|
|
|
if( m_orderQueue.empty() && target.IsValid() )
|
2006-10-09 06:17:15 +02:00
|
|
|
{
|
|
|
|
CEventTargetExhausted evt( target, action->m_Id );
|
|
|
|
DispatchEvent( &evt );
|
|
|
|
}
|
2006-02-22 23:45:16 +01:00
|
|
|
return( false );
|
|
|
|
}
|
2005-05-01 21:09:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( nextpos >= ( action->m_Speed * 2 ) )
|
|
|
|
{
|
|
|
|
// End of cycle.
|
|
|
|
m_fsm_cyclepos = NOT_IN_CYCLE;
|
2005-03-18 23:30:23 +01:00
|
|
|
return( false );
|
2005-04-22 09:12:55 +02:00
|
|
|
}
|
2004-11-11 08:09:32 +01:00
|
|
|
|
2005-05-01 21:09:13 +02:00
|
|
|
// Otherwise, increment position.
|
|
|
|
m_fsm_cyclepos = nextpos;
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
2006-02-22 23:45:16 +01:00
|
|
|
// Target's dead (or exhausted), or we cancelled? Then our work here is done.
|
2006-09-01 02:24:26 +02:00
|
|
|
if( !target || !target->m_extant
|
|
|
|
|| g_Game->GetWorld()->GetLOSManager()->GetUnitStatus( target, m_player ) == UNIT_HIDDEN )
|
2005-05-01 21:09:13 +02:00
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
PopOrder();
|
|
|
|
if( m_orderQueue.empty() && target.IsValid() )
|
2006-10-09 06:17:15 +02:00
|
|
|
{
|
|
|
|
CEventTargetExhausted evt( target, action->m_Id );
|
|
|
|
DispatchEvent( &evt );
|
|
|
|
}
|
|
|
|
|
2006-07-12 18:18:31 +02:00
|
|
|
entf_clear(ENTF_IS_RUNNING);
|
|
|
|
entf_clear(ENTF_SHOULD_RUN);
|
2005-05-01 21:09:13 +02:00
|
|
|
return( false );
|
2005-03-18 23:30:23 +01:00
|
|
|
}
|
2004-11-11 08:09:32 +01:00
|
|
|
|
2006-09-09 02:00:23 +02:00
|
|
|
CVector2D delta = CVector2D(target->m_position) - CVector2D(m_position);
|
2007-05-02 14:07:08 +02:00
|
|
|
float deltaLength = delta.Length();
|
2006-01-22 09:53:35 +01:00
|
|
|
|
2006-09-01 02:24:26 +02:00
|
|
|
float adjRange = action->m_MaxRange + m_bounds->m_radius + target->m_bounds->m_radius;
|
2004-11-11 08:09:32 +01:00
|
|
|
|
2005-05-01 21:09:13 +02:00
|
|
|
if( action->m_MinRange > 0.0f )
|
2004-11-11 08:09:32 +01:00
|
|
|
{
|
2006-09-01 02:24:26 +02:00
|
|
|
float adjMinRange = action->m_MinRange + m_bounds->m_radius + target->m_bounds->m_radius;
|
2005-04-15 06:23:33 +02:00
|
|
|
if( delta.within( adjMinRange ) )
|
|
|
|
{
|
2006-10-04 06:47:58 +02:00
|
|
|
// Too close... avoid it if allowed by the current stance.
|
2007-05-02 14:07:08 +02:00
|
|
|
if( current->m_source == CEntityOrder::SOURCE_UNIT_AI && !m_stance->AllowsMovement() )
|
2006-10-04 06:47:58 +02:00
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
PopOrder();
|
2007-02-10 04:09:52 +01:00
|
|
|
m_actor->SetAnimationState( "idle" );
|
2006-10-04 06:47:58 +02:00
|
|
|
return false; // We're not allowed to move at all by the current stance
|
|
|
|
}
|
|
|
|
|
|
|
|
entf_set(ENTF_SHOULD_RUN);
|
2007-05-02 14:07:08 +02:00
|
|
|
ChooseMovementSpeed( action->m_MinRange );
|
2006-10-04 06:47:58 +02:00
|
|
|
|
|
|
|
// The pathfinder will push its result in front of the current order
|
2007-05-02 14:07:08 +02:00
|
|
|
if( !g_Pathfinder.RequestAvoidPath( me, current, action->m_MinRange + 2.0f ) )
|
2006-10-04 06:47:58 +02:00
|
|
|
{
|
2007-02-10 04:09:52 +01:00
|
|
|
m_actor->SetAnimationState( "idle" ); // Nothing we can do.. maybe we'll find a better target
|
2007-05-02 14:07:08 +02:00
|
|
|
PopOrder();
|
2006-10-04 06:47:58 +02:00
|
|
|
}
|
|
|
|
|
2006-10-06 07:38:57 +02:00
|
|
|
return false;
|
2005-04-15 06:23:33 +02:00
|
|
|
}
|
2004-11-11 08:09:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !delta.within( adjRange ) )
|
|
|
|
{
|
2006-09-09 02:00:23 +02:00
|
|
|
// Too far away at the moment, chase after the target if allowed...
|
2007-05-02 14:07:08 +02:00
|
|
|
if( current->m_source == CEntityOrder::SOURCE_UNIT_AI && !m_stance->AllowsMovement() )
|
2006-09-09 02:00:23 +02:00
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
PopOrder();
|
2006-09-09 02:00:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-11-11 08:09:32 +01:00
|
|
|
// We're aiming to end up at a location just inside our maximum range
|
|
|
|
// (is this good enough?)
|
2007-05-02 14:07:08 +02:00
|
|
|
delta = delta.Normalize() * ( adjRange - m_bounds->m_radius );
|
2006-03-17 04:59:49 +01:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
ChooseMovementSpeed(deltaLength);
|
2006-01-08 09:25:11 +01:00
|
|
|
|
2006-12-21 15:57:13 +01:00
|
|
|
current->m_target_location = (CVector2D)target->m_position - delta;
|
2004-11-11 08:09:32 +01:00
|
|
|
|
2007-07-05 09:33:43 +02:00
|
|
|
HEntity collide;
|
|
|
|
float timeLeft;
|
|
|
|
switch( ProcessGotoHelper( current, timestep_millis, collide, timeLeft ) )
|
2004-11-11 08:09:32 +01:00
|
|
|
{
|
|
|
|
case REACHED_DESTINATION:
|
2007-07-05 09:33:43 +02:00
|
|
|
case ALREADY_AT_DESTINATION:
|
2004-11-11 08:09:32 +01:00
|
|
|
case COLLISION_WITH_DESTINATION:
|
2006-08-08 05:45:13 +02:00
|
|
|
case WOULD_LEAVE_MAP:
|
2006-09-09 02:00:23 +02:00
|
|
|
case STANCE_DISALLOWS:
|
2004-11-11 08:09:32 +01:00
|
|
|
// Not too far any more...
|
|
|
|
break;
|
|
|
|
case NORMAL:
|
|
|
|
// May or may not be close enough, check...
|
|
|
|
// (Assuming the delta above will never take us within minimum range)
|
2006-09-01 02:24:26 +02:00
|
|
|
delta = target->m_position - m_position;
|
2004-11-11 08:09:32 +01:00
|
|
|
if( delta.within( adjRange ) )
|
|
|
|
break;
|
|
|
|
// Otherwise, continue chasing
|
|
|
|
return( false );
|
|
|
|
default:
|
2007-07-05 09:33:43 +02:00
|
|
|
// We have a collision. Path around it.
|
2004-11-11 08:09:32 +01:00
|
|
|
CEntityOrder avoidance;
|
|
|
|
avoidance.m_type = CEntityOrder::ORDER_GOTO_COLLISION;
|
|
|
|
CVector2D right;
|
|
|
|
right.x = m_ahead.y; right.y = -m_ahead.x;
|
|
|
|
CVector2D avoidancePosition;
|
|
|
|
|
|
|
|
// Which is the shortest diversion, going left or right?
|
|
|
|
// (Weight a little towards the right, to stop both units dodging the same way)
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
if( ( collide->m_bounds->m_pos - m_bounds->m_pos ).Dot( right ) < 1 )
|
2004-11-11 08:09:32 +01:00
|
|
|
{
|
|
|
|
// Turn right.
|
|
|
|
avoidancePosition = collide->m_bounds->m_pos + right * ( collide->m_bounds->m_radius + m_bounds->m_radius * 2.5f );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Turn left.
|
|
|
|
avoidancePosition = collide->m_bounds->m_pos - right * ( collide->m_bounds->m_radius + m_bounds->m_radius * 2.5f );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a short path representing this detour
|
|
|
|
|
2006-12-21 15:57:13 +01:00
|
|
|
avoidance.m_target_location = avoidancePosition;
|
2004-11-11 08:09:32 +01:00
|
|
|
if( current->m_type == CEntityOrder::ORDER_GOTO_COLLISION )
|
|
|
|
m_orderQueue.pop_front();
|
|
|
|
m_orderQueue.push_front( avoidance );
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-05-01 21:09:13 +02:00
|
|
|
// Close enough, but turn to face them.
|
2006-05-04 06:14:48 +02:00
|
|
|
m_orientation.Y = atan2( delta.x, delta.y );
|
2007-05-02 14:07:08 +02:00
|
|
|
m_ahead = delta.Normalize();
|
2006-07-12 18:18:31 +02:00
|
|
|
entf_clear(ENTF_IS_RUNNING);
|
2004-11-11 08:09:32 +01:00
|
|
|
}
|
|
|
|
|
2007-02-10 04:09:52 +01:00
|
|
|
m_actor->SetAnimationState( animation, false, 1000.f / (float)action->m_Speed );
|
|
|
|
m_actor->SetAnimationSync( (float)( action->m_Speed / 2) / 1000.f );
|
2005-04-22 09:12:55 +02:00
|
|
|
|
2005-05-01 21:09:13 +02:00
|
|
|
m_fsm_cyclepos = 0;
|
2004-11-11 08:09:32 +01:00
|
|
|
|
2005-04-15 06:23:33 +02:00
|
|
|
return( false );
|
|
|
|
}
|
2004-11-11 08:09:32 +01: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
|
|
|
bool CEntity::ProcessGeneric( CEntityOrder* current, int timestep_millis )
|
2005-12-29 09:42:44 +01:00
|
|
|
{
|
2006-12-21 15:57:13 +01:00
|
|
|
if( m_actions.find( current->m_action ) == m_actions.end() )
|
2006-01-02 04:07:29 +01:00
|
|
|
{
|
|
|
|
return false; // we've been tasked as part of a group but we can't do this action
|
|
|
|
}
|
2006-12-21 15:57:13 +01:00
|
|
|
SEntityAction& action_obj = m_actions[current->m_action];
|
2007-05-02 14:07:08 +02:00
|
|
|
return( ProcessContactAction( current, timestep_millis, CEntityOrder::ORDER_GENERIC_NOPATHING, &action_obj ) );
|
2005-12-29 09:42:44 +01: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
|
|
|
bool CEntity::ProcessGenericNoPathing( CEntityOrder* current, int timestep_millis )
|
2005-12-29 09:42:44 +01:00
|
|
|
{
|
2006-12-21 15:57:13 +01:00
|
|
|
if( m_actions.find( current->m_action ) == m_actions.end() )
|
2006-01-02 04:07:29 +01:00
|
|
|
{
|
|
|
|
return false; // we've been tasked as part of a group but we can't do this action
|
|
|
|
}
|
2006-12-21 15:57:13 +01:00
|
|
|
CEventGeneric evt( current->m_target_entity, current->m_action );
|
2005-12-29 09:42:44 +01:00
|
|
|
if( !m_actor ) return( false );
|
2006-12-21 15:57:13 +01:00
|
|
|
SEntityAction& action_obj = m_actions[current->m_action];
|
2007-05-02 14:07:08 +02:00
|
|
|
return( ProcessContactActionNoPathing( current, timestep_millis, action_obj.m_Animation, &evt, &action_obj ) );
|
2005-12-29 09:42:44 +01: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
|
|
|
bool CEntity::ProcessGoto( CEntityOrder* current, int UNUSED(timestep_millis) )
|
2004-05-28 04:57:50 +02:00
|
|
|
{
|
2004-07-20 21:30:35 +02:00
|
|
|
CVector2D pos( m_position.X, m_position.Z );
|
2006-12-21 15:57:13 +01:00
|
|
|
CVector2D path_to = current->m_target_location;
|
2007-05-02 14:07:08 +02:00
|
|
|
float Distance = ( path_to - pos ).Length();
|
2006-01-05 07:13:31 +01:00
|
|
|
|
2007-06-10 20:12:47 +02:00
|
|
|
CEntityOrder::EOrderSource source = current->m_source;
|
|
|
|
m_orderQueue.pop_front();
|
|
|
|
// pop_front may delete 'current', so we mustn't use it after this point
|
|
|
|
|
2004-07-23 12:56:52 +02:00
|
|
|
// Let's just check we're going somewhere...
|
2006-01-05 07:13:31 +01:00
|
|
|
if( Distance < 0.1f )
|
2006-01-16 11:56:47 +01:00
|
|
|
{
|
2006-07-12 18:18:31 +02:00
|
|
|
//entf_clear(ENTF_IS_RUNNING);
|
|
|
|
//entf_clear(ENTF_SHOULD_RUN);
|
2004-07-20 21:30:35 +02:00
|
|
|
return( false );
|
2006-01-16 11:56:47 +01:00
|
|
|
}
|
2004-07-23 12:56:52 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
ChooseMovementSpeed( Distance );
|
2004-07-23 12:56:52 +02:00
|
|
|
|
|
|
|
// The pathfinder will push its result back into this unit's queue.
|
|
|
|
|
2007-06-10 20:12:47 +02:00
|
|
|
g_Pathfinder.RequestPath( me, path_to, source );
|
2004-08-03 01:14:54 +02:00
|
|
|
|
2004-05-28 04:57:50 +02:00
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
bool CEntity::ProcessGotoWaypoint( CEntityOrder* current, int UNUSED(timestep_milli), bool contact )
|
2006-04-09 02:36:52 +02:00
|
|
|
{
|
|
|
|
CVector2D pos( m_position.X, m_position.Z );
|
2006-12-21 15:57:13 +01:00
|
|
|
CVector2D path_to = current->m_target_location;
|
2007-05-02 14:07:08 +02:00
|
|
|
float Distance = ( path_to - pos ).Length();
|
2006-04-09 02:36:52 +02:00
|
|
|
|
2007-06-10 20:12:47 +02:00
|
|
|
CEntityOrder::EOrderSource source = current->m_source;
|
|
|
|
float pathfinder_radius = current->m_pathfinder_radius;
|
|
|
|
m_orderQueue.pop_front();
|
|
|
|
// pop_front may delete 'current', so we mustn't use it after this point
|
|
|
|
|
2006-04-09 02:36:52 +02:00
|
|
|
// Let's just check we're going somewhere...
|
|
|
|
if( Distance < 0.1f )
|
|
|
|
{
|
2006-07-12 18:18:31 +02:00
|
|
|
entf_clear(ENTF_IS_RUNNING);
|
|
|
|
//entf_clear(ENTF_SHOULD_RUN);
|
2006-04-09 02:36:52 +02:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
ChooseMovementSpeed( Distance );
|
2006-04-11 00:05:21 +02:00
|
|
|
|
2007-10-10 21:35:23 +02:00
|
|
|
//Kai: invoking triangulation or original A* pathfinding
|
|
|
|
if(g_TriPathfind)
|
|
|
|
{
|
|
|
|
g_Pathfinder.RequestTriangulationPath( me, path_to, contact, pathfinder_radius, source );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_Pathfinder.RequestLowLevelPath( me, path_to, contact, pathfinder_radius, source );
|
|
|
|
}
|
|
|
|
|
2007-10-09 09:27:45 +02:00
|
|
|
|
2007-10-10 21:35:23 +02:00
|
|
|
|
2006-04-09 02:36:52 +02:00
|
|
|
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
bool CEntity::ProcessPatrol( CEntityOrder* current, int UNUSED(timestep_millis) )
|
2004-05-28 04:57:50 +02:00
|
|
|
{
|
|
|
|
CEntityOrder this_segment;
|
|
|
|
CEntityOrder repeat_patrol;
|
2004-07-23 12:56:52 +02:00
|
|
|
|
|
|
|
// Duplicate the patrol order, push one copy onto the start of our order queue
|
|
|
|
// (that's the path we'll be taking next) and one copy onto the end of the
|
|
|
|
// queue (to keep us patrolling)
|
|
|
|
|
2004-05-28 04:57:50 +02:00
|
|
|
this_segment.m_type = CEntityOrder::ORDER_GOTO;
|
2006-12-21 15:57:13 +01:00
|
|
|
this_segment.m_pathfinder_radius = current->m_pathfinder_radius;
|
2004-05-28 04:57:50 +02:00
|
|
|
repeat_patrol.m_type = CEntityOrder::ORDER_PATROL;
|
2006-12-21 15:57:13 +01:00
|
|
|
repeat_patrol.m_pathfinder_radius = current->m_pathfinder_radius;
|
2004-05-28 04:57:50 +02:00
|
|
|
m_orderQueue.pop_front();
|
|
|
|
m_orderQueue.push_front( this_segment );
|
|
|
|
m_orderQueue.push_back( repeat_patrol );
|
|
|
|
return( true );
|
2004-06-02 18:11:32 +02:00
|
|
|
}
|
2006-02-22 23:45:16 +01:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
bool CEntity::ProcessProduce( CEntityOrder* order )
|
2006-02-22 23:45:16 +01:00
|
|
|
{
|
2006-12-21 15:57:13 +01:00
|
|
|
CEventStartProduction evt( order->m_produce_type, order->m_produce_name );
|
2006-02-22 23:45:16 +01:00
|
|
|
if( DispatchEvent( &evt ) && evt.GetTime() >= 0 )
|
|
|
|
{
|
2006-12-21 15:57:13 +01:00
|
|
|
m_productionQueue->AddItem( order->m_produce_type, order->m_produce_name, evt.GetTime() );
|
2006-02-22 23:45:16 +01:00
|
|
|
}
|
|
|
|
return( false );
|
|
|
|
}
|