/// <summary>
        /// Loads a Track definition file and constructs a Track object.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static async Task <Track> LoadAsync(string defpath, string assetpath, IGraphicsFactory f)
        {
            FileIniDataParser file = new FileIniDataParser();

            file.Parser.Configuration.CommentString = "//";
            IniData inidata = file.ReadFile(defpath);

            if (inidata == null)
            {
                return(null);
            }

            IniGetter ini      = new IniGetter(inidata);
            string    bkgpath  = ini.GetString("track", "background");
            string    maskpath = ini.GetString("track", "mask");
            Dictionary <Color, int> regionColors = new Dictionary <Color, int>();
            int regions = ini.GetInt("track", "regions");

            for (int i = 0; i < regions; ++i)
            {
                string secname = string.Format("region{0}", i);
                if (!inidata.Sections.ContainsSection(secname))
                {
                    continue;
                }
                Color col = Color.FromArgb(255,
                                           (byte)ini.GetInt(secname, "color_r"),
                                           (byte)ini.GetInt(secname, "color_g"),
                                           (byte)ini.GetInt(secname, "color_b"));
                regionColors[col] = i;
            }

            IImage background = await f.LoadImageAsync(assetpath + bkgpath);

            IBitmap mask = await f.LoadBitmapAsync(assetpath + maskpath);

            /*
             * // TODO: find out whether engine provides faster solution
             * int[,] regionMap = new int[mask.Width, mask.Height];
             * for (int x = 0; x < mask.Width; ++x)
             * {
             *  // NOTE: since MonoAGS has Y axis pointing up, we need to invert the lookup array's Y index
             *  for (int y = 0, mapy = mask.Height - 1; y < mask.Height; ++y, --mapy)
             *  {
             *      Color col = mask.GetPixel(x, y);
             *      int index = 0;
             *      regionColors.TryGetValue(col, out index);
             *      regionMap[x, mapy] = index;
             *  }
             * }
             *
             * return new Track(background, regionColors.Count, regionMap);
             */
            Size            trackSize   = mask != null ? new Size(mask.Width, mask.Height) : new Size();
            List <RaceNode> checkpoints = loadCheckpoints(assetpath, trackSize);

            TrackAIData aiData = await loadAIData(assetpath, trackSize, f);

            return(new Track(background, regionColors.Count, mask, regionColors, checkpoints, aiData));
        }
