Octrees

It was finally time to add some more efficient ways of finding ships based on their location. Things like finding closest ships to some location or all ships inside some radius around that location is needed in many places. For example when a ship wants to find the closest enemy or when some phenomenon affects all ships within some distance. So I implemented an octree to be used in the battle for this purpose. The benefits of the octree is that when looking for ships near some location, the algorithm can first iterate the bigger cubes and check if anything inside that can be close enough to the location. If not, every ship under that cube and its subcubes can be ignored, otherwise each subcube is again considered. Managing the octree of course takes some work, but with lots of ships the benefits greatly outweigh the costs.

In the video you can see visualization of the octrees and an example of its use. There are 200 ships per side, and for each frame and for each ship the closest ship from the enemy army is found. The closest ship is visualized with a blue line for the first army and with a red line for the second army. In a real case, the nearest ship search is done much more rarely, maybe closer to once per frame than 400 times per frame like here. Also, usually there should be some max distance in the search which also makes it a bit cheaper. This is not a real performance test since it is only run in the Unity editor, which makes this a bit slow for other reasons.

Unit groups

Giving commands one by one to huge numbers of ships is not very handy, not to mention all other problems like small ships getting lost in some corners of space. So I added some group controls to the combat UI. Now ships can form groups, like in many RTS-games, and there is also a button to choose all remaining ships. Groups can be selected by pushing the corresponding numeric button or by clicking near the midpoint of the group. I still keep the option of choosing ships freely, but I might change it later so that the groups are fixed like in Total War -games. That might be a much easier way to command the armies and probably wouldn’t restrict the combat too much. But will have to test and see if that is needed.

Also added a button which shows the command queue of all ship groups.

Scene transitions and basic combat AI

Worked on initializing the combat and star map scenes with correct starting information. Now if fleets meet at a star system, a combat scene is started and corresponding data is transferred to the combat scene. After the combat the star map is loaded and casualties are removed from the fleets. It is still handy to be able to test any scene you want by starting straight from that scene, and not having to navigate to that state of the game from the start. So each scene can also be started directly in which case it uses a test state for initialization which can be set from the Unity editor.

Also made a basic AI to control enemy ships in combat. Now they just queue to attack every opponent ship (player ships) one by one. I have done a coroutine based state machine for the individual ships and the same state machine could be used for the AI controlling the whole army. The state machine is very flexible and I feel that it will be enough for anything I need.

Gimbals

There will be four different mounting options for each weapon. Normal and right angle mounts are fixed to the direction they are pointing at. Then there are gimbal and turret mounts. Turrets are able to aim at targets in a semi sphere area. Turrets are more expensive, require more power, more space and they do suffer from a gimbal lock around the zenith, that is, they have some problems if the target is exactly above them. Gimbal mounts are like normal mounts, except they can rotate a bit left and right and up and down around the starting direction. They do not require a bigger mounting spot like turrets do but are more expensive than fixed mounts. They can only be mounted on forward mounts however, right angle mounts are always fixed to the direction they are pointing!

Gimbal mounts help less maneuverable ships to target faster ships and they also provide nice automatic gun harmonisation (https://en.wikipedia.org/wiki/Gun_harmonisation) with adapting convergence point, which is needed against small targets. In the video the first corvette has beams with fixed mounts. The second corvette has gimbal mounts and it hits the small fighters much better.

Updated tracking

Updated all physics based direction tracking to be a bit smarter. Now they take all movements into account and still try to guide the turret/ship/gimbal to the correct direction the fastest way without overshooting. The following videos demonstrate the effects. The turrets try to optimally match the speed of the target, taking into account its own acceleration (and deceleration) and the movement of whatever it is mounted on. If the target comes to a sudden stop while the turret is correctly tracking, the turret will overshoot a bit. Similarly, if the ship the turret is mounted on rotates, and then comes to a sudden stop, the turret will overshoot. The result is satisfyingly realistic and accurate turrets.

Relative movement

Added possibility to give movement commands relative to some other ship. Should be useful when maneuvering bigger armies and ships of different speeds. In practice it means that the move commands can be set to follow another ship. In the fist video I have an overly complicated chain of ships following one another and in the second video lots of fighters following a destroyer.

Star map view

Finally starting the star map scene. Currently there are randomly distributed stars and fleets of different colors moving around. Changing the turn makes them do the set movement. There are lots of options on the rules of how to allow the movement between stars and how to restrict it. If ships are able to jump long distances it is hard to try to defend from attacks. It might be nice to have some kind of allowed paths between stars to have at least some kind of choke points. There also might be different kinds of propulsion systems which would enable different ways of moving around. I have some ideas but I have to test them a bit to see how they would work in reality.

Attack modes

Weapons, hit points and armor are not the only things that affect the outcome of combat. Here you can see what happens when fighters try to attack a frigate with turrets. The first fighter group uses aggressive attack mode while the other one uses evasive attack mode. In defence of the underachieving frigate, it does not yet have a proper AI to deal with many fighters, instead it tries to shoot them one by one and turn to face the current target. That makes it rotate wildly, which does not help targeting.