BattleRecorder.cs 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. public struct RecordParamFighterSpawn
  6. {
  7. public long mFighterId;
  8. public int mTeamSide;
  9. public Vector3 mPosition;
  10. public Vector3 mForward;
  11. public Vector3 mRotation;
  12. public RecordParamFighterSpawn(long fighterId, int teamSide, Vector3 position, Vector3 forward, Vector3 rot)
  13. {
  14. mFighterId = fighterId;
  15. mPosition = position;
  16. mForward = forward;
  17. mRotation = rot;
  18. mTeamSide = teamSide;
  19. }
  20. public RecordParamFighterSpawn(JSONObject json)
  21. {
  22. mFighterId = json[0].l;
  23. mTeamSide = (int)json[1].n;
  24. mPosition = BattleRecorder.Vector3FromJson(json[2]);
  25. mForward = BattleRecorder.Vector3FromJson(json[3]);
  26. mRotation = BattleRecorder.Vector3FromJson(json[4]);
  27. }
  28. public JSONObject ToJson()
  29. {
  30. JSONObject json = new JSONObject();
  31. json.Add(mFighterId);
  32. json.Add(mTeamSide);
  33. json.Add(BattleRecorder.Vector3ToJson(mPosition));
  34. json.Add(BattleRecorder.Vector3ToJson(mForward));
  35. json.Add(BattleRecorder.Vector3ToJson(mRotation));
  36. return json;
  37. }
  38. }
  39. public struct RecordParamFighterSkill
  40. {
  41. public long mFighterId;
  42. public int mTeamSide;
  43. public int mSkillType;
  44. public int mSkillId;
  45. public RecordParamFighterSkill(long fighterId, int teamSide, int skillType, int skillId)
  46. {
  47. mFighterId = fighterId;
  48. mTeamSide = teamSide;
  49. mSkillType = skillType;
  50. mSkillId = skillId;
  51. }
  52. public RecordParamFighterSkill(JSONObject json)
  53. {
  54. mFighterId = json[0].l;
  55. mTeamSide = (int)json[1].n;
  56. mSkillType = (int)json[2].n;
  57. mSkillId = (int)json[3].n;
  58. }
  59. public JSONObject ToJson()
  60. {
  61. JSONObject json = new JSONObject();
  62. json.Add(mFighterId);
  63. json.Add(mTeamSide);
  64. json.Add(mSkillType);
  65. json.Add(mSkillId);
  66. return json;
  67. }
  68. }
  69. public struct RecordParamFighterPassiveSkill
  70. {
  71. public long mFighterId;
  72. public int mTeamSide;
  73. public int mSkillId;
  74. public RecordParamFighterPassiveSkill(long fighterId, int teamSide, int skillId)
  75. {
  76. mFighterId = fighterId;
  77. mTeamSide = teamSide;
  78. mSkillId = skillId;
  79. }
  80. public RecordParamFighterPassiveSkill(JSONObject json)
  81. {
  82. mFighterId = json[0].l;
  83. mTeamSide = (int)json[1].n;
  84. mSkillId = (int)json[2].n;
  85. }
  86. public JSONObject ToJson()
  87. {
  88. JSONObject json = new JSONObject();
  89. json.Add(mFighterId);
  90. json.Add(mTeamSide);
  91. json.Add(mSkillId);
  92. return json;
  93. }
  94. }
  95. public struct RecordParamFighterStartFighting
  96. {
  97. public long mFighterId;
  98. public int mTeamSide;
  99. public RecordParamFighterStartFighting(long fighterId, int teamSide)
  100. {
  101. mFighterId = fighterId;
  102. mTeamSide = teamSide;
  103. }
  104. public RecordParamFighterStartFighting(JSONObject json)
  105. {
  106. mFighterId = json[0].l;
  107. mTeamSide = (int)json[1].n;
  108. }
  109. public JSONObject ToJson()
  110. {
  111. JSONObject json = new JSONObject();
  112. json.Add(mFighterId);
  113. json.Add(mTeamSide);
  114. return json;
  115. }
  116. }
  117. public struct RecordParamFighterMove
  118. {
  119. public long mFighterId;
  120. public int mTeamSide;
  121. public long mTargetId;
  122. public int mTargetTeamSide;
  123. public float mStopDist;
  124. public RecordParamFighterMove(long fighterId, int teamSide, long targetIdx, int targetTeamSide, float stopDist)
  125. {
  126. mFighterId = fighterId;
  127. mTeamSide = teamSide;
  128. mTargetId = targetIdx;
  129. mTargetTeamSide = targetTeamSide;
  130. mStopDist = stopDist;
  131. }
  132. public RecordParamFighterMove(JSONObject json)
  133. {
  134. mFighterId = json[0].l;
  135. mTeamSide = (int)json[1].n;
  136. mTargetId = json[2].l;
  137. mTargetTeamSide = (int)json[3].n;
  138. mStopDist = json[4].f;
  139. }
  140. public JSONObject ToJson()
  141. {
  142. JSONObject json = new JSONObject();
  143. json.Add(mFighterId);
  144. json.Add(mTeamSide);
  145. json.Add(mTargetId);
  146. json.Add(mTargetTeamSide);
  147. json.Add(mStopDist);
  148. return json;
  149. }
  150. }
  151. public struct RecordParamFighterSkillData
  152. {
  153. public int mSkillId;
  154. public float mCastingTime;
  155. public long mActorId;
  156. public int mActorBaseId;
  157. public int mTeamSide;
  158. public List<stDamageParam> mDamages;
  159. public RecordParamFighterSkillData(int skillId,float castingTime,long actorId,int actorBaseId,int teamSide, List<stDamageParam> damages)
  160. {
  161. this.mSkillId = skillId;
  162. this.mCastingTime = castingTime;
  163. this.mActorId = actorId;
  164. this.mActorBaseId = actorBaseId;
  165. this.mTeamSide = teamSide;
  166. this.mDamages = damages;
  167. }
  168. }
  169. public struct RecordActorParam
  170. {
  171. public int uid;
  172. public int baseId;
  173. public int teamSide;
  174. public int life;
  175. public int sp;
  176. public RecordActorParam(int id, int bId, int team, int life, int sp)
  177. {
  178. this.uid = id;
  179. this.baseId = bId;
  180. this.teamSide = team;
  181. this.life = life;
  182. this.sp = sp;
  183. }
  184. }
  185. public class BattleRecorder
  186. {
  187. public static JSONObject Vector3ToJson(Vector3 v, float multiply = 1e3f)
  188. {
  189. JSONObject json = new JSONObject();
  190. json.Add((int)(v.x * multiply));
  191. json.Add((int)(v.y * multiply));
  192. json.Add((int)(v.z * multiply));
  193. return json;
  194. }
  195. public static Vector3 Vector3FromJson(JSONObject json, float multiply = 1e-3f)
  196. {
  197. return new Vector3(json[0].n * multiply, json[1].n * multiply, json[2].n * multiply);
  198. }
  199. public static JSONObject ColorToJson(Color clr)
  200. {
  201. JSONObject json = new JSONObject();
  202. json.Add((int)clr.r * 255);
  203. json.Add((int)clr.g * 255);
  204. json.Add((int)clr.b * 255);
  205. json.Add((int)clr.a * 255);
  206. return json;
  207. }
  208. public static Color ColorFromJson(JSONObject json)
  209. {
  210. float multipy = 1.0f / 255;
  211. return new Color(json[0].n * multipy, json[1].n * multipy, json[2].n * multipy, json[3].n * multipy);
  212. }
  213. public static JSONObject Vector2ToJson(Vector2 v, float multiply = 1e3f)
  214. {
  215. JSONObject json = new JSONObject();
  216. json.Add((int)(v.x * multiply));
  217. json.Add((int)(v.y * multiply));
  218. return json;
  219. }
  220. public static Vector2 Vector2FromJson(JSONObject json, float multiply = 1e-3f)
  221. {
  222. return new Vector2(json[0].n * multiply, json[1].n * multiply);
  223. }
  224. static List<int> mFullRecData;
  225. static int mFullRecCompIdx;
  226. public static bool EnableFullRecCompare = false;
  227. public static int LastErrorIndex = -1;
  228. public enum RecordType
  229. {
  230. FighterSpawn = 0,
  231. FighterStartFighting = 1,
  232. FighterSkill = 2,
  233. FighterMove = 3,
  234. BattleEnd = 4,
  235. FighterPassiveSkill = 5,
  236. }
  237. public class Record
  238. {
  239. public int mFieldState;
  240. public int mStateFrame;
  241. public RecordType mRecordType;
  242. public object mParams;
  243. public Record(BaseBattle battle, RecordType recType, object param)
  244. {
  245. mFieldState = battle.CurBattleField == null ? 0 : battle.CurBattleField.CurrentFieldState;
  246. if(recType == RecordType.FighterSpawn)
  247. {
  248. mStateFrame = 0;
  249. }
  250. else
  251. {
  252. mStateFrame = battle.CurBattleField == null ? battle.FrameCount : battle.CurBattleField.CurrentStateFrame;
  253. }
  254. mRecordType = recType;
  255. mParams = param;
  256. }
  257. public Record(JSONObject json)
  258. {
  259. int tmpIdx = 0;
  260. mFieldState = (int)json[tmpIdx++].n;
  261. mStateFrame = (int)json[tmpIdx++].n;
  262. mRecordType = (RecordType)(int)json[tmpIdx++].n;
  263. JSONObject paramJson = json[tmpIdx++];
  264. switch (mRecordType)
  265. {
  266. case RecordType.FighterSpawn:
  267. mParams = new RecordParamFighterSpawn(paramJson);
  268. break;
  269. case RecordType.FighterStartFighting:
  270. mParams = new RecordParamFighterStartFighting(paramJson);
  271. break;
  272. case RecordType.FighterSkill:
  273. mParams = new RecordParamFighterSkill(paramJson);
  274. break;
  275. case RecordType.FighterPassiveSkill:
  276. mParams = new RecordParamFighterPassiveSkill(paramJson);
  277. break;
  278. case RecordType.FighterMove:
  279. mParams = new RecordParamFighterMove(paramJson);
  280. break;
  281. case RecordType.BattleEnd:
  282. int[] lifeArray = new int[paramJson.Count];
  283. for (int i = 0; i < lifeArray.Length; i++)
  284. lifeArray[i] = (int)paramJson[i].n;
  285. mParams = lifeArray;
  286. break;
  287. }
  288. }
  289. public JSONObject ToJson()
  290. {
  291. JSONObject json = new JSONObject();
  292. json.Add(mFieldState);
  293. json.Add(mStateFrame);
  294. json.Add((int)mRecordType);
  295. switch (mRecordType)
  296. {
  297. case RecordType.FighterSpawn:
  298. json.Add(((RecordParamFighterSpawn)mParams).ToJson());
  299. break;
  300. case RecordType.FighterStartFighting:
  301. json.Add(((RecordParamFighterStartFighting)mParams).ToJson());
  302. break;
  303. case RecordType.FighterSkill:
  304. json.Add(((RecordParamFighterSkill)mParams).ToJson());
  305. break;
  306. case RecordType.FighterPassiveSkill:
  307. json.Add(((RecordParamFighterPassiveSkill)mParams).ToJson());
  308. break;
  309. case RecordType.FighterMove:
  310. json.Add(((RecordParamFighterMove)mParams).ToJson());
  311. break;
  312. case RecordType.BattleEnd:
  313. JSONObject lifesJson = new JSONObject();
  314. long[] lifeArray = (long[])mParams;
  315. for (int i = 0; i < lifeArray.Length; i++)
  316. lifesJson.Add(lifeArray[i]);
  317. json.Add(lifesJson);
  318. break;
  319. }
  320. return json;
  321. }
  322. }
  323. private int mMapId = 0;
  324. private int mLevelId = 0;
  325. private int mBattleMode = 0;
  326. private int mBattleSubMode = 0;
  327. private BaseBattle mBattle;
  328. private List<Record> mRecords;
  329. private int mNextRecordIdx;
  330. private List<ActorData> mLeftActors;
  331. private List<ActorData> mRightActors;
  332. //检测录像错误超时
  333. const int nCheckReplayErrorTime = 5;
  334. int nCurrReplayErrorTime = -1;
  335. bool bIsReplayError = false;
  336. public bool ResultCode { get; private set; }
  337. public long ReplayResultCode { get; private set; }
  338. public int Seed { get; set; }
  339. public ValType[] Factors { get; set; }
  340. public bool SwapReplay { get; set; }
  341. public int MapId
  342. {
  343. get { return mMapId; }
  344. }
  345. public int LevelId
  346. {
  347. get { return mLevelId; }
  348. }
  349. public int BattleMode
  350. {
  351. get { return mBattleMode; }
  352. }
  353. public int BattleSubMode
  354. {
  355. get { return mBattleSubMode; }
  356. }
  357. private string recordStr;
  358. public string RecordStr
  359. {
  360. get { return recordStr; }
  361. }
  362. public List<ActorData> LeftActors
  363. {
  364. get { return mLeftActors; }
  365. }
  366. public List<ActorData> RightActors
  367. {
  368. get { return mRightActors; }
  369. }
  370. private List<RecordParamFighterSkillData> mSkillHitRecords = new List<RecordParamFighterSkillData>();
  371. public List<RecordParamFighterSkillData> SkillHitRecords
  372. {
  373. get { return mSkillHitRecords; }
  374. }
  375. public BattleRecorder(JSONObject json)
  376. {
  377. if(json.HasField("result"))
  378. {
  379. ResultCode = json.GetField("result").b;
  380. }
  381. if(json.HasField("mapId"))
  382. {
  383. mMapId = (int)json.GetField("mapId").n;
  384. }
  385. if(json.HasField("levelId"))
  386. {
  387. mLevelId = (int)json.GetField("levelId").n;
  388. }
  389. if(json.HasField("mode"))
  390. {
  391. mBattleMode = (int)json.GetField("mode").n;
  392. }
  393. if(json.HasField("subMode"))
  394. {
  395. mBattleSubMode = (int)json.GetField("subMode").n;
  396. }
  397. if (json.HasField("seed"))
  398. {
  399. Seed = (int)json.GetField("seed").n;
  400. }
  401. if(json.HasField("factors"))
  402. {
  403. JSONObject factorsJson = json.GetField("factors");
  404. if(factorsJson.isContainer)
  405. {
  406. Factors = new ValType[factorsJson.Count];
  407. for (int idx = 0; idx < factorsJson.Count; idx++)
  408. {
  409. JSONObject factorJson = factorsJson[idx];
  410. int id = (int)factorJson.GetField("id").n;
  411. int val = (int)factorJson.GetField("val").n;
  412. Factors[idx] = new ValType(id, val);
  413. }
  414. }
  415. }
  416. mLeftActors = new List<ActorData>();
  417. mRightActors = new List<ActorData>();
  418. if (json.HasField("actors"))
  419. {
  420. JSONObject obj = json.GetField("actors");
  421. if(obj.HasField("LA"))
  422. {
  423. JSONObject leftActorsJson = obj.GetField("LA");
  424. for(int idx = 0;idx < leftActorsJson.Count;idx++)
  425. {
  426. ActorData actor = ActorData.CreateActorFromJson(leftActorsJson[idx]);
  427. if(actor!=null)
  428. {
  429. actor.IsRecordActor = true;
  430. actor.IsMainRole = false; //此处一定要加 录像模式 否则会把现有主角的模型替换(IsMainRole会引用一个模型OBJ)
  431. mLeftActors.Add(actor);
  432. }
  433. }
  434. }
  435. if(obj.HasField("RA"))
  436. {
  437. JSONObject rightActorsJson = obj.GetField("RA");
  438. for (int idx = 0; idx < rightActorsJson.Count; idx++)
  439. {
  440. ActorData actor = ActorData.CreateActorFromJson(rightActorsJson[idx]);
  441. if (actor != null)
  442. {
  443. actor.IsMainRole = false;
  444. mRightActors.Add(actor);
  445. }
  446. }
  447. }
  448. for(int idx =0; idx < mLeftActors.Count;idx++)
  449. {
  450. var actor = mLeftActors[idx];
  451. if(actor.IsFellow || actor.IsHero)
  452. {
  453. if(actor.PetId > 0)
  454. {
  455. var petActor = GetActorById(actor.PetId, 0);
  456. actor.SetPet(petActor);
  457. }
  458. }
  459. }
  460. for(int idx =0; idx < mRightActors.Count;idx++)
  461. {
  462. var actor = mRightActors[idx];
  463. if (actor.IsFellow || actor.IsHero)
  464. {
  465. if (actor.PetId > 0)
  466. {
  467. var petActor = GetActorById(actor.PetId, 1);
  468. actor.SetPet(petActor);
  469. }
  470. }
  471. }
  472. }
  473. if(json.HasField("records"))
  474. {
  475. JSONObject recordsJson = json.GetField("records");
  476. mRecords = new List<Record>();
  477. for (int i = 0; i < recordsJson.Count; i++)
  478. mRecords.Add(new Record(recordsJson[i]));
  479. }
  480. }
  481. public void SetLeftHeroName(string strName)
  482. {
  483. for (int idx = 0; idx < mLeftActors.Count; idx++)
  484. {
  485. var actor = mLeftActors[idx];
  486. if (actor.IsHero)
  487. {
  488. actor.Name = strName;
  489. }
  490. }
  491. }
  492. public BattleRecorder(BaseBattle battle)
  493. {
  494. mBattle = battle;
  495. mBattleMode = (int)mBattle.Mode;
  496. mBattleSubMode = (int)mBattle.SubBattleMode;
  497. if (mBattle.IsNormalBattle)
  498. {
  499. mMapId = battle.CurBattleField.BattleInfo.MapId;
  500. mLevelId = battle.CurBattleField.BattleInfo.LevelId;
  501. }
  502. Seed = battle.RandSeed;
  503. Factors = battle.Factors;
  504. mRecords = new List<Record>();
  505. mLeftActors = new List<ActorData>();
  506. mRightActors = new List<ActorData>();
  507. mLeftActors.AddRange(mBattle.CurBattleField.TeamActors);
  508. mRightActors.Clear();
  509. bIsReplayError = false;
  510. nCurrReplayErrorTime = -1;
  511. }
  512. public void ResetToPlayRecord(BaseBattle battle)
  513. {
  514. mBattle = battle;
  515. mNextRecordIdx = 0;
  516. bIsReplayError = false;
  517. nCurrReplayErrorTime = -1;
  518. }
  519. //检测录像错误
  520. public void CheckErrorRecord(bool bIsProcessOk)
  521. {
  522. //if (BattleMgr.Instance.GetLeftFightingTime() <= 0.0f)
  523. //{
  524. // return;
  525. //}
  526. if (bIsProcessOk)
  527. {
  528. nCurrReplayErrorTime = (int)Time.realtimeSinceStartup;
  529. return;
  530. }
  531. if (nCurrReplayErrorTime == -1)
  532. return;
  533. if (mBattle.IsPlayRecord && !bIsReplayError)
  534. {
  535. int nTime = (int)Time.realtimeSinceStartup - nCurrReplayErrorTime;
  536. if (nTime >= nCheckReplayErrorTime)//长时间 执行失败 退出战斗
  537. {
  538. nCurrReplayErrorTime = -1;
  539. //弹窗 退出战斗
  540. bIsReplayError = true;
  541. //录像错误框
  542. ShowErrorMsgBox();
  543. }
  544. }
  545. }
  546. public int ProcessFrameRecord(RecordType expectedRecType, Fighter expectFighter = null)
  547. {
  548. int state = mBattle.CurBattleField.CurrentFieldState;
  549. int stateFrame = mBattle.CurBattleField.CurrentStateFrame;
  550. int cnt = 0;
  551. //DebugHelper.LogError(string.Format("<color=#00ffff>{0} -----ProcessFrameRecord ---- stateFrame = {1} mNextRecordIdx={2}</color>", state, stateFrame, mNextRecordIdx));
  552. while (mNextRecordIdx < mRecords.Count)
  553. {
  554. Record rec = mRecords[mNextRecordIdx];
  555. if (rec.mFieldState > state)
  556. {
  557. //DebugHelper.LogError("11:" + expectedRecType + " -- " + rec.mStateFrame + " 不同:" + stateFrame + " mNextRecordIdx = " + mNextRecordIdx);
  558. CheckErrorRecord(false);
  559. return cnt;
  560. }
  561. if (rec.mFieldState == state && rec.mStateFrame > stateFrame)
  562. {
  563. //DebugHelper.LogError("22:" + expectedRecType + " -- " + rec.mStateFrame + " 不同:" + stateFrame + " mNextRecordIdx = " + mNextRecordIdx);
  564. CheckErrorRecord(false);
  565. return cnt;
  566. }
  567. if (rec.mRecordType != expectedRecType)
  568. {
  569. //DebugHelper.LogError("33:" + rec.mRecordType + " -- " + expectedRecType + " 不同:" + stateFrame + " mNextRecordIdx = " + mNextRecordIdx);
  570. CheckErrorRecord(false);
  571. return cnt;
  572. }
  573. bool bIsok = ProcessRecord(rec, expectFighter);
  574. CheckErrorRecord(bIsok);
  575. if (!bIsok)
  576. {
  577. //DebugHelper.LogError("44: ProcessRecord 失败:"+ expectedRecType+" " + stateFrame + " mNextRecordIdx = " + mNextRecordIdx);
  578. return cnt;
  579. }
  580. else
  581. {
  582. //DebugHelper.LogError(string.Format("<color=#ff0000>{0} srcFrame= {1} currentFrame={2} mNextRecordIdx={3}</color>", expectedRecType, rec.mStateFrame, stateFrame, mNextRecordIdx));
  583. }
  584. cnt++;
  585. mNextRecordIdx++;
  586. }
  587. return cnt;
  588. }
  589. public void RecordFighterSpawn(Fighter fighter)
  590. {
  591. if(fighter.TeamSide == eTeamType.Friend)
  592. {
  593. if (!mLeftActors.Contains(fighter.Actor))
  594. {
  595. mLeftActors.Add(fighter.Actor);
  596. }
  597. }
  598. else if(fighter.TeamSide == eTeamType.Enemy)
  599. {
  600. if (!mRightActors.Contains(fighter.Actor))
  601. {
  602. mRightActors.Add(fighter.Actor);
  603. }
  604. }
  605. mRecords.Add(new Record(mBattle, RecordType.FighterSpawn, new RecordParamFighterSpawn(fighter.Id, (int) fighter.TeamSide, fighter.Position, fighter.Forward,fighter.Ctrl.transform.rotation.eulerAngles)));
  606. }
  607. public void RecordFighterStartFighting(Fighter fighter)
  608. {
  609. mRecords.Add(new Record(mBattle, RecordType.FighterStartFighting, new RecordParamFighterStartFighting(fighter.Id, (int)fighter.TeamSide)));
  610. }
  611. public void RecordFighterSkill(Fighter fighter, int skillType,int skillId)
  612. {
  613. mRecords.Add(new Record(mBattle, RecordType.FighterSkill, new RecordParamFighterSkill(fighter.Id, (int)fighter.TeamSide, skillType, skillId)));
  614. }
  615. public void RecordFighterPassiveSkill(Fighter fighter,int skillId)
  616. {
  617. mRecords.Add(new Record(mBattle, RecordType.FighterPassiveSkill, new RecordParamFighterPassiveSkill(fighter.Id, (int)fighter.TeamSide, skillId)));
  618. }
  619. public void RecordFighterMove(Fighter fighter, Fighter target,float stopDist)
  620. {
  621. mRecords.Add(new Record(mBattle, RecordType.FighterMove, new RecordParamFighterMove(fighter.Id, (int)fighter.TeamSide, target.Id, (int)target.TeamSide,stopDist)));
  622. }
  623. public void RecordFighterSkillData(int skillId,float castingTime,long actorId,int actorBaseId,int teamSide,List<stDamageParam> damages)
  624. {
  625. RecordParamFighterSkillData data = new RecordParamFighterSkillData(skillId,castingTime,actorId,actorBaseId,teamSide, damages);
  626. mSkillHitRecords.Add(data);
  627. }
  628. public void RecordBattleEnd(bool win)
  629. {
  630. List<Fighter> allFigher = mBattle.FighterMgr.AllFighters;
  631. long leftLifeTotal = 0;
  632. long rightLifeTotal = 0;
  633. long[] lifeArray = new long[mLeftActors.Count + mRightActors.Count];
  634. for (int i = 0; i < mLeftActors.Count; i++)
  635. {
  636. Fighter fighter = allFigher.Find(a => a.Actor == mLeftActors[i]);
  637. if (fighter != null)
  638. {
  639. lifeArray[i] = fighter.Life;
  640. leftLifeTotal += fighter.Life;
  641. }
  642. }
  643. for (int i = 0; i < mRightActors.Count; i++)
  644. {
  645. Fighter fighter = allFigher.Find(a => a.Actor == mRightActors[i]);
  646. if (fighter != null)
  647. {
  648. lifeArray[i + mLeftActors.Count] = fighter.Life;
  649. rightLifeTotal += fighter.Life;
  650. }
  651. }
  652. ResultCode = win;
  653. mRecords.Add(new Record(mBattle, RecordType.BattleEnd, lifeArray));
  654. recordStr = CompressionUtil.Compress(ToJson().ToString());
  655. //BattleSimulator.SaveBattleRecorder(recordStr);
  656. }
  657. public JSONObject ToJson()
  658. {
  659. JSONObject json = new JSONObject();
  660. json.AddField("result",ResultCode);
  661. json.AddField("mapId",mMapId);
  662. json.AddField("levelId", mLevelId);
  663. json.AddField("mode", mBattleMode);
  664. json.AddField("subMode", mBattleSubMode);
  665. json.AddField("seed",Seed);
  666. JSONObject factorsJson = new JSONObject();
  667. if(Factors!=null && Factors.Length > 0)
  668. {
  669. for(int idx =0; idx < Factors.Length;idx++)
  670. {
  671. JSONObject factorJson = new JSONObject();
  672. factorJson.AddField("id", Factors[idx].id);
  673. factorJson.AddField("val", Factors[idx].val);
  674. factorsJson.Add(factorJson);
  675. }
  676. }
  677. json.AddField("factors", factorsJson);
  678. JSONObject actorsJson = new JSONObject();
  679. JSONObject leftActorsJson = new JSONObject();
  680. for(int idx =0; idx < mBattle.CurBattleField.TeamFighters.Count;idx++)
  681. {
  682. Fighter f = mBattle.CurBattleField.TeamFighters[idx];
  683. leftActorsJson.Add(f.RecordJson);
  684. }
  685. actorsJson.AddField("LA", leftActorsJson);
  686. JSONObject rightActorsJson = new JSONObject();
  687. for (int idx =0; idx < mBattle.CurBattleField.EnemyFighters.Count;idx++)
  688. {
  689. Fighter f = mBattle.CurBattleField.EnemyFighters[idx];
  690. rightActorsJson.Add(f.RecordJson);
  691. }
  692. actorsJson.AddField("RA", rightActorsJson);
  693. json.AddField("actors",actorsJson);
  694. JSONObject recordsJson = new JSONObject();
  695. for (int i = 0; i < mRecords.Count; i++)
  696. recordsJson.Add(mRecords[i].ToJson());
  697. json.AddField("records",recordsJson);
  698. return json;
  699. }
  700. public void Skip()
  701. {
  702. if (mNextRecordIdx >= mRecords.Count || mRecords.Count == 0)
  703. return;
  704. Record lastRec = mRecords[mRecords.Count - 1];
  705. if (lastRec.mRecordType != RecordType.BattleEnd)
  706. return;
  707. int[] lifeArray = (int[])lastRec.mParams;
  708. List<Fighter> allFigher = mBattle.FighterMgr.AllFighters;
  709. int leftAliveCount = 0;
  710. for (int i = 0; i < mLeftActors.Count; i++)
  711. {
  712. Fighter fighter = allFigher.Find(a => a.Actor == mLeftActors[i]);
  713. if (fighter != null && fighter.IsSpawned && !fighter.IsDisposed && fighter.IsAlive)
  714. {
  715. fighter.ForceIdle();
  716. fighter.StateData.BuffMgr.Clear();
  717. //fighter.Life = lifeArray[i];
  718. //if (fighter.IsAlive)
  719. // leftAliveCount++;
  720. }
  721. }
  722. int rightAliveCount = 0;
  723. for (int i = 0; i < mRightActors.Count; i++)
  724. {
  725. Fighter fighter = allFigher.Find(a => a.Actor == mRightActors[i]);
  726. int idx = i + mLeftActors.Count;
  727. if (fighter != null && fighter.IsSpawned && !fighter.IsDisposed && fighter.IsAlive)
  728. {
  729. fighter.ForceIdle();
  730. fighter.StateData.BuffMgr.Clear();
  731. //fighter.Life = lifeArray[idx];
  732. //if (fighter.IsAlive)
  733. // rightAliveCount++;
  734. }
  735. }
  736. if (mBattle.CurBattleField != null &&
  737. (mBattle.CurBattleField.IsFightingState ||
  738. mBattle.CurBattleField.IsIdleState))
  739. mBattle.CurBattleField.PeaceEnd();
  740. }
  741. Fighter GetFighterById(long id,int teamSide)
  742. {
  743. ActorData actor = null;
  744. if (teamSide == 0)
  745. {
  746. for(int idx =0; idx < mLeftActors.Count;idx++)
  747. {
  748. if(mLeftActors[idx].ID == id)
  749. {
  750. actor = mLeftActors[idx];
  751. break;
  752. }
  753. }
  754. }
  755. else
  756. {
  757. for(int idx =0; idx < mRightActors.Count;idx++)
  758. {
  759. if(mRightActors[idx].ID == id)
  760. {
  761. actor = mRightActors[idx];
  762. break;
  763. }
  764. }
  765. }
  766. if (actor == null)
  767. return null;
  768. return mBattle.FighterMgr.GetFighterByID(actor.ID, (eTeamType)teamSide);
  769. }
  770. ActorData GetActorById(long id, int teamSide)
  771. {
  772. if(teamSide == 0)
  773. {
  774. if (mLeftActors == null) return null;
  775. for(int idx =0; idx < mLeftActors.Count;idx++)
  776. {
  777. if(mLeftActors[idx].ID == id)
  778. {
  779. return mLeftActors[idx];
  780. }
  781. }
  782. }
  783. else
  784. {
  785. if (mRightActors == null) return null;
  786. for(int idx =0; idx < mRightActors.Count;idx++)
  787. {
  788. if (mRightActors[idx].ID == id)
  789. return mRightActors[idx];
  790. }
  791. }
  792. return null;
  793. }
  794. bool ProcessRecord(Record rec, object expectFighter)
  795. {
  796. switch (rec.mRecordType)
  797. {
  798. case RecordType.FighterSpawn:
  799. {
  800. RecordParamFighterSpawn param = (RecordParamFighterSpawn)rec.mParams;
  801. Fighter fighter = GetFighterById(param.mFighterId,param.mTeamSide);
  802. if (expectFighter != null && fighter != expectFighter)
  803. return false;
  804. if (fighter == null) return false;
  805. if (fighter.IsSpawned)
  806. {
  807. DebugHelper.Log("invalid fighter state!");
  808. return false;
  809. }
  810. fighter.Spawn(param.mPosition, param.mForward, Quaternion.Euler(param.mRotation));
  811. }
  812. break;
  813. case RecordType.FighterStartFighting:
  814. {
  815. RecordParamFighterStartFighting param = (RecordParamFighterStartFighting)rec.mParams;
  816. Fighter fighter = GetFighterById(param.mFighterId,param.mTeamSide);
  817. if (null == fighter ||
  818. !fighter.IsAlive ||
  819. !fighter.IsSpawned ||
  820. fighter.IsDisposed ||
  821. fighter.Actor.IsDisposed)
  822. {
  823. DebugHelper.LogError("invalid fighter state! random value {0}", Seed);
  824. return true;//角色无效跳过
  825. }
  826. if (expectFighter != null && fighter != expectFighter)
  827. return false;
  828. if (!fighter.IsAlive || fighter.IsFighting)
  829. {
  830. return false;
  831. }
  832. fighter.OnFightingStart();
  833. }
  834. break;
  835. case RecordType.FighterSkill:
  836. {
  837. RecordParamFighterSkill param = (RecordParamFighterSkill)rec.mParams;
  838. Fighter fighter = GetFighterById(param.mFighterId, param.mTeamSide);
  839. if (null == fighter ||
  840. !fighter.IsAlive ||
  841. !fighter.IsSpawned ||
  842. fighter.IsDisposed ||
  843. fighter.Actor.IsDisposed)
  844. {
  845. return true;//角色无效跳过
  846. }
  847. if (expectFighter != null && fighter != expectFighter)
  848. return false;
  849. fighter.ForceDoSkill(param.mSkillType, param.mSkillId);
  850. }
  851. break;
  852. case RecordType.FighterPassiveSkill:
  853. {
  854. RecordParamFighterPassiveSkill param = (RecordParamFighterPassiveSkill)rec.mParams;
  855. Fighter fighter = GetFighterById(param.mFighterId, param.mTeamSide);
  856. if (null == fighter ||
  857. !fighter.IsAlive ||
  858. !fighter.IsSpawned ||
  859. fighter.IsDisposed ||
  860. fighter.Actor.IsDisposed)
  861. {
  862. return true;//角色无效跳过
  863. }
  864. if (expectFighter != null && fighter != expectFighter)
  865. return false;
  866. fighter.DoPassiveSkill(param.mSkillId);
  867. }
  868. break;
  869. case RecordType.FighterMove:
  870. {
  871. RecordParamFighterMove param = (RecordParamFighterMove)rec.mParams;
  872. Fighter fighter = GetFighterById(param.mFighterId, param.mTeamSide);
  873. Fighter target = GetFighterById(param.mTargetId, param.mTargetTeamSide);
  874. if (target == null)
  875. {
  876. DebugHelper.LogError("FighterMove 目标不存在");
  877. return true;
  878. }
  879. if (expectFighter != null && fighter != expectFighter)
  880. {
  881. //DebugHelper.LogError("FighterMove 执行者 不存在");
  882. return false;
  883. }
  884. if (fighter == null) return true;
  885. bool ret = fighter.AutoChaseTo(target, param.mStopDist);
  886. }
  887. break;
  888. }
  889. return true;
  890. }
  891. public void CalculateReplayResult()
  892. {
  893. List<Fighter> allFigher = mBattle.FighterMgr.AllFighters;
  894. long leftLifeTotal = 0;
  895. long rightLifeTotal = 0;
  896. long[] lifeArray = new long[mLeftActors.Count + mRightActors.Count];
  897. for (int i = 0; i < mLeftActors.Count; i++)
  898. {
  899. Fighter fighter = allFigher.Find(a => a.Actor == mLeftActors[i]);
  900. if (fighter != null)
  901. {
  902. lifeArray[i] = fighter.Life;
  903. leftLifeTotal += fighter.Life;
  904. }
  905. }
  906. for (int i = 0; i < mRightActors.Count; i++)
  907. {
  908. Fighter fighter = allFigher.Find(a => a.Actor == mRightActors[i]);
  909. if (fighter != null)
  910. {
  911. lifeArray[i + mLeftActors.Count] = fighter.Life;
  912. rightLifeTotal += fighter.Life;
  913. // Log.D (" team {0}, actor {1}, life {2}", fighter.Id, fighter.TeamSide, fighter.Life);
  914. }
  915. }
  916. ReplayResultCode = (leftLifeTotal - rightLifeTotal) % 100000;
  917. DebugHelper.Log(string.Format("result code:{0}, replay result code:{1}", ResultCode, ReplayResultCode));
  918. }
  919. private void ShowErrorMsgBox()
  920. {
  921. LuaInterface.LuaState pLuaState = LuaMgr.GetMainState();
  922. if (null != pLuaState)
  923. {
  924. string strLuaOpen = "ManagerContainer.LuaBattleMgr:ShowErrorQuitBattleMsgBox()";
  925. LuaMgr.GetMainState().DoString(strLuaOpen);
  926. }
  927. }
  928. private void CloseErrorMsgBox()
  929. {
  930. LuaInterface.LuaState pLuaState = LuaMgr.GetMainState();
  931. if (null != pLuaState)
  932. {
  933. string strLuaClose = "ManagerContainer.LuaBattleMgr:CloseErrorQuitBattleMsgBox()";
  934. LuaMgr.GetMainState().DoString(strLuaClose);
  935. }
  936. }
  937. }