Skip to content

Commit

Permalink
v1.2.2
Browse files Browse the repository at this point in the history
- Renamed Ingestible policy back to Food policy
- Added new global option "Allow drugs to be restricted" which will include drugs in food policies (default: OFF)
  • Loading branch information
Jaxe-Dev committed Nov 9, 2018
1 parent b86e7e0 commit 50cd71c
Show file tree
Hide file tree
Showing 16 changed files with 136 additions and 110 deletions.
2 changes: 1 addition & 1 deletion About/About.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
<name>Pawn Rules</name>
<author>Jaxe</author>
<targetVersion>1.0.0</targetVersion>
<description>Mod Version: 1.2.1\n\n\nPawn Rules is a mod that allows custom rules to be assigned individually to your colonists, animals, guests and prisoners.\n\nCurrently the following rules can be applied:\n\n- Disallow certain foods\n- Disallow bonding with certain animals\n- Disallow new romances\n- Disallow constructing items that have a quality level\n\nAny of these rules can be disabled and hidden from the rules window. Rules presets and defaults can be imported and exported between games.</description>
<description>Mod Version: 1.2.2\n\n\nPawn Rules is a mod that allows custom rules to be assigned individually to your colonists, animals, guests and prisoners.\n\nCurrently the following rules can be applied:\n\n- Disallow certain foods\n- Disallow bonding with certain animals\n- Disallow new romances\n- Disallow constructing items that have a quality level\n\nAny of these rules can be disabled and hidden from the rules window. Rules presets and defaults can be imported and exported between games.</description>
</ModMetaData>
2 changes: 1 addition & 1 deletion About/Manifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<Manifest>
<identifier>PawnRules</identifier>
<version>1.2.1</version>
<version>1.2.2</version>
<manifestUri>https://raw.githubusercontent.com/Jaxe-Dev/PawnRules/master/About/Manifest.xml</manifestUri>
<downloadUri>https://github.com/Jaxe-Dev/PawnRules/releases/latest</downloadUri>
</Manifest>
9 changes: 5 additions & 4 deletions Languages/English/Keyed/Keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
<PawnRules.Preset.None>Unrestricted</PawnRules.Preset.None>
<PawnRules.Preset.Personalized>Personalized</PawnRules.Preset.Personalized>

<PawnRules.RestrictionType.Food>Ingestible Policy</PawnRules.RestrictionType.Food>
<PawnRules.RestrictionType.FoodPlural>Ingestible Policies</PawnRules.RestrictionType.FoodPlural>
<PawnRules.RestrictionType.Food>Food Policy</PawnRules.RestrictionType.Food>
<PawnRules.RestrictionType.FoodPlural>Food Policies</PawnRules.RestrictionType.FoodPlural>
<PawnRules.RestrictionType.FoodCategorization>Ingestibles</PawnRules.RestrictionType.FoodCategorization>
<PawnRules.RestrictionType.Bonding>Bonding Policy</PawnRules.RestrictionType.Bonding>
<PawnRules.RestrictionType.BondingPlural>Bonding Policies</PawnRules.RestrictionType.BondingPlural>
Expand All @@ -71,8 +71,8 @@
<PawnRules.PawnType.PersonPlural>Persons</PawnRules.PawnType.PersonPlural>
<PawnRules.PawnType.Individual>Individual</PawnRules.PawnType.Individual>

