forked from 0ad/0ad
Expose Footprint::GetShape and TemplateManager::FindAllTemplates to scripts.
Make CSimContext accessible to all component code, so they don't all have to save their own copies of it. This was SVN commit r7490.
This commit is contained in:
parent
c5f8ca32a2
commit
4fe991074f
@ -217,3 +217,8 @@ template<> jsval ScriptInterface::ToJSVal<std::vector<u32> >(JSContext* cx, cons
|
||||
{
|
||||
return ToJSVal_vector(cx, val);
|
||||
}
|
||||
|
||||
template<> jsval ScriptInterface::ToJSVal<std::vector<std::wstring> >(JSContext* cx, const std::vector<std::wstring>& val)
|
||||
{
|
||||
return ToJSVal_vector(cx, val);
|
||||
}
|
||||
|
@ -31,8 +31,6 @@ public:
|
||||
|
||||
DEFAULT_COMPONENT_ALLOCATOR(CommandQueue)
|
||||
|
||||
const CSimContext* m_Context; // never NULL (after Init/Deserialize)
|
||||
|
||||
struct Command
|
||||
{
|
||||
int player;
|
||||
@ -46,9 +44,8 @@ public:
|
||||
return "<a:component type='system'/><empty/>";
|
||||
}
|
||||
|
||||
virtual void Init(const CSimContext& context, const CParamNode& UNUSED(paramNode))
|
||||
virtual void Init(const CSimContext& UNUSED(context), const CParamNode& UNUSED(paramNode))
|
||||
{
|
||||
m_Context = &context;
|
||||
}
|
||||
|
||||
virtual void Deinit(const CSimContext& UNUSED(context))
|
||||
@ -67,9 +64,7 @@ public:
|
||||
|
||||
virtual void Deserialize(const CSimContext& context, const CParamNode& UNUSED(paramNode), IDeserializer& deserialize)
|
||||
{
|
||||
m_Context = &context;
|
||||
|
||||
JSContext* cx = m_Context->GetComponentManager().GetScriptInterface().GetContext();
|
||||
JSContext* cx = context.GetScriptInterface().GetContext();
|
||||
|
||||
u32 numCmds;
|
||||
deserialize.NumberU32_Unbounded(numCmds);
|
||||
@ -86,7 +81,7 @@ public:
|
||||
|
||||
virtual void PushClientCommand(int player, CScriptVal cmd)
|
||||
{
|
||||
JSContext* cx = m_Context->GetComponentManager().GetScriptInterface().GetContext();
|
||||
JSContext* cx = GetSimContext().GetScriptInterface().GetContext();
|
||||
|
||||
Command c = { player, CScriptValRooted(cx, cmd) };
|
||||
m_CmdQueue.push_back(c);
|
||||
@ -94,7 +89,7 @@ public:
|
||||
|
||||
virtual void ProcessCommands()
|
||||
{
|
||||
ScriptInterface& scriptInterface = m_Context->GetComponentManager().GetScriptInterface();
|
||||
ScriptInterface& scriptInterface = GetSimContext().GetScriptInterface();
|
||||
|
||||
for (size_t i = 0; i < m_CmdQueue.size(); ++i)
|
||||
{
|
||||
|
@ -35,8 +35,6 @@ public:
|
||||
|
||||
DEFAULT_COMPONENT_ALLOCATOR(Footprint)
|
||||
|
||||
const CSimContext* m_Context;
|
||||
|
||||
EShape m_Shape;
|
||||
CFixed_23_8 m_Size0; // width/radius
|
||||
CFixed_23_8 m_Size1; // height/radius
|
||||
@ -75,10 +73,8 @@ public:
|
||||
"</element>";
|
||||
}
|
||||
|
||||
virtual void Init(const CSimContext& context, const CParamNode& paramNode)
|
||||
virtual void Init(const CSimContext& UNUSED(context), const CParamNode& paramNode)
|
||||
{
|
||||
m_Context = &context;
|
||||
|
||||
if (paramNode.GetChild("Square").IsOk())
|
||||
{
|
||||
m_Shape = SQUARE;
|
||||
@ -130,17 +126,17 @@ public:
|
||||
|
||||
CFixedVector3D error(CFixed_23_8::FromInt(-1), CFixed_23_8::FromInt(-1), CFixed_23_8::FromInt(-1));
|
||||
|
||||
CmpPtr<ICmpPosition> cmpPosition(*m_Context, GetEntityId());
|
||||
CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), GetEntityId());
|
||||
if (cmpPosition.null() || !cmpPosition->IsInWorld())
|
||||
return error;
|
||||
|
||||
CmpPtr<ICmpObstructionManager> cmpObstructionManager(*m_Context, SYSTEM_ENTITY);
|
||||
CmpPtr<ICmpObstructionManager> cmpObstructionManager(GetSimContext(), SYSTEM_ENTITY);
|
||||
if (cmpObstructionManager.null())
|
||||
return error;
|
||||
|
||||
entity_pos_t spawnedRadius;
|
||||
|
||||
CmpPtr<ICmpObstruction> cmpSpawnedObstruction(*m_Context, spawned);
|
||||
CmpPtr<ICmpObstruction> cmpSpawnedObstruction(GetSimContext(), spawned);
|
||||
if (!cmpSpawnedObstruction.null())
|
||||
spawnedRadius = cmpSpawnedObstruction->GetUnitRadius();
|
||||
// else zero
|
||||
|
@ -41,8 +41,6 @@ public:
|
||||
|
||||
DEFAULT_COMPONENT_ALLOCATOR(Obstruction)
|
||||
|
||||
const CSimContext* m_Context;
|
||||
|
||||
// Template state:
|
||||
|
||||
enum {
|
||||
@ -85,10 +83,8 @@ public:
|
||||
"</optional>";
|
||||
}
|
||||
|
||||
virtual void Init(const CSimContext& context, const CParamNode& paramNode)
|
||||
virtual void Init(const CSimContext& UNUSED(context), const CParamNode& paramNode)
|
||||
{
|
||||
m_Context = &context;
|
||||
|
||||
if (paramNode.GetChild("Unit").IsOk())
|
||||
{
|
||||
m_Type = UNIT;
|
||||
@ -208,13 +204,13 @@ public:
|
||||
|
||||
virtual bool CheckCollisions()
|
||||
{
|
||||
CmpPtr<ICmpPosition> cmpPosition(*m_Context, GetEntityId());
|
||||
CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), GetEntityId());
|
||||
if (cmpPosition.null())
|
||||
return false;
|
||||
|
||||
CFixedVector3D pos = cmpPosition->GetPosition();
|
||||
|
||||
CmpPtr<ICmpObstructionManager> cmpObstructionManager(*m_Context, SYSTEM_ENTITY);
|
||||
CmpPtr<ICmpObstructionManager> cmpObstructionManager(GetSimContext(), SYSTEM_ENTITY);
|
||||
|
||||
SkipTagObstructionFilter filter(m_Tag); // ignore collisions with self
|
||||
|
||||
|
@ -35,8 +35,6 @@ public:
|
||||
|
||||
DEFAULT_COMPONENT_ALLOCATOR(Ownership)
|
||||
|
||||
const CSimContext* m_Context; // never NULL (after Init/Deserialize)
|
||||
|
||||
int32_t m_Owner;
|
||||
|
||||
static std::string GetSchema()
|
||||
@ -47,10 +45,8 @@ public:
|
||||
"<empty/>";
|
||||
}
|
||||
|
||||
virtual void Init(const CSimContext& context, const CParamNode& UNUSED(paramNode))
|
||||
virtual void Init(const CSimContext& UNUSED(context), const CParamNode& UNUSED(paramNode))
|
||||
{
|
||||
m_Context = &context;
|
||||
|
||||
m_Owner = -1;
|
||||
}
|
||||
|
||||
@ -63,10 +59,8 @@ public:
|
||||
serialize.NumberI32_Unbounded("owner", m_Owner);
|
||||
}
|
||||
|
||||
virtual void Deserialize(const CSimContext& context, const CParamNode& UNUSED(paramNode), IDeserializer& deserialize)
|
||||
virtual void Deserialize(const CSimContext& UNUSED(context), const CParamNode& UNUSED(paramNode), IDeserializer& deserialize)
|
||||
{
|
||||
m_Context = &context;
|
||||
|
||||
deserialize.NumberI32_Unbounded(m_Owner);
|
||||
}
|
||||
|
||||
@ -97,7 +91,7 @@ public:
|
||||
m_Owner = playerID;
|
||||
|
||||
CMessageOwnershipChanged msg(GetEntityId(), old, playerID);
|
||||
m_Context->GetComponentManager().PostMessage(GetEntityId(), msg);
|
||||
GetSimContext().GetComponentManager().PostMessage(GetEntityId(), msg);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -87,8 +87,6 @@ public:
|
||||
|
||||
DEFAULT_COMPONENT_ALLOCATOR(Pathfinder)
|
||||
|
||||
const CSimContext* m_Context;
|
||||
|
||||
u16 m_MapSize; // tiles per side
|
||||
Grid<u8>* m_Grid; // terrain/passability information
|
||||
|
||||
@ -105,10 +103,8 @@ public:
|
||||
return "<a:component type='system'/><empty/>";
|
||||
}
|
||||
|
||||
virtual void Init(const CSimContext& context, const CParamNode& UNUSED(paramNode))
|
||||
virtual void Init(const CSimContext& UNUSED(context), const CParamNode& UNUSED(paramNode))
|
||||
{
|
||||
m_Context = &context;
|
||||
|
||||
m_MapSize = 0;
|
||||
m_Grid = NULL;
|
||||
|
||||
@ -210,14 +206,14 @@ public:
|
||||
if (!m_Grid)
|
||||
{
|
||||
// TOOD: these bits should come from ICmpTerrain
|
||||
ssize_t size = m_Context->GetTerrain().GetTilesPerSide();
|
||||
ssize_t size = GetSimContext().GetTerrain().GetTilesPerSide();
|
||||
|
||||
debug_assert(size >= 1 && size <= 0xffff); // must fit in 16 bits
|
||||
m_MapSize = size;
|
||||
m_Grid = new Grid<u8>(m_MapSize, m_MapSize);
|
||||
}
|
||||
|
||||
CmpPtr<ICmpObstructionManager> cmpObstructionManager(*m_Context, SYSTEM_ENTITY);
|
||||
CmpPtr<ICmpObstructionManager> cmpObstructionManager(GetSimContext(), SYSTEM_ENTITY);
|
||||
cmpObstructionManager->Rasterise(*m_Grid);
|
||||
}
|
||||
|
||||
@ -865,18 +861,18 @@ void CCmpPathfinder::ComputeShortPath(const IObstructionTestFilter& filter, enti
|
||||
{
|
||||
case CCmpPathfinder::Goal::POINT:
|
||||
{
|
||||
SimRender::ConstructCircleOnGround(*m_Context, goal.x.ToFloat(), goal.z.ToFloat(), 0.2f, m_DebugOverlayShortPathLines.back());
|
||||
SimRender::ConstructCircleOnGround(GetSimContext(), goal.x.ToFloat(), goal.z.ToFloat(), 0.2f, m_DebugOverlayShortPathLines.back());
|
||||
break;
|
||||
}
|
||||
case CCmpPathfinder::Goal::CIRCLE:
|
||||
{
|
||||
SimRender::ConstructCircleOnGround(*m_Context, goal.x.ToFloat(), goal.z.ToFloat(), goal.hw.ToFloat(), m_DebugOverlayShortPathLines.back());
|
||||
SimRender::ConstructCircleOnGround(GetSimContext(), goal.x.ToFloat(), goal.z.ToFloat(), goal.hw.ToFloat(), m_DebugOverlayShortPathLines.back());
|
||||
break;
|
||||
}
|
||||
case CCmpPathfinder::Goal::SQUARE:
|
||||
{
|
||||
float a = atan2(goal.v.X.ToFloat(), goal.v.Y.ToFloat());
|
||||
SimRender::ConstructSquareOnGround(*m_Context, goal.x.ToFloat(), goal.z.ToFloat(), goal.hw.ToFloat()*2, goal.hh.ToFloat()*2, a, m_DebugOverlayShortPathLines.back());
|
||||
SimRender::ConstructSquareOnGround(GetSimContext(), goal.x.ToFloat(), goal.z.ToFloat(), goal.hw.ToFloat()*2, goal.hh.ToFloat()*2, a, m_DebugOverlayShortPathLines.back());
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -925,7 +921,7 @@ void CCmpPathfinder::ComputeShortPath(const IObstructionTestFilter& filter, enti
|
||||
|
||||
|
||||
// Find all the obstruction squares that might affect us
|
||||
CmpPtr<ICmpObstructionManager> cmpObstructionManager(*m_Context, SYSTEM_ENTITY);
|
||||
CmpPtr<ICmpObstructionManager> cmpObstructionManager(GetSimContext(), SYSTEM_ENTITY);
|
||||
std::vector<ICmpObstructionManager::ObstructionSquare> squares;
|
||||
cmpObstructionManager->GetObstructionsInRange(filter, rangeXMin - r, rangeZMin - r, rangeXMax + r, rangeZMax + r, squares);
|
||||
|
||||
@ -992,7 +988,7 @@ void CCmpPathfinder::ComputeShortPath(const IObstructionTestFilter& filter, enti
|
||||
xz.push_back(edges[i].p0.Y.ToFloat());
|
||||
xz.push_back(edges[i].p1.X.ToFloat());
|
||||
xz.push_back(edges[i].p1.Y.ToFloat());
|
||||
SimRender::ConstructLineOnGround(*m_Context, xz, m_DebugOverlayShortPathLines.back());
|
||||
SimRender::ConstructLineOnGround(GetSimContext(), xz, m_DebugOverlayShortPathLines.back());
|
||||
}
|
||||
}
|
||||
|
||||
@ -1052,7 +1048,7 @@ void CCmpPathfinder::ComputeShortPath(const IObstructionTestFilter& filter, enti
|
||||
xz.push_back(vertexes[curr.id].p.Y.ToFloat());
|
||||
xz.push_back(npos.X.ToFloat());
|
||||
xz.push_back(npos.Y.ToFloat());
|
||||
SimRender::ConstructLineOnGround(*m_Context, xz, m_DebugOverlayShortPathLines.back());
|
||||
SimRender::ConstructLineOnGround(GetSimContext(), xz, m_DebugOverlayShortPathLines.back());
|
||||
//*/
|
||||
|
||||
if (visible)
|
||||
@ -1110,7 +1106,7 @@ void CCmpPathfinder::ComputeShortPath(const IObstructionTestFilter& filter, enti
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
void CCmpPathfinder::RenderSubmit(const CSimContext& context, SceneCollector& collector)
|
||||
void CCmpPathfinder::RenderSubmit(const CSimContext& UNUSED(context), SceneCollector& collector)
|
||||
{
|
||||
for (size_t i = 0; i < m_DebugOverlayShortPathLines.size(); ++i)
|
||||
collector.Submit(&m_DebugOverlayShortPathLines[i]);
|
||||
|
@ -50,8 +50,6 @@ public:
|
||||
|
||||
DEFAULT_COMPONENT_ALLOCATOR(Position)
|
||||
|
||||
const CSimContext* m_Context; // never NULL (after Init/Deserialize)
|
||||
|
||||
// Template state:
|
||||
|
||||
enum
|
||||
@ -97,10 +95,8 @@ public:
|
||||
"</element>";
|
||||
}
|
||||
|
||||
virtual void Init(const CSimContext& context, const CParamNode& paramNode)
|
||||
virtual void Init(const CSimContext& UNUSED(context), const CParamNode& paramNode)
|
||||
{
|
||||
m_Context = &context;
|
||||
|
||||
std::wstring anchor = paramNode.GetChild("Anchor").ToString();
|
||||
if (anchor == L"pitch")
|
||||
m_AnchorType = PITCH;
|
||||
@ -233,7 +229,7 @@ public:
|
||||
}
|
||||
|
||||
entity_pos_t ground;
|
||||
CmpPtr<ICmpTerrain> cmpTerrain(*m_Context, SYSTEM_ENTITY);
|
||||
CmpPtr<ICmpTerrain> cmpTerrain(GetSimContext(), SYSTEM_ENTITY);
|
||||
if (!cmpTerrain.null())
|
||||
{
|
||||
ground = cmpTerrain->GetGroundLevel(m_X, m_Z);
|
||||
@ -302,7 +298,7 @@ public:
|
||||
GetInterpolatedPosition2D(frameOffset, x, z, rotY);
|
||||
|
||||
float ground = 0;
|
||||
CmpPtr<ICmpTerrain> cmpTerrain(*m_Context, SYSTEM_ENTITY);
|
||||
CmpPtr<ICmpTerrain> cmpTerrain(GetSimContext(), SYSTEM_ENTITY);
|
||||
if (!cmpTerrain.null())
|
||||
{
|
||||
ground = cmpTerrain->GetGroundLevel(x, z);
|
||||
@ -334,7 +330,7 @@ public:
|
||||
return mXZ;
|
||||
}
|
||||
|
||||
virtual void HandleMessage(const CSimContext& context, const CMessage& msg, bool UNUSED(global))
|
||||
virtual void HandleMessage(const CSimContext& UNUSED(context), const CMessage& msg, bool UNUSED(global))
|
||||
{
|
||||
switch (msg.GetType())
|
||||
{
|
||||
@ -372,12 +368,12 @@ private:
|
||||
if (m_InWorld)
|
||||
{
|
||||
CMessagePositionChanged msg(true, m_X, m_Z, m_RotY);
|
||||
m_Context->GetComponentManager().PostMessage(GetEntityId(), msg);
|
||||
GetSimContext().GetComponentManager().PostMessage(GetEntityId(), msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
CMessagePositionChanged msg(false, entity_pos_t(), entity_pos_t(), entity_angle_t());
|
||||
m_Context->GetComponentManager().PostMessage(GetEntityId(), msg);
|
||||
GetSimContext().GetComponentManager().PostMessage(GetEntityId(), msg);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -50,9 +50,8 @@ public:
|
||||
return "<a:component type='system'/><empty/>";
|
||||
}
|
||||
|
||||
virtual void Init(const CSimContext& context, const CParamNode& UNUSED(paramNode))
|
||||
virtual void Init(const CSimContext& UNUSED(context), const CParamNode& UNUSED(paramNode))
|
||||
{
|
||||
m_Context = &context;
|
||||
}
|
||||
|
||||
virtual void Deinit(const CSimContext& UNUSED(context))
|
||||
@ -101,8 +100,6 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
const CSimContext* m_Context;
|
||||
|
||||
struct Projectile
|
||||
{
|
||||
CUnit* unit;
|
||||
@ -129,10 +126,10 @@ REGISTER_COMPONENT_TYPE(ProjectileManager)
|
||||
|
||||
void CCmpProjectileManager::LaunchProjectile(entity_id_t source, CFixedVector3D targetPoint, entity_id_t targetEnt, CFixed_23_8 speed, CFixed_23_8 gravity)
|
||||
{
|
||||
if (!m_Context->HasUnitManager())
|
||||
if (!GetSimContext().HasUnitManager())
|
||||
return; // do nothing if graphics are disabled
|
||||
|
||||
CmpPtr<ICmpVisual> sourceVisual(*m_Context, source);
|
||||
CmpPtr<ICmpVisual> sourceVisual(GetSimContext(), source);
|
||||
if (sourceVisual.null())
|
||||
return;
|
||||
|
||||
@ -146,14 +143,14 @@ void CCmpProjectileManager::LaunchProjectile(entity_id_t source, CFixedVector3D
|
||||
std::set<CStr> selections;
|
||||
|
||||
Projectile projectile;
|
||||
projectile.unit = m_Context->GetUnitManager().CreateUnit(name, NULL, selections);
|
||||
projectile.unit = GetSimContext().GetUnitManager().CreateUnit(name, NULL, selections);
|
||||
if (!projectile.unit)
|
||||
{
|
||||
// The error will have already been logged
|
||||
return;
|
||||
}
|
||||
|
||||
CmpPtr<ICmpPosition> sourcePos(*m_Context, source);
|
||||
CmpPtr<ICmpPosition> sourcePos(GetSimContext(), source);
|
||||
if (sourcePos.null())
|
||||
return;
|
||||
|
||||
@ -168,7 +165,7 @@ void CCmpProjectileManager::LaunchProjectile(entity_id_t source, CFixedVector3D
|
||||
}
|
||||
else
|
||||
{
|
||||
CmpPtr<ICmpPosition> targetPos(*m_Context, targetEnt);
|
||||
CmpPtr<ICmpPosition> targetPos(GetSimContext(), targetEnt);
|
||||
if (targetPos.null())
|
||||
return;
|
||||
|
||||
|
@ -35,8 +35,6 @@ public:
|
||||
|
||||
DEFAULT_COMPONENT_ALLOCATOR(SoundManager)
|
||||
|
||||
const CSimContext* m_Context;
|
||||
|
||||
std::map<std::wstring, CSoundGroup*> m_SoundGroups;
|
||||
|
||||
static std::string GetSchema()
|
||||
@ -44,9 +42,8 @@ public:
|
||||
return "<a:component type='system'/><empty/>";
|
||||
}
|
||||
|
||||
virtual void Init(const CSimContext& context, const CParamNode& UNUSED(paramNode))
|
||||
virtual void Init(const CSimContext& UNUSED(context), const CParamNode& UNUSED(paramNode))
|
||||
{
|
||||
m_Context = &context;
|
||||
}
|
||||
|
||||
virtual void Deinit(const CSimContext& UNUSED(context))
|
||||
@ -116,7 +113,7 @@ public:
|
||||
CVector3D sourcePos(0, 0, 0);
|
||||
if (source != INVALID_ENTITY)
|
||||
{
|
||||
CmpPtr<ICmpPosition> cmpPosition(*m_Context, source);
|
||||
CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), source);
|
||||
if (!cmpPosition.null() && cmpPosition->IsInWorld())
|
||||
sourcePos = CVector3D(cmpPosition->GetPosition());
|
||||
}
|
||||
|
@ -53,8 +53,6 @@ public:
|
||||
|
||||
DEFAULT_COMPONENT_ALLOCATOR(UnitMotion)
|
||||
|
||||
const CSimContext* m_Context;
|
||||
|
||||
bool m_DebugOverlayEnabled;
|
||||
std::vector<SOverlayLine> m_DebugOverlayLines;
|
||||
std::vector<SOverlayLine> m_DebugOverlayShortPathLines;
|
||||
@ -95,7 +93,6 @@ public:
|
||||
|
||||
virtual void Init(const CSimContext& context, const CParamNode& paramNode)
|
||||
{
|
||||
m_Context = &context;
|
||||
m_HasTarget = false;
|
||||
|
||||
m_Speed = paramNode.GetChild("WalkSpeed").ToFixed();
|
||||
@ -150,14 +147,14 @@ public:
|
||||
context.GetComponentManager().PostMessage(GetEntityId(), msg);
|
||||
}
|
||||
|
||||
Move(context, dt);
|
||||
Move(dt);
|
||||
|
||||
break;
|
||||
}
|
||||
case MT_RenderSubmit:
|
||||
{
|
||||
const CMessageRenderSubmit& msgData = static_cast<const CMessageRenderSubmit&> (msg);
|
||||
RenderSubmit(context, msgData.collector);
|
||||
RenderSubmit(msgData.collector);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -173,8 +170,8 @@ public:
|
||||
m_DebugOverlayEnabled = enabled;
|
||||
if (enabled)
|
||||
{
|
||||
RenderPath(*m_Context, m_Path, m_DebugOverlayLines, OVERLAY_COLOUR_PATH);
|
||||
RenderPath(*m_Context, m_ShortPath, m_DebugOverlayShortPathLines, OVERLAY_COLOUR_SHORT_PATH);
|
||||
RenderPath(m_Path, m_DebugOverlayLines, OVERLAY_COLOUR_PATH);
|
||||
RenderPath(m_ShortPath, m_DebugOverlayShortPathLines, OVERLAY_COLOUR_SHORT_PATH);
|
||||
}
|
||||
}
|
||||
|
||||
@ -194,7 +191,7 @@ private:
|
||||
/**
|
||||
* Do the per-turn movement and other updates
|
||||
*/
|
||||
void Move(const CSimContext& context, CFixed_23_8 dt);
|
||||
void Move(CFixed_23_8 dt);
|
||||
|
||||
void StopAndFaceGoal(CFixedVector2D pos);
|
||||
|
||||
@ -206,7 +203,7 @@ private:
|
||||
/**
|
||||
* Change between idle/walking states; automatically sends MotionChanged messages when appropriate
|
||||
*/
|
||||
void SwitchState(const CSimContext& context, int state);
|
||||
void SwitchState(int state);
|
||||
|
||||
bool ShouldTreatTargetAsCircle(entity_pos_t range, entity_pos_t hw, entity_pos_t hh, entity_pos_t circleRadius);
|
||||
|
||||
@ -239,16 +236,16 @@ private:
|
||||
/**
|
||||
* Convert a path into a renderable list of lines
|
||||
*/
|
||||
void RenderPath(const CSimContext& context, const ICmpPathfinder::Path& path, std::vector<SOverlayLine>& lines, CColor color);
|
||||
void RenderPath(const ICmpPathfinder::Path& path, std::vector<SOverlayLine>& lines, CColor color);
|
||||
|
||||
void RenderSubmit(const CSimContext& context, SceneCollector& collector);
|
||||
void RenderSubmit(SceneCollector& collector);
|
||||
};
|
||||
|
||||
REGISTER_COMPONENT_TYPE(UnitMotion)
|
||||
|
||||
bool CCmpUnitMotion::CheckMovement(CFixedVector2D pos, CFixedVector2D target)
|
||||
{
|
||||
CmpPtr<ICmpObstructionManager> cmpObstructionManager(*m_Context, SYSTEM_ENTITY);
|
||||
CmpPtr<ICmpObstructionManager> cmpObstructionManager(GetSimContext(), SYSTEM_ENTITY);
|
||||
if (cmpObstructionManager.null())
|
||||
return false;
|
||||
|
||||
@ -279,14 +276,14 @@ bool CCmpUnitMotion::CheckMovement(CFixedVector2D pos, CFixedVector2D target)
|
||||
return true;
|
||||
}
|
||||
|
||||
void CCmpUnitMotion::Move(const CSimContext& context, CFixed_23_8 dt)
|
||||
void CCmpUnitMotion::Move(CFixed_23_8 dt)
|
||||
{
|
||||
PROFILE("Move");
|
||||
|
||||
if (!m_HasTarget)
|
||||
return;
|
||||
|
||||
CmpPtr<ICmpPosition> cmpPosition(context, GetEntityId());
|
||||
CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), GetEntityId());
|
||||
if (cmpPosition.null())
|
||||
return;
|
||||
|
||||
@ -348,7 +345,7 @@ void CCmpUnitMotion::Move(const CSimContext& context, CFixed_23_8 dt)
|
||||
|
||||
void CCmpUnitMotion::StopAndFaceGoal(CFixedVector2D pos)
|
||||
{
|
||||
SwitchState(*m_Context, IDLE);
|
||||
SwitchState(IDLE);
|
||||
FaceTowardsPoint(pos, m_FinalGoal.x, m_FinalGoal.z);
|
||||
|
||||
// TODO: if the goal was a square building, we ought to point towards the
|
||||
@ -363,14 +360,14 @@ void CCmpUnitMotion::FaceTowardsPoint(CFixedVector2D pos, entity_pos_t x, entity
|
||||
{
|
||||
entity_angle_t angle = atan2_approx(offset.X, offset.Y);
|
||||
|
||||
CmpPtr<ICmpPosition> cmpPosition(*m_Context, GetEntityId());
|
||||
CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), GetEntityId());
|
||||
if (cmpPosition.null())
|
||||
return;
|
||||
cmpPosition->TurnTo(angle);
|
||||
}
|
||||
}
|
||||
|
||||
void CCmpUnitMotion::SwitchState(const CSimContext& context, int state)
|
||||
void CCmpUnitMotion::SwitchState(int state)
|
||||
{
|
||||
debug_assert(state == IDLE || state == WALKING);
|
||||
|
||||
@ -387,7 +384,7 @@ void CCmpUnitMotion::SwitchState(const CSimContext& context, int state)
|
||||
if (state == WALKING)
|
||||
{
|
||||
CMessageMotionChanged msg(m_Speed);
|
||||
context.GetComponentManager().PostMessage(GetEntityId(), msg);
|
||||
GetSimContext().GetComponentManager().PostMessage(GetEntityId(), msg);
|
||||
}
|
||||
|
||||
if (m_State == IDLE && state == WALKING)
|
||||
@ -418,7 +415,7 @@ bool CCmpUnitMotion::MoveToPoint(entity_pos_t x, entity_pos_t z)
|
||||
{
|
||||
PROFILE("MoveToPoint");
|
||||
|
||||
CmpPtr<ICmpPosition> cmpPosition(*m_Context, GetEntityId());
|
||||
CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), GetEntityId());
|
||||
if (cmpPosition.null() || !cmpPosition->IsInWorld())
|
||||
return false;
|
||||
|
||||
@ -430,7 +427,7 @@ bool CCmpUnitMotion::MoveToPoint(entity_pos_t x, entity_pos_t z)
|
||||
|
||||
ICmpPathfinder::Goal goal;
|
||||
|
||||
CmpPtr<ICmpObstructionManager> cmpObstructionManager(*m_Context, SYSTEM_ENTITY);
|
||||
CmpPtr<ICmpObstructionManager> cmpObstructionManager(GetSimContext(), SYSTEM_ENTITY);
|
||||
if (cmpObstructionManager.null())
|
||||
return false;
|
||||
|
||||
@ -462,7 +459,7 @@ bool CCmpUnitMotion::MoveToPoint(entity_pos_t x, entity_pos_t z)
|
||||
if (!RegeneratePath(pos, false))
|
||||
return false;
|
||||
|
||||
SwitchState(*m_Context, WALKING);
|
||||
SwitchState(WALKING);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -485,7 +482,7 @@ bool CCmpUnitMotion::MoveToAttackRange(entity_id_t target, entity_pos_t minRange
|
||||
{
|
||||
PROFILE("MoveToAttackRange");
|
||||
|
||||
CmpPtr<ICmpPosition> cmpPosition(*m_Context, GetEntityId());
|
||||
CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), GetEntityId());
|
||||
if (cmpPosition.null() || !cmpPosition->IsInWorld())
|
||||
return false;
|
||||
|
||||
@ -497,13 +494,13 @@ bool CCmpUnitMotion::MoveToAttackRange(entity_id_t target, entity_pos_t minRange
|
||||
|
||||
ICmpPathfinder::Goal goal;
|
||||
|
||||
CmpPtr<ICmpObstructionManager> cmpObstructionManager(*m_Context, SYSTEM_ENTITY);
|
||||
CmpPtr<ICmpObstructionManager> cmpObstructionManager(GetSimContext(), SYSTEM_ENTITY);
|
||||
if (cmpObstructionManager.null())
|
||||
return false;
|
||||
|
||||
ICmpObstructionManager::tag_t tag = 0;
|
||||
|
||||
CmpPtr<ICmpObstruction> cmpObstruction(*m_Context, target);
|
||||
CmpPtr<ICmpObstruction> cmpObstruction(GetSimContext(), target);
|
||||
if (!cmpObstruction.null())
|
||||
tag = cmpObstruction->GetObstruction();
|
||||
|
||||
@ -611,7 +608,7 @@ bool CCmpUnitMotion::MoveToAttackRange(entity_id_t target, entity_pos_t minRange
|
||||
{
|
||||
// The target didn't have an obstruction or obstruction shape, so treat it as a point instead
|
||||
|
||||
CmpPtr<ICmpPosition> cmpTargetPosition(*m_Context, target);
|
||||
CmpPtr<ICmpPosition> cmpTargetPosition(GetSimContext(), target);
|
||||
if (cmpTargetPosition.null() || !cmpTargetPosition->IsInWorld())
|
||||
return false;
|
||||
|
||||
@ -647,7 +644,7 @@ bool CCmpUnitMotion::MoveToAttackRange(entity_id_t target, entity_pos_t minRange
|
||||
if (!RegeneratePath(pos, false))
|
||||
return false;
|
||||
|
||||
SwitchState(*m_Context, WALKING);
|
||||
SwitchState(WALKING);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -656,20 +653,20 @@ bool CCmpUnitMotion::IsInAttackRange(entity_id_t target, entity_pos_t minRange,
|
||||
// This function closely mirrors MoveToAttackRange - it needs to return true
|
||||
// after that Move has completed
|
||||
|
||||
CmpPtr<ICmpPosition> cmpPosition(*m_Context, GetEntityId());
|
||||
CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), GetEntityId());
|
||||
if (cmpPosition.null() || !cmpPosition->IsInWorld())
|
||||
return false;
|
||||
|
||||
CFixedVector3D pos3 = cmpPosition->GetPosition();
|
||||
CFixedVector2D pos (pos3.X, pos3.Z);
|
||||
|
||||
CmpPtr<ICmpObstructionManager> cmpObstructionManager(*m_Context, SYSTEM_ENTITY);
|
||||
CmpPtr<ICmpObstructionManager> cmpObstructionManager(GetSimContext(), SYSTEM_ENTITY);
|
||||
if (cmpObstructionManager.null())
|
||||
return false;
|
||||
|
||||
ICmpObstructionManager::tag_t tag = 0;
|
||||
|
||||
CmpPtr<ICmpObstruction> cmpObstruction(*m_Context, target);
|
||||
CmpPtr<ICmpObstruction> cmpObstruction(GetSimContext(), target);
|
||||
if (!cmpObstruction.null())
|
||||
tag = cmpObstruction->GetObstruction();
|
||||
|
||||
@ -707,7 +704,7 @@ bool CCmpUnitMotion::IsInAttackRange(entity_id_t target, entity_pos_t minRange,
|
||||
}
|
||||
else
|
||||
{
|
||||
CmpPtr<ICmpPosition> cmpTargetPosition(*m_Context, target);
|
||||
CmpPtr<ICmpPosition> cmpTargetPosition(GetSimContext(), target);
|
||||
if (cmpTargetPosition.null() || !cmpTargetPosition->IsInWorld())
|
||||
return false;
|
||||
|
||||
@ -724,7 +721,7 @@ bool CCmpUnitMotion::IsInAttackRange(entity_id_t target, entity_pos_t minRange,
|
||||
|
||||
bool CCmpUnitMotion::RegeneratePath(CFixedVector2D pos, bool avoidMovingUnits)
|
||||
{
|
||||
CmpPtr<ICmpPathfinder> cmpPathfinder (*m_Context, SYSTEM_ENTITY);
|
||||
CmpPtr<ICmpPathfinder> cmpPathfinder (GetSimContext(), SYSTEM_ENTITY);
|
||||
if (cmpPathfinder.null())
|
||||
return false;
|
||||
|
||||
@ -736,7 +733,7 @@ bool CCmpUnitMotion::RegeneratePath(CFixedVector2D pos, bool avoidMovingUnits)
|
||||
cmpPathfinder->ComputePath(pos.X, pos.Y, m_FinalGoal, m_Path);
|
||||
|
||||
if (m_DebugOverlayEnabled)
|
||||
RenderPath(*m_Context, m_Path, m_DebugOverlayLines, OVERLAY_COLOUR_PATH);
|
||||
RenderPath(m_Path, m_DebugOverlayLines, OVERLAY_COLOUR_PATH);
|
||||
|
||||
// If there's no waypoints then we've stopped already, otherwise move to the first one
|
||||
if (m_Path.m_Waypoints.empty())
|
||||
@ -805,7 +802,7 @@ bool CCmpUnitMotion::PickNextWaypoint(const CFixedVector2D& pos, bool avoidMovin
|
||||
goal.z = targetZ;
|
||||
}
|
||||
|
||||
CmpPtr<ICmpPathfinder> cmpPathfinder (*m_Context, SYSTEM_ENTITY);
|
||||
CmpPtr<ICmpPathfinder> cmpPathfinder (GetSimContext(), SYSTEM_ENTITY);
|
||||
if (cmpPathfinder.null())
|
||||
return false;
|
||||
|
||||
@ -821,7 +818,7 @@ bool CCmpUnitMotion::PickNextWaypoint(const CFixedVector2D& pos, bool avoidMovin
|
||||
cmpPathfinder->ComputeShortPath(*filter, pos.X, pos.Y, m_Radius, SHORT_PATH_SEARCH_RANGE, goal, m_ShortPath);
|
||||
|
||||
if (m_DebugOverlayEnabled)
|
||||
RenderPath(*m_Context, m_ShortPath, m_DebugOverlayShortPathLines, OVERLAY_COLOUR_SHORT_PATH);
|
||||
RenderPath(m_ShortPath, m_DebugOverlayShortPathLines, OVERLAY_COLOUR_SHORT_PATH);
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -847,7 +844,7 @@ bool CCmpUnitMotion::PickNextShortWaypoint(const CFixedVector2D& pos, bool avoid
|
||||
return true;
|
||||
}
|
||||
|
||||
void CCmpUnitMotion::RenderPath(const CSimContext& context, const ICmpPathfinder::Path& path, std::vector<SOverlayLine>& lines, CColor color)
|
||||
void CCmpUnitMotion::RenderPath(const ICmpPathfinder::Path& path, std::vector<SOverlayLine>& lines, CColor color)
|
||||
{
|
||||
lines.clear();
|
||||
std::vector<float> waypointCoords;
|
||||
@ -859,15 +856,15 @@ void CCmpUnitMotion::RenderPath(const CSimContext& context, const ICmpPathfinder
|
||||
waypointCoords.push_back(z);
|
||||
lines.push_back(SOverlayLine());
|
||||
lines.back().m_Color = color;
|
||||
SimRender::ConstructSquareOnGround(context, x, z, 1.0f, 1.0f, 0.0f, lines.back());
|
||||
SimRender::ConstructSquareOnGround(GetSimContext(), x, z, 1.0f, 1.0f, 0.0f, lines.back());
|
||||
}
|
||||
lines.push_back(SOverlayLine());
|
||||
lines.back().m_Color = color;
|
||||
SimRender::ConstructLineOnGround(context, waypointCoords, lines.back());
|
||||
SimRender::ConstructLineOnGround(GetSimContext(), waypointCoords, lines.back());
|
||||
|
||||
}
|
||||
|
||||
void CCmpUnitMotion::RenderSubmit(const CSimContext& UNUSED(context), SceneCollector& collector)
|
||||
void CCmpUnitMotion::RenderSubmit(SceneCollector& collector)
|
||||
{
|
||||
if (!m_DebugOverlayEnabled)
|
||||
return;
|
||||
|
@ -21,8 +21,50 @@
|
||||
|
||||
#include "simulation2/system/InterfaceScripted.h"
|
||||
|
||||
#include "simulation2/system/SimContext.h"
|
||||
#include "maths/FixedVector3D.h"
|
||||
|
||||
CScriptVal ICmpFootprint::GetShape_wrapper()
|
||||
{
|
||||
EShape shape;
|
||||
entity_pos_t size0, size1, height;
|
||||
GetShape(shape, size0, size1, height);
|
||||
|
||||
JSContext* cx = GetSimContext().GetScriptInterface().GetContext();
|
||||
|
||||
ScriptInterface::LocalRootScope scope(cx);
|
||||
if (!scope.OK())
|
||||
return JSVAL_VOID;
|
||||
|
||||
JSObject* obj = JS_NewObject(cx, NULL, NULL, NULL);
|
||||
if (!obj)
|
||||
return JSVAL_VOID;
|
||||
|
||||
if (shape == CIRCLE)
|
||||
{
|
||||
jsval ptype = ScriptInterface::ToJSVal<std::string>(cx, "circle");
|
||||
jsval pradius = ScriptInterface::ToJSVal(cx, size0);
|
||||
jsval pheight = ScriptInterface::ToJSVal(cx, height);
|
||||
JS_SetProperty(cx, obj, "type", &ptype);
|
||||
JS_SetProperty(cx, obj, "radius", &pradius);
|
||||
JS_SetProperty(cx, obj, "height", &pheight);
|
||||
}
|
||||
else
|
||||
{
|
||||
jsval ptype = ScriptInterface::ToJSVal<std::string>(cx, "square");
|
||||
jsval pwidth = ScriptInterface::ToJSVal(cx, size0);
|
||||
jsval pdepth = ScriptInterface::ToJSVal(cx, size1);
|
||||
jsval pheight = ScriptInterface::ToJSVal(cx, height);
|
||||
JS_SetProperty(cx, obj, "type", &ptype);
|
||||
JS_SetProperty(cx, obj, "width", &pwidth);
|
||||
JS_SetProperty(cx, obj, "depth", &pdepth);
|
||||
JS_SetProperty(cx, obj, "height", &pheight);
|
||||
}
|
||||
|
||||
return OBJECT_TO_JSVAL(obj);
|
||||
}
|
||||
|
||||
BEGIN_INTERFACE_WRAPPER(Footprint)
|
||||
DEFINE_INTERFACE_METHOD_1("PickSpawnPoint", CFixedVector3D, ICmpFootprint, PickSpawnPoint, entity_id_t)
|
||||
DEFINE_INTERFACE_METHOD_0("GetShape", CScriptVal, ICmpFootprint, GetShape_wrapper)
|
||||
END_INTERFACE_WRAPPER(Footprint)
|
||||
|
@ -49,6 +49,13 @@ public:
|
||||
*/
|
||||
virtual void GetShape(EShape& shape, entity_pos_t& size0, entity_pos_t& size1, entity_pos_t& height) = 0;
|
||||
|
||||
/**
|
||||
* GetShape wrapper for script calls.
|
||||
* Returns { "type": "circle", "radius": 5.0, "height": 1.0 }
|
||||
* or { "type": "square", "width": 5.0, "depth": 5.0, "height": 1.0 }
|
||||
*/
|
||||
CScriptVal GetShape_wrapper();
|
||||
|
||||
/**
|
||||
* Pick a sensible position to place a newly-spawned entity near this footprint,
|
||||
* such that it won't be in an invalid (obstructed) location regardless of the spawned unit's
|
||||
|
@ -24,4 +24,5 @@
|
||||
BEGIN_INTERFACE_WRAPPER(TemplateManager)
|
||||
DEFINE_INTERFACE_METHOD_1("GetTemplate", const CParamNode*, ICmpTemplateManager, GetTemplate, std::wstring)
|
||||
DEFINE_INTERFACE_METHOD_1("GetCurrentTemplateName", std::wstring, ICmpTemplateManager, GetCurrentTemplateName, entity_id_t)
|
||||
DEFINE_INTERFACE_METHOD_0("FindAllTemplates", std::vector<std::wstring>, ICmpTemplateManager, FindAllTemplates)
|
||||
END_INTERFACE_WRAPPER(TemplateManager)
|
||||
|
@ -241,7 +241,7 @@ created before anything else and therefore may be used, and that the components
|
||||
processed in the order determined by TypeList.h.
|
||||
|
||||
The same @c context object will be used in all these calls.
|
||||
(The component could safely store it in a <code>CSimContext* m_Context</code> member if necessary.)
|
||||
The context can also be accessed with IComponent::GetSimContext.
|
||||
|
||||
In a typical component:
|
||||
|
||||
|
@ -520,6 +520,7 @@ IComponent* CComponentManager::ConstructComponent(entity_id_t ent, ComponentType
|
||||
debug_assert(component);
|
||||
|
||||
component->SetEntityId(ent);
|
||||
component->SetSimContext(m_SimContext);
|
||||
|
||||
// Store a reference to the new component
|
||||
emap1.insert(std::make_pair(ent, component));
|
||||
|
@ -45,6 +45,9 @@ public:
|
||||
entity_id_t GetEntityId() const { return m_EntityId; }
|
||||
void SetEntityId(entity_id_t ent) { m_EntityId = ent; }
|
||||
|
||||
const CSimContext& GetSimContext() const { return *m_SimContext; }
|
||||
void SetSimContext(const CSimContext& context) { m_SimContext = &context; }
|
||||
|
||||
static u8 GetSerializationVersion() { return 0; }
|
||||
virtual void Serialize(ISerializer& serialize) = 0;
|
||||
virtual void Deserialize(const CSimContext& context, const CParamNode& paramNode, IDeserializer& deserialize) = 0;
|
||||
@ -54,6 +57,7 @@ public:
|
||||
|
||||
private:
|
||||
entity_id_t m_EntityId;
|
||||
const CSimContext* m_SimContext;
|
||||
};
|
||||
|
||||
#endif // INCLUDED_ICOMPONENT
|
||||
|
@ -19,6 +19,8 @@
|
||||
|
||||
#include "SimContext.h"
|
||||
|
||||
#include "ComponentManager.h"
|
||||
|
||||
CSimContext::CSimContext() :
|
||||
m_ComponentManager(NULL), m_UnitManager(NULL), m_Terrain(NULL)
|
||||
{
|
||||
@ -56,3 +58,8 @@ void CSimContext::SetComponentManager(CComponentManager* man)
|
||||
debug_assert(!m_ComponentManager);
|
||||
m_ComponentManager = man;
|
||||
}
|
||||
|
||||
ScriptInterface& CSimContext::GetScriptInterface() const
|
||||
{
|
||||
return GetComponentManager().GetScriptInterface();
|
||||
}
|
||||
|
@ -21,6 +21,7 @@
|
||||
class CComponentManager;
|
||||
class CUnitManager;
|
||||
class CTerrain;
|
||||
class ScriptInterface;
|
||||
|
||||
/**
|
||||
* Contains pointers to various 'global' objects that are needed by the simulation code,
|
||||
@ -40,6 +41,8 @@ public:
|
||||
|
||||
CTerrain& GetTerrain() const;
|
||||
|
||||
ScriptInterface& GetScriptInterface() const;
|
||||
|
||||
private:
|
||||
CComponentManager* m_ComponentManager;
|
||||
CUnitManager* m_UnitManager;
|
||||
|
Loading…
Reference in New Issue
Block a user