File Coverage

lib/BalanceOfPower/Commands/NagTreaty.pm
Criterion Covered Total %
statement 56 59 94.9
branch 16 24 66.6
condition 7 15 46.6
subroutine 4 4 100.0
pod 0 2 0.0
total 83 104 79.8


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