1 | #! perl |
1 | #! perl |
2 | |
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 | |
3 | # this implements the mapscript connectable |
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 |
4 | |
44 | |
5 | package safe::mapscript; |
45 | package safe::mapscript; |
6 | |
46 | |
7 | use strict qw(subs vars); |
47 | use common::sense; |
8 | |
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 | |
9 | our ($self, $activator, $state); |
69 | our ($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 objetc 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, $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 | } |
10 | |
108 | |
11 | package cf::mapscript; |
109 | package cf::mapscript; |
12 | |
110 | |
13 | use strict qw(subs vars); |
111 | use strict qw(subs vars); |
14 | |
112 | |
|
|
113 | our %CACHE; |
|
|
114 | |
15 | sub activate($$$) { |
115 | sub activate($$$) { |
16 | ($self, $activator, $state) = @_; |
116 | ($self, $state, $activator, $originator) = @_; |
17 | |
|
|
18 | warn "$self->{msg} $self->{on_activate}\n";#d# |
|
|
19 | |
117 | |
20 | ( |
118 | ( |
21 | $self->{on_activate} ||= cf::safe_eval |
119 | $CACHE{$self->msg} ||= cf::safe_eval |
22 | "package mapscript; sub {\n" |
120 | "package mapscript; sub {\n" |
23 | . "#line 1 '" . ($self->debug_desc) . "'\n" |
121 | . "#line 1 '" . ($self->debug_desc) . "'\n" |
24 | . $self->msg |
122 | . $self->msg |
25 | . "\n}" |
123 | . "\n}" |
26 | or sub { } |
124 | or sub { } |
27 | )->(); |
125 | )->(); |
28 | } |
126 | } |
29 | |
127 | |
|
|
128 | =back |
|
|
129 | |
|
|
130 | =head1 EXAMPLE |
|
|
131 | |
|
|
132 | Have a look at F<scorn/anthonty/portgate.map> for a nontrivial example. |
|
|
133 | |
|
|
134 | =cut |
|
|
135 | |
30 | 1; |
136 | 1; |