From de0ad48a2d9537d621ba0cf0715fd78f0534b562 Mon Sep 17 00:00:00 2001 From: Calclavia Date: Mon, 5 Aug 2013 17:20:08 -0400 Subject: [PATCH] Added EMC Contractor multithreading --- src/resonantinduction/ResonantInduction.java | 2 + .../contractor/ThreadPathfinding.java | 47 +++++++++++++++++++ .../contractor/TileEntityEMContractor.java | 37 +++++++++++---- 3 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 src/resonantinduction/contractor/ThreadPathfinding.java diff --git a/src/resonantinduction/ResonantInduction.java b/src/resonantinduction/ResonantInduction.java index 55caa518..5b5f345e 100644 --- a/src/resonantinduction/ResonantInduction.java +++ b/src/resonantinduction/ResonantInduction.java @@ -96,6 +96,7 @@ public class ResonantInduction private static final int BLOCK_ID_PREFIX = 3200; /** Item ID by Horfius */ private static final int ITEM_ID_PREFIX = 20150; + public static int MAX_CONTRACTOR_DISTANCE = 200; private static int NEXT_BLOCK_ID = BLOCK_ID_PREFIX; private static int NEXT_ITEM_ID = ITEM_ID_PREFIX; @@ -132,6 +133,7 @@ public class ResonantInduction // Config POWER_PER_COAL = (float) CONFIGURATION.get(Configuration.CATEGORY_GENERAL, "Coal Wattage", POWER_PER_COAL).getDouble(POWER_PER_COAL); SOUND_FXS = CONFIGURATION.get(Configuration.CATEGORY_GENERAL, "Tesla Sound FXs", SOUND_FXS).getBoolean(SOUND_FXS); + MAX_CONTRACTOR_DISTANCE = CONFIGURATION.get(Configuration.CATEGORY_GENERAL, "Max EM Contractor Path", MAX_CONTRACTOR_DISTANCE).getInt(MAX_CONTRACTOR_DISTANCE); TileEntityEMContractor.ACCELERATION = CONFIGURATION.get(Configuration.CATEGORY_GENERAL, "Contractor Item Acceleration", TileEntityEMContractor.ACCELERATION).getDouble(TileEntityEMContractor.ACCELERATION); TileEntityEMContractor.MAX_REACH = CONFIGURATION.get(Configuration.CATEGORY_GENERAL, "Contractor Max Item Reach", TileEntityEMContractor.MAX_REACH).getInt(TileEntityEMContractor.MAX_REACH); diff --git a/src/resonantinduction/contractor/ThreadPathfinding.java b/src/resonantinduction/contractor/ThreadPathfinding.java new file mode 100644 index 00000000..5f95d122 --- /dev/null +++ b/src/resonantinduction/contractor/ThreadPathfinding.java @@ -0,0 +1,47 @@ +/** + * + */ +package resonantinduction.contractor; + +import resonantinduction.base.Vector3; + +/** + * @author Calclavia + * + */ +public class ThreadPathfinding extends Thread +{ + private boolean isCompleted = false; + private PathfinderEMContractor pathfinder; + private Vector3 start; + + public ThreadPathfinding(PathfinderEMContractor pathfinder, Vector3 start) + { + this.pathfinder = pathfinder; + this.start = start; + this.setPriority(Thread.MIN_PRIORITY); + } + + public ThreadPathfinding find() + { + this.run(); + return this; + } + + @Override + public void run() + { + this.pathfinder.find(this.start); + this.isCompleted = true; + } + + public PathfinderEMContractor getPath() + { + if (this.isCompleted) + { + return this.pathfinder; + } + + return null; + } +} diff --git a/src/resonantinduction/contractor/TileEntityEMContractor.java b/src/resonantinduction/contractor/TileEntityEMContractor.java index d39b12ce..188f201c 100644 --- a/src/resonantinduction/contractor/TileEntityEMContractor.java +++ b/src/resonantinduction/contractor/TileEntityEMContractor.java @@ -50,6 +50,10 @@ public class TileEntityEMContractor extends TileEntityBase implements IPacketRec */ public boolean suck = true; + /** + * Pathfinding + */ + private ThreadPathfinding thread; private PathfinderEMContractor pathfinder; private Set pathfindingTrackers = new HashSet(); private TileEntityEMContractor linked; @@ -124,6 +128,17 @@ public class TileEntityEMContractor extends TileEntityBase implements IPacketRec } } + if (this.thread != null) + { + PathfinderEMContractor newPath = this.thread.getPath(); + + if (newPath != null) + { + this.pathfinder = newPath; + this.thread = null; + } + } + if (!this.suck) { if (this.linked != null && !this.linked.isInvalid()) @@ -514,17 +529,21 @@ public class TileEntityEMContractor extends TileEntityBase implements IPacketRec public void updatePath() { - this.pathfinder = null; - - if (this.linked != null) + if (this.thread == null) { - Vector3 start = new Vector3(this).translate(new Vector3(this.getDirection())); - Vector3 target = new Vector3(this.linked).translate(new Vector3(this.linked.getDirection())); - - if (TileEntityEMContractor.canBePath(this.worldObj, start, new Vector3(this.linked)) && TileEntityEMContractor.canBePath(this.worldObj, target, new Vector3(this.linked))) + if (this.linked != null) { - this.pathfinder = new PathfinderEMContractor(this.worldObj, target); - this.pathfinder.find(start); + Vector3 start = new Vector3(this).translate(new Vector3(this.getDirection())); + Vector3 target = new Vector3(this.linked).translate(new Vector3(this.linked.getDirection())); + + if (start.distance(target) < ResonantInduction.MAX_CONTRACTOR_DISTANCE) + { + if (TileEntityEMContractor.canBePath(this.worldObj, start, new Vector3(this.linked)) && TileEntityEMContractor.canBePath(this.worldObj, target, new Vector3(this.linked))) + { + this.thread = new ThreadPathfinding(new PathfinderEMContractor(this.worldObj, target), start); + this.thread.start(); + } + } } } }