Skip to content

Commit

Permalink
Merge pull request #29 from maarxx/add_psycasts_squashed
Browse files Browse the repository at this point in the history
Add Psycasting! Columns! Bars! Icons! A Whole Preset!
  • Loading branch information
Mehni authored Dec 12, 2020
2 parents 85d6093 + 481c8c8 commit 6c1c3c2
Show file tree
Hide file tree
Showing 13 changed files with 271 additions and 7 deletions.
32 changes: 31 additions & 1 deletion 1.1/Defs/PawnColumnDef/PawnColumns_Numbers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,36 @@
<sortable>true</sortable>
<label>Title</label>
</PawnColumnDef>

<PawnColumnDef>
<defName>Numbers_Psyfocus</defName>
<workerClass>Numbers.PawnColumnWorker_Psyfocus</workerClass>
<sortable>true</sortable>
<label>Psyfocus</label>
<modExtensions>
<li Class="Numbers.DefModExtension_NeedsRoyalty"/>
</modExtensions>
</PawnColumnDef>

<PawnColumnDef>
<defName>Numbers_Entropy</defName>
<workerClass>Numbers.PawnColumnWorker_Entropy</workerClass>
<sortable>true</sortable>
<label>Heat</label>
<modExtensions>
<li Class="Numbers.DefModExtension_NeedsRoyalty"/>
</modExtensions>
</PawnColumnDef>

<PawnColumnDef>
<defName>Numbers_PsylinkLevel</defName>
<workerClass>Numbers.PawnColumnWorker_PsylinkLevel</workerClass>
<sortable>true</sortable>
<label>Psylevel</label>
<modExtensions>
<li Class="Numbers.DefModExtension_NeedsRoyalty"/>
</modExtensions>
</PawnColumnDef>

<PawnColumnDef>
<defName>Numbers_Equipment</defName>
Expand Down Expand Up @@ -217,4 +247,4 @@
<label>Focus</label>
</PawnColumnDef>

</Defs>
</Defs>
1 change: 1 addition & 0 deletions Languages/English/Keyed/Keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<Numbers_Presets.Combat>combat</Numbers_Presets.Combat>
<Numbers_Presets.WorkTabPlus>worktab plus</Numbers_Presets.WorkTabPlus>
<Numbers_Presets.ColonistNeeds>colonist needs</Numbers_Presets.ColonistNeeds>
<Numbers_Presets.Psycasting>psycasting</Numbers_Presets.Psycasting>

<Numbers_SetAsDefault>Set current view as default</Numbers_SetAsDefault>
<Numbers_SetAsDefaultExplanation>Set the current view as the default for this type of view ({0}).\n\nThis view will then be used for new colonies.\n\nCan be deleted in the mod settings:\n [Menu] => [Options] => [Mod settings] => [Numbers].</Numbers_SetAsDefaultExplanation>
Expand Down
10 changes: 10 additions & 0 deletions Numbers/DefModExtension_NeedsRoyalty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Numbers
{
using RimWorld;
using Verse;

public class DefModExtension_NeedsRoyalty : DefModExtension
{
//nothing needed, just a tag
}
}
1 change: 1 addition & 0 deletions Numbers/DefModExtension_PawnColumnDefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public class DefModExtension_PawnColumnDefs : DefModExtension
public NeedDef need;
public StatDef stat;
public SkillDef skill;
public AbilityDef ability;
}
}
6 changes: 6 additions & 0 deletions Numbers/MainTabWindow_Numbers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ public override void DoWindowContents(Rect rect)
DoButton("TabHealth".Translate(), tmp.ToList(), ref x);
}

//abilities btn
if (ModLister.RoyaltyInstalled)
{
DoButton("Abilities".Translate(), optionsMaker.OptionsMakerForGenericDef(DefDatabase<AbilityDef>.AllDefsListForReading.OrderBy(y => y.label)), ref x);
}

//records btn
DoButton("TabRecords".Translate(), optionsMaker.OptionsMakerForGenericDef(DefDatabase<RecordDef>.AllDefsListForReading), ref x);

Expand Down
6 changes: 6 additions & 0 deletions Numbers/Numbers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ private static IEnumerable<PawnColumnDef> ImpliedPawnColumnDefs()
.Concat(DefDatabase<StatDef>
.AllDefsListForReading.Select(GenerateNewPawnColumnDefFor))
.Concat(DefDatabase<SkillDef>
.AllDefsListForReading.Select(GenerateNewPawnColumnDefFor))
.Concat(DefDatabase<AbilityDef>
.AllDefsListForReading.Select(GenerateNewPawnColumnDefFor));

