Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "UnitAnimation.h" 21 : 22 : #include "graphics/Model.h" 23 : #include "graphics/ModelAbstract.h" 24 : #include "graphics/ObjectEntry.h" 25 : #include "graphics/SkeletonAnim.h" 26 : #include "graphics/SkeletonAnimDef.h" 27 : #include "lib/debug.h" 28 : #include "lib/rand.h" 29 : #include "ps/CStr.h" 30 : #include "ps/Game.h" 31 : #include "simulation2/components/ICmpSoundManager.h" 32 : #include "simulation2/system/CmpPtr.h" 33 : 34 : #include <cmath> 35 : #include <cstdlib> 36 : #include <memory> 37 : #include <string> 38 : 39 : // Randomly modify the speed, so that units won't stay perfectly 40 : // synchronised if they're playing animations of the same length 41 0 : static float DesyncSpeed(float speed, float desync) 42 : { 43 0 : if (desync == 0.0f) 44 : return speed; 45 : 46 0 : return speed * (1.f - desync + 2.f*desync*(rand(0, 256)/255.f)); 47 : } 48 : 49 1 : CUnitAnimation::CUnitAnimation(entity_id_t ent, CModel* model, CObjectEntry* object) 50 1 : : m_Entity(ent), m_State("idle"), m_Looping(true), 51 1 : m_Speed(1.f), m_SyncRepeatTime(0.f), m_OriginalSpeed(1.f), m_Desync(0.f) 52 : { 53 1 : ReloadUnit(model, object); 54 1 : } 55 : 56 1 : void CUnitAnimation::AddModel(CModel* model, const CObjectEntry* object) 57 : { 58 1 : SModelAnimState state; 59 : 60 1 : state.model = model; 61 1 : state.object = object; 62 1 : state.anim = object->GetRandomAnimation(m_State, m_AnimationID); 63 1 : state.time = 0.f; 64 1 : state.pastLoadPos = false; 65 1 : state.pastActionPos = false; 66 1 : state.pastSoundPos = false; 67 : 68 1 : ENSURE(state.anim != NULL); // there must always be an idle animation 69 : 70 1 : m_AnimStates.push_back(state); 71 : 72 1 : model->SetAnimation(state.anim, !m_Looping); 73 : 74 : // Detect if this unit has any non-static animations 75 2 : for (CSkeletonAnim* anim : object->GetAnimations(m_State)) 76 1 : if (anim->m_AnimDef != NULL) 77 0 : m_AnimStatesAreStatic = false; 78 : 79 : // Recursively add all props 80 1 : const std::vector<CModel::Prop>& props = model->GetProps(); 81 1 : for (const CModel::Prop& prop : props) 82 : { 83 0 : CModel* propModel = prop.m_Model->ToCModel(); 84 0 : if (propModel) 85 0 : AddModel(propModel, prop.m_ObjectEntry); 86 : } 87 1 : } 88 : 89 0 : void CUnitAnimation::ReloadAnimation() 90 : { 91 0 : ReloadUnit(m_Model, m_Object); 92 0 : } 93 : 94 1 : void CUnitAnimation::ReloadUnit(CModel* model, const CObjectEntry* object) 95 : { 96 1 : m_Model = model; 97 1 : m_Object = object; 98 : 99 1 : m_AnimStates.clear(); 100 1 : m_AnimStatesAreStatic = true; 101 1 : PickAnimationID(); 102 1 : AddModel(m_Model, m_Object); 103 1 : } 104 : 105 0 : void CUnitAnimation::SetAnimationState(const CStr& name, bool once, float speed, float desync, const CStrW& actionSound) 106 : { 107 0 : m_Looping = !once; 108 0 : m_OriginalSpeed = speed; 109 0 : m_Desync = desync; 110 0 : m_ActionSound = actionSound; 111 : 112 0 : m_Speed = DesyncSpeed(m_OriginalSpeed, m_Desync); 113 0 : m_SyncRepeatTime = 0.f; 114 : 115 0 : if (name != m_State) 116 : { 117 0 : m_State = name; 118 0 : ReloadAnimation(); 119 : } 120 0 : } 121 : 122 0 : void CUnitAnimation::SetAnimationSyncRepeat(float repeatTime) 123 : { 124 0 : m_SyncRepeatTime = repeatTime; 125 0 : } 126 : 127 0 : void CUnitAnimation::SetAnimationSyncOffset(float actionTime) 128 : { 129 0 : if (m_AnimStatesAreStatic) 130 : return; 131 : 132 : // Update all the synced prop models to each coincide with actionTime 133 0 : for (std::vector<SModelAnimState>::iterator it = m_AnimStates.begin(); it != m_AnimStates.end(); ++it) 134 : { 135 0 : CSkeletonAnimDef* animDef = it->anim->m_AnimDef; 136 0 : if (animDef == NULL) 137 0 : continue; // ignore static animations 138 : 139 0 : float duration = animDef->GetDuration(); 140 : 141 0 : float actionPos = it->anim->m_ActionPos; 142 0 : bool hasActionPos = (actionPos != -1.f); 143 : 144 0 : if (!hasActionPos) 145 0 : continue; 146 : 147 0 : float speed = duration / m_SyncRepeatTime; 148 : 149 : // Need to offset so that start+actionTime*speed = actionPos 150 0 : float start = actionPos - actionTime*speed; 151 : // Wrap it so that it's within the animation 152 0 : start = fmodf(start, duration); 153 0 : if (start < 0) 154 0 : start += duration; 155 : 156 0 : it->time = start; 157 : } 158 : } 159 : 160 0 : void CUnitAnimation::Update(float time) 161 : { 162 0 : if (m_AnimStatesAreStatic) 163 0 : return; 164 : 165 0 : bool shouldPlaySound = false; 166 : 167 : // Advance all of the prop models independently 168 0 : for (std::vector<SModelAnimState>::iterator it = m_AnimStates.begin(); it != m_AnimStates.end(); ++it) 169 : { 170 0 : CSkeletonAnimDef* animDef = it->anim->m_AnimDef; 171 0 : if (!animDef) 172 0 : continue; // ignore static animations 173 : 174 0 : float duration = animDef->GetDuration(); 175 0 : float actionPos = it->anim->m_ActionPos; 176 0 : float loadPos = it->anim->m_ActionPos2; 177 : // SoundPos is either the m_SoundPos when available, or m_ActionPos. Else no sound is played. 178 : // TODO: Should they be totally independent? 179 0 : float soundPos = it->anim->m_SoundPos != -1.f ? it->anim->m_SoundPos : actionPos; 180 0 : bool hasActionPos = (actionPos != -1.f); 181 0 : bool hasLoadPos = (loadPos != -1.f); 182 0 : bool hasSoundPos = (soundPos != -1.f); 183 : 184 : // Find the current animation speed 185 0 : float speed; 186 0 : if (m_SyncRepeatTime && hasActionPos) 187 0 : speed = duration / m_SyncRepeatTime; 188 : else 189 0 : speed = m_Speed * it->anim->m_Speed; 190 : 191 : // Convert from real time to scaled animation time 192 0 : float advance = time * speed; 193 0 : float nextPos = it->time + advance; 194 : 195 : // If we're going to advance past the load point in this update, then load the ammo 196 0 : if (hasLoadPos && !it->pastLoadPos && nextPos >= loadPos) 197 : { 198 0 : it->model->ShowAmmoProp(); 199 0 : it->pastLoadPos = true; 200 : } 201 : 202 : // If we're going to advance past the action point in this update, then perform the action. 203 0 : if (hasActionPos && !it->pastActionPos && nextPos >= actionPos) 204 : { 205 0 : if (hasLoadPos) 206 0 : it->model->HideAmmoProp(); 207 : 208 0 : it->pastActionPos = true; 209 : } 210 : 211 : // If we're going to advance past the sound point in this update, then play the sound. 212 0 : if (hasSoundPos && !it->pastSoundPos && nextPos >= soundPos && !m_ActionSound.empty()) 213 : { 214 0 : shouldPlaySound = true; 215 0 : it->pastSoundPos = true; 216 : } 217 : 218 0 : if (nextPos < duration) 219 : { 220 : // If we're still within the current animation, then simply update it 221 0 : it->time += advance; 222 0 : it->model->UpdateTo(it->time); 223 : } 224 0 : else if (m_Looping) 225 : { 226 : // If we've finished the current animation and want to loop... 227 : 228 : // Wrap the timer around 229 0 : it->time = std::fmod(nextPos, duration); 230 : 231 : // Pick a new random animation 232 0 : CSkeletonAnim* anim; 233 0 : if (it->model == m_Model) 234 : { 235 : // we're handling the root model 236 : // choose animations from the complete state 237 0 : CStr oldID = m_AnimationID; 238 0 : PickAnimationID(); 239 0 : anim = it->object->GetRandomAnimation(m_State, m_AnimationID); 240 0 : if (oldID != m_AnimationID) 241 0 : for (SModelAnimState animState : m_AnimStates) 242 0 : if (animState.model != m_Model) 243 0 : animState.model->SetAnimation(animState.object->GetRandomAnimation(m_State, m_AnimationID)); 244 0 : } 245 : else 246 : // choose animations that match the root 247 0 : anim = it->object->GetRandomAnimation(m_State, m_AnimationID); 248 : 249 0 : if (anim != it->anim) 250 : { 251 0 : it->anim = anim; 252 0 : it->model->SetAnimation(anim, !m_Looping); 253 : } 254 : 255 0 : it->pastActionPos = false; 256 0 : it->pastLoadPos = false; 257 0 : it->pastSoundPos = false; 258 : 259 0 : it->model->UpdateTo(it->time); 260 : } 261 : else 262 : { 263 : // If we've finished the current animation and don't want to loop... 264 : 265 : // Update to very nearly the end of the last frame (but not quite the end else we'll wrap around when skinning) 266 0 : float nearlyEnd = duration - 1.f; 267 0 : if (std::abs(it->time - nearlyEnd) > 1.f) 268 : { 269 0 : it->time = nearlyEnd; 270 0 : it->model->UpdateTo(it->time); 271 : } 272 : } 273 : } 274 : 275 0 : if (!shouldPlaySound) 276 : return; 277 : 278 0 : CmpPtr<ICmpSoundManager> cmpSoundManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY); 279 0 : if (cmpSoundManager) 280 0 : cmpSoundManager->PlaySoundGroup(m_ActionSound, m_Entity); 281 : } 282 : 283 1 : void CUnitAnimation::PickAnimationID() 284 : { 285 2 : m_AnimationID = m_Object->GetRandomAnimation(m_State)->m_ID; 286 1 : }