-
Notifications
You must be signed in to change notification settings - Fork 1
/
my_bot.ts
executable file
·166 lines (134 loc) · 5.51 KB
/
my_bot.ts
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
`use strict`;
import {
Bot,
GameSnapshotInspector,
Lugo,
Mapper,
PLAYER_STATE,
distanceBetweenPoints,
geo,
SPECS,
Region,
DIRECTION, ORIENTATION
} from '@lugobots/lugo4node'
import {getMyExpectedPosition} from './settings';
type MethodReturn = Lugo.Order[] | { orders: Lugo.Order[]; debug_message: string; } | null;
export class MyBot implements Bot {
side: Lugo.Team.Side;
number: number;
initPosition: Lugo.Point;
mapper: Mapper;
constructor(side: Lugo.Team.Side, number: number, initPosition: Lugo.Point, mapper: Mapper) {
this.side = side
this.number = number
this.mapper = mapper
this.initPosition = initPosition
}
onDisputing(inspector: GameSnapshotInspector): MethodReturn {
try {
const orders = []
const me = inspector.getMe()
const ballPosition = inspector.getBall().getPosition()
const ballRegion = this.mapper.getRegionFromPoint(ballPosition)
const myRegion = this.mapper.getRegionFromPoint(me.getPosition())
// by default, I will stay at my tactic position
let moveDestination = getMyExpectedPosition(inspector, this.mapper, this.number)
// if the ball is max 2 blocks away from me, I will move toward the ball
if (this.isINear(myRegion, ballRegion)) {
moveDestination = ballPosition
}
const moveOrder = inspector.makeOrderMoveMaxSpeed(moveDestination)
// Try other ways to create a move Order
// const moveOrder = reader.makeOrderMoveByDirection(DIRECTION.BACKWARD)
// we can ALWAYS try to catch the ball if we are not holding it
const catchOrder = inspector.makeOrderCatch()
orders.push(moveOrder, catchOrder)
return orders
} catch (e) {
console.log(`did not play this turn`, e)
return null
}
}
onDefending(inspector: GameSnapshotInspector): MethodReturn {
try {
const orders = []
const me = inspector.getMe()
const ballPosition = inspector.getBall().getPosition()
const ballRegion = this.mapper.getRegionFromPoint(ballPosition)
const myRegion = this.mapper.getRegionFromPoint(me.getPosition())
// by default, I will stay at my tactic position
let moveDestination = getMyExpectedPosition(inspector, this.mapper, this.number)
// if the ball is max 2 blocks away from me, I will move toward the ball
if (this.isINear(myRegion, ballRegion)) {
moveDestination = ballPosition
}
const moveOrder = inspector.makeOrderMoveMaxSpeed(moveDestination)
const catchOrder = inspector.makeOrderCatch()
orders.push(moveOrder, catchOrder)
return orders
} catch (e) {
console.log(`did not play this turn`, e)
return null
}
}
onHolding(inspector: GameSnapshotInspector): MethodReturn {
try {
const orders = []
const me = inspector.getMe()
const attackGoalCenter = this.mapper.getAttackGoal().getCenter();
const opponentGoal = this.mapper.getRegionFromPoint(attackGoalCenter)
const currentRegion = this.mapper.getRegionFromPoint(me.getPosition())
let myOrder;
if (this.isINear(currentRegion, opponentGoal)) {
myOrder = inspector.makeOrderKickMaxSpeed(attackGoalCenter)
} else {
myOrder = inspector.makeOrderMoveMaxSpeed(attackGoalCenter)
}
orders.push(myOrder)
return orders
} catch (e) {
console.log(`did not play this turn`, e)
return null
}
}
onSupporting(inspector: GameSnapshotInspector): MethodReturn {
try {
const orders = []
const me = inspector.getMe()
const ballHolderPosition = inspector.getBall().getPosition()
const myOrder = inspector.makeOrderMoveMaxSpeed(ballHolderPosition)
orders.push(myOrder)
return orders
} catch (e) {
console.log(`did not play this turn`, e)
return null
}
}
asGoalkeeper(inspector: GameSnapshotInspector, state: PLAYER_STATE): MethodReturn {
try {
const orders = []
const me = inspector.getMe()
let position = inspector.getBall().getPosition()
if (state !== PLAYER_STATE.DISPUTING_THE_BALL) {
position = this.mapper.getDefenseGoal().getCenter()
}
const myOrder = inspector.makeOrderMoveMaxSpeed(position)
orders.push(myOrder, inspector.makeOrderCatch())
return orders
} catch (e) {
console.log(`did not play this turn`, e)
return null
}
}
gettingReady(inspector: GameSnapshotInspector): void {
// This method is called when the score is changed or before the game starts.
// We can change the team strategy or do anything else based on the outcome of the game so far.
// for now, we are not going anything here.
}
private isINear(myPosition: Region, targetPosition: Region): boolean {
const minDist = 2;
const colDist = myPosition.getCol() - targetPosition.getCol()
const rowDist = myPosition.getRow() - targetPosition.getRow()
return Math.hypot(colDist, rowDist) <= minDist
}
}