private void CreateUnitController(VoxelData voxelData, Coordinate coordinate) { voxelData.UnitOrAssetIndex = m_identity; IMatchUnitControllerCli unit = MatchFactoryCli.CreateUnitController(m_voxelMap.Map, coordinate, voxelData.Type, m_playerIndex, m_allAbilities); if (VoxelData.IsControllableUnit(voxelData.Type)) { m_controllableUnitsCount++; } m_idToUnit.Add(m_identity, unit); int radius = unit.DataController.Abilities.VisionRadius; m_voxelMap.Map.ForEachInRadius(coordinate, radius, (observedCell, pos) => { ObserveCell(unit.DataController.ControlledData.Owner, observedCell, pos, coordinate.Weight); }); m_minimap.Spawn(voxelData, coordinate); unchecked { m_identity++; } if (UnitCreated != null) { UnitCreated(unit); } }
private void OnReadyToPlayAll(Error error, Player[] players, Guid[] localPlayers, VoxelAbilitiesArray[] abilities, SerializedTaskArray[] taskTemplates, SerializedTaskTemplatesArray[] TaskTemplateData, AssignmentGroupArray[] assignmentGroups, Room room) { if (m_matchServer.HasError(error)) { Error(error); m_matchServer.Disconnect(); return; } //if(m_rttInfo.RTT < GameConstants.MatchEngineTick) //{ // //In case of low rtt we offset client timer by one tick to the past // m_prevTickTime += GameConstants.MatchEngineTick; //} long maxPing = GameConstants.PingTimeout; if (m_rttInfo.RTTMax > maxPing) { Debug.LogWarningFormat("Ping is too high {0} ms", m_rttInfo.RTTMax * GameConstants.MatchEngineTick); //set max ping to RTT_MAX? } m_nextTickTime = Time.realtimeSinceStartup + Math.Max(0, (maxPing - m_rttInfo.RTTMax) / 4) * GameConstants.MatchEngineTick; m_cmdQueue = new CommandsQueue(maxPing); m_isInitialized = true; m_taskEngines = new ITaskEngine[players.Length]; m_pathFinders = new IPathFinder[players.Length]; m_taskRunners = new ITaskRunner[players.Length]; for (int i = 0; i < players.Length; ++i) { Player player = players[i]; if (player.IsBot || localPlayers.Contains(players[i].Id)) { ITaskRunner taskRunner = MatchFactoryCli.CreateTaskRunner(); m_taskRunners[i] = taskRunner; IPathFinder pathFinder = MatchFactoryCli.CreatePathFinder(m_map.Map); m_pathFinders[i] = pathFinder; ITaskEngine taskEngine = MatchFactoryCli.CreateTaskEngine(m_game, taskRunner, pathFinder); taskEngine.ClientRequest += ProcessClientRequest; m_taskEngines[i] = taskEngine; } } if (Started != null) { Started(new Error(StatusCode.OK), players, localPlayers, abilities, taskTemplates, TaskTemplateData, assignmentGroups, room); } enabled = true; //update method will be called }
private void OnDestroy() { foreach (KeyValuePair <long, IMatchUnitControllerCli> kvp in m_idToUnit) { MatchFactoryCli.DestroyUnitController(kvp.Value); } if (m_selection != null) { m_selection.SelectionChanged -= OnUnitSelectionChanged; } if (m_targetSelection != null) { m_targetSelection.SelectionChanged -= OnTargetSelectionChanged; } }
private void Start() { LocalPlayerIndex = m_viewport.LocalPlayerIndex; int playerIndex = m_gameState.LocalToPlayerIndex(LocalPlayerIndex); m_cameraController = Dependencies.GameView.GetCameraController(LocalPlayerIndex); m_locationPicker = Dependencies.GameView.GetLocationPicker(LocalPlayerIndex); m_playersBot = MatchFactoryCli.CreateBotController(m_gameState.GetPlayer(playerIndex), m_engine.GetClientTaskEngine(playerIndex), new PlayerStrategy()); m_playersBot.Init(); SerializedTask[] taskTemplates = m_gameState.GetTaskTemplates(playerIndex); SerializedNamedTaskLaunchInfo[] taskTemplatesData = m_gameState.GetTaskTemplateData(playerIndex); for (int i = 0; i < taskTemplates.Length; ++i) { TaskTemplateType coreTaskType = taskTemplatesData[i].Type; m_playersBot.RegisterTask(coreTaskType, taskTemplates[i]); } }
private void OnEngineStopped(Error error) { m_progress.IsVisible = false; if (m_playerControllers != null) { for (int i = 0; i < m_playerControllers.Length; ++i) { IMatchPlayerControllerCli playerController = m_playerControllers[i]; if (playerController != null) { MatchFactoryCli.DestroyPlayerController(playerController); } } m_playerControllers = null; } m_matchEngineReconnectAttempts = GameConstants.ReconnectAttemptsCount; m_notification.ShowError("Engine stopped : " + error.ToString() + ". Trying to reconnect.."); TryReconnect(); }
private void OnEngineStarted(Error error, Player[] players, Guid[] localPlayers, VoxelAbilitiesArray[] voxelAbilities, SerializedTaskArray[] taskTemplates, SerializedTaskTemplatesArray[] taskTemplatesInfo, AssignmentGroupArray[] assignmentGroups, Room room) { m_room = room; IsReplay = room.Mode == GameMode.Replay; m_progress.IsVisible = false; if (m_engine.HasError(error)) { m_notification.ShowError(error); return; } m_voxelMap.Map.SetPlayerCount(players.Length); m_voxelMap.IsOn = true; m_players = players; m_voxelAbilities = voxelAbilities.Select(va => va.Abilities.ToDictionary(a => a.Type)).ToArray(); m_taskTemplates = taskTemplates.Select(t => t.Tasks).ToArray(); m_TaskTemplateData = taskTemplatesInfo.Select(t => t.Templates).ToArray(); if (IsReplay) { m_localPlayers = new[] { Guid.Empty }; m_isActionsMenuOpened = new bool[m_localPlayers.Length]; m_isMenuOpened = new bool[m_localPlayers.Length]; m_isContextActionInProgress = new bool[m_localPlayers.Length]; m_gameView.Initialize(1, true); } else { m_localPlayers = localPlayers; m_isActionsMenuOpened = new bool[m_localPlayers.Length]; m_isMenuOpened = new bool[m_localPlayers.Length]; m_isContextActionInProgress = new bool[m_localPlayers.Length]; m_gameView.Initialize(m_localPlayers.Length, true); } m_playerControllers = new IMatchPlayerControllerCli[m_players.Length]; for (int i = 0; i < m_playerControllers.Length; ++i) { m_playerControllers[i] = MatchFactoryCli.CreatePlayerController(transform, i, m_voxelAbilities); } for (int i = 0; i < m_playerControllers.Length; ++i) { m_playerControllers[i].ConnectWith(m_playerControllers); if (assignmentGroups[i].Groups != null) { m_playerControllers[i].AssignmentsController.SetGroups(assignmentGroups[i].Groups); } } m_playerStats = new PlayerStats[m_players.Length]; for (int i = 0; i < m_playerStats.Length; ++i) { m_playerStats[i] = new PlayerStats(true, m_playerControllers[i].UnitsCount); } IsStarted = true; if (Started != null) { Started(); } INavigation nav = Dependencies.Navigation; if (nav != null && nav.Args != null && nav.Args.ContainsKey("mapeditor")) { m_console.Write("mapeditor"); object args = nav.Args["mapeditor"]; if (args != null) { m_console.Write(args.ToString()); } } if (m_gameServer.IsConnectionStateChanging) { Debug.LogError("Game Server should be connected at this point"); } m_gameServer.ConnectionStateChanged += OnGameServerReconnected; }
private void ProcessRequest(ClientRequest request, Action <ClientRequest> callback) { Cmd cmd = request.Cmd; if (cmd != null) { if (cmd.Code == CmdCode.GrantBotCtrl) { m_bots = new IBotController[m_game.PlayersCount]; for (int i = 0; i < m_bots.Length; ++i) { Player player = m_game.GetPlayer(i); if (player.IsActiveBot) { IBotController bot = MatchFactoryCli.CreateBotController(player, m_taskEngines[i], new DefaultStrategy()); bot.Init(); m_bots[i] = bot; } } callback(request); } else if (cmd.Code == CmdCode.DenyBotCtrl) { m_bots = new IBotController[m_game.PlayersCount]; for (int i = 0; i < m_bots.Length; ++i) { IBotController bot = m_bots[i]; if (bot != null) { bot.Reset(); } } m_bots = null; callback(request); } else { IVoxelDataController dc = m_game.GetVoxelDataController(request.PlayerIndex, cmd.UnitIndex); if (cmd.Code != CmdCode.Move || dc == null) { if (dc == null) { request.Cmd.ErrorCode = CmdResultCode.Fail_NoUnit; } else { request.Cmd.ErrorCode = CmdResultCode.Fail_NotSupported; } SubmitResponse(request); } else { CoordinateCmd coordinateCmd = (CoordinateCmd)cmd; Debug.Assert(coordinateCmd.Coordinates.Length > 1); IPathFinder pathFinder = m_pathFinders[request.PlayerIndex]; #warning PathFinder should igore dataController.ControlledVoxelData pathFinder.Find(cmd.UnitIndex, -1, dc.Clone(), coordinateCmd.Coordinates, (unitIndex, path) => { coordinateCmd.Coordinates = path; request.Cmd = coordinateCmd; callback(request); }, null); } } } }
private void OnDestroy() { if (Dependencies.RemoteGameServer != null) { Dependencies.RemoteGameServer.ConnectionStateChanged -= OnRemoteGameServerConnectionStateChanged; } if (m_matchServer != null) { m_matchServer.Tick -= OnTick; m_matchServer.ReadyToPlayAll -= OnReadyToPlayAll; m_matchServer.Ping -= OnPing; m_matchServer.Paused -= OnPaused; m_matchServer.ConnectionStateChanged -= OnConnectionStateChanged; } if (m_taskEngines != null) { for (int i = 0; i < m_taskEngines.Length; ++i) { ITaskEngine taskEngine = m_taskEngines[i]; if (taskEngine != null) { taskEngine.ClientRequest -= ProcessClientRequest; MatchFactoryCli.DestroyTaskEngine(taskEngine); } } } if (m_taskRunners != null) { for (int i = 0; i < m_taskRunners.Length; ++i) { ITaskRunner taskRunner = m_taskRunners[i]; if (taskRunner != null) { MatchFactoryCli.DestroyTaskRunner(taskRunner); } } } if (m_pathFinders != null) { for (int i = 0; i < m_pathFinders.Length; ++i) { IPathFinder pathFinder = m_pathFinders[i]; if (pathFinder != null) { MatchFactoryCli.DestroyPathFinder(pathFinder); } } } if (m_bots != null) { for (int i = 0; i < m_bots.Length; ++i) { IBotController botController = m_bots[i]; if (botController != null) { MatchFactoryCli.DestroyBotController(botController); } } } }