blib/lib/Game/Entities.pm

filter: Statementstmt 98.2% 275 / 280 Branchbran 82.1% 69 / 84 Conditioncond 70.0% 42 / 60 MC/DCmcdc 34.2% 12 / 35 Subroutinesub 94.1% 48 / 51 Podpod 100% 12 / 12 totaltotal 87.7% 458 / 522 SCAR 44.9
CC
84
Cov
91%
_dump_entities
18.2
sort
14.4
delete
11.0
CRAP 89.1
↑ Summary Build »
1 # ABSTRACT: A simple entity registry for ECS designs
2 package Game::Entities;
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Sub::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 our $VERSION = '0.011';
16  
17 # The main entity registry, inspired by https://github.com/skypjack/entt
18  
19 use constant {
20 3 SPARSE => 0,
21 DENSE => 1,
22 COMPONENTS => 2,
23  
24 # Entity GUIDs are 32 bit integers:
25 # * 12 bits used for the entity version (used for recycing entities)
26 # * 20 bits used for the entity number
27 ENTITY_MASK => 0xFFFFF, # Used to convert GUIDs to entity numbers
28 VERSION_MASK => 0xFFF, # Used to convert GUIDs to entity versions
29 ENTITY_SHIFT => 20, # The size of the entity number within a GUID
30 NULL_ENTITY => 0, # The null entity
31 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 ## Entity "methods"
34  
35 3 my $base = __PACKAGE__ . '::';
36 186 ▶my $version = Sub::Util::set_subname "${base}GUID::version" => sub ($e) { $e >> ENTITY_SHIFT };
Subroutine version: called CC=1 SCAR=0.4
Subroutine __ANON__: not called CC=1 SCAR=0.4
37 342 ▶my $entity = Sub::Util::set_subname "${base}GUID::entity" => sub ($e) { $e & ENTITY_MASK };
Subroutine entity: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
38 137 ▶my $is_null = Sub::Util::set_subname "${base}GUID::is_null" => sub ($e) { $e->$entity == NULL_ENTITY };
Subroutine is_null: called CC=1 SCAR=0.4
Subroutine __ANON__: not called CC=1 SCAR=0.4
39 0 ▶my $format = Sub::Util::set_subname "${base}GUID::format" => sub ($e) { sprintf '%03X:%05X', $e->$version, $e->$entity };
Subroutine __ANON__: not called CC=1 SCAR=1.2
40  
41 ## Sparse set "methods"
42  
43 ▶my $swap_components = Sub::Util::set_subname "${base}Set::swap_components" => sub ( $set, $le, $re ) {
Subroutine swap_components: called CC=1 SCAR=0.0
44 68 my ( $ld, $rd ) = @{ $set->[SPARSE] }[ $le, $re ];
45 68 @{ $set->[COMPONENTS] }[ $ld, $rd ] = @{ $set->[COMPONENTS] }[ $rd, $ld ];
46 3 };
47  
48 ▶my $swap = Sub::Util::set_subname "${base}Set::swap" => sub ( $set, $le, $re ) {
Subroutine swap: called CC=3 SCAR=11.1
49 40 my ( $ld, $rd ) = @{ $set->[SPARSE] }[ $le, $re ];
50 40 my ( $ls, $rs ) = @{ $set->[DENSE ] }[ $ld, $rd ];
51  
52 40 ▶ Carp::confess "Cannot swap $le and $re: they are not members of the set"
Branch: 1 branch
BranchTrueFalseTotal
unless $ls == $le and $rs == $re 0 40 40
Condition33%1/3
$ls == $le and $rs == $re
!ll&&!rl&&r
0040
MC/DC0%0/2
$ls == $le and $rs == $re
$ls == $le $rs == $re
Truth table33%1/3
A and B
A= $ls == $leB= $rs == $re
ABresult
0X0
100
111
53 unless $ls == $le && $rs == $re;
54  
55 40 $set->$swap_components( $le, $re );
56 40 @{ $set->[DENSE ] }[ $ld, $rd ] = @{ $set->[DENSE ] }[ $rd, $ld ];
57 40 @{ $set->[SPARSE] }[ $ls, $rs ] = @{ $set->[SPARSE] }[ $rs, $ls ];
58 3 };
59  
60 ▶my $contains = Sub::Util::set_subname "${base}Set::contains" => sub ( $set, $index ) {
Subroutine contains: called CC=3 SCAR=11.1
61 264 ▶ my $sparse = $set->[SPARSE][$index] // return;
Condition100%2/2
$set->[0][$index] // (return)
l!l
89175
MC/DC100%1/1
$set->[0][$index] // (return)
$set->[0][$index]
Truth table100%2/2
A
A= $set->[0][$index]
Aresult
11
00
62 89 ▶ return ( $set->[DENSE][$sparse] // $index + 1 ) == $index;
Condition66%2/3
$set->[1][$sparse] // $index + 1
l!l&&r!l&&!r
8360
MC/DC0%0/2
$set->[1][$sparse] // $index + 1
$set->[1][$sparse] $index + 1
Truth table66%2/3
A // B
A= $set->[1][$sparse]B= $index + 1
ABresult
1X1
011
000
63 3 };
64  
65 ## Private, hidden methods
66  
67 ▶my $add_version = sub ($self, $index) {
Subroutine __ANON__: called CC=1 SCAR=0.0
68 162 $index | ( $self->{entities}[$index]->$version << ENTITY_SHIFT )
69 3 };
70  
71 ▶my $generate_guid = sub ($self) {
Subroutine __ANON__: called CC=2 SCAR=7.0
72 Carp::croak 'Exceeded maximum number of entities'
73 98 ▶ if @{ $self->{entities} } >= ENTITY_MASK;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"entities"};} >= 1048575 0 98 98
74  
75 98 my $guid = @{ $self->{entities} };
76 98 push @{ $self->{entities} }, $guid;
77  
78 98 return $guid;
79 3 };
80  
81 ▶my $recycle_guid = sub ($self) {
Subroutine __ANON__: called CC=2 SCAR=7.0
82 11 my $next = $self->{available};
83  
84 11 ▶ Carp::croak 'Cannot recycle GUID if none has been released'
Branch: 1 branch
BranchTrueFalseTotal
if $next->$is_null 0 11 11
85 if $next->$is_null;
86  
87 11 my $ver = $self->{entities}[$next]->$version;
88  
89 11 $self->{available} = $self->{entities}[$next]->$entity;
90  
91 11 return $self->{entities}[$next] = $next | ( $ver << ENTITY_SHIFT );
92 3 };
93  
94 ▶my $get = sub ( $self, $unsafe, $guid, @types ) {
Subroutine __ANON__: called CC=5 SCAR=16.1
95 37 my $index = $guid->$entity;
96  
97 my @got = map {
98 37 my $set = $self->{components}{"$_"};
99 40 my $sparse = $set->[SPARSE][$index];
100  
101 40 ▶ defined($sparse) && ( $unsafe || $self->check( $guid, $_ ) )
Branch: 1 branch
BranchTrueFalseTotal
defined $sparse && ($unsafe || $self->check($guid, $_)) ? : 34 6 40
Condition100%6/6
defined $sparse and $unsafe || $self->check($guid, $_)
!ll&&!rl&&r
2434
$unsafe or $self->check($guid, $_)
l!l&&r!l&&!r
9254
MC/DC100%3/3
defined $sparse and $unsafe || $self->check($guid, $_)
defined $sparse $unsafe $self->check($guid, $_)
Truth table100%4/4
A and B or C
A= defined $sparseB= $unsafeC= $self->check($guid, $_)
ABCresult
0XX0
1000
11X1
1011
102 ? $set->[COMPONENTS][$sparse] : undef
103 } @types;
104  
105 37 ▶ return $got[0] if @types == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @types == 1 34 3 37
106 3 return @got;
107 3 };
108  
109 ## Public methods
110  
111 ▶sub new ( $class ) {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
112 8 my $self = bless {}, $class;
113 8 $self->clear;
114 8 $self;
115 }
116  
117 2 ▶sub created ($self) { scalar @{ $self->{entities} } - 1 }
Subroutine created: called CC=1 SCAR=0.0
Pod documented
118  
119 # Get the number of created entities that are still valid; that is, that have
120 # not been deleted.
121 ▶sub alive ($self) {
Subroutine alive: called CC=2 SCAR=6.9
Pod documented
122 7 my $size = @{ $self->{entities} } - 1;
123 7 my $current = $self->{available};
124  
125 7 ▶ until ( $current->$is_null ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($current->$is_null) 10 7 17
126 10 $size--;
127 10 $current = $self->{entities}[ $current->$entity ];
128 }
129  
130 7 return $size;
131 }
132  
133 # Reset the registry internal storage. All entities will be deleted, and all
134 # entity IDs will be made available.
135 ▶sub clear ($self) {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
136 13 delete $self->{view_cache};
137  
138 # Keys in this hash are component type names (ie. the result of ref),
139 # and values are sparse sets of entities that "have" that component.
140 13 delete $self->{components};
141  
142 # Parameters used for recycling entity GUIDs
143 # See https://skypjack.github.io/2019-05-06-ecs-baf-part-3
144 13 $self->{entities} = [ undef ];
145 13 $self->{available} = NULL_ENTITY;
146  
147 13 return;
148 }
149  
150 # Create a new entity
151 ▶sub create ( $self, @components ) {
Subroutine create: called CC=3 SCAR=11.0
Pod documented
152 Carp::croak 'Component must be a reference'
153 111 ▶ if List::Util::any { !ref } @components;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &List::Util::any(sub { not ref $_; } , @components) 2 109 111
154  
155 109 ▶ my $guid = $self->{available}->$is_null
Branch: 1 branch
BranchTrueFalseTotal
$self->{'available'}->$is_null ? : 98 11 109
156 ? $self->$generate_guid : $self->$recycle_guid;
157  
158 109 $self->add( $guid, @components );
159  
160 109 return $guid;
161 }
162  
163 ▶sub check ( $self, $guid, $type ) {
Subroutine check: called CC=4 SCAR=13.9
Pod documented
164 206 ▶ Carp::croak 'GUID must be defined' unless defined $guid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $guid 0 206 206
165 206 ▶ Carp::croak 'Component name must be defined and not a reference'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $type or ref $type 2 204 206
Condition100%3/3
not defined $type or ref $type
l!l&&r!l&&!r
11204
MC/DC100%2/2
not defined $type or ref $type
not defined $type ref $type
Truth table100%3/3
not(A) or B
A= not defined $typeB= ref $type
ABresult
1X1
011
000
166 if ! defined $type || ref $type;
167  
168 204 my $index = $guid->$entity;
169 204 my $set = $self->{components}{"$type"};
170  
171 204 $set->$contains( $index );
172 }
173  
174 # Add or replace a component for an entity
175 ▶sub add ( $self, $guid, @components ) {
Subroutine add: called CC=8 SCAR=20.9
Pod documented
176 232 ▶ Carp::croak 'GUID must be defined' unless defined $guid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $guid 0 232 232
177  
178 232 my $index = $guid->$entity;
179 232 for my $component (@components) {
180 155 ▶ my $name = ref($component) || Carp::croak 'Component must be a reference';
Condition66%2/3
ref $component || Carp::croak('Component must be a reference')
l!l&&r!l&&!r
15302
MC/DC0%0/2
ref $component || Carp::croak('Component must be a reference')
ref $component Carp::croak('Component must be a reference')
Truth table33%1/3
A || B
A= ref $componentB= Carp::croak('Component must be a reference')
ABresult
1X1
011
000
181  
182 # SPARSE DENSE COMPONENTS
183 # \ | /
184 153 ▶ for ( $self->{components}{$name} //= [ [], [], [] ] ) {
Condition100%2/2
$self->{"components"}{$name} //= [[], [], []]
l!l
13617
MC/DC100%1/1
$self->{"components"}{$name} //= [[], [], []]
$self->{"components"}{$name}
Truth table100%2/2
A
A= $self->{"components"}{$name}
Aresult
11
00
185 # Replace component
186 153 ▶ if ( $self->check( $guid => $name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check($guid, $name)) { } 1 152 153
187 1 $_->[COMPONENTS][ $_->[SPARSE][$index] ] = $component;
188 }
189  
190 # Add component
191 else {
192 152 push @{ $_->[COMPONENTS] }, $component;
193 152 push @{ $_->[DENSE ] }, $index;
194  
195 152 $_->[SPARSE][$index] = $#{ $_->[DENSE] };
196 }
197 }
198  
199 # Adding a component invalidates any cached view that uses it
200 153 delete $self->{view_cache}{$_} for
201 0 grep { index( $_, "|$name|" ) != -1 }
202 153 keys %{ $self->{view_cache} },
203 }
204  
205 230 return;
206 }
207  
208 # Get a component for an entity
209 # The public version of this method forwards to the "safe" flavour of the
210 # private one
211 ▶sub get ( $self, $guid, @types ) {
Subroutine get: called CC=3 SCAR=11.0
Pod documented
212 30 ▶ Carp::croak 'GUID must be defined' unless defined $guid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $guid 0 30 30
213  
214 Carp::croak 'Component name must be defined and not a reference'
215 30 ▶ if List::Util::any { !defined || ref } @types;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
if &List::Util::any(sub { not defined $_ or ref $_; } , @types) 2 28 30
not defined $_ or ref $_ 32 1 33
216  
217 28 $self->$get( 0, $guid, @types );
218 }
219  
220 ▶sub delete ( $self, $guid, @types ) {
Subroutine delete: called CC=11 SCAR=24.0
Pod documented
221 17 ▶ Carp::croak 'GUID must be defined' unless defined $guid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $guid 0 17 17
222  
223 17 ▶ unless (@types) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@types) 13 4 17
224 # Remove an entity and all its components
225 13 ▶ if ( my @all = keys %{ $self->{components} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@all) = keys %{$$self{"components"};}) 2 11 13
226 2 $self->delete( $guid, @all );
227 }
228  
229 # We mark an entity as available by splitting the entity and the version
230 # and storing the incremented version only in the entities list, and the
231 # available entity ID in the 'available' slot
232  
233 13 my $ent = $guid->$entity;
234 13 my $ver = $guid->$version + 1;
235  
236 13 $self->{entities}[$ent] = $self->{available} | ( $ver << ENTITY_SHIFT );
237 13 $self->{available} = $ent;
238  
239 13 return;
240 }
241  
242 Carp::croak 'Component name must not be a reference'
243 4 ▶ if List::Util::any { ref } @types;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &List::Util::any(sub { ref $_; } , @types) 0 4 4
244  
245 4 for my $name (@types) {
246 6 ▶ next unless $self->check( $guid, $name );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check($guid, $name) 1 5 6
247  
248 5 my $e = $guid->$entity;
249  
250 5 for ( $self->{components}{$name} ) {
251 5 my ( $i, $j ) = ( $_->[SPARSE][$e], $#{ $_->[DENSE] } );
252  
253 5 for ( $_->[DENSE], $_->[COMPONENTS] ) {
254 10 @{ $_ }[ $i, $j ] = @{ $_ }[ $j, $i ];
255 10 pop @$_;
256 }
257  
258 5 ▶ $j = $_->[DENSE][$i] // next;
Condition100%2/2
$_->[1][$i] // (next)
l!l
23
MC/DC100%1/1
$_->[1][$i] // (next)
$_->[1][$i]
Truth table100%2/2
A
A= $_->[1][$i]
Aresult
11
00
259  
260 2 $_->[SPARSE][$j] = $i;
261 }
262  
263 # Deleting a component invalidates any cached view that uses it
264 5 delete $self->{view_cache}{$_} for
265 1 grep { index( $_, "|$name|" ) != -1 }
266 5 keys %{ $self->{view_cache} },
267 }
268  
269 4 return;
270 }
271  
272 # Checks if an entity identifier refers to a valid entity; that is, one that
273 # has been created and not deleted.
274 ▶sub valid ( $self, $guid ) {
Subroutine valid: called CC=4 SCAR=14.1
Pod documented
275 20 ▶ Carp::croak 'GUID must be defined' unless defined $guid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $guid 0 20 20
276  
277 20 my $pos = $guid->$entity;
278 20 ▶ $pos < @{ $self->{entities} }
Condition66%2/3
$self->{"entities"}[$pos] // $guid + 1
l!l&&r!l&&!r
1810
MC/DC0%0/2
$self->{"entities"}[$pos] // $guid + 1
$self->{"entities"}[$pos] $guid + 1
Truth table66%2/3
A // B
A= $self->{"entities"}[$pos]B= $guid + 1
ABresult
1X1
011
000
279 20 ▶ && ( $self->{entities}[$pos] // $guid + 1 ) == $guid;
Branch: 1 branch
BranchTrueFalseTotal
$pos < @{$self->{"entities"};} and ($self->{"entities"}[$pos] // $guid + 1) == $guid 19 1 20
280 }
281  
282 ▶sub sort ( $self, $name, $comparator ) {
Subroutine sort: called CC=14 SCAR=26.7
Pod documented
283 6 ▶ my $set = $self->{components}{$name}
Condition33%1/3
$self->{"components"}{$name} // Carp::croak("Cannot sort ${name}: no such component in registry")
l!l&&r!l&&!r
600
MC/DC0%0/2
$self->{"components"}{$name} // Carp::croak("Cannot sort ${name}: no such component in registry")
$self->{"components"}{$name} Carp::croak("Cannot sort ${name}: no such component in registry")
Truth table33%1/3
A // B
A= $self->{"components"}{$name}B= Carp::croak("Cannot sort ${name}: no such component in registry")
ABresult
1X1
011
000
284 // Carp::croak "Cannot sort $name: no such component in registry";
285  
286 6 my $sparse = $set->[SPARSE];
287 6 my $dense = $set->[DENSE];
288 6 my $comps = $set->[COMPONENTS];
289  
290 # Sorting a component invalidates any cached view that uses it
291 6 delete $self->{view_cache}{$_} for
292 0 grep { index( $_, "|$name|" ) != -1 }
293 6 keys %{ $self->{view_cache} };
294  
295 6 ▶ if ( ! ref $comparator ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $comparator) 4 2 6
296 4 ▶ my $other = $self->{components}{$comparator}
Condition33%1/3
$self->{"components"}{$comparator} // Carp::croak("Cannot sort according to ${comparator}: no such component in registry")
l!l&&r!l&&!r
400
MC/DC0%0/2
$self->{"components"}{$comparator} // Carp::croak("Cannot sort according to ${comparator}: no such component in registry")
$self->{"components"}{$comparator} Carp::croak("Cannot sort according to ${comparator}: no such component in registry")
Truth table33%1/3
A // B
A= $self->{"components"}{$comparator}B= Carp::croak("Cannot sort according to ${comparator}: no such component in registry")
ABresult
1X1
011
000
297 // Carp::croak "Cannot sort according to $comparator: no such component in registry";
298  
299 4 my $j = 0;
300 4 for my $i ( 0 .. $#{ $other->[DENSE] } ) {
301 60 ▶ my $this = $dense->[$j] // die "Undefined in set";
Condition50%1/2
$dense->[$j] // die("Undefined in set")
l!l
600
MC/DC0%0/1
$dense->[$j] // die("Undefined in set")
$dense->[$j]
Truth table50%1/2
A
A= $dense->[$j]
Aresult
11
00
302 60 ▶ my $that = $other->[DENSE][$i] // die 'Undefined in other';
Condition50%1/2
$other->[1][$i] // die("Undefined in other")
l!l
600
MC/DC0%0/1
$other->[1][$i] // die("Undefined in other")
$other->[1][$i]
Truth table50%1/2
A
A= $other->[1][$i]
Aresult
11
00
303  
304 60 ▶ next unless $set->$contains($that);
Branch: 1 branch
BranchTrueFalseTotal
unless $set->$contains($that) 20 40 60
305  
306 40 ▶ $set->$swap( $this, $that ) unless $this == $that;
Branch: 1 branch
BranchTrueFalseTotal
unless $this == $that 40 0 40
307 40 $j++;
308 }
309  
310 4 return;
311 }
312  
313 # See https://skypjack.github.io/2019-09-25-ecs-baf-part-5/
314 2 ▶ if ( ( prototype($comparator) // '' ) eq '$$' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((prototype $comparator // '') eq '$$') { } 1 1 2
Condition100%2/2
prototype $comparator // ""
l!l
11
MC/DC100%1/1
prototype $comparator // ""
prototype $comparator
Truth table100%2/2
A
A= prototype $comparator
Aresult
11
00
315 @$dense = sort {
316 1 $comparator->(
317 $comps->[ $sparse->[ $a ] ],
318 $comps->[ $sparse->[ $b ] ],
319 );
320 } @$dense;
321 }
322 else {
323 1 my $caller = caller;
324 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
325 @$dense = sort {
326 1 local ${ $caller . '::a' } = $comps->[ $sparse->[ $a ] ];
327 40 local ${ $caller . '::b' } = $comps->[ $sparse->[ $b ] ];
328 40 $comparator->();
329 } @$dense;
330 }
331  
332 2 for my $curr ( 0 .. $#$dense ) {
333 30 my $next = $sparse->[ $dense->[ $curr ] ];
334  
335 30 ▶ while ( $next != $curr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next != $curr) 28 30 58
336 28 $set->$swap_components( @{ $dense }[ $curr, $next ] );
337  
338 28 $sparse->[ $dense->[ $curr ] ] = $curr;
339 28 $curr = $next;
340 28 $next = $sparse->[ $dense->[ $curr ] ];
341 }
342 }
343  
344 2 return;
345 }
346  
347 package
348 3 Game::Entities::View {
349 3 ▶ no overloading;
Subroutine BEGIN: called CC=1 SCAR=0.0
350  
351 use overload
352 12 ▶ bool => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
353 ▶ '@{}' => sub ($self, @) {
Subroutine __ANON__: called CC=1 SCAR=0.0
354 2 [ List::Util::pairs @$self ];
355 3 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
356  
357 15 ▶ sub new ( $class, @view ) { bless \@view, $class }
Subroutine new: called CC=1 SCAR=0.0
358  
359 ▶ sub each ( $self, $code ) {
Subroutine each: called CC=2 SCAR=6.9
360 7 $code->( $_->[0], @{ $_->[1] } ) for List::Util::pairs @$self
361 }
362  
363 ▶ sub first ( $self, $code ) {
Subroutine first: called CC=2 SCAR=6.9
364 2 ▶ my $res = List::Util::first { $code->( $_->[0], @{ $_->[1] } ) } List::Util::pairs @$self;
Subroutine __ANON__: called CC=1 SCAR=0.0
365 2 ▶ return $res ? ( $res->[0], @{ $res->[1] } ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$res ? : 1 1 2
366 }
367  
368 3 ▶ sub entities ($self) { ( List::Util::pairkeys @$self ) }
Subroutine entities: called CC=1 SCAR=0.0
369 8 ▶ sub components ($self) { ( List::Util::pairvalues @$self ) }
Subroutine components: called CC=1 SCAR=0.0
370 }
371  
372 ▶sub view ( $self, @types ) {
Subroutine view: called CC=10 SCAR=23.1
Pod documented
373 # Return a view for all entities
374 # The view of all entities is never cached
375 22 ▶ unless (@types) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@types) 3 19 22
376 return Game::Entities::View->new(
377 13 map {; $self->$add_version( $_->$entity ) => [] }
378 grep $self->valid( $_ ),
379 3 @{ $self->{entities} }[ 1 .. $#{ $self->{entities} } ]
380 )
381 }
382  
383 # Return a view for a single component
384 19 ▶ if ( @types == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@types == 1) 13 6 19
385 13 my ($name) = @types;
386  
387 13 ▶ return $self->{view_cache}{"|$name|"} //= do {
Condition66%2/3
$self->{"view_cache"}{"|$name|"} //= do { my $set = $self->{"components"}{$name}; my $comps = $set->[2]; "Game::Entities::View"->new(map({() = ($_, $set->[1][$_]); $self->$add_version($e), [$comps->[$i]];} 0 .. $#{$$set[1];})) }
l!l&&r!l&&!r
490
MC/DC0%0/2
$self->{"view_cache"}{"|$name|"} //= do { my $set = $self->{"components"}{$name}; my $comps = $set->[2]; "Game::Entities::View"->new(map({() = ($_, $set->[1][$_]); $self->$add_version($e), [$comps->[$i]];} 0 .. $#{$$set[1];})) }
$self->{"view_cache"}{"|$name|"} do { my $set = $self->{"components"}{$name}; my $comps = $set->[2]; "Game::Entities::View"->new(map({() = ($_, $set->[1][$_]); $self->$add_version($e), [$comps->[$i]];} 0 .. $#{$$set[1];})) }
Truth table66%2/3
A //= B
A= $self->{"view_cache"}{"|$name|"}B= do { my $set = $self->{"components"}{$name}; my $comps = $set->[2]; "Game::Entities::View"->new(map({() = ($_, $set->[1][$_]); $self->$add_version($e), [$comps->[$i]];} 0 .. $#{$$set[1];})) }
ABresult
1X1
011
000
388 9 my $set = $self->{components}{$name};
389 9 my $comps = $set->[COMPONENTS];
390  
391 Game::Entities::View->new(
392 map {
393 138 my ( $i, $e ) = ( $_, $set->[DENSE][$_] );
394 138 $self->$add_version($e) => [ $comps->[$i] ];
395 9 } 0 .. $#{ $set->[DENSE] }
396 )
397 };
398 }
399  
400 # Return a view for entities that have the specified set of components
401 6 ▶ return $self->{view_cache}{'|' . join( '|', @types ) . '|' } //= do {
Condition66%2/3
$self->{"view_cache"}{'|' . join("|", @types) . '|'} //= do { my $map = $self->{"components"}; () = (sort {@{$map->{$a}[1] // [];} <=> @{$map->{$b}[1] // [];};} @types); my $set = $self->{"components"}{$short}; my $comps = $set->[2]; my @view; while (() = each @{$$set[1];}) { do { my $guid = $self->$add_version($e); next unless &List::Util::all(sub { $self->check($guid, $_); } , @rest); push @view, $guid, [map({$_ eq $short ? $comps->[$i] : $self->$get(1, $guid, $_);} @types)] }; } ; "Game::Entities::View"->new(@view) }
l!l&&r!l&&!r
330
MC/DC0%0/2
$self->{"view_cache"}{'|' . join("|", @types) . '|'} //= do { my $map = $self->{"components"}; () = (sort {@{$map->{$a}[1] // [];} <=> @{$map->{$b}[1] // [];};} @types); my $set = $self->{"components"}{$short}; my $comps = $set->[2]; my @view; while (() = each @{$$set[1];}) { do { my $guid = $self->$add_version($e); next unless &List::Util::all(sub { $self->check($guid, $_); } , @rest); push @view, $guid, [map({$_ eq $short ? $comps->[$i] : $self->$get(1, $guid, $_);} @types)] }; } ; "Game::Entities::View"->new(@view) }
$self->{"view_cache"}{'|' . join("|", @types) . '|'} do { my $map = $self->{"components"}; () = (sort {@{$map->{$a}[1] // [];} <=> @{$map->{$b}[1] // [];};} @types); my $set = $self->{"components"}{$short}; my $comps = $set->[2]; my @view; while (() = each @{$$set[1];}) { do { my $guid = $self->$add_version($e); next unless &List::Util::all(sub { $self->check($guid, $_); } , @rest); push @view, $guid, [map({$_ eq $short ? $comps->[$i] : $self->$get(1, $guid, $_);} @types)] }; } ; "Game::Entities::View"->new(@view) }
Truth table66%2/3
A //= B
A= $self->{"view_cache"}{'|' . join("|", @types) . '|'}B= do { my $map = $self->{"components"}; () = (sort {@{$map->{$a}[1] // [];} <=> @{$map->{$b}[1] // [];};} @types); my $set = $self->{"components"}{$short}; my $comps = $set->[2]; my @view; while (() = each @{$$set[1];}) { do { my $guid = $self->$add_version($e); next unless &List::Util::all(sub { $self->check($guid, $_); } , @rest); push @view, $guid, [map({$_ eq $short ? $comps->[$i] : $self->$get(1, $guid, $_);} @types)] }; } ; "Game::Entities::View"->new(@view) }
ABresult
1X1
011
000
402 3 my $map = $self->{components};
403  
404 my ( $short, @rest ) = sort {
405 3 ▶ @{ $map->{$a}[DENSE] // [] } <=> @{ $map->{$b}[DENSE] // [] }
Condition50%2/4
$map->{$a}[1] // []
l!l
30
$map->{$b}[1] // []
l!l
30
MC/DC0%0/1
$map->{$a}[1] // []
$map->{$a}[1]
MC/DC0%0/1
$map->{$b}[1] // []
$map->{$b}[1]
Truth table50%1/2
A
A= $map->{$a}[1]
Aresult
11
00
Truth table50%1/2
A
A= $map->{$b}[1]
Aresult
11
00
406 } @types;
407  
408 3 my $set = $self->{components}{$short};
409 3 my $comps = $set->[COMPONENTS];
410  
411 3 my @view;
412 3 ▶ while ( my ( $i, $e ) = each @{ $set->[DENSE] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @{$$set[1];}) 11 3 14
413 11 my $guid = $self->$add_version($e);
414  
415 11 ▶ next unless List::Util::all { $self->check( $guid => $_ ) } @rest;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &List::Util::all(sub { $self->check($guid, $_); } , @rest) 2 9 11
416  
417 push @view, $guid => [
418 map {
419 9 ▶ $_ eq $short
Branch: 1 branch
BranchTrueFalseTotal
$_ eq $short ? : 9 9 18
420 ? $comps->[$i]
421 : $self->$get( 1, $guid, $_ )
422 } @types
423 ];
424 }
425  
426 3 Game::Entities::View->new(@view);
427 };
428 }
429  
430 ▶sub _dump_entities ( $self, @types ) {
Subroutine _dump_entities: called CC=17 SCAR=29.0
431 2 local $Data::Dumper::Terse = 1;
432 2 local $Data::Dumper::Indent = 0;
433  
434 2 my @names = @types;
435 2 ▶ @names = sort keys %{ $self->{components} } unless @types;
Branch: 1 branch
BranchTrueFalseTotal
unless @types 2 0 2
436  
437 2 my $print = ! defined wantarray;
438 2 ▶ open my $fh, '>', \my $out or $print = 1;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", \my $out or $print = 1 0 2 2
439 2 ▶ $fh = *STDOUT if $print;
Branch: 1 branch
BranchTrueFalseTotal
if $print 1 1 2
440  
441 2 my $index;
442 2 for (@names) {
443 4 ▶ next unless my $set = $self->{components}{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless my $set = $self->{"components"}{$_} 0 4 4
444 4 ▶ next unless @{ $set->[SPARSE] // [] };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$set->[0] // [];} 0 4 4
Condition50%1/2
$set->[0] // []
l!l
40
MC/DC0%0/1
$set->[0] // []
$set->[0]
Truth table50%1/2
A
A= $set->[0]
Aresult
11
00
445  
446 4 ▶ print $fh "# [$_]\n" if !@types || @names > 1;
Branch: 1 branch
BranchTrueFalseTotal
if not @types or @names > 1 4 0 4
Condition33%1/3
not @types or @names > 1
l!l&&r!l&&!r
400
MC/DC0%0/2
not @types or @names > 1
not @types @names > 1
Truth table33%1/3
not(A) or B
A= not @typesB= @names > 1
ABresult
1X1
011
000
447 4 print $fh "# SPARSE DENSE WHERE COMPONENT\n";
448  
449 4 for ( 0 .. $#{ $set->[SPARSE] } ) {
450 18 my $component = $set->[COMPONENTS][$_];
451  
452 18 ▶ print $fh sprintf "# %6s %5s %12X %s\n",
Branch: 1 branch
BranchTrueFalseTotal
defined $component ? : 12 6 18
Condition100%6/6
$set->[0][$_] // "---"
l!l
144
$set->[1][$_] // "---"
l!l
126
builtin::refaddr($component) // 0
l!l
126
MC/DC100%1/1
$set->[0][$_] // "---"
$set->[0][$_]
MC/DC100%1/1
$set->[1][$_] // "---"
$set->[1][$_]
MC/DC100%1/1
builtin::refaddr($component) // 0
builtin::refaddr($component)
Truth table100%2/2
A
A= $set->[0][$_]
Aresult
11
00
Truth table100%2/2
A
A= $set->[1][$_]
Aresult
11
00
Truth table100%2/2
A
A= builtin::refaddr($component)
Aresult
11
00
453 $set->[SPARSE][$_] // '---',
454 $set->[DENSE][$_] // '---',
455 Scalar::Util::refaddr($component) // 0,
456 defined $component
457 ? Data::Dumper::Dumper($component) =~ s/[\n\r]//gr : '---';
458 }
459  
460 4 ▶ print $fh "#\n" if $index++ < $#names;
Branch: 1 branch
BranchTrueFalseTotal
if $index++ < $#names 2 2 4
461 }
462  
463 2 ▶ $out unless $print;
Branch: 1 branch
BranchTrueFalseTotal
unless $print 1 1 2
464 }
465  
466 # Clean our namespace
467 3 delete $Game::Entities::{$_} for qw(
468 COMPONENTS
469 DENSE
470 ENTITY_MASK
471 ENTITY_SHIFT
472 NULL_ENTITY
473 SPARSE
474 VERSION_MASK
475 );
476  
477 3 1;
↑ Summary Build »