Applied-Energistics-2-tiler.../hooks/MeteoriteWorldGen.java

147 lines
3.8 KiB
Java
Raw Normal View History

package appeng.hooks;
2014-08-07 08:47:42 +02:00
import java.util.Collection;
import java.util.Random;
2014-08-07 08:47:42 +02:00
import java.util.concurrent.Callable;
2014-08-07 08:47:42 +02:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraft.world.chunk.IChunkProvider;
import appeng.api.features.IWorldGen.WorldGenType;
import appeng.core.AEConfig;
import appeng.core.WorldSettings;
import appeng.core.features.registries.WorldGenRegistry;
import appeng.helpers.MeteoritePlacer;
import appeng.services.helpers.ICompassCallback;
2014-08-07 08:47:42 +02:00
import appeng.util.Platform;
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;
}
};
@Override
public void generate(Random r, int chunkX, int chunkZ, World w, IChunkProvider chunkGenerator, IChunkProvider chunkProvider)
{
2014-09-20 22:47:08 +02:00
if ( WorldGenRegistry.instance.isWorldGenEnabled( WorldGenType.Meteorites, w ) )
{
2014-08-07 08:47:42 +02:00
// add new metorites?
2014-08-14 02:36:37 +02:00
if ( r.nextFloat() < AEConfig.instance.metoriteSpawnChance )
2014-08-07 08:47:42 +02:00
{
int x = r.nextInt( 16 ) + (chunkX << 4);
int z = r.nextInt( 16 ) + (chunkZ << 4);
2014-08-07 08:47:42 +02:00
int depth = 180 + r.nextInt( 20 );
TickHandler.instance.addCallable( w, new MetoriteSpawn( x, depth, z, w ) );
}
else
TickHandler.instance.addCallable( w, new MetoriteSpawn( chunkX << 4, 128, chunkZ << 4, w ) );
}
2014-08-09 03:45:31 +02:00
else
WorldSettings.getInstance().getCompass().updateArea( w, chunkX, chunkZ );
2014-08-07 08:47:42 +02:00
}
2014-08-07 08:47:42 +02:00
class MetoriteSpawn implements Callable
{
2014-08-07 08:47:42 +02:00
final int x;
final int z;
final World w;
int depth;
2014-08-07 08:47:42 +02:00
public MetoriteSpawn(int x, int depth, int z, World w) {
this.x = x;
this.z = z;
this.w = w;
this.depth = depth;
}
2014-08-07 08:47:42 +02:00
@Override
public Object call() throws Exception
{
int chunkX = x >> 4;
int chunkZ = z >> 4;
2014-08-07 08:47:42 +02:00
double minSqDist = Double.MAX_VALUE;
2014-08-07 08:47:42 +02:00
// near by meteorites!
for (NBTTagCompound data : getNearByMetetorites( w, chunkX, chunkZ ))
{
MeteoritePlacer mp = new MeteoritePlacer();
mp.spawnMeteorite( new MeteoritePlacer.ChunkOnly( w, chunkX, chunkZ ), data );
minSqDist = Math.min( minSqDist, mp.getSqDistance( x, z ) );
}
2014-08-07 08:47:42 +02:00
2014-09-20 23:33:21 +02:00
boolean isCluster = (minSqDist < 30 * 30) && Platform.getRandomFloat() < AEConfig.instance.meteoriteClusterChance;
2014-08-07 08:47:42 +02:00
if ( minSqDist > AEConfig.instance.minMeteoriteDistanceSq || isCluster )
tryMetroite( w, depth, x, z );
WorldSettings.getInstance().setGenerated( w.provider.dimensionId, chunkX, chunkZ );
2014-08-09 03:45:31 +02:00
WorldSettings.getInstance().getCompass().updateArea( w, chunkX, chunkZ );
2014-08-07 08:47:42 +02:00
return null;
}
}
private boolean tryMetroite(World w, int depth, int x, int z)
{
for (int trys = 0; trys < 20; trys++)
{
MeteoritePlacer mp = new MeteoritePlacer();
if ( mp.spawnMeteorite( new MeteoritePlacer.ChunkOnly( w, x >> 4, z >> 4 ), x, depth, z ) )
{
2014-08-07 08:47:42 +02:00
int px = x >> 4;
int pz = z >> 4;
for (int cx = px - 6; cx < px + 6; cx++)
for (int cz = pz - 6; cz < pz + 6; cz++)
{
if ( w.getChunkProvider().chunkExists( cx, cz ) )
{
if ( px == cx && pz == cz )
continue;
if ( WorldSettings.getInstance().hasGenerated( w.provider.dimensionId, cx, cz ) )
{
MeteoritePlacer mp2 = new MeteoritePlacer();
mp2.spawnMeteorite( new MeteoritePlacer.ChunkOnly( w, cx, cz ), mp.getSettings() );
}
}
}
return true;
}
2014-08-07 08:47:42 +02:00
depth -= 15;
if ( depth < 40 )
return false;
}
2014-08-07 08:47:42 +02:00
return false;
}
2014-08-07 08:47:42 +02:00
private Collection<NBTTagCompound> getNearByMetetorites(World w, int chunkX, int chunkZ)
{
return WorldSettings.getInstance().getNearByMetetorites( w.provider.dimensionId, chunkX, chunkZ );
}
}