ItemType.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include "ItemType.h"
  2. #include <InMemoryBuffer.h>
  3. #include "ItemSkill.h"
  4. #include "ItemStack.h"
  5. ItemType::ItemType(int id,
  6. const char* name,
  7. ItemSkillLevelUpRule* levelUpRule,
  8. int brokenTypeId,
  9. ModelInfo model)
  10. : ReferenceCounter(),
  11. id(id),
  12. name(name),
  13. levelUpRule(levelUpRule),
  14. brokenTypeId(brokenTypeId),
  15. model(model)
  16. {
  17. tooltipUIML = Framework::Text("<tip><text width=\"auto\" height=\"auto\">")
  18. + name + "</text></tip>";
  19. StaticRegistry<ItemType>::INSTANCE.registerT(this, id);
  20. }
  21. ItemType::~ItemType()
  22. {
  23. if (levelUpRule) levelUpRule->release();
  24. }
  25. void ItemType::loadSuperItem(
  26. Item* zItem, Framework::StreamReader* zReader) const
  27. {
  28. zReader->lese((char*)&zItem->hp, 4);
  29. zReader->lese((char*)&zItem->maxHp, 4);
  30. zReader->lese((char*)&zItem->durability, 4);
  31. zReader->lese((char*)&zItem->maxDurability, 4);
  32. unsigned char flags = 0;
  33. zReader->lese((char*)&flags, 1);
  34. zItem->eatable = (flags | 1) == flags;
  35. zItem->placeable = (flags | 2) == flags;
  36. zItem->equippable = (flags | 4) == flags;
  37. zItem->solid = (flags | 8) == flags;
  38. zItem->usable = (flags | 16) == flags;
  39. zReader->lese((char*)&zItem->maxStackSize, 1);
  40. unsigned char len = 0;
  41. zReader->lese((char*)&len, 1);
  42. zItem->name.fillText(' ', len);
  43. zReader->lese((char*)zItem->name.getText(), len);
  44. }
  45. void ItemType::saveSuperItem(
  46. const Item* zItem, Framework::StreamWriter* zWriter) const
  47. {
  48. zWriter->schreibe((char*)&zItem->hp, 4);
  49. zWriter->schreibe((char*)&zItem->maxHp, 4);
  50. zWriter->schreibe((char*)&zItem->durability, 4);
  51. zWriter->schreibe((char*)&zItem->maxDurability, 4);
  52. unsigned char flags
  53. = (unsigned char)((zItem->usable << 4) | (zItem->solid << 3)
  54. | (zItem->equippable << 2) | (zItem->placeable << 1)
  55. | (int)zItem->eatable);
  56. zWriter->schreibe((char*)&flags, 1);
  57. zWriter->schreibe((char*)&zItem->maxStackSize, 1);
  58. unsigned char len = (unsigned char)zItem->name.getLength();
  59. zWriter->schreibe((char*)&len, 1);
  60. zWriter->schreibe(zItem->name, len);
  61. }
  62. void ItemType::loadSuperItemSkill(
  63. ItemSkill* zSkill, Framework::StreamReader* zReader) const
  64. {}
  65. void ItemType::saveSuperItemSkill(
  66. const ItemSkill* zSkill, Framework::StreamWriter* zWriter) const
  67. {}
  68. int ItemType::getId() const
  69. {
  70. return id;
  71. }
  72. const Framework::Text& ItemType::getName() const
  73. {
  74. return name;
  75. }
  76. const Framework::Text& ItemType::getTooltipUIML() const
  77. {
  78. return tooltipUIML;
  79. }
  80. const ItemType* ItemType::zBrokenItemType() const
  81. {
  82. return StaticRegistry<ItemType>::INSTANCE.zElement(brokenTypeId);
  83. }
  84. ItemStack* ItemType::createItemStack(int size) const
  85. {
  86. Item* item = createItem();
  87. if (!item) return 0;
  88. return new ItemStack(item, MIN(size, item->getMaxStackSize()));
  89. }
  90. ItemSkill* ItemType::createDefaultItemSkill() const
  91. {
  92. return 0;
  93. }
  94. void ItemType::levelUpItemSkill(ItemSkill* zSkill) const
  95. {
  96. if (levelUpRule) levelUpRule->applyOn(zSkill);
  97. }
  98. Item* ItemType::loadItem(Framework::StreamReader* zReader) const
  99. {
  100. Item* item = createItem();
  101. loadSuperItem(item, zReader);
  102. return item;
  103. }
  104. void ItemType::saveItem(
  105. const Item* zItem, Framework::StreamWriter* zWriter) const
  106. {
  107. saveSuperItem(zItem, zWriter);
  108. }
  109. Item* ItemType::cloneItem(const Item* zItem) const
  110. {
  111. Framework::InMemoryBuffer buffer;
  112. saveItem(zItem, &buffer);
  113. return loadItem(&buffer);
  114. }
  115. ItemSkill* ItemType::loadItemSkill(Framework::StreamReader* zReader) const
  116. {
  117. ItemSkill* skill = createDefaultItemSkill();
  118. loadSuperItemSkill(skill, zReader);
  119. return skill;
  120. }
  121. void ItemType::saveItemSkill(
  122. const ItemSkill* zSkill, Framework::StreamWriter* zWriter) const
  123. {
  124. saveSuperItemSkill(zSkill, zWriter);
  125. }
  126. Item* ItemType::breakItem(const Item* zItem) const
  127. {
  128. return 0;
  129. }
  130. const ModelInfo& ItemType::getModel() const
  131. {
  132. return model;
  133. }
  134. Item* ItemType::createBasicItem(int id,
  135. const char* name,
  136. float hp,
  137. float maxHp,
  138. float durability,
  139. float maxDurability,
  140. bool eatable,
  141. bool placeable,
  142. bool equippable,
  143. bool solid,
  144. bool usable,
  145. int maxStackSize)
  146. {
  147. Item* item = new Item(id, name);
  148. item->hp = hp;
  149. item->maxHp = maxHp;
  150. item->durability = durability;
  151. item->maxDurability = maxDurability;
  152. item->eatable = eatable;
  153. item->placeable = placeable;
  154. item->equippable = equippable;
  155. item->solid = solid;
  156. item->usable = usable;
  157. item->maxStackSize = maxStackSize;
  158. return item;
  159. }
  160. int ItemType::getTypeId(const char* name)
  161. {
  162. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  163. {
  164. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
  165. && StaticRegistry<ItemType>::INSTANCE.zElement(i)
  166. ->getName()
  167. .istGleich(name))
  168. return StaticRegistry<ItemType>::INSTANCE.zElement(i)->getId();
  169. }
  170. return 0;
  171. }