Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by esitoinatteso · Sep 20, 2014 at 03:46 PM · c#proceduralgenerationhow

Drunkard Walk Algorithm C# Question

Hi dear Unity's community! I've been away for a lot, but I spent that time trying and studying by myself the mysterious world of coding, lurking here and there, reading your kind answers to questions already asked that were useful to fulfill my needs!

I'm now here once more to subject to you my last achievement in this field, trusting in your unselfish dedication to these subjects and hoping to hear helpful advices. Before I continue, I think I should justify my decision to talk about this by opening a question... well, I wanted to make known the method I used to help other amateurs in their own quests for knowledge, but I also wanted to make clear that what follows is by no means finished or " correct". Thus I preferred unityAnswers to the Forums.

So, I wanted to use Procedural Generation of Contents. I learned about Drunkard Walk which is basically what I thought to do by myself, except that somebody else already thought about it. What follows is my own personal attempt to make such an algorithm. So far it doesn't stop by itself and is unable to " think" too much... it " just" build up a map of squares. Squares, yes, because I've attached this script to a plane with a box collider in my editor and I've got another script attached to a GameMaster that instantiate the first square from which to start walking.

Here it is, in all his unholy ignorance!

e's ROUGH Drunkard Square Generator

using UnityEngine; using System.Collections; using System.Collections.Generic;//List Enabled

 public class DrunkardSquareGenerator : MonoBehaviour {
 
     // This class will handle the algorithms for my Drunkard Walk Procedural Generated Content
     /*
      * I originally thought to store all the functions here, but then I tried to separate them in other scripts.
      * I'm going to put them back here once more... I kinda miss some basics in software architecture.
      * Problem is, I thought to use a List to handle all this stuff, instead of a sequence of IF statements, BUT
      * I wasn't capable of doing it because I lack the competence.
      * 
      * There will be 3 different algorithms and all of them will be capable of avoid overlap via a specific method.
      * Maybe...
      */
 
     private int chosenAlgo; // it's a random int to know which algorithm must be picked.
 
     //Algorithms' Stuff
     private List <Vector3> PosList = new List<Vector3>(); // here we will store the coordinates to choose from
     private int iPosList; // index for PosList.
     private int sPosList; // sieze of PosList.
     private Transform myTransform; // reference to this transform.
     private Vector3 posChosen; // reference to the position chosen
     private int shouldI; // int to decide if it should try to instantiate or skip
 
     private Vector3 north;//items of PosLists
     private Vector3 south;
     private Vector3 east;
     private Vector3 west;
 
     // I'll set up some things in here
     void Awake () {
 
         //set the random int to choose
         chosenAlgo = 1; // FOR TEST SAKE
         //chosenAlgo = Random.Range(1,3);
         Debug.Log ("Chosen Algo is:" + chosenAlgo);
 
         //get reference
         myTransform = gameObject.transform;
         north = new Vector3(myTransform.position.x+10.0f, 0, myTransform.position.z);
         south = new Vector3(myTransform.position.x-10.0f, 0, myTransform.position.z);
         east = new Vector3(myTransform.position.x, 0, myTransform.position.z+10.0f);
         west = new Vector3(myTransform.position.x, 0, myTransform.position.z-10.0f);
 
         //populate list
         PosList.Add(north);
         PosList.Add(south);
         PosList.Add(east);
         PosList.Add(west);
 
         sPosList = PosList.Count;
 
     }
 
     // Use this for initialization
     void Start () {
         //When the square is spawned, it will decide what to do.
         //I know this thing down here sucks but hey! at least it's working!
 
         if(chosenAlgo == 1){
             CdrunkardWalk();
         }
         if(chosenAlgo == 2){
             SdrunkardWalk();
         }
         if(chosenAlgo == 3){
             BdrunkardWalk();
         }
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     //Classic Drunkard Walk
     void CdrunkardWalk(){
         //Debug.Log ("You got to Classic"); just to be sure...
 
         //Now we have to build the algorithm... SO, for every item in List
         for(int i = 0; i<sPosList; i++){
 
             //Ask yourself if you should try to spawn
             shouldI = Random.Range(0,2);//this will return 0 to 1.9999... and since it's an int it works fine
 
             //If you said yes...
             if(shouldI == 0){
                 //Debug.Log("Tried to spawn for:" +PosList[i]);
                 //...ask yourself if you CAN spawn
                 if(Physics.CheckSphere(PosList[i], 5.0f) == false){
                     GameObject clone;
                     clone = Instantiate(this.gameObject,PosList[i],myTransform.localRotation) as GameObject;
                 }
             }
             //If you said no...
             if(shouldI == 1){
                 //Debug.Log("Not spawned for:" +PosList[i]);
             }
         }
 
     }
 
     //Straight Drunkard
     void SdrunkardWalk(){
         //Debug.Log ("You got to Straight");
     }
 
     //Backtrack Drunkard
     void BdrunkardWalk(){
         //Debug.Log ("You got to Backtrack");
     }
 }


Now, as you can see this code is reeeaaally rough... all those if statements should vanish. I was trying to but I got stuck. If you have any suggestions ( like, patient full explanations) I'd really appreciate :D

The other 2 methods that I left in blank are simple variations to the algorithm, I still have to build them. One will go back to the starting square and try to walk again, if it can... just to fix those runs in which at the very beginning one or more directions are discarded. The other will try to store in a variable the previous position chosen ( ex: north), and tweak the Positions' List so that it has more items equal to that one ( meaning it becomes a shuffle bag... i believe that's its name).

I already know I'll have problems with the other two methods but that's the fun of it, or is it not? Again, suggestions of links and/or ideas are appreciated.

Last, but not least, I'll have to make it stop before everything crashes. I was thinking about a variable that adds +1 every time something is spawned, and another one to store the total amount of squares allowed.

I hope this code can help others understand how to build these kind of algorithms from scratches, feel free to text me and thanks in advance for your attention! - e

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

By Code generated Mesh (problems with visibility) 4 Answers

Procedural terrain generation ? 0 Answers

Help in understanding Mesh Generation 1 Answer

Huge C# Procedural Generation Errors. Why? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges