Skip to content

Commit

Permalink
Randomization update
Browse files Browse the repository at this point in the history
- Randomize all regenerates whole pawn.
- Head types are restricted to gender and allowed random heads.
- Switched body randomization to vanilla method.
  • Loading branch information
ISOR3X committed Apr 22, 2024
1 parent d6e7b5b commit 16cfc54
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 75 deletions.
Binary file modified Assemblies/PawnEditor.dll
Binary file not shown.
65 changes: 19 additions & 46 deletions Source/PawnEditor/Tabs/Humanlike/Bio/Randomization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,30 @@ public partial class TabWorker_Bio_Humanlike
{
public override IEnumerable<FloatMenuOption> GetRandomizationOptions(Pawn pawn)
{
// yield return new("PawnEditor.All".Translate(), () => RandomizeAll(pawn));
yield return new("PawnEditor.All".Translate(), () => RandomizeAll(pawn));
yield return new("Appearance".Translate(), () => RandomizeAppearance(pawn));
yield return new("PawnEditor.Shape".Translate(), () => RandomizeShape(pawn));
// yield return new("PawnEditor.Shape".Translate(), () => RandomizeShape(pawn));
yield return new("Relations".Translate(), () => RandomizeRelations(pawn));
yield return new("Traits".Translate(), () => RandomizeTraits(pawn));
yield return new("Skills".Translate(), () => RandomizeSkills(pawn));
}

private static void RandomizeAll(Pawn pawn)
public static void RandomizeAll(Pawn pawn)
{
// RandomizeAppearance(pawn);
// RandomizeTraits(pawn);
// RandomizeRelations(pawn);
// RandomizeSkills(pawn);

var req = new PawnGenerationRequest(pawn.kindDef, pawn.Faction);
var newPawn = PawnGenerator.GeneratePawn(req);

pawn.skills = newPawn.skills;
pawn.story = newPawn.story;
pawn.relations = newPawn.relations;
pawn.abilities = newPawn.abilities;
pawn.style = newPawn.style;
pawn.apparel = newPawn.apparel;
pawn.inventory = newPawn.inventory;
pawn.equipment = newPawn.equipment;
pawn.health = newPawn.health;
pawn.mindState = newPawn.mindState;
pawn.ageTracker = newPawn.ageTracker;
pawn.gender = newPawn.gender;
pawn.royalty = newPawn.royalty;
pawn.genes = newPawn.genes;
pawn.ideo = newPawn.ideo;
pawn.Name = newPawn.Name;

// Delete
var oldPawn = pawn;
var position = oldPawn.Position;
var map = oldPawn.Map;
PawnEditor.PawnList.OnDelete(oldPawn);
// Replace
pawn = PawnGenerator.GeneratePawn(new PawnGenerationRequest(pawn.kindDef, PawnEditor.selectedFaction));
PawnEditor.AddPawn(pawn, PawnEditor.selectedCategory).HandleResult();
if (!PawnEditor.Pregame && map != null)
{
GenSpawn.Spawn(pawn, position, map);
PawnEditor.PawnList.UpdateCache(PawnEditor.selectedFaction, PawnEditor.selectedCategory);
}
TabWorker_FactionOverview.RecachePawns(PawnEditor.selectedFaction);
}

public static void RandomizeAppearance(Pawn pawn)
Expand Down Expand Up @@ -74,31 +63,15 @@ public static void RandomizeAppearance(Pawn pawn)

public static void RandomizeShape(Pawn pawn)
{
var bodyTypes = DefDatabase<BodyTypeDef>.AllDefs
.Where(bodyType =>
pawn.DevelopmentalStage switch
{
DevelopmentalStage.Baby or DevelopmentalStage.Newborn => bodyType == BodyTypeDefOf.Baby,
DevelopmentalStage.Child => bodyType == BodyTypeDefOf.Child,
DevelopmentalStage.Adult => bodyType != BodyTypeDefOf.Baby && bodyType != BodyTypeDefOf.Child,
_ => true
});

if (HARCompat.Active)
{
var allowedBodyTypes = HARCompat.AllowedBodyTypes(pawn);
if (!allowedBodyTypes.NullOrEmpty()) bodyTypes = bodyTypes.Intersect(allowedBodyTypes);
}

var headTypes = DefDatabase<HeadTypeDef>.AllDefs;
var headTypes = DefDatabase<HeadTypeDef>.AllDefs.Where(h => h.gender != pawn.gender.Opposite() && h.randomChosen);
if (HARCompat.Active)
{
headTypes = HARCompat.FilterHeadTypes(headTypes, pawn);
// HAR doesn't like head types not matching genders
headTypes = headTypes.Where(type => type.gender == Gender.None || type.gender == pawn.gender);
}

pawn.story.bodyType = bodyTypes.RandomElement();
pawn.story.bodyType = PawnGenerator.GetBodyTypeFor(pawn);
pawn.story.headType = headTypes.RandomElement();

pawn.drawer.renderer.SetAllGraphicsDirty();
Expand Down
2 changes: 1 addition & 1 deletion Source/PawnEditor/UI/LeftPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ AddResult AddPawnKind(PawnKindDef pawnKind) =>
Find.WindowStack.Add(new FloatMenu(list));
}

private static AddResult AddPawn(Pawn addedPawn, PawnCategory category)
public static AddResult AddPawn(Pawn addedPawn, PawnCategory category)
{
return new ConditionalInfo(CanUsePoints(addedPawn), new SuccessInfo(() =>
{
Expand Down
33 changes: 5 additions & 28 deletions Source/PawnEditor/UI/PawnEditorUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public static partial class PawnEditor
public static bool RenderHeadgear = true;
private static bool usePointLimit;
private static float remainingPoints;
private static Faction selectedFaction;
public static Faction selectedFaction;
private static Pawn selectedPawn;
private static bool showFactionInfo;
private static PawnCategory selectedCategory;
public static PawnCategory selectedCategory;
private static float cachedValue;
private static FloatMenuOption lastRandomization;
private static TabGroupDef tabGroup;
Expand Down Expand Up @@ -134,40 +134,17 @@ public static void DoBottomButtons(Rect inRect, Action onLeftButton, Action onRi
var randomRect = new Rect(Vector2.zero, Page.BottomButSize).CenteredOnXIn(inRect).CenteredOnYIn(inRect);

var buttonRect = new Rect(randomRect);

var options = GetRandomizationOptions().ToList();
if (lastRandomization != null && Widgets.ButtonImageWithBG(randomRect.TakeRightPart(20), TexUI.RotRightTex, new Vector2(12, 12)))
{
var label = lastRandomization.Label.ToLower();
lastRandomization = options.First(op => op.Label.Contains(label));
lastRandomization.action();
randomRect.TakeRightPart(1);
}

if (Widgets.ButtonText(randomRect, "Randomize".Translate()))
{
var options = GetRandomizationOptions().ToList();
if (!showFactionInfo && selectedPawn.RaceProps.Humanlike)
{
options.Insert(0, new FloatMenuOption("Randomize".Translate() + " " + "PawnEditor.All".Translate().ToLower(), () =>
{
// List, for updating left panel
var (pawns, _, _) = PawnList.GetLists();
// Copy values
var map = selectedPawn.Map;
var position = selectedPawn.Position;
var req = new PawnGenerationRequest(selectedPawn.kindDef, selectedPawn.Faction);
var index = pawns.IndexOf(selectedPawn);
// Delete
DeletePawn(selectedPawn, pawns);
TabWorker_FactionOverview.RecachePawns(selectedFaction);
PawnLister.FullyRemove(selectedPawn);
selectedPawn.Destroy();
// Replace
selectedPawn = PawnGenerator.GeneratePawn(req);
GenSpawn.Spawn(selectedPawn, position, map);
pawns.Insert(index, selectedPawn);
Select(selectedPawn);
}));
}

if (options.Count > 0)
Find.WindowStack.Add(new FloatMenu(options));
else
Expand Down

0 comments on commit 16cfc54

Please sign in to comment.