Skip to content

Commit 486ed32

Browse files
Merge pull request #11 from FluffierThanThou/A14
fix issues introduced in last commit, add proper priority communicati…
2 parents a0d7813 + d352f6f commit 486ed32

File tree

8 files changed

+58
-31
lines changed

8 files changed

+58
-31
lines changed

About/About.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<name>Work Tab</name>
44
<author>Fluffy</author>
55
<url>https://ludeon.com/forums/index.php?topic=15964.0</url>
6-
<targetVersion>0.14.1234</targetVersion>
6+
<targetVersion>0.14.1241</targetVersion>
77
<description>&lt;size=36&gt;REQUIRES CCL&lt;/size&gt;
88

99
&lt;b&gt;Description&lt;/b&gt;

Assemblies/Fluffy_Tabs.dll

0 Bytes
Binary file not shown.
2.5 KB
Binary file not shown.

Source/Fluffy_Tabs/Work/Detours_WorkSettings.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class Detours_WorkSettings : Pawn_WorkSettings
3333
public int _GetPriority( WorkTypeDef w )
3434
{
3535
Pawn pawn = pawnField.GetValue( this ) as Pawn;
36-
return pawn.Priorities().GetPriority( w );
36+
return pawn.Priorities()?.GetPriority( w ) ?? 0; // return 0 if no tracker was found
3737
}
3838

3939
/// <summary>
@@ -46,7 +46,40 @@ public int _GetPriority( WorkTypeDef w )
4646
public void _SetPriority( WorkTypeDef w, int priority )
4747
{
4848
Pawn pawn = pawnField.GetValue( this ) as Pawn;
49-
pawn.Priorities().SetPriority( w, priority );
49+
50+
51+
// set priority in work tab's priority list
52+
if (pawn.Priorities() != null)
53+
pawn.Priorities().SetPriority( w, priority );
54+
// if not available, make sure that changes ARE propagated to vanilla
55+
else
56+
SetVanillaPriority( pawn, w, priority );
57+
}
58+
59+
internal static void SetVanillaPriority( Pawn pawn, WorkTypeDef workTypeDef, int priority )
60+
{
61+
// get value
62+
DefMap<WorkTypeDef, int> priorities = GetVanillaPriorities( pawn );
63+
64+
// cop out on issues
65+
if ( priorities == null )
66+
{
67+
Log.Warning( "Vanilla priorities for " + pawn.LabelShort + " not found." );
68+
return;
69+
}
70+
71+
// update
72+
priorities[workTypeDef] = priority;
73+
74+
// write back TODO: figure out if this is needed, or if we're modifying by reference (which I suspect is the case).
75+
prioritiesField.SetValue( pawn, priorities );
76+
}
77+
78+
internal static DefMap<WorkTypeDef, int> GetVanillaPriorities( Pawn pawn )
79+
{
80+
if ( pawn?.workSettings == null )
81+
return null;
82+
return prioritiesField.GetValue( pawn.workSettings ) as DefMap<WorkTypeDef, int>;
5083
}
5184

5285
/// <summary>

Source/Fluffy_Tabs/Work/MapComponent_PawnPriorities.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public static MapComponent_Priorities Instance
3030
{
3131
get
3232
{
33+
// return null if called too damn early
34+
if ( Find.Map == null )
35+
return null;
36+
3337
// try to get from game list.
3438
if ( _instance == null )
3539
_instance = Find.Map.components.FirstOrDefault( comp => comp is MapComponent_Priorities ) as MapComponent_Priorities;

Source/Fluffy_Tabs/Work/PawnPrioritiesTracker.cs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,39 +38,26 @@ public PawnPrioritiesTracker( Pawn pawn )
3838

3939
private void InitPriorityCache()
4040
{
41-
// create list of work priorities, and initialize with default settings (logic lifted from Pawn_WorkSettings.EnableAndInitialize() )
41+
// initialize from vanilla priorities.
42+
var vanillaPriorities = Detours_WorkSettings.GetVanillaPriorities( pawn );
43+
44+
// loop over hours
4245
for ( int hour = 0; hour < GenDate.HoursPerDay; hour++ )
4346
{
44-
priorities.Add( new DefMap<WorkGiverDef, int>() );
45-
46-
// for up to six 'best' worktypes, enable
47-
int count = 1;
48-
foreach ( var worktype in DefDatabase<WorkTypeDef>
49-
.AllDefsListForReading
50-
.Where( wtd => !wtd.alwaysStartActive && !pawn.story.WorkTypeIsDisabled( wtd ) )
51-
.OrderByDescending( wtd => pawn.skills.AverageOfRelevantSkillsFor( wtd ) ) )
52-
{
53-
foreach ( var workgiver in worktype.workGiversByPriority )
54-
priorities[hour][workgiver] = 3;
5547

56-
if ( count++ > 6 )
57-
break;
58-
}
48+
// create map for this hour
49+
priorities.Add( new DefMap<WorkGiverDef, int>() );
5950

60-
// enable all always enabled types
61-
foreach ( var worktype in DefDatabase<WorkTypeDef>.AllDefsListForReading.Where( wtd => wtd.alwaysStartActive ) )
51+
// loop over worktypes
52+
foreach ( WorkTypeDef worktype in DefDatabase<WorkTypeDef>.AllDefsListForReading )
6253
{
63-
foreach ( var workgiver in worktype.workGiversByPriority )
64-
priorities[hour][workgiver] = 3;
65-
}
54+
int priority = vanillaPriorities[worktype];
6655

67-
// disable story-disabled types
68-
foreach (
69-
var worktype in
70-
DefDatabase<WorkTypeDef>.AllDefsListForReading.Where( wtd => pawn.story.WorkTypeIsDisabled( wtd ) ) )
71-
{
72-
foreach ( var workgiver in worktype.workGiversByPriority )
73-
priorities[hour][workgiver] = 0;
56+
// loop over workgivers in type
57+
foreach ( WorkGiverDef workgiver in worktype.workGiversByPriority )
58+
{
59+
priorities[hour][workgiver] = priority;
60+
}
7461
}
7562
}
7663

@@ -244,6 +231,9 @@ public void SetPriority( WorkGiverDef workgiver, int priority )
244231

245232
public void SetPriority( WorkTypeDef worktype, int priority )
246233
{
234+
// propagate to vanilla
235+
Detours_WorkSettings.SetVanillaPriority( pawn, worktype, priority );
236+
247237
foreach ( var workgiver in worktype.workGiversByPriority )
248238
SetPriority( workgiver, priority );
249239
}

Source/Fluffy_Tabs/Work/Widgets_Work.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ public static TipSignal TipForPawnWorker( Pawn pawn, WorkTypeDef worktype )
742742

743743
public static PawnPrioritiesTracker Priorities( this Pawn pawn )
744744
{
745-
return MapComponent_Priorities.Instance.WorkgiverTracker( pawn );
745+
return MapComponent_Priorities.Instance?.WorkgiverTracker( pawn );
746746
}
747747

748748
private static bool CapableOf( this Pawn pawn, WorkGiverDef workgiver )
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)