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

# Content
1 #! perl
2
3 #
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 =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
45 package cf::mapscript::eval;
46
47 use common::sense;
48
49 =item $self
50
51 The mapscript object itself.
52
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 use vars qw($self $state $activator $originator);
70
71 =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 sub find($) {
79 ref $_[0] ? $_[0]
80 : $self->map->find_link ($_[0])
81 }
82
83 =item trigger $id_or_object[, $state]
84
85 Triggers the linked chain with the given I<connected> id, or the connected
86 chain associated with the given object (if an object reference is passed),
87 and passes the given state (or C<1>, if missing) to it.
88
89 =cut
90
91 sub trigger($;$) {
92 $self->map->trigger ($_[0], $#_ ? $_[1] : 1, $self);
93 }
94
95 =item timer $id_or_object, $seconds
96
97 Starts the timer on the given mapscript object (usually, C<$id_or_object> is
98 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 sub timer($$) {
104 my $ob = (&find)[0];
105 $ob->speed_left (-$_[1] / cf::TICK);
106 $ob->set_speed (1);
107 }
108
109 package cf::mapscript;
110
111 use common::sense;
112
113 *{"main::safe::cf::mapscript::eval::"} = \%{"main::cf::mapscript::eval::"};
114
115 our %CACHE;
116
117 sub activate($$$) {
118 package cf::mapscript::eval;
119
120 ($self, $state, $activator, $originator) = @_;
121
122 (
123 $CACHE{$self->msg} ||= cf::safe_eval
124 "package cf::mapscript::eval; sub {\n"
125 . "#line 1 '" . ($self->debug_desc) . "'\n"
126 . $self->msg
127 . "\n}"
128 or sub { }
129 )->();
130 }
131
132 =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 1;