Example #1
0
 public void Init(OnvFile onv)
 {
     Onv = onv;
     LoadIndices();
 }
Example #2
0
        private void ProcessButton_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(InputFolderTextBox.Text))
            {
                MessageBox.Show("Input folder doesn't exist: " + InputFolderTextBox.Text);
                return;
            }
            if (!Directory.Exists(OutputFolderTextBox.Text))
            {
                MessageBox.Show("Output folder doesn't exist: " + OutputFolderTextBox.Text);
                return;
            }

            Cursor = Cursors.WaitCursor;


            string inputpath  = InputFolderTextBox.Text;
            string outputpath = OutputFolderTextBox.Text;

            string[] onvfiles = Directory.GetFiles(inputpath, "*.onv", SearchOption.TopDirectoryOnly);
            float    offsetx  = float.Parse(OffsetXTextBox.Text, CultureInfo.InvariantCulture);
            float    offsety  = float.Parse(OffsetYTextBox.Text, CultureInfo.InvariantCulture);
            float    offsetz  = float.Parse(OffsetZTextBox.Text, CultureInfo.InvariantCulture);
            Vector3  offset   = new Vector3(offsetx, offsety, offsetz);
            var      vehc     = VehicleCheckBox.Checked;

            if (!outputpath.EndsWith("\\"))
            {
                outputpath = outputpath + "\\";
            }

            StringBuilder errorlog = new StringBuilder();


            List <OnvFile>            onvlist = new List <OnvFile>();
            Dictionary <int, OnvFile> onvdict = new Dictionary <int, OnvFile>();
            YnvBuilder builder = new YnvBuilder();

            foreach (var onvfile in onvfiles) //load onv files...
            {
                try
                {
                    OnvFile onv = new OnvFile();
                    onv.Load(onvfile);
                    onv.InitPolys();
                    onvlist.Add(onv);
                    onvdict[onv.SectorID] = onv;
                }
                catch (Exception ex)
                {
                    string err = "Error loading " + onvfile + ":\n" + ex.ToString();
                    errorlog.AppendLine(err);
                }
            }


            foreach (var onv in onvlist) //join up all the edges
            {
                onv.InitEdges(onvdict);
            }

            foreach (var onv in onvlist) //offset all the vertices
            {
                for (int i = 0; i < onv.VerticesWS.Count; i++)
                {
                    onv.VerticesWS[i] = onv.VerticesWS[i] + offset;
                }
                foreach (var poly in onv.Polys)
                {
                    if (poly.Vertices != null)
                    {
                        for (int i = 0; i < poly.Vertices.Length; i++)
                        {
                            poly.Vertices[i] = poly.Vertices[i] + offset;
                        }
                    }
                }
                foreach (var portal in onv.Portals)
                {
                    //TODO: offset portals
                }
                //TODO: points ("Bounds")
            }

            foreach (var onv in onvlist) //create the new polys
            {
                foreach (var poly in onv.Polys)
                {
                    var ypoly = builder.AddPoly(poly.Vertices);
                    poly.NewPoly = ypoly;


                    var f1 = poly.Flags1;
                    var f2 = poly.Flags2;
                    var f3 = poly.Flags3;

                    //## FLAGS TODO
                    if ((f1 & 1) > 0)
                    {
                        ypoly.B00_AvoidUnk = true;
                    }
                    if ((f1 & 2) > 0)
                    {
                        ypoly.B01_AvoidUnk = true;
                    }
                    if ((f1 & 4) > 0)
                    {
                        ypoly.B02_IsFootpath = true;
                    }
                    if ((f1 & 8) > 0)
                    {
                        ypoly.B03_IsUnderground = true;
                    }
                    //if ((f1 & 16) > 0) ypoly.B04_Unused = true;
                    //if ((f1 & 32) > 0) ypoly.B05_Unused = true;
                    if ((f1 & 64) > 0)
                    {
                        ypoly.B06_SteepSlope = true;
                    }
                    if ((f1 & 128) > 0)
                    {
                        ypoly.B07_IsWater = true;
                    }

                    if (ypoly.B02_IsFootpath)
                    {
                        ypoly.B00_AvoidUnk = false;
                        //ypoly.B01_AvoidUnk = false;
                        ////ypoly.B01_AvoidUnk = true;
                        //ypoly.B02_IsFootpath = true;
                        ypoly.B17_IsFlatGround = true;
                        ypoly.B22_FootpathUnk1 = true;
                        //ypoly.B23_FootpathUnk2 = true;
                        //ypoly.B24_FootpathMall = true;
                    }


                    if ((f1 & 16) > 0)
                    {
                    }  //no hits
                    if ((f1 & 32) > 0)
                    {
                    }  //some hits
                    if (f1 > 255)
                    {
                    }  //no hits


                    ypoly.UnkX = 127;
                    ypoly.UnkY = 127;
                }
            }

            foreach (var onv in onvlist) //create the new edges
            {
                foreach (var poly in onv.Polys)
                {
                    var ypoly = poly.NewPoly;
                    int ec    = poly.Edges?.Length ?? 0;

                    if (ec == 0)
                    {
                        continue;
                    }            //no edges? shouldn't happen
                    if (ec != ypoly.Vertices?.Length)
                    {
                        continue;
                    }            //shouldn't happen! error log this?

                    var newedges = new YnvEdge[ec];
                    for (int i = 0; i < ec; i++)
                    {
                        var  edge    = poly.Edges[i];
                        var  yedge   = new YnvEdge();
                        bool haspoly = (edge.Poly1?.NewPoly != null);
                        bool f1      = (edge.Flags1 & 1) > 0; //nonav?
                        bool f2      = (edge.Flags1 & 2) > 0; //nonav?
                        bool f3      = (edge.Flags1 & 4) > 0; //drop?
                        bool f4      = (edge.Flags1 & 8) > 0; //suicide?
                        bool f5      = (edge.Flags1 & 1) > 0;
                        bool nonav   = f1 || f2;

                        uint edgeval = 62; //62

                        yedge.Poly1   = edge.Poly1?.NewPoly;
                        yedge.Poly2   = edge.Poly2?.NewPoly;
                        yedge.AreaID1 = 0x3FFF;
                        yedge.AreaID2 = 0x3FFF;
                        yedge.PolyID1 = 0x3FFF;
                        yedge.PolyID2 = 0x3FFF;
                        yedge._RawData._Poly1.Unk2 = haspoly ? nonav ? 2 : 0u : 1u;       //2 if nonnavigable?
                        yedge._RawData._Poly2.Unk2 = haspoly ? 0 : 1u;
                        yedge._RawData._Poly1.Unk3 = haspoly ? nonav ? 1u : edgeval : 0u; //odd if nonnavigable?   haspoly ? 42 : 0u; //TEST
                        yedge._RawData._Poly2.Unk3 = haspoly ? nonav ? 2 : 0u : 0u;       //2 if nonnavigable? 4 if cell edge! - needs to be set later in ynv's
                        newedges[i] = yedge;
                    }
                    ypoly.Edges = newedges;
                }
            }

            List <YnvFile> ynvs = null;

            if (vehc)
            {
                ynvs = new List <YnvFile>();
                foreach (var onv in onvlist)
                {
                    builder.PolyList.Clear();
                    foreach (var poly in onv.Polys)
                    {
                        var ypoly = poly.NewPoly;
                        builder.PolyList.Add(ypoly);
                    }
                    builder.VehicleName = onv.Name;
                    var ynv = builder.Build(true);
                    ynvs.AddRange(ynv);
                }
            }
            else
            {
                ynvs = builder.Build(false);
            }

            foreach (var ynv in ynvs)
            {
                byte[] data     = ynv.Save();
                string filename = outputpath + ynv.Name + ".ynv";
                File.WriteAllBytes(filename, data);
            }


            Cursor = Cursors.Default;


            if (errorlog.Length > 0)
            {
                File.WriteAllText(outputpath + "errorlog.txt", errorlog.ToString());
                MessageBox.Show("Process complete with errors, see errorlog.txt.");
            }
            else
            {
                MessageBox.Show("Process complete.");
            }
        }