Skip to content

Commit

Permalink
No closing when points negative, add portrait export
Browse files Browse the repository at this point in the history
  • Loading branch information
legodude17 committed Oct 14, 2023
1 parent b0316cf commit 45b10c5
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 97 deletions.
Binary file modified Assemblies/PawnEditor.dll
Binary file not shown.
2 changes: 2 additions & 0 deletions Languages/English/Keyed/Settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
<PawnEditor.PointLimit>Point limit</PawnEditor.PointLimit>
<PawnEditor.UseSilver>Use silver for points</PawnEditor.UseSilver>
<PawnEditor.UseSilver.Desc>Point limit will be amount of silver in the current colony, and usage of points will deduct silver.</PawnEditor.UseSilver.Desc>
<PawnEditor.CountNPCs>Count NPC pawns in points</PawnEditor.CountNPCs>
<PawnEditor.CountNPCs.Desc>If enabled, then NPC pawns will also impact your point limit.</PawnEditor.CountNPCs.Desc>
</LanguageData>
3 changes: 3 additions & 0 deletions Languages/English/Keyed/UI.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@
<PawnEditor.Teleport.Specific>Teleport to exact location</PawnEditor.Teleport.Specific>
<PawnEditor.AdditionalInfo>Additional info</PawnEditor.AdditionalInfo>
<PawnEditor.NotEnoughPoints>Not enough points. Need {0}.</PawnEditor.NotEnoughPoints>
<PawnEditor.ExportImage>Export portrait</PawnEditor.ExportImage>
<PawnEditor.ImageExported>Exported portrait of {0} to {1}.</PawnEditor.ImageExported>
<PawnEditor.NegativePoints>Remaining points is negative.</PawnEditor.NegativePoints>

<!-- Quick Actions -->
<PawnEditor.RepairAll>Repair all</PawnEditor.RepairAll>
Expand Down
6 changes: 6 additions & 0 deletions Source/PawnEditor/Dialogs/Dialog_PawnEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public override void PreOpen()
base.PreOpen();
}

public override void OnCancelKeyPressed()
{
if (PawnEditor.CanExit())
base.OnCancelKeyPressed();
}

public override void PostClose()
{
base.PostClose();
Expand Down
1 change: 1 addition & 0 deletions Source/PawnEditor/PawnEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
<Compile Include="SaveLoad\SaveLoadItem.cs" />
<Compile Include="SaveLoad\SaveLoadPatches.cs" />
<Compile Include="SaveLoad\SaveLoadUtility.cs" />
<Compile Include="Utils\PointsSystem.cs" />
<Compile Include="Utils\StartingThingsManager.cs" />
<Compile Include="UI\TexPawnEditor.cs" />
<Compile Include="Utils\TFilter.cs" />
Expand Down
3 changes: 3 additions & 0 deletions Source/PawnEditor/PawnEditorMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public override void DoSettingsWindowContents(Rect inRect)
listing.Label("PawnEditor.PointLimit".Translate() + ": " + Settings.PointLimit.ToStringMoney());
Settings.PointLimit = listing.Slider(Settings.PointLimit, 100, 1000000000);
listing.CheckboxLabeled("PawnEditor.UseSilver".Translate(), ref Settings.UseSilver, "PawnEditor.UseSilver.Desc".Translate());
listing.CheckboxLabeled("PawnEditor.CountNPCs".Translate(), ref Settings.CountNPCs, "PawnEditor.CountNPCs.Desc".Translate());
listing.End();
}

Expand Down Expand Up @@ -150,6 +151,7 @@ public static void Notify_ConfigurePawns()

public class PawnEditorSettings : ModSettings
{
public bool CountNPCs;
public bool InGameDevButton = true;
public bool OverrideVanilla = true;
public float PointLimit = 100000;
Expand All @@ -162,6 +164,7 @@ public override void ExposeData()
Scribe_Values.Look(ref InGameDevButton, nameof(InGameDevButton), true);
Scribe_Values.Look(ref PointLimit, nameof(PointLimit));
Scribe_Values.Look(ref UseSilver, nameof(UseSilver));
Scribe_Values.Look(ref CountNPCs, nameof(CountNPCs));
}
}

