I'm not sure if this should really go in game programming, general programming or whatever, so forgive me, but seeing as I am doing this in C#, might as well post it here.
I'm currently writing a text based game (just tidied up and put on windows forms) and I'm having a mental block on trying to write the game map portion. The map is (or part of) a galaxy, with stars based on a 25x21 grid. I can currently create the placement, randomly, of the stars. The next step is to basically 'link' the stars with hyperspace links. A possible example on a 3x3 grid is here:
Code:
100Y--101B--102C
oo \/ooo \/ o
| /\ | /\ |
103H--104S--105G
| \/oooo\/ |
| /\ | /\ |
106R--107Y--108p
The number itself is just a system code, the letter is just it's type (Y= Yellow Dwarf). The lines are fairly explanatory. These are the 'links' between systems via hyperspace. The example above only shows what is possible. Not all the systems shown could really be there (or even in that number order, lots of lovely but controlled randomness). The hard part is deciding whether to have a link between two existing systems, and then how to show it easily. It's not even one grid space length only, it could be up to three (horizontal or vertical only though).
The following is the part of placing the stars:
Code:
void PlaceSystems()
{
Random randomNumber = new Random();
for (int y = 0; y < 21; y++)
{
for (int x = 0; x < 25; x++)
{
int max = 70;
List<int> vert1 = new List<int>();
vert1.AddRange(new int[] { 0, 4, 5, 9, 10, 14, 15, 19, 20, 24 });
List<int> Hori1 = new List<int>();
Hori1.AddRange(new int[] { 0, 6, 7, 13, 20 });
if (Hori1.Contains(y))
{
max -= 15;
}
if (vert1.Contains(x))
{
max -= 15;
}
int xminus2 = x - 2 < 0 ? 0 : x - 2;
int xminus1 = x - 1 < 0 ? 0 : x - 1;
int yminus2 = y - 2 < 0 ? 0 : y - 2;
int yminus1 = y - 1 < 0 ? 0 : y - 1;
int xplus2 = x + 2 > 24 ? 24 : x + 2;
int xplus1 = x + 1 > 24 ? 24 : x + 1;
int yplus2 = y + 2 > 20 ? 20 : y + 2;
int yplus1 = y + 1 > 20 ? 20 : y + 1;
if ((this[xminus2, y].Exists && this[xminus1, y].Exists) || this[x, yminus2].Exists && this[x, yminus1].Exists)
{
max -= 10;
}
if ((this[xminus1, yminus1].Exists && this[xminus2, yminus2].Exists) || (this[xplus1, yminus1].Exists && this[xplus2, yminus2].Exists))
{
max -= 10;
}
if (randomNumber.Next(0, max) > 35)
{
StarTypes newStarType = StarTypes.Error;
int systemNumber = GenerateSystemNumber();
SystemStar newSystem = new SystemStar(systemNumber, newStarType.GenerateStarTypeFromNumber(randomNumber.Next(1, 26)));
SystemsList.Add(newSystem);
this[x, y] = new MapBase(systemNumber, x, y);
}
}
}
}
That is contained within the map class itself and is the first stage of generating the map.
Next it looks at where links can be possibly placed...
Code:
void FindLinkableSystems()
{
for (int y = 0; y < 21; y++)
{
for (int x = 0; x < 25; x++)
{
if (this[x, y].Exists)
{
bool foundRight = false;
bool foundLeft = false;
bool foundUp = false;
bool foundDown = false;
for (int i = 1; i < 4; i++)
{
if (!foundRight)
{
if (i + x > 24)
{
break;
}
if (this[x + i, y].Exists)
{
this[x, y].LinkableSystems.Add(new LinkDetails(this[x + i, y].StarNumber, Direction.Right, i));
foundRight = true;
}
}
}
for (int i = 1; i < 4; i++)
{
if (!foundLeft)
{
if (x - i < 0)
{
break;
}
if (this[x - i, y].Exists)
{
this[x, y].LinkableSystems.Add(new LinkDetails(this[x - i, y].StarNumber, Direction.Left, i));
foundLeft = true;
}
}
}
for (int i = 1; i < 4; i++)
{
if (!foundDown)
{
if (y + i > 20)
{
break;
}
if (this[x, y + i].Exists)
{
this[x, y].LinkableSystems.Add(new LinkDetails(this[x, y + i].StarNumber, Direction.Down, i));
foundDown = true;
}
}
}
for (int i = 1; i < 4; i++)
{
if (!foundUp)
{
if (y - i < 0)
{
break;
}
if (this[x, y - i].Exists)
{
this[x, y].LinkableSystems.Add(new LinkDetails(this[x, y - i].StarNumber, Direction.Up, i));
foundUp = true;
}
}
}
if (!(y - 1 < 0 || x - 1 < 0) && this[x - 1, y - 1].Exists)
{
this[x, y].LinkableSystems.Add(new LinkDetails(this[x - 1, y - 1].StarNumber, Direction.UpLeft, 1));
}
if (!(y + 1 > 20 || x + 1 > 24) && this[x + 1, y + 1].Exists)
{
this[x, y].LinkableSystems.Add(new LinkDetails(this[x + 1, y + 1].StarNumber, Direction.DownRight, 1));
}
if (!(y + 1 > 20 || x - 1 < 0) && this[x - 1, y + 1].Exists)
{
this[x, y].LinkableSystems.Add(new LinkDetails(this[x - 1, y + 1].StarNumber, Direction.DownLeft, 1));
}
if (!(y - 1 < 0 || x + 1 > 24) && this[x + 1, y - 1].Exists)
{
this[x, y].LinkableSystems.Add(new LinkDetails(this[x + 1, y - 1].StarNumber, Direction.UpRight, 1));
}
}
}
}
}
Yeah yeah, I could probably massively shorten that one...!
Anyways, the next step actually creates the links, but it's a little hard to actually verify it works truly correctly, which is later...
Code:
void CreateLinks()
{
for (int y = 0; y < 21; y++)
{
for (int x = 0; x < 25; x++)
{
if (this[x, y].Exists)
{
int LinkableCount = this[x, y].LinkableSystems.Count;
int subSystemsCount = SystemsList[this[x, y].StarNumber].AdjacentSystems.Count;
int availableLinksAmount = 7 - subSystemsCount;
if (LinkableCount == 1)
{
int currentSystemNumber = this[x, y].StarNumber;
int linkableSystemNumber = this[x, y].LinkableSystems[0].StarNumber;
SystemsList[currentSystemNumber].AdjacentSystems.Add(linkableSystemNumber);
SystemsList[linkableSystemNumber].AdjacentSystems.Add(currentSystemNumber);
switch (this[x, y].LinkableSystems[0].Direction)
{
case Direction.Up:
this[x, y].Uplink = true;
this[linkableSystemNumber].Downlink = true;
break;
case Direction.Down:
this[x, y].Downlink = true;
this[linkableSystemNumber].Uplink = true;
break;
case Direction.Left:
this[x, y].LeftLink = true;
this[linkableSystemNumber].Rightlink = true;
break;
case Direction.Right:
this[x, y].Rightlink = true;
this[linkableSystemNumber].LeftLink = true;
break;
case Direction.UpRight:
this[x, y].UpRightLink = true;
this[linkableSystemNumber].DownLeftLink = true;
break;
case Direction.DownRight:
this[x, y].DownRightLink = true;
this[linkableSystemNumber].UpLeftLink = true;
break;
case Direction.DownLeft:
this[x, y].DownLeftLink = true;
this[linkableSystemNumber].UpRightLink = true;
break;
case Direction.UpLeft:
this[x, y].UpLeftLink = true;
this[linkableSystemNumber].DownRightLink = true;
break;
default:
break;
}
}
int hold = 0;
}
}
}
}
Sorry for breaking the forums. Anyways, that appears to work (I don't get errors anymore and have since removed the try/catch stuff I had to place in. There are still a few holes so to speak, such as I should also be assigning the destination system in that last switch statement for the link to be placed as true in the opposite direction for the destination system back to the system it's doing at the moment. That's lots of little if's etc making sure I don't error out of range.
The hard part is actually drawing this out to a text file or control. You see, there are so many conditions it has to operate under that I just cannot think of how to do this! You see, in a system grid square, if there's not a system there it could be passed through by a hyperlink from a system one side to another the other side. It could even have links crossing over it!
It's not so much the actual code, but help on actually designing how I should go about this. If you're confused, feel free to ask more.
Thanks for your time and I hope you didn't fall asleep!