Bots And Games
自动响应
根据我们的上一个指南,我们的创作已经从一个简单的机器人发展成为一个复杂的自主代理。 现在,让我们进一步增强它的能力,添加反击功能,让它能够立即对对手的攻击进行反击,有可能在他们撤退到安全地带之前打他们个措手不及。
编写代码
将以下处理程序添加到您的 bot.lua
文件中即可:
-- 被其他玩家击中时自动攻击的处理程序。
Handlers.add(
"ReturnAttack",
Handlers.utils.hasMatchingTag("Action", "Hit"),
function (msg)
local playerEnergy = LatestGameState.Players[ao.id].energy
if playerEnergy == undefined then
print("Unable to read energy.")
ao.send({Target = Game, Action = "Attack-Failed", Reason = "Unable to read energy."})
elseif playerEnergy == 0 then
print("Player has insufficient energy.")
ao.send({Target = Game, Action = "Attack-Failed", Reason = "Player has no energy."})
else
print("Returning attack.")
ao.send({Target = Game, Action = "PlayerAttack", Player = ao.id, AttackEnergy = tostring(playerEnergy)})
end
InAction = false
ao.send({Target = ao.id, Action = "Tick"})
end
)
每当您的玩家受到攻击时,您都会收到一条包含 Hit
动作的消息。 这种设置可确保您的代理在拥有足够能量的情况下能够进行快速反击。
您可以在下面参考 bot.lua
的最新代码:
LatestGameState = LatestGameState or nil
function inRange(x1, y1, x2, y2, range)
return math.abs(x1 - x2) <= range and math.abs(y1 - y2) <= range
end
function decideNextAction()
local player = LatestGameState.Players[ao.id]
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 player.energy > 5 and targetInRange then
print("Player in range. Attacking.")
ao.send({Target = Game, Action = "PlayerAttack", Player = ao.id, AttackEnergy = tostring(player.energy)})
else
print("No player in range or insufficient energy. Moving randomly.")
local directionMap = {"Up", "Down", "Left", "Right", "UpRight", "UpLeft", "DownRight", "DownLeft"}
local randomIndex = math.random(#directionMap)
ao.send({Target = Game, Action = "PlayerMove", Player = ao.id, Direction = directionMap[randomIndex]})
end
end
Handlers.add(
"HandleAnnouncements",
Handlers.utils.hasMatchingTag("Action", "Announcement"),
function (msg)
ao.send({Target = Game, Action = "GetGameState"})
print(msg.Event .. ": " .. msg.Data)
end
)
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"})
end
)
Handlers.add(
"decideNextAction",
Handlers.utils.hasMatchingTag("Action", "UpdatedGameState"),
function ()
if LatestGameState.GameMode ~= "Playing" then
return
end
print("Deciding next action.")
decideNextAction()
end
)
Handlers.add(
"ReturnAttack",
Handlers.utils.hasMatchingTag("Action", "Hit"),
function (msg)
local playerEnergy = LatestGameState.Players[ao.id].energy
if playerEnergy == undefined then
print("Unable to read energy.")
ao.send({Target = Game, Action = "Attack-Failed", Reason = "Unable to read energy."})
elseif playerEnergy == 0 then
print("Player has insufficient energy.")
ao.send({Target = Game, Action = "Attack-Failed", Reason = "Player has no energy."})
else
print("Returning attack.")
ao.send({Target = Game, Action = "PlayerAttack", Player = ao.id, AttackEnergy = tostring(playerEnergy)})
end
InAction = false
ao.send({Target = ao.id, Action = "Tick"})
end
)
加载和测试
要激活并测试反击功能,请在您的 aos 玩家终端中加载机器人文件:
.load bot.lua
观察您的终端里自主代理的反应,现在增加了立即报复的能力。 此功能展示了代理不断发展的战略深度和自主权。 在接下来的部分中,我们将巩固迄今为止收集的所有知识,并添加一些优化功能。