예제 #1
0
        public override void Fire()
        {
            Init();  //be sure to leave this here

            if (GetNeuronValue("Auto") == 0)
            {
                return;
            }
            goToGoal = GetNeuronValue("Goal") == 1;

            ModuleBehavior mBehavior = (ModuleBehavior)FindModleu(typeof(ModuleBehavior));

            if (mBehavior == null)
            {
                return;
            }
            Module2DModel mModel = (Module2DModel)FindModleu(typeof(Module2DModel));

            if (mModel is null)
            {
                return;
            }
            ModuleEvent mEvent = (ModuleEvent)FindModleu(typeof(ModuleEvent));

            if (mEvent == null)
            {
                return;
            }
            ModuleUKSN UKS = (ModuleUKSN)FindModleu(typeof(ModuleUKSN));

            if (UKS == null)
            {
                return;
            }

            if (GetNeuronValue("ModuleBehavior", "Done") == 0)
            {
                return;
            }


            mModel.GetSegmentsFromUKS();
            IList <Thing> segments = mModel.GetUKSSegments();

            if (segments.Count == 0)
            {
                return;
            }
            Thing t = segments[0]; //TODO: this only works with just a single item in the UKS

            //figure out if any motion occured
            Segment s = Module2DModel.SegmentFromUKSThing(t);

            Module2DModel.OrderSegment(s);

            if (GetNeuronValue("E0") == 1)
            {
                GoToLandmark(UKS.Labeled("E0").References[0].T, s);
                doPush    = 2;
                doBackOff = true;
                return;
            }
            if (GetNeuronValue("E1") == 1)
            {
                GoToLandmark(UKS.Labeled("E1").References[0].T, s);
                doPush    = 2;
                doBackOff = true;
                return;
            }
            if (GetNeuronValue("E2") == 1)
            {
                GoToLandmark(UKS.Labeled("E2").References[0].T, s);
                doPush    = 2;
                doBackOff = true;
                return;
            }


            if (doPush != 0)
            {
                if (doPush == 2)
                {
                    Push(s);
                }
                doPush--;
                return;
            }
            if (doFaceSegment)
            {
                DoFaceSegment(s);
                doFaceSegment = false;
                return;
            }

            Segment s1;

            if (lastPosition == null) //create objects to keep track of the target and last position
            {
                s1                 = s.Clone();
                lastPosition       = mModel.AddSegmentToUKS(s1);
                lastPosition.Label = "LastPosition";
                lastPosition.RemoveParent(UKS.Labeled("Segment"));
                lastPosition.AddParent(UKS.Labeled("SSegment"));
                Module2DSim mSim = (Module2DSim)FindModleu(typeof(Module2DSim));
                if (mSim is null)
                {
                    return;
                }
                Segment motionTarget = mSim.GetMotionTarget();
                if (motionTarget == null)
                {
                    motionTarget          = new Segment();
                    motionTarget.P1       = new PointPlus(4, 1.5f);
                    motionTarget.P2       = new PointPlus(4, -2.5f);
                    motionTarget.theColor = (ColorInt)0xff;
                }
                endTarget       = mModel.AddSegmentToUKS(motionTarget);
                endTarget.Label = "EndTarget";
                //endTarget.RemoveParent(UKS.Labeled("Segment"));
                //endTarget.AddParent(UKS.Labeled("SSegment"));
            }
            else
            {
                s1 = Module2DModel.SegmentFromUKSThing(lastPosition);
            }

            //get motion from subtracting and then updating last position
            Angle rotation = s.Angle - s1.Angle;

            if (rotation > PI / 2)
            {
                rotation = PI - rotation;
            }
            if (rotation < -PI / 2)
            {
                rotation = PI + rotation;
            }
            Motion motion = new Motion()
            {
                P        = (Point)s.MidPoint.V - s1.MidPoint.V,
                rotation = rotation,
            };

            lastPosition.References[0].T.V = s.P1.Clone();
            lastPosition.References[1].T.V = s.P2.Clone();

            if (Abs(motion.R) > .01 || Abs(motion.rotation) > .05 && !goToGoal && !doBackOff)
            {
                //check for existing Event
                Thing currentEvent = MostLikelyEvent(t);
                if (currentEvent == null)
                {                //add new Event
                    Thing lm1 = mEvent.CreateLandmark(new List <Thing>()
                    {
                        t
                    });
                    Thing t1 = mEvent.CreateEvent(lm1);
                    Thing t3 = UKS.AddThing("m" + motionCount++, UKS.Labeled("Motion"), motion);
                    mEvent.AddOutcomePair(t1, UKS.GetOrAddThing("Push", "Action"), t3);
                }
                return;
            }

            if (doBackOff)
            {
                DoBackOff(s);
                doBackOff     = false;
                doFaceSegment = true;
                return;
            }

            if (goToGoal)
            {
                if (endTarget != null)
                {
                    Segment s2 = Module2DModel.SegmentFromUKSThing(endTarget);
                    GoToGoal(s, s2);
                    return;
                }
            }

            Explore(s);
        }
