From 0a1332f04b841bc32e09552665e5cd611fa23ca8 Mon Sep 17 00:00:00 2001 From: Erik Schilling Date: Sat, 27 Apr 2013 18:11:13 +0200 Subject: [Abilities] Added a add_hit_taken bind This allows to display hit messages in the client for abilities --- example/scripts/abilities.lua | 4 ++++ src/game-server/being.h | 33 +++++++++++++++++++++++++++++++++ src/game-server/combatcomponent.cpp | 1 - src/game-server/combatcomponent.h | 25 ------------------------- src/game-server/state.cpp | 7 +++---- src/scripting/lua.cpp | 15 +++++++++++++++ 6 files changed, 55 insertions(+), 30 deletions(-) diff --git a/example/scripts/abilities.lua b/example/scripts/abilities.lua index 7c25eb0..ce617ee 100644 --- a/example/scripts/abilities.lua +++ b/example/scripts/abilities.lua @@ -28,6 +28,10 @@ spell1:on_use(function(user, x, y, abilityId) local old_hp = being:base_attribute(ATTR_HP) local new_hp = math.max(old_hp - 5, 0) being:set_base_attribute(ATTR_HP, new_hp) + local diff = old_hp - new_hp + if diff > 0 then + being:add_hit_taken(diff) + end end end end) diff --git a/src/game-server/being.h b/src/game-server/being.h index 5ccc8e8..5827ad1 100644 --- a/src/game-server/being.h +++ b/src/game-server/being.h @@ -46,6 +46,11 @@ struct Status typedef std::map< int, Status > StatusEffects; +/** + * Type definition for a list of hits + */ +typedef std::vector Hits; + /** * Generic being (living actor). Keeps direction, destination and a few other * relevant properties. Used for characters & monsters (all animated objects). @@ -280,6 +285,10 @@ class BeingComponent : public Component const Point ¤tPos, const Point &destPos); + void addHitTaken(unsigned damage); + const Hits &getHitsTaken() const; + void clearHitsTaken(); + protected: static const int TICKS_PER_HP_REGENERATION = 100; @@ -312,6 +321,8 @@ class BeingComponent : public Component /** The last being emote Id. Used when triggering a being emoticon. */ int mEmoteId; + Hits mHitsTaken; //List of punches taken since last update. + /** Called when derived attributes need to get calculated */ static Script::Ref mRecalculateDerivedAttributesCallback; @@ -319,4 +330,26 @@ class BeingComponent : public Component static Script::Ref mRecalculateBaseAttributeCallback; }; + +inline void BeingComponent::addHitTaken(unsigned damage) +{ + mHitsTaken.push_back(damage); +} + +/** + * Gets the damage list. + */ +inline const Hits &BeingComponent::getHitsTaken() const +{ + return mHitsTaken; +} + +/** + * Clears the damage list. + */ +inline void BeingComponent::clearHitsTaken() +{ + mHitsTaken.clear(); +} + #endif // BEING_H diff --git a/src/game-server/combatcomponent.cpp b/src/game-server/combatcomponent.cpp index 38c7716..65eddc4 100644 --- a/src/game-server/combatcomponent.cpp +++ b/src/game-server/combatcomponent.cpp @@ -157,7 +157,6 @@ int CombatComponent::damage(Entity &target, if (HPloss > 0) { - mHitsTaken.push_back(HPloss); const Attribute *HP = beingComponent->getAttribute(ATTR_HP); LOG_DEBUG("Being " << target.getComponent()->getPublicID() diff --git a/src/game-server/combatcomponent.h b/src/game-server/combatcomponent.h index 168c08d..1a4f310 100644 --- a/src/game-server/combatcomponent.h +++ b/src/game-server/combatcomponent.h @@ -31,11 +31,6 @@ class Entity; -/** - * Type definition for a list of hits - */ -typedef std::vector Hits; - class CombatComponent: public Component { public: @@ -50,9 +45,6 @@ public: void removeAttack(AttackInfo *attackInfo); Attacks &getAttacks(); - const Hits &getHitsTaken() const; - void clearHitsTaken(); - int performAttack(Entity &source, const Damage &dmg); virtual int damage(Entity &target, Entity *source, const Damage &damage); @@ -72,7 +64,6 @@ protected: Entity *mTarget; Attacks mAttacks; Attack *mCurrentAttack; // Last used attack - Hits mHitsTaken; //List of punches taken since last update. }; @@ -81,22 +72,6 @@ inline Attacks &CombatComponent::getAttacks() return mAttacks; } -/** - * Gets the damage list. - */ -inline const Hits &CombatComponent::getHitsTaken() const -{ - return mHitsTaken; -} - -/** - * Clears the damage list. - */ -inline void CombatComponent::clearHitsTaken() -{ - mHitsTaken.clear(); -} - /** * Gets the attack id the being is currently performing. * For being, this is defaulted to the first one (1). diff --git a/src/game-server/state.cpp b/src/game-server/state.cpp index 6efeb14..5bca9c9 100644 --- a/src/game-server/state.cpp +++ b/src/game-server/state.cpp @@ -255,9 +255,8 @@ static void informPlayer(MapComposite *map, Entity *p) // Send damage messages. if (o->canFight()) { - CombatComponent *combatComponent = - o->getComponent(); - const Hits &hits = combatComponent->getHitsTaken(); + auto *beingComponent = o->getComponent(); + const Hits &hits = beingComponent->getHitsTaken(); for (Hits::const_iterator j = hits.begin(), j_end = hits.end(); j != j_end; ++j) { @@ -517,7 +516,7 @@ void GameState::update(int tick) a->getComponent()->clearUpdateFlags(); if (a->canFight()) { - a->getComponent()->clearHitsTaken(); + a->getComponent()->clearHitsTaken(); } } } diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 3fd1651..6258af3 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -2010,6 +2010,20 @@ static int xp_for_level(lua_State *s) return 1; } +/** LUA entity:add_hit_taken (being) + * add_hit_taken(int damage) + ** + * Adds a damage value to the taken hits of a being. This list will be send to + * all clients in the view range in order to allow to display the hit particles. + */ +static int entity_add_hit_taken(lua_State *s) +{ + Entity *c = checkBeing(s, 1); + const int damage = luaL_checkinteger(s, 2); + c->getComponent()->addHitTaken(damage); + return 0; +} + /** LUA entity:hair_color (being) * entity:hair_color() ** @@ -3754,6 +3768,7 @@ LuaScript::LuaScript(): { "has_status", entity_has_status }, { "status_time", entity_get_status_time }, { "set_status_time", entity_set_status_time }, + { "add_hit_taken", entity_add_hit_taken }, { nullptr, nullptr } }; -- cgit