blib/lib/BalanceOfPower/World.pm untested

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