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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
|
// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
// For more information, see LICENCE in the main folder
#ifdef PCRE_SUPPORT
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include "../common/timer.h"
#include "../common/malloc.h"
#include "../common/version.h"
#include "../common/nullpo.h"
#include "../common/showmsg.h"
#include "map.h"
#include "status.h"
#include "npc.h"
#include "chat.h"
#include "script.h"
#include "battle.h"
#include "pcre.h"
/// Returns the stack_data at the target index
#define script_getdata(st,i) &((st)->stack->stack_data[(st)->start+(i)])
/**
* Written by MouseJstr in a vision... (2/21/2005)
*
* This allows you to make npc listen for spoken text (global
* messages) and pattern match against that spoken text using perl
* regular expressions.
*
* Please feel free to copy this code into your own personal ragnarok
* servers or distributions but please leave my name. Also, please
* wait until I've put it into the main eA branch which means I
* believe it is ready for distribution.
*
* So, how do people use this?
*
* The first and most important function is defpattern
*
* defpattern 1, "[^:]+: (.*) loves (.*)", "label";
*
* this defines a new pattern in set 1 using perl syntax
* (http://www.troubleshooters.com/codecorn/littperl/perlreg.htm)
* and tells it to jump to the supplied label when the pattern
* is matched.
*
* each of the matched Groups will result in a variable being
* set ($p1$ through $p9$ with $p0$ being the entire string)
* before the script gets executed.
*
* activatepset 1;
*
* This activates a set of patterns.. You can have many pattern
* sets defined and many active all at once. This feature allows
* you to set up "conversations" and ever changing expectations of
* the pattern matcher
*
* deactivatepset 1;
*
* turns off a pattern set;
*
* deactivatepset -1;
*
* turns off ALL pattern sets;
*
* deletepset 1;
*
* deletes a pset
*/
/* Structure containing all info associated with a single pattern
block */
struct pcrematch_entry {
struct pcrematch_entry *next_;
char *pattern_;
pcre *pcre_;
pcre_extra *pcre_extra_;
char *label_;
};
/* A set of patterns that can be activated and deactived with a single
command */
struct pcrematch_set {
struct pcrematch_set *next_, *prev_;
struct pcrematch_entry *head_;
int setid_;
};
/*
* Entire data structure hung off a NPC
*
* The reason I have done it this way (a void * in npc_data and then
* this) was to reduce the number of patches that needed to be applied
* to a ragnarok distribution to bring this code online. I
* also wanted people to be able to grab this one file to get updates
* without having to do a large number of changes.
*/
struct npc_parse {
struct pcrematch_set *active_;
struct pcrematch_set *inactive_;
};
/**
* delete everythign associated with a entry
*
* This does NOT do the list management
*/
void finalize_pcrematch_entry(struct pcrematch_entry *e) {
//TODO: For some odd reason this causes a already-free'd error under Windows, but not *nix! [Skotlex]
#ifndef _WIN32
if (e->pcre_) {
free(e->pcre_);
e->pcre_ = NULL;
}
#endif
if (e->pcre_extra_) {
free(e->pcre_extra_);
e->pcre_ = NULL;
}
aFree(e->pattern_);
aFree(e->label_);
}
/**
* Lookup (and possibly create) a new set of patterns by the set id
*/
static struct pcrematch_set * lookup_pcreset(struct npc_data *nd,int setid)
{
struct pcrematch_set *pcreset;
struct npc_parse *npcParse = (struct npc_parse *) nd->chatdb;
if (npcParse == NULL)
nd->chatdb = npcParse = (struct npc_parse *)
aCalloc(sizeof(struct npc_parse), 1);
pcreset = npcParse->active_;
while (pcreset != NULL) {
if (pcreset->setid_ == setid)
break;
pcreset = pcreset->next_;
}
if (pcreset == NULL)
pcreset = npcParse->inactive_;
while (pcreset != NULL) {
if (pcreset->setid_ == setid)
break;
pcreset = pcreset->next_;
}
if (pcreset == NULL) {
pcreset = (struct pcrematch_set *)
aCalloc(sizeof(struct pcrematch_set), 1);
pcreset->next_ = npcParse->inactive_;
if (pcreset->next_ != NULL)
pcreset->next_->prev_ = pcreset;
pcreset->prev_ = 0;
npcParse->inactive_ = pcreset;
pcreset->setid_ = setid;
}
return pcreset;
}
/**
* activate a set of patterns.
*
* if the setid does not exist, this will silently return
*/
static void activate_pcreset(struct npc_data *nd,int setid) {
struct pcrematch_set *pcreset;
struct npc_parse *npcParse = (struct npc_parse *) nd->chatdb;
if (npcParse == NULL)
return; // Nothing to activate...
pcreset = npcParse->inactive_;
while (pcreset != NULL) {
if (pcreset->setid_ == setid)
break;
pcreset = pcreset->next_;
}
if (pcreset == NULL)
return; // not in inactive list
if (pcreset->next_ != NULL)
pcreset->next_->prev_ = pcreset->prev_;
if (pcreset->prev_ != NULL)
pcreset->prev_->next_ = pcreset->next_;
else
npcParse->inactive_ = pcreset->next_;
pcreset->prev_ = NULL;
pcreset->next_ = npcParse->active_;
if (pcreset->next_ != NULL)
pcreset->next_->prev_ = pcreset;
npcParse->active_ = pcreset;
}
/**
* deactivate a set of patterns.
*
* if the setid does not exist, this will silently return
*/
static void deactivate_pcreset(struct npc_data *nd,int setid) {
struct pcrematch_set *pcreset;
struct npc_parse *npcParse = (struct npc_parse *) nd->chatdb;
if (npcParse == NULL)
return; // Nothing to deactivate...
if (setid == -1) {
while(npcParse->active_ != NULL)
deactivate_pcreset(nd, npcParse->active_->setid_);
return;
}
pcreset = npcParse->active_;
while (pcreset != NULL) {
if (pcreset->setid_ == setid)
break;
pcreset = pcreset->next_;
}
if (pcreset == NULL)
return; // not in active list
if (pcreset->next_ != NULL)
pcreset->next_->prev_ = pcreset->prev_;
if (pcreset->prev_ != NULL)
pcreset->prev_->next_ = pcreset->next_;
else
npcParse->active_ = pcreset->next_;
pcreset->prev_ = NULL;
pcreset->next_ = npcParse->inactive_;
if (pcreset->next_ != NULL)
pcreset->next_->prev_ = pcreset;
npcParse->inactive_ = pcreset;
}
/**
* delete a set of patterns.
*/
static void delete_pcreset(struct npc_data *nd,int setid) {
int active = 1;
struct pcrematch_set *pcreset;
struct npc_parse *npcParse = (struct npc_parse *) nd->chatdb;
if (npcParse == NULL)
return; // Nothing to deactivate...
pcreset = npcParse->active_;
while (pcreset != NULL) {
if (pcreset->setid_ == setid)
break;
pcreset = pcreset->next_;
}
if (pcreset == NULL) {
active = 0;
pcreset = npcParse->inactive_;
while (pcreset != NULL) {
if (pcreset->setid_ == setid)
break;
pcreset = pcreset->next_;
}
}
if (pcreset == NULL)
return;
if (pcreset->next_ != NULL)
pcreset->next_->prev_ = pcreset->prev_;
if (pcreset->prev_ != NULL)
pcreset->prev_->next_ = pcreset->next_;
if(active)
npcParse->active_ = pcreset->next_;
else
npcParse->inactive_ = pcreset->next_;
pcreset->prev_ = NULL;
pcreset->next_ = NULL;
while (pcreset->head_) {
struct pcrematch_entry *n = pcreset->head_->next_;
finalize_pcrematch_entry(pcreset->head_);
aFree(pcreset->head_); // Cleanin' the last ones.. [Lance]
pcreset->head_ = n;
}
aFree(pcreset);
}
/**
* create a new pattern entry
*/
static struct pcrematch_entry *create_pcrematch_entry(struct pcrematch_set * set) {
struct pcrematch_entry * e = (struct pcrematch_entry *)
aCalloc(sizeof(struct pcrematch_entry), 1);
struct pcrematch_entry * last = set->head_;
// Normally we would have just stuck it at the end of the list but
// this doesn't sink up with peoples usage pattern. They wanted
// the items defined first to have a higher priority then the
// items defined later.. as a result, we have to do some work up
// front..
/* if we are the first pattern, stick us at the end */
if (last == NULL) {
set->head_ = e;
return e;
}
/* Look for the last entry */
while (last->next_ != NULL)
last = last->next_;
last->next_ = e;
e->next_ = NULL;
return e;
}
/**
* define/compile a new pattern
*/
void npc_chat_def_pattern(struct npc_data *nd, int setid,
const char *pattern, const char *label)
{
const char *err;
int erroff;
struct pcrematch_set * s = lookup_pcreset(nd, setid);
struct pcrematch_entry *e = create_pcrematch_entry(s);
e->pattern_ = aStrdup(pattern);
e->label_ = aStrdup(label);
e->pcre_ = pcre_compile(pattern, PCRE_CASELESS, &err, &erroff, NULL);
e->pcre_extra_ = pcre_study(e->pcre_, 0, &err);
}
/**
* Delete everything associated with a NPC concerning the pattern
* matching code
*
* this could be more efficent but.. how often do you do this?
*/
void npc_chat_finalize(struct npc_data *nd)
{
struct npc_parse *npcParse = (struct npc_parse *) nd->chatdb;
if (npcParse == NULL)
return;
while(npcParse->active_)
delete_pcreset(nd, npcParse->active_->setid_);
while(npcParse->inactive_)
delete_pcreset(nd, npcParse->inactive_->setid_);
// Additional cleaning up [Lance]
aFree(npcParse);
}
/**
* Handler called whenever a global message is spoken in a NPC's area
*/
int npc_chat_sub(struct block_list *bl, va_list ap)
{
struct npc_data *nd = (struct npc_data *)bl;
struct npc_parse *npcParse = (struct npc_parse *) nd->chatdb;
char *msg;
int len, pos, i;
struct map_session_data *sd;
struct npc_label_list *lst;
struct pcrematch_set *pcreset;
// Not interested in anything you might have to say...
if (npcParse == NULL || npcParse->active_ == NULL)
return 0;
msg = va_arg(ap,char*);
len = va_arg(ap,int);
sd = va_arg(ap,struct map_session_data *);
// grab the active list
pcreset = npcParse->active_;
// interate across all active sets
while (pcreset != NULL) {
struct pcrematch_entry *e = pcreset->head_;
// interate across all patterns in that set
while (e != NULL) {
int offsets[20];
char buf[255];
// perform pattern match
int r = pcre_exec(e->pcre_, e->pcre_extra_, msg, len, 0,
0, offsets, sizeof(offsets) / sizeof(offsets[0]));
if (r >= 0) {
// save out the matched strings
switch (r) {
case 10:
memcpy(buf, &msg[offsets[18]], offsets[19]);
buf[offsets[19]] = '\0';
set_var(sd, "$p9$", buf);
case 9:
memcpy(buf, &msg[offsets[16]], offsets[17]);
buf[offsets[17]] = '\0';
set_var(sd, "$p8$", buf);
case 8:
memcpy(buf, &msg[offsets[14]], offsets[15]);
buf[offsets[15]] = '\0';
set_var(sd, "$p7$", buf);
case 7:
memcpy(buf, &msg[offsets[12]], offsets[13]);
buf[offsets[13]] = '\0';
set_var(sd, "$p6$", buf);
case 6:
memcpy(buf, &msg[offsets[10]], offsets[11]);
buf[offsets[11]] = '\0';
set_var(sd, "$p5$", buf);
case 5:
memcpy(buf, &msg[offsets[8]], offsets[9]);
buf[offsets[9]] = '\0';
set_var(sd, "$p4$", buf);
case 4:
memcpy(buf, &msg[offsets[6]], offsets[7]);
buf[offsets[7]] = '\0';
set_var(sd, "$p3$", buf);
case 3:
memcpy(buf, &msg[offsets[4]], offsets[5]);
buf[offsets[5]] = '\0';
set_var(sd, "$p2$", buf);
case 2:
memcpy(buf, &msg[offsets[2]], offsets[3]);
buf[offsets[3]] = '\0';
set_var(sd, "$p1$", buf);
case 1:
memcpy(buf, &msg[offsets[0]], offsets[1]);
buf[offsets[1]] = '\0';
set_var(sd, "$p0$", buf);
}
// find the target label.. this sucks..
lst=nd->u.scr.label_list;
pos = -1;
for (i = 0; i < nd->u.scr.label_list_num; i++) {
if (strncmp(lst[i].name, e->label_, sizeof(lst[i].name)) == 0) {
pos = lst[i].pos;
break;
}
}
if (pos == -1) {
ShowWarning("Unable to find label: %s", e->label_);
// unable to find label... do something..
return 0;
}
// run the npc script
run_script(nd->u.scr.script,pos,sd->bl.id,nd->bl.id);
return 0;
}
e = e->next_;
}
pcreset = pcreset->next_;
}
return 0;
}
int mob_chat_sub(struct block_list *bl, va_list ap){
struct mob_data *md = (struct mob_data *)bl;
if(md->nd){
npc_chat_sub(&md->nd->bl, ap);
}
return 0;
}
// Various script builtins used to support these functions
int buildin_defpattern(struct script_state *st) {
int setid=conv_num(st, script_getdata(st,2));
const char *pattern=conv_str(st, script_getdata(st,3));
const char *label=conv_str(st, script_getdata(st,4));
struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
npc_chat_def_pattern(nd, setid, pattern, label);
return 0;
}
int buildin_activatepset(struct script_state *st) {
int setid=conv_num(st, script_getdata(st,2));
struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
activate_pcreset(nd, setid);
return 0;
}
int buildin_deactivatepset(struct script_state *st) {
int setid=conv_num(st, script_getdata(st,2));
struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
deactivate_pcreset(nd, setid);
return 0;
}
int buildin_deletepset(struct script_state *st) {
int setid=conv_num(st, script_getdata(st,2));
struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
delete_pcreset(nd, setid);
return 0;
}
#endif
|