Planeshift
npcclient.h
Go to the documentation of this file.
1 /*
2  * npcclient.h
3  *
4  * Copyright (C) 2003 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 __NPCCLIENT_H__
20 #define __NPCCLIENT_H__
21 //=============================================================================
22 // Crystal Space Includes
23 //=============================================================================
24 #include <csutil/csstring.h>
25 #include <csutil/hash.h>
26 #include <csutil/ref.h>
27 #include <csutil/list.h>
28 #include <iutil/vfs.h>
29 
30 struct iObjectRegistry;
31 struct iDocumentNode;
32 struct iConfigManager;
33 struct iCollideSystem;
34 
35 //=============================================================================
36 // Library Includes
37 //=============================================================================
38 #include "net/pstypes.h"
39 #include "net/npcmessages.h"
40 
41 #include "util/psconst.h"
42 #include "util/serverconsole.h"
43 #include "util/pspath.h"
44 #include "util/pspathnetwork.h"
45 #include "util/mathscript.h"
46 
47 #include "tools/celhpf.h"
48 
53 class psDatabase;
54 class MsgHandler;
55 class psNetConnection;
56 class EventManager;
57 class RecipeManager;
58 class NetworkManager;
59 class NPCType;
60 class NPC;
61 class gemNPCActor;
62 class gemNPCObject;
63 class gemNPCItem;
64 struct iVFS;
65 class Perception;
66 class Reaction;
67 class psWorld;
68 class Location;
69 class LocationType;
70 class LocationManager;
71 class Waypoint;
72 //class psPFMaps;
73 class Tribe;
74 class psPath;
75 class psPathNetwork;
76 struct iCelHNavStruct;
77 
82 {
83 public:
84 
87  struct DeferredNPC
88  {
89  PID id;
90  csString name;
91  };
92 
93 
99  psNPCClient();
100 
104  ~psNPCClient();
105 
118  bool Initialize(iObjectRegistry* object_reg, const char* host, const char* user, const char* pass, int port);
119 
123  iObjectRegistry* GetObjectReg()
124  {
125  return objreg;
126  }
127 
131  Tribe* GetTribe(int id);
132 
137  void MainLoop();
138 
144  virtual void CatchCommand(const char* cmd);
145 
149  void LoadLogSettings();
150 
154  void SaveLogSettings();
155 
159  void LoadCompleted();
160 
164  bool IsReady();
165 
169  void Tick();
170 
178  bool LoadMap(const char* mapfile);
179 
184  LocationType* FindRegion(const char* regname);
185 
190  LocationType* FindLocation(const char* locname);
191 
196  Location* FindLocation(const char* loctype, const char* name);
197 
202  Location* FindNearestLocation(const char* loctype, csVector3 &pos, iSector* sector, float range = -1, float* found_range = NULL);
203 
208  Location* FindRandomLocation(const char* loctype, csVector3 &pos, iSector* sector, float range = -1, float* found_range = NULL);
209 
214  Waypoint* FindNearestWaypoint(const csVector3 &v, iSector* sector, float range = -1, float* found_range = NULL);
215 
220  Waypoint* FindRandomWaypoint(const csVector3 &v, iSector* sector, float range = -1, float* found_range = NULL);
221 
226  Waypoint* FindNearestWaypoint(const char* group, csVector3 &v, iSector* sector, float range = -1, float* found_range = NULL);
227 
232  Waypoint* FindRandomWaypoint(const char* group, csVector3 &v, iSector* sector, float range = -1, float* found_range = NULL);
233 
239 
242  Waypoint* FindNearestWaypoint(gemNPCObject* entity, float range, float* found_range);
243 
244 
249  Waypoint* FindWaypoint(int id);
250 
255  Waypoint* FindWaypoint(const char* name, WaypointAlias** alias);
256 
260  csList<Waypoint*> FindWaypointRoute(Waypoint* start, Waypoint* end, const psPathNetwork::RouteFilter* filter);
261 
265  csList<Edge*> FindEdgeRoute(Waypoint* start, Waypoint* end, const psPathNetwork::RouteFilter* filter);
266 
271  NPCType* FindNPCType(const char* npctype_name);
272 
276  void AddRaceInfo(const psNPCRaceListMessage::NPCRaceInfo_t &raceInfo);
277 
282 
286  bool DumpRace(const char* pattern);
287 
291  float GetWalkVelocity(csString &race);
292 
296  float GetRunVelocity(csString &race);
297 
302  {
303  return network;
304  }
305 
310  {
311  return connection;
312  }
313 
317  void RegisterReaction(NPC* npc, Reaction* reaction);
318 
333  void TriggerEvent(Perception* pcpt, float maxRange=-1.0,
334  csVector3* basePos=NULL, iSector* baseSector=NULL,
335  bool sameSector=false);
336 
343  {
344  return eventmanager;
345  }
346 
353  {
354  return mathScriptEngine;
355  }
356 
360  void Disconnect();
361 
372  void SetEntityPos(EID id, csVector3 &pos, iSector* sector, InstanceID instance, bool force = false);
373 
380  NPC* FindNPCByPID(PID character_id);
381 
388  NPC* FindNPC(EID entid);
389 
396  psPath* FindPath(const char* name);
397 
406  void EnableDisableNPCs(const char* pattern, bool enable);
407 
411  void ListAllNPCs(const char* pattern);
412 
417  {
418  return npcs.GetSize();
419  }
420 
424  unsigned int GetTickCounter() const
425  {
426  return tick_counter;
427  }
428 
432  bool DumpNPC(const char* pattern);
433 
437  bool InfoNPC(const char* pattern);
438 
442  void ListAllEntities(const char* pattern, bool onlyCharacters = false);
443 
447  void ListTribes(const char* pattern);
448 
452  void ListTribeRecipes(const char* tribeID);
453 
457  void ListReactions(const char* pattern);
458 
462  void ListWaypoints(const char* pattern);
463 
467  void ListPaths(const char* pattern);
468 
472  void ListLocations(const char* pattern);
473 
479  void HandleDeath(NPC* who);
480 
481  void Add(gemNPCObject* object);
482  void Remove(gemNPCObject* object);
483  void RemoveAll();
484 
487  void Remove(NPC* npc);
488 
489  gemNPCObject* FindCharacterID(PID pid);
490  gemNPCObject* FindEntityID(EID eid);
491 
497  gemNPCObject* FindEntityByName(const char* name);
498 
504  bool CheckAttachTribes(NPC* npc);
505 
506  void AttachNPC(gemNPCActor* actor, uint8_t DRcounter, EID ownerEID, PID masterID);
507 
508  iCollideSystem* GetCollDetSys()
509  {
510  return cdsys;
511  }
512 
513  NPC* ReadSingleNPC(PID char_id, PID master_id = 0);
514 
531  NPC* ReadMasteredNPC(PID char_id, PID master_id);
532 
536  void UpdateTime(int minute, int hour, int day, int month, int year);
537 
538  iCelHNavStruct* GetNavStruct()
539  {
540  return navStruct;
541  }
542 
543  iCelHPath* ShortestPath(NPC* npc, const csVector3 &from, iSector* fromSector, const csVector3 &goal, iSector* goalSector);
544 
546  {
547  return world;
548  }
549 
550  iEngine* GetEngine()
551  {
552  return engine;
553  }
554 
555  iVFS* GetVFS()
556  {
557  return vfs;
558  }
559 
563 
569  {
570  return gameMinute;
571  }
572 
577  {
578  return gameHour;
579  }
580 
585  {
586  return gameDay;
587  }
588 
593  {
594  return gameMonth;
595  }
596 
601  {
602  return gameYear;
603  }
605 
614  void AttachObject(iObject* object, gemNPCObject* gobject);
615 
624  void UnattachObject(iObject* object, gemNPCObject* gobject);
625 
633  gemNPCObject* FindAttachedObject(iObject* object);
634 
635 
646  csArray<gemNPCObject*> FindNearbyEntities(iSector* sector, const csVector3 &pos, float radius, bool doInvisible = false);
647 
658  csArray<gemNPCActor*> FindNearbyActors(iSector* sector, const csVector3 &pos, float radius, bool doInvisible = false);
659 
665  csPtr<iDocumentNode> GetRootNode(const char* xmlfile);
666 
676  bool AddNPCType(csString newType);
677 
683  bool LoadNPCTypes(iDocumentNode* root);
684 
688  bool LoadNPCTypes();
689 
700  bool FirePerception(int NPCId, const char* perception);
701 
705  bool LoadPathNetwork();
706 
711  {
712  return pathNetwork;
713  }
714 
719  {
720  return locationManager;
721  }
722 
726  virtual double GetProperty(MathEnvironment* env, const char* ptr);
728  virtual double CalcFunction(MathEnvironment* env, const char* functionName, const double* params);
729  virtual const char* ToString();
731 
732 protected:
736  bool ReadNPCsFromDatabase();
737 
741  bool LoadLocations();
742 
746  bool LoadTribes();
747 
752  void PerceptProximityItems();
753 
759 
764 
765 public:
767 protected:
769  iObjectRegistry* objreg;
770  csRef<iEngine> engine;
771  csRef<iConfigManager > configmanager;
772  csRef<MsgHandler> msghandler;
778  csRef<iVFS> vfs;
779 
780  csHash<NPCType*, const char*> npctypes;
784  csRef<iCelHNavStruct> navStruct;
785  csPDelArray<NPC> npcs;
786  csArray<DeferredNPC> npcsDeferred;
787  csPDelArray<Tribe> tribes;
788  csHash<gemNPCObject*, EID> all_gem_objects_by_eid;
789  csHash<gemNPCObject*, PID> all_gem_objects_by_pid;
790  csPDelArray<gemNPCObject> all_gem_objects;
791  csArray<gemNPCItem*> all_gem_items;
792  csArray<gemNPCActor*> all_gem_actors;
793 
794  csHash<psNPCRaceListMessage::NPCRaceInfo_t,csString> raceInfos;
795 
796  csHash<NPC*,csString> allReactions;
797  csArray<csString> notUsedReactions;
798 
799  csRef<iCollideSystem> cdsys;
800 
802 
803  // psPFMaps* PFMaps;
804 
806  unsigned int tick_counter;
807 
812 
813  // Game Time
815  int gameHour;
816  int gameDay;
818  int gameYear;
820 
821 };
822 
825 #endif
826 
psWorld is in charge of managing all regions (zone map files) and loading/unloading them as needed...
Definition: psworld.h:47
csRef< MsgHandler > msghandler
Definition: npcclient.h:772
uint32 InstanceID
Definition: psconst.h:64
void Tick()
This does 1 AI calc per Tribe then returns.
void SaveLogSettings()
Used to save the log settings.
This embodies any perception an NPC might have, or any game event of interest.
Definition: perceptions.h:55
bool InfoNPC(const char *pattern)
Find one npc and print its current info.
csArray< gemNPCActor * > FindNearbyActors(iSector *sector, const csVector3 &pos, float radius, bool doInvisible=false)
Create a list of all nearby gem actors.
void AddRaceInfo(const psNPCRaceListMessage::NPCRaceInfo_t &raceInfo)
Add a race info to the race info list.
csHash< NPC *, csString > allReactions
Hash of all registered reactions.
Definition: npcclient.h:796
Class to hold information regarding aliases for waypoints.
Definition: waypoint.h:38
iCelHNavStruct * GetNavStruct()
Definition: npcclient.h:538
~psNPCClient()
Cleans up all allocated memory and removes all the players from the world.
bool IsReady()
If a world is ready and loaded this returns true.
static psNPCClient * npcclient
Definition: npcclient.h:766
void RegisterReaction(NPC *npc, Reaction *reaction)
Register a reaction for an NPC with the client.
NPC * ReadMasteredNPC(PID char_id, PID master_id)
Clones a master npc to a new npc whith the passed PID.
bool Initialize(iObjectRegistry *object_reg, const char *host, const char *user, const char *pass, int port)
Initialize the superclient.
int GetGameTODYear()
Get the year of the game time of day.
Definition: npcclient.h:600
iCollideSystem * GetCollDetSys()
Definition: npcclient.h:508
csHash< psNPCRaceListMessage::NPCRaceInfo_t, csString > raceInfos
Information about all the races.
Definition: npcclient.h:794
void AttachNPC(gemNPCActor *actor, uint8_t DRcounter, EID ownerEID, PID masterID)
A specific MathEnvironment to be used in a MathScript.
Definition: mathscript.h:188
bool LoadPathNetwork()
made public for the networkmanager to call it
void HandleDeath(NPC *who)
Special handling for death notifications from the server is required, to stop behavior scripting...
Keep information about deferred NPCs during loading.
Definition: npcclient.h:87
Definition: pspath.h:152
psPathNetwork * pathNetwork
Definition: npcclient.h:783
void ListTribes(const char *pattern)
List all tribes matching pattern to console.
void LoadCompleted()
To be called when the the load process is completed.
void SetEntityPos(EID id, csVector3 &pos, iSector *sector, InstanceID instance, bool force=false)
SetEntityPos finds the given ID entity, and updates its position in mesh and linmove.
void ListAllNPCs(const char *pattern)
List all NPCs matching pattern to console.
Location * FindNearestLocation(const char *loctype, csVector3 &pos, iSector *sector, float range=-1, float *found_range=NULL)
This function handles the searching for the specified object type and basically does the work for the...
csPDelArray< gemNPCObject > all_gem_objects
Definition: npcclient.h:790
void TriggerEvent(Perception *pcpt, float maxRange=-1.0, csVector3 *basePos=NULL, iSector *baseSector=NULL, bool sameSector=false)
Sends a perception to all npcs.
bool CheckAttachTribes(NPC *npc)
Loop through every tribe and check if this npc is a member.
csArray< DeferredNPC > npcsDeferred
Definition: npcclient.h:786
gemNPCObject * FindEntityID(EID eid)
int GetGameTODHour()
Get the hour of the game time of day.
Definition: npcclient.h:576
Client-side UDP handler.
Definition: connection.h:42
This defines an interface for intercepting commands instead of handling them locally in the server co...
Definition: serverconsole.h:57
int GetGameTODMinute()
Get the minute of the game time of day.
Definition: npcclient.h:568
A waypoint is a specified circle on the map with a name, location, and a list of waypoints it is conn...
Definition: waypoint.h:81
iObjectRegistry * GetObjectReg()
Just make iObjectRegistry available to other classes.
Definition: npcclient.h:123
void ListPaths(const char *pattern)
List all paths matching pattern to console.
int gameYear
Definition: npcclient.h:818
Handle all network messages inn and out of the NPC Client.
Definition: networkmgr.h:56
gemNPCObject * FindCharacterID(PID pid)
Waypoint * FindRandomWaypoint(const csVector3 &v, iSector *sector, float range=-1, float *found_range=NULL)
This iterates over all waypoints and finds the nearest one.
csPDelArray< Tribe > tribes
Definition: npcclient.h:787
gemNPCObject * FindEntityByName(const char *name)
Find named entity.
virtual double CalcFunction(MathEnvironment *env, const char *functionName, const double *params)
csRef< iCelHNavStruct > navStruct
Definition: npcclient.h:784
csList< Edge * > FindEdgeRoute(Waypoint *start, Waypoint *end, const psPathNetwork::RouteFilter *filter)
Find the shortest route between waypoint start and stop.
This stores a vector of positions listing a set of points defining a common type of location...
Definition: location.h:275
iObjectRegistry * objreg
Definition: npcclient.h:769
void ListWaypoints(const char *pattern)
List all waypoints matching pattern to console.
bool LoadMap(const char *mapfile)
Load a map into a region.
bool DumpNPC(const char *pattern)
Find one npc and print its current state info.
bool FirePerception(int NPCId, const char *perception)
Fire perception.
NPCType * FindNPCType(const char *npctype_name)
This function handles the searching for the specified NPCType name so that other NPCTypes can subclas...
unsigned int GetTickCounter() const
Retrive the current tick counter.
Definition: npcclient.h:424
Class that represents the Recipe Manager of the game.
Definition: recipe.h:148
void ListReactions(const char *pattern)
List all the registered reactions.
csArray< gemNPCObject * > FindNearbyEntities(iSector *sector, const csVector3 &pos, float radius, bool doInvisible=false)
Create a list of all nearby gem objects.
ServerConsole * serverconsole
Definition: npcclient.h:773
Manager that manage all locations and location types.
Definition: location.h:353
csHash< gemNPCObject *, PID > all_gem_objects_by_pid
Definition: npcclient.h:789
LocationManager * GetLocationManager()
Return the location manager of the npcclient.
Definition: npcclient.h:718
void UpdateTime(int minute, int hour, int day, int month, int year)
Update with time from server in order to start timed events.
void LoadLogSettings()
Used to load the log settings.
psWorld * world
Definition: npcclient.h:801
void PerceptProximityTribeHome()
Find all actors that are in a tribe home and percept tribe and actor if it is an NPC.
NPC * FindNPC(EID entid)
Find the NPC* attached to the entity with the specified EID.
void MainLoop()
Load and fork off a new thread for the Server Console.
MathScriptEngine * mathScriptEngine
Definition: npcclient.h:782
LocationType * FindRegion(const char *regname)
Find a region with a given name.
void PerceptProximityLocations()
Find all locations that are close to NPC&#39;s and percept the NPC.
float GetRunVelocity(csString &race)
Find the run velocity for a given race.
csArray< gemNPCItem * > all_gem_items
Definition: npcclient.h:791
psDatabase * database
Definition: npcclient.h:777
A reaction embodies the change in desire which occurs in an NPC when he perceives something...
Definition: reaction.h:51
virtual const char * ToString()
void EnableDisableNPCs(const char *pattern, bool enable)
Enable or disable NPCs.
This object represents each NPC managed by this superclient.
Definition: npc.h:126
int GetGameTODDay()
Get the day of the game time of day.
Definition: npcclient.h:584
csArray< csString > notUsedReactions
List of not matched reactions.
Definition: npcclient.h:797
void Remove(gemNPCObject *object)
psPath * FindPath(const char *name)
Find a given path.
bool LoadTribes()
Load Tribes from the DB.
int current_long_range_perception_index
Counters used to distribute load regarding perception generation.
Definition: npcclient.h:809
bool LoadLocations()
Load locations from the DB.
csRef< iConfigManager > configmanager
Definition: npcclient.h:771
int gameHour
Definition: npcclient.h:815
virtual double GetProperty(MathEnvironment *env, const char *ptr)
csTicks gameTimeUpdated
Definition: npcclient.h:819
psNPCRaceListMessage::NPCRaceInfo_t * GetRaceInfo(const char *name)
Get pointer to a race info to the given race name.
The main NPC Client class holding references to important superclient objects.
Definition: npcclient.h:81
csList< Waypoint * > FindWaypointRoute(Waypoint *start, Waypoint *end, const psPathNetwork::RouteFilter *filter)
Find the shortest route between waypoint start and stop.
This class is implements the user input and output console for the server.
Definition: serverconsole.h:67
Waypoint * FindWaypoint(gemNPCObject *entity)
This iterates over all waypoints and finds the one the entity is at.
bool AddNPCType(csString newType)
Add an NPCType.
int GetGameTODMonth()
Get the month of the game time of day.
Definition: npcclient.h:592
void ListAllEntities(const char *pattern, bool onlyCharacters=false)
List all known entities on superclient.
csPtr< iDocumentNode > GetRootNode(const char *xmlfile)
Load and return the root node of an xml file.
This class is the client&#39;s and server&#39;s main interface for either sending network messages out or get...
Definition: msghandler.h:106
psNetConnection * GetNetConnection()
Returns the network handler so script ops can invoke the network.
Definition: npcclient.h:309
csRef< iVFS > vfs
Definition: npcclient.h:778
bool ReadNPCsFromDatabase()
Read NPCs from the DB.
size_t GetNpcListAmount()
List the amount of npc currently loaded.
Definition: npcclient.h:416
iCelHPath * ShortestPath(NPC *npc, const csVector3 &from, iSector *fromSector, const csVector3 &goal, iSector *goalSector)
A collection of behaviors and reactions will represent a type of npc.
Definition: npcbehave.h:197
csArray< gemNPCActor * > all_gem_actors
Definition: npcclient.h:792
void RemoveAll()
void ListTribeRecipes(const char *tribeID)
List all the active recipes in a tribe.
csHash< gemNPCObject *, EID > all_gem_objects_by_eid
Definition: npcclient.h:788
int gameMonth
Definition: npcclient.h:817
void Add(gemNPCObject *object)
int current_long_range_perception_loc_index
Definition: npcclient.h:810
unsigned int tick_counter
Counter used to start events at every nth client tick.
Definition: npcclient.h:806
csRef< iCollideSystem > cdsys
Definition: npcclient.h:799
EventManager * eventmanager
Definition: npcclient.h:774
float GetWalkVelocity(csString &race)
Find the walk velocity for a given race.
bool DumpRace(const char *pattern)
Dump all race infos.
void Disconnect()
Disconnect from server nicely, before quitting.
EventManager * GetEventManager()
Returns the Event Manger.
Definition: npcclient.h:342
void ListLocations(const char *pattern)
List all locations matching pattern to console.
MathScriptEngine * GetMathScriptEngine()
Returns the Math Scripting Engine.
Definition: npcclient.h:352
int current_tribe_home_perception_index
Definition: npcclient.h:811
virtual void CatchCommand(const char *cmd)
Catch a command from the console.
psNetConnection * connection
Definition: npcclient.h:768
LocationType * FindLocation(const char *locname)
Find a location with a given name.
LocationManager * locationManager
The manager for location.
Definition: npcclient.h:781
bool LoadNPCTypes()
Load NPC Types from DB.
This holds all the formulas loaded from the MathScript table and provides a container for them...
Definition: mathscript.h:51
NPC * FindNPCByPID(PID character_id)
Find the NPC* attached to the entity with the specified character ID.
iVFS * GetVFS()
Definition: npcclient.h:555
csPDelArray< NPC > npcs
Definition: npcclient.h:785
Template class for implementing waypoint filtering when calculating routes.
Definition: pspathnetwork.h:49
A Location is a named place on the map, located dynamically by NPCs as scripted.
Definition: location.h:48
csRef< iEngine > engine
Definition: npcclient.h:770
A network of Waypoint and psPath objects.
Definition: pspathnetwork.h:42
csHash< NPCType *, const char * > npctypes
Definition: npcclient.h:780
int gameMinute
Definition: npcclient.h:814
NetworkManager * GetNetworkMgr()
Returns the network handler so script ops can invoke the network.
Definition: npcclient.h:301
Waypoint * FindNearestWaypoint(const csVector3 &v, iSector *sector, float range=-1, float *found_range=NULL)
This iterates over all waypoints and finds the nearest one.
void AttachObject(iObject *object, gemNPCObject *gobject)
Attach a server gemObject to a Crystal Space object.
Location * FindRandomLocation(const char *loctype, csVector3 &pos, iSector *sector, float range=-1, float *found_range=NULL)
This function handles the searching for the specified object type and basically does the work for the...
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
NetworkManager * network
Definition: npcclient.h:776
gemNPCObject * FindAttachedObject(iObject *object)
See if there is a gemObject attached to a given Crystal Space object.
psWorld * GetWorld()
Definition: npcclient.h:545
iEngine * GetEngine()
Definition: npcclient.h:550
Tribe * GetTribe(int id)
Make a tribe pointer available - for the network manager.
NPC * ReadSingleNPC(PID char_id, PID master_id=0)
void UnattachObject(iObject *object, gemNPCObject *gobject)
Unattach a gemObject from a Crystal Space object.
psPathNetwork * GetPathNetwork()
Return the path network of the npcclient.
Definition: npcclient.h:710
Holds list of tribe members, position of tribe home Keeps resources and handles tribe perceptions...
Definition: tribe.h:67
psNPCClient()
Constructor.
RecipeManager * recipemanager
Definition: npcclient.h:775
void PerceptProximityItems()
Find all items that are close to NPC&#39;s and percept the NPC.