-
Notifications
You must be signed in to change notification settings - Fork 9
/
defender.hpp
277 lines (231 loc) · 9.47 KB
/
defender.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#ifndef TDEFEND_HPP
#define TDEFEND_HPP
///TO BE CHANGED
#include <list>
#include "beliefstate.h"
#include "geometry.h"
#include "pose.h"
#include "constants.h"
#define ANGLE_TO_DIST 0
class TDefend
{
static const int offset = 400;
// Corner Case: wall, ball, bot in line
Point2D<int> prevBotPos;
float prevBotAngle;
public:
TDefend()
{
iState = APPROACHING;
} // TDefend
~TDefend()
{ } // ~TDefend
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 executeattack(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);
/* 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 = TDefend::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);
}
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);
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)
{
return Pose(ballPos.x,ballPos.y,Vector2D<int>::angle(ballPos, homePos));
}
else
{
return attackp;
}
}
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);
////needs to be corrected opp_pos
//Vector2D<int> opp_pos(state->awayX[state->oppBotNearestToBall],state->awayY[state->oppBotNearestToBall]);
float ang1,temp1;
float dist2=100000000;
int i;
for(i=0;i<5;i++)
{
Vector2D <int> awayPos(state->awayX[i], state->awayY[i]);
temp1=Vector2D<int>::dist(ballPos,awayPos);
if(dist2>temp1)dist2=temp1;
}
float dist = Vector2D<int>::dist(ballPos, homePos);
//float dist2= Vector2D<int>::dist(ballPos,opp_pos);
/////start changes from here //////
if(ballPos.x>0)
{
if(ballPos.x>HALF_FIELD_MAXX-DBOX_WIDTH)
{
return Pose(-0.4*(HALF_FIELD_MAXX-DBOX_WIDTH),ballPos.y,Vector2D<int>::angle(opp_goal, ballPos));
}
else if( abs(state->ballVx) > 50 && state->ballVx<0 )
{
ang1 = atan(state->ballVy/state->ballVx);
temp1=ballPos.y -((ballPos.x)-(-HALF_FIELD_MAXX + DBOX_WIDTH))*tan(ang1);
if(temp1>HALF_FIELD_MAXY)temp1 = HALF_FIELD_MAXY;
if(temp1<-HALF_FIELD_MAXY)temp1=-HALF_FIELD_MAXY;
return Pose(-0.7*(HALF_FIELD_MAXX-DBOX_WIDTH),temp1,0);
}
else
{
return Pose((0.3*(ballPos.x +(HALF_FIELD_MAXX-DBOX_WIDTH ))-(HALF_FIELD_MAXX-DBOX_WIDTH)),ballPos.y,0);
}
}
else
{
if(abs (dist-dist2)<2*BOT_BALL_THRESH && ballPos.x > -HALF_FIELD_MAXX+1.5*GOAL_DEPTH )
{
///ATTACK FUNCTION
return executeattack(state, botID);
qDebug()<<"1 is called \n";
//return Pose(ballPos.x,ballPos.y,0);
}
// else return Pose(HALF_FIELD_MAXX,ballPos.y,0);
if (ballPos.x < homePos.x && ballPos.x >-HALF_FIELD_MAXX+1.5*GOAL_DEPTH )
{
////ATTACK THE BALL
return executeattack(state, botID);
//return Pose(ballPos.x,ballPos.y,0);
}
if(ballPos.x > homePos.x && ballPos.x >-HALF_FIELD_MAXX+1.5*GOAL_DEPTH )
{
if(state->ballVx<50)
{
//Working properly
return Pose(ballPos.x,ballPos.y,0);
}
if(ballPos.x > -0.7*(HALF_FIELD_MAXX-DBOX_WIDTH))
{
if(state->ballVx > 100){
ang1 = atan(state->ballVy/state->ballVx);
temp1=ballPos.y -((ballPos.x)-(-HALF_FIELD_MAXX + DBOX_WIDTH))*tan(ang1);
if(temp1>HALF_FIELD_MAXY || temp1 < -HALF_FIELD_MAXY)
temp1 = ballPos.y;
}
else{
temp1 = ballPos.y;
}
///CAN BE CHANGED TO HALFFIELDMAXX
qDebug() <<"3 is called \n";
return Pose(-0.7*(HALF_FIELD_MAXX-DBOX_WIDTH),temp1,0);
}
else
{
qDebug()<<"4 is called \n";
//return Pose(ballPos.x,ballPos.y,0);
return executeattack(state, botID);
////Attack the ball
}
}
if(ballPos.x <-HALF_FIELD_MAXX+1.5*GOAL_DEPTH && abs(ballPos.y)>OUR_GOAL_MAXY + 1.8*BOT_RADIUS)
{
qDebug()<<"5 is called \n";
//return Pose(ballPos.x,ballPos.y,0);
return executeattack(state, botID);
///ATTACK THE BALL
}
}
return Pose(ballPos.x, ballPos.y, 0);
}
}; // class TDefend
#endif // TDEFEND_HPP