Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/StarItem.hpp
blob: 1fa7b452c32c1314790a0c18a7d37d07a672476c (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#pragma once

#include "StarSet.hpp"
#include "StarDrawable.hpp"
#include "StarItemDescriptor.hpp"
#include "StarQuests.hpp"

namespace Star {

STAR_CLASS(Item);
STAR_CLASS(GenericItem);

STAR_EXCEPTION(ItemException, StarException);

class Item {
public:
  // Config here is the configuration loaded directly from assets, directory is
  // the asset path this config was found in, that other assets should be
  // loaded relative to.
  Item(Json config, String directory, Json parameters = JsonObject());

  // For items which do not come from files
  Item();

  virtual ~Item();

  virtual ItemPtr clone() const = 0;

  // Unique identifying item name
  String name() const;

  // Number of this item that is available.
  uint64_t count() const;
  // Sets the new item count, up to a max of the maximum stack size.  If this
  // value is over stack size, returns the overflow.  If 'overfill' is set to
  // true, then will fill past max stack level.
  uint64_t setCount(uint64_t count, bool overfill = false);

  // Is this item type stackable with the given item type at all?  Base class
  // implementation compares name(), and m_parameters fields and returns true
  // if they are both the same, similarly to matches.
  virtual bool stackableWith(ItemConstPtr const& item) const;
  uint64_t maxStack() const;

  // Return how many of the given item could be shifted into this item, taking
  // into acount whether the item is stackable at all, as well as maxStack and
  // the count available.
  uint64_t couldStack(ItemConstPtr const& item) const;

  // If the given item is stackable with this one, takes as many from the given
  // item as possible and shifts it into this item's count.  Returns true if
  // any items at all were shifted.
  bool stackWith(ItemPtr const& item);

  // Does this item match the given item or itemDescriptor
  bool matches(ItemDescriptor const& descriptor, bool exactMatch = false) const;
  bool matches(ItemConstPtr const& other, bool exactMatch = false) const;

  // List of itemdescriptors for which the current item could be used in the
  // place of
  // in recipes and the like.
  List<ItemDescriptor> matchingDescriptors() const;

  // If the given number of this item is available, consumes that number and
  // returns true, otherwise returns false.
  bool consume(uint64_t count);

  // Take as many of this item as possible up to the given max (default is all)
  // and return the new set.  Implementation uses clone() method.
  ItemPtr take(uint64_t max = NPos);

  // count() is 0
  bool empty() const;

  // Builds a descriptor out of name(), count(), and m_parameters
  ItemDescriptor descriptor() const;

  String description() const;
  String friendlyName() const;

  Rarity rarity() const;
  uint64_t price() const;

  virtual List<Drawable> iconDrawables() const;
  virtual List<Drawable> dropDrawables() const;
  String largeImage() const;

  String tooltipKind() const;
  virtual String category() const;

  virtual String pickupSound() const;

  bool twoHanded() const;
  float timeToLive() const;

  List<ItemDescriptor> learnBlueprintsOnPickup() const;
  StringMap<String> collectablesOnPickup() const;

  List<QuestArcDescriptor> pickupQuestTemplates() const;
  StringSet itemTags() const;
  bool hasItemTag(String const& itemTag) const;

  // Return either a parameter given to the item or a config value, if no such
  // parameter exists.
  Json instanceValue(String const& name, Json const& def = Json()) const;

  // Returns the full set of configuration values merged with parameters
  Json instanceValues() const;

  // Returns just the base config
  Json config() const;

  // Returns just the dynamic parameters
  Json parameters() const;

  static bool itemsEqual(ItemConstPtr const& a, ItemConstPtr const& b);

protected:
  void setMaxStack(uint64_t maxStack);
  void setDescription(String const& description);
  void setRarity(Rarity rarity);
  void setPrice(uint64_t price);
  // icon drawables are pixels, not tile, based
  void setIconDrawables(List<Drawable> drawables);
  void setTwoHanded(bool twoHanded);
  void setTimeToLive(float timeToLive);

  void setInstanceValue(String const& name, Json const& val);

  String const& directory() const;

private:
  Json m_config;
  String m_directory;

  String m_name;
  uint64_t m_count;
  Json m_parameters;

  uint64_t m_maxStack;
  String m_shortDescription;
  String m_description;
  Rarity m_rarity;
  List<Drawable> m_iconDrawables;
  bool m_twoHanded;
  float m_timeToLive;
  uint64_t m_price;
  String m_tooltipKind;
  String m_largeImage;
  String m_category;
  StringSet m_pickupSounds;

  List<ItemDescriptor> m_matchingDescriptors;
  List<ItemDescriptor> m_learnBlueprintsOnPickup;
  StringMap<String> m_collectablesOnPickup;
};

class GenericItem : public Item {
public:
  GenericItem(Json const& config, String const& directory, Json const& parameters);
  virtual ItemPtr clone() const;
};

inline uint64_t itemSafeCount(ItemPtr const& item) {
  return item ? item->count() : 0;
}

inline bool itemSafeTwoHanded(ItemPtr const& item) {
  return item && item->twoHanded();
}

inline bool itemSafeOneHanded(ItemPtr const& item) {
  return item && !item->twoHanded();
}

inline ItemDescriptor itemSafeDescriptor(ItemPtr const& item) {
  return item ? item->descriptor() : ItemDescriptor();
}
}