View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.agent.module.sensomotoric;
2   
3   import java.util.logging.Level;
4   
5   import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
6   import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
7   import cz.cuni.amis.pogamut.base.utils.logging.LogCategory;
8   import cz.cuni.amis.pogamut.ut2004.agent.module.sensor.ItemDescriptors;
9   import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
10  import cz.cuni.amis.pogamut.ut2004.communication.messages.UT2004ItemType;
11  import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType.Category;
12  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.ItemPickedUp;
13  import cz.cuni.amis.pogamut.ut2004.communication.translator.itemdescriptor.ItemDescriptor;
14  import cz.cuni.amis.pogamut.ut2004.communication.translator.itemdescriptor.WeaponDescriptor;
15  
16  /**
17   * Handles specific needs of UT2004 with regards to weaponry. Currently fixes
18   * broken item descriptors.
19   * 
20   * @author mpkorstanje
21   * 
22   */
23  public class UT2004Weaponry extends Weaponry {
24  
25  	private class RedeemerPickedUpListener implements
26  			IWorldEventListener<ItemPickedUp> {
27  
28  		/**
29  		 * Constructor. Registers itself on the given WorldView object.
30  		 * 
31  		 * @param worldView
32  		 *            WorldView object to listent to.
33  		 */
34  		public RedeemerPickedUpListener(IWorldView worldView) {
35  			worldView.addEventListener(ItemPickedUp.class, this);
36  		}
37  
38  		@Override
39  		public void notify(ItemPickedUp event) {
40  			if (event == null)
41  				return;
42  			if (event.getType() == null)
43  				return;
44  			if (event.getType().getCategory() != Category.AMMO
45  					&& event.getType().getCategory() != Category.WEAPON) {
46  				return;
47  			}
48  
49  			if (event.getDescriptor().getPickupType() == UT2004ItemType.REDEEMER
50  					|| event.getDescriptor().getPickupType() == UT2004ItemType.REDEEMER_AMMO
51  					|| event.getDescriptor().getPickupType() == UT2004ItemType.ION_PAINTER
52  					|| event.getDescriptor().getPickupType() == UT2004ItemType.ION_PAINTER_AMMO)
53  
54  			{
55  
56  				ItemDescriptor descriptor = itemDescriptors.getDescriptor(event
57  						.getType());
58  
59  				if (descriptor == null) {
60  					if (log.isLoggable(Level.WARNING))
61  						log.warning("RedeemerPickedUpListener.notify(): There is no ItemDescriptor for the item type "
62  								+ event.getType() + "!");
63  					return;
64  				}
65  
66  				// UT2004 BUG !!! ... desc.getAmount()/priAmmo()/secAmmo() is 0
67  				// !!! ... it should be 1 !!!
68  				if (descriptor.getItemCategory() == Category.AMMO) {
69  					ammo.weaponUpdate(event.getType(), 1);
70  				} else if (descriptor.getItemCategory() == Category.WEAPON) {
71  					WeaponDescriptor desc = (WeaponDescriptor) descriptor;
72  					ammo.weaponUpdate(desc.getPriAmmoItemType(), 1);
73  				}
74  
75  				return;
76  			}
77  		}
78  	}
79  
80  	private RedeemerPickedUpListener redeemerPickedUpListener;
81  
82  	/**
83  	 * Constructor. Setups the memory module for a given bot.
84  	 * 
85  	 * @param bot
86  	 *            owner of the module that is using it
87  	 * @param agentInfo
88  	 *            AgentInfo memory module.
89  	 * @param itemDescriptors
90  	 *            ItemDescriptors memory module.
91  	 * @param moduleLog
92  	 */
93  	public UT2004Weaponry(UT2004Bot bot, ItemDescriptors descriptors,
94  			LogCategory moduleLog) {
95  		super(bot);
96  
97  		redeemerPickedUpListener = new RedeemerPickedUpListener(worldView);
98  	}
99  	
100 	
101 	  /**
102      * Constructor. Setups the memory module for a given bot.
103      * @param bot owner of the module that is using it
104      * @param agentInfo AgentInfo memory module.
105      * @param itemDescriptors ItemDescriptors memory module.
106      */
107     public UT2004Weaponry(UT2004Bot bot, ItemDescriptors itemDescriptors) {
108         this(bot, itemDescriptors, null);
109     }
110     
111 
112 }