ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.93 by root, Thu Dec 21 22:41:35 2006 UTC vs.
Revision 1.95 by root, Fri Dec 22 02:04:20 2006 UTC

140 140
141############################################################################# 141#############################################################################
142 142
143=head2 ATTACHABLE OBJECTS 143=head2 ATTACHABLE OBJECTS
144 144
145You can define and attach attachments to each "attachable" object in 145Many objects in crossfire are so-called attachable objects. That means you can
146crossfire+ (objects, players, clients, maps and the special "global" 146attach callbacks/event handlers (a collection of which is called an "attachment")
147to it. All such attachable objects support the following methods.
148
147class). In the following description, CLASS can be any of C<global>, 149In the following description, CLASS can be any of C<global>, C<object>
148C<object> C<player>, C<client> or C<map>. 150C<player>, C<client> or C<map> (i.e. the attachable objects in
151crossfire+).
149 152
150=over 4 153=over 4
151 154
155=item $attachable->attach ($attachment, key => $value...)
156
157=item $attachable->detach ($attachment)
158
159Attach/detach a pre-registered attachment to a specific object and give it
160the specified key/value pairs as arguments.
161
162Example, attach a minesweeper attachment to the given object, making it a
16310x10 minesweeper game:
164
165 $obj->attach (minesweeper => width => 10, height => 10);
166
167=item $bool = $attachable->attached ($name)
168
169Checks wether the named attachment is currently attached to the object.
170
171=item cf::CLASS->attach ...
172
173=item cf::CLASS->detach ...
174
175Define an anonymous attachment and attach it to all objects of the given
176CLASS. See the next function for an explanation of its arguments.
177
178You can attach to global events by using the C<cf::global> class.
179
180Example, log all player logins:
181
182 cf::player->attach (
183 on_login => sub {
184 my ($pl) = @_;
185 ...
186 },
187 );
188
189Example, 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
152=item cf::CLASS::attachment $name, ... 200=item cf::CLASS::attachment $name, ...
153 201
154Register an attachment by name through which attachable objects can refer 202Register an attachment by C<$name> through which attachable objects of the
155to this attachment. 203given CLASS can refer to this attachment.
156 204
157=item $bool = $attachable->attached ($name) 205Some classes such as crossfire maps and objects can specify attachments
206that are attached at load/instantiate time, thus the need for a name.
158 207
159Checks wether the named attachment is currently attached to the object.
160
161=item $attachable->attach ($attachment, key => $value...)
162
163=item $attachable->detach ($attachment)
164
165Attach/detach a pre-registered attachment either to a specific object
166(C<$attachable>) or all objects of the given class (if C<$attachable> is a
167class in a static method call).
168
169You can attach to global events by using the C<cf::global> class.
170
171These method calls expect any number of the following handler/hook 208These calls expect any number of the following handler/hook descriptions:
172descriptions:
173 209
174=over 4 210=over 4
175 211
176=item prio => $number 212=item prio => $number
177 213
202Look for sub functions of the name C<< on_I<event> >> in the given 238Look for sub functions of the name C<< on_I<event> >> in the given
203package and register them. Only handlers for eevents supported by the 239package and register them. Only handlers for eevents supported by the
204object/class are recognised. 240object/class are recognised.
205 241
206=back 242=back
243
244Example, define an attachment called "sockpuppet" that calls the given
245event handler when a monster attacks:
246
247 cf::object::attachment sockpuppet =>
248 on_skill_attack => sub {
249 my ($self, $victim) = @_;
250 ...
251 }
252 }
207 253
208=cut 254=cut
209 255
210# 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
211our @CB_GLOBAL = (); # registry for all global events 257our @CB_GLOBAL = (); # registry for all global events
712 758
713=head2 CORE EXTENSIONS 759=head2 CORE EXTENSIONS
714 760
715Functions and methods that extend core crossfire objects. 761Functions and methods that extend core crossfire objects.
716 762
763=head3 cf::player
764
717=over 4 765=over 4
718 766
719=item cf::player::exists $login 767=item cf::player::exists $login
720 768
721Returns true when the given account exists. 769Returns true when the given account exists.
725sub cf::player::exists($) { 773sub cf::player::exists($) {
726 cf::player::find $_[0] 774 cf::player::find $_[0]
727 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;
728} 776}
729 777
778=item $player->ext_reply ($msgid, $msgtype, %msg)
779
780Sends an ext reply to the player.
781
782=cut
783
784sub 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
730=item $player_object->reply ($npc, $msg[, $flags]) 798=item $player_object->reply ($npc, $msg[, $flags])
731 799
732Sends a message to the player, as if the npc C<$npc> replied. C<$npc> 800Sends a message to the player, as if the npc C<$npc> replied. C<$npc>
733can be C<undef>. Does the right thing when the player is currently in a 801can be C<undef>. Does the right thing when the player is currently in a
734dialogue with the given NPC character. 802dialogue with the given NPC character.
735 803
736=cut 804=cut
737 805
738# rough implementation of a future "reply" method that works 806# rough implementation of a future "reply" method that works
739# with dialog boxes. 807# with dialog boxes.
808#TODO: the first argument must go, split into a $npc->reply_to ( method
740sub cf::object::player::reply($$$;$) { 809sub cf::object::player::reply($$$;$) {
741 my ($self, $npc, $msg, $flags) = @_; 810 my ($self, $npc, $msg, $flags) = @_;
742 811
743 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; 812 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4;
744 813
748 $msg = $npc->name . " says: $msg" if $npc; 817 $msg = $npc->name . " says: $msg" if $npc;
749 $self->message ($msg, $flags); 818 $self->message ($msg, $flags);
750 } 819 }
751} 820}
752 821
753=item $player->ext_reply ($msgid, $msgtype, %msg)
754
755Sends an ext reply to the player.
756
757=cut
758
759sub cf::player::ext_reply($$$%) {
760 my ($self, $id, %msg) = @_;
761
762 $msg{msgid} = $id;
763
764 $self->send ("ext " . to_json \%msg);
765}
766
767=item $player_object->may ("access") 822=item $player_object->may ("access")
768 823
769Returns wether the given player is authorized to access resource "access" 824Returns wether the given player is authorized to access resource "access"
770(e.g. "command_wizcast"). 825(e.g. "command_wizcast").
771 826
778 (ref $cf::CFG{"may_$access"} 833 (ref $cf::CFG{"may_$access"}
779 ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} 834 ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}}
780 : $cf::CFG{"may_$access"}) 835 : $cf::CFG{"may_$access"})
781} 836}
782 837
783=cut 838=head3 cf::client
784 839
785############################################################################# 840=over 4
841
842=item $client->send_drawinfo ($text, $flags)
843
844Sends a drawinfo packet to the client. Circumvents output buffering so
845should not be used under normal circumstances.
846
847=cut
848
849sub 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
859Queues a query to the client, calling the given callback with
860the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>,
861C<cf::CS_QUERY_SINGLECHAR> or C<cf::CS_QUERY_HIDEINPUT> or C<0>.
862
863Queries can fail, so check the return code. Or don't, as queries will become
864reliable at some point in the future.
865
866=cut
867
868sub 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
884cf::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
786 908
787=head2 SAFE SCRIPTING 909=head2 SAFE SCRIPTING
788 910
789Functions that provide a safe environment to compile and execute 911Functions that provide a safe environment to compile and execute
790snippets of perl code without them endangering the safety of the server 912snippets of perl code without them endangering the safety of the server

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines