Skip to content

Commit

Permalink
v3.7.3 fixes several bugs in rescue, improves mouse positioning setti…
Browse files Browse the repository at this point in the history
…ngs, allows to turn off forcing
  • Loading branch information
pardeike committed Aug 25, 2023
1 parent 2aeaed2 commit eef0659
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 25 deletions.
Binary file modified 1.4/Assemblies/AchtungMod.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion About/About.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</li>
</modDependencies>
<packageId>brrainz.achtung</packageId>
<modVersion>3.7.2.0</modVersion>
<modVersion>3.7.3.0</modVersion>
<steamAppId>730936602</steamAppId>
<url>https://github.com/pardeike/Achtung2</url>
<description>Command your colonists like a boss!
Expand Down
2 changes: 1 addition & 1 deletion About/Manifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Manifest>
<identifier>net.pardeike.rimworld.mod.achtung</identifier>
<version>3.7.2.0</version>
<version>3.7.3.0</version>
<targetVersions>
<li>1.0.0</li>
<li>1.1.0</li>
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ModName>Achtung</ModName>
<ModFileName>Achtung</ModFileName>
<Repository>https://github.com/pardeike/Achtung2</Repository>
<ModVersion>3.7.2.0</ModVersion>
<ModVersion>3.7.3.0</ModVersion>
<ProjectGuid>{8BD5A28F-96C4-43B4-907F-600AA0162F84}</ProjectGuid>
</PropertyGroup>

Expand Down
28 changes: 17 additions & 11 deletions Source/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ public static void InstallDefs()
.DoIf(def => DefDatabase<JobDef>.GetNamedSilentFail(def.defName) == null, DefDatabase<JobDef>.Add);
}

void ShowMenu(MultiActions actions, bool forceMenu, Map map, IntVec3 cell)
bool ShowMenu(MultiActions actions, bool forceMenu, Map map, IntVec3 cell)
{
if (actions == null)
return true;
if (actions.Count(false) > 0)
{
var optionTaken = false;
Expand All @@ -72,13 +74,19 @@ void ShowMenu(MultiActions actions, bool forceMenu, Map map, IntVec3 cell)
Find.WindowStack.Add(actions.GetWindow());
}
Event.current.Use();
return false;
}

