| 1 | package BalanceOfPower::Commands::MilitarySupport; | ||
| 2 | $BalanceOfPower::Commands::MilitarySupport::VERSION = '0.300010'; | ||
| 3 | use BalanceOfPower::Constants ":all"; | ||
| 4 | use Moo; | ||
| 5 | use Array::Utils qw(intersect); | ||
| 6 | |||
| 7 | extends 'BalanceOfPower::Commands::TargetNation'; | ||
| 8 | |||
| 9 | sub get_available_targets | ||
| 10 | { | ||
| 11 | my $self = shift; | ||
| 12 | my $player = $self->actor; | ||
| 13 | return grep { $self->world->get_nation($_)->accept_military_support($player, $self->world) } $self->world->get_friends($player); | ||
| 14 | } | ||
| 15 | |||
| 16 | sub IA | ||
| 17 | { | ||
| 18 | my $self = shift; | ||
| 19 | my $actor = $self->get_nation(); | ||
| 20 | return undef if($actor->army < ARMY_TO_GIVE_MILITARY_SUPPORT); | ||
| 21 | |||
| 22 | my @crises = $self->world->get_crises($actor->name); | ||
| 23 | my @friends = $self->world->shuffle("Choosing friend to support for " . $actor->name, $self->world->get_friends($actor->name)); | ||
| 24 | my @targets = $self->get_available_targets(); | ||
| 25 | @friends = $self->world->shuffle("Mixing friends for military support for " . $actor->name, intersect(@friends, @targets)); | ||
| 26 | if(@crises > 0) | ||
| 27 | { | ||
| 28 | foreach my $c ($self->world->shuffle("Mixing crisis for war for " . $actor->name, @crises)) | ||
| 29 | { | ||
| 30 | my $enemy = $self->world->get_nation($c->destination($actor->name)); | ||
| 31 | next if $self->world->war_busy($enemy->name); | ||
| 32 | for(@friends) | ||
| 33 | { | ||
| 34 | if($self->world->border_exists($_, $enemy->name)) | ||
| 35 | { | ||
| 36 | return "MILITARY SUPPORT " . $_; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | } | ||
| 40 | } | ||
| 41 | if(@friends) | ||
| 42 | { | ||
| 43 | my $f = $friends[0]; | ||
| 44 | return "MILITARY SUPPORT " . $f; | ||
| 45 | } | ||
| 46 | return undef; | ||
| 47 | } | ||
| 48 | |||
| 49 | 1; |