public IMyBot CreateBot(MyPlayer player, MyObjectBuilder_Bot botBuilder, MyBotDefinition botDefinition) { MyObjectBuilderType obType = MyObjectBuilderType.Invalid; if (botBuilder == null) { obType = botDefinition.Id.TypeId; botBuilder = m_objectFactory.CreateObjectBuilder <MyObjectBuilder_Bot>(m_objectFactory.GetProducedType(obType)); } else { obType = botBuilder.TypeId; Debug.Assert(botDefinition.Id == botBuilder.BotDefId, "Bot builder type does not match bot definition type!"); } Debug.Assert(m_botDataByBehaviorType.ContainsKey(botDefinition.BehaviorType), "Undefined behavior type. Bot is not going to be created"); if (!m_botDataByBehaviorType.ContainsKey(botDefinition.BehaviorType)) { return(null); } var botData = m_botDataByBehaviorType[botDefinition.BehaviorType]; IMyBot output = CreateBot(m_objectFactory.GetProducedType(obType), player, botDefinition); CreateActions(output, botData.BotActionsType); CreateLogic(output, botData.LogicType, botDefinition.BehaviorSubtype); output.Init(botBuilder); return(output); }
public IMyBot CreateBot(MyPlayer player, MyObjectBuilder_Bot botBuilder, MyBotDefinition botDefinition) { MyObjectBuilderType invalid = MyObjectBuilderType.Invalid; if (botBuilder != null) { invalid = botBuilder.TypeId; } else { invalid = botDefinition.Id.TypeId; botBuilder = m_objectFactory.CreateObjectBuilder <MyObjectBuilder_Bot>(m_objectFactory.GetProducedType(invalid)); } if (!this.m_botDataByBehaviorType.ContainsKey(botDefinition.BehaviorType)) { return(null); } BehaviorData data = this.m_botDataByBehaviorType[botDefinition.BehaviorType]; IMyBot bot = this.CreateBot(m_objectFactory.GetProducedType(invalid), player, botDefinition); this.CreateActions(bot, data.BotActionsType); this.CreateLogic(bot, data.LogicType, botDefinition.BehaviorSubtype); bot.Init(botBuilder); return(bot); }
public void SpawnAtShip(MyPlayer player, string respawnShipId, MyBotDefinition botDefinition) { Debug.Assert(Sync.IsServer, "Spawning can only be called on the server!"); if (!Sync.IsServer) { return; } ResetRespawnCooldown(player.Id); if (Sync.MultiplayerActive) { SyncCooldownToPlayer(player.Id.SteamId, player.Id.SteamId == Sync.MyId); } List <MyCubeGrid> respawnGrids = new List <MyCubeGrid>(); var respawnShipDef = MyDefinitionManager.Static.GetRespawnShipDefinition(respawnShipId); MyPrefabDefinition prefabDef = respawnShipDef.Prefab; // Deploy ship Vector3D position = Vector3D.Zero; float planetSpawnHeightRatio = 0.3f; float spawnRangeMin = 500f; float spawnRangeMax = 650f; if (prefabDef.CubeGrids != null && prefabDef.CubeGrids.Length > 0) { MyObjectBuilder_CubeGrid firstGrid = prefabDef.CubeGrids[0]; if (firstGrid.UsePositionForSpawn) { position = new Vector3D( firstGrid.PositionAndOrientation.Value.Position.x, firstGrid.PositionAndOrientation.Value.Position.y, firstGrid.PositionAndOrientation.Value.Position.z); } planetSpawnHeightRatio = MyMath.Clamp(firstGrid.PlanetSpawnHeightRatio, 0.05f, 0.95f); // Clamped to prevent crazy data spawnRangeMin = firstGrid.SpawnRangeMin; spawnRangeMax = firstGrid.SpawnRangeMax; } Vector3D forward = Vector3.Forward; Vector3D up = Vector3D.Up; GetSpawnPosition(prefabDef.BoundingSphere.Radius, ref position, out forward, out up, planetSpawnHeightRatio, spawnRangeMin, spawnRangeMax); Stack <Action> callback = new Stack <Action>(); callback.Push(delegate() { PutPlayerInRespawnGrid(player, respawnGrids, botDefinition); }); MyPrefabManager.Static.SpawnPrefab( respawnGrids, prefabDef.Id.SubtypeName, position, forward, up, spawningOptions: VRage.Game.ModAPI.SpawningOptions.RotateFirstCockpitTowardsDirection, updateSync: true, callbacks: callback); }
public IMyBot CreateBot(MyPlayer player, MyObjectBuilder_Bot botBuilder, MyBotDefinition botDefinition) { var output = new MySandboxBot(player, botDefinition); if (botBuilder != null) { output.Init(botBuilder); } return(output); }
public MyAgentBot(MyPlayer player, MyBotDefinition botDefinition) { m_player = player; m_navigation = new MyBotNavigation(); m_actionCollection = null; m_botMemory = new MyBotMemory(this); m_botDefinition = botDefinition as MyAgentDefinition; m_player.Controller.ControlledEntityChanged += Controller_ControlledEntityChanged; m_navigation.ChangeEntity(m_player.Controller.ControlledEntity); }
private void SpawnInRespawn(MyPlayer player, MyRespawnComponent respawn, MyBotDefinition botDefinition) { if (MySession.Static.Settings.EnableOxygen) { player.Identity.ChangeToOxygenSafeSuit(); } if (respawn.Entity == null) { Debug.Assert(false, "Respawn does not have entity!"); SpawnInSuit(player, null, botDefinition); return; } var parent = respawn.Entity.GetTopMostParent(); if (parent.Physics == null) { Debug.Assert(false, "Respawn entity parent does not have physics!"); SpawnInSuit(player, (MyEntity)parent, botDefinition); return; } MatrixD pos; var medRoom = respawn.Entity as MyMedicalRoom; if (medRoom != null) { pos = medRoom.GetSpawnPosition(); } else { pos = respawn.GetSpawnPosition(respawn.Entity.WorldMatrix); } Vector3 velocity = parent.Physics.GetVelocityAtPoint(pos.Translation); MyMultiplayer.ReplicateImmediatelly(MyExternalReplicable.FindByObject(parent), new EndpointId(player.Id.SteamId)); player.SpawnAt(pos, velocity, (MyEntity)parent, botDefinition, true); if (medRoom != null) { medRoom.TryTakeSpawneeOwnership(player); medRoom.TrySetFaction(player); if (medRoom.ForceSuitChangeOnRespawn) { player.Character.ChangeModelAndColor(medRoom.RespawnSuitName, player.Character.ColorMask); } } }
private void OnBotCreatedEvent(int botSerialNum, MyBotDefinition botDefinition) { var agentDefinition = botDefinition as MyAgentDefinition; if (agentDefinition != null && agentDefinition.FactionTag == "SPID") { MyPlayer player = null; if (Sync.Players.TryGetPlayerById(new MyPlayer.PlayerId(Sync.MyId, botSerialNum), out player)) { player.Controller.ControlledEntityChanged += OnBotControlledEntityChanged; } } }
public MySandboxBot(MyPlayer botPlayer, MyBotDefinition botDefinition) { m_definition = botDefinition as MyAgentDefinition; m_player = botPlayer; m_navigation = new MyBotNavigation(); m_respawnRequestSent = false; m_actionCollection = null; m_botMemory = new MyBotMemory(this); m_player.Controller.ControlledEntityChanged += Controller_ControlledEntityChanged; m_navigation.ChangeEntity(m_player.Controller.ControlledEntity); }
public MyAgentBot(MyPlayer player, MyBotDefinition botDefinition) { this.m_player = player; this.m_navigation = new MyBotNavigation(); this.m_actionCollection = null; this.m_botMemory = new MyBotMemory(this); this.m_botDefinition = botDefinition as MyAgentDefinition; this.m_removeAfterDeath = this.m_botDefinition.RemoveAfterDeath; this.m_respawnRequestSent = false; this.m_botRemoved = false; this.m_player.Controller.ControlledEntityChanged += new Action <IMyControllableEntity, IMyControllableEntity>(this.Controller_ControlledEntityChanged); this.m_navigation.ChangeEntity(this.m_player.Controller.ControlledEntity); MyCestmirDebugInputComponent.PlacedAction += new Action <Vector3D, MyEntity>(this.DebugGoto); }
private void SpawnInSuit(MyPlayer player, MyEntity spawnedBy, MyBotDefinition botDefinition) { Vector3 direction; Vector3D position; GetSpawnPosition(10, out direction, out position); //Create character Matrix matrix = Matrix.CreateWorld(position, direction, Vector3.Up); MyCharacter character = MyCharacter.CreateCharacter(matrix, Vector3.Zero, player.Identity.DisplayName, player.Identity.Model, null, botDefinition, playerSteamId: player.Id.SteamId); Sync.Players.SetPlayerCharacter(player, character, spawnedBy); Sync.Players.RevivePlayer(player); }
private void SpawnInSuit(MyPlayer player, MyEntity spawnedBy, MyBotDefinition botDefinition) { Vector3D position = Vector3D.Zero; Vector3D forward = Vector3D.Forward; Vector3D up = Vector3D.Up; GetSpawnPosition(10, ref position, out forward, out up); //Create character Matrix matrix = Matrix.CreateWorld(position, forward, up); MyCharacter character = MyCharacter.CreateCharacter(matrix, Vector3.Zero, player.Identity.DisplayName, player.Identity.Model, null, botDefinition, playerSteamId: player.Id.SteamId); Sync.Players.SetPlayerCharacter(player, character, spawnedBy); Sync.Players.RevivePlayer(player); }
public MyHumanoidBot(MyPlayer player, MyBotDefinition botDefinition) : base(player, botDefinition) { Debug.Assert(botDefinition is MyHumanoidBotDefinition, "Provided bot definition is not of humanoid type"); if (m_player.Controller.ControlledEntity is MyCharacter) // when loaded player already controls entity { var character = m_player.Controller.ControlledEntity as MyCharacter; if (character.CurrentWeapon == null && StartingWeaponId.SubtypeId != MyStringHash.NullOrEmpty) { AddItems(character); } } Sandbox.Game.Gui.MyCestmirDebugInputComponent.PlacedAction += DebugGoto; }
public MyAgentBot(MyPlayer player, MyBotDefinition botDefinition) { m_player = player; m_navigation = new MyBotNavigation(); m_actionCollection = null; m_botMemory = new MyBotMemory(this); m_botDefinition = botDefinition as MyAgentDefinition; m_removeAfterDeath = m_botDefinition.RemoveAfterDeath; m_respawnRequestSent = false; m_botRemoved = false; m_player.Controller.ControlledEntityChanged += Controller_ControlledEntityChanged; m_navigation.ChangeEntity(m_player.Controller.ControlledEntity); Sandbox.Game.Gui.MyCestmirDebugInputComponent.PlacedAction += DebugGoto; }
public IMyBot CreateBot(MyPlayer player, MyObjectBuilder_Bot botBuilder, MyBotDefinition botDefinition) { Debug.Assert(m_botDataByBehaviorType.ContainsKey(botDefinition.BehaviorType), "Undefined behavior type. Bot is not going to be created"); if (!m_botDataByBehaviorType.ContainsKey(botDefinition.BehaviorType)) { return(null); } Debug.Assert(m_botTypeByDefinitionType.ContainsKey(botDefinition.TypeDefinitionId.TypeId), "Type not found. Bot is not going to be created!"); if (!m_botTypeByDefinitionType.ContainsKey(botDefinition.TypeDefinitionId.TypeId)) { return(null); } var botData = m_botDataByBehaviorType[botDefinition.BehaviorType]; var behaviorTypeData = m_botTypeByDefinitionType[botDefinition.TypeDefinitionId.TypeId]; IMyBot output = CreateBot(behaviorTypeData.BotType, player, botDefinition); CreateActions(output, botData.BotActionsType); CreateLogic(output, botData.LogicType, botDefinition.BehaviorSubtype); if (botBuilder != null) { output.Init(botBuilder); } return(output); }
private void PutPlayerInRespawnGrid(MyPlayer player, List <MyCubeGrid> respawnGrids, MyBotDefinition botDefinition) { MyCharacter character = null; MyCockpit cockpit = null; // Find cockpits List <MyCockpit> shipCockpits = new List <MyCockpit>(); foreach (MyCubeGrid grid in respawnGrids) { foreach (MyCockpit gridCockpit in grid.GetFatBlocks <MyCockpit>()) { // Ignore non-functional cockpits if (!gridCockpit.IsFunctional) { continue; } shipCockpits.Add(gridCockpit); } } // First sort cockpits by order: Ship controlling cockpits set to main, then ship controlling cockpits not set to main, lastly whatever remains, e.g. CryoChambers and Passenger Seats if (shipCockpits.Count > 1) { shipCockpits.Sort(delegate(MyCockpit cockpitA, MyCockpit cockpitB) { int controlCompare = cockpitB.EnableShipControl.CompareTo(cockpitA.EnableShipControl); if (controlCompare != 0) { return(controlCompare); } int mainCompare = cockpitB.IsMainCockpit.CompareTo(cockpitA.IsMainCockpit); if (mainCompare != 0) { return(mainCompare); } return(0); }); } // Finally, select the most important cockpit if (shipCockpits.Count > 0) { cockpit = shipCockpits[0]; } System.Diagnostics.Debug.Assert(cockpit != null, "character is spawning in ship without cockpit !"); // Create character MatrixD matrix = MatrixD.Identity; if (cockpit != null) { matrix = cockpit.WorldMatrix; matrix.Translation = cockpit.WorldMatrix.Translation - Vector3.Up - Vector3.Forward; } else if (respawnGrids.Count > 0) { matrix.Translation = respawnGrids[0].PositionComp.WorldAABB.Center + respawnGrids[0].PositionComp.WorldAABB.HalfExtents; } character = MyCharacter.CreateCharacter(matrix, Vector3.Zero, player.Identity.DisplayName, player.Identity.Model, null, botDefinition, cockpit: cockpit, playerSteamId: player.Id.SteamId); CloseRespawnShip(player); foreach (var respawnGrid in respawnGrids) { respawnGrid.ChangeGridOwnership(player.Identity.IdentityId, MyOwnershipShareModeEnum.None); respawnGrid.IsRespawnGrid = true; respawnGrid.m_playedTime = 0; player.RespawnShip.Add(respawnGrid.EntityId); } //SaveRespawnShip(player); if (cockpit != null) { cockpit.AttachPilot(character, false); MyMultiplayer.ReplicateImmediatelly(MyExternalReplicable.FindByObject(cockpit.CubeGrid), new EndpointId(player.Id.SteamId)); } if (cockpit == null) { Sync.Players.SetPlayerCharacter(player, character, null); } else { character.SetPlayer(player); Sync.Players.SetPlayerToCockpit(player, cockpit); } Sync.Players.RevivePlayer(player); }
public void SpawnAsNewPlayer(MyPlayer player, Vector3D currentPosition, string respawnShipId, bool resetIdentity, MyBotDefinition botDefinition) { Debug.Assert(Sync.IsServer, "Calling SpawnAsNewPlayer on client!"); Debug.Assert(player.Identity != null, "Spawning with empty identity!"); if (!Sync.IsServer || player.Identity == null) { return; } if (resetIdentity) { ResetPlayerIdentity(player); } if (respawnShipId != null) { SpawnAtShip(player, respawnShipId, botDefinition); } else { SpawnInSuit(player, null, botDefinition); } if (MySession.Static.Settings.EnableOxygen && player.Character.OxygenComponent != null && player.Character.OxygenComponent.NeedsOxygenFromSuit) { player.Character.OxygenComponent.SwitchHelmet(); } }
public void SpawnAtShip(MyPlayer player, string respawnShipId, MyBotDefinition botDefinition) { Debug.Assert(Sync.IsServer, "Spawning can only be called on the server!"); if (!Sync.IsServer) { return; } ResetRespawnCooldown(player.Id); if (Sync.MultiplayerActive) { SyncCooldownToPlayer(player.Id.SteamId, player.Id.SteamId == Sync.MyId); } MyCharacter character = null; MyCockpit cockpit = null; List <MyCubeGrid> respawnGrids = new List <MyCubeGrid>(); var respawnShipDef = MyDefinitionManager.Static.GetRespawnShipDefinition(respawnShipId); MyPrefabDefinition prefabDef = respawnShipDef.Prefab; // Deploy ship Vector3D position = Vector3D.Zero; float planetSpawnHeightRatio = 0.3f; float spawnRangeMin = 500f; float spawnRangeMax = 650f; if (prefabDef.CubeGrids != null && prefabDef.CubeGrids.Length > 0) { MyObjectBuilder_CubeGrid firstGrid = prefabDef.CubeGrids[0]; if (firstGrid.UsePositionForSpawn) { position = new Vector3D( firstGrid.PositionAndOrientation.Value.Position.x, firstGrid.PositionAndOrientation.Value.Position.y, firstGrid.PositionAndOrientation.Value.Position.z); } planetSpawnHeightRatio = MyMath.Clamp(firstGrid.PlanetSpawnHeightRatio, 0.05f, 0.95f); // Clamped to prevent crazy data spawnRangeMin = firstGrid.SpawnRangeMin; spawnRangeMax = firstGrid.SpawnRangeMax; } Vector3D forward = Vector3.Forward; Vector3D up = Vector3D.Up; GetSpawnPosition(prefabDef.BoundingSphere.Radius, ref position, out forward, out up, planetSpawnHeightRatio, spawnRangeMin, spawnRangeMax); MyPrefabManager.Static.SpawnPrefab( respawnGrids, prefabDef.Id.SubtypeName, position, forward, up, spawningOptions: VRage.Game.ModAPI.SpawningOptions.RotateFirstCockpitTowardsDirection, updateSync: true); // Find cockpits List <MyCockpit> shipCockpits = new List <MyCockpit>(); foreach (MyCubeGrid grid in respawnGrids) { foreach (MyCockpit gridCockpit in grid.GetFatBlocks <MyCockpit>()) { // Ignore non-functional cockpits if (!gridCockpit.IsFunctional) { continue; } shipCockpits.Add(gridCockpit); } } // First sort cockpits by order: Ship controlling cockpits set to main, then ship controlling cockpits not set to main, lastly whatever remains, e.g. CryoChambers and Passenger Seats if (shipCockpits.Count > 1) { shipCockpits.Sort(delegate(MyCockpit cockpitA, MyCockpit cockpitB) { int controlCompare = cockpitB.EnableShipControl.CompareTo(cockpitA.EnableShipControl); if (controlCompare != 0) { return(controlCompare); } int mainCompare = cockpitB.IsMainCockpit.CompareTo(cockpitA.IsMainCockpit); if (mainCompare != 0) { return(mainCompare); } return(0); }); } // Finally, select the most important cockpit if (shipCockpits.Count > 0) { cockpit = shipCockpits[0]; } System.Diagnostics.Debug.Assert(cockpit != null, "character is spawning in ship without cockpit !"); // Create character MatrixD matrix = MatrixD.Identity; if (cockpit != null) { matrix = cockpit.WorldMatrix; matrix.Translation = cockpit.WorldMatrix.Translation - Vector3.Up - Vector3.Forward; } else if (respawnGrids.Count > 0) { matrix.Translation = respawnGrids[0].PositionComp.WorldAABB.Center + respawnGrids[0].PositionComp.WorldAABB.HalfExtents; } character = MyCharacter.CreateCharacter(matrix, Vector3.Zero, player.Identity.DisplayName, player.Identity.Model, null, botDefinition, cockpit: cockpit, playerSteamId: player.Id.SteamId); CloseRespawnShip(player); foreach (var respawnGrid in respawnGrids) { respawnGrid.ChangeGridOwnership(player.Identity.IdentityId, MyOwnershipShareModeEnum.None); respawnGrid.IsRespawnGrid = true; player.RespawnShip.Add(respawnGrid.EntityId); } if (cockpit != null) { cockpit.AttachPilot(character, false); MyMultiplayer.ReplicateImmediatelly(MyExternalReplicable.FindByObject(cockpit.CubeGrid), new EndpointId(player.Id.SteamId)); } if (cockpit == null) { Sync.Players.SetPlayerCharacter(player, character, null); } else { character.SetPlayer(player); Sync.Players.SetPlayerToCockpit(player, cockpit); } Sync.Players.RevivePlayer(player); }
private IMyBot CreateBot(Type botType, MyPlayer player, MyBotDefinition botDefinition) { return(Activator.CreateInstance(botType, player, botDefinition) as IMyBot); // MW:TODO so far agent and humanoid have players so let's keep it like this }
public override bool HandleRespawnRequest(bool joinGame, bool newIdentity, long medicalRoomId, string respawnShipId, MyPlayer.PlayerId playerId, Vector3D?spawnPosition, VRage.ObjectBuilders.SerializableDefinitionId?botDefinitionId) { MyPlayer player = Sync.Players.GetPlayerById(playerId); bool spawnAsNewPlayer = newIdentity || player == null; Debug.Assert(player == null || player.Identity != null, "Respawning player has no identity!"); if (!MySessionComponentMissionTriggers.CanRespawn(playerId)) { return(false); } Vector3D currentPosition = Vector3D.Zero; if (player != null && player.Character != null) { currentPosition = player.Character.PositionComp.GetPosition(); } if (TryFindCryoChamberCharacter(player)) { //Player found in chamber; return(true); } MyBotDefinition botDefinition = null; if (botDefinitionId != null) { MyDefinitionManager.Static.TryGetBotDefinition((MyDefinitionId)botDefinitionId, out botDefinition); } if (!spawnAsNewPlayer) { if (respawnShipId != null) { SpawnAtShip(player, respawnShipId, botDefinition); return(true); } if (spawnPosition.HasValue) { Vector3D gravity = MyGravityProviderSystem.CalculateTotalGravityInPoint(spawnPosition.Value); if (Vector3D.IsZero(gravity)) { gravity = Vector3D.Down; } else { gravity.Normalize(); } Vector3D perpendicular; gravity.CalculatePerpendicularVector(out perpendicular); player.SpawnAt(MatrixD.CreateWorld(spawnPosition.Value, perpendicular, -gravity), Vector3.Zero, null, botDefinition, true); return(true); } // Find respawn block to spawn at MyRespawnComponent foundRespawn = null; if (medicalRoomId == 0 || !MyFakes.SHOW_FACTIONS_GUI) { List <MyRespawnComponent> respawns = null; var nearestRespawn = GetNearestRespawn(currentPosition, out respawns, MySession.Static.CreativeMode ? (long?)null : player.Identity.IdentityId); if (joinGame && respawns.Count > 0) { foundRespawn = respawns[MyRandom.Instance.Next(0, respawns.Count)]; } } else { foundRespawn = FindRespawnById(medicalRoomId, player); if (foundRespawn == null) { return(false); } } // If spawning in respawn block fails, we will spawn as a new player if (foundRespawn != null) { SpawnInRespawn(player, foundRespawn, botDefinition); } else { spawnAsNewPlayer = true; } } if (spawnAsNewPlayer) { bool resetIdentity = false; if (MySession.Static.Settings.PermanentDeath.Value) { var oldIdentity = Sync.Players.TryGetPlayerIdentity(playerId); resetIdentity = oldIdentity.FirstSpawnDone; } if (player == null) { var identity = Sync.Players.CreateNewIdentity(player.DisplayName); player = Sync.Players.CreateNewPlayer(identity, playerId, player.DisplayName); resetIdentity = false; } if (MySession.Static.CreativeMode) { Vector3D?correctedPos = MyEntities.FindFreePlace(currentPosition, 2, 200); if (correctedPos.HasValue) { currentPosition = correctedPos.Value; } player.SpawnAt(Matrix.CreateTranslation(currentPosition), Vector3.Zero, null, botDefinition); } else { SpawnAsNewPlayer(player, currentPosition, respawnShipId, resetIdentity, botDefinition); } } return(true); }
public void SpawnAtShip(MyPlayer player, string respawnShipId, MyBotDefinition botDefinition) { Debug.Assert(Sync.IsServer, "Spawning can only be called on the server!"); if (!Sync.IsServer) { return; } ResetRespawnCooldown(player.Id); if (Sync.MultiplayerActive) { SyncCooldownToPlayer(player.Id.SteamId); } MyCharacter character = null; MyCockpit cockpit = null; List <MyCubeGrid> respawnGrids = new List <MyCubeGrid>(); var respawnShipDef = MyDefinitionManager.Static.GetRespawnShipDefinition(respawnShipId); MyPrefabDefinition prefabDef = respawnShipDef.Prefab; // Deploy ship Vector3 direction; Vector3D position; GetSpawnPosition(prefabDef.BoundingSphere.Radius, out direction, out position); MyPrefabManager.Static.SpawnPrefab( respawnGrids, prefabDef.Id.SubtypeName, position, -direction, Vector3.CalculatePerpendicularVector(-direction), spawningOptions: Sandbox.ModAPI.SpawningOptions.RotateFirstCockpitTowardsDirection, updateSync: true); // Find cockpit foreach (var grid in respawnGrids) { foreach (var block in grid.GetBlocks()) { if (block.FatBlock is MyCockpit) { cockpit = (MyCockpit)block.FatBlock; break; } } if (cockpit != null) { break; } } System.Diagnostics.Debug.Assert(cockpit != null, "character is spawning in ship without cockpit !"); // Create character MatrixD matrix = MatrixD.Identity; if (cockpit != null) { matrix = cockpit.WorldMatrix; matrix.Translation = cockpit.WorldMatrix.Translation - Vector3.Up - Vector3.Forward; } else if (respawnGrids.Count > 0) { matrix.Translation = respawnGrids[0].PositionComp.WorldAABB.Center + respawnGrids[0].PositionComp.WorldAABB.HalfExtents; } character = MyCharacter.CreateCharacter(matrix, Vector3.Zero, player.Identity.DisplayName, player.Identity.Model, null, botDefinition, cockpit: cockpit, playerSteamId: player.Id.SteamId); CloseRespawnShip(player); foreach (var respawnGrid in respawnGrids) { respawnGrid.ChangeGridOwnership(player.Identity.IdentityId, MyOwnershipShareModeEnum.None); respawnGrid.IsRespawnGrid = true; player.RespawnShip.Add(respawnGrid.EntityId); } if (cockpit != null) { cockpit.AttachPilot(character, false); MyMultiplayer.ReplicateImmediatelly(MyExternalReplicable.FindByObject(cockpit.CubeGrid), new EndpointId(player.Id.SteamId)); } if (cockpit == null) { Sync.Players.SetPlayerCharacter(player, character, null); } else { character.SetPlayer(player); Sync.Players.SetPlayerToCockpit(player, cockpit); } Sync.Players.RevivePlayer(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); } } } } } }
public MyHumanoidBot(MyPlayer player, MyBotDefinition botDefinition) : base(player, botDefinition) { }
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 IMyBot CreateBot(Type botType, MyPlayer player, MyBotDefinition botDefinition) { object[] args = new object[] { player, botDefinition }; return(Activator.CreateInstance(botType, args) as IMyBot); }
public MyHumanoidBot(MyPlayer player, MyBotDefinition botDefinition) : base(player, botDefinition) { Debug.Assert(botDefinition is MyHumanoidBotDefinition, "Provided bot definition is not of humanoid type"); }
public MyAnimalBot(MyPlayer player, MyBotDefinition botDefinition) : base(player, botDefinition) { }
public void SpawnAt(MatrixD worldMatrix, Vector3 velocity, MyEntity spawnedBy, MyBotDefinition botDefinition, bool findFreePlace = true) { Debug.Assert(Sync.IsServer, "Spawning can be called only on the server!"); Debug.Assert(Identity != null, "Spawning with empty identity!"); if (!Sync.IsServer || Identity == null) { return; } var character = MyCharacter.CreateCharacter(worldMatrix, velocity, Identity.DisplayName, Identity.Model, Identity.ColorMask, findNearPos: false, useInventory: Id.SerialId == 0, playerSteamId: this.Id.SteamId, botDefinition: botDefinition); if (findFreePlace) { float radius; MyModel model = character.Render.GetModel(); radius = model.BoundingBox.Size.Length() / 2; const float SPHERE_REDUCTION_RATE = 0.9f; radius *= SPHERE_REDUCTION_RATE; // Offset from bottom position to center. Vector3 up = worldMatrix.Up; up.Normalize(); const float RISE_STEP = 0.01f; // 1cm Vector3 offset = up * (radius + RISE_STEP); MatrixD matrix = worldMatrix; matrix.Translation = worldMatrix.Translation + offset; // Attempt first to rotate around the given spawn location (matrix.Up axis rotation) // NOTE: A proper orbital search would be better here Vector3D?correctedPos = MyEntities.FindFreePlace(ref matrix, matrix.GetDirectionVector(Base6Directions.Direction.Up), radius, 200, 15, 0.2f); if (!correctedPos.HasValue) { // Attempt secondly to rotate around matrix.Right axis correctedPos = MyEntities.FindFreePlace(ref matrix, matrix.GetDirectionVector(Base6Directions.Direction.Right), radius, 200, 15, 0.2f); if (!correctedPos.HasValue) { // If everything fails attempt the old FindFreePlace correctedPos = MyEntities.FindFreePlace(worldMatrix.Translation + offset, radius, 200, 15, 0.2f); } } if (correctedPos.HasValue) { worldMatrix.Translation = correctedPos.Value - offset; character.PositionComp.SetWorldMatrix(worldMatrix); } } Sync.Players.SetPlayerCharacter(this, character, spawnedBy); Sync.Players.RevivePlayer(this); }
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); } }