MapGen Plugin

MapGen Plugin

Postby ismellike » 28 Jun 2013, 22:42

This plugin is an add on to /newlvl. It adds 4 new map types: hell, space, rainbow, and zombie.
The first 3 map types are from McForge. I fixed space by adding stars and rainbow by making it cool instead of random colors.
Zombie is just like /newlvl x y z flat, except the dirt is adminium (this was a suggestion I made a long time ago)
Anyways without further ado, I provide you MapGen!!

Code: Select all

using System.Windows.Forms;
using System.Drawing;
using System;
using System.IO;
namespace MCDzienny.Plugins
{
    public class MapGen : Plugin
    {
        // The name of the plugin.
        string name = "MapGen";
        // A relatively short description of the plugin.
        string description = "Generate new types of maps!";
        // A name or names of the authors of the plugin.
        string author = "ismellike";

        // A version number that is displayed to the end users.
        string version = "1.3";

        // A version number that is used for update checking. It's an absolute number that
        // represents a release number. It should be incremented by 1 each time the new version
        // is released.
        int versionNumber = 1;

        // A user control that contains graphical user interface of the plugin.
        UserControl gui = new MapGen_GUI();
        public override string Description
        {
            get { return description; }
        }

        public override string Author
        {
            get { return author; }
        }

        public override string Name
        {
            get { return name; }
        }

        public override UserControl MainInterface
        {
            get { return gui; }
        }

        public override string Version
        {
            get { return version; }
        }

        public override int VersionNumber
        {
            get { return versionNumber; }
        }
        public override void Initialize()
        {
            if (!Directory.Exists("extra/plugins"))
                Directory.CreateDirectory("extra/plugins");
            if (!Directory.Exists("extra/plugins/appdata"))
                Directory.CreateDirectory("extra/plugins/appdata");
            if (!Directory.Exists("extra/plugins/source"))
                Directory.CreateDirectory("extra/plugins/source");
        }
        public override void Terminate()
        {
        }
    }
}

