2024-11-12 12:42:21 -08:00
|
|
|
/* Copyright (C) 2025 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2010-01-09 11:20:14 -08:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2010-01-09 11:20:14 -08:00
|
|
|
* 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.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2010-01-09 11:20:14 -08:00
|
|
|
* 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
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2010-01-09 11:20:14 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include "ICmpVisual.h"
|
|
|
|
|
|
2025-08-07 10:53:23 -07:00
|
|
|
#include "graphics/Color.h"
|
2013-08-18 15:17:57 -07:00
|
|
|
#include "graphics/Decal.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
#include "graphics/Model.h"
|
2025-08-07 10:53:23 -07:00
|
|
|
#include "graphics/ModelAbstract.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
#include "graphics/ObjectBase.h"
|
|
|
|
|
#include "graphics/ObjectEntry.h"
|
|
|
|
|
#include "graphics/Unit.h"
|
2010-06-05 15:23:28 -07:00
|
|
|
#include "graphics/UnitAnimation.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
#include "graphics/UnitManager.h"
|
2025-08-07 10:53:23 -07:00
|
|
|
#include "lib/debug.h"
|
|
|
|
|
#include "lib/path.h"
|
|
|
|
|
#include "lib/types.h"
|
|
|
|
|
#include "lib/utf8.h"
|
|
|
|
|
#include "maths/BoundingBoxAligned.h"
|
|
|
|
|
#include "maths/BoundingBoxOriented.h"
|
2014-06-01 11:24:50 -07:00
|
|
|
#include "maths/BoundingSphere.h"
|
2025-08-07 10:53:23 -07:00
|
|
|
#include "maths/Fixed.h"
|
|
|
|
|
#include "maths/FixedVector3D.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
#include "maths/Matrix3D.h"
|
|
|
|
|
#include "maths/Vector3D.h"
|
|
|
|
|
#include "ps/CLogger.h"
|
2025-08-07 10:53:23 -07:00
|
|
|
#include "ps/CStr.h"
|
|
|
|
|
#include "ps/algorithm.h"
|
|
|
|
|
#include "simulation2/MessageTypes.h"
|
|
|
|
|
#include "simulation2/components/ICmpFootprint.h"
|
|
|
|
|
#include "simulation2/components/ICmpIdentity.h"
|
|
|
|
|
#include "simulation2/components/ICmpMirage.h"
|
|
|
|
|
#include "simulation2/components/ICmpOwnership.h"
|
|
|
|
|
#include "simulation2/components/ICmpPosition.h"
|
|
|
|
|
#include "simulation2/components/ICmpSound.h"
|
|
|
|
|
#include "simulation2/components/ICmpTemplateManager.h"
|
|
|
|
|
#include "simulation2/components/ICmpUnitRenderer.h"
|
|
|
|
|
#include "simulation2/components/ICmpValueModificationManager.h"
|
|
|
|
|
#include "simulation2/components/ICmpVisibility.h"
|
|
|
|
|
#include "simulation2/helpers/Player.h"
|
|
|
|
|
#include "simulation2/helpers/Position.h"
|
|
|
|
|
#include "simulation2/serialization/SerializeTemplates.h"
|
|
|
|
|
#include "simulation2/serialization/SerializedTypes.h"
|
|
|
|
|
#include "simulation2/system/Component.h"
|
2025-08-07 22:01:34 -07:00
|
|
|
#include "simulation2/system/Component.h"
|
|
|
|
|
#include "simulation2/system/Interface.h"
|
2025-08-07 10:53:23 -07:00
|
|
|
#include "simulation2/system/Message.h"
|
|
|
|
|
|
|
|
|
|
#include <cstddef>
|
2025-08-07 22:01:34 -07:00
|
|
|
#include <js/Value.h>
|
2025-08-07 10:53:23 -07:00
|
|
|
#include <map>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <utility>
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2022-03-03 14:42:26 -08:00
|
|
|
class CCmpVisualActor final : public ICmpVisual
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static void ClassInit(CComponentManager& componentManager)
|
|
|
|
|
{
|
2014-06-01 11:24:50 -07:00
|
|
|
componentManager.SubscribeToMessageType(MT_InterpolatedPositionChanged);
|
2010-01-22 12:03:14 -08:00
|
|
|
componentManager.SubscribeToMessageType(MT_OwnershipChanged);
|
2014-04-14 08:00:49 -07:00
|
|
|
componentManager.SubscribeToMessageType(MT_ValueModification);
|
2019-08-01 12:14:40 -07:00
|
|
|
componentManager.SubscribeToMessageType(MT_Create);
|
2014-06-01 11:24:50 -07:00
|
|
|
componentManager.SubscribeToMessageType(MT_Destroy);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DEFAULT_COMPONENT_ALLOCATOR(VisualActor)
|
|
|
|
|
|
2013-06-09 16:18:21 -07:00
|
|
|
private:
|
2014-04-14 08:00:49 -07:00
|
|
|
std::wstring m_BaseActorName, m_ActorName;
|
|
|
|
|
bool m_IsFoundationActor;
|
2018-05-28 12:06:09 -07:00
|
|
|
|
|
|
|
|
// Not initialized in non-visual mode
|
2010-02-07 12:06:16 -08:00
|
|
|
CUnit* m_Unit;
|
2019-08-01 12:14:40 -07:00
|
|
|
CModelAbstract::CustomSelectionShape* m_ShapeDescriptor = nullptr;
|
2010-02-07 12:06:16 -08:00
|
|
|
|
2015-03-15 16:59:48 -07:00
|
|
|
fixed m_R, m_G, m_B; // shading color
|
2010-10-04 10:34:33 -07:00
|
|
|
|
2010-02-07 12:06:16 -08:00
|
|
|
// Current animation state
|
|
|
|
|
std::string m_AnimName;
|
|
|
|
|
bool m_AnimOnce;
|
2011-10-29 08:30:46 -07:00
|
|
|
fixed m_AnimSpeed;
|
2010-04-05 16:09:34 -07:00
|
|
|
std::wstring m_SoundGroup;
|
2011-10-29 08:30:46 -07:00
|
|
|
fixed m_AnimDesync;
|
|
|
|
|
fixed m_AnimSyncRepeatTime; // 0.0 if not synced
|
2016-10-10 03:41:36 -07:00
|
|
|
fixed m_AnimSyncOffsetTime;
|
2010-02-07 12:06:16 -08:00
|
|
|
|
2016-05-30 07:07:48 -07:00
|
|
|
std::map<CStr, CStr> m_VariantSelections;
|
|
|
|
|
|
2013-02-02 18:08:20 -08:00
|
|
|
u32 m_Seed; // seed used for random variations
|
|
|
|
|
|
|
|
|
|
bool m_ConstructionPreview;
|
|
|
|
|
|
2013-06-09 16:18:21 -07:00
|
|
|
bool m_VisibleInAtlasOnly;
|
2013-12-30 07:52:42 -08:00
|
|
|
bool m_IsActorOnly; // an in-world entity should not have this or it might not be rendered.
|
2013-06-09 16:18:21 -07:00
|
|
|
|
2019-08-01 12:14:40 -07:00
|
|
|
bool m_SilhouetteDisplay;
|
|
|
|
|
bool m_SilhouetteOccluder;
|
2024-11-12 12:42:21 -08:00
|
|
|
bool m_ShadowsCast;
|
|
|
|
|
bool m_ShadowsReceive;
|
2019-08-01 12:14:40 -07:00
|
|
|
|
2014-06-01 11:24:50 -07:00
|
|
|
ICmpUnitRenderer::tag_t m_ModelTag;
|
2013-06-09 16:18:21 -07:00
|
|
|
|
|
|
|
|
public:
|
2010-04-09 12:02:39 -07:00
|
|
|
static std::string GetSchema()
|
|
|
|
|
{
|
|
|
|
|
return
|
2010-04-23 09:09:03 -07:00
|
|
|
"<a:help>Display the unit using the engine's actor system.</a:help>"
|
|
|
|
|
"<a:example>"
|
|
|
|
|
"<Actor>units/hellenes/infantry_spearman_b.xml</Actor>"
|
|
|
|
|
"</a:example>"
|
|
|
|
|
"<a:example>"
|
|
|
|
|
"<Actor>structures/hellenes/barracks.xml</Actor>"
|
|
|
|
|
"<FoundationActor>structures/fndn_4x4.xml</FoundationActor>"
|
|
|
|
|
"</a:example>"
|
|
|
|
|
"<element name='Actor' a:help='Filename of the actor to be used for this unit'>"
|
|
|
|
|
"<text/>"
|
|
|
|
|
"</element>"
|
2010-04-09 12:02:39 -07:00
|
|
|
"<optional>"
|
2010-04-23 09:09:03 -07:00
|
|
|
"<element name='FoundationActor' a:help='Filename of the actor to be used the foundation while this unit is being constructed'>"
|
|
|
|
|
"<text/>"
|
|
|
|
|
"</element>"
|
2010-04-09 12:02:39 -07:00
|
|
|
"</optional>"
|
|
|
|
|
"<optional>"
|
2013-02-02 18:08:20 -08:00
|
|
|
"<element name='Foundation' a:help='Used internally; if present, the unit will be rendered as a foundation'>"
|
|
|
|
|
"<empty/>"
|
|
|
|
|
"</element>"
|
|
|
|
|
"</optional>"
|
|
|
|
|
"<optional>"
|
|
|
|
|
"<element name='ConstructionPreview' a:help='If present, the unit should have a construction preview'>"
|
2010-04-23 09:09:03 -07:00
|
|
|
"<empty/>"
|
|
|
|
|
"</element>"
|
2011-03-18 09:57:54 -07:00
|
|
|
"</optional>"
|
2013-12-30 07:52:42 -08:00
|
|
|
"<optional>"
|
|
|
|
|
"<element name='ActorOnly' a:help='Used internally; if present, the unit will only be rendered if the user has high enough graphical settings.'>"
|
|
|
|
|
"<empty/>"
|
|
|
|
|
"</element>"
|
|
|
|
|
"</optional>"
|
2011-03-18 09:57:54 -07:00
|
|
|
"<element name='SilhouetteDisplay'>"
|
|
|
|
|
"<data type='boolean'/>"
|
|
|
|
|
"</element>"
|
|
|
|
|
"<element name='SilhouetteOccluder'>"
|
|
|
|
|
"<data type='boolean'/>"
|
2011-11-24 22:36:13 -08:00
|
|
|
"</element>"
|
|
|
|
|
"<optional>"
|
|
|
|
|
"<element name='SelectionShape'>"
|
|
|
|
|
"<choice>"
|
|
|
|
|
"<element name='Bounds' a:help='Determines the selection box based on the model bounds'>"
|
|
|
|
|
"<empty/>"
|
|
|
|
|
"</element>"
|
|
|
|
|
"<element name='Footprint' a:help='Determines the selection box based on the entity Footprint component'>"
|
|
|
|
|
"<empty/>"
|
|
|
|
|
"</element>"
|
|
|
|
|
"<element name='Box' a:help='Sets the selection shape to a box of specified dimensions'>"
|
|
|
|
|
"<attribute name='width'>"
|
2015-12-05 09:02:25 -08:00
|
|
|
"<data type='decimal'>"
|
|
|
|
|
"<param name='minExclusive'>0.0</param>"
|
|
|
|
|
"</data>"
|
2011-11-24 22:36:13 -08:00
|
|
|
"</attribute>"
|
|
|
|
|
"<attribute name='height'>"
|
2015-12-05 09:02:25 -08:00
|
|
|
"<data type='decimal'>"
|
|
|
|
|
"<param name='minExclusive'>0.0</param>"
|
|
|
|
|
"</data>"
|
2011-11-24 22:36:13 -08:00
|
|
|
"</attribute>"
|
|
|
|
|
"<attribute name='depth'>"
|
2015-12-05 09:02:25 -08:00
|
|
|
"<data type='decimal'>"
|
|
|
|
|
"<param name='minExclusive'>0.0</param>"
|
|
|
|
|
"</data>"
|
2011-11-24 22:36:13 -08:00
|
|
|
"</attribute>"
|
|
|
|
|
"</element>"
|
|
|
|
|
"<element name='Cylinder' a:help='Sets the selection shape to a cylinder of specified dimensions'>"
|
|
|
|
|
"<attribute name='radius'>"
|
2015-12-05 09:02:25 -08:00
|
|
|
"<data type='decimal'>"
|
|
|
|
|
"<param name='minExclusive'>0.0</param>"
|
|
|
|
|
"</data>"
|
2011-11-24 22:36:13 -08:00
|
|
|
"</attribute>"
|
|
|
|
|
"<attribute name='height'>"
|
2015-12-05 09:02:25 -08:00
|
|
|
"<data type='decimal'>"
|
|
|
|
|
"<param name='minExclusive'>0.0</param>"
|
|
|
|
|
"</data>"
|
2011-11-24 22:36:13 -08:00
|
|
|
"</attribute>"
|
|
|
|
|
"</element>"
|
|
|
|
|
"</choice>"
|
|
|
|
|
"</element>"
|
2013-06-09 16:18:21 -07:00
|
|
|
"</optional>"
|
|
|
|
|
"<element name='VisibleInAtlasOnly'>"
|
|
|
|
|
"<data type='boolean'/>"
|
2024-11-12 12:42:21 -08:00
|
|
|
"</element>"
|
|
|
|
|
"<optional>"
|
|
|
|
|
"<element name='ShadowsCast'>"
|
|
|
|
|
"<a:help>If true (default), the entity will cast dynamic shadows onto the environment. "
|
|
|
|
|
"Set to false to avoid unnecessary shadow computation for objects that do not need to affect scene lighting, "
|
|
|
|
|
"such as transparent effects, previews, or decorative visuals.</a:help>"
|
|
|
|
|
"<data type='boolean'/>"
|
|
|
|
|
"</element>"
|
|
|
|
|
"</optional>"
|
|
|
|
|
"<optional>"
|
|
|
|
|
"<element name='ShadowsReceive'>"
|
|
|
|
|
"<a:help>If true (default), the entity will receive dynamic shadows from other objects. "
|
|
|
|
|
"Set to false if the object should appear fully lit regardless of surrounding shadows, which can be useful "
|
|
|
|
|
"for performance tuning or effects that simulate glowing or volumetric visuals.</a:help>"
|
|
|
|
|
"<data type='boolean'/>"
|
|
|
|
|
"</element>"
|
|
|
|
|
"</optional>";
|
2010-04-09 12:02:39 -07:00
|
|
|
}
|
2010-04-15 12:59:07 -07:00
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void Init(const CParamNode& paramNode) override
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2010-08-05 03:20:47 -07:00
|
|
|
m_Unit = NULL;
|
2011-10-24 05:24:04 -07:00
|
|
|
m_R = m_G = m_B = fixed::FromInt(1);
|
|
|
|
|
|
2013-02-02 18:08:20 -08:00
|
|
|
m_ConstructionPreview = paramNode.GetChild("ConstructionPreview").IsOk();
|
|
|
|
|
|
|
|
|
|
m_Seed = GetEntityId();
|
|
|
|
|
|
2014-04-14 08:00:49 -07:00
|
|
|
m_IsFoundationActor = paramNode.GetChild("Foundation").IsOk() && paramNode.GetChild("FoundationActor").IsOk();
|
2019-08-01 12:14:40 -07:00
|
|
|
|
2021-04-11 02:23:10 -07:00
|
|
|
m_BaseActorName = paramNode.GetChild(m_IsFoundationActor ? "FoundationActor" : "Actor").ToWString();
|
2019-08-01 12:14:40 -07:00
|
|
|
ParseActorName(m_BaseActorName);
|
2010-03-12 14:28:51 -08:00
|
|
|
|
2013-06-09 16:18:21 -07:00
|
|
|
m_VisibleInAtlasOnly = paramNode.GetChild("VisibleInAtlasOnly").ToBool();
|
2013-12-30 07:52:42 -08:00
|
|
|
m_IsActorOnly = paramNode.GetChild("ActorOnly").IsOk();
|
2016-10-10 03:37:58 -07:00
|
|
|
|
2019-08-01 12:14:40 -07:00
|
|
|
m_SilhouetteDisplay = paramNode.GetChild("SilhouetteDisplay").ToBool();
|
|
|
|
|
m_SilhouetteOccluder = paramNode.GetChild("SilhouetteOccluder").ToBool();
|
2024-11-12 12:42:21 -08:00
|
|
|
|
|
|
|
|
m_ShadowsCast = m_VisibleInAtlasOnly ? false : (paramNode.GetChild("ShadowsCast").IsOk() ? paramNode.GetChild("ShadowsCast").ToBool() : true);
|
|
|
|
|
m_ShadowsReceive = m_VisibleInAtlasOnly ? false : (paramNode.GetChild("ShadowsReceive").IsOk() ? paramNode.GetChild("ShadowsReceive").ToBool() : true);
|
2010-02-07 12:06:16 -08:00
|
|
|
|
2019-08-01 12:14:40 -07:00
|
|
|
// Initialize the model's selection shape descriptor. This currently relies on the component initialization order; the
|
|
|
|
|
// Footprint component must be initialized before this component (VisualActor) to support the ability to use the footprint
|
|
|
|
|
// shape for the selection box (instead of the default recursive bounding box). See TypeList.h for the order in
|
|
|
|
|
// which components are initialized; if for whatever reason you need to get rid of this dependency, you can always just
|
|
|
|
|
// initialize the selection shape descriptor on-demand.
|
|
|
|
|
InitSelectionShapeDescriptor(paramNode);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void Deinit() override
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
if (m_Unit)
|
|
|
|
|
{
|
2011-01-16 06:08:38 -08:00
|
|
|
GetSimContext().GetUnitManager().DeleteUnit(m_Unit);
|
2010-01-09 11:20:14 -08:00
|
|
|
m_Unit = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-29 08:30:46 -07:00
|
|
|
template<typename S>
|
|
|
|
|
void SerializeCommon(S& serialize)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2010-10-23 12:59:40 -07:00
|
|
|
serialize.NumberFixed_Unbounded("r", m_R);
|
|
|
|
|
serialize.NumberFixed_Unbounded("g", m_G);
|
|
|
|
|
serialize.NumberFixed_Unbounded("b", m_B);
|
2011-10-29 08:30:46 -07:00
|
|
|
|
|
|
|
|
serialize.StringASCII("anim name", m_AnimName, 0, 256);
|
|
|
|
|
serialize.Bool("anim once", m_AnimOnce);
|
|
|
|
|
serialize.NumberFixed_Unbounded("anim speed", m_AnimSpeed);
|
|
|
|
|
serialize.String("sound group", m_SoundGroup, 0, 256);
|
|
|
|
|
serialize.NumberFixed_Unbounded("anim desync", m_AnimDesync);
|
|
|
|
|
serialize.NumberFixed_Unbounded("anim sync repeat time", m_AnimSyncRepeatTime);
|
2016-10-10 03:41:36 -07:00
|
|
|
serialize.NumberFixed_Unbounded("anim sync offset time", m_AnimSyncOffsetTime);
|
2011-10-29 08:30:46 -07:00
|
|
|
|
2020-12-19 01:10:37 -08:00
|
|
|
Serializer(serialize, "variation", m_VariantSelections);
|
2016-05-30 07:07:48 -07:00
|
|
|
|
2013-02-02 18:08:20 -08:00
|
|
|
serialize.NumberU32_Unbounded("seed", m_Seed);
|
2014-04-14 08:00:49 -07:00
|
|
|
serialize.String("actor", m_ActorName, 0, 256);
|
2013-02-02 18:08:20 -08:00
|
|
|
|
2011-10-29 08:30:46 -07:00
|
|
|
// TODO: store actor variables?
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void Serialize(ISerializer& serialize) override
|
2011-10-29 08:30:46 -07:00
|
|
|
{
|
|
|
|
|
// TODO: store the actor name, if !debug and it differs from the template
|
|
|
|
|
|
|
|
|
|
if (serialize.IsDebug())
|
|
|
|
|
{
|
2014-04-14 08:00:49 -07:00
|
|
|
serialize.String("base actor", m_BaseActorName, 0, 256);
|
2011-10-29 08:30:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SerializeCommon(serialize);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) override
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2011-01-16 06:08:38 -08:00
|
|
|
Init(paramNode);
|
2010-10-23 12:59:40 -07:00
|
|
|
|
2013-02-02 18:08:20 -08:00
|
|
|
u32 oldSeed = GetActorSeed();
|
|
|
|
|
|
2011-10-29 08:30:46 -07:00
|
|
|
SerializeCommon(deserialize);
|
|
|
|
|
|
2019-08-01 12:14:40 -07:00
|
|
|
InitModel();
|
|
|
|
|
|
2014-04-14 08:00:49 -07:00
|
|
|
// If we serialized a different seed or different actor, reload actor
|
|
|
|
|
if (oldSeed != GetActorSeed() || m_BaseActorName != m_ActorName)
|
2013-02-02 18:08:20 -08:00
|
|
|
ReloadActor();
|
2011-10-29 08:30:46 -07:00
|
|
|
else
|
2016-10-10 03:49:49 -07:00
|
|
|
ReloadUnitAnimation();
|
2011-10-29 08:30:46 -07:00
|
|
|
|
|
|
|
|
if (m_Unit)
|
|
|
|
|
{
|
2013-09-11 13:41:53 -07:00
|
|
|
CmpPtr<ICmpOwnership> cmpOwnership(GetEntityHandle());
|
2012-02-07 18:46:15 -08:00
|
|
|
if (cmpOwnership)
|
2011-10-29 08:30:46 -07:00
|
|
|
m_Unit->GetModel().SetPlayerID(cmpOwnership->GetOwner());
|
|
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2025-06-03 05:13:41 -07:00
|
|
|
void HandleMessage(const CMessage& msg, bool /*global*/) override
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
switch (msg.GetType())
|
|
|
|
|
{
|
2010-01-22 12:03:14 -08:00
|
|
|
case MT_OwnershipChanged:
|
|
|
|
|
{
|
2020-11-28 00:57:15 -08:00
|
|
|
RecomputeActorName();
|
|
|
|
|
|
2016-10-10 03:56:13 -07:00
|
|
|
if (!m_Unit)
|
|
|
|
|
break;
|
|
|
|
|
|
2010-01-22 12:03:14 -08:00
|
|
|
const CMessageOwnershipChanged& msgData = static_cast<const CMessageOwnershipChanged&> (msg);
|
2010-09-19 11:08:56 -07:00
|
|
|
m_Unit->GetModel().SetPlayerID(msgData.to);
|
2020-11-28 00:57:15 -08:00
|
|
|
|
2010-01-22 12:03:14 -08:00
|
|
|
break;
|
|
|
|
|
}
|
2014-04-14 08:00:49 -07:00
|
|
|
case MT_ValueModification:
|
|
|
|
|
{
|
2020-11-28 00:57:15 -08:00
|
|
|
// Mirages don't respond to technology modifications.
|
|
|
|
|
CmpPtr<ICmpMirage> cmpMirage(GetEntityHandle());
|
|
|
|
|
if (cmpMirage)
|
|
|
|
|
return;
|
|
|
|
|
|
2014-04-14 08:00:49 -07:00
|
|
|
const CMessageValueModification& msgData = static_cast<const CMessageValueModification&> (msg);
|
|
|
|
|
if (msgData.component != L"VisualActor")
|
|
|
|
|
break;
|
2020-11-28 00:57:15 -08:00
|
|
|
|
|
|
|
|
RecomputeActorName();
|
|
|
|
|
|
2014-04-14 08:00:49 -07:00
|
|
|
break;
|
|
|
|
|
}
|
2014-06-01 11:24:50 -07:00
|
|
|
case MT_InterpolatedPositionChanged:
|
|
|
|
|
{
|
|
|
|
|
const CMessageInterpolatedPositionChanged& msgData = static_cast<const CMessageInterpolatedPositionChanged&> (msg);
|
|
|
|
|
if (m_ModelTag.valid())
|
|
|
|
|
{
|
|
|
|
|
CmpPtr<ICmpUnitRenderer> cmpModelRenderer(GetSystemEntity());
|
|
|
|
|
cmpModelRenderer->UpdateUnitPos(m_ModelTag, msgData.inWorld, msgData.pos0, msgData.pos1);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-08-01 12:14:40 -07:00
|
|
|
case MT_Create:
|
|
|
|
|
{
|
|
|
|
|
InitModel();
|
|
|
|
|
|
|
|
|
|
SelectAnimation("idle");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-06-01 11:24:50 -07:00
|
|
|
case MT_Destroy:
|
|
|
|
|
{
|
|
|
|
|
if (m_ModelTag.valid())
|
|
|
|
|
{
|
|
|
|
|
CmpPtr<ICmpUnitRenderer> cmpModelRenderer(GetSystemEntity());
|
|
|
|
|
cmpModelRenderer->RemoveUnit(m_ModelTag);
|
|
|
|
|
m_ModelTag = ICmpUnitRenderer::tag_t();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
CBoundingBoxAligned GetBounds() const override
|
2011-11-24 22:36:13 -08:00
|
|
|
{
|
|
|
|
|
if (!m_Unit)
|
|
|
|
|
return CBoundingBoxAligned::EMPTY;
|
|
|
|
|
return m_Unit->GetModel().GetWorldBounds();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
CUnit* GetUnit() override
|
2011-12-09 02:49:08 -08:00
|
|
|
{
|
|
|
|
|
return m_Unit;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
CBoundingBoxOriented GetSelectionBox() const override
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
if (!m_Unit)
|
2011-11-24 22:36:13 -08:00
|
|
|
return CBoundingBoxOriented::EMPTY;
|
|
|
|
|
return m_Unit->GetModel().GetSelectionBox();
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
CVector3D GetPosition() const override
|
2010-03-11 12:01:16 -08:00
|
|
|
{
|
|
|
|
|
if (!m_Unit)
|
|
|
|
|
return CVector3D(0, 0, 0);
|
2010-04-17 04:44:08 -07:00
|
|
|
return m_Unit->GetModel().GetTransform().GetTranslation();
|
2010-03-11 12:01:16 -08:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
std::wstring GetProjectileActor() const override
|
2010-04-17 04:34:40 -07:00
|
|
|
{
|
|
|
|
|
if (!m_Unit)
|
|
|
|
|
return L"";
|
|
|
|
|
return m_Unit->GetObject().m_ProjectileModelName;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
CFixedVector3D GetProjectileLaunchPoint() const override
|
2010-06-04 17:49:14 -07:00
|
|
|
{
|
|
|
|
|
if (!m_Unit)
|
2017-12-23 01:27:19 -08:00
|
|
|
return CFixedVector3D();
|
2010-09-23 06:09:35 -07:00
|
|
|
|
2011-03-13 12:22:05 -07:00
|
|
|
if (m_Unit->GetModel().ToCModel())
|
|
|
|
|
{
|
|
|
|
|
// Ensure the prop transforms are correct
|
2014-06-01 11:24:50 -07:00
|
|
|
CmpPtr<ICmpUnitRenderer> cmpUnitRenderer(GetSystemEntity());
|
|
|
|
|
CmpPtr<ICmpPosition> cmpPosition(GetEntityHandle());
|
|
|
|
|
if (cmpUnitRenderer && cmpPosition)
|
|
|
|
|
{
|
|
|
|
|
float frameOffset = cmpUnitRenderer->GetFrameOffset();
|
|
|
|
|
CMatrix3D transform(cmpPosition->GetInterpolatedTransform(frameOffset));
|
|
|
|
|
m_Unit->GetModel().SetTransform(transform);
|
|
|
|
|
m_Unit->GetModel().ValidatePosition();
|
|
|
|
|
}
|
2010-09-23 06:09:35 -07:00
|
|
|
|
2011-03-13 12:22:05 -07:00
|
|
|
CModelAbstract* ammo = m_Unit->GetModel().ToCModel()->FindFirstAmmoProp();
|
|
|
|
|
if (ammo)
|
2017-12-23 01:27:19 -08:00
|
|
|
{
|
|
|
|
|
CVector3D vector = ammo->GetTransform().GetTranslation();
|
|
|
|
|
return CFixedVector3D(fixed::FromFloat(vector.X), fixed::FromFloat(vector.Y), fixed::FromFloat(vector.Z));
|
|
|
|
|
}
|
2011-03-13 12:22:05 -07:00
|
|
|
}
|
|
|
|
|
|
2017-12-23 01:27:19 -08:00
|
|
|
return CFixedVector3D();
|
2010-06-04 17:49:14 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void SetVariant(const CStr& key, const CStr& selection) override
|
2016-05-30 07:07:48 -07:00
|
|
|
{
|
2018-02-23 12:11:16 -08:00
|
|
|
if (m_VariantSelections[key] == selection)
|
|
|
|
|
return;
|
|
|
|
|
|
2016-05-30 07:07:48 -07:00
|
|
|
m_VariantSelections[key] = selection;
|
2016-10-10 03:37:58 -07:00
|
|
|
|
|
|
|
|
if (m_Unit)
|
2018-02-23 12:11:16 -08:00
|
|
|
{
|
2016-10-10 03:37:58 -07:00
|
|
|
m_Unit->SetEntitySelection(key, selection);
|
2018-02-23 12:11:16 -08:00
|
|
|
if (m_Unit->GetAnimation())
|
|
|
|
|
m_Unit->GetAnimation()->ReloadAnimation();
|
|
|
|
|
}
|
2016-05-30 07:07:48 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
std::string GetAnimationName() const override
|
2017-03-15 07:27:51 -07:00
|
|
|
{
|
|
|
|
|
return m_AnimName;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void SelectAnimation(const std::string& name, bool once = false, fixed speed = fixed::FromInt(1)) override
|
2010-02-07 12:06:16 -08:00
|
|
|
{
|
|
|
|
|
m_AnimName = name;
|
|
|
|
|
m_AnimOnce = once;
|
|
|
|
|
m_AnimSpeed = speed;
|
2018-02-24 10:13:03 -08:00
|
|
|
m_SoundGroup = L"";
|
2011-10-29 08:30:46 -07:00
|
|
|
m_AnimDesync = fixed::FromInt(1)/20; // TODO: make this an argument
|
|
|
|
|
m_AnimSyncRepeatTime = fixed::Zero();
|
2016-10-16 13:17:31 -07:00
|
|
|
m_AnimSyncOffsetTime = fixed::Zero();
|
2010-04-05 16:09:34 -07:00
|
|
|
|
2016-10-10 03:56:13 -07:00
|
|
|
SetVariant("animation", m_AnimName);
|
|
|
|
|
|
2018-05-28 12:06:09 -07:00
|
|
|
CmpPtr<ICmpSound> cmpSound(GetEntityHandle());
|
2018-02-25 06:58:31 -08:00
|
|
|
if (cmpSound)
|
|
|
|
|
m_SoundGroup = cmpSound->GetSoundGroup(wstring_from_utf8(m_AnimName));
|
|
|
|
|
|
2018-05-28 12:06:09 -07:00
|
|
|
if (!m_Unit || !m_Unit->GetAnimation() || !m_Unit->GetID())
|
|
|
|
|
return;
|
|
|
|
|
|
2025-01-23 12:47:26 -08:00
|
|
|
m_Unit->GetAnimation()->SetAnimationState(m_AnimName, m_AnimOnce, m_AnimSpeed.ToFloat(), m_AnimDesync.ToFloat(), m_SoundGroup.c_str());
|
2010-04-05 16:09:34 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void SelectMovementAnimation(const std::string& name, fixed speed) override
|
2010-09-03 02:55:14 -07:00
|
|
|
{
|
2019-07-09 12:56:28 -07:00
|
|
|
ENSURE(name == "idle" || name == "walk" || name == "run");
|
|
|
|
|
if (m_AnimName != "idle" && m_AnimName != "walk" && m_AnimName != "run")
|
|
|
|
|
return;
|
2021-03-04 10:32:48 -08:00
|
|
|
if (m_AnimName == name && speed == m_AnimSpeed)
|
|
|
|
|
return;
|
2019-07-09 12:56:28 -07:00
|
|
|
SelectAnimation(name, false, speed);
|
2010-09-03 02:55:14 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void SetAnimationSyncRepeat(fixed repeattime) override
|
2010-04-05 16:09:34 -07:00
|
|
|
{
|
2010-06-02 18:29:43 -07:00
|
|
|
m_AnimSyncRepeatTime = repeattime;
|
|
|
|
|
|
2016-10-10 03:37:58 -07:00
|
|
|
if (m_Unit && m_Unit->GetAnimation())
|
|
|
|
|
m_Unit->GetAnimation()->SetAnimationSyncRepeat(m_AnimSyncRepeatTime.ToFloat());
|
2010-06-05 15:23:28 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void SetAnimationSyncOffset(fixed actiontime) override
|
2010-06-05 15:23:28 -07:00
|
|
|
{
|
2016-10-10 03:41:36 -07:00
|
|
|
m_AnimSyncOffsetTime = actiontime;
|
|
|
|
|
|
2016-10-10 03:37:58 -07:00
|
|
|
if (m_Unit && m_Unit->GetAnimation())
|
2016-10-10 03:41:36 -07:00
|
|
|
m_Unit->GetAnimation()->SetAnimationSyncOffset(m_AnimSyncOffsetTime.ToFloat());
|
2010-02-07 12:06:16 -08:00
|
|
|
}
|
|
|
|
|
|
2025-05-21 05:55:52 -07:00
|
|
|
// TODO: Why is `a` even an argument?
|
|
|
|
|
void SetShadingColor(fixed r, fixed g, fixed b, fixed /*a*/) override
|
2010-03-17 15:56:49 -07:00
|
|
|
{
|
2010-10-04 10:34:33 -07:00
|
|
|
m_R = r;
|
|
|
|
|
m_G = g;
|
|
|
|
|
m_B = b;
|
2014-06-01 11:24:50 -07:00
|
|
|
|
|
|
|
|
if (m_Unit)
|
|
|
|
|
{
|
|
|
|
|
CModelAbstract& model = m_Unit->GetModel();
|
|
|
|
|
model.SetShadingColor(CColor(m_R.ToFloat(), m_G.ToFloat(), m_B.ToFloat(), 1.0f));
|
|
|
|
|
}
|
2010-03-17 15:56:49 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void SetVariable(const std::string& name, float value) override
|
2011-04-05 17:11:40 -07:00
|
|
|
{
|
2011-10-29 08:30:46 -07:00
|
|
|
if (m_Unit)
|
|
|
|
|
m_Unit->GetModel().SetEntityVariable(name, value);
|
2011-04-05 17:11:40 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
u32 GetActorSeed() const override
|
2010-06-02 18:29:43 -07:00
|
|
|
{
|
2013-02-02 18:08:20 -08:00
|
|
|
return m_Seed;
|
|
|
|
|
}
|
2016-10-10 03:37:58 -07:00
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void SetActorSeed(u32 seed) override
|
2013-02-02 18:08:20 -08:00
|
|
|
{
|
|
|
|
|
if (seed == m_Seed)
|
2010-06-02 18:29:43 -07:00
|
|
|
return;
|
|
|
|
|
|
2013-02-02 18:08:20 -08:00
|
|
|
m_Seed = seed;
|
|
|
|
|
ReloadActor();
|
|
|
|
|
}
|
2010-06-02 18:29:43 -07:00
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void RecomputeActorName() override
|
2020-11-28 00:57:15 -08:00
|
|
|
{
|
|
|
|
|
CmpPtr<ICmpValueModificationManager> cmpValueModificationManager(GetSystemEntity());
|
|
|
|
|
std::wstring newActorName;
|
|
|
|
|
if (m_IsFoundationActor)
|
|
|
|
|
newActorName = cmpValueModificationManager->ApplyModifications(L"VisualActor/FoundationActor", m_BaseActorName, GetEntityId());
|
|
|
|
|
else
|
|
|
|
|
newActorName = cmpValueModificationManager->ApplyModifications(L"VisualActor/Actor", m_BaseActorName, GetEntityId());
|
|
|
|
|
|
|
|
|
|
if (newActorName != m_ActorName)
|
|
|
|
|
{
|
|
|
|
|
ParseActorName(newActorName);
|
|
|
|
|
ReloadActor();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
bool HasConstructionPreview() const override
|
2013-02-02 18:08:20 -08:00
|
|
|
{
|
|
|
|
|
return m_ConstructionPreview;
|
|
|
|
|
}
|
2010-06-02 18:29:43 -07:00
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void Hotload(const VfsPath& name) override
|
2013-02-02 18:08:20 -08:00
|
|
|
{
|
|
|
|
|
if (!m_Unit)
|
|
|
|
|
return;
|
2010-06-02 18:29:43 -07:00
|
|
|
|
Implement quality levels for actors & corresponding setting.
An actor file, as referenced by the VisualActor, can now define
different actors for different "quality level" setting.
In this initial version, the quality is handled directly by the object
manager.
Actor format impact:
- '<qualitylevels>' may be used as the root node, containing actor nodes
as children.
- such actor nodes can refer to a file, or to an inline actor, or
simply be inlined.
- such actor nodes may have a 'quality' attribute, specifying the
maximum quality level of this actor. By default, 255 (the maximum) is
implied.
- The actor format remains valid, but 'groups', 'variants', 'material',
'castshadow' and 'float' can be given a [minquality, maxquality[ range
via XML attributes. Outside of this range, the XML node is ignored
(making it possible to define, in a single actor file, several quality
levels).
Quality is a 0-255 value, with:
- Range 0-99 intended for lower level-of-detail actors (billboards,
etc.)
- Range 100-200 the 'normal' range for models. 100 is "low", 150
"medium", and 200 "high".
- Range 201-255 used for higher quality actors that might be used for
e.g. cinematics.
The range is wide to make it easier to add intermediate levels in the
future and it seemed easier given that an integer value of some kind was
required anyways.
Engine impacts:
- A new CActorDef class is introduced, wrapping an art/actors XML file
and its different quality levels. ObjectBase remains the definition of a
given 'actor', now at a given quality level.
- CActorDef imposes a maximal # of quality level for a particular actor
definition (5 currently).
- CUnit is made to refer to an Actor Definition explicitly, not a
particular ObjectBase.
- As a minor optimisation, variation keys are calculated on
pointer-to-sets-of-selections, instead of raw sets-of-selections, as
this reduces copying.
- some refactoring, including better const-correctness and hotloading
support via std::shared_ptr.
Differential Revision: https://code.wildfiregames.com/D3787
This was SVN commit r25210.
2021-04-08 00:22:24 -07:00
|
|
|
if (!name.empty() && name != m_ActorName)
|
2013-02-02 18:08:20 -08:00
|
|
|
return;
|
2011-03-18 09:57:54 -07:00
|
|
|
|
2013-02-02 18:08:20 -08:00
|
|
|
ReloadActor();
|
2010-06-02 18:29:43 -07:00
|
|
|
}
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
private:
|
2025-06-14 13:03:01 -07:00
|
|
|
// Replace {phenotype} and {civ} with the correct value in m_ActorName
|
2019-08-01 12:14:40 -07:00
|
|
|
void ParseActorName(std::wstring base);
|
|
|
|
|
|
2013-02-02 18:08:20 -08:00
|
|
|
/// Helper function shared by component init and actor reloading
|
2019-08-01 12:14:40 -07:00
|
|
|
void InitModel();
|
2011-04-02 05:51:42 -07:00
|
|
|
|
2011-11-24 22:36:13 -08:00
|
|
|
/// Helper method; initializes the model selection shape descriptor from XML. Factored out for readability of @ref Init.
|
2013-02-02 18:08:20 -08:00
|
|
|
void InitSelectionShapeDescriptor(const CParamNode& paramNode);
|
|
|
|
|
|
2016-10-10 03:49:49 -07:00
|
|
|
// ReloadActor is used when the actor or seed changes.
|
2013-02-02 18:08:20 -08:00
|
|
|
void ReloadActor();
|
2016-10-10 03:49:49 -07:00
|
|
|
// ReloadUnitAnimation is used for a minimal reloading upon deserialization, when the actor and seed are identical.
|
|
|
|
|
// It is also used by ReloadActor.
|
|
|
|
|
void ReloadUnitAnimation();
|
2010-01-09 11:20:14 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
REGISTER_COMPONENT_TYPE(VisualActor)
|
|
|
|
|
|
2011-11-24 22:36:13 -08:00
|
|
|
// ------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
2019-08-01 12:14:40 -07:00
|
|
|
void CCmpVisualActor::ParseActorName(std::wstring base)
|
|
|
|
|
{
|
|
|
|
|
CmpPtr<ICmpIdentity> cmpIdentity(GetEntityHandle());
|
|
|
|
|
if (cmpIdentity)
|
|
|
|
|
{
|
2025-06-14 13:03:01 -07:00
|
|
|
PS::ReplaceSubrange(base, L"{phenotype}", cmpIdentity->GetPhenotype());
|
|
|
|
|
PS::ReplaceSubrange(base, L"{civ}", cmpIdentity->GetCiv());
|
2019-08-01 12:14:40 -07:00
|
|
|
}
|
|
|
|
|
|
2025-06-14 13:03:01 -07:00
|
|
|
m_ActorName = std::move(base);
|
2019-08-01 12:14:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CCmpVisualActor::InitModel()
|
2013-02-02 18:08:20 -08:00
|
|
|
{
|
2014-06-01 11:24:50 -07:00
|
|
|
if (!GetSimContext().HasUnitManager())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
std::wstring actorName = m_ActorName;
|
|
|
|
|
if (actorName.find(L".xml") == std::wstring::npos)
|
|
|
|
|
actorName += L".xml";
|
2023-08-28 11:01:44 -07:00
|
|
|
m_Unit = GetSimContext().GetUnitManager().CreateUnit(actorName, GetEntityId(), GetActorSeed());
|
2014-06-01 11:24:50 -07:00
|
|
|
if (!m_Unit)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
CModelAbstract& model = m_Unit->GetModel();
|
|
|
|
|
if (model.ToCModel())
|
2013-02-02 18:08:20 -08:00
|
|
|
{
|
2014-06-01 11:24:50 -07:00
|
|
|
u32 modelFlags = 0;
|
2013-02-02 18:08:20 -08:00
|
|
|
|
2019-08-01 12:14:40 -07:00
|
|
|
if (m_SilhouetteDisplay)
|
2023-10-09 11:37:56 -07:00
|
|
|
modelFlags |= ModelFlag::SILHOUETTE_DISPLAY;
|
2013-02-02 18:08:20 -08:00
|
|
|
|
2019-08-01 12:14:40 -07:00
|
|
|
if (m_SilhouetteOccluder)
|
2023-10-09 11:37:56 -07:00
|
|
|
modelFlags |= ModelFlag::SILHOUETTE_OCCLUDER;
|
2013-02-02 18:08:20 -08:00
|
|
|
|
2014-12-05 10:33:59 -08:00
|
|
|
CmpPtr<ICmpVisibility> cmpVisibility(GetEntityHandle());
|
|
|
|
|
if (cmpVisibility && cmpVisibility->GetAlwaysVisible())
|
2023-10-09 11:37:56 -07:00
|
|
|
modelFlags |= ModelFlag::IGNORE_LOS;
|
2013-02-02 18:08:20 -08:00
|
|
|
|
2014-06-01 11:24:50 -07:00
|
|
|
model.ToCModel()->AddFlagsRec(modelFlags);
|
|
|
|
|
}
|
2013-02-02 18:08:20 -08:00
|
|
|
|
2024-11-12 12:42:21 -08:00
|
|
|
if (!m_ShadowsCast && model.ToCModel())
|
|
|
|
|
model.ToCModel()->RemoveShadowsCast();
|
|
|
|
|
|
|
|
|
|
if (!m_ShadowsReceive)
|
2014-06-01 11:24:50 -07:00
|
|
|
{
|
|
|
|
|
if (model.ToCModel())
|
2024-11-12 12:42:21 -08:00
|
|
|
model.ToCModel()->RemoveShadowsReceive();
|
2014-06-01 11:24:50 -07:00
|
|
|
else if (model.ToCModelDecal())
|
2024-11-12 12:42:21 -08:00
|
|
|
model.ToCModelDecal()->RemoveShadowsReceive();
|
2014-06-01 11:24:50 -07:00
|
|
|
}
|
|
|
|
|
|
2025-01-23 12:47:26 -08:00
|
|
|
CStr anchor = m_Unit->GetObject().m_Base->m_Properties.m_AnchorType;
|
2014-06-01 11:24:50 -07:00
|
|
|
bool floating = m_Unit->GetObject().m_Base->m_Properties.m_FloatOnWater;
|
|
|
|
|
CmpPtr<ICmpPosition> cmpPosition(GetEntityHandle());
|
|
|
|
|
if (cmpPosition)
|
2025-01-23 12:47:26 -08:00
|
|
|
{
|
|
|
|
|
if (!anchor.empty())
|
|
|
|
|
cmpPosition->SetActorAnchor(anchor);
|
2014-06-01 11:24:50 -07:00
|
|
|
cmpPosition->SetActorFloating(floating);
|
2025-01-23 12:47:26 -08:00
|
|
|
}
|
2014-06-01 11:24:50 -07:00
|
|
|
|
|
|
|
|
if (!m_ModelTag.valid())
|
|
|
|
|
{
|
|
|
|
|
CmpPtr<ICmpUnitRenderer> cmpModelRenderer(GetSystemEntity());
|
|
|
|
|
if (cmpModelRenderer)
|
|
|
|
|
{
|
|
|
|
|
// TODO: this should account for all possible props, animations, etc,
|
|
|
|
|
// else we might accidentally cull the unit when it should be visible
|
|
|
|
|
CBoundingBoxAligned bounds = m_Unit->GetModel().GetWorldBoundsRec();
|
|
|
|
|
CBoundingSphere boundSphere = CBoundingSphere::FromSweptBox(bounds);
|
|
|
|
|
|
|
|
|
|
int flags = 0;
|
|
|
|
|
if (m_IsActorOnly)
|
|
|
|
|
flags |= ICmpUnitRenderer::ACTOR_ONLY;
|
|
|
|
|
if (m_VisibleInAtlasOnly)
|
|
|
|
|
flags |= ICmpUnitRenderer::VISIBLE_IN_ATLAS_ONLY;
|
|
|
|
|
|
|
|
|
|
m_ModelTag = cmpModelRenderer->AddUnit(GetEntityHandle(), m_Unit, boundSphere, flags);
|
2013-02-02 18:08:20 -08:00
|
|
|
}
|
|
|
|
|
}
|
2019-08-01 12:14:40 -07:00
|
|
|
|
|
|
|
|
// the model is now responsible for cleaning up the descriptor
|
|
|
|
|
if (m_ShapeDescriptor != nullptr)
|
|
|
|
|
m_Unit->GetModel().SetCustomSelectionShape(m_ShapeDescriptor);
|
2013-02-02 18:08:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CCmpVisualActor::InitSelectionShapeDescriptor(const CParamNode& paramNode)
|
2011-11-24 22:36:13 -08:00
|
|
|
{
|
|
|
|
|
// by default, we don't need a custom selection shape and we can just keep the default behaviour
|
2019-08-01 12:14:40 -07:00
|
|
|
m_ShapeDescriptor = nullptr;
|
2011-11-24 22:36:13 -08:00
|
|
|
|
|
|
|
|
const CParamNode& shapeNode = paramNode.GetChild("SelectionShape");
|
|
|
|
|
if (shapeNode.IsOk())
|
|
|
|
|
{
|
|
|
|
|
if (shapeNode.GetChild("Bounds").IsOk())
|
|
|
|
|
{
|
|
|
|
|
// default; no need to take action
|
|
|
|
|
}
|
|
|
|
|
else if (shapeNode.GetChild("Footprint").IsOk())
|
|
|
|
|
{
|
2013-09-11 13:41:53 -07:00
|
|
|
CmpPtr<ICmpFootprint> cmpFootprint(GetEntityHandle());
|
2012-02-07 18:46:15 -08:00
|
|
|
if (cmpFootprint)
|
2011-11-24 22:36:13 -08:00
|
|
|
{
|
|
|
|
|
ICmpFootprint::EShape fpShape; // fp stands for "footprint"
|
|
|
|
|
entity_pos_t fpSize0, fpSize1, fpHeight; // fp stands for "footprint"
|
|
|
|
|
cmpFootprint->GetShape(fpShape, fpSize0, fpSize1, fpHeight);
|
|
|
|
|
|
|
|
|
|
float size0 = fpSize0.ToFloat();
|
|
|
|
|
float size1 = fpSize1.ToFloat();
|
|
|
|
|
|
2016-10-10 03:37:58 -07:00
|
|
|
// TODO: we should properly distinguish between CIRCLE and SQUARE footprint shapes here, but since cylinders
|
|
|
|
|
// aren't implemented yet and are almost indistinguishable from boxes for small enough sizes anyway,
|
|
|
|
|
// we'll just use boxes for either case. However, for circular footprints the size0 and size1 values both
|
|
|
|
|
// represent the radius, so we do have to adjust them to match the size1 and size0's of square footprints
|
2011-11-24 22:36:13 -08:00
|
|
|
// (which represent the full width and depth).
|
|
|
|
|
if (fpShape == ICmpFootprint::CIRCLE)
|
|
|
|
|
{
|
|
|
|
|
size0 *= 2;
|
|
|
|
|
size1 *= 2;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 12:14:40 -07:00
|
|
|
m_ShapeDescriptor = new CModelAbstract::CustomSelectionShape;
|
|
|
|
|
m_ShapeDescriptor->m_Type = CModelAbstract::CustomSelectionShape::BOX;
|
|
|
|
|
m_ShapeDescriptor->m_Size0 = size0;
|
|
|
|
|
m_ShapeDescriptor->m_Size1 = size1;
|
|
|
|
|
m_ShapeDescriptor->m_Height = fpHeight.ToFloat();
|
2011-11-24 22:36:13 -08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-01-22 12:31:30 -08:00
|
|
|
LOGERROR("[VisualActor] Cannot apply footprint-based SelectionShape; Footprint component not initialized.");
|
2011-11-24 22:36:13 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (shapeNode.GetChild("Box").IsOk())
|
|
|
|
|
{
|
|
|
|
|
// TODO: we might need to support the ability to specify a different box center in the future
|
2019-08-01 12:14:40 -07:00
|
|
|
m_ShapeDescriptor = new CModelAbstract::CustomSelectionShape;
|
|
|
|
|
m_ShapeDescriptor->m_Type = CModelAbstract::CustomSelectionShape::BOX;
|
|
|
|
|
m_ShapeDescriptor->m_Size0 = shapeNode.GetChild("Box").GetChild("@width").ToFixed().ToFloat();
|
|
|
|
|
m_ShapeDescriptor->m_Size1 = shapeNode.GetChild("Box").GetChild("@depth").ToFixed().ToFloat();
|
|
|
|
|
m_ShapeDescriptor->m_Height = shapeNode.GetChild("Box").GetChild("@height").ToFixed().ToFloat();
|
2011-11-24 22:36:13 -08:00
|
|
|
}
|
|
|
|
|
else if (shapeNode.GetChild("Cylinder").IsOk())
|
|
|
|
|
{
|
2015-01-22 12:31:30 -08:00
|
|
|
LOGWARNING("[VisualActor] TODO: Cylinder selection shapes are not yet implemented; defaulting to recursive bounding boxes");
|
2011-11-24 22:36:13 -08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// shouldn't happen by virtue of validation against schema
|
2015-01-22 12:31:30 -08:00
|
|
|
LOGERROR("[VisualActor] No selection shape specified");
|
2011-11-24 22:36:13 -08:00
|
|
|
}
|
|
|
|
|
}
|
2013-02-02 18:08:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CCmpVisualActor::ReloadActor()
|
|
|
|
|
{
|
|
|
|
|
if (!m_Unit)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Save some data from the old unit
|
|
|
|
|
CColor shading = m_Unit->GetModel().GetShadingColor();
|
|
|
|
|
player_id_t playerID = m_Unit->GetModel().GetPlayerID();
|
|
|
|
|
|
|
|
|
|
// Replace with the new unit
|
|
|
|
|
GetSimContext().GetUnitManager().DeleteUnit(m_Unit);
|
|
|
|
|
|
|
|
|
|
// HACK: selection shape needs template data, but rather than storing all that data
|
|
|
|
|
// in the component, we load the template here and pass it into a helper function
|
2013-09-11 13:41:53 -07:00
|
|
|
CmpPtr<ICmpTemplateManager> cmpTemplateManager(GetSystemEntity());
|
2013-02-02 18:08:20 -08:00
|
|
|
const CParamNode* node = cmpTemplateManager->LoadLatestTemplate(GetEntityId());
|
|
|
|
|
ENSURE(node && node->GetChild("VisualActor").IsOk());
|
|
|
|
|
|
2019-08-01 12:14:40 -07:00
|
|
|
InitSelectionShapeDescriptor(node->GetChild("VisualActor"));
|
|
|
|
|
|
|
|
|
|
InitModel();
|
2013-02-02 18:08:20 -08:00
|
|
|
|
2021-04-24 02:39:33 -07:00
|
|
|
if (!m_Unit)
|
|
|
|
|
{
|
|
|
|
|
if (m_ModelTag.valid())
|
|
|
|
|
{
|
|
|
|
|
CmpPtr<ICmpUnitRenderer> cmpModelRenderer(GetSystemEntity());
|
|
|
|
|
if (cmpModelRenderer)
|
|
|
|
|
cmpModelRenderer->RemoveUnit(m_ModelTag);
|
|
|
|
|
m_ModelTag = ICmpUnitRenderer::tag_t{};
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-10 03:49:49 -07:00
|
|
|
ReloadUnitAnimation();
|
2013-02-02 18:08:20 -08:00
|
|
|
|
|
|
|
|
m_Unit->GetModel().SetShadingColor(shading);
|
|
|
|
|
|
|
|
|
|
m_Unit->GetModel().SetPlayerID(playerID);
|
2014-06-01 11:24:50 -07:00
|
|
|
|
|
|
|
|
if (m_ModelTag.valid())
|
|
|
|
|
{
|
|
|
|
|
CmpPtr<ICmpUnitRenderer> cmpModelRenderer(GetSystemEntity());
|
|
|
|
|
CBoundingBoxAligned bounds = m_Unit->GetModel().GetWorldBoundsRec();
|
|
|
|
|
CBoundingSphere boundSphere = CBoundingSphere::FromSweptBox(bounds);
|
|
|
|
|
cmpModelRenderer->UpdateUnit(m_ModelTag, m_Unit, boundSphere);
|
|
|
|
|
}
|
2011-11-24 22:36:13 -08:00
|
|
|
}
|
|
|
|
|
|
2016-10-10 03:49:49 -07:00
|
|
|
void CCmpVisualActor::ReloadUnitAnimation()
|
2010-09-03 02:55:14 -07:00
|
|
|
{
|
2016-10-08 01:49:35 -07:00
|
|
|
if (!m_Unit)
|
2010-09-03 02:55:14 -07:00
|
|
|
return;
|
|
|
|
|
|
2016-10-10 03:49:49 -07:00
|
|
|
m_Unit->SetEntitySelection(m_VariantSelections);
|
2010-09-03 02:55:14 -07:00
|
|
|
|
2016-10-10 03:49:49 -07:00
|
|
|
if (!m_Unit->GetAnimation())
|
|
|
|
|
return;
|
2012-12-06 11:46:13 -08:00
|
|
|
|
2016-10-10 03:49:49 -07:00
|
|
|
m_Unit->GetAnimation()->SetAnimationState(m_AnimName, m_AnimOnce, m_AnimSpeed.ToFloat(), m_AnimDesync.ToFloat(), m_SoundGroup.c_str());
|
2010-09-03 02:55:14 -07:00
|
|
|
|
2016-10-10 03:49:49 -07:00
|
|
|
// We'll lose the exact synchronisation but we should at least make sure it's going at the correct rate
|
|
|
|
|
if (!m_AnimSyncRepeatTime.IsZero())
|
|
|
|
|
m_Unit->GetAnimation()->SetAnimationSyncRepeat(m_AnimSyncRepeatTime.ToFloat());
|
|
|
|
|
if (!m_AnimSyncOffsetTime.IsZero())
|
|
|
|
|
m_Unit->GetAnimation()->SetAnimationSyncOffset(m_AnimSyncOffsetTime.ToFloat());
|
|
|
|
|
}
|