-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mqrause
committed
Mar 11, 2024
1 parent
7d3234e
commit de4d48d
Showing
7 changed files
with
1,670 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
#include "map_entity_stack.h" | ||
|
||
#include "creature.h" | ||
#include "game.h" | ||
#include "item.h" | ||
#include "item_category.h" | ||
#include "item_search.h" | ||
#include "mapdata.h" | ||
|
||
static const trait_id trait_INATTENTIVE( "INATTENTIVE" ); | ||
|
||
template<typename T> | ||
const T *map_entity_stack<T>::get_selected_entity() const | ||
{ | ||
if( !entities.empty() ) { | ||
// todo: check index | ||
return entities[selected_index].entity; | ||
} | ||
return nullptr; | ||
} | ||
|
||
template<typename T> | ||
std::optional<tripoint_rel_ms> map_entity_stack<T>::get_selected_pos() const | ||
{ | ||
if( !entities.empty() ) { | ||
// todo: check index | ||
return entities[selected_index].pos; | ||
} | ||
return std::nullopt; | ||
} | ||
|
||
template<typename T> | ||
int map_entity_stack<T>::get_selected_count() const | ||
{ | ||
if( !entities.empty() ) { | ||
// todo: check index | ||
return entities[selected_index].count; | ||
} | ||
return 0; | ||
} | ||
|
||
template<typename T> | ||
void map_entity_stack<T>::select_next() | ||
{ | ||
if( entities.empty() ) { | ||
return; | ||
} | ||
|
||
selected_index++; | ||
|
||
if( selected_index >= static_cast<int>( entities.size() ) ) { | ||
selected_index = 0; | ||
} | ||
} | ||
|
||
template<typename T> | ||
void map_entity_stack<T>::select_prev() | ||
{ | ||
if( entities.empty() ) { | ||
return; | ||
} | ||
|
||
if( selected_index <= 0 ) { | ||
selected_index = entities.size(); | ||
} | ||
|
||
selected_index--; | ||
} | ||
|
||
template<typename T> | ||
int map_entity_stack<T>::get_selected_index() const | ||
{ | ||
return selected_index; | ||
} | ||
|
||
template<typename T> | ||
map_entity_stack<T>::map_entity_stack() : totalcount( 0 ) | ||
{ | ||
entities.emplace_back(); | ||
} | ||
|
||
template<typename T> | ||
map_entity_stack<T>::map_entity_stack( const T *const entity, const tripoint_rel_ms &pos, | ||
const int count ) : totalcount( count ) | ||
{ | ||
entities.emplace_back( pos, 1, entity ); | ||
} | ||
|
||
template<typename T> | ||
void map_entity_stack<T>::add_at_pos( const T *const entity, const tripoint_rel_ms &pos, | ||
const int count ) | ||
{ | ||
if( entities.empty() || entities.back().pos != pos ) { | ||
entities.emplace_back( pos, 1, entity ); | ||
} else { | ||
entities.back().count++; | ||
} | ||
|
||
totalcount += count; | ||
} | ||
|
||
template<typename T> | ||
const std::string map_entity_stack<T>::get_category() const | ||
Check failure on line 103 in src/map_entity_stack.cpp GitHub Actions / build (src)
|
||
{ | ||
return std::string(); | ||
} | ||
|
||
template<> | ||
const std::string map_entity_stack<item>::get_category() const | ||
Check failure on line 109 in src/map_entity_stack.cpp GitHub Actions / build (src)
|
||
{ | ||
const item *it = get_selected_entity(); | ||
if( it ) { | ||
return it->get_category_of_contents().name_header(); | ||
} | ||
|
||
return std::string(); | ||
} | ||
|
||
template<> | ||
const std::string map_entity_stack<Creature>::get_category() const | ||
Check failure on line 120 in src/map_entity_stack.cpp GitHub Actions / build (src)
|
||
{ | ||
const Creature *mon = get_selected_entity(); | ||
if( mon ) { | ||
const Character &you = get_player_character(); | ||
if( you.has_trait( trait_INATTENTIVE ) ) { | ||
return _( "Unknown" ); | ||
} | ||
return Creature::get_attitude_ui_data( mon->attitude_to( you ) ).first.translated(); | ||
} | ||
|
||
return std::string(); | ||
} | ||
|
||
template<> | ||
const std::string map_entity_stack<map_data_common_t>::get_category() const | ||
{ | ||
const map_data_common_t *tf = get_selected_entity(); | ||
if( tf ) { | ||
if( tf->is_terrain() ) { | ||
return "TERRAIN"; | ||
} else { | ||
return "FURNITURE"; | ||
} | ||
} | ||
|
||
return std::string(); | ||
} | ||
|
||
template<typename T> | ||
bool map_entity_stack<T>::compare( const map_entity_stack<T> &, bool ) const | ||
{ | ||
return false; | ||
} | ||
|
||
template<> | ||
bool map_entity_stack<item>::compare( const map_entity_stack<item> &rhs, bool use_category ) const | ||
{ | ||
bool compare_dist = rl_dist( tripoint_rel_ms(), entities[0].pos ) < | ||
rl_dist( tripoint_rel_ms(), rhs.entities[0].pos ); | ||
|
||
if( !use_category ) { | ||
return compare_dist; | ||
} | ||
|
||
const item_category &lhs_cat = get_selected_entity()->get_category_of_contents(); | ||
const item_category &rhs_cat = rhs.get_selected_entity()->get_category_of_contents(); | ||
|
||
if( lhs_cat == rhs_cat ) { | ||
return compare_dist; | ||
} | ||
|
||
return lhs_cat < rhs_cat; | ||
} | ||
|
||
template<> | ||
bool map_entity_stack<Creature>::compare( const map_entity_stack<Creature> &rhs, | ||
bool use_category ) const | ||
{ | ||
bool compare_dist = rl_dist( tripoint_rel_ms(), entities[0].pos ) < | ||
rl_dist( tripoint_rel_ms(), rhs.entities[0].pos ); | ||
|
||
if( !use_category ) { | ||
return compare_dist; | ||
} | ||
|
||
// maybe pass you as a parameter to avoid including game.h? | ||
Character &you = get_player_character(); | ||
const Creature::Attitude att_lhs = get_selected_entity()->attitude_to( you ); | ||
const Creature::Attitude att_rhs = rhs.get_selected_entity()->attitude_to( you ); | ||
|
||
if( att_lhs == att_rhs ) { | ||
return compare_dist; | ||
} | ||
|
||
return att_lhs < att_rhs; | ||
} | ||
|
||
template<> | ||
bool map_entity_stack<map_data_common_t>::compare( const map_entity_stack<map_data_common_t> &rhs, | ||
bool use_category ) const | ||
{ | ||
bool compare_dist = rl_dist( tripoint_rel_ms(), entities[0].pos ) < | ||
rl_dist( tripoint_rel_ms(), rhs.entities[0].pos ); | ||
|
||
if( !use_category ) { | ||
return compare_dist; | ||
} | ||
|
||
const bool &lhs_cat = get_selected_entity()->is_terrain(); | ||
const bool &rhs_cat = rhs.get_selected_entity()->is_terrain(); | ||
|
||
if( lhs_cat == rhs_cat ) { | ||
return compare_dist; | ||
} | ||
|
||
return rhs_cat; | ||
} | ||
|
||
//returns the first non priority items. | ||
template<typename T> | ||
int list_filter_high_priority( std::vector<map_entity_stack<T>> &, const std::string & ) | ||
{ | ||
} | ||
|
||
template<typename T> | ||
int list_filter_low_priority( std::vector<map_entity_stack<T>> &stack, const int start, | ||
const std::string &priorities ) | ||
{ | ||
// todo: actually use it and specialization (only used for items) | ||
// TODO:optimize if necessary | ||
std::vector<map_entity_stack<T>> tempstack; | ||
const auto filter_fn = item_filter_from_string( priorities ); | ||
for( auto it = stack.begin() + start; it != stack.end(); ) { | ||
if( !priorities.empty() && it->example != nullptr && filter_fn( *it->example ) ) { | ||
tempstack.push_back( *it ); | ||
it = stack.erase( it ); | ||
} else { | ||
it++; | ||
} | ||
} | ||
|
||
int id = stack.size(); | ||
for( map_item_stack &elem : tempstack ) { | ||
stack.push_back( elem ); | ||
} | ||
return id; | ||
} | ||
|
||
// explicit template instantiation | ||
template class map_entity_stack<item>; | ||
template class map_entity_stack<Creature>; | ||
template class map_entity_stack<map_data_common_t>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#pragma once | ||
#ifndef CATA_SRC_MAP_ENTITY_STACK_H | ||
#define CATA_SRC_MAP_ENTITY_STACK_H | ||
|
||
#include "coordinates.h" | ||
#include "point.h" | ||
|
||
template<typename T> | ||
class map_entity_stack | ||
{ | ||
private: | ||
class entity_group | ||
{ | ||
public: | ||
tripoint_rel_ms pos; | ||
int count; | ||
const T *entity; | ||
|
||
//only expected to be used for things like lists and vectors | ||
entity_group() : count( 0 ), entity( nullptr ) {}; | ||
entity_group( const tripoint_rel_ms &p, int arg_count, const T *entity ) : | ||
pos( p ), count( arg_count ), entity( entity ) {}; | ||
}; | ||
|
||
int selected_index = 0; | ||
public: | ||
std::vector<entity_group> entities; | ||
int totalcount; | ||
|
||
const T *get_selected_entity() const; | ||
std::optional<tripoint_rel_ms> get_selected_pos() const; | ||
int get_selected_count() const; | ||
int get_selected_index() const; | ||
|
||
void select_next(); | ||
void select_prev(); | ||
|
||
//only expected to be used for things like lists and vectors | ||
map_entity_stack(); | ||
map_entity_stack( const T *entity, const tripoint_rel_ms &pos, int count = 1 ); | ||
|
||
// This adds to an existing entity group if the last current | ||
// entity group is the same position and otherwise creates and | ||
// adds to a new entity group. Note that it does not search | ||
// through all older entity groups for a match. | ||
void add_at_pos( const T *entity, const tripoint_rel_ms &pos, int count = 1 ); | ||
|
||
bool compare( const map_entity_stack<T> &rhs, bool use_category = false ) const; | ||
const std::string get_category() const; | ||
}; | ||
|
||
template<typename T> | ||
int list_filter_high_priority( std::vector<map_entity_stack<T>> &stack, | ||
const std::string &priorities ); | ||
template<typename T> | ||
int list_filter_low_priority( std::vector<map_entity_stack<T>> &stack, int start, | ||
const std::string &priorities ); | ||
|
||
#endif // CATA_SRC_MAP_ENTITY_STACK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.