Skip to content

Commit

Permalink
refactor: move large header lines to cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Mar 21, 2023
1 parent 0e35528 commit 4160d41
Show file tree
Hide file tree
Showing 28 changed files with 1,637 additions and 1,314 deletions.
739 changes: 739 additions & 0 deletions src/assign.cpp

Large diffs are not rendered by default.

731 changes: 77 additions & 654 deletions src/assign.h

Large diffs are not rendered by default.

75 changes: 75 additions & 0 deletions src/cellular_automata.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "cellular_automata.h"

int CellularAutomata::neighbor_count( const std::vector<std::vector<int>> &cells,
const point &size,
const point &p )
{
int neighbors = 0;
for( int ni = -1; ni <= 1; ni++ ) {
for( int nj = -1; nj <= 1; nj++ ) {
const point n( p + point( ni, nj ) );

// These neighbors are outside the bounds, so they can't contribute.
if( n.x < 0 || n.x >= size.x || n.y < 0 || n.y >= size.y ) {
continue;
}

neighbors += cells[n.x][n.y];
}
}
// Because we included ourself in the loop above, subtract ourselves back out.
neighbors -= cells[p.x][p.y];

return neighbors;
}
std::vector<std::vector<int>> CellularAutomata::generate_cellular_automaton(
const point &size,
const int alive,
const int iterations,
const int birth_limit,
const int stasis_limit )
{
std::vector<std::vector<int>> current( size.x, std::vector<int>( size.y, 0 ) );
std::vector<std::vector<int>> next( size.x, std::vector<int>( size.y, 0 ) );

// Initialize our initial set of cells.
for( int i = 0; i < size.x; i++ ) {
for( int j = 0; j < size.y; j++ ) {
current[i][j] = x_in_y( alive, 100 );
}
}

for( int iteration = 0; iteration < iterations; iteration++ ) {
for( int i = 0; i < size.x; i++ ) {
for( int j = 0; j < size.y; j++ ) {
// Skip the edges--no need to complicate this with more complex neighbor
// calculations, just keep them constant.
if( i == 0 || i == size.x - 1 || j == 0 || j == size.y - 1 ) {
next[i][j] = 0;
continue;
}

// Count our neighors.
const int neighbors = neighbor_count( current, size, point( i, j ) );

// Dead and > birth_limit neighbors, so become alive.
if( ( current[i][j] == 0 ) && ( neighbors > birth_limit ) ) {
next[i][j] = 1;
}
// Alive and > statis_limit neighbors, so stay alive.
else if( ( current[i][j] == 1 ) && ( neighbors > stasis_limit ) ) {
next[i][j] = 1;
}
// Else, die.
else {
next[i][j] = 0;
}
}
}

// Swap our current and next vectors and repeat.
std::swap( current, next );
}

return current;
}
77 changes: 9 additions & 68 deletions src/cellular_automata.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,9 @@ namespace CellularAutomata
* @param p
* @returns The number of neighbors that are alive, a value between 0 and 8.
*/
inline int neighbor_count( const std::vector<std::vector<int>> &cells, const point &size,
const point &p )
{
int neighbors = 0;
for( int ni = -1; ni <= 1; ni++ ) {
for( int nj = -1; nj <= 1; nj++ ) {
const point n( p + point( ni, nj ) );

// These neighbors are outside the bounds, so they can't contribute.
if( n.x < 0 || n.x >= size.x || n.y < 0 || n.y >= size.y ) {
continue;
}

neighbors += cells[n.x][n.y];
}
}
// Because we included ourself in the loop above, subtract ourselves back out.
neighbors -= cells[p.x][p.y];

return neighbors;
}
int neighbor_count( const std::vector<std::vector<int>> &cells,
const point &size,
const point &p );

