| 1 | |||||||||||||||||||||||||
| 2 | # Sensor - monitor conditions and trigger when they are met | ||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||
| 4 | package Games::3D::Sensor; | ||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||
| 6 | # (C) by Tels <http://bloodgate.com/> | ||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||
| 10 | 2 | ▶ | use Exporter; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 11 | 2 | ▶ | 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 | 2 | /; | |||||||||||||||||||||||
| 15 | 2 | ▶ | use Games::3D::Link; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 16 | 2 | ▶ | use vars qw/@ISA @EXPORT_OK $VERSION/; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 17 | 2 | @ISA = qw/Exporter Games::3D::Link/; | |||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||
| 19 | 2 | $VERSION = '0.01'; | |||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||
| 21 | sub COND_MET() { 1; } | ||||||||||||||||||||||||
| 22 | sub COND_NOT_MET() { 2; } | ||||||||||||||||||||||||
| 23 | sub COND_UNCOND() { 3; } | ||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||
| 25 | sub SENSOR_BELOW() { 0; } | ||||||||||||||||||||||||
| 26 | sub SENSOR_OVER() { 1; } | ||||||||||||||||||||||||
| 27 | sub SENSOR_BETWEEN() { 2; } | ||||||||||||||||||||||||
| 28 | sub SENSOR_DIED() { 3; } | ||||||||||||||||||||||||
| 29 | sub SENSOR_ACTIVE() { 4; } | ||||||||||||||||||||||||
| 30 | sub SENSOR_INACTIVE() { 5; } | ||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||
| 32 | # periodic re-check required | ||||||||||||||||||||||||
| 33 | sub SENSOR_PERIODIC() { 1000; } # dont use this type, only for comp. | ||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||
| 35 | sub SENSOR_VISIBLE() { 1001; } | ||||||||||||||||||||||||
| 36 | sub SENSOR_DISTANCE() { 1002; } | ||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||
| 38 | 2 | @EXPORT_OK = qw/ | |||||||||||||||||||||||
| 39 | COND_MET COND_NOT_MET COND_UNCOND | ||||||||||||||||||||||||
| 40 | SENSOR_BELOW | ||||||||||||||||||||||||
| 41 | SENSOR_BETWEEN | ||||||||||||||||||||||||
| 42 | SENSOR_OVER | ||||||||||||||||||||||||
| 43 | SENSOR_DIED | ||||||||||||||||||||||||
| 44 | SENSOR_ACTIVE | ||||||||||||||||||||||||
| 45 | SENSOR_INACTIVE | ||||||||||||||||||||||||
| 46 | SENSOR_VISIBLE | ||||||||||||||||||||||||
| 47 | SENSOR_DISTANCE | ||||||||||||||||||||||||
| 48 | /; | ||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||
| 50 | ############################################################################## | ||||||||||||||||||||||||
| 51 | # methods | ||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||
| 53 | sub _init | ||||||||||||||||||||||||
| 54 | 3 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
_init: called CC=13 SCAR=26.7
| |||||||||||||||||||||||||
| 55 | 3 | my $self = shift; | |||||||||||||||||||||||
| 56 | 3 | my $args = $_[0]; | |||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||
| 58 | 3 | ▶ | $args = { @_ } if ref($_[0]) ne 'HASH'; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||
| 60 | 3 | my $obj = $args->{obj}; | |||||||||||||||||||||||
| 61 | 3 | my $id = $obj->{id}; | |||||||||||||||||||||||
| 62 | $self->{watch} = { | ||||||||||||||||||||||||
| 63 | 3 | $id => [ $obj, $args->{what} ], # object(s) to watch for | |||||||||||||||||||||||
| 64 | }; | ||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||
| 66 | 3 | ▶ | $self->{A} = $args->{A} || 0; # range A..B | ||||||||||||||||||||||
|
Condition50%1/2
$args->{'A'} || 0
MC/DC0%0/1
$args->{'A'} || 0
$args->{'A'}
Truth table50%1/2
A
A= $args->{'A'}
| |||||||||||||||||||||||||
| 67 | 3 | ▶ | $self->{B} = $args->{B} || $args->{A} || 0; # range A..B | ||||||||||||||||||||||
|
Condition60%3/5
$args->{'B'} || $args->{'A'} || 0
$args->{"B"} or $args->{"A"}
MC/DC0%0/2
$args->{'B'} || $args->{'A'} || 0
$args->{"B"}
$args->{"A"}
Truth table66%2/3
A or B
A= $args->{"B"}B= $args->{"A"}
| |||||||||||||||||||||||||
| 68 | warn ("A=$self->{A} > B=$self->{B}\n") if | ||||||||||||||||||||||||
| 69 | 3 | ▶ | $self->{A} > $self->{B}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 70 | 3 | ▶ | $self->{type} = $args->{type} || SENSOR_BELOW;# signal must be below A | ||||||||||||||||||||||
|
Condition100%2/2
$args->{'type'} || 0
MC/DC100%1/1
$args->{'type'} || 0
$args->{'type'}
Truth table100%2/2
A
A= $args->{'type'}
| |||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||
| 72 | 3 | $self->{periodic} = 0; # check only on change | |||||||||||||||||||||||
| 73 | $self->{periodic} = 1 | ||||||||||||||||||||||||
| 74 | 3 | ▶ | if $self->{type} > SENSOR_PERIODIC; # check periodically | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||
| 76 | 3 | ▶ | $self->{delay} = $args->{delay} || 0; # immidiately | ||||||||||||||||||||||
|
Condition50%1/2
$args->{'delay'} || 0
MC/DC0%0/1
$args->{'delay'} || 0
$args->{'delay'}
Truth table50%1/2
A
A= $args->{'delay'}
| |||||||||||||||||||||||||
| 77 | 3 | ▶ | $self->{repeat} = $args->{repeat} || 2000; # 2 seconds if count != 1 | ||||||||||||||||||||||
|
Condition100%2/2
$args->{'repeat'} || 2000
MC/DC100%1/1
$args->{'repeat'} || 2000
$args->{'repeat'}
Truth table100%2/2
A
A= $args->{'repeat'}
| |||||||||||||||||||||||||
| 78 | 3 | ▶ | $self->{rand} = $args->{rand} || 0; # exactly | ||||||||||||||||||||||
|
Condition50%1/2
$args->{'rand'} || 0
MC/DC0%0/1
$args->{'rand'} || 0
$args->{'rand'}
Truth table50%1/2
A
A= $args->{'rand'}
| |||||||||||||||||||||||||
| 79 | 3 | ▶ | $self->{count} = $args->{count} || 1; # one signal | ||||||||||||||||||||||
|
Condition50%1/2
$args->{'count'} || 1
MC/DC0%0/1
$args->{'count'} || 1
$args->{'count'}
Truth table50%1/2
A
A= $args->{'count'}
| |||||||||||||||||||||||||
| 80 | 3 | ▶ | $self->{when} = $args->{when} || COND_UNCOND; # in and outside of range | ||||||||||||||||||||||
|
Condition100%2/2
$args->{'when'} || 3
MC/DC100%1/1
$args->{'when'} || 3
$args->{'when'}
Truth table100%2/2
A
A= $args->{'when'}
| |||||||||||||||||||||||||
| 81 | 3 | $self->{fixed_output} = $args->{fixed_output};# ON and OFF are default | |||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||
| 83 | 3 | $self; | |||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||
| 86 | sub signal | ||||||||||||||||||||||||
| 87 | 0 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
signal: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||
| 88 | 0 | my ($self,$input,$sig) = @_; | |||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||
| 90 | # my $id = $input; $id = $input->{id} if ref($id); | ||||||||||||||||||||||||
| 91 | # print "# ",$self->name()," received signal $sig from $id\n"; | ||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||
| 93 | die ("Unregistered input $input tried to send signal to link $self->{id}") | ||||||||||||||||||||||||
| 94 | 0 | ▶ | if !exists $self->{inputs}->{$input}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||
| 96 | # if the signal is DIE, DESTROY yourself | ||||||||||||||||||||||||
| 97 | 0 | ▶ | if ($sig == SIG_DIE) | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||
| 99 | 0 | $self->kill(); | |||||||||||||||||||||||
| 100 | 0 | return; | |||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||
| 102 | # if the signal is ACTIVATE or DEACTIVATE, (in)activate yourself | ||||||||||||||||||||||||
| 103 | 0 | ▶ | if ($sig == SIG_ACTIVATE) | ||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||
| 105 | 0 | $self->activate(); | |||||||||||||||||||||||
| 106 | 0 | return; # don't relay this signal | |||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||
| 108 | elsif ($sig == SIG_DEACTIVATE) | ||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||
| 110 | 0 | $self->deactivate(); | |||||||||||||||||||||||
| 111 | 0 | return; # don't relay this signal | |||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||
| 114 | # ignore all other signals | ||||||||||||||||||||||||
| 115 | 0 | return; # don't relay this signal | |||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||
| 118 | BEGIN | ||||||||||||||||||||||||
| 119 | 2 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 120 | # inherit these from Thingy instead from Link: | ||||||||||||||||||||||||
| 121 | 2 | *link = \&Games::3D::Thingy::link; | |||||||||||||||||||||||
| 122 | 2 | *unlink = \&Games::3D::Thingy::unlink; | |||||||||||||||||||||||
| 123 | 2 | *add_input = \&Games::3D::Thingy::add_input; | |||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||
| 126 | sub and_gate | ||||||||||||||||||||||||
| 127 | 0 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
and_gate: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 128 | 0 | my $self = shift; | |||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||
| 130 | 0 | warn ("Sensors cannot act as AND gates.\n"); | |||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||
| 133 | sub watch | ||||||||||||||||||||||||
| 134 | 0 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
watch: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 135 | # bind an additional target to the sensor | ||||||||||||||||||||||||
| 136 | 0 | my ($self,$dst,$what) = @_; | |||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||
| 138 | 0 | my $id = $dst->{id}; | |||||||||||||||||||||||
| 139 | $self->{watch} = { | ||||||||||||||||||||||||
| 140 | 0 | $id => [ $dst, $what ], # object(s) to watch for | |||||||||||||||||||||||
| 141 | }; | ||||||||||||||||||||||||
| 142 | 0 | $self; | |||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||
| 145 | sub when | ||||||||||||||||||||||||
| 146 | 0 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
when: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||
| 147 | 0 | my ($self) = shift; | |||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||
| 149 | 0 | ▶ | $self->{when} = shift if @_ > 0; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 150 | 0 | $self->{when}; | |||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||
| 153 | sub type | ||||||||||||||||||||||||
| 154 | 0 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
type: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||
| 155 | 0 | my ($self) = shift; | |||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||
| 157 | 0 | ▶ | $self->{type} = shift if @_ > 0; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 158 | 0 | $self->{type}; | |||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||
| 161 | 2 | 1; | |||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||
| 163 | __END__ | ||||||||||||||||||||||||