global_skills.cpp

Go to the documentation of this file.
00001 
00002 //            Copyright (C) 2004-2007 by The Allacrost Project
00003 //                         All Rights Reserved
00004 //
00005 // This code is licensed under the GNU GPL version 2. It is free software 
00006 // and you may modify it and/or redistribute it under the terms of this license.
00007 // See http://www.gnu.org/copyleft/gpl.html for details.
00009 
00016 #include <iostream>
00017 
00018 #include "utils.h"
00019 #include "video.h"
00020 #include "script.h"
00021 #include "battle_actors.h"
00022 
00023 #include "global.h"
00024 
00025 using namespace std;
00026 
00027 using namespace hoa_utils;
00028 using namespace hoa_video;
00029 using namespace hoa_script;
00030 
00031 
00032 
00033 namespace hoa_global {
00034 
00035 using namespace private_global;
00036 
00037 // -----------------------------------------------------------------------------
00038 // GlobalElementalEffect class
00039 // -----------------------------------------------------------------------------
00040 
00041 // -----------------------------------------------------------------------------
00042 // GlobalStatusEffect class
00043 // -----------------------------------------------------------------------------
00044 
00045 bool GlobalStatusEffect::IncrementIntensity(uint8 amount) {
00046   // Intensity can not be increased beyond the upper bound "extreme"
00047   if (_intensity == GLOBAL_INTENSITY_POS_EXTREME) {
00048     return false;
00049   }
00050 
00051   if (amount == 0) {
00052     if (GLOBAL_DEBUG) fprintf(stderr, "WARNING: passed 0 for amount argument to increase intensity of status effect\n");
00053     return false;
00054   }
00055 
00056   if (amount < 10) {
00057     // _intensity += amount;
00058     if (_intensity > GLOBAL_INTENSITY_POS_EXTREME) {
00059       _intensity = GLOBAL_INTENSITY_POS_EXTREME;
00060       return false;
00061     }
00062     else {
00063       return true;
00064     }
00065   }
00066   // This is done to protect against the possibility of an overflow condition
00067   else {
00068     if (GLOBAL_DEBUG) fprintf(stderr, "WARNING: amount argument was > 10 to increase intensity of status effect\n");
00069 
00070     if (_intensity != GLOBAL_INTENSITY_POS_EXTREME) {
00071       _intensity = GLOBAL_INTENSITY_POS_EXTREME;
00072     }
00073     return false;
00074   }
00075 } // bool GlobalStatusEffect::IncrementIntensity(uint8 amount)
00076 
00077 
00078 
00079 bool GlobalStatusEffect::DecrementIntensity(uint8 amount) {
00080   if (_intensity == GLOBAL_INTENSITY_INVALID) {
00081     return false;
00082   }
00083 
00084   if (amount == 0) {
00085     if (GLOBAL_DEBUG) fprintf(stderr, "WARNING: passed 0 for amount argument to decrease intensity of status effect\n");
00086     return false;
00087   }
00088 
00089   if (amount <= _intensity) {
00090     // _intensity -= amount;
00091     return true;
00092   }
00093   // This is done to protect against the possibility of an overflow condition
00094   else {
00095     if (_intensity != GLOBAL_INTENSITY_NEUTRAL) {
00096       _intensity = GLOBAL_INTENSITY_NEUTRAL;
00097     }
00098     return false;
00099   }
00100 } // bool GlobalStatusEffect::DecrementIntensity(uint8 amount)
00101 
00102 // -----------------------------------------------------------------------------
00103 // GlobalSkill class
00104 // -----------------------------------------------------------------------------
00105 
00106 GlobalSkill::GlobalSkill(uint32 id) :
00107   _id(id)
00108 {
00109   // A pointer to the skill script which will be used to load this skill
00110   ReadScriptDescriptor *skill_script = NULL;
00111 
00112   if (_id == 0) {
00113     _type = GLOBAL_SKILL_INVALID;
00114     if (GLOBAL_DEBUG)
00115       cerr << "GLOBAL ERROR: GlobalSkill constructor failed because it had an invalid id value: " << _id << endl;
00116     return;
00117   }
00118   else if (_id <= MAX_ATTACK_ID) {
00119     _type = GLOBAL_SKILL_ATTACK;
00120     skill_script = &(GlobalManager->_attack_skills_script);
00121   }
00122   else if (_id <= MAX_DEFEND_ID) {
00123     _type = GLOBAL_SKILL_DEFEND;
00124     skill_script = &(GlobalManager->_defend_skills_script);
00125   }
00126   else if (_id <= MAX_SUPPORT_ID) {
00127     _type = GLOBAL_SKILL_SUPPORT;
00128     skill_script = &(GlobalManager->_support_skills_script);
00129   }
00130   else {
00131     _type = GLOBAL_SKILL_INVALID;
00132     if (GLOBAL_DEBUG)
00133       cerr << "GLOBAL ERROR: GlobalSkill constructor failed because it had an invalid id value: " << _id << endl;
00134     _id = 0;
00135     return;
00136   }
00137 
00138   // Load the skill properties from the script
00139   if (skill_script->DoesTableExist(_id) == false) {
00140     if (GLOBAL_DEBUG)
00141       cerr << "GLOBAL ERROR: GlobalSkill constructor failed because there was no skill defined for the id: " << _id << endl;
00142     _id = 0;
00143     return;
00144   }
00145 
00146   skill_script->OpenTable(_id);
00147   _name = MakeUnicodeString(skill_script->ReadString("name"));
00148   if (skill_script->DoesStringExist("description"))
00149     _description = MakeUnicodeString(skill_script->ReadString("description"));
00150   _sp_required = skill_script->ReadUInt("sp_required");
00151   _warmup_time = skill_script->ReadUInt("warmup_time");
00152   _cooldown_time = skill_script->ReadUInt("cooldown_time");
00153   _target_type = static_cast<GLOBAL_TARGET>(skill_script->ReadInt("target_type"));
00154   _target_ally = skill_script->ReadBool("target_ally");
00155 
00156   if (skill_script->DoesFunctionExist("BattleExecute")) {
00157     _battle_execute_function = new ScriptObject();
00158     *_battle_execute_function = skill_script->ReadFunctionPointer("BattleExecute");
00159   }
00160   if (skill_script->DoesFunctionExist("MenuExecute")) {
00161     _menu_execute_function = new ScriptObject();
00162     *_menu_execute_function = skill_script->ReadFunctionPointer("MenuExecute");
00163   }
00164 
00165   // Determine the skill's usage based on which execution functions are available
00166   if (_battle_execute_function != NULL && _menu_execute_function != NULL)
00167     _usage = GLOBAL_USE_ALL;
00168   else if (_battle_execute_function != NULL && _menu_execute_function == NULL)
00169     _usage = GLOBAL_USE_BATTLE;
00170   else if (_battle_execute_function == NULL && _menu_execute_function != NULL)
00171     _usage = GLOBAL_USE_MENU;
00172   else
00173     _usage = GLOBAL_USE_INVALID;
00174 
00175   skill_script->CloseTable();
00176 
00177   if (skill_script->IsErrorDetected()) {
00178     cerr << "GLOBAL ERROR: GlobalSkill constructor experienced errors when reading Lua data. They are as follows: " << endl;
00179     cerr << skill_script->GetErrorMessages() << endl;
00180   }
00181 } // GlobalSkill::GlobalSkill()
00182 
00183 
00184 
00185 GlobalSkill::~GlobalSkill() {
00186   if (_battle_execute_function != NULL) {
00187     delete _battle_execute_function;
00188     _battle_execute_function = NULL;
00189   }
00190 
00191   if (_menu_execute_function != NULL) {
00192     delete _menu_execute_function;
00193     _menu_execute_function = NULL;
00194   }
00195 
00196 //  for (uint32 i = 0; i < _elemental_effects.size(); i++) {
00197 //    delete _elemental_effects[i];
00198 //  }
00199 //  _elemental_effects.empty();
00200 // 
00201 //  for (uint32 i = 0; i < _status_effects.size(); i++) {
00202 //    delete _status_effects[i].second;
00203 //  }
00204 //  _status_effects.empty();
00205 }
00206 
00207 
00208 
00209 void GlobalSkill::BattleExecute(hoa_battle::private_battle::BattleActor* target, hoa_battle::private_battle::BattleActor* instigator) {
00210   if (IsExecutableInBattle() == false) {
00211     if (GLOBAL_DEBUG)
00212       cerr << "GLOBAL WARNING: GlobalSkill::BattleExecute() failed because battle execution "
00213         << "was not supported by the skill: " << _id << endl;
00214     return;
00215   }
00216 
00217   if (_sp_required > instigator->GetSkillPoints()) {
00218     if (GLOBAL_DEBUG)
00219       cerr << "GLOBAL WARNING: GlobalSkill::BattleExecute() failed because there was an insufficient amount of "
00220         << "skill points to execute the skill: " << _id << endl;
00221     return;
00222   }
00223 
00224   ScriptCallFunction<void>(*_battle_execute_function, target, instigator);
00225 }
00226 
00227 
00228 
00229 void GlobalSkill::MenuExecute(GlobalCharacter* target, GlobalCharacter* instigator) {
00230   if (IsExecutableInMenu() == false) {
00231     if (GLOBAL_DEBUG)
00232       cerr << "GLOBAL WARNING: GlobalSkill::MenuExecute() failed because menu execution "
00233         << "was not supported by the skill: " << _id << endl;
00234     return;
00235   }
00236 
00237   if (_sp_required > instigator->GetSkillPoints()) {
00238     if (GLOBAL_DEBUG)
00239       cerr << "GLOBAL WARNING: GlobalSkill::MenuExecute() failed because there was an insufficient amount of "
00240         << "skill points to execute the skill: " << _id << endl;
00241     return;
00242   }
00243 
00244   ScriptCallFunction<void>(*_menu_execute_function, target, instigator);
00245 }
00246 
00247 } // namespace hoa_global

Generated on Fri Jul 6 23:11:15 2007 for Hero of Allacrost by  doxygen 1.5.1