View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.agent.module.sensomotoric;
2   
3   import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType;
4   import cz.cuni.amis.pogamut.unreal.communication.messages.UnrealId;
5   import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.ChangeWeapon;
6   import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.AddInventoryMsg;
7   import cz.cuni.amis.pogamut.ut2004.communication.translator.itemdescriptor.WeaponDescriptor;
8   import cz.cuni.amis.utils.exception.PogamutException;
9   
10  /**
11   * Class that holds information about the weapon the bot has in its inventory.
12   * <p><p>
13   * It provides information about number of primary and secondary ammo the bot has for the weapon
14   * as well as weapon's {@link WeaponDescriptor}, its {@link ItemType} and inventory {@link UnrealId}.
15   * 
16   * @author Jimmy
17   */
18  public class Weapon {
19  
20  	protected ItemType weaponType = null;
21  	protected int primaryAmmo = 0;
22  	protected int secondaryAmmo = 0;
23  	protected UnrealId inventoryId;
24  	protected WeaponDescriptor descriptor = null;
25  	
26  	protected Weapon(AddInventoryMsg weaponGained, int primaryAmmo, int secondaryAmmo) {
27  		if (weaponGained.getPickupType().getCategory() != ItemType.Category.WEAPON) {
28  			throw new PogamutException("Could not create Weapon class out of inventory item that is not a weapon.", this);
29  		}
30  		this.weaponType = weaponGained.getPickupType();
31  		this.inventoryId = weaponGained.getId();
32  		this.descriptor = (WeaponDescriptor) weaponGained.getDescriptor();
33  		this.primaryAmmo = primaryAmmo;
34  		this.secondaryAmmo = secondaryAmmo;
35  	}
36  	
37  	/**
38  	 * Returns type of the weapon.
39  	 * @return
40  	 */
41  	public ItemType getType() {
42  		return weaponType;
43  	}
44  	
45  	/**
46  	 * Returns group of the weapon.
47  	 * @return
48  	 */
49  	public ItemType.Group getGroup() {
50  		return weaponType.getGroup();
51  	}
52  
53  	/**
54  	 * Returns how many primary ammo the bot is wielding for this weapon.
55  	 * @return
56  	 */
57  	public int getPrimaryAmmo() {
58  		return primaryAmmo;
59  	}
60  
61  	/**
62  	 * Returns how many secondary ammo the bot is wielding for this weapon.
63  	 * @return
64  	 */
65  	public int getSecondaryAmmo() {
66  		return secondaryAmmo;
67  	}
68  
69  	/**
70  	 * Returns inventory ID of the weapon.
71  	 * <p><p>
72  	 * This id is sought to be used with {@link ChangeWeapon} command, use {@link UnrealId#getStringId()} to obtain
73  	 * the string representation of the weapon's inventory ID.
74  	 * 
75  	 * @return
76  	 */
77  	public UnrealId getInventoryId() {
78  		return inventoryId;
79  	}
80  
81  	/**
82  	 * Returns complete descriptor of the weapon containing various information about the weapon behavior in game.
83  	 */
84  	public WeaponDescriptor getDescriptor() {
85  		return descriptor;
86  	}
87  	
88  //	/**
89  //	 * Returns maximal effective distance of the weapon's primary firing mode.
90  //	 * @return
91  //	 */
92  // getPriMaxEffectDistance is almost always 0
93  //	public double getEffectiveDistance() {
94  //		return descriptor.getPriMaxEffectDistance();
95  //	}
96  	
97  	/**
98       * Whether the weapon has secondary ammo different from the primary.
99       * @return
100      */
101     public boolean hasSecondaryAmmoType() {
102     	return getDescriptor().getSecAmmoItemType() != null && getDescriptor().getPriAmmoItemType() != getDescriptor().getSecAmmoItemType();    		
103     }
104 
105 	/**
106 	 * Returns total amount of ammo the bot has for the weapon (both primary and secondary).
107 	 * @return
108 	 */
109 	public int getAmmo() {
110 		return getPrimaryAmmo() + getSecondaryAmmo();
111 	}
112 	
113 	@Override
114 	public String toString() {
115 		if (getType() == null) {
116 			return "Weapon[type=UNKNOWN, primary ammo=" + getPrimaryAmmo() + ", secondary ammo=" + getSecondaryAmmo() + "]";
117 		} else
118 		if (hasSecondaryAmmoType()) {
119 			return "Weapon[type=" + getType().getName() + ", primary ammo=" + getPrimaryAmmo() + ", secondary ammo=" + getSecondaryAmmo() + "]";
120 		} else {
121 			return "Weapon[type=" + getType().getName() + ", ammo=" + getPrimaryAmmo() + "]";
122 		}
123 	}
124 
125 }