package es.pcv.game.elements.items.drops;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;

import es.pcv.core.render.Point2D;
import es.pcv.game.Game;
import es.pcv.game.configuration.Config;
import es.pcv.game.elements.items.Item;

public abstract class Drops {
	
	public abstract List<Item> getDrops(Point2D p);
	public static final double R=0.1*Config.scale.getX();
	
	public static List<Item> getRandom(Point2D p,HashMap<Item, Double>drops,int max,int min){
		List<Item> r=new LinkedList<Item>();
		long normal=Math.round(Math.random()*(max-min))+min;
		Item i;
		for (int j = 0; j < normal; j++) {
			i=getRandom(p,drops);
			if (i!=null) {
				Point2D np=p.clone();
				np.addX((float) (Math.random()*(R*2)-R));np.addY((float) (Math.random()*(R*2)-R));
				i.setPos(np);
				r.add(i);
			}
		}
		return r;
	}
	
	public static Item getRandom(Point2D p, HashMap<Item, Double>drops){
		double max=0;
		for (double d:drops.values()) {
			max+=d;
		}
		double r=Math.random()*max;
		int c=0;
		for (Entry<Item, Double> e:drops.entrySet()) {
			if (c+e.getValue()>=r  && c<r) {
				Item i=e.getKey();
				return i.cloneItem();
			}else{
				c+=e.getValue();
			}
		}
		return drops.size()!=0?drops.keySet().iterator().next().cloneItem():null;
	}
	
	public void spawnDrops(Point2D p) {
		List<Item>its=getDrops(p);
		for (Item item : its) {
			Game.getGame().addElement(item);
		}		
	}
}

@@ -36,16 +36,99 @@
 drops;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.Collections;%0A
 import j
@@ -1022,16 +1022,177 @@
 drops)%7B%0A
+%09%09Item%5B%5D its=new Item%5Bdrops.size()%5D;%0A%09%09drops.keySet().toArray(its);%0A%09%09%0A%09%09List%3CItem%3Elits=Arrays.asList(its);%0A%09%09Collections.shuffle(lits);%0A%09%09return lits.get(0);/*%0A
 %09%09double
@@ -1538,16 +1538,18 @@
 ():null;
+*/
 %0A%09%7D%0A%09%0A%09p

Fix drops