Pol  Revision:4b29d2b
vitalmod.cpp
Go to the documentation of this file.
1 
11 #include "vitalmod.h"
12 
13 #include "../../bscript/berror.h"
14 #include "../../bscript/bobject.h"
15 #include "../../bscript/impstr.h"
16 #include "../cmbtcfg.h"
17 #include "../globals/settings.h"
18 #include "../mobile/attribute.h"
19 #include "../mobile/charactr.h"
20 #include "../spells.h"
21 #include "../ufunc.h"
22 #include "../uoexhelp.h"
23 #include "../vital.h"
24 
25 namespace Pol
26 {
27 namespace Bscript
28 {
29 using namespace Module;
30 template <>
33  {"ApplyRawDamage", &VitalExecutorModule::mf_ApplyRawDamage},
34  {"ApplyDamage", &VitalExecutorModule::mf_ApplyDamage},
35  {"HealDamage", &VitalExecutorModule::mf_HealDamage},
36  {"ConsumeMana", &VitalExecutorModule::mf_ConsumeMana},
37  {"ConsumeVital", &VitalExecutorModule::mf_ConsumeVital},
38  {"RecalcVitals", &VitalExecutorModule::mf_RecalcVitals},
39  {"GetVitalName", &VitalExecutorModule::mf_GetVitalName},
40  {"GetVital", &VitalExecutorModule::mf_GetVital},
41  {"SetVital", &VitalExecutorModule::mf_SetVital},
42  {"GetVitalRegenRate", &VitalExecutorModule::mf_GetVitalRegenRate},
43  {"GetVitalMaximumValue", &VitalExecutorModule::mf_GetVitalMaximumValue}};
44 }
45 namespace Module
46 {
47 using namespace Bscript;
48 
50  : Bscript::TmplExecutorModule<VitalExecutorModule>( "vitals", exec )
51 {
52 }
53 
55 {
56  Mobile::Character* chr;
57  int damage;
58  int userepsys;
59  int send_damage_packet;
60  if ( getCharacterParam( exec, 0, chr ) && getParam( 1, damage ) && getParam( 2, userepsys ) &&
61  getParam( 3, send_damage_packet ) && damage >= 0 && damage <= USHRT_MAX )
62  {
63  bool send_dmg = send_damage_packet == 2 ? Core::settingsManager.combat_config.send_damage_packet
64  : ( send_damage_packet > 0 ? true : false );
65  chr->apply_raw_damage_hundredths( static_cast<unsigned int>( damage * 100 ), GetUOController(),
66  userepsys > 0 ? true : false, send_dmg );
67  return new BLong( 1 );
68  }
69  else
70  return new BLong( 0 );
71 }
72 
74 {
75  Mobile::Character* chr;
76  double damage;
77  int userepsys;
78  int send_damage_packet;
79  if ( getCharacterParam( exec, 0, chr ) && getRealParam( 1, damage ) && getParam( 2, userepsys ) &&
80  getParam( 3, send_damage_packet ) )
81  {
82  if ( damage >= 0.0 && damage <= 30000.0 )
83  {
84  bool send_dmg = send_damage_packet == 2
86  : ( send_damage_packet > 0 ? true : false );
87  damage = chr->apply_damage( static_cast<unsigned short>( damage ), GetUOController(),
88  userepsys > 0 ? true : false, send_dmg );
89  return new BLong( static_cast<int>( damage ) );
90  }
91  else
92  return new BError( "Damage is out of range" );
93  }
94  else
95  return new BError( "Invalid parameter type" );
96 }
97 
99 {
100  Mobile::Character* chr;
101  int amount;
102  if ( getCharacterParam( exec, 0, chr ) && getParam( 1, amount ) && amount >= 0 &&
103  amount <= USHRT_MAX )
104  {
105  Mobile::Character* controller = GetUOController();
106  if ( controller )
107  controller->repsys_on_help( chr );
108 
109  chr->heal_damage_hundredths( static_cast<unsigned short>( amount ) * 100L );
110  return new BLong( 1 );
111  }
112  else
113  {
114  return new BError( "Invalid parameter" );
115  }
116 }
117 
119 {
120  Mobile::Character* chr;
121  int spellid;
122  if ( getCharacterParam( exec, 0, chr ) && getParam( 1, spellid ) )
123  {
124  if ( !Core::VALID_SPELL_ID( spellid ) )
125  return new BError( "Spell ID out of range" );
126 
127  Core::USpell* spell = Core::gamestate.spells[spellid];
128  if ( spell == nullptr )
129  return new BError( "No such spell" );
130  else if ( spell->check_mana( chr ) == false )
131  return new BLong( 0 );
132 
133  spell->consume_mana( chr );
134  if ( chr->has_active_client() )
136 
137  return new BLong( 1 );
138  }
139  else
140  {
141  return new BError( "Invalid parameter" );
142  }
143 }
144 
146 {
147  const Core::Vital* vital;
148 
149  if ( !getVitalParam( exec, 0, vital ) )
150  {
151  return new BError( "Invalid parameter type." );
152  }
153 
154  return new String( vital->name );
155 }
156 
158 {
159  Mobile::Character* chr;
160  const Core::Vital* vital;
161 
162  if ( getCharacterParam( exec, 0, chr ) && getVitalParam( exec, 1, vital ) )
163  {
164  const Mobile::VitalValue& vv = chr->vital( vital->vitalid );
165  return new BLong( vv.current() );
166  }
167  else
168  return new BError( "Invalid parameter type" );
169 }
170 
172 {
173  Mobile::Character* chr;
174  const Core::Vital* vital;
175 
176  if ( getCharacterParam( exec, 0, chr ) && getVitalParam( exec, 1, vital ) )
177  {
178  const Mobile::VitalValue& vv = chr->vital( vital->vitalid );
179  return new BLong( vv.maximum() );
180  }
181  else
182  return new BError( "Invalid parameter type" );
183 }
184 
186 {
187  Mobile::Character* chr;
188  const Core::Vital* vital;
189 
190  if ( getCharacterParam( exec, 0, chr ) && getVitalParam( exec, 1, vital ) )
191  {
192  const Mobile::VitalValue& vv = chr->vital( vital->vitalid );
193  return new BLong( vv.regenrate() );
194  }
195  else
196  return new BError( "Invalid parameter type" );
197 }
198 
199 BObjectImp* VitalExecutorModule::mf_SetVital( /* mob, vitalid, hundredths */ )
200 {
201  Mobile::Character* chr;
202  const Core::Vital* vital;
203  int value;
204 
205  if ( getCharacterParam( exec, 0, chr ) && getVitalParam( exec, 1, vital ) &&
206  getParam( 2, value, Core::VITAL_MAX_HUNDREDTHS ) )
207  {
208  Mobile::VitalValue& vv = chr->vital( vital->vitalid );
209  chr->set_current( vital, vv, value );
210  return new BLong( 1 );
211  }
212  else
213  return new BError( "Invalid parameter type" );
214 }
215 
216 BObjectImp* VitalExecutorModule::mf_ConsumeVital( /* mob, vital, hundredths */ )
217 {
218  Mobile::Character* chr;
219  const Core::Vital* vital;
220  int hundredths;
221 
222  if ( getCharacterParam( exec, 0, chr ) && getVitalParam( exec, 1, vital ) &&
223  getParam( 2, hundredths, Core::VITAL_MAX_HUNDREDTHS ) )
224  {
225  Mobile::VitalValue& vv = chr->vital( vital->vitalid );
226  bool res = chr->consume( vital, vv, hundredths );
227  return new BLong( res ? 1 : 0 );
228  }
229  else
230  return new BError( "Invalid parameter type" );
231 }
232 
233 BObjectImp* VitalExecutorModule::mf_RecalcVitals( /* mob, attributes, vitals */ )
234 {
235  Mobile::Character* chr;
236  BObjectImp* param1 = getParamImp( 1 );
237  BObjectImp* param2 = getParamImp( 2 );
238 
239  if ( getCharacterParam( exec, 0, chr ) && param1 != nullptr && param2 != nullptr )
240  {
241  if ( chr->logged_in() )
242  {
243  bool calc_attr = false;
244  bool calc_vital = false;
245 
246  if ( param1->isa( BObjectImp::OTLong ) )
247  calc_attr = static_cast<BLong*>( param1 )->isTrue();
248  else if ( param1->isa( BObjectImp::OTString ) )
249  {
250  String* attrname = static_cast<String*>( param1 );
252  if ( attr == nullptr )
253  return new BError( "Attribute not defined: " + attrname->value() );
254  chr->calc_single_attribute( attr );
255  }
256  else
257  return new BError( "Invalid parameter type" );
258 
259  if ( param2->isa( BObjectImp::OTLong ) )
260  calc_vital = static_cast<BLong*>( param2 )->isTrue();
261  else if ( param2->isa( BObjectImp::OTString ) )
262  {
263  String* vitalname = static_cast<String*>( param2 );
264  Core::Vital* vital = Core::FindVital( vitalname->value() );
265  if ( vital == nullptr )
266  return new BError( "Vital not defined: " + vitalname->value() );
267  chr->calc_single_vital( vital );
268  }
269  else
270  return new BError( "Invalid parameter type" );
271 
272  chr->calc_vital_stuff( calc_attr, calc_vital );
273  return new BLong( 1 );
274  }
275  else
276  return new BError( "Mobile must be online." );
277  }
278  else
279  return new BError( "Invalid parameter type" );
280 }
281 }
282 }
const std::string & value() const
Definition: impstr.h:67
Vital * FindVital(const std::string &str)
Definition: vital.cpp:83
VitalExecutorModule(Bscript::Executor &exec)
Definition: vitalmod.cpp:49
Bscript::BObjectImp * mf_ApplyDamage()
Definition: vitalmod.cpp:73
bool getRealParam(unsigned param, double &value)
Definition: execmodl.cpp:53
bool isa(BObjectType type) const
Definition: bobject.h:353
Network::Client * client
Definition: charactr.h:871
void send_mana_level(Client *client)
Definition: ufunc.cpp:1471
Bscript::BObjectImp * mf_ConsumeMana()
Definition: vitalmod.cpp:118
Bscript::BObjectImp * mf_ConsumeVital()
Definition: vitalmod.cpp:216
bool getParam(unsigned param, int &value)
Definition: execmodl.cpp:62
void calc_single_attribute(const Attribute *pAttr)
Definition: charactr.cpp:1630
Bscript::BObjectImp * mf_GetVital()
Definition: vitalmod.cpp:157
virtual void repsys_on_help(Character *recipient)
[15] Mobile (MA) Helps Mobile (MB)
Definition: repsys.cpp:932
void heal_damage_hundredths(unsigned int damage)
When a Mobile is Healed.
Definition: charactr.cpp:1979
void calc_vital_stuff(bool i_mod=true, bool v_mod=true)
Definition: charactr.cpp:1581
bool has_active_client() const
Definition: charactr.cpp:448
BObjectImp * getParamImp(unsigned param)
Definition: execmodl.cpp:22
bool logged_in() const
Definition: charactr.cpp:428
std::string name
Definition: vital.h:36
Bscript::BObjectImp * mf_GetVitalName()
Definition: vitalmod.cpp:145
bool consume(const Core::Vital *pVital, VitalValue &vv, unsigned int amt)
Definition: charactr.cpp:1540
bool getVitalParam(Executor &exec, unsigned param, const Vital *&vital)
Definition: uoexhelp.cpp:627
bool getCharacterParam(Bscript::Executor &exec, unsigned param, Mobile::Character *&chrptr)
Definition: uoexhelp.cpp:140
int maximum() const
Definition: charactr.h:199
void consume_mana(Mobile::Character *chr)
Definition: spells.cpp:241
Bscript::BObjectImp * mf_HealDamage()
Definition: vitalmod.cpp:98
bool check_mana(Mobile::Character *chr)
Definition: spells.cpp:231
Bscript::BObjectImp * mf_RecalcVitals()
Definition: vitalmod.cpp:233
Bscript::BObjectImp * mf_GetVitalMaximumValue()
Definition: vitalmod.cpp:171
int regenrate() const
Definition: charactr.h:202
void calc_single_vital(const Core::Vital *pVital)
Definition: charactr.cpp:1600
GameState gamestate
Definition: uvars.cpp:74
std::vector< USpell * > spells
Definition: uvars.h:186
SettingsManager settingsManager
Definition: settings.cpp:14
CombatConfig combat_config
Definition: settings.h:28
Bscript::BObjectImp * mf_ApplyRawDamage()
Definition: vitalmod.cpp:54
double apply_damage(double damage, Character *source=nullptr, bool userepsys=true, bool send_damage_packet=false)
Definition: charactr.cpp:1886
Mobile::Character * GetUOController()
Definition: vitalmod.h:67
Bscript::BObjectImp * mf_SetVital()
Definition: vitalmod.cpp:199
virtual void apply_raw_damage_hundredths(unsigned int damage, Character *source, bool userepsys=true, bool send_damage_packet=false)
Definition: charactr.cpp:1794
const VitalValue & vital(unsigned vitalid) const
Definition: charactr.h:1061
Bscript::BObjectImp * mf_GetVitalRegenRate()
Definition: vitalmod.cpp:185
unsigned vitalid
Definition: vital.h:38
int current() const
Definition: charactr.h:192
Definition: berror.cpp:12
void set_current(const Core::Vital *pVital, VitalValue &vv, unsigned int ones)
Definition: charactr.cpp:1560
static Attribute * FindAttribute(const std::string &str)
Definition: attribute.cpp:22
bool VALID_SPELL_ID(int spellid)
Definition: spells.h:146
const unsigned int VITAL_MAX_HUNDREDTHS
Definition: vital.h:57