Skip to content

Commit

Permalink
feat: smooth underwater shore (#3938)
Browse files Browse the repository at this point in the history
* Underwater shore

* Shut up linter
  • Loading branch information
Vollch authored Dec 18, 2023
1 parent 7d54e3e commit dbfd88d
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 12 deletions.
11 changes: 11 additions & 0 deletions data/json/overmap/overmap_terrain/overmap_terrain_waterbody.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
"flags": [ "NO_ROTATE", "LAKE_SHORE", "SOURCE_DRINK" ],
"mapgen": [ { "method": "builtin", "name": "lake_shore" } ]
},
{
"type": "overmap_terrain",
"id": "lake_underwater_shore",
"copy-from": "generic_water",
"name": "lake shore (submerged)",
"sym": "#",
"color": "light_blue",
"see_cost": 1,
"flags": [ "NO_ROTATE", "LAKE_SHORE", "SOURCE_DRINK" ],
"mapgen": [ { "method": "builtin", "name": "lake_shore" } ]
},
{
"type": "overmap_terrain",
"id": "lake_surface",
Expand Down
4 changes: 3 additions & 1 deletion src/mapdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ ter_id t_null,
t_fungus_mound, t_fungus, t_shrub_fungal, t_tree_fungal, t_tree_fungal_young, t_marloss_tree,
// Water, lava, etc.
t_water_moving_dp, t_water_moving_sh, t_water_sh, t_water_dp, t_swater_sh, t_swater_dp,
t_water_pool, t_sewage,
t_water_cube, t_lake_bed, t_water_pool, t_sewage,
t_lava,
// More embellishments than you can shake a stick at.
t_sandbox, t_slide, t_monkey_bars, t_backboard,
Expand Down Expand Up @@ -948,6 +948,8 @@ void set_ter_ids()
t_water_dp = ter_id( "t_water_dp" );
t_swater_sh = ter_id( "t_swater_sh" );
t_swater_dp = ter_id( "t_swater_dp" );
t_water_cube = ter_id( "t_water_cube" );
t_lake_bed = ter_id( "t_lake_bed" );
t_water_pool = ter_id( "t_water_pool" );
t_sewage = ter_id( "t_sewage" );
t_lava = ter_id( "t_lava" );
Expand Down
2 changes: 1 addition & 1 deletion src/mapdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ extern ter_id t_null,
t_fungus_mound, t_fungus, t_shrub_fungal, t_tree_fungal, t_tree_fungal_young, t_marloss_tree,
// Water, lava, etc.
t_water_moving_dp, t_water_moving_sh, t_water_sh, t_swater_sh, t_water_dp, t_swater_dp,
t_water_pool, t_sewage,
t_water_cube, t_lake_bed, t_water_pool, t_sewage,
t_lava,
// More embellishments than you can shake a stick at.
t_sandbox, t_slide, t_monkey_bars, t_backboard,
Expand Down
27 changes: 22 additions & 5 deletions src/mapgen_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "enums.h"
#include "field_type.h"
#include "flood_fill.h"
#include "game.h"
#include "game_constants.h"
#include "int_id.h"
#include "line.h"
Expand Down Expand Up @@ -2587,7 +2588,11 @@ void mapgen_lake_shore( mapgendata &dat )
// If we didn't extend an adjacent terrain, then just fill this entire location with the default
// groundcover for the region.
if( !did_extend_adjacent_terrain ) {
dat.fill_groundcover();
if( dat.zlevel() >= 0 ) {
dat.fill_groundcover();
} else {
fill_background( m, t_rock );
}
}

const oter_id river_center( "river_center" );
Expand Down Expand Up @@ -2867,15 +2872,27 @@ void mapgen_lake_shore( mapgendata &dat )
}
};

// We need to have same shoreline on different z levels, to match surface shore
// with submerged shore, to do so we'll jitter shore lines using deterministic
// random seeded with x\y coordinates
// NOLINTNEXTLINE(cata-determinism)
std::mt19937 prng( std::hash<point_abs_omt>()( dat.pos.xy() ) ^ g->get_seed() );

