blib/lib/BalanceOfPower/World.pm untested

filter: Statementstmt 0.0% 0 / 527 Branchbran 0.0% 0 / 182 Conditioncond 0.0% 0 / 28 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 44 Podpod 0.0% 0 / 34 totaltotal 0.0% 0 / 815 CC 92 SCAR 51.1
CC
92
Cov
0%
execute_decisions
600.0
init_random
306.0
stipulate_treaty
110.0
CRAP 166.5
1 package BalanceOfPower::World;
2 $BalanceOfPower::World::VERSION = '0.200000';
3 use strict;
4 use v5.10;
5  
6 use Moo;
7 use Data::Dumper;
8 use Cwd 'abs_path';
9  
10 use BalanceOfPower::Constants ':all';
11 use BalanceOfPower::Utils qw(prev_turn next_turn);
12 use BalanceOfPower::Nation;
13 use BalanceOfPower::Dice;
14 use BalanceOfPower::Commands;
15  
16 has name => (
17 is => 'ro',
18 default => 'WORLD'
19 );
20 has silent => (
21 is => 'rw',
22 default => 0
23 );
24 has first_year => (
25 is => 'ro'
26 );
27 has current_year => (
28 is => 'rw'
29 );
30 has nations => (
31 is => 'rw',
32 default => sub { [] }
33 );
34 has nation_names => (
35 is => 'rw',
36 default => sub { [] }
37 );
38 has nation_codes => (
39 is => 'rw',
40 default => sub { {} }
41 );
42 has order => (
43 is => 'rw',
44 default => ""
45 );
46 has ia_orders => (
47 is => 'rw',
48 default => sub { [] }
49 );
50 has autoplay => (
51 is => 'rw',
52 default => 0
53 );
54 has data_directory => (
55 is => 'rw',
56 default => sub {
57 my $module_file_path = __FILE__;
58 my $root_path = abs_path($module_file_path);
59 $root_path =~ s/World\.pm//;
60 my $data_directory = $root_path . "data";
61  
62 }
63 );
64 has dice => (
65 is => 'ro',
66 default => sub { BalanceOfPower::Dice->new( log_name => "bop-dice.log" ) },
67 handles => { random => 'random',
68 random10 => 'random10',
69 random_around_zero => 'random_around_zero',
70 shuffle => 'shuffle_array',
71 tricks => 'tricks',
72 forced_advisor => 'forced_advisor',
73 only_one_nation_acting => 'only_one_nation_acting',
74 dice_log => 'log_active'
75 }
76 );
77 has savefile => (
78 is => 'rw',
79 default => ""
80 );
81  
82  
83  
84 with 'BalanceOfPower::Role::GameMaster';
85 with 'BalanceOfPower::Role::Historian';
86 with 'BalanceOfPower::Role::Herald';
87 with 'BalanceOfPower::Role::Ruler';
88 with 'BalanceOfPower::Role::Mapmaker';
89 with 'BalanceOfPower::Role::Supporter';
90 with 'BalanceOfPower::Role::Diplomat';
91 with 'BalanceOfPower::Role::Merchant';
92 with 'BalanceOfPower::Role::Broker';
93 with 'BalanceOfPower::Role::Warlord';
94 with 'BalanceOfPower::Role::CrisisManager';
95 with 'BalanceOfPower::Role::Analyst';
96 with 'BalanceOfPower::Role::Recorder';
97  
98 sub get_nation
99 {
100 my $self = shift;
101 my $nation = shift;
102 if(! $nation)
103 {
104 say "Nation is undef";
105 return undef;
106 }
107 my @nations = grep { $_->name eq $nation } @{$self->nations};
108 if(@nations > 0)
109 {
110 return $nations[0];
111 }
112 else
113 {
114 say "Cannot found $nation";
115 return undef;
116 }
117 }
118 sub correct_nation_name
119 {
120 my $self = shift;
121 my $nation = shift;
122 return undef if(! $nation);
123 $nation = $self->nation_codes->{uc $nation} if(exists $self->nation_codes->{uc $nation});
124 for(@{$self->nation_names})
125 {
126 return $_ if(uc $_ eq uc $nation);
127 }
128 return undef;
129 }
130 sub check_nation_name
131 {
132 my $self = shift;
133 my $name = shift;
134 return grep {$_ eq $name} @{$self->nation_names};
135 }
136 sub get_prev_year
137 {
138 my $self = shift;
139 return prev_turn($self->current_year);
140 }
141  
142 sub load_nations_data
143 {
144 my $self = shift;
145 my $datafile = shift;
146 my $file = $self->data_directory . "/" . $datafile;
147 open(my $nations_file, "<", $file) || die $!;
148 my $area;
149 my %nations_data;
150 for(<$nations_file>)
151 {
152 my $n = $_;
153 chomp $n;
154 if(! ($n =~ /^#/))
155 {
156 my ($name, $code, $size, $government) = split(',', $n);
157 if($government eq 'd')
158 {
159 $government = 'democracy';
160 }
161 elsif($government eq 'D')
162 {
163 $government = 'dictatorship';
164 }
165 $nations_data{$name} = { code => $code,
166 area => $area,
167 size => $size,
168 government => $government ,
169 }
170  
171 }
172 else
173 {
174 $n =~ /^# (.*)$/;
175 $area = $1;
176 }
177 }
178 return %nations_data;
179 }
180  
181 #Initial values, randomly generated
182 sub init_random
183 {
184 my $self = shift;
185 my $datafile = shift;
186 my $bordersfile = shift;
187 my %nations_data = $self->load_nations_data($datafile);
188 my $flags = shift;
189  
190 my $trades = 1;
191 my $diplomacy = 1;
192 my $alliances = 1;
193 if($flags)
194 {
195 $trades = $flags->{'trades'}
196 if(exists $flags->{'trades'});
197 $diplomacy = $flags->{'diplomacy'}
198 if(exists $flags->{'diplomacy'});
199 $alliances = $flags->{'alliances'}
200 if(exists $flags->{'alliances'});
201  
202 }
203  
204 $self->delete_log();
205 $self->dice->delete_log();
206 my @nation_names = ();
207 foreach my $n (keys %nations_data)
208 {
209 push @nation_names, $n;
210 say "Working on $n" if ! $self->silent;
211 my $export_quote = $self->random10(MIN_EXPORT_QUOTE, MAX_EXPORT_QUOTE, "Export quote $n");
212 say " export quote: $export_quote" if ! $self->silent;
213 my $government_strength = $self->random10(MIN_GOVERNMENT_STRENGTH, MAX_GOVERNMENT_STRENGTH, "Government strenght $n");
214 say " government strength: $government_strength" if ! $self->silent;
215  
216 my $executive = BalanceOfPower::Executive->new( actor => $n );
217 $executive->init($self);
218 push @{$self->nations}, BalanceOfPower::Nation->new(
219 name => $n,
220 code => $nations_data{$n}->{code},
221 executive => $executive,
222 area => $nations_data{$n}->{area},
223 size => $nations_data{$n}->{size},
224 government => $nations_data{$n}->{government},
225 export_quote => $export_quote,
226 government_strength => $government_strength,
227 available_stocks => START_STOCKS->[$nations_data{$n}->{size}],
228 log_name => $self->log_name,
229 log_on_stdout => $self->log_on_stdout);
230 $self->nation_codes->{$nations_data{$n}->{code}} = $n;
231 }
232 $self->nation_names(\@nation_names);
233 $self->load_borders($bordersfile);
234 if($trades)
235 {
236 say "Trades generation..." if ! $self->silent;
237 $self->init_trades();
238 }
239 else
240 {
241 say "Trades generation skipped" if ! $self->silent;
242 }
243 if($diplomacy)
244 {
245 say "Diplomacy generation..." if ! $self->silent;
246 $self->init_diplomacy();
247 }
248 else
249 {
250 say "Diplomacy generation skipped" if ! $self->silent;
251 }
252 if($alliances)
253 {
254 say "Alliances generation..." if ! $self->silent;
255 $self->init_random_alliances();
256 }
257 else
258 {
259 say "Alliances generation skipped" if ! $self->silent;
260 }
261 }
262  
263 #Group function for all the steps involved in a turn
264 sub pre_decisions_elaborations
265 {
266 my $self = shift;
267 my $t = shift;
268 $self->init_year($t);
269 $self->war_current_year();
270 $self->player_current_year();
271 $self->war_debts();
272 $self->crisis_generator();
273 }
274 sub post_decisions_elaborations
275 {
276 my $self = shift;
277 $self->execute_stock_orders();
278 $self->execute_decisions();
279 $self->economy();
280 $self->warfare();
281 $self->internal_conflict();
282 $self->register_global_data();
283 $self->collect_events();
284 }
285  
286  
287  
288 sub elaborate_turn
289 {
290 my $self = shift;
291 my $t = shift;
292 $self->pre_decisions_elaborations($t);
293 $self->decisions();
294 $self->post_decisions_elaborations();
295 }
296  
297 #To automatically generate turns
298 sub autopilot
299 {
300 my $self = shift;
301 my $start = shift;
302 my $stop = shift;
303 $self->autoplay(1);
304 for($start..$stop)
305 {
306 my $y = $_;
307 foreach my $t (get_year_turns($y))
308 {
309 $self->elaborate_turn($t);
310 }
311 }
312 $self->autoplay(0);
313 }
314  
315  
316 # Configure current year
317 # Give production to countries. Countries split it between export and domestic and, if allowed, raise the debt in case of necessity
318 # Wealth reset
319 # Production and debt recorded
320 sub init_year
321 {
322 my $self = shift;
323 my $turn = shift;
324 if(! $turn)
325 {
326 $turn = next_turn($self->current_year);
327 }
328 $self->log("--- $turn ---");
329 say $turn if $self->autoplay();
330 $self->current_year($turn);
331 foreach my $n (@{$self->nations})
332 {
333 $n->current_year($turn);
334 $n->wealth(0);
335 my $prod = $self->calculate_production($n);
336 $n->production($prod);
337 my $prestige = $self->calculate_prestige($n);
338 $n->prestige($prestige);
339 $self->set_statistics_value($n, 'production', $prod);
340 $self->set_statistics_value($n, 'debt', $n->debt);
341 $self->set_statistics_value($n, 'prestige', $prestige);
342 }
343 }
344  
345  
346  
347 # PRODUCTION MANAGEMENT ###############################
348  
349 #Say the value of starting production used to calculate production for a turn.
350 #Usually is just the value of production the turn before, but if rebels won a civil war it has to be undef to allow a totally random generation of production.
351 sub get_base_production
352 {
353 my $self = shift;
354 my $nation = shift;
355  
356 my @newgov = $nation->get_events("NEW GOVERNMENT CREATED", prev_turn($nation->current_year));
357 my $previous_production = $self->get_statistics_value(prev_turn($nation->current_year), $nation->name, 'production');
358
359 return () if(@newgov > 0);
360 return () if(! $previous_production);
361
362 my @prods = ();
363 for(my $i = 0; $i < PRODUCTION_UNITS->[$nation->size]; $i++)
364 {
365 push @prods, $self->get_statistics_value(prev_turn($nation->current_year), $nation->name, 'production' . $i);
366 }
367 return @prods;
368 }
369 sub calculate_production
370 {
371 my $self = shift;
372 my $n = shift;
373 my @prev_prods = $self->get_base_production($n);
374 my @next_prods = ();
375 my $cost_for_retreat = 0;
376 my @retreats = $n->get_events("RETREAT FROM", prev_turn($n->current_year));
377 my $global_production = 0;
378 for(my $i = 0; $i < PRODUCTION_UNITS->[$n->size]; $i++)
379 {
380 if(@prev_prods > 0)
381 {
382 $next_prods[$i] = $prev_prods[$i] + $self->random10(MIN_DELTA_PRODUCTION, MAX_DELTA_PRODUCTION, "Delta production" . $i . " " . $n->name);
383 }
384 else
385 {
386 $next_prods[$i] = $self->random10(MIN_STARTING_PRODUCTION, MAX_STARTING_PRODUCTION, "Starting production" . $i . " " . $n->name);
387 }
388  
389 #DEFEAT COST MANAGEMENT
390 if(@retreats)
391 {
392 $next_prods[$i] -= ATTACK_FAILED_PRODUCTION_MALUS;
393 $cost_for_retreat += ATTACK_FAILED_PRODUCTION_MALUS;
394 }
395 $next_prods[$i] = 0 if($next_prods[$i] < 0);
396 $next_prods[$i] = MAX_PRODUCTION if($next_prods[$i] > MAX_PRODUCTION);
397  
398 $self->set_statistics_value($n, 'production' . $i, $next_prods[$i]);
399 $global_production += $next_prods[$i];
400 }
401 if($cost_for_retreat)
402 {
403 $self->send_event("COST FOR DEFEAT ON PRODUCTION: " . $cost_for_retreat);
404 }
405 return $global_production;
406 }
407  
408  
409 #Conquered nations give to the conqueror a quote of their production at start of the turn
410 sub war_debts
411 {
412 my $self = shift;
413 for($self->influences->all())
414 {
415 $self->loot($_);
416 }
417 $self->situation_clock();
418 }
419  
420 sub loot
421 {
422 my $self = shift;
423 my $influence = shift;
424 my $n2 = $influence->node2;
425 my $n1 = $influence->node1;
426 my $quote = $influence->get_loot_quote();
427 return if(! $quote || $quote == 0);
428 my $nation = $self->get_nation($n2);
429 my $receiver = $self->get_nation($n1);
430 my $amount_domestic = $nation->production_for_domestic >= $quote ? $quote : $nation->production_for_domestic;
431 my $amount_export = $nation->production_for_export >= $quote ? $quote : $nation->production_for_export;
432 $nation->subtract_production('domestic', $amount_domestic);
433 $nation->subtract_production('export', $amount_export);
434 $nation->register_event("PAY LOOT TO " . $receiver->name . ": $amount_domestic + $amount_export");
435 $receiver->subtract_production('domestic', -1 * $amount_domestic);
436 $receiver->subtract_production('export', -1 * $amount_export);
437 $receiver->register_event("ACQUIRE LOOT FROM " . $nation->name . ": $amount_domestic + $amount_export");
438 }
439  
440  
441  
442 # PRODUCTION MANAGEMENT END ###############################################
443  
444 # PRESTIGE MANAGEMENT #####################################################
445  
446 sub calculate_prestige
447 {
448 my $self = shift;
449 my $nation = shift;
450 my $nation_name = $nation->name;
451 my $prestige = 0;
452 my @routes = $self->routes_for_node($nation_name);
453 $prestige += @routes;
454 my @supported = $self->supporter($nation_name);
455 $prestige += @supported;
456 my @influenced = $self->has_influence($nation_name);
457 $prestige += @influenced * INFLUENCE_PRESTIGE_BONUS;
458 my $bonus = 0;
459 my @ordered_best_w = $self->order_statistics(prev_turn($nation->current_year), 'w/d');
460 if(@ordered_best_w >= BEST_WEALTH_FOR_PRESTIGE)
461 {
462 for(my $i = 0; $i < BEST_WEALTH_FOR_PRESTIGE; $i++)
463 {
464 if($ordered_best_w[$i]->{nation} eq $nation_name)
465 {
466 $bonus += BEST_WEALTH_FOR_PRESTIGE_BONUS;
467 $self->broadcast_event("ONE OF THE FIRST " . BEST_WEALTH_FOR_PRESTIGE . " NATIONS FOR WEALTH WAS " . $nation_name, $nation_name);
468 }
469 }
470 }
471 my @ordered_best_p = $self->order_statistics(prev_turn($nation->current_year), 'progress');
472 if(@ordered_best_p >= BEST_PROGRESS_FOR_PRESTIGE)
473 {
474 for(my $i = 0; $i < BEST_PROGRESS_FOR_PRESTIGE; $i++)
475 {
476 if($ordered_best_p[$i]->{nation} eq $nation_name)
477 {
478 $bonus += BEST_PROGRESS_FOR_PRESTIGE_BONUS;
479 $self->broadcast_event("ONE OF THE FIRST " . BEST_PROGRESS_FOR_PRESTIGE . " NATIONS FOR PROGRESS WAS " . $nation_name, $nation_name);
480 }
481 }
482 }
483
484 $prestige += $bonus;
485 my @wins = $nation->get_events("WAR BETWEEN .* AND .* WON BY ". $nation_name, prev_turn($nation->current_year));
486 if(@wins > 0)
487 {
488 $prestige += WAR_PRESTIGE_BONUS;
489 }
490 return $prestige;
491 }
492  
493 # PRESTIGE MANAGEMENT END #####################################################
494  
495 # DECISIONS ###############################################################
496  
497 # Decisions are collected and executed
498 sub execute_decisions
499 {
500 my $self = shift;
501 my @decisions = @{$self->ia_orders};
502 my @route_adders = ();
503 #foreach my $d (@decisions)
504 foreach my $n (@{$self->nation_names})
505 {
506 my $command = $self->control($n);
507 if(! $command)
508 {
509 my @nation_orders = grep { $_ =~ /^$n: / } @decisions; if(@nation_orders > 0)
510 {
511 $nation_orders[0] =~ /^(.*): (.*)$/;
512 $command = $2;
513 }
514 else
515 {
516 next;
517 }
518 }
519 my $nation = $self->get_nation($n);
520 if($command =~ /^DELETE TRADEROUTE (.*)->(.*)$/)
521 {
522 $self->delete_route($1, $2);
523 }
524 elsif($command =~ /^ADD ROUTE$/)
525 {
526 push @route_adders, $nation->name;
527 }
528 elsif($command =~ /^LOWER DISORDER$/)
529 {
530 $nation->lower_disorder($self);
531 }
532 elsif($command =~ /^BUILD TROOPS$/)
533 {
534 $nation->build_troops();
535 }
536 elsif($command =~ /^BOOST PRODUCTION$/)
537 {
538 $nation->boost_production();
539 }
540 elsif($command =~ /DECLARE WAR TO (.*)$/)
541 {
542 my $attacker = $nation;
543 my $defender = $self->get_nation($1);
544 if(! $self->at_war($attacker->name) && ! $self->at_war($defender->name))
545 {
546 $self->create_war($attacker, $defender);
547 }
548 }
549 elsif($command =~ /^MILITARY SUPPORT (.*)$/)
550 {
551 my $supporter = $nation;
552 my $supported = $self->get_nation($1);
553 if($supported->accept_military_support($supporter->name, $self))
554 {
555 $self->start_military_support($supporter, $supported);
556 }
557 else
558 {
559 $self->broadcast_event($supported->name . " REFUSED MILITARY SUPPORT FROM " . $supporter->name);
560 }
561 }
562 elsif($command =~ /^RECALL MILITARY SUPPORT (.*)$/)
563 {
564 my $supporter = $nation;
565 my $supported = $self->get_nation($1);
566 $self->stop_military_support($supporter, $supported);
567 }
568 elsif($command =~ /^AID INSURGENTS IN (.*)$/)
569 {
570 my $victim = $self->get_nation($1);
571 $self->aid_insurgents($nation, $victim);
572 }
573 elsif($command =~ /^TREATY (.*) WITH (.*)$/)
574 {
575 my $nation2 = $self->get_nation($2);
576 $self->stipulate_treaty($nation, $nation2, $1);
577 }
578 elsif($command =~ /^ECONOMIC AID FOR (.*)$/)
579 {
580 my $nation2 = $self->get_nation($1);
581 $self->economic_aid($nation, $nation2);
582 }
583 elsif($command =~ /^REBEL MILITARY SUPPORT (.*)$/)
584 {
585 my $nation2 = $self->get_nation($1);
586 my $rebsup = $self->rebel_supported($nation2->name);
587 if($rebsup && $rebsup->node1 ne $nation->name)
588 {
589 $self->broadcast_event("REBEL SUPPORT IN " . $nation2->name . " IMPOSSIBLE FOR " . $nation->name, $nation->name, $nation2->name);
590 }
591 else
592 {
593 $self->start_rebel_military_support($nation, $nation2);
594 }
595 }
596 elsif($command =~ /^DIPLOMATIC PRESSURE ON (.*)$/)
597 {
598 my $n2 = $1;
599 if($nation->prestige >= DIPLOMATIC_PRESSURE_PRESTIGE_COST)
600 {
601 my $under_infl = $self->is_under_influence($n2);
602 $under_infl ||= "";
603 if($under_infl ne $nation->name)
604 {
605 $self->diplomatic_pressure($nation->name, $n2);
606 }
607 else
608 {
609 $self->broadcast_event("DIPLOMATIC PRESSURE ON $n2 BY " . $nation->name . " IMPOSSIBLE! $n2 IS UNDER INFLUENCE OF " . $nation->name, $nation->name, $n2);
610 }
611 }
612 }
613 elsif($command =~ /^RECALL REBEL MILITARY SUPPORT (.*)$/)
614 {
615 my $supported = $self->get_nation($1);
616 $self->stop_rebel_military_support($nation, $supported);
617 }
618 elsif($command =~ /^MILITARY AID FOR (.*)$/)
619 {
620 my $nation2 = $self->get_nation($1);
621 $self->military_aid($nation, $nation2);
622 }
623 elsif($command =~ /^PROGRESS$/)
624 {
625 $nation->grow();
626 $self->broadcast_event("INVESTMENT IN PROGRESS FOR " . $nation->name, $nation->name);
627 }
628 $self->set_statistics_value($nation, 'order', $command);
629 }
630 $self->empty_control_orders();
631 $self->manage_route_adding(@route_adders);
632 }
633 sub empty_control_orders
634 {
635 my $self = shift;
636 foreach my $p (@{$self->players})
637 {
638 $p->empty_control_orders();
639 }
640 }
641  
642 sub manage_route_adding
643 {
644 my $self = shift;
645 my @route_adders = @_;
646 if(@route_adders > 1)
647 {
648 @route_adders = $self->shuffle("Route adders", @route_adders);
649 my $done = 0;
650 while(! $done)
651 {
652 my $node1 = shift @route_adders;
653 if($self->suitable_route_creator($node1))
654 {
655 if(@route_adders == 0)
656 {
657 $self->send_event("TRADEROUTE CREATION FAILED FOR LACK OF PARTNERS", $node1);
658 $done = 1;
659 }
660 else
661 {
662 my $complete = 0;
663 foreach my $second (@route_adders)
664 {
665 if($self->suitable_new_route($node1, $second))
666 {
667 @route_adders = grep { $_ ne $second } @route_adders;
668 $self->generate_traderoute($node1, $second, 1);
669 $complete = 1;
670 }
671 last if $complete;
672 }
673 if($complete == 0)
674 {
675 $self->send_event("TRADEROUTE CREATION FAILED FOR LACK OF PARTNERS", $node1);
676 }
677 }
678 }
679 else
680 {
681 $self->broadcast_event("TRADEROUTE CREATION NOT POSSIBLE FOR $node1", $node1);
682 }
683 $done = 1 if(@route_adders == 0);
684 }
685 }
686 }
687 sub decisions
688 {
689 my $self = shift;
690 my @decisions = ();
691 foreach my $nation (@{$self->nations})
692 {
693 my $decision;
694 $decision = $nation->decision($self);
695 if($decision)
696 {
697 push @decisions, $decision;
698 }
699 }
700 $self->ia_orders(\@decisions);
701 }
702 sub control
703 {
704 my $self = shift;
705 my $nation = shift;
706 my $quote = -1;
707 my $winner = undef;
708 my @losers = ();
709 my $winner_command;
710 foreach my $player (@{$self->players})
711 {
712 my $player_command = $player->get_control_order($nation);
713 if($player_command)
714 {
715 $player->add_influence(-1 * INFLUENCE_COST, $nation);
716 if($player->stocks($nation) > $quote)
717 {
718 if($winner)
719 {
720 push @losers, $winner;
721 }
722 $winner = $player;
723 $quote = $player->stocks($nation);
724 $winner_command = $player_command;
725 }
726 else
727 {
728 push @losers, $player;
729 }
730 }
731 }
732 if($winner)
733 {
734 $winner->register_event("ORDER FOR $nation IS EXECUTED: $winner_command");
735 for(@losers)
736 {
737 $_->register_event("ORDER FOR $nation NOT EXECUTED! " . $winner->name . " MORE POWERFUL");
738 }
739 return $winner_command;
740 }
741 else
742 {
743 return undef;
744 }
745 }
746  
747 # DECISIONS END ###########################################################
748  
749 # ECONOMY #################################################################
750  
751 # Calculate internal wealth converting domestic production to wealth
752 # Active trade routes one by one trying to generate wealth from each of them
753 # Convert remain as generating internal wealth
754 sub economy
755 {
756 my $self = shift;
757 foreach my $n (@{$self->nations})
758 {
759 $n->calculate_internal_wealth();
760 $n->calculate_trading($self);
761 $n->convert_remains();
762 if($self->at_war($n->name))
763 {
764 $n->war_cost();
765 }
766 if($self->at_civil_war($n->name))
767 {
768 $n->civil_war_cost();
769 }
770  
771 my $wealth = $n->wealth;
772 my $pu = PRODUCTION_UNITS->[$n->size];
773 my $prod = $self->get_statistics_value($self->current_year, $n->name, 'production');
774  
775 $self->set_statistics_value($n, 'wealth', $n->wealth);
776 $self->set_statistics_value($n, 'w/d', int(($wealth / $pu) * 100) / 100);
777 if($prod != 0)
778 {
779 $self->set_statistics_value($n, 'growth', int(($wealth / $prod) * 100) / 100 );
780 }
781 else
782 {
783 $self->set_statistics_value($n, 'growth', 'X' );
784 }
785 }
786 }
787 sub economic_aid
788 {
789 my $self = shift;
790 my $nation1 = shift;
791 my $nation2 = shift;
792 $nation1->subtract_production('export', ECONOMIC_AID_COST);
793 $nation2->receive_aid($nation1->name);
794 $self->broadcast_event("ECONOMIC AID FROM " . $nation1->name . " TO " . $nation2->name, $nation1->name, $nation2->name);
795 $self->change_diplomacy($nation1->name, $nation2->name, ECONOMIC_AID_DIPLOMACY_FACTOR);
796  
797 }
798  
799 # ECONOMY END #############################################################
800  
801 # INTERNAL DISORDER #######################################################
802  
803 sub internal_conflict
804 {
805 my $self = shift;
806 foreach my $n (@{$self->nations})
807 {
808 my $present_status = $n->internal_disorder_status;
809  
810 $n->calculate_disorder($self);
811
812 my $winner = $n->fight_civil_war($self);
813 if($winner && $winner eq 'rebels')
814 {
815 $n->win_civil_war('rebels', $self);
816 $n->new_government($self);
817 }
818 elsif($winner && $winner eq 'government')
819 {
820 $n->win_civil_war('government', $self);
821 }
822
823 $self->set_statistics_value($n, 'internal disorder', $n->internal_disorder);
824 }
825 }
826  
827 sub aid_insurgents
828 {
829 my $self = shift;
830 my $nation1 = shift;
831 my $nation2 = shift;
832 if($nation1->production_for_export >= AID_INSURGENTS_COST && $nation2->internal_disorder_status ne 'Civil war')
833 {
834 $self->broadcast_event("AIDS FOR INSURGENTS OF " . $nation2->name . " FROM " . $nation1->name, $nation1->name, $nation2->name);
835 $nation1->subtract_production('export', AID_INSURGENTS_COST);
836 $nation2->add_internal_disorder(INSURGENTS_AID, $self);
837 }
838 }
839  
840 sub at_civil_war
841 {
842 my $self = shift;
843 my $n = shift;
844 my $nation = $self->get_nation($n);
845 return $nation->internal_disorder_status eq 'Civil war';
846 }
847  
848 sub start_civil_war
849 {
850 my $self = shift;
851 my $nation = shift;
852 $self->broadcast_event("CIVIL WAR OUTBREAK IN " . $nation->name, $nation->name);
853 $nation->rebel_provinces(STARTING_REBEL_PROVINCES->[$nation->size]);
854 $self->war_report("Civil war in " . $nation->name . "!", $nation->name);
855 $self->lose_war($nation->name, 1);
856 }
857  
858 # INTERNAL DISORDER END ######################################################
859  
860 # WAR ######################################################################
861  
862 sub warfare
863 {
864 my $self = shift;
865 $self->fight_wars();
866 foreach my $n (@{$self->nations})
867 {
868 $self->set_statistics_value($n, 'army', $n->army);
869 }
870 }
871  
872 sub military_aid
873 {
874 my $self = shift;
875 my $nation1 = shift;
876 my $nation2 = shift;
877 $nation1->subtract_production('export', MILITARY_AID_COST);
878 $nation2->add_army(ARMY_UNIT);
879 $self->broadcast_event("MILITARY AID FROM " . $nation1->name . " TO " . $nation2->name, $nation1->name, $nation2->name);
880 $self->change_diplomacy($nation1->name, $nation2->name, MILITARY_AID_DIPLOMACY_FACTOR);
881 }
882  
883 sub war_report
884 {
885 my $self = shift;
886 my $message = shift;
887 my $nation = shift;
888 my @wars = $self->get_wars($nation);
889 for(@wars)
890 {
891 $_->register_event($message);
892 }
893 }
894  
895  
896 # WAR END ##################################################################
897  
898 # TREATIES #################################################################
899  
900 sub stipulate_treaty
901 {
902 my $self = shift;
903 my $nation1 = shift;
904 my $nation2 = shift;
905 my $type = shift;
906 my $present_treaty = $self->exists_treaty($nation1->name, $nation2->name);
907 my $diplomatic_status = $self->diplomacy_status($nation1->name, $nation2->name);
908 if($diplomatic_status eq 'HATE')
909 {
910 $self->broadcast_event("TREATY BETWEEN " . $nation1->name . " AND " . $nation2->name . " NOT POSSIBLE BECAUSE OF HATE", $nation1->name, $nation2->name);
911 return;
912 }
913 if($self->get_treaties_for_nation($nation1->name) >= $nation1->treaty_limit ||
914 $self->get_treaties_for_nation($nation2->name) >= $nation2->treaty_limit)
915 {
916 $self->broadcast_event("TREATY BETWEEN " . $nation1->name . " AND " . $nation2->name . " NOT POSSIBLE BECAUSE ONE NATION HAS ALREADY REACHED MAXIMUM ALLOWED TREATIES", $nation1->name, $nation2->name);
917 return;
918 }
919 if($nation1->prestige >= TREATY_PRESTIGE_COST)
920 {
921 if($type eq 'COM')
922 {
923 if(! $present_treaty)
924 {
925 if($self->route_exists($nation1->name, $nation2->name))
926 {
927 $self->create_treaty($nation1->name, $nation2->name, 'commercial');
928 $self->broadcast_event("COMMERCIAL TREATY BETWEEN " . $nation1->name . " AND " . $nation2->name, $nation1->name, $nation2->name);
929 }
930 else
931 {
932 $self->broadcast_event("COMMERCIAL TREATY BETWEEN " . $nation1->name . " AND " . $nation2->name . " MADE USELESS BY ROUTE CANCELATION", $nation1->name, $nation2->name);
933 }
934 }
935 }
936 elsif($type eq 'NAG')
937 {
938 if(! $present_treaty)
939 {
940 $self->create_treaty($nation1->name, $nation2->name, 'no aggression');
941 $self->broadcast_event("NO AGGRESSION TREATY BETWEEN " . $nation1->name . " AND " . $nation2->name, $nation1->name, $nation2->name);
942 }
943 }
944 else
945 {
946 if($present_treaty && $present_treaty->type ne 'alliance')
947 {
948 $self->create_treaty($nation1->name, $nation2->name, 'alliance');
949 $self->broadcast_event("ALLIANCE BETWEEN " . $nation1->name . " AND " . $nation2->name, $nation1->name, $nation2->name);
950 }
951 }
952 }
953  
954  
955 }
956  
957  
958  
959 # TRATIES END ##############################################################
960  
961 sub register_global_data
962 {
963 my $self = shift;
964 my $crises = $self->get_all_crises();
965 my $wars = $self->wars->all();
966 $self->set_statistics_value(undef, 'crises', $crises);
967 $self->set_statistics_value(undef, 'wars', $wars);
968 }
969  
970 sub collect_events
971 {
972 my $self = shift;
973 my @events_to_collect = ("DISORDER LOWERED TO",
974 "INTERNAL DISORDER LEVEL FROM",
975 "THE GOVERNMENT WON THE CIVIL WAR",
976 "THE REBELS WON THE CIVIL WAR",
977 "NEW GOVERNMENT CREATED",
978 "TRADEROUTE CREATION FAILED FOR LACK OF PARTNERS");
979 foreach my $n (@{$self->nations})
980 {
981 my @collected = ();
982 foreach my $e (@events_to_collect)
983 {
984 push @collected, $n->get_events($e, $self->current_year);
985 }
986 foreach my $c (@collected)
987 {
988 $self->register_event($n->name . ": ". $c);
989 }
990 $self->set_statistics_value($n, 'progress', $n->progress);
991 }
992 }
993  
994 ### Commands
995  
996 sub build_commands
997 {
998 my $self = shift;
999 my $commands = BalanceOfPower::Commands->new( world => $self, log_name => 'bop-commands.log', log_active => $self->log_active );
1000 return $commands;
1001 }
1002  
1003 1;