ItemType.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include <InMemoryBuffer.h>
  2. #include "ItemType.h"
  3. #include "ItemSkill.h"
  4. #include "ItemStack.h"
  5. ItemType::ItemType( int id, ItemSkillLevelUpRule* levelUpRule, const ItemType* zBrokenType )
  6. : ReferenceCounter(),
  7. id( id ),
  8. levelUpRule( levelUpRule ),
  9. zBrokenType( zBrokenType )
  10. {
  11. StaticRegistry<ItemType>::INSTANCE.registerT( this, id );
  12. }
  13. ItemType::~ItemType()
  14. {
  15. if( levelUpRule )
  16. levelUpRule->release();
  17. }
  18. void ItemType::loadSuperItem( Item* zItem, Framework::StreamReader* zReader ) const
  19. {
  20. zReader->lese( (char*)&zItem->damage, 4 );
  21. zReader->lese( (char*)&zItem->maxDamage, 4 );
  22. zReader->lese( (char*)&zItem->durability, 4 );
  23. zReader->lese( (char*)&zItem->maxDurability, 4 );
  24. unsigned char flags = 0;
  25. zReader->lese( (char*)&flags, 1 );
  26. zItem->eatable = (flags | 1) == flags;
  27. zItem->placeable = (flags | 2) == flags;
  28. zItem->equippable = (flags | 4) == flags;
  29. zItem->solid = (flags | 8) == flags;
  30. zItem->usable = (flags | 16) == flags;
  31. zReader->lese( (char*)&zItem->maxStackSize, 1 );
  32. unsigned char len = 0;
  33. zReader->lese( (char*)&len, 1 );
  34. zItem->name.fillText( ' ', len );
  35. zReader->lese( zItem->name, len );
  36. }
  37. void ItemType::saveSuperItem( const Item* zItem, Framework::StreamWriter* zWriter ) const
  38. {
  39. zWriter->schreibe( (char*)&zItem->damage, 4 );
  40. zWriter->schreibe( (char*)&zItem->maxDamage, 4 );
  41. zWriter->schreibe( (char*)&zItem->durability, 4 );
  42. zWriter->schreibe( (char*)&zItem->maxDurability, 4 );
  43. unsigned char flags = (unsigned char)(((((zItem->usable << 1) | zItem->solid << 1) | zItem->equippable << 1) | zItem->placeable << 1) | zItem->eatable);
  44. zWriter->schreibe( (char*)&flags, 1 );
  45. zWriter->schreibe( (char*)&zItem->maxStackSize, 1 );
  46. unsigned char len = (unsigned char)zItem->name.getLength();
  47. zWriter->schreibe( (char*)&len, 1 );
  48. zWriter->schreibe( zItem->name, len );
  49. }
  50. void ItemType::loadSuperItemSkill( ItemSkill* zSkill, Framework::StreamReader* zReader ) const
  51. {
  52. }
  53. void ItemType::saveSuperItemSkill( const ItemSkill* zSkill, Framework::StreamWriter* zWriter ) const
  54. {
  55. }
  56. int ItemType::getId() const
  57. {
  58. return id;
  59. }
  60. const ItemType* ItemType::zBrokenItemType() const
  61. {
  62. return zBrokenType;
  63. }
  64. ItemStack* ItemType::createItemStack( int size ) const
  65. {
  66. Item* item = createItem();
  67. if( !item )
  68. return 0;
  69. return new ItemStack( item, MIN( size, item->getMaxStackSize() ) );
  70. }
  71. ItemSkill* ItemType::createDefaultItemSkill() const
  72. {
  73. return 0;
  74. }
  75. void ItemType::levelUpItemSkill( ItemSkill* zSkill ) const
  76. {
  77. if( levelUpRule )
  78. levelUpRule->applyOn( zSkill );
  79. }
  80. Item* ItemType::loadItem( Framework::StreamReader* zReader ) const
  81. {
  82. Item* item = createItem();
  83. loadSuperItem( item, zReader );
  84. return item;
  85. }
  86. void ItemType::saveItem( const Item* zItem, Framework::StreamWriter* zWriter ) const
  87. {
  88. saveSuperItem( zItem, zWriter );
  89. }
  90. Item* ItemType::cloneItem( Item* zItem ) const
  91. {
  92. Framework::InMemoryBuffer buffer;
  93. saveItem( zItem, &buffer );
  94. return loadItem( &buffer );
  95. }
  96. ItemSkill* ItemType::loadItemSkill( Framework::StreamReader* zReader ) const
  97. {
  98. ItemSkill* skill = createDefaultItemSkill();
  99. loadSuperItemSkill( skill, zReader );
  100. return skill;
  101. }
  102. void ItemType::saveItemSkill( const ItemSkill* zSkill, Framework::StreamWriter* zWriter ) const
  103. {
  104. saveSuperItemSkill( zSkill, zWriter );
  105. }
  106. Item* ItemType::breakItem( Item* zItem ) const
  107. {
  108. return 0;
  109. }