From 0298d34406b1582e882795bf4d9d3fea18fd0135 Mon Sep 17 00:00:00 2001 From: archshift Date: Sun, 20 Jul 2014 02:56:59 -0700 Subject: Moved potion static functions to EntityEffect to create splash potions through world --- src/Entities/EntityEffect.cpp | 124 ++++++++++++++++++++++++++++++++ src/Entities/EntityEffect.h | 19 +++++ src/Entities/ProjectileEntity.cpp | 2 + src/Entities/SplashPotionEntity.cpp | 16 +++-- src/Entities/SplashPotionEntity.h | 4 +- src/Items/ItemPotion.h | 136 ++---------------------------------- src/WorldStorage/WSSAnvil.cpp | 2 +- 7 files changed, 164 insertions(+), 139 deletions(-) (limited to 'src') diff --git a/src/Entities/EntityEffect.cpp b/src/Entities/EntityEffect.cpp index 6c4b13a58..f58738aef 100644 --- a/src/Entities/EntityEffect.cpp +++ b/src/Entities/EntityEffect.cpp @@ -7,6 +7,130 @@ + +int cEntityEffect::GetPotionColor(short a_ItemDamage) +{ + // Lowest six bits + return (a_ItemDamage & 0x3f); +} + + + + + +cEntityEffect::eType cEntityEffect::GetPotionEffectType(short a_ItemDamage) +{ + // Lowest four bits + // Potion effect bits are different from entity effect values + // For reference: http://minecraft.gamepedia.com/Data_values#.22Potion_effect.22_bits + switch (a_ItemDamage & 0x0f) + { + case 0x01: return cEntityEffect::effRegeneration; + case 0x02: return cEntityEffect::effSpeed; + case 0x03: return cEntityEffect::effFireResistance; + case 0x04: return cEntityEffect::effPoison; + case 0x05: return cEntityEffect::effInstantHealth; + case 0x06: return cEntityEffect::effNightVision; + case 0x08: return cEntityEffect::effWeakness; + case 0x09: return cEntityEffect::effStrength; + case 0x0a: return cEntityEffect::effSlowness; + case 0x0c: return cEntityEffect::effInstantDamage; + case 0x0d: return cEntityEffect::effWaterBreathing; + case 0x0e: return cEntityEffect::effInvisibility; + + // No effect potions + case 0x00: + case 0x07: + case 0x0b: // Will be potion of leaping in 1.8 + case 0x0f: + { + break; + } + } + return cEntityEffect::effNoEffect; +} + + + + + +short cEntityEffect::GetPotionEffectIntensity(short a_ItemDamage) +{ + // Level II potion if the fifth lowest bit is set + return ((a_ItemDamage & 0x20) != 0) ? 1 : 0; +} + + + + + +int cEntityEffect::GetPotionEffectDuration(short a_ItemDamage) +{ + // Base duration in ticks + int base = 0; + double TierCoeff = 1, ExtCoeff = 1, SplashCoeff = 1; + + switch (GetPotionEffectType(a_ItemDamage)) + { + case cEntityEffect::effRegeneration: + case cEntityEffect::effPoison: + { + base = 900; + break; + } + + case cEntityEffect::effSpeed: + case cEntityEffect::effFireResistance: + case cEntityEffect::effNightVision: + case cEntityEffect::effStrength: + case cEntityEffect::effWaterBreathing: + case cEntityEffect::effInvisibility: + { + base = 3600; + break; + } + + case cEntityEffect::effWeakness: + case cEntityEffect::effSlowness: + { + base = 1800; + break; + } + } + + // If potion is level II, half the duration. If not, stays the same + TierCoeff = (GetPotionEffectIntensity(a_ItemDamage) > 0) ? 0.5 : 1; + + // If potion is extended, multiply duration by 8/3. If not, stays the same + // Extended potion if sixth lowest bit is set + ExtCoeff = (a_ItemDamage & 0x40) ? (8.0 / 3.0) : 1; + + // If potion is splash potion, multiply duration by 3/4. If not, stays the same + SplashCoeff = IsPotionDrinkable(a_ItemDamage) ? 1 : 0.75; + + // Ref.: + // http://minecraft.gamepedia.com/Data_values#.22Tier.22_bit + // http://minecraft.gamepedia.com/Data_values#.22Extended_duration.22_bit + // http://minecraft.gamepedia.com/Data_values#.22Splash_potion.22_bit + + return (int)(base * TierCoeff * ExtCoeff * SplashCoeff); +} + + + + + +bool cEntityEffect::IsPotionDrinkable(short a_ItemDamage) +{ + // Drinkable potion if 13th lowest bit is set + // Ref.: http://minecraft.gamepedia.com/Potions#Data_value_table + return ((a_ItemDamage & 0x2000) != 0); +} + + + + + cEntityEffect::cEntityEffect(): m_Ticks(0), m_Duration(0), diff --git a/src/Entities/EntityEffect.h b/src/Entities/EntityEffect.h index 396a9bbe0..04e25e303 100644 --- a/src/Entities/EntityEffect.h +++ b/src/Entities/EntityEffect.h @@ -36,6 +36,25 @@ public: effSaturation = 23, } ; + /** Returns the potion color (used by the client for visuals), based on the potion's damage value */ + static int GetPotionColor(short a_ItemDamage); + + + /** Translates the potion's damage value into the entity effect that the potion gives */ + static cEntityEffect::eType GetPotionEffectType(short a_ItemDamage); + + + /** Retrieves the intensity level from the potion's damage value. + Returns 0 for level I potions, 1 for level II potions. */ + static short GetPotionEffectIntensity(short a_ItemDamage); + + + /** Returns the effect duration, in ticks, based on the potion's damage value */ + static int GetPotionEffectDuration(short a_ItemDamage); + + /** Returns true if the potion with the given damage is drinkable */ + static bool IsPotionDrinkable(short a_ItemDamage); + // tolua_end /** Creates an empty entity effect */ diff --git a/src/Entities/ProjectileEntity.cpp b/src/Entities/ProjectileEntity.cpp index b5e81bc0c..43023ec28 100644 --- a/src/Entities/ProjectileEntity.cpp +++ b/src/Entities/ProjectileEntity.cpp @@ -22,6 +22,7 @@ #include "FireworkEntity.h" #include "GhastFireballEntity.h" #include "WitherSkullEntity.h" +#include "SplashPotionEntity.h" #include "Player.h" @@ -263,6 +264,7 @@ cProjectileEntity * cProjectileEntity::Create(eKind a_Kind, cEntity * a_Creator, case pkGhastFireball: return new cGhastFireballEntity (a_Creator, a_X, a_Y, a_Z, Speed); case pkFireCharge: return new cFireChargeEntity (a_Creator, a_X, a_Y, a_Z, Speed); case pkExpBottle: return new cExpBottleEntity (a_Creator, a_X, a_Y, a_Z, Speed); + case pkSplashPotion: return new cSplashPotionEntity (a_Creator, a_X, a_Y, a_Z, Speed, *a_Item); case pkWitherSkull: return new cWitherSkullEntity (a_Creator, a_X, a_Y, a_Z, Speed); case pkFirework: { diff --git a/src/Entities/SplashPotionEntity.cpp b/src/Entities/SplashPotionEntity.cpp index 6d874e957..d874a86e1 100644 --- a/src/Entities/SplashPotionEntity.cpp +++ b/src/Entities/SplashPotionEntity.cpp @@ -72,17 +72,21 @@ cSplashPotionEntity::cSplashPotionEntity( cEntity * a_Creator, double a_X, double a_Y, double a_Z, const Vector3d & a_Speed, - cEntityEffect::eType a_EntityEffectType, - cEntityEffect a_EntityEffect, - int a_PotionColor + cItem a_Item ) : super(pkSplashPotion, a_Creator, a_X, a_Y, a_Z, 0.25, 0.25), - m_EntityEffectType(a_EntityEffectType), - m_EntityEffect(a_EntityEffect), - m_PotionColor(a_PotionColor), + m_EntityEffectType(cEntityEffect::effNoEffect), + m_EntityEffect(cEntityEffect()), + m_PotionColor(0), m_DestroyTimer(-1) { SetSpeed(a_Speed); + m_EntityEffectType = cEntityEffect::GetPotionEffectType(a_Item.m_ItemDamage); + m_EntityEffect = cEntityEffect( + cEntityEffect::GetPotionEffectDuration(a_Item.m_ItemDamage), + cEntityEffect::GetPotionEffectIntensity(a_Item.m_ItemDamage) + ); + m_PotionColor = cEntityEffect::GetPotionColor(a_Item.m_ItemDamage); } diff --git a/src/Entities/SplashPotionEntity.h b/src/Entities/SplashPotionEntity.h index dd14ea82f..db1672f9b 100644 --- a/src/Entities/SplashPotionEntity.h +++ b/src/Entities/SplashPotionEntity.h @@ -29,9 +29,7 @@ public: cEntity * a_Creator, double a_X, double a_Y, double a_Z, const Vector3d & a_Speed, - cEntityEffect::eType a_EntityEffectType, - cEntityEffect a_EntityEffect, - int a_PotionColor + cItem a_Item ); cEntityEffect::eType GetEntityEffectType(void) const { return m_EntityEffectType; } diff --git a/src/Items/ItemPotion.h b/src/Items/ItemPotion.h index f16d89b39..cc974c8b9 100644 --- a/src/Items/ItemPotion.h +++ b/src/Items/ItemPotion.h @@ -17,126 +17,12 @@ public: } - /** Returns the potion color (used by the client for visuals), based on the potion's damage value */ - static int GetPotionColor(short a_ItemDamage) - { - // Lowest six bits - return (a_ItemDamage & 0x3f); - } - - - /** Translates the potion's damage value into the entity effect that the potion gives */ - static cEntityEffect::eType GetEntityEffectType(short a_ItemDamage) - { - // Lowest four bits - // Potion effect bits are different from entity effect values - // For reference: http://minecraft.gamepedia.com/Data_values#.22Potion_effect.22_bits - switch (a_ItemDamage & 0x0f) - { - case 0x01: return cEntityEffect::effRegeneration; - case 0x02: return cEntityEffect::effSpeed; - case 0x03: return cEntityEffect::effFireResistance; - case 0x04: return cEntityEffect::effPoison; - case 0x05: return cEntityEffect::effInstantHealth; - case 0x06: return cEntityEffect::effNightVision; - case 0x08: return cEntityEffect::effWeakness; - case 0x09: return cEntityEffect::effStrength; - case 0x0a: return cEntityEffect::effSlowness; - case 0x0c: return cEntityEffect::effInstantDamage; - case 0x0d: return cEntityEffect::effWaterBreathing; - case 0x0e: return cEntityEffect::effInvisibility; - - // No effect potions - case 0x00: - case 0x07: - case 0x0b: // Will be potion of leaping in 1.8 - case 0x0f: - { - break; - } - } - return cEntityEffect::effNoEffect; - } - - - /** Retrieves the intensity level from the potion's damage value. - Returns 0 for level I potions, 1 for level II potions. */ - static short GetEntityEffectIntensity(short a_ItemDamage) - { - // Level II potion if the fifth lowest bit is set - return ((a_ItemDamage & 0x20) != 0) ? 1 : 0; - } - - - /** Returns the effect duration, in ticks, based on the potion's damage value */ - static int GetEntityEffectDuration(short a_ItemDamage) - { - // Base duration in ticks - int base = 0; - double TierCoeff = 1, ExtCoeff = 1, SplashCoeff = 1; - - switch (GetEntityEffectType(a_ItemDamage)) - { - case cEntityEffect::effRegeneration: - case cEntityEffect::effPoison: - { - base = 900; - break; - } - - case cEntityEffect::effSpeed: - case cEntityEffect::effFireResistance: - case cEntityEffect::effNightVision: - case cEntityEffect::effStrength: - case cEntityEffect::effWaterBreathing: - case cEntityEffect::effInvisibility: - { - base = 3600; - break; - } - - case cEntityEffect::effWeakness: - case cEntityEffect::effSlowness: - { - base = 1800; - break; - } - } - - // If potion is level II, half the duration. If not, stays the same - TierCoeff = (GetEntityEffectIntensity(a_ItemDamage) > 0) ? 0.5 : 1; - - // If potion is extended, multiply duration by 8/3. If not, stays the same - // Extended potion if sixth lowest bit is set - ExtCoeff = (a_ItemDamage & 0x40) ? (8.0 / 3.0) : 1; - - // If potion is splash potion, multiply duration by 3/4. If not, stays the same - SplashCoeff = IsPotionDrinkable(a_ItemDamage) ? 1 : 0.75; - - // Ref.: - // http://minecraft.gamepedia.com/Data_values#.22Tier.22_bit - // http://minecraft.gamepedia.com/Data_values#.22Extended_duration.22_bit - // http://minecraft.gamepedia.com/Data_values#.22Splash_potion.22_bit - - return (int)(base * TierCoeff * ExtCoeff * SplashCoeff); - } - - - /** Returns true if the potion with the given damage is drinkable */ - static bool IsPotionDrinkable(short a_ItemDamage) - { - // Drinkable potion if 13th lowest bit is set - // Ref.: http://minecraft.gamepedia.com/Potions#Data_value_table - return ((a_ItemDamage & 0x2000) != 0); - } - - // cItemHandler overrides: virtual bool IsDrinkable(short a_ItemDamage) override { // Drinkable potion if 13th lowest bit is set // Ref.: http://minecraft.gamepedia.com/Potions#Data_value_table - return IsPotionDrinkable(a_ItemDamage); + return cEntityEffect::IsPotionDrinkable(a_ItemDamage); } @@ -145,7 +31,7 @@ public: short PotionDamage = a_Item.m_ItemDamage; // Do not throw non-splash potions: - if (IsPotionDrinkable(PotionDamage)) + if (cEntityEffect::IsPotionDrinkable(PotionDamage)) { return false; } @@ -153,18 +39,8 @@ public: Vector3d Pos = a_Player->GetThrowStartPos(); Vector3d Speed = a_Player->GetLookVector() * 7; - cSplashPotionEntity * Projectile = new cSplashPotionEntity( - a_Player, Pos.x, Pos.y, Pos.z, Speed, - GetEntityEffectType(PotionDamage), cEntityEffect(GetEntityEffectDuration(PotionDamage), - GetEntityEffectIntensity(PotionDamage)), GetPotionColor(PotionDamage) - ); - if (Projectile == NULL) - { - return false; - } - if (!Projectile->Initialize(*a_World)) + if (a_World->CreateProjectile(Pos.x, Pos.y, Pos.z, cProjectileEntity::pkSplashPotion, a_Player, &a_Player->GetEquippedItem(), &Speed) < 0) { - delete Projectile; return false; } @@ -182,12 +58,14 @@ public: short PotionDamage = a_Item->m_ItemDamage; // Do not drink undrinkable potions: - if (!IsDrinkable(a_Item->m_ItemDamage)) + if (!cEntityEffect::IsPotionDrinkable(a_Item->m_ItemDamage)) { return false; } - a_Player->AddEntityEffect(GetEntityEffectType(PotionDamage), GetEntityEffectDuration(PotionDamage), GetEntityEffectIntensity(PotionDamage)); + a_Player->AddEntityEffect(cEntityEffect::GetPotionEffectType(PotionDamage), + cEntityEffect::GetPotionEffectDuration(PotionDamage), + cEntityEffect::GetPotionEffectIntensity(PotionDamage)); a_Player->GetInventory().RemoveOneEquippedItem(); a_Player->GetInventory().AddItem(E_ITEM_GLASS_BOTTLE); return true; diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index 2851647fe..023813769 100644 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -1670,7 +1670,7 @@ void cWSSAnvil::LoadArrowFromNBT(cEntityList & a_Entities, const cParsedNBT & a_ void cWSSAnvil::LoadSplashPotionFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_TagIdx) { - std::auto_ptr SplashPotion(new cSplashPotionEntity(NULL, 0, 0, 0, Vector3d(0, 0, 0), cEntityEffect::effNoEffect, cEntityEffect(), 0)); + std::auto_ptr SplashPotion(new cSplashPotionEntity(NULL, 0, 0, 0, Vector3d(0, 0, 0), cItem())); if (!LoadProjectileBaseFromNBT(*SplashPotion.get(), a_NBT, a_TagIdx)) { return; -- cgit v1.2.3 From 60d4a165140b3b1c5202c21fd197b61cae18077c Mon Sep 17 00:00:00 2001 From: archshift Date: Sun, 20 Jul 2014 13:11:06 -0700 Subject: Fixed indentations, removed redundant init code in SplashPotionEntity.cpp --- src/Entities/EntityEffect.cpp | 2 +- src/Entities/EntityEffect.h | 3 +-- src/Entities/SplashPotionEntity.cpp | 3 --- src/Items/ItemPotion.h | 8 +++++--- 4 files changed, 7 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/Entities/EntityEffect.cpp b/src/Entities/EntityEffect.cpp index f58738aef..fdcbe822e 100644 --- a/src/Entities/EntityEffect.cpp +++ b/src/Entities/EntityEffect.cpp @@ -38,7 +38,7 @@ cEntityEffect::eType cEntityEffect::GetPotionEffectType(short a_ItemDamage) case 0x0d: return cEntityEffect::effWaterBreathing; case 0x0e: return cEntityEffect::effInvisibility; - // No effect potions + // No effect potions case 0x00: case 0x07: case 0x0b: // Will be potion of leaping in 1.8 diff --git a/src/Entities/EntityEffect.h b/src/Entities/EntityEffect.h index 04e25e303..f9c1e4eb2 100644 --- a/src/Entities/EntityEffect.h +++ b/src/Entities/EntityEffect.h @@ -44,8 +44,7 @@ public: static cEntityEffect::eType GetPotionEffectType(short a_ItemDamage); - /** Retrieves the intensity level from the potion's damage value. - Returns 0 for level I potions, 1 for level II potions. */ + /** Retrieves the intensity level from the potion's damage value. Returns 0 for level I potions, 1 for level II potions. */ static short GetPotionEffectIntensity(short a_ItemDamage); diff --git a/src/Entities/SplashPotionEntity.cpp b/src/Entities/SplashPotionEntity.cpp index d874a86e1..ed16f060d 100644 --- a/src/Entities/SplashPotionEntity.cpp +++ b/src/Entities/SplashPotionEntity.cpp @@ -75,9 +75,6 @@ cSplashPotionEntity::cSplashPotionEntity( cItem a_Item ) : super(pkSplashPotion, a_Creator, a_X, a_Y, a_Z, 0.25, 0.25), - m_EntityEffectType(cEntityEffect::effNoEffect), - m_EntityEffect(cEntityEffect()), - m_PotionColor(0), m_DestroyTimer(-1) { SetSpeed(a_Speed); diff --git a/src/Items/ItemPotion.h b/src/Items/ItemPotion.h index cc974c8b9..24614cd8a 100644 --- a/src/Items/ItemPotion.h +++ b/src/Items/ItemPotion.h @@ -63,9 +63,11 @@ public: return false; } - a_Player->AddEntityEffect(cEntityEffect::GetPotionEffectType(PotionDamage), - cEntityEffect::GetPotionEffectDuration(PotionDamage), - cEntityEffect::GetPotionEffectIntensity(PotionDamage)); + a_Player->AddEntityEffect( + cEntityEffect::GetPotionEffectType(PotionDamage), + cEntityEffect::GetPotionEffectDuration(PotionDamage), + cEntityEffect::GetPotionEffectIntensity(PotionDamage) + ); a_Player->GetInventory().RemoveOneEquippedItem(); a_Player->GetInventory().AddItem(E_ITEM_GLASS_BOTTLE); return true; -- cgit v1.2.3 From 16e0beb5b6f6c4c103acfccc1940585513b5c258 Mon Sep 17 00:00:00 2001 From: archshift Date: Sat, 26 Jul 2014 03:55:40 -0700 Subject: SplashPotionEntity: Use `const cItem &` instead of a cItem --- src/Entities/SplashPotionEntity.cpp | 2 +- src/Entities/SplashPotionEntity.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/Entities/SplashPotionEntity.cpp b/src/Entities/SplashPotionEntity.cpp index ed16f060d..fd1a0179b 100644 --- a/src/Entities/SplashPotionEntity.cpp +++ b/src/Entities/SplashPotionEntity.cpp @@ -72,7 +72,7 @@ cSplashPotionEntity::cSplashPotionEntity( cEntity * a_Creator, double a_X, double a_Y, double a_Z, const Vector3d & a_Speed, - cItem a_Item + const cItem & a_Item ) : super(pkSplashPotion, a_Creator, a_X, a_Y, a_Z, 0.25, 0.25), m_DestroyTimer(-1) diff --git a/src/Entities/SplashPotionEntity.h b/src/Entities/SplashPotionEntity.h index db1672f9b..4afc5f204 100644 --- a/src/Entities/SplashPotionEntity.h +++ b/src/Entities/SplashPotionEntity.h @@ -29,7 +29,7 @@ public: cEntity * a_Creator, double a_X, double a_Y, double a_Z, const Vector3d & a_Speed, - cItem a_Item + const cItem & a_Item ); cEntityEffect::eType GetEntityEffectType(void) const { return m_EntityEffectType; } -- cgit v1.2.3