Skip to content

Commit

Permalink
Merge pull request #95 from RimWorldCCLTeam/development
Browse files Browse the repository at this point in the history
Warning: Minimaps!
  • Loading branch information
ForsakenShell committed May 18, 2016
2 parents 61907ab + fb5fbcd commit 986ab20
Show file tree
Hide file tree
Showing 171 changed files with 8,924 additions and 3,795 deletions.
82 changes: 55 additions & 27 deletions DLL_Project/Buildings/Building_AutomatedFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public Allowances( RecipeDef recipe, bool allowed )
}

private Dictionary<ThingDef,Allowances> productionAllowances;
private Dictionary<RecipeDef, bool> recipeAllowances;

private RecipeDef currentRecipe;

Expand All @@ -51,6 +52,7 @@ private List<IntVec3> AdjacentNeighbouringCells
{
get
{
//Log.Message( string.Format( "{0}.AdjacentNeighbouringCells()", this.ThingID ) );
if( _adjacentNeighbouringCells == null )
{
_adjacentNeighbouringCells = GenAdj.CellsAdjacentCardinal( this ).ToList();
Expand Down Expand Up @@ -113,20 +115,23 @@ public CompAutomatedFactory CompAutomatedFactory

public Building_AutomatedFactory()
{
//Log.Message( "Building_AutomatedFactory.cTor()" );
nextProductIndex = 0;
currentRecipe = null;
currentProductionTick = 0;
productionAllowances = new Dictionary<ThingDef, Allowances>();
recipeAllowances = new Dictionary<RecipeDef, bool>();
}

#endregion

#region Base Class Overrides

#if DEBUG
public override void SpawnSetup()
{
//Log.Message( string.Format( "{0}.SpawnSetup()", this.ThingID ) );
base.SpawnSetup();
#if DEBUG
if( CompAutomatedFactory == null )
{
CCL_Log.TraceMod(
Expand Down Expand Up @@ -159,53 +164,51 @@ public override void SpawnSetup()
"CompAutomatedFactory.productionMode is invalid" );
return;
}
}
#endif

}

public override void ExposeData()
{
//Log.Message( string.Format( "Building_AutomatedFactory.ExposeData( {0} )", Scribe.mode.ToString() ) );
// Scribe base data
base.ExposeData();

// Scribe current recipe
string recipe = string.Empty;
if( currentRecipe != null )
{
recipe = currentRecipe.defName;
}
Scribe_Values.LookValue<string>( ref recipe, "currentRecipe", string.Empty );
if( !recipe.NullOrEmpty() )
{
currentRecipe = DefDatabase<RecipeDef>.GetNamed( recipe, true );
}

// Scribe current production tick
Scribe_Values.LookValue<int>( ref currentProductionTick, "currentProductionTick", 0 );

// Scribe recipe allowances
var pa = new Dictionary<RecipeDef, bool>();
if( Scribe.mode == LoadSaveMode.Saving )
{
if( currentRecipe != null )
{
recipe = currentRecipe.defName;
}
foreach( var entry in productionAllowances )
{
if( !pa.ContainsKey( entry.Value.recipe ) )
if( !recipeAllowances.ContainsKey( entry.Value.recipe ) )
{
pa.Add( entry.Value.recipe, entry.Value.allowed );
recipeAllowances.Add( entry.Value.recipe, entry.Value.allowed );
}
}
}
Scribe_Collections.LookDictionary<RecipeDef,bool>( ref pa, "productionAllowances", LookMode.DefReference, LookMode.Value );
if( Scribe.mode == LoadSaveMode.LoadingVars )

// Scribe data
Scribe_Values.LookValue<string>( ref recipe, "currentRecipe", string.Empty );
Scribe_Values.LookValue<int>( ref currentProductionTick, "currentProductionTick", 0 );
Scribe_Collections.LookDictionary<RecipeDef,bool>( ref recipeAllowances, "productionAllowances", LookMode.DefReference, LookMode.Value );
Scribe_Deep.LookDeep<Thing>( ref currentThing, "currentThing", null );

// Resolve cross-references
if( Scribe.mode == LoadSaveMode.ResolvingCrossRefs )
{
foreach( var pair in pa )
if( !recipe.NullOrEmpty() )
{
currentRecipe = DefDatabase<RecipeDef>.GetNamed( recipe, true );
}
foreach( var pair in recipeAllowances )
{
var key = pair.Key.products[0].thingDef;
SetAllowed( pair.Key, pair.Value );
}
ResetAndReprogramHoppers();
}

// Scribe current thing
Scribe_Deep.LookDeep<Thing>( ref currentThing, "currentThing", null );
}

public override void Tick()
Expand Down Expand Up @@ -248,6 +251,7 @@ public override string GetInspectString()

private void ProductionTick( int ticks )
{
//Log.Message( string.Format( "{0}.ProductionTick( {1} )", this.ThingID, ticks ) );
if( !CompPowerTrader.PowerOn )
{
return;
Expand Down Expand Up @@ -306,6 +310,7 @@ private void ProductionTick( int ticks )

private void RescanTick()
{
//Log.Message( string.Format( "{0}.RescanTick()", this.ThingID ) );
if( currentRecipeCount != this.def.AllRecipes.Count )
{
ResetAndReprogramHoppers();
Expand All @@ -320,6 +325,7 @@ private void RescanTick()

public void ResetAndReprogramHoppers()
{
//Log.Message( string.Format( "{0}.ResetAndReprogramHoppers()", this.ThingID ) );
resourceFilter = null;
CompHopperUser.ResetResourceSettings();
CompHopperUser.FindAndProgramHoppers();
Expand All @@ -329,6 +335,7 @@ public ThingFilter ResourceFilter
{
get
{
//Log.Message( string.Format( "{0}.ResourceFilter()", this.ThingID ) );
if( def.AllRecipes.NullOrEmpty() )
{
CCL_Log.TraceMod(
Expand Down Expand Up @@ -396,6 +403,7 @@ public ThingFilter ResourceFilter

public void SetAllowed( ThingDef thingDef, bool allowed )
{
//Log.Message( string.Format( "{0}.SetAllowed( {1}, {2} )", this.ThingID, thingDef == null ? "null" : thingDef.defName, allowed ) );
Allowances allowance;
if( productionAllowances.TryGetValue( thingDef, out allowance ) )
{
Expand All @@ -410,11 +418,13 @@ public void SetAllowed( ThingDef thingDef, bool allowed

public void SetAllowed( RecipeDef recipeDef, bool allowed )
{
var inAllowed = allowed;
var product = recipeDef.products[ 0 ].thingDef;
allowed &= (
( recipeDef.researchPrerequisite == null )||
( recipeDef.researchPrerequisite.IsFinished )
);
//Log.Message( string.Format( "{0}.SetAllowed( {1}, {2}->{3} )", this.ThingID, recipeDef.defName, inAllowed, allowed ) );
Allowances allowance;
if( productionAllowances.TryGetValue( product, out allowance ) )
{
Expand All @@ -430,6 +440,7 @@ public void SetAllowed( RecipeDef recipeDef, bool allowe

public bool GetAllowed( ThingDef thingDef )
{
//Log.Message( string.Format( "{0}.GetAllowed( {1} )", this.ThingID, thingDef == null ? "null" : thingDef.defName ) );
Allowances allowance;
if( productionAllowances.TryGetValue( thingDef, out allowance ) )
{
Expand All @@ -440,6 +451,7 @@ public bool GetAllowed( ThingDef thingDef )

public bool GetAllowed( RecipeDef recipeDef )
{
//Log.Message( string.Format( "{0}.GetAllowed( {1} )", this.ThingID, recipeDef == null ? "null" : recipeDef.defName ) );
foreach( var pair in productionAllowances )
{
if( pair.Value.recipe == recipeDef )
Expand All @@ -452,6 +464,7 @@ public bool GetAllowed( RecipeDef recipeDef )

public Allowances GetAllowance( ThingDef thingDef )
{
//Log.Message( string.Format( "{0}.GetAllowance( {1} )", this.ThingID, thingDef == null ? "null" : thingDef.defName ) );
Allowances allowance;
if( productionAllowances.TryGetValue( thingDef, out allowance ) )
{
Expand All @@ -462,6 +475,7 @@ public Allowances GetAllowance( ThingDef thingDef )

public Allowances GetAllowance( RecipeDef recipeDef )
{
//Log.Message( string.Format( "{0}.GetAllowance( {1} )", this.ThingID, recipeDef == null ? "null" : recipeDef.defName ) );
foreach( var pair in productionAllowances )
{
if( pair.Value.recipe == recipeDef )
Expand All @@ -478,6 +492,7 @@ public Allowances GetAllowance( RecipeDef recipeDef )

private Thing NextProductToProduce()
{
//Log.Message( string.Format( "{0}.NextProductToProduce()", this.ThingID ) );
if( currentRecipe != null )
{
return null;
Expand Down Expand Up @@ -522,6 +537,7 @@ private Thing NextProductToProduce()

private bool OutputThingTo( out Thing stackWith, out IntVec3 dropCell )
{
//Log.Message( string.Format( "{0}.OutputThingTo()", this.ThingID ) );
stackWith = null;
dropCell = IntVec3.Invalid;
switch( CompAutomatedFactory.Properties.outputVector )
Expand Down Expand Up @@ -666,6 +682,7 @@ private bool OutputThingTo( out Thing stackWith, out IntV

private RecipeDef TryGetProductionReadyRecipeFor( ThingDef thingDef )
{
//Log.Message( string.Format( "{0}.TryGetProductionReadyRecipeFor( {1} )", this.ThingID, thingDef == null ? "null" : thingDef.defName ) );
Allowances allowance;
if( productionAllowances.TryGetValue( thingDef, out allowance ) )
{
Expand All @@ -690,6 +707,7 @@ private RecipeDef TryGetProductionReadyRecipeFor( ThingDef thi

public RecipeDef FindRecipeForProduct( ThingDef thingDef )
{
//Log.Message( string.Format( "{0}.FindRecipeForProduct( {1} )", this.ThingID, thingDef == null ? "null" : thingDef.defName ) );
Allowances allowance;
if( productionAllowances.TryGetValue( thingDef, out allowance ) )
{
Expand All @@ -706,6 +724,7 @@ public RecipeDef FindRecipeForProduct( ThingDef thingDef )

public int ProductionTicks( ThingDef thingDef )
{
//Log.Message( string.Format( "{0}.ProductionTicks( {1} )", this.ThingID, thingDef == null ? "null" : thingDef.defName ) );
var recipe = FindRecipeForProduct( thingDef );
if( recipe == null )
{
Expand All @@ -716,6 +735,7 @@ public int ProductionTicks( ThingDef thingDef )

public bool CanDispenseNow( ThingDef thingDef )
{
//Log.Message( string.Format( "{0}.CanDispenseNow( {1} )", this.ThingID, thingDef == null ? "null" : thingDef.defName ) );
if( CompPowerTrader.PowerOn )
{
return HasEnoughResourcesInHoppersFor( thingDef );
Expand All @@ -725,11 +745,13 @@ public bool CanDispenseNow( ThingDef thingDef )

public bool CanProduce( ThingDef thingDef )
{
//Log.Message( string.Format( "{0}.CanProduce( {1} )", this.ThingID, thingDef == null ? "null" : thingDef.defName ) );
return FindRecipeForProduct( thingDef ) != null;
}

public Building AdjacentReachableHopper( Pawn reacher )
{
//Log.Message( string.Format( "{0}.AdjacentReachableHopper( {1} )", this.ThingID, reacher == null ? "null" : reacher.NameStringShort ) );
var hoppers = CompHopperUser.FindHoppers();
if( !hoppers.NullOrEmpty() )
{
Expand All @@ -752,6 +774,7 @@ public Building AdjacentReachableHopper( Pawn reacher )

public bool HasEnoughResourcesInHoppersFor( ThingDef thingDef )
{
//Log.Message( string.Format( "{0}.HasEnoughResourcesInHoppersFor( {1} )", this.ThingID, thingDef == null ? "null" : thingDef.defName ) );
var recipe = FindRecipeForProduct( thingDef );
if( recipe == null )
{
Expand All @@ -762,6 +785,7 @@ public bool HasEnoughResourcesInHoppersFor( ThingDef thi

public List<ThingDef> AllProducts()
{
//Log.Message( string.Format( "{0}.AllProducts()", this.ThingID ) );
var products = new List<ThingDef>();
foreach( var pair in productionAllowances )
{
Expand All @@ -778,6 +802,7 @@ public List<ThingDef> AllProducts()

public List<ThingDef> AllowedProducts()
{
//Log.Message( string.Format( "{0}.AllowedProducts()", this.ThingID ) );
var products = new List<ThingDef>();
foreach( var pair in productionAllowances )
{
Expand All @@ -797,6 +822,8 @@ public List<ThingDef> AllowedProducts()

public ThingDef BestProduct( Func<ThingDef,bool> where, Func<ThingDef,ThingDef,int> sort )
{
//Log.Message( string.Format( "{0}.BestProduct()", this.ThingID ) );

var thingDefs = AllProducts().Where( where.Invoke ).ToList();
thingDefs.Sort( sort.Invoke );

Expand All @@ -813,6 +840,7 @@ public ThingDef BestProduct( Func<ThingDef,bool> where, Func

public Thing TryProduceThingDef( ThingDef thingDef )
{
//Log.Message( string.Format( "{0}.TryProduceThingDef( {1} )", this.ThingID, thingDef == null ? "null" : thingDef.defName ) );
var recipe = FindRecipeForProduct( thingDef );
if( recipe == null )
{
Expand Down
Loading

0 comments on commit 986ab20

Please sign in to comment.