예제 #2
0
        public override bool Draw(bool checkDrawTimer)
        {
            if (!base.Draw(checkDrawTimer))
            {
                return(false);
            }

            Module2DModel parent = (Module2DModel)base.ParentModule;

            theCanvas.Children.Clear();
            Point windowSize   = new Point(theCanvas.ActualWidth, theCanvas.ActualHeight);
            Point windowCenter = new Point(windowSize.X / 2, windowSize.Y / 2);

            float scale = (float)Math.Min(windowSize.X, windowSize.Y) / 12;

            if (scale == 0)
            {
                return(false);
            }
            TransformGroup tg = new TransformGroup();

            tg.Children.Add(new RotateTransform(90));
            tg.Children.Add(new ScaleTransform(scale, -scale, 0, 0));// windowCenter.X, windowCenter.Y));
            tg.Children.Add(new TranslateTransform(windowCenter.X, windowCenter.Y));
            theCanvas.RenderTransform = tg;

            //draw an origin point
            theCanvas.Children.Add(new Line
            {
                X1 = -.20,
                X2 = .20,
                Y1 = 0,
                Y2 = 0,
                StrokeThickness = 1 / scale,
                Stroke          = Brushes.Black
            });
            theCanvas.Children.Add(new Line
            {
                X1 = 0,
                X2 = 0,
                Y1 = -.20,
                Y2 = .20,
                StrokeThickness = 1 / scale,
                Stroke          = Brushes.Black
            });

            //draw possible points;
            try
            {
                foreach (Thing t in parent.GetUKSPoints() ?? Enumerable.Empty <Thing>())
                {
                    if (t.V is PointPlus P1 && !float.IsInfinity(P1.X) && !float.IsInfinity(P1.Y))
                    {
                        theCanvas.Children.Add(new Line
                        {
                            X1 = P1.X,
                            X2 = P1.X,
                            Y1 = P1.Y,
                            Y2 = P1.Y,
                            StrokeThickness    = 3 / scale,
                            StrokeEndLineCap   = PenLineCap.Round,
                            StrokeStartLineCap = PenLineCap.Round,
                            // Stroke = new SolidColorBrush(P1.TheColor)
                            Stroke = new SolidColorBrush(Colors.Orange)
                        });
                    }
                }
            }
            catch { }
            //draw the objects'
            try
            {
                foreach (Thing t in parent.GetUKSSegments() ?? Enumerable.Empty <Thing>())
                {
                    Segment segment = Module2DModel.SegmentFromUKSThing(t);
                    if (float.IsNaN(segment.P1.R))
                    {
                        continue;
                    }
                    Color theColor = Utils.IntToColor(segment.theColor);
                    Point P1       = segment.P1.P;
                    Point P2       = segment.P2.P;
                    Point P1P      = P1 + (P2 - P1) * segment.P1.Conf / 4;       // .2;
                    Point P2P      = P1 + (P2 - P1) * (1 - segment.P2.Conf / 4); // .8;

                    theCanvas.Children.Add(new Line
                    {
                        X1 = P1.X,
                        X2 = P2.X,
                        Y1 = P1.Y,
                        Y2 = P2.Y,
                        StrokeThickness = 4 / scale,
                        Stroke          = new SolidColorBrush(theColor),
                    });

                    if (segment.P1.Conf != 0)
                    {
                        theCanvas.Children.Add(new Line
                        {
                            X1 = P1.X,
                            X2 = P1P.X,
                            Y1 = P1.Y,
                            Y2 = P1P.Y,
                            StrokeThickness = 4 / scale,
                            Stroke          = new SolidColorBrush(Colors.White),
                        });
                    }
                    if (segment.P2.Conf != 0)
                    {
                        theCanvas.Children.Add(new Line
                        {
                            X1 = P2.X,
                            X2 = P2P.X,
                            Y1 = P2.Y,
                            Y2 = P2P.Y,
                            StrokeThickness = 4 / scale,
                            Stroke          = new SolidColorBrush(Colors.White),
                        });
                    }
                }
            }
            catch { }

            if (parent.imagining)
            {
                //draw any imagined objects
                for (int i = 0; i < parent.imagination.Count; i++)
                {
                    Color theColor = Utils.IntToColor(parent.imagination[i].theColor);
                    Point P1       = parent.imagination[i].P1.P;
                    Point P2       = parent.imagination[i].P2.P;
                    Point P1P      = P1 + (P2 - P1) * .2;
                    Point P2P      = P1 + (P2 - P1) * .8;

                    theCanvas.Children.Add(new Line
                    {
                        X1 = P1.X,
                        X2 = P2.X,
                        Y1 = P1.Y,
                        Y2 = P2.Y,
                        StrokeThickness = 4 / scale,
                        Stroke          = new SolidColorBrush(theColor),
                        Opacity         = .5
                    });
                }


                LinearGradientBrush lb = new LinearGradientBrush();
                lb.StartPoint = new Point(0, 1);
                lb.EndPoint   = new Point(0, 0);
                lb.GradientStops.Add(new GradientStop(Colors.Transparent, 0.0));
                lb.GradientStops.Add(new GradientStop(Colors.Transparent, 0.4));
                lb.GradientStops.Add(new GradientStop(Colors.White, .76));
                lb.GradientStops.Add(new GradientStop(Colors.White, 1.0));
                Rectangle r = new Rectangle()
                {
                    Width   = 40,
                    Height  = 40,
                    Opacity = .75,
                    Fill    = lb
                };
                Canvas.SetTop(r, -20);
                Canvas.SetLeft(r, -20);
                theCanvas.Children.Add(r);
            }
            return(true);
        }