blib/lib/BalanceOfPower/Role/Diplomat.pm untested

filter: Statementstmt 0.0% 0 / 265 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 48 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 38 Podpod 0.0% 0 / 30 totaltotal 0.0% 0 / 423 CC 22 SCAR 20.0
CC
22
Cov
0%
change_diplomacy
30.0
calculate_random_friendship
12.0
get_hates
12.0
CRAP 7.4
1 package BalanceOfPower::Role::Diplomat;
2 $BalanceOfPower::Role::Diplomat::VERSION = '0.300010';
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 use BalanceOfPower::Utils qw( as_main_title as_html_box br);
13  
14 has diplomatic_relations => (
15 is => 'ro',
16 default => sub { BalanceOfPower::Relations::RelPack->new() },
17 handles => { add_diplomacy => 'add_link',
18 _diplomacy_exists => 'exists_link',
19 update_diplomacy => 'update_link',
20 get_diplomatic_relations => 'links_for_node' }
21 );
22 has treaties => (
23 is => 'ro',
24 default => sub { BalanceOfPower::Relations::RelPack->new() },
25 handles => { add_treaty => 'add_link',
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! No diplomacy between $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  
253 sub print_diplomacy
254 {
255 my $self = shift;
256 my $n = shift;
257 my $mode = shift || "print";
258 my $out;
259 my @outnodes = sort { $a->factor <=> $b->factor} $self->get_diplomatic_relations($n);
260 return BalanceOfPower::Printer::print($mode, $self, 'print_diplomacy',
261 { nation => $n,
262 relationships => \@outnodes,
263 } );
264  
265 }
266  
267  
268 sub diplomatic_pressure
269 {
270 my $self = shift;
271 my $nation1 = shift;
272 my $nation2 = shift;
273 my @friends = $self->get_friends($nation1);
274 $self->change_diplomacy($nation1, $nation2, DIPLOMATIC_PRESSURE_FACTOR);
275 $self->broadcast_event("DIPLOMATIC PRESSURE OF $nation1 ON $nation2", $nation1, $nation2);
276 for(@friends)
277 {
278 my $f = $_;
279 $self->change_diplomacy($f, $nation2, DIPLOMATIC_PRESSURE_FACTOR);
280 }
281 }
282  
283  
284 #Functions to manage relationships as crises
285 sub add_crisis
286 {
287 my $self = shift;
288 my $nation1 = shift;
289 my $nation2 = shift;
290 my $rel = $self->diplomacy_exists($nation1, $nation2);
291 if($rel->get_crisis_level == 0)
292 {
293 $rel->escalate_crisis();
294 }
295 }
296 sub delete_crisis
297 {
298 my $self = shift;
299 my $nation1 = shift;
300 my $nation2 = shift;
301 my $rel = $self->diplomacy_exists($nation1, $nation2);
302 $rel->crisis_level(0);
303 }
304 sub crisis_exists
305 {
306 my $self = shift;
307 my $nation1 = shift || "";
308 my $nation2 = shift || "";
309 my $rel = $self->diplomacy_exists($nation1, $nation2);
310 if(! $rel)
311 {
312 say "ERROR: no diplomacy between $nation1, $nation2";
313 return undef;
314 }
315 if($rel->get_crisis_level > 0)
316 {
317 return $rel;
318 }
319 else
320 {
321 return undef;
322 }
323 }
324 sub get_crises
325 {
326 my $self = shift;
327 my $nation = shift;
328 my @crises = $self->get_diplomatic_relations($nation);
329 @crises = grep { $_->get_crisis_level > 0 } @crises;
330 return @crises;
331 }
332 sub get_all_crises
333 {
334 my $self = shift;
335 my @rels = $self->diplomatic_relations->all();
336 return grep { $_->is_crisis() } @rels;
337 }
338 sub reset_crises
339 {
340 my $self = shift;
341 my $nation = shift;
342 my @rels = $self->get_diplomatic_relations($nation);
343 for(@rels)
344 {
345 $_->crisis_level(0);
346 }
347 }
348  
349 #Functions to manage treaties
350 sub create_treaty
351 {
352 my $self = shift;
353 my $nation1 = shift;
354 my $nation2 = shift;
355 my $type = shift;
356 $self->add_treaty(BalanceOfPower::Relations::Treaty->new(
357 node1 => $nation1,
358 node2 => $nation2,
359 type => $type ));
360 }
361 sub exists_treaty_by_type
362 {
363 my $self = shift;
364 my $nation1 = shift;
365 my $nation2 = shift;
366 my $type = shift;
367 my $rel = $self->exists_treaty($nation1, $nation2);
368 if( $rel && ($rel->type eq $type || $rel->type eq 'alliance')) #Alliance means both treaties are active
369 {
370 return $rel;
371 }
372 else
373 {
374 return undef;
375 }
376 }
377 sub get_treaties_for_nation_by_type
378 {
379 my $self = shift;
380 my $nation = shift;
381 my $type = shift;
382 my @treaties = $self->get_treaties_for_nation($nation);
383 return grep { $_->type eq $type } @treaties;
384 }
385  
386 #Functions to manage alliances
387 sub add_alliance
388 {
389 my $self = shift;
390 my $nation1 = shift;
391 my $nation2 = shift;
392 $self->create_treaty($nation1, $nation2, 'alliance');
393 $self->set_diplomacy($nation1, $nation2, ALLIANCE_FRIENDSHIP_FACTOR);
394 }
395 sub print_allies
396 {
397 my $self = shift;
398 my $nation = shift;
399 my $mode = shift;
400 return $self->print_treaties($nation, "ALLIANCES", 'alliance', $mode);
401 }
402 sub print_treaties
403 {
404 my $self = shift;
405 my $nation = shift;
406 my $title = shift || "TREATIES";
407 my $treaty = shift || undef;
408 my $mode = shift || 'print';
409 my @treaties = $self->treaties->all();
410 my @to_print;
411 for(@treaties)
412 {
413 if((($treaty && $_->type eq $treaty) || ! $treaty) &&
414 (($nation && $_->involve($nation)) || ! $nation))
415 {
416 push @to_print, $_;
417 }
418 }
419 return BalanceOfPower::Printer::print($mode, $self, 'print_treaties',
420 { title => $title,
421 treaties => \@to_print,
422 } );
423 }
424  
425 sub exists_alliance
426 {
427 my $self = shift;
428 my $nation1 = shift;
429 my $nation2 = shift;
430 return $self->exists_treaty_by_type($nation1, $nation2, 'alliance');
431 }
432  
433 sub get_allies
434 {
435 my $self = shift;
436 my $nation = shift;
437 return $self->get_treaties_for_nation_by_type($nation, 'alliance');
438 }
439  
440  
441 1;