View Javadoc

1   package cz.cuni.amis.pogamut.udk.agent.module.sensor;
2   
3   import java.util.HashMap;
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.IWorldView;
8   import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
9   import cz.cuni.amis.pogamut.base.utils.logging.LogCategory;
10  import cz.cuni.amis.pogamut.udk.bot.IUDKBotController;
11  import cz.cuni.amis.pogamut.udk.bot.impl.UDKBot;
12  import cz.cuni.amis.pogamut.udk.communication.messages.ItemType;
13  import cz.cuni.amis.pogamut.udk.communication.translator.itemdescriptor.ItemDescriptor;
14  import cz.cuni.amis.pogamut.udk.communication.translator.itemdescriptor.WeaponDescriptor;
15  import cz.cuni.amis.pogamut.udk.communication.translator.shared.events.ItemDescriptorObtained;
16  
17  /**
18   * Sensory module that provides mapping between {@link ItemType} and {@link ItemDescriptor} providing
19   * an easy way to obtain item descriptors for various items in UT2004.
20   * <p><p>
21   * Additionally it provides ammo-&gt;weapon mapping via {@link ItemDescriptors#getWeaponForAmmo(ItemType)}.
22   * <p><p>
23   * It is designed to be initialized inside {@link IUDKBotController#prepareBot(UDKBot)} method call
24   * and may be used since {@link IUDKBotController#botInitialized(cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.GameInfo, cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.ConfigChange, cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.InitedMessage)}
25   * is called.
26   * 
27   * @author Jimmy
28   */
29  public class ItemDescriptors extends SensorModule<UDKBot> {
30  
31  	private Map<String, ItemDescriptor> inventoryTypeDescs = new HashMap<String, ItemDescriptor>();
32  	
33  	private Map<ItemType, ItemDescriptor> descs = new HashMap<ItemType, ItemDescriptor>();
34  	
35  	private HashMap<ItemType, ItemType> ammoToWeapon = new HashMap<ItemType, ItemType>();
36  	
37  	/**
38  	 * Returns a weapon type for the given 'ammoType'.
39  	 * @param ammoType
40  	 * @return
41  	 */
42  	public ItemType getWeaponForAmmo(ItemType ammoType) {
43  		return ammoToWeapon.get(ammoType);
44  	}
45  	
46  	/**
47  	 * Tells whether the descriptor for given 'itemType' exists.
48  	 * @param itemType
49  	 * @return whether the descriptor for given 'itemType' exists
50  	 */
51  	public boolean hasDescriptor(ItemType itemType) {
52  		return descs.containsKey(itemType);
53  	}
54  	
55  	/**
56  	 * Returns the descriptor for the given 'itemType'.
57  	 * @param itemType
58  	 * @return descriptor for given 'itemType' exists
59  	 */
60  	public ItemDescriptor getDescriptor(ItemType itemType) {
61  		return descs.get(itemType);
62  	}
63  	
64  	/**
65  	 * Tells whether the descriptor for given 'inventoryType' exists.
66  	 * @param itemType
67  	 * @return whether the descriptor for given 'inventoryType' exists
68  	 */
69  	public boolean hasDescriptor(String inventoryType) {
70  		return inventoryTypeDescs.containsKey(inventoryType);
71  	}
72  	
73  	/**
74  	 * Returns the descriptor for the given 'inventoryType'.
75  	 * @param itemType
76  	 * @return descriptor for given 'inventoryType' exists
77  	 */
78  	public ItemDescriptor getDescriptor(String inventoryType) {
79  		return inventoryTypeDescs.get(inventoryType);
80  	}
81  	
82  	/*========================================================================*/
83  	
84  	/**
85  	 * {@link ItemDescriptorObtained} listener.
86  	 */
87  	private class ItemDescriptorObtainedListener implements IWorldEventListener<ItemDescriptorObtained> {
88  		private IWorldView worldView;
89  
90  		/**
91  		 * Constructor. Registers itself on the given WorldView object.
92  		 * @param worldView WorldView object to listent to.
93  		 */
94  		public ItemDescriptorObtainedListener(IWorldView worldView)
95  		{
96  			worldView.addEventListener(ItemDescriptorObtained.class, this);
97  			this.worldView = worldView;
98  		}
99  
100 		@Override
101 		public void notify(ItemDescriptorObtained event) {
102 			inventoryTypeDescs.put(event.getItemDescriptor().getInventoryType(), event.getItemDescriptor());
103 			descs.put(event.getItemDescriptor().getPickupType(), event.getItemDescriptor());
104 			if (event.getItemDescriptor() instanceof WeaponDescriptor) {
105 				WeaponDescriptor desc = (WeaponDescriptor)event.getItemDescriptor();
106 				if (desc.getPriAmmoItemType() != null) {
107 					ammoToWeapon.put(desc.getPriAmmoItemType(), desc.getPickupType());
108 				}
109 				if (desc.getSecAmmoItemType() != null) {
110 					ammoToWeapon.put(desc.getSecAmmoItemType(), desc.getPickupType());
111 				}
112 			}
113 		}
114 	}
115 
116 	/** {@link ItemDescriptorObtained} listener */
117 	private ItemDescriptorObtainedListener itemDescObtainedListener;
118 	
119 	/**
120 	 * Provides initialization of the module (clearing internal data structures). Called automatically
121 	 * during the agent starting sequence.
122 	 */
123 	@Override
124 	protected void start(boolean startPaused) {
125 		super.start(startPaused);
126 		inventoryTypeDescs.clear();
127 		descs.clear();
128 	}
129 	
130 	/**
131 	 * Constructor. Setups the memory module based on bot's world view.
132 	 * @param bot owner of the module
133 	 */
134 	public ItemDescriptors(UDKBot bot) {
135 		this(bot, null);
136 		
137 	}
138 
139 	public ItemDescriptors(UDKBot bot, LogCategory moduleLog) {
140 		super(bot, moduleLog);
141 		itemDescObtainedListener = new ItemDescriptorObtainedListener(bot.getWorldView());
142 	}
143 	
144 }