ItemType.cpp 6.8 KB

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