inventory.cpp 21 KB

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