ItemType.cpp 6.5 KB

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