Skip to content

Commit b7cf412

Browse files
refactor storage of priorities
Completely reworked how priorities are stored. Instead of using defmaps where only the priorities are stored and the workgiverdefs are assumed to stay constant, we now store a list of workgiverdefs with the priorities to ascertain ordering, and match priorities to current workgiverdefs upon loading. In addition, we no longer use a list of lists for storing priorities, but a much more compressed block of digits per pawn, where each line is an hour and each column a workgiver. Migrating from the old to new format is done by setting a 'newFormat' flag in the save game, and should work out-of-the-box.
1 parent 144abd4 commit b7cf412

File tree

9 files changed

+223
-43
lines changed

9 files changed

+223
-43
lines changed

Assemblies/Fluffy_Tabs.dll

3.5 KB
Binary file not shown.

LICENSE

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
1-
Attribution-NonCommercial-ShareAlike 4.0 International
1+
=======================================================================
2+
The following MIT license applies to the software and any documentation.
3+
Any original content (e.g. text, imagery or sounds) are licensed under
4+
CC-BY-SA 4.0, the text of which is given below.
5+
=======================================================================
6+
7+
Copyright 2017 Fluffy
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14+
15+
16+
17+
18+
19+
20+
=======================================================================
21+
The following CC BY-SA 4.0 license applies to any original content. The
22+
software and documentation are licensed under the MIT license, the text
23+
of which is given above.
24+
=======================================================================
25+
26+
Attribution-ShareAlike 4.0 International
227

328
=======================================================================
429

@@ -54,18 +79,18 @@ exhaustive, and do not form part of our licenses.
5479

5580
=======================================================================
5681

57-
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
58-
Public License
82+
Creative Commons Attribution-ShareAlike 4.0 International Public
83+
License
5984

