From 2185c72c2ca2d66b238d7d3234c173bd820d32ac Mon Sep 17 00:00:00 2001 From: archshift Date: Sat, 7 Jun 2014 16:32:37 -0700 Subject: Implemented drinkable potions, noeffect entity effect, Clears entity effects on death --- src/Items/ItemPotion.h | 137 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/Items/ItemPotion.h (limited to 'src/Items/ItemPotion.h') diff --git a/src/Items/ItemPotion.h b/src/Items/ItemPotion.h new file mode 100644 index 000000000..e34b251aa --- /dev/null +++ b/src/Items/ItemPotion.h @@ -0,0 +1,137 @@ + +#pragma once + +#include "../Entities/EntityEffects.h" + +class cItemPotionHandler: +public cItemHandler +{ + typedef cItemHandler super; + + cEntityEffect::eType GetEntityEffectType(short a_ItemDamage) + { + // Potion effect bits are different from entity effect values + // For reference: http://minecraft.gamepedia.com/Data_values#.22Potion_effect.22_bits + switch (a_ItemDamage & 15) + { + case 1: return cEntityEffect::efRegeneration; + case 2: return cEntityEffect::efSpeed; + case 3: return cEntityEffect::efFireResistance; + case 4: return cEntityEffect::efPoison; + case 5: return cEntityEffect::efInstantHealth; + case 6: return cEntityEffect::efNightVision; + case 8: return cEntityEffect::efWeakness; + case 9: return cEntityEffect::efStrength; + case 10: return cEntityEffect::efSlowness; + case 12: return cEntityEffect::efInstantDamage; + case 13: return cEntityEffect::efWaterBreathing; + case 14: return cEntityEffect::efInvisibility; + + // No effect potions + case 0: + case 7: + case 11: + case 15: + { + break; + } + } + + return cEntityEffect::efNoEffect; + } + + short GetEntityEffectIntensity(short a_ItemDamage) + { + // Level II potion if fifth bit is set + if (a_ItemDamage & 32) return 1; + else return 0; + } + + int GetEntityEffectDuration(short a_ItemDamage) + { + // Base duration in ticks + int base = 0; + double tier_multi = 1, ext_multi = 1, splash_multi = 1; + + switch (GetEntityEffectType(a_ItemDamage)) + { + case cEntityEffect::efRegeneration: + case cEntityEffect::efPoison: + { + base = 900; + break; + } + + case cEntityEffect::efSpeed: + case cEntityEffect::efFireResistance: + case cEntityEffect::efNightVision: + case cEntityEffect::efStrength: + case cEntityEffect::efWaterBreathing: + case cEntityEffect::efInvisibility: + { + base = 3600; + break; + } + + case cEntityEffect::efWeakness: + case cEntityEffect::efSlowness: + { + base = 1800; + break; + } + } + + // If potion is level 2, half the duration. If not, stays the same + tier_multi = 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 bit is set + ext_multi = a_ItemDamage & 64 ? (8.0/3.0) : 1; + + // If potion is splash potion, multiply duration by 3/4. If not, stays the same + splash_multi = !IsDrinkable(a_ItemDamage) ? 0.75 : 1; + + // For reference: 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 base * tier_multi * ext_multi * splash_multi; + } + + bool IsDrinkable(short a_ItemDamage) + { + // Drinkable potion if 13th bit is set + // For reference: http://minecraft.gamepedia.com/Potions#Data_value_table + return a_ItemDamage & 8192; + } + +public: + cItemPotionHandler(): + super(E_ITEM_POTIONS) + { + } + + virtual bool IsDrinkable(const cItem * a_Item) override + { + return IsDrinkable(a_Item->m_ItemDamage); + } + + virtual bool OnItemUse(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_Dir) override + { + // Called when potion is a splash potion + return true; + } + + virtual bool EatItem(cPlayer * a_Player, cItem * a_Item) override + { + // Called when potion is a drinkable potion + short potion_damage = a_Item->m_ItemDamage; + a_Player->AddEntityEffect(GetEntityEffectType(potion_damage), + cEntityEffect(GetEntityEffectDuration(potion_damage), + GetEntityEffectIntensity(potion_damage), + a_Player)); + a_Player->GetInventory().RemoveOneEquippedItem(); + a_Player->GetInventory().AddItem(E_ITEM_GLASS_BOTTLE); + return true; + } +}; -- cgit v1.2.3 From 58f35af6e71f11844b9c6c1d1ebd2d7390439cca Mon Sep 17 00:00:00 2001 From: archshift Date: Sat, 7 Jun 2014 21:56:01 -0700 Subject: Added splash potion functionality --- src/Items/ItemPotion.h | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'src/Items/ItemPotion.h') diff --git a/src/Items/ItemPotion.h b/src/Items/ItemPotion.h index e34b251aa..528268cfe 100644 --- a/src/Items/ItemPotion.h +++ b/src/Items/ItemPotion.h @@ -2,12 +2,18 @@ #pragma once #include "../Entities/EntityEffects.h" +#include "../Entities/SplashPotionEntity.h" class cItemPotionHandler: -public cItemHandler + public cItemHandler { typedef cItemHandler super; + int GetPotionName(short a_ItemDamage) + { + return a_ItemDamage & 63; + } + cEntityEffect::eType GetEntityEffectType(short a_ItemDamage) { // Potion effect bits are different from entity effect values @@ -118,6 +124,34 @@ public: virtual bool OnItemUse(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_Dir) override { + Vector3d Speed = a_Player->GetLookVector() * 10; + + short potion_damage = a_Item.m_ItemDamage; + cProjectileEntity * Projectile = new cSplashPotionEntity(a_Player, + (double)a_BlockX, + (double)a_BlockY, + (double)a_BlockZ, + &Speed, + GetEntityEffectType(potion_damage), + cEntityEffect(GetEntityEffectDuration(potion_damage), + GetEntityEffectIntensity(potion_damage), + a_Player), + GetPotionName(potion_damage)); + if (Projectile == NULL) + { + return false; + } + if (!Projectile->Initialize(*a_World)) + { + delete Projectile; + return false; + } + + if (!a_Player->IsGameModeCreative()) + { + a_Player->GetInventory().RemoveOneEquippedItem(); + } + // Called when potion is a splash potion return true; } -- cgit v1.2.3 From 73cea7065db458da7704917788ac80b75e042d6e Mon Sep 17 00:00:00 2001 From: archshift Date: Sun, 8 Jun 2014 03:27:22 -0700 Subject: Entity effect type: use 'eff' as a prefix instead of 'ef' --- src/Items/ItemPotion.h | 66 +++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 33 deletions(-) (limited to 'src/Items/ItemPotion.h') diff --git a/src/Items/ItemPotion.h b/src/Items/ItemPotion.h index 528268cfe..4c67e9dc7 100644 --- a/src/Items/ItemPotion.h +++ b/src/Items/ItemPotion.h @@ -20,18 +20,18 @@ class cItemPotionHandler: // For reference: http://minecraft.gamepedia.com/Data_values#.22Potion_effect.22_bits switch (a_ItemDamage & 15) { - case 1: return cEntityEffect::efRegeneration; - case 2: return cEntityEffect::efSpeed; - case 3: return cEntityEffect::efFireResistance; - case 4: return cEntityEffect::efPoison; - case 5: return cEntityEffect::efInstantHealth; - case 6: return cEntityEffect::efNightVision; - case 8: return cEntityEffect::efWeakness; - case 9: return cEntityEffect::efStrength; - case 10: return cEntityEffect::efSlowness; - case 12: return cEntityEffect::efInstantDamage; - case 13: return cEntityEffect::efWaterBreathing; - case 14: return cEntityEffect::efInvisibility; + case 1: return cEntityEffect::effRegeneration; + case 2: return cEntityEffect::effSpeed; + case 3: return cEntityEffect::effFireResistance; + case 4: return cEntityEffect::effPoison; + case 5: return cEntityEffect::effInstantHealth; + case 6: return cEntityEffect::effNightVision; + case 8: return cEntityEffect::effWeakness; + case 9: return cEntityEffect::effStrength; + case 10: return cEntityEffect::effSlowness; + case 12: return cEntityEffect::effInstantDamage; + case 13: return cEntityEffect::effWaterBreathing; + case 14: return cEntityEffect::effInvisibility; // No effect potions case 0: @@ -43,7 +43,7 @@ class cItemPotionHandler: } } - return cEntityEffect::efNoEffect; + return cEntityEffect::effNoEffect; } short GetEntityEffectIntensity(short a_ItemDamage) @@ -61,26 +61,26 @@ class cItemPotionHandler: switch (GetEntityEffectType(a_ItemDamage)) { - case cEntityEffect::efRegeneration: - case cEntityEffect::efPoison: + case cEntityEffect::effRegeneration: + case cEntityEffect::effPoison: { base = 900; break; } - case cEntityEffect::efSpeed: - case cEntityEffect::efFireResistance: - case cEntityEffect::efNightVision: - case cEntityEffect::efStrength: - case cEntityEffect::efWaterBreathing: - case cEntityEffect::efInvisibility: + case cEntityEffect::effSpeed: + case cEntityEffect::effFireResistance: + case cEntityEffect::effNightVision: + case cEntityEffect::effStrength: + case cEntityEffect::effWaterBreathing: + case cEntityEffect::effInvisibility: { base = 3600; break; } - case cEntityEffect::efWeakness: - case cEntityEffect::efSlowness: + case cEntityEffect::effWeakness: + case cEntityEffect::effSlowness: { base = 1800; break; @@ -127,16 +127,16 @@ public: Vector3d Speed = a_Player->GetLookVector() * 10; short potion_damage = a_Item.m_ItemDamage; - cProjectileEntity * Projectile = new cSplashPotionEntity(a_Player, - (double)a_BlockX, - (double)a_BlockY, - (double)a_BlockZ, - &Speed, - GetEntityEffectType(potion_damage), - cEntityEffect(GetEntityEffectDuration(potion_damage), - GetEntityEffectIntensity(potion_damage), - a_Player), - GetPotionName(potion_damage)); + cSplashPotionEntity * Projectile = new cSplashPotionEntity(a_Player, + (double)a_BlockX, + (double)a_BlockY, + (double)a_BlockZ, + &Speed, + GetEntityEffectType(potion_damage), + cEntityEffect(GetEntityEffectDuration(potion_damage), + GetEntityEffectIntensity(potion_damage), + a_Player), + GetPotionName(potion_damage)); if (Projectile == NULL) { return false; -- cgit v1.2.3 From a1a8b7c0eee2189dfc129eb366edb43a315749f9 Mon Sep 17 00:00:00 2001 From: archshift Date: Sun, 8 Jun 2014 16:31:18 -0700 Subject: Splash potion: Adjusted speed, fixed spawn position --- src/Items/ItemPotion.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'src/Items/ItemPotion.h') diff --git a/src/Items/ItemPotion.h b/src/Items/ItemPotion.h index 4c67e9dc7..029bb52cd 100644 --- a/src/Items/ItemPotion.h +++ b/src/Items/ItemPotion.h @@ -124,14 +124,11 @@ public: virtual bool OnItemUse(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_Dir) override { - Vector3d Speed = a_Player->GetLookVector() * 10; + Vector3d Pos = a_Player->GetThrowStartPos(); + Vector3d Speed = a_Player->GetLookVector() * 7; short potion_damage = a_Item.m_ItemDamage; - cSplashPotionEntity * Projectile = new cSplashPotionEntity(a_Player, - (double)a_BlockX, - (double)a_BlockY, - (double)a_BlockZ, - &Speed, + cSplashPotionEntity *Projectile = new cSplashPotionEntity(a_Player, Pos.x, Pos.y, Pos.z, &Speed, GetEntityEffectType(potion_damage), cEntityEffect(GetEntityEffectDuration(potion_damage), GetEntityEffectIntensity(potion_damage), -- cgit v1.2.3 From 3766ac96d77329c679d01d1ab1a846384acab42f Mon Sep 17 00:00:00 2001 From: archshift Date: Sun, 8 Jun 2014 17:06:15 -0700 Subject: ItemHandler: changed IsDrinkable() to take a short argument --- src/Items/ItemPotion.h | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'src/Items/ItemPotion.h') diff --git a/src/Items/ItemPotion.h b/src/Items/ItemPotion.h index 029bb52cd..c2441fa48 100644 --- a/src/Items/ItemPotion.h +++ b/src/Items/ItemPotion.h @@ -104,22 +104,17 @@ class cItemPotionHandler: return base * tier_multi * ext_multi * splash_multi; } - bool IsDrinkable(short a_ItemDamage) - { - // Drinkable potion if 13th bit is set - // For reference: http://minecraft.gamepedia.com/Potions#Data_value_table - return a_ItemDamage & 8192; - } - public: cItemPotionHandler(): super(E_ITEM_POTIONS) { } - virtual bool IsDrinkable(const cItem * a_Item) override + virtual bool IsDrinkable(short a_ItemDamage) override { - return IsDrinkable(a_Item->m_ItemDamage); + // Drinkable potion if 13th bit is set + // For reference: http://minecraft.gamepedia.com/Potions#Data_value_table + return a_ItemDamage & 8192; } virtual bool OnItemUse(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_Dir) override -- cgit v1.2.3 From 68011a004a83adc793f0df563cc924c5a2b7dddc Mon Sep 17 00:00:00 2001 From: archshift Date: Sun, 8 Jun 2014 17:17:30 -0700 Subject: Removed long function wrapping --- src/Items/ItemPotion.h | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'src/Items/ItemPotion.h') diff --git a/src/Items/ItemPotion.h b/src/Items/ItemPotion.h index c2441fa48..40748d71c 100644 --- a/src/Items/ItemPotion.h +++ b/src/Items/ItemPotion.h @@ -123,12 +123,7 @@ public: Vector3d Speed = a_Player->GetLookVector() * 7; short potion_damage = a_Item.m_ItemDamage; - cSplashPotionEntity *Projectile = new cSplashPotionEntity(a_Player, Pos.x, Pos.y, Pos.z, &Speed, - GetEntityEffectType(potion_damage), - cEntityEffect(GetEntityEffectDuration(potion_damage), - GetEntityEffectIntensity(potion_damage), - a_Player), - GetPotionName(potion_damage)); + cSplashPotionEntity *Projectile = new cSplashPotionEntity(a_Player, Pos.x, Pos.y, Pos.z, &Speed, GetEntityEffectType(potion_damage), cEntityEffect(GetEntityEffectDuration(potion_damage), GetEntityEffectIntensity(potion_damage), a_Player), GetPotionName(potion_damage)); if (Projectile == NULL) { return false; @@ -152,10 +147,7 @@ public: { // Called when potion is a drinkable potion short potion_damage = a_Item->m_ItemDamage; - a_Player->AddEntityEffect(GetEntityEffectType(potion_damage), - cEntityEffect(GetEntityEffectDuration(potion_damage), - GetEntityEffectIntensity(potion_damage), - a_Player)); + a_Player->AddEntityEffect(GetEntityEffectType(potion_damage), cEntityEffect(GetEntityEffectDuration(potion_damage), GetEntityEffectIntensity(potion_damage), a_Player)); a_Player->GetInventory().RemoveOneEquippedItem(); a_Player->GetInventory().AddItem(E_ITEM_GLASS_BOTTLE); return true; -- cgit v1.2.3 From 5b2b6e06150b6299d1e19374be092c0858b0e3a8 Mon Sep 17 00:00:00 2001 From: archshift Date: Thu, 12 Jun 2014 19:50:02 -0700 Subject: Pawn: renamed HandleEntityEffects to HandleEntityEffect Exported entity effect functions for ToLua and documented them in APIDesc.lua --- src/Items/ItemPotion.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Items/ItemPotion.h') diff --git a/src/Items/ItemPotion.h b/src/Items/ItemPotion.h index 40748d71c..2c5760e34 100644 --- a/src/Items/ItemPotion.h +++ b/src/Items/ItemPotion.h @@ -128,7 +128,7 @@ public: { return false; } - if (!Projectile->Initialize(*a_World)) + if (!Projectile->Initialize(a_World)) { delete Projectile; return false; -- cgit v1.2.3 From 045ae2ef2c0d72b4902fa5151aad095823da9300 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 13 Jun 2014 09:49:42 +0200 Subject: Fixed MSVC compilation. --- src/Items/ItemPotion.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/Items/ItemPotion.h') diff --git a/src/Items/ItemPotion.h b/src/Items/ItemPotion.h index 2c5760e34..200c13cab 100644 --- a/src/Items/ItemPotion.h +++ b/src/Items/ItemPotion.h @@ -57,7 +57,7 @@ class cItemPotionHandler: { // Base duration in ticks int base = 0; - double tier_multi = 1, ext_multi = 1, splash_multi = 1; + double TierCoeff = 1, ExtCoeff = 1, SplashCoeff = 1; switch (GetEntityEffectType(a_ItemDamage)) { @@ -88,20 +88,20 @@ class cItemPotionHandler: } // If potion is level 2, half the duration. If not, stays the same - tier_multi = GetEntityEffectIntensity(a_ItemDamage) > 0 ? 0.5 : 1; + 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 bit is set - ext_multi = a_ItemDamage & 64 ? (8.0/3.0) : 1; + ExtCoeff = (a_ItemDamage & 64) ? (8.0/3.0) : 1; // If potion is splash potion, multiply duration by 3/4. If not, stays the same - splash_multi = !IsDrinkable(a_ItemDamage) ? 0.75 : 1; + SplashCoeff = IsDrinkable(a_ItemDamage) ? 1 : 0.75; // For reference: 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 base * tier_multi * ext_multi * splash_multi; + return (int)(base * TierCoeff * ExtCoeff * SplashCoeff); } public: @@ -114,7 +114,7 @@ public: { // Drinkable potion if 13th bit is set // For reference: http://minecraft.gamepedia.com/Potions#Data_value_table - return a_ItemDamage & 8192; + return ((a_ItemDamage & 8192) != 0); } virtual bool OnItemUse(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_Dir) override @@ -128,7 +128,7 @@ public: { return false; } - if (!Projectile->Initialize(a_World)) + if (!Projectile->Initialize(*a_World)) { delete Projectile; return false; -- cgit v1.2.3 From e289fe4dd7372a029ba85722e3ce99991e9d1d6b Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 13 Jun 2014 11:04:16 +0200 Subject: Changed the AddEntityEffect() params for easier calls. --- src/Items/ItemPotion.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/Items/ItemPotion.h') diff --git a/src/Items/ItemPotion.h b/src/Items/ItemPotion.h index 200c13cab..70a926cad 100644 --- a/src/Items/ItemPotion.h +++ b/src/Items/ItemPotion.h @@ -146,8 +146,7 @@ public: virtual bool EatItem(cPlayer * a_Player, cItem * a_Item) override { // Called when potion is a drinkable potion - short potion_damage = a_Item->m_ItemDamage; - a_Player->AddEntityEffect(GetEntityEffectType(potion_damage), cEntityEffect(GetEntityEffectDuration(potion_damage), GetEntityEffectIntensity(potion_damage), a_Player)); + a_Player->AddEntityEffect(GetEntityEffectType(a_Item->m_ItemDamage), GetEntityEffectDuration(a_Item->m_ItemDamage), GetEntityEffectIntensity(a_Item->m_ItemDamage)); a_Player->GetInventory().RemoveOneEquippedItem(); a_Player->GetInventory().AddItem(E_ITEM_GLASS_BOTTLE); return true; -- cgit v1.2.3 From 9e8361976b6b0dc4c62ef48a4744ba1f59fe4346 Mon Sep 17 00:00:00 2001 From: archshift Date: Fri, 13 Jun 2014 02:41:43 -0700 Subject: Entity Effects: Clarified user, added it to AddEntityEffect Added second AddEntityEffect with a pass-by-value of the class. --- src/Items/ItemPotion.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Items/ItemPotion.h') diff --git a/src/Items/ItemPotion.h b/src/Items/ItemPotion.h index 70a926cad..853ed53a8 100644 --- a/src/Items/ItemPotion.h +++ b/src/Items/ItemPotion.h @@ -146,7 +146,7 @@ public: virtual bool EatItem(cPlayer * a_Player, cItem * a_Item) override { // Called when potion is a drinkable potion - a_Player->AddEntityEffect(GetEntityEffectType(a_Item->m_ItemDamage), GetEntityEffectDuration(a_Item->m_ItemDamage), GetEntityEffectIntensity(a_Item->m_ItemDamage)); + a_Player->AddEntityEffect(GetEntityEffectType(a_Item->m_ItemDamage), GetEntityEffectDuration(a_Item->m_ItemDamage), GetEntityEffectIntensity(a_Item->m_ItemDamage), a_Player); a_Player->GetInventory().RemoveOneEquippedItem(); a_Player->GetInventory().AddItem(E_ITEM_GLASS_BOTTLE); return true; -- cgit v1.2.3 From f5529e544cf8350daf8a20bb8d997f85ee2824f7 Mon Sep 17 00:00:00 2001 From: archshift Date: Mon, 16 Jun 2014 20:22:17 -0700 Subject: EntityEffects.x -> EntityEffect.x, Object-Oriented effects Changed effect map to take a pointer of the effect as a result. --- src/Items/ItemPotion.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Items/ItemPotion.h') diff --git a/src/Items/ItemPotion.h b/src/Items/ItemPotion.h index 853ed53a8..43b9f280d 100644 --- a/src/Items/ItemPotion.h +++ b/src/Items/ItemPotion.h @@ -1,7 +1,7 @@ #pragma once -#include "../Entities/EntityEffects.h" +#include "../Entities/EntityEffect.h" #include "../Entities/SplashPotionEntity.h" class cItemPotionHandler: -- cgit v1.2.3 From 4e6395d6ff9f34edb4dd36dc1f8e845c56b499f4 Mon Sep 17 00:00:00 2001 From: archshift Date: Fri, 11 Jul 2014 17:27:29 -0700 Subject: For now, removed creator member from Entity Effect for pointer safety --- src/Items/ItemPotion.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/Items/ItemPotion.h') diff --git a/src/Items/ItemPotion.h b/src/Items/ItemPotion.h index 43b9f280d..5badeda94 100644 --- a/src/Items/ItemPotion.h +++ b/src/Items/ItemPotion.h @@ -123,7 +123,7 @@ public: Vector3d Speed = a_Player->GetLookVector() * 7; short potion_damage = a_Item.m_ItemDamage; - cSplashPotionEntity *Projectile = new cSplashPotionEntity(a_Player, Pos.x, Pos.y, Pos.z, &Speed, GetEntityEffectType(potion_damage), cEntityEffect(GetEntityEffectDuration(potion_damage), GetEntityEffectIntensity(potion_damage), a_Player), GetPotionName(potion_damage)); + cSplashPotionEntity *Projectile = new cSplashPotionEntity(a_Player, Pos.x, Pos.y, Pos.z, &Speed, GetEntityEffectType(potion_damage), cEntityEffect(GetEntityEffectDuration(potion_damage), GetEntityEffectIntensity(potion_damage)), GetPotionName(potion_damage)); if (Projectile == NULL) { return false; @@ -146,7 +146,7 @@ public: virtual bool EatItem(cPlayer * a_Player, cItem * a_Item) override { // Called when potion is a drinkable potion - a_Player->AddEntityEffect(GetEntityEffectType(a_Item->m_ItemDamage), GetEntityEffectDuration(a_Item->m_ItemDamage), GetEntityEffectIntensity(a_Item->m_ItemDamage), a_Player); + a_Player->AddEntityEffect(GetEntityEffectType(a_Item->m_ItemDamage), GetEntityEffectDuration(a_Item->m_ItemDamage), GetEntityEffectIntensity(a_Item->m_ItemDamage)); a_Player->GetInventory().RemoveOneEquippedItem(); a_Player->GetInventory().AddItem(E_ITEM_GLASS_BOTTLE); return true; -- cgit v1.2.3 From 061010288a99fd11f91bf713ac68068c57f79be7 Mon Sep 17 00:00:00 2001 From: archshift Date: Mon, 14 Jul 2014 13:46:15 -0700 Subject: Readability and clarity changes --- src/Items/ItemPotion.h | 75 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 30 deletions(-) (limited to 'src/Items/ItemPotion.h') diff --git a/src/Items/ItemPotion.h b/src/Items/ItemPotion.h index 5badeda94..b72499431 100644 --- a/src/Items/ItemPotion.h +++ b/src/Items/ItemPotion.h @@ -11,33 +11,35 @@ class cItemPotionHandler: int GetPotionName(short a_ItemDamage) { - return a_ItemDamage & 63; + // First six bits (least significant) + return a_ItemDamage & 0x3F; } cEntityEffect::eType GetEntityEffectType(short a_ItemDamage) { + // First four bits (least significant) // Potion effect bits are different from entity effect values // For reference: http://minecraft.gamepedia.com/Data_values#.22Potion_effect.22_bits - switch (a_ItemDamage & 15) + switch (a_ItemDamage & 0xF) { - case 1: return cEntityEffect::effRegeneration; - case 2: return cEntityEffect::effSpeed; - case 3: return cEntityEffect::effFireResistance; - case 4: return cEntityEffect::effPoison; - case 5: return cEntityEffect::effInstantHealth; - case 6: return cEntityEffect::effNightVision; - case 8: return cEntityEffect::effWeakness; - case 9: return cEntityEffect::effStrength; - case 10: return cEntityEffect::effSlowness; - case 12: return cEntityEffect::effInstantDamage; - case 13: return cEntityEffect::effWaterBreathing; - case 14: return cEntityEffect::effInvisibility; + case 0x1: return cEntityEffect::effRegeneration; + case 0x2: return cEntityEffect::effSpeed; + case 0x3: return cEntityEffect::effFireResistance; + case 0x4: return cEntityEffect::effPoison; + case 0x5: return cEntityEffect::effInstantHealth; + case 0x6: return cEntityEffect::effNightVision; + case 0x8: return cEntityEffect::effWeakness; + case 0x9: return cEntityEffect::effStrength; + case 0xA: return cEntityEffect::effSlowness; + case 0xC: return cEntityEffect::effInstantDamage; + case 0xD: return cEntityEffect::effWaterBreathing; + case 0xE: return cEntityEffect::effInvisibility; // No effect potions - case 0: - case 7: - case 11: - case 15: + case 0x0: + case 0x7: + case 0xB: // Will be potion of leaping in 1.8 + case 0xF: { break; } @@ -48,9 +50,8 @@ class cItemPotionHandler: short GetEntityEffectIntensity(short a_ItemDamage) { - // Level II potion if fifth bit is set - if (a_ItemDamage & 32) return 1; - else return 0; + // Level II potion if fifth bit (from zero) is set + return (a_ItemDamage & 0x20) ? 1 : 0; } int GetEntityEffectDuration(short a_ItemDamage) @@ -91,8 +92,8 @@ class cItemPotionHandler: 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 bit is set - ExtCoeff = (a_ItemDamage & 64) ? (8.0/3.0) : 1; + // Extended potion if sixth bit (from zero) 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 = IsDrinkable(a_ItemDamage) ? 1 : 0.75; @@ -112,18 +113,26 @@ public: virtual bool IsDrinkable(short a_ItemDamage) override { - // Drinkable potion if 13th bit is set + // Drinkable potion if 13th bit (from zero) is set // For reference: http://minecraft.gamepedia.com/Potions#Data_value_table - return ((a_ItemDamage & 8192) != 0); + return ((a_ItemDamage & 0x2000) != 0); } virtual bool OnItemUse(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_Dir) override { + short PotionDamage = a_Item.m_ItemDamage; + + // Only called when potion is a splash potion + if (IsDrinkable(PotionDamage)) + { + return false; + } + Vector3d Pos = a_Player->GetThrowStartPos(); Vector3d Speed = a_Player->GetLookVector() * 7; - short potion_damage = a_Item.m_ItemDamage; - cSplashPotionEntity *Projectile = new cSplashPotionEntity(a_Player, Pos.x, Pos.y, Pos.z, &Speed, GetEntityEffectType(potion_damage), cEntityEffect(GetEntityEffectDuration(potion_damage), GetEntityEffectIntensity(potion_damage)), GetPotionName(potion_damage)); + cSplashPotionEntity *Projectile = new cSplashPotionEntity(a_Player, Pos.x, Pos.y, Pos.z, &Speed, GetEntityEffectType(PotionDamage), cEntityEffect(GetEntityEffectDuration(PotionDamage), GetEntityEffectIntensity(PotionDamage)), GetPotionName(PotionDamage)); + if (Projectile == NULL) { return false; @@ -139,14 +148,20 @@ public: a_Player->GetInventory().RemoveOneEquippedItem(); } - // Called when potion is a splash potion return true; } virtual bool EatItem(cPlayer * a_Player, cItem * a_Item) override { - // Called when potion is a drinkable potion - a_Player->AddEntityEffect(GetEntityEffectType(a_Item->m_ItemDamage), GetEntityEffectDuration(a_Item->m_ItemDamage), GetEntityEffectIntensity(a_Item->m_ItemDamage)); + short PotionDamage = a_Item->m_ItemDamage; + + // Only called when potion is a drinkable potion + if (!IsDrinkable(a_Item->m_ItemDamage)) + { + return false; + } + + a_Player->AddEntityEffect(GetEntityEffectType(PotionDamage), GetEntityEffectDuration(PotionDamage), GetEntityEffectIntensity(PotionDamage)); a_Player->GetInventory().RemoveOneEquippedItem(); a_Player->GetInventory().AddItem(E_ITEM_GLASS_BOTTLE); return true; -- cgit v1.2.3 From 64c8b0d51b0ee8323dded6d22ac3b4754daa15d2 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 17 Jul 2014 10:51:44 +0200 Subject: Reformatted cItemPotionHandler. --- src/Items/ItemPotion.h | 124 ++++++++++++++++++++++++++++++------------------- 1 file changed, 77 insertions(+), 47 deletions(-) (limited to 'src/Items/ItemPotion.h') diff --git a/src/Items/ItemPotion.h b/src/Items/ItemPotion.h index b72499431..f3afbf99b 100644 --- a/src/Items/ItemPotion.h +++ b/src/Items/ItemPotion.h @@ -9,52 +9,67 @@ class cItemPotionHandler: { typedef cItemHandler super; - int GetPotionName(short a_ItemDamage) +public: + + cItemPotionHandler(): + super(E_ITEM_POTION) + { + } + + + /** Returns the potion particle type (used by the client for visuals), based on the potion's damage value */ + static int GetPotionParticleType(short a_ItemDamage) { - // First six bits (least significant) - return a_ItemDamage & 0x3F; + // Lowest six bits + return (a_ItemDamage & 0x3f); } - cEntityEffect::eType GetEntityEffectType(short a_ItemDamage) + + /** Translates the potion's damage value into the entity effect that the potion gives */ + static cEntityEffect::eType GetEntityEffectType(short a_ItemDamage) { - // First four bits (least significant) + // 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 & 0xF) + switch (a_ItemDamage & 0x0f) { - case 0x1: return cEntityEffect::effRegeneration; - case 0x2: return cEntityEffect::effSpeed; - case 0x3: return cEntityEffect::effFireResistance; - case 0x4: return cEntityEffect::effPoison; - case 0x5: return cEntityEffect::effInstantHealth; - case 0x6: return cEntityEffect::effNightVision; - case 0x8: return cEntityEffect::effWeakness; - case 0x9: return cEntityEffect::effStrength; - case 0xA: return cEntityEffect::effSlowness; - case 0xC: return cEntityEffect::effInstantDamage; - case 0xD: return cEntityEffect::effWaterBreathing; - case 0xE: return cEntityEffect::effInvisibility; + 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 0x0: - case 0x7: - case 0xB: // Will be potion of leaping in 1.8 - case 0xF: + case 0x00: + case 0x07: + case 0x0b: // Will be potion of leaping in 1.8 + case 0x0f: { break; } } - return cEntityEffect::effNoEffect; } - - short GetEntityEffectIntensity(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 GetEntityEffectIntensity(short a_ItemDamage) { - // Level II potion if fifth bit (from zero) is set - return (a_ItemDamage & 0x20) ? 1 : 0; + // Level II potion if the fifth lowest bit is set + return ((a_ItemDamage & 0x20) != 0) ? 1 : 0; } - int GetEntityEffectDuration(short a_ItemDamage) + + /** 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; @@ -88,42 +103,49 @@ class cItemPotionHandler: } } - // If potion is level 2, half the duration. If not, stays the same + // 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 bit (from zero) is set - ExtCoeff = (a_ItemDamage & 0x40) ? (8.0/3.0) : 1; + // 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 = IsDrinkable(a_ItemDamage) ? 1 : 0.75; + SplashCoeff = IsPotionDrinkable(a_ItemDamage) ? 1 : 0.75; - // For reference: 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 + // 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); } - -public: - cItemPotionHandler(): - super(E_ITEM_POTIONS) + + + /** 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 bit (from zero) is set - // For reference: http://minecraft.gamepedia.com/Potions#Data_value_table - return ((a_ItemDamage & 0x2000) != 0); + // Drinkable potion if 13th lowest bit is set + // Ref.: http://minecraft.gamepedia.com/Potions#Data_value_table + return IsPotionDrinkable(a_ItemDamage); } + virtual bool OnItemUse(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_Dir) override { short PotionDamage = a_Item.m_ItemDamage; - // Only called when potion is a splash potion - if (IsDrinkable(PotionDamage)) + // Do not throw non-splash potions: + if (IsPotionDrinkable(PotionDamage)) { return false; } @@ -131,8 +153,11 @@ 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)), GetPotionName(PotionDamage)); - + cSplashPotionEntity * Projectile = new cSplashPotionEntity( + a_Player, Pos.x, Pos.y, Pos.z, Speed, + GetEntityEffectType(PotionDamage), cEntityEffect(GetEntityEffectDuration(PotionDamage), + GetEntityEffectIntensity(PotionDamage)), GetPotionParticleType(PotionDamage) + ); if (Projectile == NULL) { return false; @@ -151,11 +176,12 @@ public: return true; } + virtual bool EatItem(cPlayer * a_Player, cItem * a_Item) override { short PotionDamage = a_Item->m_ItemDamage; - // Only called when potion is a drinkable potion + // Do not drink undrinkable potions: if (!IsDrinkable(a_Item->m_ItemDamage)) { return false; @@ -167,3 +193,7 @@ public: return true; } }; + + + + -- cgit v1.2.3