Announcement

Collapse
No announcement yet.

SaxonRahs Tutorial Thread - Random Maze Generation & Solving

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    #16
    Code for You SaxonRah!

    and others!

    I made my own USTRUCT system for having a multi-dimensional UPROPERTY() friendly dynamic array version of your maze data structure.

    Here's that code for you and others to enjoy!

    .h
    Code:
    USTRUCT()
    struct FMazeGridRow
    {
    	GENERATED_USTRUCT_BODY()
    
    	UPROPERTY()
    	TArray<AStaticMeshActor*> Columns;
    	
    	void AddNewColumn()
    	{
    		Columns.Add(NULL);
    	}
    	//default properties
    	FMazeGridRow()
    	{
    		
    	}
    };
    Code:
    USTRUCT()
    struct FMazeGrid
    {
    	GENERATED_USTRUCT_BODY()
    
    	UPROPERTY()
    	TArray<FMazeGridRow> Rows;
    	
    	void AddNewRow()
    	{
    		Rows.Add(FMazeGridRow());
    	}
    	
    	void AddUninitialized(const int32 RowCount, const int32 ColCount)
    	{
    		//Add Rows
    		for(int32 v = 0; v < RowCount; v++)
    		{
    			AddNewRow();
    		}
    		
    		//Add Columns
    		for(int32 v = 0; v < RowCount; v++)
    		{
    			for(int32 b = 0; b < ColCount; b++)
    			{
    				Rows[v].AddNewColumn();
    			}
    		}
    	}
    	
    	void Clear()
    	{
    		if(Rows.Num() <= 0) return;
    		//~~~~~~~~~~~~~~~
    		
    		//Destroy any walls
    		const int32 RowTotal = Rows.Num();
    		const int32 ColTotal = Rows[0].Columns.Num();
    		
    		for(int32 v = 0; v < RowTotal; v++)
    		{
    			for(int32 b = 0; b < ColTotal; b++)
    			{
    				if(Rows[v].Columns[b] && Rows[v].Columns[b]->IsValidLowLevel() )
    				{
    					Rows[v].Columns[b]->Destroy();
    				}
    			}
    		}
    		
    		//Empty
    		for(int32 v = 0; v < Rows.Num(); v++)
    		{
    			Rows[v].Columns.Empty();
    		}
    		Rows.Empty();
    	}
    	//default properties
    	FMazeGrid()
    	{
    		
    	}
    };
    ~~~

    Maze Grid Var Declaration for Persistence and Global Access

    Code:
    //Now you have dynamic array benefits and also UPROPERTY()!
    UPROPERTY()
    FMazeGrid JoyMazeGrid;
    .Cpp

    Code:
    //Init Maze
    JoyMazeGrid.Clear();
    JoyMazeGrid.AddUninitialized(tileX, tileY);
    	
    //Sample usage
    //JoyMazeGrid.Rows[x].Columns[y] = SpawnBP<AStaticMeshActor>(GetWorld(), ...
    ~~~

    Summary

    The final result of all of this is that you have

    1. dynamic array benefits (easily change the maze size and regenerate during runtime)

    2. global access to the MazeGrid Actors, even from other classes

    3. GC protection from the UPROPERTY() (if not using actors/objects for some reason)

    4. And yet the syntax for 2D array using UPROPERTY() and TArray is still clean and clear and precise

    JoyMazeGrid.Rows[x].Columns[y]

    Enjoy!

    Rama

    PS:

    I also wrote a function to clear the maze / destroy any actors.


    You have to pre-initialize the array to use the intuitive 2D array accessor method.
    Last edited by Rama; 10-18-2015, 01:35 AM.
    UE4 Marketplace: Melee Weapon Plugin & Compressed Binary Save System Plugin | Rama's C++ AI Jumping Videos | Vertex Snap Editor Plugin

    ♥ Rama

    Comment


      #17
      Originally posted by Rama View Post
      Code for Everyone!
      Enjoy!

      Rama
      O_O! This is very awesome and useful stuff. Thanks allot!

      I've been so reluctant to update everything to a proper format ie wiki + the new code i have; is that ever since 4.1 - The Navmesh seems to be broken in this type of usage.
      I cannot get any type of ai to work in this kind of environment. Thus I've been doing nothing but trying to get ai to work again with it.
      If anyone can get an ai working with spawned at runtime bp's maze let me know how you've done this. I've done it seemingly 101 different ways and none of them work anymore in 4.1)
      Last edited by SaxonRah; 05-07-2014, 12:04 PM.
      Youtube
      Machine Learning C++ Plugin
      Lindenmayer System C++ Plugin

      Comment


        #18
        Just chiming in to say that this is Cool™. I've done some random generated terrain and rooms way back when, and this is nostalgic.
        It is by will alone I set my code in motion.
        It is by coding that thoughts acquire speed, the hands acquire shaking, the shaking becomes a warning.
        It is by will alone I set my code in motion.

        Comment


          #19
          Originally posted by SaxonRah View Post
          O_O! This is very awesome and useful stuff. Thanks allot!

          I've been so reluctant to update everything to a proper format ie wiki + the new code i have; is that ever since 4.1 - The Navmesh seems to be broken in this type of usage.
          I cannot get any type of ai to work in this kind of environment. Thus I've been doing nothing but trying to get ai to work again with it.
          If anyone can get an ai working with spawned at runtime bp's maze let me know how you've done this. I've done it seemingly 101 different ways and none of them work anymore in 4.1)

          Dynamic Nav Meshes broken?!?!!?!

          you should post about this on the Answerhub!



          Rama
          UE4 Marketplace: Melee Weapon Plugin & Compressed Binary Save System Plugin | Rama's C++ AI Jumping Videos | Vertex Snap Editor Plugin

          ♥ Rama

          Comment


            #20
            Originally posted by Rama View Post
            Dynamic Nav Meshes broken?!?!!?!

            you should post about this on the Answerhub!



            Rama
            I thought I should ! I wasn't sure if this was just me being an idiot, But im pretty sure they are broken now. I'll make an Answerhub question asap.
            https://answers.unrealengine.com/que...sh-broken.html
            Last edited by SaxonRah; 05-15-2014, 11:40 AM. Reason: Added AnswerHub Link
            Youtube
            Machine Learning C++ Plugin
            Lindenmayer System C++ Plugin

            Comment


              #21
              This is really cool!

              I am currently working on a modular cave system (for the market place) and having this as a feature would be really awesome

              Comment


                #22
                Good news everybody, well kinda! The reason the ai hasnt been working was because of an overlooked Realtime NavMesh bug in the 4.1 release, hopefully it will be included in the 4.2 release.

                Thanks Luos! I'm working on some
                Cellular Automaton too, you might like that a bunch more for the cave system!
                Youtube
                Machine Learning C++ Plugin
                Lindenmayer System C++ Plugin

                Comment


                  #23
                  UE4 Closes after freezing

                  I cannot get the Maze to generate, when I enter the game ( using alt+p) it freezes, and after a few secs, UE4 closes abruptly.

                  I've been working on this like 2 hours ,and i think my error relies when i set the tileX and tileY,(but as I cannot debug it, i'm not really sure),

                  to generate the maze i do :

                  EventBeginPlay->GenerateMaze(with tileX = 23, and tileY = 23)

                  I hope someone knows how to fix this.

                  If you need more info about my error ,tell me.

                  Thanks in advance.

                  EDIT:

                  I've managed to debug it, UE4 crashes here:
                  Code:
                  AStaticMeshActor* BlockTile = SpawnBP<AStaticMeshActor>(GetWorld(), TileBlockBP, GenSpawnLoc, GenSpawnRot);
                  TileBlockBP has a correct pointer,(it isnt null)

                  EDIT2:

                  It looks like SpawnBP isnt even called, when I call SpawnBP UE4 toggles a breakpoint here:
                  Code:
                  int32 Error = GetLastError();
                  And the breakpoints at the SpawnBP dont get called.
                  I don't know why , please help
                  Help is appreciated.
                  Bolla.
                  Last edited by Bolla; 06-01-2014, 03:43 PM.

                  Comment


                    #24
                    Are you getting a "Cast to StaticMeshActor failed" error? I was encountering this when extending the maze generator from Actor. I never tried to extend from HUD since SaxonRah suggested extending from Actor.

                    I ended up changing all AStaticMeshActor from the example code to AActor. This resolved the error that I was encountering and the maze is randomly generated. I only made this change as one of the various troubleshooting attempts, so I don't have a reason to back up my decision.

                    Comment


                      #25
                      Hello SaxonRah,

                      Very nice work you have here. I scripted a couple of Dungeon Generators in unrealscript. I see you have Maze Solver as well. That leads me to the following questions. Do you have any thoughts on Cheat prevention/Anti-cheat mechanism for games generating mazes such as the those posted in the top thread?
                      HeadlessStudios.com - S P A G H E T T I: Ninja Swords, Guns & Monsters & Other S**t! - Join Our Discord

                      Comment


                        #26
                        Originally posted by Locke View Post
                        Are you getting a "Cast to StaticMeshActor failed" error? I was encountering this when extending the maze generator from Actor. I never tried to extend from HUD since SaxonRah suggested extending from Actor.

                        I ended up changing all AStaticMeshActor from the example code to AActor. This resolved the error that I was encountering and the maze is randomly generated. I only made this change as one of the various troubleshooting attempts, so I don't have a reason to back up my decision.




                        Thanks a lot !! it fixed it, but I've found a better way to fix it, just Reparent the blueprints to StaticMeshActor.

                        Now i have a maze generated, but it's filled with closed loops and impossible-to enter areas :

                        Click image for larger version

