View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.communication.translator.itemdescriptor;
2   
3   import java.util.HashMap;
4   
5   import cz.cuni.amis.pogamut.base.communication.exception.CommunicationException;
6   import cz.cuni.amis.pogamut.base.utils.guice.AgentScoped;
7   import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType;
8   import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.ItemCategory;
9   
10  /**
11   * Main class responsible for the item decoration.
12   * <p><p>
13   * Items in UT2004 has a lots of characteristics which don't change over the time (at least during one game).
14   * As it is pointless to send all those information every time an item is perceived by a bot, those information 
15   * are sent through the ITCMsgs (ItemCategory). This message is used in ItemTranslator as a configuration message for an ItemDescriptor.
16   * <p><p>
17   * ItemDescriptor contains all characteristics available for the corresponding UTClass of items and is returned by the 
18   * ItemTranslator. This description is then attached to the item (in fact to all item events like AIN, INV, IPK).
19   * <p><p>
20   * Now how does it work insight? ItemTranslator uses a set of DescriptorFactories (one for each type of an item).
21   * ITCMsg messages are usually sent at the beginning of the game (classes for all items in the map). 
22   * But they can arrive in the middle of the game for a new category of an item.
23   * 
24   * TODO: maybe it is rather ItemDecorator.
25   * 
26   * @author Ondrej
27   */
28  @AgentScoped
29  public class ItemTranslator {
30  
31      private HashMap<ItemType, ItemDescriptor> descriptors = new HashMap<ItemType, ItemDescriptor>();
32  
33      private HashMap<ItemType, GeneralDescriptor> userDescriptors = new HashMap<ItemType, GeneralDescriptor>();
34  
35      public ItemTranslator() {
36      }
37  
38      public ItemType[] getItemTypes() {
39          return descriptors.values().toArray(new ItemType[0]);
40      }
41  
42      public ItemDescriptor getDescriptor(ItemTyped msg) {
43          return getDescriptor(msg.getType());
44      }
45  
46      /**
47       * Gets descriptor for this item.
48       *
49       * NOTE: User descriptors will override default Pogamut descriptors.
50       *
51       * @param type
52       * @return
53       */
54      public ItemDescriptor getDescriptor(ItemType type) {
55          //user may override our default descriptors
56          if (userDescriptors.containsKey(type))
57              return userDescriptors.get(type);
58          return descriptors.get(type);
59      }
60  
61  
62      /**
63       * Gets default Pogamut descriptor for this item.
64       *
65       * @param type
66       * @return
67       */
68      public ItemDescriptor getDefaultDescriptor(ItemType type) {
69          return descriptors.get(type);
70      }
71  
72      /**
73       * Adds custom user descriptor.
74       *
75       * @param userDescriptor
76       */
77      public void addCustomUserDescriptor(GeneralDescriptor userDescriptor) {
78          userDescriptors.put(userDescriptor.getPickupType(), userDescriptor);
79      }
80  
81      /**
82       * Default Pogamut descriptors will be created for all UT2004 items.
83       * 
84       * @param message
85       */
86      public ItemDescriptor createDescriptor(ItemCategory message) {
87      	ItemDescriptor result = null;
88          switch (message.getType().getCategory()) {
89              case AMMO:
90                  descriptors.put(message.getType(), result = AmmoDescriptorFactory.getInstance().getNewDescriptor(message));
91                  break;
92              case ARMOR:
93                  descriptors.put(message.getType(), result = ArmorDescriptorFactory.getInstance().getNewDescriptor(message));
94                  break;
95              case OTHER:
96                  descriptors.put(message.getType(), result = OtherDescriptorFactory.getInstance().getNewDescriptor(message));
97                  break;
98              case HEALTH:
99                  descriptors.put(message.getType(), result = HealthDescriptorFactory.getInstance().getNewDescriptor(message));
100                 break;
101             case SHIELD:
102                 descriptors.put(message.getType(), result = ShieldDescriptorFactory.getInstance().getNewDescriptor(message));
103                 break;
104             case ADRENALINE:
105                 descriptors.put(message.getType(), result = AdrenalineDescriptorFactory.getInstance().getNewDescriptor(message));
106                 break;
107             case WEAPON:
108                 descriptors.put(message.getType(), result = WeaponDescriptorFactory.getInstance().getNewDescriptor(message));
109                 break;
110             default:
111                 throw new CommunicationException("should not reach here - new ItemType.Category has been added and not handled inside the ItemTranslator, item type = " + message.getType(), this);
112         }
113         return result;
114     }
115 }