Mythruna

Modder's Workbench => Scripting => Topic started by: pspeed on February 10, 2012, 11:07:04 AM



Title: Scripted caves...
Post by: pspeed on February 10, 2012, 11:07:04 AM
Don't know if this picture/link will work but I just posted one to the facebook page:
http://www.facebook.com/photo.php?fbid=344621935560331&set=a.218418391514020.54331.204533646235828 (http://www.facebook.com/photo.php?fbid=344621935560331&set=a.218418391514020.54331.204533646235828)

Cave generation works in two phases:
1) random cave influencers are generated using a semi-complicated algorithm
2) when the terrain is generated, these influencers are used to roughly carve the caves/gorges out of the terrain

With the new caveSystemCreated hook, scripts can add their own caves between steps 1 and 2.

The one that made that picture looks like this:
Code:

import mythruna.db.cave.*;
        
on( [caveSystemCreated] ) {
    type, event ->
        
    event.generator.addPoint( 512, 512 ).setRadius(50).setStrength(10);  
    event.generator.addPoint( 512, 512 ).setRadius(20).setStrength(-20);  
}

Caves are only carved out of where there would have been land, so you can't plop an arbitrary blob in the middle of space... but you can do all kinds of other weird things like the above with adding/subtracting negative areas.

Here is another script that makes a small crater around the spawn point connected to a deep angled tunnel that connects to a submerged gorge:

Code:

import mythruna.db.cave.*;
        
on( [caveSystemCreated] ) {
    type, event ->
        
    // Create a relatively smooth crater centered on the surface at the spawn point.
    // Proper elevation is determined from the normal fractal terrain.
    def p = event.generator.addPoint( 512, 512 ).setRadius(10).setStrength(10);

    // This generates a cylindrical cave running from the center of that crater
    // down and to the west.  In world seed=100, this is a 60 meter deep tunnel.
    event.generator.addLine( 512, 512, p.center.z, 500, 512, 20 );    

    // From there, this generates a long, roughly 12-15 meter high, 10 meter wide (diameter)
    // unground "gorge"... basically a tunnel that is tall.
    event.generator.addWall( 500, 512, 20, 490, 400, 30 );    
}

By the time these scripts are run, all of the random caves have been generated for that area and so the scripts could technically look there and see what is already there.

And by the way, this is exactly the same way that dungeons and towns will be generated and they will allow for the same level of customization... though obviously they will be much different than simple caves are. :)


Title: Re: Scripted caves...
Post by: FutureB on February 10, 2012, 10:21:25 PM
Wow i dont understand the codeing ness but WOW man the pictures coooooool great job paul :]


Title: Re: Scripted caves...
Post by: pspeed on February 10, 2012, 10:40:43 PM
Just fun to play with.

In case you (or anyone else) are curious, the algorithm sort of works like this:
1) take already generated height map from the fractal
2) go over each cell in a block of terrain
3) if that cell is within the radius of some of the influencers (point, line, wall) then add up all of the influences
4) if that value is more than 0 then erase the cell.

So, in the above first example (and the one that generated the picture)...  Each cell that is within the 30 meter radius of the first point has strength added (in simplistic terms).  Each cell that is within the 20 meter radius of the second point has strength subtracted.  Any cells that still have positive strength are erased.

Without any points, that place is very much a landlocked hill.
With only the first point, it's a carved out "bowl" or crater, just touching the shoreline.
With the second point, some of the land is "put back" making a floating island.

There's a slightly more complicated bit where the actual strength falls off based on distance and is also used to randomize the borders, etc... but you get the gist of it.


Title: Re: Scripted caves...
Post by: BenKenobiWan on February 11, 2012, 03:34:10 PM
With your explanation, I think that actually makes sense.