<PawnRules.Rules.FoodRestrictions>Ingestible policy: {0}</PawnRules.Rules.FoodRestrictions>
<PawnRules.Rules.FoodRestrictionsDesc>Click to choose which ingestibles can be consumed.</PawnRules.Rules.FoodRestrictionsDesc>
<PawnRules.Rules.FoodRestrictions>Food policy: {0}</PawnRules.Rules.FoodRestrictions>
<PawnRules.Rules.FoodRestrictionsDesc>Click to choose which foods, drinks or other ingestibles can be consumed.</PawnRules.Rules.FoodRestrictionsDesc>
<PawnRules.Rules.BondingRestrictions>Bonding policy: {0}</PawnRules.Rules.BondingRestrictions>
<PawnRules.Rules.BondingRestrictionsDesc>Click to choose which animals can be bonded with.</PawnRules.Rules.BondingRestrictionsDesc>
<PawnRules.Rules.AllowCourting>Allow Courting</PawnRules.Rules.AllowCourting>
Expand All @@ -95,6 +95,7 @@
<PawnRules.Dialog_Global.Title>Global Options</PawnRules.Dialog_Global.Title>
<PawnRules.Dialog_Global.AllowEmergencyFood>Allow any food when malnourished</PawnRules.Dialog_Global.AllowEmergencyFood>
<PawnRules.Dialog_Global.AllowTrainingFood>Allow any food when training</PawnRules.Dialog_Global.AllowTrainingFood>
<PawnRules.Dialog_Global.AllowDrugsRestriction>Allow drugs to be restricted</PawnRules.Dialog_Global.AllowDrugsRestriction>
<PawnRules.Dialog_Global.ShowRule>Show {0}</PawnRules.Dialog_Global.ShowRule>
<PawnRules.Dialog_Global.Plans>Import/Export Plans</PawnRules.Dialog_Global.Plans>

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Pawn Rules
![Mod Version](https://img.shields.io/badge/Mod_Version-1.2.1-blue.svg)
![Mod Version](https://img.shields.io/badge/Mod_Version-1.2.2-blue.svg)
![RimWorld Version](https://img.shields.io/badge/Built_for_RimWorld-1.0-blue.svg)
![Harmony Version](https://img.shields.io/badge/Powered_by_Harmony-1.2.0.1-blue.svg)\
![Steam Subscribers](https://img.shields.io/badge/dynamic/xml.svg?label=Steam+Subscribers&query=//table/tr[2]/td[1]&colorB=blue&url=https://steamcommunity.com/sharedfiles/filedetails/%3Fid=1499843448&suffix=+total)
Expand Down
3 changes: 3 additions & 0 deletions Source/Data/Registry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal class Registry : WorldObject
public static bool ShowAllowCourting { get => _instance._showAllowCourting; set => _instance._showAllowCourting = value; }
public static bool ShowAllowArtisan { get => _instance._showAllowArtisan; set => _instance._showAllowArtisan = value; }

public static bool AllowDrugsRestriction { get => _instance._allowDrugsRestriction; set => _instance._allowDrugsRestriction = value; }
public static bool AllowEmergencyFood { get => _instance._allowEmergencyFood; set => _instance._allowEmergencyFood = value; }
public static bool AllowTrainingFood { get => _instance._allowTrainingFood; set => _instance._allowTrainingFood = value; }
public static Pawn ExemptedTrainer { get => _instance._exemptedTrainer; set => _instance._exemptedTrainer = value; }
Expand All @@ -48,6 +49,7 @@ internal class Registry : WorldObject
private bool _showAllowCourting = true;
private bool _showAllowArtisan = true;

private bool _allowDrugsRestriction;
private bool _allowEmergencyFood;
private bool _allowTrainingFood;

Expand Down Expand Up @@ -324,6 +326,7 @@ public override void ExposeData()
Scribe_Values.Look(ref _showAllowCourting, "showAllowCourting", true);
Scribe_Values.Look(ref _showAllowArtisan, "showAllowArtisan", true);

Scribe_Values.Look(ref _allowDrugsRestriction, "allowDrugsRestriction");
Scribe_Values.Look(ref _allowEmergencyFood, "allowEmergencyFood");
Scribe_Values.Look(ref _allowTrainingFood, "allowTrainingFood");

Expand Down
4 changes: 2 additions & 2 deletions Source/Data/Restriction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Restriction(XElement xml)
var defs = xml.Element("Defs")?.Elements();
if (defs == null) { return; }

foreach (var def in defs) { _defs.Add(def.Value); }
foreach (var def in defs.Where(def => RestrictionTemplate.IsValidDefName(def.Value, Type))) { _defs.Add(def.Value); }
}

public bool Matches(RestrictionTemplate template) => _defs.SequenceEqual(from category in template.Categories from member in category.Members where !member.Value select member.Def.defName);
Expand All @@ -48,7 +48,7 @@ public void Update(RestrictionTemplate template)
}

public bool Allows(Def def) => !_defs.Contains(def.defName);
public bool AllowsFood(ThingDef def, Pawn pawn) => IsVoid || pawn.InMentalState || !_defs.Contains(def.defName) || (Registry.AllowEmergencyFood && (pawn.health?.hediffSet?.HasHediff(HediffDefOf.Malnutrition) ?? false));
public bool AllowsFood(ThingDef def, Pawn pawn) => IsVoid || pawn.InMentalState || (def.IsDrug && !Registry.AllowDrugsRestriction) || !_defs.Contains(def.defName) || (Registry.AllowEmergencyFood && (pawn.health?.hediffSet?.HasHediff(HediffDefOf.Malnutrition) ?? false));

protected override void ExposePresetData()
{
Expand Down
8 changes: 8 additions & 0 deletions Source/Data/RestrictionTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public void ToggleAll(bool value)
}
}

public static bool IsValidDefName(string defName, RestrictionType type)
{
if (type == RestrictionType.Food) { return FoodCache.Any(def => def.defName == defName); }
if (type == RestrictionType.Bonding) { return AnimalCache.Any(def => def.defName == defName); }

throw new Mod.Exception("Invalid restriction type");
}

private static RestrictionTemplate GetFoodsCategorized(Restriction restriction)
{
var list = new Dictionary<string, Category> { [ThingCategoryDefOf.FoodMeals.LabelCap] = new Category(ThingCategoryDefOf.FoodMeals.LabelCap) };
Expand Down
8 changes: 4 additions & 4 deletions Source/Interface/Dialog_Alert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public override void DoWindowContents(Rect rect)
var listing = new Listing_Standard();
var vGrid = rect.GetVGrid(4f, -1f, 30f);

listing.Begin(vGrid[0]);
listing.Begin(vGrid[1]);
listing.Label(_message);
listing.End();

var hGrid = vGrid[1].GetHGrid(4f, 100f, -1f);
var hGrid = vGrid[2].GetHGrid(4f, 100f, -1f);

listing.Begin(_buttons == Buttons.Ok ? vGrid[1] : hGrid[0]);
listing.Begin(_buttons == Buttons.Ok ? vGrid[3] : hGrid[1]);

if (listing.ButtonText(_buttons == Buttons.YesNo ? Lang.Get("Button.Yes") : Lang.Get("Button.OK")))
{
Expand All @@ -59,7 +59,7 @@ public override void DoWindowContents(Rect rect)

if (_buttons == Buttons.Ok) { return; }

listing.Begin(hGrid[1]);
listing.Begin(hGrid[2]);
if (listing.ButtonText(_buttons == Buttons.YesNo ? Lang.Get("Button.No") : Lang.Get("Button.Cancel"))) { Close(); }
listing.End();
}
Expand Down
1 change: 1 addition & 0 deletions Source/Interface/Dialog_Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ protected override void DoContent(Rect rect)
var listing = new Listing_StandardPlus();
listing.Begin(rect);

Registry.AllowDrugsRestriction = listing.CheckboxLabeled(Lang.Get("Dialog_Global.AllowDrugsRestriction"), Registry.AllowDrugsRestriction);
Registry.AllowEmergencyFood = listing.CheckboxLabeled(Lang.Get("Dialog_Global.AllowEmergencyFood"), Registry.AllowEmergencyFood);
Registry.AllowTrainingFood = listing.CheckboxLabeled(Lang.Get("Dialog_Global.AllowTrainingFood"), Registry.AllowTrainingFood);

Expand Down
16 changes: 8 additions & 8 deletions Source/Interface/Dialog_Plans.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,25 @@ protected override void DoContent(Rect rect)
var hasPlans = _plans.Any();
if (hasPlans)
{
_listing.Begin(vGrid[0], true);
_listing.Begin(vGrid[1], true);
foreach (var plan in _plans)
{
if (_listing.RadioButton(plan, _selected == plan)) { _selected = plan; }
}
}
else
{
_listing.Begin(vGrid[0]);
_listing.Begin(vGrid[1]);
_listing.Label(Lang.Get("Dialog_Plans.NoneFound"));
}
_listing.End();

var tGrid = vGrid[1].GetHGrid(4f, -1f, -1f);
var bGrid = vGrid[2].GetHGrid(4f, -1f, -1f);
if (GuiPlus.ButtonText(tGrid[0], Lang.Get("Dialog_Plans.Import"), Lang.Get("Dialog_Plans.ImportDesc"), !_selected.NullOrEmpty())) { LoadPlan(); }
if (GuiPlus.ButtonText(tGrid[1], Lang.Get("Dialog_Plans.Delete"), Lang.Get("Dialog_Plans.DeleteDesc"), !_selected.NullOrEmpty())) { Dialog_Alert.Open(Lang.Get("Dialog_Plans.ConfirmDelete", _selected), Dialog_Alert.Buttons.YesNo, DeletePlan); }
if (GuiPlus.ButtonText(bGrid[0], Lang.Get("Dialog_Plans.Export"), Lang.Get("Dialog_Plans.ExportDesc"))) { Dialog_SetName.Open(Lang.Get("Dialog_SetName.PlanTitle"), Lang.Get("Dialog_SetName.PlanLabel"), SavePlan, Persistent.NameIsValid, Persistent.CreateDefaultName()); }
if (GuiPlus.ButtonText(bGrid[1], "CloseButton".Translate())) { Close(); }
var tGrid = vGrid[2].GetHGrid(4f, -1f, -1f);
var bGrid = vGrid[3].GetHGrid(4f, -1f, -1f);
if (GuiPlus.ButtonText(tGrid[1], Lang.Get("Dialog_Plans.Import"), Lang.Get("Dialog_Plans.ImportDesc"), !_selected.NullOrEmpty())) { LoadPlan(); }
if (GuiPlus.ButtonText(tGrid[2], Lang.Get("Dialog_Plans.Delete"), Lang.Get("Dialog_Plans.DeleteDesc"), !_selected.NullOrEmpty())) { Dialog_Alert.Open(Lang.Get("Dialog_Plans.ConfirmDelete", _selected), Dialog_Alert.Buttons.YesNo, DeletePlan); }
if (GuiPlus.ButtonText(bGrid[1], Lang.Get("Dialog_Plans.Export"), Lang.Get("Dialog_Plans.ExportDesc"))) { Dialog_SetName.Open(Lang.Get("Dialog_SetName.PlanTitle"), Lang.Get("Dialog_SetName.PlanLabel"), SavePlan, Persistent.NameIsValid, Persistent.CreateDefaultName()); }
if (GuiPlus.ButtonText(bGrid[2], "CloseButton".Translate())) { Close(); }
}
}
}
105 changes: 56 additions & 49 deletions Source/Interface/Dialog_Restrictions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using PawnRules.Data;
using System.Collections.Generic;
using System.Linq;
using PawnRules.Data;
using PawnRules.Patch;
using UnityEngine;
using Verse;
Expand Down Expand Up @@ -55,11 +57,7 @@ void OnAccept()

