Autoattack

Autoattack is a server mode where units might immediately attack an enemy unit if it moves adjacent to them. The implementation of this has evolved over time, generally to make it "smarter", overcome some balance issues, and give more implementation control to rulesets.
== History ==
* Warserver 0.8.6a (2006) auto-attack becomes possible even out of cities.
* Warserver 0.8.7a units consider winning chance for deciding auto-attack (originally it was 50%).
* The auto-attack mode for units in a city arrived to main Freeciv branch version 1.8.0. The flag enabling it was located in the city dialog. 
* All-server autoattack was added in 2004 with PR#2415 in October 2004 (r8788). 
* City-specific autoattack was completely removed from the game up to July 2005 (PR#13501, v.2.1.0).
* Since 2.3.5, nuclear units won't auto-attack, you can guess why it is good for the defender.
* In 3.0, the hard-coded rule above is moved to the ruleset by listing UTYFs who may not autoattack, in the will_never vector of game.ruleset. 
* In 3.0, unit types who will always be auto-attacked, are moved into units.ruleset with the UTYF "Provoking" flag. (used to be hard-coded as diplomatic and transporting units).
* In 3.x, the will_never flag list becomes the if_attacker requirement vector, giving ruleset a requirement vector control over the legal conditions for autoattack legality.
* In 3.09++ (FCW fork), ACITIVTY_VIGIL and autoattack_style server setting introduced. Allows configuring a game so only units on ACTIVITY_VIGIL will auto-attack.
* In 3.x, priority order of autoattack actions introduced: i.e., which actions are triggered in which order (capture, attack, bombard, etc.)
* In 3.09++ interactive UTYF "layered logic matrix" added for ruleset programming of rules for autoattack engagement between all attacking and defending unit_types. (See below.)
* In 3.09++ the exploit "bring unit with cargo to a tile to make the tile autoattack-proof" was fixed.
== Rules in 2.6.0 ==
The code responsible for autoattack behaviour mainly lives in server/unittools.c::static bool unit_survive_autoattack(struct unit *punit)
Autoattack is turned on or off for all units in a game by the server option autoattack (game.server.autoattack). The autoattack consideration is triggered for any unit that moves (goes, paradrops, teleports...) and the action may be performed by each of the adjacent enemy units if the criteria are met for that one. Unit actions that do not result in a movement don't trigger an autoattack.
All units on tiles now adjacent to one moved that are hostile to it and are able to attack it are sorted for the order they will auto-attack. If a unit and its transporter are both able to auto-attack, the cargo always goes first. If two units are on the same transporter depth, one with better winning chances will be first. Then, for each point of the list in an order the winning chance will be compared to a threshold (25% usually, but 90% for a sole city defender) and to the winning chance of the moved unit if it will attack first; both probabilities are evaluated regarding defenders of the stacks on the tiles, and the moved unit is always considered having at least 1 movement fragment. A unit perform autoattack if its surval chance is greater then the threshold, and either it is greater in attack on the unit then in defence against it or the unit is provoking. The last condition in 2.6 means that the moved unit has nonzero transport capacity (disregarding if it actually is transporting now or not) or can act hostile (i.e is something like Diplomat or Spy). If the moved unit for any reason can not attack a tile, any units there won't auto-attack this unit unless it is provoking.
=== Changes in 3.0 ===
Since 3.0, attack is an action controlled by actionenabler. To sort potential attackers, the probability of the action success is calculated by action_prob() function from common/actions.c; it mostly comes as winning chance rounded to 0.5%. If the unit is unseen to the potential attacker (e.g. has entered a city, who knows if there are peaceful units inside?), the function results in a special value which is pessimistically considered a sign of attack impossibility. The provoking unit types are unhardcoded with the flag "Provoking", and flags that prevent autoattack are listed in will_never ruleset setting.
== 3.09++ Autoattack implementation in FCW ==
In Freeciv-Web running 3.09++ rules, autoattacks take on a much more advanced capacity:
* VIGIL activity: The ability to "game" or "bait" the server's "artificial dumbness" by knowing its autoattack conditions, had resulted in autoattack setting being rarely used in top multiplayer games. The VIGIL activity state lets a player select only those units he wants to engage in autoattacks. Units not under VIGIL can simply never be baited into an autoattack.
* Autoattack as a way for rulesets to program and better specify realistic engagement mechanics and thus also special abilities realistically appropriate to that type of unit: e.g., giving Fighters the ability to intercept other aircraft.
* Autoattack as a medium for creating special tactics different from standard attack, to represent unique abilities and special historical tactics available to some units. See Special Unit Attacks for more information about this progressive implementation.
* Autoattacks gain many more permutations/combinations of specificity for which units qualify to provoke which other types of units, to even better diminish the "game and bait" exploits. And perhaps MORE importantly, allow the rulesets "to embrace the wormhole" created by a game that is both simultaneous and turn-based, in order to capitalize and exploit this strange dynamic in a way that turns it from an awkward thing into an advantage that enriches the rules of engagement between different unit types with different tactical abilities. The Legend below helps interpret the chart below it:
Key/Legend
LABEL 	EXPLANATION
AvidAttacker 	Unit likes to always autoattack unless overridden by other flags
no_flag 	Has no UTYF related to autoattacks
O1, O2, O3 	OddsAttackClass1,2,3: attacks ProvokingClass1,2,3 iff odds>threshold
A1, A2, A3 	AlwaysAttackClass1-3: always attacks ProvokingClass1,2,3
! if_attacker 	attack unit doesn't fulfill the if_attacker vector in game.ruleset
NonProvoking 	UTYF on a unit which makes it never autoattacked
ProvokingClass1	AlwaysAttackClass1 units always autoattack this unit
OddsAttackClass1 units attack iff >odds
ProvokingClass2	same as above but for A2,O2
ProvokingClass3	same as above but for A3,O3
-- 	No autoattack unless specified by other flags
N- 	Prohibits autoattack: overrides all other autoattack flags
ODDS 	auto-attack if odds qualify (unless overridden by "N-" or "YES")
YES 	ALWAYS auto-attacks (unless overridden by "N-")
attacker UTYF flags ⏩ 
defender UTYF flags ⬇️	no_flag	AvidAttacker	O1 	O2 	O3 	A1 	A2 	A3 	! if_attacker
NonProvoking 	N- 	N- 	N- 	N- 	N- 	N- 	N- 	N- 	N-
no_flag 	-- 	ODDS 	-- 	-- 	-- 	-- 	-- 	-- 	N-
Provoking 	ODDS 	ODDS 	ODDS	ODDS	ODDS	ODDS	ODDS	ODDS	N-
ProvokingClass1 	-- 	ODDS 	ODDS	-- 	-- 	YES 	-- 	-- 	N-
ProvokingClass2 	-- 	ODDS 	-- 	ODDS	-- 	-- 	YES 	-- 	N-
ProvokingClass3 	-- 	ODDS 	-- 	-- 	ODDS	-- 	-- 	YES 	N-
————————————————————————————————————————————————————————————————————————————————————
== Tactical Considerations ==
Early implementations of autoattack radically changed unit balance in many ways. If a player moves asynchronously with their adversaries, they can easily approach a city and do their will. But in an autoattack game, the mounted units and diplomats will probably be killed before doing what they would usually do. Strong-attack-weak-defense units (such as mounted) become less powerful since they can't rush from a distance on a strong attacker, without being killed on approach.
In early implementations, the usual way to capture cities when autoattack was on, was to "lure" the defenders to "bite the bait." This becomes similar to fishing with worms, where the enemy player isn't happy that his automatic responses are like a dumb fish. Cheap provoking units provoke attacks to diminish the defensibility of the city (loss of moves that could be used for counterattacking a strong unit or a unit fortifying adjacent to the city, loss of hitpoints of the city defenders from attacking the bait, and so on.) For many years, a recession or depression happened with regards to games using autoattack, in spite of the original reason for it being as valid as ever (explained later.) 
The above describes the "game and bait" exploits that render autoattack unappealing to many players. But one of the most important original reasons for autoattack was to diminish and ameliorate the "RTS" dynamics in a game that lives in a strange balance between turn-based and simultaneous phase. (In the case of longturn this is even more important, as it provides a way to have one's units intelligently perform up to their legal capacities without the human player needing to be logged into the game all day.) 
As of 2022/2023, there is a new era of thinking on how to integrate autoattack into a turn-based simultaneous-phase game. Originally thought to be a contradiction of unsolvable issues, turn-based simultaneity combines with ruleset-programmed autoattack to better express the tactical properties of real historical units: Fighters who sat dumb on runways express their superior interception abilities; artillery whose stats were programmed as weak defenders, nevertheless get to express their ability to sit on a hill and punish whoever comes close. The autoattack_style server setting even allows programming conditions for gaining this advantage, such as not allowing cannons to enter vigil unless occupying a tile with full move points, or for vigil-intercepting fighters to have spent less than 3 moves to go out to their patrol area, and so on.
Implementations such as those detailed above, provide inspiration for future development which will take autoattack to a new level of game play, and perhaps turn the turn-based-simultaneous-phase from a weak point of freeciv into one of its most unique aspects amongs all 4X games: expressing reactiveness, intiative, and special tactical abilities in ways other games cannot. 
Category:Gameplay