Skip to content

Commit

Permalink
Add ideo preset chooser for new factions
Browse files Browse the repository at this point in the history
Fix saving of policies (outfits etc)
Remove spectator faction when converting to sp
  • Loading branch information
Zetrith committed Oct 18, 2023
1 parent 67e613b commit b52e372
Show file tree
Hide file tree
Showing 14 changed files with 260 additions and 57 deletions.
10 changes: 9 additions & 1 deletion Source/Client/Comp/World/FactionWorldData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ public void ExposeData()
Scribe_Deep.Look(ref researchSpeed, "researchSpeed");
}

public void Tick()
public void ReassignIds()
{
foreach (DrugPolicy p in drugPolicyDatabase.policies)
p.uniqueId = Find.UniqueIDsManager.GetNextThingID();

foreach (Outfit o in outfitDatabase.outfits)
o.uniqueId = Find.UniqueIDsManager.GetNextThingID();

foreach (FoodRestriction o in foodRestrictionDatabase.foodRestrictions)
o.id = Find.UniqueIDsManager.GetNextThingID();
}

public static FactionWorldData New(int factionId)
Expand Down
37 changes: 34 additions & 3 deletions Source/Client/Comp/World/MultiplayerWorldComp.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using RimWorld;
using RimWorld.Planet;
using System.Collections.Generic;
using System.Linq;
using Verse;
using Multiplayer.Client.Persistent;
using Multiplayer.Client.Saving;
Expand Down Expand Up @@ -36,16 +37,46 @@ public void ExposeData()
Scribe_Collections.Look(ref trading, "tradingSessions", LookMode.Deep);

if (Scribe.mode == LoadSaveMode.PostLoadInit)
if (trading.RemoveAll(t => t.trader == null || t.playerNegotiator == null) > 0)
Log.Message("Some trading sessions had null entries");

DoBackCompat();
}

private void DoBackCompat()
{
if (Scribe.mode != LoadSaveMode.PostLoadInit)
return;

if (spectatorFaction == null)
{
if (spectatorFaction == null)
void AddSpectatorFaction()
{
spectatorFaction = HostUtil.AddNewFaction("Spectator", FactionDefOf.PlayerColony);

factionData[spectatorFaction.loadID] = FactionWorldData.New(spectatorFaction.loadID);
factionData[spectatorFaction.loadID].ReassignIds();

foreach (var map in Find.Maps)
MapSetup.InitNewFactionData(map, spectatorFaction);
}

if (trading.RemoveAll(t => t.trader == null || t.playerNegotiator == null) > 0)
Log.Message("Some trading sessions had null entries");
void RemoveOpponentFaction()
{
// Test faction left in by mistake in version 0.7
var opponent =
Find.FactionManager.AllFactions.FirstOrDefault(f => f.Name == "Opponent" && f.IsPlayer);

if (opponent is not null)
{
opponent.RemoveAllRelations();
Find.FactionManager.allFactions.Remove(opponent);
Log.Warning("Multiplayer removed dummy Opponent faction");
}
}

AddSpectatorFaction();
RemoveOpponentFaction();
}
}

Expand Down
59 changes: 47 additions & 12 deletions Source/Client/Factions/FactionCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@ public static void SendPawn(int sessionId, Pawn p)
}

