Skip to content

Commit

Permalink
jsonize filthy clothing zone
Browse files Browse the repository at this point in the history
  • Loading branch information
KorGgenT committed Dec 12, 2019
1 parent 41ec668 commit 2afeb6f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
6 changes: 4 additions & 2 deletions data/json/item_category.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"id": "clothing",
"type": "ITEM_CATEGORY",
"name": "CLOTHING",
"//": "zone is hardcoded due to filthy clothing",
"priority_zones": [ { "id": "LOOT_FCLOTHING", "filthy": true } ],
"zone": "LOOT_CLOTHING",
"sort_rank": -18
},
{
Expand Down Expand Up @@ -149,7 +150,8 @@
"id": "armor",
"type": "ITEM_CATEGORY",
"name": "ARMOR",
"//": "zone is hardcoded due to filthy clothing",
"priority_zones": [ { "id": "LOOT_FARMOR", "filthy": true } ],
"zone": "LOOT_ARMOR",
"sort_rank": 20
},
{
Expand Down
17 changes: 5 additions & 12 deletions src/clzones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,11 @@ zone_type_id zone_manager::get_near_zone_type_for_item( const item &it,
}
}

cata::optional<zone_type_id> zone_check_first = cat.priority_zone( it );
if( zone_check_first && has_near( *zone_check_first, where, range ) ) {
return *zone_check_first;
}

if( cat.zone() ) {
return *cat.zone();
}
Expand All @@ -819,18 +824,6 @@ zone_type_id zone_manager::get_near_zone_type_for_item( const item &it,

return zone_type_id( "LOOT_FOOD" );
}
if( cat.get_id() == "clothing" ) {
if( it.is_filthy() && has_near( zone_type_id( "LOOT_FCLOTHING" ), where, range ) ) {
return zone_type_id( "LOOT_FCLOTHING" );
}
return zone_type_id( "LOOT_CLOTHING" );
}
if( cat.get_id() == "armor" ) {
if( it.is_filthy() && has_near( zone_type_id( "LOOT_FARMOR" ), where, range ) ) {
return zone_type_id( "LOOT_FARMOR" );
}
return zone_type_id( "LOOT_ARMOR" );
}

return zone_type_id();
}
Expand Down
35 changes: 35 additions & 0 deletions src/item_category.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ bool string_id<item_category>::is_valid() const
return item_category_factory.is_valid( *this );
}

void zone_priority_data::deserialize( JsonIn &jsin )
{
JsonObject data = jsin.get_object();
load( data );
}

void zone_priority_data::load( JsonObject &jo )
{
mandatory( jo, was_loaded, "id", id );
optional( jo, was_loaded, "flags", flags );
optional( jo, was_loaded, "filthy", filthy, false );
}


void item_category::load_item_cat( const JsonObject &jo, const std::string &src )
{
item_category_factory.load( jo, src );
Expand All @@ -29,6 +43,7 @@ void item_category::load( const JsonObject &jo, const std::string & )
mandatory( jo, was_loaded, "id", id );
mandatory( jo, was_loaded, "name", name_ );
mandatory( jo, was_loaded, "sort_rank", sort_rank_ );
optional( jo, was_loaded, "priority_zones", zone_priority_ );
optional( jo, was_loaded, "zone", zone_, cata::nullopt );
}

Expand Down Expand Up @@ -68,6 +83,26 @@ cata::optional<zone_type_id> item_category::zone() const
return zone_;
}

cata::optional<zone_type_id> item_category::priority_zone( const item &it ) const
{
for( const zone_priority_data &zone_dat : zone_priority_ ) {
if( zone_dat.filthy ) {
if( it.is_filthy() ) {
return zone_dat.id;

} else {
continue;
}
}
for( const std::string &flag : zone_dat.flags ) {
if( it.has_flag( flag ) ) {
return zone_dat.id;
}
}
}
return cata::nullopt;
}

int item_category::sort_rank() const
{
return sort_rank_;
Expand Down
14 changes: 14 additions & 0 deletions src/item_category.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@

#include <string>

#include "flat_set.h"
#include "optional.h"
#include "translations.h"
#include "type_id.h"

class item;
class JsonObject;

// this is a helper struct with rules for picking a zone
struct zone_priority_data {
bool was_loaded;
zone_type_id id;
bool filthy = false;
cata::flat_set<std::string> flags;

void deserialize( JsonIn &jsin );
void load( JsonObject &jo );
};
/**
* Contains metadata for one category of items
*
Expand All @@ -26,6 +38,7 @@ class item_category
int sort_rank_ = 0;

cata::optional<zone_type_id> zone_;
std::vector<zone_priority_data> zone_priority_;

public:
/** Unique ID of this category, used when loading from JSON. */
Expand All @@ -44,6 +57,7 @@ class item_category

std::string name() const;
item_category_id get_id() const;
cata::optional<zone_type_id> priority_zone( const item &it ) const;
cata::optional<zone_type_id> zone() const;
int sort_rank() const;

Expand Down

0 comments on commit 2afeb6f

Please sign in to comment.