global.h

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 
00023 #ifndef __GLOBAL_HEADER__
00024 #define __GLOBAL_HEADER__
00025 
00026 #include "defs.h"
00027 #include "utils.h"
00028 #include "script.h"
00029 
00030 #include "global_objects.h"
00031 #include "global_actors.h"
00032 #include "global_skills.h"
00033 
00035 namespace hoa_global {
00036 
00038 extern GameGlobal* GlobalManager;
00039 
00041 extern bool GLOBAL_DEBUG;
00042 
00044 const int32 GLOBAL_BAD_EVENT = -5555555;
00045 
00068 class GlobalEventGroup {
00069   friend class GameGlobal;
00070 
00071 public:
00072   ~GlobalEventGroup() {}
00073 
00078   bool DoesEventExist(const std::string& event_name)
00079     { if (_events.find(event_name) != _events.end()) return true; else return false; }
00080 
00087   void AddNewEvent(const std::string& event_name, int32 event_value = 0);
00088 
00093   int32 GetEvent(const std::string& event_name);
00094 
00100   void SetEvent(const std::string& event_name, int32 event_value);
00101 
00103   uint32 GetNumberEvents() const
00104     { return _events.size(); }
00105 
00107   std::string GetGroupName() const
00108     { return _group_name; }
00109 
00110 private:
00112   std::string _group_name;
00113 
00119   std::map<std::string, int32> _events;
00120 
00124   GlobalEventGroup(const std::string& group_name) :
00125     _group_name(group_name) {}
00126 
00128 
00129   GlobalEventGroup(const GlobalEventGroup& geg);
00130   GlobalEventGroup& operator=(const GlobalEventGroup& geg);
00132 }; // class GlobalEventGroup
00133 
00134 
00147 class GameGlobal : public hoa_utils::Singleton<GameGlobal> {
00148   friend class hoa_utils::Singleton<GameGlobal>;
00149   friend class GlobalItem;
00150   friend class GlobalWeapon;
00151   friend class GlobalArmor;
00152   friend class GlobalSkill;
00153 
00154 public:
00155   ~GameGlobal();
00156 
00157   bool SingletonInitialize();
00158 
00164   void ClearAllData();
00165 
00167 
00168 
00179   void AddCharacter(uint32 id);
00180 
00192   void AddCharacter(GlobalCharacter* ch);
00193 
00197   void RemoveCharacter(uint32 id);
00198 
00203   GlobalCharacter* GetCharacter(uint32 id);
00204 
00209   bool IsCharacterInParty(uint32 id)
00210     { if (_characters.find(id) != _characters.end()) return true; else return false; }
00212 
00214 
00215 
00221   void AddToInventory(uint32 obj_id, uint32 obj_count = 1);
00222 
00233   void AddToInventory(GlobalObject* object);
00234 
00243   void RemoveFromInventory(uint32 obj_id);
00244 
00255   GlobalObject* RetrieveFromInventory(uint32 obj_id, bool all_counts = false);
00256 
00266   void IncrementObjectCount(uint32 obj_id, uint32 obj_count = 1);
00267 
00278   void DecrementObjectCount(uint32 obj_id, uint32 obj_count = 1);
00279 
00284   bool IsObjectInInventory(uint32 id)
00285     { if (_inventory.find(id) != _inventory.end()) return true; else return false; }
00287 
00289 
00290 
00294   bool DoesEventGroupExist(const std::string& group_name) const
00295     { if (_event_groups.find(group_name) != _event_groups.end()) return true; else return false; }
00296 
00302   bool DoesEventExist(const std::string& group_name, const std::string& event_name) const;
00303 
00310   void AddNewEventGroup(const std::string& group_name);
00311 
00323   GlobalEventGroup* GetEventGroup(const std::string& group_name) const;
00324 
00330   int32 GetEventValue(const std::string& group_name, const std::string& event_name) const;
00331 
00333   uint32 GetNumberEventGroups() const
00334     { return _event_groups.size(); }
00335 
00340   uint32 GetNumberEvents(const std::string& group_name) const;
00342 
00344   void AddDrunes(uint32 amount)
00345     { _drunes += amount; }
00346 
00348   void SubtractDrunes(uint32 amount)
00349     { if (_drunes >= amount) _drunes -= amount; }
00350 
00355   uint32 AverageActivePartyExperienceLevel() const
00356     { return static_cast<uint32>(_active_party.AverageExperienceLevel()); }
00357 
00362   void SetLocation(const hoa_utils::ustring& location_name, const std::string& location_graphic_filename);
00363 
00368   bool SaveGame(std::string& filename);
00369 
00374   bool LoadGame(const std::string& filename);
00375 
00377 
00378   void SetDrunes(uint32 amount)
00379     { _drunes = amount; }
00380 
00381   uint32 GetDrunes() const
00382     { return _drunes; }
00383 
00384   hoa_utils::ustring& GetLocationName()
00385     { return _location_name; }
00386 
00387   hoa_video::StillImage& GetLocationGraphic()
00388     { return _location_graphic; }
00389 
00390   std::vector<GlobalCharacter*>* GetCharacterOrder()
00391     { return &_character_order; };
00392 
00393   GlobalParty* GetActiveParty()
00394     { return &_active_party; }
00395 
00396   std::map<uint32, GlobalObject*>* GetInventory()
00397     { return &_inventory; }
00398 
00399   std::vector<GlobalItem*>* GetInventoryItems()
00400     { return &_inventory_items; }
00401 
00402   std::vector<GlobalWeapon*>* GetInventoryWeapons()
00403     { return &_inventory_weapons; }
00404 
00405   std::vector<GlobalArmor*>* GetInventoryHeadArmor()
00406     { return &_inventory_head_armor; }
00407 
00408   std::vector<GlobalArmor*>* GetInventoryTorsoArmor()
00409     { return &_inventory_torso_armor; }
00410 
00411   std::vector<GlobalArmor*>* GetInventoryArmArmor()
00412     { return &_inventory_arm_armor; }
00413 
00414   std::vector<GlobalArmor*>* GetInventoryLegArmor()
00415     { return &_inventory_leg_armor; }
00416 
00417   std::vector<GlobalShard*>* GetInventoryShards()
00418     { return &_inventory_shards; }
00419 
00420   std::vector<GlobalKeyItem*>* GetInventoryKeyItems()
00421     { return &_inventory_key_items; }
00423 
00424 private:
00425   GameGlobal();
00426 
00428   uint32 _drunes;
00429 
00431   hoa_utils::ustring _location_name;
00432 
00434   hoa_video::StillImage _location_graphic;
00435 
00440   std::map<uint32, GlobalCharacter*> _characters;
00441 
00445   std::vector<GlobalCharacter*> _character_order;
00446 
00451   GlobalParty _active_party;
00452 
00459   std::map<uint32, GlobalObject*> _inventory;
00460 
00466   std::vector<GlobalItem*>    _inventory_items;
00467   std::vector<GlobalWeapon*>  _inventory_weapons;
00468   std::vector<GlobalArmor*>   _inventory_head_armor;
00469   std::vector<GlobalArmor*>   _inventory_torso_armor;
00470   std::vector<GlobalArmor*>   _inventory_arm_armor;
00471   std::vector<GlobalArmor*>   _inventory_leg_armor;
00472   std::vector<GlobalShard*>   _inventory_shards;
00473   std::vector<GlobalKeyItem*> _inventory_key_items;
00475 
00477 
00478   hoa_script::ReadScriptDescriptor _items_script;
00479   hoa_script::ReadScriptDescriptor _weapons_script;
00480   hoa_script::ReadScriptDescriptor _head_armor_script;
00481   hoa_script::ReadScriptDescriptor _torso_armor_script;
00482   hoa_script::ReadScriptDescriptor _arm_armor_script;
00483   hoa_script::ReadScriptDescriptor _leg_armor_script;
00484   // hoa_script::ReadScriptDescriptor _shard_script;
00485   // hoa_script::ReadScriptDescriptor _key_items_script;
00486   hoa_script::ReadScriptDescriptor _attack_skills_script;
00487   hoa_script::ReadScriptDescriptor _defend_skills_script;
00488   hoa_script::ReadScriptDescriptor _support_skills_script;
00490 
00494   std::map<std::string, GlobalEventGroup*> _event_groups;
00495 
00496   // ----- Private methods
00497 
00503   template <class T> bool _RemoveFromInventory(uint32 obj_id, std::vector<T*>& inv);
00504 
00511   template <class T> T* _RetrieveFromInventory(uint32 obj_id, std::vector<T*>& inv, bool all_counts);
00512 
00519   template <class T> void _SaveInventory(hoa_script::WriteScriptDescriptor& file, std::string name, std::vector<T*>& inv);
00520 
00527   void _SaveCharacter(hoa_script::WriteScriptDescriptor& file, GlobalCharacter* character, bool last);
00528 
00534   void _SaveEvents(hoa_script::WriteScriptDescriptor& file, GlobalEventGroup* event_group);
00535 
00540   void _LoadInventory(hoa_script::ReadScriptDescriptor& file, std::string category_name);
00541 
00546   void _LoadCharacter(hoa_script::ReadScriptDescriptor& file, uint32 id);
00547 
00552   void _LoadEvents(hoa_script::ReadScriptDescriptor& file, const std::string& group_name);
00553 }; // class GameGlobal : public hoa_utils::Singleton<GameGlobal>
00554 
00555 //-----------------------------------------------------------------------------
00556 // Template Function Definitions
00557 //-----------------------------------------------------------------------------
00558 
00559 template <class T> bool GameGlobal::_RemoveFromInventory(uint32 obj_id, std::vector<T*>& inv) {
00560   for (typename std::vector<T*>::iterator i = inv.begin(); i != inv.end(); i++) {
00561     if ((*i)->GetID() == obj_id) {
00562       delete _inventory[obj_id];
00563       _inventory.erase(obj_id);
00564       inv.erase(i);
00565       return true;
00566     }
00567   }
00568 
00569   return false;
00570 } // template <class T> bool GameGlobal::_RemoveFromInventory(uint32 obj_id, std::vector<T*>& inv)
00571 
00572 
00573 
00574 template <class T> T* GameGlobal::_RetrieveFromInventory(uint32 obj_id, std::vector<T*>& inv, bool all_counts) {
00575   for (typename std::vector<T*>::iterator i = inv.begin(); i != inv.end(); i++) {
00576     if ((*i)->GetID() == obj_id) {
00577       T* return_object;
00578       if (all_counts == true || _inventory[obj_id]->GetCount() == 1) {
00579         return_object = *i;
00580         _inventory.erase(obj_id);
00581         inv.erase(i);
00582       }
00583       else {
00584         return_object = new T(**i);
00585         return_object->SetCount(1);
00586         _inventory[obj_id]->DecrementCount();
00587       }
00588       return return_object;
00589     }
00590   }
00591 
00592   return NULL;
00593 } // template <class T> T* GameGlobal::_RetrieveFromInventory(uint32 obj_id, std::vector<T*>& inv, bool all_counts)
00594 
00595 
00596 
00597 template <class T> void GameGlobal::_SaveInventory(hoa_script::WriteScriptDescriptor& file, std::string name, std::vector<T*>& inv) {
00598   if (file.IsFileOpen() == false) {
00599     if (GLOBAL_DEBUG)
00600       std::cerr << "GLOBAL WARNING: GameGlobal::_SaveInventory() failed because the file passed to it was not open" << std::endl;
00601     return;
00602   }
00603 
00604   file.InsertNewLine();
00605   file.WriteLine(name + " = {");
00606   for (uint32 i = 0; i < inv.size(); i++) {
00607     if (i == 0)
00608       file.WriteLine("\t", false);
00609     else
00610       file.WriteLine(", ", false);
00611     file.WriteLine("[" + NumberToString(inv[i]->GetID()) + "] = "
00612       + NumberToString(inv[i]->GetCount()), false);
00613   }
00614   file.InsertNewLine();
00615   file.WriteLine("}");
00616 } // template <class T> void GameGlobal::_SaveInventory(hoa_script::WriteScriptDescriptor& file, std::string name, std::vector<T*>& inv)
00617 
00618 } // namespace hoa_global
00619 
00620 #endif

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