| 1 | package BalanceOfPower::Role::CrisisManager; | ||
| 2 | $BalanceOfPower::Role::CrisisManager::VERSION = '0.300010'; | ||
| 3 | use strict; | ||
| 4 | use v5.10; | ||
| 5 | use Moo::Role; | ||
| 6 | use Template; | ||
| 7 | use Term::ANSIColor; | ||
| 8 | |||
| 9 | use BalanceOfPower::Constants ':all'; | ||
| 10 | use BalanceOfPower::Printer; | ||
| 11 | |||
| 12 | requires 'get_all_crises'; | ||
| 13 | requires 'get_hates'; | ||
| 14 | requires 'crisis_exists'; | ||
| 15 | requires 'war_exists'; | ||
| 16 | requires 'broadcast_event'; | ||
| 17 | requires 'add_crisis'; | ||
| 18 | |||
| 19 | sub crisis_generator | ||
| 20 | { | ||
| 21 | my $self = shift; | ||
| 22 | my @crises = $self->get_all_crises(); | ||
| 23 | my @hates = (); | ||
| 24 | foreach my $h ($self->get_hates()) | ||
| 25 | { | ||
| 26 | push @hates, $h | ||
| 27 | if(! $self->crisis_exists($h->node1, $h->node2)) | ||
| 28 | } | ||
| 29 | my $crises_to_use = \@crises; | ||
| 30 | my $hates_to_use = \@hates; | ||
| 31 | for(my $i = 0; $i < CRISIS_GENERATION_TRIES; $i++) | ||
| 32 | { | ||
| 33 | ($hates_to_use, $crises_to_use) = $self->crisis_generator_round($hates_to_use, $crises_to_use); | ||
| 34 | } | ||
| 35 | |||
| 36 | } | ||
| 37 | |||
| 38 | sub crisis_generator_round | ||
| 39 | { | ||
| 40 | my $self = shift; | ||
| 41 | my $hates_to_use = shift || [] ; | ||
| 42 | my $crises_to_use = shift || []; | ||
| 43 | my @hates = $self->shuffle("Crisis generation: choosing hate", @{ $hates_to_use }); | ||
| 44 | my @crises = $self->shuffle("Crisis generation: choosing crisis", @{ $crises_to_use}); | ||
| 45 | my @original_hates = @hates; | ||
| 46 | my @original_crises = @crises; | ||
| 47 | |||
| 48 | my $picked_hate = undef; | ||
| 49 | my $picked_crisis = undef; | ||
| 50 | if(@hates) | ||
| 51 | { | ||
| 52 | $picked_hate = shift @hates; | ||
| 53 | } | ||
| 54 | if(@crises) | ||
| 55 | { | ||
| 56 | $picked_crisis = shift @crises; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | my $action = $self->random(0, CRISIS_GENERATOR_NOACTION_TOKENS + 3, "Crisis action choose"); | ||
| 61 | if($action == 0) #NEW CRISIS | ||
| 62 | { | ||
| 63 | return (\@original_hates, \@original_crises) if ! $picked_hate; | ||
| 64 | if(! $self->war_exists($picked_hate->node1, $picked_hate->node2)) | ||
| 65 | { | ||
| 66 | $self->create_or_escalate_crisis($picked_hate->node1, $picked_hate->node2); | ||
| 67 | return (\@hates, \@original_crises); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | elsif($action == 1) #ESCALATE | ||
| 71 | { | ||
| 72 | return (\@original_hates, \@original_crises) if ! $picked_crisis; | ||
| 73 | if(! $self->war_exists($picked_crisis->node1, $picked_crisis->node2)) | ||
| 74 | { | ||
| 75 | $self->create_or_escalate_crisis($picked_crisis->node1, $picked_crisis->node2); | ||
| 76 | } | ||
| 77 | return (\@original_hates, \@crises); | ||
| 78 | } | ||
| 79 | elsif($action == 2) #COOL DOWN | ||
| 80 | { | ||
| 81 | return (\@original_hates, \@original_crises) if ! $picked_crisis; | ||
| 82 | if(! $self->war_exists($picked_crisis->node1, $picked_crisis->node2)) | ||
| 83 | { | ||
| 84 | $self->cool_down($picked_crisis->node1, $picked_crisis->node2); | ||
| 85 | } | ||
| 86 | return (\@original_hates, \@crises); | ||
| 87 | } | ||
| 88 | elsif($action == 3) #ELIMINATE | ||
| 89 | { | ||
| 90 | return (\@original_hates, \@original_crises) if ! $picked_crisis; | ||
| 91 | if(! $self->war_exists($picked_crisis->node1, $picked_crisis->node2)) | ||
| 92 | { | ||
| 93 | $self->delete_crisis($picked_crisis->node1, $picked_crisis->node2); | ||
| 94 | } | ||
| 95 | return (\@original_hates, \@crises); | ||
| 96 | } | ||
| 97 | else | ||
| 98 | { | ||
| 99 | return (\@original_hates, \@original_crises); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | sub create_or_escalate_crisis | ||
| 103 | { | ||
| 104 | my $self = shift; | ||
| 105 | my $node1 = shift || ""; | ||
| 106 | my $node2 = shift || ""; | ||
| 107 | if(my $crisis = $self->crisis_exists($node1, $node2)) | ||
| 108 | { | ||
| 109 | if(! $crisis->is_max_crisis) | ||
| 110 | { | ||
| 111 | $crisis->escalate_crisis(); | ||
| 112 | my $event = "CRISIS BETWEEN $node1 AND $node2 ESCALATES"; | ||
| 113 | if($crisis->is_max_crisis) | ||
| 114 | { | ||
| 115 | $event .= " TO MAX LEVEL"; | ||
| 116 | } | ||
| 117 | $self->broadcast_event($event, $node1, $node2); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | else | ||
| 121 | { | ||
| 122 | $self->add_crisis($node1, $node2); | ||
| 123 | $self->broadcast_event("CRISIS BETWEEN $node1 AND $node2 STARTED", $node1, $node2); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | sub cool_down | ||
| 127 | { | ||
| 128 | my $self = shift; | ||
| 129 | my $node1 = shift; | ||
| 130 | my $node2 = shift; | ||
| 131 | if(my $crisis = $self->crisis_exists($node1, $node2)) | ||
| 132 | { | ||
| 133 | $crisis->cooldown_crisis(); | ||
| 134 | if(! $crisis->is_crisis()) | ||
| 135 | { | ||
| 136 | my $event = "CRISIS BETWEEN $node1 AND $node2 ENDED"; | ||
| 137 | $self->broadcast_event($event, $node1, $node2); | ||
| 138 | } | ||
| 139 | else | ||
| 140 | { | ||
| 141 | $self->broadcast_event("CRISIS BETWEEN $node1 AND $node2 COOLED DOWN", $node1, $node2); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | sub print_all_crises | ||
| 147 | { | ||
| 148 | my $self = shift; | ||
| 149 | my $n = shift; | ||
| 150 | my $level = shift || 0; | ||
| 151 | my $mode = shift || 'print'; | ||
| 152 | my @crises; | ||
| 153 | my @war_signal; | ||
| 154 | foreach my $b ($self->get_all_crises()) | ||
| 155 | { | ||
| 156 | if(( ($n && $b->involve($n) || ! $n)) && | ||
| 157 | ( $b->get_crisis_level > $level)) | ||
| 158 | { | ||
| 159 | push @crises, $b; | ||
| 160 | if($self->war_exists($b->node1, $b->node2)) | ||
| 161 | { | ||
| 162 | push @war_signal, 1; | ||
| 163 | } | ||
| 164 | else | ||
| 165 | { | ||
| 166 | push @war_signal, 0; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | } | ||
| 170 | my %nation_codes = reverse %{$self->nation_codes}; | ||
| 171 | return BalanceOfPower::Printer::print($mode, $self, 'print_all_crises', | ||
| 172 | { crises => \@crises, | ||
| 173 | wars => \@war_signal, | ||
| 174 | nation_codes => \%nation_codes, | ||
| 175 | }); | ||
| 176 | } | ||
| 177 | 1; | ||
| 178 |