Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][CR] Add constants for easily changing the size of the reality bubble at compile time. #23376

Closed
wants to merge 8 commits into from

Conversation

mrkybe
Copy link
Contributor

@mrkybe mrkybe commented Apr 5, 2018

They should really be documented better, but I don't have enough of an understanding of the various parts to do it.

@mrkybe mrkybe changed the title Add constants for easily changing the size of the reality bubble at compile time. [CR} Add constants for easily changing the size of the reality bubble at compile time. Apr 5, 2018
@mrkybe mrkybe changed the title [CR} Add constants for easily changing the size of the reality bubble at compile time. [CR] Add constants for easily changing the size of the reality bubble at compile time. Apr 5, 2018
Copy link
Member

@kevingranade kevingranade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bunch of these aren't distances, and some if the distances shouldn't scale with map size.

@@ -890,7 +890,7 @@ void activity_handlers::firstaid_finish( player_activity *act, player *p )
static void rod_fish( player *p, int sSkillLevel, int fishChance )
{
if( sSkillLevel > fishChance ) {
std::vector<monster *> fishables = g->get_fishable(60); //get the nearby fish list.
std::vector<monster *> fishables = g->get_fishable(DISTANCE_FROM_CENTER); //get the nearby fish list.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is range at which you can attract fish to a lure, 60 already seems a bit overboard...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to fishing_distance.

src/computer.cpp Outdated
@@ -512,10 +512,10 @@ void computer::activate_function( computer_action action )
break;

case COMPACT_MAP_SEWER: {
g->u.moves -= 30;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the cost of reading the map, not distance related.

src/map.cpp Outdated
@@ -5695,18 +5695,26 @@ const visibility_variables &map::get_visibility_variables_cache() const {

lit_level map::apparent_light_at( const tripoint &p, const visibility_variables &cache ) const {
const int dist = rl_dist( g->u.pos(), p );

// Being outside of the world bubble overrides Clairvoyance.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be necessary, wasn't the fix for this in handle_mouseview?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally getting back to this PR. So apparently this function isn't only called from handle_mouseview, which is why I ended up handling it here instead of in handle_mouseview.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used heavily in map::update_visibility_cache, which is called on the entire bubble and only bubble (ie. it doesn't ever cross boundaries of the bubble).
update_visibility_cache is not cheap and apparent_light_at is the most expensive part of it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map::inbounds isn't an expensive check though. I'm not sure how else to do it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract the check to print_all_tile_info, which is the only place where this check matters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. Done.

src/map.cpp Outdated
const auto &map_cache = get_cache_ref(p.z);
bool obstructed = map_cache.seen_cache[p.x][p.y] <= LIGHT_TRANSPARENCY_SOLID + 0.1;
const float apparent_light = map_cache.seen_cache[p.x][p.y] * map_cache.lm[p.x][p.y];

// Unimpaired range is an override to strictly limit vision range based on various conditions,
// but the player can still see light sources.
if( dist > g->u.unimpaired_range() ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary change.

@@ -4512,7 +4512,7 @@ bool mattack::grenadier(monster *const z)
if (z->attitude_to( *target ) == Creature::A_FRIENDLY) {
return false;
}
int ret = grenade_helper(z, target, 30, 60, grenades);
int ret = grenade_helper(z, target, 30, DISTANCE_FROM_CENTER, grenades);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this is a range, not a position, it doesn't necessarily need updating.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upon further examination, this was a moves cost. Returned back to '60'.

src/player.cpp Outdated
@@ -2756,8 +2756,8 @@ float player::active_light() const

lumination = ( float )maxlum;

if( lumination < 60 && has_active_bionic( bio_flashlight ) ) {
lumination = 60;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a lighting level, not a distance.

src/sounds.cpp Outdated
@@ -175,7 +175,7 @@ int get_signal_for_hordes( const centroid &centr )
//Volume in tiles. Signal for hordes in submaps
//modify vol using weather vol.Weather can reduce monster hearing
const int vol = centr.volume - weather_data( g->weather ).sound_attn;
const int min_vol_cap = 60; //Hordes can't hear volume lower than this
const int min_vol_cap = DISTANCE_FROM_CENTER; //Hordes can't hear volume lower than this
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not map related.

@@ -42,7 +42,7 @@ weather_datum const weather_data( weather_type const type )
* Weather types data definition.
* Name, color in UI, ranged penalty, sight penalty,
* light modifier, sound attenuation, warn player?
* Note light modifier assumes baseline of DAYLIGHT_LEVEL at 60
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, light level, not distance.

@mrkybe mrkybe changed the title [CR] Add constants for easily changing the size of the reality bubble at compile time. [WIP][CR] Add constants for easily changing the size of the reality bubble at compile time. Apr 5, 2018
@ZhilkinSerg ZhilkinSerg added Code: Performance Performance boosting code (CPU, memory, etc.) [C++] Changes (can be) made in C++. Previously named `Code` Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style labels Apr 5, 2018
@BevapDin
Copy link
Contributor

BevapDin commented Apr 5, 2018

Please don't add macros, add constants as static constexpr.

#define DISTANCE_FROM_CENTER 60

/** Was used based off lighting but I'm not sure?*/
#define LIGHT_SIXTY_F 60.0f
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work right. You'd need to alter air transparency instead, otherwise you'll get a ton of side effects.

#define DISTANCE_FROM_CENTER 60

/** Was used based off lighting but I'm not sure?*/
#define LIGHT_SIXTY_F 60.0f
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you put the (current) value of the constant into its name, you might as well use the value directly. As soon as somebody changes the value, the name will be misleading.

Regarding two constants for the same value (int and float): define just one of them. And if you really need the other one (note: the compiler will convert the values as it needs it anyway), you should define it with a reference to the first one, e.g.

constexpr int DIST = 60;
constexpr float DIST_FLT = DIST;

But it's better to use the integer constant all the time, the compiler will automatically cast it at compile time when needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to using constexpr. My goal was to replace hardcoded values with constants while being certain that it wouldn't have any side-effects. I wanted to avoid side effects related to how integer division works vs floats, so I kept the types the same.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get rid of the int variant please.

src/map.cpp Outdated
@@ -5695,17 +5695,24 @@ const visibility_variables &map::get_visibility_variables_cache() const {

lit_level map::apparent_light_at( const tripoint &p, const visibility_variables &cache ) const {
const int dist = rl_dist( g->u.pos(), p );

// Being outside of the world bubble overrides Clairvoyance.
if(p.x < 0 || p.y < 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have map::inbounds for this, which works with tripoint directly, checks the z component as well and checks for the upper bound as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bad idea to have this check here, though.
apparent_light_at is used a lot and is one of the most expensive functions, according to profiler.

@@ -7329,7 +7329,13 @@ void game::print_all_tile_info( const tripoint &lp, const catacurses::window &w_
const int last_line, bool draw_terrain_indicators,
const visibility_variables &cache )
{
auto visibility = m.get_visibility( m.apparent_light_at( lp, cache ), cache );
visibility_type visibility;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about:

visibility = VIS_HIDDEN
if( g->map_ptr->inbounds( lp ) ) {
    visibility = m.get_visibility( m.apparent_light_at( lp, cache ), cache );
}

Just a styling thing (including the spaces around parentheses).

constexpr int player_view_distance_int = player_view_distance;

/** Distance to search for fishables from trap/fishing pole/etc */
constexpr int fishing_distance = 60;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please hoist this to a common shared header like iuse_actor.h, no need to make this visible to the entire game.

constexpr int fishing_distance = 60;

/** Light level to use for starting a fire */
constexpr float min_light_level_for_firestarter = 60.0f;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also move to iuse_actor.h, or move them both to iuse_constants.h, just not here.

@@ -88,6 +88,18 @@
/** Maximum range at which ranged attacks can be executed */
#define RANGE_HARD_CAP 60

/** Player view distance FLOAT */
constexpr float player_view_distance = 60.0f;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please hoist to a header file shared by lightmap.cpp and player.cpp, map.h would work.

@ZhilkinSerg
Copy link
Contributor

Feel free to reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[C++] Changes (can be) made in C++. Previously named `Code` Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style Code: Performance Performance boosting code (CPU, memory, etc.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants