public static void ConstrainVectorInChunk(ref Vector Vector, ref VectorI Chunk)
        {
            bool Done;

            do
            {
                Done = true;
                if (Vector.X > ChunkSize)
                {
                    Vector -= new Vector(ChunkSize, 0);
                    Chunk   = new VectorI((Chunk.X + 1) % ChunkAmount, Chunk.Y);
                    Done    = false;
                }
                else
                if (Vector.X < 0)
                {
                    Vector += new Vector(ChunkSize, 0);
                    Chunk   = new VectorI((Chunk.X + ChunkAmount - 1) % ChunkAmount, Chunk.Y);
                    Done    = false;
                }
                if (Vector.Y > ChunkSize)
                {
                    Vector -= new Vector(0, ChunkSize);
                    Chunk   = new VectorI(Chunk.X, (Chunk.Y + 1) % ChunkAmount);
                    Done    = false;
                }
                else
                if (Vector.Y < 0)
                {
                    Vector += new Vector(0, ChunkSize);
                    Chunk   = new VectorI(Chunk.X, (Chunk.Y + ChunkAmount - 1) % ChunkAmount);
                    Done    = false;
                }
            }while (!Done);
        }
            public Leaf(Branch Parent, float Pitch, float Yaw, float Roll)
            {
                this.Parent = Parent;
                this.Pitch  = Pitch;
                this.Yaw    = Yaw;
                this.Roll   = Roll;
                Tree        = Parent.Tree;
                Base        = Parent.Tip;
                RotMatrix   = new Matrix3(Parent.RotMatrix);
                //RotMatrix = RotMatrix.RotZ(Yaw).RotX(Pitch).RotZ(Roll);
                RotMatrix = RotMatrix.RotZ(Yaw).RotY(Pitch).RotZ(Roll);
                Tip1      = RotMatrix * new Vector3(0, 1, 2);
                Tip2      = RotMatrix * new Vector3(0, -1, 2);
                Norm      = Vector3.Cross(Tip1, Tip2);
                Norm.Norm();
                Tip1  += Base;
                Tip2  += Base;
                Center = (Base + Tip1 + Tip2) / 3;
                if (Center.Z > Tree.TotalHeight)
                {
                    Tree.TotalHeight = (float)Center.Z;
                }
                Vector  Pos      = new Vector((float)(Center.X) * SizeScale2d, (float)Center.Y * SizeScale2d) + Tree.Pos;
                VectorI ChunkPos = Tree.ChunkPos;

                UpdateWorld.ConstrainVectorInChunk(ref Pos, ref ChunkPos);
                this.Pos      = Pos;
                this.ChunkPos = ChunkPos;
                Col           = Tree.LeafColor;
                Vertecies     = new Vector3[] { Base, Tip1, Tip2 };
            }
        public static void AddEntity(Entity E, Vector V, VectorI C)
        {
            int  Pos       = 0;
            bool KeepGoing = true;

            while (KeepGoing)//perform insertion sort so that the entities gets drawn in the correct order
            {
                if (Pos >= Entities.Count - 1)
                {
                    Entities.Add(E);
                    KeepGoing = false;
                }
                else
                if (Entities[Pos].DrawLayer >= E.DrawLayer)
                {
                    Entities.Insert(Pos, E);
                    KeepGoing = false;
                }
                Pos++;
            }

            Chunks[C.X][C.Y].Add(E);
            E.ChunkPos = C;
            E.Pos      = V;
        }
 public Seed(Tree.Leaf.Flower Fruit, Vector Pos, VectorI Chunk)
 {
     this.Pos    = Pos;
     ChunkPos    = Chunk;
     Parent      = Fruit.Leaf.Tree;
     Parent2     = Fruit.Pollen.Leaf.Tree;
     StartEnergy = 30;
     Col         = Fruit.Col;
     Edible      = false;
 }
