1 |
# |
2 |
# This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
# |
4 |
# Copyright (©) 2008,2009,2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
# |
6 |
# 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 |
# |
11 |
# 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 |
# |
16 |
# 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 |
# |
20 |
# The authors can be reached via e-mail to <support@deliantra.net> |
21 |
# |
22 |
|
23 |
=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 |
|
43 |
package cf::mapscript::eval; |
44 |
|
45 |
use common::sense; |
46 |
|
47 |
=item $self |
48 |
|
49 |
The mapscript object itself. |
50 |
|
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 |
use vars qw($self $state $activator $originator); |
68 |
|
69 |
=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 |
sub find($) { |
77 |
ref $_[0] ? $_[0] |
78 |
: $self->map->find_link ($_[0]) |
79 |
} |
80 |
|
81 |
=item trigger $id_or_object[, $state] |
82 |
|
83 |
Triggers the linked chain with the given I<connected> id, or the connected |
84 |
chain associated with the given object (if an object reference is passed), |
85 |
and passes the given state (or C<1>, if missing) to it. |
86 |
|
87 |
=cut |
88 |
|
89 |
sub trigger($;$) { |
90 |
$self->map->trigger ($_[0], $#_ ? $_[1] : 1, $self); |
91 |
} |
92 |
|
93 |
=item timer $id_or_object, $seconds |
94 |
|
95 |
Starts the timer on the given mapscript object (usually, C<$id_or_object> is |
96 |
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 |
sub timer($$) { |
102 |
my $ob = (&find)[0]; |
103 |
$ob->speed_left (-$_[1] / cf::TICK); |
104 |
$ob->set_speed (1); |
105 |
} |
106 |
|
107 |
package cf::mapscript; |
108 |
|
109 |
use common::sense; |
110 |
|
111 |
*{"main::safe::cf::mapscript::eval::"} = \%{"main::cf::mapscript::eval::"}; |
112 |
|
113 |
our %CACHE; |
114 |
|
115 |
sub activate($$$) { |
116 |
package cf::mapscript::eval; |
117 |
|
118 |
($self, $state, $activator, $originator) = @_; |
119 |
|
120 |
( |
121 |
$CACHE{$self->msg} ||= cf::safe_eval |
122 |
"package cf::mapscript::eval; sub {\n" |
123 |
. "#line 1 '" . ($self->debug_desc) . "'\n" |
124 |
. $self->msg |
125 |
. "\n}" |
126 |
or sub { } |
127 |
)->(); |
128 |
} |
129 |
|
130 |
=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 |
1; |