Expand Down
11 changes: 11 additions & 0 deletions Source/PawnEditor/SaveLoad/SaveLoadItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ private void Load()
}
}

public class SaveItem : SaveLoadItem
{
private readonly FloatMenuOption option;

public SaveItem(FloatMenuOption option) => this.option = option;
public SaveItem(string label, Action action) => option = new(label, action);

public override FloatMenuOption MakeSaveOption() => option;
public override FloatMenuOption MakeLoadOption() => null;
}

public struct SaveLoadParms<T> where T : IExposable
{
public Action<T> OnSave;
Expand Down
10 changes: 10 additions & 0 deletions Source/PawnEditor/Tabs/Humanlike/Bio/TabWorker_Bio_Humanlike.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using RimWorld;
using UnityEngine;
Expand Down Expand Up @@ -39,6 +40,15 @@ public override IEnumerable<SaveLoadItem> GetSaveLoadItems(Pawn pawn)
{
OnLoad = info => info.CopyTo(pawn)
});
yield return new SaveItem("PawnEditor.ExportImage".Translate(), () =>
{
var text = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
if (text.NullOrEmpty()) text = GenFilePaths.SaveDataFolderPath;

text = Path.Combine(text, pawn.Name.ToStringShort + ".png");
PawnEditor.SavePawnTex(pawn, text);
Messages.Message("PawnEditor.ImageExported".Translate(pawn.Name.ToStringFull, text), MessageTypeDefOf.TaskCompletion, false);
});
}

private void DoSkills(Rect inRect, Pawn pawn)
Expand Down
122 changes: 25 additions & 97 deletions Source/PawnEditor/UI/PawnEditorUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,30 +120,49 @@ public static void DoUI(Rect inRect, Action onClose, Action onNext)
public static void DoBottomButtons(Rect inRect, Action onLeftButton, Action onRightButton)
{
Text.Font = GameFont.Small;
if (Widgets.ButtonText(inRect.TakeLeftPart(Page.BottomButSize.x), Pregame ? "Back".Translate() : "Close".Translate())) onLeftButton();
if (Widgets.ButtonText(inRect.TakeLeftPart(Page.BottomButSize.x), Pregame ? "Back".Translate() : "Close".Translate()) && CanExit()) onLeftButton();

if (Widgets.ButtonText(inRect.TakeRightPart(Page.BottomButSize.x), Pregame ? "Start".Translate() : "PawnEditor.Teleport".Translate())) onRightButton();
if (Widgets.ButtonText(inRect.TakeRightPart(Page.BottomButSize.x), Pregame ? "Start".Translate() : "PawnEditor.Teleport".Translate())
&& CanExit()) onRightButton();

var randomRect = new Rect(Vector2.zero, Page.BottomButSize).CenteredOnXIn(inRect).CenteredOnYIn(inRect);

var buttonRect = new Rect(randomRect);

if (lastRandomization != null && Widgets.ButtonImageWithBG(randomRect.TakeRightPart(20), TexUI.RotRightTex, new Vector2(12, 12)))
{
lastRandomization.action();

randomRect.TakeRightPart(1);
randomRect.TakeRightPart(1);
}

if (Widgets.ButtonText(randomRect, "Randomize".Translate())) Find.WindowStack.Add(new FloatMenu(GetRandomizationOptions().ToList()));

buttonRect.x -= 5 + buttonRect.width;

if (Widgets.ButtonText(buttonRect, "Save".Translate()))
Find.WindowStack.Add(new FloatMenu(GetSaveLoadItems().Select(item => item.MakeSaveOption()).ToList()));
Find.WindowStack.Add(new FloatMenu(GetSaveLoadItems()
.Select(static item => item.MakeSaveOption())
.Where(static option => option != null)
.ToList()));

buttonRect.x += buttonRect.width * 2 + 10;

if (Widgets.ButtonText(buttonRect, "Load".Translate()))
Find.WindowStack.Add(new FloatMenu(GetSaveLoadItems().Select(item => item.MakeLoadOption()).ToList()));
Find.WindowStack.Add(new FloatMenu(GetSaveLoadItems()
.Select(static item => item.MakeLoadOption())
.Where(static option => option != null)
.ToList()));
}

