summaryrefslogtreecommitdiffstats
path: root/source/Entities
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2013-11-14 08:04:43 +0100
committerMattes D <github@xoft.cz>2013-11-14 08:04:43 +0100
commit79a98627a2a234226b5daeb1a709060acd289226 (patch)
treeccf6e66ed4e3d944f47bcdc69eb7d33132a694f9 /source/Entities
parentUpdated the Core. (diff)
parentfixed function name (diff)
downloadcuberite-79a98627a2a234226b5daeb1a709060acd289226.tar
cuberite-79a98627a2a234226b5daeb1a709060acd289226.tar.gz
cuberite-79a98627a2a234226b5daeb1a709060acd289226.tar.bz2
cuberite-79a98627a2a234226b5daeb1a709060acd289226.tar.lz
cuberite-79a98627a2a234226b5daeb1a709060acd289226.tar.xz
cuberite-79a98627a2a234226b5daeb1a709060acd289226.tar.zst
cuberite-79a98627a2a234226b5daeb1a709060acd289226.zip
Diffstat (limited to 'source/Entities')
-rw-r--r--source/Entities/Player.cpp111
-rw-r--r--source/Entities/Player.h44
2 files changed, 152 insertions, 3 deletions
diff --git a/source/Entities/Player.cpp b/source/Entities/Player.cpp
index 2e4199629..098417dc5 100644
--- a/source/Entities/Player.cpp
+++ b/source/Entities/Player.cpp
@@ -1,4 +1,4 @@
-
+
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "Player.h"
@@ -33,6 +33,7 @@
+
cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName)
: super(etPlayer, 0.6, 1.8)
, m_GameMode(eGameMode_NotSet)
@@ -65,6 +66,7 @@ cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName)
, m_EatingFinishTick(-1)
, m_IsChargingBow(false)
, m_BowCharge(0)
+ , m_XpTotal(0)
{
LOGD("Created a player object for \"%s\" @ \"%s\" at %p, ID %d",
a_PlayerName.c_str(), a_Client->GetIPString().c_str(),
@@ -260,6 +262,108 @@ void cPlayer::Tick(float a_Dt, cChunk & a_Chunk)
+int cPlayer::CalcLevelFromXp(int a_XpTotal)
+{
+ //level 0 to 15
+ if(a_XpTotal <= XP_TO_LEVEL15)
+ {
+ return a_XpTotal / XP_PER_LEVEL_TO15;
+ }
+
+ //level 30+
+ if(a_XpTotal > XP_TO_LEVEL30)
+ {
+ return (int) (151.5 + sqrt( 22952.25 - (14 * (2220 - a_XpTotal)))) / 7;
+ }
+
+ //level 16 to 30
+ return (int) ( 29.5 + sqrt( 870.25 - (6 * ( 360 - a_XpTotal )))) / 3;
+}
+
+
+
+
+
+int cPlayer::XpForLevel(int a_Level)
+{
+ //level 0 to 15
+ if(a_Level <= 15)
+ {
+ return a_Level * XP_PER_LEVEL_TO15;
+ }
+
+ //level 30+
+ if(a_Level >= 31)
+ {
+ return (int) ( (3.5 * a_Level * a_Level) - (151.5 * a_Level) + 2220 );
+ }
+
+ //level 16 to 30
+ return (int) ( (1.5 * a_Level * a_Level) - (29.5 * a_Level) + 360 );
+}
+
+
+
+
+
+int cPlayer::XpGetLevel()
+{
+ return CalcLevelFromXp(m_XpTotal);
+}
+
+
+
+
+
+float cPlayer::XpGetPercentage()
+{
+ int currentLevel = CalcLevelFromXp(m_XpTotal);
+
+ return (float)m_XpTotal / (float)XpForLevel(1+currentLevel);
+}
+
+
+
+
+
+bool cPlayer::SetExperience(int a_XpTotal)
+{
+ if(!(a_XpTotal >= 0) || (a_XpTotal > (INT_MAX - m_XpTotal)))
+ {
+ LOGWARNING("Tried to update experiece with an invalid Xp value: %d", a_XpTotal);
+ return false; //oops, they gave us a dodgey number
+ }
+
+ m_XpTotal = a_XpTotal;
+
+ return true;
+}
+
+
+
+
+
+int cPlayer::AddExperience(int a_Xp_delta)
+{
+ if(a_Xp_delta < 0)
+ {
+ //value was negative, abort and report
+ LOGWARNING("Attempt was made to increment Xp by %d, must be positive",
+ a_Xp_delta);
+ return -1; //should we instead just return the current Xp?
+ }
+
+ LOGD("Player \"%s\" earnt %d experience", m_PlayerName.c_str(), a_Xp_delta);
+
+ m_XpTotal += a_Xp_delta;
+
+ return m_XpTotal;
+}
+
+
+
+
+
void cPlayer::StartChargingBow(void)
{
LOGD("Player \"%s\" started charging their bow", m_PlayerName.c_str());
@@ -1278,7 +1382,7 @@ bool cPlayer::LoadFromDisk()
LOGWARNING("Cannot read player data from file \"%s\"", SourceFile.c_str());
return false;
}
- f.Close();
+ f.Close(); //cool kids play nice
Json::Value root;
Json::Reader reader;
@@ -1314,6 +1418,8 @@ bool cPlayer::LoadFromDisk()
m_FoodTickTimer = root.get("foodTickTimer", 0).asInt();
m_FoodExhaustionLevel = root.get("foodExhaustion", 0).asDouble();
+ SetExperience(root.get("experience", 0).asInt());
+
m_GameMode = (eGameMode) root.get("gamemode", eGameMode_NotSet).asInt();
m_Inventory.LoadFromJson(root["inventory"]);
@@ -1354,6 +1460,7 @@ bool cPlayer::SaveToDisk()
root["rotation"] = JSON_PlayerRotation;
root["inventory"] = JSON_Inventory;
root["health"] = m_Health;
+ root["experience"] = m_XpTotal;
root["air"] = m_AirLevel;
root["food"] = m_FoodLevel;
root["foodSaturation"] = m_FoodSaturationLevel;
diff --git a/source/Entities/Player.h b/source/Entities/Player.h
index 01efa3681..ab2f94d4c 100644
--- a/source/Entities/Player.h
+++ b/source/Entities/Player.h
@@ -63,6 +63,31 @@ public:
/// Returns the currently equipped boots; empty item if none
virtual cItem GetEquippedBoots(void) const override { return m_Inventory.GetEquippedBoots(); }
+
+
+ // tolua_begin
+
+ /** Sets the experience total
+ Returns true on success
+ "should" really only be called at init or player death, plugins excepted
+ */
+ bool SetExperience(int a_XpTotal);
+
+ /* Adds Xp, "should" not inc more than MAX_EXPERIENCE_ORB_SIZE unless you're a plugin being funny, *cough* cheating
+ Returns the new total experience, -1 on error
+ */
+ int AddExperience(int a_Xp_delta);
+
+ /// Gets the experience total - XpTotal
+ inline int XpGetTotal(void) { return m_XpTotal; }
+
+ /// Gets the current level - XpLevel
+ int XpGetLevel(void);
+
+ /// Gets the experience bar percentage - XpP
+ float XpGetPercentage(void);
+
+ // tolua_end
/// Starts charging the equipped bow
void StartChargingBow(void);
@@ -289,7 +314,7 @@ public:
virtual bool IsSubmerged(void) const{ return m_IsSubmerged; }
// tolua_end
-
+
// cEntity overrides:
virtual bool IsCrouched (void) const { return m_IsCrouched; }
virtual bool IsSprinting(void) const { return m_IsSprinting; }
@@ -308,6 +333,14 @@ protected:
std::string m_PlayerName;
std::string m_LoadedWorldName;
+ /// Xp Level stuff
+ enum
+ {
+ XP_TO_LEVEL15 = 255,
+ XP_PER_LEVEL_TO15 = 17,
+ XP_TO_LEVEL30 = 825
+ } ;
+
/// Player's air level (for swimming)
int m_AirLevel;
@@ -378,6 +411,15 @@ protected:
/// The world tick in which eating will be finished. -1 if not eating
Int64 m_EatingFinishTick;
+
+ /// Player Xp level
+ int m_XpTotal;
+
+ /// Caculates the Xp needed for a given level, ref: http://minecraft.gamepedia.com/XP
+ static int XpForLevel(int a_Level);
+
+ /// inverse of XpAtLevel, ref: http://minecraft.gamepedia.com/XP values are as per this with pre-calculations
+ static int CalcLevelFromXp(int a_XpTotal);
bool m_IsChargingBow;
int m_BowCharge;