void OnCancel()
{
if (_presetList.IsUnsaved)
{
//Registry.DeletePreset(_presetList.Selected);
_presetList.Revert();
}
if (_presetList.IsUnsaved) { _presetList.Revert(); }

base.Close(doCloseSound);
}
Expand All @@ -78,96 +76,105 @@ protected override void DoContent(Rect rect)
_color = GUI.color;

var vGrid = rect.GetVGrid(4f, 42f, -1f);
var hGrid = vGrid[1].GetHGrid(8f, 200f, -1f, -1f);
DoHeader(vGrid[0]);
DoHeader(vGrid[1]);

_presetList.DoContent(hGrid[0]);
DoCategories(hGrid[1]);
DoMembers(hGrid[2]);
var hGrid = vGrid[2].GetHGrid(8f, 200f, -1f, -1f);
_presetList.DoContent(hGrid[1]);
DoCategories(hGrid[2], hGrid[3]);
}

private void DoHeader(Rect rect)
{
var grid = rect.GetHGrid(8f, 200f, -1f, -1f);
_headerList.Begin(grid[0]);
_headerList.Begin(grid[1]);
_headerList.Label(Lang.Get("Preset.Header").Italic().Bold());
_headerList.GapLine();
_headerList.End();

_headerList.Begin(grid[1]);
_headerList.Begin(grid[2]);
_headerList.Label(Lang.Get("Dialog_Restrictions.HeaderCategory").Italic().Bold());
_headerList.GapLine();
_headerList.End();

_headerList.Begin(grid[2]);
_headerList.Begin(grid[3]);
_headerList.Label(_type.Categorization.Italic().Bold());
_headerList.GapLine();
_headerList.End();
}

