Repository navigation
Expand file tree
/
Copy pathEffect.cpp
More file actions
377 lines (321 loc) · 12.6 KB
/
Copy pathEffect.cpp
File metadata and controls
377 lines (321 loc) · 12.6 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
#include "Effect.h"
#include "Pawn.h"
#include "AttributeRegistry.h"
#include "PathTrackerStruct.h"
#include <QJsonArray>
#include <QJsonObject>
#include <QPair>
#include <QUuid>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QDebug>
// ─────────────────────────────────────────────
// Time helper
// Pathfinder: 1 round = 6 sec, 1 min = 10 rounds, 1 hr = 60 min = 600 rounds
// ─────────────────────────────────────────────
static QString formatDuration(int rounds)
{
if (rounds < 10)
return QString("%1 rounds (%2 sec)").arg(rounds).arg(rounds * 6);
if (rounds < 600) {
double min = rounds / 10.0;
return QString("%1 rounds (%2 min)").arg(rounds)
.arg(min, 0, 'f', rounds % 10 == 0 ? 0 : 1);
}
double hr = rounds / 600.0;
return QString("%1 rounds (%2 hr)").arg(rounds)
.arg(hr, 0, 'f', rounds % 600 == 0 ? 0 : 2);
}
// ─────────────────────────────────────────────
// ChildEffectRef
// ─────────────────────────────────────────────
QJsonObject ChildEffectRef::saveToJson() const
{
QJsonObject obj;
obj["effectTemplateId"] = effectTemplateId;
obj["binding"] = (binding == ChildBinding::Bound) ? "Bound" : "Unbound";
obj["level"] = level;
return obj;
}
void ChildEffectRef::loadFromJson(const QJsonObject& obj)
{
effectTemplateId = obj["effectTemplateId"].toString();
binding = (obj["binding"].toString() == "Bound")
? ChildBinding::Bound : ChildBinding::Unbound;
level = obj["level"].toInt(1);
}
// ─────────────────────────────────────────────
// Stage – helper to (de)serialise the Action* list
// using the type-discriminator pattern.
// ─────────────────────────────────────────────
QJsonObject Stage::saveToJson() const
{
QJsonObject obj;
QJsonArray mods;
for (const AttributeModifier& modifier : modifiers)
mods.append(modifier.saveToJson());
obj["modifiers"] = mods;
QJsonArray children;
for (const ChildEffectRef& childEffect : childEffects)
children.append(childEffect.saveToJson());
obj["childEffects"] = children;
QJsonArray acts;
for (const auto& action : actions)
acts.append(action->saveToJson());
obj["actions"] = acts;
return obj;
}
void Stage::loadFromJson(const QJsonObject& obj)
{
modifiers.clear();
for (const QJsonValue& jsonValue : obj["modifiers"].toArray())
{
AttributeModifier modifier;
modifier.loadFromJson(jsonValue.toObject());
modifiers.append(modifier);
}
childEffects.clear();
for (const QJsonValue& jsonValue : obj["childEffects"].toArray())
{
ChildEffectRef childEffect;
childEffect.loadFromJson(jsonValue.toObject());
childEffects.append(childEffect);
}
actions.clear();
for (const QJsonValue& jsonValue : obj["actions"].toArray())
{
const QJsonObject actionObj = jsonValue.toObject();
auto newAction = ActionFactory::create(actionObj["type"].toString());
if (newAction)
{
newAction->loadFromJson(actionObj);
actions.append(std::move(newAction));
}
else
{
qWarning() << "Stage::loadFromJson: unknown action type:" << actionObj["type"].toString();
}
}
}
// ─────────────────────────────────────────────
// Action factory (delegates to ActionFactory)
// ─────────────────────────────────────────────
#include "Action.h"
// ─────────────────────────────────────────────
// EffectTemplate
// ─────────────────────────────────────────────
Stage& EffectTemplate::addStage()
{
stages.append(Stage{});
return stages.last();
}
void EffectTemplate::removeStage(int index)
{
if (index >= 0 && index < stages.size())
stages.removeAt(index);
}
QJsonObject EffectTemplate::saveToJson() const
{
QJsonObject obj;
obj["id"] = id;
obj["name"] = name;
obj["description"] = description;
obj["onset"] = onset;
obj["pictureFilePath"] = PictureFilePath;
QJsonArray stageArray;
for (const Stage& stage : stages)
stageArray.append(stage.saveToJson());
obj["stages"] = stageArray;
return obj;
}
void EffectTemplate::loadFromJson(const QJsonObject& obj)
{
id = obj["id"].toString();
name = obj["name"].toString();
description = obj["description"].toString();
onset = obj["onset"].toString();
PictureFilePath = obj["pictureFilePath"].toString();
stages.clear();
for (const QJsonValue& jsonValue : obj["stages"].toArray())
{
Stage stage;
stage.loadFromJson(jsonValue.toObject());
stages.append(stage);
}
}
// ─────────────────────────────────────────────
// EffectInstance
// ─────────────────────────────────────────────
EffectInstance* EffectInstance::fromTemplate(const EffectTemplate& effectTemplate, PawnInstance* owner)
{
auto* newEffectInstance = new EffectInstance;
newEffectInstance->templateId = effectTemplate.id;
newEffectInstance->instanceId = QUuid::createUuid().toString(QUuid::WithoutBraces);
newEffectInstance->owner = owner;
newEffectInstance->level = 0;
newEffectInstance->currentStageIndex = 0;
newEffectInstance->currentRound = 0;
// Deep-copy stages, cloning every Action so runtime state is independent.
for (const Stage& srcStage : effectTemplate.stages)
{
Stage dstStage;
dstStage.modifiers = srcStage.modifiers;
dstStage.childEffects = srcStage.childEffects;
for (const auto& srcAction : srcStage.actions)
{
dstStage.actions.append(
std::shared_ptr<Action>(srcAction->clone()));
}
newEffectInstance->stages.append(dstStage);
}
return newEffectInstance;
}
Stage* EffectInstance::currentStage()
{
if (stages.isEmpty()) return nullptr;
const int stageIndex = qBound(0, currentStageIndex, stages.size() - 1);
return &stages[stageIndex];
}
const Stage* EffectInstance::currentStage() const
{
if (stages.isEmpty()) return nullptr;
const int stageIndex = qBound(0, currentStageIndex, stages.size() - 1);
return &stages[stageIndex];
}
bool EffectInstance::advanceStage()
{
if (currentStageIndex >= stages.size() - 1) return false;
const int oldIndex = currentStageIndex;
++currentStageIndex;
if (owner)
owner->syncBoundChildEffects(this, oldIndex);
return true;
}
bool EffectInstance::regressStage()
{
if (currentStageIndex <= 0) return false;
const int oldIndex = currentStageIndex;
--currentStageIndex;
if (owner)
owner->syncBoundChildEffects(this, oldIndex);
return true;
}
void EffectInstance::fireEvent(EventType event)
{
if (event == EventType::OnRoundEnd)
currentRound++;
Stage* stage = currentStage();
if (!stage) return;
// Snapshot actions via shared_ptr to keep them alive, and capture instanceId
// locally — MaxDurationAction may call pawn->removeEffect() which destroys
// this EffectInstance (and its stages) while we are still iterating.
const QString myId = instanceId;
const QList<std::shared_ptr<Action>> actionSnapshot = stage->actions;
for (const auto& action : actionSnapshot)
{
// If this effect was removed by an earlier action in the same event, stop.
if (!owner || !owner->findEffect(myId)) return;
action->executeEvent(event, owner);
}
}
QList<QPair<QString,QString>> EffectInstance::getDisplayProperties(const EffectTemplate* tmpl) const
{
QList<QPair<QString,QString>> displayProperties;
if (stages.isEmpty())
displayProperties.append(qMakePair(QString("Stage"), QString("None")));
else
displayProperties.append({"Stage", QString("%1 / %2")
.arg(currentStageIndex + 1)
.arg(stages.size())});
if (level != 0)
displayProperties.append({"Level", QString::number(level)});
if (tmpl && !tmpl->onset.isEmpty())
displayProperties.append({"Onset", tmpl->onset});
displayProperties.append({"Active", formatDuration(currentRound)});
const Stage* stage = currentStage();
if (stage) {
for (const auto& action : stage->actions)
displayProperties.append(action->getDisplayProperties());
for (const AttributeModifier& mod : stage->modifiers) {
const AttributeRegistry& reg = PathTrackerStruct::attributes();
QString attrName = reg.hasAttribute(mod.attributeKey)
? reg.getAttribute(mod.attributeKey).displayName
: mod.attributeKey;
QString sign;
switch (mod.op) {
case ModifierOp::Plus: sign = QString("+%1").arg(mod.value); break;
case ModifierOp::Minus: sign = QString("-%1").arg(mod.value); break;
case ModifierOp::Multiply: sign = QString("×%1").arg(mod.value); break;
}
QString label = mod.description.isEmpty()
? attrName
: QString("%1 (%2)").arg(attrName, mod.description);
displayProperties.append({label, sign});
}
}
return displayProperties;
}
QWidget* EffectInstance::createRuntimeWidget(QWidget* parent)
{
// Returns a simple property->value widget row for display in a table.
auto* container = new QWidget(parent);
auto* layout = new QVBoxLayout(container);
layout->setContentsMargins(4, 2, 4, 2);
auto addRow = [&](const QString& prop, const QString& val){
auto* row = new QHBoxLayout;
auto* lbl = new QLabel(QString("<b>%1</b>").arg(prop), container);
lbl->setMinimumWidth(130);
auto* valLbl = new QLabel(val, container);
row->addWidget(lbl);
row->addWidget(valLbl);
layout->addLayout(row);
};
addRow("Effect ID", templateId);
addRow("Instance ID", instanceId);
addRow("Level", QString::number(level));
addRow("Stage", QString::number(currentStageIndex));
addRow("Round", QString::number(currentRound));
// Also add runtime widgets from current stage's actions.
if (Stage* stage = currentStage())
{
for (auto& action : stage->actions)
{
QWidget* actionWidget = action->createRuntimeWidget(container);
if (actionWidget)
layout->addWidget(actionWidget);
}
}
return container;
}
QJsonObject EffectInstance::saveToJson() const
{
QJsonObject obj;
obj["templateId"] = templateId;
obj["instanceId"] = instanceId;
obj["level"] = level;
obj["currentStageIndex"] = currentStageIndex;
obj["currentRound"] = currentRound;
// We save runtime stage state (action internals like roundsRemaining).
QJsonArray stageArray;
for (const Stage& stage : stages)
stageArray.append(stage.saveToJson());
obj["stages"] = stageArray;
return obj;
}
void EffectInstance::loadFromJson(const QJsonObject& obj)
{
templateId = obj["templateId"].toString();
instanceId = obj["instanceId"].toString();
level = obj["level"].toInt(0);
currentStageIndex = obj["currentStageIndex"].toInt(0);
currentRound = obj["currentRound"].toInt(0);
owner = nullptr; // re-linked by PawnInstance::loadFromJson
stages.clear();
for (const QJsonValue& jsonValue : obj["stages"].toArray())
{
Stage stage;
stage.loadFromJson(jsonValue.toObject());
stages.append(stage);
}
}