| 1 | |||||||||||||||||||||||||||||
| 2 | # Link - link two objects together and allow sending signal(s) between them | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Games::3D::Link; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # (C) by Tels <http://bloodgate.com/> | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | require Exporter; | |||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Games::3D::Signal qw/ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | SIG_FLIP SIG_OFF SIG_DIE | ||||||||||||||||||||||||||||
| 13 | SIG_ACTIVATE SIG_DEACTIVATE | ||||||||||||||||||||||||||||
| 14 | signal_name | ||||||||||||||||||||||||||||
| 15 | 4 | /; | |||||||||||||||||||||||||||
| 16 | 4 | ▶ | use Games::3D::Thingy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use vars qw/@ISA $VERSION/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 4 | @ISA = qw/Exporter Games::3D::Thingy/; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub DEBUG () { 0; } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | ############################################################################## | ||||||||||||||||||||||||||||
| 25 | # protected class vars | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | { | ||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub add_timer { die ("You need to set a timer callback first.") }; | ||||||||||||||||||||||||||
|
Subroutine
add_timer: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 4 | my $timer = 'Games::3D::Link'; # make it point to our add_timer() | |||||||||||||||||||||||||||
| 30 | sub timer_provider | ||||||||||||||||||||||||||||
| 31 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
timer_provider: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | $timer = shift if @_ > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 0 | $timer; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | ############################################################################## | ||||||||||||||||||||||||||||
| 38 | # methods | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub _init | ||||||||||||||||||||||||||||
| 41 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 3 | $self->SUPER::_init(@_); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 3 | $self->{input_states} = {}; # for AND gates | |||||||||||||||||||||||||||
| 47 | 3 | $self->{inputs} = {}; | |||||||||||||||||||||||||||
| 48 | 3 | $self->{outputs} = {}; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 3 | $self->{count} = 1; # send signal only once | |||||||||||||||||||||||||||
| 51 | 3 | $self->{delay} = 0; # immidiately | |||||||||||||||||||||||||||
| 52 | 3 | $self->{repeat} = 2000; # 2 seconds if count != 1 | |||||||||||||||||||||||||||
| 53 | 3 | $self->{rand} = 0; # exactly | |||||||||||||||||||||||||||
| 54 | 3 | $self->{once} = 0; # not once | |||||||||||||||||||||||||||
| 55 | 3 | $self->{fixed_output} = undef; # none (just releay) | |||||||||||||||||||||||||||
| 56 | 3 | $self->{invert} = 0; # not | |||||||||||||||||||||||||||
| 57 | 3 | $self->{and} = 0; # act as OR gate | |||||||||||||||||||||||||||
| 58 | 3 | $self; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # override signal() to be more complex than Thingy's default | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub signal | ||||||||||||||||||||||||||||
| 64 | 9 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
signal: called CC=15 SCAR=34.8
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 9 | my ($self,$input,$sig) = @_; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # my $id = $input; $id = $input->{id} if ref($id); | ||||||||||||||||||||||||||||
| 68 | # print "# ",$self->name()," received signal $sig from $id\n"; | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | die ("Unregistered input $input tried to send signal to link $self->{id}") | ||||||||||||||||||||||||||||
| 71 | 9 | ▶ | if !exists $self->{inputs}->{$input}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # if the signal is DIE, DESTROY yourself | ||||||||||||||||||||||||||||
| 74 | 9 | ▶ | if ($sig == SIG_DIE) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | { | ||||||||||||||||||||||||||||
| 76 | 0 | $self->kill(); | |||||||||||||||||||||||||||
| 77 | 0 | return; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | # if the signal is ACTIVATE or DEACTIVATE, (in)activate yourself | ||||||||||||||||||||||||||||
| 80 | 9 | ▶ | if ($sig == SIG_ACTIVATE) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 81 | { | ||||||||||||||||||||||||||||
| 82 | 0 | $self->activate(); | |||||||||||||||||||||||||||
| 83 | 0 | return; # don't relay this signal | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | elsif ($sig == SIG_DEACTIVATE) | ||||||||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||||||||
| 87 | 1 | $self->deactivate(); | |||||||||||||||||||||||||||
| 88 | 1 | return; # don't relay this signal | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # AND gate: all inputs must be in the same state to send the signal | ||||||||||||||||||||||||||||
| 92 | 8 | ▶ | if ($self->{and} && scalar keys %{$self->{inputs}} > 1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"and"} and scalar keys %{$$self{"inputs"};} > 1
MC/DC0%0/2
$self->{"and"} and scalar keys %{$$self{"inputs"};} > 1
$self->{"and"}
scalar keys %{$$self{"inputs"};} > 1
Truth table66%2/3
A and B
A= $self->{"and"}B= scalar keys %{$$self{"inputs"};} > 1
| |||||||||||||||||||||||||||||
| 93 | { | ||||||||||||||||||||||||||||
| 94 | # store the signal at the input (for AND gate) | ||||||||||||||||||||||||||||
| 95 | 0 | $self->{input_states}->{$input} = $sig; | |||||||||||||||||||||||||||
| 96 | # and check the others | ||||||||||||||||||||||||||||
| 97 | 0 | my $in = $self->{input_states}; | |||||||||||||||||||||||||||
| 98 | 0 | foreach my $i (keys %$in) | |||||||||||||||||||||||||||
| 99 | { | ||||||||||||||||||||||||||||
| 100 | # if not all match yet, don't send signal | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | return if ($in->{$i} != $sig); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 8 | ▶ | return unless $self->{active} == 1; # inactive links don't send signals | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # if we need to always send the same signal, do so | ||||||||||||||||||||||||||||
| 107 | 7 | ▶ | if (defined $self->{fixed_output}) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 108 | { | ||||||||||||||||||||||||||||
| 109 | 1 | $sig = $self->{fixed_output}; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | # otherwise we might need to invert the signal to be sent | ||||||||||||||||||||||||||||
| 112 | elsif ($self->{invert}) | ||||||||||||||||||||||||||||
| 113 | { | ||||||||||||||||||||||||||||
| 114 | 0 | $sig = Games::3D::Signal::invert($sig); # invert() | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # need to delay sending, or send more than one time | ||||||||||||||||||||||||||||
| 118 | 7 | ▶ | if ($self->{count} != 1 || $self->{delay} != 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"count"} != 1 or $self->{"delay"} != 0
MC/DC0%0/2
$self->{"count"} != 1 or $self->{"delay"} != 0
$self->{"count"} != 1
$self->{"delay"} != 0
Truth table33%1/3
A or B
A= $self->{"count"} != 1B= $self->{"delay"} != 0
| |||||||||||||||||||||||||||||
| 119 | { | ||||||||||||||||||||||||||||
| 120 | timer()->add_timer( | ||||||||||||||||||||||||||||
| 121 | $self->{delay}, $self->{count}, $self->{repeat}, $self->{rand}, | ||||||||||||||||||||||||||||
| 122 | sub | ||||||||||||||||||||||||||||
| 123 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 124 | 0 | $self->output($input,$sig); | |||||||||||||||||||||||||||
| 125 | }, | ||||||||||||||||||||||||||||
| 126 | 0 | ); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | else | ||||||||||||||||||||||||||||
| 129 | { | ||||||||||||||||||||||||||||
| 130 | 7 | print '# ',$self->name()," relays ",signal_name($sig), | |||||||||||||||||||||||||||
| 131 | " from $input to outputs.\n" if DEBUG; | ||||||||||||||||||||||||||||
| 132 | # Send signal straight away. | ||||||||||||||||||||||||||||
| 133 | 7 | $self->output($input,$sig); # send $sig to all outputs | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 7 | ▶ | $self->deactivate() if $self->{once}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | sub link | ||||||||||||||||||||||||||||
| 139 | 5 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
link: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 5 | my ($self,$src,$dst) = @_; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 5 | $self->{inputs}->{$src->{id}} = $src; | |||||||||||||||||||||||||||
| 143 | 5 | ▶ | if ($self->{and} && scalar keys %{$self->{inputs}} > 1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"and"} and scalar keys %{$$self{"inputs"};} > 1
MC/DC100%2/2
$self->{"and"} and scalar keys %{$$self{"inputs"};} > 1
$self->{"and"}
scalar keys %{$$self{"inputs"};} > 1
Truth table100%3/3
A and B
A= $self->{"and"}B= scalar keys %{$$self{"inputs"};} > 1
| |||||||||||||||||||||||||||||
| 144 | { | ||||||||||||||||||||||||||||
| 145 | 1 | $self->{input_states}->{$src->{id}} = SIG_OFF; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 5 | $self->{outputs}->{$dst->{id}} = $dst; | |||||||||||||||||||||||||||
| 148 | 5 | $src->add_output($self); # the link appears as output | |||||||||||||||||||||||||||
| 149 | 5 | $dst->add_input($self); # and input at both ends | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | sub unlink | ||||||||||||||||||||||||||||
| 153 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
unlink: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 154 | # unlink all inputs and outputs from ourself | ||||||||||||||||||||||||||||
| 155 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 1 | $self->SUPER::unlink(); | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 1 | $self->{input_states} = {}; | |||||||||||||||||||||||||||
| 160 | 1 | $self; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | # override input() to also add the input state | ||||||||||||||||||||||||||||
| 164 | sub add_input | ||||||||||||||||||||||||||||
| 165 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
add_input: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | 0 | my ($self,$src) = @_; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | $self->{inputs}->{$src->{id}} = $src; | |||||||||||||||||||||||||||
| 169 | 0 | ▶ | if ($self->{and} && scalar keys %{$self->{inputs}} > 1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"and"} and scalar keys %{$$self{"inputs"};} > 1
MC/DC0%0/2
$self->{"and"} and scalar keys %{$$self{"inputs"};} > 1
$self->{"and"}
scalar keys %{$$self{"inputs"};} > 1
Truth table0%0/3
A and B
A= $self->{"and"}B= scalar keys %{$$self{"inputs"};} > 1
| |||||||||||||||||||||||||||||
| 170 | { | ||||||||||||||||||||||||||||
| 171 | 0 | $self->{input_states}->{$src->{id}} = SIG_OFF; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 0 | $self; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | sub delay | ||||||||||||||||||||||||||||
| 177 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
delay: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||
| 178 | # Sets the initial delay of the link, the delay for each consecutive signal, | ||||||||||||||||||||||||||||
| 179 | # and the randomized offset for these times. | ||||||||||||||||||||||||||||
| 180 | # Note that the second delay only comes into play if the | ||||||||||||||||||||||||||||
| 181 | # count() was set to a value different than 1, otherwise each firing of the | ||||||||||||||||||||||||||||
| 182 | # link will use the first delay again. | ||||||||||||||||||||||||||||
| 183 | 1 | my ($self,$delay,$rand,$repeat) = @_; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 1 | ▶ | $self->{delay} = abs($delay) if defined $delay; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 1 | ▶ | $self->{repeat} = abs($repeat) if defined $repeat; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 1 | ▶ | $self->{rand} = abs($rand) if defined $rand; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 1 | ($self->{delay},$self->{repeat},$self->{rand}); | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | sub count | ||||||||||||||||||||||||||||
| 192 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
count: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 193 | # Sets the count. If != 1, the outgoing signal will be resent coun() times, | ||||||||||||||||||||||||||||
| 194 | # each time delayed by a bit specified with delay(). A count of -1 means | ||||||||||||||||||||||||||||
| 195 | # infinitely. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 4 | ▶ | if (defined $_[0]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | { | ||||||||||||||||||||||||||||
| 201 | 3 | $self->{count} = shift; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | 4 | $self->{count}; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | sub once | ||||||||||||||||||||||||||||
| 207 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
once: called CC=3 SCAR=13.1
Pod
documented
| |||||||||||||||||||||||||||||
| 208 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 1 | ▶ | $self->{once} = ($_[0] ? 1 : 0) if @_ > 0; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 211 | 1 | $self->{once}; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | sub invert | ||||||||||||||||||||||||||||
| 215 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
invert: called CC=3 SCAR=13.1
Pod
documented
| |||||||||||||||||||||||||||||
| 216 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 1 | ▶ | $self->{invert} = $_[0] ? 1 : 0 if @_ > 0; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 219 | 1 | $self->{invert}; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | sub fixed_output | ||||||||||||||||||||||||||||
| 223 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
fixed_output: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 224 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 1 | ▶ | $self->{fixed_output} = shift if @_ > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | 1 | $self->{fixed_output}; | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | sub and_gate | ||||||||||||||||||||||||||||
| 231 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
and_gate: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 232 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 3 | ▶ | if (@_ > 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | { | ||||||||||||||||||||||||||||
| 236 | 2 | ▶ | $self->{and} = $_[0] ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | 3 | $self->{and}; | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 4 | 1; | |||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | __END__ | ||||||||||||||||||||||||||||