View Javadoc

1   package nl.tudelft.pogamut.ut2004.agent.module.sensor;
2   
3   import java.util.Collection;
4   import java.util.Map;
5   
6   import cz.cuni.amis.pogamut.base.agent.module.SensorModule;
7   import cz.cuni.amis.pogamut.base.communication.worldview.object.WorldObjectId;
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.ut2004.agent.module.sensor.AgentInfo;
11  import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
12  import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType;
13  import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType.Category;
14  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.IncomingProjectile;
15  import cz.cuni.amis.utils.IFilter;
16  import cz.cuni.amis.utils.collections.MyCollections;
17  
18  /**
19   * Module to keep track of projectiles.
20   * 
21   * TODO: Horribly inefficient, use listeners.
22   * TODO: Needs ILocated on {@link IncomingProjectile}.
23   * 
24   * @author mpkorstanje
25   *
26   */
27  @SuppressWarnings("rawtypes")
28  public class Projectiles extends SensorModule<UT2004Bot> {
29  
30  	protected AgentInfo info;
31  
32  	public Projectiles(UT2004Bot<?, ?, ?> agent, AgentInfo info) {
33  		super(agent);
34  		this.info = info;
35  		//incomingProjectileListener = new IncomingProjectileListener(worldView);
36  	}
37  
38  
39  	public IncomingProjectile getNearestProjectile() {
40  		return DistanceUtils.getNearest(worldView.getAll(IncomingProjectile.class).values(), info.getLocation());
41  	}
42  
43  
44  	public Map<WorldObjectId, IncomingProjectile> getProjectiles() {
45  		return worldView.getAll(IncomingProjectile.class);
46  	}
47  
48  	public IncomingProjectile getNearestProjectile(ILocated location) {
49  		return DistanceUtils.getNearest(worldView.getAll(IncomingProjectile.class).values(), location);
50  	}
51  
52  	public IncomingProjectile getNearestProjectile(ILocated location, double maxDistance) {
53  		return DistanceUtils.getNearest(worldView.getAll(IncomingProjectile.class).values(), location, maxDistance);
54  	}
55  
56  
57  	public IncomingProjectile getNearestProjectile(ILocated location, ItemType type) {
58  		return DistanceUtils.getNearest(getProjectiles(type), location);
59  	}
60  
61  	public Collection<IncomingProjectile> getProjectiles(final ItemType type) {
62  		if (type == null) return null;
63  		if(type.getCategory() != Category.PROJECTILE) return null;
64  		
65  		return MyCollections.getFiltered(worldView.getAll(IncomingProjectile.class).values(), new IFilter<IncomingProjectile>() {
66  
67  			@Override
68  			public boolean isAccepted(IncomingProjectile object) {
69  				return type.equals(ItemType.getItemType(object.getType()));
70  			}
71  		});
72  	}
73  
74  
75  	public IncomingProjectile getNearestProjectile(ILocated location, double maxDistance, ItemType type) {
76  		return DistanceUtils.getNearest(getProjectiles(type), location, maxDistance);
77  	}
78  
79  
80  
81  
82  	
83  //	/**
84  //	 * {@link IncomingProjectile} listener.
85  //	 */
86  //	private class IncomingProjectileListener implements IWorldEventListener<IncomingProjectile> {
87  //
88  //		/**
89  //		 * Constructor. Registers itself on the given WorldView object.
90  //		 * 
91  //		 * @param worldView
92  //		 *            WorldView object to listen to.
93  //		 */
94  //		public IncomingProjectileListener(IWorldView worldView) {
95  //			// FIXME: Doesn't work.
96  //
97  //			worldView.addEventListener(IncomingProjectile.class, this);
98  //		}
99  //
100 //		@Override
101 //		public void notify(IncomingProjectile event) {
102 //			projectiles.add(event);
103 //		}
104 //	}
105 //
106 //	/** {@link IncomingProjectile} listener */
107 //	IncomingProjectileListener incomingProjectileListener;
108 
109 }