<?xml version="1.0" encoding="utf-8" ?>
<!-- This file enables extensions to the AI -->
<EternityPatchFile name="AIExtend" enabled="ai_extend" comment="Addresses are all 1.08 unless stated otherwise in the individual patches">

	<!-- 
	
		A change in the signature may indicate changes in the structure of params XG::Game::Battle::Core::MainSystem(rcx) and "SelectCharInfo"(rdx), 
		which would need to update the code in the DLL. 
		
		XG::Game::Battle::Core::MainSystem: 0x3690 (mobs)
		"SelectCharInfo": 0, (char_index), 0x4 (char_index2), 0xC (is_cpu), 0x10(team) 
	-->
	<Patch name="PatchOnBattleCreateChar" type="hook" search_start="0x1D3410" comment="signature based on 1.09" >
		<Instruction code="40 55 56 57 41 54 41 55 41 56 41 57" comment="push rbp, rsi, rdi, r12, r13, r14, r15" />
		<Instruction code="48 8D AC 24 40 FD FF FF" comment="lea rbp, [rsp-2C0h]" />
		<Instruction code="48 81 EC C0 03 00 00" comment="sub rsp, 3C0h" />
		<Instruction code="48 C7 45 80 FE FF FF FF" comment="mov qword ptr[rsp-80h], -2" />		
		<Instruction code="48 89 9C 24 10 04 00 00" comment="mov [rsp+410h], rbx" />
		<Instruction code="48 8B 05 XXXXXXXX" comment="mov rax, XXXXXXXX" />
		<Instruction code="48 33 C4" comment="xor rax, rsp" />
		<Instruction code="48 89 85 B0 02 00 00" comment="mov [rbp+2B0h], rax" />
		<Instruction code="45 8B E8" comment="mov r13d, r8d" />
		<Instruction code="44 89 44 24 58" comment="mov dword ptr[rsp+58h], r8d" />		
		<Instruction code="4C 8B E2" comment="mov r12, rdx" />
		<Instruction code="48 89 54 24 68" comment="mov [rsp+68h], rdx" />
		<Instruction code="4C 8B F1" comment="mov r14, rcx" />
		<Instruction code="48 89 4C 24 40" comment="mov [rsp+40h], rcx" />
		<Instruction code="48 63 32" comment="movsxd rsi, dword ptr [rdx]; rsi = char index" />
		<Instruction code="89 74 24 30" comment="mov [rsp+30h], esi" />		
		<Instruction code="83 FE 0D" comment="cmp esi, 0xD" />
		<Instruction code="0F 87 XXXXXXXX" comment="ja XXXXXXXX" />
		<Instruction code="48 8B FE" comment="mov rdi, rsi" />
		<Instruction code="48 89 74 24 38" comment="mov [rsp+38h], rsi" />
		<Instruction code="48 83 BC F1 90 36 00 00 00" comment="cmp qword ptr [rcx+rsi*8+3690h], 0" />
		<Instruction code="0F 85 XXXXXXXX" comment="jnz XXXXXXXX" />
		<Instruction code="45 33 C0" comment="xor r8d, r8d" />
		<Instruction code="8B 52 04" comment="mov edx, [rdx+4]; This seems to be char index too. What's the difference?" />
		<Instruction code="E8 XXXXXXXX" comment="call XXXXXXXX" />
		<Instruction code="48 89 44 24 60" comment="mov [rsp+60h], rax" />
		<Instruction code="48 85 C0" comment="test rax, rax" />
		<Instruction code="0F 84 XXXXXXXX" comment="jz XXXXXXXX" />
		<Instruction code="48 8B 18" comment="mov rbx, [rax]" />
		<Instruction code="48 85 DB" comment="test rbx, rbx" />
		<Instruction code="0F 84 XXXXXXXX" comment="jz XXXXXXXX" />
		<Instruction code="48 69 CF D0 01 00 00" comment="imul rcx, rdi, 1D0h" />
		<Instruction code="49 8D BE D0 00 00 00" comment="lea rdi, [r14+0D0h]" />
		<Instruction code="48 03 F9" comment="add rdi, rcx" />
		<Instruction code="48 89 7C 24 48" comment="mov [rsp+48h], rdi" />
		<Instruction code="8B 8B 90 00 00 00" comment="mov ecx, [rbx+90h]" />
		<Instruction code="89 0F" comment="mov [rdi], ecx" />
		<Instruction code="8B 8B 94 00 00 00" comment="mov ecx, [rbx+94h]" />
		<Instruction code="89 4F 04" comment="mov [rdi+4], ecx" />
		<Instruction code="41 8B 44 24 08" comment="mov eax, [r12+8]" />
		<Instruction code="89 47 08" comment="mov [rdi+8], eax" />
		<Instruction code="41 8B 44 24 0C" comment="mov eax, [r12+0Ch]" />
		<Instruction code="89 47 0C" comment="mov [rdi+0Ch], eax" />
		<Instruction code="41 8B 44 24 10" comment="mov eax, [r12+10h]" />
		
		<Hook value="OnBattleCreateCharPatched" setup="SetupAIExtend" />
	</Patch>
	
	<!-- A signature change here could mean that the CharDef struct/r8) needs update -->
	<!-- 0x10: mob -->
	<!-- 0x154: type -->
	<Patch name="PatchAISpecial" type="hook" search_start="0x1266E0" >
		<Instruction code="40 56 57 41 56" comment="push rsi, rdi, r14" />
		<Instruction code="48 83 EC 20" comment="sub rsp, 0x20" />
		<Instruction code="49 8B 40 10" comment="mov rax, [r8+10h]" />
		<Instruction code="49 8B F8" comment="mov rdi, r8" />
		<Instruction code="4C 8B F2" comment="mov r14, rdx" />
		<Instruction code="48 8B F1" comment="mov rsi, rcx" />
		<Instruction code="48 85 C0" comment="test rax, rax" />
		<Instruction code="75 0B" comment="jnz +0xB" />
		<Instruction code="33 C0" comment="xor eax, eax" />
		<Instruction code="48 83 C4 20" comment="add rsp, 20h" />
		<Instruction code="41 5E 5F 5E" comment="pop r14, rdi, rsi" />
		<Instruction code="C3" comment="retn" />
		<Instruction code="48 83 B8 A8 04 00 00 00" comment="cmp qword ptr [rax+4A8h], 0" />
		<Instruction code="74 EB" comment="jz -0x15" />
		<Instruction code="48 8B D7" comment="mov rdx, rdi" />
		<Instruction code="E8 XXXXXXXX" comment="call XXXXXXXX" />
		<Instruction code="83 F8 01" comment="cmp eax, 1" />
		<Instruction code="0F 85 XXXXXXXX" comment="jnz XXXXXXXX" />
		<Instruction code="44 8B 87 54 01 00 00" comment="mov r8d, [rdi+154h]" />
		
		<Hook value="AISpecialPatched" setup="SetupAISpecial" />		
	</Patch>
	
	<Patch name="PatchSkillCommand" type="hook" search_start="0x171D80" >	
		<Instruction code="48 89 5C 24 08" comment="mov [rsp+8], rbx" />
		<Instruction code="48 89 6C 24 10" comment="mov [rsp+10h], rbp" />
		<Instruction code="48 89 74 24 18" comment="mov [rsp+18h], rsi" />
		<Instruction code="57" comment="push rdi" />
		<Instruction code="48 83 EC 50" comment="sub rsp, 50h" />
		<Instruction code="8B 81 A0 00 00 00" comment="mov eax, [rcx+0A0h]" />
		<Instruction code="8B EA" comment="mov ebp, edx" />
		<Instruction code="89 81 A4 00 00 00" comment="mov [rcx+0A4h], eax" />
		
		<Hook value="SkillCommandPatched" setup="SetupSkillCommand" />	
	</Patch>
	
	<!-- Patches below are only to ensure struct definitions in the dll are correct -->
	
	<!-- Add 0xB0 to the rcx of memset to get the address of bcm field -->
	<Patch name="CheckBattleCommandAlign" type="notify" search_start="0x23AD3A" comment="addr 1.09" >
		<Instruction code="89 81 B0 0C 00 00" comment="mov [rcx+0CB0h], eax" />
		<Instruction code="33 D2" comment="xor edx, edx" />
		<Instruction code="C7 81 B4 0C 00 00 FF FF FF FF" comment="mov dword ptr [rcx+0CB4h], -1" />
		<Instruction code="41 B8 20 01 00 00" comment="mov r8d, 120h" />
		<Instruction code="48 C7 81 B8 0C 00 00 00 00 00 00" comment="mov qword ptr [rcx+0CB8h], 0" />
		<Instruction code="48 C7 81 C0 0C 00 00 00 00 00 00" comment="mov qword ptr [rcx+0CC0h], 0" />
		<Instruction code="48 81 C1 10 0D 00 00" comment="add rcx, 0D10h" />
		<Instruction code="E9 XXXXXXXX" comment="jmp memset" />
		
		<Notify value="NULL" />
	</Patch>
	
	<!-- 0x50: is_cpu -->
	<Patch name="CheckBattleMobAlign" type="notify" search_start="0x149E05" comment="takes place in constructor" >
		<Instruction code="48 8D 05 XXXXXXXX" comment="lea rax, XG::Game::Battle::Mob vtable" />
		<Instruction code="48 89 06" comment="mov [rsi], rax" />
		<Instruction code="0F 10 03" comment="movups xmm0, xmmword ptr [rbx]" />
		<Instruction code="0F 11 46 40" comment="movups xmmword ptr [rsi+40h], xmm0" />
		<Instruction code="89 7E 50" comment="mov [rsi+50h], edi" />
		<Instruction code="45 33 FF" comment="xor r15d, r15d" />
		<Instruction code="4C 89 BE 80 00 00 00" comment="mov [rsi+80h], r15" />
		<Instruction code="48 8D 05 XXXXXXXX" comment="lea rax, XG::Eva::Em::Random vtable" />
		
		<Notify value="NULL" />
	</Patch>
	
	<!-- 0xBC: cms_id -->
	<Patch name="CheckBattleMobAlign2" type="notify" search_start="0x14CBB8" >
		<Instruction code="49 8B 06" comment="mov rax, [r14]" />
		<Instruction code="48 85 C0" comment="test rax, rax" />
		<Instruction code="74 0C" comment="jz +0xC" />
		<Instruction code="8B 80 90 00 00 00" comment="mov eax, [rax+90h]" />
		<Instruction code="89 87 BC 00 00 00" comment="mov [rdi+0BCh], eax" />
		<Instruction code="4C 89 B7 90 04 00 00" comment="mov [rdi+490h], r14" />
		
		<Notify value="NULL" />
	</Patch>
	
	<!-- 0xF8: hp -->
	<!-- 0x104: ki -->
	<Patch name="CheckBattleMobAlign3" type="notify" search_start="0x161270" comment="addr 1.09.1" >
		<Instruction code="8B 83 F8 00 00 00" comment="mov eax, [rbx+0F8h]" />
		<Instruction code="48 8D 93 04 01 00 00" comment="lea rdx, [rbx+104h]" />
		<Instruction code="33 F6" comment="xor esi, esi" />
		<Instruction code="89 83 6C 21 00 00" comment="mov [rbx+216Ch], eax" />
		<Instruction code="48 8D 8B 72 21 00 00" comment="lea rcx, [rbx+2172h]" />
		<Instruction code="66 89 B3 74 21 00 00" comment="mov [rbx+2174h], si" />

		<Notify value="NULL" />
	</Patch>
	
	<!-- 0x164: stamina -->
	<Patch name="CheckBattleMobAlign4" type="notify" search_start="0x1612AF" comment="addr 1.09.1" >
		<Instruction code="48 8D 93 64 01 00 00" comment="lea rdx, [rbx+164h]" />
		<Instruction code="41 B8 01 00 00 00" comment="mov r8d, 1" />
		<Instruction code="48 8D 8B 74 21 00 00" comment="lea rcx, [rbx+2174h]" />
		<Instruction code="E8 XXXXXXXX" comment="call XXXXXXXX" />
		<Instruction code="48 8B 03" comment="mov rax, [rbx]" />
		<Instruction code="48 8B CB" comment="mov rcx, rbx" />
		<Instruction code="FF 90 D0 01 00 00" comment="call qword ptr [rax+1D0h]" />

		<Notify value="NULL" />
	</Patch>
	
	<!-- 0x260: skills -->
	<!-- 0x4A8: battle_command -->
	<Patch name="CheckBattleMobAlign5" type="notify" search_start="0x14A3B1" comment="takes place in constructor, address 1.09" >
		<Instruction code="41 8D 4F 09" comment="lea ecx, [r15+9]" />
		<Instruction code="48 8D 86 60 02 00 00" comment="lea rax, [rsi+260h]" />
		<Instruction code="0F 1F 40 00" comment="4-nop" />
		<Instruction code="4C 89 38" comment="mov [rax], r15; loop_start label" />
		<Instruction code="44 89 78 08" comment="mov [rax+8], r15d" />
		<Instruction code="48 8D 40 10" comment="lea rax, [rax+10h]" />
		<Instruction code="48 83 E9 01" comment="sub rcx, 1" />
		<Instruction code="75 EF" comment="jnz loop_start" />
		<Instruction code="48 8D 8E C0 03 00 00" comment="lea rcx, [rsi+3C0h]" />
		<Instruction code="E8 XXXXXXXX" comment="call XXXXXXXX" />
		<Instruction code="4C 89 BE 90 04 00 00" comment="mov [rsi+490h], r15" />
		<Instruction code="4C 89 BE 98 04 00 00" comment="mov [rsi+498h], r15" />
		<Instruction code="4C 89 BE A0 04 00 00" comment="mov [rsi+4A0h], r15" />
		<Instruction code="4C 89 BE A8 04 00 00" comment="mov [rsi+4A8h], r15" />
		<Instruction code="4C 89 BE 08 05 00 00" comment="mov [rsi+508h], r15" />
		<Instruction code="4C 89 BE 58 05 00 00" comment="mov [rsi+558h], r15" />
		<Instruction code="4C 89 BE 60 05 00 00" comment="mov [rsi+560h], r15" />
		<Instruction code="4C 89 BE 68 05 00 00" comment="mov [rsi+568h], r15" />
		
		<Notify value="NULL" />
	</Patch>
	
	<!-- 0x1D18: trans_control -->
	<Patch name="CheckBattleMobAlign6" type="notify" search_start="0x14E4F8" comment="addr 1.09.1" >
		<Instruction code="83 BF 28 1D 00 00 0C" comment="cmp dword ptr [rdi+1D28h], 0Ch" />
		<Instruction code="75 54" comment="jnz +0x54" />
		<Instruction code="48 8B 06" comment="mov [rax], rsi" />
		<Instruction code="48 8B CE" comment="mov rcx, rsi" />
		<Instruction code="FF 90 C0 04 00  00" comment="call qword ptr [rax+4C0h]" />
		
		<Notify value="NULL" />	
	</Patch>
	
	<!-- rdx+0x28: ai_decision -->
	<Patch name="CheckAIDef" type="notify" search_start="0x1276D0" >
		<Instruction code="8B 42 28" comment="mov eax, [rdx+28h]" />
		<Instruction code="83 C0 FB" comment="add eax, -5" />
		<Instruction code="83 F8 5B" comment="cmp eax, 5Bh" />
		<Instruction code="77 4A" comment="ja +0x4A" />
		
		<Notify value="NULL" />
	</Patch>
	
	<!-- rsi+0x3A0: unknown_interface_var -->
	<Patch name="CheckBattleMobAlign7" type="notify" search_start="0x153300" comment="addr 1.09.1" >
		<Instruction code="48 98" comment="cdqe" />
		<Instruction code="48 8B B4 C5 90 36 00 00" comment="mov rsi, [rbp+rax*8+3690h]" />
		<Instruction code="8B B6 A0 03 00 00" comment="mov esi, [rsi+3A0h]" />
		<Instruction code="EB 07" comment="jmp +7" />
		
		<Notify value="NULL" />
	</Patch>
	
	<!-- 1D0: size of object UnkMobStruct -->
	<!-- D0: offset of unk_mob_data in core mainsystem" -->
	<Patch name="CheckCoreMSAlign" type="notify" search_start="0x15EAF9" comment="addr 1.09.1" >
		<Instruction code="49 69 CD D0 01 00 00" comment="imul rcx, r13, 1D0h" />
		<Instruction code="48 8D 04 39" comment="lea, rax, [rcx+rdi]" />
		<Instruction code="41 83 FF 02" comment="cmp r15d, 2" />
		<Instruction code="75 0B" comment="jnz +B" />
		<Instruction code="41 0F B7 8C 04 E2 00 00 00" comment="movzx ecx, word ptr [r12+rax+0E2h]" />
		<Instruction code="EB 09" comment="jmp +9" />
		<Instruction code="41 0F B7 8C 04 D0 00 00 00" comment="movzx ecx, word ptr [r12+rax+0D0h]" />
		
		<Notify value="NULL" />
	</Patch>
	
</EternityPatchFile>