6085
By exercising the Licensed Rights (defined below), You accept and agree
6186
to be bound by the terms and conditions of this Creative Commons
62-
Attribution-NonCommercial-ShareAlike 4.0 International Public License
63-
("Public License"). To the extent this Public License may be
64-
interpreted as a contract, You are granted the Licensed Rights in
65-
consideration of Your acceptance of these terms and conditions, and the
66-
Licensor grants You such rights in consideration of benefits the
67-
Licensor receives from making the Licensed Material available under
68-
these terms and conditions.
87+
Attribution-ShareAlike 4.0 International Public License ("Public
88+
License"). To the extent this Public License may be interpreted as a
89+
contract, You are granted the Licensed Rights in consideration of Your
90+
acceptance of these terms and conditions, and the Licensor grants You
91+
such rights in consideration of benefits the Licensor receives from
92+
making the Licensed Material available under these terms and
93+
conditions.
6994

7095

7196
Section 1 -- Definitions.
@@ -84,7 +109,7 @@ Section 1 -- Definitions.
84109
and Similar Rights in Your contributions to Adapted Material in
85110
accordance with the terms and conditions of this Public License.
86111

87-
c. BY-NC-SA Compatible License means a license listed at
112+
c. BY-SA Compatible License means a license listed at
88113
creativecommons.org/compatiblelicenses, approved by Creative
89114
Commons as essentially the equivalent of this Public License.
90115

@@ -108,7 +133,7 @@ Section 1 -- Definitions.
108133

109134
g. License Elements means the license attributes listed in the name
110135
of a Creative Commons Public License. The License Elements of this
111-
Public License are Attribution, NonCommercial, and ShareAlike.
136+
Public License are Attribution and ShareAlike.
112137

113138
h. Licensed Material means the artistic or literary work, database,
114139
or other material to which the Licensor applied this Public
@@ -122,29 +147,21 @@ Section 1 -- Definitions.
122147
j. Licensor means the individual(s) or entity(ies) granting rights
123148
under this Public License.
124149

125-
k. NonCommercial means not primarily intended for or directed towards
126-
commercial advantage or monetary compensation. For purposes of
127-
this Public License, the exchange of the Licensed Material for
128-
other material subject to Copyright and Similar Rights by digital
129-
file-sharing or similar means is NonCommercial provided there is
130-
no payment of monetary compensation in connection with the
131-
exchange.
132-
133-
l. Share means to provide material to the public by any means or
150+
k. Share means to provide material to the public by any means or
134151
process that requires permission under the Licensed Rights, such
135152
as reproduction, public display, public performance, distribution,
136153
dissemination, communication, or importation, and to make material
137154
available to the public including in ways that members of the
138155
public may access the material from a place and at a time
139156
individually chosen by them.
140157

141-
m. Sui Generis Database Rights means rights other than copyright
158+
l. Sui Generis Database Rights means rights other than copyright
142159
resulting from Directive 96/9/EC of the European Parliament and of
143160
the Council of 11 March 1996 on the legal protection of databases,
144161
as amended and/or succeeded, as well as other essentially
145162
equivalent rights anywhere in the world.
146163

147-
n. You means the individual or entity exercising the Licensed Rights
164+
m. You means the individual or entity exercising the Licensed Rights
148165
under this Public License. Your has a corresponding meaning.
149166

150167

@@ -158,10 +175,9 @@ Section 2 -- Scope.
158175
exercise the Licensed Rights in the Licensed Material to:
159176

160177
a. reproduce and Share the Licensed Material, in whole or
161-
in part, for NonCommercial purposes only; and
178+
in part; and
162179

163-
b. produce, reproduce, and Share Adapted Material for
164-
NonCommercial purposes only.
180+
b. produce, reproduce, and Share Adapted Material.
165181

166182
2. Exceptions and Limitations. For the avoidance of doubt, where
167183
Exceptions and Limitations apply to Your use, this Public
@@ -229,9 +245,7 @@ Section 2 -- Scope.
229245
Rights, whether directly or through a collecting society
230246
under any voluntary or waivable statutory or compulsory
231247
licensing scheme. In all other cases the Licensor expressly
232-
reserves any right to collect such royalties, including when
233-
the Licensed Material is used other than for NonCommercial
234-
purposes.
248+
reserves any right to collect such royalties.
235249

236250

237251
Section 3 -- License Conditions.
@@ -276,6 +290,7 @@ following conditions.
276290
reasonable to satisfy the conditions by providing a URI or
277291
hyperlink to a resource that includes the required
278292
information.
293+
279294
3. If requested by the Licensor, You must remove any of the
280295
information required by Section 3(a)(1)(A) to the extent
281296
reasonably practicable.
@@ -287,7 +302,7 @@ following conditions.
287302

288303
1. The Adapter's License You apply must be a Creative Commons
289304
license with the same License Elements, this version or
290-
later, or a BY-NC-SA Compatible License.
305+
later, or a BY-SA Compatible License.
291306

292307
2. You must include the text of, or the URI or hyperlink to, the
293308
Adapter's License You apply. You may satisfy this condition
@@ -307,15 +322,14 @@ apply to Your use of the Licensed Material:
307322

308323
a. for the avoidance of doubt, Section 2(a)(1) grants You the right
309324
to extract, reuse, reproduce, and Share all or a substantial
310-
portion of the contents of the database for NonCommercial purposes
311-
only;
325+
portion of the contents of the database;
312326

313327
b. if You include all or a substantial portion of the database
314328
contents in a database in which You have Sui Generis Database
315329
Rights, then the database in which You have Sui Generis Database
316330
Rights (but not its individual contents) is Adapted Material,
317-
including for purposes of Section 3(b); and
318331

332+
including for purposes of Section 3(b); and
319333
c. You must comply with the conditions in Section 3(a) if You Share
320334
all or a substantial portion of the contents of the database.
321335

@@ -415,6 +429,7 @@ Section 8 -- Interpretation.
415429
that apply to the Licensor or You, including from the legal
416430
processes of any jurisdiction or authority.
417431

432+
418433
=======================================================================
419434

420435
Creative Commons is not a party to its public
@@ -435,3 +450,5 @@ the avoidance of doubt, this paragraph does not form part of the
435450
public licenses.
436451

437452
Creative Commons may be contacted at creativecommons.org.
453+
454+
0Trackers

Readme.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ Please read [this guide](http://steamcommunity.com/sharedfiles/filedetails/?id=7
3535
All current and past versions of this mod can be downloaded from [GitHub](https://github.com/FluffierThanThou/WorkTab/releases).
3636

3737
# License
38-
This mod is licensed under the Creative Commons [by-nc-sa 4.0 Int](http://creativecommons.org/licenses/by-nc-sa/4.0/) license.
38+
All original code in this mod is licensed under the [MIT license](https://opensource.org/licenses/MIT). Do what you want, but give me credit.
39+
All original content (e.g. text, imagery, sounds) in this mod is licensed under the [CC-BY-SA 4.0 license](http://creativecommons.org/licenses/by-sa/4.0/).
40+
41+
Parts of the code in this mod, and some content may be licensed by their original authors. If this is the case, the original author & license will either be given in the source code, or be in a LICENSE file next to the content. Please do not decompile my mods, but use the original source code available on [GitHub](https://github.com/FluffierThanThou/WorkTab/), so license information in the source code is preserved.
3942

4043
# Version
41-
This is version v0.16.1.8
44+
This is version v0.16.1.9

Source/Fluffy_Tabs/Core/Controller.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@ namespace Fluffy_Tabs
77
public class Controller : ModBase
88
{
99
public override string ModIdentifier => "WorkTab";
10+
private static ModLogger _logger;
11+
12+
public Controller() : base() { _logger = base.Logger; }
13+
14+
#region Overrides of ModBase
15+
16+
public override void DefsLoaded()
17+
{
18+
base.DefsLoaded();
19+
WorldObject_Priorities.OnDefsLoaded();
20+
}
21+
22+
#endregion
23+
24+
public new static ModLogger Logger => _logger;
1025

1126
public override void MapLoaded( Map map )
1227
{
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Karel Kroeze
2+
// DefMap_Extensions.cs
3+
// 2017-02-07
4+
5+
using Verse;
6+
7+
namespace Fluffy_Tabs
8+
{
9+
public static class DefMap_Extensions
10+
{
11+
public static string GetIntString<T>( this DefMap<T, int> defMap ) where T : Def, new()
12+
{
13+
string[] msg = new string[defMap.Count];
14+
for ( var i = 0; i < defMap.Count; i++ )
15+
msg[i] = defMap[i].ToString();
16+
17+
return string.Join( "", msg );
18+
}
19+
}
20+
}

Source/Fluffy_Tabs/Fluffy_Tabs.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
</ItemGroup>
5252
<ItemGroup>
5353
<Compile Include="Core\Controller.cs" />
54+
<Compile Include="Core\DefMap_Extensions.cs" />
5455
<Compile Include="Core\Widgets_UI_TextureChooser.cs" />
5556
<Compile Include="Work\Detours_WorkSettings.cs" />
5657
<Compile Include="Work\Dialog_CreateFavourite.cs" />

Source/Fluffy_Tabs/Work/PawnPrioritiesTracker.cs

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
5+
using HugsLib.Utils;
56
using UnityEngine;
67
using Verse;
8+
using static Fluffy_Tabs.Controller;
79

810
namespace Fluffy_Tabs
911
{
@@ -17,6 +19,9 @@ public class PawnPrioritiesTracker : IExposable
1719
private DefMap<WorkGiverDef, bool> _timeDependentCache = new DefMap<WorkGiverDef, bool>();
1820
private Dictionary<WorkGiverDef, string> _timeDependentTipCache = new Dictionary<WorkGiverDef, string>();
1921
private List<DefMap<WorkGiverDef, int>> priorities = new List<DefMap<WorkGiverDef, int>>();
22+
private bool newFormat = true;
23+
24+
private string _prioritiesScribeHelper;
2025

2126
#endregion Fields
2227

@@ -36,6 +41,9 @@ public PawnPrioritiesTracker( Pawn pawn )
3641

3742
private void InitPriorityCache()
3843
{
44+
// create fresh list (just to be sure).
45+
priorities = new List<DefMap<WorkGiverDef, int>>();
46+
3947
// initialize from vanilla priorities.
4048
var vanillaPriorities = Detours_WorkSettings.GetVanillaPriorities( pawn );
4149

@@ -70,15 +78,77 @@ public void AssignFavourite( WorkFavourite favourite )
7078
public void ExposeData()
7179
{
7280
Scribe_References.LookReference( ref pawn, "pawn" );
73-
Scribe_Collections.LookList( ref priorities, "priorities", LookMode.Deep );
7481
Scribe_References.LookReference( ref currentFavourite, "currentFavourite" );
7582

83+
// handle priorities, first try to see what style the old save was in.
84+
// note that while saving, newFormat is set to true. While loading,
85+
// it defaults to false. The value of newFormat should be immediately available.
86+
if ( Scribe.mode == LoadSaveMode.Saving )
87+
newFormat = true;
88+
Scribe_Values.LookValue( ref newFormat, "newFormat", false );
89+
//Controller.Logger.Message( "Loading priorities from {1} format save: {0}", Scribe.mode,
90+
// newFormat ? "NEW" : "OLD" );
91+
if ( newFormat )
92+
{
93+
switch ( Scribe.mode )
94+
{
95+
case LoadSaveMode.Saving:
96+
// this one is fairly straightforward, create string block that has one line per hour and just joins all the priorities
97+
// note; this means a hard requirement for priorities to be single digits.
98+
_prioritiesScribeHelper = priorities.Select(m => m.GetIntString()).Join("\n");
99+
Scribe_Values.LookValue(ref _prioritiesScribeHelper, "priorities", string.Empty, true);
100+
break;
101+
case LoadSaveMode.LoadingVars:
102+
// just read out the string block, we'll process it in the PostLoadInit
103+
Scribe_Values.LookValue(ref _prioritiesScribeHelper, "priorities", string.Empty, true);
104+
break;
105+
case LoadSaveMode.PostLoadInit:
106+
// create priorities data, and fill with defaults
107+
InitPriorityCache();
108+
109+
// fill in saved priorities
110+
LoadPrioritiesFromString();
111+
break;
112+
}
113+
}
114+
else
115+
{
116+
// fall back to old style
117+
Scribe_Collections.LookList(ref priorities, "priorities", LookMode.Deep);
118+
}
119+
76120
// clear tip cache so it gets rebuild after load
77121
if ( Scribe.mode == LoadSaveMode.PostLoadInit )
78122
foreach ( var workgiver in DefDatabase<WorkGiverDef>.AllDefsListForReading )
79123
_cacheDirty[workgiver] = true;
80124
}
81125

126+
private void LoadPrioritiesFromString()
127+
{
128+
// fetch priorities from the string block
129+
List<List<int>> _priorities = _prioritiesScribeHelper
130+
// first off, split the lines to get a string per hour
131+
.Split( "\n".ToCharArray() )
132+
// split lines into individual priorities
133+
.Select( IntsFromString ).ToList();
134+
135+
// fill priority tracker
136+
foreach ( WorkGiverDef workgiver in DefDatabase<WorkGiverDef>.AllDefsListForReading )
137+
{
138+
int savedWorkgiverIndex = WorldObject_Priorities.GetSavedWorkgiverIndex( workgiver );
139+
if ( savedWorkgiverIndex >= 0 )
140+
for ( int hour = 0; hour < GenDate.HoursPerDay; hour++ )
141+
priorities[hour][workgiver] = _priorities[hour][savedWorkgiverIndex];
142+
}
143+
}
144+
145+
private List<int> IntsFromString( string line )
146+
{
147+
return line.ToCharArray() // break into individual characters
148+
.Select( c => int.Parse( c.ToString() ) ) // parse as ints
149+
.ToList();
150+
}
151+
82152
public int GetPriority( WorkGiverDef workgiver )
83153
{
84154
return GetPriority( workgiver, GenLocalDate.HourOfDay( pawn.Map ) );
@@ -96,13 +166,13 @@ public int GetPriority( WorkGiverDef workgiver, int hour )
96166

97167
// force priority back to 0.
98168
priority = 0;
99-
SetPriority( workgiver, 0, hour );
169+
SetPriority( workgiver, priority, hour );
100170
}
101171
return priority;
102172
}
103173
catch ( ArgumentOutOfRangeException )
104174
{
105-
// workgiver-priority defmap is really just and ordered list indexed by a dynamically generated workgiver index int
175+
// workgiver-priority defmap is really just an ordered list indexed by a dynamically generated workgiver index int
106176
// if the number of workgivers increases, this means errors.
107177
Messages.Message( "WorkGiver database corrupted, resetting priorities for " + pawn.NameStringShort + ". Did you add mods during the game?", MessageSound.SeriousAlert );
108178
priorities = new List<DefMap<WorkGiverDef, int>>();
@@ -115,7 +185,7 @@ public int GetPriority( WorkGiverDef workgiver, int hour )
115185
public int GetPriority( WorkTypeDef worktype, int hour = -1 )
116186
{
117187
// get current hour if left default
118-
// check if pawn has a registered Map we can use for the local time (99%) of cases.
188+
// check if pawn has a registered Map we can use for the local time (99% of cases).
119189
if ( hour < 0 && pawn?.Map != null )
120190
hour = GenLocalDate.HourOfDay( pawn.Map );
121191
// if not, try use the visible map

0 commit comments

Comments
 (0)