line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package BalanceOfPower::Role::Merchant; |
2
|
|
|
|
|
|
|
$BalanceOfPower::Role::Merchant::VERSION = '0.400105'; |
3
|
13
|
|
|
13
|
|
4434
|
use strict; |
|
13
|
|
|
|
|
15
|
|
|
13
|
|
|
|
|
326
|
|
4
|
13
|
|
|
13
|
|
102
|
use v5.10; |
|
13
|
|
|
|
|
29
|
|
5
|
13
|
|
|
13
|
|
39
|
use Moo::Role; |
|
13
|
|
|
|
|
16
|
|
|
13
|
|
|
|
|
69
|
|
6
|
|
|
|
|
|
|
|
7
|
13
|
|
|
13
|
|
2746
|
use BalanceOfPower::Constants ':all'; |
|
13
|
|
|
|
|
29
|
|
|
13
|
|
|
|
|
5576
|
|
8
|
13
|
|
|
13
|
|
3452
|
use BalanceOfPower::Relations::TradeRoute; |
|
13
|
|
|
|
|
20
|
|
|
13
|
|
|
|
|
9218
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
requires 'get_nation'; |
11
|
|
|
|
|
|
|
requires 'broadcast_event'; |
12
|
|
|
|
|
|
|
requires 'change_diplomacy'; |
13
|
|
|
|
|
|
|
requires 'diplomacy_status'; |
14
|
|
|
|
|
|
|
requires 'random'; |
15
|
|
|
|
|
|
|
requires 'distance'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has trade_routes => ( |
18
|
|
|
|
|
|
|
is => 'ro', |
19
|
|
|
|
|
|
|
default => sub { BalanceOfPower::Relations::RelPack->new() }, |
20
|
|
|
|
|
|
|
handles => { add_traderoute => 'add_link', |
21
|
|
|
|
|
|
|
delete_traderoute => 'delete_link', |
22
|
|
|
|
|
|
|
route_exists => 'exists_link', |
23
|
|
|
|
|
|
|
routes_for_node => 'links_for_node', |
24
|
|
|
|
|
|
|
route_destinations_for_node => 'link_destinations_for_node' |
25
|
|
|
|
|
|
|
# print_borders => 'print_links' |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub init_trades |
30
|
|
|
|
|
|
|
{ |
31
|
2
|
|
|
2
|
0
|
6
|
my $self = shift; |
32
|
2
|
|
|
|
|
4
|
my @nations = @{$self->nations}; |
|
2
|
|
|
|
|
18
|
|
33
|
2
|
|
|
|
|
4
|
my %routes_counter; |
34
|
2
|
|
|
|
|
5
|
foreach my $n (@nations) |
35
|
|
|
|
|
|
|
{ |
36
|
10
|
100
|
|
|
|
42
|
$routes_counter{$n->name} = 0 if(! exists $routes_counter{$n->name}); |
37
|
10
|
|
|
|
|
194
|
my $how_many_routes = $self->random(MIN_STARTING_TRADEROUTES, MAX_STARTING_TRADEROUTES, "Routes to generate for " . $n->name); |
38
|
10
|
|
|
|
|
985
|
say " routes to generate: $how_many_routes [" . $routes_counter{$n->name} . "]"; |
39
|
10
|
|
|
|
|
34
|
my @my_names = @nations; |
40
|
10
|
|
|
|
|
21
|
@my_names = grep { $_->name ne $n->name } @my_names; |
|
50
|
|
|
|
|
123
|
|
41
|
10
|
|
66
|
|
|
57
|
while($routes_counter{$n->name} < $how_many_routes && @my_names > 0) |
42
|
|
|
|
|
|
|
{ |
43
|
11
|
|
|
|
|
27
|
my $second_node = $my_names[rand @my_names]; |
44
|
11
|
|
|
|
|
16
|
@my_names = grep { $_->name ne $second_node->name } @my_names; |
|
35
|
|
|
|
|
70
|
|
45
|
11
|
50
|
33
|
|
|
65
|
if($second_node->name ne $n->name && ! $self->route_exists($n->name, $second_node->name)) |
46
|
|
|
|
|
|
|
{ |
47
|
11
|
|
|
|
|
1139
|
say " creating trade route to " . $second_node->name; |
48
|
11
|
|
|
|
|
29
|
@my_names = grep { $_->name ne $second_node->name } @my_names; |
|
24
|
|
|
|
|
85
|
|
49
|
11
|
|
|
|
|
39
|
$self->generate_traderoute($n->name, $second_node->name, 0); |
50
|
11
|
|
|
|
|
27
|
$routes_counter{$n->name}++; |
51
|
11
|
100
|
|
|
|
32
|
$routes_counter{$second_node->name} = 0 if(! exists $routes_counter{$second_node->name}); |
52
|
11
|
|
|
|
|
59
|
$routes_counter{$second_node->name}++; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
sub generate_traderoute |
58
|
|
|
|
|
|
|
{ |
59
|
18
|
|
|
18
|
0
|
2884
|
my $self = shift; |
60
|
18
|
|
|
|
|
24
|
my $node1 = shift; |
61
|
18
|
|
|
|
|
18
|
my $node2 = shift; |
62
|
18
|
|
|
|
|
19
|
my $added = shift; |
63
|
|
|
|
|
|
|
|
64
|
18
|
|
|
|
|
59
|
my $n1 = $self->get_nation($node1); |
65
|
18
|
|
|
|
|
37
|
my $n2 = $self->get_nation($node2); |
66
|
18
|
|
|
|
|
61
|
my $distance = $self->distance($node1, $node2); |
67
|
18
|
|
|
|
|
23
|
my $common_factor = 2; |
68
|
18
|
50
|
|
|
|
49
|
if($distance ne 'X') |
69
|
|
|
|
|
|
|
{ |
70
|
18
|
100
|
|
|
|
56
|
if($distance == 1) |
|
|
100
|
|
|
|
|
|
71
|
|
|
|
|
|
|
{ |
72
|
7
|
|
|
|
|
10
|
$common_factor = 4; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
elsif($distance == 2) |
75
|
|
|
|
|
|
|
{ |
76
|
3
|
|
|
|
|
6
|
$common_factor = 3; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
} |
79
|
18
|
|
|
|
|
22
|
my $factor1 = $common_factor; |
80
|
18
|
|
|
|
|
17
|
my $factor2 = $common_factor; |
81
|
18
|
50
|
|
|
|
72
|
if($n1->size < $n2->size) |
82
|
|
|
|
|
|
|
{ |
83
|
0
|
|
|
|
|
0
|
$factor1 = $common_factor + TRADEROUTE_SIZE_BONUS; |
84
|
|
|
|
|
|
|
} |
85
|
18
|
50
|
|
|
|
62
|
if($n2->size < $n1->size) |
86
|
|
|
|
|
|
|
{ |
87
|
0
|
|
|
|
|
0
|
$factor2 = $common_factor + TRADEROUTE_SIZE_BONUS; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
$self->add_traderoute( |
91
|
18
|
|
|
|
|
293
|
BalanceOfPower::Relations::TradeRoute->new( |
92
|
|
|
|
|
|
|
node1 => $node1, node2 => $node2, |
93
|
|
|
|
|
|
|
factor1 => $factor1, factor2 => $factor2)); |
94
|
18
|
100
|
|
|
|
153
|
if($added) |
95
|
|
|
|
|
|
|
{ |
96
|
3
|
|
|
|
|
11
|
$n1->subtract_production('export', ADDING_TRADEROUTE_COST); |
97
|
3
|
|
|
|
|
7
|
$n2->subtract_production('export', ADDING_TRADEROUTE_COST); |
98
|
3
|
|
|
|
|
9
|
$self->change_diplomacy($node1, $node2, TRADEROUTE_DIPLOMACY_FACTOR, "TRADE CREATION"); |
99
|
3
|
|
|
|
|
21
|
my $event = { code => 'tradeadded', |
100
|
|
|
|
|
|
|
text => "TRADEROUTE ADDED: $node1<->$node2", |
101
|
|
|
|
|
|
|
involved => [$node1, $node2], |
102
|
|
|
|
|
|
|
values => [] }; |
103
|
3
|
|
|
|
|
10
|
$self->broadcast_event($event, $node1, $node2); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
sub delete_route |
108
|
|
|
|
|
|
|
{ |
109
|
1
|
|
|
1
|
0
|
1
|
my $self = shift; |
110
|
1
|
|
|
|
|
3
|
my $node1 = shift;; |
111
|
1
|
|
|
|
|
2
|
my $node2 = shift; |
112
|
1
|
|
|
|
|
3
|
my $n1 = $self->get_nation($node1); |
113
|
1
|
|
|
|
|
4
|
my $n2 = $self->get_nation($node2); |
114
|
1
|
|
|
|
|
4
|
my $present_treaty = $self->exists_treaty_by_type($node1, $node2, 'commercial'); |
115
|
1
|
50
|
|
|
|
3
|
if($present_treaty) |
116
|
|
|
|
|
|
|
{ |
117
|
0
|
|
|
|
|
0
|
my $not_event = "TRADEROUTE DELETION $node1<->$node2 BLOCKED BY TREATY"; |
118
|
0
|
|
|
|
|
0
|
$self->broadcast_event($not_event, $node1, $node2); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
else |
121
|
|
|
|
|
|
|
{ |
122
|
1
|
|
|
|
|
4
|
$self->delete_traderoute($node1, $node2); |
123
|
1
|
|
|
|
|
9
|
my $event = { code => 'tradedeleted', |
124
|
|
|
|
|
|
|
text => "TRADEROUTE DELETED: $node1<->$node2", |
125
|
|
|
|
|
|
|
involved => [$node1, $node2], |
126
|
|
|
|
|
|
|
values => [] }; |
127
|
1
|
|
|
|
|
4
|
$self->broadcast_event($event, $node1, $node2); |
128
|
1
|
|
|
|
|
5
|
$self->change_diplomacy($node1, $node2, -1 * TRADEROUTE_DIPLOMACY_FACTOR, "TRADE DELETION"); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
sub suitable_route_creator |
132
|
|
|
|
|
|
|
{ |
133
|
7
|
|
|
7
|
0
|
9
|
my $self = shift; |
134
|
7
|
|
|
|
|
14
|
my $nation = $self->get_nation( shift ); |
135
|
7
|
50
|
|
|
|
18
|
return 0 if($nation->production < ADDING_TRADEROUTE_COST); |
136
|
7
|
50
|
|
|
|
15
|
return 0 if($nation->internal_disorder_status eq 'Civil war'); |
137
|
7
|
|
|
|
|
21
|
return 1; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
sub suitable_new_route |
140
|
|
|
|
|
|
|
{ |
141
|
4
|
|
|
4
|
0
|
6
|
my $self = shift; |
142
|
4
|
|
|
|
|
7
|
my $node1 = $self->get_nation( shift ); |
143
|
4
|
|
|
|
|
8
|
my $node2 = $self->get_nation( shift ); |
144
|
4
|
50
|
|
|
|
18
|
return 0 if($self->route_exists($node1->name, $node2->name)); |
145
|
4
|
100
|
|
|
|
21
|
if($self->diplomacy_status($node1->name, $node2->name) ne 'HATE') |
146
|
|
|
|
|
|
|
{ |
147
|
3
|
50
|
|
|
|
17
|
if($self->suitable_route_creator($node2->name)) |
148
|
|
|
|
|
|
|
{ |
149
|
3
|
|
|
|
|
8
|
return 1; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
else |
153
|
|
|
|
|
|
|
{ |
154
|
1
|
|
|
|
|
14
|
$self->broadcast_event({ code => 'traderefused', |
155
|
|
|
|
|
|
|
text => $node1->name . " AND " . $node2->name . " REFUSED TO OPEN A TRADEROUTE", |
156
|
|
|
|
|
|
|
involved => [$node1->name, $node2->name] }, $node1->name, $node2->name); |
157
|
1
|
|
|
|
|
4
|
return 0; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
1; |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
|