| 1 |
root |
1.1 |
#! perl |
| 2 |
|
|
|
| 3 |
root |
1.4 |
=head1 NAME |
| 4 |
|
|
|
| 5 |
|
|
cf::mapscript |
| 6 |
|
|
|
| 7 |
|
|
=head1 DESCRIPTION |
| 8 |
|
|
|
| 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 |
| 22 |
root |
1.1 |
|
| 23 |
|
|
package safe::mapscript; |
| 24 |
|
|
|
| 25 |
|
|
use strict qw(subs vars); |
| 26 |
|
|
|
| 27 |
root |
1.4 |
=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 |
|
|
|
| 47 |
root |
1.2 |
our ($self, $state, $activator, $originator); |
| 48 |
|
|
|
| 49 |
root |
1.4 |
=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 |
root |
1.2 |
sub find($) { |
| 57 |
|
|
ref $_[0] ? $_[0] |
| 58 |
|
|
: $self->map->find_link ($_[0]) |
| 59 |
|
|
} |
| 60 |
|
|
|
| 61 |
root |
1.4 |
=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 |
root |
1.2 |
|
| 69 |
root |
1.3 |
sub trigger($;$) { |
| 70 |
root |
1.2 |
$self->map->trigger ($_[0], $#_ ? $_[1] : 1, $self); |
| 71 |
|
|
} |
| 72 |
|
|
|
| 73 |
root |
1.4 |
=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 |
root |
1.2 |
sub timer($$) { |
| 82 |
|
|
my $ob = (&find)[0]; |
| 83 |
root |
1.5 |
$ob->speed_left (-$_[1] / cf::TICK); |
| 84 |
root |
1.2 |
$ob->set_speed (1); |
| 85 |
|
|
} |
| 86 |
root |
1.1 |
|
| 87 |
|
|
package cf::mapscript; |
| 88 |
|
|
|
| 89 |
|
|
use strict qw(subs vars); |
| 90 |
|
|
|
| 91 |
root |
1.3 |
our %CACHE; |
| 92 |
|
|
|
| 93 |
root |
1.1 |
sub activate($$$) { |
| 94 |
root |
1.2 |
($self, $state, $activator, $originator) = @_; |
| 95 |
root |
1.1 |
|
| 96 |
|
|
( |
| 97 |
root |
1.3 |
$CACHE{$self->msg} ||= cf::safe_eval |
| 98 |
root |
1.1 |
"package mapscript; sub {\n" |
| 99 |
|
|
. "#line 1 '" . ($self->debug_desc) . "'\n" |
| 100 |
|
|
. $self->msg |
| 101 |
|
|
. "\n}" |
| 102 |
|
|
or sub { } |
| 103 |
|
|
)->(); |
| 104 |
|
|
} |
| 105 |
|
|
|
| 106 |
root |
1.4 |
=back |
| 107 |
|
|
|
| 108 |
|
|
=head1 EXAMPLE |
| 109 |
|
|
|
| 110 |
|
|
Have a look at F<scorn/anthonty/portgate.map> for a nontrivial example. |
| 111 |
|
|
|
| 112 |
|
|
=cut |
| 113 |
|
|
|
| 114 |
root |
1.1 |
1; |