alloc(newmem,2048,"DiabloImmortal.exe"+748302)
label(returnhere)
label(exit)
label(if01)
label(check_history)
label(setup_02)
label(setup_03)
label(shift_queue)
label(do_shift)
label(save_to_history)
label(find_free_history)
label(next_slot_ok)

newmem:
push rax
push rbx
push rcx

// Memory buffer management:
// [rax+0], [rax+8], [rax+10] -> Shift Queue (3 slots)
// [rax+50] to [rax+C8] -> History Storage (16 slots)
mov rax,newmem+500

// =========================================================================
// 1. AUTO REMOVE FOG FOR ALL TILES
// =========================================================================
cmp r14,0
je if01
cmp byte ptr [r14+18],3F
jne if01
cmp byte ptr [r14+38],1F
jne if01
cmp byte ptr [r14+50],40
jne if01
mov byte ptr [r14+18],0F
if01:

// =========================================================================
// [HARD STOP]: CHECK SLOT 26 (C8 HEX) TO STOP ALL PROCESSING
// =========================================================================
// If slot 26 (C8 Hex) is not 0 -> History is full (16 tiles) -> Stop!
cmp qword ptr [rax+C8],0
jne exit

// =========================================================================
// 2. SIGNATURE & COORDINATE FILTERS (ONLY RUN IF NOT FULL)
// =========================================================================
cmp r14,0
je exit

// Base Map Signature check
cmp byte ptr [r14+18],0F
jne exit
cmp byte ptr [r14+38],1F
jne exit
cmp byte ptr [r14+50],40
jne exit

// Bounding box coordinates filter
cmp dword ptr [r14-10],(float)655
jb exit
cmp dword ptr [r14-10],(float)715
ja exit
cmp dword ptr [r14-C],(float)478
jb exit
cmp dword ptr [r14-C],(float)538
ja exit

// =========================================================================
// 3. CHECK HISTORY LOOP (SCAN SLOT 11 TO 26 TO PREVENT DUPLICATES)
// =========================================================================
mov rcx,10                 // 10 Hex = loop 16 times
check_history:
cmp [rax+48+rcx*8],r14     // Scan memory from 50 Hex to C8 Hex
je exit                    // Exit if current flat is already in history
loop check_history

// =========================================================================
// 4. CHECK SHIFT QUEUE (SLOT 1-3) & INITIALIZE EMPTY SLOTS
// =========================================================================
cmp [rax+0],r14
je exit
cmp [rax+8],r14
je exit
cmp [rax+10],r14
je exit

// Initialize empty slots on start
cmp qword ptr [rax+0],0
jne setup_02
mov [rax+0],r14
jmp save_to_history

setup_02:
cmp qword ptr [rax+8],0
jne setup_03
mov [rax+8],r14
jmp save_to_history

setup_03:
cmp qword ptr [rax+10],0
jne shift_queue
mov [rax+10],r14
jmp save_to_history

// =========================================================================
// 5. SHIFT QUEUE: HIDE OLDEST FLAT & SHIFT SLOTS
// =========================================================================
shift_queue:
mov rbx,[rax+0]            // Get oldest map (Slot 1)
cmp rbx,0
je do_shift
mov byte ptr [rbx+38],00   // Hide oldest map

do_shift:
// Shift positions
mov rbx,[rax+8]
mov [rax+0],rbx            // Slot 2 to Slot 1

mov rbx,[rax+10]
mov [rax+8],rbx            // Slot 3 to Slot 2

mov [rax+10],r14           // Newest map to Slot 3

// =========================================================================
// 6. FORWARD LOOP: SAVE NEW MAP TO HISTORY (SLOT 11 TO 26)
// =========================================================================
save_to_history:
xor rcx,rcx                // Initialize index rcx = 0
find_free_history:
cmp qword ptr [rax+50+rcx*8],0 // Check from lowest slot (50 Hex) upwards
je next_slot_ok            // Jump if empty slot is found
inc rcx                    // Increment index if slot is occupied
cmp rcx,10                 // Max 16 slots (10 Hex)
jl find_free_history
jmp exit

next_slot_ok:
mov [rax+50+rcx*8],r14     // Save flat address to the empty slot

exit:
pop rcx
pop rbx
pop rax
jmp returnhere

"DiabloImmortal.exe"+748302:
jmp newmem
returnhere:
