ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/NPC_Dialogue.pm
(Generate patch)

Comparing deliantra/maps/perl/NPC_Dialogue.pm (file contents):
Revision 1.6 by root, Wed Jul 19 23:00:50 2006 UTC vs.
Revision 1.7 by root, Thu Jul 20 04:24:02 2006 UTC

41 my ($self) = @_; 41 my ($self) = @_;
42 42
43 $self->tell ("hi") 43 $self->tell ("hi")
44} 44}
45 45
46=item ($reply, @topics) = $dialog->tell ($msg)
47
48Tells the dialog object something and returns its response and optionally
49a number of topics that are refered to by this topic.
50
51It supports a number of command constructs. They have to follow the
52C<@match> directive, and there can be multiple commands that will be
53executed in order.
54
55=over 4
56
57=item @parse regex
58
59Parses the message using a perl regular expression (by default
60case-insensitive). Any matches will be available as C<< $match->[$index]
61>>.
62
63If the regular expression does not match, the topic is skipped.
64
65Example:
66
67 @match deposit
68 @parse deposit (\d+) (\S+)
69 @eval bank::deposit $match->[0], $match->[1]
70
71=item @cond perl
72
73Evaluates the given perl code. If it returns false (or causes an
74exception), the topic will be skipped, otherwise topic interpretation is
75resumed.
76
77The following local variables are defined within the expression:
78
79=over 4
80
81=item $who - The cf::object::player object that initiated the dialogue.
82
83=item $npc - The NPC (or magic_ear etc.) object that is being talked to.
84
85=item $msg - The actual message as passed to this method.
86
87=item $match - An arrayref with previous results from C<@parse>.
88
89=back
90
91The environment is that standard "map scripting environment", which is
92limited in the type of constructs allowed (no loops, for example).
93
94=item @eval perl
95
96Like C<@cond>, but proceed regardless of the outcome.
97
98=item @trigger connected-id
99
100Trigger all objects with the given connected-id. The trigger is stateful
101and retains state per connected-id.
102
103=back
104
105=cut
106
46sub tell { 107sub tell {
47 my ($self, $msg) = @_; 108 my ($self, $msg) = @_;
48 109
49 my $lcmsg = lc $msg; 110 my $lcmsg = lc $msg;
50 111
51 match: 112 match:
52 for my $match (@{ $self->{match} }) { 113 for my $match (@{ $self->{match} }) {
53 for (split /\|/, $match->[0]) { 114 for (split /\|/, $match->[0]) {
54 if ($_ eq "*" || $lcmsg eq lc) { 115 if ($_ eq "*" || $lcmsg eq lc) {
55 my $reply = $match->[1]; 116 my $reply = $match->[1];
56
57 # combine lines into paragraphs
58 $reply =~ s/(?<=\S)\n(?=\w)/ /g;
59 $reply =~ s/\n\n/\n/g;
60 117
61 my @replies; 118 my @replies;
62 my @match; # @match/@parse command results 119 my @match; # @match/@parse command results
63 local $self->{ob}{record_replies} = \@replies; 120 local $self->{ob}{record_replies} = \@replies;
64 121
70 no re 'eval'; # default, but make sure 127 no re 'eval'; # default, but make sure
71 @match = $msg =~ /$args/i 128 @match = $msg =~ /$args/i
72 or next match; 129 or next match;
73 130
74 } elsif ($cmd eq "cond") { 131 } elsif ($cmd eq "cond") {
75 cf::safe_eval $args, who => $self->{ob}, npc => $self->{npc}, msg => $msg 132 cf::safe_eval $args, who => $self->{ob}, npc => $self->{npc}, msg => $msg, match => \@match
76 or next match; 133 or next match;
77 134
78 } elsif ($cmd eq "eval") { 135 } elsif ($cmd eq "eval") {
79 cf::safe_eval $args, who => $self->{ob}, npc => $self->{npc}, msg => $msg; 136 cf::safe_eval $args, who => $self->{ob}, npc => $self->{npc}, msg => $msg, match => \@match;
80 warn "\@eval evaluation error: $@\n" if $@; 137 warn "\@eval evaluation error: $@\n" if $@;
138
139 } elsif ($cmd eq "trigger") {
140 my $rvalue = \$self->{npc}{dialog_trigger}{$args*1};
141
142 my $trigger = cf::object::new "magic_ear";
143 $trigger->set_value ($$rvalue);
144
145 # needs to be on the map for remove_button_link to work
146 # the same *should* be true for add_button_link....
147 $self->{npc}->map->insert_object ($trigger, 0, 0);
148
149 $trigger->add_button_link ($self->{npc}->map, $args);
150
151 $trigger->use_trigger;
152
153 $trigger->remove_button_link;
154 $trigger->remove;
155
156 $trigger->free;
157
158 $$rvalue = !$$rvalue;
81 159
82 } else { 160 } else {
83 warn "unknown dialogue command <$cmd,$args> used (from " . $self->{npc}->get_message . ")"; 161 warn "unknown dialogue command <$cmd,$args> used (from " . $self->{npc}->get_message . ")";
84 } 162 }
85 } 163 }
164
165 # combine lines into paragraphs
166 $reply =~ s/(?<=\S)\n(?=\w)/ /g;
167 $reply =~ s/\n\n/\n/g;
86 168
87 # ignores flags and npc from replies 169 # ignores flags and npc from replies
88 $reply = join "\n", (map $_->[1], @replies), $reply; 170 $reply = join "\n", (map $_->[1], @replies), $reply;
89 171
90 my @kw; 172 my @kw;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines