Ejemplo n.º 1
0
        public Habitat(EntityRandom random)
        {
            bool putBug      = random.NextBool(0.01f);
            bool putPredator = random.NextBool(0.02f);

            bug      = new GridLogic(0, putBug ? 0.01f : 0);
            predator = new GridLogic(1, putPredator ? 0.01f : 0);
        }
Ejemplo n.º 2
0
        static IEnumerable <Entity> MakeGrid2D(int horizontalResolution)
        {
            EntityRandom random = new EntityRandom(1024);

            for (int x = 0; x < horizontalResolution; x++)
            {
                for (int y = 0; y < horizontalResolution; y++)
                {
                    yield return(new Entity(
                                     new EntityID(Guid.NewGuid(), Simulation.MySpace.DeRelativate(new Vec3(0.5f + x, 0.5f + y, 0) / horizontalResolution)),
                                     Vec3.Zero,
                                     new Habitat(random),                      //check that this doesn't actually cause a fault (should get clamped)
                                     null));
                }
            }
        }
Ejemplo n.º 3
0
        public bool ExecuteMotion(ref EntityLogic.Actions actions, Entity currentState, int generation, EntityRandom random, float myYield, ref int spawn)
        {
            bool rs = false;

            switch (generation % 3)
            {
            case 0:
                //receive unicast, send broadcast
                if (animal == null)
                {
                    foreach (var msg in currentState.EnumInboundEntityMessages(channel))
                    {
                        if (!msg.IsBroadcast)                                 //coming around
                        {
                            if (animal != null)
                            {
                                throw new ExecutionException(currentState.ID, "Trying to import multiple animals");
                            }
                            animal = new Animal(msg.Payload);
                            rs     = true;
                        }
                    }
                }

                if (animal != null)
                {
                    actions.Broadcast(channel, null);                               //can i go there?
                }
                break;

            case 1:
                //receive broadcast, send unicast
                if (animal == null)
                {
                    LazyList <Actor> competitors = new LazyList <Actor>();
                    foreach (var msg in currentState.EnumInboundEntityMessages(channel))
                    {
                        if (msg.IsBroadcast && msg.Payload == null)                                 //can i go here?
                        {
                            competitors.Add(msg.Sender);
                        }
                    }
                    if (competitors.IsNotEmpty)
                    {
                        actions.Send(competitors[random.Next(competitors.Count)], channel, BitConverter.GetBytes(myYield));                                 //you can go here
                    }
                }
                break;

            case 2:
                //receive unicast, send unicast
                if (animal != null)
                {
                    LazyList <Tuple <Actor, float> > options = new LazyList <Tuple <Actor, float> >();
                    foreach (var msg in currentState.EnumInboundEntityMessages(channel))
                    {
                        if (!msg.IsBroadcast && Helper.Length(msg.Payload) == 4)                                 //i can go there
                        {
                            options.Add(new Tuple <Actor, float>(msg.Sender, BitConverter.ToSingle(msg.Payload, 0)));
                        }
                    }
                    options.Sort((a, b) => { return(a.Item2.CompareTo(b.Item2)); });
                    if (options.IsNotEmpty)
                    {
                        if (spawn > 0)
                        {
                            foreach (var o in options)
                            {
                                actions.Send(o.Item1, channel, new Animal(0).Export());                                         //coming around
                                if (--spawn <= 0)
                                {
                                    break;
                                }
                            }
                            spawn = 0;
                        }
                        else
                        {
                            actions.Send(options.Last.Item1, channel, animal.Export()); //coming around
                        }
                        animal = null;                                                  //in transit
                    }
                }
                break;
            }

            return(rs);
        }
Ejemplo n.º 4
0
        protected override void Evolve(ref Actions actions, Entity currentState, int generation, EntityRandom random, EntityRanges ranges, bool isInconsistent)
        {
#if STATE_ADV
            actions.SuppressAdvertisements = true;
#endif
            int  zero    = 0;
            bool killBug = predator.ExecuteMotion(ref actions, currentState, generation - 1, random, bug.AnimalSize, ref zero);

            food += random.NextFloat(maxFoodProduction);

            if (bug.HasAnimal)
            {
                if (killBug)
                {
                    predator.Consume(bug);
                }
                else
                {
                    float delta = Math.Min(food, 10f);
                    bug.Consume(delta);
                    food -= delta;
                    food  = Math.Max(0, food);
                    if (bug.AnimalSize > 10f)
                    {
                        bug.AnimalSize = 0.001f;
                        spawn          = 5;
                    }
                }
            }

            bug.ExecuteMotion(ref actions, currentState, generation, random, food, ref spawn);
        }
Ejemplo n.º 5
0
        protected override void Evolve(ref Actions actions, Entity currentState, int generation, EntityRandom randomSource, EntityRanges ranges, bool locationIsInconsistent)
        {
            try
            {
                FinishLoading(currentState.ID, TimeSpan.FromMilliseconds(1));
                nestedLogic.Execute(ref actions, currentState, generation, randomSource, ranges, locationIsInconsistent);

                actions.ReplaceInstantiations(inst =>
                {
                    if (inst.logic != null && !(inst.logic is DynamicCSLogic))
                    {
                        inst.logic = new DynamicCSLogic(provider, inst.logic);
                    }
                    return(inst);
                });
            }
            catch (ExecutionException ex)
            {
                if (constructor != null)
                {
                    throw new ExecutionException(currentState.ID, constructor.AssemblyName + "." + constructor.LogicName + ": " + ex.Message, ex);
                }
                throw new ExecutionException(currentState.ID, provider.AssemblyName + "." + nestedLogic.GetType() + ": " + ex.Message, ex);
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Evolves the local state, potentially generating some modifications to the base entity.
 /// The method must not change any local variables relevant to evolution. All entity modifications are limited to changes in.
 /// Evolution must be deterministic.
 /// <paramref name="newState"/>.
 /// </summary>
 /// <param name="newState">Modifications go here</param>
 /// <param name="currentState">Current entity state</param>
 /// <param name="generation">Evolution generation index, starting from 0</param>
 /// <param name="randomSource">Random source to be used exclusively for random values</param>
 /// <param name="ranges">Simulation range configuration</param>
 /// <param name="locationIsInconsistent">Set true if the location of the local entity is currently considered possibly inconsistent</param>
 protected abstract void Evolve(ref Actions newState, Entity currentState, int generation, EntityRandom randomSource, EntityRanges ranges, bool locationIsInconsistent);
Ejemplo n.º 7
0
        private int myGeneration;               //cannot initialize with anything explicitly. Assume is 0

        public void Execute(ref Actions newState, Entity currentState, int generation, EntityRandom randomSource, EntityRanges ranges, bool locationIsInconsistent)
        {
            VerifyGeneration(generation);
            Evolve(ref newState, currentState, generation, randomSource, ranges, locationIsInconsistent);
            myGeneration = generation + 2;
            //Console.WriteLine(this + "->" + myGeneration);
        }