[Solved] Insert custom gensteps?

Started by Orion, December 29, 2014, 01:44:21 PM

Previous topic - Next topic

Orion

I would like to spawn some extra structures when the map is created. I've created a genstep for it, but now I've realized all existing gensteps are hardcoded.

Does anyone know if there is another way to get it to run?

unifex

I hit the same wall trying to create my Aethervent, and I think its something that might be in the changelog for alpha 9.

I was going to look at writing something that has a TickRare() that tries to determine when the map is first loaded, and then does the generation (and then sets a flag so it never triggers again).

Maybe something that looks for the colonist count to transition from 0 to 3+, and then generates the stuff in the same way (ie I don't think there is any problem with that in the same way other stuff is generated on the fly).

Orion

I did find a possible solution that I'm exploring now (kudos to alphalol's "AlphaTweaks").

Inheriting MapComponent creates a sort of global object after loading the map. Now I'm trying to figure out how to use Scribe_Values to check if it has run before. If it's the first time, I should be able to run my GenStep.

Once alpha 9 is there, it should be simple to run the GenStep in the originally intended way.

If anyone has more experience, getting this done, please let me know. If I figure it out myself, I'll post the solution here.

Orion

#3
I found a way to check it:

using System.Text;
using RimWorld;
using Verse;

namespace SpawnTest
{
    public class MapComponent_SpawnTest : MapComponent
    {
        private bool created;

        public override void MapComponentTick()
        {
            base.MapComponentTick();
            if (!created)
            {
                Create();
            }
        }

        /// <summary>
        /// Create our stuff.
        /// </summary>
        private void Create()
        {
            Log.Warning("Creating fancy things...");

// Spawn code:

            //new Genstep_ScatterThings
            //{
            //    thingDefs = new List<ThingDef>
            //    {
            //        ThingDef.Named("NutrientPasteDispenser")
            //    },
            //    minSpacing = 5f,
            //    extraNoBuildEdgeDist = 4,
            //    countPer10kCellsRange = new FloatRange(0.7f, 1f),
            //    clearSpaceSize = 10,
            //    neededSurfaceType = TerrainAffordance.Light
            //}.Generate();

            // Will be saved when ExposeData is called later.
            created = true;
        }

        /// <summary>
        /// Upon loading or saving...
        /// </summary>
        public override void ExposeData()
        {
            base.ExposeData();

            Scribe_Values.LookValue(ref created, "Created");

            if (!created)
            {
                Log.Warning("Not created. This should not happen.");
            }
            else
            {
                //Log.Warning("Ready.");
            }
        }
    }


This seems to work fairly well, although things are spawned just after the game has started (a bit too late for my taste).

unifex

Nice, thanks, should be ok for me I think at the moment. Is there some XML definition that references the MapComponent_SpawnTest class?

Orion

Curiously not. It just automagically works. Although only for new maps (sorta makes sense).