public static bool CanExit()
{
if (Pregame && usePointLimit && remainingPoints < 0)
{
Messages.Message("PawnEditor.NegativePoints".Translate(), MessageTypeDefOf.RejectInput, false);
return false;
}

return true;
}

private static IEnumerable<SaveLoadItem> GetSaveLoadItems()
Expand Down Expand Up @@ -219,97 +238,6 @@ public static void RecachePawnList()
PortraitsCache.Clear();
}

public static void ResetPoints()
{
remainingPoints = PawnEditorMod.Settings.PointLimit;
cachedValue = 0;
if (!Pregame && PawnEditorMod.Settings.UseSilver)
{
startingSilver = ColonyInventory.AllItemsInInventory().Sum(static t => t.def == ThingDefOf.Silver ? t.stackCount : 0);
remainingPoints = startingSilver;
}

Notify_PointsUsed();
}

public static void ApplyPoints()
{
var amount = remainingPoints - startingSilver;
if (amount > 0)
{
var pos = ColonyInventory.AllItemsInInventory().FirstOrDefault(static t => t.def == ThingDefOf.Silver).Position;
var silver = ThingMaker.MakeThing(ThingDefOf.Silver);
silver.stackCount = Mathf.RoundToInt(amount);
GenPlace.TryPlaceThing(silver, pos, Find.CurrentMap, ThingPlaceMode.Near);
}
else if (amount < 0)
{
amount = -amount;
foreach (var thing in ColonyInventory.AllItemsInInventory().Where(static t => t.def == ThingDefOf.Silver))
{
var toRemove = Math.Min(thing.stackCount, Mathf.RoundToInt(amount));
thing.stackCount -= toRemove;
amount -= toRemove;

if (thing.stackCount <= 0) thing.Destroy();
if (amount < 1f) break;
}
}
}

public static bool CanUsePoints(float amount)
{
if (!usePointLimit) return true;
if (remainingPoints >= amount) return true;
Messages.Message("PawnEditor.NotEnoughPoints".Translate(amount), MessageTypeDefOf.RejectInput, false);
return false;
}

public static bool CanUsePoints(Thing thing) => CanUsePoints(GetThingValue(thing));
public static bool CanUsePoints(Pawn pawn) => CanUsePoints(GetPawnValue(pawn));

public static void Notify_PointsUsed(float? amount = null)
{
if (amount.HasValue)
remainingPoints -= amount.Value;
else
{
var value = 0f;
if (Pregame)
{
value += ValueOfPawns(Find.GameInitData.startingAndOptionalPawns);
value += ValueOfPawns(StartingThingsManager.GetPawns(PawnCategory.Animals));
value += ValueOfPawns(StartingThingsManager.GetPawns(PawnCategory.Mechs));
value += ValueOfThings(StartingThingsManager.GetStartingThingsNear());
value += ValueOfThings(StartingThingsManager.GetStartingThingsFar());
}
else
{
AllPawns.UpdateCache(null, PawnCategory.All);
value += ValueOfPawns(AllPawns.GetList());
value += ValueOfThings(ColonyInventory.AllItemsInInventory());
}


remainingPoints -= value - cachedValue;
cachedValue = value;
}
}

private static float ValueOfPawns(IEnumerable<Pawn> pawns) => pawns.Sum(GetPawnValue);
private static float ValueOfThings(IEnumerable<Thing> things) => things.Sum(GetThingValue);
private static float GetThingValue(Thing thing) => thing.MarketValue * thing.stackCount;

