-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGraphBuilder.cpp
More file actions
739 lines (625 loc) · 27.2 KB
/
GraphBuilder.cpp
File metadata and controls
739 lines (625 loc) · 27.2 KB
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
#include "GraphBuilder.h"
#include "CKAll.h"
#include <queue>
#include <stdexcept>
#undef min
#undef max
GraphBuilder::GraphBuilder(InterfaceData &targetData, CKContext *context)
: m_Data(targetData), m_Context(context) {}
void GraphBuilder::BuildGraph(CKBehavior *rootBehavior) {
if (!rootBehavior)
return;
// Initialize data structures
InitializeState();
// Process behaviors using BFS traversal
ProcessBehaviorTree(rootBehavior);
// Configure parameter links
ConfigureParameterLinks();
// Notify observers of changes
m_Data.NotifyObservers(nullptr, InterfaceData::ElementAction::Modified);
}
void GraphBuilder::InitializeState() {
m_Data.Clear();
m_BehaviorMap.clear();
m_OperationMap.clear();
m_InputParamSet.clear();
m_OutputParamSet.clear();
m_InputParams.clear();
m_OutputParams.clear();
}
void GraphBuilder::ProcessBehaviorTree(CKBehavior *rootBehavior) {
std::queue<std::pair<CKBehavior *, int>> behaviorQueue;
behaviorQueue.emplace(rootBehavior, 0);
while (!behaviorQueue.empty()) {
auto pair = behaviorQueue.front();
CKBehavior *behavior = pair.first;
int depth = pair.second;
behaviorQueue.pop();
// Create a new behavior data if not the root
BehaviorData *behaviorData = depth > 0 ? &m_Data.NewBehavior() : &m_Data.rootBehavior;
// Store behavior ID and mapping
CK_ID behaviorId = behavior->GetID();
m_BehaviorMap[behaviorId] = depth > 0 ? static_cast<int>(m_Data.behaviors.size()) - 1 : -1;
// Map operation IDs to indices
MapOperations(behavior, depth);
// Set up the behavior
SetupBehavior(*behaviorData, behavior, depth);
// Enqueue sub-behaviors for processing
EnqueueSubBehaviors(behavior, depth, behaviorQueue);
}
}
void GraphBuilder::MapOperations(CKBehavior *behavior, int depth) {
const int operationCount = behavior->GetParameterOperationCount();
for (int i = 0; i < operationCount; ++i) {
if (CKParameterOperation *operation = behavior->GetParameterOperation(i)) {
m_OperationMap[operation->GetID()] = std::make_pair(
depth > 0 ? m_Data.behaviors.size() - 1 : -1, i);
}
}
}
void GraphBuilder::EnqueueSubBehaviors(CKBehavior *behavior, int depth,
std::queue<std::pair<CKBehavior *, int>> &queue) {
const int subBehaviorCount = behavior->GetSubBehaviorCount();
for (int i = 0; i < subBehaviorCount; ++i) {
if (CKBehavior *subBehavior = behavior->GetSubBehavior(i)) {
queue.emplace(subBehavior, depth + 1);
}
}
}
BehaviorData &GraphBuilder::GetBehaviorData(CK_ID id) {
const int index = m_BehaviorMap[id];
return index >= 0 ? m_Data.behaviors[index] : m_Data.rootBehavior;
}
bool GraphBuilder::IsOperation(CK_ID id) const {
CKObject *obj = m_Context->GetObject(id);
return obj && obj->GetClassID() == CKCID_PARAMETEROPERATION;
}
void GraphBuilder::SetupBehavior(BehaviorData &behaviorData, CKBehavior *behavior, int depth) {
behaviorData.id = behavior->GetID();
behaviorData.folded = depth > 0;
behaviorData.depth = depth;
behaviorData.isBehaviorGraph = behavior->GetType() != CKBEHAVIORTYPE_BASE;
// Process parameters
ProcessParameters(behaviorData, behavior);
// Process behavior graph elements if this is a behavior graph
if (behaviorData.isBehaviorGraph) {
AddBehaviorLinks(behaviorData, behavior);
AddOperations(behaviorData, behavior);
AddLocalParameters(behaviorData, behavior);
}
}
void GraphBuilder::ProcessParameters(BehaviorData &behaviorData, CKBehavior *behavior) {
// Process input parameters
const int inputParamCount = behavior->GetInputParameterCount();
for (int i = 0; i < inputParamCount; ++i) {
if (CKParameterIn *param = behavior->GetInputParameter(i)) {
AddInputParameter(param->GetID());
}
}
// Process output parameters
const int outputParamCount = behavior->GetOutputParameterCount();
for (int i = 0; i < outputParamCount; ++i) {
if (CKParameterOut *param = behavior->GetOutputParameter(i)) {
AddOutputParameter(param->GetID());
}
}
// Process target parameter if used
if (behavior->IsUsingTarget()) {
behaviorData.isUsingTarget = true;
if (CKParameterIn *targetParam = behavior->GetTargetParameter()) {
AddInputParameter(targetParam->GetID());
}
}
// Process operation parameters
const int operationCount = behavior->GetParameterOperationCount();
for (int i = 0; i < operationCount; ++i) {
ProcessOperationParameters(behavior->GetParameterOperation(i));
}
}
void GraphBuilder::ProcessOperationParameters(CKParameterOperation *operation) {
if (!operation) return;
// Process input parameter 1
if (CKParameterIn *inParam1 = operation->GetInParameter1()) {
AddInputParameter(inParam1->GetID());
}
// Process input parameter 2
if (CKParameterIn *inParam2 = operation->GetInParameter2()) {
AddInputParameter(inParam2->GetID());
}
// Process output parameter
if (CKParameterOut *outParam = operation->GetOutParameter()) {
AddOutputParameter(outParam->GetID());
}
}
void GraphBuilder::AddInputParameter(CK_ID paramId) {
if (m_InputParamSet.insert(paramId).second) {
m_InputParams.push_back(paramId);
}
}
void GraphBuilder::AddOutputParameter(CK_ID paramId) {
if (m_OutputParamSet.insert(paramId).second) {
m_OutputParams.push_back(paramId);
}
}
void GraphBuilder::AddBehaviorLinks(BehaviorData &behaviorData, CKBehavior *behavior) {
// Add behavior links
const int linkCount = behavior->GetSubBehaviorLinkCount();
for (int i = 0; i < linkCount; ++i) {
if (CKBehaviorLink *behaviorLink = behavior->GetSubBehaviorLink(i)) {
Link link = CreateBehaviorLink(behaviorLink);
behaviorData.AddLink(link);
}
}
}
Link GraphBuilder::CreateBehaviorLink(CKBehaviorLink *behaviorLink) {
// Set start endpoint
CKBehaviorIO *inputIO = behaviorLink->GetInBehaviorIO();
CKBehavior *inputBehavior = inputIO->GetOwner();
LinkEndpoint start = {inputBehavior->GetID(), inputBehavior->GetOutputPosition(inputIO), ENDPOINT_BOUT};
if (start.index == -1) {
start.index = inputBehavior->GetInputPosition(inputIO);
start.type = inputBehavior->GetType() == CKBEHAVIORTYPE_SCRIPT ? ENDPOINT_START_BIN : ENDPOINT_BIN;
}
// Set end endpoint
CKBehaviorIO *outputIO = behaviorLink->GetOutBehaviorIO();
CKBehavior *outputBehavior = outputIO->GetOwner();
LinkEndpoint end = {outputBehavior->GetID(), outputBehavior->GetInputPosition(outputIO), ENDPOINT_BIN};
if (end.index == -1) {
end.index = outputBehavior->GetOutputPosition(outputIO);
end.type = ENDPOINT_BOUT;
}
return {behaviorLink->GetID(), LINK_TYPE_BEHAVIOR, start, end};
}
void GraphBuilder::AddOperations(BehaviorData &behaviorData, CKBehavior *behavior) {
const int operationCount = behavior->GetParameterOperationCount();
for (int i = 0; i < operationCount; ++i) {
if (CKParameterOperation *operation = behavior->GetParameterOperation(i)) {
Operation operationData(operation->GetID());
behaviorData.AddOperation(operationData);
}
}
}
void GraphBuilder::AddLocalParameters(BehaviorData &behaviorData, CKBehavior *behavior) {
const int localParamCount = behavior->GetLocalParameterCount();
for (int i = 0; i < localParamCount; ++i) {
if (CKParameterLocal *localParam = behavior->GetLocalParameter(i)) {
Parameter paramData(localParam->GetID(), PARAM_STYLE_COLLAPSED);
behaviorData.AddLocalParameter(paramData);
}
}
}
GraphBuilder::ParameterPosition GraphBuilder::GetInputParameterPosition(CKParameterIn *inputParam, CKBehavior **owner) {
ParameterPosition position = {};
CKObject *ownerObject = inputParam->GetOwner();
position.id = ownerObject->GetID();
// Check if owner is a behavior
if (ownerObject->GetClassID() == CKCID_BEHAVIOR) {
CKBehavior *ownerBeh = (CKBehavior *) ownerObject;
position.index = ownerBeh->GetInputParameterPosition(inputParam);
// Handle target parameter
if (ownerBeh->IsUsingTarget() && ownerBeh->GetTargetParameter()->GetID() == inputParam->GetID()) {
position.index = -2;
}
*owner = ownerBeh->GetParent();
position.behaviorId = (*owner)->GetID();
return position;
}
// Check if owner is a parameter operation
if (ownerObject->GetClassID() == CKCID_PARAMETEROPERATION) {
CKParameterOperation *operation = (CKParameterOperation *) ownerObject;
position.index = operation->GetInParameter1()->GetID() == inputParam->GetID() ? 0 : 1;
*owner = operation->GetOwner();
position.behaviorId = (*owner)->GetID();
return position;
}
m_Context->OutputToConsoleEx((CKSTRING) "Error: Unknown owner type for input parameter");
throw std::runtime_error("Unknown owner type for input parameter");
}
GraphBuilder::ParameterPosition GraphBuilder::GetOutputParameterPosition(
CKParameterOut *outputParam, CKBehavior **ownerBehavior) {
ParameterPosition position = {};
CKObject *ownerObject = outputParam->GetOwner();
position.id = ownerObject->GetID();
// Check if owner is a behavior
if (ownerObject->GetClassID() == CKCID_BEHAVIOR) {
CKBehavior *ownerBeh = (CKBehavior *) ownerObject;
position.index = ownerBeh->GetOutputParameterPosition(outputParam);
*ownerBehavior = ownerBeh->GetParent();
position.behaviorId = (*ownerBehavior)->GetID();
return position;
}
// Check if owner is a parameter operation
if (ownerObject->GetClassID() == CKCID_PARAMETEROPERATION) {
CKParameterOperation *operation = (CKParameterOperation *) ownerObject;
position.index = 0;
*ownerBehavior = operation->GetOwner();
position.behaviorId = (*ownerBehavior)->GetID();
return position;
}
m_Context->OutputToConsoleEx((CKSTRING) "Error: Unknown owner type for output parameter");
throw std::runtime_error("Unknown owner type for output parameter");
}
GraphBuilder::ParameterPosition GraphBuilder::GetLocalParameterPosition(CKParameterLocal *localParam) {
ParameterPosition position = {};
CKObject *ownerObject = localParam->GetOwner();
// Owner must be a behavior
if (ownerObject->GetClassID() != CKCID_BEHAVIOR) {
m_Context->OutputToConsoleEx((CKSTRING) "Error: Local parameter owner is not a behavior");
throw std::runtime_error("Local parameter owner is not a behavior");
}
CKBehavior *ownerBeh = (CKBehavior *) ownerObject;
position.id = ownerBeh->GetID();
position.index = ownerBeh->GetLocalParameterPosition(localParam);
position.behaviorId = position.id;
return position;
}
LinkEndpoint GraphBuilder::GetParameterEndpoint(CKParameter *parameter) {
// Check if parameter is local
if (parameter->GetClassID() == CKCID_PARAMETERLOCAL) {
ParameterPosition position = GetLocalParameterPosition((CKParameterLocal *) parameter);
return {position.id, position.index, ENDPOINT_PLOCAL}; // Local parameter endpoint
}
// Get output parameter endpoint
CKBehavior *dummy;
ParameterPosition position = GetOutputParameterPosition((CKParameterOut *) parameter, &dummy);
return {position.id, position.index, ENDPOINT_POUT}; // Output parameter endpoint
}
CKBehavior *GraphBuilder::GetParameterOwner(CKParameter *parameter) {
// Local parameter case
if (parameter->GetClassID() == CKCID_PARAMETERLOCAL) {
return (CKBehavior *) parameter->GetOwner();
}
// Output parameter case
if (parameter->GetClassID() == CKCID_PARAMETEROUT) {
CKObject *owner = parameter->GetOwner();
// Owner is a behavior
if (owner->GetClassID() == CKCID_BEHAVIOR) {
return ((CKBehavior *) owner)->GetParent();
}
// Owner is a parameter operation
if (owner->GetClassID() == CKCID_PARAMETEROPERATION) {
return ((CKParameterOperation *) owner)->GetOwner();
}
m_Context->OutputToConsoleEx((CKSTRING) "Error: Unknown owner type for output parameter");
}
m_Context->OutputToConsoleEx((CKSTRING) "Error: Unknown parameter type");
return nullptr;
}
GraphBuilder::ParameterPosition GraphBuilder::GetShortcutParameterPosition(CK_ID behaviorId, CK_ID sourceId) {
// Get behavior data
BehaviorData &behaviorData = GetBehaviorData(behaviorId);
// Check if shortcut already exists
const int sharedParamCount = static_cast<int>(behaviorData.sharedParams.size());
for (int i = 0; i < sharedParamCount; ++i) {
if (behaviorData.sharedParams[i].sourceId == sourceId) {
return {behaviorId, i, behaviorId};
}
}
// Create a new shortcut parameter
Parameter paramData(sourceId, PARAM_STYLE_COLLAPSED);
paramData.sourceId = sourceId;
behaviorData.AddSharedParameter(paramData);
return {behaviorId, static_cast<int>(behaviorData.sharedParams.size()) - 1, behaviorId};
}
/**
* Configures all parameter links in the behavior tree.
* This method establishes connections between parameters across different levels
* of the behavior hierarchy by:
* 1. Building chains of input parameters upwards through the behavior tree
* 2. Building chains of output parameters upwards through the behavior tree
* 3. Connecting these chains to form complete parameter links
*/
void GraphBuilder::ConfigureParameterLinks() {
// Maps of parameter IDs to their position chains in the behavior tree
ParameterChain inputChain;
ParameterChain outputChain;
// First phase: Build input parameter chains
BuildInputParameterChains(inputChain);
// Second phase: Build output parameter chains
BuildOutputParameterChains(outputChain);
// Third phase: Connect parameter chains
ConnectParameterChains(inputChain, outputChain);
}
/**
* Builds chains of input parameters upwards through the behavior tree.
* Each chain represents how an input parameter propagates up through parent behaviors.
*
* @param inputChain The map of input parameter IDs to their position chains
*/
void GraphBuilder::BuildInputParameterChains(ParameterChain &inputChain) {
for (const auto &id : m_InputParams) {
auto *inputParam = (CKParameterIn *) m_Context->GetObject(id);
if (inputParam) {
try {
BuildInputParameterChain(inputParam, inputChain);
} catch (const std::exception &e) {
m_Context->OutputToConsoleEx((CKSTRING) "Error building input parameter chain for %d: %s",
id, e.what());
}
}
}
}
/**
* Builds a chain for a single input parameter through the behavior tree.
*
* @param inputParam The input parameter
* @param inputChain The map to store the chain in
*/
void GraphBuilder::BuildInputParameterChain(CKParameterIn *inputParam, ParameterChain &inputChain) {
CKBehavior *beh = nullptr;
auto &positionChain = inputChain[inputParam->GetID()];
// Get the initial position of this parameter
positionChain.push_back(GetInputParameterPosition(inputParam, &beh));
// Create the endpoint for the parameter
LinkEndpoint lastEndpoint = {
positionChain.back().id,
positionChain.back().index,
positionChain.back().index == -2 ? ENDPOINT_TARGET_PIN : ENDPOINT_PIN,
};
// Follow the parameter up through parent behaviors and create links
while (beh && beh->GetInputParameterPosition(inputParam) != -1) {
// Add parent behavior's position for this parameter to the chain
positionChain.push_back({
beh->GetID(),
beh->GetInputParameterPosition(inputParam),
beh->GetParent()->GetID()
});
// Create a link connecting this parameter to its parent behavior's input
const LinkEndpoint start = {
positionChain.back().id,
positionChain.back().index,
ENDPOINT_PIN
};
Link link(0, LINK_TYPE_PARAMETER_OP, start, lastEndpoint);
lastEndpoint = start;
GetBehaviorData(beh->GetID()).AddLink(link);
beh = beh->GetParent();
}
}
/**
* Builds chains of output parameters upwards through the behavior tree.
* Each chain represents how an output parameter propagates up through parent behaviors.
*
* @param outputChain The map of output parameter IDs to their position chains
*/
void GraphBuilder::BuildOutputParameterChains(ParameterChain &outputChain) {
for (const auto &id : m_OutputParams) {
auto *outputParam = (CKParameterOut *) m_Context->GetObject(id);
if (outputParam) {
try {
BuildOutputParameterChain(outputParam, outputChain);
} catch (const std::exception &e) {
m_Context->OutputToConsoleEx((CKSTRING) "Error building output parameter chain for %d: %s",
id, e.what());
}
}
}
}
/**
* Builds a chain for a single output parameter through the behavior tree.
*
* @param outputParam The output parameter
* @param outputChain The map to store the chain in
*/
void GraphBuilder::BuildOutputParameterChain(CKParameterOut *outputParam, ParameterChain &outputChain) {
CKBehavior *beh = nullptr;
auto &positionChain = outputChain[outputParam->GetID()];
// Get the initial position of this parameter
positionChain.push_back(GetOutputParameterPosition(outputParam, &beh));
// Create the endpoint for the parameter
LinkEndpoint lastEndpoint = {
positionChain.back().id,
positionChain.back().index,
ENDPOINT_POUT
};
// Follow the parameter up through parent behaviors and create links
while (beh && beh->GetOutputParameterPosition(outputParam) != -1) {
// Add parent behavior's position for this parameter to the chain
positionChain.push_back({
beh->GetID(),
beh->GetOutputParameterPosition(outputParam),
beh->GetParent()->GetID()
});
// Create a link connecting this parameter to its parent behavior's output
const LinkEndpoint end = {
positionChain.back().id,
positionChain.back().index,
ENDPOINT_POUT,
};
Link link(0, LINK_TYPE_PARAMETER_OP, lastEndpoint, end);
lastEndpoint = end;
GetBehaviorData(beh->GetID()).AddLink(link);
beh = beh->GetParent();
}
}
/**
* Connects parameter chains to form complete parameter links.
* This phase links input parameters to their sources, either directly
* or through shared parameter connections.
*
* @param inputChain The map of input parameter IDs to their position chains
* @param outputChain The map of output parameter IDs to their position chains
*/
void GraphBuilder::ConnectParameterChains(const ParameterChain &inputChain, const ParameterChain &outputChain) {
for (const auto &id : m_InputParams) {
auto *inputParam = (CKParameterIn *) m_Context->GetObject(id);
if (!inputParam) continue;
try {
CKBehavior *beh = nullptr;
auto position = GetInputParameterPosition(inputParam, &beh);
// Find this parameter's positions in the chain
auto inputIt = inputChain.find(inputParam->GetID());
if (inputIt == inputChain.end()) continue;
const auto &inputPositions = inputIt->second;
// Handle direct source connections (parameter is connected to an output)
if (CKParameter *sourceParam = inputParam->GetDirectSource()) {
ConnectToDirectSource(inputParam, sourceParam, inputPositions, position, outputChain);
}
// Handle shared source connections (parameter is shared with another input)
else if (CKParameterIn *sharedInput = inputParam->GetSharedSource()) {
ConnectToSharedSource(inputParam, sharedInput, inputPositions, inputChain);
}
} catch (const std::exception &e) {
m_Context->OutputToConsoleEx((CKSTRING) "Error connecting parameter %d: %s", id, e.what());
}
}
}
/**
* Connects an input parameter to its direct source parameter.
* This establishes a link between an output parameter and an input parameter.
*
* @param inputParam The input parameter to connect
* @param sourceParam The source (output) parameter
* @param inputPositions The positions of the input parameter in the chain
* @param position The initial position of the input parameter
* @param outputChain The map of output parameter chains
*/
void GraphBuilder::ConnectToDirectSource(CKParameterIn *inputParam, CKParameter *sourceParam,
const std::vector<ParameterPosition> &inputPositions,
const ParameterPosition &position,
const ParameterChain &outputChain) {
// Find the source parameter's positions in the chain
std::vector<ParameterPosition> sourcePositions;
auto sourceIt = outputChain.find(sourceParam->GetID());
if (sourceIt != outputChain.end()) {
sourcePositions = sourceIt->second;
} else if (sourceParam->GetClassID() == CKCID_PARAMETERLOCAL) {
// Handle local parameters not in output chain
sourcePositions.push_back(GetLocalParameterPosition((CKParameterLocal *) sourceParam));
}
// Try to connect within the same behavior first
bool connected = ConnectParametersInSameBehavior(inputPositions, sourcePositions, sourceParam);
// If direct connection failed, create a shortcut connection
if (!connected) {
CreateParameterShortcut(position, sourceParam);
}
}
/**
* Connects an input parameter to another input parameter it shares with.
* This establishes a link between two input parameters that share the same source.
*
* @param inputParam The input parameter to connect
* @param sharedInput The shared input parameter (another input parameter with the same source)
* @param inputPositions The positions of the input parameter in the chain
* @param inputChain The map of input parameter chains
*/
void GraphBuilder::ConnectToSharedSource(CKParameterIn *inputParam, CKParameterIn *sharedInput,
const std::vector<ParameterPosition> &inputPositions,
const ParameterChain &inputChain) {
if (sharedInput->GetOwner()->GetClassID() != CKCID_BEHAVIOR) {
throw std::runtime_error("Shared input owner is not a behavior");
}
// Find the shared input's positions in the chain
auto sharedIt = inputChain.find(sharedInput->GetID());
if (sharedIt == inputChain.end()) return;
const auto &sharedInputPositions = sharedIt->second;
bool connected = false;
// Try to connect within the same behavior
for (const auto &inputPos : inputPositions) {
for (const auto &sharedPos : sharedInputPositions) {
if (inputPos.behaviorId == sharedPos.id) {
// Create a link from the shared input to this input
LinkEndpoint start = {
sharedPos.id,
sharedPos.index,
ENDPOINT_PIN
};
LinkEndpoint end = {
inputPos.id,
inputPos.index,
inputPos.index == -2 ? ENDPOINT_TARGET_PIN : ENDPOINT_PIN
};
Link link(0, LINK_TYPE_PARAMETER, start, end);
GetBehaviorData(inputPos.behaviorId).AddLink(link);
connected = true;
break;
}
}
if (connected) break;
}
// If no direct connection was possible, try to find a common parent
if (!connected) {
auto *sharedOwner = (CKBehavior *) sharedInput->GetOwner();
auto *inputOwner = (CKBehavior *) inputParam->GetOwner();
if (sharedOwner->GetParent() && inputOwner->GetParent() &&
sharedOwner->GetParent()->GetID() == inputOwner->GetParent()->GetID()) {
// Create shortcuts in the common parent behavior
CK_ID parentId = sharedOwner->GetParent()->GetID();
auto sharedShortcutPos = GetShortcutParameterPosition(parentId, sharedInput->GetID());
auto inputShortcutPos = GetShortcutParameterPosition(parentId, inputParam->GetID());
// Create link between shortcuts
LinkEndpoint start = {
parentId,
sharedShortcutPos.index,
ENDPOINT_POUT_SHORTCUT
};
LinkEndpoint end = {
parentId,
inputShortcutPos.index,
ENDPOINT_PIN
};
Link link(0, LINK_TYPE_PARAMETER, start, end);
GetBehaviorData(parentId).AddLink(link);
connected = true;
}
}
if (!connected) {
m_Context->OutputToConsoleEx((CKSTRING) "Cannot connect shared parameters %d <-> %d, source type is %d",
inputParam->GetID(), sharedInput->GetID(), sharedInput->GetClassID());
}
}
/**
* Tries to connect parameters within the same behavior.
*
* @param inputPositions The positions of the input parameter
* @param sourcePositions The positions of the source parameter
* @param sourceParam The source parameter
* @return True if a connection was made, false otherwise
*/
bool GraphBuilder::ConnectParametersInSameBehavior(
const std::vector<ParameterPosition> &inputPositions,
const std::vector<ParameterPosition> &sourcePositions,
CKParameter *sourceParam) {
for (const auto &inputPos : inputPositions) {
for (const auto &sourcePos : sourcePositions) {
if (inputPos.behaviorId == sourcePos.behaviorId) {
// Create a direct link within the same behavior
LinkEndpoint start = {
sourcePos.id,
sourcePos.index,
sourceParam->GetClassID() == CKCID_PARAMETERLOCAL ? ENDPOINT_PLOCAL : ENDPOINT_POUT
};
LinkEndpoint end = {
inputPos.id,
inputPos.index,
inputPos.index == -2 ? ENDPOINT_TARGET_PIN : ENDPOINT_PIN
};
Link link(0, LINK_TYPE_PARAMETER, start, end);
GetBehaviorData(inputPos.behaviorId).AddLink(link);
return true;
}
}
}
return false;
}
/**
* Creates a parameter shortcut for a connection that can't be made directly.
*
* @param position The position of the input parameter
* @param sourceParam The source parameter
*/
void GraphBuilder::CreateParameterShortcut(const ParameterPosition &position, CKParameter *sourceParam) {
auto shortcutPos = GetShortcutParameterPosition(position.behaviorId, sourceParam->GetID());
LinkEndpoint start = {
position.behaviorId,
shortcutPos.index,
ENDPOINT_POUT_SHORTCUT
};
LinkEndpoint end = {
position.id,
position.index,
position.index == -2 ? ENDPOINT_TARGET_PIN : ENDPOINT_PIN
};
Link link = {0, LINK_TYPE_PARAMETER, start, end};
GetBehaviorData(position.behaviorId).AddLink(link);
}