Name:	Maze.png
Views:	1
Size:	935.8 KB
ID:	1050999

                        (The starting tile is the bottom left one :S )
                        But I dont know why it happens :/

                        Comment


                          #27
                          Originally posted by Bolla View Post
                          Thanks a lot !! it fixed it, but I've found a better way to fix it, just Reparent the blueprints to StaticMeshActor.
                          I knew there was a better solution.

                          I ran into some issues of areas not being accessible when my mesh length and width sizes didn't match the float offset within the GenMaze function. I've got meshes that are approximately 500 length and width for all Tile pieces except TilePillarBP. The code had an offset of 400. I just changed the offset to 500 and made sure all of my meshes were the same size. I also changed the GenSpawnLoc X and Y values, under the Starting Tile Spawn, to offset instead of a static value.

                          If all of your Tile pieces are the same length and width, I'd verify that the code isn't missing any of the original logic. I haven't changed any of the original logic.

                          Comment


                            #28
                            Thanks for your answer Locke, but it stills doesnt work , (I am using the floor 400x400 template).

                            I'm going to use the depth search method, so i dont have those little 'spots' by using this method.
                            Attached Files

                            Comment


                              #29
                              Originally posted by Bolla View Post
                              Thanks for your answer Locke, but it stills doesnt work , (I am using the floor 400x400 template).

                              I'm going to use the depth search method, so i dont have those little 'spots' by using this method.
                              You did something to the logic of the generation code, the code that was written uses every other block in both x and y then chooses a single direction to build off of. There is 0 chance to get closed off areas since each wallblock has only 1 side ever built off of.



                              Originally posted by TechLord View Post
                              Hello SaxonRah,

                              Very nice work you have here. I scripted a couple of Dungeon Generators in unrealscript. I see you have Maze Solver as well. That leads me to the following questions. Do you have any thoughts on Cheat prevention/Anti-cheat mechanism for games generating mazes such as the those posted in the top thread?
                              Make everything you don't want someone to modify, replicated. So you force a local host that way its atleast way harder to cheat.
                              Last edited by SaxonRah; 06-06-2014, 02:33 AM.
                              Youtube
                              Machine Learning C++ Plugin
                              Lindenmayer System C++ Plugin

                              Comment


                                #30
                                Originally posted by SaxonRah View Post
                                Make everything you don't want someone to modify, replicated. So you force a local host that way its atleast way harder to cheat.
                                Just an idea here, but wouldn't you want to locally only have the area around the player, and the full map on the server? I'm not sure how that would work, but replicating specific tiles maybe?
                                It is by will alone I set my code in motion.
                                It is by coding that thoughts acquire speed, the hands acquire shaking, the shaking becomes a warning.
                                It is by will alone I set my code in motion.

                                Comment

                                Working...
                                X