2014-06-07 19:32:37 -04:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2014-06-16 23:22:17 -04:00
|
|
|
#include "../Entities/EntityEffect.h"
|
2014-06-08 00:56:01 -04:00
|
|
|
#include "../Entities/SplashPotionEntity.h"
|
2014-06-07 19:32:37 -04:00
|
|
|
|
|
|
|
class cItemPotionHandler:
|
2014-06-08 00:56:01 -04:00
|
|
|
public cItemHandler
|
2014-06-07 19:32:37 -04:00
|
|
|
{
|
|
|
|
typedef cItemHandler super;
|
|
|
|
|
2014-07-17 04:51:44 -04:00
|
|
|
public:
|
|
|
|
|
|
|
|
cItemPotionHandler():
|
|
|
|
super(E_ITEM_POTION)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-19 05:51:39 -04:00
|
|
|
/** Returns the potion color (used by the client for visuals), based on the potion's damage value */
|
|
|
|
static int GetPotionColor(short a_ItemDamage)
|
2014-06-08 00:56:01 -04:00
|
|
|
{
|
2014-07-17 04:51:44 -04:00
|
|
|
// Lowest six bits
|
|
|
|
return (a_ItemDamage & 0x3f);
|
2014-06-08 00:56:01 -04:00
|
|
|
}
|
|
|
|
|
2014-07-17 04:51:44 -04:00
|
|
|
|
|
|
|
/** Translates the potion's damage value into the entity effect that the potion gives */
|
|
|
|
static cEntityEffect::eType GetEntityEffectType(short a_ItemDamage)
|
2014-06-07 19:32:37 -04:00
|
|
|
{
|
2014-07-17 04:51:44 -04:00
|
|
|
// Lowest four bits
|
2014-06-07 19:32:37 -04:00
|
|
|
// Potion effect bits are different from entity effect values
|
|
|
|
// For reference: http://minecraft.gamepedia.com/Data_values#.22Potion_effect.22_bits
|
2014-07-17 04:51:44 -04:00
|
|
|
switch (a_ItemDamage & 0x0f)
|
2014-06-07 19:32:37 -04:00
|
|
|
{
|
2014-07-17 04:51:44 -04:00
|
|
|
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;
|
2014-06-07 19:32:37 -04:00
|
|
|
|
|
|
|
// No effect potions
|
2014-07-17 04:51:44 -04:00
|
|
|
case 0x00:
|
|
|
|
case 0x07:
|
|
|
|
case 0x0b: // Will be potion of leaping in 1.8
|
|
|
|
case 0x0f:
|
2014-06-07 19:32:37 -04:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-08 06:27:22 -04:00
|
|
|
return cEntityEffect::effNoEffect;
|
2014-06-07 19:32:37 -04:00
|
|
|
}
|
2014-07-17 04:51:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
/** 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)
|
2014-06-07 19:32:37 -04:00
|
|
|
{
|
2014-07-17 04:51:44 -04:00
|
|
|
// Level II potion if the fifth lowest bit is set
|
|
|
|
return ((a_ItemDamage & 0x20) != 0) ? 1 : 0;
|
2014-06-07 19:32:37 -04:00
|
|
|
}
|
|
|
|
|
2014-07-17 04:51:44 -04:00
|
|
|
|
|
|
|
/** Returns the effect duration, in ticks, based on the potion's damage value */
|
|
|
|
static int GetEntityEffectDuration(short a_ItemDamage)
|
2014-06-07 19:32:37 -04:00
|
|
|
{
|
|
|
|
// Base duration in ticks
|
|
|
|
int base = 0;
|
2014-06-13 03:49:42 -04:00
|
|
|
double TierCoeff = 1, ExtCoeff = 1, SplashCoeff = 1;
|
2014-06-07 19:32:37 -04:00
|
|
|
|
|
|
|
switch (GetEntityEffectType(a_ItemDamage))
|
|
|
|
{
|
2014-06-08 06:27:22 -04:00
|
|
|
case cEntityEffect::effRegeneration:
|
|
|
|
case cEntityEffect::effPoison:
|
2014-06-07 19:32:37 -04:00
|
|
|
{
|
|
|
|
base = 900;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-06-08 06:27:22 -04:00
|
|
|
case cEntityEffect::effSpeed:
|
|
|
|
case cEntityEffect::effFireResistance:
|
|
|
|
case cEntityEffect::effNightVision:
|
|
|
|
case cEntityEffect::effStrength:
|
|
|
|
case cEntityEffect::effWaterBreathing:
|
|
|
|
case cEntityEffect::effInvisibility:
|
2014-06-07 19:32:37 -04:00
|
|
|
{
|
|
|
|
base = 3600;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-06-08 06:27:22 -04:00
|
|
|
case cEntityEffect::effWeakness:
|
|
|
|
case cEntityEffect::effSlowness:
|
2014-06-07 19:32:37 -04:00
|
|
|
{
|
|
|
|
base = 1800;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-17 04:51:44 -04:00
|
|
|
// If potion is level II, half the duration. If not, stays the same
|
2014-06-13 03:49:42 -04:00
|
|
|
TierCoeff = (GetEntityEffectIntensity(a_ItemDamage) > 0) ? 0.5 : 1;
|
2014-06-07 19:32:37 -04:00
|
|
|
|
|
|
|
// If potion is extended, multiply duration by 8/3. If not, stays the same
|
2014-07-17 04:51:44 -04:00
|
|
|
// Extended potion if sixth lowest bit is set
|
|
|
|
ExtCoeff = (a_ItemDamage & 0x40) ? (8.0 / 3.0) : 1;
|
2014-06-07 19:32:37 -04:00
|
|
|
|
|
|
|
// If potion is splash potion, multiply duration by 3/4. If not, stays the same
|
2014-07-17 04:51:44 -04:00
|
|
|
SplashCoeff = IsPotionDrinkable(a_ItemDamage) ? 1 : 0.75;
|
2014-06-07 19:32:37 -04:00
|
|
|
|
2014-07-17 04:51:44 -04:00
|
|
|
// 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
|
2014-06-07 19:32:37 -04:00
|
|
|
|
2014-06-13 03:49:42 -04:00
|
|
|
return (int)(base * TierCoeff * ExtCoeff * SplashCoeff);
|
2014-06-07 19:32:37 -04:00
|
|
|
}
|
2014-07-17 04:51:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
/** Returns true if the potion with the given damage is drinkable */
|
|
|
|
static bool IsPotionDrinkable(short a_ItemDamage)
|
2014-06-07 19:32:37 -04:00
|
|
|
{
|
2014-07-17 04:51:44 -04:00
|
|
|
// Drinkable potion if 13th lowest bit is set
|
|
|
|
// Ref.: http://minecraft.gamepedia.com/Potions#Data_value_table
|
|
|
|
return ((a_ItemDamage & 0x2000) != 0);
|
2014-06-07 19:32:37 -04:00
|
|
|
}
|
|
|
|
|
2014-07-17 04:51:44 -04:00
|
|
|
|
|
|
|
// cItemHandler overrides:
|
2014-06-08 20:06:15 -04:00
|
|
|
virtual bool IsDrinkable(short a_ItemDamage) override
|
2014-06-07 19:32:37 -04:00
|
|
|
{
|
2014-07-17 04:51:44 -04:00
|
|
|
// Drinkable potion if 13th lowest bit is set
|
|
|
|
// Ref.: http://minecraft.gamepedia.com/Potions#Data_value_table
|
|
|
|
return IsPotionDrinkable(a_ItemDamage);
|
2014-06-07 19:32:37 -04:00
|
|
|
}
|
2014-07-17 04:51:44 -04:00
|
|
|
|
2014-06-07 19:32:37 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2014-07-14 16:46:15 -04:00
|
|
|
short PotionDamage = a_Item.m_ItemDamage;
|
|
|
|
|
2014-07-17 04:51:44 -04:00
|
|
|
// Do not throw non-splash potions:
|
|
|
|
if (IsPotionDrinkable(PotionDamage))
|
2014-07-14 16:46:15 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-08 19:31:18 -04:00
|
|
|
Vector3d Pos = a_Player->GetThrowStartPos();
|
|
|
|
Vector3d Speed = a_Player->GetLookVector() * 7;
|
2014-06-08 00:56:01 -04:00
|
|
|
|
2014-07-17 04:51:44 -04:00
|
|
|
cSplashPotionEntity * Projectile = new cSplashPotionEntity(
|
|
|
|
a_Player, Pos.x, Pos.y, Pos.z, Speed,
|
|
|
|
GetEntityEffectType(PotionDamage), cEntityEffect(GetEntityEffectDuration(PotionDamage),
|
2014-07-19 05:51:39 -04:00
|
|
|
GetEntityEffectIntensity(PotionDamage)), GetPotionColor(PotionDamage)
|
2014-07-17 04:51:44 -04:00
|
|
|
);
|
2014-06-08 00:56:01 -04:00
|
|
|
if (Projectile == NULL)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-13 03:49:42 -04:00
|
|
|
if (!Projectile->Initialize(*a_World))
|
2014-06-08 00:56:01 -04:00
|
|
|
{
|
|
|
|
delete Projectile;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!a_Player->IsGameModeCreative())
|
|
|
|
{
|
|
|
|
a_Player->GetInventory().RemoveOneEquippedItem();
|
|
|
|
}
|
|
|
|
|
2014-06-07 19:32:37 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-17 04:51:44 -04:00
|
|
|
|
2014-06-07 19:32:37 -04:00
|
|
|
virtual bool EatItem(cPlayer * a_Player, cItem * a_Item) override
|
|
|
|
{
|
2014-07-14 16:46:15 -04:00
|
|
|
short PotionDamage = a_Item->m_ItemDamage;
|
|
|
|
|
2014-07-17 04:51:44 -04:00
|
|
|
// Do not drink undrinkable potions:
|
2014-07-14 16:46:15 -04:00
|
|
|
if (!IsDrinkable(a_Item->m_ItemDamage))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
a_Player->AddEntityEffect(GetEntityEffectType(PotionDamage), GetEntityEffectDuration(PotionDamage), GetEntityEffectIntensity(PotionDamage));
|
2014-06-07 19:32:37 -04:00
|
|
|
a_Player->GetInventory().RemoveOneEquippedItem();
|
|
|
|
a_Player->GetInventory().AddItem(E_ITEM_GLASS_BOTTLE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2014-07-17 04:51:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|