The default code I provided to you was very simple: your base unit class has a method called skirmish. It's strategy consists of two elements:
Stay at max range from the nearest enemy
Fire your weapon at the nearest enemy
The problem is that our attacking and movement are linked together. What if we want to attack something other than the nearest enemy?
Attack vs. Movement
Think about attacking and movement in seperate stages
attack(Weapon w) - Targets and uses this weapon on best target in range
movement() - Decides where we want to move
First Steps - Attack Lowest Health Enemy
Units can deal the same amount of damage regardless of how much health they have left. As a result, it is very useful to eliminate enemy targets.
By firing at the most damaged enemy, rather than the nearest enemy, your units will focus their fire on the most vulnerable targets they can hit.
You can find out which unit has the least overall health by calling getCurEffectiveHealth(). This method adds up the unit's current shield + plating + structure.
You'll want to write a helper method with a header like:
Unit getLowestHealthEnemyInRadius(float radius)
Focusing your fire on a highly damaged unit is far more effective than spreading out your shots.
A Point System
Your units may have to balance a lot of different factors to decide which to attack. Mr. M's teams try to resolve this by using a point system for choosing targets.
Looping through the targets, a unit might be given "points" for having a lot of damage already, being a major threat, or being too close my base. When I add up all these points, I target the unit with the most points.
Factors To Consider
Target Health - Favor units with low health
My Damage - Favor units that I can deal the most damage to
Target's Damage - Favor units that deal a lot of damage
Enemy's Position- Favor units that are closer to me / my base ship / center of my fleet
When To Fire...
Do you always want to fire your weapon? Usually the answer is yes!
But consider waiting to fire until something is a little bit within your max range. What happens if you fire at exactly max range and then a unit moves one pixel away while you're still aiming? It cancels your attack!
You can have fancy solutions to this problem that are more precise, but you can sometimes solve this by giving your units a little wiggle room and only firing at 95% of max range.
Estimating the enemy damage and my damage can be solved by writing a single method to calculate estimated damage. Consider...