ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf/mapscript.pm
Revision: 1.7
Committed: Tue Nov 3 23:44:21 2009 UTC (14 years, 7 months ago) by root
Branch: MAIN
CVS Tags: rel-2_90, rel-2_92
Changes since 1.6: +22 -0 lines
Log Message:
tighten copyright statements for files containing no gpl code whatsoever anymore

File Contents

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