private static float GetPawnValue(Pawn pawn)
{
var num = pawn.MarketValue;
if (pawn.apparel != null)
num += pawn.apparel.WornApparel.Sum(t => t.MarketValue);
if (pawn.equipment != null)
num += pawn.equipment.AllEquipmentListForReading.Sum(t => t.MarketValue);
return num;
}

private static void SetTabGroup(TabGroupDef def)
{
tabGroup = def;
Expand Down
102 changes: 102 additions & 0 deletions Source/PawnEditor/Utils/PointsSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Linq;
using RimWorld;
using UnityEngine;
using Verse;

namespace PawnEditor;

public partial class PawnEditor
{
public static void ResetPoints()
{
remainingPoints = PawnEditorMod.Settings.PointLimit;
cachedValue = 0;
if (!Pregame && PawnEditorMod.Settings.UseSilver)
{
startingSilver = ColonyInventory.AllItemsInInventory().Sum(static t => t.def == ThingDefOf.Silver ? t.stackCount : 0);
remainingPoints = startingSilver;
}

Notify_PointsUsed();
}

public static void ApplyPoints()
{
var amount = remainingPoints - startingSilver;
if (amount > 0)
{
var pos = ColonyInventory.AllItemsInInventory().FirstOrDefault(static t => t.def == ThingDefOf.Silver).Position;
var silver = ThingMaker.MakeThing(ThingDefOf.Silver);
silver.stackCount = Mathf.RoundToInt(amount);
GenPlace.TryPlaceThing(silver, pos, Find.CurrentMap, ThingPlaceMode.Near);
}
else if (amount < 0)
{
amount = -amount;
foreach (var thing in ColonyInventory.AllItemsInInventory().Where(static t => t.def == ThingDefOf.Silver))
{
var toRemove = Math.Min(thing.stackCount, Mathf.RoundToInt(amount));
thing.stackCount -= toRemove;
amount -= toRemove;

if (thing.stackCount <= 0) thing.Destroy();
if (amount < 1f) break;
}
}
}

public static bool CanUsePoints(float amount)
{
if (!usePointLimit) return true;
if (remainingPoints >= amount) return true;
Messages.Message("PawnEditor.NotEnoughPoints".Translate(amount), MessageTypeDefOf.RejectInput, false);
return false;
}

public static bool CanUsePoints(Thing thing) => CanUsePoints(GetThingValue(thing));
public static bool CanUsePoints(Pawn pawn) => CanUsePoints(GetPawnValue(pawn));

public static void Notify_PointsUsed(float? amount = null)
{
if (amount.HasValue)
remainingPoints -= amount.Value;
else
{
var value = 0f;
if (Pregame)
{
value += ValueOfPawns(Find.GameInitData.startingAndOptionalPawns);
value += ValueOfPawns(StartingThingsManager.GetPawns(PawnCategory.Animals));
value += ValueOfPawns(StartingThingsManager.GetPawns(PawnCategory.Mechs));
value += ValueOfThings(StartingThingsManager.GetStartingThingsNear());
value += ValueOfThings(StartingThingsManager.GetStartingThingsFar());
}
else
{
AllPawns.UpdateCache(PawnEditorMod.Settings.CountNPCs ? null : Faction.OfPlayer, PawnCategory.All);
value += ValueOfPawns(AllPawns.GetList());
value += ValueOfThings(ColonyInventory.AllItemsInInventory());
}


remainingPoints -= value - cachedValue;
cachedValue = value;
}
}

private static float ValueOfPawns(IEnumerable<Pawn> pawns) => pawns.Sum(GetPawnValue);
private static float ValueOfThings(IEnumerable<Thing> things) => things.Sum(GetThingValue);
private static float GetThingValue(Thing thing) => thing.MarketValue * thing.stackCount;

private static float GetPawnValue(Pawn pawn)
{
var num = pawn.MarketValue;
if (pawn.apparel != null)
num += pawn.apparel.WornApparel.Sum(t => t.MarketValue);
if (pawn.equipment != null)
num += pawn.equipment.AllEquipmentListForReading.Sum(t => t.MarketValue);
return num;
}
}

0 comments on commit 45b10c5

Please sign in to comment.