Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411275 Posts in 69323 Topics- by 58380 Members - Latest Member: bob1029

March 28, 2024, 08:35:10 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)C# Grid Based Map question
Pages: [1]
Print
Author Topic: C# Grid Based Map question  (Read 742 times)
schwarzenego
Level 0
*


View Profile
« on: May 17, 2018, 01:14:57 PM »

Hey,

So, I am developing a strategy game which has nothing to do with clash of clans but I will use that to express my question. I have a grid of nodes, described by a list of nodes. Nodes have references to their neighbors and I've already have systems in there like basic pathfinding, road building and so on. However, I just started creating the system to build stuff, the caveat being that buildings can be larger than 1 tile (unlike roads which connect with other nearby roads but are just 1 tile in size).

The question is... what is the best way to do that? I mean, what I am doing right now is, choosing the X tiles the building will occupy (say 9 tiles), setting them all to have a reference to this building, and instantiating the building prefab in the center tile. This works, and seems like a good enough solution, but I am serializing the most barebones I can of this so I can recreate the world from a pre-set state (saving a json), and idk, but it feels I might be doing something wrong here. This is making it hard for me for stuff like rotating the building (rotating is easy, but the building has entrance and exit points and rotating those among the 9 selected tiles is going to prove not trivial), and idk, the linking between the nodes is... weak, is index based... so say this 9 tile building, at time of construction it will store a tile index in each tile, so if I click on tile 9, I have a way to calculate what are the other 8 tiles... but idk.

Anyone knows some good resources on ways to approach this problem? This has been done thousands of time already in the history of gaming but I couldnt find anything about this specific question... so any references would be super helpful!

As a ps, I do have some years making games so Im not the noobest there is, this is actually a deterministic multiplayer game and I got that going so I feel like I am struggling with a part that should be easy haha but is proving a bit of harder than expected.

Anyway, thanks for the attention!
Best,
Allan
Logged
BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #1 on: May 18, 2018, 12:50:24 AM »

This is a regalar square grid of tiles? You are so far only treating a grid as a graph of nodes, which is good for path finding etc but not helpful for other things. For a lot of the things you are describing, using x-y co-ordinates is going to work a lot better. Finding the set of tiles covered by a rectangle, or doing rotationss are both very easy in x-y co-ords, you shouldn't have any trouble.

To be more specific, a simple scheme would be to keep a list of buildings. Each building defines a rect xmin,ymin,xmax,ymax and a rotation (from 0 - 3). On your nodes (tiles), all you note is which building is occupying the tile. You need to do anything with the building, you look up the single building object from the node, and then work with that.
Logged
schwarzenego
Level 0
*


View Profile
« Reply #2 on: May 18, 2018, 04:16:37 AM »

Yeah you are right. I was getting too attached to the tiles themselves to hold data then it dawned on me that there is data that should exist outside of the tiles... as you said, an array of structures and just have the correct tiles pointing to them...

I am already working with x-y coordinates, I got the tiles referenced in a dictionary (and in a list) by coordinates, and I already have the placeholder interface to show if the tiles are occupied or not... but I wasnt separating the data structures as I should, it seems.

Seems like the way to go is to facilitate the communication between different data structures with whatever references that might be needed... so it is easy to click on a tile, and select a building for example.

About the rotations... what concerns me the most right now is the entrance/exit tiles... not sure how to go about that... I would need to rotate "relative" positions... say I have a building at 10 10, with its entrance tile at the RELATIVE position 2, -3... if I rotate the building 90 degrees I want that to become... 3, 2, then -2, 3, then -3, -2... but not sure on how to go about doing that... will give it some thought though, as I havent tried anything yet.

Thanks for the help though!
« Last Edit: May 18, 2018, 04:27:31 AM by schwarzenego » Logged
Ordnas
Level 10
*****



View Profile WWW
« Reply #3 on: May 19, 2018, 12:56:29 AM »

I think you should apply a spatial partitioning:
http://gameprogrammingpatterns.com/spatial-partition.html

Basically you can query the tile with a Map, where the key is the TileIndex, and the value is a linkedlist of entities (for simplicity only the building).
The building is a struct where you store NameID, an array of TileIndex, the rotation as an enum, and an array of TileEntranceIndex.
When you save the json, you can serialize only the buildings TileIndex and rotation built by the player.
To update the TileIndex when the building is rotated, it should be easier if you rotate the coordinate origin instead to think in terms of rotate the building. So the logic of set the building on the grid remain the same, and you just need to rotate the map.
Logged

Games:

BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #4 on: May 20, 2018, 12:38:29 PM »

About the rotations... what concerns me the most right now is the entrance/exit tiles... not sure how to go about that... I would need to rotate "relative" positions... say I have a building at 10 10, with its entrance tile at the RELATIVE position 2, -3... if I rotate the building 90 degrees I want that to become... 3, 2, then -2, 3, then -3, -2... but not sure on how to go about doing that... will give it some thought though, as I havent tried anything yet.
You basically described exactly what needs doing. There are only 4 cases, and you listed them all. Perhaps give it a bit of thought before asking others for help next time?
Logged
schwarzenego
Level 0
*


View Profile
« Reply #5 on: May 21, 2018, 07:01:31 AM »

I know the results I want to get, I didn't know the formula to get to those results, but sure, that was simple enough to find in the web once I read the answers to my question here and in other forums that made me think about it differently then what I was thinking before.

Thanks Ordnas, I went ahead and created a list of buildings so to speak, to which the tiles reference, so when a tile is clicked, it will direct to the building in the list, instead of some ugly hack I was trying to do of having the building tightly related to the tiles it occupied. I managed to serialize just the barebones this way as a new serialized array, which I just use to rebuild the industries at their specific positions when loading a new map, as if I was "planting" the buildings all over again but keeping whatever metadata I need for those structures the same as was serialized.

Thanks for the help, was able to make some good progress already!
Best,
Allan
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic