MCPcopy Create free account
hub / github.com/EasyRPG/Player / AddItem

Method AddItem

src/game_party.cpp:180–217  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

178}
179
180void Game_Party::AddItem(int item_id, int amount) {
181 if (item_id < 1 || item_id > (int) lcf::Data::items.size()) {
182 Output::Debug("Can't add item to party. {} is not a valid item ID.", item_id);
183 return;
184 }
185
186 int item_limit = GetMaxItemCount(item_id);
187
188 auto ip = GetItemIndex(item_id);
189 auto idx = ip.first;
190 auto has = ip.second;
191 if (!has) {
192 if (amount > 0) {
193 amount = std::min(amount, item_limit);
194 data.item_ids.insert(data.item_ids.begin() + idx, (int16_t)item_id);
195 data.item_counts.insert(data.item_counts.begin() + idx, (uint8_t)amount);
196 data.item_usage.insert(data.item_usage.begin() + idx, 0);
197 }
198 return;
199 }
200
201 int total_items = data.item_counts[idx] + amount;
202
203 if (total_items <= 0) {
204 data.item_ids.erase(data.item_ids.begin() + idx);
205 data.item_counts.erase(data.item_counts.begin() + idx);
206 data.item_usage.erase(data.item_usage.begin() + idx);
207 return;
208 }
209
210 data.item_counts[idx] = (uint8_t)std::min(total_items, item_limit);
211 // If the item was removed, the number of uses resets.
212 // (Adding an item never changes the number of uses, even when
213 // you already have x99 of them.)
214 if (amount < 0) {
215 data.item_usage[idx] = 0;
216 }
217}
218
219void Game_Party::RemoveItem(int item_id, int amount) {
220 AddItem(item_id, -amount);

Callers 7

ChangeEquipmentMethod · 0.80
CommandChangeItemsMethod · 0.80
DoItemMethod · 0.80
UpdateNumberInputMethod · 0.80

Calls 3

minFunction · 0.85
sizeMethod · 0.45
beginMethod · 0.45

Tested by

no test coverage detected