Planeshift
tribe.h
Go to the documentation of this file.
1 /*
2 * tribe.h
3 *
4 * Copyright (C) 2006 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
5 *
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation (version 2 of the License)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 */
19 #ifndef __TRIBE_H__
20 #define __TRIBE_H__
21 
22 //=============================================================================
23 // Crystal Space Includes
24 //=============================================================================
25 #include <csutil/array.h>
26 #include <csutil/list.h>
27 #include <csutil/priorityqueue.h>
28 #include <csutil/weakref.h>
29 #include <csutil/parray.h>
30 
31 #include <csgeom/vector3.h>
32 #include <iengine/sector.h>
33 
34 //=============================================================================
35 // Project Includes
36 //=============================================================================
37 #include <util/psconst.h>
38 #include <util/psutil.h>
39 #include <util/remotedebug.h>
40 
41 //=============================================================================
42 // Local Includes
43 //=============================================================================
44 
49 class iResultRow;
50 class EventManager;
51 class NPC;
52 class gemNPCItem;
53 class Perception;
54 class gemNPCActor;
55 class Recipe;
56 class RecipeManager;
57 class RecipeTreeNode;
58 class EventManager;
59 
60 #define TRIBE_UNLIMITED_SIZE 100
61 
67 class Tribe : public ScopedTimerCB, public RemoteDebug
68 {
69 public:
70  typedef csHash<csString,csString> BufferHash;
71 
72  typedef enum
73  {
77  } AssetType;
78  static const char* AssetTypeStr[];
79 
80  typedef enum
81  {
86  } AssetStatus;
87  static const char* AssetStatusStr[];
88 
89  struct Resource
90  {
91  int id;
92  csString name;
93  csString nick;
94  int amount;
95  };
96 
97  struct Asset
98  {
99  int id;
100  AssetType type;
101  csString name;
102  uint32_t itemUID;
103  csWeakRef<gemNPCItem> item;
104  int quantity;
105  csVector3 pos;
106  csString sectorName;
107  iSector* sector;
108  AssetStatus status;
109 
113  void Save();
114 
118  iSector* GetSector();
119 
123  gemNPCItem* GetItem();
124  };
125 
126  struct Memory
127  {
128  int id;
129  csString name;
130  csVector3 pos;
131  iSector* sector;
132  csString sectorName;
133  float radius;
134  NPC* npc;
135 
136  iSector* GetSector();
137  };
138 
139  struct MemberID
140  {
141  PID pid;
142  csString tribeMemberType;
143  };
144 
146  {
148  int timeTotal;
149  int timeLeft;
150  };
151 
155  Tribe(EventManager* eventmngr, RecipeManager* rm);
156 
160  virtual ~Tribe();
161 
165  bool Load(iResultRow &row);
166 
170  bool LoadMember(iResultRow &row);
171 
175  bool LoadResource(iResultRow &row);
176 
180  bool AddMember(PID pid, const char* tribeMemberType);
181 
185  void SaveResource(Resource* resource, bool newResource);
186 
190  bool CheckAttach(NPC* npc);
191 
195  bool AttachMember(NPC* npc, const char* tribeMemberType);
196 
200  bool HandleDeath(NPC* npc);
201 
205  int AliveCount() const;
206 
210  void HandlePerception(NPC* npc, Perception* perception);
211 
215  void AddResource(csString resource, int amount, csString nick = "");
216 
220  int CountResource(csString resource) const;
221 
225  void Advance(csTicks when,EventManager* eventmgr);
226 
227  int GetID()
228  {
229  return id;
230  }
231  const char* GetName()
232  {
233  return name.GetDataSafe();
234  }
236  {
237  return membersId.GetSize();
238  }
239  size_t GetMemberCount()
240  {
241  return members.GetSize();
242  }
243  NPC* GetMember(size_t i)
244  {
245  return members[i];
246  }
248  {
249  return resources.GetSize();
250  }
251  const Resource &GetResource(size_t n)
252  {
253  return resources[n];
254  }
255  csList<Memory*>::Iterator GetMemoryIterator()
256  {
257  csList<Memory*>::Iterator it(memories);
258  return it;
259  };
261  {
262  return npcIdleBehavior;
263  }
264  csVector3 GetHomePosition()
265  {
266  return homePos;
267  }
268  iSector* GetHomeSector();
269  csString GetHomeSectorName()
270  {
271  return homeSectorName;
272  }
273 
277  int GetMaxSize() const;
278 
279 
283  int GetReproductionCost() const;
284 
288  void GetHome(csVector3 &pos, float &radius, iSector* &sector);
289 
293  void SetHome(const csVector3 &pos, float radius, iSector* sector);
294 
303  bool CheckWithinBoundsTribeHome(NPC* npc, const csVector3 &pos, const iSector* sector);
304 
308  bool GetResource(NPC* npc, csVector3 startPos, iSector* startSector,
309  csVector3 &pos, iSector* &sector, float range, bool random);
310 
315  Tribe::Memory* GetResource(csString resourceName, NPC* npc);
316 
320  const char* GetNeededResource();
321 
325  const char* GetNeededResourceNick();
326 
330  const char* GetNeededResourceAreaType();
331 
339  float GetWealthResourceGrowth() const;
340 
348  float GetWealthResourceGrowthActive() const;
349 
358 
359 
363  bool CanGrow() const;
364 
369  bool ShouldGrow() const;
370 
376  void Memorize(NPC* npc, Perception* perception);
377 
381  Memory* FindPrivMemory(csString name,const csVector3 &pos, iSector* sector, float radius, NPC* npc);
382 
386  Memory* FindMemory(csString name,const csVector3 &pos, iSector* sector, float radius);
387 
391  Memory* FindMemory(csString name);
392 
396  void AddMemory(csString name,const csVector3 &pos, iSector* sector, float radius, NPC* npc);
397 
401  void ShareMemories(NPC* npc);
402 
406  void SaveMemory(Memory* memory);
407 
411  bool LoadMemory(iResultRow &row);
412 
416  void ForgetMemories(NPC* npc);
417 
421  Memory* FindNearestMemory(const char* name,const csVector3 &pos, const iSector* sector, float range = -1.0, float* foundRange = NULL);
422 
426  Memory* FindRandomMemory(const char* name,const csVector3 &pos, const iSector* sector, float range = -1.0, float* foundRange = NULL);
427 
441  void TriggerEvent(Perception* pcpt, float maxRange=-1.0,
442  csVector3* basePos=NULL, iSector* baseSector=NULL);
443 
453  void SendPerception(const char* pcpt, csArray<NPC*> npcs);
454 
462  void SendPerception(const char* pcpt);
463 
478  gemNPCActor* GetMostHated(NPC* npc, float range, bool includeOutsideRegion,
479  bool includeInvisible, bool includeInvincible, float* hate=NULL);
480 
484  virtual void ScopedTimerCallback(const ScopedTimer* timer);
485 
486 
492  float GetDeathRate()
493  {
494  return deathRate;
495  }
496 
503  {
504  return resourceRate;
505  }
506 
511  {
512  recipeManager = rm;
513  }
514 
519 
523  void UpdateRecipeData(int delta);
524 
535  void AddRecipe(Recipe* recipe, Recipe* parentRecipe, bool reqType = false);
536 
546  void AddCyclicRecipe(Recipe* recipe, int time);
547 
551  void DeleteCyclicRecipe(Recipe* recipe);
552 
556  void AddKnowledge(csString knowHow)
557  {
558  knowledge.Push(knowHow);
559  }
560 
564  bool CheckKnowledge(csString knowHow);
565 
569  void SaveKnowledge(csString knowHow);
570 
574  void DumpKnowledge();
575 
579  void DumpRecipesToConsole();
580 
591  bool LoadNPCMemoryBuffer(const char* name, csArray<NPC*> npcs);
592 
596  void LoadNPCMemoryBuffer(Tribe::Memory* memory, csArray<NPC*> npcs);
597 
601  bool CheckMembers(const csString &type, int number);
602 
606  bool CheckResource(csString resource, int number);
607 
611  bool CheckAsset(Tribe::AssetType type, csString name, int number);
612 
616  size_t AssetQuantity(Tribe::AssetType type, csString name);
617 
621  void Build(NPC* npc, bool pickupable);
622 
626  void Unbuild(NPC* npc, gemNPCItem* building);
627 
631  void HandlePersistItem(gemNPCItem* item);
632 
636  csArray<NPC*> SelectNPCs(const csString &type, const char* number);
637 
646  csString GetBuffer(const csString &bufferName);
647 
654  void SetBuffer(const csString &bufferName, const csString &value);
655 
661  void ReplaceBuffers(csString &result);
662 
666  void ModifyWait(Recipe* recipe, int delta);
667 
671  void LoadAsset(iResultRow &row);
672 
676  void SaveAsset(Tribe::Asset* asset, bool deletion = false);
677 
681  void AddAsset(Tribe::AssetType type, csString name, gemNPCItem* item, int quantity, int id = -1);
682 
686  Tribe::Asset* AddAsset(Tribe::AssetType type, csString name, csVector3 position, iSector* sector, Tribe::AssetStatus status);
687 
691  void RemoveAsset(Tribe::Asset* asset);
692 
696  Asset* GetAsset(Tribe::AssetType type, csString name);
697 
701  Asset* GetAsset(Tribe::AssetType type, csString name, Tribe::AssetStatus status);
702 
706  Asset* GetAsset(Tribe::AssetType type, csString name, csVector3 where, iSector* sector);
707 
711  Asset* GetAsset(gemNPCItem* item);
712 
716  Asset* GetAsset(uint32_t itemUID);
717 
721  Asset* GetRandomAsset(Tribe::AssetType type, Tribe::AssetStatus status, csVector3 pos, iSector* sector, float range);
722 
726  Asset* GetNearestAsset(Tribe::AssetType type, Tribe::AssetStatus status, csVector3 pos, iSector* sector, float range, float* locatedRange = NULL);
727 
731  Asset* GetRandomAsset(Tribe::AssetType type, csString name, Tribe::AssetStatus status, csVector3 pos, iSector* sector, float range);
732 
736  Asset* GetNearestAsset(Tribe::AssetType type, csString name, Tribe::AssetStatus status, csVector3 pos, iSector* sector, float range, float* locatedRange = NULL);
737 
741  void DeleteAsset(csString name, int quantity);
742 
746  void DeleteAsset(csString name, csVector3 pos);
747 
751  void DumpAssets();
752 
756  void DumpBuffers();
757 
771  void ProspectMine(NPC* npc, csString resource, csString nick);
772 
773 private:
777  virtual void LocalDebugReport(const csString &debugString);
778 
782  virtual void RemoteDebugReport(uint32_t clientNum, const csString &debugString);
783 
787  void UpdateDeathRate();
788 
792  void UpdateResourceRate(int amount);
793 
794  int id;
795  csString name;
796  csArray<MemberID> membersId;
797  csArray<NPC*> members;
798  csArray<NPC*> deadMembers;
799  csArray<Resource> resources;
800  csArray<csString> knowledge;
801  csPDelArray<Asset> assets;
802  csArray<CyclicRecipe> cyclicRecipes;
803  RecipeTreeNode* tribalRecipe;
804 
807  csVector3 homePos;
808  float homeRadius;
809  csString homeSectorName;
810  iSector* homeSector;
811 
813  int maxSize;
814  BufferHash tribeBuffer;
815  csString wealthResourceName;
816  csString wealthResourceNick;
817  csString wealthResourceArea;
818  float wealthResourceGrowth;
819  float wealthResourceGrowthActive;
820  int wealthResourceGrowthActiveLimit;
821  float accWealthGrowth;
822  int reproductionCost;
823  csString npcIdleBehavior;
824  csString wealthGatherNeed;
825  csList<Memory*> memories;
826 
827  csTicks lastGrowth;
828  csTicks lastAdvance;
829 
830 
831  float deathRate;
832  float resourceRate;
833  csTicks lastDeath;
834  csTicks lastResource;
835 
836  EventManager* eventManager;
837  RecipeManager* recipeManager;
838 };
839 
842 #endif
csString GetNPCIdleBehavior()
Definition: tribe.h:260
int quantity
Quantity of items of this type.
Definition: tribe.h:104
void DumpBuffers()
Dump Buffers.
void HandlePerception(NPC *npc, Perception *perception)
Handled a perception given to this tribe.
This embodies any perception an NPC might have, or any game event of interest.
Definition: perceptions.h:55
const char * GetNeededResourceAreaType()
Get a area for the most needed resource for this tribe.
size_t AssetQuantity(Tribe::AssetType type, csString name)
Return the quanitity of the given asset type matching the name.
Memory * FindPrivMemory(csString name, const csVector3 &pos, iSector *sector, float radius, NPC *npc)
Find a privat memory.
Check how long time it take to process a scope.
Definition: psutil.h:136
void SaveResource(Resource *resource, bool newResource)
Save or update an resource in database.
bool CheckKnowledge(csString knowHow)
Check if knowledge is known.
void DumpAssets()
Dump Assets.
virtual ~Tribe()
Destruct a tribe object.
bool LoadMember(iResultRow &row)
Load and add a new member to the tribe.
bool Load(iResultRow &row)
Load the tribe object.
void SaveMemory(Memory *memory)
Save a memory to the db.
void AddKnowledge(csString knowHow)
Add a knowledge token.
Definition: tribe.h:556
bool CheckAsset(Tribe::AssetType type, csString name, int number)
Check to see if enough assets are available.
Callback function for ScopedTimers.
Definition: psutil.h:121
void ModifyWait(Recipe *recipe, int delta)
Modify Wait Time for a recipe.
float radius
Definition: tribe.h:133
csHash< csString, csString > BufferHash
Definition: tribe.h:70
void SetRecipeManager(RecipeManager *rm)
Sets the tribe&#39;s recipe manager.
Definition: tribe.h:510
gemNPCActor * GetMostHated(NPC *npc, float range, bool includeOutsideRegion, bool includeInvisible, bool includeInvincible, float *hate=NULL)
Find the most hated entity for tribe within range.
int timeTotal
Total number of ticks to cycle.
Definition: tribe.h:148
bool AttachMember(NPC *npc, const char *tribeMemberType)
Attach a new member to the tribe.
csString sectorName
Name of the sector.
Definition: tribe.h:106
void SetHome(const csVector3 &pos, float radius, iSector *sector)
Set home position for the tribe.
AssetType type
Type of this asset.
Definition: tribe.h:100
void AddResource(csString resource, int amount, csString nick="")
Add a new resource to the tribe resource table.
csArray< NPC * > SelectNPCs(const csString &type, const char *number)
Returns pointers to required npcs for a task.
AssetType
Definition: tribe.h:72
int CountResource(csString resource) const
Return the amount of a given resource.
bool CheckAttach(NPC *npc)
Attach a new member to the tribe if the NPC is a member.
void DumpKnowledge()
Dump knowledge to console.
csString sectorName
Keep the sector name until sector is loaded.
Definition: tribe.h:132
bool CheckWithinBoundsTribeHome(NPC *npc, const csVector3 &pos, const iSector *sector)
Check if the position is within the bounds of the tribe home.
iSector * sector
Definition: tribe.h:131
void DeleteCyclicRecipe(Recipe *recipe)
Delete a cyclic recipe.
bool AddMember(PID pid, const char *tribeMemberType)
Adds a new member to the tribe.
csWeakRef< gemNPCItem > item
Item representing the asset.
Definition: tribe.h:103
bool CheckMembers(const csString &type, int number)
Check to see if enough members are idle.
csString GetHomeSectorName()
Definition: tribe.h:269
void SaveKnowledge(csString knowHow)
Save a knowledge piece in the database.
void ForgetMemories(NPC *npc)
Forget privat memories.
void SetBuffer(const csString &bufferName, const csString &value)
Set/Update the value of a named buffer.
csList< Memory * >::Iterator GetMemoryIterator()
Definition: tribe.h:255
Asset * GetRandomAsset(Tribe::AssetType type, Tribe::AssetStatus status, csVector3 pos, iSector *sector, float range)
Get a random asset.
const char * GetNeededResourceNick()
Get the nick for the most needed resource for this tribe.
csVector3 pos
Definition: tribe.h:130
iSector * sector
The Sector.
Definition: tribe.h:107
bool LoadNPCMemoryBuffer(const char *name, csArray< NPC * > npcs)
Set npcs memory buffers.
Memory * FindMemory(csString name, const csVector3 &pos, iSector *sector, float radius)
Find a memory.
bool ShouldGrow() const
Check if the tribe should grow by checking number of members against max size.
void HandlePersistItem(gemNPCItem *item)
Handle persist items that should be assets.
int timeLeft
Number of ticks left before re-execution.
Definition: tribe.h:149
size_t GetMemberIDCount()
Definition: tribe.h:235
void TriggerEvent(Perception *pcpt, float maxRange=-1.0, csVector3 *basePos=NULL, iSector *baseSector=NULL)
Send a perception to all members of the tribe.
Class that represents the Recipe Manager of the game.
Definition: recipe.h:148
Tribe(EventManager *eventmngr, RecipeManager *rm)
Construct a new tribe object.
bool CanGrow() const
Check if the tribe can grow by checking the tribes wealth.
static const char * AssetStatusStr[]
Definition: tribe.h:87
int GetWealthResourceGrowthActiveLimit() const
Get wealth resource growth active limit.
bool LoadMemory(iResultRow &row)
Load all stored memories from db.
int GetID()
Definition: tribe.h:227
void ReplaceBuffers(csString &result)
Replace $TBUFFER[x] with values from the NPC buffer.
void AddAsset(Tribe::AssetType type, csString name, gemNPCItem *item, int quantity, int id=-1)
Add an item asset.
static const char * AssetTypeStr[]
Definition: tribe.h:78
AssetStatus status
Status of this asset. Used for buildings.
Definition: tribe.h:108
int AliveCount() const
Count number of alive members.
Asset * GetNearestAsset(Tribe::AssetType type, Tribe::AssetStatus status, csVector3 pos, iSector *sector, float range, float *locatedRange=NULL)
Get nearest asset.
float GetResourceRate()
Retrive resource rate average value from tribe.
Definition: tribe.h:502
csString tribeMemberType
Used to select needSet by index.
Definition: tribe.h:142
csVector3 pos
Position // Used only for reservations.
Definition: tribe.h:105
void AddRecipe(Recipe *recipe, Recipe *parentRecipe, bool reqType=false)
Add a recipe.
int GetReproductionCost() const
Return the reproduction cost for this tribe.
int GetMaxSize() const
Calculate the maximum number of members for the tribe.
uint32_t itemUID
The UID of the item, 0 if no item.
Definition: tribe.h:102
float GetWealthResourceGrowth() const
Get wealth resource growth rate.
Recipe * recipe
Link to recipe.
Definition: tribe.h:147
This object represents each NPC managed by this superclient.
Definition: npc.h:126
void DumpRecipesToConsole()
Dumps all information about recipes to console.
void LoadAsset(iResultRow &row)
Load an asset from an iResultRow.
bool CheckResource(csString resource, int number)
Check to see if enough resources are available.
const char * GetName()
Definition: tribe.h:231
void SaveAsset(Tribe::Asset *asset, bool deletion=false)
Save an asset to the db - responsable for deleting assets to.
const Resource & GetResource(size_t n)
Definition: tribe.h:251
csString name
Name. Especially used for buildings.
Definition: tribe.h:101
csString GetBuffer(const csString &bufferName)
Return a named buffer from the NPC.
void ProspectMine(NPC *npc, csString resource, csString nick)
Assigned a resource to a prospect Mine.
csString nick
Definition: tribe.h:93
int amount
Definition: tribe.h:94
float GetWealthResourceGrowthActive() const
Get wealth resource growth rate active.
void Unbuild(NPC *npc, gemNPCItem *building)
Tear down a building.
void UpdateRecipeData(int delta)
Updates recipe wait times.
float GetDeathRate()
Retrive death rate average value from tribe.
Definition: tribe.h:492
iSector * GetHomeSector()
void Build(NPC *npc, bool pickupable)
Build a building on the current NPC building spot.
int id
Definition: tribe.h:99
NPC * GetMember(size_t i)
Definition: tribe.h:243
void RemoveAsset(Tribe::Asset *asset)
Remove an asset.
Keep track of remote debugging.
Definition: remotedebug.h:58
void GetHome(csVector3 &pos, float &radius, iSector *&sector)
Get home position for the tribe.
csString name
Definition: tribe.h:92
Memory * FindNearestMemory(const char *name, const csVector3 &pos, const iSector *sector, float range=-1.0, float *foundRange=NULL)
Find nearest memory to a position.
void ShareMemories(NPC *npc)
Share privat memories with the other npcs.
const char * GetNeededResource()
Get the most needed resource for this tribe.
bool HandleDeath(NPC *npc)
Remove members that die.
csVector3 GetHomePosition()
Definition: tribe.h:264
virtual void ScopedTimerCallback(const ScopedTimer *timer)
Callback for debug of long time used in scopes.
int id
Database id.
Definition: tribe.h:91
int id
Database id.
Definition: tribe.h:128
size_t GetResourceCount()
Definition: tribe.h:247
NPC * npc
Privat memory if NPC is set.
Definition: tribe.h:134
void AddCyclicRecipe(Recipe *recipe, int time)
Add a cyclic recipe.
Asset * GetAsset(Tribe::AssetType type, csString name)
Get asset.
This class represents a leaf in a recipe tree.
This object represents recipes for the tribe AI.
Definition: recipe.h:55
csString name
Definition: tribe.h:129
This class handles all queueing and invoking of timed events, such as combat, spells, NPC dialog responses, range weapons, or NPC respawning.
Definition: eventmanager.h:40
void Advance(csTicks when, EventManager *eventmgr)
Advance the tribe.
void AddMemory(csString name, const csVector3 &pos, iSector *sector, float radius, NPC *npc)
Add a new memory to the tribe.
AssetStatus
Definition: tribe.h:80
Recipe * GetTribalRecipe()
Get the main recipe.
Memory * FindRandomMemory(const char *name, const csVector3 &pos, const iSector *sector, float range=-1.0, float *foundRange=NULL)
Find a random memory within range to a position.
void SendPerception(const char *pcpt, csArray< NPC * > npcs)
Sends the given perception to the given list of npcs.
size_t GetMemberCount()
Definition: tribe.h:239
void DeleteAsset(csString name, int quantity)
Delete item assets.
bool LoadResource(iResultRow &row)
Load and add a new resource to the tribe.
void Memorize(NPC *npc, Perception *perception)
Memorize a perception.
Holds list of tribe members, position of tribe home Keeps resources and handles tribe perceptions...
Definition: tribe.h:67