diff --git a/auto-solution-rule/src/main/java/com/solution/rule/domain/simplerulepojo/Task.java b/auto-solution-rule/src/main/java/com/solution/rule/domain/simplerulepojo/Task.java index 355c992..c2d10c3 100644 --- a/auto-solution-rule/src/main/java/com/solution/rule/domain/simplerulepojo/Task.java +++ b/auto-solution-rule/src/main/java/com/solution/rule/domain/simplerulepojo/Task.java @@ -34,9 +34,9 @@ public class Task { //任务目标id private String targetId; - //作战区经纬度 - private Coordinate warZoneLocation; + //作战区经纬高 + private List warZoneLocation; - //防区经纬度 - private Coordinate defZoneLocation; + //防区经纬高 + private List defZoneLocation; } diff --git a/auto-solution-rule/src/main/resources/rules/README.md b/auto-solution-rule/src/main/resources/rules/README.md new file mode 100644 index 0000000..e250f85 --- /dev/null +++ b/auto-solution-rule/src/main/resources/rules/README.md @@ -0,0 +1,315 @@ +# fire-rule.drl 维护说明(业务+开发) + +本文档对应 `auto-solution-rule/src/main/resources/rules/fire-rule.drl` 当前版本,重点说明: +- 业务人员改哪里 +- 每个参数改了会产生什么效果 +- 如何快速判断规则是否“命中/不命中” + +## 1. 先看这一个入口:业务可改区 + +只改 `//TODO` 下的 `buildBusinessConfig()`,其他函数不要改。 + +`buildBusinessConfig()` 里每个参数的效果如下。 + +## 2. 参数-效果对照(给业务人员) + +### 2.1 武器名称映射(改名字,不改逻辑) +- `redStrikeDroneName`:空中反制组中的无人机名称。 +- `redArmedHelicopterName`:空中反制组中的武装直升机名称。 +- `redHowitzerName`:地面反制组中的迫榴炮名称。 +- `redVehicleMortarName`:地面反制组中的车载迫击炮名称。 +- `redAaWeaponName`:空中反制组中的防空导弹武器名称。 +- `redAtRocketName`:装甲反制组中的反坦克火箭名称。 +- `redAtMissileSystemName`:装甲反制组中的反坦克导弹系统名称。 +- `redMissileVehicleName`:导弹补充组中的导弹发射车名称。 + +### 2.2 白名单开关(决定“是否匹配”) +- `enableAirRule`:`true` 时,蓝方空中目标会触发红方空中反制组;`false` 时该组永不触发。 +- `enableGroundRule`:`true` 时,蓝方地面目标会触发红方炮类反制组;`false` 时不触发。 +- `enableArmorRule`:`true` 时,蓝方装甲目标会触发红方反坦克组;`false` 时不触发。 +- `enableMissileVehicleRule`:`true` 时,蓝方有导弹能力可追加导弹发射车;`false` 时不追加。 +- `enableMissileLinkage`:`true` 时开启导弹数量/范围联动;`false` 时不做导弹联动增强。 +- `allowMultiGroup`: + - `true`:同一批输入可命中多组策略并叠加武器; + - `false`:只命中第一组,后续组不再生效(更“死规则”)。 +- `enableArmedHelicopterOnAir`:空中组中是否包含武装直升机。 + +### 2.3 蓝方类型到红方方案映射(核心,可多选) + +先解释你提到的 “k”: +- 这里的 `k` 就是 **key(键名)**,例如 `map_armor_targets`。 +- `v` 是 **value(值)**,例如 `反坦克火箭,反坦克导弹系统`。 +- 规则实际含义就是:`key` 决定“哪类蓝方目标”,`value` 决定“红方上哪些武器”。 + +映射键名中文对照: +- `map_air_targets`:蓝方是空中目标时,红方使用哪些武器。 +- `map_ground_targets`:蓝方是地面目标时,红方使用哪些武器。 +- `map_armor_targets`:蓝方是装甲目标(坦克/装甲车)时,红方使用哪些武器。 +- `map_artillery_targets`:蓝方是炮类目标时,红方使用哪些武器。 +- `map_missile_targets`:蓝方有导弹能力时,红方使用哪些武器。 + +映射规则说明: +- 值必须是红方武器库内合法名称,否则该项会被忽略。 +- 为空时视为该组不配置,允许不命中。 +- 示例:`map_armor_targets=反坦克火箭,反坦克导弹系统` 表示坦克可同时触发两种红方反制武器。 + +### 2.4 数量和阈值(决定“匹配后给多少”) +- `defaultAirNum`:空中组默认数量。 +- `defaultGroundNum`:地面/装甲组默认数量。 +- `defaultMissileVehicleNum`:导弹发射车默认数量。 +- `shellRangeDefault`:炮类组件参数值,单位固定 `范围米`。 +- `missileCountOffset`:红方导弹数量 = 蓝方导弹数量 + 偏移量。 +- `missileRangeOffset`:红方导弹范围 = 蓝方导弹范围 + 偏移量(单位 `破坏范围米`)。 +- `blueMissileRangeDefault`:蓝方导弹范围缺失时采用的默认值。 +- `minBlueMissileCountForLinkage`:蓝方导弹数量达到该值才触发联动增强。 + +### 2.5 targetId 自动绑定参数(新增) +- `enableTargetAutoBind`:是否自动给红方武器写入 `targetId`。 +- `minTargetBindRatio`:最低绑定比例(例如 `0.7` 表示至少 70% 红方武器有目标)。 +- `allowReserveWithoutTarget`: + - `true`:允许少量红方武器 `targetId` 为空(火力冗余)。 + - `false`:尽量给每个红方武器分配目标。 + +绑定规则说明(固定,不需要业务改代码): +- 绑定来源是蓝方武器 `equipmentId`。 +- 匹配优先级按武器类型: + - 防空类红方武器优先绑定蓝方空中目标 + - 反装甲类红方武器优先绑定蓝方装甲目标 + - 炮类红方武器优先绑定蓝方炮类/地面目标 + - 导弹发射车优先绑定蓝方导弹能力目标 +- 当优先池不足时自动回退到地面池/全目标池,保证大部分武器有目标。 + +### 2.6 阵位规则参数(新增) +- `enablePositionRules`:阵位规则总开关。 +- 阵位输入来源:`blueTask.warZoneLocation` 与 `blueTask.defZoneLocation`(各 4 个经纬点)。 +- `fireUnitSpacingMeters`:防区/作战区点位间距(米),例如 `100` 代表约每 100 米一个火力单元。 +- `airDeployZonePreference`:飞机优先部署区域(`combat` 或 `defense`)。 +- `defensePriorityWeapons`:优先部署在防区的武器名单(逗号分隔)。 +- `groundDeployHeight` / `airDeployHeight`:地面/空中武器部署高度。 + +阵位规则效果: +- 新增两条规则: + - `阵位规则-区域解析与点位生成` + - `阵位规则-武器部署赋位` +- 飞机可在任意区(按偏好区优先);反坦克等重火力优先防区。 +- 在多边形区域内按间距生成候选点,并给红方武器写入 `weapon.coordinate`。 + +阵位输入示例(仅经纬度,4点): + +```json +{ + "warZoneLocation": [ + { "longitude": 116.3801, "latitude": 39.9001 }, + { "longitude": 116.3901, "latitude": 39.9001 }, + { "longitude": 116.3901, "latitude": 39.9101 }, + { "longitude": 116.3801, "latitude": 39.9101 } + ], + "defZoneLocation": [ + { "longitude": 116.3825, "latitude": 39.9025 }, + { "longitude": 116.3875, "latitude": 39.9025 }, + { "longitude": 116.3875, "latitude": 39.9075 }, + { "longitude": 116.3825, "latitude": 39.9075 } + ] +} +``` + +### 2.7 航迹规则参数(新增) +- `enableTrajectoryRules`:航迹规则总开关。 +- `strategyMode`:`auto/shortest/flank/interfere`。 + - `auto`:智能选择策略。 + - 其他值:强制使用指定策略(若该策略被禁用会自动回退)。 +- `enableShortest` / `enableFlank` / `enableInterfere`:各策略开关。 +- `nearDefDistanceMeters`:蓝方末端点到防区的“近距离阈值”。 +- `farDefDistanceMeters`:蓝方末端点到防区的“远距离阈值”。 +- `fastSpeedThreshold`:蓝方平均速度达到该值视为“高速”。 +- `flankOffsetMeters`:绕后追击偏移幅度(越大绕后越明显)。 +- `interfereOffsetMeters`:干扰轨迹基础偏移。 +- `interfereZigzagAmplitude`:干扰轨迹锯齿振幅(越大摆动越明显)。 +- `keepBlueHeight`:是否沿用蓝方高度。 +- `redTrackHeightOverride`:不沿用时红方统一高度。 + +航迹智能选择逻辑: +- `near + fast` -> `shortest`(最短距离追击) +- `far + fast` -> `interfere`(干扰/诱偏) +- 其他 -> `flank`(绕后追击) + +不生成保护: +- 蓝方 `trackPoints` 为空,或 `defZoneLocation` 点数不足(<3),则不生成红方航迹。 + +## 3. 当前规则行为(简版) + +- `装备组件匹配`、`组件参数匹配`:已作为 `legacy` 占位,不承担当前业务决策。 +- 主决策在 `红方武器自适应装配规则`:调用 `configureRedWeaponsByBlue(...)`,按“映射配置”添加武器。 +- 导弹增强在 `导弹联动增强规则`:调用 `applyMissileLinkage(...)`,受开关和阈值控制。 +- 任务命名在 `任务自动匹配规则`:调用 `assignTaskNameByRedWeapons(...)`,按红方最终武器自动生成任务名和 `dataType`。 +- 炮类约束:命中炮类条件时,炮类武器只保留 `炮弹` 组件,单位 `范围米`。 +- `targetId` 绑定:在装配后自动执行,尽量为红方武器绑定蓝方 `equipmentId`,允许少量空值冗余。 +- 阵位部署:按多边形区域和武器类型自动赋位,保证防区火力覆盖。 +- 航迹生成:根据蓝方 `trackPoints` 生成红方 `trackPoints`,点数与蓝方一致,支持三套策略和智能选择。 + +## 3.1 任务名称自动匹配(新增) + +任务命名依据:**红方最终武器**(不是蓝方任务名关键字)。 + +当前分类优先级: +- 导弹突击(导弹发射车) +- 防空压制(防空导弹武器/火力打击无人机/武装直升机) +- 反装甲打击(反坦克火箭/反坦克导弹系统) +- 炮火压制(迫榴炮/车载迫击炮) +- 通用打击(兜底) + +业务可调模板(在 `buildBusinessConfig()`): +- `taskName_missile_strike` / `taskDataType_missile_strike` +- `taskName_air_defence` / `taskDataType_air_defence` +- `taskName_anti_armor` / `taskDataType_anti_armor` +- `taskName_artillery` / `taskDataType_artillery` +- `taskName_general` / `taskDataType_general` + +效果说明: +- 只改这些模板文字,不改函数,也能改变最终任务展示名。 +- 若分类与武器不一致,会自动回落到 `通用打击任务`,避免“任务名和武器不符”。 + +## 4. 快速修改示例(业务常用) + +- 想让规则更“死”: + - 把 `allowMultiGroup` 改成 `false`。 +- 想让“坦克只上反坦克火箭,不上导弹系统”: + - 把 `map_armor_targets` 改成 `反坦克火箭`。 +- 想让“坦克同时上火箭和导弹系统”: + - 把 `map_armor_targets` 改成 `反坦克火箭,反坦克导弹系统`。 +- 想允许更多不命中: + - 关闭部分开关,例如 `enableGroundRule=false`、`enableArmorRule=false`。 +- 想让导弹联动更难触发: + - 提高 `minBlueMissileCountForLinkage`,例如从 `1` 调到 `3`。 +- 想提升炮类打击范围: + - 调大 `shellRangeDefault`,例如 `1500 -> 1800`。 + +## 5. 测试 JSON(按你项目常用的 Task 入参格式) + +说明:下面 JSON 是 **单个 Task** 格式,不是 `FactTask` 包装格式。 +如果你的执行入口最终需要 `FactTask`,则由开发在服务层把“蓝方 Task + 红方 Task”组装后再 `insert` 规则引擎。 + +### 5.1 不命中样例(蓝方 Task) +说明:该样例仅用于蓝方任务输入结构校验;若业务把对应映射留空或关闭开关,则允许不命中。 + +```json +{ + "id": "blue-miss-001", + "side": "蓝方", + "dataType": "打击", + "threatLevel": "2", + "taskWeapons": [ + { + "name": "攻击直升机", + "supportType": "overhead", + "number": 1, + "components": [] + } + ] +} +``` + +### 5.2 命中样例(蓝方 Task) +说明:该蓝方输入同时包含空中和装甲特征,且有导弹组件,满足命中与联动测试条件。 + +```json +{ + "id": "blue-hit-001", + "side": "蓝方", + "dataType": "打击", + "threatLevel": "3", + "taskWeapons": [ + { + "name": "攻击直升机", + "supportType": "overhead", + "number": 2, + "components": [ + { + "deviceName": "机载导弹", + "componentParams": [ + { + "attDefaultValue": "260", + "attExplain": "破坏范围米", + "number": 2 + } + ] + } + ] + }, + { + "name": "主战坦克", + "supportType": "ground", + "number": 2, + "components": [] + } + ] +} +``` + +### 5.3 红方初始 Task 样例(通常为空列表) +说明:用于和蓝方 Task 组装成规则输入对象。 + +```json +{ + "id": "red-init-001", + "side": "红方", + "dataType": "打击", + "threatLevel": "1", + "taskWeapons": [] +} +``` + +### 5.4 航迹规则不生成样例(缺防区) +```json +{ + "id": "blue-track-miss-001", + "side": "蓝方", + "dataType": "打击", + "threatLevel": "2", + "defZoneLocation": [], + "trackPoints": [ + { "index": 0, "longitude": 116.3801, "latitude": 39.9001, "height": 100, "speed": 210 }, + { "index": 1, "longitude": 116.3810, "latitude": 39.9010, "height": 100, "speed": 220 } + ], + "taskWeapons": [] +} +``` + +### 5.5 航迹规则命中样例(可生成) +```json +{ + "id": "blue-track-hit-001", + "side": "蓝方", + "dataType": "打击", + "threatLevel": "3", + "defZoneLocation": [ + { "longitude": 116.4001, "latitude": 39.9201 }, + { "longitude": 116.4051, "latitude": 39.9201 }, + { "longitude": 116.4051, "latitude": 39.9251 }, + { "longitude": 116.4001, "latitude": 39.9251 } + ], + "trackPoints": [ + { "index": 0, "longitude": 116.3801, "latitude": 39.9001, "height": 120, "speed": 240 }, + { "index": 1, "longitude": 116.3840, "latitude": 39.9040, "height": 120, "speed": 245 }, + { "index": 2, "longitude": 116.3880, "latitude": 39.9080, "height": 120, "speed": 250 }, + { "index": 3, "longitude": 116.3920, "latitude": 39.9120, "height": 120, "speed": 248 } + ], + "taskWeapons": [] +} +``` + +## 6. 开发人员说明(放在末尾) + +- **入口约定**:业务仅改 `buildBusinessConfig()`,开发改函数实现时不要破坏该入口。 +- **类型约定**:配置读取统一走 `readIntCfg(...)` / `readBooleanCfg(...)`,避免 `Map` 强转异常。 +- **策略收敛**:`configureRedWeaponsByBlue(...)` 使用 `matchedAny + allowMultiGroup` 控制“单组命中/多组叠加”。 +- **映射解析**:通过 `parseMappedWeaponNames(...)` 将逗号分隔配置解析为武器列表,非法名称会被 `isValidRedWeaponNameByConfig(...)` 过滤。 +- **联动门控**:`applyMissileLinkage(...)` 必须同时满足: + - `enableMissileLinkage=true` + - 蓝方导弹数量 `>= minBlueMissileCountForLinkage` +- **目标绑定**:`bindTargetIdsForRedWeapons(...)` 基于蓝方 `equipmentId` 分配 `targetId`,支持“优先匹配 + 绑定率阈值 + 冗余空目标”。 +- **阵位部署**:`prepareDeploymentPools(...)` + `applyWeaponDeployment(...)` 负责区域解析、点位生成与部署赋位。 +- **航迹生成**:`applyTrajectoryGeneration(...)` + `chooseTrajectoryStrategy(...)` + `generateRedTrackPoints(...)` 负责红方航迹策略生成。 +- **任务命名**:`assignTaskNameByRedWeapons(...)` 仅基于红方最终武器,避免旧版按蓝方 `drawName` 关键字造成误判。 +- **legacy 区域**:`装备组件匹配`、`组件参数匹配` 及 legacy 函数区只保留回滚,不建议继续扩展。 +- **新增武器建议**:优先补 `isAirWeapon/isGroundWeapon/isArmorWeapon/isArtilleryWeapon` 分类,再补 `ensureBasicRedComponents(...)` 模板。 diff --git a/auto-solution-rule/src/main/resources/rules/fire-rule.drl b/auto-solution-rule/src/main/resources/rules/fire-rule.drl index b2e793b..cb79be7 100644 --- a/auto-solution-rule/src/main/resources/rules/fire-rule.drl +++ b/auto-solution-rule/src/main/resources/rules/fire-rule.drl @@ -5,6 +5,8 @@ import com.solution.rule.domain.simplerulepojo.Task; import com.solution.rule.domain.simplerulepojo.Weapon; import com.solution.rule.domain.simplerulepojo.SubComponents; import com.solution.rule.domain.simplerulepojo.ComponentParam; +import com.solution.rule.domain.simplerulepojo.Coordinate; +import com.solution.rule.domain.simplerulepojo.TrackPoints; import java.util.List; import java.util.Map; @@ -160,6 +162,35 @@ function Map buildBusinessConfig() { cfg.put("taskDataType_artillery", "artillery"); cfg.put("taskDataType_general", "strike"); + // ---------- targetId 自动绑定(可改) ---------- + cfg.put("enableTargetAutoBind", Boolean.TRUE); // 是否自动给红方武器绑定蓝方目标 + cfg.put("minTargetBindRatio", "0.7"); // 最低绑定比例(大部分有目标) + cfg.put("allowReserveWithoutTarget", Boolean.TRUE); // 允许少量武器 targetId 为空(火力冗余) + + // ---------- 阵位规则参数(可改) ---------- + cfg.put("enablePositionRules", Boolean.TRUE); // 阵位规则总开关 + // 区域来源已切换到 Task 实体字段:warZoneLocation / defZoneLocation(4点经纬度) + cfg.put("fireUnitSpacingMeters", 100); // 火力单元间距(米) + cfg.put("airDeployZonePreference", "combat"); // 飞机优先部署区:combat/defense + cfg.put("defensePriorityWeapons", "反坦克导弹系统,反坦克火箭,车载迫击炮,迫榴炮"); // 优先部署防区武器 + cfg.put("groundDeployHeight", 20); // 地面武器部署高度 + cfg.put("airDeployHeight", 300); // 空中武器部署高度 + + // ---------- 航迹规则参数(可改) ---------- + cfg.put("enableTrajectoryRules", Boolean.TRUE); // 航迹规则总开关 + cfg.put("strategyMode", "auto"); // auto/shortest/flank/interfere + cfg.put("enableShortest", Boolean.TRUE); + cfg.put("enableFlank", Boolean.TRUE); + cfg.put("enableInterfere", Boolean.TRUE); + cfg.put("nearDefDistanceMeters", 800); // 近防区阈值 + cfg.put("farDefDistanceMeters", 2500); // 远防区阈值 + cfg.put("fastSpeedThreshold", 180); // 快速阈值 + cfg.put("flankOffsetMeters", 150); // 绕后偏移 + cfg.put("interfereOffsetMeters", 120); // 干扰基础偏移 + cfg.put("interfereZigzagAmplitude", 90); // 干扰锯齿幅度 + cfg.put("keepBlueHeight", Boolean.TRUE); // true=沿用蓝方高度 + cfg.put("redTrackHeightOverride", 200); // keepBlueHeight=false 时生效 + return cfg; } @@ -199,6 +230,40 @@ then assignTaskNameByRedWeapons($fact, cfg); end +//------------------------------------------------------------------------------- +rule "阵位规则-区域解析与点位生成" +agenda-group "打击任务" +salience 49 +when + $fact : FactTask(blueTask.side == "蓝方", redTask.side == "红方") +then + Map cfg = buildBusinessConfig(); + prepareDeploymentPools($fact, cfg, globalParams); +end + +//------------------------------------------------------------------------------- +rule "阵位规则-武器部署赋位" +agenda-group "打击任务" +salience 48 +when + $fact : FactTask(blueTask.side == "蓝方", redTask.side == "红方") +then + Map cfg = buildBusinessConfig(); + applyWeaponDeployment($fact, cfg, globalParams); +end + +//------------------------------------------------------------------------------- +rule "航迹规则-生成红方航迹" +agenda-group "打击任务" +salience 47 +when + // 根据蓝方 trackPoints 生成红方 trackPoints,点数保持一致 + $fact : FactTask(blueTask.side == "蓝方", redTask.side == "红方") +then + Map cfg = buildBusinessConfig(); + applyTrajectoryGeneration($fact, cfg); +end + //------------------------------------------------------------------------------- // 说明:以下函数全部是 DRL function(不是 Java 类方法) // 目标:让不懂代码的业务同事只改“可调整常量区”即可完成策略调整 @@ -304,6 +369,9 @@ function void configureRedWeaponsByBlue( if (matchedAny && hasBlueArtillery) { limitRedArtilleryToShellOnly(redWeapons, (String) cfg.get("shellRangeDefault")); } + + // 自动绑定红方武器 targetId(来源:蓝方 equipmentId) + bindTargetIdsForRedWeapons(redWeapons, blueWeapons, cfg); } function void applyMappedWeapons(List redWeapons, Map cfg, String mapKey, int defaultNum, boolean allowArmedHelicopter) { @@ -352,6 +420,25 @@ function List parseMappedWeaponNames(Map cfg, String mapKey) { return result; } +function List parseCsvList(String text) { + List result = new ArrayList(); + if (text == null || text.trim().equals("")) { + return result; + } + String[] parts = text.split(","); + for (int i = 0; i < parts.length; i++) { + String one = parts[i]; + if (one == null) { + continue; + } + String item = one.trim(); + if (!item.equals("") && !result.contains(item)) { + result.add(item); + } + } + return result; +} + function boolean isValidRedWeaponNameByConfig(Map cfg, String weaponName) { if (cfg == null || weaponName == null || weaponName.equals("")) { return false; @@ -425,6 +512,622 @@ function void applyMissileLinkage( } } +function void bindTargetIdsForRedWeapons(List redWeapons, List blueWeapons, Map cfg) { + if (!readBooleanCfg(cfg, "enableTargetAutoBind", true)) { + return; + } + if (redWeapons == null || redWeapons.isEmpty() || blueWeapons == null || blueWeapons.isEmpty()) { + return; + } + + Map pools = extractBlueTargetPools(blueWeapons); + Map cursor = new java.util.HashMap(); + int total = redWeapons.size(); + int bound = 0; + + // 第一轮:按武器类别优先匹配 + for (Object obj : redWeapons) { + Weapon redWeapon = (Weapon) obj; + if (redWeapon == null) { + continue; + } + if (!isBlank(redWeapon.getTargetId())) { + bound++; + continue; + } + String poolKey = inferBluePoolKeyForRedWeapon(redWeapon); + String targetId = pickTargetIdFromPools(pools, cursor, poolKey); + if (!isBlank(targetId)) { + redWeapon.setTargetId(targetId); + bound++; + } + } + + double minRatio = readDoubleCfg(cfg, "minTargetBindRatio", 0.7d); + boolean allowReserveWithoutTarget = readBooleanCfg(cfg, "allowReserveWithoutTarget", true); + double currentRatio = total <= 0 ? 1.0d : ((double) bound / (double) total); + if (currentRatio >= minRatio && allowReserveWithoutTarget) { + return; + } + + // 第二轮:若绑定率不足,回退到全目标池尽量补齐(仍允许复用目标) + for (Object obj : redWeapons) { + if (total > 0 && ((double) bound / (double) total) >= minRatio) { + break; + } + Weapon redWeapon = (Weapon) obj; + if (redWeapon == null || !isBlank(redWeapon.getTargetId())) { + continue; + } + String targetId = pickTargetIdFromPools(pools, cursor, "all"); + if (!isBlank(targetId)) { + redWeapon.setTargetId(targetId); + bound++; + } + } + + // 第三轮:若不允许空 targetId,最后强制从 all 池补齐(尽力而为) + if (!allowReserveWithoutTarget) { + for (Object obj : redWeapons) { + Weapon redWeapon = (Weapon) obj; + if (redWeapon == null || !isBlank(redWeapon.getTargetId())) { + continue; + } + String targetId = pickTargetIdFromPools(pools, cursor, "all"); + if (!isBlank(targetId)) { + redWeapon.setTargetId(targetId); + } + } + } +} + +function void prepareDeploymentPools(FactTask fact, Map cfg, Map runtime) { + if (!readBooleanCfg(cfg, "enablePositionRules", true)) { + return; + } + if (fact == null || runtime == null) { + return; + } + List combatPolygon = extractZonePolygonFromTask(fact, true); + List defensePolygon = extractZonePolygonFromTask(fact, false); + int spacing = readIntCfg(cfg, "fireUnitSpacingMeters", 100); + List combatPoints = buildGridPointsInPolygon(combatPolygon, spacing, readIntCfg(cfg, "groundDeployHeight", 20)); + List defensePoints = buildGridPointsInPolygon(defensePolygon, spacing, readIntCfg(cfg, "groundDeployHeight", 20)); + runtime.put("deploymentCombatPoints", combatPoints); + runtime.put("deploymentDefensePoints", defensePoints); +} + +function void applyWeaponDeployment(FactTask fact, Map cfg, Map runtime) { + if (!readBooleanCfg(cfg, "enablePositionRules", true)) { + return; + } + if (fact == null || fact.getRedTask() == null || fact.getRedTask().getTaskWeapons() == null || runtime == null) { + return; + } + List combatPoints = (List) runtime.get("deploymentCombatPoints"); + List defensePoints = (List) runtime.get("deploymentDefensePoints"); + if ((combatPoints == null || combatPoints.isEmpty()) && (defensePoints == null || defensePoints.isEmpty())) { + return; + } + Map cursor = new java.util.HashMap(); + List defensePriorityWeapons = parseCsvList((String) cfg.get("defensePriorityWeapons")); + String airPref = String.valueOf(cfg.get("airDeployZonePreference")); + int groundH = readIntCfg(cfg, "groundDeployHeight", 20); + int airH = readIntCfg(cfg, "airDeployHeight", 300); + + for (Object obj : fact.getRedTask().getTaskWeapons()) { + Weapon redWeapon = (Weapon) obj; + if (redWeapon == null) { + continue; + } + Coordinate selected = null; + if (isRedAirWeapon(redWeapon)) { + selected = pickCoordinateByPreference(combatPoints, defensePoints, cursor, airPref); + if (selected != null) { + selected = cloneCoordinateWithHeight(selected, airH); + } + } else if (defensePriorityWeapons.contains(redWeapon.getName())) { + selected = pickCoordinateByPreference(defensePoints, combatPoints, cursor, "defense"); + if (selected != null) { + selected = cloneCoordinateWithHeight(selected, groundH); + } + } else { + selected = pickCoordinateByPreference(combatPoints, defensePoints, cursor, "combat"); + if (selected != null) { + selected = cloneCoordinateWithHeight(selected, groundH); + } + } + if (selected != null) { + redWeapon.setCoordinate(selected); + } + } +} + +function void applyTrajectoryGeneration(FactTask fact, Map cfg) { + if (!readBooleanCfg(cfg, "enableTrajectoryRules", true)) { + return; + } + if (fact == null || fact.getBlueTask() == null || fact.getRedTask() == null) { + return; + } + Task blueTask = fact.getBlueTask(); + Task redTask = fact.getRedTask(); + List blueTrack = blueTask.getTrackPoints(); + if (blueTrack == null || blueTrack.isEmpty()) { + return; + } + List defZone = blueTask.getDefZoneLocation(); + if (defZone == null || defZone.size() < 3) { + return; + } + String strategy = chooseTrajectoryStrategy(blueTask, cfg); + Coordinate endPoint = findNearestDefPointToBlueTail(blueTask); + List redTrack = generateRedTrackPoints(blueTrack, strategy, cfg, endPoint); + if (redTrack != null && !redTrack.isEmpty()) { + redTask.setTrackPoints(redTrack); + } +} + +function String chooseTrajectoryStrategy(Task blueTask, Map cfg) { + String mode = String.valueOf(cfg.get("strategyMode")); + if (mode == null || mode.trim().equals("")) { + mode = "auto"; + } + mode = mode.trim().toLowerCase(); + if (!mode.equals("auto")) { + return fallbackToEnabledStrategy(mode, cfg); + } + double defMinDistance = computeDefMinDistanceMeters(blueTask); + int avgSpeed = computeAverageSpeed(blueTask.getTrackPoints()); + int near = readIntCfg(cfg, "nearDefDistanceMeters", 800); + int far = readIntCfg(cfg, "farDefDistanceMeters", 2500); + int fast = readIntCfg(cfg, "fastSpeedThreshold", 180); + String selected = "flank"; + if (avgSpeed >= fast && defMinDistance <= near) { + selected = "shortest"; + } else if (avgSpeed >= fast && defMinDistance >= far) { + selected = "interfere"; + } + return fallbackToEnabledStrategy(selected, cfg); +} + +function String fallbackToEnabledStrategy(String preferred, Map cfg) { + if (isStrategyEnabled(preferred, cfg)) { + return preferred; + } + if (isStrategyEnabled("shortest", cfg)) { + return "shortest"; + } + if (isStrategyEnabled("flank", cfg)) { + return "flank"; + } + if (isStrategyEnabled("interfere", cfg)) { + return "interfere"; + } + return "shortest"; +} + +function boolean isStrategyEnabled(String strategy, Map cfg) { + if (strategy == null) { + return false; + } + if (strategy.equals("shortest")) { + return readBooleanCfg(cfg, "enableShortest", true); + } + if (strategy.equals("flank")) { + return readBooleanCfg(cfg, "enableFlank", true); + } + if (strategy.equals("interfere")) { + return readBooleanCfg(cfg, "enableInterfere", true); + } + return false; +} + +function Coordinate findNearestDefPointToBlueTail(Task blueTask) { + if (blueTask == null || blueTask.getTrackPoints() == null || blueTask.getTrackPoints().isEmpty() || blueTask.getDefZoneLocation() == null || blueTask.getDefZoneLocation().isEmpty()) { + return null; + } + TrackPoints tail = (TrackPoints) blueTask.getTrackPoints().get(blueTask.getTrackPoints().size() - 1); + if (tail == null || tail.getLongitude() == null || tail.getLatitude() == null) { + return null; + } + Coordinate nearest = null; + double best = Double.MAX_VALUE; + for (Object obj : blueTask.getDefZoneLocation()) { + Coordinate c = (Coordinate) obj; + if (c == null || c.getLongitude() == null || c.getLatitude() == null) { + continue; + } + double d = approxDistanceMeters(tail.getLongitude().doubleValue(), tail.getLatitude().doubleValue(), c.getLongitude().doubleValue(), c.getLatitude().doubleValue()); + if (d < best) { + best = d; + nearest = c; + } + } + return nearest; +} + +function double computeDefMinDistanceMeters(Task blueTask) { + Coordinate nearest = findNearestDefPointToBlueTail(blueTask); + if (nearest == null || blueTask == null || blueTask.getTrackPoints() == null || blueTask.getTrackPoints().isEmpty()) { + return Double.MAX_VALUE; + } + TrackPoints tail = (TrackPoints) blueTask.getTrackPoints().get(blueTask.getTrackPoints().size() - 1); + return approxDistanceMeters( + tail.getLongitude().doubleValue(), + tail.getLatitude().doubleValue(), + nearest.getLongitude().doubleValue(), + nearest.getLatitude().doubleValue() + ); +} + +function int computeAverageSpeed(List trackPoints) { + if (trackPoints == null || trackPoints.isEmpty()) { + return 0; + } + int total = 0; + int count = 0; + for (Object obj : trackPoints) { + TrackPoints p = (TrackPoints) obj; + if (p == null || p.getSpeed() == null) { + continue; + } + total += p.getSpeed(); + count++; + } + if (count <= 0) { + return 0; + } + return total / count; +} + +function List generateRedTrackPoints(List blueTrackPoints, String strategy, Map cfg, Coordinate endPoint) { + List result = new ArrayList(); + if (blueTrackPoints == null || blueTrackPoints.isEmpty()) { + return result; + } + TrackPoints start = (TrackPoints) blueTrackPoints.get(0); + TrackPoints tail = (TrackPoints) blueTrackPoints.get(blueTrackPoints.size() - 1); + if (start == null || start.getLongitude() == null || start.getLatitude() == null) { + return result; + } + double sLon = start.getLongitude().doubleValue(); + double sLat = start.getLatitude().doubleValue(); + double eLon = (endPoint != null && endPoint.getLongitude() != null) ? endPoint.getLongitude().doubleValue() : (tail == null || tail.getLongitude() == null ? sLon : tail.getLongitude().doubleValue()); + double eLat = (endPoint != null && endPoint.getLatitude() != null) ? endPoint.getLatitude().doubleValue() : (tail == null || tail.getLatitude() == null ? sLat : tail.getLatitude().doubleValue()); + double dx = eLon - sLon; + double dy = eLat - sLat; + int n = blueTrackPoints.size(); + int flankOffset = readIntCfg(cfg, "flankOffsetMeters", 150); + int intBase = readIntCfg(cfg, "interfereOffsetMeters", 120); + int intAmp = readIntCfg(cfg, "interfereZigzagAmplitude", 90); + boolean keepBlueHeight = readBooleanCfg(cfg, "keepBlueHeight", true); + int redH = readIntCfg(cfg, "redTrackHeightOverride", 200); + + for (int i = 0; i < n; i++) { + double t = (n <= 1) ? 1.0d : ((double) i / (double) (n - 1)); + double baseLon = sLon + dx * t; + double baseLat = sLat + dy * t; + double offMeters = 0.0d; + if ("flank".equals(strategy)) { + offMeters = flankOffset * Math.sin(Math.PI * t); + } else if ("interfere".equals(strategy)) { + double zig = (i % 2 == 0 ? 1.0d : -1.0d) * intAmp; + offMeters = intBase * Math.sin(2.0d * Math.PI * t) + zig; + } + double latDeg = metersToLatDeg(offMeters); + double lonDeg = metersToLonDeg(offMeters, baseLat); + double norm = Math.sqrt(dx * dx + dy * dy); + if (norm < 1e-10) { + norm = 1e-10; + } + double nx = -dy / norm; + double ny = dx / norm; + double finalLon = baseLon + nx * lonDeg; + double finalLat = baseLat + ny * latDeg; + + TrackPoints blueP = (TrackPoints) blueTrackPoints.get(i); + TrackPoints redP = new TrackPoints(); + redP.setIndex(i); + redP.setLongitude(new java.math.BigDecimal(String.valueOf(finalLon))); + redP.setLatitude(new java.math.BigDecimal(String.valueOf(finalLat))); + redP.setSpeed(blueP == null || blueP.getSpeed() == null ? 0 : blueP.getSpeed()); + if (keepBlueHeight) { + redP.setHeight(blueP == null || blueP.getHeight() == null ? redH : blueP.getHeight()); + } else { + redP.setHeight(redH); + } + result.add(redP); + } + return result; +} + +function double metersToLatDeg(double meters) { + return meters / 111000.0d; +} + +function double metersToLonDeg(double meters, double latitudeDeg) { + double cos = Math.cos(Math.toRadians(latitudeDeg)); + if (Math.abs(cos) < 1e-6) { + cos = 1e-6; + } + return meters / (111000.0d * cos); +} + +function double approxDistanceMeters(double lon1, double lat1, double lon2, double lat2) { + double dx = (lon2 - lon1) * 111000.0d * Math.cos(Math.toRadians((lat1 + lat2) / 2.0d)); + double dy = (lat2 - lat1) * 111000.0d; + return Math.sqrt(dx * dx + dy * dy); +} + +function List extractZonePolygonFromTask(FactTask fact, boolean isCombat) { + // 输入约定:Task.warZoneLocation / defZoneLocation 传入 4 个经纬点(高度可空) + List result = new ArrayList(); + Task blueTask = fact == null ? null : fact.getBlueTask(); + if (blueTask == null) { + return result; + } + List source = isCombat ? blueTask.getWarZoneLocation() : blueTask.getDefZoneLocation(); + if (source == null || source.isEmpty()) { + return result; + } + for (Object oneObj : source) { + Coordinate one = (Coordinate) oneObj; + if (one == null || one.getLongitude() == null || one.getLatitude() == null) { + continue; + } + Coordinate c = new Coordinate(); + c.setLongitude(one.getLongitude()); + c.setLatitude(one.getLatitude()); + c.setHeight(one.getHeight()); + result.add(c); + } + return result; +} + +function List normalizeToCoordinateList(Object raw) { + List result = new ArrayList(); + if (raw == null) { + return result; + } + if (!(raw instanceof List)) { + return result; + } + List values = (List) raw; + for (Object obj : values) { + Coordinate c = toCoordinate(obj); + if (c != null) { + result.add(c); + } + } + return result; +} + +function Coordinate toCoordinate(Object obj) { + if (obj == null) { + return null; + } + if (obj instanceof Coordinate) { + return (Coordinate) obj; + } + if (obj instanceof Map) { + Map m = (Map) obj; + Object lon = m.get("longitude"); + Object lat = m.get("latitude"); + Object h = m.get("height"); + if (lon == null || lat == null) { + return null; + } + Coordinate c = new Coordinate(); + try { + c.setLongitude(new java.math.BigDecimal(String.valueOf(lon))); + c.setLatitude(new java.math.BigDecimal(String.valueOf(lat))); + c.setHeight(h == null ? 0 : parseIntSafe(String.valueOf(h), 0)); + return c; + } catch (Exception ex) { + return null; + } + } + return null; +} + +function List buildGridPointsInPolygon(List polygon, int spacingMeters, int defaultHeight) { + List points = new ArrayList(); + if (polygon == null || polygon.size() < 3) { + return points; + } + double step = ((double) spacingMeters) / 111000.0d; + if (step <= 0) { + step = 0.0009d; + } + double minLon = 180.0d; + double maxLon = -180.0d; + double minLat = 90.0d; + double maxLat = -90.0d; + for (Object cObj : polygon) { + Coordinate c = (Coordinate) cObj; + if (c == null || c.getLongitude() == null || c.getLatitude() == null) { + continue; + } + double lon = c.getLongitude().doubleValue(); + double lat = c.getLatitude().doubleValue(); + if (lon < minLon) minLon = lon; + if (lon > maxLon) maxLon = lon; + if (lat < minLat) minLat = lat; + if (lat > maxLat) maxLat = lat; + } + for (double lon = minLon; lon <= maxLon; lon += step) { + for (double lat = minLat; lat <= maxLat; lat += step) { + if (isPointInsidePolygon(lon, lat, polygon)) { + Coordinate c = new Coordinate(); + c.setLongitude(new java.math.BigDecimal(String.valueOf(lon))); + c.setLatitude(new java.math.BigDecimal(String.valueOf(lat))); + c.setHeight(defaultHeight); + points.add(c); + } + } + } + return points; +} + +function boolean isPointInsidePolygon(double x, double y, List polygon) { + if (polygon == null || polygon.size() < 3) { + return false; + } + boolean inside = false; + int n = polygon.size(); + int j = n - 1; + for (int i = 0; i < n; i++) { + Coordinate pi = (Coordinate) polygon.get(i); + Coordinate pj = (Coordinate) polygon.get(j); + if (pi == null || pj == null || pi.getLongitude() == null || pi.getLatitude() == null || pj.getLongitude() == null || pj.getLatitude() == null) { + j = i; + continue; + } + double xi = pi.getLongitude().doubleValue(); + double yi = pi.getLatitude().doubleValue(); + double xj = pj.getLongitude().doubleValue(); + double yj = pj.getLatitude().doubleValue(); + boolean intersect = ((yi > y) != (yj > y)) + && (x < (xj - xi) * (y - yi) / ((yj - yi) == 0 ? 1e-12 : (yj - yi)) + xi); + if (intersect) { + inside = !inside; + } + j = i; + } + return inside; +} + +function Coordinate pickCoordinateByPreference(List first, List second, Map cursor, String key) { + Coordinate c1 = pickCoordinateRoundRobin(first, cursor, "first_" + key); + if (c1 != null) { + return c1; + } + return pickCoordinateRoundRobin(second, cursor, "second_" + key); +} + +function Coordinate pickCoordinateRoundRobin(List values, Map cursor, String key) { + if (values == null || values.isEmpty()) { + return null; + } + Integer idxObj = (Integer) cursor.get(key); + int idx = idxObj == null ? 0 : idxObj.intValue(); + Coordinate value = (Coordinate) values.get(idx % values.size()); + cursor.put(key, idx + 1); + return value; +} + +function Coordinate cloneCoordinateWithHeight(Coordinate source, int height) { + if (source == null) { + return null; + } + Coordinate c = new Coordinate(); + c.setLongitude(source.getLongitude()); + c.setLatitude(source.getLatitude()); + c.setHeight(height); + return c; +} + +function Map extractBlueTargetPools(List blueWeapons) { + Map pools = new java.util.HashMap(); + pools.put("air", new ArrayList()); + pools.put("armor", new ArrayList()); + pools.put("artillery", new ArrayList()); + pools.put("ground", new ArrayList()); + pools.put("missile", new ArrayList()); + pools.put("all", new ArrayList()); + + for (Object obj : blueWeapons) { + Weapon blueWeapon = (Weapon) obj; + if (blueWeapon == null) { + continue; + } + String id = blueWeapon.getEquipmentId(); + if (isBlank(id)) { + continue; + } + addUnique((List) pools.get("all"), id); + if (isAirWeapon(blueWeapon)) { + addUnique((List) pools.get("air"), id); + } + if (isArmorWeapon(blueWeapon)) { + addUnique((List) pools.get("armor"), id); + } + if (isArtilleryWeapon(blueWeapon)) { + addUnique((List) pools.get("artillery"), id); + } + if (isGroundWeapon(blueWeapon)) { + addUnique((List) pools.get("ground"), id); + } + if (hasMissileComponent(blueWeapon)) { + addUnique((List) pools.get("missile"), id); + } + } + return pools; +} + +function String inferBluePoolKeyForRedWeapon(Weapon redWeapon) { + if (redWeapon == null || redWeapon.getName() == null) { + return "ground"; + } + String name = redWeapon.getName(); + if (name.contains("反坦克")) { + return "armor"; + } + if (name.contains("防空导弹") || name.contains("无人机") || name.contains("直升机")) { + return "air"; + } + if (name.contains("迫榴炮") || name.contains("迫击炮")) { + return "artillery"; + } + if (name.contains("导弹发射车")) { + return "missile"; + } + return "ground"; +} + +function String pickTargetIdFromPools(Map pools, Map cursor, String preferredKey) { + String fromPreferred = pickFromSinglePool(pools, cursor, preferredKey); + if (!isBlank(fromPreferred)) { + return fromPreferred; + } + if (!"ground".equals(preferredKey)) { + String fromGround = pickFromSinglePool(pools, cursor, "ground"); + if (!isBlank(fromGround)) { + return fromGround; + } + } + return pickFromSinglePool(pools, cursor, "all"); +} + +function String pickFromSinglePool(Map pools, Map cursor, String poolKey) { + if (pools == null || cursor == null || poolKey == null) { + return null; + } + List ids = (List) pools.get(poolKey); + if (ids == null || ids.isEmpty()) { + return null; + } + Integer idxObj = (Integer) cursor.get(poolKey); + int idx = idxObj == null ? 0 : idxObj.intValue(); + String id = (String) ids.get(idx % ids.size()); + cursor.put(poolKey, idx + 1); + return id; +} + +function void addUnique(List values, String value) { + if (values == null || isBlank(value)) { + return; + } + if (!containsString(values, value)) { + values.add(value); + } +} + +function boolean isBlank(String text) { + return text == null || text.trim().equals(""); +} + function void assignTaskNameByRedWeapons(FactTask fact, Map cfg) { if (fact == null || fact.getRedTask() == null) { return; @@ -809,6 +1512,21 @@ function boolean readBooleanCfg(Map cfg, String key, boolean fallback) { return "true".equalsIgnoreCase(text.trim()); } +function double readDoubleCfg(Map cfg, String key, double fallback) { + if (cfg == null || key == null) { + return fallback; + } + Object value = cfg.get(key); + if (value == null) { + return fallback; + } + try { + return Double.parseDouble(String.valueOf(value).trim()); + } catch (Exception ex) { + return fallback; + } +} + function boolean isRedAirWeapon(Weapon weapon) { if (weapon == null) { return false;