forked from kektobiologist/motion-simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attacker.hpp
216 lines (189 loc) · 7.27 KB
/
attacker.hpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#ifndef TTATTACK_HPP
#define TTATTACK_HPP
#include <list>
#include "beliefstate.h"
#include "geometry.h"
#include "pose.h"
#include "constants.h"
#define ANGLE_TO_DIST 0
class TAttack
{
static const int offset = 400;
// Corner Case: wall, ball, bot in line
Point2D<int> prevBotPos;
float prevBotAngle;
public:
TAttack()
{
iState = APPROACHING;
} // TAttack
~TAttack()
{ } // ~TAttack
enum InternalState
{
APPROACHING,
ATTACKING
} iState;
int hasAchievedOffset;
inline bool isActiveTactic(void) const
{
return true;
}
bool pointxInField(Vector2D<int> final)
{
if((final.x < HALF_FIELD_MAXX - (BALL_AT_CORNER_THRESH) && final.x > -HALF_FIELD_MAXX + (BALL_AT_CORNER_THRESH)))
{
if((final.y < HALF_FIELD_MAXY - BALL_AT_CORNER_THRESH && final.y > -HALF_FIELD_MAXY + BALL_AT_CORNER_THRESH))
{
return true;
}
else return false;
}
else return false;
}
Pose execute(BeliefState *state,int botID)
{
Vector2D<int> ballPos(state->ballX,state->ballY);
Vector2D<int> homePos(state->homeX[botID], state->homeY[botID]);
Vector2D<int> opp_goal(HALF_FIELD_MAXX, 0);
static Vector2D<float> lastVel[10];
static int index = 0;
if(index < 10) {
lastVel[index].x = state->ballVx;
lastVel[index].y = state->ballVy;
index = (index + 1) % 10;
}
Vector2D<float> avgBallVel(0.0,0.0);
for(int i=0;i<10;i++) {
avgBallVel.x += lastVel[i].x;
avgBallVel.y += lastVel[i].y;
}
avgBallVel.x /= 10.0;
avgBallVel.y /= 10.0;
float dist = Vector2D<int>::dist(ballPos, homePos);
if(dist < BOT_BALL_THRESH)
{
/* Ball is with bot. So go to goal */
printf("\n Going to Ball \n ");
//sParam.GoToPointP.align = false;
//tState = RUNNING;
Pose appro(HALF_FIELD_MAXX,0,Vector2D<int>::angle(opp_goal, ballPos));
iState = ATTACKING;
return appro;
}
else
{
/* Ball is not with bot so go to ball first */
//sParam.GoToPointP.align = true;
float ballgoaldist = Vector2D<int>::dist(ballPos, opp_goal);
float factor = (int)Vector2D<int>::dist(ballPos,homePos);
factor /= 5*MAX_BOT_SPEED;
factor =0;
//int ballPosX = state->ballPos.x;// + factor * state->ballVel.x;
//int ballPosY = state->ballPos.y;// + factor * state->ballVel.y;
float offset = TAttack::offset;//TAttack::offset * state->ballVel.abs()/6000.0+
if(homePos.x < ballPos.x)
offset = 0;
int x3 = (ballPos.x * (ballgoaldist + offset) - offset * HALF_FIELD_MAXX) / ballgoaldist;
int y3 = (ballPos.y * (ballgoaldist + offset)) / ballgoaldist;
/// logarithmic search to place offset point in field. */
//float offset = 600;
/****************************** added velocity factor in offset *****************/
factor = 0.4;
int targetX=0,targetY=0;
{
printf("Ball Veclocity : x = %f y = %f\n",avgBallVel.x,avgBallVel.y);
int ballBotDist = (int)Vector2D<int>::dist(homePos,ballPos);
printf("delta x = %d delta y = %d\n",(int)(factor * state->ballVx),(int)(factor * state->ballVy));
targetX = ballPos.x + (int)(factor * avgBallVel.x);
targetY = ballPos.y + (int)(factor * avgBallVel.y);
//x3 = targetX;//(targetX * (ballgoaldist + offset) - offset * OPP_GOAL_X) / ballgoaldist;
//y3 = targetY;//(targetY * (ballgoaldist + offset)) / ballgoaldist;
}
/**************************** done velocity factor ****************************/
/*************************** Weighted offset *********************************/
//weighted value of previously calculated offset and one using ball velocity
// P = (P1 + W*P2)/(W + 1)
float weight = (avgBallVel.x < -100 ? -(avgBallVel.x)-100 : 0.00)*sqrt(avgBallVel.y*avgBallVel.y) / (200.0);
Vector2D<int> final(0,0);
final.x = x3 + weight * targetX;
final.y = y3 + weight * targetY;
final.x = final.x/(weight + 1);
final.y = final.y/(weight + 1);
x3 = final.x, y3 = final.y;
/************************ Done offset ***************************************/
//SkillSet::comm->addCircle(x3,y3,300);
while(isPointinField(Point2D<int>(x3, y3)))
{
if(isPointinField(ballPos))
{
offset= 0;
x3 = (ballPos.x * (ballgoaldist + offset) - offset * HALF_FIELD_MAXX) / ballgoaldist;
y3 = (ballPos.y * (ballgoaldist + offset)) / ballgoaldist;
break;
}
offset /= 1.25;
if(offset <= 1.0)
break;
x3 = (ballPos.x * (ballgoaldist + offset) - offset * HALF_FIELD_MAXX) / ballgoaldist;
y3 = (ballPos.y * (ballgoaldist + offset)) / ballgoaldist;
}
offset/=1.25;
/// log search to place offset at a point not co-inciding with a bot.
Point2D<int> offsetpt(x3,y3);
/*while(1)
{
bool flag = false;
for(int i=0; i < 5; i++)
{
Vector2D<int> homePosi(state->homeX[i],state->awayX[i]);
if(intersects(offsetpt, homePosi, (int)BOT_RADIUS*3))
{
if(i == botID)
continue;
flag = true;
offset /= 1.1;
}
}
for(int i=0; i < 5; i++)
{
Vector2D<int> awayPosi(state->awayX[i], state->awayY[i]);
if(intersects(offsetpt, awayPosi, (int)BOT_RADIUS*3))
{
flag = true;
offset /= 1.1;
}
}
//x3 = (ballPosX * (ballgoaldist + offset) - offset * OPP_GOAL_X) / ballgoaldist;
//y3 = (ballPosY * (ballgoaldist + offset)) / ballgoaldist;
if(!flag)
break;
if(offset <= 2.0)
{
offset = 0;
//x3 = (ballPosX * (ballgoaldist + offset) - offset * OPP_GOAL_X) / ballgoaldist;
//y3 = (ballPosY * (ballgoaldist + offset)) / ballgoaldist;
break;
}
}*/
int dist2 = Vector2D<int>::dist(offsetpt, homePos);
if(dist2 < 200)
hasAchievedOffset = 1;
else if(dist > 2 * offset)
hasAchievedOffset = 0;
if(ballPos.x < homePos.x)
hasAchievedOffset = 0;
Pose attackp(x3,y3,Vector2D<int>::angle(opp_goal,ballPos));
if(hasAchievedOffset)
{
Pose attackpt(ballPos.x,ballPos.y,Vector2D<int>::angle(ballPos, homePos));
return attackpt;
}
else
{
return attackp;
}
}
}
}; // class TAttack
#endif // TTCharge_HPP