/// <summary> /// Called for update this instance. /// </summary> public void SubTick() { int subTick = this._level.GetLogicTime(); for (int i = 0; i < this._commandList.Count; i++) { LogicCommand command = this._commandList[i]; if (command.GetExecuteSubTick() < subTick) { Debugger.Error(string.Format("Execute command failed! Command should have been executed already. (type={0} server_tick={1} command_tick={2}", command.GetCommandType(), subTick, command.GetExecuteSubTick())); } if (command.GetExecuteSubTick() == subTick) { if (this.IsCommandAllowedInCurrentState(command)) { int result = command.Execute(this._level); if (result != 0) { Debugger.Warning("Execute command failed, code: " + result); } else { if (this._listener != null) { this._listener.CommandExecuted(command); } } this._commandList.Remove(i--); } else { Debugger.Error(string.Format("Execute command failed! Command not allowed in current state. (type={0} current_state={1}", command.GetCommandType(), this._level.GetState())); } } } }
/// <summary> /// Encodes this instance. /// </summary> public void Encode(ChecksumEncoder encoder) { encoder.EnableCheckSum(false); encoder.WriteInt(this._commandList.Count); for (int i = 0; i < this._commandList.Count; i++) { LogicCommand command = this._commandList[i]; encoder.WriteInt(command.GetCommandType()); command.Encode(encoder); } encoder.EnableCheckSum(true); }
/// <summary> /// Gets a value indicating whether the specified command is allowed in current state. /// </summary> public bool IsCommandAllowedInCurrentState(LogicCommand command) { int commandType = command.GetCommandType(); int state = this._level.GetState(); if (commandType >= 1000) { Debugger.Error("Execute command failed! Debug commands are not allowed when debug is off."); return(false); } if (commandType >= 500 && commandType < 700 && state != 1) { Debugger.Error("Execute command failed! Command is only allowed in home state."); return(false); } if (commandType >= 700 && commandType < 800 && state != 2 && state != 5) { Debugger.Error("Execute command failed! Command is only allowed in attack state."); return(false); } if (state == 4) { Debugger.Warning("Execute command failed! Commands are not allowed in visit state."); return(false); } return(true); }
/// <summary> /// Saves the specified command to json. /// </summary> public static void SaveCommandToJSON(LogicJSONObject jsonObject, LogicCommand command) { jsonObject.Put("ct", new LogicJSONNumber(command.GetCommandType())); jsonObject.Put("c", command.GetJSONForReplay()); }
/// <summary> /// Encodes a command. /// </summary> public static void EncodeCommand(ChecksumEncoder encoder, LogicCommand command) { encoder.WriteInt(command.GetCommandType()); command.Encode(encoder); }