private static void SetDefaultFactionStates(long recivedFactionId) { IMyFaction recievedFaction = MySession.Static.Factions.TryGetFactionById(recivedFactionId); MyFactionDefinition recievedFactionDefiniton = MyDefinitionManager.Static.TryGetFactionDefinition(recievedFaction.Tag); foreach (var factionPair in MySession.Static.Factions) { MyFaction faction = factionPair.Value; if (faction.FactionId == recivedFactionId) { continue; } if (recievedFactionDefiniton != null) // If I have definition, force my relations on everyone. { SetDefaultFactionStateInternal(faction.FactionId, recievedFaction, recievedFactionDefiniton); } else // Otherwise search for default factions and set their relations on me. { MyFactionDefinition factionDefiniton = MyDefinitionManager.Static.TryGetFactionDefinition(faction.Tag); if (factionDefiniton == null) { continue; } SetDefaultFactionStateInternal(recivedFactionId, faction, factionDefiniton); } } }
/// <summary> /// Creates faction from definition. /// </summary> /// <param name="founderId">Identity id of the owner.</param> /// <param name="factionId">Faction id to be used for the faction.</param> /// <param name="factionDef">Faction definition.</param> /// <returns>If true than faction was created.</returns> private static bool CreateFactionInternal(long founderId, long factionId, MyFactionDefinition factionDef) { if (MySession.Static.Factions.Contains(factionId)) { return(false); } var newFaction = new MyFaction( id: factionId, tag: factionDef.Tag, name: factionDef.DisplayNameText, desc: factionDef.DescriptionText, privateInfo: "", creatorId: founderId); MySession.Static.Factions.Add(newFaction); MySession.Static.Factions.AddPlayerToFaction(founderId, factionId); newFaction.AcceptHumans = factionDef.AcceptHumans; newFaction.AutoAcceptMember = factionDef.AutoAcceptMember; newFaction.EnableFriendlyFire = factionDef.EnableFriendlyFire; AfterFactionCreated(founderId, factionId); return(true); }
public static void Initialize() { if (_setupComplete) { return; } _setupComplete = true; foreach (var faction in MyAPIGateway.Session.Factions.Factions) { MyFactionDefinition def = MyDefinitionManager.Static.TryGetFactionDefinition(faction.Value.Tag); if (def == null) { // Player faction, Vanilla Trader, or some other mods faction that creates everything in code and nothing in the .sbc if (MyAPIGateway.Players.TryGetSteamId(faction.Value.FounderId) > 0) { // Player faction of some sort if (Settings.PlayerFactionExclusionList.Any(x => faction.Value.Description.StartsWith(x))) { // Player pirate PlayerPirateFactions.Add(faction.Key, faction.Value); continue; } // Regular player faction PlayerFactions.Add(faction.Key, faction.Value); continue; } // Vanilla trader faction VanillaTradeFactions.Add(faction.Key, faction.Value); continue; } if (Settings.FactionTags[FactionTypes.Neutral].Contains(def.Tag)) { LawfulFactions.Add(faction.Key, faction.Value); NonTraderNpcFactions.Add(faction.Key, faction.Value); continue; } if (Settings.FactionTags[FactionTypes.Enforcement].Contains(def.Tag)) { EnforcementFactions.Add(faction.Key, faction.Value); NonTraderNpcFactions.Add(faction.Key, faction.Value); continue; } if (Settings.FactionTags[FactionTypes.Hostile].Contains(def.Tag)) { PirateFactions.Add(faction.Key, faction.Value); NonTraderNpcFactions.Add(faction.Key, faction.Value); continue; } if (Settings.FactionTags[FactionTypes.Player].Contains(def.Tag)) { PlayerFactions.Add(faction.Key, faction.Value); continue; } // I'm guessing this is a NPC faction and it's not mine. AllNonEemNpcFactions.Add(faction.Key, faction.Value); NonTraderNpcFactions.Add(faction.Key, faction.Value); } }
/// <summary> /// Creates faction on server and sends message to client to create one there. If faction definition is provided than faction will be created from definition and /// faction name, description and private info will be taken from definition. /// </summary> /// <param name="founderId">Founder id</param> /// <param name="factionTag">Faction tag</param> /// <param name="factionName">Faction name</param> /// <param name="description">Faction Description</param> /// <param name="privateInfo">Private info</param> /// <param name="factionDef">Optional: faction definition.</param> private static void CreateFactionServer(long founderId, string factionTag, string factionName, string description, string privateInfo, MyFactionDefinition factionDef = null) { Debug.Assert(Sync.IsServer, "Faction ID can only be allocated on the server!"); if (!Sync.IsServer) { return; } long factionId = MyEntityIdentifier.AllocateId(MyEntityIdentifier.ID_OBJECT_TYPE.FACTION); var faction = MySession.Static.Factions.TryGetFactionById(factionId); var senderFaction = MySession.Static.Factions.TryGetPlayerFaction(founderId); if (senderFaction == null && faction == null && !MySession.Static.Factions.FactionTagExists(factionTag) && !MySession.Static.Factions.FactionNameExists(factionName) && Sync.Players.HasIdentity(founderId)) { bool createFromDef = factionDef == null ? false : true; if (createFromDef) { CreateFactionInternal(founderId, factionId, factionDef); } else { CreateFactionInternal(founderId, factionId, factionTag, factionName, description, privateInfo); } AddFactionMsg newMsg = new AddFactionMsg(); newMsg.FactionId = factionId; newMsg.FounderId = founderId; newMsg.FactionTag = factionTag; newMsg.FactionName = factionName; newMsg.FactionDescription = description; newMsg.FactionPrivateInfo = privateInfo; newMsg.CreateFromDefinition = createFromDef; Sync.Layer.SendMessageToAll(ref newMsg, MyTransportMessageEnum.Success); // Call myself. SetDefaultFactionStates(factionId); // Call everyone else. MyMultiplayer.RaiseStaticEvent(x => SetDefaultFactionStates, factionId); } }
/// <summary> /// Sets default faction relation on provided faction. /// </summary> /// <param name="factionId">Faction on which set the default faction relations.</param> /// <param name="defaultFaction">Default faction which contains definition of the relation.</param> /// <param name="defaultFactionDef">Default faction definition.</param> private static void SetDefaultFactionStateInternal(long factionId, IMyFaction defaultFaction, MyFactionDefinition defaultFactionDef) { MyFactionStateChange stateChange = DetermineRequestFromRelation(defaultFactionDef.DefaultRelation); MySession.Static.Factions.ApplyFactionStateChange(stateChange, defaultFaction.FactionId, factionId, defaultFaction.FounderId, defaultFaction.FounderId); var handler = MySession.Static.Factions.FactionStateChanged; if (handler != null) { handler(stateChange, defaultFaction.FactionId, factionId, defaultFaction.FounderId, defaultFaction.FounderId); } }