Skip to content

Commit

Permalink
oter_id: add and use some directional utility functions
Browse files Browse the repository at this point in the history
Add some functions to parse oter_id and return the base string
without any directional suffixes, or the rotation int for the
the suffix, or the directional suffix itself.

Use these functions to make some code in recipe_groups, conditions,
and mapgen a little more correct.
  • Loading branch information
mlangsdorf committed Sep 23, 2019
1 parent f1c60ff commit a778635
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 44 deletions.
3 changes: 2 additions & 1 deletion src/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "map.h"
#include "mission.h"
#include "npc.h"
#include "overmap.h"
#include "overmapbuffer.h"
#include "recipe.h"
#include "recipe_groups.h"
Expand Down Expand Up @@ -375,7 +376,7 @@ void conditional_t<T>::set_at_om_location( JsonObject &jo, const std::string &me
return !recipe_group::get_recipes_by_id( "all_faction_base_types",
omt_ref.id().c_str() ).empty();
} else {
return omt_ref == oter_id( location );
return omt_ref == oter_id( oter_no_dir( oter_id( location ) ) );
}
};
}
Expand Down
16 changes: 3 additions & 13 deletions src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7913,19 +7913,9 @@ bool update_mapgen_function_json::update_map( const tripoint &omt_pos, const poi

// If the existing map is rotated, we need to rotate it back to the north
// orientation before applying our updates.
int rotation = 0;
// Yeah, because a terrain named "tt_east" (length 7) is clearly not rotated and does not to be checked.
if( map_id.size() > 7 ) {
if( map_id.substr( map_id.size() - 6, 6 ) == "_south" ) {
rotation = 2;
md.m.rotate( rotation );
} else if( map_id.substr( map_id.size() - 5, 5 ) == "_east" ) {
rotation = 3;
md.m.rotate( rotation );
} else if( map_id.substr( map_id.size() - 5, 5 ) == "_west" ) {
rotation = 1;
md.m.rotate( rotation );
}
int rotation = oter_get_rotation( overmap_buffer.ter( omt_pos ) );
if( rotation > 0 ) {
md.m.rotate( rotation );
}

const bool applied = update_map( md, offset, verify );
Expand Down
63 changes: 63 additions & 0 deletions src/overmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4467,3 +4467,66 @@ std::string enum_to_string<ot_match_type>( ot_match_type data )
} // namespace io

constexpr tripoint overmap::invalid_tripoint;

std::string oter_no_dir( const oter_id &oter )
{
std::string base_oter_id = oter.id().c_str();
size_t oter_len = base_oter_id.size();
if( oter_len > 7 ) {
if( base_oter_id.substr( oter_len - 6, 6 ) == "_south" ) {
return base_oter_id.substr( 0, oter_len - 6 );
} else if( base_oter_id.substr( oter_len - 6, 6 ) == "_north" ) {
return base_oter_id.substr( 0, oter_len - 6 );
}
}
if( oter_len > 6 ) {
if( base_oter_id.substr( oter_len - 5, 5 ) == "_west" ) {
return base_oter_id.substr( 0, oter_len - 5 );
} else if( base_oter_id.substr( oter_len - 5, 5 ) == "_east" ) {
return base_oter_id.substr( 0, oter_len - 5 );
}
}
return base_oter_id;
}

int oter_get_rotation( const oter_id &oter )
{
std::string base_oter_id = oter.id().c_str();
size_t oter_len = base_oter_id.size();
if( oter_len > 7 ) {
if( base_oter_id.substr( oter_len - 6, 6 ) == "_south" ) {
return 2;
} else if( base_oter_id.substr( oter_len - 6, 6 ) == "_north" ) {
return 0;
}
}
if( oter_len > 6 ) {
if( base_oter_id.substr( oter_len - 5, 5 ) == "_west" ) {
return 1;
} else if( base_oter_id.substr( oter_len - 5, 5 ) == "_east" ) {
return 3;
}
}
return 0;
}

std::string oter_get_rotation_string( const oter_id &oter )
{
std::string base_oter_id = oter.id().c_str();
size_t oter_len = base_oter_id.size();
if( oter_len > 7 ) {
if( base_oter_id.substr( oter_len - 6, 6 ) == "_south" ) {
return "_south";
} else if( base_oter_id.substr( oter_len - 6, 6 ) == "_north" ) {
return "_north";
}
}
if( oter_len > 6 ) {
if( base_oter_id.substr( oter_len - 5, 5 ) == "_west" ) {
return "_west";
} else if( base_oter_id.substr( oter_len - 5, 5 ) == "_east" ) {
return "_east";
}
}
return "";
}
15 changes: 15 additions & 0 deletions src/overmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,19 @@ bool is_ot_match( const std::string &name, const oter_id &oter,
*/
om_special_sectors get_sectors( int sector_width );

/**
* Returns the string of oter without any directional suffix
*/
std::string oter_no_dir( const oter_id &oter );

/**
* Return 0, 1, 2, 3 respectively if the suffix is _north, _west, _south, _east
* Return 0 if theres' no suffix
*/
int oter_get_rotation( const oter_id &oter );

/**
* Return the directional suffix or "" if there isn't one.
*/
std::string oter_get_rotation_string( const oter_id &oter );
#endif
32 changes: 2 additions & 30 deletions src/recipe_groups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "generic_factory.h"
#include "json.h"
#include "debug.h"
#include "overmap.h"
#include "string_id.h"
#include "type_id.h"

Expand Down Expand Up @@ -93,42 +94,13 @@ std::map<recipe_id, translation> recipe_group::get_recipes_by_id( const std::str
}
const recipe_group_data &group = recipe_groups_data.obj( group_id( id ) );
if( om_terrain_id != "ANY" ) {
std::string base_om_ter_id = om_terrain_id;
size_t om_ter_len = om_terrain_id.size();
if( om_ter_len > 7 ) {
if( om_terrain_id.substr( om_ter_len - 6, 6 ) == "_south" ) {
base_om_ter_id = om_terrain_id.substr( 0, om_ter_len - 6 );
} else if( om_terrain_id.substr( om_ter_len - 6, 6 ) == "_north" ) {
base_om_ter_id = om_terrain_id.substr( 0, om_ter_len - 6 );
}
}
if( om_ter_len > 6 ) {
if( om_terrain_id.substr( om_ter_len - 5, 5 ) == "_west" ) {
base_om_ter_id = om_terrain_id.substr( 0, om_ter_len - 5 );
} else if( om_terrain_id.substr( om_ter_len - 5, 5 ) == "_east" ) {
base_om_ter_id = om_terrain_id.substr( 0, om_ter_len - 5 );
}
}
std::string base_om_ter_id = oter_no_dir( oter_id( om_terrain_id ) );

for( const auto &recp : group.recipes ) {
const auto &recp_terrain = group.om_terrains.find( recp.first );
if( recp_terrain == group.om_terrains.end() ) {
continue;
}
std::string base_om_ter_id = om_terrain_id;
size_t om_ter_len = om_terrain_id.size();
if( om_ter_len > 7 ) {
if( om_terrain_id.substr( om_ter_len - 6, 6 ) == "_south" ) {
base_om_ter_id = om_terrain_id.substr( 0, om_ter_len - 6 );
} else if( om_terrain_id.substr( om_ter_len - 6, 6 ) == "_north" ) {
base_om_ter_id = om_terrain_id.substr( 0, om_ter_len - 6 );
} else if( om_terrain_id.substr( om_ter_len - 5, 5 ) == "_west" ) {
base_om_ter_id = om_terrain_id.substr( 0, om_ter_len - 5 );
} else if( om_terrain_id.substr( om_ter_len - 5, 5 ) == "_east" ) {
base_om_ter_id = om_terrain_id.substr( 0, om_ter_len - 5 );
}
}

if( recp_terrain->second.find( base_om_ter_id ) != recp_terrain->second.end() ) {
all_rec.emplace( recp );
}
Expand Down

0 comments on commit a778635

Please sign in to comment.