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