ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/NPC_Dialogue.pm
Revision: 1.5
Committed: Wed Jul 19 22:20:07 2006 UTC (17 years, 10 months ago) by root
Branch: MAIN
Changes since 1.4: +29 -2 lines
Log Message:
make perl say the default say, convert bank script to new dialogue system

File Contents

# Content
1 =head1 NAME
2
3 NPC_Dialogue
4
5 =head1 DESCRIPTION
6
7 NPC dialogue support module.
8
9 =cut
10
11 package NPC_Dialogue;
12
13 use strict;
14
15 sub has_dialogue($) {
16 my ($ob) = @_;
17
18 $ob->get_message =~ /^\@match /;
19 }
20
21 sub parse_message($) {
22 map [split /\n/, $_, 2],
23 grep length,
24 split /^\@match /m,
25 $_[0]
26 }
27
28 sub new {
29 my ($class, %arg) = @_;
30
31 my $self = bless {
32 %arg,
33 }, $class;
34
35 $self->{match} ||= [parse_message $self->{npc}->get_message];
36
37 $self;
38 }
39
40 sub greet {
41 my ($self) = @_;
42
43 $self->tell ("hi")
44 }
45
46 sub tell {
47 my ($self, $msg) = @_;
48
49 my $lcmsg = lc $msg;
50
51 match:
52 for my $match (@{ $self->{match} }) {
53 for (split /\|/, $match->[0]) {
54 if ($_ eq "*" || $lcmsg eq lc) {
55 my $reply = $match->[1];
56
57 my @match; # @match/@parse command results
58
59 # now execute @-commands (which can result in a no-match)
60 while ($reply =~ s/^\@(\w+)\s*([^\n]*)\n?//) {
61 my ($cmd, $args) = ($1, $2);
62
63 if ($cmd eq "parse" || $cmd eq "match") { # match is future rename
64 no re 'eval'; # default, but make sure
65 @match = $msg =~ /$args/i
66 or next match;
67
68 } elsif ($cmd eq "cond") {
69 cf::safe_eval $args, who => $self->{ob}, npc => $self->{npc}, msg => $msg
70 or next match;
71
72 } elsif ($cmd eq "eval") {
73 cf::safe_eval $args, who => $self->{ob}, npc => $self->{npc}, msg => $msg;
74 warn "\@eval evaluation error: $@\n" if $@;
75
76 } else {
77 warn "unknown dialogue command <$cmd,$args> used (from " . $self->{npc}->get_message . ")";
78 }
79 }
80
81 # combine lines into paragraphs
82 $reply =~ s/(?<=\S)\n(?=\w)/ /g;
83 $reply =~ s/\n\n/\n/g;
84
85 my @kw;
86 # now mark up all matching keywords
87 for my $match (@{ $self->{match} }) {
88 for (sort { (length $b) <=> (length $a) } split /\|/, $match->[0]) {
89 if ($reply =~ /\b\Q$_\E\b/i) {
90 push @kw, $_;
91 last;
92 }
93 }
94 }
95
96 return wantarray ? ($reply, @kw) : $reply;
97 }
98 }
99 }
100
101 ()
102 }
103
104 1