|
|
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 (©) 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 | # |
|
|
23 | |
1 | =head1 NAME |
24 | =head1 NAME |
2 | |
25 | |
3 | cf::match - object matching language |
26 | cf::match - object matching language |
4 | |
27 | |
5 | =head1 DESCRIPTION |
28 | =head1 DESCRIPTION |
… | |
… | |
12 | |
35 | |
13 | Match the object if it has a slaying field of C<key1>: |
36 | Match the object if it has a slaying field of C<key1>: |
14 | |
37 | |
15 | slaying = "key1" |
38 | slaying = "key1" |
16 | |
39 | |
17 | Match the object if it has an object with name C<force> and |
40 | Match the object if it has an object with name C<force> and |
18 | slaying C<poison> in it's inventory: |
41 | slaying C<poison> in it's inventory: |
19 | |
42 | |
20 | has (name = "force" and slaying = "poison") |
43 | has (name = "force" and slaying = "poison") |
21 | |
44 | |
22 | Find all inventory objects with value >= 10, which are not invisible: |
45 | Find all inventory objects with value >= 10, which are not invisible: |
… | |
… | |
92 | Not that C<not> only negates a condition and not the whole match |
115 | Not that C<not> only negates a condition and not the whole match |
93 | expressions, thus |
116 | expressions, thus |
94 | |
117 | |
95 | not applied in inv |
118 | not applied in inv |
96 | |
119 | |
97 | is true if there is I<any> non-object in the inventory. To negate a whole |
120 | is true if there is I<any> non-applied object in the inventory. To negate |
98 | match, you have to use a sub-match. To check whether there is I<no> |
121 | a whole match, you have to use a sub-match: To check whether there is |
99 | applied object in someones inventory, write this: |
122 | I<no> applied object in someones inventory, write this: |
100 | |
123 | |
101 | not (applied in inv) |
124 | not (applied in inv) |
102 | |
125 | |
103 | Example: match applied weapons. |
126 | Example: match applied weapons. |
104 | |
127 | |
105 | applied type=WEAPON |
128 | applied type=WEAPON |
106 | |
129 | |
107 | Example: match horns or rods. |
130 | Example: match horns or rods. |
108 | |
131 | |
109 | type=HORN or type=ROD |
132 | type=HORN or type=ROD |
|
|
133 | |
|
|
134 | Example: see if the originator is a player. |
|
|
135 | |
|
|
136 | type=PLAYER of originator |
110 | |
137 | |
111 | =item in ... |
138 | =item in ... |
112 | |
139 | |
113 | The in operator takes the context set and modifies it in various ways. As |
140 | The in operator takes the context set and modifies it in various ways. As |
114 | a less technical description, think of the C<in> as being a I<look into> |
141 | a less technical description, think of the C<in> as being a I<look into> |
… | |
… | |
203 | Starts with the default object - this is the object passed to the match to |
230 | Starts with the default object - this is the object passed to the match to |
204 | match against by default. Matches have an explicit C<of object> appended, |
231 | match against by default. Matches have an explicit C<of object> appended, |
205 | but submatches start at the current object, and in this case C<of object> |
232 | but submatches start at the current object, and in this case C<of object> |
206 | can be used to start at the original object once more. |
233 | can be used to start at the original object once more. |
207 | |
234 | |
|
|
235 | =item of self |
|
|
236 | |
|
|
237 | Starts with the object initiating/asking for the match - this is basically |
|
|
238 | always the object that the match expression is attached to. |
|
|
239 | |
208 | =item of source |
240 | =item of source |
209 | |
241 | |
210 | Starts with the I<source> object - this object is sometimes passed to |
242 | Starts with the I<source> object - this object is sometimes passed to |
211 | matches and represents the object that is the source of the action, such |
243 | matches and represents the object that is the source of the action, such |
212 | as a rod or a potion when it is applied. Often, the I<source> is the same |
244 | as a rod or a potion when it is applied. Often, the I<source> is the same |
… | |
… | |
219 | the original initiator of an action, most commonly a player or monster. |
251 | the original initiator of an action, most commonly a player or monster. |
220 | |
252 | |
221 | This object is often identical to the I<source> (e.g. when a player casts |
253 | This object is often identical to the I<source> (e.g. when a player casts |
222 | a spell, the player is both source and originator). |
254 | a spell, the player is both source and originator). |
223 | |
255 | |
224 | =item of self |
|
|
225 | |
|
|
226 | Starts with the object initiating/asking for the match - this is basically |
|
|
227 | always the object that the match expression is attached to. |
|
|
228 | |
|
|
229 | =back |
256 | =back |
230 | |
257 | |
231 | =head2 EXPRESSIONS |
258 | =head2 EXPRESSIONS |
232 | |
259 | |
233 | Expressions used in conditions usually consist of simple boolean checks |
260 | Expressions used in conditions usually consist of simple boolean checks |
… | |
… | |
242 | |
269 | |
243 | =item scalar object attributes |
270 | =item scalar object attributes |
244 | |
271 | |
245 | Object attributes that consist of a single value (C<name>, C<title>, |
272 | Object attributes that consist of a single value (C<name>, C<title>, |
246 | C<value> and so on) can be specified by simply using their name, in which |
273 | C<value> and so on) can be specified by simply using their name, in which |
247 | acse their corresponding value is used. |
274 | case their corresponding value is used. |
248 | |
275 | |
249 | =item array objects attributes |
276 | =item array objects attributes |
250 | |
277 | |
251 | The C<resist> array can be accessed by specifying C<< resist [ ATNR_type ] |
278 | The C<resist> array can be accessed by specifying C<< resist [ ATNR_type ] |
252 | >>. |
279 | >>. |
… | |
… | |
256 | resist[ATNR_ACID] > 30 |
283 | resist[ATNR_ACID] > 30 |
257 | |
284 | |
258 | =item functions |
285 | =item functions |
259 | |
286 | |
260 | Some additional functions with or without arguments in parentheses are |
287 | Some additional functions with or without arguments in parentheses are |
261 | available. |
288 | available. They are documented in their own section, below. |
262 | |
289 | |
263 | =item { BLOCK } |
290 | =item { BLOCK } |
264 | |
291 | |
265 | You can specify perl code to execute by putting it inside curly |
292 | You can specify perl code to execute by putting it inside curly |
266 | braces. The last expression evaluated inside will become the result. |
293 | braces. The last expression evaluated inside will become the result. |
… | |
… | |
305 | |
332 | |
306 | =item none |
333 | =item none |
307 | |
334 | |
308 | This simply evaluates to false, and simply makes matching I<never> a bit |
335 | This simply evaluates to false, and simply makes matching I<never> a bit |
309 | easier to read. |
336 | easier to read. |
|
|
337 | |
|
|
338 | =item archname |
|
|
339 | |
|
|
340 | The same as C<< { $_->arch->archname } >> - the archetype name is commonly |
|
|
341 | used to match items, so this shortcut is provided. |
|
|
342 | |
|
|
343 | =item resist_xxx |
|
|
344 | |
|
|
345 | Resistancy values such as C<resist_physical>, C<resist_magic>, |
|
|
346 | C<resists_fire> etc. are directly available (but can also be accessed via |
|
|
347 | array syntax, i.e. C<resists[ATNR_FIRE]>). |
|
|
348 | |
|
|
349 | =item body_xxx_info and body_xxx_used |
|
|
350 | |
|
|
351 | Every body location (e.g. C<body_neck_info>, C<body_arm_used> etc.) can |
|
|
352 | be accessed via these functions (these are aliases to more cumbersome C<< { |
|
|
353 | $_->slot_info (body_xxx) } >> and C<slot_used> method calls). |
|
|
354 | |
|
|
355 | Example: (e.g. on a door) match only players that have no arms. |
|
|
356 | |
|
|
357 | match type=PLAYER and body_arm_info=0 |
310 | |
358 | |
311 | =item has(condition) |
359 | =item has(condition) |
312 | |
360 | |
313 | True iff the object has a matching inventory object. |
361 | True iff the object has a matching inventory object. |
314 | |
362 | |
… | |
… | |
433 | 1 |
481 | 1 |
434 | }, |
482 | }, |
435 | none => sub { |
483 | none => sub { |
436 | 0 |
484 | 0 |
437 | }, |
485 | }, |
|
|
486 | archname => sub { |
|
|
487 | '$_->arch->archname' |
|
|
488 | }, |
438 | ); |
489 | ); |
|
|
490 | |
|
|
491 | # resist_xxx |
|
|
492 | for my $atnr (0 .. cf::NROFATTACKS - 1) { |
|
|
493 | $special{"resist_" . cf::attacktype_name ($atnr)} = sub { "\$_->resist ($atnr)" }; |
|
|
494 | } |
|
|
495 | |
|
|
496 | # body_xxx_info and _used |
|
|
497 | for my $slot (0 .. cf::NUM_BODY_LOCATIONS - 1) { |
|
|
498 | my $name = cf::object::slot_name $slot; |
|
|
499 | |
|
|
500 | $special{"body_$name\_info"} = sub { "\$_->slot_info ($slot)" }; |
|
|
501 | $special{"body_$name\_used"} = sub { "\$_->slot_used ($slot)" }; |
|
|
502 | } |
439 | |
503 | |
440 | sub constant { |
504 | sub constant { |
441 | ws; |
505 | ws; |
442 | |
506 | |
443 | return $1 if /\G([\-\+0-9\.]+)/gc; |
507 | return $1 if /\G([\-\+0-9\.]+)/gc; |
… | |
… | |
688 | |
752 | |
689 | $res |
753 | $res |
690 | } |
754 | } |
691 | |
755 | |
692 | if (0) {#d# |
756 | if (0) {#d# |
693 | die parse 1, 'stats.pow'; |
757 | die parse 1, 'type=PLAYER and body_arm_info=0'; |
694 | exit 0; |
758 | exit 0; |
695 | } |
759 | } |
696 | |
760 | |
697 | our %CACHE; |
761 | our %CACHE; |
698 | |
762 | |
699 | sub compile($$) { |
763 | sub compile($$) { |
700 | my ($wantarray, $match) = @_; |
764 | my ($wantarray, $match) = @_; |
701 | my $expr = parse $wantarray, $match; |
765 | my $expr = parse $wantarray, $match; |
702 | warn "MATCH DEBUG $match,$wantarray => $expr\n";#d# |
766 | # warn "MATCH DEBUG $match,$wantarray => $expr\n";#d# |
703 | $expr = eval " |
767 | $expr = eval " |
704 | package cf::match::exec; |
768 | package cf::match::exec; |
705 | sub { |
769 | sub { |
706 | my (\$object, \$self, \$source, \$originator) = \@_; |
770 | my (\$object, \$self, \$source, \$originator) = \@_; |
707 | $expr |
771 | $expr |