コード例 #1
0
        public static void MakePalette(string[] astr)
        {
            // -makepal 16 templates.tc palsize fixpalsize backgroundpalsize fixed.pal out.pal

            // tile size

            Size sizTile = new Size(0, 0);

            sizTile.Width  = int.Parse(astr[1]);
            sizTile.Height = sizTile.Width;

            // Load template collection

            TemplateDoc tmpd = (TemplateDoc)DocManager.OpenDocument(astr[2]);

            // palette size

            int cPalEntries = int.Parse(astr[3]);

            // entries fixed

            int cPalEntriesFixed = int.Parse(astr[4]);

            // entries for background

            int cPalEntriesBackground = int.Parse(astr[5]);

            // fixed palette

            Palette palFixed = new Palette(astr[6]);

            // output palette

            string strFilePalOut = astr[7];

            // If this template collection already has a palette it has already been quantized; we don't
            // want that.

            if (tmpd.GetPalette() != null)
            {
                new Exception("Template collection has already been quantized!");
            }

            // Scale templates if needed

            if (sizTile.Width != tmpd.TileSize.Width || sizTile.Height != tmpd.TileSize.Height)
            {
                TemplateTools.ScaleTemplates(tmpd, sizTile);
            }

            // Quantize

            TemplateTools.QuantizeTemplates(tmpd, palFixed, cPalEntries, cPalEntriesFixed, cPalEntriesBackground);

            // Save the new palette out

            Palette palNew = tmpd.GetPalette();

            palNew.SaveJasc(strFilePalOut);
        }
コード例 #2
0
        private void menuItemScaleDown_Click(object sender, System.EventArgs e)
        {
            // If no template doc active, bail

            if (m_tmpdActive == null)
            {
                return;
            }

            // Make sure 24 x 24 (could actually allow any sort of conversion...)

            if (m_tmpdActive.TileSize.Width != 24 && m_tmpdActive.TileSize.Height != 24)
            {
                MessageBox.Show(DocManager.GetFrameParent(), "The current template collection must be 24 x 24 tile size");
                return;
            }

            // Get busy

            TemplateDoc tmpdDst = TemplateTools.CloneTemplateDoc(m_tmpdActive);

            TemplateTools.ScaleTemplates(tmpdDst, new Size(16, 16));
            TemplateTools.QuantizeTemplates(tmpdDst, null, 0, 0, 0);
            DocManager.SetActiveDocument(typeof(TemplateDoc), tmpdDst);
        }
コード例 #3
0
        private void menuItemQuantizeOnly_Click(object sender, System.EventArgs e)
        {
            if (m_tmpdActive == null)
            {
                return;
            }
            TemplateDoc tmpdDst = TemplateTools.CloneTemplateDoc(m_tmpdActive);

            TemplateTools.QuantizeTemplates(tmpdDst, null, 0, 0, 0);
            DocManager.SetActiveDocument(typeof(TemplateDoc), tmpdDst);
        }
