Esempio n. 1
0
        public void Generate(int minHealth, int maxHealth)
        {
            if (this.state == State.Stopped)
            {
                Random r = new Random();
                for (int i = 0; i < sizeX; i++)
                {
                    for (int j = 0; j < sizeY; j++)
                    {
                        int val = r.Next(minHealth, maxHealth);
                        data[i, j] = new Victim(val);
                    }
                }

                if (this.FieldProgressEvent != null)
                {
                    this.FieldProgressEvent();
                }
            }
        }
Esempio n. 2
0
        public void Generate(int minHealth, int maxHealth)
        {
            if (this.state == State.Stopped)
              {
            Random r = new Random();
            for (int i = 0; i < sizeX; i++)
            {
              for (int j = 0; j < sizeY; j++)
              {
            int val = r.Next(minHealth, maxHealth);
            data[i, j] = new Victim(val);
              }
            }

            if (this.FieldProgressEvent != null)
            {
              this.FieldProgressEvent();
            }
              }
        }
Esempio n. 3
0
        public void Start()
        {
            if (this.state != State.Stopped)
            {
                return;
            }

            this.state = State.Started;

            Thread t = new Thread(() =>
            {
                Logger.Instance.Add("global", "", "----Start----");
                bool activityPresent = true;
                while (activityPresent)
                {
                    Logger.Instance.Add("global", "", "-----");

                    activityPresent = false;

                    for (int i = 0; i < sizeX; i++)
                    {
                        for (int j = 0; j < sizeY; j++)
                        {
                            Victim v = this.data[i, j];
                            if (v.IsInfected && !v.IsDead)
                            {
                                foreach (InfectionSpeciman inf in v.Infections)
                                {
                                    if (inf.IsDead)
                                    {
                                        continue;
                                    }
                                    activityPresent = true;

                                    Logger.Instance.Add("infection", inf.GetHashCode().ToString(), String.Format("On victim cell: {0}x{1}", i, j));

                                    int eaten    = Math.Min(v.Health, inf.Type.Aggression);
                                    v.Health    -= eaten;
                                    inf.Balance += eaten;
                                    Logger.Instance.Add("infection", inf.GetHashCode().ToString(), "Eaten: " + eaten);

                                    inf.Balance -= Consumption.ofSize(inf.Type);  // self-feeding
                                    inf.Balance -= Consumption.ofStore(inf.Type); // each store slot consumes one point (doesn't store, just consume)
                                    Logger.Instance.Add("infection", inf.GetHashCode().ToString(), "Spent on self: " + Consumption.ofSize(inf.Type));
                                    Logger.Instance.Add("infection", inf.GetHashCode().ToString(), "Spent on store: " + Consumption.ofStore(inf.Type));

                                    inf.SpreadCounter++;
                                    if (!inf.IsDead && inf.IsSpreadTime)
                                    {
                                        List <KeyValuePair <Victim, double> > nb =
                                            this.getNeighbours(i, j, inf.Type.SpreadDistance)
                                            .Where((v1) => !v1.Key.IsInfected && !v1.Key.IsDead && v1.Key.Health <= inf.Type.Size * Consumption.POWER)
                                            .ShuffleNeighbours((kvp) => kvp.Key.Health, inf.Type.StrengthPref)
                                            .ToList();
                                        for (int k = 0; k < inf.Type.SpreadArea; k++)
                                        {
                                            if (k < nb.Count)
                                            {
                                                inf.Balance -= Consumption.ofSpread(inf.Type, nb[k].Value);
                                                Logger.Instance.Add("infection", inf.GetHashCode().ToString(), "Spent on spread: " + Consumption.ofSpread(inf.Type, nb[k].Value));
                                                if (!inf.IsDead)
                                                {
                                                    InfectionSpeciman newSpec = inf.Clone();
                                                    nb[k].Key.Infect(newSpec);
                                                    Logger.Instance.Add("infection", inf.GetHashCode().ToString(), String.Format("Spread to victim with health: {0}", nb[k].Key.Health));
                                                }
                                                else
                                                {
                                                    break;
                                                }
                                            }
                                        }
                                    }

                                    if (!inf.IsDead && inf.Balance > 0) // storing left
                                    {
                                        inf.Balance = Math.Min(inf.Balance, inf.Type.StoreSize);
                                    }
                                }

                                if (!v.IsDead)
                                {
                                    List <InfectionSpeciman> dead = v.Infections.Where((inf) => inf.IsDead).ToList();
                                    foreach (var inf in dead)
                                    {
                                        v.Cure(inf);
                                    }
                                }
                            }
                        }
                    }

                    if (this.FieldProgressEvent != null)
                    {
                        this.FieldProgressEvent();
                    }

                    if (this.stopEvent.WaitOne(Field.STEP_TIME))
                    {
                        break;
                    }
                    this.step++;
                }

                Logger.Instance.Add("global", "", "-----End-----");
                this.state = State.Stopped;

                if (this.FieldProgressEvent != null)
                {
                    this.FieldProgressEvent();
                }
            });

            t.Start();
        }