RoleManager.lua 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. RoleManager = {}
  2. local this = RoleManager
  3. --我方死亡列表
  4. local _RealDeadList={}
  5. -- 将死之人
  6. local _DeadRoleList = {}
  7. -- 重生之人
  8. local _ReliveRoleList = {}
  9. -- 所有角色
  10. local PosList = {}
  11. -- 删除节点
  12. local removeObjList = BattleList.New()
  13. -- 角色对象池
  14. local rolePool = BattleObjectPool.New(function ()
  15. return RoleLogic.New()
  16. end)
  17. local bMyAllDead
  18. local bEnemyAllDead
  19. --替补数据
  20. local tibuRoleList={}
  21. local tibuData = {}
  22. local enemy_tibuData = {}
  23. -- 替补状态
  24. -- 0 不存在
  25. -- 1 存在且在替补席 (未有单位死亡备战)
  26. -- 3 存在且上阵中 (有战斗单位死亡准备上阵)
  27. -- 4 (角色出场时)
  28. -- 2 存在且已经上阵 (替补角色生成已在场上) 替补对象生成后
  29. -- 0 (不存在替补) --- 1 (存在初始化 备战ing)--- 3(场上有一个对象死亡等待上场)--- 4(开始上场直到上场动画回合结束)----2(回合结束到之后状态)
  30. SubstiState={
  31. none=0,
  32. stage=1,
  33. waiting=3,
  34. onshow=4,
  35. showed=5
  36. }
  37. local Tibu_State
  38. local enemyTibu_State
  39. local RecordCaculate={}
  40. -- 初始化
  41. function this.Init()
  42. bMyAllDead = false
  43. bEnemyAllDead = false
  44. Tibu_State=0
  45. enemyTibu_State = 0
  46. RecordCaculate={}
  47. this.Clear()
  48. end
  49. local isLog=false
  50. --temp角色日志
  51. function RoleManager.Log(str)
  52. if isLog then
  53. --LogError(str)
  54. end
  55. end
  56. --to do check yh 角色数据添加 camp==0 我方英雄 1 敌方英雄 战斗替补上阵回合增加角色上阵信息
  57. function this.getCaRecord()
  58. return RecordCaculate
  59. end
  60. -- 角色数据添加
  61. local curUid
  62. function this.AddRole(roleData, position, isHelp)
  63. if not curUid then
  64. curUid = 0
  65. end
  66. curUid = curUid + 1
  67. local role = rolePool:Get()
  68. -- 判断当前角色作为替补角色出现
  69. if not roleData.isTibu then
  70. RoleManager.Log("no tibu")
  71. role:Init(curUid, roleData, position)
  72. else
  73. RoleManager.Log("is tibu")
  74. if roleData.camp == 0 then
  75. role = tibuRoleList[1]
  76. elseif roleData.camp == 1 then
  77. role = tibuRoleList[2]
  78. end
  79. end
  80. -- objList:Add(curUid, role)
  81. if roleData.camp == 0 then
  82. RoleManager.Log("slef on"..position)
  83. PosList[position] = role -- 1-9 我方英雄
  84. role.position = position
  85. else
  86. -- 检测固定上场位置
  87. if position + 9 > 18 or position + 9 <= 9 then
  88. if position < 1 then
  89. position= position + 9
  90. elseif position > 9 then
  91. position = position - 9
  92. end
  93. end
  94. role.position = position
  95. RoleManager.Log("enemy on"..(position ))
  96. PosList[position + 9] = role-- 10-18 敌方英雄
  97. end
  98. if not role:IsRealDead() then
  99. if role:IsSubstitute() then
  100. BattleLogic.Event:DispatchEvent(BattleEventName.AddRole, role, isHelp,true)
  101. local RoundEndTibuSetState
  102. RoundEndTibuSetState =function (isHave,nullNum,ReCheckRound)
  103. if role.camp == 0 then Tibu_State=2 end
  104. if role.camp == 1 then enemyTibu_State=2 end
  105. BattleLogic.Event:RemoveEvent(BattleEventName.RoundBegin, RoundEndTibuSetState)
  106. end
  107. BattleLogic.Event:AddEvent(BattleEventName.RoundBegin, RoundEndTibuSetState)
  108. BattleLogic.ChangeRecordPos(role.position,false)
  109. local ResetTibuRound
  110. ResetTibuRound =function (isHave,nullNum,ReCheckRound)
  111. ReCheckRound(true,nullNum-1)
  112. BattleLogic.Event:RemoveEvent(BattleEventName.BattleRoundCheck, ResetTibuRound)
  113. end
  114. BattleLogic.Event:AddEvent(BattleEventName.BattleRoundCheck, ResetTibuRound)
  115. return
  116. end
  117. BattleLogic.Event:DispatchEvent(BattleEventName.AddRole, role, isHelp,false)
  118. end
  119. end
  120. ------------- 抽象替补逻辑
  121. function this.InitTibuData(roleData,position)
  122. local date
  123. if roleData.camp == 0 then
  124. date = tibuData
  125. if roleData == nil then
  126. tibuData.HasTibu = false
  127. tibuData.AddTibu = false
  128. Tibu_State = 0
  129. else
  130. if not curUid then
  131. curUid = 0
  132. end
  133. curUid = curUid + 1
  134. Tibu_State = 1
  135. local role = rolePool:Get()
  136. role:Init(curUid, roleData, position)
  137. tibuRoleList[1] = role
  138. tibuData.roleData = roleData
  139. tibuData.position = position
  140. tibuData.HasTibu = true
  141. tibuData.AddTibu = false
  142. end
  143. else
  144. date = enemy_tibuData
  145. if roleData == nil then
  146. enemy_tibuData.HasTibu = false
  147. enemy_tibuData.AddTibu = false
  148. enemyTibu_State = 0
  149. else
  150. if not curUid then
  151. curUid = 0
  152. end
  153. curUid = curUid + 1
  154. enemyTibu_State = 1
  155. local role = rolePool:Get()
  156. role:Init(curUid, roleData, position)
  157. tibuRoleList[2] = role
  158. enemy_tibuData.roleData = roleData
  159. enemy_tibuData.position = position
  160. enemy_tibuData.HasTibu = true
  161. enemy_tibuData.AddTibu = false
  162. end
  163. end
  164. return date
  165. end
  166. function this.getTibuStateByCamp(_camp)
  167. if _camp == 0 or _camp == nil then
  168. return Tibu_State
  169. elseif _camp == 1 then
  170. return enemyTibu_State
  171. end
  172. end
  173. function this.getTibuState()
  174. if Tibu_State ~= nil then
  175. RoleManager.Log("@@@@@@@@@@@@Tibu_State"..Tibu_State.."xxxxxxx enemyTibu_State"..enemyTibu_State)
  176. return Tibu_State,enemyTibu_State
  177. end
  178. RoleManager.Log("@@@@@@@@@@@@@@Tibu_State is nil")
  179. end
  180. function this.HasTibu(_camp)
  181. if _camp == 0 then
  182. if tibuData ~= nil then
  183. return tibuData.HasTibu
  184. else
  185. return false
  186. end
  187. elseif _camp == 1 then
  188. if enemy_tibuData ~= nil then
  189. return enemy_tibuData.HasTibu
  190. else
  191. return false
  192. end
  193. end
  194. end
  195. function this.SetTibuState(_camp,_state)
  196. if _camp == 0 then
  197. Tibu_State =_state
  198. elseif _camp == 1 then
  199. enemyTibu_State = _state
  200. end
  201. end
  202. function this.AddTibuToWaitingState(_camp)
  203. if _camp == 0 then
  204. Tibu_State = 3
  205. elseif _camp == 1 then
  206. enemyTibu_State = 3
  207. end
  208. end
  209. function this.IsRoleTibuAdding(_camp)
  210. if _camp == 0 then
  211. if Tibu_State == 3 then
  212. return true
  213. end
  214. return false
  215. elseif _camp == 1 then
  216. if enemyTibu_State == 3 then
  217. return true
  218. end
  219. return false
  220. end
  221. end
  222. function this.TibuClear()
  223. tibuData.HasTibu = false
  224. tibuData.AddTibu = false
  225. Tibu_State = 0
  226. enemy_tibuData.HasTibu = false
  227. enemy_tibuData.AddTibu = false
  228. enemyTibu_State = 0
  229. end
  230. --todo check yh 增加替补
  231. function this.AddTibu(_camp)
  232. if _camp == 0 then
  233. if tibuData.HasTibu then
  234. -- RoleManager.Log("............................1 my"..tibuData.position)
  235. Tibu_State = 4
  236. tibuData.AddTibu = true
  237. this.AddRole(tibuData.roleData,tibuData.position,false)
  238. bMyAllDead = false
  239. tibuData.HasTibu = false
  240. RoleManager.getTibuState()
  241. BattleLogic.Event:DispatchEvent(BattleEventName.OnAddTibuRole, tibuData.roleData)
  242. end
  243. elseif _camp == 1 then
  244. if enemy_tibuData.HasTibu then
  245. -- RoleManager.Log("............................1 enemy po"..enemy_tibuData.position)
  246. enemyTibu_State = 4
  247. enemy_tibuData.AddTibu = true
  248. this.AddRole(enemy_tibuData.roleData,enemy_tibuData.position - 9,false)
  249. bEnemyAllDead = false
  250. enemy_tibuData.HasTibu = false
  251. RoleManager.getTibuState()
  252. BattleLogic.Event:DispatchEvent(BattleEventName.OnAddTibuRole, enemy_tibuData.roleData)
  253. end
  254. end
  255. -- RoleManager.Log("............................2")
  256. end
  257. function this.setTibuPos(pos,_camp)
  258. if _camp == 0 then
  259. tibuData.position = pos
  260. elseif _camp == 1 then
  261. enemy_tibuData.position = pos
  262. end
  263. end
  264. -- 是否满编 !!pve 不适用
  265. function this.IsNotFullTeam(_camp)
  266. local teamFull = 5 --满编队 5人 --引用外部函数注意!
  267. local idx = 0
  268. for _, ro in pairs(PosList) do
  269. if ro.camp == _camp and ro ~= nil then
  270. idx = idx +1
  271. end
  272. end
  273. if idx < teamFull then
  274. return true
  275. end
  276. return false
  277. end
  278. --空场位置 返回第一个
  279. function this.MyTeamEmptyPos(_camp)
  280. for i =1 , 9 do
  281. if PosList[i+_camp*9] == nil then return i end
  282. end
  283. for po, ro in pairs(PosList) do
  284. if ro.camp == _camp and ro ==nil and not this.IsReliveRole(ro) then
  285. RoleManager.Log("po"..po)
  286. return po
  287. end
  288. end
  289. end
  290. -- 获取场上一个存活对象
  291. function this.getMyTeamOneAlive()
  292. for _, ro in pairs(PosList) do
  293. if ro.camp == 0 and ro:IsDead()==false then
  294. return ro
  295. end
  296. end
  297. return nil
  298. end
  299. --我方场上一人触发被动技能
  300. function this.TriggerMyTeamOneAlivePassivity(passivity)
  301. if tibuRoleList ~= nil then
  302. for _,ro in pairs(tibuRoleList) do
  303. local role = ro
  304. if role~=nil then
  305. role:AddPassiveByOther(passivity)
  306. -- LogError("TriggerMyTeamOneAlivePassivity")
  307. end
  308. end
  309. end
  310. end
  311. --给所有存活人员加上被动buff
  312. function this.AddPassiveToAliveRoles(role)
  313. for _, ro in pairs(PosList) do
  314. if ro.camp == 0 and ro:IsDead()==false then
  315. for idx = 1, #role.passivity do
  316. -- v:AddPassive(id)
  317. -- v:AddBuff()
  318. local v = role.passivity[idx]
  319. local ids = v[1]
  320. for k = 1, #ids do
  321. local args = {}
  322. for j = 1, #v[k + 1] do
  323. args[j] = v[k + 1][j]
  324. end
  325. local id = ids[k]
  326. if BattleUtil.Passivity[id] then
  327. BattleUtil.Passivity[id](ro, args)
  328. -- 加入被动列表
  329. table.insert(ro.passiveList, {id, args})
  330. else
  331. end
  332. end
  333. end
  334. end
  335. end
  336. if not role:IsRealDead() then
  337. BattleLogic.Event:DispatchEvent(BattleEventName.AddRole, role, isHelp)
  338. end
  339. this.Clone()
  340. end
  341. --当前场上没有角色
  342. function this.IsMyTeamEmpty(_camp)
  343. for _, v in pairs(PosList) do
  344. if v.camp == _camp and v:IsDead()==false then
  345. return false
  346. end
  347. end
  348. return true
  349. end
  350. --to do check yh 我方全部阵亡
  351. function this.IsMyAllDead()
  352. return bMyAllDead
  353. end
  354. --to do check yh 某方一人阵亡
  355. function this.IsMyOneDead(_camp)
  356. for pos, role in pairs(_RealDeadList) do
  357. if role.camp==_camp then
  358. --and not BattleLogic.BuffMgr:HasBuff(role,BuffName.NoDead)
  359. return true
  360. end
  361. end
  362. return false
  363. end
  364. --to do check yh 我方一人阵亡位置
  365. function this.IsMyOneDeadPosCanNotRelive(_camp)
  366. local upPos = -1
  367. for pos, role in pairs(_RealDeadList) do
  368. if role.camp==_camp then
  369. --被替补无法复活 role:GoDead() and not BattleLogic.BuffMgr:HasBuff(role,BuffName.NoDead)
  370. -- role:SetReliveFilter(false)
  371. upPos = pos
  372. break
  373. end
  374. end
  375. if upPos == -1 then upPos = this.MyTeamEmptyPos(_camp) end
  376. return upPos
  377. end
  378. ------------- 抽象替补逻辑 end
  379. function this.Update()
  380. bMyAllDead = true
  381. bEnemyAllDead = true
  382. for _, v in pairs(PosList) do
  383. if not v:IsRealDead() then
  384. v:Update()
  385. if v.camp == 0 then
  386. bMyAllDead = false
  387. else
  388. bEnemyAllDead = false
  389. end
  390. end
  391. end
  392. -- to do check yh 更新战斗检测
  393. if tibuData~=nil then
  394. if bMyAllDead and tibuData.HasTibu then
  395. bMyAllDead = false
  396. end
  397. end
  398. if enemy_tibuData~=nil then
  399. if bEnemyAllDead and enemy_tibuData.HasTibu then
  400. bEnemyAllDead = false
  401. end
  402. end
  403. if bEnemyAllDead then
  404. BattleLogic.Event:DispatchEvent(BattleEventName.BattleOrderEnd, BattleLogic.CurOrder)
  405. end
  406. end
  407. -- 获取结果
  408. function this.GetResult()
  409. -- RecordResult={}
  410. -- for _, v in pairs(PosList) do
  411. -- if v~=nil then
  412. -- local isdead="false"
  413. -- if v.isRealDead then isdead = "true" end
  414. -- local _rolinfo =tostring(_) .." hp:".. tostring(v:GetRoleData(RoleDataName.Hp)) .." camp:"..tostring(v.camp) .." isdead:".. isdead
  415. -- table.insert(RecordResult , _rolinfo)
  416. -- end
  417. -- end
  418. -- if bMyAllDead then
  419. -- local _set=" bMyAllDead is true"
  420. -- table.insert(RecordResult,_set)
  421. -- end
  422. -- if bEnemyAllDead then
  423. -- local _set=" bEnemyAllDead is true"
  424. -- table.insert(RecordResult,_set)
  425. -- end
  426. if bMyAllDead then
  427. return 0
  428. end
  429. if bEnemyAllDead then
  430. return 1
  431. end
  432. end
  433. function this.CheadListOnDeadPool()
  434. if _RealDeadList~=nil then
  435. for pos, role in pairs(_RealDeadList) do
  436. return role
  437. end
  438. end
  439. return nil
  440. end
  441. -- 加入将死的人
  442. function this.AddDeadRole(role)
  443. _DeadRoleList[role.position + role.camp * 9] = role
  444. _RealDeadList[role.position + role.camp * 9] = role
  445. end
  446. -- 检测是有人将死
  447. function this.CheckDead()
  448. local isDeadFrame = false
  449. local removePos = {}
  450. for pos, role in pairs(_DeadRoleList) do
  451. if role:GoDead() then
  452. isDeadFrame = true
  453. table.insert(removePos, pos)
  454. end
  455. end
  456. for _, pos in ipairs(removePos) do
  457. _DeadRoleList[pos] = nil
  458. end
  459. return isDeadFrame
  460. end
  461. -- 加入复活之人
  462. function this.AddReliveRole(role)
  463. _ReliveRoleList[role.position + role.camp * 9] = role
  464. _RealDeadList[role.position + role.camp * 9] = nil
  465. end
  466. -- 是否在复活列表
  467. function this.IsReliveRole(role)
  468. if _ReliveRoleList==nil then
  469. return false
  470. end
  471. for ro in pairs(_ReliveRoleList) do
  472. if ro==role then
  473. return true
  474. end
  475. end
  476. return false
  477. end
  478. -- 检测是否有人复活
  479. function this.CheckRelive()
  480. local isReliveFrame = false
  481. local removePos = {}
  482. for pos, role in pairs(_ReliveRoleList) do
  483. if role:Relive() then
  484. isReliveFrame = true
  485. table.insert(removePos, pos)
  486. end
  487. end
  488. for _, pos in ipairs(removePos) do
  489. _ReliveRoleList[pos] = nil
  490. end
  491. return isReliveFrame
  492. end
  493. -- 获取角色数据
  494. function this.GetRole(camp, pos)
  495. return PosList[pos + camp*9]
  496. end
  497. function this.GetRoleByPos(pos)
  498. return PosList[pos]
  499. end
  500. -- 获取某阵营所有角色
  501. function this.GetRoleByCamp(camp)
  502. local list = {}
  503. for i = 1, 6 do
  504. list[i] = PosList[i + camp * 6]
  505. end
  506. return list
  507. end
  508. --查找角色替补数据
  509. function RoleManager.QueryTibu(func, inCludeDeadRole)
  510. local list = {}
  511. local index = 1
  512. if #tibuRoleList > 0 then
  513. if func then
  514. for camp, v in pairs(tibuRoleList) do
  515. if func(v) and (inCludeDeadRole or not v:IsRealDead()) then
  516. list[index] = v
  517. index = index + 1
  518. end
  519. end
  520. end
  521. end
  522. table.sort(list, function(a, b)
  523. return a.position < b.position
  524. end)
  525. return list
  526. end
  527. -- 查找角色
  528. function RoleManager.Query(func, inCludeDeadRole)
  529. local list = {}
  530. local index = 1
  531. if func then
  532. for pos, v in pairs(PosList) do
  533. if func(v) and (inCludeDeadRole or not v:IsRealDead()) then
  534. list[index] = v
  535. index = index + 1
  536. end
  537. end
  538. end
  539. table.sort(list, function(a, b)
  540. return a.position < b.position
  541. end)
  542. return list
  543. end
  544. --> 查找死亡角色
  545. function RoleManager.QueryDead(func)
  546. local list = {}
  547. local index = 1
  548. if func then
  549. for pos, v in pairs(PosList) do
  550. if func(v) and v:IsRealDead() then
  551. list[index] = v
  552. index = index + 1
  553. end
  554. end
  555. end
  556. table.sort(list, function(a, b)
  557. return a.position < b.position
  558. end)
  559. return list
  560. end
  561. --对位规则: 1敌方相同对位 2若死亡或不存在,选取相邻阵位最近且阵位索引最小的
  562. function this.GetAggro(role)
  563. -- -- 计算开始位置
  564. -- local startPos = role.position
  565. -- startPos = startPos > 3 and startPos - 3 or startPos
  566. -- local target
  567. -- local enemyCamp = role.camp == 0 and 1 or 0
  568. -- -- c=0 前排 c=1 后排
  569. -- for c = 0, 1 do
  570. -- startPos = startPos + c*3
  571. -- -- 向左
  572. -- for i = startPos, c*3+1, -1 do
  573. -- local pos = i + enemyCamp * 6
  574. -- if PosList[pos] and not PosList[pos]:IsRealDead() then
  575. -- target = PosList[pos]
  576. -- break
  577. -- end
  578. -- end
  579. -- if target then return target end
  580. -- -- 向右
  581. -- for i = startPos + 1, c*3+3 do
  582. -- local pos = i + enemyCamp * 6
  583. -- if PosList[pos] and not PosList[pos]:IsRealDead() then
  584. -- target = PosList[pos]
  585. -- break
  586. -- end
  587. -- end
  588. -- if target then return target end
  589. -- end
  590. -->
  591. -- 计算开始位置
  592. local startPos = role.position
  593. startPos = startPos > 6 and startPos - 6 or (startPos > 3 and startPos - 3 or startPos)
  594. local target
  595. local enemyCamp = role.camp == 0 and 1 or 0
  596. local tempStartPos = startPos --< 重置用
  597. -- c=0 前排 c=1 中排
  598. for c = 0, 2 do
  599. startPos = tempStartPos
  600. startPos = startPos + c*3
  601. -- 向左
  602. for i = startPos, c*3+1, -1 do
  603. local pos = i + enemyCamp * 9
  604. if PosList[pos] and not PosList[pos]:IsRealDead() then
  605. target = PosList[pos]
  606. break
  607. end
  608. end
  609. if target then return target end
  610. -- 向右
  611. for i = startPos + 1, c*3+3 do
  612. local pos = i + enemyCamp * 9
  613. if PosList[pos] and not PosList[pos]:IsRealDead() then
  614. target = PosList[pos]
  615. break
  616. end
  617. end
  618. if target then return target end
  619. end
  620. end
  621. -- 获取没有进入死亡状态的仇恨目标
  622. function this.GetAliveAggro(role)
  623. -- 计算开始位置
  624. local startPos = role.position
  625. startPos = startPos > 3 and startPos - 3 or startPos
  626. local target
  627. local enemyCamp = role.camp == 0 and 1 or 0
  628. -- c=0 前排 c=1 后排
  629. for c = 0, 1 do
  630. startPos = startPos + c*3
  631. -- 向左
  632. for i = startPos, c*3+1, -1 do
  633. local pos = i + enemyCamp * 6
  634. if PosList[pos] and not PosList[pos]:IsDead() then
  635. target = PosList[pos]
  636. break
  637. end
  638. end
  639. if target then return target end
  640. -- 向右
  641. for i = startPos + 1, c*3+3 do
  642. local pos = i + enemyCamp * 6
  643. if PosList[pos] and not PosList[pos]:IsDead() then
  644. target = PosList[pos]
  645. break
  646. end
  647. end
  648. if target then return target end
  649. end
  650. --> 只有被动用 暂不动
  651. end
  652. --对位规则: 1敌方相同对位 2若死亡或不存在,选取相邻阵位最近且阵位索引最小的
  653. function this.GetArrAggroList(role, arr)
  654. -- -- 重构数据
  655. -- local plist = {}
  656. -- for _, role in ipairs(arr) do
  657. -- plist[role.position] = role
  658. -- end
  659. -- -- 计算开始位置
  660. -- local startPos = role.position
  661. -- startPos = startPos > 3 and startPos - 3 or startPos
  662. -- local targetList = {}
  663. -- -- c=0 前排 c=1 后排
  664. -- for c = 0, 1 do
  665. -- startPos = startPos + c*3
  666. -- -- 向左
  667. -- for i = startPos, c*3+1, -1 do
  668. -- local pos = i
  669. -- if plist[pos] and not plist[pos]:IsRealDead() then
  670. -- table.insert(targetList, plist[pos])
  671. -- end
  672. -- end
  673. -- -- 向右
  674. -- for i = startPos + 1, c*3+3 do
  675. -- local pos = i
  676. -- if plist[pos] and not plist[pos]:IsRealDead() then
  677. -- table.insert(targetList, plist[pos])
  678. -- end
  679. -- end
  680. -- end
  681. -- table.sort(targetList, function(a, b)
  682. -- return a.position < b.position
  683. -- end)
  684. -- return targetList
  685. -->
  686. -- 重构数据
  687. local plist = {}
  688. for _, role in ipairs(arr) do
  689. plist[role.position] = role
  690. end
  691. -- 计算开始位置
  692. local startPos = role.position
  693. startPos = startPos > 6 and startPos - 6 or (startPos > 3 and startPos - 3 or startPos)
  694. local targetList = {}
  695. local tempStartPos = startPos --< 重置用
  696. -- c=0 前排 c=1 中排
  697. for c = 0, 2 do
  698. startPos = tempStartPos
  699. startPos = startPos + c*3
  700. -- 向左
  701. for i = startPos, c*3+1, -1 do
  702. local pos = i
  703. if plist[pos] and not plist[pos]:IsRealDead() then
  704. table.insert(targetList, plist[pos])
  705. end
  706. end
  707. -- 向右
  708. for i = startPos + 1, c*3+3 do
  709. local pos = i
  710. if plist[pos] and not plist[pos]:IsRealDead() then
  711. table.insert(targetList, plist[pos])
  712. end
  713. end
  714. end
  715. table.sort(targetList, function(a, b)
  716. return a.position < b.position
  717. end)
  718. return targetList
  719. end
  720. --获取对位相邻站位的人 chooseType 1 我方 2 敌方(对位的敌人受到嘲讽的影响,若对位的敌人死亡,则选取相邻最近的作为目标)
  721. function this.GetNeighbor(role, chooseType)
  722. local posList = {}
  723. local target
  724. if chooseType == 1 then
  725. target = role
  726. else
  727. if role.lockTarget and not role.lockTarget:IsRealDead() then
  728. target = role.lockTarget
  729. else
  730. target = this.GetAggro(role)
  731. end
  732. end
  733. if target then
  734. local list = this.Query(function (r) return r.camp == target.camp end)
  735. for i=1, #list do
  736. if not list[i]:IsRealDead() then
  737. if list[i].position == target.position + 3 -- 后排的人
  738. or list[i].position == target.position - 3 -- 前排的人
  739. or (math.abs(target.position - list[i].position) <= 1 and math.floor((target.position-1)/3) == math.floor((list[i].position-1)/3)) then -- 旁边的人和自己
  740. table.insert(posList, list[i])
  741. end
  742. end
  743. end
  744. end
  745. table.sort(posList, function(a, b)
  746. return a.position < b.position
  747. end)
  748. return posList
  749. end
  750. function this.Clear()
  751. --todo check yh add
  752. this.TibuClear()
  753. for _, obj in pairs(PosList) do
  754. obj:Dispose()
  755. removeObjList:Add(obj)
  756. end
  757. PosList = {}
  758. while removeObjList.size > 0 do
  759. rolePool:Put(removeObjList.buffer[removeObjList.size])
  760. removeObjList:Remove(removeObjList.size)
  761. end
  762. _DeadRoleList = {}
  763. _ReliveRoleList = {}
  764. _RealDeadList= {}
  765. tibuRoleList={}
  766. end
  767. -- 多波
  768. function this.ClearEnemy()
  769. local removePos = {}
  770. for pos, obj in pairs(PosList) do
  771. if obj.camp == 1 then
  772. removePos[pos] = 1
  773. BattleLogic.Event:DispatchEvent(BattleEventName.RemoveRole, obj)
  774. obj:Dispose()
  775. removeObjList:Add(obj)
  776. end
  777. end
  778. for pos, _ in pairs(removePos) do
  779. PosList[pos] = nil
  780. end
  781. end
  782. --> 获取当前最高速度阵营
  783. function RoleManager.GetFastCamp()
  784. local maxSpeed = -1
  785. local maxSpeedCamp = 0
  786. for pos = 1, 18 do
  787. while true
  788. do
  789. local role = RoleManager.GetRoleByPos(pos)
  790. if role and not role:IsRealDead() then
  791. if maxSpeed < role:GetRoleData(RoleDataName.Speed) then
  792. maxSpeed = role:GetRoleData(RoleDataName.Speed)
  793. maxSpeedCamp = role.camp
  794. end
  795. end
  796. break
  797. end
  798. end
  799. return maxSpeedCamp
  800. end
  801. --> deprecated 改为服务器算好位置
  802. --> 检测是否有人物数据 根据 位置信息和当前阵型 (formationPos 需要是对应camp的阵型)
  803. function RoleManager.CheckIsHaveRole(camp, position, formationPos)
  804. local pos = nil
  805. local isInFormation = false
  806. for i = 1, 5 do
  807. if formationPos[i] then
  808. if position == formationPos[i] then
  809. isInFormation = true
  810. end
  811. else
  812. --Logerror("### RoleManager.CheckIsHaveRole error!!!")
  813. end
  814. end
  815. if isInFormation then
  816. if camp == 0 then
  817. pos = position
  818. elseif camp == 1 then
  819. pos = position + 9
  820. end
  821. if PosList[pos] then
  822. return true
  823. else
  824. return false
  825. end
  826. else
  827. return false
  828. end
  829. return false
  830. end
  831. function RoleManager.CheckIsExistRole(camp, position)
  832. local pos = nil
  833. if camp == 0 then
  834. pos = position
  835. elseif camp == 1 then
  836. pos = position + 9
  837. end
  838. if PosList[pos] then
  839. return true
  840. else
  841. return false
  842. end
  843. end
  844. function this.getOneRoleProperty(role,pro)
  845. local _line=""
  846. if role ~= nil then
  847. local date =role:GetData(pro)
  848. if date~=nil then
  849. _line =tostring(date)
  850. end
  851. end
  852. return _line
  853. end
  854. function this.printLogicInfo()
  855. local line=""
  856. local tb ={}
  857. line =line.." frame".. BattleLogic.CurFrame()
  858. line =line.." curRround"..BattleLogic.GetCurRound()
  859. line =line.." curMove"..BattleLogic.GetMoveTimes()
  860. return line
  861. end
  862. local _cacheTeam
  863. function this.printEveryOneDate(_posList)
  864. _cacheTeam={}
  865. if _posList == nil then _posList = PosList end
  866. for pos,_role in pairs(PosList) do
  867. local info =this.printOneProper(_role,"self ")
  868. local prop={
  869. _info=info,
  870. _pos=pos
  871. }
  872. if prop.info==nil then
  873. prop.info="null"
  874. end
  875. table.insert(_cacheTeam,prop)
  876. end
  877. return _cacheTeam
  878. end
  879. function this.GetTeamInfos()
  880. return this.printEveryOneDate(PosList)
  881. end
  882. function this.printOneProper(_role,_sign)
  883. local _line=_role.roleId.." \n"
  884. _line=_line..this.getOneRoleProperty(_role,RoleDataName.Hp).." ".."--生命 \n"
  885. _line=_line..this.getOneRoleProperty(_role,RoleDataName.MaxHp).." ".."--最大生命 \n"
  886. _line=_line..this.getOneRoleProperty(_role,RoleDataName.Attack).." ".."--攻击力 \n"
  887. _line=_line..this.getOneRoleProperty(_role,RoleDataName.PhysicalDefence).." ".."--护甲 \n"
  888. _line=_line..this.getOneRoleProperty(_role,RoleDataName.MagicDefence).." ".."--魔抗 \n"
  889. _line=_line..this.getOneRoleProperty(_role,RoleDataName.Speed).." ".."--速度 \n"
  890. _line=_line..this.getOneRoleProperty(_role,RoleDataName.DamageBocusFactor).."-> ".."--伤害加成系数(%) \n"
  891. _line=_line..this.getOneRoleProperty(_role,RoleDataName.DamageReduceFactor).." ".."--伤害减免系数(%) \n"
  892. _line=_line..this.getOneRoleProperty(_role,RoleDataName.Hit).." ".."--施法率(% \n"
  893. _line=_line..this.getOneRoleProperty(_role,RoleDataName.Dodge).." ".."--后期基础施法率(%) \n"
  894. _line=_line..this.getOneRoleProperty(_role,RoleDataName.Crit).." ".."--暴击率(%) \n"
  895. _line=_line..this.getOneRoleProperty(_role,RoleDataName.CritDamageFactor).." ".."--暴击伤害系数(%) \n"
  896. _line=_line..this.getOneRoleProperty(_role,RoleDataName.Tenacity).." ".."--抗暴率(%) \n"
  897. _line=_line..this.getOneRoleProperty(_role,RoleDataName.TreatFacter).." ".."--治疗加成系数(%) \n"
  898. _line=_line..this.getOneRoleProperty(_role,RoleDataName.CureFacter).." ".."--受到治疗加成系数(%) \n"
  899. _line=_line..this.getOneRoleProperty(_role,RoleDataName.PhysicalDamage).." ".."--< 物伤 \n"
  900. _line=_line..this.getOneRoleProperty(_role,RoleDataName.MagicDamage).." ".."--< 法伤 \n"
  901. _line=_line..this.getOneRoleProperty(_role,RoleDataName.PhysicalImmune).."-> ".."--< 物免 \n"
  902. _line=_line..this.getOneRoleProperty(_role,RoleDataName.MagicImmune).." ->".."--< 法免 \n"
  903. _line=_line..this.getOneRoleProperty(_role,RoleDataName.SpeedAddition).." ".."--< 速度加成 \n"
  904. _line=_line..this.getOneRoleProperty(_role,RoleDataName.AttackAddition).." ".."--< 攻击加成 \n"
  905. _line=_line..this.getOneRoleProperty(_role,RoleDataName.ArmorAddition).." ".."--< 护甲加成 \n"
  906. _line=_line..this.getOneRoleProperty(_role,RoleDataName.ControlProbability).." ".."--< 控制几率 \n"
  907. _line=_line..this.getOneRoleProperty(_role,RoleDataName.ControlResist).." ".."--< 控制抵抗 \n"
  908. _line=_line..this.getOneRoleProperty(_role,RoleDataName.SkillDamage).." ".."--< 技能伤害 \n"
  909. -- _line=_line..this.getOneRoleProperty(_role,RoleDataName.DamageToMage).." ".."--< 对高爆型伤害 \n"
  910. -- _line=_line..this.getOneRoleProperty(_role,RoleDataName.DamageToFighter).." ".."--< 对穿甲型伤害 \n"
  911. -- _line=_line..this.getOneRoleProperty(_role,RoleDataName.DamageToDefender).." ".."--< 对防御型伤害 \n"
  912. -- _line=_line..this.getOneRoleProperty(_role,RoleDataName.DamageToHealer).." ".."--< 对辅助型伤害 \n"
  913. -- _line=_line..this.getOneRoleProperty(_role,RoleDataName.DefenceFromFighter).." ".."--< 受穿甲型伤害降低 \n"
  914. -- _line=_line..this.getOneRoleProperty(_role,RoleDataName.DefenceFromMage).." ".."--< 受高爆型伤害降低 \n"
  915. -- _line=_line..this.getOneRoleProperty(_role,RoleDataName.DefenceFromDefender).." ".."--< 受防御型伤害降低 \n"
  916. -- _line=_line..this.getOneRoleProperty(_role,RoleDataName.DefenceFromHealer).." ".."--< 受辅助型伤害降低 \n"
  917. _line=_line..this.getOneRoleProperty(_role,RoleDataName.CriDamageReduceRate).." ".."--< 暴伤抵抗 \n"
  918. _line=_line..this.getOneRoleProperty(_role,RoleDataName.HealCritical).." ".."--< 修理暴击 \n"
  919. _line=_line..this.getOneRoleProperty(_role,RoleDataName.HealCriEffect).." ".."--< 修理暴击效果 \n"
  920. _line=_line..this.getOneRoleProperty(_role,RoleDataName.MaxHpPercentage).." ".."--< 生命加成 \n"
  921. if _role.isRealDead then
  922. _line=_line.."》》》isdead"
  923. else
  924. _line=_line.."》》》not dead"
  925. end
  926. BattleLogManager.Log(
  927. _sign.." round:"..BattleLogic.GetCurRound().."frame:"..BattleLogic.CurFrame(),
  928. "tcamp:".. _role.camp,
  929. "tpos:".. _role.position,
  930. "prop:".. _line," end"
  931. )
  932. return _line
  933. end
  934. ------------------- 回滚状态同步
  935. --记录当前回滚帧
  936. this.nowRecored={}
  937. function this.printMirror()
  938. -- --获取克隆的人员输出属性
  939. -- BattleLogManager.Log(
  940. -- "~~~~cloneMirror round:"..BattleLogic.GetCurRound().."frame:"..BattleLogic.CurFrame().." start"
  941. -- )
  942. -- if ##this.nowRecored > 0 and #this.nowRecored._fPosList > 0 then
  943. -- local _posList = this.nowRecored._fPosList
  944. -- this.printEveryOneDate(_posList)
  945. -- end
  946. -- BattleLogManager.Log(
  947. -- "~~~~~cloneMirror round:"..BattleLogic.GetCurRound().."frame:"..BattleLogic.CurFrame().." end"
  948. -- )
  949. end
  950. --记录角色计算数据
  951. function this.LogCa(...)
  952. -- local args = {...}
  953. -- local log = args[1] .. ":\n"
  954. -- log = log .. string.format("%s = %s, ", "frame", BattleLogic.CurFrame())
  955. -- for i = 2, #args, 2 do
  956. -- local key = args[i]
  957. -- local value = args[i + 1]
  958. -- log = log .. string.format("%s = %s, ", key, value)
  959. -- end
  960. -- table.insert(RecordCaculate, log)
  961. end
  962. function this.LogArgs(id,args)
  963. if #args > 0 then
  964. for i,arg in pairs(args) do
  965. --LogError("id:"..id.." ".."arg["..i.."]:"..arg)
  966. end
  967. end
  968. end
  969. -------------------工具方法--------------
  970. local function compare(_myrole,_other)
  971. --Logerror("compare ".._myrole.roleId.." ")
  972. return _myrole:Equle(_other)
  973. end
  974. local function getRoleInPool(_role,_pool)
  975. --Logerror("getRole".._role.roleId)
  976. local ro =_pool:Find(_role,compare)
  977. return ro
  978. end
  979. local function tableCloneUID(intb,cptb,_newpool)
  980. for pos, rol in pairs(cptb) do
  981. intb[pos]=getRoleInPool(rol,_newpool)
  982. -- --Logerror("R..................y")
  983. end
  984. return intb
  985. end
  986. -------------------工具方法--------------
  987. function this.XCpy()
  988. local _cpPool = BattleObjectPool.New(function ()
  989. return RoleLogic.New()
  990. end)
  991. local function _cpRoles(_role)
  992. --Logerror("ori".._role.roleId)
  993. local _r = _role:Clone()
  994. _cpPool:Put(_r)
  995. --Logerror("new".._r.roleId)
  996. end
  997. rolePool:Foreach(_cpRoles)
  998. --Logerror("_cpPool........."..tostring(_cpPool:Count()))
  999. end
  1000. function this.CopyTableRole(_tb)
  1001. local _copyTb=BattleDictionary.New()
  1002. if _tb~=nil and #_tb>0 then
  1003. for pos=1,#_tb do
  1004. local _rol=_tb[pos]:Clone()
  1005. _copyTb:Add(pos,_rol)
  1006. end
  1007. end
  1008. return _copyTb
  1009. end
  1010. --克隆关键数据
  1011. function this.Clone()
  1012. local _roleCp = BattleObjectPool.New(function ()
  1013. return RoleLogic.New()
  1014. end)
  1015. local function _cpRoles(_role)
  1016. --Logerror("ori".._role.roleId)
  1017. local _r = _role:Clone()
  1018. _roleCp:Put(_r)
  1019. --Logerror("new".._r.roleId)
  1020. end
  1021. rolePool:Foreach(_cpRoles)
  1022. -- local _roleCp = rolePool:CloneType("RoleLogic") 做镜像数据
  1023. -- 将死之人
  1024. local function CopyTableRole(_tb,_copyTb)
  1025. if _tb~=nil and #_tb>0 then
  1026. for pos,role in pairs(_tb) do
  1027. local _rol=role:Clone()
  1028. --Logerror("_rol........."..role.roleId)
  1029. _copyTb:Add(pos,_rol)
  1030. end
  1031. end
  1032. end
  1033. -- local _roleCp = rolePool:CloneType("RoleLogic") 做镜像数据
  1034. -- 将死之人
  1035. local _CpDeadRoleList = BattleDictionary.New()
  1036. -- 重生之人
  1037. local _CpReliveRoleList = BattleDictionary.New()
  1038. -- 所有角色
  1039. local _CpPosList = BattleDictionary.New()
  1040. CopyTableRole(_DeadRoleList,_CpDeadRoleList)
  1041. CopyTableRole(_ReliveRoleList,_CpReliveRoleList)
  1042. CopyTableRole(PosList,_CpPosList)
  1043. local frame = {
  1044. _frolepool=_roleCp,
  1045. _fDeadRoleList = _CpDeadRoleList,
  1046. _fReliveRoleList = _CpReliveRoleList,
  1047. _fPosList = _CpPosList
  1048. }
  1049. return frame
  1050. end
  1051. -- 更新当前角色池中的所有角色数据
  1052. local function ContainsRoleInTable(tab,role)
  1053. if #tab <=0 then return false end
  1054. for pos,ro in pairs(tab) do
  1055. if role:Equle(ro) then
  1056. return true
  1057. end
  1058. end
  1059. return true
  1060. end
  1061. local function FindRoleInTable(tab,role)
  1062. --Logerror("F..............1")
  1063. if #tab <=0 then return nil end
  1064. for pos,ro in pairs(tab) do
  1065. if role:Equle(ro) then
  1066. return ro
  1067. end
  1068. end
  1069. return nil
  1070. end
  1071. local function FindRolePosInTable(tab,role)
  1072. if #tab <=0 then return nil end
  1073. for pos,ro in pairs(tab) do
  1074. if role:Equle(ro) then
  1075. return pos
  1076. end
  1077. end
  1078. return nil
  1079. end
  1080. --回滚战斗数据
  1081. function this.RollBack(_frame)
  1082. -- 重置对象池所有玩家数据
  1083. local frame = _frame
  1084. local _rolPol = frame._frolepool
  1085. local _FDead= frame._fDeadRoleList
  1086. local _FRelive= frame._fReliveRoleList
  1087. local _FPos= frame._fPosList
  1088. --Logerror("R..............5")
  1089. ---- 原始角色池找到对象
  1090. ---- 回滚到帧记录的对象数据
  1091. -- local function XrollBack(_role)
  1092. -- local mirro = _rolPol:Find(_role,compare)
  1093. -- if mirro ~= nil then
  1094. -- _role:RoleBack(mirro)
  1095. -- --Logerror("R..............6")
  1096. -- end
  1097. -- end
  1098. -- rolePool:Foreach(XrollBack)
  1099. --Logerror("R..............7")
  1100. -- 角色数据回滚
  1101. local function XrolFucDate(orTb,MirTb)
  1102. if #orTb<=0 then return end
  1103. --Logerror("f..............1")
  1104. for pos,role in pairs(orTb) do
  1105. if MirTb:Contains(pos) then
  1106. local _mir =MirTb:Get(pos)
  1107. -- LogError("f..............2")
  1108. this.printOneProper(_mir,"miro ")
  1109. role:RoleBack(_mir)
  1110. end
  1111. end
  1112. --Logerror("R..............x")
  1113. end
  1114. -- 角色站位回滚 --todo
  1115. local function XrolFucPos(orTb,MirTb)
  1116. if #orTb<=0 then return end
  1117. for pos,role in pairs(orTb) do
  1118. if ContainsRoleInTable(role,MirTb) then -- 在镜像数据中 回滚数据
  1119. local _ps = FindRolePosInTable(role,MirTb)
  1120. if _ps ~= pos then
  1121. --Logerror(role.roleId.."pos is error")
  1122. end
  1123. end
  1124. end
  1125. end
  1126. -- XrolFucDate(_DeadRoleList,_FDead)
  1127. --Logerror("R..............8")
  1128. -- XrolFucDate(_ReliveRoleList,_FRelive)
  1129. --Logerror("R..............9")
  1130. XrolFucDate(PosList,_FPos)
  1131. --Logerror("R..............10")
  1132. this.nowRecored = frame
  1133. return frame
  1134. end
  1135. return this