Exemple #2
0
        public static VehicleConfig LoadConfig(Track track, string iniFilePath)
        {
            FileIniDataParser file = new FileIniDataParser();

            file.Parser.Configuration.CommentString = "//";
            IniData inidata = file.ReadFile(iniFilePath);

            if (inidata == null || !inidata.Sections.ContainsSection("car"))
            {
                return(null);
            }

            IniGetter     ini = new IniGetter(inidata);
            VehicleConfig cfg = new VehicleConfig();

            cfg.BodyLength           = ini.GetFloat("car", "bodyLength");
            cfg.BodyWidth            = ini.GetFloat("car", "bodyWidth");
            cfg.DistanceBetweenAxles = ini.GetFloat("car", "distanceBetweenAxles");
            cfg.BodyMass             = ini.GetFloat("car", "bodyMass");
            cfg.BodyAerodynamics     = ini.GetFloat("car", "bodyAerodynamics");
            cfg.HardImpactLossFactor = ini.GetFloat("car", "hardImpactLossFactor");
            cfg.SoftImpactLossFactor = ini.GetFloat("car", "softImpactLossFactor");
            cfg.EngineMaxPower       = ini.GetFloat("car", "engineMaxPower");
            cfg.StillTurningVelocity = ini.GetFloat("car", "stillTurningVelocity");
            cfg.DriftVelocityFactor  = ini.GetFloat("car", "driftVelocityFactor");
            cfg.UISteeringAngle      = ini.GetFloat("car_control", "steeringAngle");
            return(cfg);
        }
        public static void Load(string filepath, ref GameConfig gameCfg, ref RaceEventConfig raceCfg)
        {
            if (!File.Exists(filepath))
            {
                return;
            }
            FileIniDataParser file = new FileIniDataParser();

            file.Parser.Configuration.CommentString = "//";
            IniData   inidata = file.ReadFile(filepath);
            IniGetter ini     = new IniGetter(inidata);

            gameCfg.MusicVolume = ini.GetInt("main", "music", gameCfg.MusicVolume);

            raceCfg.PlayerDriver = ini.GetInt("race", "driver", raceCfg.PlayerDriver);
            raceCfg.Laps         = ini.GetInt("race", "laps", raceCfg.Laps);
            raceCfg.Opponents    = ini.GetInt("race", "opponents", raceCfg.Opponents);
            string          physics = ini.GetString("race", "physics", raceCfg.Physics.ToString());
            RacePhysicsMode mode;

            if (Enum.TryParse <RacePhysicsMode>(physics, out mode))
            {
                raceCfg.Physics = mode;
            }
            raceCfg.CarCollisions = ini.GetBool("race", "car_collisions", raceCfg.CarCollisions);

            /* TODO
             *
             * IsDebugMode = ini.ReadBool("main", "debug_mode", IsDebugMode);
             */
        }
        private static async Task loadAIRegionsData(TrackAIData data, string assetpath, IGraphicsFactory f)
        {
            IBitmap regionMask = await f.LoadBitmapAsync(assetpath + "airegions.bmp");

            if (regionMask == null)
            {
                return;
            }

            FileIniDataParser file = new FileIniDataParser();

            file.Parser.Configuration.CommentString = "//";
            IniData inidata = file.ReadFile(assetpath + "airegions.ini");

            if (inidata == null)
            {
                return;
            }

            IniGetter ini = new IniGetter(inidata);
            Dictionary <Color, float> regionAngles = new Dictionary <Color, float>();
            int regions = ini.GetInt("ai", "regions");

            for (int i = 0; i < regions; ++i)
            {
                string secname = string.Format("region{0}", i);
                if (!inidata.Sections.ContainsSection(secname))
                {
                    continue;
                }
                Color col = Color.FromArgb(255,
                                           (byte)ini.GetInt(secname, "color_r"),
                                           (byte)ini.GetInt(secname, "color_g"),
                                           (byte)ini.GetInt(secname, "color_b"));
                regionAngles[col] = MathUtils.DegreesToRadians(ini.GetFloat(secname, "angle"));
            }

            data.AIRegionMask   = regionMask;
            data.AIRegionAngles = regionAngles;
            return;
        }
        public static TrackConfig LoadConfig(Track track, string iniFilePath)
        {
            FileIniDataParser file = new FileIniDataParser();

            file.Parser.Configuration.CommentString = "//";
            IniData inidata = file.ReadFile(iniFilePath);

            if (inidata == null)
            {
                return(null);
            }

            IniGetter   ini = new IniGetter(inidata);
            TrackConfig cfg = new TrackConfig();

            cfg.Gravity       = ini.GetFloat("track", "gravity");
            cfg.AirResistance = ini.GetFloat("track", "air_resistance");
            int regions = ini.GetInt("track", "regions");

            cfg.Regions = new List <TrackRegionConfig>();
            for (int i = 0; i < regions; ++i)
            {
                string secname = String.Format("area{0}", i);
                if (!inidata.Sections.ContainsSection(secname))
                {
                    continue;
                }
                TrackRegionConfig r = new TrackRegionConfig();
                r.IsObstacle         = ini.GetBool(secname, "is_obstacle");
                r.TerraSlideFriction = ini.GetFloat(secname, "slide_friction");
                r.TerraRollFriction  = ini.GetFloat(secname, "roll_friction");
                r.TerraGrip          = ini.GetFloat(secname, "grip");
                r.EnvResistance      = ini.GetFloat(secname, "env_resistance");
                cfg.Regions.Add(r);
            }
            return(cfg);
        }