Fixed Dead Lock + Refractor of Compass Service.

This commit is contained in:
AlgorithmX2
2014-03-10 23:33:55 -05:00
parent d5748eb804
commit a61b1525f0
3 changed files with 155 additions and 249 deletions
-1
View File
@@ -37,7 +37,6 @@ public class WorldSettings extends Configuration
AEFolder = aeFolder; AEFolder = aeFolder;
compass = new CompassService( AEFolder ); compass = new CompassService( AEFolder );
(new Thread( compass, "AE Compass Service" )).start();
for (int dimID : get( "DimensionManager", "StorageCells", new int[0] ).getIntList()) for (int dimID : get( "DimensionManager", "StorageCells", new int[0] ).getIntList())
{ {
+28 -29
View File
@@ -1,10 +1,13 @@
package appeng.hooks; package appeng.hooks;
import java.util.Random; import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.chunk.IChunkProvider; import net.minecraft.world.chunk.IChunkProvider;
import appeng.api.util.DimensionalCoord; import appeng.api.util.DimensionalCoord;
import appeng.core.AELog;
import appeng.core.WorldSettings; import appeng.core.WorldSettings;
import appeng.helpers.MeteoritePlacer; import appeng.helpers.MeteoritePlacer;
import appeng.services.helpers.ICompassCallback; import appeng.services.helpers.ICompassCallback;
@@ -25,11 +28,6 @@ final public class MeteoriteWorldGen implements IWorldGenerator
distance = dist; distance = dist;
else else
distance = Double.MAX_VALUE; distance = Double.MAX_VALUE;
synchronized (this)
{
notify();
}
} }
}; };
@@ -43,37 +41,38 @@ final public class MeteoriteWorldGen implements IWorldGenerator
int z = r.nextInt( 16 ) + (chunkZ << 4); int z = r.nextInt( 16 ) + (chunkZ << 4);
myGen obj = new myGen(); myGen obj = new myGen();
WorldSettings.getInstance().getCompass().getCompassDirection( new DimensionalCoord( w, x, 128, z ), 70, obj ); Future<?> future = WorldSettings.getInstance().getCompass().getCompassDirection( new DimensionalCoord( w, x, 128, z ), 70, obj );
synchronized (obj) try
{ {
try
{
obj.wait();
} future.get();
catch (InterruptedException e)
if ( obj.distance > 1000 * 500 )
{ {
// meh int depth = 180 + r.nextInt( 20 );
return; for (int trys = 0; trys < 20; trys++)
{
MeteoritePlacer mp = new MeteoritePlacer();
if ( mp.spawnMeteorite( w, x, depth, z ) )
return;
depth -= 15;
if ( depth < 40 )
return;
}
} }
} }
catch (InterruptedException e)
if ( obj.distance > 1000 * 500 )
{ {
int depth = 180 + r.nextInt( 20 ); AELog.error( e );
for (int trys = 0; trys < 20; trys++) }
{ catch (ExecutionException e)
MeteoritePlacer mp = new MeteoritePlacer(); {
AELog.error( e );
if ( mp.spawnMeteorite( w, x, depth, z ) )
return;
depth -= 15;
if ( depth < 40 )
return;
}
} }
} }
+127 -219
View File
@@ -2,35 +2,23 @@ package appeng.services;
import java.io.File; import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.world.World; import net.minecraft.world.World;
import appeng.api.AEApi; import appeng.api.AEApi;
import appeng.api.util.DimensionalCoord; import appeng.api.util.DimensionalCoord;
import appeng.services.helpers.CompassException;
import appeng.services.helpers.CompassReader; import appeng.services.helpers.CompassReader;
import appeng.services.helpers.ICompassCallback; import appeng.services.helpers.ICompassCallback;
public class CompassService implements Runnable public class CompassService implements ThreadFactory
{ {
private class CompassMessage private class CMUpdatePost implements Runnable
{
public boolean isUpdate()
{
return false;
}
public boolean isRequest()
{
return false;
}
};
private class CMUpdatePost extends CompassMessage
{ {
public final World world; public final World world;
@@ -39,12 +27,6 @@ public class CompassService implements Runnable
public final int doubleChunkY; // 32 blocks instead of 16. public final int doubleChunkY; // 32 blocks instead of 16.
public final boolean value; public final boolean value;
@Override
public boolean isUpdate()
{
return true;
}
public CMUpdatePost(World w, int cx, int cz, int dcy, boolean val) { public CMUpdatePost(World w, int cx, int cz, int dcy, boolean val) {
world = w; world = w;
chunkX = cx; chunkX = cx;
@@ -52,32 +34,125 @@ public class CompassService implements Runnable
chunkZ = cz; chunkZ = cz;
value = val; value = val;
} }
@Override
public void run()
{
CompassReader cr = getReader( world );
cr.setHasBeacon( chunkX, chunkZ, doubleChunkY, value );
cr.close();
}
}; };
private class CMDirectionRequest extends CompassMessage private class CMDirectionRequest implements Runnable
{ {
public final int maxRange; public final int maxRange;
public final DimensionalCoord coord; public final DimensionalCoord coord;
public final ICompassCallback callback; public final ICompassCallback callback;
@Override
public boolean isRequest()
{
return true;
}
public CMDirectionRequest(DimensionalCoord coord, int getMaxRange, ICompassCallback cc) { public CMDirectionRequest(DimensionalCoord coord, int getMaxRange, ICompassCallback cc) {
this.coord = coord; this.coord = coord;
this.maxRange = getMaxRange; this.maxRange = getMaxRange;
callback = cc; callback = cc;
} }
@Override
public void run()
{
int cx = coord.x >> 4;
int cz = coord.z >> 4;
CompassReader cr = getReader( coord.getWorld() );
// Am I standing on it?
if ( cr.hasBeacon( cx, cz ) )
{
callback.calculatedDirection( true, true, -999, 0 );
return;
}
// spiral outward...
for (int offset = 1; offset < maxRange; offset++)
{
int minx = cx - offset;
int minz = cz - offset;
int maxx = cx + offset;
int maxz = cz + offset;
int closest = Integer.MAX_VALUE;
int chosen_x = cx;
int chosen_z = cz;
for (int z = minz; z <= maxz; z++)
{
if ( cr.hasBeacon( minx, z ) )
{
int closness = dist( cx, cz, minx, z );
if ( closness < closest )
{
closest = closness;
chosen_x = minx;
chosen_z = z;
}
}
if ( cr.hasBeacon( maxx, z ) )
{
int closness = dist( cx, cz, maxx, z );
if ( closness < closest )
{
closest = closness;
chosen_x = maxx;
chosen_z = z;
}
}
}
for (int x = minx + 1; x < maxx; x++)
{
if ( cr.hasBeacon( x, minz ) )
{
int closness = dist( cx, cz, x, minz );
if ( closness < closest )
{
closest = closness;
chosen_x = x;
chosen_z = minz;
}
}
if ( cr.hasBeacon( x, maxz ) )
{
int closness = dist( cx, cz, x, maxz );
if ( closness < closest )
{
closest = closness;
chosen_x = x;
chosen_z = maxz;
}
}
}
if ( closest < Integer.MAX_VALUE )
{
callback.calculatedDirection( true, false, rad( cx, cz, chosen_x, chosen_z ), dist( cx, cz, chosen_x, chosen_z ) );
return;
}
}
// didn't find shit...
callback.calculatedDirection( false, true, -999, 999 );
}
}; };
private LinkedList<CompassMessage> jobList = new LinkedList(); public Future<?> getCompassDirection(DimensionalCoord coord, int maxRange, ICompassCallback cc)
{
return executor.submit( new CMDirectionRequest( coord, maxRange, cc ) );
}
public void updateArea(World w, int x, int y, int z) public Future<?> updateArea(World w, int x, int y, int z)
{ {
int cx = x >> 4; int cx = x >> 4;
int cdy = y >> 5; int cdy = y >> 5;
@@ -102,67 +177,26 @@ public class CompassService implements Runnable
Block blk = w.getBlock( i, k, j ); Block blk = w.getBlock( i, k, j );
if ( blk == skystone && w.getBlockMetadata( i, k, j ) == 0 ) if ( blk == skystone && w.getBlockMetadata( i, k, j ) == 0 )
{ {
postJob( new CMUpdatePost( w, cx, cz, cdy, true ) ); return executor.submit( new CMUpdatePost( w, cx, cz, cdy, true ) );
return;
} }
} }
} }
} }
postJob( new CMUpdatePost( w, cx, cz, cdy, false ) ); return executor.submit( new CMUpdatePost( w, cx, cz, cdy, false ) );
} }
public void getCompassDirection(DimensionalCoord coord, int maxRange, ICompassCallback cc)
{
postJob( new CMDirectionRequest( coord, maxRange, cc ) );
}
private void postJob(CompassMessage msg)
{
synchronized (jobList)
{
if ( msg != null )
jobList.offer( msg );
jobList.notify();
}
}
private CompassMessage getNextMessage()
{
CompassMessage myMsg = null;
while (myMsg == null && run)
{
synchronized (jobList)
{
try
{
myMsg = jobList.poll();
overOberdened = jobList.isEmpty();
if ( myMsg == null )
jobList.wait();
}
catch (InterruptedException e)
{
// :P
}
}
}
return myMsg;
}
boolean overOberdened = false;
HashMap<World, CompassReader> worldSet = new HashMap(); HashMap<World, CompassReader> worldSet = new HashMap();
ExecutorService executor;
final File rootFolder; final File rootFolder;
public CompassService(File aEFolder) { public CompassService(File aEFolder) {
rootFolder = aEFolder; rootFolder = aEFolder;
executor = Executors.newSingleThreadExecutor( this );
} }
public CompassReader getReader(World w) private CompassReader getReader(World w)
{ {
CompassReader cr = worldSet.get( w ); CompassReader cr = worldSet.get( w );
@@ -175,97 +209,6 @@ public class CompassService implements Runnable
return cr; return cr;
} }
private void processRequest(CMDirectionRequest req)
{
int cx = req.coord.x >> 4;
int cz = req.coord.z >> 4;
CompassReader cr = getReader( req.coord.getWorld() );
// Am I standing on it?
if ( cr.hasBeacon( cx, cz ) )
{
req.callback.calculatedDirection( true, true, -999, 0 );
return;
}
// spiral outward...
for (int offset = 1; offset < req.maxRange; offset++)
{
int minx = cx - offset;
int minz = cz - offset;
int maxx = cx + offset;
int maxz = cz + offset;
int closest = Integer.MAX_VALUE;
int chosen_x = cx;
int chosen_z = cz;
for (int z = minz; z <= maxz; z++)
{
if ( cr.hasBeacon( minx, z ) )
{
int closness = dist( cx, cz, minx, z );
if ( closness < closest )
{
closest = closness;
chosen_x = minx;
chosen_z = z;
}
}
if ( cr.hasBeacon( maxx, z ) )
{
int closness = dist( cx, cz, maxx, z );
if ( closness < closest )
{
closest = closness;
chosen_x = maxx;
chosen_z = z;
}
}
}
for (int x = minx + 1; x < maxx; x++)
{
if ( cr.hasBeacon( x, minz ) )
{
int closness = dist( cx, cz, x, minz );
if ( closness < closest )
{
closest = closness;
chosen_x = x;
chosen_z = minz;
}
}
if ( cr.hasBeacon( x, maxz ) )
{
int closness = dist( cx, cz, x, maxz );
if ( closness < closest )
{
closest = closness;
chosen_x = x;
chosen_z = maxz;
}
}
}
if ( closest < Integer.MAX_VALUE )
{
req.callback.calculatedDirection( true, false, rad( cx, cz, chosen_x, chosen_z ), dist( cx, cz, chosen_x, chosen_z ) );
if ( !overOberdened )
cr.close();
return;
}
}
// didn't find shit...
req.callback.calculatedDirection( false, true, -999, 999 );
if ( !overOberdened )
cr.close();
}
private int dist(int ax, int az, int bx, int bz) private int dist(int ax, int az, int bx, int bz)
{ {
int up = (bz - az) * 16; int up = (bz - az) * 16;
@@ -282,58 +225,23 @@ public class CompassService implements Runnable
return Math.atan2( -up, side ) - Math.PI / 2.0; return Math.atan2( -up, side ) - Math.PI / 2.0;
} }
private void processUpdate(CMUpdatePost req)
{
CompassReader cr = getReader( req.world );
cr.setHasBeacon( req.chunkX, req.chunkZ, req.doubleChunkY, req.value );
cr.close();
}
boolean run = true;
boolean stopped = false;
@Override
public void run()
{
while (run)
{
CompassMessage myMsg = getNextMessage();
try
{
if ( myMsg != null )
{
if ( myMsg.isRequest() )
processRequest( (CMDirectionRequest) myMsg );
else if ( myMsg.isUpdate() )
processUpdate( (CMUpdatePost) myMsg );
}
}
catch (CompassException ce)
{
ce.inner.printStackTrace();
}
}
stopped = true;
}
public void kill() public void kill()
{ {
run = false; executor.shutdown();
postJob( null );
while (!stopped) try
{ {
try executor.awaitTermination( 6, TimeUnit.MINUTES );
{ }
Thread.sleep( 100 ); catch (InterruptedException e)
} {
catch (InterruptedException e) // wrap this up..
{
// :P
}
} }
} }
@Override
public Thread newThread(Runnable job)
{
return new Thread( job, "AE Compass Service" );
}
} }