Skip to content

Commit

Permalink
[Core] add new lua version bufcnt & add subplay fw
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-tz committed Apr 9, 2024
1 parent 6d298c7 commit c1ee379
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 53 deletions.
2 changes: 2 additions & 0 deletions Core/src/WorldModel/WorldModel_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ void CWorldModel::SPlayFSMSwitchClearAll(bool clear_flag)
// 暂时只有清理 球被提出的状态
KickStatus::Instance()->resetKick2ForceClose(true,this->vision()->getCycle());
BallStatus::Instance()->clearKickCmd();

// not used, replaced by new lua bufcnt
BufferCounter::Instance()->clear();
// TODO
return ;
Expand Down
58 changes: 58 additions & 0 deletions Core/tactics/play/TestSubScript.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
local start_pos = CGeoPoint(0,0)
local dist = 1000
local rotateSpeed = 1 -- rad/s

local runPos = function()
local angle = rotateSpeed * vision:getCycle() / param.frameRate
return start_pos + Utils.Polar2Vector(dist, angle)
end
local subScript = false
local DSS_FLAG = bit:_or(flag.allow_dss, flag.dodge_ball)

local PLAY_NAME = ""
return {

__init__ = function(name, args)
print("in __init__ func : ",name, args)
start_pos = args.pos or CGeoPoint(0,0)
dist = args.dist or 1000
subScript = true
PLAY_NAME = name
end,

firstState = "init",
["init"] = {
switch = function()
if bufcnt(true,30) then
if not subScript then
gSubPlay.new(PLAY_NAME .. "task1", "TestSubScript", {pos=start_pos + Utils.Polar2Vector(2*dist, math.pi/4), dist=2000})
gSubPlay.new(PLAY_NAME .. "task2", "TestSubScript", {pos=start_pos + Utils.Polar2Vector(2*dist, -math.pi/4*3), dist=500})
rotateSpeed = -1
end
return "run"
end
end,
Leader = task.stop(),
match = "[L]"
},
["run"] = {
switch = function()
-- print("markdebug : ",gSubPlayFiles)
-- for key, value in pairs(gSubPlayFiles) do
-- print("printFileTable: ", key, value)
-- end
end,
Assister = gSubPlay.roleTask("task1","Leader"),
Fronter = gSubPlay.roleTask("task2","Leader"),
Leader = task.goCmuRush(runPos, 0, nil, DSS_FLAG),
match = "(LAF)"
},

name = "TestSubScript",
applicable = {
exp = "a",
a = true
},
attribute = "attack",
timeout = 99999
}
56 changes: 25 additions & 31 deletions ZBin/lua_scripts/Play.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ gIsRefPlayExit = false
gCurrentBallStatus="None"
gLastBallStatus=""

--gCurrentOurBallAction="None"
-----add by zhyaic 2014.5.22-----
gExternStopCycle = 0
gExternExitCycle = 0

gPlayFileTable = {}

function gPlay.Next()
local index = 1
return function()
Expand Down Expand Up @@ -116,10 +116,7 @@ function DoRolePosMatch(curPlay, isPlaySwitched, isStateSwitched)
end
gActiveRole = {}
for rolename, itask in pairs(curPlay[gRealState]) do
--YuN 2016.03.30 增加函数模式下的匹配
if(type(itask) == "function" and rolename ~= "match" and rolename~="switch") then
--print("closure of task!!!!!!!!!!!!!")
--curPlay[gRealState][rolename]=task()
itask = itask()
end

Expand All @@ -133,27 +130,25 @@ function DoRolePosMatch(curPlay, isPlaySwitched, isStateSwitched)
end
curPlay[gRealState][rolename].name = "continue"
end
gRolePos[rolename] = itask[2]--curPlay[gRealState][rolename][2]()
gRolePos[rolename] = itask[2]
end
end

