Beispiel #1
0
        private void GenRooms(object sender, EventArgs e) //generate rooms (button pressed)
        {
            int t;                                        //used to swap minwide and maxwide

            if (!int.TryParse(numbertries.Text, out numtries))
            {
                numtries = 5000;                                                //set number tries from form
            }
            if (!int.TryParse(minimumwidth.Text, out minwide))
            {
                minwide = 50;                                                //set minimum width from form
            }
            if (!int.TryParse(maximumwidth.Text, out maxwide))
            {
                maxwide = 100;                                                //set maximum width from form
            }
            if (maxwide < minwide)
            {
                t = minwide; minwide = maxwide; maxwide = t;
            }                                                                       //if maximum width less than minimum width, swap them
            if (!int.TryParse(spaceadded.Text, out spacer))
            {
                spacer = 50; //set spacer size from form
            }
            numrooms = 0;    //number rooms = 0
            for (int i = 0; i < maxrooms; i++)
            {
                isroom[i] = false; //clear all rooms
            }
            keepgoing = true;      //set flag to keep going
            while (keepgoing)      //while we should keep going
            {
                if (!AddRoom())
                {
                    keepgoing = false;             //if unable to add a room, clear keep going flag (stop)
                }
            }
            tunnelsizetext.Enabled = true; //Enable tunnel size text on form
            tunnelsize.Enabled     = true; //Enable tunnel size on form
            gentunnels.Enabled     = true; //Enable get tunnels button
            numtunnels             = 0;    //number tunnels = 0
            for (int i = 0; i < maxtunnels; i++)
            {
                istunnel[i] = false; //clear all tunnels
            }
            DoSort();                //sort rooms
            MapArea.Invalidate();    //redraw map area
            InfoArea.Invalidate();   //redraw info area
        }
Beispiel #2
0
        private void GenerateTunnels(object sender, EventArgs e) //generate tunnels (button pressed)
        {
            if (!int.TryParse(tunnelsize.Text, out int size))
            {
                size = 24;        //get tunnel size from form
            }
            numtunnels = 0;       //set number tunnels to 0
            Tunnel path, endpath; //temporary tunnels storage

            for (int i = 0; i < maxtunnels; i++)
            {
                istunnel[i] = false;           //clear all tunnels
            }
            int first = 0;                     //set first to 0

            for (int i = 0; i < maxrooms; i++) //use first room
            {
                if (isroom[i])                 //if room exists
                {
                    first = i;                 //set first to room number
                    i     = maxrooms;          //exit loop
                }
            }
            keepgoing = true;                              //set keep going flag to true
            while (keepgoing)                              //while we are supposed to keep going
            {
                int next = 0;                              //set next room to 0
                for (int i = first + 1; i < maxrooms; i++) //use first room (after first)
                {
                    if (isroom[i])                         //if room exists
                    {
                        next = i;                          //set next to room number
                        i    = maxrooms;                   //exit loop
                    }
                }
                if (next == 0)
                {
                    keepgoing = false;            //if there was no next room, we are done
                }
                else //otherwise (keep going)
                {
                    int dist = rooms[next].GetDistance(rooms[first]);      //get distance from first room to next room
                    for (int i = first - 1; i > -1; i--)                   //check all rooms before first room (already processed rooms)
                    {
                        if (rooms[next].GetDistance(rooms[i]) < dist)      //if room is at a shorter distance
                        {
                            first = i;                                     //set first to that room number
                            dist  = rooms[next].GetDistance(rooms[first]); //set that room's distance for further testing
                        }
                    }
                    rooms[first].GetDir(rooms[next], out int xdir, out int ydir);                                 //get hint for direction
                    if (Math.Abs(xdir) > Math.Abs(ydir))                                                          //if x distance is greater than y distance
                    {
                        if (xdir > 0)                                                                             //if x direction is positive
                        {
                            path    = new Tunnel(rooms[first].LastX() + 1, rooms[first].MidY() - size / 2, size); //make first tunnel
                            path    = AddTunnel(path, 0, 0);                                                      //add it to tunnels
                            path    = AddTunnel(path, size, 0);                                                   //add next tunnel
                            endpath = new Tunnel(rooms[next].X - size + 1, rooms[next].MidY() - size / 2, size);  //make end tunnel
                        }
                        else //otherwise (x direction is negative)
                        {
                            path    = new Tunnel(rooms[first].X - size + 1, rooms[first].MidY() - size / 2, size); //make first tunnel
                            path    = AddTunnel(path, 0, 0);                                                       //add it to tunnels
                            path    = AddTunnel(path, -size, 0);                                                   //add next tunnel
                            endpath = new Tunnel(rooms[next].LastX() + 1, rooms[next].MidY() - size / 2, size);    //make end tunnel
                        }
                    }
                    else //otherwise (y distance greater than x distance)
                    {
                        if (ydir > 0) //if y direction is positive
                        {
                            path    = new Tunnel(rooms[first].MidX() - size / 2, rooms[first].LastY() + 1, size); //make first tunnel
                            path    = AddTunnel(path, 0, 0);                                                      //add it to tunnels
                            path    = AddTunnel(path, 0, size);                                                   //add next tunnel
                            endpath = new Tunnel(rooms[next].MidX() - size / 2, rooms[next].Y - size + 1, size);  //make end tunnel
                        }
                        else //otherwise (y direction is negative)
                        {
                            path    = new Tunnel(rooms[first].MidX() - size / 2, rooms[first].Y - size + 1, size); //make first tunnel
                            path    = AddTunnel(path, 0, 0);                                                       //add it to tunnels
                            path    = AddTunnel(path, 0, -size);                                                   //add next tunnel
                            endpath = new Tunnel(rooms[next].MidX() - size / 2, rooms[next].LastY() + 1, size);    //make end tunnel
                        }
                    }
                    while (!path.IsHit(rooms[next]))              //while tunnel has not reached next room
                    {
                        path.GetDir(endpath, out xdir, out ydir); //get hint for direction to end tunnel
                        if ((xdir < size) && (-size < xdir) && (ydir < size) && (-size < ydir))
                        {
                            path.GetDir(rooms[next], out xdir, out ydir); //if end tunnel too close, use next room
                        }
                        if (Math.Abs(xdir) > Math.Abs(ydir))              //if x distance greater than y distance
                        {
                            if (xdir > 0)
                            {
                                path = AddTunnel(path, size, 0);           //if x direction positive, add tunnel that way
                            }
                            else
                            {
                                path = AddTunnel(path, -size, 0);  //otherwise (x direction negative), add tunnel that way
                            }
                        }
                        else //otherwise (y distance greater than x distance)
                        {
                            if (ydir > 0)
                            {
                                path = AddTunnel(path, 0, size);           //if y direction position, add tunnel that way
                            }
                            else
                            {
                                path = AddTunnel(path, 0, -size);  //otherwise (y direction negative), add tunnel that way
                            }
                        }
                    }
                    first = next; //set first room to next room (set as processed)
                }
            }
            MapArea.Invalidate();  //redraw map area
            InfoArea.Invalidate(); //redraw information area
        }