Henrybk

InPortalDist.pm

Mar 9th, 2017
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.34 KB | None | 0 0
  1. package eventMacro::Condition::InPortalDist;
  2.  
  3. use strict;
  4. use Globals;
  5. use Utils;
  6. use base 'eventMacro::Conditiontypes::NumericConditionState';
  7.  
  8. sub _hooks {
  9.     ['add_portal_list','portal_disappeared'];
  10. }
  11.  
  12. sub _dynamic_hooks {
  13.     ['packet/actor_movement_interrupted','packet/high_jump','packet/character_moves','packet_mapChange'];
  14. }
  15.  
  16. sub _parse_syntax {
  17.     my ( $self, $condition_code ) = @_;
  18.    
  19.     $self->{list_size} = 0;
  20.     $self->{fulfilled_actor} = undef;
  21.    
  22.     $self->SUPER::_parse_syntax($condition_code);
  23. }
  24.  
  25. sub _get_val {
  26.     my ( $self ) = @_;
  27.     return $self->{current_actor_distance};
  28.     undef $self->{current_actor_distance};
  29. }
  30.  
  31. sub validate_condition {
  32.     my ( $self, $callback_type, $callback_name, $args ) = @_;
  33.    
  34.     if ($callback_type eq 'variable') {
  35.         $self->update_validator_var($callback_name, $args);
  36.        
  37.     } elsif ($callback_type eq 'hook') {
  38.        
  39.         if ($callback_name eq 'add_portal_list') {
  40.             $self->{list_size} = $portalsList->size;
  41.             if ($self->{list_size} == 1) {
  42.                 $self->add_or_remove_dynamic_hooks(1);
  43.             }
  44.            
  45.             $self->{current_actor_distance} = distance($char->{pos_to}, $args->{pos_to});
  46.             if ( !defined $self->{fulfilled_actor} && $self->validator_check) {
  47.                 $self->{fulfilled_actor} = $args;
  48.             }
  49.            
  50.         } elsif ( $callback_name eq 'portal_disappeared' ) {
  51.             $self->{list_size} = ($portalsList->size - 1);
  52.             if ($self->{list_size} == 0) {
  53.                 $self->add_or_remove_dynamic_hooks(0);
  54.             }
  55.            
  56.             if (defined $self->{fulfilled_actor} && $args->{portal}->{binID} == $self->{fulfilled_actor}->{binID}) {
  57.                 $self->search_for_dist_in_actors_list;
  58.             }
  59.            
  60.         } elsif ($callback_name eq 'packet/character_moves' || ($callback_name eq 'packet/actor_movement_interrupted' && Actor::get($args->{ID})->isa('Actor::You')) || ($callback_name eq 'packet/high_jump' && Actor::get($args->{ID})->isa('Actor::You'))) {
  61.            
  62.             if (defined $self->{fulfilled_actor}) {
  63.                 $self->{current_actor_distance} = distance($char->{pos_to}, $self->{fulfilled_actor}->{pos_to});
  64.                 return $self->SUPER::validate_condition if ( $self->validator_check );
  65.                 $self->search_for_dist_in_actors_list;
  66.             } else {
  67.                 $self->search_for_dist_in_actors_list;
  68.             }
  69.            
  70.         } elsif ($callback_name eq 'packet_mapChange') {
  71.             if ($self->{list_size} > 0) {
  72.                 $self->add_or_remove_dynamic_hooks(0);
  73.             }
  74.             $self->{list_size} = 0;
  75.             $self->{fulfilled_actor} = undef;
  76.         }
  77.        
  78.     } elsif ($callback_type eq 'recheck') {
  79.         $self->search_for_dist_in_actors_list;
  80.     }
  81.    
  82.     return $self->SUPER::validate_condition( (defined $self->{fulfilled_actor} ? 1 : 0) );
  83. }
  84.  
  85. sub search_for_dist_in_actors_list {
  86.     my ($self) = @_;
  87.     $self->{fulfilled_actor} = undef;
  88.     foreach my $actor (@{$portalsList->getItems}) {
  89.         $self->{current_actor_distance} = distance($char->{pos_to}, $actor->{pos_to});
  90.         next unless ( $self->validator_check );
  91.         $self->{fulfilled_actor} = $actor;
  92.         last;
  93.     }
  94. }
  95.  
  96. sub get_new_variable_list {
  97.     my ($self) = @_;
  98.     my $new_variables;
  99.    
  100.     $new_variables->{".".$self->{name}."Last"."Pos"} = sprintf("%d %d %s", $self->{fulfilled_actor}->{pos_to}{x}, $self->{fulfilled_actor}->{pos_to}{y}, $field->baseName);
  101.     $new_variables->{".".$self->{name}."Last"."BinId"} = $self->{fulfilled_actor}->{binID};
  102.     $new_variables->{".".$self->{name}."Last"."Dist"} = distance($char->{pos_to}, $self->{fulfilled_actor}->{pos_to});
  103.    
  104.     return $new_variables;
  105. }
  106.  
  107. 1;
Advertisement
Add Comment
Please, Sign In to add comment