package zmaster587.advancedRocketry.util;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;

public class AsteroidSmall {
	public String ID;
	public int distance;					//distance from the star, impacts fuelcost
	public int mass;						//factor of the amount of material total
	public int minLevel;					//Minimum level of the telescope required
	public float massVariability;			//variability of mass
	public float richness;					//factor of the ratio of ore to stone
	public float richnessVariability;		//variability of richness
	public float probability;				//probability of the asteroid spawning
	public float timeMultiplier;
	public List<Float> stackProbabilites;	//
	public List<ItemStack> itemStacks;
	public static Random rand = new Random();
	private static final int precision = 1000;
	
	
	public AsteroidSmall() {
		stackProbabilites = new LinkedList<Float>();
		itemStacks = new LinkedList<ItemStack>();
	}
	
	public String getName() {return ID;}
	public int getDistance() { return distance; }
	public int getMass() {return mass;}
	public int getMinLevel() {return minLevel;}
	public float getProbability() {return probability;}

	public List<StackEntry> getHarvest(long seed) {
		return getHarvest(seed, 1);
	}
	
	
	/**
	 * @param seed to use in RNG
	 * @param uncertainty how uncertain the outcome should be, 1 is default settings, 0 is 100% known
	 * @return
	 */
	public List<StackEntry> getHarvest(long seed, float uncertainty) {
		
		List<StackEntry> entries = new LinkedList<StackEntry>();
		rand.setSeed(seed);
		
		int myMass = (int)(mass + ((rand.nextFloat()*massVariability)*mass) - massVariability*mass/2f);
		int numOres = (int) (myMass*(richness + rand.nextFloat()*richnessVariability - richnessVariability/2f));
		
		StackEntry entry = new StackEntry();
		entry.stack = new ItemStack(Blocks.COBBLESTONE, myMass - numOres);
		entry.variablility = (int)(uncertainty*entry.stack.getCount());
		entry.midpoint =  (int)(entry.variablility*rand.nextFloat() - uncertainty*entry.variablility/2f);
		
		if(entry.midpoint + myMass - numOres < entry.variablility)
			entry.midpoint = entry.variablility;
		else
			entry.midpoint += myMass - numOres;
		entries.add(entry);
		
		HashMap<Item, Integer> ores = new HashMap<Item, Integer>();
		
		float normFactor = 0;
		for(Float prob : stackProbabilites)
			normFactor += prob;
		 //normFactor /= stackProbabilites.size(); 
		
		for(int i = 0; i < numOres; i++) {
			
			float probability = rand.nextFloat();
			
			float currentLocation = 0;
			int counter = 0;
			Item item = null;
			
			for(Float prob : stackProbabilites) {
				if(probability <= currentLocation + prob/normFactor && currentLocation <= probability) {
					item = itemStacks.get(counter).getItem();
					break;
				}
				
				counter++;
				currentLocation += prob/normFactor;
			}
			
			Integer number = ores.get(item);
			if(number == null)
				ores.put(item, 1);
			else
				ores.put(item, number + 1);
		}
		
		for(ItemStack iterStack : itemStacks) {
			
			Integer num = ores.get(iterStack.getItem());
			
			if(num == null)
				continue;
			
			ItemStack stack = new ItemStack(iterStack.getItem(), num, iterStack.getMetadata());
			entry = new StackEntry();
			
			entry.stack = stack;
			entry.variablility = (int) (uncertainty*rand.nextFloat()*num);
			
			entry.midpoint =  (int)(entry.variablility*rand.nextFloat() - entry.variablility/2f);
			
			if(entry.midpoint + num < entry.variablility)
				entry.midpoint = entry.variablility;
			else
				entry.midpoint += num;
			
			entries.add(entry);
		}
		
		return entries;
	}
	
	
	public static class StackEntry {
		public ItemStack stack;
		public int variablility;
		public int midpoint;
		public boolean isKnown;
	}
}

@@ -36,16 +36,44 @@
 .util;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -168,16 +168,61 @@
 andom;%0A%0A
+import zmaster587.libVulpes.block.BlockMeta;%0A
 import n
@@ -2415,66 +2415,47 @@
 %09%0A%09%09
-HashMap%3CItem, Integer%3E
+int
  ores
+%5B%5D
  = new 
-HashMap%3CItem, Integer%3E
+int%5BitemStacks.size
 ()
+%5D
 ;%0A%09%09
@@ -3025,171 +3025,111 @@
 %0A%09%09%09
-Integer number = ores.get(item);%0A%09%09%09if(number == null)%0A%09%09%09%09ores.put(item, 1);%0A%09%09%09else%0A%09%09%09%09ores.put(item, number + 1);%0A%09%09%7D%0A%09%09%0A%09%09for(ItemStack iterStack : itemStacks
+if(counter %3C itemStacks.size())%0A%09%09%09%09ores%5Bcounter%5D++;%0A%09%09%7D%0A%09%09%0A%09%09for(int i = 0; i %3C itemStacks.size(); i++
 ) %7B%0A
@@ -3139,15 +3139,11 @@
 %0A%09%09%09
-I
+i
 nt
-eger
  num
@@ -3153,33 +3153,11 @@
 ores
-.get(iterStack.getItem())
+%5Bi%5D
 ;%0A%09%09
@@ -3171,20 +3171,17 @@
 (num == 
-null
+0
 )%0A%09%09%09%09co
@@ -3230,22 +3230,30 @@
 tack(ite
-r
+m
 Stack
+s.get(i)
 .getItem
@@ -3268,14 +3268,22 @@
  ite
-r
+m
 Stack
+s.get(i)
 .get

fix #1029