[SyncMethod]
public static void CreateFaction(int sessionId, string factionName, int tile, string scenario, FactionRelationKind relation)
public static void CreateFaction(int sessionId, string factionName, int tile, string scenario, FactionRelationKind relation, ChooseIdeoInfo chooseIdeoInfo)
{
PrepareState(sessionId);

var self = TickPatch.currentExecutingCmdIssuedBySelf;

LongEventHandler.QueueLongEvent(delegate
{
int id = Find.UniqueIDsManager.GetNextFactionID();
var newFaction = NewFaction(id, factionName, FactionDefOf.PlayerColony);
int newFactionID = Find.UniqueIDsManager.GetNextFactionID();

var newFaction = NewFaction(
newFactionID,
factionName,
FactionDefOf.PlayerColony,
chooseIdeoInfo
);

newFaction.hidden = true;

Expand All @@ -44,6 +50,10 @@ public static void CreateFaction(int sessionId, string factionName, int tile, st
}

FactionContext.Push(newFaction);

foreach (var pawn in StartingPawnUtility.StartingAndOptionalPawns)
pawn.ideo.SetIdeo(newFaction.ideos.PrimaryIdeo);

var newMap = GenerateNewMap(tile, scenario);
FactionContext.Pop();

Expand Down Expand Up @@ -94,7 +104,7 @@ private static Map GenerateNewMap(int tile, string scenario)
Find.WorldObjects.Add(mapParent);

var prevScenario = Find.Scenario;
Current.Game.scenarioInt = ScenarioLister.AllScenarios().First(s => s.name == scenario);
Current.Game.scenarioInt = DefDatabase<ScenarioDef>.AllDefs.First(s => s.defName == scenario).scenario;

try
{
Expand All @@ -116,7 +126,7 @@ private static void InitNewGame()
ResearchUtility.ApplyPlayerStartingResearch();
}

public static void SetInitialInitData()
public static void SetGameInitData()
{
Current.Game.InitData = new GameInitData
{
Expand All @@ -127,7 +137,7 @@ public static void SetInitialInitData()

public static void PrepareState(int sessionId)
{
SetInitialInitData();
SetGameInitData();

if (pawnStore.TryGetValue(sessionId, out var pawns))
{
Expand All @@ -140,29 +150,54 @@ public static void PrepareState(int sessionId)
}
}

private static Faction NewFaction(int id, string name, FactionDef def)
private static Faction NewFaction(int id, string name, FactionDef def, ChooseIdeoInfo chooseIdeoInfo)
{
Faction faction = Find.FactionManager.AllFactions.FirstOrDefault(f => f.loadID == id);

if (faction == null)
{
faction = new Faction() { loadID = id, def = def };

faction = new Faction { loadID = id, def = def };
faction.ideos = new FactionIdeosTracker(faction);
faction.ideos.ChooseOrGenerateIdeo(new IdeoGenerationParms());

if (!ModsConfig.IdeologyActive || Find.IdeoManager.classicMode)
{
faction.ideos.SetPrimary(Faction.OfPlayer.ideos.PrimaryIdeo);
}
else
{
var newIdeo = GenerateIdeo(chooseIdeoInfo);
faction.ideos.SetPrimary(newIdeo);
Find.IdeoManager.Add(newIdeo);
}

foreach (Faction other in Find.FactionManager.AllFactionsListForReading)
faction.TryMakeInitialRelationsWith(other);

Find.FactionManager.Add(faction);

Multiplayer.WorldComp.factionData[faction.loadID] =
FactionWorldData.New(faction.loadID);
var newWorldFactionData = FactionWorldData.New(faction.loadID);
Multiplayer.WorldComp.factionData[faction.loadID] = newWorldFactionData;
newWorldFactionData.ReassignIds();
}

faction.Name = name;
faction.def = def;

return faction;
}

private static Ideo GenerateIdeo(ChooseIdeoInfo chooseIdeoInfo)
{
List<MemeDef> list = chooseIdeoInfo.SelectedIdeo.memes.ToList();

if (chooseIdeoInfo.SelectedStructure != null)
list.Add(chooseIdeoInfo.SelectedStructure);
else if (DefDatabase<MemeDef>.AllDefsListForReading.Where(m => m.category == MemeCategory.Structure && IdeoUtility.IsMemeAllowedFor(m, Find.Scenario.playerFaction.factionDef)).TryRandomElement(out var result))
list.Add(result);

Ideo ideo = IdeoGenerator.GenerateIdeo(new IdeoGenerationParms(Find.FactionManager.OfPlayer.def, forceNoExpansionIdeo: false, null, null, list, chooseIdeoInfo.SelectedIdeo.classicPlus, forceNoWeaponPreference: true));
new Page_ChooseIdeoPreset { selectedStyles = chooseIdeoInfo.SelectedStyles }.ApplySelectedStylesToIdeo(ideo);

return ideo;
}
}
46 changes: 39 additions & 7 deletions Source/Client/Factions/FactionSidebar.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Multiplayer.Client.Experimental;
using Multiplayer.Client.Factions;
using Multiplayer.Client.Util;
using Multiplayer.Common;
Expand Down Expand Up @@ -66,10 +68,28 @@ private static void DrawFactionCreator()
{
PreparePawns();

Find.WindowStack.Add(new Page_ConfigureStartingPawns
var pages = new List<Page>();
Page_ChooseIdeo_Multifaction chooseIdeoPage = null;

if (ModsConfig.IdeologyActive && !Find.IdeoManager.classicMode)
pages.Add(chooseIdeoPage = new Page_ChooseIdeo_Multifaction());

pages.Add(new Page_ConfigureStartingPawns
{
nextAct = DoCreateFaction
nextAct = () =>
{
DoCreateFaction(
new ChooseIdeoInfo(
chooseIdeoPage?.pageChooseIdeo.selectedIdeo,
chooseIdeoPage?.pageChooseIdeo.selectedStructure,
chooseIdeoPage?.pageChooseIdeo.selectedStyles
)
);
}
});

var page = PageUtility.StitchedPages(pages);
Find.WindowStack.Add(page);
}
}
}
Expand All @@ -81,7 +101,7 @@ private static void PreparePawns()

try
{
FactionCreator.SetInitialInitData();
FactionCreator.SetGameInitData();

// Create starting pawns
new ScenPart_ConfigPage_ConfigureStartingPawns { pawnCount = Current.Game.InitData.startingPawnCount }
Expand All @@ -93,7 +113,7 @@ private static void PreparePawns()
}
}

private static void DoCreateFaction()
private static void DoCreateFaction(ChooseIdeoInfo chooseIdeoInfo)
{
int sessionId = Multiplayer.session.playerId;
var prevState = Current.programStateInt;
Expand All @@ -107,8 +127,14 @@ private static void DoCreateFaction()
p
);

FactionCreator.CreateFaction(sessionId, factionName, Find.WorldInterface.SelectedTile,
scenario, hostility);
FactionCreator.CreateFaction(
sessionId,
factionName,
Find.WorldInterface.SelectedTile,
scenario,
hostility,
chooseIdeoInfo
);
}
finally
{
Expand Down Expand Up @@ -172,3 +198,9 @@ public static bool Button(string text, float width, float height = 35f)
return Widgets.ButtonText(Layouter.Rect(width, height), text);
}
}

