1 | #! perl |
1 | # |
|
|
2 | # This file is part of Deliantra, the Roguelike Realtime MMORPG. |
|
|
3 | # |
|
|
4 | # Copyright (©) 2008,2009,2010,2011,2012 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 | # |
2 | |
22 | |
3 | # this implements the mapscript connectable |
23 | =head1 NAME |
4 | |
24 | |
5 | package safe::mapscript; |
25 | cf::mapscript |
6 | |
26 | |
7 | use strict qw(subs vars); |
27 | =head1 DESCRIPTION |
8 | |
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 | |
9 | our ($self, $state, $activator, $originator); |
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 |
10 | |
75 | |
11 | sub find($) { |
76 | sub find($) { |
12 | ref $_[0] ? $_[0] |
77 | ref $_[0] ? $_[0] |
13 | : $self->map->find_link ($_[0]) |
78 | : $self->map->find_link ($_[0]) |
14 | } |
79 | } |
15 | |
80 | |
16 | sub get($) { |
81 | =item trigger $id_or_object[, $state] |
17 | (&find)[0]->value |
|
|
18 | } |
|
|
19 | |
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 | |
20 | sub set($;$) { |
89 | sub trigger($;$) { |
21 | $self->map->trigger ($_[0], $#_ ? $_[1] : 1, $self); |
90 | $self->map->trigger ($_[0], $#_ ? $_[1] : 1, $self); |
22 | } |
91 | } |
23 | |
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 | |
24 | sub timer($$) { |
101 | sub timer($$) { |
25 | my $ob = (&find)[0]; |
102 | my $ob = (&find)[0]; |
26 | $ob->speed_left ($_[1] / -cf::TICK); |
103 | $ob->speed_left (-$_[1] / cf::TICK); |
27 | $ob->set_speed (1); |
104 | $ob->set_speed (1); |
28 | } |
105 | } |
29 | |
106 | |
30 | package cf::mapscript; |
107 | package cf::mapscript; |
31 | |
108 | |
32 | use strict qw(subs vars); |
109 | use common::sense; |
|
|
110 | |
|
|
111 | *{"main::safe::cf::mapscript::eval::"} = \%{"main::cf::mapscript::eval::"}; |
|
|
112 | |
|
|
113 | our %CACHE; |
33 | |
114 | |
34 | sub activate($$$) { |
115 | sub activate($$$) { |
|
|
116 | package cf::mapscript::eval; |
|
|
117 | |
35 | ($self, $state, $activator, $originator) = @_; |
118 | ($self, $state, $activator, $originator) = @_; |
36 | |
119 | |
37 | warn "$self->{msg} $self->{on_activate}\n";#d# |
|
|
38 | |
|
|
39 | ( |
120 | ( |
40 | $self->{on_activate} ||= cf::safe_eval |
121 | $CACHE{$self->msg} ||= cf::safe_eval |
41 | "package mapscript; sub {\n" |
122 | "package cf::mapscript::eval; sub {\n" |
42 | . "#line 1 '" . ($self->debug_desc) . "'\n" |
123 | . "#line 1 '" . ($self->debug_desc) . "'\n" |
43 | . $self->msg |
124 | . $self->msg |
44 | . "\n}" |
125 | . "\n}" |
45 | or sub { } |
126 | or sub { } |
46 | )->(); |
127 | )->(); |
47 | } |
128 | } |
48 | |
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 | |
49 | 1; |
138 | 1; |