public bool MouseDown(Vector3 pos)
{
colonists = Tools.GetSelectedColonists();

if (colonists.Count == 0 || Achtung.Settings.positioningEnabled == false)
if (colonists.Count == 0)
return true;

var doPositioning = Achtung.Settings.positioningEnabled;
var doForceMenu = Achtung.Settings.maxForcedItems > 0;
if (doPositioning == false && doForceMenu == false)
return true;

if (isDragging && Event.current.button == (int)Button.left && groupMovement == false)
Expand All @@ -91,16 +99,14 @@ public bool MouseDown(Vector3 pos)
if (Event.current.button != (int)Button.right)
return true;

var actions = new MultiActions(colonists, UI.MouseMapPosition());
var actions = doForceMenu ? new MultiActions(colonists, UI.MouseMapPosition()) : null;
var achtungPressed = Tools.IsModKeyPressed(Achtung.Settings.achtungKey);
var allDrafted = colonists.All(colonist => colonist.pawn.Drafted || colonist.pawn.IsColonyMechPlayerControlled || achtungPressed);
var mixedDrafted = !allDrafted && colonists.Any(colonist => colonist.pawn.Drafted);

var forceMenu = false;
if (Achtung.Settings.forceCommandMenuMode == CommandMenuMode.Auto && !mixedDrafted)
{
forceMenu = allDrafted == false;
}
else
{
forceMenu = Tools.IsModKeyPressed(Achtung.Settings.forceCommandMenuKey);
Expand All @@ -124,22 +130,22 @@ public bool MouseDown(Vector3 pos)

if (forceMenu || (subjectClicked && achtungPressed == false) || standableClicked == false)
{
ShowMenu(actions, forceMenu, map, cell);
return false;
if (actions == null)
return true;
return ShowMenu(actions, forceMenu, map, cell);
}

if (achtungPressed)
Tools.DraftWithSound(colonists, true);

// in multiplayer, drafting will update pawn.Drafted in the same tick, so we fake it
if (allDrafted)
if (allDrafted && doPositioning)
{
StartDragging(pos, achtungPressed);
return true;
}

ShowMenu(actions, forceMenu, map, cell);
return false;
return ShowMenu(actions, forceMenu, map, cell);
}

private void StartDragging(Vector3 pos, bool asGroup)
Expand Down Expand Up @@ -375,7 +381,7 @@ public bool HandleEvents()
{
var pos = UI.MouseMapPosition();
var runOriginal = true;
switch (Event.current.type)
switch (Event.current.rawType)
{
case EventType.MouseDown:
runOriginal = MouseDown(pos);
Expand Down
48 changes: 41 additions & 7 deletions Source/DynamicWorkTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,50 @@
using System.Linq;
using Verse;

namespace AchtungMod
namespace Brrainz
{
[StaticConstructorOnStartup]
public class DynamicWorkTypes
{
internal static readonly Dictionary<WorkTypeDef, WorkTypeDef> disabledWorkTypePairs = new();

static DynamicWorkTypes()
{
var harmony = new Harmony("brrainz.lib.dynamicworktypes");
var method = SymbolExtensions.GetMethodInfo((Pawn pawn) => pawn.GetDisabledWorkTypes(default));
var postfix = SymbolExtensions.GetMethodInfo(() => Pawn_GetDisabledWorkTypes_Postfix(default));
harmony.Patch(method, postfix: new HarmonyMethod(postfix) { priority = Priority.Last });
}

public static List<WorkTypeDef> Pawn_GetDisabledWorkTypes_Postfix(List<WorkTypeDef> input)
{
var output = new List<WorkTypeDef>(input);
foreach (var pair in DynamicWorkTypes.disabledWorkTypePairs)
if (input.Contains(pair.Key))
output.Add(pair.Value);
return output;
}

static void Reload<T>() where T : Def
{
DefDatabase<T>.ClearCachedData();
DefDatabase<T>.ResolveAllReferences(false, true);
}

static void UpdatePawns()
static void UpdatePawns(int index, int copyFrom)
{
Find.Maps.Do(map => map.mapPawns.AllPawnsSpawned.DoIf(p => p.workSettings != null, p =>
{
p.workSettings.priorities = null;
if (copyFrom != -1)
{
var value = p.workSettings.priorities.values[copyFrom];
p.workSettings.priorities.values.Insert(index, value);
}
else
p.workSettings.priorities.values.Remove(index);
p.workSettings.workGiversDirty = true;
p.workSettings.EnableAndInitialize();
p.cachedDisabledWorkTypes = null;
p.cachedDisabledWorkTypesPermanent = null;
p.workSettings.CacheWorkGiversInOrder();
p.Notify_DisabledWorkTypesChanged();
}));
Expand All @@ -44,9 +71,10 @@ static void Update(PawnTableDef workTable)
}
}

public static WorkTypeDef AddWorkTypeDef(WorkTypeDef def, WorkGiverDef workgiver)
public static WorkTypeDef AddWorkTypeDef(WorkTypeDef def, WorkTypeDef copyFrom, WorkGiverDef workgiver)
{
DefDatabase<WorkTypeDef>.Add(def);
disabledWorkTypePairs[copyFrom] = def;

var saved = workgiver.workType;
workgiver.workType = def;
Expand Down Expand Up @@ -76,15 +104,21 @@ public static WorkTypeDef AddWorkTypeDef(WorkTypeDef def, WorkGiverDef workgiver

Reload<PawnColumnDef>();
Reload<PawnTableDef>();
UpdatePawns();

var index = DefDatabase<WorkTypeDef>.defsList.IndexOf(def);
var indexToCopy = DefDatabase<WorkTypeDef>.defsList.IndexOf(copyFrom);
UpdatePawns(index, indexToCopy);

return saved;
}

public static void RemoveWorkTypeDef(WorkTypeDef def, WorkTypeDef savedDef, WorkGiverDef workGiver)
{
_ = disabledWorkTypePairs.Remove(savedDef);

var oldWorkGiver = workGiver.workType;
workGiver.workType = savedDef;
var index = DefDatabase<WorkTypeDef>.defsList.IndexOf(def);
DefDatabase<WorkTypeDef>.Remove(def);

var columnDef = DefDatabase<PawnColumnDef>.AllDefsListForReading.FirstOrDefault(d => d.workType == oldWorkGiver);
Expand All @@ -99,7 +133,7 @@ public static void RemoveWorkTypeDef(WorkTypeDef def, WorkTypeDef savedDef, Work
Reload<WorkGiverDef>();
Reload<PawnColumnDef>();
Reload<PawnTableDef>();
UpdatePawns();
UpdatePawns(index, -1);
}
}
}
2 changes: 1 addition & 1 deletion Source/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static void Prefix()
var rescuing = DefDatabase<WorkTypeDef>.GetNamedSilentFail(Tools.RescuingWorkTypeDef.defName);
var doctorRescueWorkGiver = DefDatabase<WorkGiverDef>.GetNamed("DoctorRescue");
if (rescuing == null && Achtung.Settings.rescueEnabled)
Tools.savedWorkTypeDef = DynamicWorkTypes.AddWorkTypeDef(Tools.RescuingWorkTypeDef, doctorRescueWorkGiver);
Tools.savedWorkTypeDef = DynamicWorkTypes.AddWorkTypeDef(Tools.RescuingWorkTypeDef, WorkTypeDefOf.Doctor, doctorRescueWorkGiver);

Log.Message($"Achtung v{Performance.GetModVersionString()} Info: To make Achtung log some performance info, create an empty 'AchtungPerformance.txt' file in same directory as Player.log");
}
Expand Down
8 changes: 5 additions & 3 deletions Source/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public override void ExposeData()
{
base.ExposeData();
Scribe_Values.Look(ref positioningEnabled, "positioningEnabled", true, true);
Scribe_Values.Look(ref rescueEnabled, "rescueEnabled", true, true);
Scribe_Values.Look(ref achtungKey, "achtungKey", AchtungModKey.Alt, true);
Scribe_Values.Look(ref forceCommandMenuMode, "forceCommandMenuMode", CommandMenuMode.Auto, true);
Scribe_Values.Look(ref forceCommandMenuKey, "forceCommandMenuKey", AchtungModKey.Ctrl, true);
Expand Down Expand Up @@ -114,7 +115,7 @@ public static void DoWindowContents(Rect canvas)
var rescuing = DefDatabase<WorkTypeDef>.GetNamedSilentFail(Tools.RescuingWorkTypeDef.defName);
var doctorRescueWorkGiver = DefDatabase<WorkGiverDef>.GetNamed("DoctorRescue");
if (rescuing == null && Achtung.Settings.rescueEnabled)
Tools.savedWorkTypeDef = DynamicWorkTypes.AddWorkTypeDef(Tools.RescuingWorkTypeDef, doctorRescueWorkGiver);
Tools.savedWorkTypeDef = DynamicWorkTypes.AddWorkTypeDef(Tools.RescuingWorkTypeDef, WorkTypeDefOf.Doctor, doctorRescueWorkGiver);
else if (rescuing != null && Achtung.Settings.rescueEnabled == false)
DynamicWorkTypes.RemoveWorkTypeDef(Tools.RescuingWorkTypeDef, Tools.savedWorkTypeDef, doctorRescueWorkGiver);

Expand All @@ -134,7 +135,8 @@ public static void DoWindowContents(Rect canvas)
list.Gap(10);
list.ValueLabeled("WorkMarkers", ref Achtung.Settings.workMarkers);
list.Gap(10);
list.SliderLabeled("MaxForcedItems", ref Achtung.Settings.maxForcedItems, 0, UnlimitedForcedItems, (n) => n >= UnlimitedForcedItems ? "MaxForcedItemsUnlimited".Translate().ToString() : $"{n}");
static string forcedItemsString(int n) => n == 0 ? "Disabled".Translate().ToString() : n >= UnlimitedForcedItems ? "MaxForcedItemsUnlimited".Translate().ToString() : $"{n}";
list.SliderLabeled("MaxForcedItems", ref Achtung.Settings.maxForcedItems, 0, UnlimitedForcedItems, forcedItemsString);

list.End();

Expand All @@ -149,4 +151,4 @@ public static void DoWindowContents(Rect canvas)
list.End();
}
}
}
}

0 comments on commit eef0659

Please sign in to comment.