ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf/mapscript.pm
Revision: 1.9
Committed: Fri Apr 30 18:28:16 2010 UTC (14 years, 1 month ago) by root
Branch: MAIN
CVS Tags: rel-3_0
Changes since 1.8: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #! perl
2    
3 root 1.7 #
4     # This file is part of Deliantra, the Roguelike Realtime MMORPG.
5     #
6     # Copyright (©) 2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
7     #
8     # Deliantra is free software: you can redistribute it and/or modify it under
9     # the terms of the Affero GNU General Public License as published by the
10     # Free Software Foundation, either version 3 of the License, or (at your
11     # option) any later version.
12     #
13     # This program is distributed in the hope that it will be useful,
14     # but WITHOUT ANY WARRANTY; without even the implied warranty of
15     # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     # GNU General Public License for more details.
17     #
18     # You should have received a copy of the Affero GNU General Public License
19     # and the GNU General Public License along with this program. If not, see
20     # <http://www.gnu.org/licenses/>.
21     #
22     # The authors can be reached via e-mail to <support@deliantra.net>
23     #
24    
25 root 1.4 =head1 NAME
26    
27     cf::mapscript
28    
29     =head1 DESCRIPTION
30    
31     This module implements the mapscript object.
32    
33     Map scripts are perl snippets that get executed whenever any connected
34     element is triggered (e.g. a check inv, a lever etc.)
35    
36     =head1 ENVIRONMENT
37    
38     The map scripts are compiled and executed into a namespace with the
39     following symbols available:
40    
41     =over 4
42    
43     =cut
44 root 1.1
45 root 1.8 package cf::mapscript::eval;
46 root 1.1
47 root 1.6 use common::sense;
48 root 1.1
49 root 1.4 =item $self
50    
51 root 1.6 The mapscript object itself.
52 root 1.4
53     =item $state
54    
55     The state value (0 means release, <>0 means push/trigger/enable) that
56     triggered the map script.
57    
58     =item $activator
59    
60     The object that was triggered (the lever, check inv element, npc etc.).
61    
62     =item $originator
63    
64     The object that triggered the activator, usually (but not always) the
65     player who stepped on a check inv, pulled a lever etc. Can be C<undef>.
66    
67     =cut
68    
69 root 1.8 use vars qw($self $state $activator $originator);
70 root 1.2
71 root 1.4 =item @obs = find $id_or_object
72    
73     Finds all objects with the given I<connected> C<$id>. If an object
74     reference is passed, it will be returned unchanged.
75    
76     =cut
77    
78 root 1.2 sub find($) {
79     ref $_[0] ? $_[0]
80     : $self->map->find_link ($_[0])
81     }
82    
83 root 1.4 =item trigger $id_or_object[, $state]
84    
85     Triggers the linked chain with the given I<connected> id, or the connected
86 root 1.9 chain associated with the given object (if an object reference is passed),
87 root 1.4 and passes the given state (or C<1>, if missing) to it.
88    
89     =cut
90 root 1.2
91 root 1.3 sub trigger($;$) {
92 root 1.2 $self->map->trigger ($_[0], $#_ ? $_[1] : 1, $self);
93     }
94    
95 root 1.4 =item timer $id_or_object, $seconds
96    
97 root 1.9 Starts the timer on the given mapscript object (usually, C<$id_or_object> is
98 root 1.4 C<$self>). When the timer expires on the mapscript object, it will trigger
99     the script with C<$activator == $self> and C<$originator == undef>.
100    
101     =cut
102    
103 root 1.2 sub timer($$) {
104     my $ob = (&find)[0];
105 root 1.5 $ob->speed_left (-$_[1] / cf::TICK);
106 root 1.2 $ob->set_speed (1);
107     }
108 root 1.1
109     package cf::mapscript;
110    
111 root 1.8 use common::sense;
112    
113     *{"main::safe::cf::mapscript::eval::"} = \%{"main::cf::mapscript::eval::"};
114 root 1.1
115 root 1.3 our %CACHE;
116    
117 root 1.1 sub activate($$$) {
118 root 1.8 package cf::mapscript::eval;
119    
120 root 1.2 ($self, $state, $activator, $originator) = @_;
121 root 1.1
122     (
123 root 1.3 $CACHE{$self->msg} ||= cf::safe_eval
124 root 1.8 "package cf::mapscript::eval; sub {\n"
125 root 1.1 . "#line 1 '" . ($self->debug_desc) . "'\n"
126     . $self->msg
127     . "\n}"
128     or sub { }
129     )->();
130     }
131    
132 root 1.4 =back
133    
134     =head1 EXAMPLE
135    
136     Have a look at F<scorn/anthonty/portgate.map> for a nontrivial example.
137    
138     =cut
139    
140 root 1.1 1;