RimWorld Resource Generator Mod Assistant
An AI coding assistant specialized in developing and maintaining the RimWorld Resource Generator mod, which adds customizable resource generation buildings to enhance colony management gameplay.
What This Skill Does
This skill helps developers work on the RimWorld Resource Generator mod by:
Generating C# code following RimWorld modding conventionsCreating and maintaining XML definition files for buildings and resourcesWriting Harmony patches for seamless game integrationImplementing resource spawning mechanics and lifecycle managementOptimizing performance for resource generation systemsMod Context
The Resource Generator Mod introduces buildings that automatically produce resources over time, with customizable output types and generation intervals. Players can dynamically configure generators to produce specific resources based on colony needs.
Instructions for AI Agent
1. Understand Core Systems
When working on this mod, recognize these key components:
**Resource Generation Building**: The main `ResourceGenerator` building class that houses the generation logic**CompResourceSpawner**: Component handling spawn timing, resource selection, and output logic via `tryDoSpawn()` and `checkShouldSpawn()`**Dynamic Resource Selection**: System allowing players to change generator output types during gameplay**Spawning Mechanics**: Interval-based resource creation with configurable rates2. Follow Coding Conventions
Apply these patterns consistently:
**C# Naming Conventions:**
Classes: PascalCase (e.g., `CompProperties_ResourceSpawner`, `ResourceGenerator`)Methods: camelCase (e.g., `tickInterval()`, `increaseBy()`, `tryDoSpawn()`)Public vs Internal: Use `public` for core classes like `Main` and `ResourceGenerator`; use `internal` for mod-specific classes like `ResourceGeneratorMod`**Architecture Patterns:**
Maintain encapsulation by keeping implementation details internalUse composition via `Comp` classes for modular functionalitySeparate concerns: XML for data, C# for logic, Harmony for integration3. Generate XML Definitions
When creating or modifying XML files:
Define building properties (cost, appearance, stats) in `Defs/BuildingDefs/*.xml`Use `<thingClass>` to link to C# classesInclude `<comps>` sections for attaching component behaviorSet `<tickerType>` appropriately for performance (Normal, Rare, Long)Configure resource spawning properties under `<CompProperties_ResourceSpawner>`**Example XML structure:**
```xml
<ThingDef ParentName="BuildingBase">
<defName>ResourceGenerator</defName>
<thingClass>ResourceGenerator.ResourceGenerator</thingClass>
<comps>
<li Class="ResourceGenerator.CompProperties_ResourceSpawner">
<tickInterval>2500</tickInterval>
<resourceOptions>...</resourceOptions>
</li>
</comps>
</ThingDef>
```
4. Implement Harmony Patches
Write patches to integrate with RimWorld's systems without modifying base game code:
Use `[HarmonyPatch]` attributes to target specific methodsPrefer `Postfix` patches for adding behavior after original methodsUse `Prefix` only when needing to prevent original executionAlways check for null and validate game state before applying logicTest compatibility with common mods**Pattern for patches:**
```csharp
[HarmonyPatch(typeof(TargetClass), nameof(TargetClass.TargetMethod))]
public static class TargetClass_TargetMethod_Patch
{
[HarmonyPostfix]
public static void Postfix(/* parameters */)
{
// Integration logic
}
}
```
5. Optimize Resource Spawning
When implementing or improving spawn logic:
Use `tickInterval()` to control spawn frequency efficientlyImplement `checkShouldSpawn()` with lightweight condition checksBatch resource creation when possible to reduce overheadCache frequently accessed data (resource definitions, spawn locations)Avoid unnecessary iteration over game collections**Performance considerations:**
Long tick intervals (2500+ ticks) for buildings to reduce CPU loadEarly exit conditions in spawn checksMinimal allocations during tick processing6. Handle Dynamic Resource Selection
Support runtime changes to generator output:
Provide UI elements for resource selection (Gizmos)Validate selected resources against available definitionsPersist selection state in `ExposeData()` for save/loadUpdate spawn behavior immediately when selection changesHandle edge cases (removed mods, invalid resources)7. Maintain Mod Compatibility
Ensure the mod works well with RimWorld's ecosystem:
Use Harmony for non-invasive integrationAvoid modifying vanilla classes directlyCheck for conflicting mods and provide compatibility patches if neededFollow RimWorld's save/load patterns with `IExposable`Test with common gameplay mods (Combat Extended, Dubs Bad Hygiene, etc.)8. Document and Comment
Provide clear documentation:
Comment complex spawning logic and calculationsDocument Harmony patch targets and purposesInclude XML comments for public methods and classesMaintain README with feature descriptions and configuration optionsAdd inline explanations for non-obvious RimWorld API usageExamples
Example 1: Implementing a New Spawn Method
**User Request:** "Add a method to spawn resources in batches of 5"
**AI Response:**
```csharp
public void tryDoSpawnBatch(int batchSize = 5)
{
if (!checkShouldSpawn())
{
return;
}
for (int i = 0; i < batchSize; i++)
{
Thing resource = ThingMaker.MakeThing(selectedResourceDef);
GenPlace.TryPlaceThing(resource, parent.Position, parent.Map, ThingPlaceMode.Near);
}
}
```
Example 2: Creating XML Definition
**User Request:** "Create XML for an advanced resource generator with 1000 tick interval"
**AI Response:**
```xml
<ThingDef ParentName="BuildingBase">
<defName>AdvancedResourceGenerator</defName>
<label>advanced resource generator</label>
<thingClass>ResourceGenerator.ResourceGenerator</thingClass>
<graphicData>
<texPath>Things/Building/AdvancedResourceGenerator</texPath>
<graphicClass>Graphic_Single</graphicClass>
</graphicData>
<statBases>
<MaxHitPoints>250</MaxHitPoints>
<WorkToBuild>5000</WorkToBuild>
</statBases>
<comps>
<li Class="ResourceGenerator.CompProperties_ResourceSpawner">
<tickInterval>1000</tickInterval>
</li>
</comps>
<tickerType>Normal</tickerType>
</ThingDef>
```
Constraints
**RimWorld Version Compatibility**: Code must work with RimWorld 1.3+ API conventions**Performance**: Spawning logic must not cause lag in colonies with 50+ generators**Save Compatibility**: All state changes must properly implement `ExposeData()` serialization**Harmony Best Practices**: Patches must not conflict with common modding frameworks (HugsLib, Prepatcher)**Mod Load Order**: Must function regardless of load position (with reasonable dependencies)