UpdateRole(curPlay[gRealState].match, isPlaySwitched, isStateSwitched)
-- SetRoleAndNumToCPlusPlus()
-- add by zhyaic for test 2013.5.24
-- if vision:getCurrentRefereeMsg() == "TheirIndirectKick" or
-- vision:getCurrentRefereeMsg() == "TheirDirectKick" or
-- vision:getCurrentRefereeMsg() == "GameStop" then
-- UsePenaltyCleaner(curPlay)
-- end
end

function SetNextPlay(name)
gNextPlay = name
end

function PlayFSMClearAll()
world:SPlayFSMSwitchClearAll(true)
bufcntClear()
end

function ResetPlay(name)
local curPlay = gPlayTable[name]
world:SPlayFSMSwitchClearAll(true)
PlayFSMClearAll()
--------------------------------
if curPlay.firstState ~= nil then
gCurrentState = curPlay.firstState
Expand All @@ -166,7 +161,7 @@ end

function ResetPlayWithLastMatch(name)
local curPlay = gPlayTable[name]
world:SPlayFSMSwitchClearAll(true)
PlayFSMClearAll()
if curPlay.firstState ~= nil then
gCurrentState = curPlay.firstState
else
Expand All @@ -175,36 +170,35 @@ function ResetPlayWithLastMatch(name)
gTimeCounter = 0
end

function _RunPlaySwitch(curPlay, curState)
local newState
if curPlay.switch ~= nil then
newState = curPlay:switch()
else
if curState ~= "exit" and curState ~= "finish" then
newState = curPlay[curState]:switch()
end
end
return newState
end

function RunPlay(name)

if(gPlayTable[name] == nil) then
print("Error In RunPlay: "..name)
else
local curPlay = gPlayTable[name]
local curState
-- gLastState = gCurrentState
local curState = _RunPlaySwitch(curPlay, gCurrentState)
local isStateSwitched = false
if curPlay.switch ~= nil then
curState = curPlay:switch()
--gCurrentState = curPlay:switch()
else
if gCurrentState ~= "exit" and gCurrentState ~= "finish" then
curState = curPlay[gCurrentState]:switch()
end
--gCurrentState = curPlay[gCurrentState]:switch()
end

-- if gCurrentState == nil then
-- gCurrentState = gLastState
if curState ~= nil then
gLastState = gCurrentState
gCurrentState = curState
isStateSwitched = true
world:SPlayFSMSwitchClearAll(true)
PlayFSMClearAll()
end

-- debugEngine:gui_debug_msg(vision:ourPlayer(gRoleNum[rolename]):Pos(), rolename)

gSubPlay.step()
DoRolePosMatch(curPlay, false, isStateSwitched)
gExceptionNum={}

Expand Down
1 change: 1 addition & 0 deletions ZBin/lua_scripts/RoleMatch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ gRoleNum = {
["z"] = -1
}


gLastRoleNum = {

}
Expand Down
4 changes: 2 additions & 2 deletions ZBin/lua_scripts/SelectPlay.lua
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ end

gLastPlay = gCurrentPlay
gNextPlay = ""
debugEngine:gui_debug_msg(CGeoPoint:new_local(-param.pitchLength*2/5, param.pitchWidth/2+200),gCurrentPlay)
debugEngine:gui_debug_msg_fix(CGeoPoint:new_local(-param.pitchLength*2/5, param.pitchWidth/2+200),gCurrentPlay)
RunPlay(gCurrentPlay)
gLastTask = gRoleTask
gRoleTask = {}
Expand All @@ -174,7 +174,7 @@ gRoleTask = {}
--print("ball",ball.posX(),ball.valid())
--print("raw",vision:rawBall():X(),vision:rawBall():Valid())
--print(vision:getBallVelStable(),vision:ballVelValid())
debugEngine:gui_debug_msg(CGeoPoint:new_local(-param.pitchLength*2/5, param.pitchWidth/2),gCurrentState)
debugEngine:gui_debug_msg_fix(CGeoPoint:new_local(-param.pitchLength*2/5, param.pitchWidth/2),gCurrentState)
--world:drawReflect(gRoleNum["Tier"])
--defenceInfo:setNoChangeFlag()
--print(vision:getCycle()..vision:getCurrentRefereeMsg(),vision:gameState():gameOn())
85 changes: 85 additions & 0 deletions ZBin/lua_scripts/SubPlay.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
SubPlay = {
}
function SubPlay.pack(name, play, param)
local curState = play.firstState
if play.__init__ ~= nil then
play.__init__(name, param)
end
return {
name = name,
roleMapping = {},
play = play,
curState = curState,
lastState = nil,
}
end

