View Javadoc

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