Planeshift

tribe.h

Go to the documentation of this file.
00001 /*
00002 * tribe.h
00003 *
00004 * Copyright (C) 2006 Atomic Blue ([email protected], http://www.atomicblue.org)
00005 *
00006 *
00007 * This program is free software; you can redistribute it and/or
00008 * modify it under the terms of the GNU General Public License
00009 * as published by the Free Software Foundation (version 2 of the License)
00010 * This program is distributed in the hope that it will be useful,
00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 * GNU General Public License for more details.
00014 * You should have received a copy of the GNU General Public License
00015 * along with this program; if not, write to the Free Software
00016 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 *
00018 */
00019 #ifndef __TRIBE_H__
00020 #define __TRIBE_H__
00021 
00022 //=============================================================================
00023 // Crystal Space Includes
00024 //=============================================================================
00025 #include <csutil/array.h>
00026 #include <csutil/list.h>
00027 #include <csutil/priorityqueue.h>
00028 #include <csutil/weakref.h>
00029 #include <csutil/parray.h>
00030 
00031 #include <csgeom/vector3.h>
00032 #include <iengine/sector.h>
00033 
00034 //=============================================================================
00035 // Project Includes
00036 //=============================================================================
00037 #include <util/psconst.h>
00038 #include <util/psutil.h>
00039 #include <util/remotedebug.h>
00040 
00041 //=============================================================================
00042 // Local Includes
00043 //=============================================================================
00044 
00049 class iResultRow;
00050 class EventManager;
00051 class NPC;
00052 class gemNPCItem;
00053 class Perception;
00054 class gemNPCActor;
00055 class Recipe;
00056 class RecipeManager;
00057 class RecipeTreeNode;
00058 class EventManager;
00059 
00060 #define TRIBE_UNLIMITED_SIZE   100
00061 
00065 class Tribe : public ScopedTimerCB, public RemoteDebug
00066 {
00067 public:
00068     typedef csHash<csString,csString> BufferHash;
00069 
00070     typedef enum
00071     {
00072         ASSET_TYPE_ITEM,
00073         ASSET_TYPE_BUILDING,
00074         ASSET_TYPE_BUILDINGSPOT
00075     } AssetType;
00076     static const char* AssetTypeStr[];
00077 
00078     typedef enum
00079     {
00080         ASSET_STATUS_NOT_APPLICABLE,
00081         ASSET_STATUS_NOT_USED,
00082         ASSET_STATUS_INCONSTRUCTION,
00083         ASSET_STATUS_CONSTRUCTED
00084     } AssetStatus;
00085     static const char* AssetStatusStr[];
00086 
00087     struct Resource
00088     {
00089         int      id;                     
00090         csString name;
00091         csString nick;
00092         int      amount;
00093     };
00094 
00095     struct Asset
00096     {
00097         int                   id;
00098         AssetType             type;      
00099         csString              name;      
00100         uint32_t              itemUID;   
00101         csWeakRef<gemNPCItem> item;      
00102         int                   quantity;  
00103         csVector3             pos;       
00104         csString              sectorName;
00105         iSector*              sector;    
00106         AssetStatus           status;    
00107 
00111         void        Save();
00112 
00116         iSector*    GetSector();
00117 
00121         gemNPCItem* GetItem();
00122     };
00123 
00124     struct Memory
00125     {
00126         int       id;          
00127         csString  name;
00128         csVector3 pos;
00129         iSector*  sector;
00130         csString  sectorName;  
00131         float     radius;
00132         NPC*      npc;         
00133 
00134         iSector* GetSector();
00135     };
00136 
00137     struct MemberID
00138     {
00139         PID       pid;
00140         csString  tribeMemberType; 
00141     };
00142 
00143     struct CyclicRecipe
00144     {
00145         Recipe* recipe;     
00146         int     timeTotal;  
00147         int     timeLeft;   
00148     };
00149 
00153     Tribe(EventManager* eventmngr, RecipeManager* rm);
00154 
00158     virtual ~Tribe();
00159 
00163     bool Load(iResultRow &row);
00164 
00168     bool LoadMember(iResultRow &row);
00169 
00173     bool LoadResource(iResultRow &row);
00174 
00178     bool AddMember(PID pid, const char* tribeMemberType);
00179 
00183     void SaveResource(Resource* resource, bool newResource);
00184 
00188     bool CheckAttach(NPC* npc);
00189 
00193     bool AttachMember(NPC* npc, const char* tribeMemberType);
00194 
00198     bool HandleDeath(NPC* npc);
00199 
00203     int AliveCount() const;
00204 
00208     void HandlePerception(NPC* npc, Perception* perception);
00209 
00213     void AddResource(csString resource, int amount, csString nick = "");
00214 
00218     int CountResource(csString resource) const;
00219 
00223     void Advance(csTicks when,EventManager* eventmgr);
00224 
00225     int GetID()
00226     {
00227         return id;
00228     }
00229     const char* GetName()
00230     {
00231         return name.GetDataSafe();
00232     }
00233     size_t GetMemberIDCount()
00234     {
00235         return membersId.GetSize();
00236     }
00237     size_t GetMemberCount()
00238     {
00239         return members.GetSize();
00240     }
00241     NPC* GetMember(size_t i)
00242     {
00243         return members[i];
00244     }
00245     size_t GetResourceCount()
00246     {
00247         return resources.GetSize();
00248     }
00249     const Resource &GetResource(size_t n)
00250     {
00251         return resources[n];
00252     }
00253     csList<Memory*>::Iterator GetMemoryIterator()
00254     {
00255         csList<Memory*>::Iterator it(memories);
00256         return it;
00257     };
00258     csString GetNPCIdleBehavior()
00259     {
00260         return npcIdleBehavior;
00261     }
00262     csVector3 GetHomePosition()
00263     {
00264         return homePos;
00265     }
00266     iSector* GetHomeSector();
00267     csString GetHomeSectorName()
00268     {
00269         return homeSectorName;
00270     }
00271 
00275     int GetMaxSize() const;
00276 
00277 
00281     int GetReproductionCost() const;
00282 
00286     void GetHome(csVector3 &pos, float &radius, iSector* &sector);
00287 
00291     void SetHome(const csVector3 &pos, float radius, iSector* sector);
00292 
00301     bool CheckWithinBoundsTribeHome(NPC* npc, const csVector3 &pos, const iSector* sector);
00302 
00306     bool GetResource(NPC* npc, csVector3 startPos, iSector* startSector,
00307                      csVector3 &pos, iSector* &sector, float range, bool random);
00308 
00313     Tribe::Memory* GetResource(csString resourceName, NPC* npc);
00314 
00318     const char* GetNeededResource();
00319 
00323     const char* GetNeededResourceNick();
00324 
00328     const char* GetNeededResourceAreaType();
00329 
00337     float GetWealthResourceGrowth() const;
00338 
00346     float GetWealthResourceGrowthActive() const;
00347 
00355     int GetWealthResourceGrowthActiveLimit() const;
00356 
00357 
00361     bool CanGrow() const;
00362 
00367     bool ShouldGrow() const;
00368 
00374     void Memorize(NPC* npc, Perception* perception);
00375 
00379     Memory* FindPrivMemory(csString name,const csVector3 &pos, iSector* sector, float radius, NPC* npc);
00380 
00384     Memory* FindMemory(csString name,const csVector3 &pos, iSector* sector, float radius);
00385 
00389     Memory* FindMemory(csString name);
00390 
00394     void AddMemory(csString name,const csVector3 &pos, iSector* sector, float radius, NPC* npc);
00395 
00399     void ShareMemories(NPC* npc);
00400 
00404     void SaveMemory(Memory* memory);
00405 
00409     bool LoadMemory(iResultRow &row);
00410 
00414     void ForgetMemories(NPC* npc);
00415 
00419     Memory* FindNearestMemory(const char* name,const csVector3 &pos, const iSector* sector, float range = -1.0, float* foundRange = NULL);
00420 
00424     Memory* FindRandomMemory(const char* name,const csVector3 &pos, const iSector* sector, float range = -1.0, float* foundRange = NULL);
00425 
00439     void TriggerEvent(Perception* pcpt, float maxRange=-1.0,
00440                       csVector3* basePos=NULL, iSector* baseSector=NULL);
00441 
00451     void SendPerception(const char* pcpt, csArray<NPC*> npcs);
00452 
00460     void SendPerception(const char* pcpt);
00461 
00476     gemNPCActor* GetMostHated(NPC* npc, float range, bool includeOutsideRegion,
00477                               bool includeInvisible, bool includeInvincible, float* hate=NULL);
00478 
00482     virtual void ScopedTimerCallback(const ScopedTimer* timer);
00483 
00484 
00490     float GetDeathRate()
00491     {
00492         return deathRate;
00493     }
00494 
00500     float GetResourceRate()
00501     {
00502         return resourceRate;
00503     }
00504 
00508     void SetRecipeManager(RecipeManager* rm)
00509     {
00510         recipeManager = rm;
00511     }
00512 
00516     Recipe* GetTribalRecipe();
00517 
00521     void UpdateRecipeData(int delta);
00522 
00533     void AddRecipe(Recipe* recipe, Recipe* parentRecipe, bool reqType = false);
00534 
00544     void AddCyclicRecipe(Recipe* recipe, int time);
00545 
00549     void DeleteCyclicRecipe(Recipe* recipe);
00550 
00554     void AddKnowledge(csString knowHow)
00555     {
00556         knowledge.Push(knowHow);
00557     }
00558 
00562     bool CheckKnowledge(csString knowHow);
00563 
00567     void SaveKnowledge(csString knowHow);
00568 
00572     void DumpKnowledge();
00573 
00577     void DumpRecipesToConsole();
00578 
00589     bool LoadNPCMemoryBuffer(const char* name, csArray<NPC*> npcs);
00590 
00594     void LoadNPCMemoryBuffer(Tribe::Memory* memory, csArray<NPC*> npcs);
00595 
00599     bool CheckMembers(const csString &type, int number);
00600 
00604     bool CheckResource(csString resource, int number);
00605 
00609     bool CheckAsset(Tribe::AssetType type, csString name, int number);
00610 
00614     size_t AssetQuantity(Tribe::AssetType type, csString name);
00615 
00619     void Build(NPC* npc, bool pickupable);
00620 
00624     void Unbuild(NPC* npc, gemNPCItem* building);
00625 
00629     void HandlePersistItem(gemNPCItem* item);
00630 
00634     csArray<NPC*> SelectNPCs(const csString &type, const char* number);
00635 
00644     csString GetBuffer(const csString &bufferName);
00645 
00652     void SetBuffer(const csString &bufferName, const csString &value);
00653 
00659     void ReplaceBuffers(csString &result);
00660 
00664     void ModifyWait(Recipe* recipe, int delta);
00665 
00669     void LoadAsset(iResultRow &row);
00670 
00674     void SaveAsset(Tribe::Asset* asset, bool deletion = false);
00675 
00679     void AddAsset(Tribe::AssetType type, csString name, gemNPCItem* item, int quantity, int id = -1);
00680 
00684     Tribe::Asset* AddAsset(Tribe::AssetType type, csString name, csVector3 position, iSector* sector, Tribe::AssetStatus status);
00685 
00689     void RemoveAsset(Tribe::Asset* asset);
00690 
00694     Asset* GetAsset(Tribe::AssetType type, csString name);
00695 
00699     Asset* GetAsset(Tribe::AssetType type, csString name, Tribe::AssetStatus status);
00700 
00704     Asset* GetAsset(Tribe::AssetType type, csString name, csVector3 where, iSector* sector);
00705 
00709     Asset* GetAsset(gemNPCItem* item);
00710 
00714     Asset* GetAsset(uint32_t itemUID);
00715 
00719     Asset* GetRandomAsset(Tribe::AssetType type, Tribe::AssetStatus status, csVector3 pos, iSector* sector, float range);
00720 
00724     Asset* GetNearestAsset(Tribe::AssetType type, Tribe::AssetStatus status, csVector3 pos, iSector* sector, float range, float* locatedRange = NULL);
00725 
00729     Asset* GetRandomAsset(Tribe::AssetType type, csString name, Tribe::AssetStatus status, csVector3 pos, iSector* sector, float range);
00730 
00734     Asset* GetNearestAsset(Tribe::AssetType type, csString name, Tribe::AssetStatus status, csVector3 pos, iSector* sector, float range, float* locatedRange = NULL);
00735 
00739     void DeleteAsset(csString name, int quantity);
00740 
00744     void DeleteAsset(csString name, csVector3 pos);
00745 
00749     void DumpAssets();
00750 
00754     void DumpBuffers();
00755 
00769     void ProspectMine(NPC* npc, csString resource, csString nick);
00770 
00771 private:
00775     virtual void LocalDebugReport(const csString &debugString);
00776 
00780     virtual void RemoteDebugReport(uint32_t clientNum, const csString &debugString);
00781 
00785     void UpdateDeathRate();
00786 
00790     void UpdateResourceRate(int amount);
00791 
00792     int                       id;                               
00793     csString                  name;                             
00794     csArray<MemberID>         membersId;                        
00795     csArray<NPC*>             members;                          
00796     csArray<NPC*>             deadMembers;                      
00797     csArray<Resource>         resources;                        
00798     csArray<csString>         knowledge;                        
00799     csPDelArray<Asset>        assets;                           
00800     csArray<CyclicRecipe>     cyclicRecipes;                    
00801     RecipeTreeNode*           tribalRecipe;                     
00802 
00805     csVector3                 homePos;                          
00806     float                     homeRadius;                       
00807     csString                  homeSectorName;                   
00808     iSector*                  homeSector;                       
00809 
00811     int                       maxSize;
00812     BufferHash                tribeBuffer;                      
00813     csString                  wealthResourceName;
00814     csString                  wealthResourceNick;
00815     csString                  wealthResourceArea;
00816     float                     wealthResourceGrowth;
00817     float                     wealthResourceGrowthActive;
00818     int                       wealthResourceGrowthActiveLimit;
00819     float                     accWealthGrowth;                  
00820     int                       reproductionCost;
00821     csString                  npcIdleBehavior;                  
00822     csString                  wealthGatherNeed;
00823     csList<Memory*>           memories;
00824 
00825     csTicks                   lastGrowth;
00826     csTicks                   lastAdvance;
00827 
00828 
00829     float                     deathRate;                        
00830     float                     resourceRate;                     
00831     csTicks                   lastDeath;                        
00832     csTicks                   lastResource;                     
00833 
00834     EventManager*             eventManager;                     
00835     RecipeManager*            recipeManager;                    
00836 };
00837 
00840 #endif