View Javadoc

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