2004-12-12 20:43:55 +01:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "Unit.h"
|
|
|
|
#include "Model.h"
|
2005-05-10 09:13:25 +02:00
|
|
|
#include "ObjectEntry.h"
|
2006-03-17 04:59:49 +01:00
|
|
|
#include "ObjectManager.h"
|
2006-04-24 00:22:18 +02:00
|
|
|
#include "SkeletonAnim.h"
|
2006-03-17 04:59:49 +01:00
|
|
|
#include "SkeletonAnimDef.h"
|
2004-12-12 20:43:55 +01:00
|
|
|
|
2006-03-17 04:59:49 +01:00
|
|
|
CUnit::CUnit(CObjectEntry* object, CEntity* entity, const std::set<CStrW>& actorSelections)
|
|
|
|
: m_Object(object), m_Model(object->m_Model->Clone()), m_Entity(entity),
|
|
|
|
m_ID(-1), m_ActorSelections(actorSelections)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CUnit::~CUnit()
|
|
|
|
{
|
2004-12-12 20:43:55 +01:00
|
|
|
delete m_Model;
|
|
|
|
}
|
2005-05-10 09:13:25 +02:00
|
|
|
|
|
|
|
void CUnit::ShowAmmunition()
|
|
|
|
{
|
2006-03-17 04:59:49 +01:00
|
|
|
if (!m_Object->m_AmmunitionModel || !m_Object->m_AmmunitionPoint)
|
2005-05-10 09:13:25 +02:00
|
|
|
return;
|
2006-04-24 00:22:18 +02:00
|
|
|
m_Model->AddProp(m_Object->m_AmmunitionPoint, m_Object->m_AmmunitionModel->Clone(), m_Object);
|
2005-05-10 09:13:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CUnit::HideAmmunition()
|
|
|
|
{
|
2006-03-17 04:59:49 +01:00
|
|
|
if (!m_Object->m_AmmunitionModel || !m_Object->m_AmmunitionPoint)
|
2005-05-10 09:13:25 +02:00
|
|
|
return;
|
2006-03-17 04:59:49 +01:00
|
|
|
|
|
|
|
// Find out what the usual prop is:
|
2005-05-10 09:13:25 +02:00
|
|
|
std::vector<CModel::Prop>& props = m_Object->m_Model->GetProps();
|
|
|
|
std::vector<CModel::Prop>::iterator it;
|
2006-03-17 04:59:49 +01:00
|
|
|
for (it = props.begin(); it != props.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it->m_Point == m_Object->m_AmmunitionPoint)
|
2005-05-10 09:13:25 +02:00
|
|
|
{
|
2006-04-24 00:22:18 +02:00
|
|
|
m_Model->AddProp(m_Object->m_AmmunitionPoint, it->m_Model->Clone(), m_Object);
|
2005-05-10 09:13:25 +02:00
|
|
|
return;
|
|
|
|
}
|
2006-03-17 04:59:49 +01:00
|
|
|
}
|
2005-05-10 09:13:25 +02:00
|
|
|
// No usual prop.
|
2006-03-17 04:59:49 +01:00
|
|
|
m_Model->RemoveProp(m_Object->m_AmmunitionPoint);
|
2005-05-21 03:40:32 +02:00
|
|
|
}
|
|
|
|
|
2006-04-24 00:22:18 +02:00
|
|
|
|
|
|
|
static CSkeletonAnim* GetRandomAnimation(const CStr& name, CObjectEntry* object)
|
2005-05-21 03:40:32 +02:00
|
|
|
{
|
2006-04-24 00:22:18 +02:00
|
|
|
CSkeletonAnim* anim = object->GetRandomAnimation(name);
|
|
|
|
|
|
|
|
// Fall back to 'idle', if no matching animation is found
|
|
|
|
if (anim == NULL && name != "idle")
|
|
|
|
anim = object->GetRandomAnimation("idle");
|
|
|
|
|
|
|
|
// Every object should have an idle animation (even if it's a dummy static one)
|
|
|
|
debug_assert(anim != NULL);
|
|
|
|
|
|
|
|
return anim;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool SetRandomAnimation(const CStr& name, bool once, float speed,
|
|
|
|
CModel* model, CObjectEntry* object)
|
|
|
|
{
|
|
|
|
CSkeletonAnim* anim = GetRandomAnimation(name, object);
|
2005-05-21 03:40:32 +02:00
|
|
|
if (anim)
|
|
|
|
{
|
2006-03-18 05:38:41 +01:00
|
|
|
float actualSpeed = 1000.f;
|
|
|
|
if (speed && anim->m_AnimDef)
|
|
|
|
actualSpeed = speed * anim->m_AnimDef->GetDuration();
|
2006-04-24 00:22:18 +02:00
|
|
|
model->SetAnimation(anim, once, actualSpeed);
|
|
|
|
|
|
|
|
// Recursively apply the animation name to props
|
|
|
|
const std::vector<CModel::Prop>& props = model->GetProps();
|
|
|
|
for (std::vector<CModel::Prop>::const_iterator it = props.begin(); it != props.end(); ++it)
|
|
|
|
{
|
|
|
|
bool ok = SetRandomAnimation(name, once, speed, it->m_Model, it->m_ObjectEntry);
|
|
|
|
if (! ok)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-05-21 03:40:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-03-17 04:59:49 +01:00
|
|
|
// This shouldn't happen, since GetRandomAnimation tries to always
|
|
|
|
// return something valid
|
2005-05-21 03:40:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-17 04:59:49 +01:00
|
|
|
|
2005-05-21 03:40:32 +02:00
|
|
|
|
2006-04-24 00:22:18 +02:00
|
|
|
bool CUnit::SetRandomAnimation(const CStr& name, bool once, float speed)
|
|
|
|
{
|
|
|
|
return ::SetRandomAnimation(name, once, speed, m_Model, m_Object);
|
|
|
|
}
|
2006-03-17 04:59:49 +01:00
|
|
|
|
2006-04-24 00:22:18 +02:00
|
|
|
CSkeletonAnim* CUnit::GetRandomAnimation(const CStr& name)
|
|
|
|
{
|
|
|
|
return ::GetRandomAnimation(name, m_Object);
|
2005-05-21 03:40:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CUnit::IsPlayingAnimation(const CStr& name)
|
|
|
|
{
|
2006-03-17 04:59:49 +01:00
|
|
|
return (m_Model->GetAnimation() && m_Model->GetAnimation()->m_Name == name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CUnit::SetPlayerID(int id)
|
|
|
|
{
|
|
|
|
m_PlayerID = id;
|
|
|
|
m_Model->SetPlayerID(m_PlayerID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CUnit::SetEntitySelection(const CStrW& selection)
|
|
|
|
{
|
2006-04-14 05:14:43 +02:00
|
|
|
CStrW selection_lc = selection.LowerCase();
|
|
|
|
|
2006-03-17 04:59:49 +01:00
|
|
|
// If we've already selected this, don't do anything
|
2006-04-14 05:14:43 +02:00
|
|
|
if (m_EntitySelections.find(selection_lc) != m_EntitySelections.end())
|
2006-03-17 04:59:49 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Just allow one selection at a time
|
|
|
|
m_EntitySelections.clear();
|
2006-04-14 05:14:43 +02:00
|
|
|
m_EntitySelections.insert(selection_lc);
|
2006-03-17 04:59:49 +01:00
|
|
|
|
|
|
|
ReloadObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CUnit::ReloadObject()
|
|
|
|
{
|
|
|
|
std::vector<std::set<CStrW> > selections;
|
|
|
|
// TODO: push world selections (seasons, etc) (and reload whenever they're changed)
|
|
|
|
selections.push_back(m_EntitySelections);
|
|
|
|
selections.push_back(m_ActorSelections);
|
|
|
|
|
|
|
|
// If these selections give a different object, change this unit to use it
|
|
|
|
CObjectEntry* newObject = g_ObjMan.FindObjectVariation(m_Object->m_Base, selections);
|
|
|
|
if (newObject != m_Object)
|
|
|
|
{
|
|
|
|
CModel* newModel = newObject->m_Model->Clone();
|
|
|
|
// Copy old settings to the new model
|
|
|
|
newModel->SetPlayerID(m_PlayerID);
|
|
|
|
newModel->SetTransform(m_Model->GetTransform());
|
|
|
|
// TODO: preserve selection of animation, anim offset, etc?
|
|
|
|
|
|
|
|
delete m_Model;
|
|
|
|
m_Model = newModel;
|
|
|
|
m_Object = newObject;
|
|
|
|
}
|
2005-05-21 03:40:32 +02:00
|
|
|
}
|