-
Notifications
You must be signed in to change notification settings - Fork 0
/
DummyFlagLock.java
116 lines (94 loc) · 3.79 KB
/
DummyFlagLock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import core.Vector;
import core.ai.AiFlagInfo;
import core.ai.AiMapInfo;
import core.ai.AiPlayerInfo;
import core.ai.AiZombieInfo;
import core.constants.ZombieConstants;
import core.player.PlayerController;
public class DummyFlagLock extends PlayerController
{
final float CTRL_NOISE_SCALE = 1.1f;
final float CTRL_ESCAPE_PRIOR = 2.0f;
final float CTRL_FLAG_PRIOR = 1.0f;
final float CTRL_ESCAPE_POWER = 2.0f;
final boolean CTRL_FLAG_LOCK_ENABLED = true;
int[] m_lockedFlags = null;
boolean[] m_lastOwnFlags = null;
public String getName()
{
return "Dummy";
}
public String getAuthor()
{
return "iftrue";
}
public void onGameStarted( String gameTypeName, AiMapInfo map, AiPlayerInfo ownPlayer )
{
if( CTRL_FLAG_LOCK_ENABLED )
{
// Initialize flag lock states
m_lockedFlags = new int[ map.getNumFlags() ];
m_lastOwnFlags = new boolean[ map.getNumFlags() ];
for( int i = 0; i < m_lockedFlags.length; m_lockedFlags[ i ] = 0, m_lastOwnFlags[ i++ ] = false );
}
}
@Override
public Vector think( AiMapInfo map, AiPlayerInfo ownPlayer )
{
Vector pos = ownPlayer.getPosition();
// Nearest flag information
AiFlagInfo[] flags = map.getFlags();
Vector flagMovement = new Vector( Float.MAX_VALUE, Float.MAX_VALUE );
for( int i = 0; i < flags.length; ++i )
{
if( CTRL_FLAG_LOCK_ENABLED )
// Decrease lock state counters for flags (to avoid two bots "fighting" for same flag)
--m_lockedFlags[ i ];
if( flags[ i ].isOwner( ownPlayer ) )
{
if( CTRL_FLAG_LOCK_ENABLED )
{
if( !m_lastOwnFlags[ i ] )
// Lock flag for 20 steps
m_lockedFlags[ i ] = 20;
}
continue;
}
if( CTRL_FLAG_LOCK_ENABLED )
// Save if it is our flag this turn
m_lastOwnFlags[ i ] = flags[ i ].isOwner( ownPlayer );
Vector m = flags[ i ].getPosition().sub( pos );
if( ( !CTRL_FLAG_LOCK_ENABLED || m_lockedFlags[ i ] <= 0 ) && m.length() < flagMovement.length() )
flagMovement = m;
}
// This will fail if we own all flags
flagMovement.normalize();
// Escape movement
float radius = ownPlayer.getCurrentNoiseRadius() * CTRL_NOISE_SCALE;
Vector escape = new Vector( 0,0 );
AiZombieInfo[] zombies = map.getZombiesInRadius( pos, radius );
for( int i = 0; i < zombies.length; ++i )
{
Vector thisZombieVect = zombies[ i ].getPosition().sub( pos );
escape.addReference( thisZombieVect.mult(
-(float)Math.pow( radius / thisZombieVect.length(), CTRL_ESCAPE_POWER )
) );
}
if( escape.length() != 0 )
escape.normalize();
Vector movement = escape.mult( CTRL_ESCAPE_PRIOR ).add(
flagMovement.mult( CTRL_FLAG_PRIOR )
);
if( movement.length() == 0 )
return new Vector( 0, 0 );
movement.normalize();
// Don't fall out of the world
float distanceToMid = pos.add( movement ).length();
while( distanceToMid > ZombieConstants.MAP_RADIUS )
{
movement = movement.add( pos.mult( -1.0f ) );
distanceToMid = pos.add( movement ).length();
}
return movement.getNoramlized();
}
}