<EternityPatchFile name="MobControl" enabled="take_ally_control" comment="This file contains patches for the ally control feature" >	

	<!-- All addresses are 1.09.01 unless stated otherwise -->
	
	<Patch name="LocateChangeBody" type="notify"  search_start="0x1D3B50" >
		<Instruction code="40 55 56 57 41 54 41 55 41 56 41 57" comment="push rbp, rsi, rdi, r12-r15" />
		<Instruction code="48 8D AC 24 90 FA FF FF" comment="lea rbp, [rsp-570h]" />
		<Instruction code="48 81 EC 70 06 00 00" comment="sub rsp, 670h" />
		<Instruction code="48 8B 05 XXXXXXXX" comment="mov rax, XXXXXXXX" />
		<Instruction code="48 33 C4" comment="xor rax, rsp" />
		<Instruction code="48 89 85 60 05 00 00" comment="mov [rbp+560h], rax" />
		<Instruction code="48 63 C2" comment="mov rax, edx" />
		
		<Notify value="SetupChangeBody" />
	</Patch>	
	
	<Patch name="PatchUsesMobControl" type="hook" search_start="0x1922DF" >
		<Instruction code="E8 XXXXXXXX" comment="call uses_mob_control" />
		<Instruction code="85 C0" comment="test eax, eax" />
		<Instruction code="74 34" comment="jz +0x34" />
		<Instruction code="48 8B 0F" comment="mov rcx, [rdi]" />
		<Instruction code="48 81 C1 B0 00 00 00" comment="add rcx, B0h" />
		<Instruction code="E8 XXXXXXXX" comment="call get_mob_control" />
		<Instruction code="48 85 C0" comment="test rax, rax" />
		<Instruction code="0F 84 01 02 00 00" comment="jz +0x201" />
		
		<Hook type="call" value="UsesMobControlPatched" setup="SetupUsesMobControl" />
	</Patch>
	
	<Patch name="PatchGetMobControl" type="hook" search_start="0x1DBB6C" >
		<Instruction code="E8 XXXXXXXX" comment="call get_mob_control" />
		<Instruction code="48 8B F8" comment="mov rdi, rax" />
		<Instruction code="4D 8B F5" comment="mov r14, r13" />
		<Instruction code="48 85 C0" comment="test rax, rax" />
		<Instruction code="4C 0F 45 F0" comment="cmovnz r14, rax" />
		<Instruction code="4C 89 B5 A0 01 00 00" comment="mov [rbp+1A0h], r14" />
		<Instruction code="41 8B 46 44" comment="mov eax, [r14+44h]" />
		<Instruction code="89 45 94" comment="mov [rbp-6Ch], eax" />
		
		<Hook type="call" value="GetMobControlPatched" setup="SetupGetMobControl" />
	</Patch>
	
	<Patch name="PatchUsesMobControl2" type="hook" search_start="0x192438" >
		<Instruction code="E8 XXXXXXXX" comment="call uses_mob_control" />
		<Instruction code="85 C0" comment="test eax, eax" />
		<Instruction code="0F 85 BC 00 00 00" comment="jnz +0xBC" />
		<Instruction code="48 8B 0F" comment="mov rcx, [rdi]" />
		<Instruction code="E8 XXXXXXXX" comment="call XXXXXXXX" />
		<Instruction code="48 63 F0" comment="movsxd rsi, eax" />
		<Instruction code="85 C0" comment="test eax, eax" />
		
		<Hook type="call" value="UsesMobControlPatched" />
	</Patch>
	
	<!-- This patch will break on the dll side if the address of the virtual method changes -->
	<Patch name="PatchMobControlDialogueIsPlayer" type="notify" search_start="0x67D510" >
		<Instruction code="44 8B BD C8 00 00 00" comment="mov r15d, [rbp+C8]; original cms" />
		<Instruction code="48 85 C0" comment="test rax, rax" />
		<Instruction code="74 73" comment="jz +73" />
		<Instruction code="48 8B 10" comment="mov rdx, [rax]" />
		<Instruction code="48 8B C8" comment="mov rcx, rax" />
		<Instruction code="FF 92 C8 02 00 00" comment="call qword ptr[rdx+2C8]" />
		
		<Notify inst_index="5" value="OnDialogueIsPlayerLocated" />
	</Patch>
	
	<Patch name="PatchMobControlDialogueGetEventCastCom" type="hook" search_start="0x67D58F" >
		<Instruction code="8B 4C 24 54" comment="mov ecx, [rsp+54h]" />
		<Instruction code="E8 XXXXXXXX" comment="call GetEventCastComFromIndex" />
		<Instruction code="48 8B F0" comment="mov rsi, rax" />
		<Instruction code="48 85 C0" comment="test rax, rax" />
		<Instruction code="74 7D" comment="jz +7D" />
		<Instruction code="48 8B 00" comment="mov rax, [rax]" />
		<Instruction code="48 8B CE" comment="mov rcx, rsi" />
		<Instruction code="FF 90 C8 02 00 00" comment="call qword ptr[rax+2C8]" />
		
		<Hook type="call" inst_index="1" value="GetEventCastComFromIndexPatched" setup="SetupGetEventCastComFromIndex" />
	</Patch>
	
	<Patch name="PatchMobControlPatchUntr1" type="hook" search_start="0x1D3BCE" >
		<Instruction code="41 F6 87 B0 00 00 00 10" comment="test byte ptr [r15+0B0h], 10h; if transformed" />
		<Instruction code="74 0D" comment="jz +D" />
		<Instruction code="BA 01 00 00 00" comment="mov edx, 1" />
		<Instruction code="49 8B CF" comment="mov rcx, r15" />
		<Instruction code="E8 XXXXXXXX" comment="call Mob_Untransform" />
		<Instruction code="41 8B 8D BC 00 00 00" comment="mov ecx, [r13+0BCh]; ecx = cms" />
		<Instruction code="E8 XXXXXXXX" comment="call IsCac" />
		<Instruction code="85 C0" comment="test eax, eax" />
		
		<Hook type="call" inst_index="4" value="DummyMobUntransformForMobControl" setup="SetupMobUntransformForMobControl" />
	
	</Patch>
	
	<Patch name="PatchMobControlPatchUntr2" type="hook" search_start="0x1D3BF5" comment="happens a bit below the patch above" >
		<Instruction code="41 F6 85 B0 00 00 00 10" comment="test byte ptr [r13+0B0h], 10h; if transformed" />
		<Instruction code="74 0D" comment="jz +D" />
		<Instruction code="BA 01 00 00 00" comment="mov edx, 1" />
		<Instruction code="49 8B CD" comment="mov rcx, r13" />
		<Instruction code="E8 XXXXXXXX" comment="call Mob_Untransform" />
		<Instruction code="49 8D 8F A0 0A 00 00" comment="lea rcx, [r15+0AA0h]" />
		<Instruction code="E8 XXXXXXXX" comment="call XXXXXXXX" />
		<Instruction code="49 8D 8D A0 0A 00 00" comment="lea rcx, [r13+0AA0h]" />
		
		<Hook type="call" inst_index="4" value="DummyMobUntransformForMobControl" />
	</Patch>

	<!-- Just to protect the index 0x1C24 in the Battle_Mob object -->
	<Patch name="ProtectMobLoadedVar" type="notify" search_start="0x6B4710" comment="Takes place in qed cond 30" >
		<Instruction code="83 B8 24 1C 00 00 01" comment="cmp dword ptr [rax+1C24h], 1" />
		<Instruction code="77 30" comment="ja +0x30" />
		<Instruction code="48 83 B8 10 1B 00 00 00" comment="cmp qword ptr [rax+1B10h], 0" />
		<Instruction code="74 26" comment="je +0x26" />
		<Instruction code="8B 88 24 1C 00 00" comment="mov ecx, [rax+1C24h]" />
		
		<Notify value="NULL" />		
	</Patch>
</EternityPatchFile>