Example #1
0
        public static idMapBrush Parse(idLexer lexer, Vector3 origin, bool newFormat = true, float version = idMapFile.CurrentMapVersion)
        {
            idToken               token;
            idMapBrushSide        side;
            List <idMapBrushSide> sides = new List <idMapBrushSide>();
            idDict dict = new idDict();

            Vector3[] planePoints = new Vector3[3];

            if (lexer.ExpectTokenString("{") == false)
            {
                return(null);
            }

            do
            {
                if ((token = lexer.ReadToken()) == null)
                {
                    lexer.Error("idMapBrush::Parse: unexpected EOF");
                    return(null);
                }

                if (token.ToString() == "}")
                {
                    break;
                }

                // here we may have to jump over brush epairs ( only used in editor )
                do
                {
                    // if token is a brace
                    if (token.ToString() == "(")
                    {
                        break;
                    }

                    // the token should be a key string for a key/value pair
                    if (token.Type != TokenType.String)
                    {
                        lexer.Error("idMapBrush::Parse: unexpected {0}, expected ( or epair key string", token.ToString());
                        return(null);
                    }


                    string key = token.ToString();

                    if (((token = lexer.ReadTokenOnLine()) == null) || (token.Type != TokenType.String))
                    {
                        lexer.Error("idMapBrush::Parse: expected epair value string not found");
                        return(null);
                    }

                    dict.Set(key, token.ToString());

                    // try to read the next key
                    if ((token = lexer.ReadToken()) == null)
                    {
                        lexer.Error("idMapBrush::Parse: unexpected EOF");
                        return(null);
                    }
                }while(true);

                lexer.UnreadToken = token;

                side = new idMapBrushSide();
                sides.Add(side);

                if (newFormat == true)
                {
                    float[] tmp = lexer.Parse1DMatrix(4);

                    if (tmp == null)
                    {
                        lexer.Error("idMapBrush::Parse: unable to read brush side plane definition");
                        return(null);
                    }
                    else
                    {
                        side.Plane = new Plane(tmp[0], tmp[1], tmp[2], tmp[3]);
                    }
                }
                else
                {
                    // read the three point plane definition
                    float[] tmp, tmp2, tmp3;

                    if (((tmp = lexer.Parse1DMatrix(3)) == null) ||
                        ((tmp2 = lexer.Parse1DMatrix(3)) == null) ||
                        ((tmp3 = lexer.Parse1DMatrix(3)) == null))
                    {
                        lexer.Error("idMapBrush::Parse: unable to read brush side plane definition");
                        return(null);
                    }

                    planePoints[0] = new Vector3(tmp[0], tmp[1], tmp[2]) - origin;
                    planePoints[1] = new Vector3(tmp2[0], tmp2[1], tmp2[2]) - origin;
                    planePoints[2] = new Vector3(tmp3[0], tmp3[1], tmp3[2]) - origin;

                    side.Plane.FromPoints(planePoints[0], planePoints[1], planePoints[2]);
                }

                // read the texture matrix
                // this is odd, because the texmat is 2D relative to default planar texture axis
                float[,] tmp5 = lexer.Parse2DMatrix(2, 3);

                if (tmp5 == null)
                {
                    lexer.Error("idMapBrush::Parse: unable to read brush side texture matrix");
                    return(null);
                }

                side.TextureMatrix[0] = new Vector3(tmp5[0, 0], tmp5[0, 1], tmp5[0, 2]);
                side.TextureMatrix[1] = new Vector3(tmp5[1, 0], tmp5[1, 1], tmp5[1, 2]);
                side.Origin           = origin;

                // read the material
                if ((token = lexer.ReadTokenOnLine()) == null)
                {
                    lexer.Error("idMapBrush::Parse: unable to read brush side material");
                    return(null);
                }

                // we had an implicit 'textures/' in the old format...
                if (version < 2.0f)
                {
                    side.Material = "textures/" + token.ToString();
                }
                else
                {
                    side.Material = token.ToString();
                }

                // Q2 allowed override of default flags and values, but we don't any more
                if (lexer.ReadTokenOnLine() != null)
                {
                    if (lexer.ReadTokenOnLine() != null)
                    {
                        if (lexer.ReadTokenOnLine() != null)
                        {
                        }
                    }
                }
            }while(true);

            if (lexer.ExpectTokenString("}") == false)
            {
                return(null);
            }

            idMapBrush brush = new idMapBrush();

            foreach (idMapBrushSide s in sides)
            {
                brush.AddSide(s);
            }

            brush.Dict = dict;

            return(brush);
        }