Planeshift
psnpcdialog.h
Go to the documentation of this file.
1 /*
2  * psnpcdialog.h
3  *
4  * Copyright (C) 2001 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 #ifndef __PS_IMP_NPC_DIALOG__
21 #define __PS_IMP_NPC_DIALOG__
22 //=============================================================================
23 // Crystal Space Includes
24 //=============================================================================
25 #include <cstypes.h>
26 #include <iutil/comp.h>
27 #include <iutil/eventh.h>
28 #include <csutil/scf.h>
29 #include <cstool/collider.h>
30 #include <ivaria/collider.h>
31 
32 //=============================================================================
33 // Project Includes
34 //=============================================================================
35 #include "util/psconst.h"
36 #include "util/psstring.h"
37 
38 #include <idal.h>
39 
40 
41 //=============================================================================
42 // Local Includes
43 //=============================================================================
44 #include "dictionary.h"
45 
46 class gemNPC;
47 class NPCDialogDict;
48 class NpcResponse;
49 class GemActor;
50 class Client;
51 
52 struct iObjectRegistry;
53 
60 {
61  csString area;
62  int priority;
63 
65  {
66  return (priority==other.priority && area == other.area);
67  };
68  bool operator<(KnowledgeArea &other)
69  {
70  if(priority<other.priority)
71  {
72  return true;
73  }
74  else if(priority>other.priority)
75  {
76  return false;
77  }
78  // Priority is equal so check area.
79  if(strcmp(area,other.area)<0)
80  {
81  return true;
82  }
83  else
84  {
85  return false;
86  }
87  };
88 };
89 
94 {
95 protected:
96  csArray<NpcTerm*> terms;
97  csString str;
98 
99 public:
101  void AddToSentence(NpcTerm* next_word);
102  size_t TermLength()
103  {
104  return terms.GetSize();
105  }
106  const csString &GetString();
108  {
109  terms = other.terms;
110  str=other.str;
111  }
112  NpcTerm* &Term(size_t i)
113  {
114  return terms[i];
115  }
116 };
117 
118 
125 {
126 protected:
128  {
129  int playerID;
131  csTicks when;
132  };
133  csArray<DialogHistoryEntry> history;
134  int counter;
135 public:
137  {
138  counter=0;
139  }
140 
141  void AddToHistory(int playerID, int responseID, csTicks when);
142  bool EverSaid(int playerID, int responseID, csTicks &howLongAgo, int &times);
143 };
144 #define MAX_HISTORY_LEN 100
145 
152 {
153 protected:
154  gemNPC* self;
155  csArray<KnowledgeArea*> knowareas;
156 
157  psString antecedent_her,
158  antecedent_him,
159  antecedent_it,
161  iDataConnection* db;
162  csRandomGen* randomgen;
166 
167  NpcResponse* FindResponse(csString &trigger,const char* text, int questID = -1);
168  NpcResponse* FindResponseWithAllPrior(const char* area,const char* trigger, int questID = -1);
169  bool CheckPronouns(psString &text);
170  void UpdateAntecedents(NpcResponse* resp);
171  void AddBadText(const char* text,const char* trigger);
172 
173 protected:
174  void CleanPunctuation(psString &str, bool cleanQMark = true);
175 
180  void FilterKnownTerms(const psString &text, NpcTriggerSentence &trigger, Client* client);
181 
186  NpcResponse* FindOrGeneralizeTrigger(Client* client,NpcTriggerSentence &trigger,
187  csArray<int> &gen_terms, int word);
188 
189 
190  NpcResponse* ErrorResponse(const psString &text, const char* trigger);
191  NpcResponse* RepeatedResponse(const psString &text, NpcResponse* resp, csTicks when, int times);
192 
193 public:
194  psNPCDialog(gemNPC* npc);
195  virtual ~psNPCDialog();
196 
197  bool Initialize(iDataConnection* db, PID npcID);
198  bool Initialize(iDataConnection* db);
199  bool LoadKnowledgeAreas(PID npcID);
200 
201  NpcResponse* Respond(const char* text,Client* client);
202 
203  NpcResponse* FindXMLResponse(Client* client, csString trigger, int questID = -1);
204 
205  bool AddWord(const char* word);
206  bool AddSynonym(const char* word,const char* synonym);
207  bool AddKnowledgeArea(const char* ka_name);
208  bool AddResponse(const char* area,const char* words,const char* response,const char* minfaction);
209  bool AssignNPCArea(const char* npcname,const char* areaname);
210 
211  void SubstituteKeywords(Client* player, csString &response) const;
212 
213 
215  // void AddNewTrigger( int databaseID );
216 
218  // void AddNewResponse ( int databaseID );
219 
220  void DumpDialog();
221 };
222 
223 class psDialogManager;
224 
225 class psTriggerBlock;
226 class psAttitudeBlock;
227 class psSpecialResponse;
228 class psResponse;
229 
234 {
235 public:
239  void AddTrigger(psTriggerBlock* trigger);
240 
244  void AddSpecialResponse(psSpecialResponse* response);
245 
249  void PrintInfo();
250 
257  csArray<psTriggerBlock*> triggers;
258 
259 
266  csArray<psSpecialResponse*> special;
267 
276  int GetPriorID(int internalID);
277 
281  csArray<int> triggerIDs;
282 
286  csArray<int> responseIDs;
287 
288  csString area;
289 
290  bool WriteToDatabase();
291 
292  int InsertResponse(csString &response);
293  int InsertResponseSet(psResponse &response);
294  int InsertTrigger(const char* trigger, const char* area, int maxAttitude,
295  int minAttitude, int responseID, int priorID, int questID);
296 };
297 
298 
299 //--------------------------------------------------------------------------
300 
312 {
313 public:
317  psSpecialResponse(csRef<iDocumentNode> responseNode, int questID);
318 
319  csString type;
320 
321  csString response;
322 
323  int attMin;
324 
325  int attMax;
326 
327  int questID;
328 };
329 
330 
331 //--------------------------------------------------------------------------
332 
342 {
343 public:
345  {
346  manager = mgr;
347  priorResponseID = 0;
348  questID = -1;
349  }
350 
356  csStringArray phraseList;
357 
363  csArray<psAttitudeBlock*> attitudes;
364 
366  {
367  priorResponseID = 0;
368  }
369 
373  void Create(csRef<iDocumentNode> triggerNode, int questID);
374 
378  void SetPrior(int id)
379  {
380  priorResponseID = id;
381  }
382 
384 
386 
387  csString area;
388 
390 
391  int questID;
392 };
393 
394 
395 //--------------------------------------------------------------------------
396 
401 {
402 public:
409  csStringArray responses;
410 
411  csString script;
412  csString pronoun_him,
413  pronoun_her,
414  pronoun_it,
415  pronoun_them;
416 
417  int givenID;
418 
420 
421  int questID;
422 
423 };
424 
425 
426 //--------------------------------------------------------------------------
427 
435 {
436 public:
437  static int responseID;
438 
440  {
441  manager = mgr;
442  responseSet.givenID = ++responseID;
443  }
444 
446 
450  void Create(csRef<iDocumentNode> attitudeNode, int questID);
451 
453 
455 
457 };
458 
459 #endif
460 
461 
462 
iDataConnection * db
Definition: psnpcdialog.h:161
psAttitudeBlock(psDialogManager *mgr)
Definition: psnpcdialog.h:439
static int responseID
Used to generate the internal id&#39;s.
Definition: psnpcdialog.h:437
csArray< int > triggerIDs
Keep track of the database id&#39;s of the triggers.
Definition: psnpcdialog.h:281
A set of trigger data.
Definition: psnpcdialog.h:341
csString type
The trigger name.
Definition: psnpcdialog.h:319
This is an attitude class data that has the responses.
Definition: psnpcdialog.h:434
gemActor * currentplayer
Definition: psnpcdialog.h:163
psDialogManager * manager
Main dialog manager.
Definition: psnpcdialog.h:456
csArray< DialogHistoryEntry > history
Definition: psnpcdialog.h:133
csString area
The knowledge area of this trigger.
Definition: psnpcdialog.h:387
This class right now holds a simple circular MRU list of responses, so the npc can tell if he is gett...
Definition: psnpcdialog.h:124
csArray< int > responseIDs
Keep track of the database id&#39;s of responses.
Definition: psnpcdialog.h:286
psTriggerBlock(psDialogManager *mgr)
Definition: psnpcdialog.h:344
bool operator==(KnowledgeArea &other)
Definition: psnpcdialog.h:64
int attMax
The max attitude required for this response.
Definition: psnpcdialog.h:325
A phrase recognized by the dialog system.
Definition: dictionary.h:202
int givenID
The internal ID given by the dialog manager.
Definition: psnpcdialog.h:417
int questID
questID of trigger
Definition: psnpcdialog.h:391
Client * currentClient
Definition: psnpcdialog.h:164
A simple response.
Definition: psnpcdialog.h:400
int responseID
What response was said.
Definition: psnpcdialog.h:130
int databaseID
The actual databaseID.
Definition: psnpcdialog.h:419
psResponse responseSet
The response set for this attitude.
Definition: psnpcdialog.h:445
Any semi-autonomous object, either a player or an NPC.
Definition: gem.h:1170
psString antecedent_them
Definition: psnpcdialog.h:157
DialogHistory dialogHistory
Definition: psnpcdialog.h:165
csString response
The response given.
Definition: psnpcdialog.h:321
int databaseID
Holds the real databaseID of this trigger.
Definition: psnpcdialog.h:385
int counter
Current location in circular buffer.
Definition: psnpcdialog.h:134
csStringArray responses
The response lists.
Definition: psnpcdialog.h:409
void SetPrior(int id)
This sets the prior responseID for this trigger data.
Definition: psnpcdialog.h:378
int maxAttitude
The max attitude that these responses are for.
Definition: psnpcdialog.h:452
This is used to handle <specificknowledge> and <specialresponse> tags.
Definition: psnpcdialog.h:233
This class collects data of a netclient.
Definition: client.h:95
void void Initialize(iObjectRegistry *object_reg)
csTicks when
Timestamp of response.
Definition: psnpcdialog.h:131
csRandomGen * randomgen
Definition: psnpcdialog.h:162
Definition: gem.h:1910
csString pronoun_them
Definition: psnpcdialog.h:412
int questID
The questID of the response.
Definition: psnpcdialog.h:421
psDialogManager * manager
Main mangaer.
Definition: psnpcdialog.h:389
This class exists per NPC, and holds all dialog triggers, responses and scripts for this particular N...
Definition: psnpcdialog.h:151
This class holds several possible responses and an action script for the npc to run whenever an appro...
Definition: dictionary.h:394
csStringArray phraseList
A list of phrases for this trigger.
Definition: psnpcdialog.h:356
csArray< psTriggerBlock * > triggers
A list of all the triggers.
Definition: psnpcdialog.h:257
csString area
Definition: psnpcdialog.h:61
csString str
String version built from array.
Definition: psnpcdialog.h:97
csString script
Definition: psnpcdialog.h:411
A KnowledgeArea is a set of triggers and answers the NPC can provide when asked.
Definition: psnpcdialog.h:59
int attMin
The min attitude required for this response.
Definition: psnpcdialog.h:323
int minAttitude
The min NPC attitude that these responses are for.
Definition: psnpcdialog.h:454
Sentence written by the user represented as sequence of known terms.
Definition: psnpcdialog.h:93
int playerID
Who was the response said to.
Definition: psnpcdialog.h:129
void operator=(NpcTriggerSentence &other)
Definition: psnpcdialog.h:107
int priorResponseID
Holds the prior responseID.
Definition: psnpcdialog.h:383
csArray< psSpecialResponse * > special
A list of all the special responses.
Definition: psnpcdialog.h:266
NpcTerm *& Term(size_t i)
Definition: psnpcdialog.h:112
csArray< NpcTerm * > terms
Not PArray because these ptrs are shared.
Definition: psnpcdialog.h:96
A special NPC response.
Definition: psnpcdialog.h:311
bool operator<(KnowledgeArea &other)
Definition: psnpcdialog.h:68
csArray< KnowledgeArea * > knowareas
Definition: psnpcdialog.h:155
csArray< psAttitudeBlock * > attitudes
The list of attitudes.
Definition: psnpcdialog.h:363