View Javadoc

1   package nl.tudelft.goal.ut2004.selector;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.List;
6   import java.util.Map;
7   
8   import cz.cuni.amis.pogamut.base.utils.math.DistanceUtils;
9   import cz.cuni.amis.pogamut.base3d.worldview.object.ILocated;
10  import cz.cuni.amis.pogamut.unreal.communication.messages.UnrealId;
11  import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType;
12  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.ItemPickedUp;
13  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
14  import cz.cuni.amis.utils.IFilter;
15  import cz.cuni.amis.utils.collections.MyCollections;
16  
17  /**
18   * Selects the closest friendly {@link Player}.
19   * 
20   * @author mpkorstanje
21   * 
22   */
23  public final class ClosestFriendlyWithLinkGun extends ContextSelector {
24  
25  	/* (non-Javadoc)
26  	 * @see java.lang.Object#toString()
27  	 */
28  	@Override
29  	public String toString() {
30  		return "ClosestFriendlyWithLinkGun";
31  	}
32  
33  	@Override
34  	public ILocated select(Collection<? extends ILocated> targets) {
35  		
36  		//Optimization, faster then selecting from collection.
37  		Map<UnrealId, Player> friends = modules.getPlayers().getVisibleFriends();
38  		List<Player> friendsWithLinkGun = MyCollections.getFiltered(friends.values(), new IFilter<Player>() {
39  
40  			@Override
41  			public boolean isAccepted(Player p) {
42  				return ItemType.LINK_GUN.equals(ItemType.getItemType(p.getWeapon()));
43  			}
44  		});
45  		
46  		return DistanceUtils.getNearest(friendsWithLinkGun, modules.getInfo().getLocation());
47  	}
48  
49  }