lib/BalanceOfPower/Role/Warlord.pm

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