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