| 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
Condition33%1/3
$ls == $le and $rs == $re
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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)
MC/DC100%1/1
$set->[0][$index] // (return)
$set->[0][$index]
Truth table100%2/2
A
A= $set->[0][$index]
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 89 | ▶ | return ( $set->[DENSE][$sparse] // $index + 1 ) == $index; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$set->[1][$sparse] // $index + 1
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
defined $sparse and $unsafe || $self->check($guid, $_)
$unsafe or $self->check($guid, $_)
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, $_)
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | ? $set->[COMPONENTS][$sparse] : undef | ||||||||||||||||||||||||||||||||||||||||||
| 103 | } @types; | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 37 | ▶ | return $got[0] if @types == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 109 | ▶ | my $guid = $self->{available}->$is_null | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 206 | ▶ | Carp::croak 'Component name must be defined and not a reference' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $type or ref $type
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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')
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')
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | # SPARSE DENSE COMPONENTS | ||||||||||||||||||||||||||||||||||||||||||
| 183 | # \ | / | ||||||||||||||||||||||||||||||||||||||||||
| 184 | 153 | ▶ | for ( $self->{components}{$name} //= [ [], [], [] ] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"components"}{$name} //= [[], [], []]
MC/DC100%1/1
$self->{"components"}{$name} //= [[], [], []]
$self->{"components"}{$name}
Truth table100%2/2
A
A= $self->{"components"}{$name}
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | # Replace component | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 153 | ▶ | if ( $self->check( $guid => $name ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 17 | ▶ | unless (@types) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | # Remove an entity and all its components | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 13 | ▶ | if ( my @all = keys %{ $self->{components} } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | 4 | for my $name (@types) { | |||||||||||||||||||||||||||||||||||||||||
| 246 | 6 | ▶ | next unless $self->check( $guid, $name ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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)
MC/DC100%1/1
$_->[1][$i] // (next)
$_->[1][$i]
Truth table100%2/2
A
A= $_->[1][$i]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | 20 | my $pos = $guid->$entity; | |||||||||||||||||||||||||||||||||||||||||
| 278 | 20 | ▶ | $pos < @{ $self->{entities} } | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"entities"}[$pos] // $guid + 1
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
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 20 | ▶ | && ( $self->{entities}[$pos] // $guid + 1 ) == $guid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 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")
MC/DC0%0/1
$dense->[$j] // die("Undefined in set")
$dense->[$j]
Truth table50%1/2
A
A= $dense->[$j]
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | 60 | ▶ | my $that = $other->[DENSE][$i] // die 'Undefined in other'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$other->[1][$i] // die("Undefined in other")
MC/DC0%0/1
$other->[1][$i] // die("Undefined in other")
$other->[1][$i]
Truth table50%1/2
A
A= $other->[1][$i]
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | 60 | ▶ | next unless $set->$contains($that); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | 40 | ▶ | $set->$swap( $this, $that ) unless $this == $that; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
prototype $comparator // ""
MC/DC100%1/1
prototype $comparator // ""
prototype $comparator
Truth table100%2/2
A
A= prototype $comparator
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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];}))
}
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];}))
}
| |||||||||||||||||||||||||||||||||||||||||||
| 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)
}
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)
}
| |||||||||||||||||||||||||||||||||||||||||||
| 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] // []
$map->{$b}[1] // []
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]
Truth table50%1/2
A
A= $map->{$b}[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||
| 417 | push @view, $guid => [ | ||||||||||||||||||||||||||||||||||||||||||
| 418 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 419 | 9 | ▶ | $_ eq $short | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | 2 | my $print = ! defined wantarray; | |||||||||||||||||||||||||||||||||||||||||
| 438 | 2 | ▶ | open my $fh, '>', \my $out or $print = 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 439 | 2 | ▶ | $fh = *STDOUT if $print; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||
| 441 | 2 | my $index; | |||||||||||||||||||||||||||||||||||||||||
| 442 | 2 | for (@names) { | |||||||||||||||||||||||||||||||||||||||||
| 443 | 4 | ▶ | next unless my $set = $self->{components}{$_}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 444 | 4 | ▶ | next unless @{ $set->[SPARSE] // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$set->[0] // []
MC/DC0%0/1
$set->[0] // []
$set->[0]
Truth table50%1/2
A
A= $set->[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||
| 446 | 4 | ▶ | print $fh "# [$_]\n" if !@types || @names > 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not @types or @names > 1
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
$set->[0][$_] // "---"
$set->[1][$_] // "---"
builtin::refaddr($component) // 0
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][$_]
Truth table100%2/2
A
A= $set->[1][$_]
Truth table100%2/2
A
A= builtin::refaddr($component)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||
| 463 | 2 | ▶ | $out unless $print; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||||||||||||