View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.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.ut2004.bot.IUT2004BotController;
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.translator.itemdescriptor.ItemDescriptor;
14  import cz.cuni.amis.pogamut.ut2004.communication.translator.itemdescriptor.WeaponDescriptor;
15  import cz.cuni.amis.pogamut.ut2004.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 IUT2004BotController#prepareBot(UT2004Bot)} method call
24   * and may be used since {@link IUT2004BotController#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<UT2004Bot> {
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  		if (ammoType == null) return null;
44  		return ammoToWeapon.get(ammoType);
45  	}
46  	
47  	/**
48  	 * Tells whether the descriptor for given 'itemType' exists.
49  	 * @param itemType
50  	 * @return whether the descriptor for given 'itemType' exists
51  	 */
52  	public boolean hasDescriptor(ItemType itemType) {
53  		if (itemType == null) return false;
54  		return descs.containsKey(itemType);
55  	}
56  	
57  	/**
58  	 * Returns the descriptor for the given 'itemType'.
59  	 * @param itemType
60  	 * @return descriptor for given 'itemType' exists
61  	 */
62  	public ItemDescriptor getDescriptor(ItemType itemType) {
63  		if (itemType == null) return null;
64  		return descs.get(itemType);
65  	}
66  	
67  	/**
68  	 * Tells whether the descriptor for given 'inventoryType' exists.
69  	 * @param itemType
70  	 * @return whether the descriptor for given 'inventoryType' exists
71  	 */
72  	public boolean hasDescriptor(String inventoryType) {
73  		if (inventoryType == null) return false;
74  		return inventoryTypeDescs.containsKey(inventoryType);
75  	}
76  	
77  	/**
78  	 * Returns the descriptor for the given 'inventoryType'.
79  	 * @param itemType
80  	 * @return descriptor for given 'inventoryType' exists
81  	 */
82  	public ItemDescriptor getDescriptor(String inventoryType) {
83  		if (inventoryType == null) return null;
84  		return inventoryTypeDescs.get(inventoryType);
85  	}
86  	
87  	/*========================================================================*/
88  	
89  	/**
90  	 * {@link ItemDescriptorObtained} listener.
91  	 */
92  	private class ItemDescriptorObtainedListener implements IWorldEventListener<ItemDescriptorObtained> {
93  		private IWorldView worldView;
94  
95  		/**
96  		 * Constructor. Registers itself on the given WorldView object.
97  		 * @param worldView WorldView object to listent to.
98  		 */
99  		public ItemDescriptorObtainedListener(IWorldView worldView)
100 		{
101 			worldView.addEventListener(ItemDescriptorObtained.class, this);
102 			this.worldView = worldView;
103 		}
104 
105 		@Override
106 		public void notify(ItemDescriptorObtained event) {
107 			log.info("Processing: " + event);
108 			if (event.getItemDescriptor() == null) {
109 				return;
110 			}
111 			inventoryTypeDescs.put(event.getItemDescriptor().getInventoryType(), event.getItemDescriptor());
112 			descs.put(event.getItemDescriptor().getPickupType(), event.getItemDescriptor());
113 			if (event.getItemDescriptor() instanceof WeaponDescriptor) {
114 				WeaponDescriptor desc = (WeaponDescriptor)event.getItemDescriptor();
115 				if (desc.getPriAmmoItemType() != null) {
116 					ammoToWeapon.put(desc.getPriAmmoItemType(), desc.getPickupType());
117 				}
118 				if (desc.getSecAmmoItemType() != null) {
119 					ammoToWeapon.put(desc.getSecAmmoItemType(), desc.getPickupType());
120 				}
121 			}
122 		}
123 	}
124 
125 	/** {@link ItemDescriptorObtained} listener */
126 	private ItemDescriptorObtainedListener itemDescObtainedListener;
127 	
128 	/**
129 	 * Constructor. Setups the memory module based on bot's world view.
130 	 * @param bot owner of the module
131 	 */
132 	public ItemDescriptors(UT2004Bot bot) {
133 		this(bot, null);
134 		
135 	}
136 
137 	public ItemDescriptors(UT2004Bot bot, LogCategory moduleLog) {
138 		super(bot, moduleLog);
139 		itemDescObtainedListener = new ItemDescriptorObtainedListener(bot.getWorldView());
140 		
141 		cleanUp();
142 	}
143 	
144 	@Override
145 	protected void cleanUp() {
146 		super.cleanUp();
147 		inventoryTypeDescs.clear();
148 		descs.clear();
149 	}
150 	
151 }