ItemType.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. void ItemType::setItemAttribute(
  135. Item* zItem, Framework::Text name, Framework::JSON::JSONValue* zValue) const
  136. {
  137. if (zValue->getType() == Framework::JSON::JSONType::NUMBER)
  138. {
  139. if (name.istGleich("hp"))
  140. zItem->hp = (float)zValue->asNumber()->getNumber();
  141. else if (name.istGleich("maxHp"))
  142. zItem->maxHp = (float)zValue->asNumber()->getNumber();
  143. else if (name.istGleich("durability"))
  144. zItem->durability = (float)zValue->asNumber()->getNumber();
  145. else if (name.istGleich("maxDurability"))
  146. zItem->maxDurability = (float)zValue->asNumber()->getNumber();
  147. }
  148. else if (zValue->getType() == Framework::JSON::JSONType::STRING)
  149. {
  150. if (name.istGleich("name"))
  151. zItem->name = zValue->asString()->getString();
  152. }
  153. }
  154. void ItemType::addItemAttributes(
  155. Item* zItem, Framework::JSON::JSONObject* zItemObjet) const
  156. {
  157. zItemObjet->addValue("hp", new Framework::JSON::JSONNumber((double)zItem->hp));
  158. zItemObjet->addValue("maxHp", new Framework::JSON::JSONNumber((double)zItem->maxHp));
  159. zItemObjet->addValue("durability", new Framework::JSON::JSONNumber((double)zItem->durability));
  160. zItemObjet->addValue("maxDurability",
  161. new Framework::JSON::JSONNumber((double)zItem->maxDurability));
  162. zItemObjet->addValue("name", new Framework::JSON::JSONString(zItem->name));
  163. }
  164. Item* ItemType::createBasicItem(int id,
  165. const char* name,
  166. float hp,
  167. float maxHp,
  168. float durability,
  169. float maxDurability,
  170. bool eatable,
  171. bool placeable,
  172. bool equippable,
  173. bool solid,
  174. bool usable,
  175. int maxStackSize)
  176. {
  177. Item* item = new Item(id, name);
  178. item->hp = hp;
  179. item->maxHp = maxHp;
  180. item->durability = durability;
  181. item->maxDurability = maxDurability;
  182. item->eatable = eatable;
  183. item->placeable = placeable;
  184. item->equippable = equippable;
  185. item->solid = solid;
  186. item->usable = usable;
  187. item->maxStackSize = maxStackSize;
  188. return item;
  189. }
  190. int ItemType::getTypeId(const char* name)
  191. {
  192. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  193. {
  194. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
  195. && StaticRegistry<ItemType>::INSTANCE.zElement(i)
  196. ->getName()
  197. .istGleich(name))
  198. return StaticRegistry<ItemType>::INSTANCE.zElement(i)->getId();
  199. }
  200. return 0;
  201. }
  202. const ItemType* ItemType::zByName(const char* name)
  203. {
  204. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  205. {
  206. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
  207. && StaticRegistry<ItemType>::INSTANCE.zElement(i)
  208. ->getName()
  209. .istGleich(name))
  210. return StaticRegistry<ItemType>::INSTANCE.zElement(i);
  211. }
  212. return 0;
  213. }