Skip to content

Commit

Permalink
Merge pull request #122 from RimWorldCCLTeam/development
Browse files Browse the repository at this point in the history
v0.14.0.1 - Bugfixes
  • Loading branch information
ForsakenShell authored Jul 17, 2016
2 parents 4e278d1 + 2a04e03 commit 3e2054a
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 25 deletions.
1 change: 1 addition & 0 deletions DLL_Project/CommunityCoreLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@
<Compile Include="Detours\JobGiver_BingeAlcohol.cs" />
<Compile Include="Detours\ThingListGroupHelper.cs" />
<Compile Include="Detours\GenConstruct.cs" />
<Compile Include="Detours\WorkGiver_Warden_DeliverFood.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
1 change: 1 addition & 0 deletions DLL_Project/Controller/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static class Data

public static MainMonoBehaviour cclMonoBehaviour;

public static string cclModIdentifier = string.Empty;
public static ModContentPack cclMod;
public static ModHelperDef cclHelperDef;

Expand Down
21 changes: 11 additions & 10 deletions DLL_Project/Detours/FoodUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ internal static Thing SpawnedFoodSearchInnerScan( Pawn eater, IntV

#endregion

#if DEVELOPER
internal static void DumpThingsRequestedForGroup( ThingRequest thingRequest, List<Thing> thingsRequested )
{
var str = string.Format( "ListerThings.ThingsMatching( {0} ) ::\n", thingRequest );
Expand All @@ -59,6 +60,7 @@ internal static void DumpThingsRequestedForGroup( ThingRequest th
}
CCL_Log.Message( str );
}
#endif

internal static bool _GetFoodDefAlcohol;
internal static ThingDef _GetFoodDef( Thing foodSource )
Expand Down Expand Up @@ -277,14 +279,12 @@ internal static Thing _BestFoodSourceOnMap( Pawn getter, Pawn eate
desperate
? FoodPreferability.DesperateOnly
:
( !eater.RaceProps.Humanlike
? FoodPreferability.NeverForNutrition
:
( eater.needs.food.CurCategory <= HungerCategory.UrgentlyHungry
? FoodPreferability.RawBad
: FoodPreferability.MealAwful
)
);
!eater.RaceProps.Humanlike
? FoodPreferability.NeverForNutrition
:
eater.needs.food.CurCategory >= HungerCategory.UrgentlyHungry
? FoodPreferability.RawBad
: FoodPreferability.MealAwful;

var thingRequest =
(
Expand Down Expand Up @@ -339,7 +339,7 @@ internal static Thing _BestFoodSourceOnMap( Pawn getter, Pawn eate
false );
if( potentialFoodSource == null )
{
Log.Message( "Non-humanlike closest reachable desperate..." );
//CCL_Log.Message( "Non-humanlike closest reachable desperate..." );
validator.desperate = true;
potentialFoodSource = GenClosest.ClosestThingReachable(
getter.Position,
Expand Down Expand Up @@ -389,12 +389,13 @@ internal bool ValidateFast( Thing t )
return false;
}
if(
( t.Faction != null )&&
( t.Faction != getter.Faction )&&
( t.Faction != getter.HostFaction )
)
{
Profiler.EndSample();
//CCL_Log.Message( string.Format( "{0} cannot use {1} because it is the wrong faction", getter.LabelShort, t.ThingID ) );
//CCL_Log.Message( string.Format( "{0} cannot use {1} because it is the wrong faction - Faction for {1} is {2} - Faction for {0} is {3}, host is {4}", getter.LabelShort, t.ThingID, t.Faction?.Name, getter.Faction?.Name, getter.HostFaction?.Name ) );
return false;
}
if( !t.IsSociallyProper( getter ) )
Expand Down
143 changes: 143 additions & 0 deletions DLL_Project/Detours/WorkGiver_Warden_DeliverFood.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using System.Linq;

using RimWorld;
using Verse;

namespace CommunityCoreLibrary.Detour
{

internal static class _WorkGiver_Warden_DeliverFood
{

#region Helper Methods

internal static float NutritionAvailableForFrom( Pawn p, Thing foodSource )
{
if(
( foodSource.def.IsNutritionGivingIngestible )&&
( p.RaceProps.WillAutomaticallyEat( foodSource ) )
)
{
return foodSource.def.ingestible.nutrition * (float) foodSource.stackCount;
}
if(
( p.RaceProps.ToolUser )&&
( p.health.capacities.CapableOf( PawnCapacityDefOf.Manipulation ) )&&
( foodSource.def.IsFoodMachine() )
)
{
if( foodSource is Building_NutrientPasteDispenser )
{
var NPD = foodSource as Building_NutrientPasteDispenser;
if( NPD.CanDispenseNow )
{
return 99999f;
}
}
if( foodSource is Building_AutomatedFactory )
{
var FS = foodSource as Building_AutomatedFactory;
var foodDef = FS.BestProduct( FoodSynthesis.IsMeal, FoodSynthesis.SortMeal );
if( foodDef != null )
{
return 99999f;
}
}
}
return 0.0f;
}

#endregion

#region Reflected Methods

internal static bool _FoodAvailableInRoomTo( Pawn prisoner )
{
if(
( prisoner.carrier.CarriedThing != null )&&
( NutritionAvailableForFrom( prisoner, prisoner.carrier.CarriedThing ) > 0.0f )
)
{
//Log.Message( "Prisoner is carrying food" );
return true;
}
var neededNutrition = 0.0f;
var foodNutrition = 0.0f;
var room = prisoner.Position.GetRoom();
if( room == null )
{ // This should never actually happen...
//Log.Message( "Prisoner is not in a room!" );
return false;
}
for( int regionIndex = 0; regionIndex < room.RegionCount; ++regionIndex )
{
var region = room.Regions[ regionIndex ];
var foodSources = region.ListerThings.ThingsInGroup( ThingRequestGroup.FoodSourceNotPlantOrTree );
if(
( prisoner.health.capacities.CapableOf( PawnCapacityDefOf.Manipulation ) )&&
( foodSources.Any( (source) =>
{
if( source.def.IsFoodMachine() )
{
if(
( source is Building_NutrientPasteDispenser )&&
( ((Building_NutrientPasteDispenser)source).CanDispenseNow )
)
{
return true;
}
if(
( source is Building_AutomatedFactory )&&
( ((Building_AutomatedFactory)source).BestProduct( FoodSynthesis.IsMeal, FoodSynthesis.SortMeal ) != null )
)
{
return true;
}
}
return false;
} ) )
)
{
//Log.Message( "Prisoner has access to a stocked food machine" );
return true;
}
for( int foodIndex = 0; foodIndex < foodSources.Count; ++foodIndex )
{
var foodSource = foodSources[ foodIndex ];
if(
( foodSource.def.IsFoodMachine() )||
(
( foodSource.def.IsNutritionGivingIngestible )&&
( foodSource.def.ingestible.preferability > FoodPreferability.NeverForNutrition )
)
)
{
foodNutrition += NutritionAvailableForFrom( prisoner, foodSource );
}
}
var pawns = region.ListerThings.ThingsInGroup( ThingRequestGroup.Pawn );
for( int pawnIndex = 0; pawnIndex < pawns.Count; ++pawnIndex )
{
var pawn = pawns[ pawnIndex ] as Pawn;
if(
( pawn.IsPrisonerOfColony )&&
( pawn.needs.food.CurLevelPercentage < ( pawn.needs.food.PercentageThreshHungry + 0.0199999995529652 ) )&&
(
( pawn.carrier.CarriedThing == null )||
( !pawn.RaceProps.WillAutomaticallyEat( pawn.carrier.CarriedThing ) )
)
)
{
neededNutrition += pawn.needs.food.NutritionWanted;
}
}
}
//Log.Message( string.Format( "return {0} + 0.5f >= {1};", foodNutrition, neededNutrition ) );
return foodNutrition + 0.5f >= neededNutrition;
}

#endregion

}

}
1 change: 1 addition & 0 deletions DLL_Project/Extensions/ResearchProjectDef_Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ internal static bool _IsLockedOut( this ResearchProjectDef researchProjectDef, R
foreach ( var p in researchProjectDef.prerequisites )
{
if (
( p.defName == researchProjectDef.defName )||
( p.defName == initialDef.defName )||
( p._IsLockedOut( initialDef ) )
)
Expand Down
36 changes: 36 additions & 0 deletions DLL_Project/Extensions/Room_Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public static class Room_Extensions

internal static FieldInfo _cachedCellCount;
internal static FieldInfo _cachedOpenRoofCount;
internal static FieldInfo _statsAndRoleDirty;

#region Cached Cell Count

public static int CachedCellCountGet( this Room room )
{
Expand All @@ -23,6 +26,7 @@ public static int CachedCellCountGet( this Room room )
}
return (int)_cachedCellCount.GetValue( room );
}

public static void CachedCellCountSet( this Room room, int value )
{
if( _cachedCellCount == null )
Expand All @@ -32,6 +36,10 @@ public static void CachedCellCountSet( this Room room, int valu
_cachedCellCount.SetValue( room, value );
}

#endregion

#region Cached Open Roof Count

public static int CachedOpenRoofCountGet( this Room room )
{
if( _cachedOpenRoofCount == null )
Expand All @@ -50,6 +58,32 @@ public static void CachedOpenRoofCountSet( this Room room, int
_cachedOpenRoofCount.SetValue( room, value );
}

#endregion

#region Stats and Role Dirty Flag

public static bool StatsAndRoleDirtyGet( this Room room )
{
if( _statsAndRoleDirty == null )
{
_statsAndRoleDirty = typeof( Room ).GetField( "statsAndRoleDirty", BindingFlags.Instance | BindingFlags.NonPublic );
}
return (bool)_statsAndRoleDirty.GetValue( room );
}

public static void StatsAndRoleDirtySet( this Room room, bool value )
{
if( _statsAndRoleDirty == null )
{
_statsAndRoleDirty = typeof( Room ).GetField( "statsAndRoleDirty", BindingFlags.Instance | BindingFlags.NonPublic );
}
_statsAndRoleDirty.SetValue( room, value );
}

#endregion

#region Portal Enumeration

public static List<Building_Door> Portals( this Room room )
{
var portals = new List<Building_Door>();
Expand All @@ -63,6 +97,8 @@ public static List<Building_Door> Portals( this Room room )
return portals;
}

#endregion

}

}
21 changes: 16 additions & 5 deletions DLL_Project/Extensions/ThingDef_Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,23 @@ public static void RecacheRecipes( this ThingDef thingDef, bool

public static bool IsFoodMachine( this ThingDef thingDef )
{
if(
( typeof( Building_NutrientPasteDispenser ).IsAssignableFrom( thingDef.thingClass ) )||
( typeof( Building_AutomatedFactory ).IsAssignableFrom( thingDef.thingClass ) )
)
if( typeof( Building_NutrientPasteDispenser ).IsAssignableFrom( thingDef.thingClass ) )
{
return thingDef.building.isMealSource;
return true;
}
if( typeof( Building_AutomatedFactory ).IsAssignableFrom( thingDef.thingClass ) )
{ // Make sure we are only return factories which are configured as food synthesizers
var propsFactory = thingDef.GetCompProperty<CompProperties_AutomatedFactory>();
if( propsFactory != null )
{
if(
( propsFactory.outputVector == FactoryOutputVector.DirectToPawn )&&
( propsFactory.productionMode == FactoryProductionMode.PawnInteractionOnly )
)
{
return true;
}
}
}
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions DLL_Project/PostBuild.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ REM This is a local file copy after build. Get it once and your .gitnore
REM should handle it after that. Make all your local copies at the end.

REM Set this to your local RimWorld install path and CCL Assemblies directory
REM Example:
REM Set InstalledCCLAssemblies="C:\Games\Steam\steamapps\common\RimWorld\Mods\Community Core Library\Assemblies"
Set InstalledCCLAssemblies="/badkarma/"

if NOT EXIST %InstalledCCLAssemblies% (
Expand Down
6 changes: 6 additions & 0 deletions DLL_Project/SpecialInjectors/DetourInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ public override bool Inject()
if( !Detours.TryDetourFromTo( RimWorld_GenConstruct_CanBuildOnTerrain, CCL_GenConstruct_CanBuildOnTerrain ) )
return false;

// Detour RimWorld.WorkGiver_Warden_DeliverFood.FoodAvailableInRoomTo
MethodInfo RimWorld_WorkGiver_Warden_DeliverFood_FoodAvailableInRoomTo = typeof( WorkGiver_Warden_DeliverFood ).GetMethod( "FoodAvailableInRoomTo", BindingFlags.Static | BindingFlags.NonPublic );
MethodInfo CCL_WorkGiver_Warden_DeliverFood_FoodAvailableInRoomTo = typeof( Detour._WorkGiver_Warden_DeliverFood ).GetMethod( "_FoodAvailableInRoomTo", BindingFlags.Static | BindingFlags.NonPublic );
if( !Detours.TryDetourFromTo( RimWorld_WorkGiver_Warden_DeliverFood_FoodAvailableInRoomTo, CCL_WorkGiver_Warden_DeliverFood_FoodAvailableInRoomTo ) )
return false;

/*
// Detour
MethodInfo foo = typeof( foo_class ).GetMethod( "foo_method", BindingFlags.Static | BindingFlags.NonPublic );
Expand Down
Loading

0 comments on commit 3e2054a

Please sign in to comment.