Ejemplo n.º 1
0
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            if (cc != null && cc.Length > 0)
            {
                bool findState = false;
                string endState = null;
                string state = "none";
                char tile = '\0';
                int mapWidth = -1;
                Dictionary<string, float> values = new Dictionary<string, float>();
                int numAnimals = 0;
                int oldPercent = -1;

                for (int i = 0; i < cc.Length; i++)
                {
                    string line = cc[i];
                    if (line == "--")
                    {
                        findState = true;
                        continue;
                    }

                    int percent = (int)(((float)i / (float)cc.Length) * 100);
                    if (percent != oldPercent)
                    {
                        oldPercent = percent;
                        worker.ReportProgress(percent);
                    }

                    if (findState)
                    {
                        findState = false;
                        endState = line;
                    }

                    if (state == null || state.Equals("none", StringComparison.CurrentCultureIgnoreCase) ||
                        state.Equals("TileData", StringComparison.CurrentCultureIgnoreCase))
                    {

                    }
                    else if (state.Equals("tiles", StringComparison.CurrentCultureIgnoreCase))
                    {
                        if (mCharCount < mColors.Length)
                        {
                            if (tile == '\0')
                            {
                                tile = line[0];
                            }
                            else
                            {
                                int t = int.Parse(line);
                                if (!mColourMap.ContainsKey(tile))
                                {
                                    Console.WriteLine("Adding char {0} with colour {1}", tile, mColors[t].ToKnownColor().ToString());
                                    mColourMap[tile] = mColors[t];
                                    //addColour(mColors[t], tile, true);
                                    mCharCount++;
                                    tile = '\0';
                                }
                            }
                        }
                    }
                    else if (state.Equals("options", StringComparison.CurrentCultureIgnoreCase))
                    {
                        switch (line)
                        {
                            case "current_time":
                                mCurrentTime = float.Parse(cc[++i]);
                                break;
                            case "current_day":
                                mCurrentDay = int.Parse(cc[++i]);
                                break;
                            case "day_length":
                                mDayLength = float.Parse(cc[++i]);
                                break;
                            default:
                                break;
                        }
                    }
                    else if (state.Equals("map", StringComparison.CurrentCultureIgnoreCase))
                    {
                        if (endState != null)
                        {
                            renderImage(ref mMapTotal);
                        }
                        else
                        {
                            if (mMapList.Count == 0)
                            {
                                mapWidth = line.Length;
                            }
                            mMapList.Add(line);
                        }
                    }
                    else if (state.Equals("entities", StringComparison.CurrentCultureIgnoreCase) ||
                                state.Equals("RemovedEntities", StringComparison.CurrentCultureIgnoreCase))
                    {
                        if (line == "Animal")
                        {
                            i++;
                            Animal a = new Animal();
                            a.load(ref cc, ref i);
                            i--;

                            string identifier = a.graphic.ToString() + '_' + a.species;
                            if (!mPopulations.ContainsKey(identifier))
                            {
                                mPopulations[identifier] = new PopulationStat();
                            }

                            if (a.isDead)
                            {
                                mPopulations[identifier].dead++;
                            }
                            else
                            {
                                mPopulations[identifier].alive++;
                            }
                        }
                    }
                    else if (state.Equals("history", StringComparison.CurrentCultureIgnoreCase))
                    {
                        if (line.Equals("DayEvent", StringComparison.CurrentCultureIgnoreCase))
                        {
                            DayEvents day = new DayEvents();
                            day.load(ref cc, ref i);
                            mHistory.Add(day);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Unknown state {0}", state);
                    }

                    if (endState != null)
                    {
                        state = endState;
                        endState = null;
                    }
                }
                mValues = new List<KeyPair>();
                foreach (KeyValuePair<string, float> pair in values)
                {
                    float value = pair.Value / numAnimals;
                    KeyPair p = new KeyPair(pair.Key, value);
                    if (pair.Key.Equals("diet", StringComparison.CurrentCultureIgnoreCase))
                    {
                        p.max = 1.0f;
                    }
                    mValues.Add(p);
                }
            }
        }