« blib/lib/BalanceOfPower/Commands/RebelMilitarySupport.pm
↑ Summary
blib/lib/BalanceOfPower/Relations/TradeRoute.pm »
| 1 | package BalanceOfPower::Role::Diplomat; | ||
| 2 | $BalanceOfPower::Role::Diplomat::VERSION = '0.200000'; | ||
| 3 | use strict; | ||
| 4 | use v5.10; | ||
| 5 | use Moo::Role; | ||
| 6 | |||
| 7 | use BalanceOfPower::Constants ':all'; | ||
| 8 | |||
| 9 | use BalanceOfPower::Relations::Friendship; | ||
| 10 | use BalanceOfPower::Relations::Treaty; | ||
| 11 | use BalanceOfPower::Relations::RelPack; | ||
| 12 | |||
| 13 | has diplomatic_relations => ( | ||
| 14 | is => 'ro', | ||
| 15 | default => sub { BalanceOfPower::Relations::RelPack->new() }, | ||
| 16 | handles => { add_diplomacy => 'add_link', | ||
| 17 | _diplomacy_exists => 'exists_link', | ||
| 18 | update_diplomacy => 'update_link', | ||
| 19 | get_diplomatic_relations => 'links_for_node' } | ||
| 20 | ); | ||
| 21 | has treaties => ( | ||
| 22 | is => 'ro', | ||
| 23 | default => sub { BalanceOfPower::Relations::RelPack->new() }, | ||
| 24 | handles => { add_treaty => 'add_link', | ||
| 25 | print_treaties => 'print_links', | ||
| 26 | exists_treaty => 'exists_link', | ||
| 27 | get_treaties_for_nation => 'links_for_node', | ||
| 28 | reset_treaties => 'delete_link_for_node', | ||
| 29 | delete_treaty => 'delete_link', } | ||
| 30 | ); | ||
| 31 | |||
| 32 | requires 'random'; | ||
| 33 | requires 'distance'; | ||
| 34 | requires 'border_exists'; | ||
| 35 | requires 'broadcast_event'; | ||
| 36 | requires 'is_under_influence'; | ||
| 37 | |||
| 38 | sub init_diplomacy | ||
| 39 | { | ||
| 40 | my $self = shift; | ||
| 41 | my @nations = @{$self->nation_names}; | ||
| 42 | foreach my $n1 (@nations) | ||
| 43 | { | ||
| 44 | foreach my $n2 (@nations) | ||
| 45 | { | ||
| 46 | if($n1 ne $n2 && ! $self->_diplomacy_exists($n1, $n2)) | ||
| 47 | { | ||
| 48 | my $minimum_friendship = 0; | ||
| 49 | my $rel = BalanceOfPower::Relations::Friendship->new( node1 => $n1, | ||
| 50 | node2 => $n2, | ||
| 51 | factor => $self->calculate_random_friendship($n1, $n2)); | ||
| 52 | $self->add_diplomacy($rel); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | #Random friendship is function of the distance | ||
| 59 | sub calculate_random_friendship | ||
| 60 | { | ||
| 61 | my $self = shift; | ||
| 62 | my $nation1 = shift; | ||
| 63 | my $nation2 = shift; | ||
| 64 | my $distance = $self->distance($nation1, $nation2); | ||
| 65 | $distance = 3 if $distance > 3; | ||
| 66 | |||
| 67 | my $middle = 50; | ||
| 68 | |||
| 69 | my $polar_factor = ( 4 - $distance ) * 5; | ||
| 70 | my $random_floor = ( ( 3 - $distance ) * 5 ) + 25; | ||
| 71 | |||
| 72 | my $side = $self->random(0, 1, "Side for friendship between $nation1 and $nation2"); | ||
| 73 | $side = $side == 0 ? -1 : 1; | ||
| 74 | my $random_factor = $self->random(0, $random_floor, "Random factor for friendship between $nation1 and $nation2 [floor: $random_floor]"); | ||
| 75 | |||
| 76 | my $friendship = $middle + ( $side * ( $polar_factor + $random_factor ) ); | ||
| 77 | return $friendship; | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | sub init_random_alliances | ||
| 82 | { | ||
| 83 | my $self = shift; | ||
| 84 | my @nations = @{$self->nation_names}; | ||
| 85 | for(my $i = 0; $i < STARTING_ALLIANCES; $i++) | ||
| 86 | { | ||
| 87 | my $n1 = $nations[$self->random(0, $#nations, "Nation1 for random alliance")]; | ||
| 88 | my $n2 = $nations[$self->random(0, $#nations, "Nation2 for random alliance")]; | ||
| 89 | if($n1 ne $n2) | ||
| 90 | { | ||
| 91 | $self->add_alliance($n1, $n2); | ||
| 92 | $self->broadcast_event("ALLIANCE BETWEEN $n1 AND $n2 CREATED", $n1, $n2); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | } | ||
| 96 | sub reroll_diplomacy | ||
| 97 | { | ||
| 98 | my $self = shift; | ||
| 99 | my $nation = shift; | ||
| 100 | my @rels = $self->get_diplomatic_relations($nation); | ||
| 101 | for(@rels) | ||
| 102 | { | ||
| 103 | $_->factor($self->random(0 ,100, "Reroll diplomacy for " . $_->node1 . ", " . $_->node2)); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | sub diplomacy_exists | ||
| 108 | { | ||
| 109 | my $self = shift; | ||
| 110 | my $n1 = shift; | ||
| 111 | my $n2 = shift; | ||
| 112 | my $r = $self->_diplomacy_exists($n1, $n2); | ||
| 113 | if(! defined $r) | ||
| 114 | { | ||
| 115 | say "ERROR! $n1, $n2"; | ||
| 116 | } | ||
| 117 | return $r; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub get_hates | ||
| 121 | { | ||
| 122 | my $self = shift; | ||
| 123 | my $nation = shift; | ||
| 124 | my @hates = $self->diplomatic_relations->query( sub { my $rel = shift; return $rel->status eq 'HATE' }); | ||
| 125 | my @out = (); | ||
| 126 | foreach my $r (@hates) | ||
| 127 | { | ||
| 128 | if(($nation && $r->has_node($nation)) || (! $nation)) | ||
| 129 | { | ||
| 130 | if(! $self->is_under_influence($r->node1) && ! $self->is_under_influence($r->node2)) | ||
| 131 | { | ||
| 132 | push @out, $r; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | } | ||
| 136 | return @out; | ||
| 137 | } | ||
| 138 | sub get_nations_with_status | ||
| 139 | { | ||
| 140 | my $self = shift; | ||
| 141 | my $nation = shift; | ||
| 142 | my $status = shift; | ||
| 143 | my @st_array = @{$status}; | ||
| 144 | my @relations = $self->get_diplomatic_relations($nation); | ||
| 145 | my @out = (); | ||
| 146 | for(@relations) | ||
| 147 | { | ||
| 148 | my $r = $_; | ||
| 149 | if(grep{ $_ eq $r->status } @st_array) | ||
| 150 | { | ||
| 151 | push @out, $r->destination($nation); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | return @out; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub get_friends | ||
| 158 | { | ||
| 159 | my $self = shift; | ||
| 160 | my $nation = shift; | ||
| 161 | return $self->get_nations_with_status($nation, ['FRIENDSHIP', 'ALLIANCE', 'INFLUENCE PRESENT']); | ||
| 162 | } | ||
| 163 | sub set_diplomacy | ||
| 164 | { | ||
| 165 | my $self = shift; | ||
| 166 | my $node1 = shift; | ||
| 167 | my $node2 = shift; | ||
| 168 | my $new_factor = shift; | ||
| 169 | my $r = $self->diplomacy_exists($node1, $node2); | ||
| 170 | return undef if(!$r ); #Should never happen | ||
| 171 | $r->factor($new_factor); | ||
| 172 | return $r; | ||
| 173 | } | ||
| 174 | sub copy_diplomacy | ||
| 175 | { | ||
| 176 | my $self = shift; | ||
| 177 | my $nation_from = shift; | ||
| 178 | my $nation_to = shift; | ||
| 179 | my @relations = $self->get_diplomatic_relations($nation_from); | ||
| 180 | for(@relations) | ||
| 181 | { | ||
| 182 | my $r = $_; | ||
| 183 | my $other = $r->destination($nation_from); | ||
| 184 | if($other ne $nation_to) | ||
| 185 | { | ||
| 186 | $self->set_diplomacy($nation_to, $other, $r->factor); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | |||
| 191 | } | ||
| 192 | |||
| 193 | sub change_diplomacy | ||
| 194 | { | ||
| 195 | my $self = shift; | ||
| 196 | my $node1 = shift; | ||
| 197 | my $node2 = shift; | ||
| 198 | my $dipl = shift; | ||
| 199 | my $r = $self->diplomacy_exists($node1, $node2); | ||
| 200 | return if(!$r ); #Should never happen | ||
| 201 | return if $r->status eq 'ALLIANCE' || $r->status eq 'INFLUENCE PRESENT'; | ||
| 202 | my $present_status = $r->status; | ||
| 203 | $r->change_factor($dipl); | ||
| 204 | my $actual_status = $r->status; | ||
| 205 | if($present_status ne $actual_status) | ||
| 206 | { | ||
| 207 | $self->broadcast_event("RELATION BETWEEN $node1 AND $node2 CHANGED FROM $present_status TO $actual_status", $node1, $node2); | ||
| 208 | if($actual_status eq 'HATE') | ||
| 209 | { | ||
| 210 | $self->diplomatic_breakdown($node1, $node2); | ||
| 211 | } | ||
| 212 | } | ||
| 213 | } | ||
| 214 | sub diplomacy_status | ||
| 215 | { | ||
| 216 | my $self = shift; | ||
| 217 | my $n1 = shift; | ||
| 218 | my $n2 = shift; | ||
| 219 | my $r = $self->diplomacy_exists($n1, $n2); | ||
| 220 | return $r->status; | ||
| 221 | } | ||
| 222 | |||
| 223 | sub diplomatic_breakdown | ||
| 224 | { | ||
| 225 | my $self = shift; | ||
| 226 | my $n1 = shift; | ||
| 227 | my $n2 = shift; | ||
| 228 | my $treaty = $self->exists_treaty($n1, $n2); | ||
| 229 | if($treaty) | ||
| 230 | { | ||
| 231 | $self->broadcast_event($treaty->short_tag . " TREATY BETWEEN $n1 AND $n2 BROKEN", $n1, $n2); | ||
| 232 | } | ||
| 233 | $self->stop_military_support($self->get_nation($n1), $self->get_nation($n2), 1); | ||
| 234 | $self->stop_military_support($self->get_nation($n2), $self->get_nation($n1), 1); | ||
| 235 | } | ||
| 236 | |||
| 237 | sub diplomacy_for_node | ||
| 238 | { | ||
| 239 | my $self = shift; | ||
| 240 | my $node = shift; | ||
| 241 | my %relations; | ||
| 242 | foreach my $n (@{$self->nation_names}) | ||
| 243 | { | ||
| 244 | if($n ne $node) | ||
| 245 | { | ||
| 246 | my $real_r = $self->diplomacy_exists($node, $n); | ||
| 247 | $relations{$n} = $real_r->factor; | ||
| 248 | } | ||
| 249 | } | ||
| 250 | return %relations;; | ||
| 251 | } | ||
| 252 | sub print_diplomacy | ||
| 253 | { | ||
| 254 | my $self = shift; | ||
| 255 | my $n = shift; | ||
| 256 | my $out; | ||
| 257 | my @outnodes; | ||
| 258 | foreach my $f ($self->diplomatic_relations->all()) | ||
| 259 | { | ||
| 260 | if($f->has_node($n)) | ||
| 261 | { | ||
| 262 | my $real_r = $self->diplomacy_exists($n, $f->destination($n)); | ||
| 263 | push @outnodes, $real_r; | ||
| 264 | } | ||
| 265 | } | ||
| 266 | foreach my $rr (sort { $a->factor <=> $b->factor} @outnodes) | ||
| 267 | { | ||
| 268 | $out .= $rr->print($n) . "\n"; | ||
| 269 | } | ||
| 270 | return $out; | ||
| 271 | } | ||
| 272 | |||
| 273 | |||
| 274 | sub diplomatic_pressure | ||
| 275 | { | ||
| 276 | my $self = shift; | ||
| 277 | my $nation1 = shift; | ||
| 278 | my $nation2 = shift; | ||
| 279 | my @friends = $self->get_friends($nation1); | ||
| 280 | $self->change_diplomacy($nation1, $nation2, DIPLOMATIC_PRESSURE_FACTOR); | ||
| 281 | $self->broadcast_event("DIPLOMATIC PRESSURE OF $nation1 ON $nation2", $nation1, $nation2); | ||
| 282 | for(@friends) | ||
| 283 | { | ||
| 284 | my $f = $_; | ||
| 285 | $self->change_diplomacy($f, $nation2, DIPLOMATIC_PRESSURE_FACTOR); | ||
| 286 | } | ||
| 287 | } | ||
| 288 | |||
| 289 | |||
| 290 | #Functions to manage relationships as crises | ||
| 291 | sub add_crisis | ||
| 292 | { | ||
| 293 | my $self = shift; | ||
| 294 | my $nation1 = shift; | ||
| 295 | my $nation2 = shift; | ||
| 296 | my $rel = $self->diplomacy_exists($nation1, $nation2); | ||
| 297 | if($rel->get_crisis_level == 0) | ||
| 298 | { | ||
| 299 | $rel->escalate_crisis(); | ||
| 300 | } | ||
| 301 | } | ||
| 302 | sub delete_crisis | ||
| 303 | { | ||
| 304 | my $self = shift; | ||
| 305 | my $nation1 = shift; | ||
| 306 | my $nation2 = shift; | ||
| 307 | my $rel = $self->diplomacy_exists($nation1, $nation2); | ||
| 308 | $rel->crisis_level(0); | ||
| 309 | } | ||
| 310 | sub crisis_exists | ||
| 311 | { | ||
| 312 | my $self = shift; | ||
| 313 | my $nation1 = shift || ""; | ||
| 314 | my $nation2 = shift || ""; | ||
| 315 | my $rel = $self->diplomacy_exists($nation1, $nation2); | ||
| 316 | if(! $rel) | ||
| 317 | { | ||
| 318 | say "ERROR: $nation1 <-> $nation2"; | ||
| 319 | return undef; | ||
| 320 | } | ||
| 321 | if($rel->get_crisis_level > 0) | ||
| 322 | { | ||
| 323 | return $rel; | ||
| 324 | } | ||
| 325 | else | ||
| 326 | { | ||
| 327 | return undef; | ||
| 328 | } | ||
| 329 | } | ||
| 330 | sub get_crises | ||
| 331 | { | ||
| 332 | my $self = shift; | ||
| 333 | my $nation = shift; | ||
| 334 | my @crises = $self->get_diplomatic_relations($nation); | ||
| 335 | @crises = grep { $_->get_crisis_level > 0 } @crises; | ||
| 336 | return @crises; | ||
| 337 | } | ||
| 338 | sub get_all_crises | ||
| 339 | { | ||
| 340 | my $self = shift; | ||
| 341 | my @rels = $self->diplomatic_relations->all(); | ||
| 342 | return grep { $_->is_crisis() } @rels; | ||
| 343 | } | ||
| 344 | sub reset_crises | ||
| 345 | { | ||
| 346 | my $self = shift; | ||
| 347 | my $nation = shift; | ||
| 348 | my @rels = $self->get_diplomatic_relations($nation); | ||
| 349 | for(@rels) | ||
| 350 | { | ||
| 351 | $_->crisis_level(0); | ||
| 352 | } | ||
| 353 | } | ||
| 354 | |||
| 355 | #Functions to manage treaties | ||
| 356 | sub create_treaty | ||
| 357 | { | ||
| 358 | my $self = shift; | ||
| 359 | my $nation1 = shift; | ||
| 360 | my $nation2 = shift; | ||
| 361 | my $type = shift; | ||
| 362 | $self->add_treaty(BalanceOfPower::Relations::Treaty->new( | ||
| 363 | node1 => $nation1, | ||
| 364 | node2 => $nation2, | ||
| 365 | type => $type )); | ||
| 366 | } | ||
| 367 | sub exists_treaty_by_type | ||
| 368 | { | ||
| 369 | my $self = shift; | ||
| 370 | my $nation1 = shift; | ||
| 371 | my $nation2 = shift; | ||
| 372 | my $type = shift; | ||
| 373 | my $rel = $self->exists_treaty($nation1, $nation2); | ||
| 374 | if( $rel && ($rel->type eq $type || $rel->type eq 'alliance')) #Alliance means both treaties are active | ||
| 375 | { | ||
| 376 | return $rel; | ||
| 377 | } | ||
| 378 | else | ||
| 379 | { | ||
| 380 | return undef; | ||
| 381 | } | ||
| 382 | } | ||
| 383 | sub get_treaties_for_nation_by_type | ||
| 384 | { | ||
| 385 | my $self = shift; | ||
| 386 | my $nation = shift; | ||
| 387 | my $type = shift; | ||
| 388 | my @treaties = $self->get_treaties_for_nation($nation); | ||
| 389 | return grep { $_->type eq $type } @treaties; | ||
| 390 | } | ||
| 391 | |||
| 392 | #Functions to manage alliances | ||
| 393 | sub add_alliance | ||
| 394 | { | ||
| 395 | my $self = shift; | ||
| 396 | my $nation1 = shift; | ||
| 397 | my $nation2 = shift; | ||
| 398 | $self->create_treaty($nation1, $nation2, 'alliance'); | ||
| 399 | $self->set_diplomacy($nation1, $nation2, ALLIANCE_FRIENDSHIP_FACTOR); | ||
| 400 | } | ||
| 401 | sub print_allies | ||
| 402 | { | ||
| 403 | my $self = shift; | ||
| 404 | my @treaties = $self->treaties->all(); | ||
| 405 | my $out = ""; | ||
| 406 | for(@treaties) | ||
| 407 | { | ||
| 408 | if($_->type eq 'alliance') | ||
| 409 | { | ||
| 410 | $out .= $_->print . "\n"; | ||
| 411 | } | ||
| 412 | } | ||
| 413 | return $out; | ||
| 414 | } | ||
| 415 | |||
| 416 | sub exists_alliance | ||
| 417 | { | ||
| 418 | my $self = shift; | ||
| 419 | my $nation1 = shift; | ||
| 420 | my $nation2 = shift; | ||
| 421 | return $self->exists_treaty_by_type($nation1, $nation2, 'alliance'); | ||
| 422 | } | ||
| 423 | |||
| 424 | sub get_allies | ||
| 425 | { | ||
| 426 | my $self = shift; | ||
| 427 | my $nation = shift; | ||
| 428 | return $self->get_treaties_for_nation_by_type($nation, 'alliance'); | ||
| 429 | } | ||
| 430 | |||
| 431 | |||
| 432 | 1; |