From c1ee379348ed74bddb4e741addf1faec1966651b Mon Sep 17 00:00:00 2001 From: mark Date: Tue, 9 Apr 2024 16:56:02 +0800 Subject: [PATCH] [Core] add new lua version bufcnt & add subplay fw --- Core/src/WorldModel/WorldModel_basic.cpp | 2 + Core/tactics/play/TestSubScript.lua | 58 ++++++++++++++++ ZBin/lua_scripts/Play.lua | 56 +++++++--------- ZBin/lua_scripts/RoleMatch.lua | 1 + ZBin/lua_scripts/SelectPlay.lua | 4 +- ZBin/lua_scripts/SubPlay.lua | 85 ++++++++++++++++++++++++ ZBin/lua_scripts/Zeus.lua | 7 +- ZBin/lua_scripts/play/Test/TestRun.lua | 4 -- ZBin/lua_scripts/utils/bufcnt.lua | 74 ++++++++++++++++----- ZBin/lua_scripts/utils/utils.lua | 11 +++ 10 files changed, 249 insertions(+), 53 deletions(-) create mode 100644 Core/tactics/play/TestSubScript.lua create mode 100644 ZBin/lua_scripts/SubPlay.lua diff --git a/Core/src/WorldModel/WorldModel_basic.cpp b/Core/src/WorldModel/WorldModel_basic.cpp index b421a33..7467186 100644 --- a/Core/src/WorldModel/WorldModel_basic.cpp +++ b/Core/src/WorldModel/WorldModel_basic.cpp @@ -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 ; diff --git a/Core/tactics/play/TestSubScript.lua b/Core/tactics/play/TestSubScript.lua new file mode 100644 index 0000000..a448d35 --- /dev/null +++ b/Core/tactics/play/TestSubScript.lua @@ -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 +} \ No newline at end of file diff --git a/ZBin/lua_scripts/Play.lua b/ZBin/lua_scripts/Play.lua index 9d4093e..9816c0c 100644 --- a/ZBin/lua_scripts/Play.lua +++ b/ZBin/lua_scripts/Play.lua @@ -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() @@ -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 @@ -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 @@ -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 @@ -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={} diff --git a/ZBin/lua_scripts/RoleMatch.lua b/ZBin/lua_scripts/RoleMatch.lua index 51a0bdd..3de08c6 100644 --- a/ZBin/lua_scripts/RoleMatch.lua +++ b/ZBin/lua_scripts/RoleMatch.lua @@ -40,6 +40,7 @@ gRoleNum = { ["z"] = -1 } + gLastRoleNum = { } diff --git a/ZBin/lua_scripts/SelectPlay.lua b/ZBin/lua_scripts/SelectPlay.lua index fcd5ee3..1545c55 100644 --- a/ZBin/lua_scripts/SelectPlay.lua +++ b/ZBin/lua_scripts/SelectPlay.lua @@ -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 = {} @@ -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()) diff --git a/ZBin/lua_scripts/SubPlay.lua b/ZBin/lua_scripts/SubPlay.lua new file mode 100644 index 0000000..75f3668 --- /dev/null +++ b/ZBin/lua_scripts/SubPlay.lua @@ -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 \ No newline at end of file diff --git a/ZBin/lua_scripts/Zeus.lua b/ZBin/lua_scripts/Zeus.lua index 1a61a93..a4d4a63 100644 --- a/ZBin/lua_scripts/Zeus.lua +++ b/ZBin/lua_scripts/Zeus.lua @@ -12,6 +12,7 @@ math.random(1,10) require(OPPONENT_NAME) require("Skill") require("Play") +require("SubPlay") require("cond") require("pos") require("dir") @@ -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) diff --git a/ZBin/lua_scripts/play/Test/TestRun.lua b/ZBin/lua_scripts/play/Test/TestRun.lua index 3e5b11d..a69888c 100644 --- a/ZBin/lua_scripts/play/Test/TestRun.lua +++ b/ZBin/lua_scripts/play/Test/TestRun.lua @@ -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), diff --git a/ZBin/lua_scripts/utils/bufcnt.lua b/ZBin/lua_scripts/utils/bufcnt.lua index 172bcd6..7136364 100644 --- a/ZBin/lua_scripts/utils/bufcnt.lua +++ b/ZBin/lua_scripts/utils/bufcnt.lua @@ -1,4 +1,62 @@ -function bufcnt( cond, buf, cnt ) +local BufCnter = {} +function BufCnter.new(buf, cnt) + return { + cndCnter = 0, + cycCnter = 0, + BUF = buf, + CNT = cnt or 9999, + -- lastCycle = vision:getCycle(), + } +end +function BufCnter.update(bc, cond, buf, cnt) + local cnt = cnt or 9999 + -- bc.lastCycle = vision:getCycle() + bc.cycCnter = bc.cycCnter + 1 + bc.cndCnter = cond and bc.cndCnter + 1 or 0 + if bc.cycCnter >= cnt then + return true + end + if bc.cndCnter >= buf then + return true + end + return false +end + +local __BCs = {} +function bufcnt(cond, buf, cnt) + local scope = gSubPlay.curScope + if scope == nil or scope == "" then + scope = "__G__" + end + if __BCs[scope] == nil then + __BCs[scope] = { + index = 0, + lastCycle = vision:getCycle(), + } + end + if __BCs[scope].lastCycle ~= vision:getCycle() then + __BCs[scope].index = 0 + end + + local index = __BCs[scope].index + + if __BCs[scope][index] == nil then + __BCs[scope][index] = BufCnter.new(buf, cnt) + end + local res = BufCnter.update(__BCs[scope][index],cond, buf, cnt) + __BCs[scope].index = index + 1 + return res +end + +function bufcntClear() + local scope = gSubPlay.curScope + if scope == nil or scope == "" then + scope = "__G__" + end + __BCs[scope] = nil +end + +function ___bufcnt( cond, buf, cnt ) if buf == "normal" then buf = 6 elseif buf == "slow" then @@ -15,17 +73,3 @@ function bufcnt( cond, buf, cnt ) return false end end - -local COND_FIX_STATUS = false - -function minbufcnt(c, min_cycle, buf, max_cycle) - if bufcnt(true, min_cycle) then - COND_FIX_STATUS = true - end - if bufcnt(COND_FIX_STATUS and c, buf, max_cycle) then - COND_FIX_STATUS = false - return true - end - - return false -end \ No newline at end of file diff --git a/ZBin/lua_scripts/utils/utils.lua b/ZBin/lua_scripts/utils/utils.lua index 2abbae6..2e041fb 100644 --- a/ZBin/lua_scripts/utils/utils.lua +++ b/ZBin/lua_scripts/utils/utils.lua @@ -1,3 +1,14 @@ +local warningX = 0 +local warningY = 0 +function warning(msg) + msg = "WARNING: " .. msg + print( msg) + debugEngine:gui_debug_msg(CGeoPoint(warningX, warningY), msg, param.CYAN) + warningY = warningY + 120 + if warningY > param.pitchWidth/2 then + warningY = -param.pitchWidth/2 + end +end function _c(f,...) -- auto convert for closure if type(f) == "function" then return f(...)