| 1 | package BalanceOfPower::Role::Warlord; | ||
| 2 | $BalanceOfPower::Role::Warlord::VERSION = '0.300010'; | ||
| 3 | use strict; | ||
| 4 | use v5.10; | ||
| 5 | |||
| 6 | use Moo::Role; | ||
| 7 | |||
| 8 | use Term::ANSIColor; | ||
| 9 | |||
| 10 | use BalanceOfPower::Constants ':all'; | ||
| 11 | use BalanceOfPower::Printer; | ||
| 12 | use BalanceOfPower::Relations::Crisis; | ||
| 13 | use BalanceOfPower::Relations::War; | ||
| 14 | |||
| 15 | requires 'empire'; | ||
| 16 | requires 'border_exists'; | ||
| 17 | requires 'get_nation'; | ||
| 18 | requires 'get_hates'; | ||
| 19 | requires 'occupy'; | ||
| 20 | requires 'broadcast_event'; | ||
| 21 | requires 'send_event'; | ||
| 22 | requires 'empire'; | ||
| 23 | requires 'get_group_borders'; | ||
| 24 | requires 'get_allies'; | ||
| 25 | requires 'supported'; | ||
| 26 | requires 'supporter'; | ||
| 27 | requires 'military_support_garbage_collector'; | ||
| 28 | requires 'random'; | ||
| 29 | requires 'change_diplomacy'; | ||
| 30 | requires 'get_crises'; | ||
| 31 | requires 'delete_crisis'; | ||
| 32 | requires 'at_civil_war'; | ||
| 33 | requires 'discard_war_bonds'; | ||
| 34 | requires 'cash_war_bonds'; | ||
| 35 | |||
| 36 | has wars => ( | ||
| 37 | is => 'ro', | ||
| 38 | default => sub { BalanceOfPower::Relations::RelPack->new() }, | ||
| 39 | handles => { at_war => 'first_link_for_node', | ||
| 40 | add_war => 'add_link', | ||
| 41 | get_wars => 'links_for_node', | ||
| 42 | war_exists => 'exists_link', | ||
| 43 | delete_war_link => 'delete_link', | ||
| 44 | get_attackers => 'links_for_node2' | ||
| 45 | } | ||
| 46 | ); | ||
| 47 | |||
| 48 | has memorial => ( | ||
| 49 | is => 'rw', | ||
| 50 | default => sub { [] } | ||
| 51 | ); | ||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | sub war_busy | ||
| 57 | { | ||
| 58 | my $self = shift; | ||
| 59 | my $n = shift; | ||
| 60 | return $self->at_civil_war($n) || $self->at_war($n); | ||
| 61 | } | ||
| 62 | sub in_military_range | ||
| 63 | { | ||
| 64 | my $self = shift; | ||
| 65 | my $nation1 = shift; | ||
| 66 | my $nation2 = shift; | ||
| 67 | my $hostile = shift || 1; | ||
| 68 | if($self->border_exists($nation1, $nation2)) | ||
| 69 | { | ||
| 70 | return 1; | ||
| 71 | } | ||
| 72 | my @supported = $self->supporter($nation1); | ||
| 73 | for(@supported) | ||
| 74 | { | ||
| 75 | my $nation_supported = $_->destination($nation1); | ||
| 76 | my $treaty = $self->exists_treaty_by_type($nation_supported, $nation2, 'no aggression'); | ||
| 77 | if(! $hostile || ! $treaty) | ||
| 78 | { | ||
| 79 | if(! $self->war_busy($nation_supported)) | ||
| 80 | { | ||
| 81 | if($nation_supported eq $nation2) | ||
| 82 | { | ||
| 83 | return 1; | ||
| 84 | } | ||
| 85 | if($self->border_exists($nation_supported, $nation2)) | ||
| 86 | { | ||
| 87 | return 1; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | else | ||
| 91 | { | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | my @empire = $self->empire($nation1); | ||
| 96 | for(@empire) | ||
| 97 | { | ||
| 98 | my $ally = $_; | ||
| 99 | if(! $self->war_busy($ally)) | ||
| 100 | { | ||
| 101 | return 1 if $ally eq $nation2 || $self->border_exists($ally, $nation2); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | return 0; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub war_current_year | ||
| 108 | { | ||
| 109 | my $self = shift; | ||
| 110 | for($self->wars->all) | ||
| 111 | { | ||
| 112 | $_->current_year($self->current_year); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | sub create_war | ||
| 117 | { | ||
| 118 | my $self = shift; | ||
| 119 | my $attacker = shift || ""; | ||
| 120 | my $defender = shift || ""; | ||
| 121 | |||
| 122 | if(! $self->war_exists($attacker->name, $defender->name)) | ||
| 123 | { | ||
| 124 | $self->broadcast_event("CRISIS BETWEEN " . $attacker->name . " AND " . $defender->name . " BECAME WAR", $attacker->name, $defender->name); | ||
| 125 | my @attacker_coalition = $self->empire($attacker->name); | ||
| 126 | @attacker_coalition = grep { ! $self->at_war($_) } @attacker_coalition; | ||
| 127 | @attacker_coalition = grep { ! $self->at_civil_war($_) } @attacker_coalition; | ||
| 128 | my @defender_coalition = $self->empire($defender->name); | ||
| 129 | @defender_coalition = grep { ! $self->at_war($_) } @defender_coalition; | ||
| 130 | @defender_coalition = grep { ! $self->at_civil_war($_) } @defender_coalition; | ||
| 131 | |||
| 132 | #Allies management | ||
| 133 | my @attacker_allies = $self->get_allies($attacker->name); | ||
| 134 | my @defender_allies = $self->get_allies($defender->name); | ||
| 135 | for(@attacker_allies) | ||
| 136 | { | ||
| 137 | my $ally_name = $_->destination($attacker->name); | ||
| 138 | my $ally = $self->get_nation( $ally_name ); | ||
| 139 | if($ally->good_prey($defender, $self, ALLY_CONFLICT_LEVEL_FOR_INVOLVEMENT, 0 )) | ||
| 140 | { | ||
| 141 | if(! grep { $_ eq $ally_name } @attacker_coalition) | ||
| 142 | { | ||
| 143 | push @attacker_coalition, $ally_name; | ||
| 144 | $ally->register_event("JOIN WAR AS ALLY OF " . $attacker->name ." AGAINST " . $defender->name); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 | for(@defender_allies) | ||
| 149 | { | ||
| 150 | my $ally_name = $_->destination($defender->name); | ||
| 151 | my $ally = $self->get_nation( $ally_name ); | ||
| 152 | if($ally->good_prey($attacker, $self, ALLY_CONFLICT_LEVEL_FOR_INVOLVEMENT, 0 )) | ||
| 153 | { | ||
| 154 | if(! grep { $_ eq $ally_name } @defender_coalition) | ||
| 155 | { | ||
| 156 | push @defender_coalition, $ally_name; | ||
| 157 | $ally->register_event("JOIN WAR AS ALLY OF " . $defender->name ." AGAINST " . $attacker->name); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | my @attacker_targets = $self->get_group_borders(\@attacker_coalition, \@defender_coalition); | ||
| 163 | my @defender_targets = $self->get_group_borders(\@defender_coalition, \@attacker_coalition); | ||
| 164 | my @war_couples; | ||
| 165 | my @couples_factions; | ||
| 166 | my %used; | ||
| 167 | for(@attacker_coalition, @defender_coalition) | ||
| 168 | { | ||
| 169 | $used{$_} = 0; | ||
| 170 | } | ||
| 171 | #push @war_couples, [$attacker->name, $defender->name]; | ||
| 172 | $used{$attacker->name} = 1; | ||
| 173 | $used{$defender->name} = 1; | ||
| 174 | my $faction = 1; | ||
| 175 | my $done = 0; | ||
| 176 | my $faction0_done = 0; | ||
| 177 | my $faction1_done = 0; | ||
| 178 | while(! $done) | ||
| 179 | { | ||
| 180 | my @potential_attackers; | ||
| 181 | if($faction == 0) | ||
| 182 | { | ||
| 183 | @potential_attackers = grep { $used{$_} == 0 } @attacker_coalition; | ||
| 184 | } | ||
| 185 | elsif($faction == 1) | ||
| 186 | { | ||
| 187 | @potential_attackers = grep { $used{$_} == 0 } @defender_coalition; | ||
| 188 | } | ||
| 189 | if(@potential_attackers == 0) | ||
| 190 | { | ||
| 191 | if($faction0_done == 1 && $faction == 1 || | ||
| 192 | $faction1_done == 1 && $faction == 0) | ||
| 193 | { | ||
| 194 | $done = 1; | ||
| 195 | last; | ||
| 196 | } | ||
| 197 | else | ||
| 198 | { | ||
| 199 | if($faction == 0) | ||
| 200 | { | ||
| 201 | $faction0_done = 1; | ||
| 202 | $faction = 1; | ||
| 203 | } | ||
| 204 | else | ||
| 205 | { | ||
| 206 | $faction1_done = 1; | ||
| 207 | $faction = 0; | ||
| 208 | } | ||
| 209 | next; | ||
| 210 | } | ||
| 211 | |||
| 212 | } | ||
| 213 | @potential_attackers = $self->shuffle("War creation. Choosing attackers", @potential_attackers); | ||
| 214 | my $attack_now = $potential_attackers[0]; | ||
| 215 | my $defend_now = undef; | ||
| 216 | my $free_level = 0; | ||
| 217 | my $searching = 1; | ||
| 218 | while($searching) | ||
| 219 | { | ||
| 220 | my @potential_defenders; | ||
| 221 | if($faction == 0) | ||
| 222 | { | ||
| 223 | @potential_defenders = grep { ! $self->exists_treaty_by_type($_, $attack_now, 'no aggression') } @defender_coalition; | ||
| 224 | if(@potential_defenders == 0) | ||
| 225 | { | ||
| 226 | @attacker_coalition = grep { ! $attack_now eq $_ } @attacker_coalition; | ||
| 227 | $self->broadcast_event("NO POSSIBILITY TO PARTECIPATE TO WAR LINKED TO WAR BETWEEN " . $attacker->name . " AND " .$defender->name . " FOR $attack_now", $attack_now); | ||
| 228 | last; | ||
| 229 | } | ||
| 230 | } | ||
| 231 | elsif($faction == 1) | ||
| 232 | { | ||
| 233 | @potential_defenders = grep { ! $self->exists_treaty_by_type($_, $attack_now, 'no aggression') } @attacker_coalition; | ||
| 234 | if(@potential_defenders == 0) | ||
| 235 | { | ||
| 236 | @defender_coalition = grep { ! $attack_now eq $_ } @defender_coalition; | ||
| 237 | $self->broadcast_event("NO POSSIBILITY TO PARTECIPATE TO WAR LINKED TO WAR BETWEEN " . $attacker->name . " AND " .$defender->name . " FOR $attack_now", $attack_now); | ||
| 238 | last; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | @potential_defenders = grep { $used{$_} <= $free_level } @potential_defenders; | ||
| 242 | if(@potential_defenders > 0) | ||
| 243 | { | ||
| 244 | @potential_defenders = $self->shuffle("War creation. Choosing defenders", @potential_defenders); | ||
| 245 | $defend_now = $potential_defenders[0]; | ||
| 246 | $searching = 0; | ||
| 247 | } | ||
| 248 | else | ||
| 249 | { | ||
| 250 | $free_level++; | ||
| 251 | } | ||
| 252 | } | ||
| 253 | if($defend_now) | ||
| 254 | { | ||
| 255 | push @war_couples, [$attack_now, $defend_now]; | ||
| 256 | push @couples_factions, $faction; | ||
| 257 | $used{$defend_now} += 1; | ||
| 258 | } | ||
| 259 | $used{$attack_now} += 1; | ||
| 260 | if($faction == 0) | ||
| 261 | { | ||
| 262 | $faction = 1; | ||
| 263 | } | ||
| 264 | else | ||
| 265 | { | ||
| 266 | $faction = 0; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | my %attacker_leaders; | ||
| 270 | my $war_id = time; | ||
| 271 | my $war = BalanceOfPower::Relations::War->new(node1 => $attacker->name, | ||
| 272 | node2 => $defender->name, | ||
| 273 | attack_leader => $attacker->name, | ||
| 274 | war_id => $war_id, | ||
| 275 | node1_faction => 0, | ||
| 276 | node2_faction => 1, | ||
| 277 | start_date => $self->current_year, | ||
| 278 | log_active => 0, | ||
| 279 | ); | ||
| 280 | $war = $self->war_starting_report($war); | ||
| 281 | $self->add_war($war); | ||
| 282 | $attacker_leaders{$defender->name} = $attacker->name; | ||
| 283 | $self->broadcast_event("WAR BETWEEN " . $attacker->name . " AND " .$defender->name . " STARTED", $attacker->name, $defender->name); | ||
| 284 | my $faction_counter = 0; | ||
| 285 | foreach my $c (@war_couples) | ||
| 286 | { | ||
| 287 | my $leader; | ||
| 288 | if(exists $attacker_leaders{$c->[1]}) | ||
| 289 | { | ||
| 290 | $leader = $attacker_leaders{$c->[1]} | ||
| 291 | } | ||
| 292 | else | ||
| 293 | { | ||
| 294 | $leader = $c->[0]; | ||
| 295 | $attacker_leaders{$c->[1]} = $c->[0]; | ||
| 296 | } | ||
| 297 | my $faction1; | ||
| 298 | my $faction2; | ||
| 299 | if($couples_factions[$faction_counter] == 0) | ||
| 300 | { | ||
| 301 | $faction1 = 0; | ||
| 302 | $faction2 = 1; | ||
| 303 | } | ||
| 304 | else | ||
| 305 | { | ||
| 306 | $faction1 = 1; | ||
| 307 | $faction2 = 0; | ||
| 308 | } | ||
| 309 | my $node1 = $c->[0]; | ||
| 310 | my $node2 = $c->[1]; | ||
| 311 | my $war = BalanceOfPower::Relations::War->new(node1 => $node1, | ||
| 312 | node2 => $node2, | ||
| 313 | attack_leader => $leader, | ||
| 314 | war_id => $war_id, | ||
| 315 | node1_faction => $faction1, | ||
| 316 | node2_faction => $faction2, | ||
| 317 | start_date => $self->current_year, | ||
| 318 | log_active => 0); | ||
| 319 | $war = $self->war_starting_report($war); | ||
| 320 | $self->add_war($war); | ||
| 321 | $self->broadcast_event("WAR BETWEEN " . $node1 . " AND " . $node2 . " STARTED (LINKED TO WAR BETWEEN " . $attacker->name . " AND " .$defender->name . ")", $node1, $node2); | ||
| 322 | } | ||
| 323 | } | ||
| 324 | } | ||
| 325 | sub war_starting_report | ||
| 326 | { | ||
| 327 | my $self = shift; | ||
| 328 | my $war = shift; | ||
| 329 | my $node1 = $war->node1; | ||
| 330 | my $node2 = $war->node2; | ||
| 331 | $war->register_event("Starting army for " . $node1 . ": " . $self->get_nation($node1)->army); | ||
| 332 | $war->register_event("Progress of " . $node1 . ": " . $self->get_nation($node1)->progress); | ||
| 333 | my $sup1 = $self->supported($node1); | ||
| 334 | if($sup1) | ||
| 335 | { | ||
| 336 | $war->register_event("$node1 is supported by " . $sup1->node1 . ": " . $sup1->army); | ||
| 337 | } | ||
| 338 | $war->register_event("Starting army for " . $node2 . ": " . $self->get_nation($node2)->army); | ||
| 339 | $war->register_event("Progress of " . $node2 . ": " . $self->get_nation($node2)->progress); | ||
| 340 | my $sup2 = $self->supported($node2); | ||
| 341 | if($sup2) | ||
| 342 | { | ||
| 343 | $war->register_event("$node2 is supported by " . $sup2->node1 . ": " . $sup2->army); | ||
| 344 | } | ||
| 345 | return $war; | ||
| 346 | } | ||
| 347 | |||
| 348 | sub army_for_war | ||
| 349 | { | ||
| 350 | my $self = shift; | ||
| 351 | my $nation = shift; | ||
| 352 | my $supported = $self->supported($nation->name); | ||
| 353 | my $army = $nation->army; | ||
| 354 | if($supported) | ||
| 355 | { | ||
| 356 | $army += $supported->army; | ||
| 357 | } | ||
| 358 | return $army; | ||
| 359 | } | ||
| 360 | |||
| 361 | sub damage_from_battle | ||
| 362 | { | ||
| 363 | my $self = shift; | ||
| 364 | my $nation = shift; | ||
| 365 | my $damage = shift; | ||
| 366 | my $attacker = shift; | ||
| 367 | my $supported = $self->supported($nation->name); | ||
| 368 | if($supported && $self->exists_treaty_by_type($attacker->name, $supported->node1, 'no aggression')) | ||
| 369 | { | ||
| 370 | $supported = undef; | ||
| 371 | } | ||
| 372 | my $flip = 0; | ||
| 373 | my $army_damage = 0; | ||
| 374 | while($damage > 0) | ||
| 375 | { | ||
| 376 | if($flip == 0) | ||
| 377 | { | ||
| 378 | if($supported && $supported->army > 0) | ||
| 379 | { | ||
| 380 | $supported->casualities(1); | ||
| 381 | $damage--; | ||
| 382 | } | ||
| 383 | $flip = 1; | ||
| 384 | } | ||
| 385 | else | ||
| 386 | { | ||
| 387 | $army_damage++; | ||
| 388 | $damage--; | ||
| 389 | $flip = 0; | ||
| 390 | } | ||
| 391 | } | ||
| 392 | $nation->add_army(-1 * $army_damage); | ||
| 393 | if($supported) | ||
| 394 | { | ||
| 395 | if($supported->army <= 0) | ||
| 396 | { | ||
| 397 | $self->broadcast_event("MILITARY SUPPORT TO " . $supported->node2 . " BY " . $supported->node1 . " DESTROYED", $supported->node1, $supported->node2); | ||
| 398 | $self->war_report("Military support to ". $supported->node2 . " by " . $supported->node1 . " destroyed", $supported->node2); | ||
| 399 | } | ||
| 400 | } | ||
| 401 | $self->military_support_garbage_collector(); | ||
| 402 | } | ||
| 403 | |||
| 404 | sub fight_wars | ||
| 405 | { | ||
| 406 | my $self = shift; | ||
| 407 | my %losers; | ||
| 408 | my %war_bonds_issued = (); | ||
| 409 | foreach my $w ($self->wars->all()) | ||
| 410 | { | ||
| 411 | if(! exists $war_bonds_issued{$w->node1}) | ||
| 412 | { | ||
| 413 | $self->issue_war_bonds($w->node1); | ||
| 414 | $war_bonds_issued{$w->node1} = 1; | ||
| 415 | } | ||
| 416 | if(! exists $war_bonds_issued{$w->node2}) | ||
| 417 | { | ||
| 418 | $self->issue_war_bonds($w->node2); | ||
| 419 | $war_bonds_issued{$w->node2} = 1; | ||
| 420 | } | ||
| 421 | |||
| 422 | #As Risiko | ||
| 423 | $self->broadcast_event("WAR BETWEEN " . $w->node1 . " AND " . $w->node2 . " GO ON", $w->node1, $w->node2); | ||
| 424 | my $attacker = $self->get_nation($w->node1); | ||
| 425 | my $defender = $self->get_nation($w->node2); | ||
| 426 | my $attacker_army = $self->army_for_war($attacker); | ||
| 427 | my $defender_army = $self->army_for_war($defender); | ||
| 428 | my $attack = $attacker_army >= ARMY_FOR_BATTLE ? ARMY_FOR_BATTLE : $attacker_army; | ||
| 429 | my $defence = $defender_army >= ARMY_FOR_BATTLE ? ARMY_FOR_BATTLE : $defender_army; | ||
| 430 | my $attacker_damage = 0; | ||
| 431 | my $defender_damage = 0; | ||
| 432 | my $counter = $attack < $defence ? $attack : $defence; | ||
| 433 | my $progress_delta = $attacker->progress - $defender->progress; | ||
| 434 | my $attacker_progress_bonus; | ||
| 435 | my $defender_progress_bonus; | ||
| 436 | if($progress_delta > 0) | ||
| 437 | { | ||
| 438 | $attacker_progress_bonus = $progress_delta * PROGRESS_BATTLE_FACTOR; | ||
| 439 | $defender_progress_bonus = 0; | ||
| 440 | } | ||
| 441 | else | ||
| 442 | { | ||
| 443 | $attacker_progress_bonus = 0; | ||
| 444 | $defender_progress_bonus = $progress_delta * PROGRESS_BATTLE_FACTOR * -1; | ||
| 445 | } | ||
| 446 | for(my $i = 0; $i < $counter; $i++) | ||
| 447 | { | ||
| 448 | my $att = int(($self->random(1, 60, "War risiko: throw for attacker " . $attacker->name) + $attacker_progress_bonus) / 10) + 1; | ||
| 449 | my $def = int(($self->random(1, 60, "War risiko: throw for defender " . $defender->name) + $defender_progress_bonus) / 10) + 1; | ||
| 450 | |||
| 451 | if($att > $def) | ||
| 452 | { | ||
| 453 | $defender_damage++; | ||
| 454 | } | ||
| 455 | else | ||
| 456 | { | ||
| 457 | $attacker_damage++; | ||
| 458 | } | ||
| 459 | } | ||
| 460 | if(my $sup = $self->supported($attacker->name)) | ||
| 461 | { | ||
| 462 | my $supporter_n = $sup->start($attacker->name); | ||
| 463 | if(! $self->exists_treaty_by_type($defender->name, $supporter_n, 'no aggression')) | ||
| 464 | { | ||
| 465 | $self->broadcast_event("RELATIONS BETWEEN " . $defender->name . " AND " . $supporter_n . " CHANGED FOR WAR WITH " . $attacker->name, $attacker->name, $defender->name, $supporter_n); | ||
| 466 | $self->change_diplomacy($defender->name, $supporter_n, -1 * DIPLOMACY_MALUS_FOR_SUPPORT); | ||
| 467 | } | ||
| 468 | } | ||
| 469 | if(my $sup = $self->supported($defender->name)) | ||
| 470 | { | ||
| 471 | my $supporter_n = $sup->start($defender->name); | ||
| 472 | if(! $self->exists_treaty_by_type($attacker->name, $supporter_n, 'no aggression')) | ||
| 473 | { | ||
| 474 | $self->broadcast_event("RELATIONS BETWEEN " . $attacker->name . " AND " . $supporter_n . " CHANGED FOR WAR WITH " . $defender->name, $attacker->name, $defender->name, $supporter_n); | ||
| 475 | $self->change_diplomacy($attacker->name, $supporter_n, -1 * DIPLOMACY_MALUS_FOR_SUPPORT); | ||
| 476 | } | ||
| 477 | } | ||
| 478 | |||
| 479 | $self->damage_from_battle($attacker, $attacker_damage, $defender); | ||
| 480 | $self->damage_from_battle($defender, $defender_damage, $attacker); | ||
| 481 | $attacker->register_event("CASUALITIES IN WAR WITH " . $defender->name . ": $attacker_damage"); | ||
| 482 | $defender->register_event("CASUALITIES IN WAR WITH " . $attacker->name . ": $defender_damage"); | ||
| 483 | if($attacker->army == 0) | ||
| 484 | { | ||
| 485 | $losers{$attacker->name} = 1; | ||
| 486 | } | ||
| 487 | elsif($defender->army == 0) | ||
| 488 | { | ||
| 489 | $losers{$defender->name} = 1; | ||
| 490 | } | ||
| 491 | } | ||
| 492 | for(keys %losers) | ||
| 493 | { | ||
| 494 | $self->lose_war($_); | ||
| 495 | } | ||
| 496 | } | ||
| 497 | |||
| 498 | sub lose_war | ||
| 499 | { | ||
| 500 | my $self = shift; | ||
| 501 | my $loser = shift; | ||
| 502 | my $internal_disorder = shift; | ||
| 503 | $internal_disorder ||= 0; | ||
| 504 | my @wars = $self->get_wars($loser); | ||
| 505 | my $retreat_penality = 0; | ||
| 506 | my @conquerors = (); | ||
| 507 | my $conquerors_leader = ""; | ||
| 508 | foreach my $w (@wars) | ||
| 509 | { | ||
| 510 | my $other; | ||
| 511 | my $winner_role; | ||
| 512 | if($w->node1 eq $loser) | ||
| 513 | { | ||
| 514 | #Loser is the attacker | ||
| 515 | $retreat_penality = 1; | ||
| 516 | $other = $w->node2; | ||
| 517 | $winner_role = "[DEFENDER]"; | ||
| 518 | $self->send_event("RETREAT FROM " . $other, $loser); | ||
| 519 | } | ||
| 520 | elsif($w->node2 eq $loser) | ||
| 521 | { | ||
| 522 | #Loser is the defender | ||
| 523 | $other = $w->node1; | ||
| 524 | push @conquerors, $w->node1; | ||
| 525 | $self->delete_crisis($loser, $other); | ||
| 526 | $conquerors_leader = $w->attack_leader; | ||
| 527 | $winner_role = "[ATTACKER]"; | ||
| 528 | $self->get_nation($loser)->internal_disorder(AFTER_CONQUERED_INTERNAL_DISORDER); | ||
| 529 | } | ||
| 530 | my $ending_line = "WAR BETWEEN $other AND $loser WON BY $other $winner_role"; | ||
| 531 | |||
| 532 | $self->broadcast_event($ending_line, $other, $loser); | ||
| 533 | my $history_line = ""; | ||
| 534 | $self->cash_war_bonds($other); | ||
| 535 | $self->discard_war_bonds($loser); | ||
| 536 | $history_line .= "$other $winner_role won the war"; | ||
| 537 | $self->delete_war($other, $loser, $history_line); | ||
| 538 | } | ||
| 539 | if(@conquerors > 0) | ||
| 540 | { | ||
| 541 | $self->occupy($loser, \@conquerors, $conquerors_leader, $internal_disorder); | ||
| 542 | } | ||
| 543 | } | ||
| 544 | |||
| 545 | sub delete_war | ||
| 546 | { | ||
| 547 | my $self = shift; | ||
| 548 | my $nation1 = shift; | ||
| 549 | my $nation2 = shift; | ||
| 550 | my $ending_line = shift; | ||
| 551 | my $war = $self->war_exists($nation1, $nation2); | ||
| 552 | $war->end_date($self->current_year); | ||
| 553 | $war->register_event($ending_line); | ||
| 554 | push @{$self->memorial}, $war; | ||
| 555 | $self->delete_war_link($nation1, $nation2); | ||
| 556 | } | ||
| 557 | |||
| 558 | |||
| 559 | sub print_wars | ||
| 560 | { | ||
| 561 | my $self = shift; | ||
| 562 | my $nation = shift; | ||
| 563 | my $mode = shift || 'print'; | ||
| 564 | my %grouped_wars; | ||
| 565 | foreach my $w ($self->wars->all()) | ||
| 566 | { | ||
| 567 | if(! exists $grouped_wars{$w->war_id}) | ||
| 568 | { | ||
| 569 | $grouped_wars{$w->war_id} = []; | ||
| 570 | } | ||
| 571 | push @{$grouped_wars{$w->war_id}}, $w; | ||
| 572 | } | ||
| 573 | my @wars; | ||
| 574 | foreach my $k (keys %grouped_wars) | ||
| 575 | { | ||
| 576 | my %war; | ||
| 577 | $war{'name'} = $k; | ||
| 578 | $war{'conflicts'} = []; | ||
| 579 | foreach my $w ( @{$grouped_wars{$k}}) | ||
| 580 | { | ||
| 581 | my %subwar; | ||
| 582 | $subwar{'node1'} = $w->node1; | ||
| 583 | $subwar{'node2'} = $w->node2; | ||
| 584 | my $nation1 = $self->get_nation($w->node1); | ||
| 585 | my $nation2 = $self->get_nation($w->node2); | ||
| 586 | $subwar{'army1'} = $nation1->army; | ||
| 587 | $subwar{'army2'} = $nation2->army; | ||
| 588 | $subwar{'node1_faction'} = $w->node1_faction; | ||
| 589 | $subwar{'node2_faction'} = $w->node2_faction; | ||
| 590 | push @{$war{'conflicts'}}, \%subwar; | ||
| 591 | } | ||
| 592 | push @wars, \%war; | ||
| 593 | } | ||
| 594 | my @civil_wars; | ||
| 595 | foreach my $n (@{$self->nation_names}) | ||
| 596 | { | ||
| 597 | if($self->at_civil_war($n)) | ||
| 598 | { | ||
| 599 | push @civil_wars, $n; | ||
| 600 | } | ||
| 601 | } | ||
| 602 | return BalanceOfPower::Printer::print($mode, $self, 'print_wars', | ||
| 603 | { wars => \@wars, | ||
| 604 | civil_wars => \@civil_wars } ); | ||
| 605 | } | ||
| 606 | sub dump_memorial | ||
| 607 | { | ||
| 608 | my $self = shift; | ||
| 609 | my $io = shift; | ||
| 610 | my $indent = shift; | ||
| 611 | foreach my $w (@{$self->memorial}) | ||
| 612 | { | ||
| 613 | print {$io} $w->dump($io, $indent); | ||
| 614 | } | ||
| 615 | } | ||
| 616 | sub load_memorial | ||
| 617 | { | ||
| 618 | my $self = shift; | ||
| 619 | my $data = shift; | ||
| 620 | my $war_data = ""; | ||
| 621 | my @memorial; | ||
| 622 | my @lines = split "\n", $data; | ||
| 623 | foreach my $l (@lines) | ||
| 624 | { | ||
| 625 | if($l !~ /^\s/) | ||
| 626 | { | ||
| 627 | if($war_data) | ||
| 628 | { | ||
| 629 | push @memorial, BalanceOfPower::Relations::War->load($war_data); | ||
| 630 | $war_data = $l . "\n"; | ||
| 631 | } | ||
| 632 | else | ||
| 633 | { | ||
| 634 | $war_data = $l . "\n"; | ||
| 635 | } | ||
| 636 | } | ||
| 637 | else | ||
| 638 | { | ||
| 639 | $war_data .= $l . "\n"; | ||
| 640 | } | ||
| 641 | } | ||
| 642 | return \@memorial; | ||
| 643 | } | ||
| 644 | |||
| 645 | |||
| 646 | 1; |