Applied-Energistics-2-tiler.../src/main/java/appeng/services/CompassService.java

323 lines
7.4 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* 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>.
*/
package appeng.services;
import java.io.File;
import java.util.HashMap;
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.world.World;
2014-08-09 03:45:31 +02:00
import net.minecraft.world.chunk.Chunk;
import appeng.api.AEApi;
import appeng.api.util.DimensionalCoord;
import appeng.services.helpers.CompassReader;
import appeng.services.helpers.ICompassCallback;
public class CompassService implements ThreadFactory
{
2014-08-07 08:47:42 +02:00
int jobSize = 0;
private class CMUpdatePost implements Runnable
{
public final World world;
public final int chunkX, chunkZ;
public final int doubleChunkY; // 32 blocks instead of 16.
public final boolean value;
public CMUpdatePost(World w, int cx, int cz, int dcy, boolean val) {
2014-12-29 15:13:47 +01:00
this.world = w;
this.chunkX = cx;
this.doubleChunkY = dcy;
this.chunkZ = cz;
this.value = val;
}
@Override
public void run()
{
2014-12-29 15:13:47 +01:00
CompassService.this.jobSize--;
2014-08-07 08:47:42 +02:00
2014-12-29 15:13:47 +01:00
CompassReader cr = CompassService.this.getReader( this.world );
cr.setHasBeacon( this.chunkX, this.chunkZ, this.doubleChunkY, this.value );
2014-08-07 08:47:42 +02:00
2014-12-29 15:13:47 +01:00
if ( CompassService.this.jobSize() < 2 )
CompassService.this.cleanUp();
}
2014-09-28 00:50:06 +02:00
}
private class CMDirectionRequest implements Runnable
{
public final int maxRange;
public final DimensionalCoord coord;
public final ICompassCallback callback;
public CMDirectionRequest(DimensionalCoord coord, int getMaxRange, ICompassCallback cc) {
this.coord = coord;
this.maxRange = getMaxRange;
2014-12-29 15:13:47 +01:00
this.callback = cc;
}
@Override
public void run()
{
2014-12-29 15:13:47 +01:00
CompassService.this.jobSize--;
2014-08-07 08:47:42 +02:00
2014-12-29 15:13:47 +01:00
int cx = this.coord.x >> 4;
int cz = this.coord.z >> 4;
2014-12-29 15:13:47 +01:00
CompassReader cr = CompassService.this.getReader( this.coord.getWorld() );
// Am I standing on it?
if ( cr.hasBeacon( cx, cz ) )
{
2014-12-29 15:13:47 +01:00
this.callback.calculatedDirection( true, true, -999, 0 );
2014-08-07 08:47:42 +02:00
2014-12-29 15:13:47 +01:00
if ( CompassService.this.jobSize() < 2 )
CompassService.this.cleanUp();
2014-08-07 08:47:42 +02:00
return;
}
// spiral outward...
2014-12-29 15:13:47 +01:00
for (int offset = 1; offset < this.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 ) )
{
2014-12-29 15:13:47 +01:00
int closeness = CompassService.this.dist( cx, cz, minX, z );
2014-09-21 01:05:13 +02:00
if ( closeness < closest )
{
2014-09-21 01:05:13 +02:00
closest = closeness;
chosen_x = minX;
chosen_z = z;
}
}
if ( cr.hasBeacon( maxX, z ) )
{
2014-12-29 15:13:47 +01:00
int closeness = CompassService.this.dist( cx, cz, maxX, z );
2014-09-21 01:05:13 +02:00
if ( closeness < closest )
{
2014-09-21 01:05:13 +02:00
closest = closeness;
chosen_x = maxX;
chosen_z = z;
}
}
}
for (int x = minX + 1; x < maxX; x++)
{
if ( cr.hasBeacon( x, minZ ) )
{
2014-12-29 15:13:47 +01:00
int closeness = CompassService.this.dist( cx, cz, x, minZ );
2014-09-21 01:05:13 +02:00
if ( closeness < closest )
{
2014-09-21 01:05:13 +02:00
closest = closeness;
chosen_x = x;
chosen_z = minZ;
}
}
if ( cr.hasBeacon( x, maxZ ) )
{
2014-12-29 15:13:47 +01:00
int closeness = CompassService.this.dist( cx, cz, x, maxZ );
2014-09-21 01:05:13 +02:00
if ( closeness < closest )
{
2014-09-21 01:05:13 +02:00
closest = closeness;
chosen_x = x;
chosen_z = maxZ;
}
}
}
if ( closest < Integer.MAX_VALUE )
{
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
2014-12-29 15:13:47 +01:00
if ( CompassService.this.jobSize() < 2 )
CompassService.this.cleanUp();
2014-08-07 08:47:42 +02: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
2014-12-29 15:13:47 +01:00
if ( CompassService.this.jobSize() < 2 )
CompassService.this.cleanUp();
}
2014-09-28 00:50:06 +02:00
}
public Future<?> getCompassDirection(DimensionalCoord coord, int maxRange, ICompassCallback cc)
{
2014-12-29 15:13:47 +01:00
this.jobSize++;
return this.executor.submit( new CMDirectionRequest( coord, maxRange, cc ) );
}
2014-08-07 08:47:42 +02:00
public int jobSize()
{
2014-12-29 15:13:47 +01:00
return this.jobSize;
2014-08-07 08:47:42 +02:00
}
public void cleanUp()
{
2014-12-29 15:13:47 +01:00
for (CompassReader cr : this.worldSet.values())
2014-08-07 08:47:42 +02:00
cr.close();
}
2014-08-09 03:45:31 +02:00
public void updateArea(World w, int chunkX, int chunkZ)
{
int x = chunkX << 4;
int z = chunkZ << 4;
2014-12-29 15:13:47 +01:00
this.updateArea( w, x, 16, z );
this.updateArea( w, x, 16 + 32, z );
this.updateArea( w, x, 16 + 64, z );
this.updateArea( w, x, 16 + 96, z );
2014-08-09 03:45:31 +02:00
2014-12-29 15:13:47 +01:00
this.updateArea( w, x, 16 + 128, z );
this.updateArea( w, x, 16 + 160, z );
this.updateArea( w, x, 16 + 192, z );
this.updateArea( w, x, 16 + 224, z );
2014-08-09 03:45:31 +02:00
}
public Future<?> updateArea(World w, int x, int y, int z)
{
2014-12-29 15:13:47 +01:00
this.jobSize++;
2014-08-07 08:47:42 +02:00
int cx = x >> 4;
int cdy = y >> 5;
int cz = z >> 4;
int low_y = cdy << 5;
int hi_y = low_y + 32;
Block skystone = AEApi.instance().blocks().blockSkyStone.block();
2014-08-09 03:45:31 +02:00
// lower level...
Chunk c = w.getChunkFromBlockCoords( x, z );
for (int i = 0; i < 16; i++)
{
2014-08-09 03:45:31 +02:00
for (int j = 0; j < 16; j++)
{
for (int k = low_y; k < hi_y; k++)
{
2014-08-09 03:45:31 +02:00
Block blk = c.getBlock( i, k, j );
if ( blk == skystone && c.getBlockMetadata( i, k, j ) == 0 )
{
2014-12-29 15:13:47 +01:00
return this.executor.submit( new CMUpdatePost( w, cx, cz, cdy, true ) );
}
}
}
}
2014-12-29 15:13:47 +01:00
return this.executor.submit( new CMUpdatePost( w, cx, cz, cdy, false ) );
}
final HashMap<World, CompassReader> worldSet = new HashMap<World, CompassReader>();
final ExecutorService executor;
final File rootFolder;
public CompassService(File aEFolder) {
2014-12-29 15:13:47 +01:00
this.rootFolder = aEFolder;
this.executor = Executors.newSingleThreadExecutor( this );
this.jobSize = 0;
}
private CompassReader getReader(World w)
{
2014-12-29 15:13:47 +01:00
CompassReader cr = this.worldSet.get( w );
if ( cr == null )
{
2014-12-29 15:13:47 +01:00
cr = new CompassReader( w.provider.dimensionId, this.rootFolder );
this.worldSet.put( w, cr );
}
return cr;
}
private int dist(int ax, int az, int bx, int bz)
{
int up = (bz - az) * 16;
int side = (bx - ax) * 16;
return up * up + side * side;
}
private double rad(int ax, int az, int bx, int bz)
{
int up = bz - az;
int side = bx - ax;
return Math.atan2( -up, side ) - Math.PI / 2.0;
}
public void kill()
{
2014-12-29 15:13:47 +01:00
this.executor.shutdown();
try
{
2014-12-29 15:13:47 +01:00
this.executor.awaitTermination( 6, TimeUnit.MINUTES );
this.jobSize = 0;
2014-12-29 15:13:47 +01:00
for (CompassReader cr : this.worldSet.values())
{
cr.close();
}
2014-12-29 15:13:47 +01:00
this.worldSet.clear();
}
catch (InterruptedException e)
{
// wrap this up..
}
}
@Override
public Thread newThread(Runnable job)
{
return new Thread( job, "AE Compass Service" );
}
}