… | |
… | |
11 | =cut |
11 | =cut |
12 | |
12 | |
13 | package NPC_Dialogue; |
13 | package NPC_Dialogue; |
14 | |
14 | |
15 | use strict; |
15 | use strict; |
16 | |
|
|
17 | sub has_dialogue($) { |
|
|
18 | my ($ob) = @_; |
|
|
19 | |
|
|
20 | $ob->msg =~ /^\@match /; |
|
|
21 | } |
|
|
22 | |
16 | |
23 | sub parse_message($) { |
17 | sub parse_message($) { |
24 | map [split /\n/, $_, 2], |
18 | map [split /\n/, $_, 2], |
25 | grep length, |
19 | grep length, |
26 | split /^\@match /m, |
20 | split /^\@match /m, |
… | |
… | |
107 | =back |
101 | =back |
108 | |
102 | |
109 | The environment is that standard "map scripting environment", which is |
103 | The environment is that standard "map scripting environment", which is |
110 | limited in the type of constructs allowed (no loops, for example). |
104 | limited in the type of constructs allowed (no loops, for example). |
111 | |
105 | |
|
|
106 | Here is a example: |
|
|
107 | |
|
|
108 | =over 4 |
|
|
109 | |
|
|
110 | =item B<matching for an item name> |
|
|
111 | |
|
|
112 | @match hi |
|
|
113 | @cond grep $_->name =~ /royalty/, $who->inv |
|
|
114 | You got royalties there! Wanna have! |
|
|
115 | |
|
|
116 | You may want to change the C<name> method there to something like C<title>, |
|
|
117 | C<slaying> or any other method that is allowed to be called on a |
|
|
118 | C<cf::object> here. |
|
|
119 | |
|
|
120 | =item B<matching for an item name and removing the matched item> |
|
|
121 | |
|
|
122 | @match found earhorn |
|
|
123 | @cond grep $_->slaying =~ /Gramp's walking stick/, $who->inv |
|
|
124 | @eval my @g = grep { $_->slaying =~ /Gramp's walking stick/ } $who->inv; $g[0]->decrease; |
|
|
125 | Thanks for the earhorn! |
|
|
126 | |
|
|
127 | This example is a bit more complex. The C<@eval> statement will search |
|
|
128 | the players inventory for the same term as the C<@cond> and then |
|
|
129 | decreases the number of objects used there. |
|
|
130 | |
|
|
131 | (See also the map: C<scorn/houses/cornerbrook.map> for an example how this is |
|
|
132 | used in the real world :-) |
|
|
133 | |
|
|
134 | =back |
|
|
135 | |
112 | =item @eval perl |
136 | =item @eval perl |
113 | |
137 | |
114 | Like C<@cond>, but proceed regardless of the outcome. |
138 | Like C<@cond>, but proceed regardless of the outcome. |
115 | |
139 | |
116 | =item @msg perl |
140 | =item @msg perl |
… | |
… | |
182 | When a state argument is given it should be either 0 or 1. 1 will 'push' the connection |
206 | When a state argument is given it should be either 0 or 1. 1 will 'push' the connection |
183 | and 0 will 'release' the connection. This is useful for example when you want to |
207 | and 0 will 'release' the connection. This is useful for example when you want to |
184 | let a npc control a door. |
208 | let a npc control a door. |
185 | |
209 | |
186 | Trigger all objects with the given connected-id by 'releasing' the connection. |
210 | Trigger all objects with the given connected-id by 'releasing' the connection. |
|
|
211 | |
|
|
212 | =item @playersound face-name |
|
|
213 | |
|
|
214 | Plays the given sound face (either an alias or sound file path) so that |
|
|
215 | only the player talking to the npc can hear it. |
|
|
216 | |
|
|
217 | =item @npcsound face-name |
|
|
218 | |
|
|
219 | Plays the given sound face (either an alias or sound file path) as if |
|
|
220 | the npc had made that sound, i.e. it will be located at the npc and all |
|
|
221 | players near enough can hear it. |
187 | |
222 | |
188 | =item @addtopic topic |
223 | =item @addtopic topic |
189 | |
224 | |
190 | Adds the given topic names (separated by C<|>) to the list of topics |
225 | Adds the given topic names (separated by C<|>) to the list of topics |
191 | returned. |
226 | returned. |
… | |
… | |
233 | or next topic; |
268 | or next topic; |
234 | |
269 | |
235 | } elsif ($cmd eq "comment") { |
270 | } elsif ($cmd eq "comment") { |
236 | # nop |
271 | # nop |
237 | |
272 | |
|
|
273 | } elsif ($cmd eq "playersound") { |
|
|
274 | $self->{ob}->contr->play_sound (cf::sound::find $args); |
|
|
275 | |
|
|
276 | } elsif ($cmd eq "npcsound") { |
|
|
277 | $self->{npc}->play_sound (cf::sound::find $args); |
|
|
278 | |
238 | } elsif ($cmd eq "cond") { |
279 | } elsif ($cmd eq "cond") { |
239 | cf::safe_eval $args, %vars |
280 | cf::safe_eval $args, %vars |
240 | or next topic; |
281 | or next topic; |
241 | |
282 | |
242 | } elsif ($cmd eq "eval") { |
283 | } elsif ($cmd eq "eval") { |
… | |
… | |
290 | } |
331 | } |
291 | } |
332 | } |
292 | |
333 | |
293 | delete $self->{npc}{$self->{ob}->name}{dialog_state} unless %$state; |
334 | delete $self->{npc}{$self->{ob}->name}{dialog_state} unless %$state; |
294 | delete $self->{ob}{dialog_flag} unless %$flag; |
335 | delete $self->{ob}{dialog_flag} unless %$flag; |
295 | |
|
|
296 | # combine lines into paragraphs |
|
|
297 | $reply =~ s/(?<=\S)\n(?=\w)/ /g; |
|
|
298 | $reply =~ s/\n\n/\n/g; |
|
|
299 | |
336 | |
300 | # ignores flags and npc from replies |
337 | # ignores flags and npc from replies |
301 | $reply = join "\n", (map $_->[1], @replies), $reply; |
338 | $reply = join "\n", (map $_->[1], @replies), $reply; |
302 | |
339 | |
303 | # now mark up all matching keywords |
340 | # now mark up all matching keywords |
… | |
… | |
308 | last; |
345 | last; |
309 | } |
346 | } |
310 | } |
347 | } |
311 | } |
348 | } |
312 | |
349 | |
|
|
350 | $self->{npc}->use_trigger if $self->{npc}->type == cf::MAGIC_EAR; |
|
|
351 | |
313 | return wantarray ? ($reply, @kw) : $reply; |
352 | return wantarray ? ($reply, @kw) : $reply; |
314 | } |
353 | } |
315 | } |
354 | } |
316 | } |
355 | } |
317 | |
356 | |