blib/lib/Games/Axmud/Buffer.pm untested

filter: Statementstmt 0.0% 0 / 335 Branchbran 0.0% 0 / 106 Conditioncond 0.0% 0 / 186 MC/DCmcdc 0.0% 0 / 97 Subroutinesub 0.0% 0 / 64 Podpod 0.0% 0 / 55 totaltotal 0.0% 0 / 843 CC 54 SCAR 48.3
CC
54
Cov
0%
interpretCmd
552.0
new
90.0
copyLine
72.0
CRAP 125.8
1 # Copyright (C) 2011-2024 A S Lewis
2 #
3 # This program is free software: you can redistribute it and/or modify it under the terms of the GNU
4 # General Public License as published by the Free Software Foundation, either version 3 of the
5 # License, or (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
8 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License along with this program. If not,
12 # see <http://www.gnu.org/licenses/>.
13 #
14 #
15 # Games::Axmud::Buffer::XXX
16 # Display, instruction and world command buffer objects
17  
18 { package Games::Axmud::Buffer::Display;
19  
20 use strict;
21 use warnings;
22 # use diagnostics;
23  
24 use Glib qw(TRUE FALSE);
25  
26 our @ISA = qw(Games::Axmud);
27  
28 ##################
29 # Constructors
30  
31 sub new {
32  
33 # Called by GA::Session->updateDisplayBuffer
34 # The display buffer stores lines of text received from the world and displayed in the
35 # session's default textview object
36 #
37 # Expected arguments
38 # $session - The calling GA::Session
39 # $parent - Which registry stores this object - always 'session' for this type of
40 # buffer object
41 # $number - A number for this buffer object unique to the parent's registry
42 # $line - The original line of text received from the world
43 # $stripLine - $line after being stripped of escape sequences
44 # $modLine - $stripLine after being modified by any matching interfaces (identical
45 # to $stripLine if none match)
46 # $time - The time at which the line was received, in seconds (matches
47 # GA::Session->sessionTime)
48 # $newLineFlag - Flag set to TRUE if the line ends with a newline character; FALSE if
49 # not
50 #
51 # Optional arguments
52 # $offsetHashRef - Hash of Axmud colour/style tags in the form
53 # $offsetHash{$offset} = reference_to_list_of_Axmud_colour_style_tags
54 # - Each offset represents the position of a character in $modLine,
55 # immediately before which the equivalent escape sequence was received
56 # (see the comments in GA::Session->updateDisplayBuffer)
57 # - If 'undef', it is treated the same as an empty hash reference
58 # $tagHashRef - Hash of Axmud colour/style tags in the form
59 # $tagHash{tag} = reference_to_list_of_offsets_where_tags_occur
60 # - If 'undef', it is treated the same as an empty hash reference
61 # $initialListRef - Reference to a list of Axmud colour/style tags that actually applied
62 # at the beginning of the line (may be an empty list)
63 # - If 'undef', it is treated the same as an empty list reference
64 # $mxpFlagHashRef - Hash of text appearing between matching custom elements which define
65 # tag properties. Hash in the form
66 # $mxpFlagHash{tag_property} = string
67 # - If 'undef', it is treated the same way as an empty hash reference
68 #
69 # Return values
70 # 'undef' on improper arguments
71 # Blessed reference to the newly-created object on success
72  
73 my (
74 $class, $session, $parent, $number, $line, $stripLine, $modLine, $time, $newLineFlag,
75 $offsetHashRef, $tagHashRef, $initialListRef, $mxpFlagHashRef, $check,
76 ) = @_;
77  
78 # Local variables
79 my ($emptyFlag, $prevBufferObj);
80  
81 # Check for improper arguments
82 if (
83 ! defined $class || ! defined $session || ! defined $parent || ! defined $number
84 || ! defined $line || ! defined $stripLine || ! defined $modLine || ! defined $time
85 || ! defined $newLineFlag || defined $check
86 ) {
87 return $axmud::CLIENT->writeImproper($class . '->new', @_);
88 }
89  
90 # Work out whether this counts as an empty line, or not
91 if ($modLine =~ m/^\s*[\n\r]*$/) {
92 $emptyFlag = TRUE;
93 } else {
94 $emptyFlag = FALSE;
95 }
96  
97 # Use empty hash/list references, if none specified
98 if (! defined $offsetHashRef) {
99  
100 $offsetHashRef = {};
101 }
102  
103 if (! defined $tagHashRef) {
104  
105 $tagHashRef = {};
106 }
107  
108 if (! defined $initialListRef) {
109  
110 $initialListRef = [];
111 }
112  
113 if (! defined $mxpFlagHashRef) {
114  
115 $mxpFlagHashRef = {};
116 }
117  
118 # Setup
119 my $self = {
120 _objName => 'display_buff_' . $number,
121 _objClass => $class,
122 _parentFile => undef, # No parent file object
123 _parentWorld => undef, # No parent file object
124 _privFlag => TRUE, # All IVs are private
125  
126 # The calling GA::Session
127 session => $session,
128 # Which registry stores this object - always 'session' for this type of buffer object
129 parent => $parent,
130 # A number for this buffer object unique to the parent's registry
131 number => $number,
132  
133 # The original line of text received from the world
134 line => $line,
135 # The line after being stripped of escape sequences
136 stripLine => $stripLine,
137 # The stripped line after (possibly) being modified by interfaces
138 modLine => $modLine,
139 # A list containing parts of the line. For lines that are received in a single packet,
140 # will contain one item (the same as $line); for lines that are received over two or
141 # more packets, will contain two or more items
142 partList => [$line],
143 # The time at which the line was received, in seconds (matches
144 # GA::Session->sessionTime. If parts of the line were received at different times, the
145 # time at which the first part was received)
146 time => $time,
147 # Flag set to TRUE if the line ends with a newline character; FALSE if not. When FALSE,
148 # text sent to the world is added to this object until the first newline character is
149 # sent (using the $self->update method)
150 newLineFlag => $newLineFlag,
151 # Flag set to TRUE if $modLine contains no characters (besides final newline
152 # characters) or just empty space (besides final newline characters); used for empty
153 # line suppression
154 emptyFlag => $emptyFlag,
155  
156 # Hash of Axmud colour/style tags in the form
157 # $offsetHash{$offset} = reference_to_list_of_Axmud_colour_&_style_tags
158 # NB Create a new hash reference because we can't assume the original one will remain
159 # unaltered
160 offsetHash => {%$offsetHashRef},
161 # Hash of Axmud colour/style tags in the form
162 # ->tagHash{tag} = reference_to_list_of_offsets_where_tags_occur
163 tagHash => {%$tagHashRef},
164 # Hash of 'mxpm_mode' tags that appear anywhere on this line, in the form
165 # $mxpModeHash{mode} = undef
166 # ...where 'mode' is a number in the range 10-12, 19, 20-99
167 mxpModeHash => {},
168  
169 # List of Axmud colour/style tags that were applied to the first character of this line,
170 # when it was displayed in the 'main' window
171 # e.g. if the previous line used a 'blue' tag, but no 'attribs_off' after it, and if
172 # this line has no tags at all at offset 0, then the 'blue' tag still applies, and it
173 # exists in this list
174 # e.g. if this line has a 'red' tag at offset 0, then that tag exists in this list,
175 # rather than the 'blue' tag that applied at the end of the previous line
176 initialTagList => [@$initialListRef],
177 # List of Axmud colour/style tags that applied at the end of this line (i.e. the tags
178 # that will apply to the beginning of the next line, BEFORE any of the next line's
179 # tags are processed)
180 finalTagList => [], # Set below
181 # List of Axmud colour/style tags that applied at the end of the previous line (copied
182 # from the buffer object's ->finalTagList from the previous line)
183 previousTagList => [], # Set below
184  
185 # MXP custom elements can define tag properties, e.g. from the MXP spec,
186 # <RName>...</RName>
187 # If a closing tag has been processed on this line, and if the element defines tag
188 # properties, the text between the matching tags is stored in this buffer object
189 # (If the text between the two matching tags contained newline characters, they have
190 # been removed)
191 # Hash in the form
192 # $mxpFlagHash{tag_property} = text
193 mxpFlagTextHash => {%$mxpFlagHashRef},
194 };
195  
196 # Bless the object into existence
197 bless $self, $class;
198  
199 # If we've got a complete line, we can set the final tag list (otherwise, we must wait until
200 # the call to $self->update)
201 if ($newLineFlag) {
202  
203 $self->{finalTagList} = [$session->currentTabObj->textViewObj->listColourStyleTags()];
204 }
205  
206 # If this isn't the first buffer line, we can also set the previous tag list
207 $prevBufferObj = $session->ivShow('displayBufferHash', ($number - 1));
208 if ($prevBufferObj) {
209  
210 $self->{previousTagList} = [$prevBufferObj->finalTagList];
211 }
212  
213 # Set the contents of $self->mxpModeHash, using any 'mxpm_mode' tags found in ->offsetHash
214 $self->updateModes();
215  
216 return $self;
217 }
218  
219 ##################
220 # Methods
221  
222 sub update {
223  
224 # Called by GA::Session->updateDisplayBuffer when this object stores the most
225 # recently-displayed line of text from the world, and that line hasn't yet been terminated
226 # with a newline character
227 # Updates the stored IVs
228 #
229 # Expected arguments
230 # $line - The original line of text received from the world
231 # $stripLine - $line after being stripped of escape sequences
232 # $modLine - $stripLine after being modified by any matching interfaces (identical
233 # to $stripLine if none match)
234 # $newLineFlag - Flag set to TRUE if the line ends with a newline character; FALSE if
235 # not
236 # $offsetHashRef - Hash of Axmud colour/style tags in the form
237 # $offsetHash{$offset} = reference_to_list_of_Axmud_colour_style_tags
238 # - Each offset represents the position of a character in $modLine,
239 # immediately before which the equivalent escape sequence was
240 # received (see the comments in GA::Session->updateDisplayBuffer)
241 # $tagHashRef - Hash of Axmud colour/style tags in the form
242 # $tagHash{tag} = reference_to_list_of_offsets_where_tags_occur
243 # $mxpFlagHashRef - Hash of text appearing between matching custom elements which define
244 # tag properties in the form
245 # $mxpFlagHash{tag_property} = string
246 #
247 # Return values
248 # 'undef' on improper arguments
249 # 1 on success
250  
251 my (
252 $self, $line, $stripLine, $modLine, $newLineFlag, $offsetHashRef, $tagHashRef,
253 $mxpFlagHashRef,
254 $check,
255 ) = @_;
256  
257 # Local variables
258 my (
259 $size,
260 %newTagHash, %newOffsetHash,
261 );
262  
263 # Check for improper arguments
264 if (
265 ! defined $line || ! defined $stripLine || ! defined $modLine || ! defined $newLineFlag
266 || ! defined $offsetHashRef || ! defined $tagHashRef || defined $check
267 ) {
268 return $axmud::CLIENT->writeImproper($self->_objClass . '->update', @_);
269 }
270  
271 # The contents of $tagHashRef must be merged with the existing contents of $self->tagHash
272 # At the same time, we must modify all the offsets in $tagHashRef; e.g. if the tag 'red'
273 # has the list of offsets [0, 4, 9], and if $self->modLine already contains 50
274 # characters, the offsets should be stored as [50, 54, 59]
275  
276 # Get the existing size of $self->modLine
277 $size = length ($self->modLine);
278  
279 # Import the current tag hash for speed...
280 %newTagHash = $self->tagHash;
281 # ...and then update it with the new data
282 foreach my $tag (keys %$tagHashRef) {
283  
284 my (
285 $offsetListRef, $listRef,
286 @newOffsetList,
287 );
288  
289 # Convert a list like [0, 4, 9] to a list like [50, 54, 59]
290 $offsetListRef = $$tagHashRef{$tag};
291 foreach my $offset (@$offsetListRef) {
292  
293 push (@newOffsetList, ($offset + $size));
294 }
295  
296 # Update the existing hash of tags
297 if (exists $newTagHash{$tag}) {
298  
299 $listRef = $newTagHash{$tag};
300 push (@$listRef, @newOffsetList);
301  
302 } else {
303  
304 $newTagHash{$tag} = \@newOffsetList;
305 }
306 }
307  
308 # Do the same with $offsetHashRef
309  
310 # Import the current offset hash for speed...
311 %newOffsetHash = $self->offsetHash;
312 # ...and then update it with the new data
313 foreach my $offset (keys %$offsetHashRef) {
314  
315 $newOffsetHash{$offset + $size} = $$offsetHashRef{$offset};
316 }
317  
318 # MXP tag properties for custom elements are rarer, so we don't need to import anything for
319 # speed
320 foreach my $key (keys %$mxpFlagHashRef) {
321  
322 my $value = $$mxpFlagHashRef{$key};
323  
324 # (Guard against possibly that two sets of tags, e.g. <RName>...</RName>, appear on the
325 # same line
326 if (! $self->ivExists('mxpFlagTextHash', $key)) {
327  
328 $self->ivAdd('mxpFlagTextHash', $key, $value);
329  
330 } else {
331  
332 $self->ivAdd(
333 'mxpFlagTextHash',
334 $key,
335 $self->ivShow('mxpFlagTextHash', $key) . $value,
336 );
337 }
338 }
339  
340 # Update IVs. If $line is an empty string, it's because a packet beginning with a newline
341 # character was processed (otherwise, $line is a string whose initial portion matches the
342 # contents of $self->line)
343 if (length ($line) > length ($self->line)) {
344  
345 $self->ivPush('partList', substr($line, length($self->line)));
346 }
347  
348 $self->ivPoke('line', $self->line . $line);
349 $self->ivPoke('stripLine', $self->stripLine . $stripLine);
350 $self->ivPoke('modLine', $self->modLine . $modLine);
351 $self->ivPoke('newLineFlag', $newLineFlag);
352  
353 if ($self->modLine =~ m/^\s*[\n\r]*$/) {
354 $self->ivPoke('emptyFlag', TRUE);
355 } else {
356 $self->ivPoke('emptyFlag', FALSE);
357 }
358  
359 $self->ivPoke('offsetHash', %newOffsetHash);
360 $self->ivPoke('tagHash', %newTagHash);
361  
362 if ($newLineFlag) {
363  
364 $self->ivPoke(
365 'finalTagList',
366 $self->session->currentTabObj->textViewObj->listColourStyleTags(),
367 );
368 }
369  
370 # Update the ->mxpModeHash IV
371 $self->updateModes();
372  
373 return 1;
374 }
375  
376 sub updateModes {
377  
378 # Called by $self->new and $self->update
379 # $self->mxpModeHash stores all the MXP modes (but only those in the range 10-12, 19, 20-99)
380 # that apply to this line
381 # Update the hash
382 #
383 # Expected arguments
384 # (none besides $self)
385 #
386 # Return values
387 # 'undef' on improper arguments
388 # 1 on success
389  
390 my ($self, $check) = @_;
391  
392 # Local variables
393 my (%offsetHash, %modeHash);
394  
395 # Check for improper arguments
396 if (defined $check) {
397  
398 return $axmud::CLIENT->writeImproper($self->_objClass . '->updateModes', @_);
399 }
400  
401 # Import IVs (for convenience)
402 %offsetHash = $self->offsetHash;
403 %modeHash = $self->mxpModeHash;
404  
405 foreach my $offset (keys %offsetHash) {
406  
407 my ($listRef, $mode);
408  
409 $listRef = $offsetHash{$offset};
410  
411 foreach my $tag (@$listRef) {
412  
413 if (substr($tag, 0, 5) eq 'mxpm_') {
414  
415 $mode = substr($tag, 5);
416 $modeHash{$mode} = undef;
417 }
418 }
419 }
420  
421 # Update the IV
422 $self->ivPoke('mxpModeHash', %modeHash);
423  
424 return 1;
425 }
426  
427 sub copyLine {
428  
429 # Can be called by anything (for example, called by the Channels task)
430 # Copies the contents of the line stored in this object into another textview, preserving
431 # the colour and style of the original line
432 # Normal links (e.g. a line containing 'http://mywebsite.com') are clickable, but MXP links
433 # and so on are not
434 #
435 # Expected arguments
436 # $textViewObj - The textview object (GA::Obj::TextView) into which the line is copied
437 #
438 # Return values
439 # 'undef' on improper arguments
440 # 1 on success
441  
442 my ($self, $textViewObj, $check) = @_;
443  
444 # Local variables
445 my (
446 $lastOffset, $lastNewline,
447 @previousTagList, @lastList,
448 %origHash, %modHash,
449 );
450  
451 # Check for improper arguments
452 if (! defined $textViewObj || defined $check) {
453  
454 return $axmud::CLIENT->writeImproper($self->_objClass . '->copyLine', @_);
455 }
456  
457 # Import the textview object's hash of styles and colours that apply at the current text
458 # insertion position, so it can be restored when this function has finished
459 %modHash = %origHash = $textViewObj->colourStyleHash;
460  
461 # The colour/style tags that applied at the beginning of the original line must be applied
462 # to that hash
463 @previousTagList = $self->previousTagList;
464 %modHash = $textViewObj->applyColourStyleTags(
465 $self->session,
466 \@previousTagList,
467 %modHash,
468 );
469  
470 $textViewObj->set_colourStyleHash(%modHash);
471  
472 # Split the original line into portions, and insert each portion (along with the colour/
473 # style tags that apply to that portion) into the textview
474 foreach my $offset (sort {$a <=> $b} ($self->ivKeys('offsetHash'))) {
475  
476 my ($listRef, $newline, $portion);
477  
478 # Get a list of colour/style tags that apply to this offset
479 $listRef = $self->ivShow('offsetHash', $offset);
480  
481 # Decide where the newline character should be put
482 if ($textViewObj->newLineDefault eq 'before') {
483  
484 if ($offset == 0) {
485 $newline = 'before';
486 } else {
487 $newline = 'echo';
488 }
489  
490 } elsif (
491 $textViewObj->newLineDefault eq 'after'
492 || $textViewObj->newLineDefault eq 'nl'
493 ) {
494 if ($offset == 0) {
495 $newline = 'echo';
496 } else {
497 $newline = 'after';
498 }
499 }
500  
501 # Now we know where the previous portion of text ends, so we can display it
502 if (defined $lastOffset) {
503  
504 $portion = substr($self->stripLine, $lastOffset, ($offset - $lastOffset));
505  
506 %modHash = $textViewObj->colourStyleHash;
507 %modHash = $textViewObj->applyColourStyleTags(
508 $self->session,
509 \@lastList,
510 %modHash,
511 );
512  
513 $textViewObj->set_colourStyleHash(%modHash);
514  
515 $textViewObj->insertText(
516 $portion,
517 $lastNewline,
518 $textViewObj->listColourStyleTags(),
519 );
520 }
521  
522 # This portion is displayed in the next iteration of the loop, so we know where the
523 # portion ends
524 $lastOffset = $offset;
525 $lastNewline = $newline;
526 @lastList = @$listRef;
527 }
528  
529 # Display the final portion
530 if (defined $lastOffset) {
531  
532 %modHash = $textViewObj->colourStyleHash;
533  
534 %modHash = $textViewObj->applyColourStyleTags(
535 $self->session,
536 \@lastList,
537 %modHash,
538 );
539  
540 $textViewObj->set_colourStyleHash(%modHash);
541  
542 $textViewObj->insertText(
543 substr($self->stripLine, $lastOffset),
544 $lastNewline,
545 $textViewObj->listColourStyleTags(),
546 );
547 }
548  
549 # Restore the textview's original colour/style hash, restoring it to its previous state
550 $textViewObj->set_colourStyleHash(%origHash);
551  
552 # Operation complete
553 return 1;
554 }
555  
556 ##################
557 # Accessors - set
558  
559 ##################
560 # Accessors - get
561  
562 sub session
563 { $_[0]->{session} }
564 sub parent
565 { $_[0]->{parent} }
566 sub number
567 { $_[0]->{number} }
568  
569 sub line
570 { $_[0]->{line} }
571 sub stripLine
572 { $_[0]->{stripLine} }
573 sub modLine
574 { $_[0]->{modLine} }
575 sub partList
576 { my $self = shift; return @{$self->{partList}}; }
577 sub time
578 { $_[0]->{time} }
579 sub newLineFlag
580 { $_[0]->{newLineFlag} }
581 sub emptyFlag
582 { $_[0]->{emptyFlag} }
583  
584 sub offsetHash
585 { my $self = shift; return %{$self->{offsetHash}}; }
586 sub tagHash
587 { my $self = shift; return %{$self->{tagHash}}; }
588 sub mxpModeHash
589 { my $self = shift; return %{$self->{mxpModeHash}}; }
590  
591 sub initialTagList
592 { my $self = shift; return @{$self->{initialTagList}}; }
593 sub finalTagList
594 { my $self = shift; return @{$self->{finalTagList}}; }
595 sub previousTagList
596 { my $self = shift; return @{$self->{previousTagList}}; }
597  
598 sub mxpFlagTextHash
599 { my $self = shift; return %{$self->{mxpFlagTextHash}}; }
600 }
601  
602 { package Games::Axmud::Buffer::Instruct;
603  
604 use strict;
605 use warnings;
606 # use diagnostics;
607  
608 use Glib qw(TRUE FALSE);
609  
610 our @ISA = qw(Games::Axmud);
611  
612 ##################
613 # Constructors
614  
615 sub new {
616  
617 # Called by GA::Client->updateInstructBuffer or GA::Session->updateInstructBuffer
618 # The instruction buffer object stores instructions processed by a session, including
619 # world/forced world/client commands/echo/Perl/script/multi commands
620 # In addition, if the user types 'north;kill troll;eat corpse', that chain of world
621 # commands is stored as a single GA::Buffer::Instruct (and also as three separate
622 # GA::Buffer::Cmd objects in the world command buffer)
623 #
624 # Expected arguments
625 # $session - The calling GA::Session
626 # $parent - Which registry stores this object - 'client' if stored in
627 # GA::Client->instructBufferHash, 'session' if stored in
628 # GA::Session->instructBufferHash
629 # $number - A number for this buffer object unique to the parent's registry
630 # $instruct - The instruction itself, e.g. ';setworld deathmud', 'north;kill orc'
631 # $type - The type of instruction: 'client' for a client command, 'world' for a
632 # world command, 'perl' for a Perl command and 'echo' for an echo
633 # command
634 # $time - The time at which the instruction was received, in seconds (matches
635 # GA::Client->clientTime or GA::Session->sessionTime)
636 #
637 # Return values
638 # 'undef' on improper arguments
639 # Blessed reference to the newly-created object on success
640  
641 my ($class, $session, $parent, $number, $instruct, $type, $time, $check) = @_;
642  
643 # Check for improper arguments
644 if (
645 ! defined $class || ! defined $session || ! defined $parent || ! defined $number
646 || ! defined $instruct || ! defined $type || defined $check
647 ) {
648 return $axmud::CLIENT->writeImproper($class . '->new', @_);
649 }
650  
651 # Setup
652 my $self = {
653 _objName => 'instruct_buff_' . $number,
654 _objClass => $class,
655 _parentFile => undef, # No parent file object
656 _parentWorld => undef, # No parent file object
657 _privFlag => TRUE, # All IVs are private
658  
659 # The calling GA::Session
660 session => $session,
661 # Which registry stores this object - 'client' if stored in
662 # GA::Client->instructBufferHash, 'session' if stored in
663 # GA::Session->instructBufferHash
664 parent => $parent,
665 # A number for this buffer object unique to the parent's registry
666 number => $number,
667  
668 # The instruction itself, e.g. ';setworld deathmud', 'north;kill orc'
669 instruct => $instruct,
670 # The type of instruction: 'client' for a client command, 'world' for a world command
671 # or a forced world command, 'echo' for an echo command, 'perl' for a Perl command,
672 # 'script' for a script command and 'multi' for a multi command
673 type => $type,
674 # The time at which the instruction was received, in seconds (matches
675 # GA::Client->clientTime or GA::Session->sessionTime. Set to 'undef' if the
676 # session's ->status is still 'waiting')
677 time => $time,
678 };
679  
680 # Bless the object into existence
681 bless $self, $class;
682 return $self;
683 }
684  
685 ##################
686 # Methods
687  
688 ##################
689 # Accessors - set
690  
691 ##################
692 # Accessors - get
693  
694 sub session
695 { $_[0]->{session} }
696 sub parent
697 { $_[0]->{parent} }
698 sub number
699 { $_[0]->{number} }
700  
701 sub instruct
702 { $_[0]->{instruct} }
703 sub type
704 { $_[0]->{type} }
705 sub time
706 { $_[0]->{time} }
707 }
708  
709 { package Games::Axmud::Buffer::Cmd;
710  
711 use strict;
712 use warnings;
713 # use diagnostics;
714  
715 use Glib qw(TRUE FALSE);
716  
717 our @ISA = qw(Games::Axmud);
718  
719 ##################
720 # Constructors
721  
722 sub new {
723  
724 # Called by GA::Client->updateCmdBuffer or GA::Session->updateCmdBuffer
725 # The world command buffer object stores individual world commands processed by a session
726 #
727 # Expected arguments
728 # $session - The calling GA::Session
729 # $parent - Which registry stores this object - 'client' if stored in
730 # GA::Client->instructBufferHash, 'session' if stored in
731 # GA::Session->instructBufferHash
732 # $number - A number for this buffer object unique to the parent's registry
733 # - The Locator task can create one of these objects to handle a move by
734 # the character, when they are following someone else; in which case,
735 # $number should be set to -1
736 # $cmd - The command itself, e.g. 'north', 'kill orc', 'eat corpse'
737 # $time - The time at which the command was sent, in seconds (matches
738 # GA::Client->clientTime or GA::Session->sessionTime)
739 #
740 # Return values
741 # 'undef' on improper arguments
742 # Blessed reference to the newly-created object on success
743  
744 my ($class, $session, $parent, $number, $cmd, $time, $check) = @_;
745  
746 # Check for improper arguments
747 if (
748 ! defined $class || ! defined $session || ! defined $parent || ! defined $number
749 || ! defined $cmd || ! defined $number || ! defined $time || defined $check
750 ) {
751 return $axmud::CLIENT->writeImproper($class . '->new', @_);
752 }
753  
754 # Setup
755 my $self = {
756 _objName => 'cmd_buff_' . $number,
757 _objClass => $class,
758 _parentFile => undef, # No parent file object
759 _parentWorld => undef, # No parent file object
760 _privFlag => TRUE, # All IVs are private
761  
762 # The calling GA::Session
763 session => $session,
764 # Which registry stores this object - 'client' if stored in
765 # GA::Client->instructBufferHash, 'session' if stored in
766 # GA::Session->instructBufferHash
767 parent => $parent,
768 # A number for this buffer object unique to the parent's registry
769 # NB The Locator task can create one of these objects to handle a move by the character,
770 # when they are following someone else; in which case, $number should be set to -1
771 number => $number,
772  
773 # The command itself, e.g. 'north', 'kill orc', 'eat corpse'
774 cmd => $cmd,
775 # The time at which the command was sent, in seconds (matches
776 # GA::Client->clientTime or GA::Session->sessionTime)
777 time => $time,
778  
779 # IVs set by $self->interpretCmd, which decides whether $cmd is a look/glance or a
780 # movement command (or something different)
781 # Flag set to TRUE if this is a 'look'/'short_look' command, FALSE if not
782 lookFlag => FALSE,
783 # Flag set to TRUE if this is a 'glance'/'short_glance' command, FALSE if not
784 glanceFlag => FALSE,
785 # Flag set to TRUE if this is a movement command (including redirect mode commands,
786 # assisted moves and teleport commands), FALSE if not
787 moveFlag => FALSE,
788 # If it's a movement command, the equivalent standard command ('go', 'fly', 'sail' etc)
789 # and the (unabbreviated) direction of movement. Both are set to 'undef' for redirect
790 # mode commands/assisted moves.
791 moveDir => undef,
792 moveVerb => undef,
793 # Flag set to TRUE if this is an involuntary movement command caused by a follow
794 # pattern (GA::Profile::World->followPatternList and ->followAnchorPatternList)
795 followFlag => FALSE,
796 # Flag set to TRUE if this is an involuntary movement command caused by a follow
797 # anchor pattern (GA::Profile::World->->followAnchorPatternList only), meaning that
798 # no new room statement is expected
799 followAnchorFlag => FALSE,
800  
801 # Flag set to TRUE if this was a redirect mode command (in which case, ->moveFlag will
802 # also be TRUE). Set to FALSE otherwise
803 redirectFlag => FALSE,
804 # For redirect mode commands, the substituted command (e.g. if $cmd is 'north',
805 # $redirectCmd might be 'sail north'). Set to 'undef' if not a redirect mode command
806 redirectCmd => undef,
807  
808 # Flag set to TRUE if this was a teleportation command, resulting from a call by
809 # GA::Cmd::Teleport->do (or by equivalent code)
810 teleportFlag => FALSE,
811 # If this is a teleportation command, the world model number of the destination room
812 # (if known)
813 teleportDestRoom => undef,
814  
815 # Flag set to TRUE if this was an assisted move, FALSE if not
816 assistedFlag => FALSE,
817 # For assisted moves, the standard primary direction (corresponding to the custom
818 # primary direction $cmd). Set to 'undef' if not an assisted move
819 assistedPrimary => undef,
820 # For assisted moves, a list of one or more world commands sent in place of $cmd. An
821 # empty list if not an assisted move
822 assistedList => [],
823 # For assisted moves, the GA::Obj::Exit used for the move. Set to 'undef' if not an
824 # assisted move
825 assistedExitObj => undef,
826 };
827  
828 # Bless the object into existence
829 bless $self, $class;
830 return $self;
831 }
832  
833 ##################
834 # Methods
835  
836 sub interpretCmd {
837  
838 # Called by GA::Session->updateCmdBuffer (but not for redirect mode commands or assisted
839 # moves)
840 # When the Locator task is running, it needs to know whether this is a look/glance or a
841 # movement command, or a different sort of command
842 # This function uses the highest-priority command cage to decide, and then sets this
843 # object's IVs, so that the Locator task can consult them when it's ready
844 #
845 # Expected arguments
846 # $cage - The highest-priority command cage
847 #
848 # Return values
849 # 'undef' on improper arguments
850 # 1 on success
851  
852 my ($self, $cage, $check) = @_;
853  
854 # Local variablesinterpretCmd
855 my (
856 $session, $cmd, $dirType, $lookCmd, $shLookCmd, $glanceCmd, $shGlanceCmd, $moveFlag,
857 @patternList, @standardList,
858 );
859  
860 # Check for improper arguments
861 if (! defined $cage || defined $check) {
862  
863 return $axmud::CLIENT->writeImproper($self->_objClass . '->interpretCmd', @_);
864 }
865  
866 # Import IVs (for convenience)
867 $session = $self->session;
868 $cmd = $self->cmd;
869 # Later on we'll need to know what kind of direction $cmd is (if any)
870 $dirType = $session->currentDict->ivShow('combDirHash', $cmd);
871  
872 # (Do consult inferior cages)
873 $lookCmd = $cage->ivShow('cmdHash', 'look', $session);
874 $shLookCmd = $cage->ivShow('cmdHash', 'short_look', $session);
875 if (($lookCmd && $lookCmd eq $cmd) || ($shLookCmd && $shLookCmd eq $cmd)) {
876  
877 # It's a 'look' command
878 $self->ivPoke('lookFlag', TRUE);
879 return 1;
880 }
881  
882 $glanceCmd = $cage->ivShow('cmdHash', 'glance', $session);
883 $shGlanceCmd = $cage->ivShow('cmdHash', 'short_glance', $session);
884 if (($glanceCmd && $glanceCmd eq $cmd) || ($shGlanceCmd && $shGlanceCmd eq $cmd)) {
885  
886 # It's a 'glance command'
887 $self->ivPoke('glanceFlag', TRUE);
888 return 1;
889 }
890  
891 # There are several standard commands in the commands cage that are movement commands
892 # (stored in $cage->moveCmdList)
893 # The replacement strings usually contain the word 'direction'
894 # Compile a list of patterns with the word 'direction' replaced by (.*), so that the
895 # replacement string 'ride direction' gets converted to 'ride (.*)', which matches the
896 # commands 'ride north', 'ride up' or even 'ride out'. Ignore any patterns which contain
897 # only '(.*)', and would therefore match any string
898 # (If GA::Session->moveMode is set to 'is_move' or 'not_move', then $cmd has already been
899 # classified as definitely a movement command, or definitely not a movement command)
900 if ($session->moveMode eq 'unknown') {
901  
902 foreach my $verb ($cage->moveCmdList) {
903  
904 # (Do consult inferior cages)
905 my $replaceString = $cage->ivShow('cmdHash', $verb, $session);
906 if ($replaceString && $replaceString ne 'direction') {
907  
908 $replaceString =~ s/direction/(.*)/;
909  
910 # Ignore patterns with no alphanumeric chars
911 if ($replaceString =~ m/\w/) {
912  
913 push (@patternList, $replaceString);
914 # Store the standard command in a parallel list
915 push (@standardList, $verb);
916 }
917 }
918 }
919  
920 # Does $cmd match any of these patterns?
921 if (@patternList) {
922  
923 for (my $count = 0; $count < scalar @patternList; $count++) {
924  
925 my $pattern = $patternList[$count];
926  
927 if ($cmd =~ m/$pattern/i) {
928  
929 # It's a move command
930 $self->ivPoke('moveFlag', TRUE);
931 # Also store the standard command and the unabbreviated form of the
932 # specified direction (for convenience)
933 $self->ivPoke('moveVerb', $standardList[$count]);
934  
935 if (
936 $dirType
937 && ($dirType eq 'primaryDir' || $dirType eq 'primaryAbbrev')
938 ) {
939 $self->ivPoke('moveDir', $session->currentDict->checkPrimaryDir($1));
940  
941 } elsif (
942 $dirType
943 && ($dirType eq 'secondaryDir' || $dirType eq 'secondaryAbbrev')
944 ) {
945 $self->ivPoke('moveDir', $session->currentDict->checkSecondaryDir($1));
946  
947 } elsif (
948 $dirType
949 && ($dirType eq 'relativeDir' || $dirType eq 'relativeAbbrev')
950 ) {
951 $self->ivPoke('moveDir', $session->currentDict->checkRelativeDir($1));
952  
953 } else {
954  
955 $self->ivPoke('moveDir', $1);
956 }
957  
958 return 1;
959 }
960 }
961 }
962 }
963  
964 # If GA::Session->moveMode is 'is_move', this is definitely a movement command. If
965 # ->moveMose is 'unknown', we have to work out for ourselves whether it's a movement
966 # command, or not
967 if ($session->moveMode eq 'is_move') {
968  
969 $moveFlag = TRUE;
970  
971 } elsif ($session->moveMode eq 'unknown') {
972  
973 if (
974 # It's a command using a recognised custom direction...
975 defined $dirType
976 # The automapper knows the current location, and $cmd matches the directions of any
977 # of its exits
978 || (
979 $session->mapObj->currentRoom
980 && $session->mapObj->currentRoom->ivExists('exitNumHash', $cmd)
981 ) || (
982 $session->mapObj->ghostRoom
983 && $session->mapObj->ghostRoom->ivExists('exitNumHash', $cmd)
984 )
985 ) {
986 $moveFlag = TRUE;
987 }
988  
989 # Also Check alternative nominal directions in the current/ghost rooms' exits
990 if (! $moveFlag && $session->mapObj->currentRoom) {
991  
992 OUTER: foreach my $exitNum (
993 $session->mapObj->currentRoom->ivValues('exitNumHash')
994 ) {
995 my $exitObj = $session->worldModelObj->ivShow('exitModelHash', $exitNum);
996  
997 if (
998 defined $exitObj->altDir
999 && index($exitObj->altDir, $cmd) > -1
1000 ) {
1001 $moveFlag = TRUE;
1002 last OUTER;
1003 }
1004 }
1005 }
1006  
1007 if (! $moveFlag && $session->mapObj->ghostRoom) {
1008  
1009 OUTER: foreach my $exitNum (
1010 $session->mapObj->ghostRoom->ivValues('exitNumHash')
1011 ) {
1012 my $exitObj = $session->worldModelObj->ivShow('exitModelHash', $exitNum);
1013  
1014 if (
1015 defined $exitObj->altDir
1016 && index($exitObj->altDir, $cmd) > -1
1017 ) {
1018 $moveFlag = TRUE;
1019 last OUTER;
1020 }
1021 }
1022 }
1023 }
1024  
1025 if ($moveFlag) {
1026  
1027 # It's a movement command
1028 $self->ivPoke('moveFlag', TRUE);
1029 # If it's an abbreviated primary/secondary direction, store the unabbreviated form in
1030 # ->moveDir
1031 if ($dirType && ($dirType eq 'primaryDir' || $dirType eq 'primaryAbbrev')) {
1032 $self->ivPoke('moveDir', $session->currentDict->checkPrimaryDir($cmd));
1033 } elsif ($dirType && ($dirType eq 'secondaryDir' || $dirType eq 'secondaryAbbrev')) {
1034 $self->ivPoke('moveDir', $session->currentDict->checkSecondaryDir($cmd));
1035 } elsif ($dirType && ($dirType eq 'relativeDir' || $dirType eq 'relativeAbbrev')) {
1036 $self->ivPoke('moveDir', $session->currentDict->checkRelativeDir($cmd));
1037 } else {
1038 $self->ivPoke('moveDir', $cmd);
1039 }
1040  
1041 # The Axmud standard command for this situation is 'go' (see the comments in the command
1042 # cage)
1043 $self->ivPoke('moveVerb', 'go');
1044 }
1045  
1046 # Check complete
1047 return 1;
1048 }
1049  
1050 sub addRedirect {
1051  
1052 # Called by GA::Session->updateCmdBuffer (for redirect mode commands only)
1053 # Sets this object's redirect mode IVs
1054 #
1055 # Expected arguments
1056 # $redirectCmd - The substituted command e.g. if $cmd is 'north', $redirectCmd might be
1057 # 'sail north')
1058 #
1059 # Return values
1060 # 'undef' on improper arguments
1061 # 1 on success
1062  
1063 my ($self, $redirectCmd, $check) = @_;
1064  
1065 # Check for improper arguments
1066 if (! defined $redirectCmd || defined $check) {
1067  
1068 return $axmud::CLIENT->writeImproper($self->_objClass . '->addRedirect', @_);
1069 }
1070  
1071 # Set IVs
1072 $self->ivPoke('moveFlag', TRUE);
1073 $self->ivPoke('redirectFlag', TRUE);
1074 $self->ivPoke('redirectCmd', $redirectCmd);
1075  
1076 return 1;
1077 }
1078  
1079 sub addAssisted {
1080  
1081 # Called by GA::Session->updateCmdBuffer (for assisted moves only)
1082 # Sets this object's assisted move IVs
1083 #
1084 # Expected arguments
1085 # $standardCmd - The standard primary direction equivalent to the custom primary
1086 # direction already stored in $self->cmd
1087 # $assistedCmd - The sequence of world commands used in the assisted move (e.g.
1088 # 'open door;north')
1089 # $exitObj - The GA::Obj::Exit used in the move (an exit in the exit model)
1090 #
1091 # Return values
1092 # 'undef' on improper arguments
1093 # 1 on success
1094  
1095 my ($self, $standardCmd, $assistedCmd, $exitObj, $check) = @_;
1096  
1097 # Local variables
1098 my (
1099 $cmdSep,
1100 @list,
1101 );
1102  
1103 # Check for improper arguments
1104 if (
1105 ! defined $standardCmd || ! defined $assistedCmd || ! defined $exitObj
1106 || defined $check
1107 ) {
1108 return $axmud::CLIENT->writeImproper($self->_objClass . '->addAssisted', @_);
1109 }
1110  
1111 # Set IVs
1112 $self->ivPoke('moveFlag', TRUE);
1113 $self->ivPoke('assistedFlag', TRUE);
1114 $self->ivPoke('assistedPrimary', $standardCmd);
1115 $self->ivPoke('assistedExitObj', $exitObj);
1116  
1117 $cmdSep = $axmud::CLIENT->cmdSep;
1118 @list = split(m/$cmdSep/, $assistedCmd);
1119 $self->ivPoke('assistedList', @list);
1120  
1121 return 1;
1122 }
1123  
1124 sub addTeleport {
1125  
1126 # Called by GA::Session->updateCmdBuffer (for teleport commands resulting from a call by
1127 # GA::Cmd::Teleport->do, or equivalent code)
1128 # Sets this object's teleport IVs
1129 #
1130 # Expected arguments
1131 # (none besides $self)
1132 #
1133 # Optional arguments
1134 # $destRoom - The world model number of the destination room (if known; 'undef' if not)
1135 #
1136 # Return values
1137 # 'undef' on improper arguments
1138 # 1 on success
1139  
1140 my ($self, $destRoom, $check) = @_;
1141  
1142 # Check for improper arguments
1143 if (defined $check) {
1144  
1145 return $axmud::CLIENT->writeImproper($self->_objClass . '->addTeleport', @_);
1146 }
1147  
1148 # Set IVs
1149 $self->ivPoke('moveFlag', TRUE);
1150 $self->ivPoke('teleportFlag', TRUE);
1151 $self->ivPoke('teleportDestRoom', $destRoom); # May be 'undef'
1152  
1153 return 1;
1154 }
1155  
1156 sub addFollow {
1157  
1158 # Called by GA::Task::Locator->processLine (for an involuntary follow move)
1159 # Sets this object's follow IVs
1160 #
1161 # Expected arguments
1162 # (none besides $self)
1163 #
1164 # Optional arguments
1165 # $anchorFlag - TRUE if no room statement is expected (meaning that the matching line
1166 # itself acts as a room anchor); FALSE (or 'undef') if a room
1167 # statement is expected as normal
1168 #
1169 # Return values
1170 # 'undef' on improper arguments
1171 # 1 on success
1172  
1173 my ($self, $anchorFlag, $check) = @_;
1174  
1175 # Check for improper arguments
1176 if (defined $check) {
1177  
1178 return $axmud::CLIENT->writeImproper($self->_objClass . '->addFollow', @_);
1179 }
1180  
1181 # Set IVs
1182 $self->ivPoke('moveFlag', TRUE);
1183 $self->ivPoke('moveDir', $self->cmd);
1184 $self->ivPoke('moveVerb', 'go');
1185 $self->ivPoke('followFlag', TRUE);
1186 if ($anchorFlag) {
1187  
1188 $self->ivPoke('followAnchorFlag', TRUE);
1189 }
1190  
1191 return 1;
1192 }
1193  
1194 sub addMove {
1195  
1196 # Called by GA::Task::Locator->processLine (for an involuntary exit in a known direction)
1197 # Sets this object's IVs
1198 #
1199 # Expected arguments
1200 # (none besides $self)
1201 #
1202 # Return values
1203 # 'undef' on improper arguments
1204 # 1 on success
1205  
1206 my ($self, $check) = @_;
1207  
1208 # Check for improper arguments
1209 if (defined $check) {
1210  
1211 return $axmud::CLIENT->writeImproper($self->_objClass . '->addMove', @_);
1212 }
1213  
1214 # Set IVs
1215 $self->ivPoke('moveFlag', TRUE);
1216 $self->ivPoke('moveDir', $self->cmd);
1217 $self->ivPoke('moveVerb', 'go');
1218  
1219 return 1;
1220 }
1221  
1222 ##################
1223 # Accessors - set
1224  
1225 ##################
1226 # Accessors - get
1227  
1228 sub session
1229 { $_[0]->{session} }
1230 sub parent
1231 { $_[0]->{parent} }
1232 sub number
1233 { $_[0]->{number} }
1234  
1235 sub cmd
1236 { $_[0]->{cmd} }
1237 sub time
1238 { $_[0]->{time} }
1239  
1240 sub lookFlag
1241 { $_[0]->{lookFlag} }
1242 sub glanceFlag
1243 { $_[0]->{glanceFlag} }
1244 sub moveFlag
1245 { $_[0]->{moveFlag} }
1246 sub moveDir
1247 { $_[0]->{moveDir} }
1248 sub moveVerb
1249 { $_[0]->{moveVerb} }
1250 sub followFlag
1251 { $_[0]->{followFlag} }
1252 sub followAnchorFlag
1253 { $_[0]->{followAnchorFlag} }
1254  
1255 sub redirectFlag
1256 { $_[0]->{redirectFlag} }
1257 sub redirectCmd
1258 { $_[0]->{redirectCmd} }
1259  
1260 sub teleportFlag
1261 { $_[0]->{teleportFlag} }
1262 sub teleportDestRoom
1263 { $_[0]->{teleportDestRoom} }
1264  
1265 sub assistedFlag
1266 { $_[0]->{assistedFlag} }
1267 sub assistedPrimary
1268 { $_[0]->{assistedPrimary} }
1269 sub assistedList
1270 { my $self = shift; return @{$self->{assistedList}}; }
1271 sub assistedExitObj
1272 { $_[0]->{assistedExitObj} }
1273 }
1274  
1275 # Package must return a true value
1276 1