namespace MCDzienny
{
    public partial class MGen
    {
        static float[] overlay;
        static float[] terrain;
        static float divide;
        float Range(float input, float low, float high)
        {
            if (high <= low) { return low; }
            return low + (input * (high - low));
        }
        ushort Evaluate(Level lvl, float height)
        {
            ushort temp = (ushort)(height * lvl.depth);
            if (temp < 0) return 0;
            if (temp > lvl.depth - 1) return (ushort)(lvl.depth - 1);
            return temp;
        }
        void FilterAverage(Level Lvl)
        {

            float[] filtered = new float[terrain.Length];

            for (int bb = 0; bb < terrain.Length; bb++)
            {
                ushort x = (ushort)(bb % Lvl.width);
                ushort y = (ushort)(bb / Lvl.width);
                filtered[bb] = GetAverage9(x, y, Lvl);
            }

            for (int bb = 0; bb < terrain.Length; bb++)
            {
                terrain[bb] = filtered[bb];
            }
        }
        float GetPixel(ushort x, ushort y, Level Lvl)
        {
            if (x < 0) { return 0.0f; }
            if (x >= Lvl.width) { return 0.0f; }
            if (y < 0) { return 0.0f; }
            if (y >= Lvl.height) { return 0.0f; }
            divide += 1.0f;
            return terrain[x + y * Lvl.width];
        }
        float GetAverage9(ushort x, ushort y, Level Lvl)
        {
            divide = 0.0f;
            float temp = GetPixel(x, y, Lvl);
            temp += GetPixel((ushort)(x + 1), y, Lvl);
            temp += GetPixel((ushort)(x - 1), y, Lvl);
            temp += GetPixel(x, (ushort)(y + 1), Lvl);
            temp += GetPixel(x, (ushort)(y - 1), Lvl);

            temp += GetPixel((ushort)(x + 1), (ushort)(y + 1), Lvl);
            temp += GetPixel((ushort)(x - 1), (ushort)(y + 1), Lvl);
            temp += GetPixel((ushort)(x + 1), (ushort)(y - 1), Lvl);
            temp += GetPixel((ushort)(x - 1), (ushort)(y - 1), Lvl);

            return temp / divide;
        }
        public void GenerateHell(Level Lvl, ushort width, ushort height, ushort depth)
        {
            terrain = new float[Lvl.width * Lvl.height];
            overlay = new float[Lvl.width * Lvl.height];
            float RangeLow = .3f;
            float RangeHigh = 1.3f;
            ushort LavaLevel = 5;
            Random rand = new Random();
            GenerateFault(terrain, Lvl, "hell", rand);
            FilterAverage(Lvl);
            GeneratePerlinNoise(overlay, Lvl, "", rand);
            for (int bb = 0; bb < terrain.Length; bb++)
            {
                ushort x = (ushort)(bb % Lvl.width);
                ushort y = (ushort)(bb / Lvl.width);
                ushort z = Evaluate(Lvl, Range(terrain[bb], RangeLow, RangeHigh));
                if (z > LavaLevel)
                {
                    for (ushort zz = 0; z - zz >= 0; zz++)
                    {
                        if (z > (LavaLevel - 1))
                        {
                            if (zz == 0) { Lvl.skipChange(x, (ushort)(z - zz), y, Block.rock); }      //top layer
                            else if (zz < 3) { Lvl.skipChange(x, (ushort)(z - zz), y, Block.rock); }
                            else if (zz < 2) { Lvl.skipChange(x, (ushort)(z - zz), y, Block.lava); }//next few
                            else { Lvl.skipChange(x, (ushort)(z - zz), y, Block.obsidian); }
                        }
                        else
                        {
                            Lvl.skipChange(x, (ushort)(z - zz), y, Block.lava);
                        }
                        if (overlay[bb] < 0.3f)
                        {
                            switch (rand.Next(13))
                            {
                                case 9:
                                case 10:
                                case 11:
                                case 12:
                                    Lvl.skipChange(x, (ushort)(z + 1), y, Block.lava); //change to lava when time
                                    break;
                                default:
                                    break;
                            }
                        }
                        // if (zz == z) Lvl.skipChange(x, (ushort)(z - zz), y, Block.opsidian);
                        Lvl.skipChange(x, (ushort)(z), y, (rand.Next(100) % 3 == 1 ? Block.darkgrey : Block.obsidian));

                    }
                }

                else
                {
                    for (ushort zz = 0; LavaLevel - zz >= 0; zz++)
                    {

                        if (LavaLevel - zz > z - 1)
                        { /*if (Lvl.GetTile(x, z, y) == Block.air)*/ Lvl.skipChange(x, (ushort)(LavaLevel - zz), y, Block.lava); }    //better fill the water aboce me
                        else if (LavaLevel - zz > z - 3)
                        {
                            if (overlay[bb] < .9f)
                            {
                                if (zz < z) Lvl.skipChange(x, (ushort)(z - zz), (ushort)(y), Block.lava);
                                else Lvl.skipChange(x, (ushort)(z - zz), y, Block.rock);
                            }
                            else
                            {
                                Lvl.skipChange(x, (ushort)(LavaLevel - zz), (ushort)(y - 5), Block.lava);  //killer lava
                            }
                        }
                        else
                        {
                            Lvl.skipChange(x, (ushort)(LavaLevel - zz), y, Block.stone); //and just make the rest cobblestone
                        }
                    }
                }
            }
        }
        #region ==FaultGen==
        void GenerateFault(float[] array, Level Lvl, string type, Random rand)
        {
            float startheight = 0.5f;
            float dispAux;
            ushort i, j, k, halfX, halfZ;
            float a, b, c, w, d;

            float DispMax, DispMin, DispChange;
            DispMax = 0.01f;
            DispChange = -0.0025f;
            if (type.Equals("mountains"))
            {
                DispMax = 0.02f;
                startheight = 0.6f;
            }
            else if (type.Equals("hell"))
            {
                DispMax = 0.02f;
                startheight = 0.04f;
            }
            else if (type.Equals("overlay"))
            {
                DispMax = 0.02f;
                DispChange = -0.01f;
            }

            for (int x = 0; x < array.Length; x++)
            {
                array[x] = startheight;
            }
            DispMin = -DispMax;
            float disp = DispMax;
            halfX = (ushort)(Lvl.width / 2);
            halfZ = (ushort)(Lvl.height / 2);
            int numIterations = (int)((Lvl.width + Lvl.height));
            for (k = 0; k < numIterations; k++)
            {
                d = (float)Math.Sqrt(halfX * halfX + halfZ * halfZ);
                w = (float)(rand.NextDouble() * 360);
                a = (float)Math.Cos(w);
                b = (float)Math.Sin(w);

                c = ((float)rand.NextDouble()) * 2 * d - d;
                for (i = 0; i < Lvl.height; i++)
                {
                    for (j = 0; j < Lvl.width; j++)
                    {
                        if ((i - halfZ) * a + (j - halfX) * b + c > 0)
                            dispAux = disp;
                        else
                            dispAux = -disp;
                        AddTerrainHeight(array, j, i, Lvl.width, dispAux);

                    }
                }

                disp += DispChange;
                if (disp < DispMin) { disp = DispMax; }
            }
        }
        #endregion
        void AddTerrainHeight(float[] array, ushort x, ushort y, ushort width, float height)
        {
            int temp = x + y * width;
            if (temp < 0) return;
            if (temp > array.Length) return;

            array[temp] += height;

            if (array[temp] > 1.0f) array[temp] = 1.0f;
            if (array[temp] < 0.0f) array[temp] = 0.0f;
        }

