forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactive_item_cache.h
More file actions
101 lines (87 loc) · 3.38 KB
/
active_item_cache.h
File metadata and controls
101 lines (87 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#pragma once
#ifndef CATA_SRC_ACTIVE_ITEM_CACHE_H
#define CATA_SRC_ACTIVE_ITEM_CACHE_H
#include <cstddef>
#include <list>
#include <unordered_map>
#include <vector>
#include "coordinates.h"
#include "item_pocket.h"
#include "point.h"
#include "safe_reference.h"
class item;
// A struct used to uniquely identify an item within a submap or vehicle.
struct item_reference {
point_rel_ms location;
safe_reference<item> item_ref;
// parent invalidating would also invalidate item_ref so it's safe to use a raw pointers here
item *parent = nullptr;
std::vector<item_pocket const *> pocket_chain;
float spoil_multiplier() const;
bool has_watertight_container() const;
};
enum class special_item_type : int {
none,
corpse,
explosive
};
template<typename T>
constexpr bool operator==( safe_reference<T> const &lhs, safe_reference<T> const &rhs )
{
// To ensure they hasn't expired
return !!lhs && !!rhs && lhs.get() == rhs.get();
}
namespace std
{
template <>
struct hash<special_item_type> {
std::size_t operator()( const special_item_type &k ) const noexcept {
return static_cast<size_t>( k );
}
};
} // namespace std
class active_item_cache
{
private:
std::unordered_map<int, std::list<item_reference>> active_items;
std::unordered_map<special_item_type, std::list<item_reference>> special_items;
std::unordered_map<int, std::unordered_map<item *, safe_reference<item>>> active_items_index;
public:
/**
* Adds the reference to the cache. Does nothing if the reference is already in the cache.
* Relies on the fact that item::processing_speed() is a constant.
* These two operations are really the same, but tailored to their usages.
* The submap coordinates are for submaps, and the relative ones are for vehicles.
*/
bool add( item &it, point_sm_ms location, item *parent = nullptr,
std::vector<item_pocket const *> const &pocket_chain = {} );
bool add( item &it, point_rel_ms location, item *parent = nullptr,
std::vector<item_pocket const *> const &pocket_chain = {} );
/**
* Returns true if the cache is empty
*/
bool empty() const;
/**
* Returns a vector of all cached active item references.
* Broken references are removed from the cache.
*/
std::vector<item_reference> get();
/**
* Returns the first size() / processing_speed() elements of each list, rounded up.
* Items returned are rotated to the back of their respective lists, otherwise only the
* first n items will ever be processed.
* Broken references encountered when collecting the items to be processed are removed from
* the cache.
* Relies on the fact that item::processing_speed() is a constant.
*/
std::vector<item_reference> get_for_processing();
/**
* Returns the currently tracked list of special active items.
*/
std::vector<item_reference> get_special( special_item_type type );
/** Subtract delta from every item_reference's location */
void subtract_locations( const point_rel_ms &delta );
void rotate_locations( int turns, const point_rel_ms &dim );
void mirror( const point_rel_ms &dim, bool horizontally );
};
#endif // CATA_SRC_ACTIVE_ITEM_CACHE_H