Jump to content

Recommended Posts

I'm trying to make a world gen mod that adds a single instance of something in any of a variety of Rooms anywhere on the map. The behavior I want is similar to how only 1 Chester can spawn, and he can either spawn on "ChessBarrens", "BGDeciduous", "DeepDeciduous", "MagicalDeciduous", "DeciduousMole", "MolesvilleDeciduous", "DeciduousClearing", "PondyGrass", "TallbirdNests", and many other rooms, it would take all day to name every place that Chester can possibly spawn.

So I looked up how Chester's Eyebone spawning worked and tried to copy that behavior. My first attempt was to try it out using Hutch's Fishbowl. So my modworldgenmain looked like this:

local function PreInitFreeHutch(room)
  if not room.tags then
    room.tags = {"ExitPiece"} -- Don't know what this does! But I saw it on every room that included "Chester_Eyebone"... ¯\_(ツ)_/¯
  end

  table.insert(room.tags, "Hutch_Fishbowl")
end

AddRoomPreInit("BGMarsh", PreInitFreeHutch)
AddRoomPreInit("Marsh", PreInitFreeHutch)
AddRoomPreInit("SpiderMarsh", PreInitFreeHutch)

This, to my surprise, worked. I thought, oh this is simple. All I have to do is drop the name of the prefab I want in the room.tags table. So next I try another simple test, the same code above except using "lightbulb" instead of "Hutch_Fishbowl".

Nope. I was wrong. It's not that simple. I just got lucky in my initial test. The prefab I was testing with was already setup to work this way due to Hutch's Fishbowl spawning in a random location within the caves. World gen crashes because storygen sees a room tag and it has no idea what to do with it.

 

So I did some more digging around and found map/maptags.lua. This is the place. I need to add my own tags in here so that when story gen finds the tags that i added to the rooms it will know what to do. I struggled with this for a bit, but eventually got it working with my new modworldgenmain:

local function AddLightbulbTag(story)
  story.map_tags.TagData["Lightbulb"] = true
  story.map_tags.Tag["Lightbulb"] = function(tagdata)
                                        if tagdata["Lightbulb"] == false then
                                            return
                                        end
                                        tagdata["Lightbulb"] = false
                                        return "ITEM", "flower_cave_triple"
                                    end
end

AddGlobalClassPostConstruct("map/storygen", "Story", AddLightbulbTag)

local function PreInitLightbulb(room)
  if not room.tags then
    room.tags = {"ExitPiece"} -- Still don't know what this does! ¯\_(ツ)_/¯
  end
  table.insert(room.tags, "Lightbulb")
end

AddRoomPreInit("BGMarsh", PreInitLightbulb)
AddRoomPreInit("Marsh", PreInitLightbulb)
AddRoomPreInit("SpiderMarsh", PreInitLightbulb)

 

And this got it working again. Yay!

 

Now I wanted to try get 2 different things spawning in a single random location. So I try it out with a light bulb plant and a glow berry plant.

 

local function AddTags(story)
  story.map_tags.TagData["Lightbulb"] = true
  story.map_tags.TagData["Glowberry"] = true
  story.map_tags.Tag["Lightbulb"] = function(tagdata)
                                        if tagdata["Lightbulb"] == false then
                                            return
                                        end
                                        tagdata["Lightbulb"] = false
                                        return "ITEM", "flower_cave_triple"
                                    end
  story.map_tags.Tag["Glowberry"] = function(tagdata)
                                        if tagdata["Glowberry"] == false then
                                            return
                                        end
                                        tagdata["Glowberry"] = false
                                        return "ITEM", "wormlight_plant"
                                    end
end

AddGlobalClassPostConstruct("map/storygen", "Story", AddTags)

local function PreInitFn(room)
  if not room.tags then
    room.tags = {"ExitPiece"} -- Still don't know what this does! ¯\_(ツ)_/¯
  end
  table.insert(room.tags, "Lightbulb")
  table.insert(room.tags, "Glowberry")
end

AddRoomPreInit("BGMarsh", PreInitFn)
AddRoomPreInit("Marsh", PreInitFn)
AddRoomPreInit("SpiderMarsh", PreInitFn)

This works, sorta, but there's a problem.

Every time the lightbulb plant and the glowberry plant spawn in the same room. There are tons of BGMarsh, Marsh, and SpiderMarsh rooms in the world, but it's always picking the same exact room for both of these.

So I think what's happening is when the world is generating rooms it is just plopping my "flower_cave_triple" and "wormlight_plant" both in the first BGMarsh, Marsh, or SpiderMarsh room that is created, the TagData booleans get toggled to False, and that's why they never appear in any following rooms of these types.

But this is not what I want. I don't care if they both just happen to spawn in the same room very rarely. I want them each to spawn in their own room chosen at random, not 1 random room for both of them every single time.

How can I achieve the behavior I am aiming for?

Let's say I want 1 lightbulb plant to spawn on any random marsh tile, and also 1 glowberry plant to spawn on any random marsh tile. But I don't want them to always spawn in the same marsh together. It' OK if that happens some of the time by chance, but I don't want it to happen 100% of the time.

My guess is that you are trying to spawn them in the same room and this room is possibly small enough so they are always close to each other. One solution is to find a solution to spawn them in different room.

 

Another is to make one (or both) of your prefabs SetPiece so you can spawn them with different rules, but it's not the best solution if you just want simple item spawn.

I'm not trying to spawn them in the same room, they just are spawning in the same room. I want them to be able to spawn in any swamp. I don't know how to make them spawn in random swamps but not the same room.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...