| 1 | package BalanceOfPower::Role::Ruler; | ||
| 2 | $BalanceOfPower::Role::Ruler::VERSION = '0.200000'; | ||
| 3 | use strict; | ||
| 4 | use Moo::Role; | ||
| 5 | |||
| 6 | use BalanceOfPower::Constants ':all'; | ||
| 7 | |||
| 8 | use BalanceOfPower::Relations::Influence; | ||
| 9 | |||
| 10 | requires 'broadcast_event'; | ||
| 11 | requires 'get_nation'; | ||
| 12 | |||
| 13 | has influences => ( | ||
| 14 | is => 'ro', | ||
| 15 | default => sub { BalanceOfPower::Relations::RelPack->new() }, | ||
| 16 | handles => { reset_influences => 'delete_link_for_node', | ||
| 17 | print_influences => 'print_links', | ||
| 18 | add_influence => 'add_link' } | ||
| 19 | ); | ||
| 20 | sub influences_garbage_collector | ||
| 21 | { | ||
| 22 | my $self = shift; | ||
| 23 | $self->influences->garbage_collector(sub { my $rel = shift; return $rel->status == -1 }); | ||
| 24 | } | ||
| 25 | sub is_under_influence | ||
| 26 | { | ||
| 27 | my $self = shift; | ||
| 28 | my $nation = shift; | ||
| 29 | my @rels = $self->influences->query( | ||
| 30 | sub { | ||
| 31 | my $rel = shift; | ||
| 32 | return 0 if($rel->node2 ne $nation); | ||
| 33 | return $rel->actual_influence() | ||
| 34 | }, $nation); | ||
| 35 | if(@rels > 0) | ||
| 36 | { | ||
| 37 | return $rels[0]->start($nation); | ||
| 38 | } | ||
| 39 | else | ||
| 40 | { | ||
| 41 | return undef; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | sub print_nation_situation | ||
| 45 | { | ||
| 46 | my $self = shift; | ||
| 47 | my $nation = shift; | ||
| 48 | my $domination = $self->is_under_influence($nation); | ||
| 49 | return "$nation is under control of $domination" if $domination; | ||
| 50 | my @influence = $self->has_influence($nation); | ||
| 51 | if(@influence > 0) | ||
| 52 | { | ||
| 53 | my $out = ""; | ||
| 54 | for(@influence) | ||
| 55 | { | ||
| 56 | $out .= "$nation has influence on $_\n"; | ||
| 57 | } | ||
| 58 | return $out; | ||
| 59 | } | ||
| 60 | else | ||
| 61 | { | ||
| 62 | return "$nation is free"; | ||
| 63 | } | ||
| 64 | |||
| 65 | } | ||
| 66 | sub has_influence | ||
| 67 | { | ||
| 68 | my $self = shift; | ||
| 69 | my $nation = shift; | ||
| 70 | my @influences = $self->influences->query( | ||
| 71 | sub { | ||
| 72 | my $rel = shift; | ||
| 73 | return 0 if($rel->node1 ne $nation); | ||
| 74 | return $rel->actual_influence() | ||
| 75 | }, $nation); | ||
| 76 | my @out = (); | ||
| 77 | for(@influences) | ||
| 78 | { | ||
| 79 | push @out, $_->destination($nation); | ||
| 80 | } | ||
| 81 | return @out; | ||
| 82 | } | ||
| 83 | sub empire | ||
| 84 | { | ||
| 85 | my $self = shift; | ||
| 86 | my $n = shift; | ||
| 87 | if(my $domination = $self->is_under_influence($n)) | ||
| 88 | { | ||
| 89 | my $dominator = $domination; | ||
| 90 | my @allies = $self->has_influence($dominator); | ||
| 91 | push @allies, $dominator; | ||
| 92 | return @allies; | ||
| 93 | } | ||
| 94 | else | ||
| 95 | { | ||
| 96 | my @allies = $self->has_influence($n); | ||
| 97 | push @allies, $n; | ||
| 98 | return @allies; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | sub occupy | ||
| 102 | { | ||
| 103 | my $self = shift; | ||
| 104 | my $nation = shift; | ||
| 105 | my $occupiers = shift; | ||
| 106 | my $leader = shift; | ||
| 107 | my $internal_disorder = shift || 0; | ||
| 108 | $self->get_nation($nation)->occupation($self); | ||
| 109 | my $occupied_progress = $self->get_nation($nation)->progress; | ||
| 110 | |||
| 111 | my @occupiers_array = @{$occupiers}; | ||
| 112 | my $real_leader = $self->is_under_influence($leader); | ||
| 113 | if($real_leader) | ||
| 114 | { | ||
| 115 | @occupiers_array = grep { $_ ne $real_leader } @occupiers_array; | ||
| 116 | push @occupiers_array, $real_leader; | ||
| 117 | $leader = $real_leader; | ||
| 118 | } | ||
| 119 | foreach my $c (@occupiers_array) | ||
| 120 | { | ||
| 121 | if($c eq $leader) | ||
| 122 | { | ||
| 123 | $self->add_influence(BalanceOfPower::Relations::Influence->new( node1 => $c, | ||
| 124 | node2 => $nation, | ||
| 125 | status => 0, | ||
| 126 | next => $internal_disorder ? 2 : 1, | ||
| 127 | clock => 0 )); | ||
| 128 | $self->set_diplomacy($nation, $c, DOMINION_DIPLOMACY); | ||
| 129 | $self->copy_diplomacy($c, $nation); | ||
| 130 | if($self->get_nation($c)->progress < $occupied_progress) | ||
| 131 | { | ||
| 132 | $self->get_nation($c)->progress($occupied_progress); | ||
| 133 | $self->broadcast_event("$c ACQUIRES PROGRESS FROM $nation: $occupied_progress", $c, $nation); | ||
| 134 | } | ||
| 135 | } | ||
| 136 | else | ||
| 137 | { | ||
| 138 | $self->add_influence(BalanceOfPower::Relations::Influence->new( node1 => $c, | ||
| 139 | node2 => $nation, | ||
| 140 | status => 0, | ||
| 141 | clock => 0 )); | ||
| 142 | $self->set_diplomacy($nation, $c, DIPLOMACY_AFTER_OCCUPATION); | ||
| 143 | $self->get_nation($c)->grow(); | ||
| 144 | } | ||
| 145 | $self->broadcast_event("$c OCCUPIES $nation", $c, $nation); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | sub situation_clock | ||
| 149 | { | ||
| 150 | my $self = shift; | ||
| 151 | foreach my $i ($self->influences->all()) | ||
| 152 | { | ||
| 153 | my $old_status = $i->status_label; | ||
| 154 | my $new_status = $i->click(); | ||
| 155 | if($new_status && $old_status ne $new_status) | ||
| 156 | { | ||
| 157 | if($new_status eq 'dominate') | ||
| 158 | { | ||
| 159 | $self->broadcast_event($i->node1 . " DOMINATES " . $i->node2, $i->node1, $i->node2); | ||
| 160 | } | ||
| 161 | elsif($new_status eq 'control') | ||
| 162 | { | ||
| 163 | $self->broadcast_event($i->node1 . " CONTROLS " . $i->node2, $i->node1, $i->node2); | ||
| 164 | } | ||
| 165 | } | ||
| 166 | } | ||
| 167 | $self->influences_garbage_collector(); | ||
| 168 | } | ||
| 169 | |||
| 170 | 1; |