… | |
… | |
3 | NPC_Dialogue |
3 | NPC_Dialogue |
4 | |
4 | |
5 | =head1 DESCRIPTION |
5 | =head1 DESCRIPTION |
6 | |
6 | |
7 | NPC dialogue support module. |
7 | NPC dialogue support module. |
|
|
8 | |
|
|
9 | =over 4 |
8 | |
10 | |
9 | =cut |
11 | =cut |
10 | |
12 | |
11 | package NPC_Dialogue; |
13 | package NPC_Dialogue; |
12 | |
14 | |
… | |
… | |
26 | } |
28 | } |
27 | |
29 | |
28 | sub new { |
30 | sub new { |
29 | my ($class, %arg) = @_; |
31 | my ($class, %arg) = @_; |
30 | |
32 | |
|
|
33 | $arg{ob} = $arg{pl}->ob; |
|
|
34 | |
31 | my $self = bless { |
35 | my $self = bless { |
32 | %arg, |
36 | %arg, |
33 | }, $class; |
37 | }, $class; |
34 | |
38 | |
35 | $self->{match} ||= [parse_message $self->{npc}->msg]; |
39 | $self->{match} ||= [parse_message $self->{npc}->msg]; |
… | |
… | |
102 | |
106 | |
103 | =back |
107 | =back |
104 | |
108 | |
105 | The environment is that standard "map scripting environment", which is |
109 | The environment is that standard "map scripting environment", which is |
106 | limited in the type of constructs allowed (no loops, for example). |
110 | limited in the type of constructs allowed (no loops, for example). |
|
|
111 | |
|
|
112 | Here is a example: |
|
|
113 | |
|
|
114 | =over 4 |
|
|
115 | |
|
|
116 | =item B<matching for an item name> |
|
|
117 | |
|
|
118 | @match hi |
|
|
119 | @cond grep $_->name =~ /royalty/, $who->inv |
|
|
120 | You got royalties there! Wanna have! |
|
|
121 | |
|
|
122 | You may want to change the C<name> method there to something like C<title>, |
|
|
123 | C<slaying> or any other method that is allowed to be called on a |
|
|
124 | C<cf::object> here. |
|
|
125 | |
|
|
126 | =item B<matching for an item name and removing the matched item> |
|
|
127 | |
|
|
128 | @match found earhorn |
|
|
129 | @cond grep $_->slaying =~ /Gramp's walking stick/, $who->inv |
|
|
130 | @eval my @g = grep { $_->slaying =~ /Gramp's walking stick/ } $who->inv; $g[0]->decrease_ob_nr (1); |
|
|
131 | Thanks for the earhorn! |
|
|
132 | |
|
|
133 | This example is a bit more complex. The C<@eval> statement will search |
|
|
134 | the players inventory for the same term as the C<@cond> and then |
|
|
135 | decreases the number of objects used there. |
|
|
136 | |
|
|
137 | (See also the map: C<scorn/houses/cornerbrook.map> for an example how this is |
|
|
138 | used in the real world :-) |
|
|
139 | |
|
|
140 | =back |
107 | |
141 | |
108 | =item @eval perl |
142 | =item @eval perl |
109 | |
143 | |
110 | Like C<@cond>, but proceed regardless of the outcome. |
144 | Like C<@cond>, but proceed regardless of the outcome. |
111 | |
145 | |
… | |
… | |
164 | |
198 | |
165 | @match hi |
199 | @match hi |
166 | @ifflag kings_quest |
200 | @ifflag kings_quest |
167 | Hi, I was told you want to do the kings quest? |
201 | Hi, I was told you want to do the kings quest? |
168 | |
202 | |
169 | =item @trigger connected-id |
203 | =item @trigger connected-id [state] |
170 | |
204 | |
171 | Trigger all objects with the given connected-id. The trigger is stateful |
205 | Trigger all objects with the given connected-id. |
172 | and retains state per connected-id. |
206 | |
|
|
207 | When the state argument is omitted the trigger is stateful and retains an |
|
|
208 | internal state per connected-id. There is a limitation to the use of this: The |
|
|
209 | state won't be changed when the connection is triggered by other triggers. So |
|
|
210 | be careful when triggering the connection from other objects. |
|
|
211 | |
|
|
212 | When a state argument is given it should be either 0 or 1. 1 will 'push' the connection |
|
|
213 | and 0 will 'release' the connection. This is useful for example when you want to |
|
|
214 | let a npc control a door. |
|
|
215 | |
|
|
216 | Trigger all objects with the given connected-id by 'releasing' the connection. |
173 | |
217 | |
174 | =item @addtopic topic |
218 | =item @addtopic topic |
175 | |
219 | |
176 | Adds the given topic names (separated by C<|>) to the list of topics |
220 | Adds the given topic names (separated by C<|>) to the list of topics |
177 | returned. |
221 | returned. |
… | |
… | |
186 | my $lcmsg = lc $msg; |
230 | my $lcmsg = lc $msg; |
187 | |
231 | |
188 | topic: |
232 | topic: |
189 | for my $match (@{ $self->{match} }) { |
233 | for my $match (@{ $self->{match} }) { |
190 | for (split /\|/, $match->[0]) { |
234 | for (split /\|/, $match->[0]) { |
191 | if ($_ eq "*" || $lcmsg eq lc) { |
235 | if ($_ eq "*" || $lcmsg =~ /\b\Q$_\E\b/i) { |
192 | my $reply = $match->[1]; |
236 | my $reply = $match->[1]; |
193 | my @kw; |
237 | my @kw; |
194 | |
238 | |
195 | my @replies; |
239 | my @replies; |
196 | my @match; # @match/@parse command results |
240 | my @match; # @match/@parse command results |
… | |
… | |
251 | my ($name, $value) = split /\s+/, $args, 2; |
295 | my ($name, $value) = split /\s+/, $args, 2; |
252 | $state->{$name} eq $value |
296 | $state->{$name} eq $value |
253 | or next topic; |
297 | or next topic; |
254 | |
298 | |
255 | } elsif ($cmd eq "trigger") { |
299 | } elsif ($cmd eq "trigger") { |
|
|
300 | my ($con, $state) = split /\s+/, $args, 2; |
|
|
301 | $con = $con * 1; |
|
|
302 | |
|
|
303 | if (defined $state) { |
|
|
304 | $self->{npc}->map->trigger ($args, $state); |
|
|
305 | } else { |
256 | my $rvalue = \$self->{npc}{dialog_trigger}{$args*1}; |
306 | my $rvalue = \$self->{npc}{dialog_trigger}{$con}; |
257 | $self->{npc}->map->trigger ($args, $$rvalue = !$$rvalue); |
307 | $self->{npc}->map->trigger ($con, $$rvalue = !$$rvalue); |
|
|
308 | } |
258 | |
309 | |
259 | } elsif ($cmd eq "addtopic") { |
310 | } elsif ($cmd eq "addtopic") { |
260 | push @kw, split /\|/, $args; |
311 | push @kw, split /\|/, $args; |
261 | $self->{add_topic}->(split /\s*\|\s*/, $args) if $self->{add_topic}; |
312 | $self->{add_topic}->(split /\s*\|\s*/, $args) if $self->{add_topic}; |
262 | |
313 | |