Faction Test 100.4.0.0

This commit is contained in:
Markus Isberg
2022-11-14 18:28:28 +02:00
parent 87426b68b2
commit c772b61fc1
412 changed files with 16984 additions and 5530 deletions
@@ -24,8 +24,6 @@ namespace Barotrauma
public const int MaxMoney = int.MaxValue / 2; //about 1 billion
public const int InitialMoney = 8500;
//duration of the cinematic + credits at the end of the campaign
protected const float EndCinematicDuration = 240.0f;
//duration of the camera transition at the end of a round
protected const float EndTransitionDuration = 5.0f;
//there can be no events before this time has passed during the 1st campaign round
@@ -45,7 +43,8 @@ namespace Barotrauma
public UpgradeManager UpgradeManager;
public MedicalClinic MedicalClinic;
public List<Faction> Factions;
private List<Faction> factions;
public IReadOnlyList<Faction> Factions => factions;
public CampaignMetadata CampaignMetadata;
@@ -139,6 +138,15 @@ namespace Barotrauma
public virtual bool PurchasedLostShuttles { get; set; }
public virtual bool PurchasedItemRepairs { get; set; }
private static bool AnyOneAllowedToManageCampaign(ClientPermissions permissions)
{
if (GameMain.NetworkMember == null) { return true; }
//allow managing if no-one with permissions is alive
return
GameMain.NetworkMember.ConnectedClients.Count == 1 ||
GameMain.NetworkMember.ConnectedClients.None(c => c.InGame && c.Character is { IsIncapacitated: false, IsDead: false } && (IsOwner(c) || c.HasPermission(permissions)));
}
protected CampaignMode(GameModePreset preset, CampaignSettings settings)
: base(preset)
{
@@ -211,7 +219,7 @@ namespace Barotrauma
return Level.Loaded?.StartLocation ?? Map.CurrentLocation;
}
public List<Submarine> GetSubsToLeaveBehind(Submarine leavingSub)
public static List<Submarine> GetSubsToLeaveBehind(Submarine leavingSub)
{
//leave subs behind if they're not docked to the leaving sub and not at the same exit
return Submarine.Loaded.FindAll(sub =>
@@ -266,7 +274,7 @@ namespace Barotrauma
wasDocked = Level.Loaded.StartOutpost != null && connectedSubs.Contains(Level.Loaded.StartOutpost);
}
public int GetHullRepairCost()
public static int GetHullRepairCost()
{
float totalDamage = 0;
foreach (Structure wall in Structure.WallList)
@@ -283,7 +291,7 @@ namespace Barotrauma
return (int)Math.Min(totalDamage * HullRepairCostPerDamage, MaxHullRepairCost);
}
public int GetItemRepairCost()
public static int GetItemRepairCost()
{
float totalRepairDuration = 0.0f;
foreach (Item item in Item.ItemList)
@@ -299,12 +307,12 @@ namespace Barotrauma
return (int)Math.Min(totalRepairDuration * ItemRepairCostPerRepairDuration, MaxItemRepairCost);
}
public void InitCampaignData()
public void InitFactions()
{
Factions = new List<Faction>();
factions = new List<Faction>();
foreach (FactionPrefab factionPrefab in FactionPrefab.Prefabs)
{
Factions.Add(new Faction(CampaignMetadata, factionPrefab));
factions.Add(new Faction(CampaignMetadata, factionPrefab));
}
}
@@ -341,10 +349,9 @@ namespace Barotrauma
currentLocation.DeselectMission(mission);
}
}
if (levelData.HasBeaconStation && !levelData.IsBeaconActive)
{
var beaconMissionPrefabs = MissionPrefab.Prefabs.Where(m => m.Tags.Any(t => t.Equals("beaconnoreward", StringComparison.OrdinalIgnoreCase))).OrderBy(m => m.UintIdentifier);
var beaconMissionPrefabs = MissionPrefab.Prefabs.Where(m => m.Tags.Contains("beaconnoreward")).OrderBy(m => m.UintIdentifier);
if (beaconMissionPrefabs.Any())
{
Random rand = new MTRandom(ToolBox.StringToInt(levelData.Seed));
@@ -357,7 +364,7 @@ namespace Barotrauma
}
if (levelData.HasHuntingGrounds)
{
var huntingGroundsMissionPrefabs = MissionPrefab.Prefabs.Where(m => m.Tags.Any(t => t.Equals("huntinggrounds", StringComparison.OrdinalIgnoreCase))).OrderBy(m => m.UintIdentifier);
var huntingGroundsMissionPrefabs = MissionPrefab.Prefabs.Where(m => m.Tags.Contains("huntinggrounds")).OrderBy(m => m.UintIdentifier);
if (!huntingGroundsMissionPrefabs.Any())
{
DebugConsole.AddWarning("Could not find a hunting grounds mission for the level. No mission with the tag \"huntinggrounds\" found.");
@@ -383,15 +390,95 @@ namespace Barotrauma
weights[i] = weight;
}
var huntingGroundsMissionPrefab = ToolBox.SelectWeightedRandom(prefabs, weights, rand);
if (!Missions.Any(m => m.Prefab.Tags.Any(t => t.Equals("huntinggrounds", StringComparison.OrdinalIgnoreCase))))
if (!Missions.Any(m => m.Prefab.Tags.Contains("huntinggrounds")))
{
extraMissions.Add(huntingGroundsMissionPrefab.Instantiate(Map.SelectedConnection.Locations, Submarine.MainSub));
}
}
}
foreach (Faction faction in factions.OrderBy(f => f.Prefab.MenuOrder))
{
if (currentLocation.Faction != faction && currentLocation.SecondaryFaction != faction &&
map.SelectedLocation?.Faction != faction && map.SelectedLocation?.SecondaryFaction != faction)
{
continue;
}
foreach (var automaticMission in faction.Prefab.AutomaticMissions)
{
if (faction.Reputation.Value < automaticMission.MinReputation || faction.Reputation.Value > automaticMission.MaxReputation) { continue; }
Random rand = new MTRandom(ToolBox.StringToInt(levelData.Seed + TotalPassedLevels));
if (levelData.Type != automaticMission.LevelType) { continue; }
float probability =
MathHelper.Lerp(
automaticMission.MinProbability,
automaticMission.MaxProbability,
MathUtils.InverseLerp(automaticMission.MinReputation, automaticMission.MaxReputation, faction.Reputation.Value));
if (rand.NextDouble() < probability)
{
var missionPrefabs = MissionPrefab.Prefabs.Where(m => m.Tags.Any(t => t == automaticMission.MissionTag)).OrderBy(m => m.UintIdentifier);
if (missionPrefabs.Any())
{
var missionPrefab = ToolBox.SelectWeightedRandom(missionPrefabs, p => (float)p.Commonness, rand);
if (missionPrefab.Type == MissionType.Pirate && Missions.Any(m => m.Prefab.Type == MissionType.Pirate))
{
continue;
}
if (automaticMission.LevelType == LevelData.LevelType.Outpost)
{
extraMissions.Add(missionPrefab.Instantiate(new Location[] { currentLocation, currentLocation }, Submarine.MainSub));
}
else
{
extraMissions.Add(missionPrefab.Instantiate(Map.SelectedConnection.Locations, Submarine.MainSub));
}
}
}
}
}
}
if (levelData.Biome.IsEndBiome)
{
Identifier endMissionTag = Identifier.Empty;
if (levelData.Type == LevelData.LevelType.LocationConnection)
{
int locationIndex = map.EndLocations.IndexOf(map.SelectedLocation);
if (locationIndex > -1)
{
endMissionTag = ("endlevel_locationconnection_" + locationIndex).ToIdentifier();
}
}
else
{
int locationIndex = map.EndLocations.IndexOf(map.CurrentLocation);
if (locationIndex > -1)
{
endMissionTag = ("endlevel_location_" + locationIndex).ToIdentifier();
}
}
if (!endMissionTag.IsEmpty)
{
var endLevelMissionPrefabs = MissionPrefab.Prefabs.Where(m => m.Tags.Contains(endMissionTag)).OrderBy(m => m.UintIdentifier);
if (endLevelMissionPrefabs.Any())
{
Random rand = new MTRandom(ToolBox.StringToInt(levelData.Seed));
var endLevelMissionPrefab = ToolBox.SelectWeightedRandom(endLevelMissionPrefabs, p => (float)p.Commonness, rand);
if (!Missions.Any(m => m.Prefab.Type == endLevelMissionPrefab.Type))
{
if (levelData.Type == LevelData.LevelType.LocationConnection)
{
extraMissions.Add(endLevelMissionPrefab.Instantiate(map.SelectedConnection.Locations, Submarine.MainSub));
}
else
{
extraMissions.Add(endLevelMissionPrefab.Instantiate(new Location[] { map.CurrentLocation, map.CurrentLocation }, Submarine.MainSub));
}
}
}
}
}
}
public void LoadNewLevel()
{
if (GameMain.NetworkMember != null && GameMain.NetworkMember.IsClient)
@@ -486,13 +573,6 @@ namespace Barotrauma
{
if (leavingSub.AtEndExit)
{
if (Map.EndLocation != null &&
map.SelectedLocation == Map.EndLocation &&
Map.EndLocation.Connections.Any(c => c.LevelData == Level.Loaded.LevelData))
{
nextLevel = map.StartLocation.LevelData;
return TransitionType.End;
}
if (Level.Loaded.EndLocation != null && Level.Loaded.EndLocation.Type.HasOutpost && Level.Loaded.EndOutpost != null)
{
nextLevel = Level.Loaded.EndLocation.LevelData;
@@ -537,8 +617,32 @@ namespace Barotrauma
}
else if (Level.Loaded.Type == LevelData.LevelType.Outpost)
{
nextLevel = map.SelectedLocation == null ? null : map.SelectedConnection?.LevelData;
return nextLevel == null ? TransitionType.None : TransitionType.LeaveLocation;
int currentEndLocationIndex = map.EndLocations.IndexOf(map.CurrentLocation);
if (currentEndLocationIndex > -1)
{
if (currentEndLocationIndex == map.EndLocations.Count - 1)
{
//at the last end location, end of campaign
nextLevel = map.StartLocation?.LevelData;
return TransitionType.End;
}
else if (leavingSub.AtEndExit && currentEndLocationIndex < map.EndLocations.Count - 1)
{
//more end locations to go, progress to the next one
nextLevel = map.EndLocations[currentEndLocationIndex + 1]?.LevelData;
return TransitionType.ProgressToNextLocation;
}
else
{
nextLevel = null;
return TransitionType.None;
}
}
else
{
nextLevel = map.SelectedLocation == null ? null : map.SelectedConnection?.LevelData;
return nextLevel == null ? TransitionType.None : TransitionType.LeaveLocation;
}
}
else
{
@@ -551,7 +655,7 @@ namespace Barotrauma
/// <summary>
/// Which submarine is at a position where it can leave the level and enter another one (if any).
/// </summary>
private Submarine GetLeavingSub()
private static Submarine GetLeavingSub()
{
if (Level.IsLoadedOutpost)
{
@@ -606,8 +710,17 @@ namespace Barotrauma
static Submarine GetLeavingSubAtEnd(IEnumerable<Character> leavingPlayers)
{
if (Level.Loaded.EndOutpost != null && Level.Loaded.EndOutpost.ExitPoints.Any())
{
Submarine closestSub = Submarine.FindClosest(Level.Loaded.EndOutpost.WorldPosition, ignoreOutposts: true, ignoreRespawnShuttle: true, teamType: leavingPlayers.FirstOrDefault()?.TeamID);
if (closestSub == null || !closestSub.AtEndExit) { return null; }
return closestSub.DockedTo.Contains(Submarine.MainSub) ? Submarine.MainSub : closestSub;
}
//no "end" in outpost levels
if (Level.Loaded.Type == LevelData.LevelType.Outpost) { return null; }
if (Level.Loaded.Type == LevelData.LevelType.Outpost)
{
return null;
}
if (Level.Loaded.EndOutpost == null)
{
@@ -738,8 +851,9 @@ namespace Barotrauma
foreach (Location location in Map.Locations)
{
location.LevelData = new LevelData(location, location.Biome.AdjustedMaxDifficulty);
location.Reset();
location.Reset(this);
}
Map.ClearLocationHistory();
Map.SetLocation(Map.Locations.IndexOf(Map.StartLocation));
Map.SelectLocation(-1);
if (Map.Radiation != null)
@@ -771,11 +885,56 @@ namespace Barotrauma
protected virtual void EndCampaignProjSpecific() { }
/// <summary>
/// Returns a random faction based on their ControlledOutpostPercentage
/// </summary>
/// <param name="allowEmpty">If true, the method can return null if the sum of the factions ControlledOutpostPercentage is less than 100%</param>
public Faction GetRandomFaction(Rand.RandSync randSync, bool allowEmpty = true)
{
return GetRandomFaction(Factions, randSync, secondary: false, allowEmpty);
}
/// <summary>
/// Returns a random faction based on their SecondaryControlledOutpostPercentage
/// </summary>
/// <param name="allowEmpty">If true, the method can return null if the sum of the factions SecondaryControlledOutpostPercentage is less than 100%</param>
public Faction GetRandomSecondaryFaction(Rand.RandSync randSync, bool allowEmpty = true)
{
return GetRandomFaction(Factions, randSync, secondary: true, allowEmpty);
}
public static Faction GetRandomFaction(IEnumerable<Faction> factions, Rand.RandSync randSync, bool secondary = false, bool allowEmpty = true)
{
return GetRandomFaction(factions, Rand.GetRNG(randSync), secondary, allowEmpty);
}
public static Faction GetRandomFaction(IEnumerable<Faction> factions, Random random, bool secondary = false, bool allowEmpty = true)
{
List<Faction> factionsList = factions.OrderBy(f => f.Prefab.Identifier).ToList();
List<float> weights = factionsList.Select(f => secondary ? f.Prefab.SecondaryControlledOutpostPercentage : f.Prefab.ControlledOutpostPercentage).ToList();
float percentageSum = weights.Sum();
if (percentageSum < 100.0f && allowEmpty)
{
//chance of non-faction-specific outposts if percentage of controlled outposts is <100
factionsList.Add(null);
weights.Add(100.0f - percentageSum);
}
return ToolBox.SelectWeightedRandom(factionsList, weights, random);
}
public bool TryHireCharacter(Location location, CharacterInfo characterInfo, Client client = null)
{
if (characterInfo == null) { return false; }
if (characterInfo.MinReputationToHire.factionId != Identifier.Empty)
{
if (GetReputation(characterInfo.MinReputationToHire.factionId) < characterInfo.MinReputationToHire.reputation)
{
return false;
}
}
if (!TryPurchase(client, characterInfo.Salary)) { return false; }
characterInfo.IsNewHire = true;
characterInfo.Title = null;
location.RemoveHireableCharacter(characterInfo);
CrewManager.AddCharacterInfo(characterInfo);
GameAnalyticsManager.AddMoneySpentEvent(characterInfo.Salary, GameAnalyticsManager.MoneySink.Crew, characterInfo.Job?.Prefab.Identifier.Value ?? "unknown");
@@ -946,11 +1105,28 @@ namespace Barotrauma
if (npc == null || attacker == null || npc.IsDead || npc.IsInstigator) { return; }
if (npc.TeamID != CharacterTeamType.FriendlyNPC) { return; }
if (!attacker.IsRemotePlayer && attacker != Character.Controlled) { return; }
Location location = Map?.CurrentLocation;
if (location != null)
if (npc.HumanPrefab?.Faction != null && Factions.FirstOrDefault(f => f.Prefab.Identifier == npc.HumanPrefab.Faction) is Faction faction)
{
location.Reputation.AddReputation(-attackResult.Damage * Reputation.ReputationLossPerNPCDamage);
faction.Reputation?.AddReputation(-attackResult.Damage * Reputation.ReputationLossPerNPCDamage);
}
else
{
Location location = Map?.CurrentLocation;
if (location != null)
{
location.Reputation?.AddReputation(-attackResult.Damage * Reputation.ReputationLossPerNPCDamage);
}
}
}
public float GetReputation(Identifier factionIdentifier)
{
var faction =
factionIdentifier == "location".ToIdentifier() ?
factions.Find(f => f == Map?.CurrentLocation?.Faction) :
factions.Find(f => f.Prefab.Identifier == factionIdentifier);
return faction?.Reputation?.Value ?? 0.0f;
}
public abstract void Save(XElement element);
@@ -1025,7 +1201,7 @@ namespace Barotrauma
}
}
protected void LeaveUnconnectedSubs(Submarine leavingSub)
protected static void LeaveUnconnectedSubs(Submarine leavingSub)
{
if (leavingSub != Submarine.MainSub && !leavingSub.DockedTo.Contains(Submarine.MainSub))
{
@@ -2,6 +2,7 @@
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace Barotrauma
@@ -17,6 +18,9 @@ namespace Barotrauma
[Serialize("", IsPropertySaveable.Yes)]
public string PresetName { get; set; } = string.Empty;
[Serialize(true, IsPropertySaveable.Yes)]
public bool TutorialEnabled { get; set; }
[Serialize(false, IsPropertySaveable.Yes), NetworkSerialize]
public bool RadiationEnabled { get; set; }
@@ -103,12 +107,9 @@ namespace Barotrauma
private static int GetAddedMissionCount()
{
int count = 0;
foreach (Character character in GameSession.GetSessionCrewCharacters(CharacterType.Both))
{
count += (int)character.GetStatValue(StatTypes.ExtraMissionCount);
}
return count;
var characters = GameSession.GetSessionCrewCharacters(CharacterType.Both);
if (!characters.Any()) { return 0; }
return characters.Max(static character => (int)character.GetStatValue(StatTypes.ExtraMissionCount));
}
}
}
@@ -105,9 +105,9 @@ namespace Barotrauma
#endif
}
CampaignID = currentCampaignID;
CampaignMetadata = new CampaignMetadata(this);
CampaignMetadata = new CampaignMetadata();
UpgradeManager = new UpgradeManager(this);
InitCampaignData();
InitFactions();
}
public static MultiPlayerCampaign StartNew(string mapSeed, CampaignSettings settings)
@@ -133,13 +133,13 @@ namespace Barotrauma
}
partial void InitProjSpecific();
public static string GetCharacterDataSavePath(string savePath)
{
return Path.Combine(SaveUtil.MultiplayerSaveFolder, Path.GetFileNameWithoutExtension(savePath) + "_CharacterData.xml");
return Path.Combine(Path.GetDirectoryName(savePath), Path.GetFileNameWithoutExtension(savePath) + "_CharacterData.xml");
}
public string GetCharacterDataSavePath()
public static string GetCharacterDataSavePath()
{
return GetCharacterDataSavePath(GameMain.GameSession.SavePath);
}
@@ -190,11 +190,11 @@ namespace Barotrauma
//map already created, update it
//if we're not downloading the initial save file (LastSaveID > 0),
//show notifications about location type changes
map.LoadState(subElement, LastSaveID > 0);
map.LoadState(this, subElement, LastSaveID > 0);
}
break;
case "metadata":
CampaignMetadata = new CampaignMetadata(this, subElement);
CampaignMetadata = new CampaignMetadata(subElement);
break;
case "upgrademanager":
case "pendingupgrades":
@@ -237,10 +237,10 @@ namespace Barotrauma
};
}
CampaignMetadata ??= new CampaignMetadata(this);
CampaignMetadata ??= new CampaignMetadata();
UpgradeManager ??= new UpgradeManager(this);
InitCampaignData();
InitFactions();
#if SERVER
characterData.Clear();
string characterDataPath = GetCharacterDataSavePath();
@@ -29,6 +29,14 @@ namespace Barotrauma
public readonly Sprite Banner;
public readonly EndMessageInfo EndMessage;
public enum EndType { None, Continue, Restart }
public readonly record struct EndMessageInfo(
EndType EndType,
Identifier NextTutorialIdentifier);
public TutorialPrefab(ContentFile file, ContentXElement element) : base(file, element.GetAttributeIdentifier("identifier", ""))
{
Order = element.GetAttributeInt("order", int.MaxValue);
@@ -59,6 +67,13 @@ namespace Barotrauma
}
EventIdentifier = element.GetChildElement("scriptedevent")?.GetAttributeIdentifier("identifier", "") ?? Identifier.Empty;
if (element.GetChildElement("endmessage") is ContentXElement endMessageElement)
{
EndMessage = new EndMessageInfo(
EndType: endMessageElement.GetAttributeEnum("type", EndType.None),
NextTutorialIdentifier: endMessageElement.GetAttributeIdentifier("nexttutorial", Identifier.Empty));
}
}
public CharacterInfo GetTutorialCharacterInfo()