コード例 #4
0
        public static void ExportLevels(string[] astr, int nVersion)
        {
            // Get tile size

            Size sizTile = new Size(0, 0);

            sizTile.Width  = int.Parse(astr[1]);
            sizTile.Height = sizTile.Width;

            // Get depth

            int nDepth = Int32.Parse(astr[2]);

            // Get background threshold

            double nAreaBackgroundThreshold = double.Parse(astr[3]);

            // Background luminance multiplier

            double nLuminanceMultBackground = double.Parse(astr[4]);

            // Background saturation multiplier

            double nSaturationMultBackground = double.Parse(astr[5]);

            // Foreground luminance multiplier

            double nLuminanceMultForeground = double.Parse(astr[6]);

            // Foreground saturation multiplier

            double nSaturationMultForeground = double.Parse(astr[7]);

            // Palette directory

            string strPalDir = astr[8];

            // Get output directory

            string strDir = Path.GetFullPath(astr[9]);

            // Expand filespecs
            ArrayList alsFiles = new ArrayList();

            for (int n = 9; n < astr.Length; n++)
            {
                string strFileT = Path.GetFileName(astr[n]);
                string strDirT  = Path.GetDirectoryName(astr[n]);
                if (strDirT == "")
                {
                    strDirT = ".";
                }
                string[] astrFiles = Directory.GetFiles(strDirT, strFileT);
                alsFiles.AddRange(astrFiles);
            }

            // Attempt to process these level doc
            ArrayList alsTileSets = new ArrayList();

            foreach (string strFile in alsFiles)
            {
                Console.Write("Loading " + strFile + "...");
                LevelDoc lvld = (LevelDoc)DocManager.OpenDocument(strFile);
                if (lvld == null)
                {
                    throw new Exception("Could not load level doc " + strFile);
                }
                Console.Write(" Done.\n");

                // Size this template collection if necessary

                TemplateDoc tmpd = lvld.GetTemplateDoc();
                if (sizTile.Width != tmpd.TileSize.Width || sizTile.Height != tmpd.TileSize.Height)
                {
                    TemplateTools.ScaleTemplates(tmpd, sizTile);
                }

                // Get appropriate TileSet, or make one if this map
                // uses a new tile collection
                TileSet tset = null;
                foreach (TileSet tsetT in alsTileSets)
                {
                    if (tsetT.TileCollectionFileName == Path.GetFileName(lvld.GetTemplateDoc().GetPath()))
                    {
                        tset = tsetT;
                        break;
                    }
                }

                // Create new tile set if none found
                if (tset == null)
                {
                    string strTcPath = lvld.GetTemplateDoc().GetPath();
                    string strTcName = Path.GetFileNameWithoutExtension(strTcPath);
                    string strTcDir  = Path.GetDirectoryName(strTcPath);
                    string strT3     = strTcDir + Path.DirectorySeparatorChar + strPalDir + Path.DirectorySeparatorChar + strTcName;
                    tset = new TileSet(lvld.GetTemplateDoc(), Path.GetFileName(strTcPath), strT3, nDepth, sizTile);
                    alsTileSets.Add(tset);
                }

                // Get and save a tile map for this level
                string strTmap     = Path.GetFileName(lvld.GetPath().Replace(".ld", ".tmap"));
                string strFileTmap = strDir + Path.DirectorySeparatorChar + strTmap;
                Console.Write("Creating & writing " + strFileTmap + "...");
                TileMap tmap = TileMap.CreateFromImage(tset, lvld.GetMapBitmap(tmpd.TileSize, tmpd, true), tmpd.TileSize);
                tmap.Save(strFileTmap);
                Console.Write(" Done.\n");

                // Get and save terrain map for this level
                string strTrmap     = Path.GetFileName(lvld.GetPath().Replace(".ld", ".trmap"));
                string strFileTrmap = strDir + Path.DirectorySeparatorChar + strTrmap;
                Console.Write("Creating & writing " + strFileTrmap + "...");
                TerrainMap trmap = new TerrainMap(lvld.GetTerrainMap(tmpd.TileSize, tmpd, false));
                trmap.Save(strFileTrmap);
                Console.Write(" Done.\n");

                // Save .ini for this level doc
                string strFileIni = strDir + Path.DirectorySeparatorChar + Path.GetFileName(strFile).Replace(".ld", ".lvl");
                Console.Write("Writing " + strFileIni + "...");
                lvld.SaveIni(strFileIni, nVersion, strTmap, strTrmap, tset.PalBinFileName);
                Console.Write(" Done.\n");
                lvld.Dispose();
            }

            // Now write out tilesets
            foreach (TileSet tset in alsTileSets)
            {
                // Save tile set
                string strFileTset = strDir + Path.DirectorySeparatorChar + tset.FileName;
                Console.Write("Creating & writing " + strFileTset + ", " + tset.Count.ToString() + " tiles...");
                tset.Save(strFileTset);
                tset.SaveMini(strFileTset, nAreaBackgroundThreshold, nLuminanceMultBackground, nSaturationMultBackground, nLuminanceMultForeground, nSaturationMultForeground);
                Console.Write(" Done.\n");
            }
        }