private void DoCategories(Rect rect)
private void DoCategories(Rect categoriesRect, Rect membersRect)
{
var vGrid = rect.GetVGrid(4f, -1f, 30f);
_categoryList.Begin(vGrid[0], true);
var vGrid = categoriesRect.GetVGrid(4f, -1f, 30f);
_categoryList.Begin(vGrid[1], true);

var members = new Dictionary<string, RestrictionTemplate.Toggle[]>();

foreach (var category in _template.Categories)
{
var state = category.GetListState();

var showAll = _presetList.EditMode || (_rules.Pawn == null);
if (_type == RestrictionType.Food) { members[category.Label] = category.Members.Where(member => member.Def is ThingDef def && (!def.IsDrug || Registry.AllowDrugsRestriction) && (showAll || _rules.Pawn.RaceProps.CanEverEat(def))).ToArray(); }
else if (_type == RestrictionType.Bonding) { members[category.Label] = category.Members.ToArray(); }
else { throw new Mod.Exception("Unsupported restriction type"); }

if (members[category.Label].Length == 0) { continue; }

if (_presetList.EditMode)
{
_categoryList.CheckboxPartial(category.Label, ref state);
category.UpdateState(state);
continue;
}
else
{
GUI.color = GuiPlus.ReadOnlyColor;
_categoryList.CheckboxPartial(category.Label, ref state);
GUI.color = _color;
}
}

if ((_type == RestrictionType.Food) && (_rules.Pawn != null) && !category.Members.Any(member => _rules.Pawn.RaceProps.CanEverEat((ThingDef) member.Def))) { continue; }
_membersList.Gap();
_categoryList.End();

GUI.color = GuiPlus.ReadOnlyColor;
_categoryList.CheckboxPartial(category.Label, ref state);
GUI.color = _color;
if (!_presetList.EditMode)
{
DoMembers(membersRect, members);
return;
}
_categoryList.End();

if (!_presetList.EditMode) { return; }
var hGrid = vGrid[2].GetHGrid(4f, -1f, -1f);
var categoryButtons = new Listing_Standard();
categoryButtons.Begin(hGrid[1]);
if (categoryButtons.ButtonText(Lang.Get("Button.RestrictionsAllowOn"))) { _template.ToggleAll(true); }
categoryButtons.End();
categoryButtons.Begin(hGrid[2]);
if (categoryButtons.ButtonText(Lang.Get("Button.RestrictionsAllowOff"))) { _template.ToggleAll(false); }
categoryButtons.End();

var hGrid = vGrid[1].GetHGrid(4f, -1f, -1f);
_categoryList.Begin(hGrid[0]);
if (_categoryList.ButtonText(Lang.Get("Button.RestrictionsAllowOn"))) { _template.ToggleAll(true); }
_categoryList.End();
_categoryList.Begin(hGrid[1]);
if (_categoryList.ButtonText(Lang.Get("Button.RestrictionsAllowOff"))) { _template.ToggleAll(false); }
_categoryList.End();
DoMembers(membersRect, members);
}

