Skip to content

Commit

Permalink
Merge pull request #34195 from BevapDin/meh
Browse files Browse the repository at this point in the history
Refactor loops over x and y into one loop over tripoint.
  • Loading branch information
kevingranade authored Sep 28, 2019
2 parents 0b58608 + 5c4c8e3 commit 37e0c61
Show file tree
Hide file tree
Showing 26 changed files with 715 additions and 888 deletions.
19 changes: 8 additions & 11 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4186,18 +4186,15 @@ void activity_handlers::tree_communion_do_turn( player_activity *act, player *p
}
return;
}
for( int dx = -1; dx <= 1; dx++ ) {
for( int dy = -1; dy <= 1; dy++ ) {
tripoint neighbor = tpt + point( dx, dy );
if( seen.find( neighbor ) != seen.end() ) {
continue;
}
seen.insert( neighbor );
if( !overmap_buffer.ter( neighbor ).obj().is_wooded() ) {
continue;
}
q.push( neighbor );
for( const tripoint &neighbor : points_in_radius( tpt, 1 ) ) {
if( seen.find( neighbor ) != seen.end() ) {
continue;
}
seen.insert( neighbor );
if( !overmap_buffer.ter( neighbor ).obj().is_wooded() ) {
continue;
}
q.push( neighbor );
}
q.pop();
}
Expand Down
22 changes: 5 additions & 17 deletions src/clzones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,16 +587,9 @@ void zone_manager::cache_data()
const std::string &type_hash = elem.get_type_hash();
auto &cache = area_cache[type_hash];

tripoint start = elem.get_start_point();
tripoint end = elem.get_end_point();

// Draw marked area
for( int x = start.x; x <= end.x; ++x ) {
for( int y = start.y; y <= end.y; ++y ) {
for( int z = start.z; z <= end.z; ++z ) {
cache.insert( tripoint( x, y, z ) );
}
}
for( const tripoint &p : tripoint_range( elem.get_start_point(), elem.get_end_point() ) ) {
cache.insert( p );
}
}
}
Expand All @@ -613,16 +606,11 @@ void zone_manager::cache_vzones()
const std::string &type_hash = elem->get_type_hash();
auto &cache = area_cache[type_hash];

tripoint start = elem->get_start_point();
tripoint end = elem->get_end_point();
// @todo looks very similar to the above cache_data - maybe merge it?

// Draw marked area
for( int x = start.x; x <= end.x; ++x ) {
for( int y = start.y; y <= end.y; ++y ) {
for( int z = start.z; z <= end.z; ++z ) {
cache.insert( tripoint( x, y, z ) );
}
}
for( const tripoint &p : tripoint_range( elem->get_start_point(), elem->get_end_point() ) ) {
cache.insert( p );
}
}
}
Expand Down
293 changes: 126 additions & 167 deletions src/computer.cpp

Large diffs are not rendered by default.