コード例 #5
0
        void WriteMiniTiles(BinaryWriter bwtr, Palette pal, TemplateDoc tmpd, int cx, bool fNext, double nAreaBackgroundThreshold, double nLuminanceMultBackground, double nSaturationMultBackground, double nLuminanceMultForeground, double nSaturationMultForeground)
        {
            // struct MiniTileSetHeader { // mtshdr
            //  ushort offNext;
            //    ushort cTiles;
            //    ushort cxTile;
            //    ushort cyTile;
            // };

            ushort offNext = 0;

            if (fNext)
            {
                offNext = (ushort)(8 + m_alsTileData.Count * cx * cx);
            }
            bwtr.Write(Misc.SwapUShort(offNext));
            bwtr.Write(Misc.SwapUShort((ushort)m_alsTileData.Count));
            bwtr.Write(Misc.SwapUShort((ushort)cx));
            bwtr.Write(Misc.SwapUShort((ushort)cx));

            // If a background template exists, use it to distinguish foreground from background objects for better minimaps

            Size      sizTile        = tmpd.TileSize;
            ArrayList alsColors      = new ArrayList();
            Template  tmplBackground = tmpd.GetBackgroundTemplate();

            if (tmplBackground != null && nAreaBackgroundThreshold >= 0.0)
            {
                // Get despeckled hue map of background, calc mean and
                // std dev for filtering purposes

                Bitmap bmHueBackground = TemplateTools.MakeHueMap(tmplBackground.Bitmap);
                TemplateTools.DespeckleGrayscaleBitmap(bmHueBackground, 9, 50);
                double nMean   = TemplateTools.CalcGrayscaleMean(bmHueBackground);
                double nStdDev = TemplateTools.CalcGrayscaleStandardDeviation(bmHueBackground, nMean);

                // Go through each tile, first make a mask that'll delineate foreground from background

                Bitmap bmTile = new Bitmap(sizTile.Width, sizTile.Height);
                foreach (TileData td in m_alsTileData)
                {
                    // Need to turn data back into a bitmap - doh!

                    for (int y = 0; y < sizTile.Height; y++)
                    {
                        for (int x = 0; x < sizTile.Width; x++)
                        {
                            Color clr = (Color)td.aclr[y * sizTile.Width + x];
                            bmTile.SetPixel(x, y, clr);
                        }
                    }

                    // Create mask which'll replace background with transparent color (255, 0, 255)

                    Bitmap bmMask = TemplateTools.MakeHueMap(bmTile);
                    TemplateTools.DespeckleGrayscaleBitmap(bmMask, 9, 50);
                    TemplateTools.SubtractGrayscaleDistribution(bmMask, nMean, nStdDev);

                    // Now scale tile down to desired size, using mask as input

                    Bitmap bmScaled = TemplateTools.ScaleTemplateBitmap(bmTile, bmMask, cx, cx, nAreaBackgroundThreshold, nLuminanceMultBackground, nSaturationMultBackground, nLuminanceMultForeground, nSaturationMultForeground);

                    // Grab the data

                    for (int y = 0; y < cx; y++)
                    {
                        for (int x = 0; x < cx; x++)
                        {
                            alsColors.Add(bmScaled.GetPixel(x, y));
                        }
                    }
                    bmScaled.Dispose();
                    bmMask.Dispose();
                }
            }
            else
            {
                // No background template; just scale

                Bitmap bmTile = new Bitmap(sizTile.Width, sizTile.Height);
                foreach (TileData td in m_alsTileData)
                {
                    // Need to turn data back into a bitmap - doh!

                    for (int y = 0; y < sizTile.Height; y++)
                    {
                        for (int x = 0; x < sizTile.Width; x++)
                        {
                            Color clr = (Color)td.aclr[y * sizTile.Width + x];
                            bmTile.SetPixel(x, y, clr);
                        }
                    }

                    // Now scale tile down to desired size, using mask as input

                    Bitmap bmScaled = TemplateTools.ScaleTemplateBitmap(bmTile, null, cx, cx, 1.0, 1.0, 1.0, 1.0, 1.0);

                    // Grab the data

                    for (int y = 0; y < cx; y++)
                    {
                        for (int x = 0; x < cx; x++)
                        {
                            alsColors.Add(bmScaled.GetPixel(x, y));
                        }
                    }
                    bmScaled.Dispose();
                }
            }

            // Palette match and write results

            foreach (Color clr in alsColors)
            {
                bwtr.Write((byte)pal.FindClosestEntry(clr));
            }
        }
コード例 #6
0
ファイル: tile.cs プロジェクト: vikas100/hostile-takeover
        // IMapItem

        public override Bitmap GetBitmap(Size sizTile, TemplateDoc tmpd)
        {
            // Get the template. This'll invalidate m_bmCache if necessary

            Template tmpl = GetTemplate(tmpd);

            // If already cached, use it

            if (m_bmCache != null)
            {
                return(m_bmCache);
            }

            // If we haven't mapped to a template, create a correctly sized bitmap as a placeholder

            bool   fDispose = false;
            Bitmap bm;

            if (tmpl != null)
            {
                bm = tmpl.Bitmap;

                // Dont use the bitmap directly, it might have knobbies on that'll create
                // problems at compile time.

#if false
                if (m_afVisible == null)
                {
                    return(bm);
                }
#endif
            }
            else
            {
                fDispose = true;
                bm       = new Bitmap(m_afOccupancy.GetLength(1) * sizTile.Width, m_afOccupancy.GetLength(0) * sizTile.Height);
                using (Graphics gT = Graphics.FromImage(bm))
                    gT.Clear(Color.Firebrick);
            }

            // Fill in the chunks that are visible

            int cxT = m_afOccupancy.GetLength(1) * sizTile.Width;
            int cyT = m_afOccupancy.GetLength(0) * sizTile.Height;
            m_bmCache = new Bitmap(cxT, cyT);
            using (Graphics g = Graphics.FromImage(m_bmCache)) {
                g.Clear(Color.FromArgb(255, 0, 255));
                for (int ty = 0; ty < m_afOccupancy.GetLength(0); ty++)
                {
                    for (int tx = 0; tx < m_afOccupancy.GetLength(1); tx++)
                    {
                        if (!m_afOccupancy[ty, tx])
                        {
                            continue;
                        }
                        if (!IsVisible(tx, ty))
                        {
                            continue;
                        }
                        Rectangle rcDst = new Rectangle(tx * sizTile.Width, ty * sizTile.Height, sizTile.Width, sizTile.Height);
                        g.DrawImage(bm, rcDst, tx * sizTile.Width, ty * sizTile.Height, sizTile.Width, sizTile.Height, GraphicsUnit.Pixel);
                    }
                }
            }
            if (fDispose)
            {
                bm.Dispose();
            }

            // Hide the areas that are invisible

            TemplateTools.MakeTransparent(m_bmCache);
            return(m_bmCache);
        }