Пример #1
0
        public clsGateway GatewayCreate(sXY_int PosA, sXY_int PosB)
        {
            if ( PosA.X >= 0 & PosA.X < Terrain.TileSize.X &
                 PosA.Y >= 0 & PosA.Y < Terrain.TileSize.Y &
                 PosB.X >= 0 & PosB.X < Terrain.TileSize.X &
                 PosB.Y >= 0 & PosB.Y < Terrain.TileSize.Y ) //is on map
            {
                if ( PosA.X == PosB.X | PosA.Y == PosB.Y ) //is straight
                {
                    clsGateway Gateway = new clsGateway();

                    Gateway.PosA = PosA;
                    Gateway.PosB = PosB;

                    Gateway.MapLink.Connect(Gateways);

                    return Gateway;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
Пример #2
0
        public clsResult Load_LND(string Path)
        {
            clsResult ReturnResult =
                new clsResult("Loading LND from " + Convert.ToString(ControlChars.Quote) + Path + Convert.ToString(ControlChars.Quote));

            try
            {
                string strTemp = "";
                string strTemp2 = "";
                int X = 0;
                int Y = 0;
                int A = 0;
                int B = 0;
                int Tile_Num = 0;
                SimpleList<string> LineData = default(SimpleList<string>);
                int Line_Num = 0;
                sLNDTile[] LNDTile = null;
                SimpleList<clsLNDObject> LNDObjects = new SimpleList<clsLNDObject>();
                clsUnitAdd UnitAdd = new clsUnitAdd();

                UnitAdd.Map = this;

                BinaryReader Reader = default(BinaryReader);
                try
                {
                    Reader = new BinaryReader(new FileStream(Path, FileMode.Open), App.UTF8Encoding);
                }
                catch ( Exception ex )
                {
                    ReturnResult.ProblemAdd(ex.Message);
                    return ReturnResult;
                }
                LineData = IOUtil.BytesToLinesRemoveComments(Reader);
                Reader.Close();

                Array.Resize(ref LNDTile, LineData.Count);

                string strTemp3 = "";
                bool GotTiles = default(bool);
                bool GotObjects = default(bool);
                bool GotGates = default(bool);
                bool GotTileTypes = default(bool);
                byte[] LNDTileType = new byte[0];
                string[] ObjectText = new string[11];
                string[] GateText = new string[4];
                string[] TileTypeText = new string[256];
                int LNDTileTypeCount = 0;
                SimpleList<clsGateway> LNDGates = new SimpleList<clsGateway>();
                clsGateway Gateway = default(clsGateway);
                int C = 0;
                int D = 0;
                bool GotText = default(bool);
                bool FlipX = default(bool);
                bool FlipZ = default(bool);
                byte Rotation = 0;
                sXY_int NewTileSize = new sXY_int();
                double dblTemp = 0;

                Line_Num = 0;
                while ( Line_Num < LineData.Count )
                {
                    strTemp = LineData[Line_Num];

                    A = strTemp.IndexOf("TileWidth ") + 1;
                    if ( A == 0 )
                    {
                    }
                    else
                    {
                    }

                    A = strTemp.IndexOf("TileHeight ") + 1;
                    if ( A == 0 )
                    {
                    }
                    else
                    {
                    }

                    A = strTemp.IndexOf("MapWidth ") + 1;
                    if ( A == 0 )
                    {
                    }
                    else
                    {
                        IOUtil.InvariantParse(strTemp.Substring(strTemp.Length - (strTemp.Length - (A + 8)), strTemp.Length - (A + 8)), ref NewTileSize.X);
                        goto LineDone;
                    }

                    A = strTemp.IndexOf("MapHeight ") + 1;
                    if ( A == 0 )
                    {
                    }
                    else
                    {
                        IOUtil.InvariantParse(strTemp.Substring(strTemp.Length - (strTemp.Length - (A + 9)), strTemp.Length - (A + 9)), ref NewTileSize.Y);
                        goto LineDone;
                    }

                    A = strTemp.IndexOf("Textures {") + 1;
                    if ( A == 0 )
                    {
                    }
                    else
                    {
                        Line_Num++;
                        strTemp = LineData[Line_Num];

                        strTemp2 = strTemp.ToLower();
                        if ( strTemp2.IndexOf("tertilesc1") + 1 > 0 )
                        {
                            Tileset = App.Tileset_Arizona;

                            goto LineDone;
                        }
                        else if ( strTemp2.IndexOf("tertilesc2") + 1 > 0 )
                        {
                            Tileset = App.Tileset_Urban;

                            goto LineDone;
                        }
                        else if ( strTemp2.IndexOf("tertilesc3") + 1 > 0 )
                        {
                            Tileset = App.Tileset_Rockies;

                            goto LineDone;
                        }

                        goto LineDone;
                    }

                    A = strTemp.IndexOf("Tiles {") + 1;
                    if ( A == 0 || GotTiles )
                    {
                    }
                    else
                    {
                        Line_Num++;
                        while ( Line_Num < LineData.Count )
                        {
                            strTemp = LineData[Line_Num];

                            A = strTemp.IndexOf("}") + 1;
                            if ( A == 0 )
                            {
                                A = strTemp.IndexOf("TID ") + 1;
                                if ( A == 0 )
                                {
                                    ReturnResult.ProblemAdd("Tile ID missing");
                                    return ReturnResult;
                                }
                                else
                                {
                                    strTemp2 = strTemp.Substring(strTemp.Length - (strTemp.Length - A - 3), strTemp.Length - A - 3);
                                    A = strTemp2.IndexOf(" ") + 1;
                                    if ( A > 0 )
                                    {
                                        strTemp2 = strTemp2.Substring(0, A - 1);
                                    }
                                    Int16 temp_Result = LNDTile[Tile_Num].TID;
                                    IOUtil.InvariantParse(strTemp2, ref temp_Result);
                                }

                                A = strTemp.IndexOf("VF ") + 1;
                                if ( A == 0 )
                                {
                                    ReturnResult.ProblemAdd("Tile VF missing");
                                    return ReturnResult;
                                }
                                else
                                {
                                    strTemp2 = strTemp.Substring(strTemp.Length - (strTemp.Length - A - 2), strTemp.Length - A - 2);
                                    A = strTemp2.IndexOf(" ") + 1;
                                    if ( A > 0 )
                                    {
                                        strTemp2 = strTemp2.Substring(0, A - 1);
                                    }
                                    Int16 temp_Result2 = LNDTile[Tile_Num].VF;
                                    IOUtil.InvariantParse(strTemp2, ref temp_Result2);
                                }

                                A = strTemp.IndexOf("TF ") + 1;
                                if ( A == 0 )
                                {
                                    ReturnResult.ProblemAdd("Tile TF missing");
                                    return ReturnResult;
                                }
                                else
                                {
                                    strTemp2 = strTemp.Substring(strTemp.Length - (strTemp.Length - A - 2), strTemp.Length - A - 2);
                                    A = strTemp2.IndexOf(" ") + 1;
                                    if ( A > 0 )
                                    {
                                        strTemp2 = strTemp2.Substring(0, A - 1);
                                    }
                                    Int16 temp_Result3 = LNDTile[Tile_Num].TF;
                                    IOUtil.InvariantParse(strTemp2, ref temp_Result3);
                                }

                                A = strTemp.IndexOf(" F ") + 1;
                                if ( A == 0 )
                                {
                                    ReturnResult.ProblemAdd("Tile flip missing");
                                    return ReturnResult;
                                }
                                else
                                {
                                    strTemp2 = Strings.Right(strTemp, strTemp.Length - A - 2);
                                    A = strTemp2.IndexOf(" ") + 1;
                                    if ( A > 0 )
                                    {
                                        strTemp2 = strTemp2.Substring(0, A - 1);
                                    }
                                    short temp_Result4 = LNDTile[Tile_Num].F;
                                    IOUtil.InvariantParse(strTemp2, ref temp_Result4);
                                }

                                A = strTemp.IndexOf(" VH ") + 1;
                                if ( A == 0 )
                                {
                                    ReturnResult.ProblemAdd("Tile height is missing");
                                    return ReturnResult;
                                }
                                else
                                {
                                    strTemp3 = strTemp.Substring(strTemp.Length - (strTemp.Length - A - 3), strTemp.Length - A - 3);
                                    for ( A = 0; A <= 2; A++ )
                                    {
                                        B = strTemp3.IndexOf(" ") + 1;
                                        if ( B == 0 )
                                        {
                                            ReturnResult.ProblemAdd("A tile height value is missing");
                                            return ReturnResult;
                                        }
                                        strTemp2 = strTemp3.Substring(0, B - 1);
                                        strTemp3 = strTemp3.Substring(strTemp3.Length - (strTemp3.Length - B), strTemp3.Length - B);

                                        if ( A == 0 )
                                        {
                                            short temp_Result5 = LNDTile[Tile_Num].Vertex0Height;
                                            IOUtil.InvariantParse(strTemp2, ref temp_Result5);
                                        }
                                        else if ( A == 1 )
                                        {
                                            short temp_Result6 = LNDTile[Tile_Num].Vertex1Height;
                                            IOUtil.InvariantParse(strTemp2, ref temp_Result6);
                                        }
                                        else if ( A == 2 )
                                        {
                                            Int16 temp_Result7 = LNDTile[Tile_Num].Vertex2Height;
                                            IOUtil.InvariantParse(strTemp2, ref temp_Result7);
                                        }
                                    }
                                    short temp_Result8 = LNDTile[Tile_Num].Vertex3Height;
                                    IOUtil.InvariantParse(strTemp3, ref temp_Result8);
                                }

                                Tile_Num++;
                            }
                            else
                            {
                                GotTiles = true;
                                goto LineDone;
                            }

                            Line_Num++;
                        }

                        GotTiles = true;
                        goto LineDone;
                    }

                    A = strTemp.IndexOf("Objects {") + 1;
                    if ( A == 0 || GotObjects )
                    {
                    }
                    else
                    {
                        Line_Num++;
                        while ( Line_Num < LineData.Count )
                        {
                            strTemp = LineData[Line_Num];

                            A = strTemp.IndexOf("}") + 1;
                            if ( A == 0 )
                            {
                                C = 0;
                                ObjectText[0] = "";
                                GotText = false;
                                for ( B = 0; B <= strTemp.Length - 1; B++ )
                                {
                                    if ( strTemp[B] != ' ' && strTemp[B] != '\t' )
                                    {
                                        GotText = true;
                                        ObjectText[C] += strTemp[B].ToString();
                                    }
                                    else
                                    {
                                        if ( GotText )
                                        {
                                            C++;
                                            if ( C == 11 )
                                            {
                                                ReturnResult.ProblemAdd("Too many fields for an object, or a space at the end.");
                                                return ReturnResult;
                                            }
                                            ObjectText[C] = "";
                                            GotText = false;
                                        }
                                    }
                                }

                                clsLNDObject NewObject = new clsLNDObject();
                                IOUtil.InvariantParse(ObjectText[0], NewObject.ID);
                                IOUtil.InvariantParse(ObjectText[1], ref NewObject.TypeNum);
                                NewObject.Code = ObjectText[2].Substring(1, ObjectText[2].Length - 2); //remove quotes
                                IOUtil.InvariantParse(ObjectText[3], ref NewObject.PlayerNum);
                                NewObject.Name = ObjectText[4].Substring(1, ObjectText[4].Length - 2); //remove quotes
                                IOUtil.InvariantParse(ObjectText[5], ref NewObject.Pos.X);
                                IOUtil.InvariantParse(ObjectText[6], ref NewObject.Pos.Y);
                                IOUtil.InvariantParse(ObjectText[7], ref NewObject.Pos.Z);
                                if ( IOUtil.InvariantParse(ObjectText[8], ref dblTemp) )
                                {
                                    NewObject.Rotation.X = (int)(MathUtil.Clamp_dbl(dblTemp, 0.0D, 359.0D));
                                }
                                if ( IOUtil.InvariantParse(ObjectText[9], ref dblTemp) )
                                {
                                    NewObject.Rotation.Y = (int)(MathUtil.Clamp_dbl(dblTemp, 0.0D, 359.0D));
                                }
                                if ( IOUtil.InvariantParse(ObjectText[10], ref dblTemp) )
                                {
                                    NewObject.Rotation.Z = (int)(MathUtil.Clamp_dbl(dblTemp, 0.0D, 359.0D));
                                }
                                LNDObjects.Add(NewObject);
                            }
                            else
                            {
                                GotObjects = true;
                                goto LineDone;
                            }

                            Line_Num++;
                        }

                        GotObjects = true;
                        goto LineDone;
                    }

                    A = strTemp.IndexOf("Gates {") + 1;
                    if ( A == 0 || GotGates )
                    {
                    }
                    else
                    {
                        Line_Num++;
                        while ( Line_Num < LineData.Count )
                        {
                            strTemp = LineData[Line_Num];

                            A = strTemp.IndexOf("}") + 1;
                            if ( A == 0 )
                            {
                                C = 0;
                                GateText[0] = "";
                                GotText = false;
                                for ( B = 0; B <= strTemp.Length - 1; B++ )
                                {
                                    if ( strTemp[B] != ' ' && strTemp[B] != '\t' )
                                    {
                                        GotText = true;
                                        GateText[C] += strTemp[B].ToString();
                                    }
                                    else
                                    {
                                        if ( GotText )
                                        {
                                            C++;
                                            if ( C == 4 )
                                            {
                                                ReturnResult.ProblemAdd("Too many fields for a gateway, or a space at the end.");
                                                return ReturnResult;
                                            }
                                            GateText[C] = "";
                                            GotText = false;
                                        }
                                    }
                                }

                                Gateway = new clsGateway();
                                IOUtil.InvariantParse(GateText[0], ref Gateway.PosA.X);
                                Gateway.PosA.X = Math.Max(Gateway.PosA.X, 0);
                                IOUtil.InvariantParse(GateText[1], ref Gateway.PosA.Y);
                                Gateway.PosA.Y = Math.Max(Gateway.PosA.Y, 0);
                                IOUtil.InvariantParse(GateText[2], ref Gateway.PosB.X);
                                Gateway.PosB.X = Math.Max(Gateway.PosB.X, 0);
                                IOUtil.InvariantParse(GateText[3], ref Gateway.PosB.Y);
                                Gateway.PosB.Y = Math.Max(Gateway.PosB.Y, 0);
                                LNDGates.Add(Gateway);
                            }
                            else
                            {
                                GotGates = true;
                                goto LineDone;
                            }

                            Line_Num++;
                        }

                        GotGates = true;
                        goto LineDone;
                    }

                    A = strTemp.IndexOf("Tiles {") + 1;
                    if ( A == 0 || GotTileTypes || !GotTiles )
                    {
                    }
                    else
                    {
                        Line_Num++;
                        while ( Line_Num < LineData.Count )
                        {
                            strTemp = LineData[Line_Num];

                            A = strTemp.IndexOf("}") + 1;
                            if ( A == 0 )
                            {
                                C = 0;
                                TileTypeText[0] = "";
                                GotText = false;
                                for ( B = 0; B <= strTemp.Length - 1; B++ )
                                {
                                    if ( strTemp[B] != ' ' && strTemp[B] != '\t' )
                                    {
                                        GotText = true;
                                        TileTypeText[C] += strTemp[B].ToString();
                                    }
                                    else
                                    {
                                        if ( GotText )
                                        {
                                            C++;
                                            if ( C == 256 )
                                            {
                                                ReturnResult.ProblemAdd("Too many fields for tile types.");
                                                return ReturnResult;
                                            }
                                            TileTypeText[C] = "";
                                            GotText = false;
                                        }
                                    }
                                }

                                if ( TileTypeText[C] == "" || TileTypeText[C] == " " )
                                {
                                    C--;
                                }

                                for ( D = 0; D <= C; D++ )
                                {
                                    Array.Resize(ref LNDTileType, LNDTileTypeCount + 1);
                                    LNDTileType[LNDTileTypeCount] = Math.Min(byte.Parse(TileTypeText[D]), (byte)11);
                                    LNDTileTypeCount++;
                                }
                            }
                            else
                            {
                                GotTileTypes = true;
                                goto LineDone;
                            }

                            Line_Num++;
                        }

                        GotTileTypes = true;
                        goto LineDone;
                    }

                    LineDone:
                    Line_Num++;
                }

                Array.Resize(ref LNDTile, Tile_Num);

                SetPainterToDefaults();

                if ( NewTileSize.X < 1 | NewTileSize.Y < 1 )
                {
                    ReturnResult.ProblemAdd("The LND\'s terrain dimensions are missing or invalid.");
                    return ReturnResult;
                }

                TerrainBlank(NewTileSize);
                TileType_Reset();

                for ( Y = 0; Y <= Terrain.TileSize.Y - 1; Y++ )
                {
                    for ( X = 0; X <= Terrain.TileSize.X - 1; X++ )
                    {
                        Tile_Num = Y * Terrain.TileSize.X + X;
                        //lnd uses different order! (3 = 2, 2 = 3), this program goes left to right, lnd goes clockwise around each tile
                        Terrain.Vertices[X, Y].Height = (byte)(LNDTile[Tile_Num].Vertex0Height);
                    }
                }

                for ( Y = 0; Y <= Terrain.TileSize.Y - 1; Y++ )
                {
                    for ( X = 0; X <= Terrain.TileSize.X - 1; X++ )
                    {
                        Tile_Num = Y * Terrain.TileSize.X + X;

                        Terrain.Tiles[X, Y].Texture.TextureNum = LNDTile[Tile_Num].TID - 1;

                        //ignore higher values
                        A = Convert.ToInt32(Conversion.Int(LNDTile[Tile_Num].F / 64.0D));
                        LNDTile[Tile_Num].F = (short)(LNDTile[Tile_Num].F - A * 64);

                        A = (int)(Conversion.Int(LNDTile[Tile_Num].F / 16.0D));
                        LNDTile[Tile_Num].F = (short)(LNDTile[Tile_Num].F - A * 16);
                        if ( A < 0 | A > 3 )
                        {
                            ReturnResult.ProblemAdd("Invalid flip value.");
                            return ReturnResult;
                        }
                        Rotation = (byte)A;

                        A = (int)(Conversion.Int(LNDTile[Tile_Num].F / 8.0D));
                        LNDTile[Tile_Num].F -= (short)(A * 8);
                        FlipZ = A == 1;

                        A = (int)(Conversion.Int(LNDTile[Tile_Num].F / 4.0D));
                        LNDTile[Tile_Num].F -= (short)(A * 4);
                        FlipX = A == 1;

                        A = Convert.ToInt32(Conversion.Int(LNDTile[Tile_Num].F / 2.0D));
                        LNDTile[Tile_Num].F -= (short)(A * 2);
                        Terrain.Tiles[X, Y].Tri = A == 1;

                        //vf, tf, ignore

                        TileUtil.OldOrientation_To_TileOrientation(Rotation, FlipX, FlipZ, Terrain.Tiles[X, Y].Texture.Orientation);
                    }
                }

                clsUnit NewUnit = default(clsUnit);
                sXYZ_int XYZ_int = new sXYZ_int();
                clsUnitType NewType = default(clsUnitType);
                UInt32 AvailableID = 0;
                clsLNDObject CurrentObject = default(clsLNDObject);

                AvailableID = 1U;
                foreach ( clsLNDObject tempLoopVar_CurrentObject in LNDObjects )
                {
                    CurrentObject = tempLoopVar_CurrentObject;
                    if ( CurrentObject.ID >= AvailableID )
                    {
                        AvailableID = CurrentObject.ID + 1U;
                    }
                }
                foreach ( clsLNDObject tempLoopVar_CurrentObject in LNDObjects )
                {
                    CurrentObject = tempLoopVar_CurrentObject;
                    switch ( CurrentObject.TypeNum )
                    {
                        case 0:
                            NewType = App.ObjectData.FindOrCreateUnitType(CurrentObject.Code, clsUnitType.enumType.Feature, -1);
                            break;
                        case 1:
                            NewType = App.ObjectData.FindOrCreateUnitType(CurrentObject.Code, clsUnitType.enumType.PlayerStructure, -1);
                            break;
                        case 2:
                            NewType = App.ObjectData.FindOrCreateUnitType(CurrentObject.Code, clsUnitType.enumType.PlayerDroid, -1);
                            break;
                        default:
                            NewType = null;
                            break;
                    }
                    if ( NewType != null )
                    {
                        NewUnit = new clsUnit();
                        NewUnit.Type = NewType;
                        if ( CurrentObject.PlayerNum < 0 | CurrentObject.PlayerNum >= Constants.PlayerCountMax )
                        {
                            NewUnit.UnitGroup = ScavengerUnitGroup;
                        }
                        else
                        {
                            NewUnit.UnitGroup = UnitGroups[CurrentObject.PlayerNum];
                        }
                        XYZ_int.X = (int)CurrentObject.Pos.X;
                        XYZ_int.Y = (int)CurrentObject.Pos.Y;
                        XYZ_int.Z = (int)CurrentObject.Pos.Z;
                        NewUnit.Pos = MapPos_From_LNDPos(XYZ_int);
                        NewUnit.Rotation = CurrentObject.Rotation.Y;
                        if ( CurrentObject.ID == 0U )
                        {
                            CurrentObject.ID = AvailableID;
                            App.ZeroIDWarning(NewUnit, CurrentObject.ID, ReturnResult);
                        }
                        UnitAdd.NewUnit = NewUnit;
                        UnitAdd.ID = CurrentObject.ID;
                        UnitAdd.Perform();
                        App.ErrorIDChange(CurrentObject.ID, NewUnit, "Load_LND");
                        if ( AvailableID == CurrentObject.ID )
                        {
                            AvailableID = NewUnit.ID + 1U;
                        }
                    }
                }

                foreach ( clsGateway tempLoopVar_Gateway in LNDGates )
                {
                    Gateway = tempLoopVar_Gateway;
                    GatewayCreate(Gateway.PosA, Gateway.PosB);
                }

                if ( Tileset != null )
                {
                    for ( A = 0; A <= Math.Min(LNDTileTypeCount - 1, Tileset.TileCount) - 1; A++ )
                    {
                        Tile_TypeNum[A] = LNDTileType[A + 1]; //lnd value 0 is ignored
                    }
                }
            }
            catch ( Exception ex )
            {
                ReturnResult.ProblemAdd(ex.Message);
                return ReturnResult;
            }

            return ReturnResult;
        }