private static PawnColumnDef GenerateNewPawnColumnDefFor(Def def)
Expand Down Expand Up @@ -390,6 +392,10 @@ private static PawnColumnDef GenerateNewPawnColumnDefFor(Def def)
pcd.workerClass = typeof(PawnColumnWorker_Skill);
pcd.GetModExtension<DefModExtension_PawnColumnDefs>().skill = (SkillDef)def;
break;
case AbilityDef _:
pcd.workerClass = typeof(PawnColumnWorker_Ability);
pcd.GetModExtension<DefModExtension_PawnColumnDefs>().ability = (AbilityDef)def;
break;
default:
throw new ArgumentException($"Unsupported Def of type {def.GetType()}");
}
Expand Down
4 changes: 2 additions & 2 deletions Numbers/Numbers_Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public static bool IsWildAnimal(this Pawn p)
=> p.Faction == null
&& p.AnimalOrWildMan();

public static DefModExtension_PawnColumnDefs Ext(this PawnColumnDef def)
public static DefModExtension_PawnColumnDefs Ext(this PawnColumnDef def, bool throwError = true)
{
if (!def.HasModExtension<DefModExtension_PawnColumnDefs>())
if (throwError && !def.HasModExtension<DefModExtension_PawnColumnDefs>())
{
Log.Error("Numbers expected DefModExtension PawnColumnDefs, got null");
return null;
Expand Down
16 changes: 13 additions & 3 deletions Numbers/OptionsMaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ private static IEnumerable<PawnColumnDef> MainTable
=> new[] { DefDatabase<PawnColumnDef>.GetNamedSilentFail("Numbers_Inspiration"),
DefDatabase<PawnColumnDef>.GetNamedSilentFail("Numbers_Inventory"),
DefDatabase<PawnColumnDef>.GetNamedSilentFail("Numbers_SelfTend"),
DefDatabase<PawnColumnDef>.GetNamedSilentFail("Numbers_Meditation"), }
.Concat(DefDatabase<PawnTableDef>.GetNamed("Assign").columns
.Concat(DefDatabase<PawnTableDef>.GetNamed("Restrict").columns).Where(x => pcdValidator(x)));
DefDatabase<PawnColumnDef>.GetNamedSilentFail("Numbers_Meditation"),
DefDatabase<PawnColumnDef>.GetNamedSilentFail("Numbers_Psyfocus"),
DefDatabase<PawnColumnDef>.GetNamedSilentFail("Numbers_Entropy"),
DefDatabase<PawnColumnDef>.GetNamedSilentFail("Numbers_PsylinkLevel") }
.Concat(DefDatabase<PawnTableDef>.GetNamed("Assign").columns)
.Concat(DefDatabase<PawnTableDef>.GetNamed("Restrict").columns).Where(x => pcdValidator(x) && filterRoyalty(x));

private static readonly Func<PawnColumnDef, bool> filterRoyalty = pcd => ModLister.RoyaltyInstalled || !pcd.HasModExtension<DefModExtension_NeedsRoyalty>();

private static IEnumerable<PawnColumnDef> WildAnimals
=> new[] { DefDatabase<PawnColumnDef>.GetNamedSilentFail("Numbers_Wildness"),
Expand All @@ -79,11 +84,16 @@ public List<FloatMenuOption> PresetOptionsMaker()
new FloatMenuOption("Numbers_Presets.Load".Translate("Numbers_Presets.Combat".Translate()), () => ChangeMainTableTo(StaticConstructorOnGameStart.combatPreset)),
new FloatMenuOption("Numbers_Presets.Load".Translate("Numbers_Presets.WorkTabPlus".Translate()), () => ChangeMainTableTo(StaticConstructorOnGameStart.workTabPlusPreset)),
new FloatMenuOption("Numbers_Presets.Load".Translate("Numbers_Presets.ColonistNeeds".Translate()), () => ChangeMainTableTo(StaticConstructorOnGameStart.colonistNeedsPreset)),
//maybe Psycasting here, index 6, referenced below
new FloatMenuOption("Numbers_SetAsDefault".Translate(), SetAsDefault,
extraPartWidth: 29f,
extraPartOnGUI: rect => Numbers_Utility.InfoCardButton(rect.x + 5f, rect.y + (rect.height - 24f) / 2, "Numbers_SetAsDefaultExplanation".Translate(PawnTable.LabelCap))),
new FloatMenuOption("Numbers_LoadDefault".Translate(), LoadDefault)
};
if (ModLister.RoyaltyInstalled)
{
list.Insert(6, new FloatMenuOption("Numbers_Presets.Load".Translate("Numbers_Presets.Psycasting".Translate()), () => ChangeMainTableTo(StaticConstructorOnGameStart.psycastingPreset)));
}

return list;
}
Expand Down
53 changes: 53 additions & 0 deletions Numbers/PawnColumnWorkers/PawnColumnWorker_Ability.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace Numbers
{
using RimWorld;
using UnityEngine;
using Verse;

[StaticConstructorOnStartup]
public class PawnColumnWorker_Ability : PawnColumnWorker_Icon
{

protected override Texture2D GetIconFor(Pawn pawn)
{
AbilityDef abilityDef = def.Ext().ability;
foreach (Ability a in pawn.abilities.abilities)
{
if (a.def == abilityDef)
{
return abilityDef.uiIcon;
}
}
return null;
}

public override int GetMinWidth(PawnTable table)
{
return 26;
}

protected override string GetHeaderTip(PawnTable table) => def.Ext().ability.GetTooltip() + "\n\n" + "Numbers_ColumnHeader_Tooltip".Translate();

public override void DoHeader(Rect rect, PawnTable table)
{
Rect interactableHeaderRect = GetInteractableHeaderRect(rect, table);
if (Mouse.IsOver(interactableHeaderRect))
{
Widgets.DrawHighlight(interactableHeaderRect);
string headerTip = GetHeaderTip(table);
if (!headerTip.NullOrEmpty())
{
TooltipHandler.TipRegion(interactableHeaderRect, headerTip);
}
}
if (Widgets.ButtonInvisible(interactableHeaderRect))
{
HeaderClicked(rect, table);
}

Texture2D abilityIcon = def.Ext().ability.uiIcon;
Rect position = new Rect(rect.x, rect.yMax - 26, 26, 26);
GUI.DrawTexture(position, abilityIcon);
}
}
}
50 changes: 50 additions & 0 deletions Numbers/PawnColumnWorkers/PawnColumnWorker_Entropy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace Numbers
{
using System.Collections.Generic;
using System.Reflection;
using RimWorld;
using UnityEngine;
using Verse;

[StaticConstructorOnStartup]
public class PawnColumnWorker_Entropy : PawnColumnWorker
{
private static readonly Texture2D EntropyBarTex = SolidColorMaterials.NewSolidColorTexture(new Color(0.46f, 0.34f, 0.35f));

//mostly from PawnColumnWorker_Need

public override void DoCell(Rect rect, Pawn pawn, PawnTable table)
{
if (!pawn.HasPsylink)
return;

float curEntropyLevel = pawn.psychicEntropy.EntropyRelativeValue;

float barHeight = 14f;
float barWidth = barHeight + 15f;
if (rect.height < 50f)
{
barHeight *= Mathf.InverseLerp(0f, 50f, rect.height);
}

Text.Font = (rect.height <= 55f) ? GameFont.Tiny : GameFont.Small;
Text.Anchor = TextAnchor.UpperLeft;
Rect rect3 = new Rect(rect.x, rect.y + rect.height / 2f, rect.width, rect.height / 2f);
rect3 = new Rect(rect3.x + barWidth, rect3.y, rect3.width - barWidth * 2f, rect3.height - barHeight);

Widgets.FillableBar(rect3, curEntropyLevel, EntropyBarTex);

Text.Font = GameFont.Small;
}

public override int GetMinWidth(PawnTable table)
=> Mathf.Max(base.GetMinWidth(table), 110);

public override int Compare(Pawn a, Pawn b)
{
int hasPsylink = a.HasPsylink.CompareTo(b.HasPsylink);
if (hasPsylink != 0) { return hasPsylink; }
return (a.psychicEntropy?.EntropyRelativeValue ?? 0f).CompareTo(b.psychicEntropy?.EntropyRelativeValue ?? 0f);
}
}
}
61 changes: 61 additions & 0 deletions Numbers/PawnColumnWorkers/PawnColumnWorker_Psyfocus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
namespace Numbers
{
using System.Collections.Generic;
using System.Reflection;
using RimWorld;
using UnityEngine;
using Verse;

public class PawnColumnWorker_Psyfocus : PawnColumnWorker
{
//mostly from PawnColumnWorker_Need

public override void DoCell(Rect rect, Pawn pawn, PawnTable table)
{
if (!pawn.HasPsylink)
return;

float curPsyfocusLevel = pawn.psychicEntropy.CurrentPsyfocus;
float targetPsyfocusLevel = pawn.psychicEntropy.TargetPsyfocus;

float barHeight = 14f;
float barWidth = barHeight + 15f;
if (rect.height < 50f)
{
barHeight *= Mathf.InverseLerp(0f, 50f, rect.height);
}

Text.Font = (rect.height <= 55f) ? GameFont.Tiny : GameFont.Small;
Text.Anchor = TextAnchor.UpperLeft;
Rect rect3 = new Rect(rect.x, rect.y + rect.height / 2f, rect.width, rect.height / 2f);
rect3 = new Rect(rect3.x + barWidth, rect3.y, rect3.width - barWidth * 2f, rect3.height - barHeight);

Widgets.FillableBar(rect3, curPsyfocusLevel);

DrawPsyfocusTargetMarkerAt(rect3, targetPsyfocusLevel);
Text.Font = GameFont.Small;
}

private void DrawPsyfocusTargetMarkerAt(Rect barRect, float pct)
{
float seekerSize = 12f;
if (barRect.width < 150f)
{
seekerSize /= 2f;
}
Vector2 vector = new Vector2(barRect.x + barRect.width * pct, barRect.y + barRect.height);
Rect position = new Rect(vector.x - seekerSize / 2f, vector.y, seekerSize, seekerSize);
GUI.DrawTexture(position, StaticConstructorOnGameStart.BarInstantMarkerTex);
}

public override int GetMinWidth(PawnTable table)
=> Mathf.Max(base.GetMinWidth(table), 110);

public override int Compare(Pawn a, Pawn b)
{
int hasPsylink = a.HasPsylink.CompareTo(b.HasPsylink);
if (hasPsylink != 0) { return hasPsylink; }
return (a.psychicEntropy?.CurrentPsyfocus ?? 0f).CompareTo(b.psychicEntropy?.CurrentPsyfocus ?? 0f);
}
}
}
18 changes: 18 additions & 0 deletions Numbers/PawnColumnWorkers/PawnColumnWorker_PsylinkLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Numbers
{
using System;
using RimWorld;
using Verse;

public class PawnColumnWorker_PsylinkLevel : PawnColumnWorker_Text
{
protected override string GetTextFor(Pawn pawn)
=> (pawn.psychicEntropy?.Psylink?.level ?? 0).ToString();

public override int Compare(Pawn a, Pawn b)
=> (a.psychicEntropy?.Psylink?.level ?? 0).CompareTo((b.psychicEntropy?.Psylink?.level ?? 0));

public override int GetMinWidth(PawnTable table)
=> base.GetMinWidth(table) + 10;
}
}
20 changes: 19 additions & 1 deletion Numbers/StaticConstructorOnGameStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ static class StaticConstructorOnGameStart
public static List<PawnColumnDef> combatPreset = new List<PawnColumnDef>(),
workTabPlusPreset = new List<PawnColumnDef>(),
colonistNeedsPreset = new List<PawnColumnDef>(),
psycastingPreset = new List<PawnColumnDef>(),
medicalPreset = new List<PawnColumnDef>();