13 changes: 5 additions & 8 deletions src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "messages.h"
#include "monster.h"
#include "mtype.h"
#include "map_iterator.h"
#include "npc.h"
#include "output.h"
#include "projectile.h"
Expand Down Expand Up @@ -292,14 +293,10 @@ bool Creature::sees( const tripoint &t, bool is_player, int range_mod ) const
static bool overlaps_vehicle( const std::set<tripoint> &veh_area, const tripoint &pos,
const int area )
{
tripoint tmp = pos;
int &x = tmp.x;
int &y = tmp.y;
for( x = pos.x - area; x < pos.x + area; x++ ) {
for( y = pos.y - area; y < pos.y + area; y++ ) {
if( veh_area.count( tmp ) > 0 ) {
return true;
}
for( const tripoint &tmp : tripoint_range( pos - tripoint( area, area, 0 ),
pos + tripoint( area - 1, area - 1, 0 ) ) ) {
if( veh_area.count( tmp ) > 0 ) {
return true;
}
}

Expand Down
58 changes: 25 additions & 33 deletions src/editmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "vehicle.h"
#include "vpart_position.h"
#include "cata_utility.h"
#include "map_iterator.h"
#include "creature.h"
#include "game_constants.h"
#include "int_id.h"
Expand Down Expand Up @@ -400,32 +401,29 @@ void editmap::uber_draw_ter( const catacurses::window &w, map *m )
if( refresh_mplans ) {
hilights["mplan"].points.clear();
}
for( int x = start.x, sx = 0; x <= end.x; x++, sx++ ) {
for( int y = start.y, sy = 0; y <= end.y; y++, sy++ ) {
tripoint p{ x, y, target.z };
int sym = game_map ? '%' : ' ';
if( x >= 0 && x < msize && y >= 0 && y < msize ) {
if( game_map ) {
Creature *critter = g->critter_at( p );
if( critter != nullptr ) {
critter->draw( w, center.xy(), false );
} else {
m->drawsq( w, g->u, p, false, draw_itm, center, false, true );
}
if( refresh_mplans ) {
monster *mon = dynamic_cast<monster *>( critter );
if( mon != nullptr && mon->pos() != mon->move_target() ) {
for( auto &location : line_to( mon->pos(), mon->move_target() ) ) {
hilights["mplan"].points[location] = 1;
}
}
}
for( const tripoint &p : tripoint_range( start, end ) ) {
int sym = game_map ? '%' : ' ';
if( p.x >= 0 && p.x < msize && p.y >= 0 && p.y < msize ) {
if( game_map ) {
Creature *critter = g->critter_at( p );
if( critter != nullptr ) {
critter->draw( w, center.xy(), false );
} else {
m->drawsq( w, g->u, p, false, draw_itm, center, false, true );
}
if( refresh_mplans ) {
monster *mon = dynamic_cast<monster *>( critter );
if( mon != nullptr && mon->pos() != mon->move_target() ) {
for( auto &location : line_to( mon->pos(), mon->move_target() ) ) {
hilights["mplan"].points[location] = 1;
}
}
}
} else {
mvwputch( w, point( sx, sy ), c_dark_gray, sym );
m->drawsq( w, g->u, p, false, draw_itm, center, false, true );
}
} else {
mvwputch( w, p.xy() - start.xy(), c_dark_gray, sym );
}
}
if( refresh_mplans ) {
Expand Down Expand Up @@ -1325,13 +1323,10 @@ void editmap::recalc_target( shapetype shape )
switch( shape ) {
case editmap_circle: {
int radius = rl_dist( origin, target );
for( int x = origin.x - radius; x <= origin.x + radius; x++ ) {
for( int y = origin.y - radius; y <= origin.y + radius; y++ ) {
const tripoint p( x, y, z );
if( rl_dist( p, origin ) <= radius ) {
if( editmap_boundaries.contains_half_open( p ) ) {
target_list.push_back( p );
}
for( const tripoint &p : g->m.points_in_radius( origin, radius ) ) {
if( rl_dist( p, origin ) <= radius ) {
if( editmap_boundaries.contains_half_open( p ) ) {
target_list.push_back( p );
}
}
}
Expand Down Expand Up @@ -1690,11 +1685,8 @@ void editmap::mapgen_preview( const real_coords &tc, uilist &gmenu )
hilights["mapgentgt"].draw( *this, true );
g->draw_panels();
tmpmap.reset_vehicle_cache( target.z );
for( int x = 0; x < SEEX * 2; x++ ) {
for( int y = 0; y < SEEY * 2; y++ ) {
tmpmap.drawsq( w_preview, g->u, tripoint( x, y, target.z ),
false, true, tripoint( SEEX, SEEY, target.z ), false, true );
}
for( const tripoint &p : tmpmap.points_on_zlevel() ) {
tmpmap.drawsq( w_preview, g->u, p, false, true, tripoint( SEEX, SEEY, target.z ), false, true );
}
wrefresh( w_preview );
}
Expand Down
157 changes: 77 additions & 80 deletions src/explosion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "shadowcasting.h"
#include "sounds.h"
#include "string_formatter.h"
#include "map_iterator.h"
#include "translations.h"
#include "trap.h"
#include "units.h"
Expand Down Expand Up @@ -356,10 +357,9 @@ static std::vector<tripoint> shrapnel( const tripoint &src, int power,
// TODO: Calculate range based on max effective range for projectiles.
// Basically bisect between 0 and map diameter using shrapnel_calc().
// Need to update shadowcasting to support limiting range without adjusting initial distance.
const tripoint start = { 0, 0, src.z };
const tripoint end = { g->m.getmapsize() *SEEX, g->m.getmapsize() *SEEY, src.z };
const tripoint_range area = g->m.points_on_zlevel( src.z );

g->m.build_obstacle_cache( start, end, obstacle_cache );
g->m.build_obstacle_cache( area.min(), area.max() + tripoint_south_east, obstacle_cache );

// Shadowcasting normally ignores the origin square,
// so apply it manually to catch monsters standing on the explosive.
Expand All @@ -373,90 +373,87 @@ static std::vector<tripoint> shrapnel( const tripoint &src, int power,
( visited_cache, obstacle_cache, src.xy(), 0, initial_cloud );

// Now visited_caches are populated with density and velocity of fragments.
for( int x = start.x; x < end.x; x++ ) {
for( int y = start.y; y < end.y; y++ ) {
fragment_cloud &cloud = visited_cache[x][y];
if( cloud.density <= MIN_FRAGMENT_DENSITY ||
cloud.velocity <= MIN_EFFECTIVE_VELOCITY ) {
continue;
}
distrib.emplace_back( x, y, src.z );
tripoint target( x, y, src.z );
int damage = ballistic_damage( cloud.velocity, fragment_mass );
auto critter = g->critter_at( target );
if( damage > 0 && critter && !critter->is_dead_state() ) {
std::poisson_distribution<> d( cloud.density );
int hits = d( rng_get_engine() );
dealt_projectile_attack frag;
frag.proj = proj;
frag.proj.speed = cloud.velocity;
frag.proj.impact = damage_instance::physical( 0, damage, 0, 0 );
// dealt_dag->m.total_damage() == 0 means armor block
// dealt_dag->m.total_damage() > 0 means took damage
// Need to diffentiate target among player, npc, and monster
// Do we even print monster damage?
int damage_taken = 0;
int damaging_hits = 0;
int non_damaging_hits = 0;
for( int i = 0; i < hits; ++i ) {
frag.missed_by = rng_float( 0.05, 1.0 );
critter->deal_projectile_attack( nullptr, frag, false );
if( frag.dealt_dam.total_damage() > 0 ) {
damaging_hits++;
damage_taken += frag.dealt_dam.total_damage();
} else {
non_damaging_hits++;
}
add_msg( m_debug, "Shrapnel hit %s at %d m/s at a distance of %d",
critter->disp_name(),
frag.proj.speed, rl_dist( src, target ) );
add_msg( m_debug, "Shrapnel dealt %d damage", frag.dealt_dam.total_damage() );
if( critter->is_dead_state() ) {
break;
}
for( const tripoint &target : area ) {
fragment_cloud &cloud = visited_cache[target.x][target.y];
if( cloud.density <= MIN_FRAGMENT_DENSITY ||
cloud.velocity <= MIN_EFFECTIVE_VELOCITY ) {
continue;
}
distrib.emplace_back( target );
int damage = ballistic_damage( cloud.velocity, fragment_mass );
auto critter = g->critter_at( target );
if( damage > 0 && critter && !critter->is_dead_state() ) {
std::poisson_distribution<> d( cloud.density );
int hits = d( rng_get_engine() );
dealt_projectile_attack frag;
frag.proj = proj;
frag.proj.speed = cloud.velocity;
frag.proj.impact = damage_instance::physical( 0, damage, 0, 0 );
// dealt_dag->m.total_damage() == 0 means armor block
// dealt_dag->m.total_damage() > 0 means took damage
// Need to diffentiate target among player, npc, and monster
// Do we even print monster damage?
int damage_taken = 0;
int damaging_hits = 0;
int non_damaging_hits = 0;
for( int i = 0; i < hits; ++i ) {
frag.missed_by = rng_float( 0.05, 1.0 );
critter->deal_projectile_attack( nullptr, frag, false );
if( frag.dealt_dam.total_damage() > 0 ) {
damaging_hits++;
damage_taken += frag.dealt_dam.total_damage();
} else {
non_damaging_hits++;
}
int total_hits = damaging_hits + non_damaging_hits;
if( total_hits > 0 && g->u.sees( *critter ) ) {
// Building a phrase to summarize the fragment effects.
// Target, Number of impacts, total amount of damage, proportion of deflected fragments.
std::map<int, std::string> impact_count_descriptions = {
{ 1, _( "a" ) }, { 2, _( "several" ) }, { 5, _( "many" ) },
{ 20, _( "a large number of" ) }, { 100, _( "a huge number of" ) },
{ std::numeric_limits<int>::max(), _( "an immense number of" ) }
};
std::string impact_count = std::find_if(
impact_count_descriptions.begin(), impact_count_descriptions.end(),
[total_hits]( const std::pair<int, std::string> &desc ) {
return desc.first >= total_hits;
} )->second;
std::string damage_description = ( damage_taken > 0 ) ?
string_format( _( "dealing %d damage" ), damage_taken ) :
_( "but they deal no damage" );
if( critter->is_player() ) {
add_msg( ngettext( "You are hit by %s bomb fragment, %s.",
"You are hit by %s bomb fragments, %s.", total_hits ),
impact_count, damage_description );
} else if( critter->is_npc() ) {
critter->add_msg_if_npc(
ngettext( "<npcname> is hit by %s bomb fragment, %s.",
"<npcname> is hit by %s bomb fragments, %s.",
total_hits ),
impact_count, damage_description );
} else {
add_msg( ngettext( "The %s is hit by %s bomb fragment, %s.",
"The %s is hit by %s bomb fragments, %s.", total_hits ),
critter->disp_name(), impact_count, damage_description );
}
add_msg( m_debug, "Shrapnel hit %s at %d m/s at a distance of %d",
critter->disp_name(),
frag.proj.speed, rl_dist( src, target ) );
add_msg( m_debug, "Shrapnel dealt %d damage", frag.dealt_dam.total_damage() );
if( critter->is_dead_state() ) {
break;
}
}
if( g->m.impassable( target ) ) {
if( optional_vpart_position vp = g->m.veh_at( target ) ) {
vp->vehicle().damage( vp->part_index(), damage );
int total_hits = damaging_hits + non_damaging_hits;
if( total_hits > 0 && g->u.sees( *critter ) ) {
// Building a phrase to summarize the fragment effects.
// Target, Number of impacts, total amount of damage, proportion of deflected fragments.
std::map<int, std::string> impact_count_descriptions = {
{ 1, _( "a" ) }, { 2, _( "several" ) }, { 5, _( "many" ) },
{ 20, _( "a large number of" ) }, { 100, _( "a huge number of" ) },
{ std::numeric_limits<int>::max(), _( "an immense number of" ) }
};
std::string impact_count = std::find_if(
impact_count_descriptions.begin(), impact_count_descriptions.end(),
[total_hits]( const std::pair<int, std::string> &desc ) {
return desc.first >= total_hits;
} )->second;
std::string damage_description = ( damage_taken > 0 ) ?
string_format( _( "dealing %d damage" ), damage_taken ) :
_( "but they deal no damage" );
if( critter->is_player() ) {
add_msg( ngettext( "You are hit by %s bomb fragment, %s.",
"You are hit by %s bomb fragments, %s.", total_hits ),
impact_count, damage_description );
} else if( critter->is_npc() ) {
critter->add_msg_if_npc(
ngettext( "<npcname> is hit by %s bomb fragment, %s.",
"<npcname> is hit by %s bomb fragments, %s.",
total_hits ),
impact_count, damage_description );
} else {
g->m.bash( target, damage / 10, true );
add_msg( ngettext( "The %s is hit by %s bomb fragment, %s.",
"The %s is hit by %s bomb fragments, %s.", total_hits ),
critter->disp_name(), impact_count, damage_description );
}
}
}
if( g->m.impassable( target ) ) {
if( optional_vpart_position vp = g->m.veh_at( target ) ) {
vp->vehicle().damage( vp->part_index(), damage );
} else {
g->m.bash( target, damage / 10, true );
}
}
}

return distrib;
Expand Down
15 changes: 6 additions & 9 deletions src/faction_camp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3198,15 +3198,12 @@ std::vector<std::pair<std::string, tripoint>> talk_function::om_building_region(
const tripoint &omt_pos, int range, bool purge )
{
std::vector<std::pair<std::string, tripoint>> om_camp_region;
for( int x = -range; x <= range; x++ ) {
for( int y = -range; y <= range; y++ ) {
const tripoint omt_near_pos = omt_pos + point( x, y );
oter_id &omt_rnear = overmap_buffer.ter( omt_near_pos );
std::string om_rnear_id = omt_rnear.id().c_str();
if( !purge || ( om_rnear_id.find( "faction_base_" ) != std::string::npos &&
om_rnear_id.find( "faction_base_camp" ) == std::string::npos ) ) {
om_camp_region.push_back( std::make_pair( om_rnear_id, omt_near_pos ) );
}
for( const tripoint &omt_near_pos : points_in_radius( omt_pos, range ) ) {
oter_id &omt_rnear = overmap_buffer.ter( omt_near_pos );
std::string om_rnear_id = omt_rnear.id().c_str();
if( !purge || ( om_rnear_id.find( "faction_base_" ) != std::string::npos &&
om_rnear_id.find( "faction_base_camp" ) == std::string::npos ) ) {
om_camp_region.push_back( std::make_pair( om_rnear_id, omt_near_pos ) );
}
}
return om_camp_region;
Expand Down
Loading

0 comments on commit 37e0c61

Please sign in to comment.