示例#5
0
 public Chunk(VectorI Pos)
 {
     this.Pos   = Pos;
     Entities   = new List <Entity>();
     EntitiesCM = new List <CollideMove>();
     EntitiesCS = new List <CollideStatic>();
     Nutrients  = 0;
     MaxWater   = 100;
     Water      = MaxWater;
 }
        public Tree(Vector Pos, VectorI ChunkPos, float StartEnergy = 5)
        {
            Col           = Color.SaddleBrown;
            LeafColor     = Color.ForestGreen;
            BranchColor   = Color.SaddleBrown;
            this.Pos      = Pos;
            this.ChunkPos = ChunkPos;
            Timer         = Form1.Rand.Next(MaxTimer);
            Energy        = StartEnergy;
            //Inputs: TreeAge,TreeEnergy,IsBranch,Layer,ChildBranches,ChildLeaves,LocalEnergy(if branch sum of all children),LocalAge,HasFlower,FruitRatio
            //Outputs: SpawnLeaf(if below -1, drop instead),LeafAngle1,LeafAngle2,LeafAngle3,SpawnBranch,BranchAngle1,BranchAngle2,ThickerBranch,LongerBranch,SpawnSeed,SpawnFlower,FruitRate
            int InputCount  = 10;
            int OutputCount = 12;

            NeuralNetwork = new FeedForwardNetwork(InputCount, OutputCount, 8, 1, NeuralNetwork.OutputType.Linear);
            Inputs        = new float[InputCount];
            FeedForwardNetwork N = NeuralNetwork as FeedForwardNetwork;

            /*for (int i = 0; i < N.WeightMatrix[0].Length; i++)
             * {
             *  N.WeightMatrix[0][i][0] = 0;
             * }
             * for (int i = 0; i < N.WeightMatrix[1].Length; i++)
             * {
             *  N.WeightMatrix[1][i][9] = 0;
             * }*/

            /*N.WeightMatrix[N.ToIndex(0,3,0)] = 0.1f;
             * N.WeightMatrix[N.ToIndex(0, 1, 0)] = 2;
             * N.WeightMatrix[N.ToIndex(0, 10, 0)] = -2;
             * N.WeightMatrix[N.ToIndex(1, 0, 9)] = 1;
             *
             * //N.WeightMatrix[0][0][0] = 1;
             * N.WeightMatrix[N.ToIndex(0, 5, 1)] = -1;
             * N.WeightMatrix[N.ToIndex(0, 3, 1)] = -0.5f;
             * N.WeightMatrix[N.ToIndex(0, 10, 1)] = -0.3f;
             * N.WeightMatrix[N.ToIndex(0, 1, 1)] = 1;
             * N.WeightMatrix[N.ToIndex(1, 1, 0)] = 1;
             * N.WeightMatrix[N.ToIndex(1, 1, 4)] = 1;
             *
             * N.WeightMatrix[N.ToIndex(0, 6, 2)] = 2;
             * N.WeightMatrix[N.ToIndex(1, 8, 1)] = 0.5f;
             * N.WeightMatrix[N.ToIndex(1, 2, 2)] = 2f;
             *
             * //N.WeightMatrix[1][8][9] = -1f;
             *
             * N.Mutate(MutationRate);*/
            N.GenerateRandom(-2, 2);

            Root = new Branch(this, null, 3, 0, 0);
            AddLeaf(Root, new Leaf(Root, 40, Form1.Rand.Next(0, 360), 0));
            Branches.Add(Root);

            UpdateArea();
        }
        public static Vector GetRelativePosition(VectorI Chunk1, Vector V1, VectorI Chunk2, Vector V2)
        {
            VectorI RelativeChunk   = Chunk2 - Chunk1;
            Vector  RelativeInChunk = V2 - V1;
            float   X = (RelativeChunk.X) * ChunkSize;

            if (RelativeChunk.X > 0)
            {
                if (RelativeChunk.X * 2 > ChunkAmount)
                {
                    X -= ChunkAmount * ChunkSize;
                }
            }
            else
            {
                if (RelativeChunk.X * 2 < -ChunkAmount)
                {
                    X += ChunkAmount * ChunkSize;
                }
            }
            X += RelativeInChunk.X;
            float Y = (RelativeChunk.Y) * ChunkSize;

            if (RelativeChunk.Y > 0)
            {
                if (RelativeChunk.Y * 2 > ChunkAmount)
                {
                    Y -= ChunkAmount * ChunkSize;
                }
            }
            else
            {
                if (RelativeChunk.Y * 2 < -ChunkAmount)
                {
                    Y += ChunkAmount * ChunkSize;
                }
            }
            Y += RelativeInChunk.Y;
            return(new Vector(X, Y));
        }
        public Tree(Tree Parent, Vector Pos, VectorI ChunkPos)
        {
            Col           = Color.SaddleBrown;
            LeafColor     = Color.ForestGreen;
            BranchColor   = Color.SaddleBrown;
            Timer         = Form1.Rand.Next(MaxTimer);
            Generation    = Parent.Generation + 1;
            NeuralNetwork = new FeedForwardNetwork(Parent.NeuralNetwork as FeedForwardNetwork);

            MutationRate = Parent.MutationRate * (1 + NeuralNetwork.Gaussian() * 0.2f);
            LimitMax(ref MutationRate, 0.01f);
            NeuralNetwork.Mutate(MutationRate);


            Inputs        = new float[Parent.Inputs.Length];
            this.Pos      = Pos;
            this.ChunkPos = ChunkPos;
            Root          = new Branch(this, null, 3, 0, Form1.Rand.Next(0, 360));
            AddLeaf(Root, new Leaf(Root, 40, Form1.Rand.Next(0, 360), 0));

            Branches.Add(Root);
            Col = Color.ForestGreen;
            UpdateArea();
        }
 public void ScreenToWorld(float x, float y, out Vector V, out VectorI C)
 {
     V = new Vector((x - Form1.PB.Width / 2) / Zoom + CameraPos2.X, (y - Form1.PB.Height / 2) / Zoom + CameraPos2.Y);
     C = CameraChunk;
     UpdateWorld.ConstrainVectorInChunk(ref V, ref C);
 }
        public PointF WorldToScreen(VectorI Chunk, Vector Pos)
        {
            Vector V = UpdateWorld.GetRelativePosition(CameraChunk, CameraPos2, Chunk, Pos) * Zoom;

            return(new PointF(V.X, V.Y));
        }
        void DrawWorld()
        {
            if (Selected != null)
            {
                CameraPos2  = Selected.Pos;
                CameraChunk = Selected.ChunkPos;
                if (Selected is Creature C)
                {
                    C.Selected = true;
                }
                //if(Lock)
                //Graphics.RotateTransform(-SelectedCreature.Angle-90);
                CameraPosition = Selected.Pos.ToPoint();

                /*if (BlindMode)
                 * {
                 *  PointF P = WorldToScreen(SelectedCreature.Pos.X, SelectedCreature.Pos.Y);
                 *  SelectedCreature.Show(this, P.X, P.Y, Zoom);
                 * }*/
            }

            Graphics.TranslateTransform(Size.Width / 2, Size.Height / 2);
            VectorI ChunkOffset = new VectorI(UpdateWorld.ChunkAmount / 2, UpdateWorld.ChunkAmount / 2);

            for (int i = 0; i < UpdateWorld.ChunkAmount; i++)
            {
                for (int j = 0; j < UpdateWorld.ChunkAmount; j++)
                {
                    PointF Pos = new PointF(
                        ((i - ChunkOffset.X) * UpdateWorld.ChunkSize - CameraPos2.X) * Zoom,
                        ((j - ChunkOffset.Y) * UpdateWorld.ChunkSize - CameraPos2.Y) * Zoom);
                    Graphics.DrawString(
                        ((i - ChunkOffset.X + CameraChunk.X + UpdateWorld.ChunkAmount) % UpdateWorld.ChunkAmount).ToString() + "\n" +
                        ((j - ChunkOffset.Y + CameraChunk.Y + UpdateWorld.ChunkAmount) % UpdateWorld.ChunkAmount).ToString(), new Font("Arial Black", Zoom * 20), Brushes.Black, Pos);
                    Graphics.DrawRectangle(new Pen(Color.Black, 1),
                                           Pos.X,
                                           Pos.Y,
                                           UpdateWorld.ChunkSize * Zoom, UpdateWorld.ChunkSize * Zoom);
                }
            }

            /*if (SelectedCreature == null || !BlindMode)
             * {
             *  foreach (var E in Form1.Eggs)
             *  {
             *      PointF P = WorldToScreen(E.Pos.X, E.Pos.Y);
             *      E.Show(this, P.X, P.Y, Zoom);
             *  }
             *  Font FontC = new Font(Font.Name, 15 * Zoom);
             *  foreach (var C in Form1.Creatures)
             *  {
             *      PointF P = WorldToScreen(C.Pos.X, C.Pos.Y);
             *      C.Show(this, P.X, P.Y, Zoom);
             *  }
             *  foreach (var S in Form1.Seeds)
             *  {
             *      PointF P = WorldToScreen(S.Pos.X, S.Pos.Y);
             *      S.Show(this, P.X, P.Y, Zoom);
             *  }
             *  foreach (var C in Form1.Creatures)
             *  {
             *      PointF P = WorldToScreen(C.Pos.X, C.Pos.Y);
             *      Graphics.DrawString(C.Name, FontC, Brushes.Black, P.X, P.Y - C.Radius * Zoom * 1.5f, SF);
             *  }
             *  foreach (var T in Form1.Trees)
             *  {
             *      PointF P = WorldToScreen(T.Pos.X, T.Pos.Y);
             *      T.Show2D(this, P.X, P.Y, Zoom);
             *  }
             *
             * }*/
            Font FontC = new Font("Arial Black", 15 * Zoom);

            foreach (Tree T in UpdateWorld.Trees)
            {
                PointF P = WorldToScreen(T.ChunkPos, T.Pos);
                T.ShowShadows(this, P.X, P.Y, Zoom);
            }
            foreach (Entity E in UpdateWorld.Entities)
            {
                PointF P = WorldToScreen(E.ChunkPos, E.Pos);
                E.Show(this, P.X, P.Y, Zoom);
                if (E is Creature C)
                {
                    Graphics.DrawString(C.Name, FontC, Brushes.Black, P.X, P.Y - C.Radius * Zoom * 1.5f, SF);
                }
            }
            Graphics.ResetTransform();
        }
 public void GetRandomPosition(out Vector V, out VectorI C)
 {
     V = new Vector(Form1.Rand.Next((int)ChunkSize), Form1.Rand.Next((int)ChunkSize));
     C = new VectorI(Form1.Rand.Next(ChunkAmount), Form1.Rand.Next(ChunkAmount));
 }