public static Type animalTab;
Expand Down Expand Up @@ -79,7 +80,8 @@ private static void AddHeadersToPawnColumns()
&& x.defName.StartsWith("Numbers_")
&& !(x.Worker is PawnColumnWorker_AllHediffs
|| x.Worker is PawnColumnWorker_SelfTend
|| x.Worker is PawnColumnWorker_ManhunterOnTameFailChance))) //special treatment for those.
|| x.Worker is PawnColumnWorker_ManhunterOnTameFailChance
|| x.Worker is PawnColumnWorker_Ability))) //special treatment for those.
{
pawnColumnDef.headerTip += (pawnColumnDef.headerTip.NullOrEmpty() ? "" : "\n\n") + "Numbers_ColumnHeader_Tooltip".Translate();
}
Expand All @@ -90,9 +92,25 @@ private static void PopulatePresets()
combatPreset.AddRange(DefDatabase<PawnTableDef>.GetNamed("Numbers_CombatPreset").columns);
workTabPlusPreset.AddRange(DefDatabase<PawnTableDef>.GetNamed("Numbers_WorkTabPlusPreset").columns);
colonistNeedsPreset.AddRange(DefDatabase<PawnTableDef>.GetNamed("Numbers_ColonistNeedsPreset").columns);
PopulatePsycastingPreset();
PopulateMedicalPreset();
}

private static void PopulatePsycastingPreset()
{
psycastingPreset.Add(DefDatabase<PawnColumnDef>.GetNamed("Label"));
psycastingPreset.Add(DefDatabase<PawnColumnDef>.GetNamed("Numbers_PsylinkLevel"));
psycastingPreset.Add(DefDatabase<PawnColumnDef>.GetNamed("Numbers_Psyfocus"));
psycastingPreset.Add(DefDatabase<PawnColumnDef>.GetNamed("Numbers_Entropy"));
psycastingPreset.AddRange(
DefDatabase<PawnColumnDef>.AllDefsListForReading.Where(pcd => pcd.Ext(throwError: false)?.ability != null).ToList()
.OrderBy(x => x.Ext().ability.level)
.ThenBy(x => x.Ext().ability.PsyfocusCost)
.ThenBy(x => x.Ext().ability.EntropyGain)
.ThenBy(x => x.Ext().ability.defName)
);
}

private static void PopulateMedicalPreset()
{
medicalPreset.AddRange(new List<PawnColumnDef>
Expand Down

0 comments on commit 6c1c3c2

Please sign in to comment.