-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.lua
281 lines (262 loc) · 9.36 KB
/
bot.lua
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
278
279
280
281
-- 初始化全局变量来存储最新的游戏状态和游戏主机进程。
LatestGameState = LatestGameState or nil
InAction = InAction or false -- 防止代理同时采取多个操作。
Logs = Logs or {}
colors = {
red = "\27[31m",
green = "\27[32m",
blue = "\27[34m",
reset = "\27[0m",
gray = "\27[90m"
}
function addLog(msg, text) -- 函数定义注释用于性能,可用于调试
Logs[msg] = Logs[msg] or {}
table.insert(Logs[msg], text)
end
-- 检查两个点是否在给定范围内。
-- @param x1, y1: 第一个点的坐标
-- @param x2, y2: 第二个点的坐标
-- @param range: 点之间允许的最大距离
-- @return: Boolean 指示点是否在指定范围内
function inRange(x1, y1, x2, y2, range)
return math.abs(x1 - x2) <= range and math.abs(y1 - y2) <= range
end
function calcDistance(x1, y1, x2, y2)
return math.sqrt(math.pow((y2 - y1), 2) + math.pow((x2 - x1), 2))
end
function is_equal(a, b, epsilon) return math.abs(a - b) < (epsilon or 1e-9) end
-- find near person,return personX,personY
function findRecentPerson()
local player = LatestGameState.Players[ao.id]
local minDistance = math.maxinteger
local x = 0
local y = 0
for target, state in pairs(LatestGameState.Players) do
if target ~= ao.id then
local distance = calcDistance(player.x, player.y, state.x, state.y)
print(
colors.blue .. "distance data:" .. distance .. ":" .. player.x ..
',' .. player.y .. ":" .. state.x .. ',' .. state.y ..
" minDistance:" .. minDistance)
if is_equal(minDistance, distance) or distance < minDistance then
x = state.x
y = state.y
minDistance=distance
end
end
end
return x, y
end
-- go person
function goPerson(personX, personY)
local player = LatestGameState.Players[ao.id]
local distanceX = math.abs(personX - player.x)
local distanceY = math.abs(personY - player.y)
local moveStr = ""
if distanceX <= distanceY then
if player.y <= personY then
moveStr = moveStr .. "Up"
else
moveStr = moveStr .. "Down"
end
if player.x ~= personX then
if player.x < personX then
moveStr = moveStr .. "Right"
else
moveStr = moveStr .. "Left"
end
end
else
if player.x <= personX then
moveStr = moveStr .. "Right"
else
moveStr = moveStr .. "Left"
end
if player.y ~= personY then
if player.y < personY then
moveStr = "Up" .. moveStr
else
moveStr = "Down" .. moveStr
end
end
end
return moveStr
end
function getPersonNumber() return #LatestGameState.Players - 1 end
-- void persons
function voidPerson(personX, personY)
local player = LatestGameState.Players[ao.id]
local str = goPerson(personX, personY)
local swtichResult = {
Up = "Down",
Down = "Up",
Left = "Right",
Right = "Left",
UpRight = "UpLeft",
UpLeft = "UpRight",
DownRight = "DownLeft",
DownLeft = "DownRight"
}
-- void go person in border when only have one person
if getPersonNumber() == 1 then
if player.x == 40 or player.y == 40 or player.y == 1 or player.x == 1 then
return swtichResult[swtichResult[str]]
end
end
return swtichResult[str]
end
-- if have one person,attack he!
-- if have moer person,avoid all person,wait only hava one person!
-- if i can't beat he ,avoid him until the end of the game
function decideNextAction()
local player = LatestGameState.Players[ao.id]
local personNum = getPersonNumber()
local personX, personY = findRecentPerson()
local playerEnergy = LatestGameState.Players[ao.id].energy
if playerEnergy == undefined then playerEnergy = 0 end
local moveOrder = ""
print(colors.red .. "Recent Person:" .. personX .. ',' .. personY)
print(colors.red .. "My Position:" .. player.x .. ',' .. player.y)
if personNum ~= 1 then
moveOrder = voidPerson(personX, personY)
print(colors.red .. "personNum != 1 move:" .. moveOrder)
ao.send({
Target = Game,
Action = "PlayerMove",
Player = ao.id,
Direction = moveOrder
})
else
local targetInRange = false
for target, state in pairs(LatestGameState.Players) do
if target ~= ao.id and
inRange(player.x, player.y, state.x, state.y, 1) then
targetInRange = true
break
end
end
if playerEnergy <= 10 then
moveOrder = voidPerson(personX, personY)
print(colors.red .. "playerEnergy <= 10 move:" .. moveOrder)
ao.send({
Target = Game,
Action = "PlayerMove",
Player = ao.id,
Direction = moveOrder
})
else
if targetInRange then
print(colors.red .. "Player in range. Attacking." ..
colors.reset)
ao.send({
Target = Game,
Action = "PlayerAttack",
Player = ao.id,
AttackEnergy = tostring(player.energy)
})
else
moveOrder = goPerson(personX, personY)
print(colors.red .. "go target:" .. moveOrder)
ao.send({
Target = Game,
Action = "PlayerMove",
Player = ao.id,
Direction = moveOrder
})
end
end
end
InAction = false -- InAction 逻辑添加
end
-- 打印游戏公告并触发游戏状态更新的handler。
Handlers.add("PrintAnnouncements",
Handlers.utils.hasMatchingTag("Action", "Announcement"),
function(msg)
if msg.Event == "Started-Waiting-Period" then
ao.send({Target = ao.id, Action = "AutoPay"})
elseif (msg.Event == "Tick" or msg.Event == "Started-Game") and not InAction then
InAction = true -- InAction 逻辑添加
ao.send({Target = Game, Action = "GetGameState"})
elseif InAction then -- InAction 逻辑添加
print("Previous action still in progress. Skipping.")
end
print(colors.green .. msg.Event .. ": " .. msg.Data .. colors.reset)
end)
-- 触发游戏状态更新的handler。
Handlers.add("GetGameStateOnTick",
Handlers.utils.hasMatchingTag("Action", "Tick"), function()
if not InAction then -- InAction 逻辑添加
InAction = true -- InAction 逻辑添加
print(colors.gray .. "Getting game state..." .. colors.reset)
ao.send({Target = Game, Action = "GetGameState"})
else
print("Previous action still in progress. Skipping.")
end
end)
-- 等待期开始时自动付款确认的handler。
Handlers.add("AutoPay", Handlers.utils.hasMatchingTag("Action", "AutoPay"),
function(msg)
print("Auto-paying confirmation fees.")
ao.send({
Target = Game,
Action = "Transfer",
Recipient = Game,
Quantity = "1000"
})
end)
-- 接收游戏状态信息后更新游戏状态的handler。
Handlers.add("UpdateGameState",
Handlers.utils.hasMatchingTag("Action", "GameState"), function(msg)
local json = require("json")
LatestGameState = json.decode(msg.Data)
ao.send({Target = ao.id, Action = "UpdatedGameState"})
print("Game state updated. Print \'LatestGameState\' for detailed view.")
end)
-- 决策下一个最佳操作的handler。
Handlers.add("decideNextAction",
Handlers.utils.hasMatchingTag("Action", "UpdatedGameState"),
function()
if LatestGameState.GameMode ~= "Playing" then
InAction = false -- InAction 逻辑添加
return
end
print("Deciding next action.")
decideNextAction()
ao.send({Target = ao.id, Action = "Tick"})
end)
-- 被其他玩家击中时自动攻击的handler。
Handlers.add("ReturnAttack", Handlers.utils.hasMatchingTag("Action", "Hit"),
function(msg)
if not InAction then -- InAction 逻辑添加
InAction = true -- InAction 逻辑添加
local playerEnergy = LatestGameState.Players[ao.id].energy
if playerEnergy == undefined then
print(colors.red .. "Unable to read energy." .. colors.reset)
ao.send({
Target = Game,
Action = "Attack-Failed",
Reason = "Unable to read energy."
})
elseif playerEnergy == 0 then
print(colors.red .. "Player has insufficient energy." ..
colors.reset)
ao.send({
Target = Game,
Action = "Attack-Failed",
Reason = "Player has no energy."
})
else
print(colors.red .. "Returning attack." .. colors.reset)
ao.send({
Target = Game,
Action = "PlayerAttack",
Player = ao.id,
AttackEnergy = tostring(playerEnergy)
})
end
InAction = false -- InAction 逻辑添加
ao.send({Target = ao.id, Action = "Tick"})
else
print("Previous action still in progress. Skipping.")
end
end)