/**
* Generate a cellular automaton using the provided parameters.
Expand All @@ -54,53 +36,12 @@ inline int neighbor_count( const std::vector<std::vector<int>> &cells, const poi
* @param stasis_limit Alive cells with > statis_limit neighbors stay alive
* @returns The width x height grid of cells. Each cell is a 0 if dead or a 1 if alive.
*/
inline std::vector<std::vector<int>> generate_cellular_automaton( const point &size,
const int alive, const int iterations, const int birth_limit, const int stasis_limit )
{
std::vector<std::vector<int>> current( size.x, std::vector<int>( size.y, 0 ) );
std::vector<std::vector<int>> next( size.x, std::vector<int>( size.y, 0 ) );

// Initialize our initial set of cells.
for( int i = 0; i < size.x; i++ ) {
for( int j = 0; j < size.y; j++ ) {
current[i][j] = x_in_y( alive, 100 );
}
}

for( int iteration = 0; iteration < iterations; iteration++ ) {
for( int i = 0; i < size.x; i++ ) {
for( int j = 0; j < size.y; j++ ) {
// Skip the edges--no need to complicate this with more complex neighbor
// calculations, just keep them constant.
if( i == 0 || i == size.x - 1 || j == 0 || j == size.y - 1 ) {
next[i][j] = 0;
continue;
}

// Count our neighors.
const int neighbors = neighbor_count( current, size, point( i, j ) );

// Dead and > birth_limit neighbors, so become alive.
if( ( current[i][j] == 0 ) && ( neighbors > birth_limit ) ) {
next[i][j] = 1;
}
// Alive and > statis_limit neighbors, so stay alive.
else if( ( current[i][j] == 1 ) && ( neighbors > stasis_limit ) ) {
next[i][j] = 1;
}
// Else, die.
else {
next[i][j] = 0;
}
}
}

// Swap our current and next vectors and repeat.
std::swap( current, next );
}

return current;
}
std::vector<std::vector<int>> generate_cellular_automaton(
const point &size,
const int alive,
const int iterations,
const int birth_limit,
const int stasis_limit );
} // namespace CellularAutomata

#endif // CATA_SRC_CELLULAR_AUTOMATA_H
33 changes: 33 additions & 0 deletions src/coordinates.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "coordinates.h"

void real_coords::fromabs( const point &abs )
{
const point norm( std::abs( abs.x ), std::abs( abs.y ) );
abs_pos = abs;

if( abs.x < 0 ) {
abs_sub.x = ( abs.x - SEEX + 1 ) / SEEX;
sub_pos.x = SEEX - 1 - ( ( norm.x - 1 ) % SEEX );
abs_om.x = ( abs_sub.x - subs_in_om_n ) / subs_in_om;
om_sub.x = subs_in_om_n - ( ( ( norm.x - 1 ) / SEEX ) % subs_in_om );
} else {
abs_sub.x = norm.x / SEEX;
sub_pos.x = abs.x % SEEX;
abs_om.x = abs_sub.x / subs_in_om;
om_sub.x = abs_sub.x % subs_in_om;
}
om_pos.x = om_sub.x / 2;

if( abs.y < 0 ) {
abs_sub.y = ( abs.y - SEEY + 1 ) / SEEY;
sub_pos.y = SEEY - 1 - ( ( norm.y - 1 ) % SEEY );
abs_om.y = ( abs_sub.y - subs_in_om_n ) / subs_in_om;
om_sub.y = subs_in_om_n - ( ( ( norm.y - 1 ) / SEEY ) % subs_in_om );
} else {
abs_sub.y = norm.y / SEEY;
sub_pos.y = abs.y % SEEY;
abs_om.y = abs_sub.y / subs_in_om;
om_sub.y = abs_sub.y % subs_in_om;
}
om_pos.y = om_sub.y / 2;
}
31 changes: 1 addition & 30 deletions src/coordinates.h
Original file line number Diff line number Diff line change
Expand Up @@ -643,36 +643,7 @@ struct real_coords {
fromabs( ap );
}

void fromabs( const point &abs ) {
const point norm( std::abs( abs.x ), std::abs( abs.y ) );
abs_pos = abs;

if( abs.x < 0 ) {
abs_sub.x = ( abs.x - SEEX + 1 ) / SEEX;
sub_pos.x = SEEX - 1 - ( ( norm.x - 1 ) % SEEX );
abs_om.x = ( abs_sub.x - subs_in_om_n ) / subs_in_om;
om_sub.x = subs_in_om_n - ( ( ( norm.x - 1 ) / SEEX ) % subs_in_om );
} else {
abs_sub.x = norm.x / SEEX;
sub_pos.x = abs.x % SEEX;
abs_om.x = abs_sub.x / subs_in_om;
om_sub.x = abs_sub.x % subs_in_om;
}
om_pos.x = om_sub.x / 2;

if( abs.y < 0 ) {
abs_sub.y = ( abs.y - SEEY + 1 ) / SEEY;
sub_pos.y = SEEY - 1 - ( ( norm.y - 1 ) % SEEY );
abs_om.y = ( abs_sub.y - subs_in_om_n ) / subs_in_om;
om_sub.y = subs_in_om_n - ( ( ( norm.y - 1 ) / SEEY ) % subs_in_om );
} else {
abs_sub.y = norm.y / SEEY;
sub_pos.y = abs.y % SEEY;
abs_om.y = abs_sub.y / subs_in_om;
om_sub.y = abs_sub.y % subs_in_om;
}
om_pos.y = om_sub.y / 2;
}
void fromabs( const point &abs );

