Skip to content

Commit

Permalink
Merge pull request #87 from RimWorldCCLTeam/development
Browse files Browse the repository at this point in the history
v0.13.1.1 - Bug fixes
  • Loading branch information
ForsakenShell committed Apr 27, 2016
2 parents 628120d + af5d0cc commit 61907ab
Show file tree
Hide file tree
Showing 46 changed files with 532 additions and 3,119 deletions.
521 changes: 256 additions & 265 deletions DLL_Project/CommunityCoreLibrary.csproj

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion DLL_Project/Defs/ModHelperDef/ThingComps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,18 @@ public bool Inject( ModHelperDef def )
// TODO: Make a full copy using the comp in this def as a template
// Currently adds the comp in this def so all target use the same def
var targetDef = DefDatabase< ThingDef >.GetNamed( targetName );
targetDef.comps.Add( compSet.compProps );
if( targetDef.HasComp( compSet.compProps.compClass ) )
{
CCL_Log.TraceMod(
def,
Verbosity.Warnings,
string.Format( "Trying to inject ThingComp '{0}' into '{1}' when it already exists (another mod may have already injected).", compSet.compProps.compClass.ToString(), targetName ),
"ThingComp Injector" );
}
else
{
targetDef.comps.Add( compSet.compProps );
}
}
}

