火力规则:装备类型规则、目标规则实现、阵位规则、航迹规则
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user