private void DoMembers(Rect rect)
private void DoMembers(Rect membersRect, Dictionary<string, RestrictionTemplate.Toggle[]> categories)
{
_membersList.Begin(rect, true);
_membersList.Begin(membersRect, true);

foreach (var category in _template.Categories)
foreach (var category in categories.Where(category => category.Value.Length > 0))
{
if ((_type == RestrictionType.Food) && (_rules.Pawn != null) && !category.Members.Any(member => _rules.Pawn.RaceProps.CanEverEat((ThingDef) member.Def))) { continue; }

_membersList.LabelTiny(category.Label.Bold());
_membersList.LabelTiny(category.Key.Bold());

foreach (var member in category.Members)
foreach (var member in category.Value)
{
if (_presetList.EditMode)
if (_presetList.EditMode) { _membersList.CheckboxLabeled(member.Def.LabelCap, ref member.Value, member.Def.description); }
else
{
_membersList.CheckboxLabeled(member.Def.LabelCap, ref member.Value, member.Def.description);
continue;
GUI.color = GuiPlus.ReadOnlyColor;
_membersList.CheckboxLabeled(member.Def.LabelCap, ref member.Value, member.Def.description, false);
GUI.color = _color;
}

if ((_type == RestrictionType.Food) && (_rules.Pawn != null) && !_rules.Pawn.RaceProps.CanEverEat((ThingDef) member.Def)) { continue; }

GUI.color = GuiPlus.ReadOnlyColor;
_membersList.CheckboxLabeled(member.Def.LabelCap, ref member.Value, member.Def.description, false);
GUI.color = _color;
}

_membersList.Gap();
}

_membersList.Gap();
_membersList.End();
}
}
Expand Down
Loading

0 comments on commit 50cd71c

Please sign in to comment.