// specifically for the subjective position returned by overmap::draw
void fromomap( const point &rel_om, const point &rel_om_pos ) {
Expand Down
20 changes: 20 additions & 0 deletions src/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,24 @@ event::fields_type event::get_fields( event_type type )
type, std::make_integer_sequence<int, static_cast<int>( event_type::num_event_types )> {} );
}

cata_variant event::get_variant( const std::string &key ) const
{
auto it = data_.find( key );
if( it == data_.end() ) {
debugmsg( "No such key %s in event of type %s", key,
io::enum_to_string( type_ ) );
abort();
}
return it->second;
}

cata_variant event::get_variant_or_void( const std::string &key ) const
{
auto it = data_.find( key );
if( it == data_.end() ) {
return cata_variant();
}
return it->second;
}

} // namespace cata
20 changes: 3 additions & 17 deletions src/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -580,23 +580,9 @@ class event
return time_;
}

cata_variant get_variant( const std::string &key ) const {
auto it = data_.find( key );
if( it == data_.end() ) {
debugmsg( "No such key %s in event of type %s", key,
io::enum_to_string( type_ ) );
abort();
}
return it->second;
}

cata_variant get_variant_or_void( const std::string &key ) const {
auto it = data_.find( key );
if( it == data_.end() ) {
return cata_variant();
}
return it->second;
}
cata_variant get_variant( const std::string &key ) const;

cata_variant get_variant_or_void( const std::string &key ) const;

template<cata_variant_type Type>
auto get( const std::string &key ) const {
Expand Down
70 changes: 70 additions & 0 deletions src/font_loader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "font_loader.h"

void ensure_unifont_loaded( std::vector<std::string> &font_list )
{
if( std::find( std::begin( font_list ), std::end( font_list ), "unifont" ) == font_list.end() ) {
font_list.emplace_back( PATH_INFO::fontdir() + "unifont.ttf" );
}
}

void font_loader::load_throws( const std::string &path )
{
try {
cata_ifstream stream = std::move( cata_ifstream().mode( cata_ios_mode::binary ).open( path ) );
JsonIn json( *stream );
JsonObject config = json.get_object();
if( config.has_string( "typeface" ) ) {
typeface.emplace_back( config.get_string( "typeface" ) );
} else {
config.read( "typeface", typeface );
}
if( config.has_string( "map_typeface" ) ) {
map_typeface.emplace_back( config.get_string( "map_typeface" ) );
} else {
config.read( "map_typeface", map_typeface );
}
if( config.has_string( "overmap_typeface" ) ) {
overmap_typeface.emplace_back( config.get_string( "overmap_typeface" ) );
} else {
config.read( "overmap_typeface", overmap_typeface );
}

ensure_unifont_loaded( typeface );
ensure_unifont_loaded( map_typeface );
ensure_unifont_loaded( overmap_typeface );

} catch( const std::exception &err ) {
throw std::runtime_error( std::string( "loading font settings from " ) + path + " failed: " +
err.what() );
}
}

void font_loader::load()
{
const std::string user_fontconfig = PATH_INFO::user_fontconfig();
const std::string fontconfig = PATH_INFO::fontconfig();
bool try_user = true;
if( !file_exist( user_fontconfig ) ) {
if( !copy_file( fontconfig, user_fontconfig ) ) {
try_user = false;
DebugLog( DL::Error, DC::SDL ) << "failed to create user font config file "
<< user_fontconfig;
}
}
if( try_user ) {
font_loader copy = *this;
try {
load_throws( user_fontconfig );
return;
} catch( const std::exception &e ) {
DebugLog( DL::Error, DC::SDL ) << e.what();
}
*this = copy;
}
try {
load_throws( fontconfig );
} catch( const std::exception &e ) {
DebugLog( DL::Error, DC::SDL ) << e.what();
abort();
}
}
Loading

0 comments on commit 4160d41

Please sign in to comment.