public record ChooseIdeoInfo(
IdeoPresetDef SelectedIdeo,
MemeDef SelectedStructure,
List<StyleCategoryDef> SelectedStyles
) : ISyncSimple;
7 changes: 5 additions & 2 deletions Source/Client/Factions/MultifactionPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ static IEnumerable<Gizmo> Postfix(IEnumerable<Gizmo> gizmos, Pawn __instance)
foreach (var gizmo in gizmos)
yield return gizmo;

if (__instance.Faction is { IsPlayer: true } && __instance.Faction != Faction.OfPlayer)
if (Multiplayer.Client == null || Multiplayer.RealPlayerFaction == Multiplayer.WorldComp.spectatorFaction)
yield break;

if (__instance.Faction is { IsPlayer: true } &&__instance.Faction != Faction.OfPlayer)
{
var otherFaction = __instance.Faction;

yield return new Command_Action()
yield return new Command_Action
{
defaultLabel = "Change faction relation",
icon = MultiplayerStatic.ChangeRelationIcon,
Expand Down
49 changes: 49 additions & 0 deletions Source/Client/Factions/Page_ChooseIdeo_Multifaction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Linq;
using RimWorld;
using UnityEngine;
using Verse;

namespace Multiplayer.Client.Factions;

public class Page_ChooseIdeo_Multifaction : Page
{
public override string PageTitle => "ChooseYourIdeoligion".Translate();

public Page_ChooseIdeoPreset pageChooseIdeo = new();

public override void DoWindowContents(Rect inRect)
{
DrawPageTitle(inRect);
float totalHeight = 0f;
Rect mainRect = GetMainRect(inRect);
TaggedString descText = "ChooseYourIdeoligionDesc".Translate();
float descHeight = Text.CalcHeight(descText, mainRect.width);
Rect descRect = mainRect;
descRect.yMin += totalHeight;
Widgets.Label(descRect, descText);
totalHeight += descHeight + 10f;

pageChooseIdeo.DrawStructureAndStyleSelection(inRect);

Rect outRect = mainRect;
outRect.width = 954f;
outRect.yMin += totalHeight;
float num3 = (InitialSize.x - 937f) / 2f;

Widgets.BeginScrollView(
viewRect: new Rect(0f - num3, 0f, 921f, pageChooseIdeo.totalCategoryListHeight + 100f),
outRect: outRect,
scrollPosition: ref pageChooseIdeo.leftScrollPosition);

totalHeight = 0f;
pageChooseIdeo.lastCategoryGroupLabel = "";
foreach (IdeoPresetCategoryDef item in DefDatabase<IdeoPresetCategoryDef>.AllDefsListForReading.Where(c => c != IdeoPresetCategoryDefOf.Classic && c != IdeoPresetCategoryDefOf.Custom && c != IdeoPresetCategoryDefOf.Fluid))
{
pageChooseIdeo.DrawCategory(item, ref totalHeight);
}
pageChooseIdeo.totalCategoryListHeight = totalHeight;
Widgets.EndScrollView();

DoBottomButtons(inRect);
}
}
7 changes: 4 additions & 3 deletions Source/Client/Factions/SidebarPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ static class UIRootPrefix
{
static void Postfix()
{
// if (Multiplayer.Client != null && Multiplayer.RealPlayerFaction != null && Find.FactionManager != null)
// Find.FactionManager.ofPlayer = Multiplayer.RealPlayerFaction;
if (Multiplayer.Client != null && Multiplayer.RealPlayerFaction != null && Find.FactionManager != null)
Find.FactionManager.ofPlayer = Multiplayer.RealPlayerFaction;

Layouter.BeginFrame();

if (Multiplayer.Client != null &&
Multiplayer.RealPlayerFaction == Multiplayer.WorldComp.spectatorFaction &&
!TickPatch.Simulating &&
Find.WindowStack.WindowOfType<Page_ConfigureStartingPawns>() == null
!Multiplayer.IsReplay &&
Find.WindowStack.WindowOfType<Page>() == null
)
Find.WindowStack.ImmediateWindow(
"MpWindowFaction".GetHashCode(),
Expand Down
Loading

0 comments on commit b52e372

Please sign in to comment.