-
Notifications
You must be signed in to change notification settings - Fork 11
/
SyncedAnimation.cpp
581 lines (491 loc) · 17.8 KB
/
SyncedAnimation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#include "SyncedAnimation.h"
#include "tinyxml2.h"
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
std::string fileNameSynchedAnim = "SceneDirectorSynchedAnim.xml";
SyncedAnimation::SyncedAnimation()
{
m_isNull = true;
}
SyncedAnimation::SyncedAnimation(std::string title, std::vector<Animation> actorAnimations, float deltaZLocation)
{
m_title = title;
m_actorAnimations = actorAnimations;
m_deltaZLocation = deltaZLocation;
m_isNull = false;
}
SyncedAnimation::SyncedAnimation(std::string title, std::string category, bool isProperSynced, std::vector<Animation> actorAnimations, std::vector<Animation> objectAnimations, std::vector<GTAObject> syncObjects, float deltaZLocation)
{
m_title = title;
//log_to_file("SyncedAnimation: " + m_title + " Category " + category);
m_category = category;
if (m_category.empty()) {
m_category = "<missing category>";
}
m_isProperSynced = isProperSynced;
m_actorAnimations = actorAnimations;
m_objectAnimations = objectAnimations;
m_deltaZLocation = deltaZLocation;
m_syncObjects = syncObjects;
m_isNull = false;
//log_to_file("SyncedAnimation end");
}
void SyncedAnimation::executeSyncedAnimation(bool silent, std::vector<Actor*> syncActors, bool useFirstActorLocation, Vector3 directLocation, bool doLoop, bool useFirstActorRotation, float rotation)
{
log_to_file("SyncedAnimation->executeSyncedAnimation " + toString());
DWORD ticksStart = GetTickCount();
m_doLooping = doLoop;
if (syncActors.size() < m_actorAnimations.size()) {
if (!silent) {
set_status_text("Missing " + std::to_string(m_actorAnimations.size()- syncActors.size()) + " actor(s) but will still execute synched animation");
}
}
log_to_file("m_objectAnimation ");
//must be at least as many objects as object animations
for (int i = 0; i < m_objectAnimations.size(); i++) {
if (m_syncObjects.size() <= i || m_syncObjects[i].isNull()) {
set_status_text("Must have at least " + std::to_string(m_objectAnimations.size()) + " objects");
return;
}
}
//load actor animations
for (auto &animation : m_actorAnimations) {
STREAMING::REQUEST_ANIM_DICT(animation.animLibrary);
log_to_file("Loading animation " + animation.toString());
while (!STREAMING::HAS_ANIM_DICT_LOADED(animation.animLibrary))
{
WAIT(0);
if (GetTickCount() - ticksStart > 5000) {
set_status_text("Failed to load actor animations");
return;
}
}
}
//load object animations
for (auto &animation : m_objectAnimations) {
STREAMING::REQUEST_ANIM_DICT(animation.animLibrary);
while (!STREAMING::HAS_ANIM_DICT_LOADED(animation.animLibrary))
{
WAIT(0);
if (GetTickCount() - ticksStart > 5000) {
set_status_text("Failed to load object animations");
return;
}
}
}
//load objects
for (auto >aObject : m_syncObjects) {
if (gtaObject.objHash == -1) {
gtaObject.objHash = GAMEPLAY::GET_HASH_KEY(gtaObject.objName);
}
STREAMING::REQUEST_MODEL(gtaObject.objHash);
while (!STREAMING::HAS_MODEL_LOADED(gtaObject.objHash))
{
WAIT(0);
if (GetTickCount() - ticksStart > 5000) {
log_to_file("Failed to load GTAObject name:" + std::string(gtaObject.objName) + " Hash:" + std::to_string(gtaObject.objHash));
set_status_text("Failed to load objects");
return;
}
}
}
//Setup scene for synced animation
Vector3 sceneLocation = directLocation;
if (useFirstActorLocation && syncActors.size() >= 1) {
sceneLocation = ENTITY::GET_ENTITY_COORDS(syncActors.at(0)->getActorPed(), true);
}
else {
//Vector3 actorLocation = ENTITY::GET_ENTITY_COORDS(syncActors.at(0)->getActorPed(), true);
//log_to_file("Difference in location x:" + std::to_string(sceneLocation.x - actorLocation.x) + " y:" + std::to_string(sceneLocation.y - actorLocation.y) + " z:" + std::to_string(sceneLocation.z - actorLocation.z));
}
float m_currentRotation = rotation;
if (useFirstActorRotation && syncActors.size() >= 1) {
m_currentRotation = ENTITY::GET_ENTITY_ROTATION(syncActors.at(0)->getActorPed(), 2).z;
}
if (m_isProperSynced) {
log_to_file("About to create scene startHeading=" + std::to_string(m_currentRotation));
m_sceneId = PED::CREATE_SYNCHRONIZED_SCENE(sceneLocation.x, sceneLocation.y, sceneLocation.z + m_deltaZLocation, 0.0, 0.0, m_currentRotation, 2);
PED::SET_SYNCHRONIZED_SCENE_LOOPED(m_sceneId, doLoop);
log_to_file("About to add animations for actors");
//Add the animations to the scene
int actorIndex = 0;
for (auto &animation : m_actorAnimations) {
if (syncActors.size() > actorIndex) {
log_to_file("Adding animation to " + std::to_string(syncActors.at(actorIndex)->getActorPed()) + " actor index:" + std::to_string(actorIndex));
AI::TASK_SYNCHRONIZED_SCENE(syncActors.at(actorIndex)->getActorPed(), m_sceneId, animation.animLibrary, animation.animName, 1000.0, -4.0, 64, 0, 0x447a0000, 0);
m_pedsInScene.push_back(syncActors.at(actorIndex)->getActorPed());
m_pedsInSceneStartLocation.push_back(ENTITY::GET_ENTITY_COORDS(syncActors.at(actorIndex)->getActorPed(), true));
m_pedsInSceneStartHeading.push_back(ENTITY::GET_ENTITY_HEADING(syncActors.at(actorIndex)->getActorPed()));
actorIndex++;
}
else {
log_to_file("Too few actors, so skipping animation " + animation.strShortcutIndex);
}
}
log_to_file("About to add animations for objects");
int objectIndex = 0;
for (auto &animation : m_objectAnimations) {
//create object if it doesn't exist
if (m_syncObjects[objectIndex].objReference == 0) {
log_to_file("About to create object with name " + std::string(m_syncObjects[objectIndex].objName) + " and hash " + std::to_string(m_syncObjects[objectIndex].objHash));
int newObjectRef = OBJECT::CREATE_OBJECT(m_syncObjects[objectIndex].objHash, sceneLocation.x, sceneLocation.y, sceneLocation.z, true, true, false);
m_syncObjects[objectIndex].objReference = newObjectRef;
m_objectsInScene.push_back(newObjectRef);
}
log_to_file("Object animation objReference:" + std::to_string(m_syncObjects[objectIndex].objReference) + " Animation " + animation.animName);
ENTITY::PLAY_SYNCHRONIZED_ENTITY_ANIM(m_syncObjects[objectIndex].objReference, m_sceneId, animation.animName, animation.animLibrary, 1000.0, -4.0, 0, 0x447a0000);
objectIndex++;
}
log_to_file("About to execute the synchronized scene");
//execute the scene
PED::SET_SYNCHRONIZED_SCENE_PHASE(m_sceneId, 0.0);
}
else {//"fake" synchronized scene where we should not teleport actors, only play animations on
m_ticksStarted = GetTickCount();
int actorIndex = 0;
for (auto &animation : m_actorAnimations) {
if (syncActors.size() > actorIndex) {
int duration = animation.duration;
if (doLoop) {
duration = -1;
}
AI::TASK_PLAY_ANIM(syncActors.at(actorIndex)->getActorPed(), animation.animLibrary, animation.animName, 8.0f, -8.0f, duration, getDefaultAnimationFlag().id, 8.0f, 0, 0, 0);
m_pedsInScene.push_back(syncActors.at(actorIndex)->getActorPed());
m_pedsInSceneStartLocation.push_back(ENTITY::GET_ENTITY_COORDS(syncActors.at(actorIndex)->getActorPed(), true));
m_pedsInSceneStartHeading.push_back(ENTITY::GET_ENTITY_HEADING(syncActors.at(actorIndex)->getActorPed()));
actorIndex++;
}
else {
log_to_file("Too few actors, so skipping animation " + animation.strShortcutIndex);
}
}
}
}
void SyncedAnimation::previewSyncedAnimation(std::vector<Actor*> syncActors, bool useFirstActorLocation, Vector3 directLocation, bool doLoop, bool useFirstActorRotation, float rotation)
{
executeSyncedAnimation(false,syncActors, useFirstActorLocation, directLocation, true, useFirstActorLocation, rotation);
PED::SET_SYNCHRONIZED_SCENE_RATE(m_sceneId, 0.1f);
}
void SyncedAnimation::updateLocationOfScene(Vector3 location)
{
if (m_sceneId != 0) {
PED::SET_SYNCHRONIZED_SCENE_ORIGIN(m_sceneId, location.x, location.y, location.z, 0, 0, m_currentRotation, 2);
}
}
bool SyncedAnimation::isCompleted()
{
if (m_isProperSynced) {
if (m_sceneId != 0) {
if (PED::IS_SYNCHRONIZED_SCENE_LOOPED(m_sceneId)) {
return false;
}
float sceneStatus = PED::GET_SYNCHRONIZED_SCENE_PHASE(m_sceneId);
if (sceneStatus >= 1.0) {
return true;
}
else {
return false;
}
}
else {
return true;
}
}
else {
if (m_doLooping) {
log_to_file("Is looping therefore not complete");
return false;
}
int maxLength = 0;
for (auto &animation : m_actorAnimations) {
if (animation.duration > maxLength) {
maxLength = animation.duration;
}
}
if (GetTickCount() - m_ticksStarted >= (DWORD) maxLength) {
return true;
}
else {
return false;
}
}
}
void SyncedAnimation::cleanupAfterExecution(bool doDeleteObjects, bool teleportActorsBackToStart)
{
log_to_file("SyncedAnimation cleanupAfterExecution ");
if (m_isProperSynced) {
int i = 0;
for (auto &ped : m_pedsInScene) {
//no idea what 2+3 param is
ENTITY::STOP_SYNCHRONIZED_ENTITY_ANIM(ped, 0.0, 1);
if (teleportActorsBackToStart) {
AI::CLEAR_PED_TASKS(ped);
ENTITY::SET_ENTITY_COORDS_NO_OFFSET(ped, m_pedsInSceneStartLocation[i].x, m_pedsInSceneStartLocation[i].y, m_pedsInSceneStartLocation[i].z, 0, 0, 1);
ENTITY::SET_ENTITY_HEADING(ped, m_pedsInSceneStartHeading[i]);
}
i++;
}
for (auto &obj : m_objectsInScene) {
//no idea what 2+3 param is
ENTITY::STOP_SYNCHRONIZED_ENTITY_ANIM(obj, 0.0, 1);
if (doDeleteObjects) {
OBJECT::DELETE_OBJECT(&obj);
}
}
if (doDeleteObjects) {
for (auto >aObject : m_syncObjects) {
gtaObject.objReference = 0;
}
}
log_to_file("SyncedAnimation trying to dispose scene ");
//dispose scene
PED::_0xCD9CC7E200A52A6F(m_sceneId);
m_sceneId = 0;
}
else {
int i = 0;
for (auto &ped : m_pedsInScene) {
//no idea what 2+3 param is
AI::CLEAR_PED_TASKS(ped);
if (teleportActorsBackToStart) {
ENTITY::SET_ENTITY_COORDS_NO_OFFSET(ped, m_pedsInSceneStartLocation[i].x, m_pedsInSceneStartLocation[i].y, m_pedsInSceneStartLocation[i].z, 0, 0, 1);
ENTITY::SET_ENTITY_HEADING(ped, m_pedsInSceneStartHeading[i]);
}
i++;
}
for (auto &obj : m_objectsInScene) {
//no idea what 2+3 param is
ENTITY::STOP_SYNCHRONIZED_ENTITY_ANIM(obj, 0.0, 1);
if (doDeleteObjects) {
OBJECT::DELETE_OBJECT(&obj);
}
}
}
}
void SyncedAnimation::setLooping(bool doLoop)
{
if (m_sceneId!=0 && m_isProperSynced) {
log_to_file("Setting looping for scene to " + doLoop);
PED::SET_SYNCHRONIZED_SCENE_LOOPED(m_sceneId, doLoop);
}
}
bool SyncedAnimation::matchesFilter(std::string filterStr)
{
std::string m_titleLC = std::string(m_title);
std::transform(m_titleLC.begin(), m_titleLC.end(), m_titleLC.begin(), ::tolower);
std::transform(filterStr.begin(), filterStr.end(), filterStr.begin(), ::tolower);
std::vector<std::string> filterTokens = StringUtils::split(filterStr, ' ');
FILTER_OPERATORS currentFitlerOperator = FILTER_OR;
bool matchesFilter = true;
int nrMatches = 0;
for (auto token : filterTokens) {
if (!token.empty() && (token.compare("AND") == 0 || token.compare("and") == 0)) {
currentFitlerOperator = FILTER_AND;
continue;
}
else if (!token.empty() && (token.compare("OR") == 0 || token.compare("or") == 0)) {
currentFitlerOperator = FILTER_OR;
continue;
}
else if (!token.empty() && (token.compare("NOT") == 0 || token.compare("not") == 0)) {
currentFitlerOperator = FILTER_NOT;
continue;
}
if (currentFitlerOperator == FILTER_AND) {
if (m_titleLC.find(token) == std::string::npos) {
return false;
}
}
else if (currentFitlerOperator == FILTER_NOT) {
if (m_titleLC.find(token) != std::string::npos) {
return false;
}
}
else {//FILTER_OR
if (m_titleLC.find(token) != std::string::npos) {
nrMatches++;
}
}
}
if (nrMatches > 0) {
return true;
}
else {
return false;
}
}
bool SyncedAnimation::matchesCategory(std::string categoryStr)
{
if (std::string(m_category).find(categoryStr) == std::string::npos || (categoryStr.empty() && m_category.empty())) {
return false;
}
else {
return true;
}
}
bool SyncedAnimation::isNull()
{
return m_isNull;
}
SyncedAnimation* SyncedAnimation::createCopy()
{
//remove any references
for (auto >aObject : m_syncObjects) {
gtaObject.objReference = 0;
}
SyncedAnimation* syncedAnimCopy = new SyncedAnimation(m_title,m_category,m_isProperSynced, m_actorAnimations, m_objectAnimations,m_syncObjects, m_deltaZLocation);
return syncedAnimCopy;
}
std::string SyncedAnimation::toString()
{
std::string objString = m_title + " Anims:";
for (auto &animation : m_actorAnimations) {
objString += std::to_string(animation.shortcutIndex) + " ";
}
if (m_objectAnimations.size() > 0) {
objString += "ObjectAnims:";
for (auto &animation : m_objectAnimations) {
objString += std::to_string(animation.shortcutIndex) + " ";
}
}
objString += "Z-adjustment:" + roundNumber(m_deltaZLocation);
return objString;
}
void SyncedAnimation::clearObjectReferences()
{
for (auto >aObject : m_syncObjects) {
gtaObject.objReference = 0;
}
}
std::string SyncedAnimation::getCategory()
{
return m_category;
}
std::string SyncedAnimation::getTitle()
{
return m_title;
}
bool SyncedAnimation::isProperSynced()
{
return m_isProperSynced;
}
int SyncedAnimation::getLength()
{
int maxLength = 0;
for (auto animation : m_actorAnimations) {
if (animation.duration > maxLength) {
maxLength = animation.duration;
}
}
return maxLength;
}
int SyncedAnimation::getNrOfActors()
{
return m_actorAnimations.size();
}
bool SyncedAnimation::isActive()
{
if (m_sceneId == 0) {
return false;
} {
return true;
}
}
float SyncedAnimation::getDeltaZ()
{
return m_deltaZLocation;
}
void SyncedAnimation::setDeltaZ(float deltaZLocation)
{
m_deltaZLocation = deltaZLocation;
}
//Defined all SyncedAnimations identified so far
//SyncedAnimation aSyncedAnimation = SyncedAnimation("Test",std::vector<Animation> { getAnimationForShortcutIndex(1801), getAnimationForShortcutIndex(1803) }, 0.0);
std::vector<SyncedAnimation> gtaSyncedAnimations;
bool initializeSyncedAnimations() {
log_to_file("action_load_synchedanims");
tinyxml2::XMLDocument doc = new tinyxml2::XMLDocument();
doc.LoadFile(fileNameSynchedAnim.c_str());
if (doc.Error()) {
set_status_text("Synched animations file file " + fileNameSynchedAnim + " could not be loaded. Error: " + doc.ErrorName());
log_to_file("Synched animations file file " + fileNameSynchedAnim + " could not be loaded. Error: " + doc.ErrorName());
return false;
}
tinyxml2::XMLElement* synchedAnimationsElement = doc.RootElement();
int synchedAnimsLoaded = 0;
for (tinyxml2::XMLElement* synchedAnimElement = synchedAnimationsElement->FirstChildElement("SynchedAnim");
synchedAnimElement;
synchedAnimElement = synchedAnimElement->NextSiblingElement())
{
std::string title = synchedAnimElement->Attribute("title");
std::string category = synchedAnimElement->Attribute("category");
bool actorsAligned = synchedAnimElement->BoolAttribute("actorsAligned");
float deltaZ = synchedAnimElement->FloatAttribute("deltaZ");
std::vector<Animation> actorAnims;
for (tinyxml2::XMLElement* actorAnimElement = synchedAnimElement->FirstChildElement("ActorAnim");
actorAnimElement;
actorAnimElement = actorAnimElement->NextSiblingElement("ActorAnim"))
{
int animIndex = actorAnimElement->IntAttribute("animIndex");
actorAnims.push_back(getAnimationForShortcutIndex(animIndex));
}
std::vector<Animation> objectAnims;
for (tinyxml2::XMLElement* objectAnimElement = synchedAnimElement->FirstChildElement("ObjectAnim");
objectAnimElement;
objectAnimElement = objectAnimElement->NextSiblingElement("ObjectAnim"))
{
int animIndex = objectAnimElement->IntAttribute("animIndex");
objectAnims.push_back(getAnimationForShortcutIndex(animIndex));
}
std::vector<GTAObject> objectProps;
for (tinyxml2::XMLElement* objectPropsElement = synchedAnimElement->FirstChildElement("Object");
objectPropsElement;
objectPropsElement = objectPropsElement->NextSiblingElement("Object"))
{
std::string propName = objectPropsElement->Attribute("propName");
objectProps.push_back(getGTAObjectFromObjName(propName));
}
//log_to_file("SynchedAnim " + title + " - " + category + " ActorAnims " + std::to_string(actorAnims.size()) + " ObjectAnims " + std::to_string(objectAnims.size()) + " ObjectProps " + std::to_string(objectProps.size()));
gtaSyncedAnimations.push_back(SyncedAnimation(title, category, actorsAligned, actorAnims, objectAnims, objectProps, deltaZ));
synchedAnimsLoaded++;
}
set_status_text("Loaded " + std::to_string(synchedAnimsLoaded) + " synchronized anims from " + fileNameSynchedAnim);
log_to_file("Loaded " + std::to_string(synchedAnimsLoaded) + " synchronized anims from " + fileNameSynchedAnim);
return true;
}
std::vector<SyncedAnimation> getAllSyncedAnimations() {
return gtaSyncedAnimations;
}
std::vector<SyncedAnimation> getSyncedAnimations(std::string category) {
std::vector<SyncedAnimation> syncedAnimations;
for (auto syncedAnim : gtaSyncedAnimations) {
if (syncedAnim.matchesCategory(category) ) {
syncedAnimations.push_back(syncedAnim);
}
}
//std::sort(syncedAnimations.begin(), syncedAnimations.end());
return syncedAnimations;
}
std::vector<std::pair<std::string, int>> getAllSyncedAnimationCategories()
{
std::map <std::string,std::string> mapCategories;
log_to_file("Copying categories to map");
for (auto syncedAnim : gtaSyncedAnimations) {
mapCategories.insert({ syncedAnim.getCategory(),syncedAnim.getCategory() });
}
log_to_file("Copying categories to vector");
std::vector<std::pair<std::string, int>> vCategories;
for (std::map <std::string, std::string>::iterator it = mapCategories.begin(); it != mapCategories.end(); ++it) {
std::string category = it->first;
//not the most efficient, but shouldn't be a perf issue
int count = getSyncedAnimations(category).size();
vCategories.push_back(std::make_pair(category, count));
}
log_to_file("Sorting categories");
std::sort(vCategories.begin(), vCategories.end());
return vCategories;
}