File Coverage

lib/BalanceOfPower/Commands/NagTreaty.pm
Criterion Covered Total %
statement 55 59 93.2
branch 16 24 66.6
condition 7 15 46.6
subroutine 4 4 100.0
pod 0 2 0.0
total 82 104 78.8


line stmt bran cond sub pod time code
1             package BalanceOfPower::Commands::NagTreaty;
2             $BalanceOfPower::Commands::NagTreaty::VERSION = '0.400115';
3 13     13   65 use Moo;
  13         21  
  13         73  
4 13     13   3083 use Array::Utils qw(intersect);
  13         23  
  13         8642  
5              
6             extends 'BalanceOfPower::Commands::TargetNation';
7             with 'BalanceOfPower::Commands::Role::TreatiesUnderLimit';
8              
9             sub get_available_targets
10             {
11 9     9 0 23 my $self = shift;
12 9         48 my @targets = $self->SUPER::get_available_targets();
13 9         26 my $nation = $self->actor;
14 9         17 @targets = grep {! $self->world->exists_treaty($nation, $_) } @targets;
  36         119  
15 9         23 @targets = grep { $self->world->diplomacy_status($nation, $_) ne 'HATE' } @targets;
  36         103  
16 9         54 return $self->nations_under_treaty_limit(@targets);
17             }
18              
19             sub IA
20             {
21 9     9 0 11 my $self = shift;
22 9         22 my $actor = $self->get_nation();
23 9         33 my @available_targets = $self->get_available_targets();
24 9 50       29 return undef if @available_targets == 0;
25 9         60 my @near = $self->world->near_nations($actor->name, 1);
26              
27 9         69 my @friendly_neighbors = $self->world->shuffle("Mixing neighbors to choose about NAG treaty", intersect(@near, @available_targets));
28 9         64 my @ordered_friendly_neighbors = ();
29 9         14 my $dangerous_neighbor = 0;
30 9         23 for(@friendly_neighbors)
31             {
32 7         8 my $n = $_;
33 7 50       39 if(! $self->world->exists_treaty($self->name, $n))
34             {
35 7         24 my $supporter = $self->world->supported($n);
36 7 100       13 if($supporter)
37             {
38 1         4 my $supporter_nation = $supporter->node1;
39 1 50       6 if($supporter_nation eq $actor->name)
40             {
41             #I'm the supporter of this nation!
42 0         0 push @ordered_friendly_neighbors, { nation => $n,
43             interest => 0 };
44             }
45             else
46             {
47 1 50       8 if($self->world->crisis_exists($actor->name, $supporter_nation))
    0          
48             {
49 1         5 push @ordered_friendly_neighbors, { nation => $n,
50             interest => 100 };
51 1         4 $dangerous_neighbor = 1;
52             }
53             elsif($self->world->diplomacy_status($actor->name, $supporter_nation) eq 'HATE')
54             {
55 0         0 push @ordered_friendly_neighbors, { nation => $n,
56             interest => 10 };
57             }
58             else
59             {
60 0         0 push @ordered_friendly_neighbors, { nation => $n,
61             interest => 2 };
62             }
63             }
64             }
65             else
66             {
67 6         27 push @ordered_friendly_neighbors, { nation => $n,
68             interest => 1 };
69             }
70             }
71             }
72 9 100 100     47 if(@ordered_friendly_neighbors > 0 && $dangerous_neighbor)
73             {
74 1         5 @ordered_friendly_neighbors = sort { $b->{interest} <=> $a->{interest} } @ordered_friendly_neighbors;
  1         4  
75 1         7 return "TREATY NAG WITH " . $ordered_friendly_neighbors[0]->{nation};
76             }
77             else
78             {
79             #Scanning crises
80 8         55 my @crises = $self->world->get_crises($actor->name);
81 8 100       25 if(@crises > 0)
82             {
83 6         149 foreach my $c ($self->world->shuffle("Mixing crisis for war for " . $actor->name, @crises))
84             {
85             #NAG with enemy supporter
86 9         57 my $enemy = $c->destination($actor->name);
87 9         46 my $supporter = $self->world->supported($enemy);
88 9 100       24 if($supporter)
89             {
90 1         5 my $supporter_nation = $supporter->node1;
91 1 50 33     24 if($supporter_nation ne $actor->name &&
      33        
92             $self->world->diplomacy_status($actor->name, $supporter_nation) ne 'HATE' &&
93             ! $self->world->exists_treaty($actor->name, $supporter_nation))
94             {
95 1         7 return "TREATY NAG WITH " . $supporter_nation;
96             }
97             }
98             #NAG with enemy ally
99 8         42 my @allies = $self->world->get_allies($enemy);
100 8         145 for($self->world->shuffle("Mixing allies of enemy for a NAG", @allies))
101             {
102 1         6 my $all = $_->destination($enemy);
103 1 50 33     13 if($all ne $actor->name &&
      33        
104             $self->world->diplomacy_status($actor->name, $all) ne 'HATE' &&
105             ! $self->world->exists_treaty($actor->name, $all))
106             {
107 1         8 return "TREATY NAG WITH " . $all;
108             }
109             }
110             }
111             }
112 6 100       24 if(@ordered_friendly_neighbors > 0)
113             {
114 1         4 @ordered_friendly_neighbors = sort { $b->{interest} <=> $a->{interest} } @ordered_friendly_neighbors;
  0         0  
115 1         8 return "TREATY NAG WITH " . $ordered_friendly_neighbors[0]->{nation};
116             }
117 5         19 return undef;
118             }
119             }
120              
121             1;