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.12 by root, Tue Nov 6 21:52:55 2012 UTC

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
5package safe::mapscript; 25cf::mapscript
6 26
7use strict qw(subs vars); 27=head1 DESCRIPTION
8 28
29This module implements the mapscript object.
30
31Map scripts are perl snippets that get executed whenever any connected
32element is triggered (e.g. a check inv, a lever etc.)
33
34=head1 ENVIRONMENT
35
36The map scripts are compiled and executed into a namespace with the
37following symbols available:
38
39=over 4
40
41=cut
42
43package cf::mapscript::eval;
44
45use common::sense;
46
47=item $self
48
49The mapscript object itself.
50
51=item $state
52
53The state value (0 means release, <>0 means push/trigger/enable) that
54triggered the map script.
55
56=item $activator
57
58The object that was triggered (the lever, check inv element, npc etc.).
59
60=item $originator
61
62The object that triggered the activator, usually (but not always) the
63player who stepped on a check inv, pulled a lever etc. Can be C<undef>.
64
65=cut
66
9our ($self, $state, $activator, $originator); 67use vars qw($self $state $activator $originator);
68
69=item @obs = find $id_or_object
70
71Finds all objects with the given I<connected> C<$id>. If an object
72reference is passed, it will be returned unchanged.
73
74=cut
10 75
11sub find($) { 76sub 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
16sub get($) { 81=item trigger $id_or_object[, $state]
17 (&find)[0]->value
18}
19 82
83Triggers the linked chain with the given I<connected> id, or the connected
84chain associated with the given object (if an object reference is passed),
85and passes the given state (or C<1>, if missing) to it.
86
87=cut
88
20sub set($;$) { 89sub 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
95Starts the timer on the given mapscript object (usually, C<$id_or_object> is
96C<$self>). When the timer expires on the mapscript object, it will trigger
97the script with C<$activator == $self> and C<$originator == undef>.
98
99=cut
100
24sub timer($$) { 101sub 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
30package cf::mapscript; 107package cf::mapscript;
31 108
32use strict qw(subs vars); 109use common::sense;
110
111*{"main::safe::cf::mapscript::eval::"} = \%{"main::cf::mapscript::eval::"};
112
113our %CACHE;
33 114
34sub activate($$$) { 115sub 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
134Have a look at F<scorn/anthonty/portgate.map> for a nontrivial example.
135
136=cut
137
491; 1381;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines