2014-11-14 12:02:52 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
2015-05-28 20:01:21 +02:00
|
|
|
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
|
2014-11-14 12:02:52 +01:00
|
|
|
*
|
|
|
|
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
|
|
|
*/
|
|
|
|
|
2014-03-02 09:35:11 +01:00
|
|
|
package appeng.services;
|
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
|
2014-03-02 09:35:11 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.util.HashMap;
|
2015-01-03 02:53:14 +01:00
|
|
|
import java.util.Map;
|
2014-03-11 05:33:55 +01:00
|
|
|
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;
|
2015-05-02 15:39:59 +02:00
|
|
|
import javax.annotation.Nonnull;
|
2014-03-02 09:35:11 +01:00
|
|
|
|
2015-05-28 20:01:21 +02:00
|
|
|
import com.google.common.base.Preconditions;
|
|
|
|
|
2014-03-02 09:35:11 +01:00
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.world.World;
|
2014-08-09 03:45:31 +02:00
|
|
|
import net.minecraft.world.chunk.Chunk;
|
2015-08-19 19:49:56 +02:00
|
|
|
import net.minecraftforge.event.world.WorldEvent;
|
|
|
|
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
|
|
|
|
2014-03-02 09:35:11 +01:00
|
|
|
import appeng.api.AEApi;
|
|
|
|
import appeng.api.util.DimensionalCoord;
|
2015-03-09 17:35:19 +01:00
|
|
|
import appeng.services.compass.CompassReader;
|
|
|
|
import appeng.services.compass.ICompassCallback;
|
2015-08-19 19:49:56 +02:00
|
|
|
import appeng.util.Platform;
|
2014-03-02 09:35:11 +01:00
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
|
2015-05-28 20:01:21 +02:00
|
|
|
public final class CompassService
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
2015-01-03 02:53:14 +01:00
|
|
|
private static final int CHUNK_SIZE = 16;
|
2015-05-28 20:01:21 +02:00
|
|
|
|
|
|
|
private final Map<World, CompassReader> worldSet = new HashMap<World, CompassReader>( 10 );
|
2015-01-03 02:53:14 +01:00
|
|
|
private final ExecutorService executor;
|
|
|
|
|
2015-05-28 20:01:21 +02:00
|
|
|
/**
|
|
|
|
* AE2 Folder for each world
|
|
|
|
*/
|
|
|
|
private final File worldCompassFolder;
|
|
|
|
|
|
|
|
private int jobSize;
|
|
|
|
|
|
|
|
public CompassService( @Nonnull final File worldCompassFolder, @Nonnull final ThreadFactory factory )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
2015-05-28 20:01:21 +02:00
|
|
|
Preconditions.checkNotNull( worldCompassFolder );
|
|
|
|
|
|
|
|
this.worldCompassFolder = worldCompassFolder;
|
|
|
|
this.executor = Executors.newSingleThreadExecutor( factory );
|
2015-01-03 02:53:14 +01:00
|
|
|
this.jobSize = 0;
|
|
|
|
}
|
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
public Future<?> getCompassDirection( final DimensionalCoord coord, final int maxRange, final ICompassCallback cc )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
|
|
|
this.jobSize++;
|
|
|
|
return this.executor.submit( new CMDirectionRequest( coord, maxRange, cc ) );
|
|
|
|
}
|
|
|
|
|
2015-08-19 19:49:56 +02:00
|
|
|
/**
|
|
|
|
* Ensure the a compass service is removed once a world gets unloaded by forge.
|
|
|
|
*
|
|
|
|
* @param event the event containing the unloaded world.
|
|
|
|
*/
|
|
|
|
@SubscribeEvent
|
2015-09-30 14:24:40 +02:00
|
|
|
public void unloadWorld( final WorldEvent.Unload event )
|
2015-08-19 19:49:56 +02:00
|
|
|
{
|
|
|
|
if( Platform.isServer() && this.worldSet.containsKey( event.world ) )
|
|
|
|
{
|
|
|
|
final CompassReader compassReader = this.worldSet.remove( event.world );
|
|
|
|
|
|
|
|
compassReader.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
public int jobSize()
|
|
|
|
{
|
|
|
|
return this.jobSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cleanUp()
|
|
|
|
{
|
2015-08-19 19:49:56 +02:00
|
|
|
for( final CompassReader cr : this.worldSet.values() )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-01-03 02:53:14 +01:00
|
|
|
cr.close();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-01-03 02:53:14 +01:00
|
|
|
}
|
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
public void updateArea( final World w, final int chunkX, final int chunkZ )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
2015-08-19 19:49:56 +02:00
|
|
|
final int x = chunkX << 4;
|
|
|
|
final int z = chunkZ << 4;
|
2015-01-03 02:53:14 +01:00
|
|
|
|
|
|
|
this.updateArea( w, x, CHUNK_SIZE, z );
|
|
|
|
this.updateArea( w, x, CHUNK_SIZE + 32, z );
|
|
|
|
this.updateArea( w, x, CHUNK_SIZE + 64, z );
|
|
|
|
this.updateArea( w, x, CHUNK_SIZE + 96, z );
|
|
|
|
|
|
|
|
this.updateArea( w, x, CHUNK_SIZE + 128, z );
|
|
|
|
this.updateArea( w, x, CHUNK_SIZE + 160, z );
|
|
|
|
this.updateArea( w, x, CHUNK_SIZE + 192, z );
|
|
|
|
this.updateArea( w, x, CHUNK_SIZE + 224, z );
|
|
|
|
}
|
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
public Future<?> updateArea( final World w, final int x, final int y, final int z )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
|
|
|
this.jobSize++;
|
|
|
|
|
2015-08-19 19:49:56 +02:00
|
|
|
final int cx = x >> 4;
|
|
|
|
final int cdy = y >> 5;
|
|
|
|
final int cz = z >> 4;
|
2015-01-03 02:53:14 +01:00
|
|
|
|
2015-08-19 19:49:56 +02:00
|
|
|
final int low_y = cdy << 5;
|
|
|
|
final int hi_y = low_y + 32;
|
2015-01-03 02:53:14 +01:00
|
|
|
|
|
|
|
// lower level...
|
2015-08-19 19:49:56 +02:00
|
|
|
final Chunk c = w.getChunkFromChunkCoords( cx, cz );
|
2015-01-03 02:53:14 +01:00
|
|
|
|
2015-08-19 19:49:56 +02:00
|
|
|
for( final Block skyStoneBlock : AEApi.instance().definitions().blocks().skyStone().maybeBlock().asSet() )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int i = 0; i < CHUNK_SIZE; i++ )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int j = 0; j < CHUNK_SIZE; j++ )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int k = low_y; k < hi_y; k++ )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
final Block blk = c.getBlock( i, k, j );
|
2015-06-16 02:44:59 +02:00
|
|
|
if( blk == skyStoneBlock )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
|
|
|
return this.executor.submit( new CMUpdatePost( w, cx, cz, cdy, true ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.executor.submit( new CMUpdatePost( w, cx, cz, cdy, false ) );
|
|
|
|
}
|
|
|
|
|
2015-08-19 19:49:56 +02:00
|
|
|
public void kill()
|
|
|
|
{
|
|
|
|
this.executor.shutdown();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
this.executor.awaitTermination( 6, TimeUnit.MINUTES );
|
|
|
|
this.jobSize = 0;
|
|
|
|
|
|
|
|
for( final CompassReader cr : this.worldSet.values() )
|
|
|
|
{
|
|
|
|
cr.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.worldSet.clear();
|
|
|
|
}
|
|
|
|
catch( final InterruptedException e )
|
|
|
|
{
|
|
|
|
// wrap this up..
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
private CompassReader getReader( final World w )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
|
|
|
CompassReader cr = this.worldSet.get( w );
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( cr == null )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
2015-05-28 20:01:21 +02:00
|
|
|
cr = new CompassReader( w.provider.getDimensionId(), this.worldCompassFolder );
|
2015-01-03 02:53:14 +01:00
|
|
|
this.worldSet.put( w, cr );
|
|
|
|
}
|
|
|
|
|
|
|
|
return cr;
|
|
|
|
}
|
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
private int dist( final int ax, final int az, final int bx, final int bz )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
2015-08-19 19:49:56 +02:00
|
|
|
final int up = ( bz - az ) * CHUNK_SIZE;
|
|
|
|
final int side = ( bx - ax ) * CHUNK_SIZE;
|
2015-01-03 02:53:14 +01:00
|
|
|
|
|
|
|
return up * up + side * side;
|
|
|
|
}
|
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
private double rad( final int ax, final int az, final int bx, final int bz )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
2015-08-19 19:49:56 +02:00
|
|
|
final int up = bz - az;
|
|
|
|
final int side = bx - ax;
|
2015-01-03 02:53:14 +01:00
|
|
|
|
|
|
|
return Math.atan2( -up, side ) - Math.PI / 2.0;
|
|
|
|
}
|
|
|
|
|
2014-03-11 05:33:55 +01:00
|
|
|
private class CMUpdatePost implements Runnable
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
public final World world;
|
|
|
|
|
2015-03-26 12:13:34 +01:00
|
|
|
public final int chunkX;
|
|
|
|
public final int chunkZ;
|
2014-03-02 09:35:11 +01:00
|
|
|
public final int doubleChunkY; // 32 blocks instead of 16.
|
|
|
|
public final boolean value;
|
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
public CMUpdatePost( final World w, final int cx, final int cz, final int dcy, final boolean val )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.world = w;
|
|
|
|
this.chunkX = cx;
|
|
|
|
this.doubleChunkY = dcy;
|
|
|
|
this.chunkZ = cz;
|
|
|
|
this.value = val;
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|
2014-03-11 05:33:55 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
CompassService.this.jobSize--;
|
2014-08-07 08:47:42 +02:00
|
|
|
|
2015-08-19 19:49:56 +02:00
|
|
|
final CompassReader cr = CompassService.this.getReader( this.world );
|
2014-12-29 15:13:47 +01:00
|
|
|
cr.setHasBeacon( this.chunkX, this.chunkZ, this.doubleChunkY, this.value );
|
2014-08-07 08:47:42 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( CompassService.this.jobSize() < 2 )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
CompassService.this.cleanUp();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-03-11 05:33:55 +01:00
|
|
|
}
|
2014-09-28 00:50:06 +02:00
|
|
|
}
|
2014-03-02 09:35:11 +01:00
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
|
2014-03-11 05:33:55 +01:00
|
|
|
private class CMDirectionRequest implements Runnable
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
|
|
|
|
2014-03-03 00:18:15 +01:00
|
|
|
public final int maxRange;
|
2014-03-05 04:12:23 +01:00
|
|
|
public final DimensionalCoord coord;
|
2014-03-02 09:35:11 +01:00
|
|
|
public final ICompassCallback callback;
|
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
public CMDirectionRequest( final DimensionalCoord coord, final int getMaxRange, final ICompassCallback cc )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
2014-03-02 09:35:11 +01:00
|
|
|
this.coord = coord;
|
2014-03-03 00:18:15 +01:00
|
|
|
this.maxRange = getMaxRange;
|
2014-12-29 15:13:47 +01:00
|
|
|
this.callback = cc;
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|
|
|
|
|
2014-03-11 05:33:55 +01:00
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
CompassService.this.jobSize--;
|
2014-08-07 08:47:42 +02:00
|
|
|
|
2015-08-19 19:49:56 +02:00
|
|
|
final int cx = this.coord.x >> 4;
|
|
|
|
final int cz = this.coord.z >> 4;
|
2014-03-11 05:33:55 +01:00
|
|
|
|
2015-08-19 19:49:56 +02:00
|
|
|
final CompassReader cr = CompassService.this.getReader( this.coord.getWorld() );
|
2014-03-11 05:33:55 +01:00
|
|
|
|
|
|
|
// Am I standing on it?
|
2015-04-03 08:54:31 +02:00
|
|
|
if( cr.hasBeacon( cx, cz ) )
|
2014-03-11 05:33:55 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.callback.calculatedDirection( true, true, -999, 0 );
|
2014-08-07 08:47:42 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( CompassService.this.jobSize() < 2 )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
CompassService.this.cleanUp();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-08-07 08:47:42 +02:00
|
|
|
|
2014-03-11 05:33:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// spiral outward...
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int offset = 1; offset < this.maxRange; offset++ )
|
2014-03-11 05:33:55 +01:00
|
|
|
{
|
2015-08-19 19:49:56 +02:00
|
|
|
final int minX = cx - offset;
|
|
|
|
final int minZ = cz - offset;
|
|
|
|
final int maxX = cx + offset;
|
|
|
|
final int maxZ = cz + offset;
|
2014-03-11 05:33:55 +01:00
|
|
|
|
|
|
|
int closest = Integer.MAX_VALUE;
|
|
|
|
int chosen_x = cx;
|
|
|
|
int chosen_z = cz;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int z = minZ; z <= maxZ; z++ )
|
2014-03-11 05:33:55 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( cr.hasBeacon( minX, z ) )
|
2014-03-11 05:33:55 +01:00
|
|
|
{
|
2015-08-19 19:49:56 +02:00
|
|
|
final int closeness = CompassService.this.dist( cx, cz, minX, z );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( closeness < closest )
|
2014-03-11 05:33:55 +01:00
|
|
|
{
|
2014-09-21 01:05:13 +02:00
|
|
|
closest = closeness;
|
2014-09-28 11:47:17 +02:00
|
|
|
chosen_x = minX;
|
2014-03-11 05:33:55 +01:00
|
|
|
chosen_z = z;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( cr.hasBeacon( maxX, z ) )
|
2014-03-11 05:33:55 +01:00
|
|
|
{
|
2015-08-19 19:49:56 +02:00
|
|
|
final int closeness = CompassService.this.dist( cx, cz, maxX, z );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( closeness < closest )
|
2014-03-11 05:33:55 +01:00
|
|
|
{
|
2014-09-21 01:05:13 +02:00
|
|
|
closest = closeness;
|
2014-09-28 11:47:17 +02:00
|
|
|
chosen_x = maxX;
|
2014-03-11 05:33:55 +01:00
|
|
|
chosen_z = z;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int x = minX + 1; x < maxX; x++ )
|
2014-03-11 05:33:55 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( cr.hasBeacon( x, minZ ) )
|
2014-03-11 05:33:55 +01:00
|
|
|
{
|
2015-08-19 19:49:56 +02:00
|
|
|
final int closeness = CompassService.this.dist( cx, cz, x, minZ );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( closeness < closest )
|
2014-03-11 05:33:55 +01:00
|
|
|
{
|
2014-09-21 01:05:13 +02:00
|
|
|
closest = closeness;
|
2014-03-11 05:33:55 +01:00
|
|
|
chosen_x = x;
|
2014-09-28 11:47:17 +02:00
|
|
|
chosen_z = minZ;
|
2014-03-11 05:33:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( cr.hasBeacon( x, maxZ ) )
|
2014-03-11 05:33:55 +01:00
|
|
|
{
|
2015-08-19 19:49:56 +02:00
|
|
|
final int closeness = CompassService.this.dist( cx, cz, x, maxZ );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( closeness < closest )
|
2014-03-11 05:33:55 +01:00
|
|
|
{
|
2014-09-21 01:05:13 +02:00
|
|
|
closest = closeness;
|
2014-03-11 05:33:55 +01:00
|
|
|
chosen_x = x;
|
2014-09-28 11:47:17 +02:00
|
|
|
chosen_z = maxZ;
|
2014-03-11 05:33:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( closest < Integer.MAX_VALUE )
|
2014-03-11 05:33:55 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.callback.calculatedDirection( true, false, CompassService.this.rad( cx, cz, chosen_x, chosen_z ), CompassService.this.dist( cx, cz, chosen_x, chosen_z ) );
|
2014-08-07 08:47:42 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( CompassService.this.jobSize() < 2 )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
CompassService.this.cleanUp();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-08-07 08:47:42 +02:00
|
|
|
|
2014-03-11 05:33:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// didn't find shit...
|
2014-12-29 15:13:47 +01:00
|
|
|
this.callback.calculatedDirection( false, true, -999, 999 );
|
2014-08-07 08:47:42 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( CompassService.this.jobSize() < 2 )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
CompassService.this.cleanUp();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-03-11 05:33:55 +01:00
|
|
|
}
|
2014-09-28 00:50:06 +02:00
|
|
|
}
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|