polserver/pol-core/pol/module/vitalmod.cpp
turleypol bce1154d7a
ClangTidy readability-else-after-return (#857)
* trigger tidy

* Automated clang-tidy change: readability-else-after-return

* compile test

* rerun

* Automated clang-tidy change: readability-else-after-return

* trigger..

* Automated clang-tidy change: readability-else-after-return

* manually removed a few

* Automated clang-tidy change: readability-else-after-return

* removed duplicate code

* fix remaining warnings

* fixed scope

---------

Co-authored-by: Clang Tidy <clang-tidy@users.noreply.github.com>
2026-01-18 09:35:52 +01:00

246 lines
7.3 KiB
C++

/** @file
*
* @par History
* - 2006/10/07 Shinigami: GCC 3.4.x fix - added "template<>" to TmplExecutorModule
* - 2009/09/22 Turley: repsys param to applydamage
* - 2009/11/20 Turley: RecalcVitals can update single Attributes/Vitals - based on Tomi
* - 2010/01/15 Turley: (Tomi) send damage param ApplyDamage/ApplyRawDamage
*/
#include "vitalmod.h"
#include "../../bscript/berror.h"
#include "../../bscript/bobject.h"
#include "../../bscript/impstr.h"
#include "../cmbtcfg.h"
#include "../globals/settings.h"
#include "../mobile/attribute.h"
#include "../mobile/charactr.h"
#include "../spells.h"
#include "../ufunc.h"
#include "../vital.h"
#include <module_defs/vitals.h>
namespace Pol::Module
{
using namespace Bscript;
VitalExecutorModule::VitalExecutorModule( Bscript::Executor& exec )
: Bscript::TmplExecutorModule<VitalExecutorModule, Core::PolModule>( exec )
{
}
BObjectImp* VitalExecutorModule::mf_ApplyRawDamage()
{
Mobile::Character* chr;
int damage;
int userepsys;
int send_damage_packet;
if ( getCharacterParam( 0, chr ) && getParam( 1, damage ) && getParam( 2, userepsys ) &&
getParam( 3, send_damage_packet ) && damage >= 0 && damage <= USHRT_MAX )
{
bool send_dmg = send_damage_packet == 2 ? Core::settingsManager.combat_config.send_damage_packet
: ( send_damage_packet > 0 ? true : false );
chr->apply_raw_damage_hundredths( static_cast<unsigned int>( damage * 100 ), GetUOController(),
userepsys > 0 ? true : false, send_dmg );
return new BLong( 1 );
}
return new BLong( 0 );
}
BObjectImp* VitalExecutorModule::mf_ApplyDamage()
{
Mobile::Character* chr;
double damage;
int userepsys;
int send_damage_packet;
if ( getCharacterParam( 0, chr ) && getRealParam( 1, damage ) && getParam( 2, userepsys ) &&
getParam( 3, send_damage_packet ) )
{
if ( damage >= 0.0 && damage <= 30000.0 )
{
bool send_dmg = send_damage_packet == 2
? Core::settingsManager.combat_config.send_damage_packet
: ( send_damage_packet > 0 ? true : false );
damage = chr->apply_damage( static_cast<unsigned short>( damage ), GetUOController(),
userepsys > 0 ? true : false, send_dmg );
return new BLong( static_cast<int>( damage ) );
}
return new BError( "Damage is out of range" );
}
return new BError( "Invalid parameter type" );
}
BObjectImp* VitalExecutorModule::mf_HealDamage()
{
Mobile::Character* chr;
int amount;
if ( getCharacterParam( 0, chr ) && getParam( 1, amount ) && amount >= 0 && amount <= USHRT_MAX )
{
Mobile::Character* controller = GetUOController();
if ( controller )
controller->repsys_on_help( chr );
chr->heal_damage_hundredths( static_cast<unsigned short>( amount ) * 100L );
return new BLong( 1 );
}
return new BError( "Invalid parameter" );
}
BObjectImp* VitalExecutorModule::mf_ConsumeMana()
{
Mobile::Character* chr;
int spellid;
if ( getCharacterParam( 0, chr ) && getParam( 1, spellid ) )
{
if ( !Core::VALID_SPELL_ID( spellid ) )
return new BError( "Spell ID out of range" );
Core::USpell* spell = Core::gamestate.spells[spellid];
if ( spell == nullptr )
return new BError( "No such spell" );
if ( spell->check_mana( chr ) == false )
return new BLong( 0 );
spell->consume_mana( chr );
if ( chr->has_active_client() )
Core::send_mana_level( chr->client );
return new BLong( 1 );
}
return new BError( "Invalid parameter" );
}
BObjectImp* VitalExecutorModule::mf_GetVitalName( /* alias_name */ )
{
const Core::Vital* vital;
if ( !getVitalParam( 0, vital ) )
{
return new BError( "Invalid parameter type." );
}
return new String( vital->name );
}
BObjectImp* VitalExecutorModule::mf_GetVital( /* mob, vitalid */ )
{
Mobile::Character* chr;
const Core::Vital* vital;
if ( getCharacterParam( 0, chr ) && getVitalParam( 1, vital ) )
{
const Mobile::VitalValue& vv = chr->vital( vital->vitalid );
return new BLong( vv.current() );
}
return new BError( "Invalid parameter type" );
}
BObjectImp* VitalExecutorModule::mf_GetVitalMaximumValue( /* mob, vitalid */ )
{
Mobile::Character* chr;
const Core::Vital* vital;
if ( getCharacterParam( 0, chr ) && getVitalParam( 1, vital ) )
{
const Mobile::VitalValue& vv = chr->vital( vital->vitalid );
return new BLong( vv.maximum() );
}
return new BError( "Invalid parameter type" );
}
BObjectImp* VitalExecutorModule::mf_GetVitalRegenRate( /* mob, vitalid */ )
{
Mobile::Character* chr;
const Core::Vital* vital;
if ( getCharacterParam( 0, chr ) && getVitalParam( 1, vital ) )
{
const Mobile::VitalValue& vv = chr->vital( vital->vitalid );
return new BLong( vv.regenrate() );
}
return new BError( "Invalid parameter type" );
}
BObjectImp* VitalExecutorModule::mf_SetVital( /* mob, vitalid, hundredths */ )
{
Mobile::Character* chr;
const Core::Vital* vital;
int value;
if ( getCharacterParam( 0, chr ) && getVitalParam( 1, vital ) &&
getParam( 2, value, Core::VITAL_MAX_HUNDREDTHS ) )
{
Mobile::VitalValue& vv = chr->vital( vital->vitalid );
chr->set_current( vital, vv, value, Mobile::Character::VitalDepletedReason::SCRIPT );
return new BLong( 1 );
}
return new BError( "Invalid parameter type" );
}
BObjectImp* VitalExecutorModule::mf_ConsumeVital( /* mob, vital, hundredths */ )
{
Mobile::Character* chr;
const Core::Vital* vital;
int hundredths;
if ( getCharacterParam( 0, chr ) && getVitalParam( 1, vital ) &&
getParam( 2, hundredths, Core::VITAL_MAX_HUNDREDTHS ) )
{
Mobile::VitalValue& vv = chr->vital( vital->vitalid );
bool res =
chr->consume( vital, vv, hundredths, Mobile::Character::VitalDepletedReason::SCRIPT );
return new BLong( res ? 1 : 0 );
}
return new BError( "Invalid parameter type" );
}
BObjectImp* VitalExecutorModule::mf_RecalcVitals( /* mob, attributes, vitals */ )
{
Mobile::Character* chr;
BObjectImp* param1 = getParamImp( 1 );
BObjectImp* param2 = getParamImp( 2 );
if ( getCharacterParam( 0, chr ) && param1 != nullptr && param2 != nullptr )
{
if ( chr->logged_in() )
{
bool calc_attr = false;
bool calc_vital = false;
if ( auto* v = impptrIf<BLong>( param1 ) )
calc_attr = v->isTrue();
else if ( auto* attrname = impptrIf<String>( param1 ) )
{
Mobile::Attribute* attr = Mobile::Attribute::FindAttribute( attrname->value() );
if ( attr == nullptr )
return new BError( "Attribute not defined: " + attrname->value() );
chr->calc_single_attribute( attr );
}
else
return new BError( "Invalid parameter type" );
if ( auto* v = impptrIf<BLong>( param2 ) )
calc_vital = v->isTrue();
else if ( auto* vitalname = impptrIf<String>( param2 ) )
{
Core::Vital* vital = Core::FindVital( vitalname->value() );
if ( vital == nullptr )
return new BError( "Vital not defined: " + vitalname->value() );
chr->calc_single_vital( vital );
}
else
return new BError( "Invalid parameter type" );
chr->calc_vital_stuff( calc_attr, calc_vital );
return new BLong( 1 );
}
return new BError( "Mobile must be online." );
}
return new BError( "Invalid parameter type" );
}
} // namespace Pol::Module