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

Less duplicated gates #16441

Merged
merged 2 commits into from
Apr 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 12 additions & 33 deletions data/json/gates.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,17 @@
{
"type" : "gate",
"handle" : "t_gates_mech_control",
"wall" : "t_wall",
"door" : "t_door_metal_locked",
"floor" : "t_floor",
"messages" : {
"pull" : "You turn the handle...",
"open" : "The gate is opened!",
"close" : "The gate is closed!",
"fail" : "The gate can't be closed!"
},
"moves" : 1800,
"bashing_damage" : 40
},
{
"type" : "gate",
"handle" : "t_gates_control_concrete",
"wall" : "t_concrete_wall",
"door" : "t_door_metal_locked",
"floor" : "t_floor",
"messages" : {
"pull" : "You turn the handle...",
"open" : "The gate is opened!",
"close" : "The gate is closed!",
"fail" : "The gate can't be closed!"
},
"moves" : 1800,
"bashing_damage" : 40
},
{
"type" : "gate",
"handle" : "t_gates_control_brick",
"wall" : "t_brick_wall",
"other_handles" : [
"t_gates_control_concrete",
"t_gates_control_brick"
],
"door" : "t_door_metal_locked",
"floor" : "t_floor",
"walls" : [
"t_wall",
"t_concrete_wall",
"t_brick_wall"
],
"messages" : {
"pull" : "You turn the handle...",
"open" : "The gate is opened!",
Expand All @@ -50,6 +28,7 @@
"wall" : "t_wall_wood",
"door" : "t_door_metal_locked",
"floor" : "t_dirtfloor",
"walls" : "t_wall_wood",
"messages" : {
"pull" : "You pull the rope...",
"open" : "The barn doors opened!",
Expand All @@ -62,9 +41,9 @@
{
"type" : "gate",
"handle" : "t_palisade_pulley",
"wall" : "t_palisade",
"door" : "t_palisade_gate",
"floor" : "t_palisade_gate_o",
"walls" : "t_palisade",
"messages" : {
"pull" : "You pull the rope...",
"open" : "The palisade gate swings open!",
Expand All @@ -77,9 +56,9 @@
{
"type" : "gate",
"handle" : "t_gates_control_metal",
"wall" : "t_wall_metal",
"door" : "t_door_metal_locked",
"floor" : "t_metal_floor",
"walls" : "t_wall_metal",
"messages" : {
"pull" : "You throw the lever...",
"open" : "The door rises!",
Expand Down
21 changes: 14 additions & 7 deletions src/gates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@ using gate_id = string_id<gate_data>;
struct gate_data {

gate_data() :
wall(),
door(),
floor(),
moves( 0 ),
bash_dmg( 0 ),
was_loaded( false ) {};

gate_id id;

ter_str_id wall;
ter_str_id door;
ter_str_id floor;
std::vector<ter_str_id> walls;

std::string pull_message;
std::string open_message;
Expand All @@ -40,22 +37,23 @@ struct gate_data {
bool was_loaded;

void load( JsonObject &jo );
bool is_suitable_wall( const tripoint &pos ) const;
};

gate_id get_gate_id( const tripoint &pos )
{
return gate_id( g->m.get_ter( pos ).str() );
}

generic_factory<gate_data> gates_data( "gate type", "handle" );
generic_factory<gate_data> gates_data( "gate type", "handle", "other_handles" );

}

void gate_data::load( JsonObject &jo )
{
mandatory( jo, was_loaded, "wall", wall );
mandatory( jo, was_loaded, "door", door );
mandatory( jo, was_loaded, "floor", floor );
mandatory( jo, was_loaded, "walls", walls, string_id_reader<ter_t> {} );

if( !was_loaded || jo.has_member( "messages" ) ) {
JsonObject messages_obj = jo.get_object( "messages" );
Expand All @@ -70,6 +68,15 @@ void gate_data::load( JsonObject &jo )
optional( jo, was_loaded, "bashing_damage", bash_dmg, 0 );
}

bool gate_data::is_suitable_wall( const tripoint &pos ) const
{
const auto wid = g->m.ter( pos );
const auto iter = std::find_if( walls.begin(), walls.end(), [ wid ]( const ter_str_id & wall ) {
return wall.id() == wid;
} );
return iter != walls.end();
}

void gates::load_gates( JsonObject &jo )
{
gates_data.load( jo );
Expand Down Expand Up @@ -110,7 +117,7 @@ void gates::open_gate( const tripoint &pos )
static const tripoint dir[4] = { { 1, 0, 0 }, { 0, 1, 0 }, { -1, 0, 0 }, { 0, -1, 0 } };
const tripoint wall_pos = pos + dir[i];

if( g->m.ter( wall_pos ) != gate.wall.id() ) {
if( !gate.is_suitable_wall( wall_pos ) ) {
continue;
}

Expand Down