… | |
… | |
74 | print STDERR "cfperl: $msg"; |
74 | print STDERR "cfperl: $msg"; |
75 | LOG llevError, "cfperl: $msg"; |
75 | LOG llevError, "cfperl: $msg"; |
76 | }; |
76 | }; |
77 | } |
77 | } |
78 | |
78 | |
|
|
79 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
|
|
80 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
|
|
81 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
|
|
82 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
|
|
83 | @safe::cf::map::ISA = @cf::map::ISA = 'cf::attachable'; |
79 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
84 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
80 | |
85 | |
81 | # we bless all objects into (empty) derived classes to force a method lookup |
86 | # we bless all objects into (empty) derived classes to force a method lookup |
82 | # within the Safe compartment. |
87 | # within the Safe compartment. |
83 | for my $pkg (qw( |
88 | for my $pkg (qw( |
|
|
89 | cf::global |
84 | cf::object cf::object::player |
90 | cf::object cf::object::player |
85 | cf::client cf::player |
91 | cf::client cf::player |
86 | cf::arch cf::living |
92 | cf::arch cf::living |
87 | cf::map cf::party cf::region |
93 | cf::map cf::party cf::region |
88 | )) { |
94 | )) { |
… | |
… | |
132 | |
138 | |
133 | =cut |
139 | =cut |
134 | |
140 | |
135 | ############################################################################# |
141 | ############################################################################# |
136 | |
142 | |
137 | =head2 EVENTS AND OBJECT ATTACHMENTS |
143 | =head2 ATTACHABLE OBJECTS |
|
|
144 | |
|
|
145 | Many objects in crossfire are so-called attachable objects. That means you can |
|
|
146 | attach callbacks/event handlers (a collection of which is called an "attachment") |
|
|
147 | to it. All such attachable objects support the following methods. |
|
|
148 | |
|
|
149 | In the following description, CLASS can be any of C<global>, C<object> |
|
|
150 | C<player>, C<client> or C<map> (i.e. the attachable objects in |
|
|
151 | crossfire+). |
138 | |
152 | |
139 | =over 4 |
153 | =over 4 |
140 | |
154 | |
141 | =item $object->attach ($attachment, key => $value...) |
|
|
142 | |
|
|
143 | =item $object->detach ($attachment) |
|
|
144 | |
|
|
145 | Attach/detach a pre-registered attachment to an object. |
|
|
146 | |
|
|
147 | =item $player->attach ($attachment, key => $value...) |
|
|
148 | |
|
|
149 | =item $player->detach ($attachment) |
|
|
150 | |
|
|
151 | Attach/detach a pre-registered attachment to a player. |
|
|
152 | |
|
|
153 | =item $map->attach ($attachment, key => $value...) |
|
|
154 | |
|
|
155 | =item $map->detach ($attachment) |
|
|
156 | |
|
|
157 | Attach/detach a pre-registered attachment to a client. |
|
|
158 | |
|
|
159 | =item $client->attach ($attachment, key => $value...) |
155 | =item $attachable->attach ($attachment, key => $value...) |
160 | |
156 | |
161 | =item $client->detach ($attachment) |
157 | =item $attachable->detach ($attachment) |
162 | |
158 | |
163 | Attach/detach a pre-registered attachment to a map. |
159 | Attach/detach a pre-registered attachment to a specific object and give it |
|
|
160 | the specified key/value pairs as arguments. |
164 | |
161 | |
165 | =item $bool = $object->attached ($name) |
162 | Example, attach a minesweeper attachment to the given object, making it a |
|
|
163 | 10x10 minesweeper game: |
166 | |
164 | |
167 | =item $bool = $player->attached ($name) |
165 | $obj->attach (minesweeper => width => 10, height => 10); |
168 | |
166 | |
169 | =item $bool = $client->attached ($name) |
167 | =item $bool = $attachable->attached ($name) |
170 | |
|
|
171 | =item $bool = $map->attached ($name) |
|
|
172 | |
168 | |
173 | Checks wether the named attachment is currently attached to the object. |
169 | Checks wether the named attachment is currently attached to the object. |
174 | |
170 | |
175 | =item cf::attach_global ... |
171 | =item cf::CLASS->attach ... |
176 | |
172 | |
177 | Attach handlers for global events. |
173 | =item cf::CLASS->detach ... |
178 | |
174 | |
179 | This and all following C<attach_*>-functions expect any number of the |
175 | Define an anonymous attachment and attach it to all objects of the given |
180 | following handler/hook descriptions: |
176 | CLASS. See the next function for an explanation of its arguments. |
|
|
177 | |
|
|
178 | You can attach to global events by using the C<cf::global> class. |
|
|
179 | |
|
|
180 | Example, log all player logins: |
|
|
181 | |
|
|
182 | cf::player->attach ( |
|
|
183 | on_login => sub { |
|
|
184 | my ($pl) = @_; |
|
|
185 | ... |
|
|
186 | }, |
|
|
187 | ); |
|
|
188 | |
|
|
189 | Example, attach to the jeweler skill: |
|
|
190 | |
|
|
191 | cf::object->attach ( |
|
|
192 | type => cf::SKILL, |
|
|
193 | subtype => cf::SK_JEWELER, |
|
|
194 | on_use_skill => sub { |
|
|
195 | my ($sk, $ob, $part, $dir, $msg) = @_; |
|
|
196 | ... |
|
|
197 | }, |
|
|
198 | ); |
|
|
199 | |
|
|
200 | =item cf::CLASS::attachment $name, ... |
|
|
201 | |
|
|
202 | Register an attachment by C<$name> through which attachable objects of the |
|
|
203 | given CLASS can refer to this attachment. |
|
|
204 | |
|
|
205 | Some classes such as crossfire maps and objects can specify attachments |
|
|
206 | that are attached at load/instantiate time, thus the need for a name. |
|
|
207 | |
|
|
208 | These calls expect any number of the following handler/hook descriptions: |
181 | |
209 | |
182 | =over 4 |
210 | =over 4 |
183 | |
211 | |
184 | =item prio => $number |
212 | =item prio => $number |
185 | |
213 | |
… | |
… | |
187 | by another C<prio> setting). Lower priority handlers get executed |
215 | by another C<prio> setting). Lower priority handlers get executed |
188 | earlier. The default priority is C<0>, and many built-in handlers are |
216 | earlier. The default priority is C<0>, and many built-in handlers are |
189 | registered at priority C<-1000>, so lower priorities should not be used |
217 | registered at priority C<-1000>, so lower priorities should not be used |
190 | unless you know what you are doing. |
218 | unless you know what you are doing. |
191 | |
219 | |
|
|
220 | =item type => $type |
|
|
221 | |
|
|
222 | (Only for C<< cf::object->attach >> calls), limits the attachment to the |
|
|
223 | given type of objects only (the additional parameter C<subtype> can be |
|
|
224 | used to further limit to the given subtype). |
|
|
225 | |
192 | =item on_I<event> => \&cb |
226 | =item on_I<event> => \&cb |
193 | |
227 | |
194 | Call the given code reference whenever the named event happens (event is |
228 | Call the given code reference whenever the named event happens (event is |
195 | something like C<instantiate>, C<apply>, C<use_skill> and so on, and which |
229 | something like C<instantiate>, C<apply>, C<use_skill> and so on, and which |
196 | handlers are recognised generally depends on the type of object these |
230 | handlers are recognised generally depends on the type of object these |
… | |
… | |
205 | package and register them. Only handlers for eevents supported by the |
239 | package and register them. Only handlers for eevents supported by the |
206 | object/class are recognised. |
240 | object/class are recognised. |
207 | |
241 | |
208 | =back |
242 | =back |
209 | |
243 | |
210 | =item cf::attach_to_type $object_type, $subtype, ... |
244 | Example, define an attachment called "sockpuppet" that calls the given |
|
|
245 | event handler when a monster attacks: |
211 | |
246 | |
212 | Attach handlers for a specific object type (e.g. TRANSPORT) and |
247 | cf::object::attachment sockpuppet => |
213 | subtype. If C<$subtype> is zero or undef, matches all objects of the given |
248 | on_skill_attack => sub { |
214 | type. |
249 | my ($self, $victim) = @_; |
215 | |
250 | ... |
216 | =item cf::attach_to_objects ... |
251 | } |
217 | |
252 | } |
218 | Attach handlers to all objects. Do not use this except for debugging or |
|
|
219 | very rare events, as handlers are (obviously) called for I<all> objects in |
|
|
220 | the game. |
|
|
221 | |
|
|
222 | =item cf::attach_to_players ... |
|
|
223 | |
|
|
224 | Attach handlers to all players. |
|
|
225 | |
|
|
226 | =item cf::attach_to_clients ... |
|
|
227 | |
|
|
228 | Attach handlers to all players. |
|
|
229 | |
|
|
230 | =item cf::attach_to_maps ... |
|
|
231 | |
|
|
232 | Attach handlers to all maps. |
|
|
233 | |
|
|
234 | =item cf:register_attachment $name, ... |
|
|
235 | |
|
|
236 | Register an attachment by name through which objects can refer to this |
|
|
237 | attachment. |
|
|
238 | |
|
|
239 | =item cf:register_player_attachment $name, ... |
|
|
240 | |
|
|
241 | Register an attachment by name through which players can refer to this |
|
|
242 | attachment. |
|
|
243 | |
|
|
244 | =item cf:register_map_attachment $name, ... |
|
|
245 | |
|
|
246 | Register an attachment by name through which maps can refer to this |
|
|
247 | attachment. |
|
|
248 | |
253 | |
249 | =cut |
254 | =cut |
250 | |
255 | |
251 | # the following variables are defined in .xs and must not be re-created |
256 | # the following variables are defined in .xs and must not be re-created |
252 | our @CB_GLOBAL = (); # registry for all global events |
257 | our @CB_GLOBAL = (); # registry for all global events |
… | |
… | |
256 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
261 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
257 | our @CB_MAP = (); |
262 | our @CB_MAP = (); |
258 | |
263 | |
259 | my %attachment; |
264 | my %attachment; |
260 | |
265 | |
261 | sub _attach_cb($\%$$$) { |
266 | sub _attach_cb($$$$) { |
262 | my ($registry, $undo, $event, $prio, $cb) = @_; |
267 | my ($registry, $event, $prio, $cb) = @_; |
263 | |
268 | |
264 | use sort 'stable'; |
269 | use sort 'stable'; |
265 | |
270 | |
266 | $cb = [$prio, $cb]; |
271 | $cb = [$prio, $cb]; |
267 | |
272 | |
268 | @{$registry->[$event]} = sort |
273 | @{$registry->[$event]} = sort |
269 | { $a->[0] cmp $b->[0] } |
274 | { $a->[0] cmp $b->[0] } |
270 | @{$registry->[$event] || []}, $cb; |
275 | @{$registry->[$event] || []}, $cb; |
271 | |
|
|
272 | push @{$undo->{cb}}, [$event, $cb]; |
|
|
273 | } |
276 | } |
274 | |
277 | |
275 | # attach handles attaching event callbacks |
278 | # attach handles attaching event callbacks |
276 | # the only thing the caller has to do is pass the correct |
279 | # the only thing the caller has to do is pass the correct |
277 | # registry (== where the callback attaches to). |
280 | # registry (== where the callback attaches to). |
278 | sub _attach(\@$@) { |
281 | sub _attach { |
279 | my ($registry, $klass, @arg) = @_; |
282 | my ($registry, $klass, @arg) = @_; |
280 | |
283 | |
|
|
284 | my $object_type; |
281 | my $prio = 0; |
285 | my $prio = 0; |
282 | |
|
|
283 | my %undo = ( |
|
|
284 | registry => $registry, |
|
|
285 | cb => [], |
|
|
286 | ); |
|
|
287 | |
|
|
288 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
286 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
289 | |
287 | |
290 | while (@arg) { |
288 | while (@arg) { |
291 | my $type = shift @arg; |
289 | my $type = shift @arg; |
292 | |
290 | |
293 | if ($type eq "prio") { |
291 | if ($type eq "prio") { |
294 | $prio = shift @arg; |
292 | $prio = shift @arg; |
295 | |
293 | |
|
|
294 | } elsif ($type eq "type") { |
|
|
295 | $object_type = shift @arg; |
|
|
296 | $registry = $CB_TYPE[$object_type] ||= []; |
|
|
297 | |
|
|
298 | } elsif ($type eq "subtype") { |
|
|
299 | defined $object_type or Carp::croak "subtype specified without type"; |
|
|
300 | my $object_subtype = shift @arg; |
|
|
301 | $registry = $CB_TYPE[$object_type + $object_subtype * NUM_SUBTYPES] ||= []; |
|
|
302 | |
296 | } elsif ($type eq "package") { |
303 | } elsif ($type eq "package") { |
297 | my $pkg = shift @arg; |
304 | my $pkg = shift @arg; |
298 | |
305 | |
299 | while (my ($name, $id) = each %cb_id) { |
306 | while (my ($name, $id) = each %cb_id) { |
300 | if (my $cb = $pkg->can ($name)) { |
307 | if (my $cb = $pkg->can ($name)) { |
301 | _attach_cb $registry, %undo, $id, $prio, $cb; |
308 | _attach_cb $registry, $id, $prio, $cb; |
302 | } |
309 | } |
303 | } |
310 | } |
304 | |
311 | |
305 | } elsif (exists $cb_id{$type}) { |
312 | } elsif (exists $cb_id{$type}) { |
306 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg; |
313 | _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; |
307 | |
314 | |
308 | } elsif (ref $type) { |
315 | } elsif (ref $type) { |
309 | warn "attaching objects not supported, ignoring.\n"; |
316 | warn "attaching objects not supported, ignoring.\n"; |
310 | |
317 | |
311 | } else { |
318 | } else { |
312 | shift @arg; |
319 | shift @arg; |
313 | warn "attach argument '$type' not supported, ignoring.\n"; |
320 | warn "attach argument '$type' not supported, ignoring.\n"; |
314 | } |
321 | } |
315 | } |
322 | } |
316 | |
|
|
317 | \%undo |
|
|
318 | } |
323 | } |
319 | |
324 | |
320 | sub _attach_attachment { |
325 | sub _object_attach { |
321 | my ($obj, $name, %arg) = @_; |
326 | my ($obj, $name, %arg) = @_; |
322 | |
327 | |
323 | return if exists $obj->{_attachment}{$name}; |
328 | return if exists $obj->{_attachment}{$name}; |
324 | |
|
|
325 | my $res; |
|
|
326 | |
329 | |
327 | if (my $attach = $attachment{$name}) { |
330 | if (my $attach = $attachment{$name}) { |
328 | my $registry = $obj->registry; |
331 | my $registry = $obj->registry; |
329 | |
332 | |
330 | for (@$attach) { |
333 | for (@$attach) { |
331 | my ($klass, @attach) = @$_; |
334 | my ($klass, @attach) = @$_; |
332 | $res = _attach @$registry, $klass, @attach; |
335 | _attach $registry, $klass, @attach; |
333 | } |
336 | } |
334 | |
337 | |
335 | $obj->{$name} = \%arg; |
338 | $obj->{$name} = \%arg; |
336 | } else { |
339 | } else { |
337 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
340 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
338 | } |
341 | } |
339 | |
342 | |
340 | $obj->{_attachment}{$name} = undef; |
343 | $obj->{_attachment}{$name} = undef; |
341 | |
|
|
342 | $res->{attachment} = $name; |
|
|
343 | $res |
|
|
344 | } |
344 | } |
345 | |
345 | |
346 | *cf::object::attach = |
346 | sub cf::attachable::attach { |
347 | *cf::player::attach = |
347 | if (ref $_[0]) { |
348 | *cf::client::attach = |
348 | _object_attach @_; |
349 | *cf::map::attach = sub { |
349 | } else { |
350 | my ($obj, $name, %arg) = @_; |
350 | _attach shift->_attach_registry, @_; |
351 | |
351 | } |
352 | _attach_attachment $obj, $name, %arg; |
|
|
353 | }; |
352 | }; |
354 | |
353 | |
355 | # all those should be optimised |
354 | # all those should be optimised |
356 | *cf::object::detach = |
355 | sub cf::attachable::detach { |
357 | *cf::player::detach = |
|
|
358 | *cf::client::detach = |
|
|
359 | *cf::map::detach = sub { |
|
|
360 | my ($obj, $name) = @_; |
356 | my ($obj, $name) = @_; |
361 | |
357 | |
|
|
358 | if (ref $obj) { |
362 | delete $obj->{_attachment}{$name}; |
359 | delete $obj->{_attachment}{$name}; |
363 | reattach ($obj); |
360 | reattach ($obj); |
|
|
361 | } else { |
|
|
362 | Carp::croak "cannot, currently, detach class attachments"; |
|
|
363 | } |
364 | }; |
364 | }; |
365 | |
365 | |
366 | *cf::object::attached = |
366 | sub cf::attachable::attached { |
367 | *cf::player::attached = |
|
|
368 | *cf::client::attached = |
|
|
369 | *cf::map::attached = sub { |
|
|
370 | my ($obj, $name) = @_; |
367 | my ($obj, $name) = @_; |
371 | |
368 | |
372 | exists $obj->{_attachment}{$name} |
369 | exists $obj->{_attachment}{$name} |
373 | }; |
|
|
374 | |
|
|
375 | sub attach_global { |
|
|
376 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
|
|
377 | } |
370 | } |
378 | |
371 | |
379 | sub attach_to_type { |
372 | for my $klass (qw(GLOBAL OBJECT PLAYER CLIENT MAP)) { |
380 | my $type = shift; |
373 | eval "#line " . __LINE__ . " 'cf.pm' |
381 | my $subtype = shift; |
374 | sub cf::\L$klass\E::_attach_registry { |
|
|
375 | (\\\@CB_$klass, KLASS_$klass) |
|
|
376 | } |
382 | |
377 | |
383 | _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_ |
378 | sub cf::\L$klass\E::attachment { |
384 | } |
|
|
385 | |
|
|
386 | sub attach_to_objects { |
|
|
387 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
|
|
388 | } |
|
|
389 | |
|
|
390 | sub attach_to_players { |
|
|
391 | _attach @CB_PLAYER, KLASS_PLAYER, @_ |
|
|
392 | } |
|
|
393 | |
|
|
394 | sub attach_to_clients { |
|
|
395 | _attach @CB_CLIENT, KLASS_CLIENT, @_ |
|
|
396 | } |
|
|
397 | |
|
|
398 | sub attach_to_maps { |
|
|
399 | _attach @CB_MAP, KLASS_MAP, @_ |
|
|
400 | } |
|
|
401 | |
|
|
402 | sub register_attachment { |
|
|
403 | my $name = shift; |
379 | my \$name = shift; |
404 | |
380 | |
405 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
|
|
406 | } |
|
|
407 | |
|
|
408 | sub register_player_attachment { |
|
|
409 | my $name = shift; |
|
|
410 | |
|
|
411 | $attachment{$name} = [[KLASS_PLAYER, @_]]; |
|
|
412 | } |
|
|
413 | |
|
|
414 | sub register_client_attachment { |
|
|
415 | my $name = shift; |
|
|
416 | |
|
|
417 | $attachment{$name} = [[KLASS_CLIENT, @_]]; |
|
|
418 | } |
|
|
419 | |
|
|
420 | sub register_map_attachment { |
|
|
421 | my $name = shift; |
|
|
422 | |
|
|
423 | $attachment{$name} = [[KLASS_MAP, @_]]; |
381 | \$attachment{\$name} = [[KLASS_$klass, \@_]]; |
|
|
382 | } |
|
|
383 | "; |
|
|
384 | die if $@; |
424 | } |
385 | } |
425 | |
386 | |
426 | our $override; |
387 | our $override; |
427 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
388 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
428 | |
389 | |
… | |
… | |
474 | |
435 | |
475 | =cut |
436 | =cut |
476 | |
437 | |
477 | ############################################################################# |
438 | ############################################################################# |
478 | |
439 | |
479 | =head2 METHODS VALID FOR ALL CORE OBJECTS |
440 | =head2 METHODS VALID FOR ALL ATTACHABLE OBJECTS |
|
|
441 | |
|
|
442 | Attachable objects includes objects, players, clients and maps. |
480 | |
443 | |
481 | =over 4 |
444 | =over 4 |
482 | |
445 | |
483 | =item $object->valid, $player->valid, $client->valid, $map->valid |
446 | =item $object->valid |
484 | |
447 | |
485 | Just because you have a perl object does not mean that the corresponding |
448 | Just because you have a perl object does not mean that the corresponding |
486 | C-level object still exists. If you try to access an object that has no |
449 | C-level object still exists. If you try to access an object that has no |
487 | valid C counterpart anymore you get an exception at runtime. This method |
450 | valid C counterpart anymore you get an exception at runtime. This method |
488 | can be used to test for existence of the C object part without causing an |
451 | can be used to test for existence of the C object part without causing an |
… | |
… | |
490 | |
453 | |
491 | =back |
454 | =back |
492 | |
455 | |
493 | =cut |
456 | =cut |
494 | |
457 | |
495 | *cf::object::valid = |
|
|
496 | *cf::player::valid = |
|
|
497 | *cf::client::valid = |
|
|
498 | *cf::map::valid = \&cf::_valid; |
|
|
499 | |
|
|
500 | ############################################################################# |
458 | ############################################################################# |
501 | # object support |
459 | # object support |
502 | |
460 | |
503 | sub instantiate { |
461 | sub instantiate { |
504 | my ($obj, $data) = @_; |
462 | my ($obj, $data) = @_; |
… | |
… | |
523 | |
481 | |
524 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
482 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
525 | if (my $attach = $attachment{$name}) { |
483 | if (my $attach = $attachment{$name}) { |
526 | for (@$attach) { |
484 | for (@$attach) { |
527 | my ($klass, @attach) = @$_; |
485 | my ($klass, @attach) = @$_; |
528 | _attach @$registry, $klass, @attach; |
486 | _attach $registry, $klass, @attach; |
529 | } |
487 | } |
530 | } else { |
488 | } else { |
531 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
489 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
532 | } |
490 | } |
533 | } |
491 | } |
… | |
… | |
589 | } |
547 | } |
590 | |
548 | |
591 | () |
549 | () |
592 | } |
550 | } |
593 | |
551 | |
594 | attach_to_objects |
552 | cf::object->attach ( |
595 | prio => -1000000, |
553 | prio => -1000000, |
596 | on_clone => sub { |
554 | on_clone => sub { |
597 | my ($src, $dst) = @_; |
555 | my ($src, $dst) = @_; |
598 | |
556 | |
599 | @{$dst->registry} = @{$src->registry}; |
557 | @{$dst->registry} = @{$src->registry}; |
… | |
… | |
601 | %$dst = %$src; |
559 | %$dst = %$src; |
602 | |
560 | |
603 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
561 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
604 | if exists $src->{_attachment}; |
562 | if exists $src->{_attachment}; |
605 | }, |
563 | }, |
606 | ; |
564 | ); |
607 | |
565 | |
608 | ############################################################################# |
566 | ############################################################################# |
609 | # command handling &c |
567 | # command handling &c |
610 | |
568 | |
611 | =item cf::register_command $name => \&callback($ob,$args); |
569 | =item cf::register_command $name => \&callback($ob,$args); |
… | |
… | |
640 | #warn "registering extcmd '$name' to '$caller'"; |
598 | #warn "registering extcmd '$name' to '$caller'"; |
641 | |
599 | |
642 | $EXTCMD{$name} = [$cb, $caller]; |
600 | $EXTCMD{$name} = [$cb, $caller]; |
643 | } |
601 | } |
644 | |
602 | |
645 | attach_to_players |
603 | cf::player->attach ( |
646 | on_command => sub { |
604 | on_command => sub { |
647 | my ($pl, $name, $params) = @_; |
605 | my ($pl, $name, $params) = @_; |
648 | |
606 | |
649 | my $cb = $COMMAND{$name} |
607 | my $cb = $COMMAND{$name} |
650 | or return; |
608 | or return; |
… | |
… | |
670 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
628 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
671 | } |
629 | } |
672 | |
630 | |
673 | cf::override; |
631 | cf::override; |
674 | }, |
632 | }, |
675 | ; |
633 | ); |
676 | |
634 | |
677 | sub register { |
635 | sub register { |
678 | my ($base, $pkg) = @_; |
636 | my ($base, $pkg) = @_; |
679 | |
637 | |
680 | #TODO |
638 | #TODO |
… | |
… | |
770 | defined $path or return; |
728 | defined $path or return; |
771 | |
729 | |
772 | unlink "$path.pst"; |
730 | unlink "$path.pst"; |
773 | }; |
731 | }; |
774 | |
732 | |
775 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
733 | cf::map->attach (prio => -10000, package => cf::mapsupport::); |
776 | |
734 | |
777 | ############################################################################# |
735 | ############################################################################# |
778 | # load/save perl data associated with player->ob objects |
736 | # load/save perl data associated with player->ob objects |
779 | |
737 | |
780 | sub all_objects(@) { |
738 | sub all_objects(@) { |
781 | @_, map all_objects ($_->inv), @_ |
739 | @_, map all_objects ($_->inv), @_ |
782 | } |
740 | } |
783 | |
741 | |
784 | # TODO: compatibility cruft, remove when no longer needed |
742 | # TODO: compatibility cruft, remove when no longer needed |
785 | attach_to_players |
743 | cf::player->attach ( |
786 | on_load => sub { |
744 | on_load => sub { |
787 | my ($pl, $path) = @_; |
745 | my ($pl, $path) = @_; |
788 | |
746 | |
789 | for my $o (all_objects $pl->ob) { |
747 | for my $o (all_objects $pl->ob) { |
790 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
748 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
… | |
… | |
792 | |
750 | |
793 | %$o = %{ Storable::thaw pack "H*", $value }; |
751 | %$o = %{ Storable::thaw pack "H*", $value }; |
794 | } |
752 | } |
795 | } |
753 | } |
796 | }, |
754 | }, |
797 | ; |
755 | ); |
798 | |
756 | |
799 | ############################################################################# |
757 | ############################################################################# |
800 | |
758 | |
801 | =head2 CORE EXTENSIONS |
759 | =head2 CORE EXTENSIONS |
802 | |
760 | |
803 | Functions and methods that extend core crossfire objects. |
761 | Functions and methods that extend core crossfire objects. |
|
|
762 | |
|
|
763 | =head3 cf::player |
804 | |
764 | |
805 | =over 4 |
765 | =over 4 |
806 | |
766 | |
807 | =item cf::player::exists $login |
767 | =item cf::player::exists $login |
808 | |
768 | |
… | |
… | |
813 | sub cf::player::exists($) { |
773 | sub cf::player::exists($) { |
814 | cf::player::find $_[0] |
774 | cf::player::find $_[0] |
815 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
775 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
816 | } |
776 | } |
817 | |
777 | |
|
|
778 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
|
|
779 | |
|
|
780 | Sends an ext reply to the player. |
|
|
781 | |
|
|
782 | =cut |
|
|
783 | |
|
|
784 | sub cf::player::ext_reply($$$%) { |
|
|
785 | my ($self, $id, %msg) = @_; |
|
|
786 | |
|
|
787 | $msg{msgid} = $id; |
|
|
788 | |
|
|
789 | $self->send ("ext " . to_json \%msg); |
|
|
790 | } |
|
|
791 | |
|
|
792 | =back |
|
|
793 | |
|
|
794 | =head3 cf::object::player |
|
|
795 | |
|
|
796 | =over 4 |
|
|
797 | |
818 | =item $player_object->reply ($npc, $msg[, $flags]) |
798 | =item $player_object->reply ($npc, $msg[, $flags]) |
819 | |
799 | |
820 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
800 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
821 | can be C<undef>. Does the right thing when the player is currently in a |
801 | can be C<undef>. Does the right thing when the player is currently in a |
822 | dialogue with the given NPC character. |
802 | dialogue with the given NPC character. |
823 | |
803 | |
824 | =cut |
804 | =cut |
825 | |
805 | |
826 | # rough implementation of a future "reply" method that works |
806 | # rough implementation of a future "reply" method that works |
827 | # with dialog boxes. |
807 | # with dialog boxes. |
|
|
808 | #TODO: the first argument must go, split into a $npc->reply_to ( method |
828 | sub cf::object::player::reply($$$;$) { |
809 | sub cf::object::player::reply($$$;$) { |
829 | my ($self, $npc, $msg, $flags) = @_; |
810 | my ($self, $npc, $msg, $flags) = @_; |
830 | |
811 | |
831 | $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
812 | $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
832 | |
813 | |
… | |
… | |
836 | $msg = $npc->name . " says: $msg" if $npc; |
817 | $msg = $npc->name . " says: $msg" if $npc; |
837 | $self->message ($msg, $flags); |
818 | $self->message ($msg, $flags); |
838 | } |
819 | } |
839 | } |
820 | } |
840 | |
821 | |
841 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
|
|
842 | |
|
|
843 | Sends an ext reply to the player. |
|
|
844 | |
|
|
845 | =cut |
|
|
846 | |
|
|
847 | sub cf::player::ext_reply($$$%) { |
|
|
848 | my ($self, $id, %msg) = @_; |
|
|
849 | |
|
|
850 | $msg{msgid} = $id; |
|
|
851 | |
|
|
852 | $self->send ("ext " . to_json \%msg); |
|
|
853 | } |
|
|
854 | |
|
|
855 | =item $player_object->may ("access") |
822 | =item $player_object->may ("access") |
856 | |
823 | |
857 | Returns wether the given player is authorized to access resource "access" |
824 | Returns wether the given player is authorized to access resource "access" |
858 | (e.g. "command_wizcast"). |
825 | (e.g. "command_wizcast"). |
859 | |
826 | |
… | |
… | |
866 | (ref $cf::CFG{"may_$access"} |
833 | (ref $cf::CFG{"may_$access"} |
867 | ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} |
834 | ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} |
868 | : $cf::CFG{"may_$access"}) |
835 | : $cf::CFG{"may_$access"}) |
869 | } |
836 | } |
870 | |
837 | |
871 | =cut |
838 | =head3 cf::client |
872 | |
839 | |
873 | ############################################################################# |
840 | =over 4 |
|
|
841 | |
|
|
842 | =item $client->send_drawinfo ($text, $flags) |
|
|
843 | |
|
|
844 | Sends a drawinfo packet to the client. Circumvents output buffering so |
|
|
845 | should not be used under normal circumstances. |
|
|
846 | |
|
|
847 | =cut |
|
|
848 | |
|
|
849 | sub cf::client::send_drawinfo { |
|
|
850 | my ($self, $text, $flags) = @_; |
|
|
851 | |
|
|
852 | utf8::encode $text; |
|
|
853 | $self->send_packet (sprintf "drawinfo %d %s", $flags, $text); |
|
|
854 | } |
|
|
855 | |
|
|
856 | |
|
|
857 | =item $success = $client->query ($flags, "text", \&cb) |
|
|
858 | |
|
|
859 | Queues a query to the client, calling the given callback with |
|
|
860 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
|
|
861 | C<cf::CS_QUERY_SINGLECHAR> or C<cf::CS_QUERY_HIDEINPUT> or C<0>. |
|
|
862 | |
|
|
863 | Queries can fail, so check the return code. Or don't, as queries will become |
|
|
864 | reliable at some point in the future. |
|
|
865 | |
|
|
866 | =cut |
|
|
867 | |
|
|
868 | sub cf::client::query { |
|
|
869 | my ($self, $flags, $text, $cb) = @_; |
|
|
870 | |
|
|
871 | return unless $self->state == ST_PLAYING |
|
|
872 | || $self->state == ST_SETUP |
|
|
873 | || $self->state == ST_CUSTOM; |
|
|
874 | |
|
|
875 | $self->state (ST_CUSTOM); |
|
|
876 | |
|
|
877 | utf8::encode $text; |
|
|
878 | push @{ $self->{query_queue} }, [(sprintf "query %d %s", $flags, $text), $cb]; |
|
|
879 | |
|
|
880 | $self->send_packet ($self->{query_queue}[0][0]) |
|
|
881 | if @{ $self->{query_queue} } == 1; |
|
|
882 | } |
|
|
883 | |
|
|
884 | cf::client->attach ( |
|
|
885 | on_reply => sub { |
|
|
886 | my ($ns, $msg) = @_; |
|
|
887 | |
|
|
888 | # this weird shuffling is so that direct followup queries |
|
|
889 | # get handled first |
|
|
890 | my $queue = delete $ns->{query_queue}; |
|
|
891 | |
|
|
892 | (shift @$queue)->[1]->($msg); |
|
|
893 | |
|
|
894 | push @{ $ns->{query_queue} }, @$queue; |
|
|
895 | |
|
|
896 | if (@{ $ns->{query_queue} } == @$queue) { |
|
|
897 | if (@$queue) { |
|
|
898 | $ns->send_packet ($ns->{query_queue}[0][0]); |
|
|
899 | } else { |
|
|
900 | $ns->state (ST_PLAYING); |
|
|
901 | } |
|
|
902 | } |
|
|
903 | }, |
|
|
904 | ); |
|
|
905 | |
|
|
906 | =back |
|
|
907 | |
874 | |
908 | |
875 | =head2 SAFE SCRIPTING |
909 | =head2 SAFE SCRIPTING |
876 | |
910 | |
877 | Functions that provide a safe environment to compile and execute |
911 | Functions that provide a safe environment to compile and execute |
878 | snippets of perl code without them endangering the safety of the server |
912 | snippets of perl code without them endangering the safety of the server |
… | |
… | |
1078 | $DB->{$_[0]} = $_[1]; |
1112 | $DB->{$_[0]} = $_[1]; |
1079 | } |
1113 | } |
1080 | db_dirty; |
1114 | db_dirty; |
1081 | } |
1115 | } |
1082 | |
1116 | |
1083 | attach_global |
1117 | cf::global->attach ( |
1084 | prio => 10000, |
1118 | prio => 10000, |
1085 | on_cleanup => sub { |
1119 | on_cleanup => sub { |
1086 | db_sync; |
1120 | db_sync; |
1087 | }, |
1121 | }, |
1088 | ; |
1122 | ); |
1089 | } |
1123 | } |
1090 | |
1124 | |
1091 | ############################################################################# |
1125 | ############################################################################# |
1092 | # the server's main() |
1126 | # the server's main() |
1093 | |
1127 | |