Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Code Project,

I have been facing a problem for the last three days. I have been trying to fix it myself no luck. Basically, I am creating a flat grass map by only generating air unless the (Y cooardinate) is below half of the size of the view(ChunkSize).

Here is the code:

// -- Generating the Landscape.
C#
// -- Create the Chunk.
/// <summary>
/// Creates the Chunk at the Cooardinate and returns the string..
/// </summary>
/// <param name="?"></param>
private string CreateChunk(int ChunkX, int ChunkY)
{

    // -- Return String.
    string _ReturnString = "";

    // -- Create a stream writer.
    StreamWriter _Writer = new StreamWriter(Path + ChunkX + "_" + ChunkY + ".txt");

    // -- Initialize.
    for (int i = 0; i < World.ChunkSize; i++)
        for (int j = 0; j < World.ChunkSize; j++)
            _ReturnString += (char)BlockID.BLOCK_GRASS ;

    // -- Write the world bytes.
    for (int i = 0; i < ChunkSize; i++)
    {
        for (int j = 0; j < ChunkSize; j++)
        {
            // -- Block.
            char Block;
            if (ChunkY > 0 || (ChunkY == 0 && j > ChunkSize / 2))
            {
                Block = (char)BlockID.BLOCK_GRASS;
            }
            else
                Block = (char)BlockID.BLOCK_AIR;

            // -- Add Return.
            _ReturnString = ReplaceAtIndex(i * j, Block, _ReturnString);


        }
    }

    // -- Write.
    _Writer.Write(_ReturnString);

    // -- Close the Writer.
    _Writer.Flush();
    _Writer.Close();

    // -- Return.
    return _ReturnString;
}


// -- Get a block and also join a string.

C#
// -- Change a character.
/// <summary>
/// Changes a specific character in a string.
/// </summary>
/// <param name="i"></param>
/// <param name="value"></param>
/// <param name="word"></param>
/// <returns></returns>
static string ReplaceAtIndex(int i, char value, string word)
{
    // -- Create an array of characters out of the string.
    char[] _Characters = word.ToCharArray();

    // -- Change the character
    _Characters[i] = value;

    // -- Return.
    return string.Join("", _Characters);
}


// -- Get a block.
/// <summary>
/// Returns the block given from the arguments. Note: Also returns air if out of bounds.
/// </summary>
/// <param name="BlockX">X for the block.</param>
/// <param name="BlockY">Y for the block.</param>
/// <param name="Chunk">The chunk to check in.</param>
/// <returns></returns>
public static char GetBlock(int BlockX, int BlockY, string Chunk)
{
    // -- Block.
    char _Block = (char)BlockID.BLOCK_AIR;


    // -- Get the block.
    _Block = Chunk[BlockX * BlockY];

    // -- Return.
    return _Block;
}



// -- And most importantly the drawing.
C#
// -- Draw World.
        /// <summary>
        /// Draws the World on the Graphics given as parameter.
        /// </summary>
        /// <param name="g"></param>
        public void DrawWorld()
        {
            // -- Loop through the chunkX.
            for (int i = 0; i < World.ChunkSize; i++)
            {
                // -- Loop through the chunkY.
                for (int j = 0; j < World.ChunkSize; j++)
                {
                    // -- Get the Block.
                    char Block = (char)World.GetBlock(i, j, ThePlayer.LoadedChunks[5]);

                    // -- Color.
                    Color toDraw = Color.Transparent;

                    // -- Get the Right Color.
                    switch (Block)
                    {
                        case (char)BlockID.BLOCK_AIR:
                            break;

                        case (char)BlockID.BLOCK_DIRT:
                            toDraw = Color.Brown;
                            break;
                        case (char)BlockID.BLOCK_GRASS:
                            toDraw = Color.Green;
                            break;
                    }
                    

                    // -- Draw the Block.
                    int Size = BlockID.BlockSize;
                    int X = i == 0 ? 0 : i * Size;
                    int Y = j == 0 ? 0 : j * Size;
                    Rectangle _Rec = new Rectangle(X, Y, BlockID.BlockSize, BlockID.BlockSize);
                    spriteBatch.Draw(ImageBlock, _Rec, toDraw);

                }
            }
        }


And if you are wondering what the result is:
http://i46.tinypic.com/de0daq.jpg[^]

Edit: For people who do not understand the problem. The problem is that all the green blocks(ID.GrasS) should be under the Height / 2 of the room and now they are all over the place, but I do not understand how I am reading this out wrong ?.

Please help ! :(
Posted
Updated 24-Dec-12 7:19am
v2
Comments
Sergey Alexandrovich Kryukov 24-Dec-12 12:38pm    
Help with what? Is there a question here? A problem?
—SA
Yvar Birx 24-Dec-12 12:51pm    
Yes, I am generating 'flat grass' and the blocks should be below half of the room, although they are spazzing all over the Room. So the problem is 'reading' out the blocks. THAT is the problem.
austinbox 24-Dec-12 15:02pm    
Seems a bit fishy that you have two loops with the same variable names, wouldn't that alone givew you an error?
Yvar Birx 24-Dec-12 15:07pm    
No, the thing is, they are even sizes. The Chunk has to be 20 by 20. And I am not going to make two variables valueing 20. :P
Yvar Birx 24-Dec-12 15:13pm    
What do you exactly mean ? (Thanks for helping me out btw)

The first two for loops make sure the string has the length it needs to have, otherwise I can't change the character.

The next two loops Actually generate the landscape.

1 solution

I think your code could be simplified a little. I'm guessting that I was the x-axis and J was the y-axis

C#
for (int i = 0; i < World.ChunkSize * World.ChunkSize; i++)
     _ReturnString += (char)BlockID.BLOCK_AIR;
 
// -- Write the world bytes.
for (int y = 0; y < ChunkSize; y++)
{
     for (int x = 0; x < ChunkSize; x++)
     {
          // -- Block.
         char Block = (char)BlockID.BLOCK_AIR;
         if (y < ChunkSize / 2)
              Block = (char)BlockID.BLOCK_GRASS;
         // -- Add Return.
         _ReturnString = ReplaceAtIndex(x * y, Block, _ReturnString);
     }
}


Try that.
 
Share this answer
 
v2
Comments
Yvar Birx 24-Dec-12 15:22pm    
This, made it a lot more compact and easy thanks, unfortunatly, reading it out still seems to be messed up, am I using the right calculation(I am using the right chunk don't worry about that).

Pic:

http://tinypic.com/r/xm99ah/6
austinbox 24-Dec-12 15:26pm    
It's got to be your reading, I'm working on it...

EDIT: I wouldn't suggest storing data in a one-dimensional array.
Loading is alot easier swhen you use a multi-dimensional array.
I would suggest redoing all of your drawing/loading with this pseudocode.

Block[,] blocks = new Block[width, height];
for (int x = 0; x < blocks.GetLength(0); x++)
{
for (int y = 0; y < blocks.GetLength(1); y++)
{//Add filling code here ex. blocks[x, y] = BLOCKID
}
}

//And to get a blockid
int blockid = blocks[x, y];
Yvar Birx 24-Dec-12 15:34pm    
Well, that is the problem. I have 9 chunks loaded in, whenever the player goes into another chunk the chunks are reloaded. This is ThePlayer.LoadedChunks. It's an array of strings, and I can't make an array out of an array. :L
Yvar Birx 24-Dec-12 15:35pm    
I could transform the string into an array though, the chunk the player is in, to draw. ?
Yvar Birx 24-Dec-12 15:36pm    
I also store those chunk strings in a file. ;l

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900