public bool CanSpawnMoreBots(MyPlayer.PlayerId pid) { if (!Sync.IsServer) { Debug.Assert(false, "Server only"); return(false); } int perPlayerBotMultiplier = (MySession.Static.CreativeMode ? MySession.Static.MaxPlayers : 1); if (MySteam.UserId == pid.SteamId) { AgentSpawnData spawnData = default(AgentSpawnData); if (m_agentsToSpawn.TryGetValue(pid.SerialId, out spawnData)) { if (spawnData.CreatedByPlayer) { return(Bots.GetCreatedBotCount() < BotFactory.MaximumBotPerPlayer * perPlayerBotMultiplier); } else { return(Bots.GetGeneratedBotCount() < BotFactory.MaximumUncontrolledBotCount); } } else { Debug.Assert(false, "Bot doesn't exist"); return(false); } } else { int botCount = 0; var lookedPlayer = pid.SteamId; var players = Sync.Players.GetAllPlayers(); if (MySession.Static.CreativeMode) { foreach (var player in players) { if (player.SerialId != 0) { ++botCount; } } } else { foreach (var player in players) { if (player.SteamId == lookedPlayer && player.SerialId != 0) { botCount++; } } } return(botCount < BotFactory.MaximumBotPerPlayer * perPlayerBotMultiplier); } }
public void SpawnAgent(AgentSpawnData data) { GetAgentsObject(); GameObject agentObject = GameObject.Instantiate(settings.baseCharacterPrefab, data.position, Quaternion.identity); agentObject.transform.parent = agentsObject; Agent agent; switch (data.type) { case AgentType.player: agent = agentObject.AddComponent <PlayerAgent>(); break; default: agent = agentObject.AddComponent <Agent>(); break; } agent.Init(settings.baseCharacterController, settings.baseParticleController, data.team); agent.SetName(data.name); if (!string.IsNullOrEmpty(data.skin)) { if (skins.ContainsKey(data.skin)) { agent.SetSkin(skins[data.skin]); } } foreach (TechniqueSet set in settings.techniqueSettings.defaultSets) { TechniqueGenerator.Instance.AddTechniqueSet(agent, set); } }
public bool CanSpawnMoreBots(MyPlayer.PlayerId pid) { if (!Sync.IsServer) { return(false); } if (MyFakes.DEVELOPMENT_PRESET) { return(true); } if (Sync.MyId == pid.SteamId) { AgentSpawnData data = new AgentSpawnData(); return(this.m_agentsToSpawn.TryGetValue(pid.SerialId, out data) && (!data.CreatedByPlayer ? (this.Bots.GetGeneratedBotCount() < BotFactory.MaximumUncontrolledBotCount) : (this.Bots.GetCreatedBotCount() < BotFactory.MaximumBotPerPlayer))); } int num = 0; ulong steamId = pid.SteamId; foreach (MyPlayer player in Sync.Players.GetOnlinePlayers()) { if (player.Id.SteamId != steamId) { continue; } if (player.Id.SerialId != 0) { num++; } } return(num < BotFactory.MaximumBotPerPlayer); }
public bool CanSpawnMoreBots(MyPlayer.PlayerId pid) { if (!Sync.IsServer) { Debug.Assert(false, "Server only"); return(false); } if (MyFakes.ENABLE_BRAIN_SIMULATOR) { return(true); } if (MyFakes.DEVELOPMENT_PRESET) { return(true); } if (MySteam.UserId == pid.SteamId) { AgentSpawnData spawnData = default(AgentSpawnData); if (m_agentsToSpawn.TryGetValue(pid.SerialId, out spawnData)) { if (spawnData.CreatedByPlayer) { return(Bots.GetCreatedBotCount() < BotFactory.MaximumBotPerPlayer); } else { return(Bots.GetGeneratedBotCount() < BotFactory.MaximumUncontrolledBotCount); } } else { Debug.Assert(false, "Bot doesn't exist"); return(false); } } else { int botCount = 0; var lookedPlayer = pid.SteamId; var players = Sync.Players.GetOnlinePlayers(); foreach (var player in players) { if (player.Id.SteamId == lookedPlayer && player.Id.SerialId != 0) { botCount++; } } return(botCount < BotFactory.MaximumBotPerPlayer); } }
private int SpawnNewBotInternal(MyAgentDefinition agentDefinition, Vector3D?spawnPosition = null, bool createdByPlayer = false) { var currentHighestBotID = MyAIComponent.GenerateBotId(m_lastSpawnedBot); var newBotId = currentHighestBotID; EnsureIdentityUniqueness(newBotId); m_agentsToSpawn[newBotId] = new AgentSpawnData(agentDefinition, spawnPosition, createdByPlayer); m_lastSpawnedBot = newBotId; Sync.Players.RequestNewPlayer(newBotId, MyDefinitionManager.Static.GetRandomCharacterName(), agentDefinition.BotModel); return(newBotId); }
private int SpawnNewBotInternal(MyAgentDefinition agentDefinition, Vector3D?spawnPosition = null, bool createAlways = false) { var currentHighestBotID = MyAIComponent.GenerateBotId(m_lastSpawnedBot); var newBotId = currentHighestBotID; EnsureIdentityUniqueness(newBotId); m_agentsToSpawn[newBotId] = new AgentSpawnData(agentDefinition, spawnPosition, createAlways); m_lastSpawnedBot = newBotId; Sync.Players.RequestNewPlayer(newBotId, agentDefinition.DisplayNameText, agentDefinition.BotModel); return(newBotId); }
//----------------------------------------------------------------------------- public float3?Sample(AgentSpawnData spawnData, float3 _hit) { // First sample is choosen randomly AddSample(new float3(Random.value * m_DistributionRect.width, 0, Random.value * m_DistributionRect.height), spawnData.AgentDistCellSize); while (m_ActiveSamples.Length > 0) { // Pick a random active sample int i = (int)Random.value * m_ActiveSamples.Length; if (i >= m_ActiveSamples.Length) { continue; } float3 sample = m_ActiveSamples[i]; // Try random candidates between [radius, 2 * radius] from that sample. for (int index = 0; index < spawnData.AgentDistMaxTries; ++index) { float angle = 2 * Mathf.PI * Random.value; // See: http://stackoverflow.com/questions/9048095/create-random-number-within-an-annulus/9048443#9048443 float randomNumber = Mathf.Sqrt(Random.value * 3 * m_RadiusSquared + m_RadiusSquared); var candidate = sample + randomNumber * new float3(math.cos(angle), 0, math.sin(angle)); // Accept candidates if it's inside the rect and farther than 2 * radius to any existing sample. if (m_DistributionRect.Contains(new float2(candidate.x, candidate.z)) && IsFarEnough(candidate, spawnData.AgentDistCellSize)) { var agentPos = new float3(candidate.x + _hit.x, 0, candidate.z + _hit.z); var gridIndex = GridUtilties.WorldToIndex(m_AgentData.GridSettings[0], agentPos); if (gridIndex < 0)// || m_agentData.TileCost[gridIndex].Value > spawnData.AgentDistSpawningThreshold) { continue; } candidate.y = Main.TerrainHeight[gridIndex]; AddSample(candidate, spawnData.AgentDistCellSize); return(agentPos); } } // If we couldn't find a valid candidate after k attempts, remove this sample from the active samples queue m_ActiveSamples[i] = m_ActiveSamples[m_ActiveSamples.Length - 1]; m_ActiveSamples.RemoveAtSwapBack(m_ActiveSamples.Length - 1); } return(null); }
private int SpawnNewBotInternal(MyAgentDefinition agentDefinition, Vector3D?spawnPosition = null, bool createdByPlayer = false) { int newBotId = 0; foreach (var player in Sync.Players.GetOnlinePlayers()) { if (player.Id.SteamId == Sync.MyId && player.Id.SerialId > newBotId) { newBotId = player.Id.SerialId; } } newBotId++; m_agentsToSpawn[newBotId] = new AgentSpawnData(agentDefinition, spawnPosition, createdByPlayer); Sync.Players.RequestNewPlayer(newBotId, MyDefinitionManager.Static.GetRandomCharacterName(), agentDefinition.BotModel); return(newBotId); }
//----------------------------------------------------------------------------- private void CreateAgent(AgentSpawnData spawnData, float3 _pos) { Archetypes.CreateAgent(PostUpdateCommands, _pos, spawnData.AgentMesh, spawnData.AgentMaterial, m_AgentData.GridSettings[0]); }
private int SpawnNewBotInternal(MyAgentDefinition agentDefinition, Vector3D? spawnPosition = null, bool createdByPlayer = false) { int newBotId = 0; foreach (var player in Sync.Players.GetOnlinePlayers()) { if (player.Id.SteamId == Sync.MyId && player.Id.SerialId > newBotId) { newBotId = player.Id.SerialId; } } newBotId++; m_agentsToSpawn[newBotId] = new AgentSpawnData(agentDefinition, spawnPosition, createdByPlayer); Sync.Players.RequestNewPlayer(newBotId, MyDefinitionManager.Static.GetRandomCharacterName(), agentDefinition.BotModel); return newBotId; }
private void CreateBot(int playerNumber, MyObjectBuilder_Bot botBuilder) { Debug.Assert(BotFactory != null, "Bot factory is not set! Cannot create a new bot!"); if (BotFactory == null) return; var newPlayer = Sync.Clients.LocalClient.GetPlayer(playerNumber); if (newPlayer == null) return; var isBotSpawned = m_agentsToSpawn.ContainsKey(playerNumber); var isLoading = botBuilder != null; var createdByPlayer = false; MyBotDefinition botDefinition = null; AgentSpawnData spawnData = default(AgentSpawnData); // We have to get the bot object builder and bot definition somehow // Either, the bot is being spawned on this computer and the definition was saved in the spawn data // or the bot is just being created from the object builder (MP bot creation, etc.), so the definition is there if (isBotSpawned) { spawnData = m_agentsToSpawn[playerNumber]; createdByPlayer = spawnData.CreatedByPlayer; botDefinition = spawnData.AgentDefinition; m_agentsToSpawn.Remove(playerNumber); } else { if (botBuilder == null || botBuilder.BotDefId.TypeId.IsNull) { MyPlayer missingBotPlayer = null; if (Sync.Players.TryGetPlayerById(new MyPlayer.PlayerId(Sync.MyId, playerNumber), out missingBotPlayer)) { Sync.Players.RemovePlayer(missingBotPlayer); } return; } MyDefinitionManager.Static.TryGetBotDefinition(botBuilder.BotDefId, out botDefinition); Debug.Assert(botDefinition != null, "Bot definition could not be found."); if (botDefinition == null) return; } if ((newPlayer.Character == null || !newPlayer.Character.IsDead) && BotFactory.CanCreateBotOfType(botDefinition.BehaviorType, isLoading) || createdByPlayer) { IMyBot bot = null; if (isBotSpawned) bot = BotFactory.CreateBot(newPlayer, botBuilder, spawnData.AgentDefinition); else bot = BotFactory.CreateBot(newPlayer, botBuilder, botDefinition); if (bot == null) { MyLog.Default.WriteLine("Could not create a bot for player " + newPlayer + "!"); } else { m_botCollection.AddBot(playerNumber, bot); if (isBotSpawned && bot is IMyEntityBot) (bot as IMyEntityBot).Spawn(spawnData.SpawnPosition, createdByPlayer); if (BotCreatedEvent != null) { BotCreatedEvent(playerNumber, bot.BotDefinition); } } } else { // hack for removing uncontrolled bot players or saved dead characters var player = Sync.Players.GetPlayerById(new MyPlayer.PlayerId(Sync.MyId, playerNumber)); Sync.Players.RemovePlayer(player); } }
private void CreateBot(int playerNumber, MyObjectBuilder_Bot botBuilder) { Debug.Assert(BotFactory != null, "Bot factory is not set! Cannot create a new bot!"); if (BotFactory == null) { return; } var newPlayer = Sync.Clients.LocalClient.GetPlayer(playerNumber); if (newPlayer == null) { return; } var isBotSpawned = m_agentsToSpawn.ContainsKey(playerNumber); var isLoading = botBuilder != null; var createdByPlayer = false; MyBotDefinition botDefinition = null; AgentSpawnData spawnData = default(AgentSpawnData); if (isBotSpawned) { spawnData = m_agentsToSpawn[playerNumber]; createdByPlayer = spawnData.CreatedByPlayer; botDefinition = spawnData.AgentDefinition; m_agentsToSpawn.Remove(playerNumber); } else { Debug.Assert(botBuilder != null && !botBuilder.BotDefId.TypeId.IsNull, "Null or invalid bot builder. Bot is not going to be created"); if (botBuilder == null || botBuilder.BotDefId.TypeId.IsNull) { return; } botDefinition = MyDefinitionManager.Static.GetBotDefinition(botBuilder.BotDefId); Debug.Assert(botDefinition != null, "Bot definition could not be found."); if (botDefinition == null) { return; } } if ((newPlayer.Character == null || !newPlayer.Character.IsDead) && BotFactory.CanCreateBotOfType(botDefinition.BehaviorType, isLoading) || createdByPlayer) { IMyBot bot = null; if (isBotSpawned) { bot = BotFactory.CreateBot(newPlayer, botBuilder, spawnData.AgentDefinition); } else { bot = BotFactory.CreateBot(newPlayer, botBuilder, botDefinition); } if (bot == null) { MyLog.Default.WriteLine("Could not create a bot for player " + newPlayer + "!"); } else { m_botCollection.AddBot(playerNumber, bot); if (isBotSpawned && bot is IMyEntityBot) { (bot as IMyEntityBot).Spawn(spawnData.SpawnPosition, createdByPlayer); } } } else { // hack for removing uncontrolled bot players or saved dead characters var player = Sync.Players.TryGetPlayerById(new MyPlayer.PlayerId(MySteam.UserId, playerNumber)); Sync.Players.RemovePlayer(player); } }
private void CreateBot(int playerNumber, MyObjectBuilder_Bot botBuilder) { if (BotFactory != null) { MyPlayer player = Sync.Clients.LocalClient.GetPlayer(playerNumber); if (player != null) { int num1; bool flag = this.m_agentsToSpawn.ContainsKey(playerNumber); bool load = botBuilder != null; bool spawnedByPlayer = false; MyBotDefinition botDefinition = null; AgentSpawnData data = new AgentSpawnData(); if (flag) { data = this.m_agentsToSpawn[playerNumber]; spawnedByPlayer = data.CreatedByPlayer; botDefinition = data.AgentDefinition; this.m_agentsToSpawn.Remove(playerNumber); } else { if ((botBuilder == null) || botBuilder.BotDefId.TypeId.IsNull) { MyPlayer player2 = null; if (Sync.Players.TryGetPlayerById(new MyPlayer.PlayerId(Sync.MyId, playerNumber), out player2)) { Sync.Players.RemovePlayer(player2, true); } return; } MyDefinitionManager.Static.TryGetBotDefinition(botBuilder.BotDefId, out botDefinition); if (botDefinition == null) { return; } } if ((player.Character == null) || !player.Character.IsDead) { num1 = (int)BotFactory.CanCreateBotOfType(botDefinition.BehaviorType, load); } else { num1 = 0; } if ((num1 | spawnedByPlayer) == 0) { MyPlayer playerById = Sync.Players.GetPlayerById(new MyPlayer.PlayerId(Sync.MyId, playerNumber)); Sync.Players.RemovePlayer(playerById, true); } else { IMyBot newBot = null; newBot = !flag?BotFactory.CreateBot(player, botBuilder, botDefinition) : BotFactory.CreateBot(player, botBuilder, data.AgentDefinition); if (newBot == null) { MyLog.Default.WriteLine("Could not create a bot for player " + player + "!"); } else { this.m_botCollection.AddBot(playerNumber, newBot); if (flag && (newBot is IMyEntityBot)) { (newBot as IMyEntityBot).Spawn(data.SpawnPosition, spawnedByPlayer); } if (this.BotCreatedEvent != null) { this.BotCreatedEvent(playerNumber, newBot.BotDefinition); } } } } } }
private int SpawnNewBotInternal(MyAgentDefinition agentDefinition, Vector3D? spawnPosition = null, bool createdByPlayer = false) { var currentHighestBotID = MyAIComponent.GenerateBotId(m_lastSpawnedBot); var newBotId = currentHighestBotID; EnsureIdentityUniqueness(newBotId); m_agentsToSpawn[newBotId] = new AgentSpawnData(agentDefinition, spawnPosition, createdByPlayer); m_lastSpawnedBot = newBotId; Sync.Players.RequestNewPlayer(newBotId, MyDefinitionManager.Static.GetRandomCharacterName(), agentDefinition.BotModel); return newBotId; }
private int SpawnNewBotInternal(MyAgentDefinition agentDefinition, Vector3D? spawnPosition = null, bool createAlways = false) { var currentHighestBotID = MyAIComponent.GenerateBotId(m_lastSpawnedBot); var newBotId = currentHighestBotID; EnsureIdentityUniqueness(newBotId); m_agentsToSpawn[newBotId] = new AgentSpawnData(agentDefinition, spawnPosition, createAlways); m_lastSpawnedBot = newBotId; Sync.Players.RequestNewPlayer(newBotId, agentDefinition.DisplayNameText, agentDefinition.BotModel); return newBotId; }