Ejemplo n.º 1
0
        public void SaveToDb(string name)
        {
            Console.WriteLine ("Saving to db");

            Map map = new Map () {
                Height = this.h,
                Width = this.w
            };

            GameState game = new GameState () {
                Name = name,
                Map = map,
            };

            string[] deleteTables = new string[] {
                "gamestate",
                "map",
                "hex",
                "hexresource",
                "map_cities",
                "map_rivers",
                "map_hexes",
                "hex_edges",
                "hex_effects",
                "kingdom"
            };

            foreach (string deleteTable in deleteTables)
                MySqlProvider.Provider.ExecuteNonQuery ("truncate " + deleteTable + ";");

            map.Game = game;
            map.Save ();
            game.Save ();

            RecordList<Hex> hexes = new RecordList<Hex> ();
            for (int x = 0; x < w; x++) {
                for (int y = 0; y < h; y++) {
                    Hex hex = new Hex ();
                    hex.EnsureId ();
                    hex.X = x;
                    hex.Y = y;
                    Color c = b.GetPixel (x, y);
                    if (c == blue)
                        hex.Terrain = TerrainType.Sea;
                    else if (c == white)
                        hex.Terrain = TerrainType.Mountains;
                    else if (c == darkGreen)
                        hex.Terrain = TerrainType.Forest;
                    else if (c == town)
                    {
                        hex.Terrain = TerrainType.Plains;
                    }
                    else
                        hex.Terrain = TerrainType.Plains;
                    hexes.Add (hex);
                    hexMap[x,y] = hex;
                }
            }

            //cities have to be done in a separate pass to account for growth.
            map.Hexes = hexes;
            map.EnsureMapIsBuilt (true);

            //check coastals
            foreach (Hex h in hexes)
                if (h.Terrain == TerrainType.Sea)
                    CheckCoastal (h);

            hexes.Save ();

            foreach (var kvp in riverPaths)
            {
                River r = new River();
                int z = 0;
                foreach(Point p in kvp.Value)
                {
                    r.Path.Add (new Triple<int> () {
                        X = p.X,
                        Y = p.Y,
                        Z = z++
                    });
                }
                r.Save ();
                r.Path.Save ();
                r.SaveRelations("Path");
                map.Rivers.Add (r);
            }

            map.SaveRelations ("Rivers");
            map.SaveRelations ("Hexes");
        }