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
19
20
21
22
23
24
25
26
27
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
39
40
41
42 public ItemType getWeaponForAmmo(ItemType ammoType) {
43 if (ammoType == null) return null;
44 return ammoToWeapon.get(ammoType);
45 }
46
47
48
49
50
51
52 public boolean hasDescriptor(ItemType itemType) {
53 if (itemType == null) return false;
54 return descs.containsKey(itemType);
55 }
56
57
58
59
60
61
62 public ItemDescriptor getDescriptor(ItemType itemType) {
63 if (itemType == null) return null;
64 return descs.get(itemType);
65 }
66
67
68
69
70
71
72 public boolean hasDescriptor(String inventoryType) {
73 if (inventoryType == null) return false;
74 return inventoryTypeDescs.containsKey(inventoryType);
75 }
76
77
78
79
80
81
82 public ItemDescriptor getDescriptor(String inventoryType) {
83 if (inventoryType == null) return null;
84 return inventoryTypeDescs.get(inventoryType);
85 }
86
87
88
89
90
91
92 private class ItemDescriptorObtainedListener implements IWorldEventListener<ItemDescriptorObtained> {
93 private IWorldView worldView;
94
95
96
97
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
126 private ItemDescriptorObtainedListener itemDescObtainedListener;
127
128
129
130
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 }