2010-01-09 20:20:14 +01:00
|
|
|
/* Copyright (C) 2010 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "simulation2/system/Component.h"
|
|
|
|
#include "ICmpUnitMotion.h"
|
|
|
|
|
|
|
|
#include "ICmpPosition.h"
|
2010-01-29 22:13:18 +01:00
|
|
|
#include "ICmpPathfinder.h"
|
2010-01-09 20:20:14 +01:00
|
|
|
#include "simulation2/MessageTypes.h"
|
|
|
|
|
|
|
|
class CCmpUnitMotion : public ICmpUnitMotion
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void ClassInit(CComponentManager& componentManager)
|
|
|
|
{
|
2010-01-22 21:03:14 +01:00
|
|
|
componentManager.SubscribeToMessageType(MT_Update);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DEFAULT_COMPONENT_ALLOCATOR(UnitMotion)
|
|
|
|
|
2010-01-30 14:11:58 +01:00
|
|
|
const CSimContext* m_Context;
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
// Template state:
|
2010-02-10 20:28:46 +01:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
CFixed_23_8 m_Speed; // in units per second
|
|
|
|
|
|
|
|
// Dynamic state:
|
2010-02-10 20:28:46 +01:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
bool m_HasTarget;
|
2010-01-30 14:11:58 +01:00
|
|
|
ICmpPathfinder::Path m_Path;
|
2010-02-12 23:46:53 +01:00
|
|
|
// These values contain undefined junk if !HasTarget:
|
|
|
|
entity_pos_t m_TargetX, m_TargetZ; // currently-selected waypoint
|
|
|
|
entity_pos_t m_FinalTargetX, m_FinalTargetZ; // final target center (used to face towards it)
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-02-10 20:28:46 +01:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
IDLE,
|
|
|
|
WALKING,
|
|
|
|
STOPPING
|
|
|
|
};
|
|
|
|
int m_State;
|
|
|
|
|
2010-04-09 21:02:39 +02:00
|
|
|
static std::string GetSchema()
|
|
|
|
{
|
|
|
|
return
|
2010-04-23 18:09:03 +02:00
|
|
|
"<a:help>Provides the unit with the ability to move around the world by itself.</a:help>"
|
|
|
|
"<a:example>"
|
|
|
|
"<WalkSpeed>7.0</WalkSpeed>"
|
|
|
|
"</a:example>"
|
|
|
|
"<element name='WalkSpeed' a:help='Basic movement speed (in metres per second)'>"
|
2010-04-09 21:02:39 +02:00
|
|
|
"<ref name='positiveDecimal'/>"
|
|
|
|
"</element>";
|
|
|
|
}
|
2010-04-23 18:09:03 +02:00
|
|
|
|
2010-02-07 21:06:16 +01:00
|
|
|
virtual void Init(const CSimContext& context, const CParamNode& paramNode)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-01-30 14:11:58 +01:00
|
|
|
m_Context = &context;
|
2010-01-09 20:20:14 +01:00
|
|
|
m_HasTarget = false;
|
2010-02-07 21:06:16 +01:00
|
|
|
|
|
|
|
m_Speed = paramNode.GetChild("WalkSpeed").ToFixed();
|
2010-02-10 20:28:46 +01:00
|
|
|
|
|
|
|
m_State = IDLE;
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Deinit(const CSimContext& UNUSED(context))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Serialize(ISerializer& serialize)
|
|
|
|
{
|
|
|
|
serialize.Bool("has target", m_HasTarget);
|
|
|
|
if (m_HasTarget)
|
|
|
|
{
|
2010-01-30 14:11:58 +01:00
|
|
|
// TODO: m_Path
|
2010-01-09 20:20:14 +01:00
|
|
|
serialize.NumberFixed_Unbounded("target x", m_TargetX);
|
|
|
|
serialize.NumberFixed_Unbounded("target z", m_TargetZ);
|
2010-02-12 23:46:53 +01:00
|
|
|
// TODO: m_FinalTargetAngle
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
2010-02-10 20:28:46 +01:00
|
|
|
|
|
|
|
// TODO: m_State
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Deserialize(const CSimContext& context, const CParamNode& paramNode, IDeserializer& deserialize)
|
|
|
|
{
|
|
|
|
Init(context, paramNode);
|
|
|
|
|
|
|
|
deserialize.Bool(m_HasTarget);
|
|
|
|
if (m_HasTarget)
|
|
|
|
{
|
|
|
|
deserialize.NumberFixed_Unbounded(m_TargetX);
|
|
|
|
deserialize.NumberFixed_Unbounded(m_TargetZ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-22 21:03:14 +01:00
|
|
|
virtual void HandleMessage(const CSimContext& context, const CMessage& msg, bool UNUSED(global))
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
|
|
|
switch (msg.GetType())
|
|
|
|
{
|
|
|
|
case MT_Update:
|
|
|
|
{
|
|
|
|
CFixed_23_8 dt = static_cast<const CMessageUpdate&> (msg).turnLength;
|
2010-02-10 20:28:46 +01:00
|
|
|
|
|
|
|
if (m_State == STOPPING)
|
|
|
|
{
|
2010-02-12 23:46:53 +01:00
|
|
|
m_State = IDLE;
|
2010-02-10 20:28:46 +01:00
|
|
|
CMessageMotionChanged msg(CFixed_23_8::FromInt(0));
|
|
|
|
context.GetComponentManager().PostMessage(GetEntityId(), msg);
|
|
|
|
}
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
Move(context, dt);
|
2010-02-10 20:28:46 +01:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-10 20:28:46 +01:00
|
|
|
void SwitchState(const CSimContext& context, int state)
|
|
|
|
{
|
|
|
|
debug_assert(state == IDLE || state == WALKING);
|
|
|
|
|
|
|
|
// IDLE -> IDLE -- no change
|
|
|
|
// IDLE -> WALKING -- send a MotionChanged message
|
|
|
|
// WALKING -> IDLE -- set to STOPPING, so we'll send MotionChanged in the next Update
|
|
|
|
// WALKING -> WALKING -- no change
|
|
|
|
// STOPPING -> IDLE -- stay in STOPPING
|
|
|
|
// STOPPING -> WALKING -- set to WALKING, send no messages
|
|
|
|
|
|
|
|
if (m_State == IDLE && state == WALKING)
|
|
|
|
{
|
|
|
|
CMessageMotionChanged msg(m_Speed);
|
|
|
|
context.GetComponentManager().PostMessage(GetEntityId(), msg);
|
|
|
|
m_State = WALKING;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_State == WALKING && state == IDLE)
|
|
|
|
{
|
|
|
|
m_State = STOPPING;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_State == STOPPING && state == IDLE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_State == STOPPING && state == WALKING)
|
|
|
|
{
|
|
|
|
m_State = WALKING;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-06 21:58:48 +01:00
|
|
|
virtual void MoveToPoint(entity_pos_t x, entity_pos_t z, entity_pos_t minRadius, entity_pos_t maxRadius)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-01-30 14:11:58 +01:00
|
|
|
CmpPtr<ICmpPathfinder> cmpPathfinder (*m_Context, SYSTEM_ENTITY);
|
|
|
|
if (cmpPathfinder.null())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CmpPtr<ICmpPosition> cmpPosition(*m_Context, GetEntityId());
|
|
|
|
if (cmpPosition.null())
|
|
|
|
return;
|
|
|
|
|
2010-02-10 20:28:46 +01:00
|
|
|
SwitchState(*m_Context, WALKING);
|
|
|
|
|
2010-01-30 14:11:58 +01:00
|
|
|
CFixedVector3D pos = cmpPosition->GetPosition();
|
|
|
|
|
|
|
|
m_Path.m_Waypoints.clear();
|
|
|
|
|
2010-02-06 21:58:48 +01:00
|
|
|
// u32 cost;
|
|
|
|
// entity_pos_t r = entity_pos_t::FromInt(0); // TODO: should get this from the entity's size
|
|
|
|
// if (cmpPathfinder->CanMoveStraight(pos.X, pos.Z, x, z, r, cost))
|
|
|
|
// {
|
|
|
|
// m_TargetX = x;
|
|
|
|
// m_TargetZ = z;
|
|
|
|
// m_HasTarget = true;
|
|
|
|
// }
|
|
|
|
// else
|
2010-01-30 14:11:58 +01:00
|
|
|
{
|
2010-02-06 21:58:48 +01:00
|
|
|
ICmpPathfinder::Goal goal;
|
|
|
|
goal.x = x;
|
|
|
|
goal.z = z;
|
|
|
|
goal.minRadius = minRadius;
|
|
|
|
goal.maxRadius = maxRadius;
|
|
|
|
cmpPathfinder->SetDebugPath(pos.X, pos.Z, goal);
|
|
|
|
cmpPathfinder->ComputePath(pos.X, pos.Z, goal, m_Path);
|
2010-02-07 21:06:16 +01:00
|
|
|
|
|
|
|
// If there's no waypoints then we've stopped already, otherwise move to the first one
|
|
|
|
if (m_Path.m_Waypoints.empty())
|
2010-02-10 20:28:46 +01:00
|
|
|
{
|
|
|
|
m_HasTarget = false;
|
|
|
|
SwitchState(*m_Context, IDLE);
|
|
|
|
}
|
2010-02-07 21:06:16 +01:00
|
|
|
else
|
2010-02-10 20:28:46 +01:00
|
|
|
{
|
2010-02-12 23:46:53 +01:00
|
|
|
m_FinalTargetX = x;
|
|
|
|
m_FinalTargetZ = z;
|
2010-01-30 14:11:58 +01:00
|
|
|
PickNextWaypoint(pos);
|
2010-02-10 20:28:46 +01:00
|
|
|
}
|
2010-01-30 14:11:58 +01:00
|
|
|
}
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2010-02-07 21:06:16 +01:00
|
|
|
virtual CFixed_23_8 GetSpeed()
|
|
|
|
{
|
|
|
|
return m_Speed;
|
|
|
|
}
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
void Move(const CSimContext& context, CFixed_23_8 dt);
|
2010-01-30 14:11:58 +01:00
|
|
|
|
|
|
|
void PickNextWaypoint(const CFixedVector3D& pos);
|
2010-01-09 20:20:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_COMPONENT_TYPE(UnitMotion)
|
|
|
|
|
|
|
|
|
|
|
|
void CCmpUnitMotion::Move(const CSimContext& context, CFixed_23_8 dt)
|
|
|
|
{
|
|
|
|
if (!m_HasTarget)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CmpPtr<ICmpPosition> cmpPosition(context, GetEntityId());
|
|
|
|
if (cmpPosition.null())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CFixedVector3D pos = cmpPosition->GetPosition();
|
|
|
|
pos.Y = CFixed_23_8::FromInt(0); // remove Y so it doesn't influence our distance calculations
|
|
|
|
|
2010-01-30 14:11:58 +01:00
|
|
|
// We want to move (at most) m_Speed*dt units from pos towards the next waypoint
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-01-30 14:11:58 +01:00
|
|
|
while (dt > CFixed_23_8::FromInt(0))
|
|
|
|
{
|
|
|
|
CFixedVector3D target(m_TargetX, CFixed_23_8::FromInt(0), m_TargetZ);
|
|
|
|
CFixedVector3D offset = target - pos;
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-01-30 14:11:58 +01:00
|
|
|
// Face towards the target
|
|
|
|
entity_angle_t angle = atan2_approx(offset.X, offset.Z);
|
2010-02-10 20:28:46 +01:00
|
|
|
cmpPosition->TurnTo(angle);
|
|
|
|
|
|
|
|
// Work out how far we can travel in dt
|
|
|
|
CFixed_23_8 maxdist = m_Speed.Multiply(dt);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-02-10 20:28:46 +01:00
|
|
|
// If the target is close, we can move there directly
|
2010-01-30 14:11:58 +01:00
|
|
|
if (offset.Length() <= maxdist)
|
|
|
|
{
|
|
|
|
// If we've reached the last waypoint, stop
|
|
|
|
if (m_Path.m_Waypoints.empty())
|
|
|
|
{
|
|
|
|
cmpPosition->MoveTo(target.X, target.Z);
|
2010-02-12 23:46:53 +01:00
|
|
|
|
|
|
|
// If we didn't reach the final goal, point towards it now
|
|
|
|
if (target.X != m_FinalTargetX || target.Z != m_FinalTargetZ)
|
|
|
|
{
|
|
|
|
CFixedVector3D final(m_FinalTargetX, CFixed_23_8::FromInt(0), m_FinalTargetZ);
|
|
|
|
CFixedVector3D finalOffset = final - target;
|
|
|
|
entity_angle_t angle = atan2_approx(finalOffset.X, finalOffset.Z);
|
|
|
|
cmpPosition->TurnTo(angle);
|
|
|
|
}
|
|
|
|
|
2010-01-30 14:11:58 +01:00
|
|
|
m_HasTarget = false;
|
2010-02-10 20:28:46 +01:00
|
|
|
SwitchState(context, IDLE);
|
2010-01-30 14:11:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, spend the rest of the time heading towards the next waypoint
|
2010-02-10 20:28:46 +01:00
|
|
|
dt = dt - (offset.Length() / m_Speed);
|
2010-01-30 14:11:58 +01:00
|
|
|
pos = target;
|
|
|
|
PickNextWaypoint(pos);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Not close enough, so just move in the right direction
|
|
|
|
offset.Normalize(maxdist);
|
|
|
|
pos += offset;
|
|
|
|
cmpPosition->MoveTo(pos.X, pos.Z);
|
|
|
|
return;
|
|
|
|
}
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
2010-01-30 14:11:58 +01:00
|
|
|
}
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-01-30 14:11:58 +01:00
|
|
|
void CCmpUnitMotion::PickNextWaypoint(const CFixedVector3D& pos)
|
|
|
|
{
|
|
|
|
// We can always pick the immediate next waypoint
|
|
|
|
debug_assert(!m_Path.m_Waypoints.empty());
|
|
|
|
m_TargetX = m_Path.m_Waypoints.back().x;
|
|
|
|
m_TargetZ = m_Path.m_Waypoints.back().z;
|
|
|
|
m_Path.m_Waypoints.pop_back();
|
|
|
|
m_HasTarget = true;
|
|
|
|
|
|
|
|
// To smooth the motion and avoid grid-constrained motion, we could try picking some
|
|
|
|
// subsequent waypoints instead, if we can reach them without hitting any obstacles
|
|
|
|
|
|
|
|
CmpPtr<ICmpPathfinder> cmpPathfinder (*m_Context, SYSTEM_ENTITY);
|
|
|
|
if (cmpPathfinder.null())
|
|
|
|
return;
|
2010-01-29 22:13:18 +01:00
|
|
|
|
2010-01-30 14:11:58 +01:00
|
|
|
for (size_t i = 0; i < 3 && !m_Path.m_Waypoints.empty(); ++i)
|
|
|
|
{
|
|
|
|
u32 cost;
|
|
|
|
entity_pos_t r = entity_pos_t::FromInt(0); // TODO: should get this from the entity's size
|
|
|
|
if (!cmpPathfinder->CanMoveStraight(pos.X, pos.Z, m_Path.m_Waypoints.back().x, m_Path.m_Waypoints.back().z, r, cost))
|
|
|
|
break;
|
|
|
|
m_TargetX = m_Path.m_Waypoints.back().x;
|
|
|
|
m_TargetZ = m_Path.m_Waypoints.back().z;
|
|
|
|
m_Path.m_Waypoints.pop_back();
|
|
|
|
}
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|