// Given two points, return a point that is midway between the two points and then
// jittered by a random amount in proportion to the length of the line segment.
const auto jittered_midpoint = [&]( point from, point to ) {
const int jitter = rl_dist( from, to ) / 4;
const point midpoint( ( from.x + to.x ) / 2 + rng( -jitter, jitter ),
( from.y + to.y ) / 2 + rng( -jitter, jitter ) );
std::uniform_int_distribution<int> roll( -jitter, jitter );
const point midpoint( ( from.x + to.x ) / 2 + roll( prng ),
( from.y + to.y ) / 2 + roll( prng ) );
return midpoint;
};

ter_id edge_tile = dat.zlevel() >= 0 ? t_water_sh : t_rock;
ter_id water_tile = dat.zlevel() >= 0 ? t_water_dp :
dat.zlevel() == dat.region.overmap_lake.lake_depth ? t_lake_bed :
t_water_cube;

// For each of our valid shoreline line segments, generate a slightly more interesting
// set of line segments by splitting the line into four segments with jittered
// midpoints, and then draw shallow water for four each of those.
Expand Down Expand Up @@ -2906,7 +2923,7 @@ void mapgen_lake_shore( mapgendata &dat )
std::vector<point> water_points = ff::point_flood_fill_4_connected( starting_point, visited,
should_fill );
for( auto &wp : water_points ) {
m->ter_set( wp, t_water_dp );
m->ter_set( wp, water_tile );
m->furn_set( wp, f_null );
}
};
Expand All @@ -2931,7 +2948,7 @@ void mapgen_lake_shore( mapgendata &dat )

// We previously placed our shallow water but actually did a t_null instead to make sure that we didn't
// pick up shallow water from our extended terrain. Now turn those nulls into t_water_sh.
m->translate( t_null, t_water_sh );
m->translate( t_null, edge_tile );
}

void mremove_trap( map *m, point p )
Expand Down
4 changes: 2 additions & 2 deletions src/mapgendata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mapgendata::mapgendata( map &mp, dummy_settings_t )
: density_( 0 )
, when_( calendar::turn )
, mission_( nullptr )
, zlevel_( 0 )
, pos( tripoint_zero )
, region( dummy_regional_settings )
, m( mp )
, default_groundcover( region.default_groundcover )
Expand All @@ -50,9 +50,9 @@ mapgendata::mapgendata( const tripoint_abs_omt &over, map &mp, const float densi
, density_( density )
, when_( when )
, mission_( miss )
, zlevel_( over.z() )
, t_above( overmap_buffer.ter( over + tripoint_above ) )
, t_below( overmap_buffer.ter( over + tripoint_below ) )
, pos( over )
, region( overmap_buffer.get_settings( over ) )
, m( mp )
, default_groundcover( region.default_groundcover )
Expand Down
5 changes: 2 additions & 3 deletions src/mapgendata.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ class mapgendata
float density_;
time_point when_;
::mission *mission_;
int zlevel_;
mapgen_arguments mapgen_args_;

public:
Expand All @@ -89,6 +88,7 @@ class mapgendata

std::unordered_map<cube_direction, std::string> joins;

const tripoint_abs_omt pos;
const regional_settings &region;

map &m;
Expand Down Expand Up @@ -135,8 +135,7 @@ class mapgendata
return mission_;
}
int zlevel() const {
// TODO: should be able to determine this from the map itself
return zlevel_;
return pos.z();
}

void set_dir( int dir_in, int val );
Expand Down
5 changes: 5 additions & 0 deletions src/overmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4100,6 +4100,7 @@ void overmap::place_lakes()

const oter_id lake_surface( "lake_surface" );
const oter_id lake_shore( "lake_shore" );
const oter_id lake_underwater_shore( "lake_underwater_shore" );
const oter_id lake_water_cube( "lake_water_cube" );
const oter_id lake_bed( "lake_bed" );

Expand Down Expand Up @@ -4178,6 +4179,10 @@ void overmap::place_lakes()
ter_set( tripoint_om_omt( p, z ), lake_water_cube );
}
ter_set( tripoint_om_omt( p, settings->overmap_lake.lake_depth ), lake_bed );
} else {
for( int z = -1; z >= settings->overmap_lake.lake_depth; z-- ) {
ter_set( tripoint_om_omt( p, z ), lake_underwater_shore );
}
}
}

Expand Down

0 comments on commit dbfd88d

Please sign in to comment.