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.1 by root, Thu Jan 8 03:03:24 2009 UTC vs.
Revision 1.8 by root, Sun Jan 10 19:21:47 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines