Skip to content

Commit

Permalink
[added]
Browse files Browse the repository at this point in the history
+ Modders Resource release with more robust error checking
+ [Un]locking of research projects through advanced research
+ [Un]locking of plants through advanced research
+ Mod Help OTab and associated HelpDef and HelpCategoryDef
+ CCLVersionDef for dependant mods
+ Lots of functions and extensions for C# modders
+ MapComponent injection to existing saves

[changed]
+ User release which does minimal error checking to improve performance
+ Restructured _Mod directory
+ AdvancedResearchDef buildingDefs refactored to thingDefs
+ Lots of code refactoring and optimizations
+ Lots of code beautification and code consistancy changes
  • Loading branch information
ForsakenShell committed Aug 16, 2015
1 parent ac73f70 commit 4e9d46e
Show file tree
Hide file tree
Showing 78 changed files with 1,928 additions and 405 deletions.
78 changes: 51 additions & 27 deletions DLL_Project/Defs/AdvancedResearchDef.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;

using RimWorld;
using Verse;

namespace CommunityCoreLibrary
Expand Down Expand Up @@ -62,40 +63,47 @@ public bool ResearchComplete()
public bool IsRecipeToggle()
{
// Determine if this def toggles recipes
return
( recipeDefs != null )&&( recipeDefs.Count > 0 )&&
( sowTags == null )||( sowTags.Count == 0 )&&
( thingDefs != null )&&( thingDefs.Count > 0 );
return (
( ( recipeDefs != null )&&( recipeDefs.Count > 0 ) )&&
( ( sowTags == null )||( ( sowTags != null )&&( sowTags.Count == 0 ) ) )&&
( ( thingDefs != null )&&( thingDefs.Count > 0 ) )
);
}

public bool IsPlantToggle()
{
// Determine if this def toggles plant sow tags
return
( recipeDefs == null )||( recipeDefs.Count == 0 )&&
( sowTags != null )&&( sowTags.Count > 0 )&&
( thingDefs != null )&&( thingDefs.Count > 0 );
return (
( ( recipeDefs == null )||( ( recipeDefs != null )&&( recipeDefs.Count == 0 ) ) )&&
( ( sowTags != null )&&( sowTags.Count > 0 ) )&&
( ( thingDefs != null )&&( thingDefs.Count > 0 ) )
);
}

public bool IsBuildingToggle()
{
// Determine if this def toggles buildings
return
( recipeDefs == null )||( recipeDefs.Count == 0 )&&
( sowTags == null )||( sowTags.Count == 0 )&&
( thingDefs != null )&&( thingDefs.Count > 0 );
return (
( ( recipeDefs == null )||( ( recipeDefs != null )&&( recipeDefs.Count == 0 ) ) )&&
( ( sowTags == null )||( ( sowTags != null )&&( sowTags.Count == 0 ) ) )&&
( ( thingDefs != null )&&( thingDefs.Count > 0 ) )
);
}

public bool HasCallbacks()
{
// Determine if this def has callbacks
return ( researchMods != null )&&( researchMods.Count > 0 );
return (
( ( researchMods != null )&&( researchMods.Count > 0 ) )
);
}

public bool IsResearchToggle()
{
// Determine if this def toggles research
return ( effectedResearchDefs != null )&&( effectedResearchDefs.Count > 0 );
return (
( ( effectedResearchDefs != null )&&( effectedResearchDefs.Count > 0 ) )
);
}

#endregion
Expand All @@ -111,37 +119,53 @@ public void ToggleRecipes( List< ThingDef > buildingCach
{

// Go through each recipe
foreach( var recipe in recipeDefs )
foreach( var recipeDef in recipeDefs )
{

// Make sure recipe has user list
if( recipeDef.recipeUsers == null )
{
recipeDef.recipeUsers = new List<ThingDef>();
}

if( Hide )
{
// Hide recipe

// Remove building from recipe
if( ( recipe.recipeUsers != null )&&
( recipe.recipeUsers.IndexOf( buildingDef ) >= 0 ) )
if( recipeDef.recipeUsers.IndexOf( buildingDef ) >= 0 )
{
recipe.recipeUsers.Remove( buildingDef );
recipeDef.recipeUsers.Remove( buildingDef );
}

// Remove recipe from building
if( ( buildingDef.recipes != null )&&
( buildingDef.recipes.IndexOf( recipe ) >= 0 ) )
( buildingDef.recipes.IndexOf( recipeDef ) >= 0 ) )
{
buildingDef.recipes.Remove( recipe );
buildingDef.recipes.Remove( recipeDef );
}
}
else
{
// Make sure recipe has user list
if( recipe.recipeUsers == null )

// Remove bill on any table of this def using this recipe
var buildings = Find.ListerBuildings.AllBuildingsColonistOfDef( buildingDef );
foreach( var building in buildings )
{
recipe.recipeUsers = new List<ThingDef>();
var BillGiver = building as IBillGiver;
for( int i = 0; i < BillGiver.BillStack.Count; ++ i )
{
var bill = BillGiver.BillStack[ i ];
if( bill.recipe == recipeDef )
{
BillGiver.BillStack.Delete( bill );
continue;
}
}
}

}
else
{
// Add building to recipe
recipe.recipeUsers.Add( buildingDef );
recipeDef.recipeUsers.Add( buildingDef );
}
}

Expand Down
1 change: 1 addition & 0 deletions DLL_Project/ITabs/ModInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public ModInit()
{
gameObject = new GameObject( "CCLController" );
gameObject.AddComponent< Controller >();
Object.DontDestroyOnLoad( gameObject );
}
}

Expand Down
129 changes: 32 additions & 97 deletions DLL_Project/MapComponents/ResearchControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ public class ResearchControl : MapComponent
{

const int TicksPerSecond = 60;
const int UpdatedPerSecond = 2;
const int UpdateTicks = TicksPerSecond / UpdatedPerSecond;
const int UpdatesPerSecond = 2;
const int BaseUpdateTicks = TicksPerSecond / UpdatesPerSecond;
int UpdateTicks;

List< AdvancedResearchDef > advancedResearch;
int tickCount;

bool okToProcess;
bool wasGodMode;

bool firstRun = true;
bool okToProcess;

// These are used to optimize the process so the same data
// isn't constantly reprocessed with every itteration.
Expand All @@ -41,29 +41,16 @@ void InitComponent()
firstRun = false;
okToProcess = false;

// Make sure the hidden research exists
if( Research.Locker == null )
{
Log.Error( "Community Core Library :: Advanced Research :: Missing Research.Locker!" );
return;
}

// Get the [advanced] research defs
advancedResearch = DefDatabase< AdvancedResearchDef >.AllDefs.OrderBy( a => a.Priority ).ToList();

if( ( advancedResearch == null )||
( advancedResearch.Count < 1 ) )
( advancedResearch.Count == 0 ) )
{
Log.Message( "Community Core Library :: Advanced Research :: No advanced research defined, hybernating..." );
return;
}

// Do sanity checks
if( !SanityChecks() )
{
return;
}

// Build research quick-reference
var researchProjects = DefDatabase< ResearchProjectDef >.AllDefs.ToList();
foreach( var researchProject in researchProjects )
Expand All @@ -75,74 +62,12 @@ void InitComponent()
SetInitialState();

// Set for an immediate research check
tickCount = 0;

Log.Message( "Community Core Library :: Advanced Research :: Initialized" );
UpdateTicks = 0;
okToProcess = true;
}

bool SanityChecks()
{
// Perform sanity checks of all the defs
bool enableProcessing = true;

#if DEBUG
// Validate each advanced research def
foreach( var Advanced in advancedResearch )
{
// Validate recipes
if( Advanced.IsRecipeToggle() )
{
// Make sure thingDefs are of the appropriate type (has ITab_Bills)
foreach( var buildingDef in Advanced.thingDefs )
{
if( !buildingDef.inspectorTabs.Contains( typeof( ITab_Bills ) ) )
{
Log.Error( "Community Core Library :: Advanced Research :: thingDef( " + buildingDef.defName + " ) is of inappropriate type in AdvancedResearchDef( " + Advanced.defName + " ) - Must contain <inspectorTabs> \"ITab_Bills\"" );
enableProcessing = false;
}
}

}

// Validate plant sowTags
if( Advanced.IsPlantToggle() )
{
// Make sure things are of the appropriate class (Plant)
foreach( var plantDef in Advanced.thingDefs )
{
if( plantDef.thingClass != typeof( Plant ) )
{
Log.Error( "Community Core Library :: Advanced Research :: thingDef( " + plantDef.defName + " ) is of inappropriate type in AdvancedResearchDef( " + Advanced.defName + " ) - Must be <thingClass> \"Plant\"" );
enableProcessing = false;
}
}

// Make sure sowTags are valid (!null or empty)
for( int i = 0; i < Advanced.sowTags.Count; i++ )
{
var sowTag = Advanced.sowTags[ i ];
if( string.IsNullOrEmpty( sowTag ) )
{
Log.Error( "Community Core Library :: Advanced Research :: sowTags( index = " + i + " ) resolved to null in AdvancedResearchDef( " + Advanced.defName + " )" );
enableProcessing = false;
}
}
}

}
#endif

// Set the processing flag
okToProcess = enableProcessing;

// Return pass/fail
return enableProcessing;
}

public override void MapComponentTick()
void UpdateComponent()
{
base.MapComponentTick();

if( firstRun )
{
InitComponent();
Expand All @@ -153,19 +78,31 @@ public override void MapComponentTick()
return;
}

tickCount--;
if( tickCount > 0 )
UpdateTicks--;
if( UpdateTicks > 0 )
{
return;
}

tickCount = UpdateTicks;
UpdateTicks = BaseUpdateTicks;

wasGodMode |= Game.GodMode;

CheckAdvancedResearch();
}

public override void MapComponentOnGUI()
{
base.MapComponentOnGUI();
UpdateComponent();
}

public override void MapComponentTick()
{
base.MapComponentTick();
UpdateComponent();
}

#endregion

#region Cache Processing
Expand Down Expand Up @@ -204,26 +141,24 @@ void SetInitialState()
// Set the initial state of the advanced research
PrepareCaches();

// Get each advanced research def
foreach( var Advanced in advancedResearch )
// Process each advanced research def in reverse order
for( int i = advancedResearch.Count - 1; i >= 0; i-- )
{
if( Advanced.IsRecipeToggle() )
{
var Advanced = advancedResearch[ i ];

if( Advanced.IsRecipeToggle() ){
// Recipe toggle
Advanced.ToggleRecipes( buildingCache, true );
}
if( Advanced.IsPlantToggle() )
{
if( Advanced.IsPlantToggle() ){
// Plant toggle
Advanced.TogglePlants( true );
}
if( Advanced.IsBuildingToggle() )
{
if( Advanced.IsBuildingToggle() ){
// Building toggle
Advanced.ToggleBuildings( true );
}
if( Advanced.IsResearchToggle() )
{
if( Advanced.IsResearchToggle() ){
// Research toggle
Advanced.ToggleResearch( true );
}
Expand Down
Loading

0 comments on commit 4e9d46e

Please sign in to comment.