void CheckCommandLogic(DebugMsg msg, EntityInfo entityInfo, ComponentInfo compInfo) { PlayerCommandRecordComponent pcrc = m_world.GetEntity(entityInfo.id).GetComp <PlayerCommandRecordComponent>(); PlayerCommandBase compLocal = pcrc.GetInputCahae(msg.frame); if (compLocal == null) { return; } compLocal.time = 0; string content = Serializer.Serialize(compLocal); if (!content.Equals(compInfo.content)) { string log = "error: frame " + msg.frame + " currentFrame:" + m_world.FrameCount + " id:" + msg + " msg.id " + entityInfo.id + " comp:" + compInfo.m_compName + "\n remote:" + compInfo.content + "\n local:" + content + "\n"; Debug.LogWarning(log); string record = ""; for (int k = msg.frame; k > msg.frame - 10; k--) { PlayerCommandBase tmp = pcrc.GetInputCahae(k); record += "\nframe " + k + " c: " + Serializer.Serialize(tmp); } Debug.Log(record); } else { //Debug.Log(" confirm " + compInfo.content); } }
void SelfCommandLogic(EntityBase entity) { //Debug.Log("SelfCommandLogic " + m_world.FrameCount); if (m_world.IsLocal) { //构建一份新指令并发送 T ncmd = new T(); ncmd.frame = m_world.FrameCount; ncmd.id = entity.ID; BuildCommand(ncmd); entity.ChangeComp(ncmd); } else { //先取服务器缓存 AddComp(entity); PlayerCommandRecordComponent rc = entity.GetComp <PlayerCommandRecordComponent>(ComponentType.PlayerCommandRecordComponent); T cmd = (T)rc.GetInputCahae(m_world.FrameCount); cmd = (T)cmd.DeepCopy(); entity.ChangeComp(cmd); //构建一份新指令并发送 T ncmd = new T(); ncmd.frame = m_world.FrameCount + 1; ncmd.id = entity.ID; BuildCommand(ncmd); if (ncmd.EqualsCmd(cmd)) { sameCmdCache.frame = m_world.FrameCount + 1; sameCmdCache.time = ClientTime.GetTime(); sameCmdCache.id = entity.ID; if (NetworkManager.IsConnect) { ProtocolAnalysisService.SendCommand(sameCmdCache); } } else { ncmd.frame = m_world.FrameCount + 1; ncmd.time = ClientTime.GetTime(); if (NetworkManager.IsConnect) { ProtocolAnalysisService.SendCommand(ncmd); } } } }
void ReceviceCommandMsg(T cmd, params object[] objs) { //Debug.Log("ReceviceCommandMsg frame " + cmd.frame + " frame " + Serializer.Serialize(cmd)); if (SyncDebugSystem.isDebug) { SyncDebugSystem.RecordMsg("cmd_commandComponent", cmd.frame, Serializer.Serialize(cmd)); } //立即返回确认消息 AffirmMsg amsg = new AffirmMsg(); amsg.index = cmd.frame; amsg.time = cmd.time; ProtocolAnalysisService.SendCommand(amsg); if (m_world.IsStart) { EntityBase entity = m_world.GetEntity(cmd.id); AddComp(entity); //自动添加记录组件 PlayerCommandRecordComponent pcrc = entity.GetComp <PlayerCommandRecordComponent>(ComponentType.PlayerCommandRecordComponent); PlayerCommandBase record = pcrc.GetInputCahae(cmd.frame); //判断和本地的预测有没有冲突 if (record == null || !record.EqualsCmd(cmd)) { pcrc.SetConflict(cmd.frame, true); } else { pcrc.SetConflict(cmd.frame, false); } if (pcrc.lastInputFrame < cmd.frame) { pcrc.lastInputFrame = cmd.frame; } pcrc.RecordCommand(cmd); //数据完整校验 if (cmd.frame != 0 && pcrc.GetAllMessage(cmd.frame) && !pcrc.GetAllMessage(cmd.frame - 1)) { ReSendMessage(cmd.frame - 1, cmd.id); } //Recalc(); } else { GameDataCacheComponent gdcc = m_world.GetSingletonComp <GameDataCacheComponent>(); gdcc.m_noExecuteCommandList.Add(cmd); } }
void ReceviceCommandMsg(T cmd, params object[] objs) { if (m_world.IsStart) { //Debug.Log("ReceviceCommandMsg " + cmd.frame + " world " + m_world.FrameCount); PlayerCommandRecordComponent pcrc = m_world.GetEntity(cmd.id).GetComp <PlayerCommandRecordComponent>(); //判断帧数 if (m_world.FrameCount >= cmd.frame) { PlayerCommandBase input = null; if (m_world.FrameCount == cmd.frame) { input = pcrc.m_forecastInput; } else { input = pcrc.GetInputCahae(cmd.frame); } //替换原来的记录 pcrc.ReplaceCommand(cmd); //与本地预测做判断,如果不一致则需要重新演算 if (!cmd.EqualsCmd(input)) { Recalc(cmd.frame); } else { //清除以前的记录 m_world.ClearBefore(cmd.frame - 1); } } //存入缓存 else { //Debug.Log("存入缓存"); pcrc.m_serverCache.Add(cmd); } } else { //存入未执行命令列表 GameDataCacheComponent gdcc = m_world.GetSingletonComp <GameDataCacheComponent>(); gdcc.m_noExecuteCommandList.Add(cmd); } }
/// <summary> /// 重演算的时候读取输入缓存 /// </summary> /// <param name="frame"></param> /// <param name="deltaTime"></param> public override void OnlyCallByRecalc(int frame, int deltaTime) { List <EntityBase> list = GetEntityList(); for (int i = 0; i < list.Count; i++) { PlayerCommandRecordComponent rc = list[i].GetComp <PlayerCommandRecordComponent>(); T cmd = (T)rc.GetInputCahae(frame); if (cmd == null) { Debug.LogError("重演算没有读取到输入记录! frame:" + frame + " ID: " + list[i].ID); return; //TODO } list[i].ChangeComp(cmd); } }
void OtherCommandLogic(EntityBase entity) { AddComp(entity); PlayerCommandRecordComponent rc = entity.GetComp <PlayerCommandRecordComponent>(); //先取服务器缓存 T cmd = (T)rc.GetInputCahae(m_world.FrameCount); //没有的话预测一份 if (cmd == null) { cmd = (T)rc.GetForecastInput(m_world.FrameCount); } rc.RecordCommand(cmd); entity.ChangeComp(cmd); }
void LoadPlayerInput(int frameCount) { List <EntityBase> list = m_world.GetEntiyList(new string[] { "PlayerCommandRecordComponent", typeof(T).Name }); for (int i = 0; i < list.Count; i++) { PlayerCommandRecordComponent pcrc = list[i].GetComp <PlayerCommandRecordComponent>(); T cmd = (T)pcrc.GetInputCahae(frameCount); if (cmd != null) { list[i].ChangeComp(cmd); } else { Debug.Log("LoadPlayerInput faild frameCount:" + frameCount); } } }
void OtherCommandLogic(EntityBase entity) { //Debug.Log("OtherCommandLogic " + m_world.FrameCount); AddComp(entity); PlayerCommandRecordComponent rc = entity.GetComp <PlayerCommandRecordComponent>(); //先取服务器缓存 T cmd = (T)rc.GetInputCahae(m_world.FrameCount); //没有的话预测一份 if (cmd == null) { //Debug.Log("预测本地操作 " + m_world.FrameCount + " id " + entity.ID); cmd = (T)rc.GetForecastInput(m_world.FrameCount); } rc.RecordCommand(cmd); entity.ChangeComp(cmd); //Debug.Log("Other cmd " + entity.ID + " content " + Serializer.Serialize(cmd) + " " + m_world.FrameCount); }
void ReceviceSameCmdMsg(SameCommand cmd, params object[] objs) { if (m_world.IsStart) { EntityBase entity = m_world.GetEntity(cmd.id); AddComp(entity); //自动添加记录组件 PlayerCommandRecordComponent pcrc = entity.GetComp <PlayerCommandRecordComponent>(); PlayerCommandBase record = pcrc.GetInputCahae(cmd.frame - 1); if (record != null) { PlayerCommandBase sameCmd = record.DeepCopy(); sameCmd.frame = cmd.frame; ReceviceCommandMsg((T)sameCmd); } //缓存中没有数据,重新请求 else { ReSendMessage(cmd.frame, cmd.id); } } }
/// <summary> /// 重演算的时候读取输入缓存 /// </summary> /// <param name="frame"></param> /// <param name="deltaTime"></param> public void OnlyCallByRecalc(int frame, int deltaTime) { List <EntityBase> list = GetEntityList(); //Debug.Log("OnlyCallByRecalc count " + list.Count); for (int i = 0; i < list.Count; i++) { AddComp(list[i]); PlayerCommandRecordComponent rc = list[i].GetComp <PlayerCommandRecordComponent>(); T cmd = (T)rc.GetInputCahae(frame); //Debug.Log("recalc cmd " + list[i].ID + " content " + Serializer.Serialize(cmd) + " " + m_world.FrameCount); if (cmd == null) { Debug.LogError("重演算没有读取到输入记录! frame:" + frame + " ID: " + list[i].ID); } else { list[i].ChangeComp(cmd); } } }
void SelfCommandLogic(EntityBase entity) { //Debug.Log("SelfCommandLogic " + m_world.FrameCount); //先取服务器缓存 AddComp(entity); PlayerCommandRecordComponent rc = entity.GetComp <PlayerCommandRecordComponent>(); T cmd = (T)rc.GetInputCahae(m_world.FrameCount); //没有的话构建一份 if (cmd == null) { cmd = new T(); cmd.frame = m_world.FrameCount; cmd.id = entity.ID; BuildCommand(cmd); rc.RecordCommand(cmd); //Debug.Log("Self cmd " + entity.ID + " content " + Serializer.Serialize(cmd) + " " + m_world.FrameCount); } else { //Debug.Log("读取 服务器缓存 输入"); } if (!m_world.IsLocal) { T record = (T)rc.GetInputCahae(m_world.FrameCount - 1); cmd.frame = m_world.FrameCount - 1; if (record != null && record.EqualsCmd(cmd)) { sameCmdCache.frame = m_world.FrameCount; sameCmdCache.time = ClientTime.GetTime(); sameCmdCache.id = entity.ID; if (NetworkManager.IsConnect) { ProtocolAnalysisService.SendCommand(sameCmdCache); } //Debug.Log("send same " + m_world.FrameCount + " id " + sameCmdCache.id); } else { //Debug.Log("send cmd " + m_world.FrameCount + " id " + cmd.id); cmd.frame = m_world.FrameCount; cmd.time = ClientTime.GetTime(); if (NetworkManager.IsConnect) { ProtocolAnalysisService.SendCommand(cmd); } } } entity.ChangeComp(cmd); }
public void DebugLogic(DebugMsg msg) { if (msg.frame == m_world.FrameCount) { for (int i = 0; i < msg.infos.Count; i++) { if (m_world.GetEntityIsExist(msg.infos[i].id)) { EntityBase entity = m_world.GetEntity(msg.infos[i].id); for (int j = 0; j < msg.infos[i].infos.Count; j++) { ComponentBase compLocal = entity.GetComp(msg.infos[i].infos[j].m_compName); if (IsFilter(msg.infos[i].infos[j].m_compName)) { string content = Serializer.Serialize(compLocal); if (!content.Equals(msg.infos[i].infos[j].content)) { RecordSystemBase rsb = m_world.GetRecordSystemBase(msg.infos[i].infos[j].m_compName); string log = "error: frame" + msg.frame + " currentFrame:" + m_world.FrameCount + " id:" + entity.ID + " msg.id " + msg.infos[i].id + " comp:" + msg.infos[i].infos[j].m_compName + "\n remote:" + msg.infos[i].infos[j].content + "\n local:" + content + "\n"; Debug.LogWarning(log); rsb.PrintRecord(entity.ID); syncLog += log; //派发冲突 GlobalEvent.DispatchEvent(c_isConflict, msg.frame); } else { //Debug.Log("ReceviceDebugMsg correct! frame " + msg.frame + " currentFrame:" + m_world.FrameCount + " id:" + entity.ID + " msg.id " + msg.infos[i].id + " comp:" + msg.infos[i].infos[j].m_compName + " content :"+ msg.infos[i].infos[j].content); } } } } else { string log = "error not find entity frame " + msg.frame + " currentFrame:" + m_world.FrameCount + " id:" + msg.infos[i].id + "\n"; Debug.LogWarning(log); syncLog += log; } } } else if (msg.frame < m_world.FrameCount) { for (int i = 0; i < msg.infos.Count; i++) { if (m_world.GetEntityIsExist(msg.infos[i].id)) { EntityBase entity = m_world.GetEntity(msg.infos[i].id); for (int j = 0; j < msg.infos[i].infos.Count; j++) { if (msg.infos[i].infos[j].m_compName == "CommandComponent") { PlayerCommandRecordComponent pcrc = m_world.GetEntity(msg.infos[i].id).GetComp <PlayerCommandRecordComponent>(); PlayerCommandBase compLocal = pcrc.GetInputCahae(msg.frame); if (compLocal == null) { return; } compLocal.time = 0; string content = Serializer.Serialize(compLocal); if (!content.Equals(msg.infos[i].infos[j].content)) { string log = "error: frame " + msg.frame + " currentFrame:" + m_world.FrameCount + " id:" + entity.ID + " msg.id " + msg.infos[i].id + " comp:" + msg.infos[i].infos[j].m_compName + "\n remote:" + msg.infos[i].infos[j].content + "\n local:" + content + "\n"; Debug.LogWarning(log); string record = ""; for (int k = msg.frame; k > msg.frame - 10; k--) { PlayerCommandBase tmp = pcrc.GetInputCahae(k); record += "\nframe " + k + " c: " + Serializer.Serialize(tmp); } Debug.Log(record); } else { //Debug.Log(" confirm " + msg.infos[i].infos[j].content); } } else { RecordSystemBase rsb = m_world.GetRecordSystemBase(msg.infos[i].infos[j].m_compName); ComponentBase compLocal = rsb.GetRecord(msg.infos[i].id, msg.frame); if (IsFilter(msg.infos[i].infos[j].m_compName)) { if (compLocal != null) { string content = Serializer.Serialize(compLocal); if (!content.Equals(msg.infos[i].infos[j].content)) { string log = "error: frame " + msg.frame + " currentFrame:" + m_world.FrameCount + " id:" + entity.ID + " msg.id " + msg.infos[i].id + " comp:" + msg.infos[i].infos[j].m_compName + "\n remote:" + msg.infos[i].infos[j].content + "\n local:" + content + "\n"; Debug.LogWarning(log); rsb.PrintRecord(entity.ID); syncLog += log; } else { //Debug.Log("ReceviceDebugMsg correct! frame " + msg.frame + " currentFrame:" + m_world.FrameCount + " id:" + entity.ID + " msg.id " + msg.infos[i].id + " comp:" + msg.infos[i].infos[j].m_compName + " content :" + msg.infos[i].infos[j].content); } //派发冲突 GlobalEvent.DispatchEvent(c_isConflict, msg.frame); } else { string log = "not find Record ->> frame:" + msg.frame + " id " + msg.infos[i].id + " compName: " + msg.infos[i].infos[j].m_compName + " currentframe: " + m_world.FrameCount + " content " + msg.infos[i].infos[j].content; //Debug.LogWarning(log); syncLog += log; } } } } } else { //string log = "error not find entity frame " + msg.frame + " currentFrame:" + m_world.FrameCount + " id:" + msg.infos[i].id + "\n"; //Debug.LogWarning(log); //syncLog += log; } } } else { string log = "服务器超前 msg:" + msg.frame + " m_world:" + m_world.FrameCount + "\n"; //Debug.LogWarning(log); syncLog += log; } }
void ReceviceCommandMsg(T cmd, params object[] objs) { //Debug.Log("ReceviceCommandMsg id" + cmd.id + " frame " + cmd.frame); if (m_world.IsStart) { PlayerCommandRecordComponent pcrc = m_world.GetEntity(cmd.id).GetComp <PlayerCommandRecordComponent>(); pcrc.lastInputFrame = cmd.frame; if (cmd.frame <= m_world.FrameCount) { //过去的命令 //如果与本地预测结果不一致,则重新演算 //不过要等到这一帧所有命令到齐才重计算 PlayerCommandBase record = pcrc.GetInputCahae(cmd.frame); if (record == null || !record.EqualsCmd(cmd)) { pcrc.m_isConflict = true; } else { pcrc.m_isConflict = false; } pcrc.RecordCommand(cmd); List <EntityBase> list = GetEntityList(); bool isAllMessage = false; bool isConflict = false; for (int i = 0; i < list.Count; i++) { PlayerCommandRecordComponent tmp = m_world.GetEntity(list[i].ID).GetComp <PlayerCommandRecordComponent>(); isAllMessage |= (tmp.lastInputFrame >= m_world.FrameCount); isConflict |= tmp.m_isConflict; } if (isAllMessage) { if (isConflict) { m_world.eventSystem.ClearCache(cmd.frame); //Debug.Log("消息不同重计算 "); Recalc(cmd.frame); } else { //此时派发确定性 m_world.eventSystem.DispatchCertainty(cmd.frame); } } if (isAllMessage) { } } else { pcrc.RecordCommand(cmd); } } else { //存入未执行命令列表 Debug.Log("存入未执行命令列表"); GameDataCacheComponent gdcc = m_world.GetSingletonComp <GameDataCacheComponent>(); //gdcc.m_noExecuteCommandList.Add(cmd); } }