Lua events can now prevent the combat (just as is done in this code) without the need to resort to submarine flags.
Yes, now that this is possible in TOTPP 0.16, I agree it would probably be a better approach to preventing shore bombardment. (At the time I released MM, TOTPP 0.15.1 was the latest version and that didn't provide this capability.) I'll add it to the list of improvements I'd like to make in the next Medieval Millennium update.
I tried to implement this into Medieval Millennium tonight, writing code to prevent shore bombardment without resorting to giving all ships the submarine flag, but unfortunately ran into a roadblock.
The first issue is that even when I prevent the combat with
coroutine.yield(true, defender) there is still attack
animation that takes place -- even though no damage is taken by either unit, it looks like the defender lost a round. And it's always the defender, too: even if I use
coroutine.yield(true, attacker) the animation still appears on top of the defending unit for some reason. But overall, this is a minor concern -- a visual distraction, but not really detrimental to gameplay.
Second, as you noted in the code of your
combat-value-firepower-inspector.zip in the first post above, the attempted attack increments
attacker.moveSpent, so this needs to be subtracted in the event code in order to leave the attacker "unchanged". Easy enough, done.
But third, and here's where I'm stuck: the attempted attack
also increments "attacks made this turn" for the unit, and that's causing a problem due to the Rules.txt settings I have in the @ ATTACKS section. In MM, I limit all unit types to at most 1 attack per turn. So if a ship attempts shore bombardment and my event blocks the combat, the ship is now prevented (for the remainder of that turn) from making a different
valid attack on an opponent's ship at sea.
This made me realize (and I'm going to tag
@TheNamelessOne here) that it would be great if
unittype.attacksPerTurn could be added as a new
get/set field, reflecting the contents of the @ ATTACKS section, and also
unit.attacksThisTurn could be added as a new
get/set field to give visibility to this counter for each unit. If I had the latter, then I could simply reset that to 0 after the attempted shore bombardment and I think I'd be in business.
Without that, the only thing I can imagine doing is actually
deleting the attacking unit entirely and immediately creating a perfect copy of it on the same tile, except that the copy wouldn't have attempted an attack this turn -- so it would be free to conduct a valid attack on another ship. But that also requires transferring any units onboard the first ship to its replacement. And at this point I'm thinking that maybe leaving the submarine flag in place is the simpler and better option.
I haven't gotten far enough yet to tell whether the AI will get trapped in an infinite loop of attempting shore bombardments that are continuously blocked. I hope not, but that's another factor that inclines me to think maybe the submarine flag is preferable.
I'm open to any feedback or suggestions!