Planeshift
npc.h
Go to the documentation of this file.
1 /*
2 * npc.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 
20 /* This file holds definitions for ALL global variables in the planeshift
21 * server, normally you should move global variables into the psServer class
22 */
23 #ifndef __NPC_H__
24 #define __NPC_H__
25 //=============================================================================
26 // Crystal Space Includes
27 //=============================================================================
28 #include <csutil/hash.h>
29 
30 struct iMovable;
31 
32 #include "util/psutil.h"
33 #include "util/gameevent.h"
34 #include "util/remotedebug.h"
35 
36 //=============================================================================
37 // Local Includes
38 //=============================================================================
39 #include "tribe.h"
40 #include "gem.h"
41 #include "stat.h"
42 
43 class Behavior;
44 class EventManager;
45 class HateList;
46 class LocationType;
47 class NPCType;
48 class NetworkManager;
49 class Tribe;
50 class Waypoint;
51 class gemNPCActor;
52 class gemNPCObject;
53 class iResultRow;
54 class psLinearMovement;
55 class psNPCClient;
56 class psNPCTick;
57 struct iCollideSystem;
58 struct HateListEntry;
59 
64 #define NPC_BRAIN_TICK 200
65 
66 // Define a variadic macro for debugging prints for NPCs.
67 // In this way only the check IsDebugging is executed for each NPC unless debugging
68 // is turned on. Using the Printf functions will cause all args to be
69 // resolved and that will result in a lot off spilled CPU.
70 #define NPCDebug(npc,debug,...) \
71  { if (npc->IsDebugging()) { npc->Printf(debug, __VA_ARGS__); }}
72 
77 class HateList
78 {
79 protected:
80  csHash<HateListEntry*, EID> hatelist;
81 
82 public:
83  HateList(psNPCClient* npcclient, iEngine* engine, psWorld* world)
84  {
85  this->npcclient = npcclient;
86  this->engine = engine;
87  this->world = world;
88  }
89  ~HateList();
90 
91  void AddHate(EID entity_id, float delta);
92 
109  gemNPCActor* GetMostHated(NPC* npc, csVector3 &pos, iSector* sector, float range, LocationType* region,
110  bool includeOutsideRegion, bool includeInvisible, bool includeInvincible, float* hate);
111  bool Remove(EID entity_id);
112  void DumpHateList(csString &output, const csVector3 &myPos, iSector* mySector);
113  void DumpHateList(NPC* npc, const csVector3 &myPos, iSector* mySector);
114  void Clear();
115  float GetHate(EID ent);
116 
117 private:
118  psNPCClient* npcclient;
119  iEngine* engine;
120  psWorld* world;
121 };
122 
126 class NPC : private ScopedTimerCB, public iScriptableVar, public RemoteDebug
127 {
128 public:
132  typedef struct
133  {
134  csVector3 pos;
135  iSector* sector;
136  float angle;
138  float radius;
139  csWeakRef<gemNPCObject> target;
140  } Locate;
141  typedef csHash<Locate*,csString> LocateHash;
142 
143  enum
144  {
145  LOCATION_NONE = 0,
146  LOCATION_POS = 1,
147  LOCATION_SECTOR = 2,
148  LOCATION_ANGLE = 4,
149  LOCATION_WP = 8,
150  LOCATION_RADIUS = 16,
151  LOCATION_TARGET = 32,
152  LOCATION_ALL = -1
153  };
154 
155 protected:
156 
157  typedef csHash<csString,csString> BufferHash;
158 
161  csString origtype;
162  csString type;
163  PID pid;
164  csString name;
165  csTicks last_update;
167  iMovable* movable;
168 
169  uint8_t DRcounter;
170  csVector3 lastDrPosition;
171  iSector* lastDrSector;
172  csTicks lastDrTime;
174  float lastDrYRot;
175  csVector3 lastDrVel;
177 
179  LocateHash storedLocates;
180 
181  float ang_vel,vel;
183  float runVelocity;
184  float scale;
185  csString region_name;
189  bool alive;
190  EID owner_id;
192 
193  csArray<csString> autoMemorizeTypes;
194 
196  csString tribeMemberType;
198 
199  csVector3 spawnPosition;
200  iSector* spawnSector;
201 
203 
204  // Stats
209 
210  csArray< csWeakRef<gemNPCActor> > controlledActors;
211 
212  // Initial position checks
213  csVector3 checkedPos;
214  iSector* checkedSector;
215  bool checked;
217  bool disabled;
218 
219  int fallCounter; // Incremented if the NPC fall off the map
220 
221  void Advance(csTicks when);
222 
226  void TickPostProcess(csTicks when);
227 
229 
230 public:
231 
232  NPC(psNPCClient* npcclient, NetworkManager* networkmanager, psWorld* world, iEngine* engine, iCollideSystem* cdsys);
233  virtual ~NPC();
234 
235 
236  void Tick();
237 
238  PID GetPID()
239  {
240  return pid;
241  }
245  EID GetEID();
246  iMovable* GetMovable()
247  {
248  return movable;
249  }
250  psLinearMovement* GetLinMove();
251 
252  uint8_t GetDRCounter(csTicks when, const csVector3 &pos, float yRot, iSector* sector,
253  const csVector3 &vel, float angVel)
254  {
255  lastDrTime = when;
256  lastDrPosition = pos;
257  lastDrSector = sector;
258  lastDrYRot = yRot;
259  lastDrVel = vel;
260  lastDrAngVel = angVel;
261  return ++DRcounter;
262  }
263  void SetDRCounter(uint8_t counter)
264  {
265  DRcounter = counter;
266  }
267 
273  bool Load(iResultRow &row,csHash<NPCType*, const char*> &npctypes, EventManager* eventmanager, PID usePID);
274 
278  void Load(const char* name, PID pid, NPCType* type, const char* region_name, int debugging, bool disabled, EventManager* eventmanager);
279 
282  bool InsertCopy(PID use_char_id, PID ownerPID);
283 
286  bool Delete();
287 
288  void SetActor(gemNPCActor* actor);
290  {
291  return npcActor;
292  }
293  const char* GetName()
294  {
295  return name.GetDataSafe();
296  }
297  void SetAlive(bool a);
298  bool IsAlive() const
299  {
300  return alive;
301  }
302  void Disable(bool disable = true);
303  bool IsDisabled()
304  {
305  return disabled;
306  }
307 
308  Behavior* GetCurrentBehavior();
309  NPCType* GetBrain();
310  const char* GetOrigBrainType()
311  {
312  return origtype;
313  }
314 
319  void SetBrain(NPCType* type);
320 
324  void ScopedTimerCallback(const ScopedTimer* timer);
325 
332  csString Info(const csString &infoRequestSubCmd);
333 
340  void Dump();
341 
345  void DumpState(csString &output);
346 
350  void DumpBehaviorList(csString &output);
351 
355  void DumpReactionList(csString &output);
356 
360  void DumpHateList(csString &output);
361 
365  void DumpHateList(NPC* npc);
366 
370  void DumpDebugLog(csString &output);
371 
375  void DumpMemory(csString &output);
376 
380  void DumpControlled(csString &output);
381 
387  void ClearState();
388 
404  void TriggerEvent(Perception* pcpt, float maxRange=-1.0,
405  csVector3* basePos=NULL, iSector* baseSector=NULL,
406  bool sameSector=false);
407 
415  void TriggerEvent(const char* pcpt);
416 
417  void SetLastPerception(Perception* pcpt);
419  {
420  return last_perception;
421  }
422 
435  gemNPCActor* GetMostHated(float range, bool includeOutsideRegion, bool includeInvisible,
436  bool includeInvincible, float* hate=NULL);
437 
453  gemNPCActor* GetMostHated(csVector3 &pos, iSector* sector, float range, LocationType* region, bool includeOutsideRegion,
454  bool includeInvisible, bool includeInvincible, float* hate);
455 
456 
463  float GetEntityHate(gemNPCActor* entity);
464 
471  void AddToHateList(gemNPCActor* attacker,float delta);
472 
476  void RemoveFromHateList(EID who);
477 
481  void SetLocate(const csString &destination, const NPC::Locate &locate);
482 
486  void GetActiveLocate(csVector3 &pos, iSector* &sector, float &rot);
487 
493  void GetActiveLocate(Waypoint* &wp);
494 
500  float GetActiveLocateRadius() const;
501 
507  bool CopyLocate(csString source, csString destination, unsigned int flags);
508 
514  void ReplaceLocations(csString &result);
515 
519  float GetAngularVelocity();
520 
524  float GetVelocity();
525 
529  float GetWalkVelocity();
530 
534  float GetRunVelocity();
535 
536  csString &GetRegionName()
537  {
538  return region_name;
539  }
540  LocationType* GetRegion();
541 
547  {
548  return insideRegion;
549  }
550 
556  void SetInsideRegion(bool inside)
557  {
558  insideRegion = inside;
559  }
560 
561 
571  gemNPCActor* GetNearestActor(float range, csVector3 &destPosition, iSector* &destSector, float &destRange);
572 
582  gemNPCActor* GetNearestNPC(float range, csVector3 &destPosition, iSector* &destSector, float &destRange);
583 
593  gemNPCActor* GetNearestPlayer(float range, csVector3 &destPosition, iSector* &destSector, float &destRange);
594 
595  gemNPCActor* GetNearestVisibleFriend(float range);
596 
597  gemNPCActor* GetNearestDeadActor(float range);
598 
602  void AddAutoMemorize(csString types);
603 
607  void RemoveAutoMemorize(csString types);
608 
612  bool HasAutoMemorizeTypes() const
613  {
614  return !autoMemorizeTypes.IsEmpty();
615  }
616 
620  bool ContainAutoMemorizeType(const csString &type);
621 
622 private:
626  virtual void LocalDebugReport(const csString &debugString);
627 
631  virtual void RemoteDebugReport(uint32_t clientNum, const csString &debugString);
632 
633 public:
634  gemNPCObject* GetTarget();
635  void SetTarget(gemNPCObject* t);
636 
637  gemNPCObject* GetOwner();
638  const char* GetOwnerName();
639 
648  void SetOwner(EID owner_EID);
649 
653  void SetTribe(Tribe* new_tribe);
654 
660  Tribe* GetTribe();
661 
665  void SetTribeMemberType(const char* tribeMemberType);
666 
669  const csString &GetTribeMemberType() const;
670 
676  {
677  return insideTribeHome;
678  }
679 
685  void SetInsideTribeHome(bool inside)
686  {
687  insideTribeHome = inside;
688  }
689 
694 
695 
699  float GetHP();
700 
704  float GetMaxHP() const;
705 
709  float GetHPRate() const;
710 
714  float GetMana();
715 
719  float GetMaxMana() const;
720 
724  float GetManaRate() const;
725 
729  float GetPysStamina();
730 
734  float GetMaxPysStamina() const;
735 
739  float GetPysStaminaRate() const;
740 
744  float GetMenStamina();
745 
749  float GetMaxMenStamina() const;
750 
754  float GetMenStaminaRate() const;
755 
759  void TakeControl(gemNPCActor* actor);
760 
764  void ReleaseControl(gemNPCActor* actor);
765 
769  void UpdateControlled();
770 
774  void CheckPosition();
775 
782  void StoreSpawnPosition();
783 
787  const csVector3 &GetSpawnPosition() const;
788 
792  iSector* GetSpawnSector() const;
793 
794 
801  {
802  ++fallCounter;
803  }
804 
811  {
812  return fallCounter;
813  }
814 
823  csString GetBuffer(const csString &bufferName);
824 
831  void SetBuffer(const csString &bufferName, const csString &value);
832 
838  void ReplaceBuffers(csString &result);
839 
844  {
845  return bufferMemory;
846  }
847 
853  void SetBufferMemory(Tribe::Memory* memory);
854 
860  void SetBuildingSpot(Tribe::Asset* buildingSpot);
861 
865  Tribe::Asset* GetBuildingSpot();
866 
867 private:
871  virtual double GetProperty(MathEnvironment* env, const char* ptr);
873  virtual double CalcFunction(MathEnvironment* env, const char* functionName, const double* params);
874  virtual const char* ToString();
876 
877 private:
878  psNPCTick* tick;
880  NetworkManager* networkmanager;
881  psWorld* world;
882  iCollideSystem* cdsys;
883 
884  BufferHash npcBuffer;
885  Tribe::Memory* bufferMemory;
886  Tribe::Asset* buildingSpot;
887 
888  friend class psNPCTick;
889 
890 };
891 
894 class psNPCTick : public psGameEvent
895 {
896 protected:
898 
899 public:
900  psNPCTick(int offsetticks, NPC* npc): psGameEvent(0,offsetticks,"psNPCTick"), npc(npc) {};
901 
902  virtual void Trigger()
903  {
904  if(npc)
905  {
906  npc->tick = NULL;
907  npc->Tick();
908  }
909  }
910 
911  void Remove()
912  {
913  npc = NULL;
914  }
915 
916 
917  virtual csString ToString() const
918  {
919  return "psNPCTick";
920  }
921 };
922 
924 {
926  float hate_amount;
927 };
928 
931 #endif
932 
float runVelocity
Definition: npc.h:183
psWorld is in charge of managing all regions (zone map files) and loading/unloading them as needed...
Definition: psworld.h:47
Stat hp
Definition: npc.h:205
virtual csString ToString() const
Return a string with information about the event.
Definition: npc.h:917
This embodies any perception an NPC might have, or any game event of interest.
Definition: perceptions.h:55
Check how long time it take to process a scope.
Definition: psutil.h:136
psNPCClient * npcclient
Global connection to the NPC Client.
iMovable * GetMovable()
Definition: npc.h:246
This object represents the entities which have attacked or hurt the NPC and prioritizes them...
Definition: npc.h:77
Callback function for ScopedTimers.
Definition: psutil.h:121
float GetHate(EID ent)
A specific MathEnvironment to be used in a MathScript.
Definition: mathscript.h:188
iSector * spawnSector
The stored sector that this NPC where spawned.
Definition: npc.h:200
csTicks last_update
Definition: npc.h:165
csHash< Locate *, csString > LocateHash
Definition: npc.h:141
float hate_amount
Definition: npc.h:926
HateList(psNPCClient *npcclient, iEngine *engine, psWorld *world)
Definition: npc.h:83
csString tribeMemberType
What type/class is this NPC in the tribe.
Definition: npc.h:196
iSector * lastDrSector
Definition: npc.h:171
bool IsInsideTribeHome()
Check the inside tribe home state of the npc.
Definition: npc.h:675
NPC * npc
Definition: npc.h:897
bool IsInsideRegion()
Check the inside region state of the npc.
Definition: npc.h:546
iSector * sector
The sector for the located object.
Definition: npc.h:135
bool insideTribeHome
State variable for inside outside tribe home checks.
Definition: npc.h:197
Structure to hold located positions.
Definition: npc.h:132
csArray< csString > autoMemorizeTypes
Used to store what types of perceptions to memorize without changing behaviors.
Definition: npc.h:193
Perception * last_perception
Definition: npc.h:188
Perception * GetLastPerception()
Definition: npc.h:418
NPCType * brain
Definition: npc.h:160
void SetInsideTribeHome(bool inside)
Set the inside tribe home state.
Definition: npc.h:685
A set of operations building a generic behavior for a NPC.
Definition: npcbehave.h:340
void SetInsideRegion(bool inside)
Set the inside region state.
Definition: npc.h:556
float lastDrYRot
Definition: npc.h:174
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
Stat mana
Definition: npc.h:206
Movement related class.
Definition: linmove.h:68
float vel
Definition: npc.h:181
PID pid
Definition: npc.h:163
Represents a stat for the NPC.
Definition: stat.h:29
Handle all network messages inn and out of the NPC Client.
Definition: networkmgr.h:56
EID entity_id
Definition: npc.h:925
const char * GetOrigBrainType()
Definition: npc.h:310
csVector3 lastDrVel
Definition: npc.h:175
void AddHate(EID entity_id, float delta)
This stores a vector of positions listing a set of points defining a common type of location...
Definition: location.h:275
Tribe * tribe
Definition: npc.h:195
EID owner_id
Definition: npc.h:190
int GetFallCounter()
Return the fall counter.
Definition: npc.h:810
LocateHash storedLocates
List of stored locate locations.
Definition: npc.h:179
virtual void Trigger()
Abstract event processing function.
Definition: npc.h:902
const char * GetName()
Definition: npc.h:293
csString origtype
Name of NPCType for &#39;reload&#39;.
Definition: npc.h:161
void Remove()
Definition: npc.h:911
The event that makes the NPC brain go TICK.
Definition: npc.h:894
void IncrementFallCounter()
Increment the fall counter.
Definition: npc.h:800
psNPCTick(int offsetticks, NPC *npc)
Definition: npc.h:900
This object represents each NPC managed by this superclient.
Definition: npc.h:126
float lastDrAngVel
Definition: npc.h:176
float radius
The radius of the located object.
Definition: npc.h:138
bool alive
Definition: npc.h:189
EID target_id
Definition: npc.h:191
void Tick()
void Clear()
bool IsAlive() const
Definition: npc.h:298
uint8_t DRcounter
Definition: npc.h:169
csHash< csString, csString > BufferHash
Definition: npc.h:157
float angle
The angle of the located object.
Definition: npc.h:136
iMovable * movable
Definition: npc.h:167
The main NPC Client class holding references to important superclient objects.
Definition: npcclient.h:81
gemNPCActor * npcActor
Definition: npc.h:166
NPCType * oldbrain
delete at checkpoint
Definition: npc.h:159
csTicks lastDrTime
Definition: npc.h:172
LocationType * region
Cached pointer to the region.
Definition: npc.h:186
Locate * activeLocate
The current "Active" locate.
Definition: npc.h:178
csVector3 lastDrPosition
Definition: npc.h:170
float scale
Model scale value.
Definition: npc.h:184
A collection of behaviors and reactions will represent a type of npc.
Definition: npcbehave.h:197
bool disabled
Definition: npc.h:217
iSector * checkedSector
Definition: npc.h:214
csVector3 pos
The position of the located object.
Definition: npc.h:134
bool IsDisabled()
Definition: npc.h:303
bool HasAutoMemorizeTypes() const
Check if the NPC has anything to automemorize.
Definition: npc.h:612
gemNPCActor * GetMostHated(NPC *npc, csVector3 &pos, iSector *sector, float range, LocationType *region, bool includeOutsideRegion, bool includeInvisible, bool includeInvincible, float *hate)
Find the most hated entity within range of given position.
Keep track of remote debugging.
Definition: remotedebug.h:58
csString type
Definition: npc.h:162
void SetDRCounter(uint8_t counter)
Definition: npc.h:263
int fallCounter
Definition: npc.h:219
HateList hatelist
Definition: npc.h:228
csArray< csWeakRef< gemNPCActor > > controlledActors
Actors that are dragged/pushed around by this NPC.
Definition: npc.h:210
csHash< HateListEntry *, EID > hatelist
Definition: npc.h:80
uint8_t GetDRCounter(csTicks when, const csVector3 &pos, float yRot, iSector *sector, const csVector3 &vel, float angVel)
Definition: npc.h:252
PID GetPID()
Definition: npc.h:238
csVector3 checkedPos
Definition: npc.h:213
Stat menStamina
Definition: npc.h:208
csWeakRef< gemNPCObject > target
The located target.
Definition: npc.h:139
Stat pysStamina
Definition: npc.h:207
float walkVelocity
Definition: npc.h:182
bool checked
Definition: npc.h:215
bool Remove(EID entity_id)
void DumpHateList(csString &output, const csVector3 &myPos, iSector *mySector)
psNPCRaceListMessage::NPCRaceInfo_t * raceInfo
Definition: npc.h:202
All scheduled events must inherit from this class.
Definition: gameevent.h:36
Waypoint * wp
The nearest waypoint to the located object.
Definition: npc.h:137
csString & GetRegionName()
Definition: npc.h:536
csVector3 spawnPosition
The stored position that this NPC where spawned.
Definition: npc.h:199
bool checkedResult
Definition: npc.h:216
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
gemNPCActor * GetActor()
Definition: npc.h:289
csString region_name
Region name as loaded from db.
Definition: npc.h:185
bool insideRegion
State variable for inside outside region checks.
Definition: npc.h:187
Tribe::Memory * GetBufferMemory()
Retrive the current buffer memory of the npc.
Definition: npc.h:843
bool lastDrMoving
Definition: npc.h:173
csString name
Definition: npc.h:164
Holds list of tribe members, position of tribe home Keeps resources and handles tribe perceptions...
Definition: tribe.h:67