View Javadoc

1   package nl.tudelft.pogamut.ut2004.agent.module.shooting.weapon;
2   
3   import nl.tudelft.pogamut.ut2004.agent.module.shooting.AbstractWeaponShooting;
4   import nl.tudelft.pogamut.ut2004.agent.module.shooting.util.FacingUtil;
5   import cz.cuni.amis.pogamut.base3d.worldview.object.ILocated;
6   import cz.cuni.amis.pogamut.ut2004.agent.module.sensomotoric.Weaponry;
7   import cz.cuni.amis.pogamut.ut2004.agent.module.sensor.AgentInfo;
8   import cz.cuni.amis.pogamut.ut2004.agent.module.sensor.WeaponPref;
9   import cz.cuni.amis.pogamut.ut2004.bot.command.ImprovedShooting;
10  import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
11  import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType;
12  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
13  
14  public class AssaultRifleShooting extends AbstractWeaponShooting {
15  
16  	protected static final WeaponPref ASSAULT_RIFLE_PRIMARY = new WeaponPref(ItemType.ASSAULT_RIFLE, true);
17  	protected static final double ASSAULT_RIFLE_SEC_SPLASH_RADIUS = 200;
18  	private static final int ASSAULT_RIFLE_SEC_SPLASH_DAMAGE = 60;
19  
20  	public AssaultRifleShooting(UT2004Bot<?, ?, ?> agent, AgentInfo info, ImprovedShooting shoot, Weaponry weaponry) {
21  		super(agent, info, shoot, weaponry);
22  	}
23  
24  	@Override
25  	protected void shoot() {
26  
27  		// Wrong weapon, wait up.
28  		if (!isWeaponReady()) {
29  			return;
30  		}
31  
32  		// Charge secondary.
33  		if (!hasTarget() && weaponPref.isSecondary()) {
34  			chargeSecondary();
35  			return;
36  		}
37  		// No target, no shoot.
38  		else if (!hasTarget()) {
39  			shoot.stopShooting();
40  			return;
41  		}
42  
43  		boolean facing = FacingUtil.isFacing2D(info, target, FACING_ANGLE);
44  		boolean close = info.getLocation().getDistance(target.getLocation()) < ASSAULT_RIFLE_SEC_SPLASH_RADIUS;
45  		boolean healthy = info.getHealth() > ASSAULT_RIFLE_SEC_SPLASH_DAMAGE;
46  		boolean primary = weaponPref.isPrimary();
47  		boolean safe = !close || healthy || primary;
48  
49  		if (!(target instanceof Player)) {
50  			shootTarget(target, facing, safe);
51  			return;
52  		}
53  
54  		Player player = (Player) target;
55  
56  		if (weaponPref.isPrimary()) {
57  			shootPrimary(player, facing, safe);
58  		} else {
59  			shootSecondary(player, facing, safe);
60  		}
61  	}
62  
63  	/**
64  	 * Shoot the target, if we are facing it and is safe to do so. Won't blow
65  	 * secondary charge if it not safe to do so.
66  	 * 
67  	 * @param target
68  	 * @param facing
69  	 */
70  	protected void shootTarget(ILocated target, boolean facing, boolean safe) {
71  		if (facing && safe) {
72  			shoot.shootPrimary(target);
73  		} else if (!info.isSecondaryShooting()) {
74  			shoot.stopShooting();
75  		}
76  	}
77  
78  	/**
79  	 * Starts shooting secondary mode.
80  	 */
81  	protected void chargeSecondary() {
82  		shoot.shootSecondary();
83  	}
84  
85  	/**
86  	 * Shoots a visible player we are facing with the primary mode, otherwise
87  	 * charges up using secondary.
88  	 * 
89  	 * @param player
90  	 * @param facing
91  	 */
92  	protected void shootSecondary(Player player, boolean facing, boolean safe) {
93  		// Use primary to shoot visible players.
94  		// This will cause the first shot to be a charged shot.
95  		if (player.isVisible() && facing && safe) {
96  			shoot.shootSecondaryCharged(player, 0);
97  		} else {
98  			chargeSecondary();
99  		}
100 	}
101 
102 	/**
103 	 * Shoots primary when it is safe to do so. If secondary was previously
104 	 * shooting it won't blow the charge prematurely.
105 	 * 
106 	 * @param player
107 	 * @param facing
108 	 */
109 	protected void shootPrimary(Player player, boolean facing, boolean safe) {
110 		if (player.isVisible() && facing && safe) {
111 			shoot.shootPrimary(target);
112 		} else if (!info.isSecondaryShooting()) {
113 			shoot.stopShooting();
114 		}
115 	}
116 
117 	@Override
118 	protected WeaponPref getDefaultWeaponPref() {
119 		return ASSAULT_RIFLE_PRIMARY;
120 	}
121 
122 }