gSubPlay = {
playTable = {},
curScope = "",
}
gSubPlayFiles = {}

function gSubPlay.new(name, playName, initParam)
print("in initPlay : ", name, initParam)
if gSubPlay.playTable[name] ~= nil then
warning("subPlay exist, reinit a new one - ", name)
end
if gSubPlayFiles[playName] == nil then
warning(string.format("subPlay file not exist - %s", playName))
return
end
spec = dofile(gSubPlayFiles[playName])
gSubPlay.playTable[name] = SubPlay.pack(name, spec, initParam)
end

function gSubPlay.step()
local debugX = -2000
local debugY = param.pitchWidth/2+200
local step = -130
for key, play in pairs(gSubPlay.playTable) do
gSubPlay.curScope = key
local _subPlay = play.play
local curState = play.curState
curState = _RunPlaySwitch(_subPlay, curState)
local isStateSwitched = false
if curState ~= nil then
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
play.lastState = play.curState
play.curState = curState
isStateSwitched = true
PlayFSMClearAll()
end
-- print("ID : , play : , curState :", key, _subPlay.name, play.curState)
gSubPlay.curScope = ""
debugEngine:gui_debug_msg_fix(CGeoPoint(debugX, debugY), key .. " - " .. play.curState, param.GREEN, 0,100)
debugY = debugY + step
end
end

function gSubPlay.getState(name)
if gSubPlay.playTable[name] == nil then
warning("(call gSubPlay.getState) subPlay not exist - ", name)
return nil
end
return gSubPlay.playTable[name].curState
end

function gSubPlay.roleTask(name, role)
return function()
if gSubPlay.playTable[name] == nil then
warning("roleTask not exist - ", name, role)
return
end
local _subPlayState = gSubPlay.playTable[name].play[gSubPlay.playTable[name].curState]
return _subPlayState[role]
end
end

-- function gSubPlay.getRoleNum(roleName)
-- print("in getRoleNum : ", roleName)
-- if gSubPlay.curScope == nil then
-- return gRoleNum[roleName]
-- end
-- return -1
-- end
7 changes: 6 additions & 1 deletion ZBin/lua_scripts/Zeus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ math.random(1,10)
require(OPPONENT_NAME)
require("Skill")
require("Play")
require("SubPlay")
require("cond")
require("pos")
require("dir")
Expand Down Expand Up @@ -104,7 +105,11 @@ for _, value in ipairs(tactic_packages) do
-- print("Skill Files : ",table.concat(play_files, ","))
for _, filename in ipairs(play_files) do
print("Init TPs Play : ",filename)
dofile(filename)
local res = dofile(filename)
if res ~= nil and res.name ~= nil then
gPlayTable.CreatePlay(res)
gSubPlayFiles[res.name] = filename
end
end
else
print("Tactic Dir Not Exists : ",tactic_dir)
Expand Down
4 changes: 0 additions & 4 deletions ZBin/lua_scripts/play/Test/TestRun.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ firstState = "run1",

["run1"] = {
switch = function()
print(skillapi:get_size())
for i=0,skillapi:get_size()-1 do
print(i .. " " .. skillapi:get_name(i))
end
end,
Assister = task.goCmuRush(testPos[1],0, nil, DSS_FLAG),
Leader = task.goCmuRush(testPos[2],0, nil, DSS_FLAG),
Expand Down
Loading

0 comments on commit c1ee379

Please sign in to comment.