Repository navigation
Expand file tree
/
Copy pathPawn.cpp
More file actions
489 lines (423 loc) · 17.3 KB
/
Copy pathPawn.cpp
File metadata and controls
489 lines (423 loc) · 17.3 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
#include "Pawn.h"
#include "pawnrow.h"
#include "PathTrackerStruct.h"
#include <QJsonArray>
#include <QJsonObject>
#include <QUuid>
#include <QDebug>
// ═══════════════════════════════════════════════════════════════════════════════
// PawnTemplate
// ═══════════════════════════════════════════════════════════════════════════════
void PawnTemplate::setBaseAttribute(const QString& key, int value)
{
for (AttributeValue& attributeValue : baseAttributes)
{
if (attributeValue.key == key)
{
attributeValue.baseValue = value;
attributeValue.currentValue = value;
return;
}
}
// Not found – insert new entry.
AttributeValue attributeValue;
attributeValue.key = key;
attributeValue.baseValue = value;
attributeValue.currentValue = value;
baseAttributes.append(attributeValue);
}
int PawnTemplate::getBaseAttribute(const QString& key) const
{
for (const AttributeValue& attributeValue : baseAttributes)
if (attributeValue.key == key) return attributeValue.baseValue;
return 0;
}
QJsonObject PawnTemplate::saveToJson() const
{
QJsonObject obj;
obj["id"] = id;
obj["name"] = name;
obj["pictureFilePath"] = PictureFilePath;
QJsonArray attributeArray;
for (const AttributeValue& attributeValue : baseAttributes)
attributeArray.append(attributeValue.saveToJson());
obj["baseAttributes"] = attributeArray;
QJsonArray effectArray;
for (const ChildEffectRef& effectReference : defaultEffects)
effectArray.append(effectReference.saveToJson());
obj["defaultEffects"] = effectArray;
return obj;
}
void PawnTemplate::loadFromJson(const QJsonObject& obj)
{
id = obj["id"].toString();
name = obj["name"].toString();
PictureFilePath = obj["pictureFilePath"].toString();
baseAttributes.clear();
for (const QJsonValue& jsonValue : obj["baseAttributes"].toArray())
{
AttributeValue attributeValue;
attributeValue.loadFromJson(jsonValue.toObject());
baseAttributes.append(attributeValue);
}
defaultEffects.clear();
for (const QJsonValue& jsonValue : obj["defaultEffects"].toArray())
{
ChildEffectRef effectReference;
effectReference.loadFromJson(jsonValue.toObject());
defaultEffects.append(effectReference);
}
}
// ═══════════════════════════════════════════════════════════════════════════════
// PawnState helpers
// ═══════════════════════════════════════════════════════════════════════════════
static QString StateToString(PawnState state)
{
switch (state) {
case PawnState::Waiting: return "Waiting";
case PawnState::Active: return "Active";
case PawnState::Spent: return "Spent";
case PawnState::Unconscious: return "Unconscious";
case PawnState::Dead: return "Dead";
}
return "Waiting";
}
static PawnState StateFromString(const QString& s)
{
if (s == "Active") return PawnState::Active;
if (s == "Spent") return PawnState::Spent;
if (s == "Unconscious") return PawnState::Unconscious;
if (s == "Dead") return PawnState::Dead;
return PawnState::Waiting;
}
// ═══════════════════════════════════════════════════════════════════════════════
// PawnInstance
// ═══════════════════════════════════════════════════════════════════════════════
PawnInstance* PawnInstance::fromTemplate(const PawnTemplate& pawnTemplate)
{
auto* newPawnInstance = new PawnInstance;
newPawnInstance->instanceId = QUuid::createUuid().toString(QUuid::WithoutBraces);
newPawnInstance->templateId = pawnTemplate.id;
newPawnInstance->displayName = pawnTemplate.name;
newPawnInstance->attributes = pawnTemplate.baseAttributes; // deep copy of flat structs
// Default effects are applied by the caller (EncounterInstance) after construction
// using PathTrackerStruct::findEffectTemplate.
return newPawnInstance;
}
// ── Attribute access ──────────────────────────────────────────────────────────
int PawnInstance::getAttribute(const QString& key) const
{
for (const AttributeValue& attributeValue : attributes)
if (attributeValue.key == key) return attributeValue.currentValue;
return 0;
}
void PawnInstance::setAttribute(const QString& key, int value)
{
for (AttributeValue& attributeValue : attributes)
{
if (attributeValue.key == key)
{
attributeValue.currentValue = value;
return;
}
}
// Attribute not in list – add it (runtime-only attribute e.g. temp HP).
AttributeValue attributeValue;
attributeValue.key = key;
attributeValue.baseValue = value;
attributeValue.currentValue = value;
attributes.append(attributeValue);
}
void PawnInstance::resolveAttributes()
{
// Step 1: reset every attribute's currentValue to its baseValue,
// except HP which is a tracked resource managed explicitly via setAttribute.
for (AttributeValue& attributeValue : attributes)
if (attributeValue.key != HP_KEY)
attributeValue.currentValue = attributeValue.baseValue;
// Step 2: apply modifiers from every active effect's current stage.
for (const auto& effectInstance : effects)
{
const Stage* stage = effectInstance->currentStage();
if (!stage) continue;
for (const AttributeModifier& attributeModifier : stage->modifiers)
{
for (AttributeValue& attributeValue : attributes)
{
if (attributeValue.key == attributeModifier.attributeKey)
{
attributeValue.currentValue = attributeModifier.apply(attributeValue.currentValue);
break;
}
}
}
}
}
// ── Effect management ─────────────────────────────────────────────────────────
void PawnInstance::applyEffect(const EffectTemplate& tmpl)
{
auto* inst = EffectInstance::fromTemplate(tmpl, this);
effects.emplace_back(inst);
// Fire the OnApplied event immediately.
inst->fireEvent(EventType::OnApplied);
// Auto-apply all child effects defined in the initial stage.
const Stage* stage = inst->currentStage();
if (stage)
{
for (const ChildEffectRef& child : stage->childEffects)
{
EffectTemplate* childTmpl =
PathTrackerStruct::instance().findEffectTemplate(child.effectTemplateId);
if (childTmpl)
applyEffect(*childTmpl); // recursive – also fires OnApplied and resolves
}
}
resolveAttributes();
}
void PawnInstance::removeEffect(const QString& effectInstanceId)
{
for (int i = 0; i < effects.size(); ++i)
{
if (effects[i]->instanceId == effectInstanceId)
{
// Fire removal event before erasing.
effects[i]->fireEvent(EventType::OnRemoved);
// Also remove any Bound child effects.
const Stage* stage = effects[i]->currentStage();
if (stage)
{
for (const ChildEffectRef& child : stage->childEffects)
{
if (child.binding == ChildBinding::Bound)
{
// Find and remove child effect by templateId match.
for (int j = effects.size() - 1; j >= 0; --j)
{
if (effects[j]->templateId == child.effectTemplateId)
{
effects[j]->fireEvent(EventType::OnRemoved);
effects.erase(effects.begin() + j);
break;
}
}
}
}
}
effects.erase(effects.begin() + i);
resolveAttributes();
return;
}
}
qWarning() << "PawnInstance::removeEffect – instanceId not found:" << effectInstanceId;
}
void PawnInstance::syncBoundChildEffects(EffectInstance* inst, int oldStageIndex)
{
// Collect bound children from the old stage.
QList<QString> toRemove;
if (oldStageIndex >= 0 && oldStageIndex < inst->stages.size())
{
for (const ChildEffectRef& child : inst->stages[oldStageIndex].childEffects)
{
if (child.binding == ChildBinding::Bound)
toRemove.append(child.effectTemplateId);
}
}
// Remove them (they were bound to the old stage, not the new one).
for (const QString& tid : toRemove)
{
for (int i = static_cast<int>(effects.size()) - 1; i >= 0; --i)
{
if (effects[i]->templateId == tid)
{
effects[i]->fireEvent(EventType::OnRemoved);
effects.erase(effects.begin() + i);
break;
}
}
}
// Apply all children declared in the new stage.
const Stage* newStage = inst->currentStage();
if (newStage)
{
for (const ChildEffectRef& child : newStage->childEffects)
{
EffectTemplate* childTmpl =
PathTrackerStruct::instance().findEffectTemplate(child.effectTemplateId);
if (childTmpl)
applyEffect(*childTmpl);
}
}
resolveAttributes();
}
EffectInstance* PawnInstance::findEffect(const QString& effectInstanceId)
{
for (const auto& effectInstance : effects)
if (effectInstance->instanceId == effectInstanceId) return effectInstance.get();
return nullptr;
}
// ── Event forwarding ─────────────────────────────────────────────────────────
void PawnInstance::fireEvent(EventType event)
{
// Iterate a copy of the list – effects may be removed during iteration
// (e.g. MaxDurationAction calls removeEffect).
QList<EffectInstance*> snapshot;
for (const auto& effectInstance : effects)
snapshot.append(effectInstance.get());
for (EffectInstance* effectInstance : snapshot)
{
// Guard: effect may have been removed by an earlier action this round.
if (findEffect(effectInstance->instanceId))
effectInstance->fireEvent(event);
}
}
// ── Serialization ─────────────────────────────────────────────────────────────
QJsonObject PawnInstance::saveToJson() const
{
QJsonObject obj;
obj["instanceId"] = instanceId;
obj["templateId"] = templateId;
obj["displayName"] = displayName;
QJsonArray attributeArray;
for (const AttributeValue& attributeValue : attributes)
attributeArray.append(attributeValue.saveToJson());
obj["attributes"] = attributeArray;
QJsonArray effectArray;
for (const auto& effectInstance : effects)
effectArray.append(effectInstance->saveToJson());
obj["effects"] = effectArray;
obj["state"] = StateToString(state);
obj["savedInitiative"] = savedInitiative;
if (!note.isEmpty())
obj["note"] = note;
return obj;
}
void PawnInstance::loadFromJson(const QJsonObject& obj)
{
instanceId = obj["instanceId"].toString();
templateId = obj["templateId"].toString();
displayName = obj["displayName"].toString();
state = StateFromString(obj["state"].toString("Waiting"));
savedInitiative = obj["savedInitiative"].toInt(0);
note = obj["note"].toString();
attributes.clear();
for (const QJsonValue& jsonValue : obj["attributes"].toArray())
{
AttributeValue attributeValue;
attributeValue.loadFromJson(jsonValue.toObject());
attributes.append(attributeValue);
}
effects.clear();
for (const QJsonValue& jsonValue : obj["effects"].toArray())
{
auto* newEffectInstance = new EffectInstance;
newEffectInstance->loadFromJson(jsonValue.toObject());
newEffectInstance->owner = this; // re-link owner pointer
effects.emplace_back(newEffectInstance);
}
}
// ═══════════════════════════════════════════════════════════════════════════════
// Player
// ═══════════════════════════════════════════════════════════════════════════════
Player* Player::create(const QString& name)
{
auto* newPlayer = new Player;
newPlayer->instanceId = QUuid::createUuid().toString(QUuid::WithoutBraces);
newPlayer->templateId = {}; // Players have no template
newPlayer->displayName = name;
newPlayer->playerName = name;
return newPlayer;
}
QJsonObject Player::saveToJson() const
{
QJsonObject obj = PawnInstance::saveToJson();
obj["playerName"] = playerName;
obj["isPlayer"] = true;
obj["pictureFilePath"] = PictureFilePath;
return obj;
}
void Player::loadFromJson(const QJsonObject& obj)
{
PawnInstance::loadFromJson(obj);
playerName = obj["playerName"].toString(displayName);
PictureFilePath = obj["pictureFilePath"].toString();
}
// ═══════════════════════════════════════════════════════════════════════════════
// PlayerRegistry
// ═══════════════════════════════════════════════════════════════════════════════
PlayerRegistry& PlayerRegistry::instance()
{
static PlayerRegistry s_instance;
return s_instance;
}
void PlayerRegistry::addPlayer(Player* player)
{
Q_ASSERT(player);
// Enforce uniqueness by instanceId.
for (const auto& existingPlayer : m_players)
{
if (existingPlayer->instanceId == player->instanceId)
{
qWarning() << "PlayerRegistry::addPlayer – duplicate instanceId, ignored.";
delete player;
return;
}
}
m_players.emplace_back(player);
}
void PlayerRegistry::removePlayer(const QString& instanceId)
{
for (int i = 0; i < m_players.size(); ++i)
{
if (m_players[i]->instanceId == instanceId)
{
m_players.erase(m_players.begin() + i);
return;
}
}
}
Player* PlayerRegistry::findPlayer(const QString& instanceId)
{
for (const auto& player : m_players)
if (player->instanceId == instanceId) return player.get();
return nullptr;
}
QList<Player*> PlayerRegistry::allPlayers()
{
QList<Player*> result;
result.reserve(m_players.size());
for (const auto& player : m_players)
result.append(player.get());
return result;
}
QJsonArray PlayerRegistry::saveToJson() const
{
QJsonArray arr;
for (const auto& player : m_players)
arr.append(player->saveToJson());
return arr;
}
void PlayerRegistry::loadFromJson(const QJsonArray& arr)
{
m_players.clear();
for (const QJsonValue& jsonValue : arr)
{
auto* player = new Player;
player->loadFromJson(jsonValue.toObject());
m_players.emplace_back(player);
}
}
// ═══════════════════════════════════════════════════════════════════════════════
// List widget factories
// ═══════════════════════════════════════════════════════════════════════════════
QWidget* PawnInstance::createListWidget(int initiative)
{
PawnRow* row = new PawnRow();
row->SetData(this, initiative);
return row;
}
QWidget* Player::createListWidget(int initiative)
{
PawnRow* row = new PawnRow();
row->SetData(this, initiative);
return row;
}