public BaseEntity(MyObjectBuilder_EntityBase baseEntity) : base(baseEntity) { if (baseEntity != null) { m_entityId = baseEntity.EntityId; if (baseEntity.PositionAndOrientation != null) { m_positionOrientation = baseEntity.PositionAndOrientation.GetValueOrDefault(); } else { m_positionOrientation = new MyPositionAndOrientation(); m_positionOrientation.Position = UtilityFunctions.GenerateRandomBorderPosition(new Vector3(-500000, -500000, -500000), new Vector3(500000, 500000, 500000)); m_positionOrientation.Forward = new Vector3(0, 0, 1); m_positionOrientation.Up = new Vector3(0, 1, 0); } } else { m_entityId = 0; m_positionOrientation = new MyPositionAndOrientation(); m_positionOrientation.Position = UtilityFunctions.GenerateRandomBorderPosition(new Vector3(-500000, -500000, -500000), new Vector3(500000, 500000, 500000)); m_positionOrientation.Forward = new Vector3(0, 0, 1); m_positionOrientation.Up = new Vector3(0, 1, 0); } m_linearVelocity = new Vector3(0, 0, 0); m_angularVelocity = new Vector3(0, 0, 0); m_maxLinearVelocity = (float)104.7; }
public override void Init(MyObjectBuilder_WorldGeneratorOperation builder) { base.Init(builder); var ob = builder as MyObjectBuilder_WorldGeneratorOperation_AddShipPrefab; PrefabFile = ob.PrefabFile; Transform = ob.Transform; RandomRadius = ob.RandomRadius; }
public void SendPilotRelativeEntryUpdate(ref MyPositionAndOrientation relativeEntry) { UpdatePilotRelativeEntryMsg msg; msg.EntityId = m_shipController.EntityId; msg.RelativeEntry = relativeEntry; MySession.Static.SyncLayer.SendMessageToAll(ref msg, MyTransportMessageEnum.Success); }
private void RaisePilotRelativeEntryUpdated(MyPositionAndOrientation relativeEntry) { var handler = PilotRelativeEntryUpdated; if (handler != null) handler(relativeEntry); }
public BaseEntity(MyObjectBuilder_EntityBase baseEntity, Object backingObject) : base(baseEntity, backingObject) { if (baseEntity != null) { m_entityId = baseEntity.EntityId; if (baseEntity.PositionAndOrientation != null) { m_positionOrientation = baseEntity.PositionAndOrientation.GetValueOrDefault(); } else { m_positionOrientation = new MyPositionAndOrientation(); m_positionOrientation.Position = UtilityFunctions.GenerateRandomBorderPosition(new Vector3(-500000, -500000, -500000), new Vector3(500000, 500000, 500000)); m_positionOrientation.Forward = new Vector3(0, 0, 1); m_positionOrientation.Up = new Vector3(0, 1, 0); } } else { m_entityId = 0; m_positionOrientation = new MyPositionAndOrientation(); m_positionOrientation.Position = UtilityFunctions.GenerateRandomBorderPosition(new Vector3(-500000, -500000, -500000), new Vector3(500000, 500000, 500000)); m_positionOrientation.Forward = new Vector3(0, 0, 1); m_positionOrientation.Up = new Vector3(0, 1, 0); } m_networkManager = new BaseEntityNetworkManager(this, GetEntityNetworkManager(BackingObject)); m_linearVelocity = new Vector3(0, 0, 0); m_angularVelocity = new Vector3(0, 0, 0); m_maxLinearVelocity = (float)104.7; Action action = InternalRegisterEntityMovedEvent; SandboxGameAssemblyWrapper.Instance.EnqueueMainGameAction(action); }
public void SpawnCargoShipGroup(Vector3 startPosition, Vector3 stopPosition, ulong remoteUserId = 0) { try { //Load the spawn groups SpawnGroupsDefinitionsManager spawnGroupsDefinitionsManager = new SpawnGroupsDefinitionsManager(); FileInfo contentDataFile = new FileInfo(Path.Combine(MyFileSystem.ContentPath, "Data", "SpawnGroups.sbc")); spawnGroupsDefinitionsManager.Load(contentDataFile); //Calculate lowest and highest frequencies float lowestFrequency = 999999; float highestFrequency = 0; foreach (SpawnGroupDefinition entry in spawnGroupsDefinitionsManager.Definitions) { if (entry.Frequency < lowestFrequency) lowestFrequency = entry.Frequency; if (entry.Frequency > highestFrequency) highestFrequency = entry.Frequency; } if (lowestFrequency <= 0) lowestFrequency = 1; //Get a list of which groups *could* spawn Random random = new Random((int)DateTime.Now.ToBinary()); double randomChance = random.NextDouble(); randomChance = randomChance * (highestFrequency / lowestFrequency); List<SpawnGroupDefinition> possibleGroups = new List<SpawnGroupDefinition>(); foreach (SpawnGroupDefinition entry in spawnGroupsDefinitionsManager.Definitions) { if (entry.Frequency >= randomChance) { possibleGroups.Add(entry); } } //Determine which group *will* spawn randomChance = random.NextDouble(); int randomShipIndex = Math.Max(0, Math.Min((int)Math.Round(randomChance * possibleGroups.Count, 0), possibleGroups.Count-1)); SpawnGroupDefinition randomSpawnGroup = possibleGroups[randomShipIndex]; ChatManager.Instance.SendPrivateChatMessage(remoteUserId, "Spawning cargo group '" + randomSpawnGroup.Name + "' ..."); //Spawn the ships in the group Matrix orientation = Matrix.CreateLookAt(startPosition, stopPosition, new Vector3(0, 1, 0)); foreach (SpawnGroupPrefab entry in randomSpawnGroup.Prefabs) { FileInfo prefabFile = new FileInfo(Path.Combine(MyFileSystem.ContentPath, "Data", "Prefabs", entry.SubtypeId + ".sbc")); if (!prefabFile.Exists) continue; //Create the ship CubeGridEntity cubeGrid = new CubeGridEntity(prefabFile); //Set the ship position and orientation Vector3 shipPosition = Vector3.Transform(entry.Position, orientation) + startPosition; orientation.Translation = shipPosition; MyPositionAndOrientation newPositionOrientation = new MyPositionAndOrientation(orientation); cubeGrid.PositionAndOrientation = newPositionOrientation; //Set the ship velocity //Speed is clamped between 1.0f and the max cube grid speed Vector3 travelVector = stopPosition - startPosition; travelVector.Normalize(); Vector3 shipVelocity = travelVector * (float)Math.Min(cubeGrid.MaxLinearVelocity, Math.Max(1.0f, entry.Speed)); cubeGrid.LinearVelocity = shipVelocity; cubeGrid.IsDampenersEnabled = false; foreach (MyObjectBuilder_CubeBlock cubeBlock in cubeGrid.BaseCubeBlocks) { //Set the beacon names if (cubeBlock.TypeId == typeof(MyObjectBuilder_Beacon)) { MyObjectBuilder_Beacon beacon = (MyObjectBuilder_Beacon)cubeBlock; beacon.CustomName = entry.BeaconText; } //Set the owner of every block //TODO - Find out if setting to an arbitrary non-zero works for this cubeBlock.Owner = PlayerMap.Instance.GetServerVirtualPlayerId(); cubeBlock.ShareMode = MyOwnershipShareModeEnum.Faction; } //And add the ship to the world SectorObjectManager.Instance.AddEntity(cubeGrid); //Force a refresh of the cube grid List<CubeBlockEntity> cubeBlocks = cubeGrid.CubeBlocks; } ChatManager.Instance.SendPrivateChatMessage(remoteUserId, "Cargo group '" + randomSpawnGroup.DisplayName + "' spawned with " + randomSpawnGroup.Prefabs.Length.ToString() + " ships at " + startPosition.ToString()); } catch (Exception ex) { LogManager.ErrorLog.WriteLine(ex); } }
public void SpawnCargoShipGroup(Vector3 startPosition, Vector3 stopPosition, ulong remoteUserId = 0) { try { //Calculate lowest and highest frequencies float lowestFrequency = 999999; float highestFrequency = 0; foreach (MySpawnGroupDefinition entry in MyDefinitionManager.Static.GetSpawnGroupDefinitions()) { if (entry.Frequency < lowestFrequency) lowestFrequency = entry.Frequency; if (entry.Frequency > highestFrequency) highestFrequency = entry.Frequency; } if (lowestFrequency <= 0) lowestFrequency = 1; //Get a list of which groups *could* spawn Random random = new Random((int)DateTime.Now.ToBinary()); double randomChance = random.NextDouble(); randomChance = randomChance * (highestFrequency / lowestFrequency); List<MySpawnGroupDefinition> possibleGroups = new List<MySpawnGroupDefinition>(); foreach (MySpawnGroupDefinition entry in MyDefinitionManager.Static.GetSpawnGroupDefinitions()) { if (entry.Frequency >= randomChance) { possibleGroups.Add(entry); } } //Determine which group *will* spawn randomChance = random.NextDouble(); int randomShipIndex = Math.Max(0, Math.Min((int)Math.Round(randomChance * possibleGroups.Count, 0), possibleGroups.Count - 1)); MySpawnGroupDefinition randomSpawnGroup = possibleGroups[randomShipIndex]; ChatManager.Instance.SendPrivateChatMessage(remoteUserId, "Spawning cargo group '" + randomSpawnGroup.DisplayNameText.ToString() + "' ..."); //Spawn the ships in the group Matrix orientation = Matrix.CreateLookAt(startPosition, stopPosition, new Vector3(0, 1, 0)); foreach (MySpawnGroupDefinition.SpawnGroupPrefab entry in randomSpawnGroup.Prefabs) { MyPrefabDefinition matchedPrefab = null; foreach (var prefabEntry in MyDefinitionManager.Static.GetPrefabDefinitions()) { MyPrefabDefinition prefabDefinition = prefabEntry.Value; if (prefabDefinition.Id.SubtypeId.ToString() == entry.SubtypeId) { matchedPrefab = prefabDefinition; break; } } if (matchedPrefab == null) continue; //TODO - Build this to iterate through all cube grids in the prefab MyObjectBuilder_CubeGrid objectBuilder = matchedPrefab.CubeGrids[0]; //Create the ship CubeGridEntity cubeGrid = new CubeGridEntity(objectBuilder); //Set the ship position and orientation Vector3 shipPosition = Vector3.Transform(entry.Position, orientation) + startPosition; orientation.Translation = shipPosition; MyPositionAndOrientation newPositionOrientation = new MyPositionAndOrientation(orientation); cubeGrid.PositionAndOrientation = newPositionOrientation; //Set the ship velocity //Speed is clamped between 1.0f and the max cube grid speed Vector3 travelVector = stopPosition - startPosition; travelVector.Normalize(); Vector3 shipVelocity = travelVector * (float)Math.Min(cubeGrid.MaxLinearVelocity, Math.Max(1.0f, entry.Speed)); cubeGrid.LinearVelocity = shipVelocity; cubeGrid.IsDampenersEnabled = false; foreach (MyObjectBuilder_CubeBlock cubeBlock in cubeGrid.BaseCubeBlocks) { //Set the beacon names if (cubeBlock.TypeId == typeof(MyObjectBuilder_Beacon)) { MyObjectBuilder_Beacon beacon = (MyObjectBuilder_Beacon)cubeBlock; beacon.CustomName = entry.BeaconText; } //Set the owner of every block //TODO - Find out if setting to an arbitrary non-zero works for this // TODO Fix the rest of the player manager methods //cubeBlock.Owner = PlayerMap.Instance.GetServerVirtualPlayerId(); cubeBlock.ShareMode = MyOwnershipShareModeEnum.Faction; } //And add the ship to the world SectorObjectManager.Instance.AddEntity(cubeGrid); } ChatManager.Instance.SendPrivateChatMessage(remoteUserId, "Cargo group '" + randomSpawnGroup.DisplayNameText.ToString() + "' spawned with " + randomSpawnGroup.Prefabs.Count.ToString() + " ships at " + startPosition.ToString()); } catch (Exception ex) { LogManager.ErrorLog.WriteLine(ex); } }
public void AttachPilot(MyCharacter pilot, bool storeOriginalPilotWorld = true, bool calledFromInit = false) { System.Diagnostics.Debug.Assert(pilot != null); System.Diagnostics.Debug.Assert(m_pilot == null); m_pilot = pilot; m_pilot.OnMarkForClose += m_pilotClosedHandler; m_pilot.IsUsing = this; //m_soundEmitter.OwnedBy = m_pilot; if (MyFakes.ENABLE_NEW_SOUNDS) StartLoopSound(); if (storeOriginalPilotWorld) { m_pilotRelativeWorld = (Matrix)MatrixD.Multiply(pilot.WorldMatrix, this.PositionComp.GetWorldMatrixNormalizedInv()); if (Sync.IsServer) { var relativeEntry = new MyPositionAndOrientation(m_pilotRelativeWorld.Value); SyncObject.SendPilotRelativeEntryUpdate(ref relativeEntry); } } if (pilot.InScene) MyEntities.Remove(pilot); m_pilot.Physics.Enabled = false; m_pilot.PositionComp.SetWorldMatrix(WorldMatrix); m_pilot.Physics.Clear(); //m_pilot.SetPosition(GetPosition() - WorldMatrix.Forward * 0.5f); Hierarchy.AddChild(m_pilot, true, true); var gunEntity = m_pilot.CurrentWeapon as MyEntity; if (gunEntity != null) { var ob = gunEntity.GetObjectBuilder(); m_pilotGunDefinition = ob.GetId(); } else m_pilotGunDefinition = null; MyAnimationDefinition animationDefinition; MyDefinitionId id = new MyDefinitionId(typeof(MyObjectBuilder_AnimationDefinition), BlockDefinition.CharacterAnimation); if (!MyDefinitionManager.Static.TryGetDefinition(id, out animationDefinition) && !MyFileSystem.FileExists(BlockDefinition.CharacterAnimation)) { BlockDefinition.CharacterAnimation = null; } PlacePilotInSeat(pilot); m_rechargeSocket.PlugIn(m_pilot.SuitBattery); // Control should be handled elsewhere if we initialize the grid in the Init(...) if (!calledFromInit) GiveControlToPilot(); m_pilot.SwitchToWeapon(null); }
protected override void sync_PilotRelativeEntryUpdated(MyPositionAndOrientation relativeEntry) { base.sync_PilotRelativeEntryUpdated(relativeEntry); m_pilotRelativeWorld = relativeEntry.GetMatrix(); }