rimworld-animations-patch_m.../Source/Scripts/Defs/BodyAddonData.cs
AbstractConcept 001121649b v2.0.3
- Fixed a bug that was causing alien addons to not render when pawns were wearing apparel
2023-02-07 22:08:58 -06:00

211 lines
No EOL
9.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Verse;
using RimWorld;
using AlienRace;
using Rimworld_Animations;
using UnityEngine;
using HarmonyLib;
namespace Rimworld_Animations_Patch
{
public class BodyAddonData
{
public AlienPartGenerator.BodyAddon bodyAddon;
public BodyPartRecord bodyPartRecord;
public List<Vector3> bodyAddonOffsets = new List<Vector3>();
public bool alignsWithHead = false;
private Pawn pawn;
private string bodyType;
private PawnRenderFlags renderFlags;
private bool canDraw = false;
private bool bodyPartMissing = false;
public BodyAddonData(Pawn pawn, AlienPartGenerator.BodyAddon bodyAddon, bool isPortrait = false)
{
this.pawn = pawn;
this.bodyAddon = bodyAddon;
if (isPortrait)
{ renderFlags |= PawnRenderFlags.Portrait; }
bodyPartRecord = pawn.def?.race?.body?.AllParts?.FirstOrDefault(x => x.def == bodyAddon?.bodyPart);
alignsWithHead = bodyAddon.alignWithHead || (bodyPartRecord != null && bodyPartRecord.IsInGroup(BodyPartGroupDefOf.FullHead));
GenerateOffsets();
UpdateVisibility();
}
public void GenerateOffsets()
{
bodyType = pawn.story.bodyType.defName;
bodyAddonOffsets.Clear();
int bodyAddonIndex = (pawn.def as ThingDef_AlienRace).alienRace.generalSettings.alienPartGenerator.bodyAddons.IndexOf(bodyAddon);
AlienPartGenerator.AlienComp alienComp = pawn.GetComp<AlienPartGenerator.AlienComp>();
Graphic addonGraphic = alienComp.addonGraphics[bodyAddonIndex];
for (int i = 0; i < 4; i++)
{
Rot4 apparentRotation = new Rot4(i);
// Get basic offset for body addon
AlienPartGenerator.RotationOffset defaultOffsets = bodyAddon.defaultOffsets.GetOffset(apparentRotation);
Vector3 bodyTypeOffset = (defaultOffsets != null) ? defaultOffsets.GetOffset(renderFlags.FlagSet(PawnRenderFlags.Portrait), pawn.story.bodyType, pawn.story.headType) : Vector3.zero;
AlienPartGenerator.RotationOffset rotationOffsets = bodyAddon.offsets.GetOffset(apparentRotation);
Vector3 bodyAddonOffset = bodyTypeOffset + ((rotationOffsets != null) ? rotationOffsets.GetOffset(renderFlags.FlagSet(PawnRenderFlags.Portrait), pawn.story.bodyType, pawn.story.headType) : Vector3.zero);
// Offset private parts so that they render over tattoos but under apparel (rendering under tatoos looks weird)
if (bodyAddon.bodyPart == PatchBodyPartDefOf.Genitals || bodyAddon.bodyPart == PatchBodyPartDefOf.Chest || bodyAddon.bodyPart == PatchBodyPartDefOf.Anus || addonGraphic.path.Contains("belly") || addonGraphic.path.Contains("Belly"))
{
bodyAddonOffset.y = (bodyAddonOffset.y + 0.40f) / 1000f + 0.012f;
// Erected penises should be drawn over apparel
if (pawn.RaceProps.Humanlike &&
addonGraphic.path.Contains("penis", StringComparison.OrdinalIgnoreCase) &&
(addonGraphic.path.Contains("flaccid", StringComparison.OrdinalIgnoreCase) == false) &&
apparentRotation == Rot4.South)
{ bodyAddonOffset.y += 0.010f; }
}
// Otherwise use the standard offsets
else
{ bodyAddonOffset.y = 0.3f + bodyAddonOffset.y; }
// Draw addons infront of body
if (!bodyAddon.inFrontOfBody)
{ bodyAddonOffset.y *= -1f; }
// Adjust for facing
if (apparentRotation == Rot4.North)
{
if (bodyAddon.layerInvert)
{ bodyAddonOffset.y = -bodyAddonOffset.y; }
}
if (apparentRotation == Rot4.East)
{ bodyAddonOffset.x = -bodyAddonOffset.x; }
// Adjustment for body addons attached to the head that are not marked as such
if (alignsWithHead && bodyAddon.alignWithHead == false)
{ bodyAddonOffset -= pawn.Drawer.renderer.BaseHeadOffsetAt(apparentRotation); }
// Done
bodyAddonOffsets.Add(bodyAddonOffset);
}
}
public Vector3 GetOffset(Rot4 facing)
{
if (pawn.story.bodyType.defName != bodyType)
{ GenerateOffsets(); }
return bodyAddonOffsets[facing.AsInt];
}
public bool CanDraw()
{
return bodyPartMissing == false && SitutationalVisibiltyCheck() && (pawn.Drawer.renderer.graphics.apparelGraphics.Any() == false || canDraw);
}
public bool SitutationalVisibiltyCheck()
{
if (pawn == null || bodyAddon == null) return false;
if (renderFlags.FlagSet(PawnRenderFlags.Portrait) == false && pawn.CurrentBed()?.def.building.bed_showSleeperBody == false && bodyAddon.drawnInBed == false)
{ return false; }
if (renderFlags.FlagSet(PawnRenderFlags.Portrait) == false && (pawn.GetPosture() == PawnPosture.LayingOnGroundNormal || pawn.GetPosture() == PawnPosture.LayingOnGroundFaceUp) && bodyAddon.drawnOnGround == false)
{ return false; }
if (bodyAddon.backstoryRequirement != null && pawn.story?.AllBackstories?.Any((BackstoryDef x) => x == bodyAddon.backstoryRequirement) == false)
{ return false; }
if (bodyAddon.drawnDesiccated == false && pawn.Corpse?.GetRotStage() == RotStage.Dessicated)
{ return false; }
if (pawn.gender == Gender.Female && bodyAddon.drawForFemale == false || pawn.gender == Gender.Male && bodyAddon.drawForMale == false)
{ return false; }
if (bodyAddon.bodyTypeRequirement != null && pawn.story?.bodyType != bodyAddon.bodyTypeRequirement)
{ return false; }
return true;
}
public void UpdateVisibility()
{
canDraw = true;
if (pawn == null || bodyAddon == null || bodyPartRecord == null) return;
if (pawn.health?.hediffSet?.GetNotMissingParts()?.Contains(bodyPartRecord) == false)
{ bodyPartMissing = true; return; }
if (pawn?.apparel?.WornApparel == null || pawn.apparel.WornApparel.NullOrEmpty())
{ return; }
foreach (Apparel apparel in pawn.apparel.WornApparel)
{
CompApparelVisibility comp = apparel?.TryGetComp<CompApparelVisibility>();
if (comp == null) continue;
LoadRimNudeData(comp);
if (comp.isBeingWorn == false) continue;
if (bodyAddon.bodyPart == PatchBodyPartDefOf.Genitals ||
bodyAddon.bodyPart == PatchBodyPartDefOf.Anus ||
bodyAddon.bodyPart == PatchBodyPartDefOf.Chest ||
bodyAddon.hediffGraphics?.Any(x => x.path.NullOrEmpty() == false && x.path.Contains("belly", StringComparison.OrdinalIgnoreCase)) == true)
{
if ((bodyAddon.bodyPart == PatchBodyPartDefOf.Genitals || bodyAddon.bodyPart == PatchBodyPartDefOf.Anus) && comp.coversGroin)
{ canDraw = false; return; };
if (bodyAddon.bodyPart == PatchBodyPartDefOf.Chest && comp.coversChest)
{ canDraw = false; return; };
if (bodyAddon.hediffGraphics?.Any(x => x.path.NullOrEmpty() == false && x.path.Contains("belly", StringComparison.OrdinalIgnoreCase)) == true && comp.coversBelly)
{ canDraw = false; return; }
}
else if (apparel.def.apparel.hatRenderedFrontOfFace || apparel.def.apparel.hatRenderedAboveBody || apparel.def.apparel.hatRenderedBehindHead)
{ return; }
else
{
if (bodyAddon.hiddenUnderApparelFor?.Any(x => apparel.def.apparel.bodyPartGroups?.Contains(x) == true) == true)
{ canDraw = false; return; };
if (bodyAddon.hiddenUnderApparelTag?.Any(x => apparel.def.apparel.tags?.Contains(x) == true) == true)
{ canDraw = false; return; };
}
}
}
public void LoadRimNudeData(CompApparelVisibility comp)
{
if (comp?.rimNudeDataStatus == RimNudeDataStatus.Unavailable)
{ return; }
if (comp?.rimNudeDataStatus == RimNudeDataStatus.NotLoaded)
{
RimNudeData rimNudeData = ApparelSettings.GetRimNudeData(comp?.apparel);
if (rimNudeData == null)
{
comp.rimNudeDataStatus = RimNudeDataStatus.Unavailable;
return;
}
comp.coversBelly = rimNudeData.coversBelly;
comp.coversChest = rimNudeData.coversChest;
comp.coversGroin = rimNudeData.coversGroin;
comp.rimNudeDataStatus = RimNudeDataStatus.Loaded;
}
}
}
}