CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2011
    Posts
    3

    Java 2d world generation algorithm. Please forgive my sense of humor...

    I know there is an easier way to do what I'm trying to do. The problem? I don't know what it is.

    So here's what I'm doing in a nutshell. I'm trying to create a 2d world generator, that performs in a predictable (as in stable) but still slightly random way.

    I've created a monster piece of code, that is very shaky at the very best. This beast of a program consists of six different classes which will be listed below, and it has several tricks, my favorite being an out of bounds error that seems to get worse the more I try to fix it, which is admittedly a failing on my part.

    that error occurs here:

    Code:
    			int startCell_X = generator.nextInt(worldMap.getHeight());
    			int startCell_Y = generator.nextInt(worldMap.getWidth());
    				
    			if ((startCell_X + newIsland.getHeight() < worldMap.getHeight() -1 ) && (startCell_Y + newIsland.getWidth() < worldMap.getWidth()-1)){
    				for (int i = 0; i < newIsland.islandCells.length; i++){
    					for (int j = 0; j < newIsland.islandCells[i].length; j++){
    						worldMap.mapCells[startCell_X + i ][startCell_Y + j].setTerrain(newIsland.islandCells[i][j].getTerrain());
    					}
    				}
    			}	
    			else{
    				continue;
    			}
    In any event, that lovely little plate of spaghetti is proudly served up by the IslandBuilder class within my program. This class is generally responsible for building. This class is the latest addition to the motley crew known as Mapper including five other miscreants known as...

    Mapper.java <-The main class of the program. Provides instructions to the others and is an all around boss.

    Map.java <-This class is responsible for describing exactly what the heck a map is...man this guy is like the wikipedia for maps....

    Cell.java <-This class describes the individual units that make up the map, including terrain type value etc.

    Island.java <-Describes an island to the IslandBuilder

    MapBuilder <-Responsible for constructing, maintaining and printing the world map.

    IslandBuilder.java <-Responsible for creating individual islands on the map.

    Anyway, I could really use some help. This whole monstrosity works together to intentionally frustrate the ever living hell out of me, mostly because I think I did it in a way that is not quite optimal.

    I have spent time on google looking for a solution, but haven't found anything similar enough to how I did it, to see what's causing me problems.

    I have attached my code so that you may see what I am dealing with or if you don't want to download strange code you may view them in pastebin

    I'm available to answer any questions, so please ask them... Thanks!

    Rooster
    Attached Files Attached Files

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Java 2d world generation algorithm. Please forgive my sense of humor...

    To start with you need to add a print out statement in your inner loop that prints out the values of all the participating variables. Then run the code and when it fails you will be able to see the particular value(s) that caused it to fail.

    Then, if the problem is not immediately obvious, add more print statements to the surrounding code to find what is causing the variables value to be too large/small.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Oct 2011
    Posts
    3

    Re: Java 2d world generation algorithm. Please forgive my sense of humor...

    Bump::Come on guys, I've made it this far all on my own...I'm just kinda stuck. Even if I'm doing it totally wrong, at least give me that pointer to jump off on...I don't want anyone to "do it for me"...I just need a pointer in the right direction.

  4. #4
    Join Date
    Oct 2011
    Posts
    3

    Re: Java 2d world generation algorithm. Please forgive my sense of humor...

    Oh ****....stupid firefox didn't refresh the page. Sorry for the negative post....I just hadn't seen the reply yet...

  5. #5
    Join Date
    Nov 2011
    Posts
    6

    Re: Java 2d world generation algorithm. Please forgive my sense of humor...

    Code:
    worldMap.mapCells[startCell_X + i ][startCell_Y + j].setTerrain(...
    Your mapCells array has dimensions 'width' and 'height', while you allocate values at indices spanning (anywhere from 0 to width/height) to (anywhere from 0 to width/height plus islandCells.length). Obviously, the sum might exceed its actual length.

    I don't know what exactly it is you're trying to do with the initial random offset, but you'll have to install a workaround that does not let it go past the actual length of mapCells.

  6. #6
    Join Date
    Mar 2014
    Posts
    6

    Re: Java 2d world generation algorithm. Please forgive my sense of humor...

    Quote Originally Posted by rooster5105 View Post
    I know there is an easier way to do what I'm trying to do. The problem? I don't know what it is.

    So here's what I'm doing in a nutshell. I'm trying to create a 2d barcode world generator, that performs in a predictable (as in stable) but still slightly random way.
    haha, I did not feel the sense of humor. By the way, you can look at this thread first.

    http://www.gamedev.net/topic/615682-...ld-generation/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured