Planeshift
npcbehave.h
Go to the documentation of this file.
1 /*
2 * npcbehave.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 __NPCBEHAVE_H__
24 #define __NPCBEHAVE_H__
25 //=============================================================================
26 // Crystal Space Includes
27 //=============================================================================
28 #include <csgeom/matrix3.h>
29 #include <csutil/array.h>
30 #include <iutil/document.h>
31 
32 struct iSector;
33 
34 //=============================================================================
35 // Library Includes
36 //=============================================================================
37 #include "util/eventmanager.h"
38 #include "util/gameevent.h"
39 #include "util/consoleout.h"
40 #include "util/pspath.h"
41 
42 //=============================================================================
43 // Local Includes
44 //=============================================================================
45 #include "perceptions.h"
46 #include "walkpoly.h"
47 #include "npcoperations.h"
48 
53 class ScriptOperation;
54 class Perception;
55 class Reaction;
56 class Behavior;
57 class NPC;
58 class EventManager;
59 class BehaviorSet;
60 class Waypoint;
61 
70 {
71 protected:
72  csPDelArray<Behavior> behaviors;
74 
75 public:
78  BehaviorSet();
79 
90  bool Add(Behavior* behavior);
91 
99  Behavior* Find(const char* name);
100 
109  void DeepCopy(BehaviorSet &other);
110 
119  void ClearState(NPC* npc);
120 
129  void UpdateNeeds(float delta, NPC* npc);
130 
135  Behavior* Schedule(NPC* npc);
136 
145  void Advance(csTicks delta, NPC* npc);
146 
154  void Execute(NPC* npc, bool forceRunScript);
155 
163  void Interrupt(NPC* npc);
164 
168  {
169  return active;
170  }
171 
176  void DumpBehaviorList(csString &output, NPC* npc);
177 
182  csString InfoBehaviors(NPC* npc);
183 
186  float GetHighestNeed(NPC* npc);
187 
188 };
189 
190 
197 class NPCType
198 {
199 protected:
200  NPC* npc;
201  csString name;
202  csPDelArray<Reaction> reactions;
204  float ang_vel;
205  float vel;
208  ScriptOperation::VelSource velSource;
211 
215  csString fallingPerception;
216 
217 public:
218  NPCType();
219  NPCType(NPCType &other, NPC* npc);
220 
221  ~NPCType();
222  void DeepCopy(NPCType &other);
223  void ClearState(NPC* npc);
224 
225  bool Load(iDocumentNode* node);
226  bool Load(iResultRow &node);
227  const char* GetName()
228  {
229  return name.GetDataSafe();
230  }
231 
239  Behavior* Find(const char* name)
240  {
241  return behaviors.Find(name);
242  }
243 
244  void Advance(csTicks delta, NPC* npc);
245  void Interrupt(NPC* npc);
246  void FirePerception(NPC* npc,Perception* pcpt);
247 
248  void DumpBehaviorList(csString &output, NPC* npc)
249  {
250  behaviors.DumpBehaviorList(output, npc);
251  }
252  csString InfoBehaviors(NPC* npc)
253  {
254  return behaviors.InfoBehaviors(npc);
255  }
256 
261  csString InfoReactions(NPC* npc);
262 
267  void DumpReactionList(csString &output, NPC* npc);
268 
270  {
271  return behaviors.GetCurrentBehavior();
272  }
273 
278  float GetHighestNeed(NPC* npc);
279 
284  float GetAngularVelocity(NPC* npc);
285 
290  float GetVelocity(NPC* npc);
291 
294  void SetVelSource(ScriptOperation::VelSource velSource, float vel);
295 
298  const csString &GetCollisionPerception() const;
299 
304  const csString &GetOutOfBoundsPerception() const;
305 
310  const csString &GetInBoundsPerception() const;
311 
314  const csString &GetFallingPerception() const;
315 
316 
317 private:
322  void AddReaction(Reaction* reaction);
323 
328  void InsertReaction(Reaction* reaction);
329 };
330 
331 
340 class Behavior
341 {
342 public:
343  // Used to indicate the result of the RunScript operation of a behavior.
345  {
349  BEHAVIOR_FAILED
350  };
351 protected:
352  csString name;
353 
354  csPDelArray<ScriptOperation> sequence;
355  size_t current_step;
356  size_t stepCount;
357  bool loop;
358  bool isActive;
363  float init_need;
366 
367  float current_need;
368  float new_need;
369 
370  bool interrupted;
371 
373  float minLimit;
375  float maxLimit;
376  csString failurePerception;
377 
378 public:
379 
380  Behavior();
381  Behavior(const char* n);
382  Behavior(Behavior &other);
383 
384  virtual ~Behavior() { };
385 
386  const char* GetName()
387  {
388  return name;
389  }
390 
391  void DeepCopy(Behavior &other);
392  bool Load(iDocumentNode* node);
393 
394  bool LoadScript(iDocumentNode* node,bool top_level=true);
395 
396  void UpdateNeed(float delta, NPC* npc);
397  BehaviorResult Advance(float delta, NPC* npc);
398  float CurrentNeed()
399  {
400  return current_need;
401  }
402  float NewNeed()
403  {
404  return new_need;
405  }
406 
416  void CommitAdvance();
417 
428  void ApplyNeedDelta(NPC* npc, float deltaDesire);
429 
430 
442  void ApplyNeedAbsolute(NPC* npc, float absoluteDesire);
443 
444  void SetIsActive(bool flag)
445  {
446  isActive = flag;
447  }
448 
455  bool IsActive()
456  {
457  return isActive;
458  }
459 
460  void SetCurrentStep(int step);
461  size_t GetCurrentStep()
462  {
463  return current_step;
464  }
465  size_t GetLastStep()
466  {
467  return sequence.GetSize();
468  }
469  void ResetNeed()
470  {
471  current_need = new_need = init_need;
472  }
473 
474  bool ApplicableToNPCState(NPC* npc);
475  void DoCompletionDecay(NPC* npc);
478  void StartScript(NPC* npc);
481  void OperationCompleted(NPC* npc);
484  void OperationFailed(NPC* npc);
485  void SetStartStep();
486  void InterruptScript(NPC* npc);
488  {
489  return interrupted;
490  }
492  {
493  interrupted = false;
494  }
495  Behavior* SetCompletionDecay(float completion_decay)
496  {
497  this->completion_decay = completion_decay;
498  return this;
499  }
500 
501  inline bool operator==(const Behavior &other)
502  {
503  return (current_need == other.current_need &&
504  name == other.name);
505  }
506  bool operator<(Behavior &other)
507  {
508  if(current_need > other.current_need)
509  return true;
510  if(current_need < other.current_need)
511  return false;
512  if(strcmp(name,other.name)>0)
513  return true;
514  return false;
515  }
516 
517  // For testing purposes only.
518 
519  Behavior* SetDecay(float need_decay_rate)
520  {
521  this->need_decay_rate = need_decay_rate;
522  return this;
523  }
524  Behavior* SetGrowth(float need_growth_rate)
525  {
526  this->need_growth_rate = need_growth_rate;
527  return this;
528  }
529  Behavior* SetInitial(float init_need)
530  {
531  this->init_need = init_need;
532  return this;
533  }
534 
539  void Failure(NPC* npc, ScriptOperation* op);
540 };
541 
543 {
544 public:
552  static void GetPosition(gemNPCObject* object, csVector3 &pos, float &yrot, iSector* &sector);
553 
560  static void GetPosition(gemNPCObject* object, csVector3 &pos, iSector* &sector);
561 
562  static void SetPosition(gemNPCObject* objecty, const csVector3 &pos, iSector* = NULL);
563  static void SetRotationAngle(gemNPCObject* object, float angle);
564  static void GetRotationAngle(gemNPCObject* object, float &yrot)
565  {
566  csVector3 pos;
567  iSector* sector;
568  GetPosition(object,pos,yrot,sector);
569  }
573  static float CalculateIncidentAngle(const csVector3 &pos, const csVector3 &dest);
574 
575  // Clamp the angle within 0 to 2*PI
576  static void ClampRadians(float &target_angle);
577 
578  // Normalize angle within -PI to PI
579  static void NormalizeRadians(float &target_angle);
580 
581  static csVector3 DisplaceTargetPos(const iSector* mySector, const csVector3 &myPos,
582  const iSector* targetSector, const csVector3 &targetPos,
583  float offset);
584  static csVector3 DisplaceTargetPos(const iSector* mySector, const csVector3 &myPos,
585  const iSector* targetSector, const csVector3 &targetPos,
586  float offset, float angle);
587 
588  static float Calc2DDistance(const csVector3 &a, const csVector3 &b);
589 
598  static csString ReplaceNPCVariables(NPC* npc, const csString &object);
599 
608  static bool ReplaceNPCVariablesBool(NPC* npc, const csString &object);
609 
610 };
611 
614 #endif
615 
This is the base class for all operations in action scripts.
Definition: npcoperations.h:69
csString collisionPerception
Global perception value for falling.
Definition: npcbehave.h:212
This embodies any perception an NPC might have, or any game event of interest.
Definition: perceptions.h:55
csString failurePerception
Perception to fire if any operation fails without own failure perception.
Definition: npcbehave.h:376
void DeepCopy(BehaviorSet &other)
Do a deap copy.
float NewNeed()
Definition: npcbehave.h:402
csString fallingPerception
Global perception value for falling.
Definition: npcbehave.h:215
csPDelArray< Behavior > behaviors
The set of behavoirs for this NPCType.
Definition: npcbehave.h:72
Behavior * SetGrowth(float need_growth_rate)
Definition: npcbehave.h:524
float current_need
The current need of this behavior after last advance.
Definition: npcbehave.h:367
csString InfoBehaviors(NPC *npc)
Info about the behavior list for debug.
BehaviorResult
Definition: npcbehave.h:344
size_t GetLastStep()
Definition: npcbehave.h:465
size_t GetCurrentStep()
Definition: npcbehave.h:461
void DumpBehaviorList(csString &output, NPC *npc)
Definition: npcbehave.h:248
Behavior * GetCurrentBehavior()
Return current active behavior.
Definition: npcbehave.h:167
bool is_applicable_when_dead
Definition: npcbehave.h:359
csString interruptPerception
Perception to fire if interrupted.
Definition: npcbehave.h:365
float need_decay_rate
need lessens while performing behavior
Definition: npcbehave.h:360
float GetHighestNeed(NPC *npc)
Return hight current or new need.
A set of operations building a generic behavior for a NPC.
Definition: npcbehave.h:340
float ang_vel
Default ang_vel for this NPCType. < Will be used for all behaviors unless overriden < by each behavio...
Definition: npcbehave.h:204
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
bool resume_after_interrupt
Resume at active step after interrupt.
Definition: npcbehave.h:364
float completion_decay
need lessens AFTER behavior script is complete. Use -1 to remove all need
Definition: npcbehave.h:362
csString name
The name of this behavior.
Definition: npcbehave.h:352
csPDelArray< Reaction > reactions
The reactions available for this NPCType.
Definition: npcbehave.h:202
void Advance(csTicks delta, NPC *npc)
Advances the behaviors.
bool Add(Behavior *behavior)
Add a behavior to the brain.
Behavior * SetDecay(float need_decay_rate)
Definition: npcbehave.h:519
void DumpBehaviorList(csString &output, NPC *npc)
Dump the behavior list for debug.
csString InfoBehaviors(NPC *npc)
Definition: npcbehave.h:252
void ResetNeed()
Definition: npcbehave.h:469
Used to indicate that an behavior has more steps.
Definition: npcbehave.h:346
float need_growth_rate
need grows while not performing behavior
Definition: npcbehave.h:361
Behavior * Find(const char *name)
Find a behavior in the set.
bool interrupted
Set to true if this behavior is interruped by another in a BehaviorSet.
Definition: npcbehave.h:370
bool minLimitValid
True if a minimum limit for the need for this behavior has been set.
Definition: npcbehave.h:372
This behavior will complete at a later stage.
Definition: npcbehave.h:348
csString outOfBoundsPerception
Global perception value for falling.
Definition: npcbehave.h:213
void UpdateNeeds(float delta, NPC *npc)
Update needs for all behaviors.
const char * GetName()
Definition: npcbehave.h:386
bool maxLimitValid
True if a maximum limit for the need for this behavior has been set.
Definition: npcbehave.h:374
Behavior * GetCurrentBehavior()
Definition: npcbehave.h:269
virtual ~Behavior()
Definition: npcbehave.h:384
csPDelArray< ScriptOperation > sequence
Sequence of ScriptOperations.
Definition: npcbehave.h:354
void Interrupt(NPC *npc)
Interrupt the current active behavior.
A reaction embodies the change in desire which occurs in an NPC when he perceives something...
Definition: reaction.h:51
NPC * npc
Pointer to the NPC for this brain.
Definition: npcbehave.h:200
float new_need
The accumulated change to the need after last advance.
Definition: npcbehave.h:368
This object represents each NPC managed by this superclient.
Definition: npc.h:126
Behavior * Find(const char *name)
Find a behavior in the set.
Definition: npcbehave.h:239
float init_need
starting need, also used in ClearState resets
Definition: npcbehave.h:363
BehaviorSet behaviors
The set of behaviors available for this NPCType.
Definition: npcbehave.h:203
This behavior completed.
Definition: npcbehave.h:347
csString inBoundsPerception
Global perception value for falling.
Definition: npcbehave.h:214
const char * GetName()
Definition: npcbehave.h:227
bool IsInterrupted()
Definition: npcbehave.h:487
size_t current_step
The ScriptOperation in the sequence that is currently executed.
Definition: npcbehave.h:355
A collection of behaviors and reactions will represent a type of npc.
Definition: npcbehave.h:197
Behavior * Schedule(NPC *npc)
Rearrange the behavior set based on need.
void Execute(NPC *npc, bool forceRunScript)
Execute script operations.
Behavior * SetCompletionDecay(float completion_decay)
Definition: npcbehave.h:495
void SetIsActive(bool flag)
Definition: npcbehave.h:444
float minLimit
The minimum value to limit the need if minLimitValid has been set true.
Definition: npcbehave.h:373
size_t stepCount
The number of script operation done in this periode.
Definition: npcbehave.h:356
bool loop
True if this behavior should start over when completed all operations.
Definition: npcbehave.h:357
bool IsActive()
Used to check if the behavior is active.
Definition: npcbehave.h:455
void ClearState(NPC *npc)
Prepare the set after use.
bool operator<(Behavior &other)
Definition: npcbehave.h:506
Behavior * SetInitial(float init_need)
Definition: npcbehave.h:529
csString name
The name of this NPC type.
Definition: npcbehave.h:201
static void GetRotationAngle(gemNPCObject *object, float &yrot)
Definition: npcbehave.h:564
This is the set of Behaviors available for an NPC.
Definition: npcbehave.h:69
BehaviorSet()
Constructor.
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
bool operator==(const Behavior &other)
Definition: npcbehave.h:501
Behavior * active
Points to the current active behavior.
Definition: npcbehave.h:73
float maxLimit
The maximum value to limit the need if maxLimitValid has been set true.
Definition: npcbehave.h:375
void ClearInterrupted()
Definition: npcbehave.h:491
float CurrentNeed()
Definition: npcbehave.h:398
bool isActive
Set to true when this behavior is active.
Definition: npcbehave.h:358