Kod: Zaznacz cały
namespace IslandSurvival.Generators
{
class MapGen
{
// New Brush
mapBrush mBrush;
// Public map Fields
public int[,] mapTerrain;
public int randomRange;
// Private map Fields
Random rnd;
changeMatrix chMatrix;
public MapGen()
{
this.mapTerrain = new int[500, 500];
for (int x = 0; x < 500; x++)
{
for (int y = 0; y < 500; y++)
{
mapTerrain[x, y] = 0;
}
}
this.chMatrix = new changeMatrix();
this.randomRange = 10;
this.mBrush = new mapBrush();
this.rnd = new Random();
}
public void GenerateBase()
{
// Generate the Land[2] and Water[0-1] masses
// Single Brush Step
for (int i = 0; i < 5; i++)
{
if (this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] == 0)
{
if (rnd.Next(0, 8) != 0)
{
this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = 1;
mBrush.turn(Cnst.CONST_TURN_LEFT);
mBrush.move();
}
else
{
this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = 1;
mBrush.turn(Cnst.CONST_TURN_RIGHT);
mBrush.move();
}
}
else if (this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] == 1)
{
if (rnd.Next(0, 8) != 0)
{
this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = 2;
mBrush.turn(Cnst.CONST_TURN_RIGHT);
mBrush.move();
}
else
{
this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = 2;
mBrush.turn(Cnst.CONST_TURN_LEFT);
mBrush.move();
}
}
else if (this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] == 2)
{
if (rnd.Next(0, 8) != 0)
{
//this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = 0;
mBrush.turn(Cnst.CONST_TURN_LEFT);
mBrush.move();
}
else
{
//this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = 0;
mBrush.turn(Cnst.CONST_TURN_RIGHT);
mBrush.move();
}
}
// Send the brush to a random place!
if (rnd.Next(0, 50000) == 0)
{
this.mBrush.position.X = rnd.Next(100, 400);
this.mBrush.position.Y = rnd.Next(100, 400);
}
}
// ------ End of base generation
}
public void ClearMap()
{
for (int x = 0; x < 500; x++)
{
for (int y = 0; y < 500; y++)
{
mapTerrain[x, y] = 0;
}
}
}
public void doSmoothenBeaches()
{
// Set matrix values
for (int x = 0; x < 500; x++)
{
for (int y = 0; y < 500; y++)
{
if (GetNeighbourCount(0, x, y) > 0)
{
chMatrix.setValue(1, x, y);
}
}
}
// Check matrix and set changes to the map
for (int x = 0; x < 500; x++)
{
for (int y = 0; y < 500; y++)
{
if (chMatrix.values[x, y] == 1)
{
this.mapTerrain[x, y] = 1;
}
}
}
// Clear the matrix
chMatrix.clear();
}
public void doMoistBeaches()
{
// Set matrix values
for (int x = 0; x < 500; x++)
{
for (int y = 0; y < 500; y++)
{
if (GetNeighbourCount(0, x, y) > 1 && this.mapTerrain[x, y] != 0)
{
chMatrix.setValue(1, x, y);
}
}
}
// Check matrix and set changes to the map
for (int x = 0; x < 500; x++)
{
for (int y = 0; y < 500; y++)
{
if (chMatrix.values[x, y] == 1)
{
if (rnd.Next(0, 10) == 0)
this.mapTerrain[x, y] = 0;
else
this.mapTerrain[x, y] = 1;
}
}
}
// Clear the matrix
chMatrix.clear();
}
public void doRemoveSingles(int ID)
{
for (int i = 0; i < 10; i++)
{
// Set matrix values
for (int x = 0; x < 500; x++)
{
for (int y = 0; y < 500; y++)
{
if (GetSelfNeighbourCount(ID, x, y) < 2 && this.mapTerrain[x, y] == ID)
{
chMatrix.setValue(1, x, y);
}
}
}
// Check matrix and set changes to the map
for (int x = 0; x < 500; x++)
{
for (int y = 0; y < 500; y++)
{
if (chMatrix.values[x, y] == 1)
{
if (ID == 1)
this.mapTerrain[x, y] = 0;
if (ID == 0)
this.mapTerrain[x, y] = 2;
}
}
}
// Clear the matrix
chMatrix.clear();
}
}
public void GenerateForests()
{
// Generate the Forest [3] areas
// Single Brush Step
for (int i = 0; i < 5; i++)
{
if (this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] == Cnst.CONST_TILE_GRASS)
{
if (rnd.Next(0, 8) != 0)
{
this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = Cnst.CONST_TILE_FOREST;
mBrush.turn(Cnst.CONST_TURN_LEFT);
mBrush.move();
}
else
{
this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = Cnst.CONST_TILE_FOREST;
mBrush.turn(Cnst.CONST_TURN_RIGHT);
mBrush.move();
}
}
else if (this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] == Cnst.CONST_TILE_FOREST)
{
if (rnd.Next(0, 8) != 0)
{
//this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = 2;
mBrush.turn(Cnst.CONST_TURN_RIGHT);
mBrush.move();
}
else
{
//this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = 2;
mBrush.turn(Cnst.CONST_TURN_LEFT);
mBrush.move();
}
}
else // Send the brush to a random place!
{
this.mBrush.position.X = rnd.Next(100, 400);
this.mBrush.position.Y = rnd.Next(100, 400);
}
// Send the brush to a random place!
if (rnd.Next(0, 10000) == 0)
{
this.mBrush.position.X = rnd.Next(100, 400);
this.mBrush.position.Y = rnd.Next(100, 400);
}
}
// ------ End of base generation
}
public void GenerateMountains()
{
// Generate the Forest [3] areas
// Single Brush Step
for (int i = 0; i < 5; i++)
{
if (this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] == Cnst.CONST_TILE_GRASS)
{
if (rnd.Next(0, 8) != 0)
{
this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = Cnst.CONST_TILE_ROCK;
mBrush.turn(Cnst.CONST_TURN_LEFT);
mBrush.move();
}
else
{
this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = Cnst.CONST_TILE_ROCK;
mBrush.turn(Cnst.CONST_TURN_RIGHT);
mBrush.move();
}
}
else if (this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] == Cnst.CONST_TILE_ROCK)
{
if (rnd.Next(0, 8) != 0)
{
//this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = 2;
mBrush.turn(Cnst.CONST_TURN_RIGHT);
mBrush.move();
}
else
{
//this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = 2;
mBrush.turn(Cnst.CONST_TURN_LEFT);
mBrush.move();
}
}
else // Send the brush to a random place!
{
this.mBrush.position.X = rnd.Next(100, 400);
this.mBrush.position.Y = rnd.Next(100, 400);
}
// Send the brush to a random place!
if (rnd.Next(0, 80000) == 0)
{
this.mBrush.position.X = rnd.Next(100, 400);
this.mBrush.position.Y = rnd.Next(100, 400);
}
}
// ------ End of base generation
}
public void GenerateDeserts()
{
// Generate the Forest [3] areas
// Single Brush Step
for (int i = 0; i < 5; i++)
{
if (this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] == Cnst.CONST_TILE_GRASS)
{
if (rnd.Next(0, 8) != 0)
{
this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = Cnst.CONST_TILE_DESERT;
mBrush.turn(Cnst.CONST_TURN_LEFT);
mBrush.move();
}
else
{
this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = Cnst.CONST_TILE_DESERT;
mBrush.turn(Cnst.CONST_TURN_RIGHT);
mBrush.move();
}
}
else if (this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] == Cnst.CONST_TILE_DESERT)
{
if (rnd.Next(0, 8) != 0)
{
//this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = 2;
mBrush.turn(Cnst.CONST_TURN_RIGHT);
mBrush.move();
}
else
{
//this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = 2;
mBrush.turn(Cnst.CONST_TURN_LEFT);
mBrush.move();
}
}
else // Send the brush to a random place!
{
this.mBrush.position.X = rnd.Next(100, 400);
this.mBrush.position.Y = rnd.Next(100, 400);
}
// Send the brush to a random place!
if (rnd.Next(0, 80000) == 0)
{
this.mBrush.position.X = rnd.Next(100, 400);
this.mBrush.position.Y = rnd.Next(100, 400);
}
}
// ------ End of base generation
}
public void GenerateFreshWater()
{
// Generate the Fresh Water [5] areas
// Single Brush Step
for (int i = 0; i < 2; i++)
{
if (this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] == Cnst.CONST_TILE_FOREST)
{
if (rnd.Next(0, 8) != 0)
{
this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = Cnst.CONST_TILE_FRESH_WATER;
mBrush.turn(Cnst.CONST_TURN_LEFT);
mBrush.move();
}
else
{
this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = Cnst.CONST_TILE_FRESH_WATER;
mBrush.turn(Cnst.CONST_TURN_RIGHT);
mBrush.move();
}
}
else if (this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] == Cnst.CONST_TILE_FRESH_WATER)
{
if (rnd.Next(0, 8) != 0)
{
//this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = 2;
mBrush.turn(Cnst.CONST_TURN_RIGHT);
mBrush.move();
}
else
{
//this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = 2;
mBrush.turn(Cnst.CONST_TURN_LEFT);
mBrush.move();
}
}
else // Send the brush to a random place!
{
this.mBrush.position.X = rnd.Next(100, 400);
this.mBrush.position.Y = rnd.Next(100, 400);
}
// Send the brush to a random place!
if (rnd.Next(0, 3) == 0)
{
this.mBrush.position.X = rnd.Next(100, 400);
this.mBrush.position.Y = rnd.Next(100, 400);
}
}
// ------ End of base generation
}
public void GenerateDirt()
{
// Generate the Dirt [7] areas
// Single Brush Step
for (int i = 0; i < 2; i++)
{
if (this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] == Cnst.CONST_TILE_FOREST)
{
if (rnd.Next(0, 8) != 0)
{
this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = Cnst.CONST_TILE_DIRT;
mBrush.turn(Cnst.CONST_TURN_LEFT);
mBrush.move();
}
else
{
this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = Cnst.CONST_TILE_DIRT;
mBrush.turn(Cnst.CONST_TURN_RIGHT);
mBrush.move();
}
}
else if (this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] == Cnst.CONST_TILE_DIRT)
{
if (rnd.Next(0, 8) != 0)
{
//this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = 2;
mBrush.turn(Cnst.CONST_TURN_RIGHT);
mBrush.move();
}
else
{
//this.mapTerrain[(int)mBrush.position.X, (int)mBrush.position.Y] = 2;
mBrush.turn(Cnst.CONST_TURN_LEFT);
mBrush.move();
}
}
else // Send the brush to a random place!
{
this.mBrush.position.X = rnd.Next(100, 400);
this.mBrush.position.Y = rnd.Next(100, 400);
}
// Send the brush to a random place!
if (rnd.Next(0, 30) == 0)
{
this.mBrush.position.X = rnd.Next(100, 400);
this.mBrush.position.Y = rnd.Next(100, 400);
}
}
// ------ End of base generation
}
public void doEnlargeBeaches()
{
// Set matrix values
for (int x = 0; x < 500; x++)
{
for (int y = 0; y < 500; y++)
{
if ((GetNeighbourCount(1, x, y) > 2 && rnd.Next(0, 10) == 1) && (mapTerrain[x, y] == Cnst.CONST_TILE_GRASS || mapTerrain[x, y] == Cnst.CONST_TILE_FOREST))
{
chMatrix.setValue(1, x, y);
}
}
}
// Check matrix and set changes to the map
for (int x = 0; x < 500; x++)
{
for (int y = 0; y < 500; y++)
{
if (chMatrix.values[x, y] == 1)
{
this.mapTerrain[x, y] = 1;
}
}
}
// Clear the matrix
chMatrix.clear();
}
public int GetNeighbourCount(int NEIGHBOURID, int X, int Y)
{
int count = 0;
if (X > 1 && X < 499 && Y > 1 && Y < 499 && this.mapTerrain[X, Y] != NEIGHBOURID)
{
// Gora lewo
if (this.mapTerrain[X - 1, Y - 1] == NEIGHBOURID)
{
count++;
}
// Gora
if (this.mapTerrain[X, Y - 1] == NEIGHBOURID)
{
count++;
}
// Gora prawo
if (this.mapTerrain[X + 1, Y - 1] == NEIGHBOURID)
{
count++;
}
// Srodek lewo
if (this.mapTerrain[X - 1, Y] == NEIGHBOURID)
{
count++;
}
// Srodek prawo
if (this.mapTerrain[X + 1, Y] == NEIGHBOURID)
{
count++;
}
// Dol lewo
if (this.mapTerrain[X - 1, Y + 1] == NEIGHBOURID)
{
count++;
}
// Dol
if (this.mapTerrain[X, Y + 1] == NEIGHBOURID)
{
count++;
}
// Dol prawo
if (this.mapTerrain[X + 1, Y + 1] == NEIGHBOURID)
{
count++;
}
}
return count;
}
public int GetSelfNeighbourCount(int NEIGHBOURID, int X, int Y)
{
int count = 0;
if (X > 1 && X < 499 && Y > 1 && Y < 499)
{
// Gora lewo
if (this.mapTerrain[X - 1, Y - 1] == NEIGHBOURID)
{
count++;
}
// Gora
if (this.mapTerrain[X, Y - 1] == NEIGHBOURID)
{
count++;
}
// Gora prawo
if (this.mapTerrain[X + 1, Y - 1] == NEIGHBOURID)
{
count++;
}
// Srodek lewo
if (this.mapTerrain[X - 1, Y] == NEIGHBOURID)
{
count++;
}
// Srodek prawo
if (this.mapTerrain[X + 1, Y] == NEIGHBOURID)
{
count++;
}
// Dol lewo
if (this.mapTerrain[X - 1, Y + 1] == NEIGHBOURID)
{
count++;
}
// Dol
if (this.mapTerrain[X, Y + 1] == NEIGHBOURID)
{
count++;
}
// Dol prawo
if (this.mapTerrain[X + 1, Y + 1] == NEIGHBOURID)
{
count++;
}
}
return count;
}
}
class mapBrush
{
public int direction;
public Vector2 position;
Random rnd;
public mapBrush()
{
this.direction = Cnst.CONST_DIR_NONE;
this.position = new Vector2(255, 255);
this.rnd = new Random();
}
public void move()
{
switch (this.direction)
{
case Cnst.CONST_DIR_UP:
if (/*this.position.Y > 20 &&*/ Vector2.Distance(this.position, new Vector2(250, 250)) < 210)
this.position.Y--;
else
{
this.position.X = rnd.Next(100, 400);
this.position.Y = rnd.Next(100, 400);
}
break;
case Cnst.CONST_DIR_DOWN:
if (/*this.position.Y < 479 &&*/ Vector2.Distance(this.position, new Vector2(250, 250)) < 210)
this.position.Y++;
else
{
this.position.X = rnd.Next(100, 400);
this.position.Y = rnd.Next(100, 400);
}
break;
case Cnst.CONST_DIR_RIGHT:
if (/*this.position.X < 479 &&*/ Vector2.Distance(this.position, new Vector2(250, 250)) < 210)
this.position.X++;
else
{
this.position.X = rnd.Next(100, 400);
this.position.Y = rnd.Next(100, 400);
}
break;
case Cnst.CONST_DIR_LEFT:
if (/*this.position.X > 20 &&*/ Vector2.Distance(this.position, new Vector2(250, 250)) < 210)
this.position.X--;
else
{
this.position.X = rnd.Next(100, 400);
this.position.Y = rnd.Next(100, 400);
}
break;
default:
break;
}
}
public void turn(int TURN)
{
if (TURN == Cnst.CONST_TURN_LEFT)
{
switch (this.direction)
{
case Cnst.CONST_DIR_UP:
this.direction = Cnst.CONST_DIR_LEFT;
break;
case Cnst.CONST_DIR_RIGHT:
this.direction = Cnst.CONST_DIR_UP;
break;
case Cnst.CONST_DIR_DOWN:
this.direction = Cnst.CONST_DIR_RIGHT;
break;
case Cnst.CONST_DIR_LEFT:
this.direction = Cnst.CONST_DIR_DOWN;
break;
default:
this.direction = Cnst.CONST_DIR_UP;
break;
}
}
else if (TURN == Cnst.CONST_TURN_RIGHT)
{
switch (this.direction)
{
case Cnst.CONST_DIR_UP:
this.direction = Cnst.CONST_DIR_RIGHT;
break;
case Cnst.CONST_DIR_RIGHT:
this.direction = Cnst.CONST_DIR_DOWN;
break;
case Cnst.CONST_DIR_DOWN:
this.direction = Cnst.CONST_DIR_LEFT;
break;
case Cnst.CONST_DIR_LEFT:
this.direction = Cnst.CONST_DIR_UP;
break;
default:
this.direction = Cnst.CONST_DIR_UP;
break;
}
}
}
}
class changeMatrix
{
public int[,] values;
public changeMatrix()
{
this.values = new int[500, 500];
for (int x = 0; x < 500; x++)
{
for (int y = 0; y < 500; y++)
{
values[x, y] = 0;
}
}
}
public void setValue(int VAL, int X, int Y)
{
this.values[X, Y] = VAL;
}
public void clear()
{
for (int x = 0; x < 500; x++)
{
for (int y = 0; y < 500; y++)
{
this.values[x, y] = 0;
}
}
}
}
}