forked from ChatScript/ChatScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopicSystem.h
More file actions
244 lines (210 loc) · 8.79 KB
/
topicSystem.h
File metadata and controls
244 lines (210 loc) · 8.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#ifndef _TOPICSYSTEMH
#define _TOPICSYSTEMH
#ifdef INFORMATION
Copyright (C)2011-2019 by Bruce Wilcox
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#endif
#define LAYER_KERNEL -1
#define LAYER_0 0
#define LAYER_1 1
#define LAYER_BOOT 2
#define LAYER_USER 3
#define MAX_LABEL_SIZE 100
#define NO_REJOINDER -1
#define BLOCKED_REJOINDER -2
// kinds of top level rules
#define QUESTION '?'
#define STATEMENT 's'
#define STATEMENT_QUESTION 'u'
#define RANDOM_GAMBIT 'r'
#define GAMBIT 't'
#define ENDUNIT '`' // marks end of a rule in compiled script
#define DUPLICATETOPICSEPARATOR '~'
#define USED_CODES 75 // Encode/Decode is base 75
#define ENDUNITTEXT "`000 " // script compile rule closing sequence
#define JUMP_OFFSET 4 // immediately after any ` are 3 characters of jump info, then either "space letter :" or "space null" or "space x" if coming from 000
#define MAX_JUMP_OFFSET (USED_CODES*USED_CODES*USED_CODES)
#define MAX_REFERENCE_TOPICS 1000 // how many topics a sentence can refer to at once
#define SAVEOLDCONTEXT() int oldRuleID = currentRuleID; int oldTopic = currentTopicID; char* oldRule = currentRule; int oldRuleTopic = currentRuleTopic;
#define RESTOREOLDCONTEXT() currentRuleID = oldRuleID; currentTopicID = oldTopic; currentRule = oldRule; currentRuleTopic = oldRuleTopic;
// decompose a currentRuleID into its pieces
#define TOPLEVELID(x) ((unsigned int) (x & 0x0000ffff))
#define REJOINDERID(x) ( ((unsigned int)x) >> 16)
#define MAKE_REJOINDERID(x) (x << 16)
// decompose RULEMAX info into gambits and top level rules
#define MAKE_GAMBIT_MAX(x) (x << 16)
#define GAMBIT_MAX(x) (x >> 16)
#define RULE_MAX(x) (x & 0x0000ffff)
// used by FindNextRule
#define NEXTRULE -1 // any responder,gambit,rejoinder
#define NEXTTOPLEVEL 2 // only responders and gambits
#define NOMORERULES 0x0fffffff // finished walking a rule index map
typedef void(*TOPIC_FUNCTION)(char* topicName, uint64 data);
#define MAX_TOPIC_STACK 50
extern int currentBeforeLayer;
extern bool stats;
extern unsigned int ruleCount;
extern char timeStamp[NUMBER_OF_LAYERS][20];
extern char compileVersion[NUMBER_OF_LAYERS][20];
extern char buildStamp[NUMBER_OF_LAYERS][150];
extern char* howTopic;
extern bool ruleErased;
extern bool hypotheticalMatch;
extern unsigned int duplicateCount;
extern unsigned int xrefCount;
extern bool norejoinder;
extern int currentTopicID;
extern char* currentRule;
extern int currentRuleID;
extern int currentReuseID;
extern int currentReuseTopic;
extern int currentRuleTopic;
extern bool shared;
extern bool loading;
extern int outputRejoinderRuleID;
extern int outputRejoinderTopic;
extern int inputRejoinderRuleID;
extern int inputRejoinderTopic;
extern int sampleTopic;
extern int sampleRule;
typedef struct topicBlock
{
char* topicName;
char* topicRestriction;
char* topicScript;
char* topicSourceFileName;
unsigned char* topicDebugRule;
unsigned char* topicTimingRule;
unsigned char* topicUsed;
unsigned int* ruleOffset;
unsigned int* gambitTag;
unsigned int* responderTag;
int topicFlags;
int topicChecksum;
int topicMaxRule;
int topicDebug;
int topicNoDebug;
int topicTiming;
int topicLastGambitted;
int topicLastRejoindered;
int topicLastRespondered;
unsigned short int topicBytesRules;
} topicBlock;
topicBlock* TI(int topicid);
#define MAX_RECENT 100
extern unsigned short topicContext[MAX_RECENT + 1];
extern char labelContext[100][MAX_RECENT+ 1];
extern int inputContext[MAX_RECENT+ 1];
extern unsigned int contextIndex;
extern int numberOfTopicsInLayer[NUMBER_OF_LAYERS+1];
extern topicBlock* topicBlockPtrs[NUMBER_OF_LAYERS+1];
extern int numberOfTopics;
extern int topicIndex,pendingTopicIndex,originalPendingTopicIndex;
extern int topicStack[MAX_TOPIC_STACK+1];
extern int pendingTopicList[MAX_TOPIC_STACK+1];
extern int originalPendingTopicList[MAX_TOPIC_STACK+1];
void SetSampleFile(int topic);
void ResetContext();
FunctionResult ProcessRuleOutput(char* rule, unsigned int id,char* buffer, bool refine = false);
FunctionResult TestRule(int responderID,char* ptr,char* buffer,bool refine=false);
FunctionResult PerformTopic(int active,char* buffer,char* rule = NULL,unsigned int id = 0);
bool Repeatable(char* rule);
char* GetTopicLocals(int topic);
void CleanOutput(char* word);
bool DifferentTopicContext(int depthadjust, int topicid);
FunctionResult LoadLayer(int layer, const char* name,unsigned int build);
void ResetTopicReply();
void SetRejoinder(char* rule);
void SetErase(bool force = false);
void UndoErase(char* ptr,int topic,int id);
void AddKeep(char* ptr);
int TopicInUse(int topic);
int PushTopic(int topic);
void PopTopic();
void WalkTopics(char* function, char* buffer);
bool CheckTopicTrace();
bool CheckTopicTime();
FunctionResult DoOutput(char* buffer,char* rule, unsigned int id, bool refine = false);
unsigned int EstablishTopicTrace();
unsigned int EstablishTopicTiming();
char* GetRuleIDFromText(char* ptr, int & id);
char* GetVerify(char* tag,int & topicid, int &id);// ~topic.#.#=LABEL<~topic.#.# is a maximally complete why
void UnwindUserLayerProtect();
void InitKeywords(const char* name,const char* layer,unsigned int build,bool mark=false,bool concept=true);
bool AreDebugMarksSet();
bool AreTimingMarksSet();
// encoding
void DummyEncode(char* &data);
void Encode(unsigned int val,char* &ptr,int size = false);
unsigned int Decode(char* data,int single = 0);
char* FullEncode(uint64 val,char* ptr);
uint64 FullDecode(char* data);
char* WriteUserContext(char* ptr,bool sharefile);
bool ReadUserContext();
// data accessors
int TopicUsedCount(int topic);
void GetActiveTopicName(char* buffer);
unsigned int FindTopicIDByName(char* request,bool exact = false);
char* FindNextRule(signed char level, char* at,int& id);
int GetTopicFlags(int topic);
char* GetRuleTag(int& topic,int& id,char* tag);
char* GetRule(int topic, int id);
char* GetLabelledRule(int& topic, char* word,char* arg2,bool &fulllabel, bool& crosstopic,int& id, int baseTopic);
int HasGambits(int topic);
char* FindNextLabel(int topic,char* label, char* ptr,int &id,bool alwaysAllowed);
char* RuleBefore(int topic,char* rule);
char* GetTopicFile(int topic);
void CreateFakeTopics(char* data);
void ReleaseFakeTopics();
bool TopLevelQuestion(char* word);
bool TopLevelStatement(char* word);
bool TopLevelGambit(char* word);
bool Rejoinder(char* word);
char* GetLabel(char* rule,char* label);
char* GetPattern(char* rule,char* label,char* pattern,int limit = MAX_WORD_SIZE); // returns start of output and modified pattern
char* GetOutputCopy(char* ptr); // returns copy of output only
bool TopLevelRule(char* word);
char* GetTopicName(int topic,bool actual = true);
char* GetTopicData(int topic);
void SetTopicData(int topic,char* data);
bool BlockedBotAccess(int topic);
void TraceSample(int topic, int ruleID, unsigned int how = STDTRACELOG);
bool SetRuleDisableMark(int topic, int id);
void ClearRuleDisableMark(int topic,int id);
bool UsableRule(int topic,int n);
void AddRepeatable(char* ptr);
void AddTopicFlag(int topic, unsigned int flag);
void RemoveTopicFlag(int topic, unsigned int flag);
void SetTopicDebugMark(int topic,unsigned int val);
void SetTopicTimingMark(int topic, unsigned int val);
void SetDebugRuleMark(int topic,unsigned int id);
void SetTimingRuleMark(int topic, unsigned int id);
char* ShowRule(char* rule,bool concise = false);
char* DisplayTopicFlags(int topic);
void FlushDisabled();
void AddContext(int topic, char* label);
unsigned int InContext(int topic, char* label);
void SetContext(bool val);
// bulk topic I/O
char* WriteUserTopics(char* ptr,bool sharefile);
bool ReadUserTopics();
// general topic system control
void LoadTopicSystem();
void ResetTopicSystem(bool safe);
void ResetTopics();
void ResetTopic(int id);
// Interesting topics
unsigned int GetPendingTopicUnchanged();
void AddPendingTopic(int topic);
bool RemovePendingTopic(int topic);
void ClearPendingTopics();
void PendingTopics(int set);
char* ShowPendingTopics();
bool IsCurrentTopic(int topic);
#endif