        //hur hur, more copy pasted code :/
        #region ==PerlinGen==
        void GeneratePerlinNoise(float[] array, Level Lvl, string type, Random rand)
        {
            GenerateNormalized(array, 0.7f, 8, Lvl.width, Lvl.height, rand.Next(), 64);
        }
        void GenerateNormalized(float[] array, float persistence, int octaves, int width, int height, int seed, float zoom)
        {
            float min = 0;
            float max = 0;
            //float * pDataFloat = new float[width * height];

            //Generate raw float data
            for (int y = 0; y < height; ++y)
            {
                for (int x = 0; x < width; ++x)
                {
                    float total = 0;
                    float frequency = 1;
                    float amplitude = 1;

                    for (int i = 0; i < octaves; ++i)
                    {
                        total = total + InterpolatedNoise(x * frequency / zoom, y * frequency / zoom, seed) * amplitude;
                        frequency *= 2;
                        amplitude *= persistence;
                    }

                    array[y * width + x] = total;

                    min = total < min ? total : min;
                    max = total > max ? total : max;
                }
            }

            //Normalize
            for (int i = 0; i < width * height; ++i)
            {
                array[i] = (array[i] - min) / (max - min);
                //array[i] = (255 << 24) | ((unsigned char) (red * ((pDataFloat[i] - min) / (max - min)) * 255) << 16) |
                //((unsigned char) (green * ((pDataFloat[i] - min) / (max - min)) * 255) << 8) | (unsigned char) (blue * ((pDataFloat[i] - min) / (max - min)) * 255);
            }
        }
        float InterpolatedNoise(float x, float y, int seed)
        {
            int wholePartX = (int)x;
            float fractionPartX = x - wholePartX;

            int wholePartY = (int)y;
            float fractionPartY = y - wholePartY;

            float v1 = SmoothNoise(wholePartX, wholePartY, seed);
            float v2 = SmoothNoise(wholePartX + 1, wholePartY, seed);
            float v3 = SmoothNoise(wholePartX, wholePartY + 1, seed);
            float v4 = SmoothNoise(wholePartX + 1, wholePartY + 1, seed);

            float i1 = Interpolate(v1, v2, fractionPartX);
            float i2 = Interpolate(v3, v4, fractionPartX);

            return Interpolate(i1, i2, fractionPartY);
        }
        float SmoothNoise(int x, int y, int seed)
        {
            float corners = (Noise(x - 1, y - 1, seed) + Noise(x + 1, y - 1, seed) + Noise(x - 1, y + 1, seed) + Noise(x + 1, y + 1, seed)) / 16;
            float sides = (Noise(x - 1, y, seed) + Noise(x + 1, y, seed) + Noise(x, y - 1, seed) + Noise(x, y + 1, seed) / 8);
            float center = Noise(x, y, seed) / 4;
            return corners + sides + center;
        }
        float Noise(int x, int y, int seed)
        {
            int n = x + y * 57 + seed;
            n = (n << 13) ^ n;
            return (float)(1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
            //return value is always in range [-1.0, 1.0]
        }
        float Interpolate(float a, float b, float x)
        {
            float ft = x * 3.1415927f;
            float f = (float)(1 - Math.Cos(ft)) * .5f;

            return a * (1 - f) + b * f;
        }
        public void GenerateMap(string name, ushort x, ushort y, ushort z, string type, Level lvl)
        {
            try
            {
                ushort width = x, height = y, depth = z;
                Random r = new Random();
                ushort num = (ushort)((uint)depth / 2U);
                switch (type)
                {
                    case "zombie":
                        for (x = (ushort)0; (int)x < (int)width; ++x)
                        {
                            for (z = (ushort)0; (int)z < (int)height; ++z)
                            {
                                for (y = (ushort)0; (int)y <= (int)num; ++y)
                                    lvl.SetTile(x, y, z, (int)y < (int)num ? Block.blackrock : Block.grass);
                            }
                        }
                        break;
                    case "space":
                        for (x = (ushort)0; (int)x < (int)width; ++x)
                        {
                            for (z = (ushort)0; (int)z < (int)height; ++z)
                            {
                                for (y = (ushort)0; (int)y < (int)depth; ++y)
                                {
                                    if ((int)y == 0)
                                        lvl.SetTile(x, y, z, (byte)7);
                                    else if ((int)x == 0 || (int)x == (int)width - 1 || ((int)z == 0 || (int)z == (int)height - 1) || ((int)y == 1 || (int)y == (int)depth - 1))
                                        lvl.SetTile(x, y, z, r.Next(0, 60) == 25 ? Block.white : Block.obsidian);
                                }
                            }
                        }
                        break;
                    case "rainbow":
                        byte block = 21;
                        for (x = (ushort)0; (int)x < (int)width; ++x)
                        {
                            for (z = (ushort)0; (int)z < (int)height; ++z)
                            {
                                for (y = (ushort)0; (int)y < (int)depth; ++y)
                                {
                                    if (y == 0)
                                        lvl.SetTile(x, y, z, Block.blackrock);
                                    if ((int)y == (int)depth - 1)
                                        if (block == 33)
                                        {
                                            block = 21;
                                        }
                                        else
                                        {
                                            block += 1;
                                        }
                                    if ((int)y == 1 || (int)y == (int)depth - 1 || ((int)x == 0 || (int)x == (int)width - 1) || ((int)z == 0 || (int)z == (int)height - 1))
                                        lvl.SetTile(x, y, z, block);
                                }
                            }
                        }
                        break;
                    case "hell":
                        for (x = (ushort)0; (int)x < (int)width; ++x)
                        {
                            for (z = (ushort)0; (int)z < (int)height; ++z)
                            {
                                for (y = (ushort)0; (int)y < (int)depth; ++y)
                                {
                                    if ((int)y == 0)
                                        lvl.SetTile(x, y, z, (byte)7);
                                    else if ((int)x == 0 || (int)x == (int)width - 1 || ((int)z == 0 || (int)z == (int)height - 1) || ((int)y == 0 || (int)y == (int)depth - 1))
                                        lvl.SetTile(x, y, z, (byte)49);
                                    else if (((int)x == 1 || (int)x == (int)width - 2 || ((int)z == 1 || (int)z == (int)height - 2)) && r.Next(1000) == 7)
                                    {
                                        for (int index = 1; index < (int)depth - (int)y; ++index)
                                            lvl.SetTile(x, (ushort)((uint)depth - (uint)index), z, (byte)10);
                                    }
                                }
                            }
                        }
                        GenerateHell(lvl, x, y, z);
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        #endregion
    }
}


namespace MCDzienny.Plugins
{
    public partial class MapGen_GUI : UserControl
    {
        public MapGen_GUI()
        {
            InitializeComponent();
        }
    }
}

namespace MCDzienny.Plugins
{
    partial class MapGen_GUI
    {
        private System.ComponentModel.IContainer components = null;
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        #region Windows Form Designer generated code
        void addit_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("No name given.");
                return;
            }
            if (comboBox1.Text == "")
            {
                MessageBox.Show("No type given.");
                return;
            }
            if (!comboBox1.Items.Contains(comboBox1.Text))
            {
                MessageBox.Show("Invalid map type");
                return;
            }
            if (!checkBox2.Checked && File.Exists("levels/" + textBox1.Text + ".lvl"))
            {
                MessageBox.Show("Map already exists.");
                return;
            }
            ushort x, y, z;
            try
            {
                x = Convert.ToUInt16(comboBox2.Text); y = Convert.ToUInt16(comboBox3.Text); z = Convert.ToUInt16(comboBox4.Text);
            }
            catch
            {
                MessageBox.Show("Invalid sizes");
                return;
            }
            MGen mg = new MGen();
            Level nlvl = new Level(textBox1.Text, x, y, z, "freebuild");
            mg.GenerateMap(textBox1.Text, x, y, z, comboBox1.Text, nlvl);
            nlvl.Save(true);
            if (checkBox1.Checked)
                nlvl = Level.Load(nlvl.name);
            MessageBox.Show(nlvl.name + " was made.");
        }
        private void InitializeComponent()
        {
            addit = new Button();
            textBox1 = new TextBox();
            label1 = new Label();
            comboBox1 = new ComboBox();
            label2 = new Label();
            checkBox1 = new CheckBox();
            comboBox2 = new ComboBox();
            comboBox3 = new ComboBox();
            comboBox4 = new ComboBox();
            label3 = new Label();
            label4 = new Label();
            checkBox2 = new CheckBox();
            label5 = new Label();
            SuspendLayout();
            //
            // addit
            //
            addit.BackColor = Color.White;
            addit.Location = new Point(18, 115);
            addit.Name = "addit";
            addit.Size = new Size(480, 23);
            addit.TabIndex = 0;
            addit.Text = "Generate Map";
            addit.UseVisualStyleBackColor = false;
            addit.Click += new System.EventHandler(addit_Click);
            //
            // textBox1
            //
            textBox1.Location = new Point(78, 12);
            textBox1.Name = "textBox1";
            textBox1.Size = new Size(414, 21);
            textBox1.TabIndex = 1;
            //
            // label1
            //
            label1.AutoSize = true;
            label1.Location = new Point(9, 15);
            label1.Name = "label1";
            label1.Size = new Size(63, 14);
            label1.TabIndex = 2;
            label1.Text = "Map Name";
            //
            // comboBox1
            //
            comboBox1.FormattingEnabled = true;
            comboBox1.Location = new Point(78, 39);
            comboBox1.Name = "comboBox1";
            comboBox1.Size = new Size(121, 22);
            comboBox1.TabIndex = 3;
            comboBox1.Items.Add("zombie");
            comboBox1.Items.Add("hell");
            comboBox1.Items.Add("rainbow");
            comboBox1.Items.Add("space");
            //
            // label2
            //
            label2.AutoSize = true;
            label2.Location = new Point(15, 42);
            label2.Name = "label2";
            label2.Size = new Size(57, 14);
            label2.TabIndex = 4;
            label2.Text = "Map Type";
            //
            // checkBox1
            //
            checkBox1.AutoSize = true;
            checkBox1.Location = new Point(369, 42);
            checkBox1.Name = "checkBox1";
            checkBox1.Size = new Size(49, 18);
            checkBox1.TabIndex = 5;
            checkBox1.Text = "Load";
            checkBox1.UseVisualStyleBackColor = true;
            //
            // comboBox2
            //
            this.comboBox2.FormattingEnabled = true;
            this.comboBox2.Location = new System.Drawing.Point(18, 87);
            this.comboBox2.Name = "comboBox2";
            this.comboBox2.Size = new System.Drawing.Size(121, 22);
            this.comboBox2.TabIndex = 6;
            //
            // comboBox3
            //
            this.comboBox3.FormattingEnabled = true;
            this.comboBox3.Location = new System.Drawing.Point(198, 87);
            this.comboBox3.Name = "comboBox3";
            this.comboBox3.Size = new System.Drawing.Size(121, 22);
            this.comboBox3.TabIndex = 7;
            //
            // comboBox4
            //
            this.comboBox4.FormattingEnabled = true;
            this.comboBox4.Location = new System.Drawing.Point(369, 87);
            this.comboBox4.Name = "comboBox4";
            this.comboBox4.Size = new System.Drawing.Size(121, 22);
            this.comboBox4.TabIndex = 8;
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(49, 69);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(37, 14);
            this.label3.TabIndex = 9;
            this.label3.Text = "Width";
            //
            // label4
            //
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(234, 69);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(40, 14);
            this.label4.TabIndex = 10;
            this.label4.Text = "Height";
            //
            // label5
            //
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(402, 69);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(38, 14);
            this.label5.TabIndex = 11;
            this.label5.Text = "Depth";
            //
            // checkBox2
            //
            checkBox2.AutoSize = true;
            checkBox2.Location = new Point(251, 41);
            checkBox2.Name = "checkBox2";
            checkBox2.Size = new Size(70, 18);
            checkBox2.TabIndex = 12;
            checkBox2.Text = "Override";
            checkBox2.UseVisualStyleBackColor = true;
            //
            // GUI
            //
            AutoScaleDimensions = new SizeF(6F, 14F);
            AutoScaleMode = AutoScaleMode.Font;
            BackColor = Color.Beige;
            ClientSize = new Size(504, 646);
            Controls.Add(checkBox2);
            Controls.Add(label5);
            Controls.Add(label4);
            Controls.Add(label3);
            Controls.Add(comboBox4);
            Controls.Add(comboBox3);
            Controls.Add(comboBox2);
            Controls.Add(checkBox1);
            Controls.Add(label2);
            Controls.Add(comboBox1);
            Controls.Add(label1);
            Controls.Add(textBox1);
            Controls.Add(addit);
            Font = new Font("Miramonte", 8.25F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(0)));
            ForeColor = Color.Black;
            Name = "GUI";
            Text = "GUI";
            ResumeLayout(false);
            PerformLayout();
            foreach (Control c in Controls)
            {
                if (c is ComboBox)
                    if (c != comboBox1)
                    {
                        ((ComboBox)c).Items.Add(16);
                        ((ComboBox)c).Items.Add(32);
                        ((ComboBox)c).Items.Add(64);
                        ((ComboBox)c).Items.Add(128);
                        ((ComboBox)c).Items.Add(256);
                        ((ComboBox)c).Items.Add(512);
                        ((ComboBox)c).Items.Add(1024);
                        ((ComboBox)c).Text = "64";
                    }
            }
        }

        #endregion


        private Button addit;
        public TextBox textBox1;
        private Label label1;
        public ComboBox comboBox1;
        private Label label2;
        public CheckBox checkBox1;
        public ComboBox comboBox2;
        public ComboBox comboBox3;
        public ComboBox comboBox4;
        private Label label3;
        private Label label4;
        private Label label5;
        private CheckBox checkBox2;
    }
}


Any questions/suggestions accepted.
I will make the "hell" type cooler soon.
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: MapGen Plugin

Postby ismellike » 29 Jun 2013, 01:59

Hey guys, updated "hell" to make it look good. I used MCForge's MapGenerator.cs to help me with it.
hell now has autogenerated terrain.
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas


Return to Plugins

Who is online

Users browsing this forum: No registered users and 2 guests

cron