forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item_location.h
101 lines (77 loc) · 3.14 KB
/
item_location.h
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_ITEM_LOCATION_H
#define CATA_SRC_ITEM_LOCATION_H
#include <memory>
#include <string>
struct tripoint;
class item;
class Character;
class map_cursor;
class vehicle_cursor;
class JsonIn;
class JsonOut;
/**
* A lightweight handle to an item independent of it's location
* Unlike a raw pointer can be (de-)serialized to/from JSON
* Provides a generic interface of querying, obtaining and removing an item
* Is invalidated by many operations (including copying of the item)
*/
class item_location
{
public:
enum class type : int {
invalid = 0,
character = 1,
map = 2,
vehicle = 3,
container = 4
};
item_location();
static const item_location nowhere;
item_location( Character &ch, item *which );
item_location( const map_cursor &mc, item *which );
item_location( const vehicle_cursor &vc, item *which );
item_location( const item_location &container, item *which );
void serialize( JsonOut &js ) const;
void deserialize( JsonIn &js );
bool operator==( const item_location &rhs ) const;
bool operator!=( const item_location &rhs ) const;
explicit operator bool() const;
item &operator*();
const item &operator*() const;
item *operator->();
const item *operator->() const;
/** Returns the type of location where the item is found */
type where() const;
/** Returns the position where the item is found */
tripoint position() const;
/** Describes the item location
* @param ch if set description is relative to character location */
std::string describe( const Character *ch = nullptr ) const;
/** Move an item from the location to the character inventory
* @param ch Character who's inventory gets the item
* @param qty if specified limits maximum obtained charges
* @warning caller should restack inventory if item is to remain in it
* @warning all further operations using this class are invalid
* @warning it is unsafe to call this within unsequenced operations (see #15542)
* @return item_location for the item */
item_location obtain( Character &ch, int qty = -1 );
/** Calculate (but do not deduct) number of moves required to obtain an item
* @see item_location::obtain */
int obtain_cost( const Character &ch, int qty = -1 ) const;
/** Removes the selected item from the game
* @warning all further operations using this class are invalid */
void remove_item();
/** Gets the selected item or nullptr */
item *get_item();
const item *get_item() const;
void set_should_stack( bool should_stack ) const;
/** returns the parent item, or an invalid location if it has no parent */
item_location parent_item() const;
// This is a dirty hack, don't use. TODO: Make not necessary, then delete
void make_dirty();
private:
class impl;
std::shared_ptr<impl> ptr;
};
#endif // CATA_SRC_ITEM_LOCATION_H