// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) braces deadcode 

package net.minecraft.src;

import java.io.*;
import java.util.*;
import net.minecraft.client.Minecraft;

// Referenced classes of package net.minecraft.src:
//            BaseMod, EntityGolem, ModLoader, RenderGolem, 
//            WorldGenTower, Block, World

public class mod_AS_BattleTowers extends BaseModMp
{
	@Override
    public void load()
    {
        ModLoader.registerEntityID(AS_EntityGolem.class, "Golem", 27);
		ModLoaderMp.registerNetClientHandlerEntity(AS_EntityGolemFireball.class, 27);
		
        spawncounter = 200;
        try
        {
            Properties properties = new Properties();
            File file = new File((new StringBuilder()).append(Minecraft.getMinecraftDir().getCanonicalPath()).append("/mods/BattleTowers.txt").toString());
            configpath = (new StringBuilder()).append(Minecraft.getMinecraftDir().getCanonicalPath()).append("/mods/BattleTowers.txt").toString();
            properties.load(new InputStreamReader(new FileInputStream(file)));
            rarity = Integer.valueOf(properties.getProperty("rarity")).intValue();
			minDistanceBetweenTowers = Double.valueOf(properties.getProperty("minDistanceBetweenTowers")).doubleValue();
			towerDestroyerEnabled = Integer.valueOf(properties.getProperty("towerDestroyerEnabled")).intValue();
        }
        catch(FileNotFoundException filenotfoundexception)
        {
            System.err.println("BattleTowers properties file not found, using default values: "+filenotfoundexception);
			UseDefaults();
        }
        catch(IOException ioexception)
        {
            System.err.println("Error reading BattleTowers properties file, using default values: "+ioexception);
			UseDefaults();
        }
        catch(NumberFormatException numberformatexception)
        {
            System.err.println("Incorrect value property, using default value. "+numberformatexception);
			UseDefaults();
        }
        if(rarity < 1)
        {
            rarity = 1;
        }
		raritycounter = rarity * 100;
		
		TowerPositions = new ArrayList();
		TowerDestroyers = new ArrayList();
		
		time = System.currentTimeMillis();
		ModLoader.setInGameHook(this, true, true);
    }
	
	private boolean serverRequested = false;
	
	private void checkServerSetting()
	{
		if (serverRequested) return;
		serverRequested = true;
		
		Packet230ModLoader packet = new Packet230ModLoader();
		packet.packetType = 1;
		ModLoaderMp.sendPacket(this, packet);
	}
	
	@Override
	public boolean onTickInGame(float derp, net.minecraft.client.Minecraft mc)
	{
		if (mc.thePlayer == null || mc.theWorld == null) return true;
		
		if (System.currentTimeMillis() > time + 1000L) // its a one second timer OMFG
		{
			checkServerSetting();
		
			for(int i = 0; i < TowerDestroyers.size(); i++)
			{
				AS_TowerDestroyer temp = (AS_TowerDestroyer)TowerDestroyers.get(i);
				temp.Update();
			}
		}
		
		return true;
	}
	
	public static void registerTowerDestroyer(AS_TowerDestroyer td)
	{
		TowerDestroyers.add(td);
	}
	
	public static void unRegisterTowerDestroyer(AS_TowerDestroyer td)
	{
		TowerDestroyers.remove(td);
	}
	
	@Override
	public void handlePacket(Packet230ModLoader packet)
	{
		switch(packet.packetType)
		{
			case 0:
			{
				towerDestroyerEnabled = packet.dataInt[0];
			}
		}
	}
	
	public void UseDefaults()
	{
        rarity = 3;
		minDistanceBetweenTowers = 64D;
		towerDestroyerEnabled = 1;
	}

	@Override
    public void modsLoaded()
    {
        System.err.println("BATTLE TOWERS CONFIG");
        System.err.println((new StringBuilder()).append("Tower Rarity ").append(String.valueOf(rarity)).toString());
		System.err.println((new StringBuilder()).append("Min Tower Distance ").append(String.valueOf(minDistanceBetweenTowers)).toString());
        System.err.println((new StringBuilder()).append("MINECRAFT PROPERTIES ").append(String.valueOf(configpath)).toString());
    }

	@Override
    public void addRenderer(Map map)
    {
        map.put(net.minecraft.src.AS_EntityGolem.class, new AS_RenderGolem());
    }
	
	static boolean isWorking = false;

	@Override
    public void generateSurface(World world, Random random, int x, int z)
    {
		if (!isWorking)
		{
			isWorking = true;
			if(spawncounter > raritycounter)
			{
				boolean spawn = true;
				double dist;
				double mindist = 100D;
				
				for(int i = 0; i < TowerPositions.size(); i++)
				{
					ChunkCoordinates temp = (ChunkCoordinates)TowerPositions.get(i);
					dist = temp.getSqDistanceTo(x, 64, z);
					
					if (dist < mindist) mindist = dist;
					
					if (dist < minDistanceBetweenTowers)
					{
						spawn = false;
						break;
					}
				}

				if (spawn)
				{
					if(AttemptToSpawnTower(world, random, x, z))
					{
						if (TowerPositions.size() > 30)
						{
							TowerPositions.remove(0);
						}
						
						ChunkCoordinates temp = new ChunkCoordinates(x, 64, z);
						TowerPositions.add(temp);
						spawncounter = 0;
						
						//System.err.println("Battle Tower spawned at [ "+x+" | "+z+" ]");
					}
				}
			}
			else
			{
				spawncounter++;
			}
			isWorking = false;
		}
	}
	
	private boolean AttemptToSpawnTower(World world, Random random, int x, int z)
	{
		int y = GetSurfaceBlockHeight(world, x, z);
		if (y == 49) return false;
		
		return ((new AS_WorldGenTower()).generate(world, random, x, y, z));
	}
	
	private int GetSurfaceBlockHeight(World world, int x, int z)
	{
		int h = 50;
		
		do
		{
			h++;
		}
		while (world.getBlockId(x, h, z) != 0);
		
		return h-1;
	}

	@Override
    public String getVersion()
    {
        return "1.2.3";
    }
	
	private List TowerPositions;
	private static List TowerDestroyers;
	private int raritycounter = 400;
    private int spawncounter;
    private static int rarity;
	private double minDistanceBetweenTowers;
	
	public static int towerDestroyerEnabled;
	
    public static String configpath;
	private long time;
}
