ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf/mapscript.pm
(Generate patch)

Comparing deliantra/server/lib/cf/mapscript.pm (file contents):
Revision 1.2 by root, Thu Jan 8 04:35:04 2009 UTC vs.
Revision 1.13 by root, Sat Nov 17 23:40:02 2018 UTC

1#! perl 1#
2# This file is part of Deliantra, the Roguelike Realtime MMORPG.
3#
4# Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5# Copyright (©) 2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6#
7# Deliantra is free software: you can redistribute it and/or modify it under
8# the terms of the Affero GNU General Public License as published by the
9# Free Software Foundation, either version 3 of the License, or (at your
10# option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the Affero GNU General Public License
18# and the GNU General Public License along with this program. If not, see
19# <http://www.gnu.org/licenses/>.
20#
21# The authors can be reached via e-mail to <support@deliantra.net>
22#
2 23
3# this implements the mapscript connectable 24=head1 NAME
4 25
5package safe::mapscript; 26cf::mapscript
6 27
7use strict qw(subs vars); 28=head1 DESCRIPTION
8 29
30This module implements the mapscript object.
31
32Map scripts are perl snippets that get executed whenever any connected
33element is triggered (e.g. a check inv, a lever etc.)
34
35=head1 ENVIRONMENT
36
37The map scripts are compiled and executed into a namespace with the
38following symbols available:
39
40=over 4
41
42=cut
43
44package cf::mapscript::eval;
45
46use common::sense;
47
48=item $self
49
50The mapscript object itself.
51
52=item $state
53
54The state value (0 means release, <>0 means push/trigger/enable) that
55triggered the map script.
56
57=item $activator
58
59The object that was triggered (the lever, check inv element, npc etc.).
60
61=item $originator
62
63The object that triggered the activator, usually (but not always) the
64player who stepped on a check inv, pulled a lever etc. Can be C<undef>.
65
66=cut
67
9our ($self, $state, $activator, $originator); 68use vars qw($self $state $activator $originator);
69
70=item @obs = find $id_or_object
71
72Finds all objects with the given I<connected> C<$id>. If an object
73reference is passed, it will be returned unchanged.
74
75=cut
10 76
11sub find($) { 77sub find($) {
12 ref $_[0] ? $_[0] 78 ref $_[0] ? $_[0]
13 : $self->map->find_link ($_[0]) 79 : $self->map->find_link ($_[0])
14} 80}
15 81
16sub get($) { 82=item trigger $id_or_object[, $state]
17 (&find)[0]->value
18}
19 83
84Triggers the linked chain with the given I<connected> id, or the connected
85chain associated with the given object (if an object reference is passed),
86and passes the given state (or C<1>, if missing) to it.
87
88=cut
89
20sub set($;$) { 90sub trigger($;$) {
21 $self->map->trigger ($_[0], $#_ ? $_[1] : 1, $self); 91 $self->map->trigger ($_[0], $#_ ? $_[1] : 1, $self);
22} 92}
23 93
94=item timer $id_or_object, $seconds
95
96Starts the timer on the given mapscript object (usually, C<$id_or_object> is
97C<$self>). When the timer expires on the mapscript object, it will trigger
98the script with C<$activator == $self> and C<$originator == undef>.
99
100=cut
101
24sub timer($$) { 102sub timer($$) {
25 my $ob = (&find)[0]; 103 my $ob = (&find)[0];
26 $ob->speed_left ($_[1] / -cf::TICK); 104 $ob->speed_left (-$_[1] / cf::TICK);
27 $ob->set_speed (1); 105 $ob->set_speed (1);
28} 106}
29 107
30package cf::mapscript; 108package cf::mapscript;
31 109
32use strict qw(subs vars); 110use common::sense;
111
112*{"main::safe::cf::mapscript::eval::"} = \%{"main::cf::mapscript::eval::"};
113
114our %CACHE;
33 115
34sub activate($$$) { 116sub activate($$$) {
117 package cf::mapscript::eval;
118
35 ($self, $state, $activator, $originator) = @_; 119 ($self, $state, $activator, $originator) = @_;
36 120
37 warn "$self->{msg} $self->{on_activate}\n";#d#
38
39 ( 121 (
40 $self->{on_activate} ||= cf::safe_eval 122 $CACHE{$self->msg} ||= cf::safe_eval
41 "package mapscript; sub {\n" 123 "package cf::mapscript::eval; sub {\n"
42 . "#line 1 '" . ($self->debug_desc) . "'\n" 124 . "#line 1 '" . ($self->debug_desc) . "'\n"
43 . $self->msg 125 . $self->msg
44 . "\n}" 126 . "\n}"
45 or sub { } 127 or sub { }
46 )->(); 128 )->();
47} 129}
48 130
131=back
132
133=head1 EXAMPLE
134
135Have a look at F<scorn/anthonty/portgate.map> for a nontrivial example.
136
137=cut
138
491; 1391;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines