global_objects.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 namespace hoa_global {
00032 
00033 using namespace private_global;
00034 
00035 GlobalObject* GlobalCreateNewObject(uint32 id, uint32 count) {
00036   GlobalObject* new_object = NULL;
00037 
00038   if (id == 0) {
00039     if (GLOBAL_DEBUG)
00040       cerr << "GLOBAL WARNING: GlobalCreateNewObject() was given a zero id argument" << endl;
00041     return NULL;
00042   }
00043   else if (id <= MAX_ITEM_ID)
00044     new_object = new GlobalItem(id, count);
00045   else if (id <= MAX_WEAPON_ID)
00046     new_object = new GlobalWeapon(id, count);
00047   else if (id <= MAX_LEG_ARMOR_ID)
00048     new_object = new GlobalArmor(id, count);
00049   else if (id <= MAX_SHARD_ID)
00050     new_object = new GlobalShard(id, count);
00051   else if (id <= MAX_KEY_ITEM_ID)
00052     new_object = new GlobalKeyItem(id, count);
00053   else {
00054     if (GLOBAL_DEBUG)
00055       cerr << "GLOBAL WARNING: GlobalCreateNewObject() was given an invalid id argument" << endl;
00056   }
00057 
00058   return new_object;
00059 }
00060 
00061 // -----------------------------------------------------------------------------
00062 // GlobalItem class
00063 // -----------------------------------------------------------------------------
00064 
00065 GlobalItem::GlobalItem(uint32 id, uint32 count) :
00066   GlobalObject(id, count)
00067 {
00068   if (_id == 0 || _id > MAX_ITEM_ID) {
00069     cerr << "GLOBAL ERROR: GlobalItem constructor failed due to an invalid id assignment: " << _id << endl;
00070     exit(1);
00071   }
00072 
00073   ReadScriptDescriptor& script_file = GlobalManager->_items_script;
00074 
00075   if (script_file.DoesTableExist(_id) == false) {
00076     cerr << "GLOBAL ERROR: GlobalItem constructor failed because the table containing the "
00077       << "item definition did not exist for item id: " << _id << endl;
00078     exit(1);
00079   }
00080 
00081   // Load the item data from the script
00082   script_file.OpenTable(_id);
00083   _name = MakeUnicodeString(script_file.ReadString("name"));
00084   _description = MakeUnicodeString(script_file.ReadString("description"));
00085   _icon_image.SetFilename(script_file.ReadString("icon"));
00086   _target_type = static_cast<GLOBAL_TARGET>(script_file.ReadInt("target_type"));
00087   _target_ally = script_file.ReadBool("target_ally");
00088   _price = script_file.ReadUInt("standard_price");
00089 
00090   if (script_file.DoesFunctionExist("BattleUse")) {
00091     _battle_use_function = new ScriptObject();
00092     *_battle_use_function = script_file.ReadFunctionPointer("BattleUse");
00093   }
00094   if (script_file.DoesFunctionExist("MenuUse")) {
00095     _menu_use_function = new ScriptObject();
00096     *_menu_use_function = script_file.ReadFunctionPointer("MenuUse");
00097   }
00098 
00099   // Determine the items's usage based on which use functions are available
00100   if (_battle_use_function != NULL && _menu_use_function != NULL)
00101     _usage = GLOBAL_USE_ALL;
00102   else if (_battle_use_function != NULL && _menu_use_function == NULL)
00103     _usage = GLOBAL_USE_BATTLE;
00104   else if (_battle_use_function == NULL && _menu_use_function != NULL)
00105     _usage = GLOBAL_USE_MENU;
00106   else
00107     _usage = GLOBAL_USE_INVALID;
00108 
00109   if (_icon_image.Load() == false) {
00110     if (GLOBAL_DEBUG)
00111       cerr << "GLOBAL WARNING: GlobalItem constructor failed to load the icon image for the item: " << _id << endl;
00112   }
00113 
00114   script_file.CloseTable();
00115 
00116   if (script_file.IsErrorDetected()) {
00117     if (GLOBAL_DEBUG) {
00118       cerr << "GLOBAL WARNING: GlobalItem constructor incurred script reading errors. They are as follows: " << endl;
00119       cerr << script_file.GetErrorMessages() << endl;
00120     }
00121     return;
00122   }
00123 } // void GlobalItem::GlobalItem(uint32 id, uint32 count = 1)
00124 
00125 
00126 
00127 GlobalItem::~GlobalItem() {
00128   if (_battle_use_function != NULL) {
00129     delete _battle_use_function;
00130     _battle_use_function = NULL;
00131   }
00132   if (_menu_use_function != NULL) {
00133     delete _menu_use_function;
00134     _menu_use_function = NULL;
00135   }
00136 } // void GlobalItem::~GlobalItem()
00137 
00138 
00139 
00140 void GlobalItem::BattleUse(hoa_battle::private_battle::BattleActor* target, hoa_battle::private_battle::BattleActor* instigator) {
00141   if (IsUsableInBattle() == false) {
00142     if (GLOBAL_DEBUG)
00143       cerr << "GLOBAL WARNING: GlobalItem::BattleUse() failed because battle usage "
00144         << "was not supported by the item: " << _id << endl;
00145     return;
00146   }
00147 
00148   if (_count == 0) {
00149     if (GLOBAL_DEBUG)
00150       cerr << "GLOBAL WARNING: GlobalItem::BattleUse() failed because the count of the item "
00151         << "was set to zero for item: " << _id << endl;
00152     return;
00153   }
00154 
00155   ScriptCallFunction<void>(*_battle_use_function, target, instigator);
00156   _count--;
00157 } // void GlobalItem::BattleUse(hoa_battle::private_battle::BattleActor* target, hoa_battle::private_battle::BattleActor* instigator)
00158 
00159 
00160 
00161 void GlobalItem::MenuUse(GlobalCharacter* target) {
00162   if (IsUsableInMenu() == false) {
00163     if (GLOBAL_DEBUG)
00164       cerr << "GLOBAL WARNING: GlobalItem::MenuUse() failed because menu usage "
00165         << "was not supported by the item: " << _id << endl;
00166     return;
00167   }
00168 
00169   if (_count == 0) {
00170     if (GLOBAL_DEBUG)
00171       cerr << "GLOBAL WARNING: GlobalItem::MenuUse() failed because the count of the item "
00172         << "was set to zero for item: " << _id << endl;
00173     return;
00174   }
00175 
00176   ScriptCallFunction<void>(*_menu_use_function, target);
00177   _count--;
00178 } // void GlobalItem::MenuUse(GlobalCharacter* target)
00179 
00180 // -----------------------------------------------------------------------------
00181 // GlobalWeapon class
00182 // -----------------------------------------------------------------------------
00183 
00184 GlobalWeapon::GlobalWeapon(uint32 id, uint32 count) :
00185   GlobalObject(id, count)
00186 {
00187   if (_id <= MAX_ITEM_ID || _id > MAX_WEAPON_ID) {
00188     cerr << "GLOBAL ERROR: GlobalWeapon constructor failed due to an invalid id assignment: " << _id << endl;
00189     exit(1);
00190   }
00191 
00192   ReadScriptDescriptor& script_file = GlobalManager->_weapons_script;
00193 
00194   if (script_file.DoesTableExist(_id) == false) {
00195     cerr << "GLOBAL ERROR: GlobalWeapon constructor failed because the table containing the "
00196       << "weapon definition did not exist for weapon id: " << _id << endl;
00197     exit(1);
00198   }
00199 
00200   // Load the weapon data from the script
00201   script_file.OpenTable(_id);
00202   _name = MakeUnicodeString(script_file.ReadString("name"));
00203   _description = MakeUnicodeString(script_file.ReadString("description"));
00204   _icon_image.SetFilename(script_file.ReadString("icon"));
00205   _physical_attack = script_file.ReadUInt("physical_attack");
00206   _metaphysical_attack = script_file.ReadUInt("metaphysical_attack");
00207   _price = script_file.ReadUInt("standard_price");
00208   _usable_by = script_file.ReadUInt("usable_by");
00209 
00210   if (_icon_image.Load() == false) {
00211     if (GLOBAL_DEBUG)
00212       cerr << "GLOBAL WARNING: GlobalWeapon constructor failed to load the icon image for the weapon: " << _id << endl;
00213   }
00214 
00215   script_file.CloseTable();
00216 
00217   if (script_file.IsErrorDetected()) {
00218     if (GLOBAL_DEBUG) {
00219       cerr << "GLOBAL WARNING: GlobalWeapon constructor incurred script reading errors. They are as follows: " << endl;
00220       cerr << script_file.GetErrorMessages() << endl;
00221     }
00222     return;
00223   }
00224 } // void GlobalWeapon::GlobalWeapon(uint32 id, uint32 count = 1)
00225 
00226 // -----------------------------------------------------------------------------
00227 // GlobalArmor class
00228 // -----------------------------------------------------------------------------
00229 
00230 GlobalArmor::GlobalArmor(uint32 id, uint32 count) :
00231   GlobalObject(id, count)
00232 {
00233   if (_id <= MAX_WEAPON_ID || _id > MAX_LEG_ARMOR_ID) {
00234     cerr << "GLOBAL ERROR: GlobalArmor constructor failed due to an invalid id assignment: " << _id << endl;
00235     exit(1);
00236   }
00237 
00238   // Figure out the appropriate script reference to grab based on the id value
00239   ReadScriptDescriptor* script_file;
00240   if (_id <= MAX_HEAD_ARMOR_ID) {
00241     script_file = &(GlobalManager->_head_armor_script);
00242   }
00243   else if (_id <= MAX_TORSO_ARMOR_ID) {
00244     script_file = &(GlobalManager->_torso_armor_script);
00245   }
00246   else if (_id <= MAX_ARM_ARMOR_ID) {
00247     script_file = &(GlobalManager->_arm_armor_script);
00248   }
00249   else {
00250     script_file = &(GlobalManager->_leg_armor_script);
00251   }
00252 
00253   if (script_file->DoesTableExist(_id) == false) {
00254     cerr << "GLOBAL ERROR: GlobalArmor constructor failed because the table containing the "
00255       << "armor definition did not exist for armor id: " << _id << endl;
00256     exit(1);
00257   }
00258 
00259   // Load the armor data from the script
00260   script_file->OpenTable(_id);
00261   _name = MakeUnicodeString(script_file->ReadString("name"));
00262   _description = MakeUnicodeString(script_file->ReadString("description"));
00263   _icon_image.SetFilename(script_file->ReadString("icon"));
00264   _physical_defense = script_file->ReadUInt("physical_defense");
00265   _metaphysical_defense = script_file->ReadUInt("metaphysical_defense");
00266   _price = script_file->ReadUInt("standard_price");
00267   _usable_by = script_file->ReadUInt("usable_by");
00268 
00269   if (_icon_image.Load() == false) {
00270     if (GLOBAL_DEBUG)
00271       cerr << "GLOBAL WARNING: GlobalArmor constructor failed to load the icon image for the armor: " << _id << endl;
00272   }
00273 
00274   script_file->CloseTable();
00275 
00276   if (script_file->IsErrorDetected()) {
00277     if (GLOBAL_DEBUG) {
00278       cerr << "GLOBAL WARNING: GlobalArmor constructor incurred script reading errors. They are as follows: " << endl;
00279       cerr << script_file->GetErrorMessages() << endl;
00280     }
00281     return;
00282   }
00283 } // void GlobalArmor::GlobalArmor(uint32 id, uint32 count = 1)
00284 
00285 
00286 
00287 GLOBAL_OBJECT GlobalArmor::GetObjectType() const {
00288   if (_id <= MAX_HEAD_ARMOR_ID)
00289     return GLOBAL_OBJECT_HEAD_ARMOR;
00290   else if (_id <= MAX_TORSO_ARMOR_ID)
00291     return GLOBAL_OBJECT_TORSO_ARMOR;
00292   else if (_id <= MAX_ARM_ARMOR_ID)
00293     return GLOBAL_OBJECT_ARM_ARMOR;
00294   else
00295     return GLOBAL_OBJECT_LEG_ARMOR;
00296 }
00297 
00298 } // namespace hoa_global

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