Expand Down
94 changes: 59 additions & 35 deletions DLL_Project/Detours/JobDriver_SocialRelax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ namespace CommunityCoreLibrary.Detour
internal static class _JobDriver_SocialRelax
{

internal const TargetIndex GatherSpotInd = TargetIndex.A;
internal const TargetIndex OccupyInd = TargetIndex.B;
internal const TargetIndex AlcoholInd = TargetIndex.C;
internal const TargetIndex DispenserInd = TargetIndex.C;
internal const TargetIndex GatherSpotParentInd = TargetIndex.A;
internal const TargetIndex ChairOrSpotInd = TargetIndex.B;
internal const TargetIndex OptionalDrinkInd = TargetIndex.C;

internal static FieldInfo _GetPawnDrawTracker;

Expand All @@ -47,38 +46,64 @@ internal static Pawn_DrawTracker GetPawnDrawTracker( this Pawn pawn )

#region Helper Methods

internal static bool AlcoholOrDispenser( this JobDriver_SocialRelax obj )
internal static Thing GatherSpotParent( this JobDriver_SocialRelax obj )
{
if( obj.Alcohol() != null )
{
return true;
}
return( obj.Dispenser() != null );
return obj.pawn.CurJob.GetTarget( GatherSpotParentInd ).Thing;
}

internal static IntVec3 ClosestGatherSpotParentCell( this JobDriver_SocialRelax obj )
{
return obj.GatherSpotParent().OccupiedRect().ClosestCellTo( obj.pawn.Position );
}

internal static Thing GatherSpot( this JobDriver_SocialRelax obj )
internal static bool HasChair( this JobDriver_SocialRelax obj )
{
return obj.pawn.CurJob.GetTarget( GatherSpotInd ).Thing;
return obj.pawn.CurJob.GetTarget( ChairOrSpotInd ).HasThing;
}

internal static IntVec3 OccupySpot( this JobDriver_SocialRelax obj )
{
return obj.pawn.CurJob.GetTarget( OccupyInd ).Cell;
return obj.pawn.CurJob.GetTarget( ChairOrSpotInd ).Cell;
}

internal static Thing OccupyThing( this JobDriver_SocialRelax obj )
{
return obj.pawn.CurJob.GetTarget( OccupyInd ).Thing;
return obj.pawn.CurJob.GetTarget( ChairOrSpotInd ).Thing;
}

internal static bool IsDrink( this JobDriver_SocialRelax obj )
{
if( !obj.pawn.CurJob.GetTarget( OptionalDrinkInd ).HasThing )
{
return false;
}
var thing = obj.pawn.CurJob.GetTarget( OptionalDrinkInd ).Thing;
return thing.def.IsAlcohol();
}

internal static bool IsDispenser( this JobDriver_SocialRelax obj )
{
if( !obj.pawn.CurJob.GetTarget( OptionalDrinkInd ).HasThing )
{
return false;
}
var thing = obj.pawn.CurJob.GetTarget( OptionalDrinkInd ).Thing;
return thing is Building_AutomatedFactory;
}

internal static bool HasDrinkOrDispenser( this JobDriver_SocialRelax obj )
{
return obj.pawn.CurJob.GetTarget( OptionalDrinkInd ).HasThing;
}

internal static Thing Alcohol( this JobDriver_SocialRelax obj )
{
return obj.pawn.CurJob.GetTarget( AlcoholInd ).Thing;
return obj.pawn.CurJob.GetTarget( OptionalDrinkInd ).Thing;
}

internal static Thing Dispenser( this JobDriver_SocialRelax obj )
internal static Building_AutomatedFactory Dispenser( this JobDriver_SocialRelax obj )
{
return obj.pawn.CurJob.GetTarget( DispenserInd ).Thing;
return obj.pawn.CurJob.GetTarget( OptionalDrinkInd ).Thing as Building_AutomatedFactory;
}

#endregion
Expand All @@ -87,40 +112,39 @@ internal static Thing Dispenser( this JobDriver_SocialRelax obj )

internal static IEnumerable<Toil> _MakeNewToils( this JobDriver_SocialRelax obj )
{
obj.EndOnDespawnedOrNull( GatherSpotInd, JobCondition.Incompletable );
obj.EndOnDespawnedOrNull( GatherSpotParentInd, JobCondition.Incompletable );

if( obj.OccupyThing() != null )
if( obj.HasChair() )
{
obj.EndOnDespawnedOrNull( OccupyInd, JobCondition.Incompletable );
obj.EndOnDespawnedOrNull( ChairOrSpotInd, JobCondition.Incompletable );
}
yield return Toils_Reserve.Reserve( OccupyInd, 1 );
yield return Toils_Reserve.Reserve( ChairOrSpotInd, 1 );

if( obj.AlcoholOrDispenser() )
if( obj.HasDrinkOrDispenser() )
{
if( obj.Dispenser() is Building_AutomatedFactory )
obj.FailOnDestroyedNullOrForbidden( OptionalDrinkInd );
yield return Toils_Reserve.Reserve( OptionalDrinkInd, 1 );
if( obj.IsDispenser() )
{
obj.EndOnDespawnedOrNull( DispenserInd, JobCondition.Incompletable );
yield return Toils_Goto.GotoThing( DispenserInd, PathEndMode.InteractionCell );
yield return Toils_FoodSynthesizer.TakeAlcoholFromSynthesizer( AlcoholInd, obj.pawn );
yield return Toils_Goto.GotoThing( OptionalDrinkInd, PathEndMode.InteractionCell );
yield return Toils_FoodSynthesizer.TakeAlcoholFromSynthesizer( OptionalDrinkInd, obj.pawn );
}
else
{
obj.FailOnDestroyedNullOrForbidden( AlcoholInd );
yield return Toils_Reserve.Reserve( AlcoholInd, 1 );
yield return Toils_Goto.GotoThing( AlcoholInd, PathEndMode.OnCell );
yield return Toils_Haul.StartCarryThing( AlcoholInd );
yield return Toils_Goto.GotoThing( OptionalDrinkInd, PathEndMode.OnCell );
yield return Toils_Haul.StartCarryThing( OptionalDrinkInd );
}
}

yield return Toils_Goto.GotoCell( OccupyInd, PathEndMode.OnCell );
yield return Toils_Goto.GotoCell( ChairOrSpotInd, PathEndMode.OnCell );

var pawnDrawer = obj.pawn.GetPawnDrawTracker();
var pawnFaceTarget = obj.GatherSpot().OccupiedRect().ClosestCellTo( obj.pawn.Position );
var relax = new Toil();
relax.defaultCompleteMode = ToilCompleteMode.Delay;
relax.defaultDuration = obj.pawn.CurJob.def.joyDuration;
relax.tickAction = new Action( () =>
{
var pawnDrawer = obj.pawn.GetPawnDrawTracker();
var pawnFaceTarget = obj.ClosestGatherSpotParentCell();
pawnDrawer.rotator.FaceCell( pawnFaceTarget );
obj.pawn.GainComfortFromCellIfPossible();
JoyUtility.JoyTickCheckEnd( obj.pawn, JoyTickFullJoyAction.GoToNextToil, 1f );
Expand All @@ -132,9 +156,9 @@ internal static IEnumerable<Toil> _MakeNewToils( this JobDriver_SocialRelax obj
relax.socialMode = RandomSocialMode.SuperActive;
yield return relax;

if( obj.Alcohol() != null )
if( obj.IsDrink() )
{
yield return Toils_Ingest.FinalizeIngest( obj.pawn, AlcoholInd );
yield return Toils_Ingest.FinalizeIngest( obj.pawn, OptionalDrinkInd );
}
}

Expand Down
94 changes: 75 additions & 19 deletions DLL_Project/Detours/JoyGiver_SocialRelax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,101 @@ namespace CommunityCoreLibrary.Detour
internal static class _JoyGiver_SocialRelax
{

private const float _GatherRadius = 3.9f;
private static List<CompGatherSpot> _workingSpots;
private static readonly int _NumRadiusCells;
private static readonly List<IntVec3> _RadialPatternMiddleOutward;
internal const float _GatherRadius = 3.9f;
internal static FieldInfo _workingSpots;
internal static FieldInfo _NumRadiusCells;
internal static FieldInfo _RadialPatternMiddleOutward;

static _JoyGiver_SocialRelax()
#region Reflected Methods

internal static List<CompGatherSpot> workingSpots()
{
_workingSpots = typeof( JoyGiver_SocialRelax ).GetField( "workingSpots", BindingFlags.Static | BindingFlags.NonPublic ).GetValue( null ) as List<CompGatherSpot>;
_NumRadiusCells = GenRadial.NumCellsInRadius( _GatherRadius );
_RadialPatternMiddleOutward = typeof( JoyGiver_SocialRelax ).GetField( "RadialPatternMiddleOutward", BindingFlags.Static | BindingFlags.NonPublic ).GetValue( null ) as List<IntVec3>;
if( _workingSpots == null )
{
_workingSpots = typeof( JoyGiver_SocialRelax ).GetField( "workingSpots", BindingFlags.Static | BindingFlags.NonPublic );
if( _workingSpots == null )
{
CCL_Log.Trace(
Verbosity.FatalErrors,
"Unable to get field 'workingSpots' in 'JoyGiver_SocialRelax'",
"Internal Detours" );
}
}
return (List<CompGatherSpot>) _workingSpots.GetValue( null );
}

internal static int NumRadiusCells()
{
if( _NumRadiusCells == null )
{
_NumRadiusCells = typeof( JoyGiver_SocialRelax ).GetField( "NumRadiusCells", BindingFlags.Static | BindingFlags.NonPublic );
if( _NumRadiusCells == null )
{
CCL_Log.Trace(
Verbosity.FatalErrors,
"Unable to get field 'NumRadiusCells' in 'JoyGiver_SocialRelax'",
"Internal Detours" );
}
}
return (int) _NumRadiusCells.GetValue( null );
}

internal static List<IntVec3> RadialPatternMiddleOutward()
{
if( _RadialPatternMiddleOutward == null )
{
_RadialPatternMiddleOutward = typeof( JoyGiver_SocialRelax ).GetField( "RadialPatternMiddleOutward", BindingFlags.Static | BindingFlags.NonPublic );
if( _RadialPatternMiddleOutward == null )
{
CCL_Log.Trace(
Verbosity.FatalErrors,
"Unable to get field 'RadialPatternMiddleOutwards' in 'JoyGiver_SocialRelax'",
"Internal Detours" );
}
}
return (List<IntVec3>) _RadialPatternMiddleOutward.GetValue( null );
}

#endregion

#region Detoured Methods

internal static Job _TryGiveJobInt( this JoyGiver_SocialRelax obj, Pawn pawn, Predicate<CompGatherSpot> gatherSpotValidator )
{
var JoyGiver_SocialRelax_TryUseThing = new _JoyGiver_SocialRelax._TryUseThing();
JoyGiver_SocialRelax_TryUseThing.pawn = pawn;

if( GatherSpotLister.activeSpots.Count == 0 )
if( GatherSpotLister.activeSpots.NullOrEmpty() )
{
return (Job) null;
}

_workingSpots.Clear();
var workingSpots = _JoyGiver_SocialRelax.workingSpots();
var NumRadiusCells = _JoyGiver_SocialRelax.NumRadiusCells();
var RadialPatternMiddleOutward = _JoyGiver_SocialRelax.RadialPatternMiddleOutward();

workingSpots.Clear();
for( int index = 0; index < GatherSpotLister.activeSpots.Count; ++index )
{
_workingSpots.Add( GatherSpotLister.activeSpots[ index ] );
workingSpots.Add( GatherSpotLister.activeSpots[ index ] );
}

CompGatherSpot compGatherSpot;
while( GenCollection.TryRandomElement<CompGatherSpot>( _workingSpots, out compGatherSpot ) )
while( GenCollection.TryRandomElement<CompGatherSpot>( workingSpots, out compGatherSpot ) )
{
_workingSpots.Remove( compGatherSpot );
workingSpots.Remove( compGatherSpot );
if(
( !( (Thing)compGatherSpot.parent ).IsForbidden( pawn ) )&&
( !compGatherSpot.parent.IsForbidden( pawn ) )&&
( pawn.CanReach(
compGatherSpot.parent,
PathEndMode.Touch,
Danger.None,
false ) )&&
( compGatherSpot.parent.IsSociallyProper( pawn ) )&&
( gatherSpotValidator == null )||
( gatherSpotValidator( compGatherSpot ) )
(
( gatherSpotValidator == null )||
( gatherSpotValidator( compGatherSpot ) )
)
)
{
Job job = (Job) null;
Expand All @@ -80,9 +134,9 @@ internal static Job _TryGiveJobInt( this JoyGiver_SocialRelax obj, Pawn pawn, Pr
}
else
{
for( int index = 0; index < _RadialPatternMiddleOutward.Count; ++index)
for( int index = 0; index < RadialPatternMiddleOutward.Count; ++index)
{
Building sittableThing = ( compGatherSpot.parent.Position + _RadialPatternMiddleOutward[ index ] ).GetEdifice();
Building sittableThing = ( compGatherSpot.parent.Position + RadialPatternMiddleOutward[ index ] ).GetEdifice();
if(
( sittableThing != null )&&
( sittableThing.def.building.isSittable )&&
Expand All @@ -109,7 +163,7 @@ internal static Job _TryGiveJobInt( this JoyGiver_SocialRelax obj, Pawn pawn, Pr
{
for( int index = 0; index < 30; ++index )
{
IntVec3 occupySpot = compGatherSpot.parent.Position + GenRadial.RadialPattern[ Rand.Range( 1, _NumRadiusCells ) ];
IntVec3 occupySpot = compGatherSpot.parent.Position + GenRadial.RadialPattern[ Rand.Range( 1, NumRadiusCells ) ];
if(
( pawn.CanReserveAndReach(
occupySpot,
Expand Down Expand Up @@ -204,6 +258,8 @@ internal bool CanUseThing( Thing t )
}
}

#endregion

}

}
Loading

0 comments on commit 61907ab

Please sign in to comment.