inventory.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "inventory.h"
  17. #include "serialization.h"
  18. #include "debug.h"
  19. #include <sstream>
  20. #include "log.h"
  21. #include "itemdef.h"
  22. #include "util/strfnd.h"
  23. #include "content_mapnode.h" // For loading legacy MaterialItems
  24. #include "nameidmapping.h" // For loading legacy MaterialItems
  25. #include "util/serialize.h"
  26. #include "util/string.h"
  27. /*
  28. ItemStack
  29. */
  30. static content_t content_translate_from_19_to_internal(content_t c_from)
  31. {
  32. for (const auto &tt : trans_table_19) {
  33. if(tt[1] == c_from) {
  34. return tt[0];
  35. }
  36. }
  37. return c_from;
  38. }
  39. ItemStack::ItemStack(const std::string &name_, u16 count_,
  40. u16 wear_, IItemDefManager *itemdef) :
  41. name(itemdef->getAlias(name_)),
  42. count(count_),
  43. wear(wear_)
  44. {
  45. if (name.empty() || count == 0)
  46. clear();
  47. else if (itemdef->get(name).type == ITEM_TOOL)
  48. count = 1;
  49. }
  50. void ItemStack::serialize(std::ostream &os) const
  51. {
  52. if (empty())
  53. return;
  54. // Check how many parts of the itemstring are needed
  55. int parts = 1;
  56. if(count != 1)
  57. parts = 2;
  58. if(wear != 0)
  59. parts = 3;
  60. if (!metadata.empty())
  61. parts = 4;
  62. os<<serializeJsonStringIfNeeded(name);
  63. if(parts >= 2)
  64. os<<" "<<count;
  65. if(parts >= 3)
  66. os<<" "<<wear;
  67. if (parts >= 4) {
  68. os << " ";
  69. metadata.serialize(os);
  70. }
  71. }
  72. void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
  73. {
  74. clear();
  75. // Read name
  76. name = deSerializeJsonStringIfNeeded(is);
  77. // Skip space
  78. std::string tmp;
  79. std::getline(is, tmp, ' ');
  80. if(!tmp.empty())
  81. throw SerializationError("Unexpected text after item name");
  82. if(name == "MaterialItem")
  83. {
  84. // Obsoleted on 2011-07-30
  85. u16 material;
  86. is>>material;
  87. u16 materialcount;
  88. is>>materialcount;
  89. // Convert old materials
  90. if(material <= 0xff)
  91. material = content_translate_from_19_to_internal(material);
  92. if(material > 0xfff)
  93. throw SerializationError("Too large material number");
  94. // Convert old id to name
  95. NameIdMapping legacy_nimap;
  96. content_mapnode_get_name_id_mapping(&legacy_nimap);
  97. legacy_nimap.getName(material, name);
  98. if(name.empty())
  99. name = "unknown_block";
  100. if (itemdef)
  101. name = itemdef->getAlias(name);
  102. count = materialcount;
  103. }
  104. else if(name == "MaterialItem2")
  105. {
  106. // Obsoleted on 2011-11-16
  107. u16 material;
  108. is>>material;
  109. u16 materialcount;
  110. is>>materialcount;
  111. if(material > 0xfff)
  112. throw SerializationError("Too large material number");
  113. // Convert old id to name
  114. NameIdMapping legacy_nimap;
  115. content_mapnode_get_name_id_mapping(&legacy_nimap);
  116. legacy_nimap.getName(material, name);
  117. if(name.empty())
  118. name = "unknown_block";
  119. if (itemdef)
  120. name = itemdef->getAlias(name);
  121. count = materialcount;
  122. }
  123. else if(name == "node" || name == "NodeItem" || name == "MaterialItem3"
  124. || name == "craft" || name == "CraftItem")
  125. {
  126. // Obsoleted on 2012-01-07
  127. std::string all;
  128. std::getline(is, all, '\n');
  129. // First attempt to read inside ""
  130. Strfnd fnd(all);
  131. fnd.next("\"");
  132. // If didn't skip to end, we have ""s
  133. if(!fnd.at_end()){
  134. name = fnd.next("\"");
  135. } else { // No luck, just read a word then
  136. fnd.start(all);
  137. name = fnd.next(" ");
  138. }
  139. fnd.skip_over(" ");
  140. if (itemdef)
  141. name = itemdef->getAlias(name);
  142. count = stoi(trim(fnd.next("")));
  143. if(count == 0)
  144. count = 1;
  145. }
  146. else if(name == "MBOItem")
  147. {
  148. // Obsoleted on 2011-10-14
  149. throw SerializationError("MBOItem not supported anymore");
  150. }
  151. else if(name == "tool" || name == "ToolItem")
  152. {
  153. // Obsoleted on 2012-01-07
  154. std::string all;
  155. std::getline(is, all, '\n');
  156. // First attempt to read inside ""
  157. Strfnd fnd(all);
  158. fnd.next("\"");
  159. // If didn't skip to end, we have ""s
  160. if(!fnd.at_end()){
  161. name = fnd.next("\"");
  162. } else { // No luck, just read a word then
  163. fnd.start(all);
  164. name = fnd.next(" ");
  165. }
  166. count = 1;
  167. // Then read wear
  168. fnd.skip_over(" ");
  169. if (itemdef)
  170. name = itemdef->getAlias(name);
  171. wear = stoi(trim(fnd.next("")));
  172. }
  173. else
  174. {
  175. do // This loop is just to allow "break;"
  176. {
  177. // The real thing
  178. // Apply item aliases
  179. if (itemdef)
  180. name = itemdef->getAlias(name);
  181. // Read the count
  182. std::string count_str;
  183. std::getline(is, count_str, ' ');
  184. if (count_str.empty()) {
  185. count = 1;
  186. break;
  187. }
  188. count = stoi(count_str);
  189. // Read the wear
  190. std::string wear_str;
  191. std::getline(is, wear_str, ' ');
  192. if(wear_str.empty())
  193. break;
  194. wear = stoi(wear_str);
  195. // Read metadata
  196. metadata.deSerialize(is);
  197. // In case fields are added after metadata, skip space here:
  198. //std::getline(is, tmp, ' ');
  199. //if(!tmp.empty())
  200. // throw SerializationError("Unexpected text after metadata");
  201. } while(false);
  202. }
  203. if (name.empty() || count == 0)
  204. clear();
  205. else if (itemdef && itemdef->get(name).type == ITEM_TOOL)
  206. count = 1;
  207. }
  208. void ItemStack::deSerialize(const std::string &str, IItemDefManager *itemdef)
  209. {
  210. std::istringstream is(str, std::ios::binary);
  211. deSerialize(is, itemdef);
  212. }
  213. std::string ItemStack::getItemString() const
  214. {
  215. std::ostringstream os(std::ios::binary);
  216. serialize(os);
  217. return os.str();
  218. }
  219. ItemStack ItemStack::addItem(ItemStack newitem, IItemDefManager *itemdef)
  220. {
  221. // If the item is empty or the position invalid, bail out
  222. if(newitem.empty())
  223. {
  224. // nothing can be added trivially
  225. }
  226. // If this is an empty item, it's an easy job.
  227. else if(empty())
  228. {
  229. const u16 stackMax = newitem.getStackMax(itemdef);
  230. *this = newitem;
  231. // If the item fits fully, delete it
  232. if (count <= stackMax) {
  233. newitem.clear();
  234. } else { // Else the item does not fit fully. Return the rest.
  235. count = stackMax;
  236. newitem.remove(count);
  237. }
  238. }
  239. // If item name or metadata differs, bail out
  240. else if (name != newitem.name
  241. || metadata != newitem.metadata)
  242. {
  243. // cannot be added
  244. }
  245. // If the item fits fully, add counter and delete it
  246. else if(newitem.count <= freeSpace(itemdef))
  247. {
  248. add(newitem.count);
  249. newitem.clear();
  250. }
  251. // Else the item does not fit fully. Add all that fits and return
  252. // the rest.
  253. else
  254. {
  255. u16 freespace = freeSpace(itemdef);
  256. add(freespace);
  257. newitem.remove(freespace);
  258. }
  259. return newitem;
  260. }
  261. bool ItemStack::itemFits(ItemStack newitem,
  262. ItemStack *restitem,
  263. IItemDefManager *itemdef) const
  264. {
  265. // If the item is empty or the position invalid, bail out
  266. if(newitem.empty())
  267. {
  268. // nothing can be added trivially
  269. }
  270. // If this is an empty item, it's an easy job.
  271. else if(empty())
  272. {
  273. const u16 stackMax = newitem.getStackMax(itemdef);
  274. // If the item fits fully, delete it
  275. if (newitem.count <= stackMax) {
  276. newitem.clear();
  277. } else { // Else the item does not fit fully. Return the rest.
  278. newitem.remove(stackMax);
  279. }
  280. }
  281. // If item name or metadata differs, bail out
  282. else if (name != newitem.name
  283. || metadata != newitem.metadata)
  284. {
  285. // cannot be added
  286. }
  287. // If the item fits fully, delete it
  288. else if(newitem.count <= freeSpace(itemdef))
  289. {
  290. newitem.clear();
  291. }
  292. // Else the item does not fit fully. Return the rest.
  293. else
  294. {
  295. u16 freespace = freeSpace(itemdef);
  296. newitem.remove(freespace);
  297. }
  298. if(restitem)
  299. *restitem = newitem;
  300. return newitem.empty();
  301. }
  302. ItemStack ItemStack::takeItem(u32 takecount)
  303. {
  304. if(takecount == 0 || count == 0)
  305. return ItemStack();
  306. ItemStack result = *this;
  307. if(takecount >= count)
  308. {
  309. // Take all
  310. clear();
  311. }
  312. else
  313. {
  314. // Take part
  315. remove(takecount);
  316. result.count = takecount;
  317. }
  318. return result;
  319. }
  320. ItemStack ItemStack::peekItem(u32 peekcount) const
  321. {
  322. if(peekcount == 0 || count == 0)
  323. return ItemStack();
  324. ItemStack result = *this;
  325. if(peekcount < count)
  326. result.count = peekcount;
  327. return result;
  328. }
  329. /*
  330. Inventory
  331. */
  332. InventoryList::InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef):
  333. m_name(name),
  334. m_size(size),
  335. m_itemdef(itemdef)
  336. {
  337. clearItems();
  338. }
  339. void InventoryList::clearItems()
  340. {
  341. m_items.clear();
  342. for (u32 i=0; i < m_size; i++) {
  343. m_items.emplace_back();
  344. }
  345. //setDirty(true);
  346. }
  347. void InventoryList::setSize(u32 newsize)
  348. {
  349. if(newsize != m_items.size())
  350. m_items.resize(newsize);
  351. m_size = newsize;
  352. }
  353. void InventoryList::setWidth(u32 newwidth)
  354. {
  355. m_width = newwidth;
  356. }
  357. void InventoryList::setName(const std::string &name)
  358. {
  359. m_name = name;
  360. }
  361. void InventoryList::serialize(std::ostream &os) const
  362. {
  363. //os.imbue(std::locale("C"));
  364. os<<"Width "<<m_width<<"\n";
  365. for (const auto &item : m_items) {
  366. if (item.empty()) {
  367. os<<"Empty";
  368. } else {
  369. os<<"Item ";
  370. item.serialize(os);
  371. }
  372. os<<"\n";
  373. }
  374. os<<"EndInventoryList\n";
  375. }
  376. void InventoryList::deSerialize(std::istream &is)
  377. {
  378. //is.imbue(std::locale("C"));
  379. clearItems();
  380. u32 item_i = 0;
  381. m_width = 0;
  382. for(;;)
  383. {
  384. std::string line;
  385. std::getline(is, line, '\n');
  386. std::istringstream iss(line);
  387. //iss.imbue(std::locale("C"));
  388. std::string name;
  389. std::getline(iss, name, ' ');
  390. if (name == "EndInventoryList") {
  391. break;
  392. }
  393. // This is a temporary backwards compatibility fix
  394. if (name == "end") {
  395. break;
  396. }
  397. if (name == "Width") {
  398. iss >> m_width;
  399. if (iss.fail())
  400. throw SerializationError("incorrect width property");
  401. }
  402. else if(name == "Item")
  403. {
  404. if(item_i > getSize() - 1)
  405. throw SerializationError("too many items");
  406. ItemStack item;
  407. item.deSerialize(iss, m_itemdef);
  408. m_items[item_i++] = item;
  409. }
  410. else if(name == "Empty")
  411. {
  412. if(item_i > getSize() - 1)
  413. throw SerializationError("too many items");
  414. m_items[item_i++].clear();
  415. }
  416. }
  417. }
  418. InventoryList::InventoryList(const InventoryList &other)
  419. {
  420. *this = other;
  421. }
  422. InventoryList & InventoryList::operator = (const InventoryList &other)
  423. {
  424. m_items = other.m_items;
  425. m_size = other.m_size;
  426. m_width = other.m_width;
  427. m_name = other.m_name;
  428. m_itemdef = other.m_itemdef;
  429. //setDirty(true);
  430. return *this;
  431. }
  432. bool InventoryList::operator == (const InventoryList &other) const
  433. {
  434. if(m_size != other.m_size)
  435. return false;
  436. if(m_width != other.m_width)
  437. return false;
  438. if(m_name != other.m_name)
  439. return false;
  440. for(u32 i=0; i<m_items.size(); i++)
  441. {
  442. ItemStack s1 = m_items[i];
  443. ItemStack s2 = other.m_items[i];
  444. if(s1.name != s2.name || s1.wear!= s2.wear || s1.count != s2.count ||
  445. s1.metadata != s2.metadata)
  446. return false;
  447. }
  448. return true;
  449. }
  450. const std::string &InventoryList::getName() const
  451. {
  452. return m_name;
  453. }
  454. u32 InventoryList::getSize() const
  455. {
  456. return m_items.size();
  457. }
  458. u32 InventoryList::getWidth() const
  459. {
  460. return m_width;
  461. }
  462. u32 InventoryList::getUsedSlots() const
  463. {
  464. u32 num = 0;
  465. for (const auto &m_item : m_items) {
  466. if (!m_item.empty())
  467. num++;
  468. }
  469. return num;
  470. }
  471. u32 InventoryList::getFreeSlots() const
  472. {
  473. return getSize() - getUsedSlots();
  474. }
  475. const ItemStack& InventoryList::getItem(u32 i) const
  476. {
  477. assert(i < m_size); // Pre-condition
  478. return m_items[i];
  479. }
  480. ItemStack& InventoryList::getItem(u32 i)
  481. {
  482. assert(i < m_size); // Pre-condition
  483. return m_items[i];
  484. }
  485. ItemStack InventoryList::changeItem(u32 i, const ItemStack &newitem)
  486. {
  487. if(i >= m_items.size())
  488. return newitem;
  489. ItemStack olditem = m_items[i];
  490. m_items[i] = newitem;
  491. //setDirty(true);
  492. return olditem;
  493. }
  494. void InventoryList::deleteItem(u32 i)
  495. {
  496. assert(i < m_items.size()); // Pre-condition
  497. m_items[i].clear();
  498. }
  499. ItemStack InventoryList::addItem(const ItemStack &newitem_)
  500. {
  501. ItemStack newitem = newitem_;
  502. if(newitem.empty())
  503. return newitem;
  504. /*
  505. First try to find if it could be added to some existing items
  506. */
  507. for(u32 i=0; i<m_items.size(); i++)
  508. {
  509. // Ignore empty slots
  510. if(m_items[i].empty())
  511. continue;
  512. // Try adding
  513. newitem = addItem(i, newitem);
  514. if(newitem.empty())
  515. return newitem; // All was eaten
  516. }
  517. /*
  518. Then try to add it to empty slots
  519. */
  520. for(u32 i=0; i<m_items.size(); i++)
  521. {
  522. // Ignore unempty slots
  523. if(!m_items[i].empty())
  524. continue;
  525. // Try adding
  526. newitem = addItem(i, newitem);
  527. if(newitem.empty())
  528. return newitem; // All was eaten
  529. }
  530. // Return leftover
  531. return newitem;
  532. }
  533. ItemStack InventoryList::addItem(u32 i, const ItemStack &newitem)
  534. {
  535. if(i >= m_items.size())
  536. return newitem;
  537. ItemStack leftover = m_items[i].addItem(newitem, m_itemdef);
  538. //if(leftover != newitem)
  539. // setDirty(true);
  540. return leftover;
  541. }
  542. bool InventoryList::itemFits(const u32 i, const ItemStack &newitem,
  543. ItemStack *restitem) const
  544. {
  545. if(i >= m_items.size())
  546. {
  547. if(restitem)
  548. *restitem = newitem;
  549. return false;
  550. }
  551. return m_items[i].itemFits(newitem, restitem, m_itemdef);
  552. }
  553. bool InventoryList::roomForItem(const ItemStack &item_) const
  554. {
  555. ItemStack item = item_;
  556. ItemStack leftover;
  557. for(u32 i=0; i<m_items.size(); i++)
  558. {
  559. if(itemFits(i, item, &leftover))
  560. return true;
  561. item = leftover;
  562. }
  563. return false;
  564. }
  565. bool InventoryList::containsItem(const ItemStack &item, bool match_meta) const
  566. {
  567. u32 count = item.count;
  568. if (count == 0)
  569. return true;
  570. for (auto i = m_items.rbegin(); i != m_items.rend(); ++i) {
  571. if (count == 0)
  572. break;
  573. if (i->name == item.name && (!match_meta || (i->metadata == item.metadata))) {
  574. if (i->count >= count)
  575. return true;
  576. count -= i->count;
  577. }
  578. }
  579. return false;
  580. }
  581. ItemStack InventoryList::removeItem(const ItemStack &item)
  582. {
  583. ItemStack removed;
  584. for (auto i = m_items.rbegin(); i != m_items.rend(); ++i) {
  585. if (i->name == item.name) {
  586. u32 still_to_remove = item.count - removed.count;
  587. removed.addItem(i->takeItem(still_to_remove), m_itemdef);
  588. if (removed.count == item.count)
  589. break;
  590. }
  591. }
  592. return removed;
  593. }
  594. ItemStack InventoryList::takeItem(u32 i, u32 takecount)
  595. {
  596. if(i >= m_items.size())
  597. return ItemStack();
  598. ItemStack taken = m_items[i].takeItem(takecount);
  599. //if(!taken.empty())
  600. // setDirty(true);
  601. return taken;
  602. }
  603. void InventoryList::moveItemSomewhere(u32 i, InventoryList *dest, u32 count)
  604. {
  605. // Take item from source list
  606. ItemStack item1;
  607. if (count == 0)
  608. item1 = changeItem(i, ItemStack());
  609. else
  610. item1 = takeItem(i, count);
  611. if (item1.empty())
  612. return;
  613. // Try to add the item to destination list
  614. u32 dest_size = dest->getSize();
  615. // First try all the non-empty slots
  616. for (u32 dest_i = 0; dest_i < dest_size; dest_i++) {
  617. if (!m_items[dest_i].empty()) {
  618. item1 = dest->addItem(dest_i, item1);
  619. if (item1.empty()) return;
  620. }
  621. }
  622. // Then try all the empty ones
  623. for (u32 dest_i = 0; dest_i < dest_size; dest_i++) {
  624. if (m_items[dest_i].empty()) {
  625. item1 = dest->addItem(dest_i, item1);
  626. if (item1.empty()) return;
  627. }
  628. }
  629. // If we reach this, the item was not fully added
  630. // Add the remaining part back to the source item
  631. addItem(i, item1);
  632. }
  633. u32 InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i,
  634. u32 count, bool swap_if_needed, bool *did_swap)
  635. {
  636. if(this == dest && i == dest_i)
  637. return count;
  638. // Take item from source list
  639. ItemStack item1;
  640. if(count == 0)
  641. item1 = changeItem(i, ItemStack());
  642. else
  643. item1 = takeItem(i, count);
  644. if(item1.empty())
  645. return 0;
  646. // Try to add the item to destination list
  647. u32 oldcount = item1.count;
  648. item1 = dest->addItem(dest_i, item1);
  649. // If something is returned, the item was not fully added
  650. if(!item1.empty())
  651. {
  652. // If olditem is returned, nothing was added.
  653. bool nothing_added = (item1.count == oldcount);
  654. // If something else is returned, part of the item was left unadded.
  655. // Add the other part back to the source item
  656. addItem(i, item1);
  657. // If olditem is returned, nothing was added.
  658. // Swap the items
  659. if (nothing_added && swap_if_needed) {
  660. // Tell that we swapped
  661. if (did_swap != NULL) {
  662. *did_swap = true;
  663. }
  664. // Take item from source list
  665. item1 = changeItem(i, ItemStack());
  666. // Adding was not possible, swap the items.
  667. ItemStack item2 = dest->changeItem(dest_i, item1);
  668. // Put item from destination list to the source list
  669. changeItem(i, item2);
  670. }
  671. }
  672. return (oldcount - item1.count);
  673. }
  674. /*
  675. Inventory
  676. */
  677. Inventory::~Inventory()
  678. {
  679. clear();
  680. }
  681. void Inventory::clear()
  682. {
  683. m_dirty = true;
  684. for (auto &m_list : m_lists) {
  685. delete m_list;
  686. }
  687. m_lists.clear();
  688. }
  689. void Inventory::clearContents()
  690. {
  691. m_dirty = true;
  692. for (InventoryList *list : m_lists) {
  693. for (u32 j=0; j<list->getSize(); j++) {
  694. list->deleteItem(j);
  695. }
  696. }
  697. }
  698. Inventory::Inventory(IItemDefManager *itemdef)
  699. {
  700. m_dirty = false;
  701. m_itemdef = itemdef;
  702. }
  703. Inventory::Inventory(const Inventory &other)
  704. {
  705. *this = other;
  706. m_dirty = false;
  707. }
  708. Inventory & Inventory::operator = (const Inventory &other)
  709. {
  710. // Gracefully handle self assignment
  711. if(this != &other)
  712. {
  713. m_dirty = true;
  714. clear();
  715. m_itemdef = other.m_itemdef;
  716. for (InventoryList *list : other.m_lists) {
  717. m_lists.push_back(new InventoryList(*list));
  718. }
  719. }
  720. return *this;
  721. }
  722. bool Inventory::operator == (const Inventory &other) const
  723. {
  724. if(m_lists.size() != other.m_lists.size())
  725. return false;
  726. for(u32 i=0; i<m_lists.size(); i++)
  727. {
  728. if(*m_lists[i] != *other.m_lists[i])
  729. return false;
  730. }
  731. return true;
  732. }
  733. void Inventory::serialize(std::ostream &os) const
  734. {
  735. for (InventoryList *list : m_lists) {
  736. os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
  737. list->serialize(os);
  738. }
  739. os<<"EndInventory\n";
  740. }
  741. void Inventory::deSerialize(std::istream &is)
  742. {
  743. clear();
  744. for(;;)
  745. {
  746. std::string line;
  747. std::getline(is, line, '\n');
  748. std::istringstream iss(line);
  749. std::string name;
  750. std::getline(iss, name, ' ');
  751. if (name == "EndInventory") {
  752. break;
  753. }
  754. // This is a temporary backwards compatibility fix
  755. if (name == "end") {
  756. break;
  757. }
  758. if (name == "List") {
  759. std::string listname;
  760. u32 listsize;
  761. std::getline(iss, listname, ' ');
  762. iss>>listsize;
  763. InventoryList *list = new InventoryList(listname, listsize, m_itemdef);
  764. list->deSerialize(is);
  765. m_lists.push_back(list);
  766. }
  767. else
  768. {
  769. throw SerializationError("invalid inventory specifier: " + name);
  770. }
  771. }
  772. }
  773. InventoryList * Inventory::addList(const std::string &name, u32 size)
  774. {
  775. m_dirty = true;
  776. s32 i = getListIndex(name);
  777. if(i != -1)
  778. {
  779. if(m_lists[i]->getSize() != size)
  780. {
  781. delete m_lists[i];
  782. m_lists[i] = new InventoryList(name, size, m_itemdef);
  783. }
  784. return m_lists[i];
  785. }
  786. //don't create list with invalid name
  787. if (name.find(' ') != std::string::npos) return NULL;
  788. InventoryList *list = new InventoryList(name, size, m_itemdef);
  789. m_lists.push_back(list);
  790. return list;
  791. }
  792. InventoryList * Inventory::getList(const std::string &name)
  793. {
  794. s32 i = getListIndex(name);
  795. if(i == -1)
  796. return NULL;
  797. return m_lists[i];
  798. }
  799. std::vector<const InventoryList*> Inventory::getLists()
  800. {
  801. std::vector<const InventoryList*> lists;
  802. for (auto list : m_lists) {
  803. lists.push_back(list);
  804. }
  805. return lists;
  806. }
  807. bool Inventory::deleteList(const std::string &name)
  808. {
  809. s32 i = getListIndex(name);
  810. if(i == -1)
  811. return false;
  812. m_dirty = true;
  813. delete m_lists[i];
  814. m_lists.erase(m_lists.begin() + i);
  815. return true;
  816. }
  817. const InventoryList * Inventory::getList(const std::string &name) const
  818. {
  819. s32 i = getListIndex(name);
  820. if(i == -1)
  821. return NULL;
  822. return m_lists[i];
  823. }
  824. const s32 Inventory::getListIndex(const std::string &name) const
  825. {
  826. for(u32 i=0; i<m_lists.size(); i++)
  827. {
  828. if(m_lists[i]->getName() == name)
  829. return i;
  830. }
  831. return -1;
  832. }
  833. //END