Skip to content

Commit

Permalink
Merge pull request #32481 from ZhilkinSerg/refactor-fields-step-6
Browse files Browse the repository at this point in the history
Refactor fields (step 6)
  • Loading branch information
kevingranade authored Jul 23, 2019
2 parents 0c1db81 + 83e74e3 commit 9dd7b1e
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 157 deletions.
133 changes: 99 additions & 34 deletions data/json/field_type.json

Large diffs are not rendered by default.

28 changes: 8 additions & 20 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2654,31 +2654,19 @@ nc_color Character::symbol_color() const

const auto &fields = g->m.field_at( pos() );

// Priority: electricity, fire, acid, gases
bool has_elec = false;
bool has_fire = false;
bool has_acid = false;
bool has_elec = false;
bool has_fume = false;
for( const auto &field : fields ) {
if( field.first == fd_incendiary || field.first == fd_fire ) {
has_fire = true;
}
if( field.first == fd_electricity ) {
has_elec = true;
}
if( field.first == fd_acid ) {
has_acid = true;
has_elec = field.first.obj().has_elec;
if( has_elec ) {
return hilite( basic );
}
if( field.first == fd_relax_gas || field.first == fd_fungal_haze ||
field.first == fd_fungicidal_gas || field.first == fd_toxic_gas ||
field.first == fd_tear_gas || field.first == fd_nuke_gas ||
field.first == fd_smoke ) {
has_fume = true;
}
}
// Priority: electricity, fire, acid, gases
// Can't just return in the switch, because field order is alphabetic
if( has_elec ) {
return hilite( basic );
has_fire = field.first.obj().has_fire;
has_acid = field.first.obj().has_acid;
has_fume = field.first.obj().has_fume;
}
if( has_fire ) {
return red_background( basic );
Expand Down
15 changes: 15 additions & 0 deletions src/field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ int field_entry::move_cost() const
return type.obj().get_move_cost( intensity - 1 );
}

float field_entry::light_emitted() const
{
return type.obj().get_light_emitted( intensity - 1 );
}

float field_entry::translucency() const
{
return type.obj().get_translucency( intensity - 1 );
}

bool field_entry::is_transparent() const
{
return type.obj().get_transparent( intensity - 1 );
}

nc_color field_entry::color() const
{
return type.obj().get_color( intensity - 1 );
Expand Down
4 changes: 4 additions & 0 deletions src/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class field_entry
//returns the move cost of this field
int move_cost() const;

float light_emitted() const;
float translucency() const;
bool is_transparent() const;

//Returns the field_type_id of the current field entry.
field_type_id get_field_type() const;

Expand Down
11 changes: 11 additions & 0 deletions src/field_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,20 @@ void field_type::load( JsonObject &jo, const std::string & )
fallback_intensity_level.dangerous );
optional( jao, was_loaded, "move_cost", intensity_level.move_cost,
fallback_intensity_level.move_cost );
optional( jao, was_loaded, "light_emitted", intensity_level.light_emitted,
fallback_intensity_level.light_emitted );
optional( jao, was_loaded, "translucency", intensity_level.translucency,
fallback_intensity_level.translucency );
intensity_levels.emplace_back( intensity_level );
}
optional( jo, was_loaded, "underwater_age_speedup", underwater_age_speedup, 0_turns );
optional( jo, was_loaded, "decay_amount_factor", decay_amount_factor, 0 );
optional( jo, was_loaded, "apply_slime_factor", apply_slime_factor, 0 );
optional( jo, was_loaded, "dirty_transparency_cache", dirty_transparency_cache, false );
optional( jo, was_loaded, "has_fire", has_fire, false );
optional( jo, was_loaded, "has_acid", has_acid, false );
optional( jo, was_loaded, "has_elec", has_elec, false );
optional( jo, was_loaded, "has_fume", has_fume, false );
optional( jo, was_loaded, "priority", priority, 0 );
optional( jo, was_loaded, "half_life", half_life, 0_turns );
if( jo.has_member( "phase" ) ) {
Expand Down
15 changes: 15 additions & 0 deletions src/field_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ struct field_intensity_level {
bool dangerous = false;
bool transparent = true;
int move_cost = 0;
float light_emitted = 0.0f;
float translucency = 0.0f;
};

struct field_type {
Expand All @@ -45,6 +47,13 @@ struct field_type {
std::vector<field_intensity_level> intensity_levels;

time_duration underwater_age_speedup = 0_turns;
int decay_amount_factor = 0;
int apply_slime_factor = 0;
bool dirty_transparency_cache = false;
bool has_fire = false;
bool has_acid = false;
bool has_elec = false;
bool has_fume = false;

int priority = 0;
time_duration half_life = 0_turns;
Expand Down Expand Up @@ -75,6 +84,12 @@ struct field_type {
int get_move_cost( int level = 0 ) const {
return intensity_levels[level].move_cost;
}
float get_light_emitted( int level = 0 ) const {
return intensity_levels[level].light_emitted;
}
float get_translucency( int level = 0 ) const {
return intensity_levels[level].translucency;
}

bool is_dangerous() const {
return std::any_of( intensity_levels.begin(), intensity_levels.end(),
Expand Down
69 changes: 5 additions & 64 deletions src/lightmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,31 +130,11 @@ bool map::build_transparency_cache( const int zlev )
}
for( const auto &fld : cur_submap->fld[sx][sy] ) {
const field_entry &cur = fld.second;
const field_type_id type = cur.get_field_type();
const int intensity = cur.get_field_intensity();

if( type.obj().get_transparent( intensity - 1 ) ) {
if( cur.is_transparent() ) {
continue;
}

// Fields are either transparent or not, however we want some to be translucent
if( type == fd_cigsmoke || type == fd_weedsmoke || type == fd_cracksmoke ||
type == fd_methsmoke || type == fd_relax_gas ) {
value *= 5;
} else if( type == fd_smoke || type == fd_incendiary || type == fd_toxic_gas ||
type == fd_tear_gas ) {
if( intensity == 3 ) {
value = LIGHT_TRANSPARENCY_SOLID;
} else if( intensity == 2 ) {
value *= 10;
}
} else if( type == fd_nuke_gas ) {
value *= 10;
} else if( type == fd_fire ) {
value *= 1.0 - ( intensity * 0.3 );
} else {
value = LIGHT_TRANSPARENCY_SOLID;
}
value = value * cur.translucency();
}
// TODO: [lightmap] Have glass reduce light as well
}
Expand Down Expand Up @@ -352,48 +332,9 @@ void map::generate_lightmap( const int zlev )

for( auto &fld : cur_submap->fld[sx][sy] ) {
const field_entry *cur = &fld.second;
// TODO: [lightmap] Attach light brightness to fields
const auto ft = cur->get_field_type();
if( ft == fd_fire ) {
if( 3 == cur->get_field_intensity() ) {
add_light_source( p, 160 );
} else if( 2 == cur->get_field_intensity() ) {
add_light_source( p, 60 );
} else {
add_light_source( p, 20 );
}
}
if( ft == fd_fire_vent || ft == fd_flame_burst ) {
add_light_source( p, 20 );
}
if( ft == fd_electricity || ft == fd_plasma ) {

if( 3 == cur->get_field_intensity() ) {
add_light_source( p, 20 );
} else if( 2 == cur->get_field_intensity() ) {
add_light_source( p, 4 );
} else {
// Kinda a hack as the square will still get marked.
apply_light_source( p, LIGHT_SOURCE_LOCAL );
}
}
if( ft == fd_incendiary ) {
if( 3 == cur->get_field_intensity() ) {
add_light_source( p, 160 );
} else if( 2 == cur->get_field_intensity() ) {
add_light_source( p, 60 );
} else {
add_light_source( p, 20 );
}
}
if( ft == fd_laser ) {
apply_light_source( p, 4 );
}
if( ft == fd_spotlight ) {
add_light_source( p, 80 );
}
if( ft == fd_dazzling ) {
add_light_source( p, 5 );
const int light_emitted = cur->light_emitted();
if( light_emitted > 0 ) {
add_light_source( p, light_emitted );
}
}
}
Expand Down
24 changes: 4 additions & 20 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2695,9 +2695,6 @@ void map::decay_fields_and_scent( const time_duration &amount )
// Decay scent separately, so that later we can use field count to skip empty submaps
g->scent.decay();

const time_duration amount_fire = amount / 3; // Decay fire by this much
const time_duration amount_liquid = amount / 2; // Decay washable fields (blood, guts etc.) by this
const time_duration amount_gas = amount / 5; // Decay gas type fields by this
// Coordinate code copied from lightmap calculations
// TODO: Z
const int smz = abs_sub.z;
Expand Down Expand Up @@ -2738,23 +2735,10 @@ void map::decay_fields_and_scent( const time_duration &amount )
to_proc--;
field_entry &cur = fp.second;
const field_type_id type = cur.get_field_type();
if( type == fd_fire ) {
cur.set_field_age( cur.get_field_age() + amount_fire );
}
if( type == fd_blood || type == fd_bile || type == fd_gibs_flesh ||
type == fd_gibs_veggy || type == fd_slime || type == fd_blood_veggy ||
type == fd_blood_insect || type == fd_blood_invertebrate ||
type == fd_gibs_insect || type == fd_gibs_invertebrate ) {
cur.set_field_age( cur.get_field_age() + amount_liquid );
}
if( type == fd_smoke || type == fd_toxic_gas || type == fd_fungicidal_gas ||
type == fd_tear_gas || type == fd_nuke_gas || type == fd_cigsmoke ||
type == fd_weedsmoke || type == fd_cracksmoke || type == fd_methsmoke ||
type == fd_relax_gas || type == fd_fungal_haze || type == fd_cold_air1 ||
type == fd_cold_air2 || type == fd_cold_air3 || type == fd_cold_air4 ||
type == fd_hot_air1 || type == fd_hot_air2 || type == fd_hot_air3 ||
type == fd_hot_air4 ) {
cur.set_field_age( cur.get_field_age() + amount_gas );
const int decay_amount_factor = type.obj().decay_amount_factor;
if( decay_amount_factor != 0 ) {
const time_duration decay_amount = amount / decay_amount_factor;
cur.set_field_age( cur.get_field_age() + decay_amount );
}
}
}
Expand Down
23 changes: 4 additions & 19 deletions src/map_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ bool map::process_fields_in_submap( submap *const current_submap,
debugmsg( "Whoooooa intensity of %d", cur.get_field_intensity() );
}

dirty_transparency_cache = curtype.obj().dirty_transparency_cache;

// Don't process "newborn" fields. This gives the player time to run if they need to.
if( cur.get_field_age() == 0_turns ) {
curtype = fd_null;
Expand Down Expand Up @@ -455,11 +457,8 @@ bool map::process_fields_in_submap( submap *const current_submap,

// TODO: Allow spreading to the sides if age < 0 && intensity == 3
}
if( curtype == fd_slime ) {
sblk.apply_slime( p, cur.get_field_intensity() * 10 );
}
if( curtype == fd_plasma || curtype == fd_laser ) {
dirty_transparency_cache = true;
if( curtype.obj().apply_slime_factor > 0 ) {
sblk.apply_slime( p, cur.get_field_intensity() * curtype.obj().apply_slime_factor );
}
if( curtype == fd_fire ) {
// Entire objects for ter/frn for flags
Expand Down Expand Up @@ -941,32 +940,26 @@ bool map::process_fields_in_submap( submap *const current_submap,
}
}
if( curtype == fd_smoke || curtype == fd_tear_gas ) {
dirty_transparency_cache = true;
spread_gas( cur, p, 10, 0_turns, sblk );
}
if( curtype == fd_relax_gas ) {
dirty_transparency_cache = true;
spread_gas( cur, p, 15, 5_minutes, sblk );
}
if( curtype == fd_fungal_haze ) {
dirty_transparency_cache = true;
spread_gas( cur, p, 13, 5_turns, sblk );
if( one_in( 10 - 2 * cur.get_field_intensity() ) ) {
// Haze'd terrain
fungal_effects( *g, g->m ).spread_fungus( p );
}
}
if( curtype == fd_toxic_gas ) {
dirty_transparency_cache = true;
spread_gas( cur, p, 30, 3_minutes, sblk );
}

if( curtype == fd_cigsmoke ) {
dirty_transparency_cache = true;
spread_gas( cur, p, 250, 6_minutes, sblk );
}
if( curtype == fd_weedsmoke ) {
dirty_transparency_cache = true;
spread_gas( cur, p, 200, 6_minutes, sblk );

if( one_in( 20 ) ) {
Expand All @@ -978,7 +971,6 @@ bool map::process_fields_in_submap( submap *const current_submap,
}

if( curtype == fd_methsmoke ) {
dirty_transparency_cache = true;
spread_gas( cur, p, 175, 7_minutes, sblk );
if( one_in( 20 ) ) {
if( npc *const np = g->critter_at<npc>( p ) ) {
Expand All @@ -987,7 +979,6 @@ bool map::process_fields_in_submap( submap *const current_submap,
}
}
if( curtype == fd_cracksmoke ) {
dirty_transparency_cache = true;
spread_gas( cur, p, 175, 8_minutes, sblk );

if( one_in( 20 ) ) {
Expand All @@ -997,7 +988,6 @@ bool map::process_fields_in_submap( submap *const current_submap,
}
}
if( curtype == fd_nuke_gas ) {
dirty_transparency_cache = true;
int extra_radiation = rng( 0, cur.get_field_intensity() );
adjust_radiation( p, extra_radiation );
spread_gas( cur, p, 15, 1_minutes, sblk );
Expand All @@ -1011,7 +1001,6 @@ bool map::process_fields_in_submap( submap *const current_submap,
spread_gas( cur, p, 100, 100_minutes, sblk );
}
if( curtype == fd_gas_vent ) {
dirty_transparency_cache = true;
for( const tripoint &pnt : points_in_radius( p, cur.get_field_intensity() - 1 ) ) {
field &wandering_field = get_field( pnt );
tmpfld = wandering_field.find_field( fd_toxic_gas );
Expand All @@ -1023,7 +1012,6 @@ bool map::process_fields_in_submap( submap *const current_submap,
}
}
if( curtype == fd_smoke_vent ) {
dirty_transparency_cache = true;
for( const tripoint &pnt : points_in_radius( p, cur.get_field_intensity() - 1 ) ) {
field &wandering_field = get_field( pnt );
tmpfld = wandering_field.find_field( fd_smoke );
Expand Down Expand Up @@ -1232,7 +1220,6 @@ bool map::process_fields_in_submap( submap *const current_submap,
}
}
if( curtype == fd_bees ) {
dirty_transparency_cache = true;
// Poor bees are vulnerable to so many other fields.
// TODO: maybe adjust effects based on different fields.
if( curfield.find_field( fd_web ) ||
Expand Down Expand Up @@ -1286,7 +1273,6 @@ bool map::process_fields_in_submap( submap *const current_submap,
}
if( curtype == fd_incendiary ) {
// Needed for variable scope
dirty_transparency_cache = true;
tripoint dst( p.x + rng( -1, 1 ), p.y + rng( -1, 1 ), p.z );
if( has_flag( TFLAG_FLAMMABLE, dst ) ||
has_flag( TFLAG_FLAMMABLE_ASH, dst ) ||
Expand All @@ -1307,7 +1293,6 @@ bool map::process_fields_in_submap( submap *const current_submap,
make_rubble( p );
}
if( curtype == fd_fungicidal_gas ) {
dirty_transparency_cache = true;
spread_gas( cur, p, 120, 1_minutes, sblk );
// Check the terrain and replace it accordingly to simulate the fungus dieing off
const ter_t &ter = map_tile.get_ter_t();
Expand Down

0 comments on commit 9dd7b1e

Please sign in to comment.