Skip to content

Commit

Permalink
优化寻路,增加总运算时间限制
Browse files Browse the repository at this point in the history
  • Loading branch information
way-zer committed Jan 31, 2024
1 parent cf96f47 commit 9ab976a
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Date: Fri, 23 Jun 2023 16:21:59 +0800
Subject: [PATCH] SO: improve ControlPathfinder.java (limit memory use)

---
core/src/mindustry/ai/ControlPathfinder.java | 21 +++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)
core/src/mindustry/ai/ControlPathfinder.java | 23 +++++++++++++++++---
1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/core/src/mindustry/ai/ControlPathfinder.java b/core/src/mindustry/ai/ControlPathfinder.java
index 4ff9f7e384f50411470de468170f8b02db2af824..2a3ce7bf1d9f327cb866ab349d97d9e613f29f11 100644
index 4ff9f7e384f50411470de468170f8b02db2af824..8fa6039a97917c683016ba73dce61ccf1a8c272a 100644
--- a/core/src/mindustry/ai/ControlPathfinder.java
+++ b/core/src/mindustry/ai/ControlPathfinder.java
@@ -13,6 +13,10 @@ import mindustry.game.*;
Expand All @@ -27,7 +27,7 @@ index 4ff9f7e384f50411470de468170f8b02db2af824..2a3ce7bf1d9f327cb866ab349d97d9e6
//TODO this FPS-based update system could be flawed.
private static final long maxUpdate = Time.millisToNanos(30);
+ @MindustryXApi
+ public static int maxWorking = 20;
+ public static int maxWorking = 24;
private static final int updateFPS = 60;
private static final int updateInterval = 1000 / updateFPS;
private static final int wallImpassableCap = 1_000_000;
Expand Down Expand Up @@ -59,19 +59,21 @@ index 4ff9f7e384f50411470de468170f8b02db2af824..2a3ce7bf1d9f327cb866ab349d97d9e6
}

private static boolean raycast(int team, PathCost type, int x1, int y1, int x2, int y2){
@@ -463,9 +474,13 @@ public class ControlPathfinder{
@@ -463,9 +474,15 @@ public class ControlPathfinder{
requestSize = requests.size;

//total update time no longer than maxUpdate
+ //MDTX: changed
+ var count = Math.min(requestSize, maxWorking);
+ var count = Math.min(requestSize, maxWorking / controlPath.threads.length);
+ long ns = Time.nanos();
for(var req : requests){
- //TODO this is flawed with many paths
- req.update(maxUpdate / requests.size);
+ if(!req.done && !workingRequests.containsKey(req) && workingRequests.size() > maxWorking) continue;
+ req.update(maxUpdate / count);
+ if(req.done) workingRequests.remove(req);
+ else workingRequests.put(req, this);
+ if(Time.nanos() - ns > maxUpdate) break;
}
}

0 comments on commit 9ab976a

Please sign in to comment.