1 | #! perl |
1 | #! perl |
2 | |
2 | |
|
|
3 | =head1 NAME |
|
|
4 | |
|
|
5 | cf::mapscript |
|
|
6 | |
|
|
7 | =head1 DESCRIPTION |
|
|
8 | |
3 | # this implements the mapscript connectable |
9 | This module implements the mapscript object. |
|
|
10 | |
|
|
11 | Map scripts are perl snippets that get executed whenever any connected |
|
|
12 | element is triggered (e.g. a check inv, a lever etc.) |
|
|
13 | |
|
|
14 | =head1 ENVIRONMENT |
|
|
15 | |
|
|
16 | The map scripts are compiled and executed into a namespace with the |
|
|
17 | following symbols available: |
|
|
18 | |
|
|
19 | =over 4 |
|
|
20 | |
|
|
21 | =cut |
4 | |
22 | |
5 | package safe::mapscript; |
23 | package safe::mapscript; |
6 | |
24 | |
7 | use strict qw(subs vars); |
25 | use common::sense; |
8 | |
26 | |
|
|
27 | =item $self |
|
|
28 | |
|
|
29 | The mapscript object itself. |
|
|
30 | |
|
|
31 | =item $state |
|
|
32 | |
|
|
33 | The state value (0 means release, <>0 means push/trigger/enable) that |
|
|
34 | triggered the map script. |
|
|
35 | |
|
|
36 | =item $activator |
|
|
37 | |
|
|
38 | The object that was triggered (the lever, check inv element, npc etc.). |
|
|
39 | |
|
|
40 | =item $originator |
|
|
41 | |
|
|
42 | The object that triggered the activator, usually (but not always) the |
|
|
43 | player who stepped on a check inv, pulled a lever etc. Can be C<undef>. |
|
|
44 | |
|
|
45 | =cut |
|
|
46 | |
9 | our ($self, $activator, $state); |
47 | our ($self, $state, $activator, $originator); |
|
|
48 | |
|
|
49 | =item @obs = find $id_or_object |
|
|
50 | |
|
|
51 | Finds all objects with the given I<connected> C<$id>. If an object |
|
|
52 | reference is passed, it will be returned unchanged. |
|
|
53 | |
|
|
54 | =cut |
|
|
55 | |
|
|
56 | sub find($) { |
|
|
57 | ref $_[0] ? $_[0] |
|
|
58 | : $self->map->find_link ($_[0]) |
|
|
59 | } |
|
|
60 | |
|
|
61 | =item trigger $id_or_object[, $state] |
|
|
62 | |
|
|
63 | Triggers the linked chain with the given I<connected> id, or the connected |
|
|
64 | chain associated with the given object (if an objetc reference is passed), |
|
|
65 | and passes the given state (or C<1>, if missing) to it. |
|
|
66 | |
|
|
67 | =cut |
|
|
68 | |
|
|
69 | sub trigger($;$) { |
|
|
70 | $self->map->trigger ($_[0], $#_ ? $_[1] : 1, $self); |
|
|
71 | } |
|
|
72 | |
|
|
73 | =item timer $id_or_object, $seconds |
|
|
74 | |
|
|
75 | Starts the timer on the given mapscript object (usually, $id_or_object is |
|
|
76 | C<$self>). When the timer expires on the mapscript object, it will trigger |
|
|
77 | the script with C<$activator == $self> and C<$originator == undef>. |
|
|
78 | |
|
|
79 | =cut |
|
|
80 | |
|
|
81 | sub timer($$) { |
|
|
82 | my $ob = (&find)[0]; |
|
|
83 | $ob->speed_left (-$_[1] / cf::TICK); |
|
|
84 | $ob->set_speed (1); |
|
|
85 | } |
10 | |
86 | |
11 | package cf::mapscript; |
87 | package cf::mapscript; |
12 | |
88 | |
13 | use strict qw(subs vars); |
89 | use strict qw(subs vars); |
14 | |
90 | |
|
|
91 | our %CACHE; |
|
|
92 | |
15 | sub activate($$$) { |
93 | sub activate($$$) { |
16 | ($self, $activator, $state) = @_; |
94 | ($self, $state, $activator, $originator) = @_; |
17 | |
|
|
18 | warn "$self->{msg} $self->{on_activate}\n";#d# |
|
|
19 | |
95 | |
20 | ( |
96 | ( |
21 | $self->{on_activate} ||= cf::safe_eval |
97 | $CACHE{$self->msg} ||= cf::safe_eval |
22 | "package mapscript; sub {\n" |
98 | "package mapscript; sub {\n" |
23 | . "#line 1 '" . ($self->debug_desc) . "'\n" |
99 | . "#line 1 '" . ($self->debug_desc) . "'\n" |
24 | . $self->msg |
100 | . $self->msg |
25 | . "\n}" |
101 | . "\n}" |
26 | or sub { } |
102 | or sub { } |
27 | )->(); |
103 | )->(); |
28 | } |
104 | } |
29 | |
105 | |
|
|
106 | =back |
|
|
107 | |
|
|
108 | =head1 EXAMPLE |
|
|
109 | |
|
|
110 | Have a look at F<scorn/anthonty/portgate.map> for a nontrivial example. |
|
|
111 | |
|
|
112 | =cut |
|
|
113 | |
30 | 1; |
114 | 1; |