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