ItemType.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include "ItemType.h"
  2. #include <InMemoryBuffer.h>
  3. #include "Game.h"
  4. #include "ItemSkill.h"
  5. #include "ItemStack.h"
  6. ItemType::ItemType(Framework::Text name,
  7. ModelInfo* model,
  8. int maxStackSize,
  9. Framework::RCArray<Framework::Text> groups)
  10. : ReferenceCounter(),
  11. name(name),
  12. model(model),
  13. maxStackSize(maxStackSize),
  14. groups(groups)
  15. {
  16. tooltipUIML = Framework::Text("<tip><text width=\"auto\" height=\"auto\">")
  17. + name + "</text></tip>";
  18. }
  19. ItemType::~ItemType()
  20. {
  21. if (model) model->release();
  22. }
  23. bool ItemType::initialize(Game* zGame)
  24. {
  25. return true;
  26. }
  27. void ItemType::loadSuperItem(
  28. Item* zItem, Framework::StreamReader* zReader) const
  29. {
  30. zReader->lese((char*)&zItem->hp, 4);
  31. zReader->lese((char*)&zItem->maxHp, 4);
  32. zReader->lese((char*)&zItem->durability, 4);
  33. zReader->lese((char*)&zItem->maxDurability, 4);
  34. unsigned char flags = 0;
  35. zReader->lese((char*)&flags, 1);
  36. zItem->eatable = (flags | 1) == flags;
  37. zItem->placeable = (flags | 2) == flags;
  38. zItem->equippable = (flags | 4) == flags;
  39. zItem->solid = (flags | 8) == flags;
  40. zItem->usable = (flags | 16) == flags;
  41. unsigned char len = 0;
  42. zReader->lese((char*)&len, 1);
  43. zItem->name.fillText(' ', len);
  44. zReader->lese((char*)zItem->name.getText(), len);
  45. }
  46. void ItemType::saveSuperItem(
  47. const Item* zItem, Framework::StreamWriter* zWriter) const
  48. {
  49. zWriter->schreibe((char*)&zItem->hp, 4);
  50. zWriter->schreibe((char*)&zItem->maxHp, 4);
  51. zWriter->schreibe((char*)&zItem->durability, 4);
  52. zWriter->schreibe((char*)&zItem->maxDurability, 4);
  53. unsigned char flags
  54. = (unsigned char)((zItem->usable << 4) | (zItem->solid << 3)
  55. | (zItem->equippable << 2) | (zItem->placeable << 1)
  56. | (int)zItem->eatable);
  57. zWriter->schreibe((char*)&flags, 1);
  58. unsigned char len = (unsigned char)zItem->name.getLength();
  59. zWriter->schreibe((char*)&len, 1);
  60. zWriter->schreibe(zItem->name, len);
  61. }
  62. int ItemType::getId() const
  63. {
  64. return id;
  65. }
  66. const Framework::Text& ItemType::getName() const
  67. {
  68. return name;
  69. }
  70. const Framework::Text& ItemType::getTooltipUIML() const
  71. {
  72. return tooltipUIML;
  73. }
  74. const ItemType* ItemType::zBrokenItemType() const
  75. {
  76. return 0;
  77. }
  78. ItemStack* ItemType::createItemStack(int size) const
  79. {
  80. Item* item = createItem();
  81. if (!item) return 0;
  82. return new ItemStack(item, MIN(size, item->getMaxStackSize()));
  83. }
  84. ItemSkill* ItemType::createDefaultItemSkill() const
  85. {
  86. return 0;
  87. }
  88. void ItemType::levelUpItemSkill(ItemSkill* zSkill) const {}
  89. Item* ItemType::loadItem(Framework::StreamReader* zReader) const
  90. {
  91. Item* item = createItem();
  92. loadSuperItem(item, zReader);
  93. return item;
  94. }
  95. void ItemType::saveItem(
  96. const Item* zItem, Framework::StreamWriter* zWriter) const
  97. {
  98. saveSuperItem(zItem, zWriter);
  99. }
  100. Item* ItemType::cloneItem(const Item* zItem) const
  101. {
  102. Framework::InMemoryBuffer buffer;
  103. saveItem(zItem, &buffer);
  104. return loadItem(&buffer);
  105. }
  106. Item* ItemType::breakItem(const Item* zItem) const
  107. {
  108. return 0;
  109. }
  110. ModelInfo* ItemType::zModel() const
  111. {
  112. return model;
  113. }
  114. void ItemType::setItemAttribute(
  115. Item* zItem, Framework::Text name, Framework::JSON::JSONValue* zValue) const
  116. {
  117. bool ok = true;
  118. if (zValue->getType() == Framework::JSON::JSONType::NUMBER)
  119. {
  120. if (name.istGleich("hp"))
  121. zItem->hp = (float)zValue->asNumber()->getNumber();
  122. else if (name.istGleich("maxHp"))
  123. zItem->maxHp = (float)zValue->asNumber()->getNumber();
  124. else if (name.istGleich("durability"))
  125. zItem->durability = (float)zValue->asNumber()->getNumber();
  126. else if (name.istGleich("maxDurability"))
  127. zItem->maxDurability = (float)zValue->asNumber()->getNumber();
  128. else
  129. ok = false;
  130. }
  131. else if (zValue->getType() == Framework::JSON::JSONType::STRING)
  132. {
  133. if (name.istGleich("name"))
  134. zItem->name = zValue->asString()->getString();
  135. else
  136. ok = false;
  137. }
  138. if (!ok)
  139. {
  140. std::cout << "Invalid Item Attribute '" << name << "' for item type '"
  141. << getName() << "'\n";
  142. }
  143. }
  144. void ItemType::addItemAttributes(
  145. Item* zItem, Framework::JSON::JSONObject* zItemObjet) const
  146. {
  147. zItemObjet->addValue(
  148. "hp", new Framework::JSON::JSONNumber((double)zItem->hp));
  149. zItemObjet->addValue(
  150. "maxHp", new Framework::JSON::JSONNumber((double)zItem->maxHp));
  151. zItemObjet->addValue("durability",
  152. new Framework::JSON::JSONNumber((double)zItem->durability));
  153. zItemObjet->addValue("maxDurability",
  154. new Framework::JSON::JSONNumber((double)zItem->maxDurability));
  155. zItemObjet->addValue("name", new Framework::JSON::JSONString(zItem->name));
  156. }
  157. void ItemType::setTypeId(int id)
  158. {
  159. this->id = id;
  160. }
  161. int ItemType::getMaxStackSize() const
  162. {
  163. return maxStackSize;
  164. }
  165. const Framework::RCArray<Framework::Text>& ItemType::getGroups() const
  166. {
  167. return groups;
  168. }
  169. Item* ItemType::createBasicItem(int id,
  170. const char* name,
  171. float hp,
  172. float maxHp,
  173. float durability,
  174. float maxDurability,
  175. bool eatable,
  176. bool placeable,
  177. bool equippable,
  178. bool solid,
  179. bool usable)
  180. {
  181. Item* item = new Item(id, name);
  182. item->hp = hp;
  183. item->maxHp = maxHp;
  184. item->durability = durability;
  185. item->maxDurability = maxDurability;
  186. item->eatable = eatable;
  187. item->placeable = placeable;
  188. item->equippable = equippable;
  189. item->solid = solid;
  190. item->usable = usable;
  191. return item;
  192. }
  193. int ItemType::getTypeId(const char* name)
  194. {
  195. for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
  196. {
  197. if (Game::INSTANCE->zItemType(i)
  198. && Game::INSTANCE->zItemType(i)->getName().istGleich(name))
  199. return Game::INSTANCE->zItemType(i)->getId();
  200. }
  201. return 0;
  202. }
  203. const ItemType* ItemType::zByName(const char* name)
  204. {
  205. for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
  206. {
  207. if (Game::INSTANCE->zItemType(i)
  208. && Game::INSTANCE->zItemType(i)->getName().istGleich(name))
  209. return Game::INSTANCE->zItemType(i);
  210. }
  211. return 0;
  212. }