Sign in to follow this  
Scrimbles

Is there an easy if statement to check if a world has caves enabled? [edit: The answer is yes! Solution inside]

Recommended Posts

Scrimbles    4125

I need to do a check if a world has caves enabled, to either add something in the worldgenmain.lua or not, I was wondering if theres an easy statement to the effect of

 

"if TheWorld.hascaves then..." etc.

 

Thankus

Edited by Ogrecakes
  • Like 1

Share this post


Link to post
Share on other sites
Ultroman    735

UPDATE: Doesn't work.

This is probably not the most optimal way, but do this after the whole world is done generating: use FindEntity to search for "cave_entrance" entities, and if you don't find any, then there are no caves. But if you can put caves on and set sinkholes to "None" and somehow still go into the caves, that would be an edge-case my solution doesn't handle. Otherwise, it should work fine. You'd also have to use this check somewhere: if TheWorld.worldprefab == "cave" then

Edited by Ultroman
  • Like 2

Share this post


Link to post
Share on other sites
Desblat    640

I feel like there must be a world tag for that. 

I just put that into mod config. Also I make crafting reciped that don't strickly require caves. 

Btw you should keep in mind that some people use cave content on surface mods. 

  • Like 1

Share this post


Link to post
Share on other sites
Hornete    8368
6 hours ago, Ultroman said:

This is probably not the most optimal way, but do this after the whole world is done generating: use FindEntity to search for "cave_entrance" entities,

There may be a problem with that, I'm pretty sure the cave entrance entities ALWAYS exist. The reason for this is if a player wants to add caves to a non caves world it makes the process a little easier.

They're invisible in non caves world but they do exist iirc

Share this post


Link to post
Share on other sites
Ultroman    735
3 hours ago, Hornete said:

There may be a problem with that, I'm pretty sure the cave entrance entities ALWAYS exist. The reason for this is if a player wants to add caves to a non caves world it makes the process a little easier.

They're invisible in non caves world but they do exist iirc

Ah, well there goes that solution. I searched the game code for an hour, and didn't find anything. Just a lot of ways to see if your CURRENT world is a cave, which is pretty useless.

  • Sad 1

Share this post


Link to post
Share on other sites
CarlZalph    4904
On 2/29/2020 at 7:20 AM, Hornete said:

There may be a problem with that, I'm pretty sure the cave entrance entities ALWAYS exist. The reason for this is if a player wants to add caves to a non caves world it makes the process a little easier.

They're invisible in non caves world but they do exist iirc

This is correct, and the code they use to detect it is:

    if not TheWorld.ismastersim then
        return inst
    end
    
    if TheNet:GetServerIsClientHosted() and not (TheShard:IsMaster() or TheShard:IsSecondary()) then
        --On non-sharded servers we'll make these vanish for now, but still generate them

So by logic, I would surmise that being able to detect if the world does have shards (caves/etc), then invert that to become:

if TheWorld.ismastersim and not (TheNet:GetServerIsClientHosted() and not (TheShard:IsMaster() or TheShard:IsSecondary()))
then
  -- Shards are on
end

 

Edited by CarlZalph
IsSlave changed to IsSecondary during some update.
  • Thanks 4

Share this post


Link to post
Share on other sites
Scrimbles    4125
13 hours ago, CarlZalph said:

This is correct, and the code they use to detect it is:


    if not TheWorld.ismastersim then
        return inst
    end
    
    if TheNet:GetServerIsClientHosted() and not (TheShard:IsMaster() or TheShard:IsSlave()) then
        --On non-sharded servers we'll make these vanish for now, but still generate them

So by logic, I would surmise that being able to detect if the world does have shards (caves/etc), then invert that to become:


if TheWorld.ismastersim and not (TheNet:GetServerIsClientHosted() and not (TheShard:IsMaster() or TheShard:IsSlave()))
then
  -- Shards are on
end

 

This definitely works for fully generated and active server, tested it with both caved/caveless and a print("") for each and it worked.

Unfortunately, when used in modworldgenmain, TheWorld comes up as nil, same with TheNet and TheShard.

 

Doing ~= nil checks for any of them doesn't do anything, and adding GLOBAL. before them still comes up as nil.

Perhaps world generation is done before the server is put online, in which case I think I might be out of luck for this solution :(

Edited by Ogrecakes
  • Like 1

Share this post


Link to post
Share on other sites
CarlZalph    4904
10 hours ago, Ogrecakes said:

This definitely works for fully generated and active server, tested it with both caved/caveless and a print("") for each and it worked.

Unfortunately, when used in modworldgenmain, TheWorld comes up as nil, same with TheNet and TheShard.

 

Doing ~= nil checks for any of them doesn't do anything, and adding GLOBAL. before them still comes up as nil.

Perhaps world generation is done before the server is put online, in which case I think I might be out of luck for this solution :(

Mmm, unfortunate.  What's the end goal for this check?

Perhaps there may be a different approach to what you're wanting done instead of lurking in modworldgenmain.

Share this post


Link to post
Share on other sites
Scrimbles    4125
1 hour ago, CarlZalph said:

Mmm, unfortunate.  What's the end goal for this check?

Perhaps there may be a different approach to what you're wanting done instead of lurking in modworldgenmain.

The mod involves spawning in a set piece for players who are on a server without caves.

It attaches a timer to the toadstool for a whole set of events, but I want to make sure that the toadstool can be put in caveless worlds too.

Currently its just a toggleable setting in the mod menu, and that works fine, but it would be much more ideal for it to be automatic.

I will probably look into the server retrofitting for when the oasis was added to the game, or maybe find a way to detect the swamp (where the setpiece spawns) and just spawn in the required prefab randomly after the world starts.

  • Like 1

Share this post


Link to post
Share on other sites

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
Sign in to follow this