Index: binaries/data/mods/public/simulation/components/UnitAI.js
===================================================================
--- binaries/data/mods/public/simulation/components/UnitAI.js	(révision 17095)
+++ binaries/data/mods/public/simulation/components/UnitAI.js	(copie de travail)
@@ -819,7 +819,8 @@
 		},
 
 		"Order.Stop": function(msg) {
-			this.CallMemberFunction("Stop", [false]);
+			if (!this.IsAttackingAsFormation())
+				this.CallMemberFunction("Stop", [false]);
 			this.FinishOrder();
 		},
 
@@ -1148,26 +1149,27 @@
 			"ATTACKING": {
 				// Wait for individual members to finish
 				"enter": function(msg) {
-					var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation);
-					cmpFormation.SetRearrange(true);
-					cmpFormation.MoveMembersIntoFormation(false, false);
-					this.StartTimer(200, 200);
-
 					var target = this.order.data.target;
+					var allowCapture = this.order.data.allowCapture;
 					// Check if we are already in range, otherwise walk there
 					if (!this.CheckTargetAttackRange(target, target))
 					{
 						if (this.TargetIsAlive(target) && this.CheckTargetVisible(target))
 						{
-							var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack);
-							var range = cmpAttack.GetRange(target);
-							this.PushOrderFront("WalkToTargetRange", { "target": target, "min": range.min, "max": range.max });
-							return;
+							this.FinishOrder();
+							this.PushOrderFront("Attack", { "target": target, "force": false, "allowCapture": allowCapture });
+							return true;
 						}
 						this.FinishOrder();
-						return;
+						return true;
 					}
 
+					var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation);
+					// TODO fix the rearranging while attacking as formation
+					cmpFormation.SetRearrange(!this.IsAttackingAsFormation());
+					cmpFormation.MoveMembersIntoFormation(false, false);
+					this.StartTimer(200, 200);
+					return false;
 				},
 
 				"Timer": function(msg) {
@@ -1178,8 +1180,6 @@
 					{
 						if (this.TargetIsAlive(target) && this.CheckTargetVisible(target))
 						{
-							var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack);
-							var range = cmpAttack.GetRange(target);
 							this.FinishOrder();
 							this.PushOrderFront("Attack", { "target": target, "force": false, "allowCapture": allowCapture });
 							return;
@@ -1191,6 +1191,9 @@
 
 				"leave": function(msg) {
 					this.StopTimer();
+					var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation);
+					if (cmpFormation)
+						cmpFormation.SetRearrange(true);
 				},
 			},
 		},
@@ -4230,9 +4233,8 @@
 	// for formation members, the formation will take care of the range check
 	if (this.IsFormationMember())
 	{
-		var cmpFormationAttack = Engine.QueryInterface(this.formationController, IID_Attack);
 		var cmpFormationUnitAI = Engine.QueryInterface(this.formationController, IID_UnitAI);
-		if (cmpFormationAttack && cmpFormationAttack.CanAttackAsFormation() && cmpFormationUnitAI && cmpFormationUnitAI.GetCurrentState() == "FORMATIONCONTROLLER.ATTACKING")
+		if (cmpFormationUnitAI && cmpFormationUnitAI.IsAttackingAsFormation())
 			return false;
 	}
 
@@ -4240,7 +4242,7 @@
 	if (cmpFormation)
 		target = cmpFormation.GetClosestMember(this.entity);
 
-	if (type!= "Ranged")
+	if (type != "Ranged")
 		return this.MoveToTargetRange(target, IID_Attack, type);
 
 	if (!this.CheckTargetVisible(target))
