Repository navigation
Expand file tree
/
Copy pathTacticalAnalyzer.cpp
More file actions
1498 lines (1347 loc) · 59.8 KB
/
Copy pathTacticalAnalyzer.cpp
File metadata and controls
1498 lines (1347 loc) · 59.8 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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "TacticalAnalyzer.h"
#include "ExecutionFusion.h"
#include "GameData.h"
#include "ThreatRanker.h"
#include <QDateTime>
#include <QDebug>
#include <QMutexLocker>
#include <QRegularExpression>
#include <QtGlobal>
namespace RM {
namespace {
constexpr int kTacticalDefaultRobotMaxHp = 150;
QString tacticalRobotPrefix(bool isAlly) {
return isAlly ? QStringLiteral("A") : QStringLiteral("E");
}
QString tacticalRobotIdLabel(int robotId, bool isAlly) {
return QStringLiteral("%1%2")
.arg(tacticalRobotPrefix(isAlly))
.arg(robotId % 100);
}
QString tacticalRobotTypeIcon(RobotType type) {
switch (type) {
case RobotType::HERO:
return QStringLiteral("hero");
case RobotType::ENGINEER:
return QStringLiteral("engineer");
case RobotType::AERIAL:
return QStringLiteral("aerial");
case RobotType::SENTRY:
return QStringLiteral("sentry");
case RobotType::INFANTRY_3:
case RobotType::INFANTRY_4:
case RobotType::INFANTRY_5:
default:
return QStringLiteral("infantry");
}
}
RobotType tacticalDefaultTypeForSlot(int slot) {
switch (slot) {
case 1:
return RobotType::HERO;
case 2:
return RobotType::ENGINEER;
case 6:
return RobotType::AERIAL;
case 7:
return RobotType::SENTRY;
case 3:
case 4:
case 5:
default:
return RobotType::INFANTRY_3;
}
}
QString tacticalRobotTypeLabel(RobotType type, int slot) {
switch (type) {
case RobotType::HERO:
return QStringLiteral("英雄");
case RobotType::ENGINEER:
return QStringLiteral("工程");
case RobotType::AERIAL:
return QStringLiteral("空中");
case RobotType::SENTRY:
return QStringLiteral("哨兵");
case RobotType::INFANTRY_3:
case RobotType::INFANTRY_4:
case RobotType::INFANTRY_5:
default:
return QStringLiteral("步兵%1").arg(slot);
}
}
//默认最大血量
int tacticalDefaultMaxHpForSlot(int slot) {
Q_UNUSED(slot)
return kTacticalDefaultRobotMaxHp;
}
qint64 robotLastUpdateMs(const RobotData &robot) {
return robot.lastUpdateTime.isValid()
? robot.lastUpdateTime.toMSecsSinceEpoch()
: qint64(0);
}
bool robotHasUsablePosition(const RobotData &robot) {
return robot.posX != 0.0f || robot.posY != 0.0f;
}
int pct(int value, int maxValue) {
if (maxValue <= 0) {
return 0;
}
return qBound(0, qRound(value * 100.0 / maxValue), 100);
}
QString tacticalSignedNumber(int value) {
return QStringLiteral("%1%2")
.arg(value >= 0 ? QStringLiteral("+") : QString())
.arg(value);
}
QString tacticalLeadText(int diff) {
if (diff > 0) {
return QStringLiteral("我方领先 %1").arg(diff);
}
if (diff < 0) {
return QStringLiteral("敌方领先 %1").arg(qAbs(diff));
}
return QStringLiteral("双方持平");
}
QVariantMap tacticalMetric(const QString &key,
const QString &title,
const QVariant &value,
const QString &status,
const QString &source,
int confidence,
const QString &subText = QString()) {
QVariantMap metric;
metric["key"] = key;
metric["title"] = title;
metric["value"] = value;
metric["status"] = status;
metric["source"] = source;
metric["confidence"] = qBound(0, confidence, 100);
metric["compareText"] = subText;
metric["subText"] = subText;
return metric;
}
} // namespace
// ═══════════════════════════════════════════════════════════
// 构造 / 配置
// ═══════════════════════════════════════════════════════════
TacticalAnalyzer::TacticalAnalyzer(GameData *gameData, QObject *parent)
: QObject(parent), m_gameData(gameData) {
QObject::connect(&m_timer, &QTimer::timeout, this, &TacticalAnalyzer::analyze);
}
void TacticalAnalyzer::setConfig(const TacticalConfig &cfg) {
QMutexLocker lock(&m_mutex);
m_config = cfg;
if (m_ranker) {
m_ranker->setConfig(cfg);
}
if (m_freshness) {
m_freshness->setThresholds(cfg.freshnessFreshMs,
cfg.freshnessDegradedMs,
cfg.freshnessStaleMs);
}
if (m_fusion) {
m_fusion->setConfig(cfg);
}
}
void TacticalAnalyzer::start(int intervalMs) {
const int boundedIntervalMs = qBound(50, intervalMs, 1000);
if (m_timer.isActive()) {
m_timer.setInterval(boundedIntervalMs);
return;
}
if (m_useMockData) {
loadMockData();
emit snapshotUpdated();
} else {
analyze();
}
m_timer.start(boundedIntervalMs);
}
void TacticalAnalyzer::stop() {
m_timer.stop();
}
bool TacticalAnalyzer::isRunning() const {
return m_timer.isActive();
}
void TacticalAnalyzer::setUseMockData(bool use) {
{
QMutexLocker lock(&m_mutex);
if (m_useMockData == use) {
return;
}
m_useMockData = use;
}
emit useMockDataChanged();
analyze();
}
QString TacticalAnalyzer::layoutMode() const {
QMutexLocker lock(&m_mutex);
return m_snapshot.layoutMode;
}
void TacticalAnalyzer::setLayoutMode(const QString &mode) {
const QString normalized =
(mode == QStringLiteral("video_primary")) ? QStringLiteral("video_primary")
: QStringLiteral("map_primary");
{
QMutexLocker lock(&m_mutex);
if (m_snapshot.layoutMode == normalized) {
return;
}
m_snapshot.layoutMode = normalized;
}
emit layoutModeChanged();
emit snapshotUpdated();
}
// ═══════════════════════════════════════════════════════════
// 主分析入口
// ═══════════════════════════════════════════════════════════
void TacticalAnalyzer::analyze() {
if (m_useMockData) {
analyzeMock();
} else {
analyzeReal();
}
}
// ═══════════════════════════════════════════════════════════
// 模拟数据,与 TacticalMock.qml 的展示字段保持一致
// ═══════════════════════════════════════════════════════════
void TacticalAnalyzer::loadMockData() {
QMutexLocker lock(&m_mutex);
// 头部
m_snapshot.header.matchMode = "正赛";
m_snapshot.header.redScore = 3;
m_snapshot.header.blueScore = 1;
m_snapshot.header.currentRound = 4;
m_snapshot.header.totalRounds = 5;
m_snapshot.header.timeRemaining = "3:42";
m_snapshot.header.stage = "BATTLE";
m_snapshot.header.linkStatus = "链路 OK";
m_snapshot.header.linkLatency = 23;
// 资源
m_snapshot.resources.allyBaseHp = 3200;
m_snapshot.resources.allyBaseMax = 5000;
m_snapshot.resources.enemyBaseHp = 4100;
m_snapshot.resources.enemyBaseMax = 5000;
m_snapshot.resources.allyOutpostHp = 420;
m_snapshot.resources.allyOutpostMax = 1500;
m_snapshot.resources.enemyOutpostHp = 780;
m_snapshot.resources.enemyOutpostMax = 1500;
m_snapshot.resources.allyOutpostDestroyed = false;
m_snapshot.resources.enemyOutpostDestroyed = false;
m_snapshot.resources.economyDiff = -350;
m_snapshot.resources.damageDiff = 1420;
m_snapshot.resources.hpDiff = 800;
m_snapshot.topStatus.allyRemainingEconomy = 5000;
m_snapshot.topStatus.allyTotalEconomyObtained = 5000;
m_snapshot.topStatus.allyTechLevel = 0;
m_snapshot.topStatus.allyEncryptionLevel = 0;
m_snapshot.topStatus.allyFortressOccupationSec = 0;
m_snapshot.topStatus.enemyFortressOccupationSec = 0;
m_snapshot.topStatus.respawnGoldCost = 340;
m_snapshot.topStatus.affordableRespawnCount = 14;
m_snapshot.topStatus.respawnEconomyVisible = true;
// 关键事件
QVariantList events;
{
KeyEventItem ev;
ev.time = "3:45"; ev.icon = "!";
ev.text = "敌方前哨站被摧毁"; ev.color = "#17dd56"; ev.priority = "P1";
events.append(toVariantMap(ev));
}
{
KeyEventItem ev;
ev.time = "3:45"; ev.icon = "★";
ev.text = "大能量机关被激活"; ev.color = "#FFAA00"; ev.priority = "P0";
events.append(toVariantMap(ev));
}
{
KeyEventItem ev;
ev.time = "3:15"; ev.icon = "⚠";
ev.text = "敌方英雄狙击伤害 450"; ev.color = "#d52424"; ev.priority = "P2";
events.append(toVariantMap(ev));
}
m_snapshot.keyEvents = events;
// 图传覆盖
m_snapshot.video.targetId = "E3";
m_snapshot.video.targetLabel = "敌 3号步兵";
m_snapshot.video.targetHp = 84;
m_snapshot.video.targetMaxHp = 400;
m_snapshot.video.distance = 15.2;
m_snapshot.video.lockQuality = 0.92;
m_snapshot.video.hasHologram = true;
m_snapshot.video.compensationText = "瞄准: ↓2.3";
// 主决策
m_snapshot.decision.title = "集火击杀敌方3号步兵";
m_snapshot.decision.priority = "P1";
m_snapshot.decision.confidence = 87;
m_snapshot.decision.windowText = "窗口 ~8秒";
m_snapshot.decision.reasons = QStringList{
"残血 84/400", "雷达易伤激活", "A3可支援", "基地安全"
};
m_snapshot.decision.fallbackActions = QStringList{
"转火敌方哨兵", "控图保热"
};
// 目标排序
QVariantList targets;
{
TargetRankItem t;
t.rank = 1; t.id = "E3"; t.label = "敌步兵3";
t.hp = 84; t.maxHp = 400; t.threat = 0.92; t.icon = "infantry";
targets.append(toVariantMap(t));
}
{
TargetRankItem t;
t.rank = 2; t.id = "E2"; t.label = "敌工程2";
t.hp = 310; t.maxHp = 600; t.threat = 0.65; t.icon = "engineer";
targets.append(toVariantMap(t));
}
{
TargetRankItem t;
t.rank = 3; t.id = "E4"; t.label = "敌步兵4";
t.hp = 220; t.maxHp = 400; t.threat = 0.58; t.icon = "infantry";
targets.append(toVariantMap(t));
}
{
TargetRankItem t;
t.rank = 4; t.id = "E1"; t.label = "敌英雄1";
t.hp = 680; t.maxHp = 800; t.threat = 0.45; t.icon = "hero";
targets.append(toVariantMap(t));
}
m_snapshot.topTargets = targets;
// 雷达地图
m_snapshot.radar.radarAgeMs = 118;
m_snapshot.radar.focusTargetId = "E3";
m_snapshot.radar.mapImageSource = "qrc:/images/minimap_bg.png";
{
QVariantList ally;
ally.append(toVariantMap(MapRobot{"A1", "A1", 0.25, 0.55, 45}));
ally.append(toVariantMap(MapRobot{"A2", "A2", 0.32, 0.62, 90}));
ally.append(toVariantMap(MapRobot{"A3", "A3", 0.28, 0.48, 30}));
ally.append(toVariantMap(MapRobot{"A4", "A4", 0.22, 0.42, 0}));
ally.append(toVariantMap(MapRobot{"A6", "A6", 0.38, 0.55, 135}));
ally.append(toVariantMap(MapRobot{"A7", "A7", 0.42, 0.70, 180}));
m_snapshot.radar.allyRobots = ally;
}
{
QVariantList enemy;
enemy.append(toVariantMap(MapRobot{"E1", "E1", 0.60, 0.55, 225}));
enemy.append(toVariantMap(MapRobot{"E2", "E2", 0.68, 0.42, 270}));
enemy.append(toVariantMap(MapRobot{"E3", "E3", 0.55, 0.38, 200}));
enemy.append(toVariantMap(MapRobot{"E4", "E4", 0.72, 0.60, 240}));
m_snapshot.radar.enemyRobots = enemy;
}
{
QVariantList routes;
QVariantMap rt;
rt["fromX"] = 0.28; rt["fromY"] = 0.48;
rt["toX"] = 0.55; rt["toY"] = 0.38;
routes.append(rt);
m_snapshot.radar.routes = routes;
}
{
QVariantList dzs;
QVariantMap dz;
dz["centerX"] = 0.65; dz["centerY"] = 0.50; dz["radius"] = 0.08;
dzs.append(dz);
m_snapshot.radar.dangerZones = dzs;
}
{
QVariantList buffs;
QVariantMap bz;
bz["x"] = 0.45; bz["y"] = 0.35;
buffs.append(bz);
m_snapshot.radar.buffZones = buffs;
}
// 我方执行能力
QVariantList execs;
{
AllyExecutionCard c;
c.id = "A1"; c.label = "A1 H"; c.capPct = 85; c.heatPct = 30;
c.ammoPct = 70; c.lockedTarget = "E3"; c.canFire = true;
execs.append(toVariantMap(c));
}
{
AllyExecutionCard c;
c.id = "A2"; c.label = "A2 E"; c.capPct = 60; c.heatPct = 70;
c.ammoPct = 40; c.lockedTarget = "-"; c.canFire = false;
execs.append(toVariantMap(c));
}
{
AllyExecutionCard c;
c.id = "A3"; c.label = "A3 I3"; c.capPct = 90; c.heatPct = 20;
c.ammoPct = 55; c.lockedTarget = "E3"; c.canFire = true;
execs.append(toVariantMap(c));
}
{
AllyExecutionCard c;
c.id = "A7"; c.label = "A7 S"; c.capPct = 100; c.heatPct = 0;
c.ammoPct = 80; c.lockedTarget = "-"; c.canFire = false;
execs.append(toVariantMap(c));
}
m_snapshot.allyExecution = execs;
// 2026 阵容没有 5 号步兵,列表保留 1、2、3、4、6、7 号位。
m_snapshot.allyRobotList = QVariantList{
QVariantMap{{"id", "A1"}, {"slot", 1}, {"team", "ally"}, {"type", "hero"}, {"label", "英雄"}, {"hp", 2580}, {"maxHp", 4000}, {"hpPct", 65}, {"heatPct", 30}, {"capPct", 85}, {"ammoPct", 70}, {"online", true}, {"alive", true}, {"stale", false}, {"ageMs", 120}},
QVariantMap{{"id", "A2"}, {"slot", 2}, {"team", "ally"}, {"type", "engineer"}, {"label", "工程"}, {"hp", 420}, {"maxHp", 500}, {"hpPct", 84}, {"heatPct", -1}, {"capPct", 60}, {"ammoPct", 40}, {"online", true}, {"alive", true}, {"stale", false}, {"ageMs", 160}},
QVariantMap{{"id", "A3"}, {"slot", 3}, {"team", "ally"}, {"type", "infantry"}, {"label", "步兵3"}, {"hp", 320}, {"maxHp", 400}, {"hpPct", 80}, {"heatPct", 20}, {"capPct", 90}, {"ammoPct", 55}, {"online", true}, {"alive", true}, {"stale", false}, {"ageMs", 140}},
QVariantMap{{"id", "A4"}, {"slot", 4}, {"team", "ally"}, {"type", "infantry"}, {"label", "步兵4"}, {"hp", 0}, {"maxHp", 400}, {"hpPct", 0}, {"heatPct", -1}, {"capPct", -1}, {"ammoPct", -1}, {"online", false}, {"alive", false}, {"stale", true}, {"ageMs", 999999}},
QVariantMap{{"id", "A6"}, {"slot", 6}, {"team", "ally"}, {"type", "aerial"}, {"label", "空中"}, {"hp", 210}, {"maxHp", 300}, {"hpPct", 70}, {"heatPct", -1}, {"capPct", -1}, {"ammoPct", 80}, {"online", true}, {"alive", true}, {"stale", false}, {"ageMs", 210}},
QVariantMap{{"id", "A7"}, {"slot", 7}, {"team", "ally"}, {"type", "sentry"}, {"label", "哨兵"}, {"hp", 560}, {"maxHp", 600}, {"hpPct", 93}, {"heatPct", 0}, {"capPct", 100}, {"ammoPct", 80}, {"online", true}, {"alive", true}, {"stale", false}, {"ageMs", 170}}
};
m_snapshot.enemyRobotList = QVariantList{
QVariantMap{{"id", "E1"}, {"slot", 1}, {"team", "enemy"}, {"type", "hero"}, {"label", "敌英雄"}, {"hp", 680}, {"maxHp", 800}, {"hpPct", 85}, {"heatPct", -1}, {"capPct", -1}, {"ammoPct", -1}, {"online", true}, {"alive", true}, {"stale", false}, {"ageMs", 180}},
QVariantMap{{"id", "E2"}, {"slot", 2}, {"team", "enemy"}, {"type", "engineer"}, {"label", "敌工程"}, {"hp", 310}, {"maxHp", 600}, {"hpPct", 52}, {"heatPct", -1}, {"capPct", -1}, {"ammoPct", -1}, {"online", true}, {"alive", true}, {"stale", false}, {"ageMs", 180}},
QVariantMap{{"id", "E3"}, {"slot", 3}, {"team", "enemy"}, {"type", "infantry"}, {"label", "敌步兵3"}, {"hp", 84}, {"maxHp", 400}, {"hpPct", 21}, {"heatPct", -1}, {"capPct", -1}, {"ammoPct", -1}, {"online", true}, {"alive", true}, {"stale", false}, {"ageMs", 120}},
QVariantMap{{"id", "E4"}, {"slot", 4}, {"team", "enemy"}, {"type", "infantry"}, {"label", "敌步兵4"}, {"hp", 220}, {"maxHp", 400}, {"hpPct", 55}, {"heatPct", -1}, {"capPct", -1}, {"ammoPct", -1}, {"online", true}, {"alive", true}, {"stale", false}, {"ageMs", 250}},
QVariantMap{{"id", "E6"}, {"slot", 6}, {"team", "enemy"}, {"type", "aerial"}, {"label", "敌空中"}, {"hp", 0}, {"maxHp", 300}, {"hpPct", 0}, {"heatPct", -1}, {"capPct", -1}, {"ammoPct", -1}, {"online", false}, {"alive", false}, {"stale", true}, {"ageMs", 999999}},
QVariantMap{{"id", "E7"}, {"slot", 7}, {"team", "enemy"}, {"type", "sentry"}, {"label", "敌哨兵"}, {"hp", 520}, {"maxHp", 600}, {"hpPct", 87}, {"heatPct", -1}, {"capPct", -1}, {"ammoPct", -1}, {"online", true}, {"alive", true}, {"stale", false}, {"ageMs", 240}}
};
const int allyEconomy = 1280;
const int enemyEconomy = 1630;
const int allyDamage = 2560;
const int enemyDamage = 1140;
m_snapshot.analysisMetrics = QVariantList{
tacticalMetric("ally_economy", "我方总经济", allyEconomy,
allyEconomy >= enemyEconomy ? "good" : "warn",
"GlobalLogisticsStatus", 65,
QStringLiteral("敌方 %1").arg(enemyEconomy)),
tacticalMetric("enemy_economy", "敌方总经济", enemyEconomy,
enemyEconomy > allyEconomy ? "warn" : "good",
"GlobalLogisticsStatus", 65,
QStringLiteral("我方 %1").arg(allyEconomy)),
tacticalMetric("ally_damage", "我方总伤害", allyDamage,
allyDamage >= enemyDamage ? "good" : "warn",
"RobotInjuryStat", 70,
QStringLiteral("敌方 %1").arg(enemyDamage)),
tacticalMetric("enemy_damage", "敌方总伤害", enemyDamage,
enemyDamage > allyDamage ? "warn" : "good",
"RobotInjuryStat", 70,
QStringLiteral("我方 %1").arg(allyDamage)),
tacticalMetric("hp_diff", "总血量差", "+800", "good", "GlobalUnitStatus", 80,
QStringLiteral("我方领先 800"))
};
m_snapshot.cameraPreviewData = QVariantMap{
{"connected", false},
{"fps", "--"},
{"latencyMs", 0},
{"decodeMs", 0},
{"renderMs", 0},
{"grayFrameRate", 0},
{"stall", false},
{"sourceName", "0x0310 H264"}
};
m_snapshot.linkHealth = QVariantMap{
{"mqttStatus", "ok"},
{"mqttLatencyMs", 23},
{"videoStatus", "unknown"},
{"videoLatencyMs", 0},
{"radarStatus", "ok"},
{"radarAgeMs", 118},
{"commandStatus", "disabled"}
};
// 趋势预测
m_snapshot.predictions.threatHistory = {0.30, 0.45, 0.55, 0.62, 0.70};
m_snapshot.predictions.economyHistory = {320, 350, 380, 410, 440};
m_snapshot.predictions.threatTrend = "↗";
m_snapshot.predictions.threatDesc = "持续上升";
m_snapshot.predictions.economyProjection = "+120";
{
QVariantList predEvents;
QVariantMap pe1;
pe1["time"] = "T+8s"; pe1["text"] = "前哨站将被摧毁"; pe1["color"] = "#FF4444";
predEvents.append(pe1);
QVariantMap pe2;
pe2["time"] = "T+15s"; pe2["text"] = "大能量机关刷新"; pe2["color"] = "#FFAA00";
predEvents.append(pe2);
m_snapshot.predictions.predictedEvents = predEvents;
}
m_snapshot.snapshotTimestamp = QDateTime::currentMSecsSinceEpoch();
}
void TacticalAnalyzer::analyzeMock() {
bool needsInitialSnapshot = false;
{
QMutexLocker lock(&m_mutex);
needsInitialSnapshot = m_snapshot.snapshotTimestamp <= 0;
}
// 手动调用 analyze() 时也应得到完整快照,不能依赖 start() 预先加载。
if (needsInitialSnapshot) {
loadMockData();
} else {
QMutexLocker lock(&m_mutex);
m_snapshot.snapshotTimestamp = QDateTime::currentMSecsSinceEpoch();
}
// 信号必须在解锁后发出,避免 QML 同步回调死锁。
emit snapshotUpdated();
}
QVariantMap TacticalAnalyzer::dashboardData() const {
return m_gameData && m_gameData->uses2027Protocol() ? m_gameData->dashboardData() : QVariantMap{};
}
void TacticalAnalyzer::analyzeReal() {
// 从 GameData 读取真实数据,并交给 ThreatRanker 计算威胁排序。
if (!m_gameData) {
emit snapshotUpdated();
return;
}
{
QMutexLocker lock(&m_mutex);
updateHeader();
updateResources();
updateTopStatus();
updateKeyEvents();
updateRobotLists();
updateAllyExecution();
updateTopTargets();
updateVideoOverlay();
updateRadarData();
updateAnalysisMetrics();
updateCameraPreviewData();
updateLinkHealth();
updateMainDecision();
updatePredictions();
m_snapshot.snapshotTimestamp = QDateTime::currentMSecsSinceEpoch();
}
// 信号在解锁后发出,避免同步回调再次取锁。
emit snapshotUpdated();
}
// ═══════════════════════════════════════════════════════════
// 各面板 Real 数据更新
// ═══════════════════════════════════════════════════════════
void TacticalAnalyzer::updateHeader() {
const auto &gs = m_gameData->getGameState();
auto &h = m_snapshot.header;
h.matchMode = "正赛";
h.redScore = gs.redScore;
h.blueScore = gs.blueScore;
h.currentRound = gs.currentRound;
h.totalRounds = 5;
h.stage = (gs.gameProgress == GameStage::PREPARATION) ? "准备"
: (gs.gameProgress == GameStage::BATTLE) ? "BATTLE"
: "其他";
int secs = gs.gameTime;
h.timeRemaining = QString("%1:%2")
.arg(secs / 60).arg(secs % 60, 2, 10, QChar('0'));
h.linkStatus = m_useMockData ? "Mock" : "链路 OK";
h.linkLatency = 0;
}
void TacticalAnalyzer::updateResources() {
auto &r = m_snapshot.resources;
const bool redPerspective = m_gameData->currentRobotId() < 100;
r.allyBaseHp = redPerspective ? m_gameData->redBaseHealth()
: m_gameData->blueBaseHealth();
r.allyBaseMax = redPerspective ? m_gameData->redBaseMaxHealth()
: m_gameData->blueBaseMaxHealth();
r.enemyBaseHp = redPerspective ? m_gameData->blueBaseHealth()
: m_gameData->redBaseHealth();
r.enemyBaseMax = redPerspective ? m_gameData->blueBaseMaxHealth()
: m_gameData->redBaseMaxHealth();
r.allyOutpostHp = redPerspective ? m_gameData->redOutpostHealth()
: m_gameData->blueOutpostHealth();
r.allyOutpostMax = redPerspective ? m_gameData->redOutpostMaxHealth()
: m_gameData->blueOutpostMaxHealth();
r.enemyOutpostHp = redPerspective ? m_gameData->blueOutpostHealth()
: m_gameData->redOutpostHealth();
r.enemyOutpostMax = redPerspective ? m_gameData->blueOutpostMaxHealth()
: m_gameData->redOutpostMaxHealth();
r.allyOutpostDestroyed = redPerspective ? m_gameData->redOutpostDestroyed()
: m_gameData->blueOutpostDestroyed();
r.enemyOutpostDestroyed = redPerspective ? m_gameData->blueOutpostDestroyed()
: m_gameData->redOutpostDestroyed();
r.allyBaseInvincible = redPerspective ? m_gameData->redBaseInvincible()
: m_gameData->blueBaseInvincible();
r.enemyBaseInvincible = redPerspective ? m_gameData->blueBaseInvincible()
: m_gameData->redBaseInvincible();
r.allyDefenseBonus = redPerspective ? m_gameData->redDefenseBonus()
: m_gameData->blueDefenseBonus();
r.enemyDefenseBonus = redPerspective ? m_gameData->blueDefenseBonus()
: m_gameData->redDefenseBonus();
int redEco = m_gameData->redEconomy();
int blueEco = m_gameData->blueEconomy();
r.economyDiff = redPerspective ? redEco - blueEco : blueEco - redEco;
r.damageDiff = redPerspective
? m_gameData->redTotalDamage() - m_gameData->blueTotalDamage()
: m_gameData->blueTotalDamage() - m_gameData->redTotalDamage();
r.hpDiff = redPerspective
? m_gameData->redRobotTotalHP() - m_gameData->blueRobotTotalHP()
: m_gameData->blueRobotTotalHP() - m_gameData->redRobotTotalHP();
}
void TacticalAnalyzer::updateTopStatus() {
auto &t = m_snapshot.topStatus;
const bool redPerspective = m_gameData->currentRobotId() < 100;
t.allyRemainingEconomy = redPerspective
? static_cast<int>(m_gameData->redRemainingEconomy())
: static_cast<int>(m_gameData->blueRemainingEconomy());
t.allyTotalEconomyObtained = redPerspective
? static_cast<qulonglong>(m_gameData->redTotalEconomyObtained())
: static_cast<qulonglong>(m_gameData->blueTotalEconomyObtained());
t.allyTechLevel = redPerspective
? static_cast<int>(m_gameData->redTechLevel())
: static_cast<int>(m_gameData->blueTechLevel());
t.allyEncryptionLevel = redPerspective
? static_cast<int>(m_gameData->redEncryptionLevel())
: static_cast<int>(m_gameData->blueEncryptionLevel());
t.allyFortressOccupationSec = m_gameData->allyFortressOccupationSec();
t.enemyFortressOccupationSec = m_gameData->enemyFortressOccupationSec();
const GameStage stage = m_gameData->getCurrentStage();
const bool battleActive = stage == GameStage::BATTLE &&
!m_gameData->is_paused();
if (battleActive) {
const int remainingTime =
qBound(0, static_cast<int>(m_gameData->getGameTime()), 420);
const int elapsedMinutes = (420 - remainingTime + 59) / 60;
const int robotLevel = qMax(1, m_gameData->robotLevel());
t.respawnGoldCost = elapsedMinutes * 80 + robotLevel * 20;
t.affordableRespawnCount =
qMax(0, m_gameData->currentTeamEconomy()) / t.respawnGoldCost;
} else {
t.respawnGoldCost = 0;
t.affordableRespawnCount = 0;
}
t.respawnEconomyVisible =
battleActive || stage == GameStage::NOT_STARTED ||
stage == GameStage::PREPARATION || stage == GameStage::SELF_CHECK ||
stage == GameStage::COUNTDOWN;
}
void TacticalAnalyzer::updateKeyEvents() {
// 直接复用左侧系统消息链路中的结构化消息项(text/color),
QVariantList events;
QVariantList rawMsgs = m_gameData->getSystemMessages();
static const QRegularExpression kTimestampPrefix(
QStringLiteral("^(\\d+:\\d{2})\\s+(.*)$"));
auto shouldHideFromTactical = [](const QString &message) {
return message.contains(QStringLiteral("模块离线")) ||
message.contains(QStringLiteral("黄牌已累计")) ||
message.contains(QStringLiteral("未选择底盘性能")) ||
message.contains(QStringLiteral("未选择发射机构类型"));
};
//从最后一条消息开始取
for (int i = rawMsgs.size() - 1; i >= 0; --i) {
const QVariantMap msgItem = rawMsgs[i].toMap();
QString fullText = msgItem.value(QStringLiteral("text")).toString();
QString color = msgItem.value(QStringLiteral("color")).toString();
QString timeText;
QString bodyText = fullText;
//提取时间文字和文本文字
const QRegularExpressionMatch match = kTimestampPrefix.match(fullText);
if (match.hasMatch()) {
timeText = match.captured(1);
bodyText = match.captured(2);
}
if (shouldHideFromTactical(bodyText)) {
continue;
}
//如果没有自带时间,则用当前游戏时间补上
if (timeText.isEmpty()) {
const auto &gs = m_gameData->getGameState();
const int secs = gs.gameTime;
timeText = QStringLiteral("%1:%2")
.arg(secs / 60)
.arg(secs % 60, 2, 10, QChar('0'));
}
KeyEventItem ev;
ev.text = bodyText;
ev.color = color.isEmpty() ? QStringLiteral("#AABBCC") : color;
ev.time = timeText;
events.append(toVariantMap(ev));
if (events.size() >= m_config.maxKeyEvents) {
break;
}
}
m_snapshot.keyEvents = events;
}
void TacticalAnalyzer::updateVideoOverlay() {
// 使用当前操控机器人信息填充图传覆盖
int myId = m_gameData->currentRobotId();
QVariantMap myRobot = m_gameData->getMyRobot();
auto &v = m_snapshot.video;
v.targetId = QString("R%1").arg(myId);
v.targetLabel = "当前操控";
v.targetHp = myRobot.value("hp", 0).toInt();
v.targetMaxHp = myRobot.value("maxHp", kTacticalDefaultRobotMaxHp).toInt();
v.distance = 0.0;
v.lockQuality = 0.5;
v.hasHologram = false;
v.compensationText = "";
// 如果有敌方主目标,优先显示
if (m_ranker) {
// 取 TOP1 目标
if (!m_snapshot.topTargets.isEmpty()) {
QVariantMap top = m_snapshot.topTargets.first().toMap();
v.targetId = top.value("id", "?").toString();
v.targetLabel = top.value("label", "目标").toString();
v.targetHp = top.value("hp", 0).toInt();
v.targetMaxHp = top.value("maxHp", kTacticalDefaultRobotMaxHp).toInt();
v.hasHologram = true;
}
}
}
void TacticalAnalyzer::updateMainDecision() {
auto &d = m_snapshot.decision;
const auto &gs = m_gameData->getGameState();
// 默认背景观察
d.title = "态势观察";
d.priority = "P3";
d.confidence = 50;
d.windowText = "";
d.reasons = QStringList{"等待比赛数据..."};
d.fallbackActions = QStringList{};
if (!m_ranker || gs.gameProgress != GameStage::BATTLE) return;
// 收集环境数据用于决策
const bool redPerspective = m_gameData->currentRobotId() < 100;
int allyBaseHp = redPerspective ? m_gameData->redBaseHealth()
: m_gameData->blueBaseHealth();
int allyBaseMax = redPerspective ? m_gameData->redBaseMaxHealth()
: m_gameData->blueBaseMaxHealth();
double allyBaseRatio = (allyBaseMax > 0)
? static_cast<double>(allyBaseHp) / allyBaseMax : 1.0;
int enemyOutpostHp = redPerspective ? m_gameData->blueOutpostHealth()
: m_gameData->redOutpostHealth();
int enemyOutpostMax = redPerspective ? m_gameData->blueOutpostMaxHealth()
: m_gameData->redOutpostMaxHealth();
double enemyOutpostRatio = (enemyOutpostMax > 0)
? static_cast<double>(enemyOutpostHp) / enemyOutpostMax : 1.0;
// 统计可射击的己方数量
int fireReady = 0;
int ammoLow = 0;
for (const auto &card : m_snapshot.allyExecution) {
QVariantMap c = card.toMap();
if (c.value("canFire", false).toBool()) fireReady++;
if (c.value("ammoPct", 100).toInt() < m_config.ammoLowRatio * 100) ammoLow++;
}
bool decisionSelected = false;
// P0: 基地紧急防守
if (allyBaseRatio < m_config.baseDefendHpRatio) {
d.title = "回防基地";
d.priority = "P0";
d.confidence = 85;
d.windowText = "立即";
d.reasons = QStringList{
QString("己方基地血量 %1%").arg(qRound(allyBaseRatio * 100)),
"紧急防守最高优先级"
};
d.fallbackActions = QStringList{"撤退", "呼叫支援"};
decisionSelected = true;
}
// P1: 推进前哨
else if (enemyOutpostRatio < m_config.outpostPushHpRatio
&& fireReady >= m_config.fireReadyMin) {
d.title = "推进敌方前哨站";
d.priority = "P1";
d.confidence = 70;
d.windowText = QString("敌哨残血 %1%").arg(qRound(enemyOutpostRatio * 100));
d.reasons = QStringList{
QString("敌方前哨站残血 %1%").arg(qRound(enemyOutpostRatio * 100)),
QString("己方火力 %1 台就绪").arg(fireReady)
};
d.fallbackActions = QStringList{"集火目标", "控图保热"};
decisionSelected = true;
}
// P1: 击杀窗口 — 由 ThreatRanker 评分驱动
else if (!m_snapshot.topTargets.isEmpty()) {
QVariantMap top = m_snapshot.topTargets.first().toMap();
int hp = top.value("hp", 0).toInt();
int maxHp = top.value("maxHp", kTacticalDefaultRobotMaxHp).toInt();
double hpRatio = maxHp > 0 ? static_cast<double>(hp) / maxHp : 1.0;
if (hpRatio < m_config.killWindowHpRatio && fireReady >= m_config.fireReadyMin) {
d.title = QString("集火击杀 %1").arg(top.value("label").toString());
d.priority = "P1";
d.confidence = qMin(90, qRound(top.value("threat", 0.5).toDouble() * 100));
d.windowText = QString("残血 %1/%2").arg(hp).arg(maxHp);
d.reasons = QStringList{
QString("残血 %1/%2").arg(hp).arg(maxHp),
QString("威胁评分 %1").arg(top.value("threat", 0).toDouble(), 0, 'f', 2),
QString("火力就绪 %1 台").arg(fireReady)
};
d.fallbackActions = QStringList{"转火哨兵", "经济补给"};
decisionSelected = true;
}
}
// P2: 经济补给
if (!decisionSelected && ammoLow > 0) {
d.title = "控图补给";
d.priority = "P2";
d.confidence = 60;
d.windowText = QString("%1 台弹量低").arg(ammoLow);
d.reasons = QStringList{
QString("%1 台机器人弹量不足 %2%").arg(ammoLow).arg(qRound(m_config.ammoLowRatio * 100)),
"建议回撤补给"
};
d.fallbackActions = QStringList{"控图保热", "等待窗口"};
decisionSelected = true;
}
// 防抖: 首次决策直接采用, 后续切换需确认帧数 + 时间 + 分数差
bool firstDecision = m_lastDecision.title.isEmpty();
if (!firstDecision && m_lastDecision.title != d.title) {
m_decisionFrameCount++;
qint64 now = QDateTime::currentMSecsSinceEpoch();
if (m_decisionFrameCount < m_config.decisionConfirmFrames
&& (now - m_lastDecisionTime) < m_config.decisionHoldMs) {
d = m_lastDecision; // 保持旧决策
return;
}
}
m_lastDecision = d;
m_decisionFrameCount = 0;
m_lastDecisionTime = QDateTime::currentMSecsSinceEpoch();
}
void TacticalAnalyzer::updateTopTargets() {
if (!m_ranker || !m_freshness) return;
const auto &allRobots = m_gameData->getAllRobots();
int myId = m_gameData->currentRobotId();
bool isRed = (myId < 100);
// 分离己方/敌方机器人
QMap<int, QVariantMap> allyRobots, enemyRobots;
for (const auto &robot : allRobots) {
QVariantMap info = m_gameData->getRobotInfo(robot.robotId);
if (info.isEmpty()) continue;
const bool isAlly = (robot.robotId < 100) == isRed;
info["label"] = tacticalRobotIdLabel(robot.robotId, isAlly);
info["icon"] = tacticalRobotTypeIcon(robot.type);
info["x"] = robot.posX;
info["y"] = robot.posY;
info["angle"] = robot.angle;
info["type"] = static_cast<int>(robot.type);
info["isHighLight"] = robot.isHighLight > 0;
info["hasVulnerability"] = robot.buffs.defenseOrVulnerability.level < 0;
if (m_freshness) {
const qint64 lastUpdateMs = robotLastUpdateMs(robot);
if (lastUpdateMs > 0) {
m_freshness->touch(QStringLiteral("enemy_pos_%1").arg(robot.robotId),
lastUpdateMs);
}
}
if (isAlly) {
allyRobots[robot.robotId] = info;
} else {
enemyRobots[robot.robotId] = info;
}
}
const bool redPerspective = m_gameData->currentRobotId() < 100;
int allyBaseHp = redPerspective ? m_gameData->redBaseHealth()
: m_gameData->blueBaseHealth();
// 调用 ThreatRanker
auto scores = m_ranker->rankTargets(allyRobots, enemyRobots,
allyBaseHp, *m_freshness);
// 转换为 TargetRankItem
QVariantList items;
int maxCount = qMin(scores.size(), m_config.maxTopTargets);
for (int i = 0; i < maxCount; ++i) {
const auto &s = scores[i];
TargetRankItem item;
item.rank = i + 1;
item.id = tacticalRobotIdLabel(s.robotId, false);
item.label = s.label.isEmpty() ? item.id : s.label;
item.icon = s.icon;
item.hp = s.hp;
item.maxHp = s.maxHp;
item.threat = s.totalScore;
items.append(toVariantMap(item));
}
m_snapshot.topTargets = items;
}
void TacticalAnalyzer::updateRadarData() {
auto &rd = m_snapshot.radar;
rd.mapImageSource = m_gameData->mapBackground();
const auto &allRobots = m_gameData->getAllRobots();
int myId = m_gameData->currentRobotId();
bool isRed = (myId < 100);
QVariantList ally, enemy;
qint64 newestRadarUpdateMs = 0;
const bool season2027 = m_gameData->uses2027Protocol();
for (const auto &robot : allRobots) {
// 只接入有位置数据的机器人,零点本身也是有效坐标。
if (season2027 ? !robot.lastPositionTime.isValid() : !robotHasUsablePosition(robot))
continue;
MapRobot mr;
bool isAlly = (robot.robotId < 100) == isRed;
mr.id = tacticalRobotIdLabel(robot.robotId, isAlly);
mr.label = mr.id;
//坐标归一化
QPointF normalized;
if (m_gameData->uses2027Protocol()) {
normalized = m_gameData->normalizedMapPosition(robot.posX, robot.posY);
} else if (m_mapper) {
normalized = m_mapper->battlefieldToNormalized(robot.posX, robot.posY);
} else {
normalized = QPointF(static_cast<double>(robot.posX) / m_config.battlefieldWidth,
static_cast<double>(robot.posY) / m_config.battlefieldHeight);
}
mr.x = qBound(0.0, normalized.x(), 1.0);
mr.y = qBound(0.0, normalized.y(), 1.0);
// 2027's bearing is already north-zero, as drawn by the map marker.
mr.angle = m_gameData->uses2027Protocol() || !m_mapper
? robot.angle : m_mapper->yawToCanvasAngle(robot.angle);
const qint64 updateMs = season2027 ? robot.lastPositionTime.toMSecsSinceEpoch()
: robotLastUpdateMs(robot);
newestRadarUpdateMs = qMax(newestRadarUpdateMs, updateMs);
QVariantMap map = toVariantMap(mr);
if (season2027 && !robot.hasPositionHeading)
map.remove(QStringLiteral("angle"));
map["ageMs"] = updateMs > 0
? QDateTime::currentMSecsSinceEpoch() - updateMs
: 999999;
map["number"] = robot.robotId % 100;
map["team"] = isAlly ? QStringLiteral("ally") : QStringLiteral("enemy");
map["visible"] = true;
map["predicted"] = false;
map["stale"] = map.value("ageMs").toInt() > m_config.freshnessStaleMs;
map["hpPct"] = pct(robot.currentHP, robot.maxHP);
map["focus"] = false;
if (isAlly) ally.append(map);
else enemy.append(map);
}
rd.allyRobots = ally;
rd.enemyRobots = enemy;
// 主目标:取当前威胁排名第一的敌方
if (!m_snapshot.topTargets.isEmpty()) {
rd.focusTargetId = m_snapshot.topTargets.first().toMap()
.value("id", "").toString();
}
rd.radarAgeMs = newestRadarUpdateMs > 0