ItemType.cpp 6.6 KB

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