Ref_BallPlace_11vs11_new.lua
约 2398 字
前言
Ref_BallPlace_11vs11_new.lua 是为 11 辆车设计的, BallPlacement 阶段的脚本。此脚本用于在收到裁判盒的放球指令后,做出相应反应
请注意,无论我们接收到的指令是 BALL_PLACEMENT_YELLOW 还是 BALL_PLACEMENT_BLUE ,跳转的均为这个脚本,此脚本会根据是否我方需要放球来进行具体的操作
提示
你可以在这一位置找到文件: ZBin/lua_scripts/play/Ref/BallPlacement/Ref_BallPlace_11vs11_new.lua
local SIDE_POS, MIDDLE_POS, INTER_POS, SIDE2_POS, INTER2_POS = pos.refStopAroundBall()
local standpos = {
CGeoPoint:new_local(-param.pitchLength/2+param.penaltyDepth+200,-700),
CGeoPoint:new_local(-param.pitchLength/2+param.penaltyDepth+200,700),
CGeoPoint:new_local(0,1000),
CGeoPoint:new_local(0,-1000),
CGeoPoint:new_local(-param.pitchLength/2+500,0),
CGeoPoint:new_local(param.pitchLength/4-500,0),
CGeoPoint:new_local(-param.pitchLength/4+200,-1000),
CGeoPoint:new_local(-param.pitchLength/4+200,1000),
}
local run_mode = 1
local GETBALL = 1
local PUSHBALL = 2
local PASSBALL = 3
local RECEIVE = 4
local BALLPLACE_MODE = 5
local ballplcae_cnt = 0
local run_once = true
local last_cycle = 0
local matchStr = ""
local lasteMatchStr = ""
local ballPlaceTasks = {}
local getBallPlacementPos = function(index)
return function()
return posmodule:getPos(index,BALLPLACE_MODE)
end
end
local getAttackerNum = function(num)
return defenceSquence:getAttackNum(num)
end
local getballcnt = 0
local lastCycle = 0
local ACC = 5000
local function otherflag()
local flag = function()
if cond.ourBallPlace() then
return flag.our_ball_placement + flag.allow_dss + flag.avoid_stop_ball_circle
else
return flag.our_ball_placement + flag.allow_dss
end
end
return flag
end
local LEADER_TASK = function()
return function()
if vision:getCycle() - lastCycle > 15 then
run_mode = GETBALL
getballcnt = 0
end
local ipos = function()
return ball.pos() + Utils.Polar2Vector(25*10,player.toBallDir(gRoleNum["Leader"])+math.pi)
end
if cond.ballPlaceFinish() and not cond.ballPlaceUnfinish() then
if player.toBallDist(gRoleNum["Leader"]) > 18*10 then
return task.goCmuRush(MIDDLE_POS, player.toBallDir, _, flag.dodge_ball + flag.allow_dss + flag.our_ball_placement)
else
return task.goCmuRush(ipos, player.toBallDir, _, flag.dodge_ball + flag.allow_dss + flag.our_ball_placement)
end
end
local getballPoint = ball.pos() + Utils.Polar2Vector(20*10,(ball.pos() - ball.placementPos()):dir())
if player.toPointDist(gRoleNum["Leader"],getballPoint) < 1*15 then
getballcnt = getballcnt + 1
end
if getballcnt > 15 then
run_mode=PUSHBALL
end
if not Utils.IsInField(ball.pos(),0) then
getballcnt = 0
run_mode=GETBALL
end
debugEngine:gui_debug_msg(CGeoPoint(-200,0),"NOW MODE:"..run_mode)
-- debugEngine:gui_debug_msg(CGeoPoint(-200,200),player.toPointDist("Leader",getballPoint))
lastCycle = vision:getCycle()
return task.fetchball(ball.placementPos,2500,run_mode)
end
end
local ASSISTER_TASK = function()
return function()
local ipos = function()
return ball.placementPos() --+ Utils.Polar2Vector(90*10,player.toOurGoalDir("Assister"))
end
-- if (ball.pos()-ball.placementPos()):mod() > 400*10 then
-- return task.goCmuRush(ipos,player.toBallDir,ACC,otherflag())
-- else
return task.goCmuRush(getBallPlacementPos(2),0,ACC,otherflag())
-- end
end
end
local generateBallPlaceTasks = function()
ballPlaceTasks = {}
local backNum = 2
local subStr = {"A","S","M","D","B","R","F","K","C"}
local curindex = 1
local posNum = 11
local enemyNum = defenceSquence:attackerAmount()
local defendNum = 0
local RealDefendNum = 0
table.insert(ballPlaceTasks,task.goCmuRush(getBallPlacementPos(1),0, ACC,otherflag()))
--rollpoling
table.insert(ballPlaceTasks,task.goCmuRush(pos.rollpolingPos,0, ACC,otherflag()))
gRoleNum["Assister"] = messi:worstNum()
matchStr = "{A}{L}"
curindex = curindex + 1
if cond.ourBallPlace() then
matchStr = matchStr.."("
gRoleNum["Leader"] = beckham:kickNum()
debugEngine:gui_debug_msg(CGeoPoint(0,(param.pitchWidth/2+600)*(IS_RIGHT and -1 or 1)),"Leader: "..gRoleNum["Leader"])
debugEngine:gui_debug_msg(CGeoPoint(0,(param.pitchWidth/2+500)*(IS_RIGHT and -1 or 1)),"kickNum: "..beckham:kickNum())
table.insert(ballPlaceTasks,LEADER_TASK()())
--normalPos
for i = 2,posNum do
table.insert(ballPlaceTasks,task.goCmuRush(getBallPlacementPos(i),0, ACC,otherflag()))
if curindex > 9 then
break
end
matchStr = matchStr..subStr[curindex]
curindex = curindex + 1
end
elseif cond.theirBallPlace() then
matchStr = matchStr.."("
--marking
if ball.posX() > 1000 then
defendNum = enemyNum
else
defendNum = enemyNum
end
debugEngine:gui_debug_msg(CGeoPoint(0,(param.pitchWidth/2+200)*(IS_RIGHT and -1 or 1)),"ourValidNum: "..cond.validNum())
debugEngine:gui_debug_msg(CGeoPoint(0,(param.pitchWidth/2+400)*(IS_RIGHT and -1 or 1)),"defendNum: "..defendNum)
for i = 0,defendNum-1 do
if not enemy.IsTooClose2Ball(getAttackerNum(i)) then
if ball.placementPos():x() < 0 then
table.insert(ballPlaceTasks,task.wmarking("Zero",flag.avoid_stop_ball_circle,getAttackerNum(i)))
if curindex > 9 then
break
end
matchStr = matchStr..subStr[curindex]
curindex = curindex + 1
end
end
end
matchStr = matchStr..")("
table.insert(ballPlaceTasks,task.goCmuRush(getBallPlacementPos(2),0, ACC,otherflag()))
--normalPos
for i = 3,posNum do
table.insert(ballPlaceTasks,task.goCmuRush(getBallPlacementPos(i),0, ACC,otherflag()))
if curindex > 9 then
break
end
matchStr = matchStr..subStr[curindex]
curindex = curindex + 1
end
else
table.insert(ballPlaceTasks,task.goCmuRush(getBallPlacementPos(2),0, ACC,otherflag()))
--normalPos
for i = 3,posNum do
table.insert(ballPlaceTasks,task.goCmuRush(getBallPlacementPos(i),0, ACC,otherflag()))
if curindex > 9 then
break
end
matchStr = matchStr..subStr[curindex]
curindex = curindex + 1
end
end
-- matchStr = matchStr.."["
-- matchStr = matchStr..subStr[curindex]
-- curindex = curindex + 1
matchStr = matchStr..")"
debugEngine:gui_debug_msg(CGeoPoint:new_local(0, (-param.pitchWidth/2-200)*(IS_RIGHT and -1 or 1)), matchStr)
end
local ballplaceMatch = function()
return matchStr
end
local ballplaceTask = function(index)
return function()
return ballPlaceTasks[index]
end
end
gPlayTable.CreatePlay{
firstState = "beginning",
switch = function()
if cond.isGameOn() then
-- ballplcae_cnt = 0
return "exit"
end
-- ballplcae_cnt = ballplcae_cnt + 1
if vision:getCycle() - last_cycle > 5 then
run_once = true
end
generateBallPlaceTasks()
if beckham:KickNumChanged() or run_once or gRoleNum["Assister"] == gRoleNum["Leader"] then
run_once = false
return "run"
end
last_cycle = vision:getCycle()
end,
["beginning"] = {
Goalie = task.goalie(),
Leader = task.stop(),
Assister = task.stop(),
Special = task.stop(),
Middle = task.stop(),
Defender = task.stop(),
Breaker = task.stop(),
Receiver = task.stop(),
Fronter = task.stop(),
Kicker = task.stop(),
Center = task.stop(),
match = "[LASMDBRFKC]"
},
["run"] = {
Goalie = ballplaceTask(1),
Assister = ballplaceTask(2),
Leader = ballplaceTask(3),
Special = ballplaceTask(4),
Middle = ballplaceTask(5),
Defender = ballplaceTask(6),
Breaker = ballplaceTask(7),
Receiver = ballplaceTask(8),
Fronter = ballplaceTask(9),
Kicker = ballplaceTask(10),
Center = ballplaceTask(11),
match = ballplaceMatch
},
name = "Ref_BallPlace_11vs11_new",
applicable ={
exp = "a",
a = true
},
attribute = "defense",
timeout = 99999
}这个脚本和其他 Lua 脚本大差不差,也是两个状态,脚本初始化,会先进入 beginning 状态
beginning
beginning 状态的存在是因为脚本第一次进入,还没有进行过任务分配,在任务分配完成后,会自动跳转到 run 状态
run
["run"] = {
Goalie = ballplaceTask(1),
Assister = ballplaceTask(2),
Leader = ballplaceTask(3),
Special = ballplaceTask(4),
Middle = ballplaceTask(5),
Defender = ballplaceTask(6),
Breaker = ballplaceTask(7),
Receiver = ballplaceTask(8),
Fronter = ballplaceTask(9),
Kicker = ballplaceTask(10),
Center = ballplaceTask(11),
match = ballplaceMatch
},一如既往的,我们使用 table 来为机器人分配任务, generateBallPlaceTasks() 是任务分配的实现,我们来看这个。
初始化
local generateBallPlaceTasks = function()
ballPlaceTasks = {}
local backNum = 2
local subStr = {"A","S","M","D","B","R","F","K","C"}
local curindex = 1
local posNum = 11
local enemyNum = defenceSquence:attackerAmount()
local defendNum = 0
local RealDefendNum = 0依旧是初始化阶段,声明一些必要的变量,如 backNum 后卫默认数量,需要盯防的敌方车总数 enemyNum
Goalie 任务
我们的 Goalie 在放球阶段,不能去跑 task.goalie 了,Goalie 会去抢球的,因此我们也要给 Goalie 分配位置
table.insert(ballPlaceTasks,task.goCmuRush(getBallPlacementPos(1),0, ACC,otherflag()))在 PosModule 中,posmodule:getPos(index,BALLPLACE_MODE) ,索引为 0 的是放球车的位置,索引为 1 的是 Goalie 的位置, Goalie 会站在 CGeoPoint(-PITCH_LENGTH/2+PENALTY_DEPTH-300,0) 的位置上。
Assister 任务
Assister 不属于放球车,他只需要不阻碍放球路线即可
table.insert(ballPlaceTasks,task.goCmuRush(pos.rollpolingPos,0, ACC,otherflag()))
gRoleNum["Assister"] = messi:worstNum()
matchStr = "{A}{L}"
curindex = curindex + 1由于 Assister 不需要干什么事,所以把 messi:worstNum() 给他,这个是在场上,红外可能失灵的车号
其他任务
剩余车辆的分配被分为了 3 种情况:
我方放球
if cond.ourBallPlace() then
matchStr = matchStr.."("
gRoleNum["Leader"] = beckham:kickNum()
debugEngine:gui_debug_msg(CGeoPoint(0,(param.pitchWidth/2+600)*(IS_RIGHT and -1 or 1)),"Leader: "..gRoleNum["Leader"])
debugEngine:gui_debug_msg(CGeoPoint(0,(param.pitchWidth/2+500)*(IS_RIGHT and -1 or 1)),"kickNum: "..beckham:kickNum())
table.insert(ballPlaceTasks,LEADER_TASK()())
--normalPos
for i = 2,posNum do
table.insert(ballPlaceTasks,task.goCmuRush(getBallPlacementPos(i),0, ACC,otherflag()))
if curindex > 9 then
break
end
matchStr = matchStr..subStr[curindex]
curindex = curindex + 1
endLeader 任务
在我方放球阶段,我们需要派出一辆放球车去放球,这里我们选择 Leader ,我们把 Beckham 中 Kicker 的 车号给他,这辆车一般不会出现问题,不会有各种红外故障。
我们来看 LEADER_TASK() 的实现:
local LEADER_TASK = function()
return function()
if vision:getCycle() - lastCycle > 15 then
run_mode = GETBALL
getballcnt = 0
end
local ipos = function()
return ball.pos() + Utils.Polar2Vector(25*10,player.toBallDir(gRoleNum["Leader"])+math.pi)
end
if cond.ballPlaceFinish() and not cond.ballPlaceUnfinish() then
if player.toBallDist(gRoleNum["Leader"]) > 18*10 then
return task.goCmuRush(MIDDLE_POS, player.toBallDir, _, flag.dodge_ball + flag.allow_dss + flag.our_ball_placement)
else
return task.goCmuRush(ipos, player.toBallDir, _, flag.dodge_ball + flag.allow_dss + flag.our_ball_placement)
end
end
local getballPoint = ball.pos() + Utils.Polar2Vector(20*10,(ball.pos() - ball.placementPos()):dir())
if player.toPointDist(gRoleNum["Leader"],getballPoint) < 1*15 then
getballcnt = getballcnt + 1
end
if getballcnt > 15 then
run_mode=PUSHBALL
end
if not Utils.IsInField(ball.pos(),0) then
getballcnt = 0
run_mode=GETBALL
end
debugEngine:gui_debug_msg(CGeoPoint(-200,0),"NOW MODE:"..run_mode)
-- debugEngine:gui_debug_msg(CGeoPoint(-200,200),player.toPointDist("Leader",getballPoint))
lastCycle = vision:getCycle()
return task.fetchball(ball.placementPos,2500,run_mode)
end
end从这里大概也可以看出,放球的 Skill 在 Lua 层做了辅助,但是主要的实现在 task.fetchball()。
超时重置
if vision:getCycle() - lastCycle > 15 then
run_mode = GETBALL
getballcnt = 0
end如果和上一帧的差值达到了 15 帧,说明 Medusa 卡死过了,需要重置状态为 GETBALL 模式,计数器清零,放弃当前动作,重新开始
GETBALL 拿球
Leader 的初始状态是 GETBALL ,放球车要想放球,得先去拿球。Lua 层会把 GETBALL 状态传给 task.fetchball() , Skill 会让车去拿球
拿球的 SKill 中,如果是场外球,车会尝试接近,先跑到球点在场上的映射点,然后开启吸球电机,直接接近球,把球吸住
PUSHBALL 推球
为何使用推球而不是吸球
我们使用推球而不是吸球,也是有原因的。虽然说开启吸球电机,吸着球把球运到放球点也是可行的,且控球更加稳定。
但是长时间的开启吸球电机,让球在草地上蹭,会给吸球电机带来很大的阻力,长时间吸球电机高功耗的使用,会缩短吸球电机的寿命,还会让吸球的效果变差。我们的车本来就容易坏,天天修车裘老板要打过来了。
当 Lua 层认为 getballcnt 计数器连续 > 15 ,Lua 层认为车已经拿到了球,可以准备把球运过来了, Lua 会修改传入参数,把 PUSHBALL 发给 fetchball。
如果我们的车可以跑到球的后面,直接把球推过去,那么我们会选择 球和放球点连线的反向延长线上一点,从这个位置向球运动,把球运过来。
如果我们跑不到球的后面,我们会先尝试把球嘬出来,然后再推球
fetchball 会把球推到指定位置
当超过 15 帧,或者球出界,状态会回到 GETBALL 重新拿球。
放球检查
当 task.fetchball 完成放球后,他会给 WorldModel::Instance()->setBPFinished(bool); 发信号,我们可以通过 cond.ballPlaceFinish() 来检查这一状态。
当我们收到了放球完成的信号,且视觉上认为球的位置与放球点的偏差小于 12cm ,则我们会认定放球完成,放球车会撤出球的区域,否则我们会重新放球,或者去怼一下球,看看能不能放球成功。
剩余车辆
--normalPos
for i = 2,posNum do
table.insert(ballPlaceTasks,task.goCmuRush(getBallPlacementPos(i),0, ACC,otherflag()))
if curindex > 9 then
break
end
matchStr = matchStr..subStr[curindex]
curindex = curindex + 1
end剩下的车会从 PosModule 拿要跑的坐标,并设置躲避放球区域的 flag 标志。
敌方放球
敌方放球的时候,我们只需要注意,不要干扰对面放球就可以了。
elseif cond.theirBallPlace() then
matchStr = matchStr.."("
--marking
if ball.posX() > 1000 then
defendNum = enemyNum
else
defendNum = enemyNum
end
debugEngine:gui_debug_msg(CGeoPoint(0,(param.pitchWidth/2+200)*(IS_RIGHT and -1 or 1)),"ourValidNum: "..cond.validNum())
debugEngine:gui_debug_msg(CGeoPoint(0,(param.pitchWidth/2+400)*(IS_RIGHT and -1 or 1)),"defendNum: "..defendNum)
for i = 0,defendNum-1 do
if not enemy.IsTooClose2Ball(getAttackerNum(i)) then
if ball.placementPos():x() < 0 then
table.insert(ballPlaceTasks,task.wmarking("Zero",flag.avoid_stop_ball_circle,getAttackerNum(i)))
if curindex > 9 then
break
end
matchStr = matchStr..subStr[curindex]
curindex = curindex + 1
end
end
end
matchStr = matchStr..")("
table.insert(ballPlaceTasks,task.goCmuRush(getBallPlacementPos(2),0, ACC,otherflag()))
--normalPos
for i = 3,posNum do
table.insert(ballPlaceTasks,task.goCmuRush(getBallPlacementPos(i),0, ACC,otherflag()))
if curindex > 9 then
break
end
matchStr = matchStr..subStr[curindex]
curindex = curindex + 1
end对面放球,我们不用去盯防对面,所以 DefendNum 设置的是 0
因此,剩下的所有车辆都会按照 PosModule 提供的点位在自己的位置就位。