diff --git a/core/Registration.java b/core/Registration.java index 94dd6954..d0f68d97 100644 --- a/core/Registration.java +++ b/core/Registration.java @@ -79,9 +79,11 @@ import appeng.core.localization.PlayerMessages; import appeng.debug.BlockChunkloader; import appeng.debug.BlockItemGen; import appeng.debug.ToolDebugCard; +import appeng.debug.ToolEraser; import appeng.debug.ToolMeteoritePlacer; import appeng.debug.ToolReplicatorCard; import appeng.hooks.AETrading; +import appeng.hooks.MeteoriteWorldGen; import appeng.hooks.QuartzWorldGen; import appeng.hooks.TickHandler; import appeng.items.materials.ItemMaterial; @@ -343,6 +345,7 @@ public class Registration items.itemFacade = addFeature( ItemFacade.class ); items.itemCrystalSeed = addFeature( ItemCrystalSeed.class ); + addFeature( ToolEraser.class ); addFeature( ToolMeteoritePlacer.class ); addFeature( ToolDebugCard.class ); addFeature( ToolReplicatorCard.class ); @@ -556,6 +559,9 @@ public class Registration if ( AEConfig.instance.isFeatureEnabled( AEFeature.CertusQuartzWorldGen ) ) GameRegistry.registerWorldGenerator( new QuartzWorldGen(), 0 ); + if ( AEConfig.instance.isFeatureEnabled( AEFeature.MeteoriteWorldGen ) ) + GameRegistry.registerWorldGenerator( new MeteoriteWorldGen(), 0 ); + recipeHandler.registerHandlers(); } } diff --git a/core/features/AEFeature.java b/core/features/AEFeature.java index ef69ab8f..eaa93a0c 100644 --- a/core/features/AEFeature.java +++ b/core/features/AEFeature.java @@ -6,7 +6,7 @@ public enum AEFeature // is just flat out required by tons of // important stuff. - CertusQuartzWorldGen("World"), + CertusQuartzWorldGen("World"), MeteoriteWorldGen("World"), DecorativeLights("World"), DecorativeQuartzBlocks("World"), diff --git a/core/sync/packets/PacketCompassRequest.java b/core/sync/packets/PacketCompassRequest.java index da39036d..9314b354 100644 --- a/core/sync/packets/PacketCompassRequest.java +++ b/core/sync/packets/PacketCompassRequest.java @@ -8,7 +8,6 @@ import java.io.IOException; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import appeng.api.util.DimensionalCoord; -import appeng.core.AELog; import appeng.core.WorldSettings; import appeng.core.sync.AppEngPacket; import appeng.core.sync.network.INetworkInfo; @@ -32,7 +31,7 @@ public class PacketCompassRequest extends AppEngPacket implements ICompassCallba } @Override - public void calculatedDirection(boolean hasResult, boolean spin, double radians) + public void calculatedDirection(boolean hasResult, boolean spin, double radians, double dist) { NetworkHandler.instance.sendTo( new PacketCompassResponse( this, hasResult, spin, radians ), (EntityPlayerMP) talkBackTo ); } @@ -40,11 +39,10 @@ public class PacketCompassRequest extends AppEngPacket implements ICompassCallba @Override public void serverPacketData(INetworkInfo manager, AppEngPacket packet, EntityPlayer player) { - AELog.info( "PacketCompassRequest.serverPacketData" ); talkBackTo = player; DimensionalCoord loc = new DimensionalCoord( player.worldObj, this.cx << 4, this.cdy << 5, this.cz << 4 ); - WorldSettings.getInstance().getCompass().getCompassDirection( loc, this ); + WorldSettings.getInstance().getCompass().getCompassDirection( loc, 128, this ); } // api diff --git a/core/sync/packets/PacketCompassResponse.java b/core/sync/packets/PacketCompassResponse.java index 4f4c544d..405e4897 100644 --- a/core/sync/packets/PacketCompassResponse.java +++ b/core/sync/packets/PacketCompassResponse.java @@ -6,7 +6,6 @@ import io.netty.buffer.Unpooled; import java.io.IOException; import net.minecraft.entity.player.EntityPlayer; -import appeng.core.AELog; import appeng.core.sync.AppEngPacket; import appeng.core.sync.network.INetworkInfo; import appeng.hooks.CompassManager; @@ -33,7 +32,6 @@ public class PacketCompassResponse extends AppEngPacket @Override public void clientPacketData(INetworkInfo network, AppEngPacket packet, EntityPlayer player) { - AELog.info( "PacketCompassResponse.clientPacketData" ); CompassManager.instance.postResult( attunement, cx << 4, cdy << 5, cz << 4, cr ); } diff --git a/debug/ToolEraser.java b/debug/ToolEraser.java new file mode 100644 index 00000000..18ab2d39 --- /dev/null +++ b/debug/ToolEraser.java @@ -0,0 +1,75 @@ +package appeng.debug; + +import java.util.EnumSet; +import java.util.LinkedList; +import java.util.List; + +import net.minecraft.block.Block; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; +import appeng.api.util.WorldCoord; +import appeng.core.AELog; +import appeng.core.features.AEFeature; +import appeng.items.AEBaseItem; +import appeng.util.Platform; + +public class ToolEraser extends AEBaseItem +{ + + public ToolEraser() { + super( ToolEraser.class ); + setfeature( EnumSet.of( AEFeature.Debug, AEFeature.Creative ) ); + } + + @Override + public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ) + { + if ( Platform.isClient() ) + return false; + + Block blk = world.getBlock( x, y, z ); + int meta = world.getBlockMetadata( x, y, z ); + + int blocks = 0; + List next = new LinkedList(); + next.add( new WorldCoord( x, y, z ) ); + + while (blocks < 90000 && !next.isEmpty()) + { + + List c = next; + next = new LinkedList(); + + for (WorldCoord wc : c) + { + Block c_blk = world.getBlock( wc.x, wc.y, wc.z ); + int c_meta = world.getBlockMetadata( wc.x, wc.y, wc.z ); + + if ( c_blk == blk && c_meta == meta ) + { + blocks++; + world.setBlock( wc.x, wc.y, wc.z, Platform.air ); + + check( world, wc.x + 1, wc.y, wc.z, next ); + check( world, wc.x - 1, wc.y, wc.z, next ); + check( world, wc.x, wc.y + 1, wc.z, next ); + check( world, wc.x, wc.y - 1, wc.z, next ); + check( world, wc.x, wc.y, wc.z + 1, next ); + check( world, wc.x, wc.y, wc.z - 1, next ); + } + } + + } + + AELog.info( "Delete " + blocks + " blocks" ); + + return true; + } + + private void check(World world, int i, int y, int z, List next) + { + next.add( new WorldCoord( i, y, z ) ); + } + +} diff --git a/helpers/MeteoritePlacer.java b/helpers/MeteoritePlacer.java index dda41399..776e1959 100644 --- a/helpers/MeteoritePlacer.java +++ b/helpers/MeteoritePlacer.java @@ -43,7 +43,7 @@ public class MeteoritePlacer else if ( a > 0.8 ) put( w, x, y, z, Blocks.stone ); else if ( a > 0.7 ) - put( w, x, y, z, Blocks.dirt ); + put( w, x, y, z, Blocks.grass ); else if ( a > 0.6 ) put( w, x, y, z, skystone ); else if ( a > 0.5 ) @@ -105,7 +105,7 @@ public class MeteoritePlacer public void getOther(World w, int x, int y, int z, double a) { - if ( a > 0.8 ) + if ( a > 0.66 ) put( w, x, y, z, Blocks.glass ); } @@ -118,6 +118,7 @@ public class MeteoritePlacer Fallout type = new Fallout(); Block skystone = AEApi.instance().blocks().blockSkyStone.block(); + Block skychest = AEApi.instance().blocks().blockSkyChest.block(); double real_sizeOfMetorite = (Math.random() * 6.0) + 2; double real_crator = real_sizeOfMetorite * 2 + 5; @@ -126,7 +127,6 @@ public class MeteoritePlacer double crator = real_crator * real_crator; public MeteoritePlacer() { - Block skystone = AEApi.instance().blocks().blockSkyStone.block(); validSpawn.add( Blocks.stone ); validSpawn.add( Blocks.cobblestone ); @@ -277,6 +277,8 @@ public class MeteoritePlacer if ( dx * dx * 0.7 + dy * dy * (j > y ? 1.4 : 0.8) + dz * dz * 0.7 < sizeOfMetorite ) put( w, i, j, k, skystone ); } + + put( w, x, y, z, skychest ); } private void Decay(World w, int x, int y, int z) @@ -310,12 +312,14 @@ public class MeteoritePlacer double dist = dx * dx + dy * dy + dz * dz; Block xf = w.getBlock( i, j - 1, k ); - - double height = crator * 0.1 - Math.abs( dist - crator * 1.7 ); - - if ( xf != blk && height > 0 && Math.random() > 0.5 ) + if ( !xf.isReplaceable( w, i, j - 1, k ) ) { - type.getRandomFall( w, i, j, k ); + double height = crator * 0.1 - Math.abs( dist - crator * 1.7 ); + + if ( xf != blk && height > 0 && Math.random() > 0.5 ) + { + type.getRandomFall( w, i, j, k ); + } } } } diff --git a/hooks/CompassManager.java b/hooks/CompassManager.java index 8da588a6..9afd890c 100644 --- a/hooks/CompassManager.java +++ b/hooks/CompassManager.java @@ -48,7 +48,6 @@ public class CompassManager public void postResult(long attunement, int x, int y, int z, CompassResult res) { - AELog.info( "CompassManager.postResult" ); CompassReq r = new CompassReq( attunement, x, y, z ); reqs.put( r, res ); } @@ -89,7 +88,6 @@ public class CompassManager private void requestUpdate(CompassReq r) { - AELog.info( "CompassManager.requestUpdate" ); try { diff --git a/hooks/MeteoriteWorldGen.java b/hooks/MeteoriteWorldGen.java new file mode 100644 index 00000000..1a13bccf --- /dev/null +++ b/hooks/MeteoriteWorldGen.java @@ -0,0 +1,81 @@ +package appeng.hooks; + +import java.util.Random; + +import net.minecraft.world.World; +import net.minecraft.world.chunk.IChunkProvider; +import appeng.api.util.DimensionalCoord; +import appeng.core.WorldSettings; +import appeng.helpers.MeteoritePlacer; +import appeng.services.helpers.ICompassCallback; +import cpw.mods.fml.common.IWorldGenerator; + +final public class MeteoriteWorldGen implements IWorldGenerator +{ + + class myGen implements ICompassCallback + { + + double distance = 0; + + @Override + public void calculatedDirection(boolean hasResult, boolean spin, double radians, double dist) + { + if ( hasResult ) + distance = dist; + else + distance = Double.MAX_VALUE; + + synchronized (this) + { + notify(); + } + } + + }; + + @Override + public void generate(Random r, int chunkX, int chunkZ, World w, IChunkProvider chunkGenerator, IChunkProvider chunkProvider) + { + if ( r.nextFloat() > 0.9 ) + { + int x = r.nextInt( 16 ) + (chunkX << 4); + int z = r.nextInt( 16 ) + (chunkZ << 4); + + myGen obj = new myGen(); + WorldSettings.getInstance().getCompass().getCompassDirection( new DimensionalCoord( w, x, 128, z ), 30, obj ); + + synchronized (obj) + { + try + { + obj.wait(); + + } + catch (InterruptedException e) + { + // meh + return; + } + } + + if ( obj.distance > 1000 * 1000 ) + { + int depth = 180 + r.nextInt( 20 ); + 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; + + } + } + + } + } +} diff --git a/services/CompassService.java b/services/CompassService.java index 0dfb3e07..2a22f08f 100644 --- a/services/CompassService.java +++ b/services/CompassService.java @@ -8,7 +8,6 @@ import net.minecraft.block.Block; import net.minecraft.world.World; import appeng.api.AEApi; import appeng.api.util.DimensionalCoord; -import appeng.core.AELog; import appeng.services.helpers.CompassException; import appeng.services.helpers.CompassReader; import appeng.services.helpers.ICompassCallback; @@ -59,6 +58,7 @@ public class CompassService implements Runnable { public final DimensionalCoord coord; + public final int maxRange; public final ICompassCallback callback; @Override @@ -67,8 +67,9 @@ public class CompassService implements Runnable return true; } - public CMDirectionRequest(DimensionalCoord coord, ICompassCallback cc) { + public CMDirectionRequest(DimensionalCoord coord, int getMaxRange, ICompassCallback cc) { this.coord = coord; + this.maxRange = getMaxRange; callback = cc; } @@ -111,9 +112,9 @@ public class CompassService implements Runnable postJob( new CMUpdatePost( w, cx, cz, cdy, false ) ); } - public void getCompassDirection(DimensionalCoord coord, ICompassCallback cc) + public void getCompassDirection(DimensionalCoord coord, int maxRange, ICompassCallback cc) { - postJob( new CMDirectionRequest( coord, cc ) ); + postJob( new CMDirectionRequest( coord, maxRange, cc ) ); } private void postJob(CompassMessage msg) @@ -137,6 +138,8 @@ public class CompassService implements Runnable try { myMsg = jobList.poll(); + overOberdened = jobList.isEmpty(); + if ( myMsg == null ) jobList.wait(); } @@ -150,6 +153,7 @@ public class CompassService implements Runnable return myMsg; } + boolean overOberdened = false; HashMap worldSet = new HashMap(); final File rootFolder; @@ -173,8 +177,6 @@ public class CompassService implements Runnable private void processRequest(CMDirectionRequest req) { - AELog.info( "CompassService.processRequest" ); - int cx = req.coord.x >> 4; int cz = req.coord.z >> 4; @@ -183,8 +185,7 @@ public class CompassService implements Runnable // Am I standing on it? if ( cr.hasBeacon( cx, cz ) ) { - req.callback.calculatedDirection( true, true, -999 ); - cr.close(); + req.callback.calculatedDirection( true, true, -999, 0 ); return; } @@ -196,20 +197,32 @@ public class CompassService implements Runnable 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 ) ) { - req.callback.calculatedDirection( true, false, rad( cx, cz, minx, z ) ); - cr.close(); - return; + int closness = dist( cx, cz, minx, z ); + if ( closness < closest ) + { + closest = closness; + chosen_x = minx; + chosen_z = z; + } } if ( cr.hasBeacon( maxx, z ) ) { - req.callback.calculatedDirection( true, false, rad( cx, cz, maxx, z ) ); - cr.close(); - return; + int closness = dist( cx, cz, maxx, z ); + if ( closness < closest ) + { + closest = closness; + chosen_x = maxx; + chosen_z = z; + } } } @@ -217,23 +230,48 @@ public class CompassService implements Runnable { if ( cr.hasBeacon( x, minz ) ) { - req.callback.calculatedDirection( true, false, rad( cx, cz, x, minz ) ); - cr.close(); - return; + int closness = dist( cx, cz, x, minz ); + if ( closness < closest ) + { + closest = closness; + chosen_x = x; + chosen_z = minz; + } } if ( cr.hasBeacon( x, maxz ) ) { - req.callback.calculatedDirection( true, false, rad( cx, cz, x, maxz ) ); - cr.close(); - return; + 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 ); - cr.close(); + req.callback.calculatedDirection( false, true, -999, 999 ); + if ( !overOberdened ) + cr.close(); + } + + 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) diff --git a/services/helpers/CompassReader.java b/services/helpers/CompassReader.java index 41b0f235..ac8d5aaf 100644 --- a/services/helpers/CompassReader.java +++ b/services/helpers/CompassReader.java @@ -16,6 +16,7 @@ public class CompassReader { for (CompassRegion r : regions.values()) r.close(); + regions.clear(); } diff --git a/services/helpers/CompassRegion.java b/services/helpers/CompassRegion.java index 2a25d190..bbeb8ba1 100644 --- a/services/helpers/CompassRegion.java +++ b/services/helpers/CompassRegion.java @@ -1,8 +1,9 @@ package appeng.services.helpers; -import java.io.EOFException; import java.io.File; import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; public class CompassRegion { @@ -18,6 +19,7 @@ public class CompassRegion boolean hasFile = false; final File rootFolder; RandomAccessFile raf = null; + ByteBuffer buffer; public void close() { @@ -25,6 +27,7 @@ public class CompassRegion { if ( hasFile ) { + buffer = null; raf.close(); raf = null; hasFile = false; @@ -94,8 +97,9 @@ public class CompassRegion { try { - raf.seek( cx + cz * 0x400 ); - raf.writeByte( val ); + buffer.put( cx + cz * 0x400, (byte) val ); + // raf.seek( cx + cz * 0x400 ); + // raf.writeByte( val ); } catch (Throwable t) { @@ -107,10 +111,11 @@ public class CompassRegion { try { - raf.seek( cx + cz * 0x400 ); - return raf.readByte(); + return buffer.get( cx + cz * 0x400 ); + // raf.seek( cx + cz * 0x400 ); + // return raf.readByte(); } - catch (EOFException eof) + catch (IndexOutOfBoundsException outofBounds) { return 0; } @@ -131,6 +136,8 @@ public class CompassRegion try { raf = new RandomAccessFile( fName, "rw" ); + FileChannel fc = raf.getChannel(); + buffer = fc.map( FileChannel.MapMode.READ_WRITE, 0, 0x400 * 0x400 );// fc.size() ); hasFile = true; } catch (Throwable t) @@ -156,4 +163,5 @@ public class CompassRegion return new File( folder + File.separatorChar + world + "_" + low_x + "_" + low_z + ".dat" ); } + } diff --git a/services/helpers/ICompassCallback.java b/services/helpers/ICompassCallback.java index 4b816488..e924e232 100644 --- a/services/helpers/ICompassCallback.java +++ b/services/helpers/ICompassCallback.java @@ -10,6 +10,6 @@ public interface ICompassCallback * @param spin * @param radians */ - public void calculatedDirection(boolean hasResult, boolean spin, double radians); + public void calculatedDirection(boolean hasResult, boolean spin, double radians, double dist); }