@@ -4332,14 +4334,9 @@
 	// for formation members, the formation will take care of the range check
 	if (this.IsFormationMember())
 	{
-		var cmpFormationAttack = Engine.QueryInterface(this.formationController, IID_Attack);
 		var cmpFormationUnitAI = Engine.QueryInterface(this.formationController, IID_UnitAI);
-
-		if (cmpFormationAttack &&
-		    cmpFormationAttack.CanAttackAsFormation() &&
-		    cmpFormationUnitAI &&
-		    cmpFormationUnitAI.GetCurrentState() == "FORMATIONCONTROLLER.COMBAT.ATTACKING" &&
-		    cmpFormationUnitAI.order.data.target == target)
+		if (cmpFormationUnitAI && cmpFormationUnitAI.IsAttackingAsFormation()
+			&& cmpFormationUnitAI.order.data.target == target)
 			return true;
 	}
 
@@ -5777,6 +5774,15 @@
 	return (cmpPack && cmpPack.IsPacking());
 };
 
+//// Formation specific functions ////
+
+UnitAI.prototype.IsAttackingAsFormation = function()
+{
+	var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack);
+	return cmpAttack && cmpAttack.CanAttackAsFormation()
+		&& this.GetCurrentState() == "FORMATIONCONTROLLER.COMBAT.ATTACKING";
+};
+
 //// Animal specific functions ////
 
 UnitAI.prototype.MoveRandomly = function(distance)
Index: binaries/data/mods/public/simulation/templates/formations/phalanx.xml
===================================================================
--- binaries/data/mods/public/simulation/templates/formations/phalanx.xml	(révision 17095)
+++ binaries/data/mods/public/simulation/templates/formations/phalanx.xml	(copie de travail)
@@ -13,6 +13,6 @@
     <MaxRows>6</MaxRows>
   </Formation>
   <FormationAttack>
-    <CanAttackAsFormation>false</CanAttackAsFormation>
+    <CanAttackAsFormation>true</CanAttackAsFormation>
   </FormationAttack>
 </Entity>
Index: binaries/data/mods/public/simulation/templates/formations/skirmish.xml
===================================================================
--- binaries/data/mods/public/simulation/templates/formations/skirmish.xml	(révision 17095)
+++ binaries/data/mods/public/simulation/templates/formations/skirmish.xml	(copie de travail)
@@ -10,6 +10,6 @@
     <MinColumns>3</MinColumns>
   </Formation>
   <FormationAttack>
-    <CanAttackAsFormation>false</CanAttackAsFormation>
+    <CanAttackAsFormation>true</CanAttackAsFormation>
   </FormationAttack>
 </Entity>
Index: binaries/data/mods/public/simulation/templates/formations/syntagma.xml
===================================================================
--- binaries/data/mods/public/simulation/templates/formations/syntagma.xml	(révision 17095)
+++ binaries/data/mods/public/simulation/templates/formations/syntagma.xml	(copie de travail)
@@ -27,6 +27,6 @@
     </Animations>    
   </Formation>
   <FormationAttack>
-    <CanAttackAsFormation>false</CanAttackAsFormation>
+    <CanAttackAsFormation>true</CanAttackAsFormation>
   </FormationAttack>
 </Entity>
Index: binaries/data/mods/public/simulation/templates/formations/testudo.xml
===================================================================
--- binaries/data/mods/public/simulation/templates/formations/testudo.xml	(révision 17095)
+++ binaries/data/mods/public/simulation/templates/formations/testudo.xml	(copie de travail)
@@ -20,6 +20,6 @@
     </Animations>
   </Formation>
   <FormationAttack>
-    <CanAttackAsFormation>false</CanAttackAsFormation>
+    <CanAttackAsFormation>true</CanAttackAsFormation>
   </FormationAttack>
 </Entity>
Index: binaries/data/mods/public/simulation/templates/formations/wedge.xml
===================================================================
--- binaries/data/mods/public/simulation/templates/formations/wedge.xml	(révision 17095)
+++ binaries/data/mods/public/simulation/templates/formations/wedge.xml	(copie de travail)
@@ -8,6 +8,6 @@
     <FormationShape>triangle</FormationShape>
   </Formation>
   <FormationAttack>
-    <CanAttackAsFormation>false</CanAttackAsFormation>
+    <CanAttackAsFormation>true</CanAttackAsFormation>
   </FormationAttack>
 </Entity>
