So to make up some time wasted leading up to Christmas I have worked extra hard on making sure a basic resource system was working before this blog post, along with a few known bug fixes that have needed to be added.
The basic idea of the resource nodes is to allow the player to gather resources while playing the game. They are gathered by having a worker unit near them, and allowing them to return to their base with the resource. (My actual plan has been changed to the node system rather than having buildings just do it for you as it brings a little more for the player to do to the game, and isn't too difficult to do.) Spheres in the ground are currently acting as resource nodes until much later down the line alternatives can be found. They are currently set to spawn in a random location that is walkable (not already taken up by a building or unit). As the resources need to be returned to a town centre, one of these buildings are now set to spawn at the start like it should do with a unit next to it in the correct position (more later).
For now a basic system has been made, where a worker unit can be selected and told to go to a resource node. Once there they will start collecting that resource. Then when they reach a limit they will return to the town centre, deposit that resource and go back to that resource node. When the node runs out of availiable resource it will be deleted. To know the unit cannot return, when it drops off the resource it checks to see the resource still exists and if it has above 0 amount.
A big part of the week has been more focused on bug fixing so that this resource system can work smoothly. The main and biggest bug was having units go to the cloeset point to a building, and NOT stacking ontop of other worker units. A lot of time was spent on a solution that ultimately had to be scraped and be rethought of as it was causing errors when it came to calculating free spaces. As I had the closest point calculated on the side the unit was on by checking the two closest corners, then checking every node between those corners. This worked well, the problem occured when it got to calculating up the sides and far side of the building from the unit. After many errors slowly being fixed but no clear solution in sight, I realised a far simpler way around this.
Rather than using some fairly complicated algorithms at the time of sending a unit, it is far easier to have all the possible locations around the building calculated by simply adding all the surrounding nodes to an array. This is done by getting the bottom left corner of the building, minusing the nide diameter size to end out at the first availiable node. Then by going along both x and z axis until arriving at the top right corner node. Once all of these had been added it was easy just to do a distance check against each nodes world position and find the closest location. Then when all locations are taken a unit simply won't go to that building and remain idle. This currently works when sending one unit at a time, as I am not yet sure how formation is going to work and how it affects sending units to one building. A backup idea will be to queue sending all selected units, so there is a gap so it can detect what spaces are actually availiable, as currently all units are trying to go to the same place, and once one is sent it is unwalkable breaking the remaining units.
A big change was made to my whole worker script attached to worker units to support the use of enums acting as states rather than just bool variables. This has helped simplify my code more making it far easier to follow which solves a small issue when I came back to look at it a couple weeks ago after a short while. I also added a sub state section, as the way I had written the code before I had a lot of bools that defined what was heppening, like MovingToBuilding. This still comes under the Building state so was given a substate. Without these substates it actually breaks some of my code as they are useful for checking when a worker unit is actually in position next to a building or resource.
There are a couple of bugs both remaining and now popping up from this new way of doing as expected. The first is with the closest location of the resources nodes. Each resource node only takes up one node on the A* grid, meaning the surrounding nodes should be directly touching it, however when a unit is sent to it, they are leaving one additional node gap. Another small bug is the closest location doesn't take into account it's current position. So if a node is the perfect distance away from the town centre, meaning the unit logically would have to do no travelling, it will still move to a neighbour node as it doesn't check it's current position.
One final thing which isn't a bug that needs to be added, is for a worker when they are still gathering but their node has round out of resources they need to find the closest node of the same resource type. This along with some bug fixes should be done over the next week or so. With some additional time I may work on updating more UI stuff to display information to the player such as the resource amount remaining on a resource node, how much a worker is holding, and what state a worker is in. But more importantly I am going to start thinking about combat.