Exemplo n.º 1
0
    public static void SaveMap(TrackSavable track, string path)
    {
        List <byte>    bytes = new List <byte>();
        ByteFileParser io    = new ByteFileParser(bytes);

        //ignore "CDTRK" string in the start of the file
        io.WriteFlag("CDTRK");

        //unused current time thing
        io.WriteInt(track.CurrentTime);

        //author
        io.WriteString(track.Author);

        //comment
        io.WriteString(track.Comment);

        //skip unneeded block
        for (int n = 0; n < 20; n++)
        {
            io.WriteInt(1);
            io.WriteString("CDTrack editor by Wurunduk");
        }

        //track style (race, derby, htf)
        io.WriteByte(track.Style);

        //ambience
        io.WriteString(track.Ambience);

        //amount of fileds used on map
        io.WriteUShort(track.FieldFilesNumber);

        //name of fields
        for (int n = 0; n < track.FieldFilesNumber; n++)
        {
            io.WriteString(track.FieldFiles[n]);
        }

        //width and height in tiles
        io.WriteUShort(track.Width);
        io.WriteUShort(track.Height);

        for (int y = 0; y < track.Height; y++)
        {
            for (int x = 0; x < track.Width; x++)
            {
                io.WriteUShort(track.TrackTiles[y][x].FieldId);
                io.WriteByte(track.TrackTiles[y][x].Rotation);
                io.WriteByte(track.TrackTiles[y][x].IsMirrored);
                io.WriteByte(track.TrackTiles[y][x].Height);
            }
        }

        io.WriteUShort(track.DynamicObjectFilesNumber);

        for (int i = 0; i < track.DynamicObjectFilesNumber; i++)
        {
            io.WriteString(track.DynamicObjectFiles[i]);
        }

        io.WriteUShort(track.DynamicObjectsNumber);

        for (int i = 0; i < track.DynamicObjectsNumber; i++)
        {
            io.WriteUShort(track.DynamicObjects[i].ObjectId);
            io.WriteVector3(track.DynamicObjects[i].Position);
            io.WriteFloat(track.DynamicObjects[i].Rotation);
        }


        io.WriteUShort(track.CheckpointsNumber);
        for (int i = 0; i < track.CheckpointsNumber; i++)
        {
            io.WriteUShort(track.Checkpoints[i]);
        }

        io.WriteByte(track.Permission);

        io.WriteFloat(track.GroundBumpyness);

        io.WriteByte(track.Scenery);

        for (int y = 0; y < track.Height * 4 + 1; y++)
        {
            for (int x = 0; x < track.Width * 4 + 1; x++)
            {
                io.WriteFloat(track.Heightmap[y][x]);
            }
        }

        File.WriteAllBytes(path, io.Data.ToArray());
    }
Exemplo n.º 2
0
    //reads map from file to Track object
    public static TrackSavable ReadMap(string path)
    {
        TrackSavable   Track = new TrackSavable();
        List <byte>    data  = new List <byte>(File.ReadAllBytes(path));
        ByteFileParser io    = new ByteFileParser(data);

        //ignore "CDTRK" string in the start of the file
        io.SetReadingOffest(5);

        //unused current time thing
        Track.CurrentTime = io.ReadInt();

        //author
        Track.Author = io.ReadString();

        //comment
        Track.Comment = io.ReadString();

        //skip unneeded block
        for (int n = 0; n < 20; n++)
        {
            io.ReadInt();
            io.ReadString();
        }

        //track style (race, derby, htf)
        Track.Style = io.ReadByte();

        //ambience
        Track.Ambience = io.ReadString();

        //amount of fileds used on map
        Track.FieldFilesNumber = io.ReadUShort();

        //name of fields
        Track.FieldFiles = new List <string>(Track.FieldFilesNumber);
        for (int n = 0; n < Track.FieldFilesNumber; n++)
        {
            Track.FieldFiles.Add(io.ReadString());
        }

        //width and height in tiles
        Track.Width  = io.ReadUShort();
        Track.Height = io.ReadUShort();

        Track.TrackTiles = new List <List <TrackTileSavable> >(Track.Height);

        for (int y = 0; y < Track.Height; y++)
        {
            Track.TrackTiles.Add(new List <TrackTileSavable>(Track.Width));
            for (int x = 0; x < Track.Width; x++)
            {
                TrackTileSavable newTile = new TrackTileSavable();
                newTile.FieldId    = io.ReadUShort();
                newTile.Rotation   = io.ReadByte();
                newTile.IsMirrored = io.ReadByte();
                newTile.Height     = io.ReadByte();

                Track.TrackTiles[y].Add(newTile);
            }
        }


        Track.DynamicObjectFilesNumber = io.ReadUShort();
        Track.DynamicObjectFiles       = new List <string>(Track.DynamicObjectFilesNumber);
        for (int i = 0; i < Track.DynamicObjectFilesNumber; i++)
        {
            Track.DynamicObjectFiles.Add(io.ReadString());
        }

        Track.DynamicObjectsNumber = io.ReadUShort();
        Track.DynamicObjects       = new List <DynamicObjectSavable>(Track.DynamicObjectsNumber);
        for (int i = 0; i < Track.DynamicObjectsNumber; i++)
        {
            DynamicObjectSavable newDynamicObject = new DynamicObjectSavable();
            newDynamicObject.ObjectId = io.ReadUShort();
            newDynamicObject.Position = io.ReadVector3();
            newDynamicObject.Rotation = io.ReadFloat();
            Track.DynamicObjects.Add(newDynamicObject);
        }


        Track.CheckpointsNumber = io.ReadUShort();
        Track.Checkpoints       = new List <ushort>(Track.CheckpointsNumber);
        for (int i = 0; i < Track.CheckpointsNumber; i++)
        {
            Track.Checkpoints.Add(io.ReadUShort());
        }

        Track.Permission = io.ReadByte();

        Track.GroundBumpyness = io.ReadFloat();

        Track.Scenery = io.ReadByte();

        Track.Heightmap = new List <List <float> >();

        for (int y = 0; y < Track.Height * 4 + 1; y++)
        {
            Track.Heightmap.Add(new List <float>());
            for (int x = 0; x < Track.Width * 4 + 1; x++)
            {
                Track.Heightmap[y].Add(io.ReadFloat());
            }
        }

        return(Track);
    }
Exemplo n.º 3
0
    public static P3DModel LoadFromBytes(List <byte> data)
    {
        P3DModel       model = new P3DModel();
        ByteFileParser io    = new ByteFileParser(data);

        //skip 'p3d' text and version, cause i am a bad guy
        io.SetReadingOffest(4);

        model.P3DLength = io.ReadFloat();
        model.P3DHeight = io.ReadFloat();
        model.P3DDepth  = io.ReadFloat();

        //skip 'tex' text
        io.AddReadingOffset(3);
        //4 reserved bytes
        io.AddReadingOffset(4);

        model.P3DNumTextures = io.ReadByte();

        model.P3DRenderInfo = new P3DModel.RenderInfo[model.P3DNumTextures];
        for (int i = 0; i < model.P3DNumTextures; i++)
        {
            P3DModel.RenderInfo newRenderInfo = new P3DModel.RenderInfo();
            newRenderInfo.TextureFile = io.ReadString().ToLower();
            model.P3DRenderInfo[i]    = newRenderInfo;
        }

        //skip lights test
        io.AddReadingOffset(6);
        //4 reserved bytes
        io.AddReadingOffset(4);

        model.P3DNumLights = io.ReadShort();
        model.P3DLights    = new P3DModel.P3DLight[model.P3DNumLights];
        for (int i = 0; i < model.P3DNumLights; i++)
        {
            P3DModel.P3DLight newP3DLight = new P3DModel.P3DLight();

            newP3DLight.Name = io.ReadString();

            newP3DLight.Pos = io.ReadVector3();

            newP3DLight.Range = io.ReadFloat();
            newP3DLight.Color = io.ReadInt();

            newP3DLight.ShowCorona         = io.ReadByte();
            newP3DLight.ShowLensFlares     = io.ReadByte();
            newP3DLight.LightUpEnvironment = io.ReadByte();
            model.P3DLights[i]             = newP3DLight;
        }

        //skip meshes test
        io.AddReadingOffset(6);
        //4 reserved bytes
        io.AddReadingOffset(4);

        model.P3DNumMeshes = io.ReadShort();
        model.P3DMeshes    = new P3DModel.P3DMesh[model.P3DNumMeshes];
        for (int m = 0; m < model.P3DNumMeshes; m++)
        {
            //skip submesh test
            io.AddReadingOffset(7);
            //4 reserved bytes
            io.AddReadingOffset(4);

            P3DModel.P3DMesh newP3DMesh = new P3DModel.P3DMesh(model.P3DNumTextures);

            newP3DMesh.Name = io.ReadString();

            newP3DMesh.Flags    = io.ReadUInt();
            newP3DMesh.LocalPos = io.ReadVector3();

            newP3DMesh.Length = io.ReadFloat();
            newP3DMesh.Height = io.ReadFloat();
            newP3DMesh.Depth  = io.ReadFloat();

            for (int i = 0; i < model.P3DNumTextures; i++)
            {
                P3DModel.TextureInfo newTextureInfo = new P3DModel.TextureInfo();

                newTextureInfo.TextureStart       = io.ReadShort();
                newTextureInfo.NumFlat            = io.ReadShort();
                newTextureInfo.NumFlatMetal       = io.ReadShort();
                newTextureInfo.NumGouraud         = io.ReadShort();
                newTextureInfo.NumGouraudMetal    = io.ReadShort();
                newTextureInfo.NumGouraudMetalEnv = io.ReadShort();
                newTextureInfo.NumShining         = io.ReadShort();
                newP3DMesh.TextureInfos[i]        = newTextureInfo;
            }


            newP3DMesh.NumVertices = io.ReadShort();
            newP3DMesh.Vertex      = new Vector3[newP3DMesh.NumVertices];
            for (int i = 0; i < newP3DMesh.NumVertices; i++)
            {
                newP3DMesh.Vertex[i] = io.ReadVector3();
            }


            newP3DMesh.NumPolys = io.ReadShort();
            newP3DMesh.Poly     = new P3DModel.P3DTexPolygon[newP3DMesh.NumPolys];
            for (int i = 0; i < newP3DMesh.NumPolys; i++)
            {
                P3DModel.P3DTexPolygon newP3DTexPolygon = new P3DModel.P3DTexPolygon();

                newP3DTexPolygon.P1 = io.ReadShort();
                newP3DTexPolygon.U1 = io.ReadFloat();
                newP3DTexPolygon.V1 = io.ReadFloat();

                newP3DTexPolygon.P2 = io.ReadShort();
                newP3DTexPolygon.U2 = io.ReadFloat();
                newP3DTexPolygon.V2 = io.ReadFloat();

                newP3DTexPolygon.P3 = io.ReadShort();
                newP3DTexPolygon.U3 = io.ReadFloat();
                newP3DTexPolygon.V3 = io.ReadFloat();

                newP3DMesh.Poly[i] = newP3DTexPolygon;
            }


            for (int i = 0; i < model.P3DNumTextures; i++)
            {
                short PolyInTex = newP3DMesh.TextureInfos[i].TextureStart;

                for (int n = 0; n < newP3DMesh.TextureInfos[i].NumFlat; n++)
                {
                    newP3DMesh.Poly[PolyInTex + n].Material = P3DModel.P3DMaterial.Flat;
                    newP3DMesh.Poly[PolyInTex + n].Texture  = model.P3DRenderInfo[i].TextureFile;
                }
                PolyInTex += newP3DMesh.TextureInfos[i].NumFlat;


                for (int n = 0; n < newP3DMesh.TextureInfos[i].NumFlatMetal; n++)
                {
                    newP3DMesh.Poly[PolyInTex + n].Material = P3DModel.P3DMaterial.FlatMetal;
                    newP3DMesh.Poly[PolyInTex + n].Texture  = model.P3DRenderInfo[i].TextureFile;
                }
                PolyInTex += newP3DMesh.TextureInfos[i].NumFlatMetal;

                for (int n = 0; n < newP3DMesh.TextureInfos[i].NumGouraud; n++)
                {
                    newP3DMesh.Poly[PolyInTex + n].Material = P3DModel.P3DMaterial.Gouraud;
                    newP3DMesh.Poly[PolyInTex + n].Texture  = model.P3DRenderInfo[i].TextureFile;
                }
                PolyInTex += newP3DMesh.TextureInfos[i].NumGouraud;

                for (int n = 0; n < newP3DMesh.TextureInfos[i].NumGouraudMetal; n++)
                {
                    newP3DMesh.Poly[PolyInTex + n].Material = P3DModel.P3DMaterial.GouraudMetal;
                    newP3DMesh.Poly[PolyInTex + n].Texture  = model.P3DRenderInfo[i].TextureFile;
                }
                PolyInTex += newP3DMesh.TextureInfos[i].NumGouraudMetal;

                for (int n = 0; n < newP3DMesh.TextureInfos[i].NumGouraudMetalEnv; n++)
                {
                    newP3DMesh.Poly[PolyInTex + n].Material = P3DModel.P3DMaterial.GouraudMetalEnv;
                    newP3DMesh.Poly[PolyInTex + n].Texture  = model.P3DRenderInfo[i].TextureFile;
                }
                PolyInTex += newP3DMesh.TextureInfos[i].NumGouraudMetalEnv;

                for (int n = 0; n < newP3DMesh.TextureInfos[i].NumShining; n++)
                {
                    newP3DMesh.Poly[PolyInTex + n].Material = P3DModel.P3DMaterial.Shining;
                    newP3DMesh.Poly[PolyInTex + n].Texture  = model.P3DRenderInfo[i].TextureFile;
                }
            }
            model.P3DMeshes[m] = newP3DMesh;
        }

        //skip user test
        io.AddReadingOffset(4);
        //4 reserved bytes
        io.AddReadingOffset(4);

        model.P3DUserDataSize = io.ReadInt();

        if (model.P3DUserDataSize != 0)
        {
            model.P3DUserDataPtr = io.ReadString(model.P3DUserDataSize);
        }
        else
        {
            model.P3DUserDataPtr = "";
        }
        return(model);
    }