File Coverage

lib/BalanceOfPower/Commands/NagTreaty.pm
Criterion Covered Total %
statement 56 59 94.9
branch 17 24 70.8
condition 7 15 46.6
subroutine 4 4 100.0
pod 0 2 0.0
total 84 104 80.7


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