inventory.cpp 19 KB

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