TILE_LEN	  equ 256
TILE_LEN_W	  equ TILE_LEN
TILE_LEN_H	  equ TILE_LEN
RGB_TILE_SIZE_1N4 equ (TILE_LEN*TILE_LEN*3/4) ;1/4 ç áâì ¯ ¬ï⨠ª à⨭ª¨
RGB_TILE_SIZE_1N2 equ (TILE_LEN*TILE_LEN*3/2) ;1/2 ç áâì ¯ ¬ï⨠ª à⨭ª¨
RGB_TILE_SIZE	  equ TILE_LEN*TILE_LEN*3     ;à §¬¥à 1-© ª à⨭ª¨
TREE_ICON_SYS16_BMP_SIZE equ 256*3*11+54 ;à §¬¥à bmp ä ©«  á á¨á⥬­ë¬¨ ¨ª®­ª ¬¨
TREE_ICON_NOD16_BMP_SIZE equ 256*3* 1+54
TILE_TEXT_MAX_LEN equ 40 ;¬ ªá¨¬ «ì­ ï ¤«¨­­  ⥪áâ  ¤«ï ¢ë¢®¤  ¨¬¥­¨ ä ©« 

FRACT_TILE_00 equ 1 ;¤à®¡­ë© ä ©«, 1/4 - ¢¥àå­¨© «¥¢ë© 㣮«
FRACT_TILE_01 equ 2 ;¤à®¡­ë© ä ©«, 1/4 - ­¨¦­¨© «¥¢ë© 㣮«
FRACT_TILE_10 equ 4 ;¤à®¡­ë© ä ©«, 1/4 - ¢¥àå­¨© ¯à ¢ë© 㣮«
FRACT_TILE_11 equ 8 ;¤à®¡­ë© ä ©«, 1/4 - ­¨¦­¨© ¯à ¢ë© 㣮«
FRACT_TILE_ALL equ 15 ;15=1+2+4+8
FRACT_TILE_BIT00 equ 0
FRACT_TILE_BIT01 equ 1
FRACT_TILE_BIT10 equ 2
FRACT_TILE_BIT11 equ 3

;ä« £¨, ­ã¦­ë¥ ¯à¨ à áâ¢ ­¨¨:
SCALE_0BIT_USE equ 8
SCALE_1BIT_USE equ 9
SCALE_0_USE equ 0x100 ;ä« £ ®âªàëâ¨ï ª à⨭ª¨ ⥪ã饣® ã஢­ï
SCALE_1_USE equ 0x200 ;ä« £ ®âªàëâ¨ï ª à⨭ª¨ ¢ëá襣® ã஢­ï (¡ã¤¥â à áâ¢ âáï ¢ ¯à®¯à¨á®¢ª¥)

met_fr_opn db 0 ;ä« £ ®âªàëâ¨ï ä ©«  á ¬¥âª ¬¨

size_tile_struc equ 32 ;à §¬¥à áâàãªâãàë â ©« 
max_tiles_cols	equ 4 ;¬ ªá¨¬ «ì­®¥ ç¨á«® â ©«®¢ ¯® è¨à¨­¥ ®ª­ 
max_tiles_rows	equ 3 ;¬ ªá¨¬ «ì­®¥ ç¨á«® â ©«®¢ ¯® ¢ëá®â¥ ®ª­ 
max_tiles_count equ (max_tiles_cols*max_tiles_rows) ;¬ ªá¨¬ «ì­®¥ ç¨á«® â ©«®¢ ¢ ¯à®£à ¬¬¥

tile_coord_x	equ dword[edi] ;ª®®à¤¨­ â  x (ª ª ¯ à ¬¥âà ¤«ï § £à㧪¨)
tile_coord_y	equ dword[edi+ 4] ;ª®®à¤¨­ â  y (ª ª ¯ à ¬¥âà ¤«ï § £à㧪¨)
tile_zoom	equ  byte[edi+ 8] ;¬ áèâ ¡ § £à㦠¥¬®£® â ©« 
tile_err_f	equ  byte[edi+ 9] ;ª®¤ ®è¨¡ª¨ ¯à¨ ®âªàë⨨ ¨§®¡à ¦¥­¨ï
tile_drobn	equ  word[edi+10] ;¯®«¥ ¤«ï á®åà ­¥­¨ï १ã«ìâ â®¢ ®âªàëâ¨ï ¤à®¡­ëå ª à⨭®ª
tile_offs_l_load  equ 12 ;offset params last load tile
tile_key_coord_x  equ dword[edi+12] ;ª®®à¤¨­ â  x (ª ª ª«îç ¯®á«¥¤­¥© § £à㧪¨)
tile_key_coord_y  equ dword[edi+16] ;ª®®à¤¨­ â  y (ª ª ª«îç ¯®á«¥¤­¥© § £à㧪¨)
tile_key_path_len equ  byte[edi+21] ;¤«¨­­  ⥪áâ  ¢ ¯®«¥ Cache
tile_rgb_icon	equ dword[edi+22] ;㪠§ â¥«ì ­  ¡ãä¥à ¤«ï åà ­¥­¨ï ®á­®¢­®© ¨«¨ à áâï­ã⮩ ª à⨭ª¨
tile_drobn_icon equ dword[edi+26] ;㪠§ â¥«ì ­  ¡ãä¥à ¤«ï åà ­¥­¨ï 4-å ¤à®¡­ëå ª à⨭®ª
tile_delta_x	equ  byte[edi+30] ;ᤢ¨£ â ©«  ®â­®á¨â¥«ì­® ªãàá®à  ¯® ®á¨ x
tile_delta_y	equ  byte[edi+31] ;ᤢ¨£ â ©«  ®â­®á¨â¥«ì­® ªãàá®à  ¯® ®á¨ y


; *** ¯®à冷ª ¢ë§®¢  ä㭪権 ¯à¨ § £à㧪¥ â ©«®¢ ***
;load_tile
;       get_tile_path
;       load_new_tile (⥪ãé ï ª à⨭ª )
;       get_tile_path
;       load_new_tile (­¨¦­ïï ª à⨭ª )
;       get_tile_path
;       load_new_tile (­¨¦­ïï ª à⨭ª )
;       get_tile_path
;       load_new_tile (­¨¦­ïï ª à⨭ª )
;       get_tile_path
;       load_new_tile (­¨¦­ïï ª à⨭ª )
;       get_parent_tiles (横«¨ç¥áª¨© ¯à®á¬®âà த¨â¥«ì᪨å â ©«®¢)
;               get_tile_path
;               load_new_tile (¢¥àå­ïï ª à⨭ª )


;¯¥à¥¤¢¨£ ¥¬ ª®®à¤¨­ âã ªãàá®à  ¢¯à ¢®
align 4
CursorMoveRight:
push ebx ecx
	xor ebx,ebx
	xor ecx,ecx
	mov cl,byte[map.zoom]
	dec ecx
	cmp cx,0
	jle .no_move ;¯à¨ 0-¬ ¬ áèâ ¡¥ ­¥ ¤¢¨£ ¥¬áï
	@@:
		rol ebx,1
		or ebx,1 ;­ ª ¯«¨¢ ¥¬ ¬ ªá¨¬ «ì­ãî ª®®à¤¨­ âã, ¯à¨ ¤ ­®¬ ¬ áèâ ¡¥
		loop @b
	cmp dword[map.coord_x],ebx
	jge .no_move ;¥á«¨ ª®®à¤¨­ â  ªà ©­ïï, ­¥ ¤¢¨£ ¥¬áï
		inc dword[map.coord_x]
		;ᤢ¨£ ¥¬ á¥âªã â ©«®¢ ¢«¥¢®, çâ®-¡ë ç áâì â ©«®¢ ᮢ¯ «¨ ¨ ¯à¨è«®áì ¬¥­ìè¥ § £à㦠âì ­®¢ëå
		stdcall tiles_grid_move_left, tile_00,max_tiles_count,max_tiles_cols
		call but_Refresh
	.no_move:
pop ecx ebx
	ret

;¯¥à¥¤¢¨£ ¥¬ ª®®à¤¨­ âã ªãàá®à  ¢­¨§
align 4
CursorMoveDown:
push ebx ecx
	xor ebx,ebx
	xor ecx,ecx
	mov cl,byte[map.zoom]
	dec ecx
	cmp cx,0
	jle .no_move ;¯à¨ 0-¬ ¬ áèâ ¡¥ ­¥ ¤¢¨£ ¥¬áï
	@@:
		rol ebx,1
		or ebx,1 ;­ ª ¯«¨¢ ¥¬ ¬ ªá¨¬ «ì­ãî ª®®à¤¨­ âã, ¯à¨ ¤ ­®¬ ¬ áèâ ¡¥
		loop @b
	cmp dword[map.coord_y],ebx
	jge .no_move ;¥á«¨ ª®®à¤¨­ â  ªà ©­ïï, ­¥ ¤¢¨£ ¥¬áï
		inc dword[map.coord_y]
		;ᤢ¨£ ¥¬ á¥âªã â ©«®¢ ¢¢¥àå
		stdcall tiles_grid_move_up, tile_00,max_tiles_count,max_tiles_rows
		call but_Refresh
	.no_move:
pop ecx ebx
	ret

;¯¥à¥¬¥é¥­¨¥ ¬¥âª¨ ¢¢¥àå, ¯® ᯨáªã
align 4
but_met_up:
	stdcall dword[tl_node_move_up], tree2
	ret

;¯¥à¥¬¥é¥­¨¥ ¬¥âª¨ ¢­¨§, ¯® ᯨáªã?
align 4
but_met_dn:
	stdcall dword[tl_node_move_down], tree2
	ret

;¬¥­ï¥¬ à áè¨à¥­¨¥ ¤«ï ª àâë
align 4
fun_new_map_type:
push eax
	stdcall dword[tl_node_get_data], tree1
	cmp eax,0
	je @f
		mov dword[txt_tile_type],eax
	@@:
pop eax
	ret

;§ ¤ ¥¬ à á¯®«®¦¥­¨¥ â ©«®¢ ¯® á¥âª¥
align 4
proc tiles_init_grid uses eax ebx ecx edx edi, beg_tiles:dword, count:dword, cols:dword
	xor eax,eax
	xor ebx,ebx
	mov edi,[beg_tiles]
	mov ecx,[count]
	mov edx,[cols]
	@@:
		mov tile_delta_x,al
		mov tile_delta_y,bl
		add edi,size_tile_struc
		inc ax
		cmp eax,edx
		jl .old_lin
			xor eax,eax
			inc bx
		.old_lin:
		loop @b
	ret
endp

;ᤢ¨£ ¥¬ á¥âªã ¢«¥¢®, ¢ë«¥§è¨¥ á⮫¡æë ¡ã¤ãâ ¯¥à¥ª¨­ãâë ­  ¯à ¢ãî áâ®à®­ã
;äã­ªæ¨ï ­ã¦­  ¤«ï ®¯â¨¬¨§ æ¨¨ ®¡à é¥­¨ï ª ¤¨áªã, ¯à¨ ¤¢¨¦¥­¨¨ ªãàá®à  ¬ë ¤¢¨£ ¥¬
;â ©«ë ¢ ¯à®â¨¢®¯®«®¦­ãî áâ®à®­ã, ¢ १ã«ìâ â¥ ª®®à¤¨­ âë ¡®«ì設á⢠ â ©«®¢ ­¥ ¬¥­ïîâáï
;  §­ ç¨â ¯à®£à ¬¬  ­¥ ¡ã¤¥â ¯¥à¥ç¨â뢠âì ¨å § ­®¢®, 祬 ¨ ¤®á⨣ ¥âáï ®¯â¨¬¨§ æ¨ï
align 4
proc tiles_grid_move_left uses eax ebx ecx edx edi, beg_tiles:dword, count:dword, cols:dword
	xor eax,eax
	xor ebx,ebx
	mov edi,[beg_tiles]
	mov ecx,[count]
	mov edx,[cols]
	@@:
		cmp tile_delta_x,0
		jne .no_jump
			add tile_delta_x,dl ;¥á«¨ á⮫¡¥æ = 0, â® ¤®¡ ¢«ï¥¬ 'cols' çâ®-¡ë ­¥ ¢ë« §¨« ¢«¥¢®
		.no_jump:
		dec tile_delta_x
		add edi,size_tile_struc
		loop @b
	ret
endp

;ᤢ¨£ ¥¬ á¥âªã ¢¯à ¢®, ¢ë«¥§è¨¥ á⮫¡æë ¡ã¤ãâ ¯¥à¥ª¨­ãâë ­  «¥¢ãî áâ®à®­ã
;¤¥« ¥â ⮦¥ çâ® ¨ tiles_grid_move_left ⮫쪮 ¤¢¨£ ¥â ª®®à¤¨­ âë ¢ ¤àã£ãî áâ®à®­ã
align 4
proc tiles_grid_move_right uses eax ebx ecx edx edi, beg_tiles:dword, count:dword, cols:dword
	xor eax,eax
	xor ebx,ebx
	mov edi,[beg_tiles]
	mov ecx,[count]
	mov edx,[cols]
	@@:
		inc tile_delta_x
		cmp tile_delta_x,dl
		jl .no_jump
			mov tile_delta_x,0 ;¥á«¨ á⮫¡¥æ = 'cols', â® áâ ¢¨¬ ¥¬ã 0 çâ®-¡ë ­¥ ¢ë« §¨« ¢¯à ¢®
		.no_jump:
		add edi,size_tile_struc
		loop @b
	ret
endp

align 4
proc tiles_grid_move_up uses eax ebx ecx edx edi, beg_tiles:dword, count:dword, rows:dword
	xor eax,eax
	xor ebx,ebx
	mov edi,[beg_tiles]
	mov ecx,[count]
	mov edx,[rows]
	@@:
		cmp tile_delta_y,0
		jne .no_jump
			add tile_delta_y,dl ;¥á«¨ áâப  = 0, â® ¤®¡ ¢«ï¥¬ 'rows' çâ®-¡ë ­¥ ¢ë« §¨«  ¢­¨§
		.no_jump:
		dec tile_delta_y
		add edi,size_tile_struc
		loop @b
	ret
endp

align 4
proc tiles_grid_move_down uses eax ebx ecx edx edi, beg_tiles:dword, count:dword, rows:dword
	xor eax,eax
	xor ebx,ebx
	mov edi,[beg_tiles]
	mov ecx,[count]
	mov edx,[rows]
	@@:
		inc tile_delta_y
		cmp tile_delta_y,dl
		jl .no_jump
			mov tile_delta_y,0 ;¥á«¨ áâப  = 'rows', â® áâ ¢¨¬ ¥© 0 çâ®-¡ë ­¥ ¢ë« §¨«  ¢¢¥àå
		.no_jump:
		add edi,size_tile_struc
		loop @b
	ret
endp

align 4
proc array_tile_function uses ecx edi, beg_tiles:dword, count:dword, funct:dword
	mov edi,[beg_tiles]
	mov ecx,[count]
	@@:
		stdcall [funct],edi
		add edi,size_tile_struc
		loop @b
	ret
endp

align 4
draw_tiles:
pushad
	movzx ecx,byte[map.zoom]
	mov esi,[sc.work_text]
	or  esi,0x40000000
	mov edi,[sc.work]
	mcall SF_DRAW_NUMBER,0x20000,, (128 shl 16)+13

	stdcall array_tile_function, tile_00,max_tiles_count,draw_tile
popad
	ret

; edi - pointer tile struct
;output:
; eax - 7
; edx - coordinates to tile draw

align 4
proc tile_clear_area, height:dword
pushad
	mov cx,dx
	shl ecx,16
	mov cx,word[height]

	shr edx,16
	mov bx,dx
	shl ebx,16
	mov bx,TILE_LEN_W

	mov edx,0xffffd0
	mcall SF_DRAW_RECT
popad
	ret
endp

;­ à¨á®¢ âì ®¤¨­ tile
align 4
proc draw_tile, tile:dword
pushad
	mov edi,[tile]

;*** set base point ***
	mov edx,5*65536+35
	movzx eax,tile_delta_y
	shl ax,8 ;*256
	add edx,eax
	movzx eax,tile_delta_x
	shl eax,8+16
	add edx,eax
;*** set base point ***

	mov ebx,dword[procinfo.box.width] ;¯à®¢¥à塞 ¢« §¨â «¨ ª à⨭ª  ¯® ®á¨ x
	sub ebx,min_window_w-5 ;5 - ®âáâ㯠᫥¢  ¤«ï 1-© ª à⨭ª¨
	shl ebx,16
	mov bx,dx ;­¨¦­ïï ª®®à¤¨­ â  ¯®ª  ­¥ ãç¨â뢠¥âáï, ¯®â®¬ã ¤¥« ¥¬ ¨å à ¢­ë¬¨
	cmp edx,ebx
	jg .end_f

	;¯à®¢¥à塞 ¢« §¨â «¨ ª à⨭ª  ¯® ®á¨ y
	mcall SF_STYLE_SETTINGS,SSF_GET_SKIN_HEIGHT ;eax = ¢ëá®â  ᪨­ 
	mov ecx,dword[procinfo.box.height]
	mov ebx,edx
	and ebx,0xffff ;ebx = ª®®à¤¨­ â  'y' ¤«ï ¢ë¢®¤  â ©« 
	add eax,ebx
	add eax,5+5 ;5-à ¬ª 

	cmp ecx,eax
	jle .end_f

	sub ecx,eax
	cmp ecx,TILE_LEN_H
	jle @f
		mov ecx,TILE_LEN_H
	@@:

	mov eax,SF_PUT_IMAGE
	bt tile_drobn,SCALE_0BIT_USE
	jc .draw
	bt tile_drobn,SCALE_1BIT_USE
	jc .draw
	jmp @f
		.draw:
		mov ebx,tile_rgb_icon
		or ecx,TILE_LEN_W*65536 ;§ ¤ ­¨¥ è¨à¨­ë ¤«ï 楫®© ª à⨭ª¨
		int 0x40 ;¢ë¢®¤ ­  íªà ­ 楫®© ¨«¨ 㢥«¨ç¨­®© ª à⨭ª¨
		jmp .end_clear_tile
	@@:
		cmp tile_drobn,FRACT_TILE_ALL
		je .end_clear_tile
			stdcall tile_clear_area, ecx ;ç¨á⪠ ®¡« á⨠⠩« 
	.end_clear_tile:

	cmp tile_drobn,0
	je .info ;¥á«¨ ­¥â ­¨ ®¤­®© ¤à®¡­®© ª à⨭ª¨

	mov si,cx ;si - ¢ëá®â  ­¨¦­¥© ¤à®¡­®© ª à⨭ª¨
	sub si,TILE_LEN_H/2 ;si - ¢ëá®â  ­¨¦­¥© ¤à®¡­®© ª à⨭ª¨
	cmp cx,TILE_LEN_H/2
	jl @f
		mov cx,TILE_LEN_H/2 ;§ ¤ ­¨¥ ¢ëá®âë ¤«ï ¢¥àå­¥© ¤à®¡­®© ª à⨭ª¨
	@@:

	and ecx,0xffff ;áâ¨à ­¨¥ áâ à®© è¨à¨­ë
	or ecx,(TILE_LEN_W/2)*65536 ;§ ¤ ­¨¥ è¨à¨­ë ¤«ï ¤à®¡­®© ª à⨭ª¨
	mov ebx,tile_drobn_icon
	bt tile_drobn,FRACT_TILE_BIT00
	jae @f
		int 0x40
	@@:
	add dx,TILE_LEN_H/2
	add ebx,RGB_TILE_SIZE_1N4
	bt tile_drobn,FRACT_TILE_BIT01
	jae @f
		xchg cx,si
		int 0x40
		xchg cx,si
	@@:

	sub dx,TILE_LEN_H/2
	ror edx,16
	add dx,TILE_LEN_W/2
	ror edx,16
	add ebx,RGB_TILE_SIZE_1N4
	bt tile_drobn,FRACT_TILE_BIT10
	jae @f
		int 0x40
	@@:

	add dx,TILE_LEN_H/2
	add ebx,RGB_TILE_SIZE_1N4
	bt tile_drobn,FRACT_TILE_BIT11
	jae @f
		xchg cx,si
		int 0x40
	@@:
	jmp .end_f

	.info: ;¢ë¢®¤ ¨­ä®à¬ æ¨¨, ¯à¨ ­¥ 㤠筮¬ ®âªàë⨨ ª à⨭®ª
	bt tile_drobn,SCALE_0BIT_USE
	jc .end_f
	bt tile_drobn,SCALE_1BIT_USE
	jc .end_f
		mov ebx,edx
		add ebx,0x30003
		mov edx,tile_rgb_icon ;¯à¨ ­¥ã¤ ç­®¬ ®âªàë⨨ ¢ tile_rgb_icon ¯ãâì ª ä ©«ã
;;;    mov dword[edx+RGB_TILE_SIZE_1N4],0 ;áâ ¢¨¬ ­ã«¨, ­  á«ãç © ¥á«¨ ¢ áâப¥ á ¨¬¥­¥¬ ä ©«  ¬ãá®à
		stdcall [str_len],tile_rgb_icon,RGB_TILE_SIZE_1N4
		mov edi,eax

		mov eax,SF_DRAW_TEXT
		mov ecx,0xff
;or ecx,0x80000000
		mov esi,TILE_TEXT_MAX_LEN

	@@:
		cmp edi,TILE_TEXT_MAX_LEN
		jg .no_line_last
			mov esi,edi
			int 0x40
			jmp .end_f
		.no_line_last:
		int 0x40
		add edx,esi
		sub edi,esi
		add ebx,10 ;®¯ã᪠¥¬áï ­  ­¨¦­îî áâபã
		jmp @b
	.end_f:
popad
	ret
endp

;㢥«¨ç¥­¨¥ ¬ áèâ ¡ 
align 4
but_ZoomP:
	cmp byte[map.zoom],24
	jge @f
		inc byte[map.zoom]
		shl dword[map.coord_x],1
		shl dword[map.coord_y],1
		call but_Refresh
	@@:
	ret

;㬥­ì襭¨¥ ¬ áèâ ¡ 
align 4
but_ZoomM:
	cmp byte[map.zoom],1
	jle @f
		dec byte[map.zoom]
		shr dword[map.coord_x],1
		shr dword[map.coord_y],1
		call but_Refresh
	@@:
	ret

;á®åà ­¥­¨¥ ä ©«  á ¬¥âª ¬¨
align 4
but_MetSave:
	stdcall dword[tl_save_mem], tree2,0,[bmp_icon],TREE_ICON_SYS16_BMP_SIZE

	copy_path fn_metki,sys_path,file_name,0
	push eax ebx ecx edi
		stdcall dword[tl_get_mem_size], tree2,[bmp_icon]
		inc eax ;¤«ï § ¯¨á¨ ¢ ä ©« § ¢¥àè î饣® ­ã«ï

		m2m [run_file_70.Buffer], [bmp_icon]
		mov [run_file_70.Function], SSF_CREATE_FILE
		mov [run_file_70.Position], 0
		mov [run_file_70.Flags], 0
		mov [run_file_70.Count], eax
		mov byte[run_file_70+20], 0
		mov [run_file_70.FileName], file_name
		mcall SF_FILE,run_file_70
	pop edi ecx ebx eax
	jmp still

;áç¨â뢠¥¬ ä ©« á ¬¥âª ¬¨
align 4
but_MetLoad:
	cmp byte[met_fr_opn],0
	jne .end_f
	copy_path fn_metki,sys_path,file_name,0
	pushad
		mov byte[met_fr_opn],1
		mov [run_file_70.Function], SSF_READ_FILE
		mov [run_file_70.Position], 0
		mov [run_file_70.Flags], 0
		mov [run_file_70.Count], TREE_ICON_SYS16_BMP_SIZE
		mov byte[run_file_70+20], 0
		mov [run_file_70.FileName], file_name
		m2m [run_file_70.Buffer], [bmp_icon]
		mcall SF_FILE,run_file_70
		cmp ebx,0xffffffff
		je @f
			stdcall [tl_load_mem], tree2,0,[bmp_icon],TREE_ICON_SYS16_BMP_SIZE
			stdcall [tl_cur_beg], tree2
			stdcall [tl_draw], tree2
		@@:
		mov byte[file_name],0 ;clear file name
	popad
	.end_f:
	ret

;¤®¡ ¢«¥­¨¥ ¬¥âª¨
align 4
but_MetAdd:
push eax
	mov eax,dword[bmp_icon]
	m2m dword[eax],  dword[map.coord_x]
	m2m dword[eax+4],dword[map.coord_y]
	m2m  word[eax+8], word[map.zoom]
	add eax,12
	stdcall dword[str_copy], eax,30,dword[edit2.text],30
	stdcall dword[tl_node_add], tree2, 0, dword[bmp_icon] 
	stdcall dword[tl_draw], tree2
pop eax
	jmp still

;¯¥à¥å®¤ ª ¢ë¡à ­­®© ¬¥âª¥
align 4
fun_goto_met:
	push eax
	stdcall dword[tl_node_get_data], tree2
	cmp eax,0
	je @f ;ªãàá®à ­¥ á⮨⠭  ¬¥âª¥
		m2m dword[map.coord_x],dword[eax]
		m2m dword[map.coord_y],dword[eax+4]
		m2m word[map.zoom],word[eax+8]
		call but_Refresh
	@@:
	pop eax
	ret

;®¡­®¢¨âì ª àâã
align 4
but_Refresh:
	call fun_new_map_type ;¯à®¢¥à塞 ­¥ ¯®¬¥­ï«áï «¨ ⨯ ¯à®á¬ âਢ ¥¬ëå ä ©«®¢

	stdcall array_tile_function, tile_00,max_tiles_count,load_tile
	stdcall draw_tiles
	ret

align 4
proc tile_init uses eax edi, tile:dword
	mov edi,[tile]
	stdcall mem.Alloc, RGB_TILE_SIZE+300
	mov tile_rgb_icon,eax
	stdcall mem.Alloc, RGB_TILE_SIZE
	mov tile_drobn_icon,eax
	ret
endp

align 4
proc tile_destroy uses edi, tile:dword
	mov edi,[tile]
	stdcall mem.Free,tile_rgb_icon
	stdcall mem.Free,tile_drobn_icon
	ret
endp


;§ £à㦠¥â â ©«ë, ¢ ®¯à¥¤¥«¥­­®© ®¡« áâ¨
align 4
proc load_tile uses eax ebx ecx edi, tile:dword
  mov edi,dword[tile] ;ª®¯¨à㥬 㪠§ â¥«ì ­  áâàãªâãàã â ©« , çâ®-¡ë à ¡®â «¨ ª®­áâ ­âë

  stdcall mem_copy, map,edi, 9 ;ª®¯¨à㥬 ª®®à¤¨­ âë ªãàá®à  ¢ áâàãªâãàã â ©« 
  movzx eax,tile_delta_x
  add tile_coord_x,eax ;ᤢ¨£ ¥¬ ª®®à¤¨­ âë â ©« , ®â­®á¨â¥«ì­® ªãàá®à 
  mov al,tile_delta_y
  add tile_coord_y,eax
  stdcall get_tile_path,edi    ;¡¥à¥¬ ¯ãâì ª â ©«ã

;***
  ; ¯à®¢¥àª  ª®®à¤¨­ â ¯®á«¥¤­¥© § £à㦥­­®© ª à⨭ª¨
  mov eax,edi
  mov ecx,9
  @@:
    mov bl,byte[eax+tile_offs_l_load]
    cmp byte[eax],bl
    jne .st_load
    inc eax
    loop @b
  mov ebx,dword[edit1.size];¯à®¢¥à塞 â ª¦¥ ⨯ ª àâ, ª®â®àë© ¡ë« à ­¥¥ § £à㦥­
  cmp tile_key_path_len,bl ;¯à¨ ¨§¬¥­¥­¨¨ ⨯  ª àâë ­ã¦­® â ª¦¥ ¯¥à¥ç¨â뢠âì ä ©«ë
  jne .st_load

  jmp @f    ;¥á«¨ ª®®à¤¨­ âë ᮢ¯ «¨ ⮠㦥 ­¥ § £à㦠¥¬
  .st_load: ;start load tile

  ; ãáâ ­®¢ª  ­®¢ëå ª®®à¤¨­ â ¤«ï ¯®á«¥¤­¥© § £à㦥­­®© ª à⨭ª¨
  mov eax,edi
  add eax,tile_offs_l_load
  stdcall mem_copy, edi,eax,9
  mov ebx,dword[edit1.size]
  mov tile_key_path_len,bl
;***

  ;mov tile_drobn,0
  mov tile_drobn,SCALE_0_USE ;áâ ¢¨¬ ä« £ ®âªàëâ¨ï ª à⨭ª¨ ⥪ã饣® ã஢­ï
  stdcall load_new_tile,edi
  cmp tile_err_f,0
  je @f ;¥á«¨ 㤠筮 ®âªà뫨 ⥪ã騩 ä ©« â® ®áâ «ì­ë¥ ¤¥©áâ¢¨ï ¯à®¯ã᪠¥¬
;  cmp tile_err_f,6
;  je @f
    mov tile_drobn,0
    bt dword[ch2.flags],1 ;¯à®¢¥à塞 ®¯æ¨î '¡à âì á­¨§ã'
    jae .opt_drobn_false

    ;¥á«¨ ä ©« ­¥ ­ ©¤¥­, ¯®¯ ¤ ¥¬ á
    inc tile_zoom
    shl tile_coord_y,1
    shl tile_coord_x,1
    stdcall get_tile_path,edi
    stdcall load_new_tile,edi

    cmp tile_err_f,0
    jne .no_tile_00 ;¥á«¨ ­¥ã¤ ç­® ®âªà뫨 â® ­¥ 㬥­ìè ¥¬ ¨§®¡à ¦¥­¨¥
      or tile_drobn,FRACT_TILE_00
; 1 ᦠ⨥ ª à⨭ª¨ ¯® ¢ëá®â¥
      stdcall img_rgb_hdiv2, tile_rgb_icon, TILE_LEN*TILE_LEN, TILE_LEN
; 2 ᦠ⨥ ª à⨭ª¨ ¯® è¨à¨­¥
      stdcall img_rgb_wdiv2, tile_rgb_icon, TILE_LEN*TILE_LEN/2
; 3 ª®¯¨à®¢ ­¨¥ 1-© ª à⨭ª¨ ¢ १¥à¢­ë© ¡ãä¥à
      stdcall mem_copy, tile_rgb_icon, tile_drobn_icon, RGB_TILE_SIZE_1N4
    .no_tile_00:

    ;¥á«¨ ä ©« ­¥ ­ ©¤¥­, ¯®¯ ¤ ¥¬ á
    inc tile_coord_y
    stdcall get_tile_path,edi
    stdcall load_new_tile,edi

    cmp tile_err_f,0
    jne .no_tile_01 ;¥á«¨ ­¥ã¤ ç­® ®âªà뫨 â® ­¥ 㬥­ìè ¥¬ ¨§®¡à ¦¥­¨¥
;      mov byte[map.drobn],0
      or tile_drobn,FRACT_TILE_01
; 1 ᦠ⨥ ª à⨭ª¨ ¯® ¢ëá®â¥
      stdcall img_rgb_hdiv2, tile_rgb_icon, TILE_LEN*TILE_LEN, TILE_LEN
; 2 ᦠ⨥ ª à⨭ª¨ ¯® è¨à¨­¥
      stdcall img_rgb_wdiv2, tile_rgb_icon, TILE_LEN*TILE_LEN/2
; 3 ª®¯¨à®¢ ­¨¥ 2-© ª à⨭ª¨ ¢ १¥à¢­ë© ¡ãä¥à
      mov eax,tile_drobn_icon
      add eax,RGB_TILE_SIZE_1N4 ; ᤢ¨£ ª à⨭ª¨ ¢­¨§ (â. ª. ®­  ­¨¦­ïï)
      stdcall mem_copy, tile_rgb_icon, eax, RGB_TILE_SIZE_1N4
    .no_tile_01:

    ;¥á«¨ ä ©« ­¥ ­ ©¤¥­, ¯®¯ ¤ ¥¬ á
    dec tile_coord_y
    inc tile_coord_x
    stdcall get_tile_path,edi
    stdcall load_new_tile,edi

    cmp tile_err_f,0
    jne .no_tile_10 ;¥á«¨ ­¥ã¤ ç­® ®âªà뫨 â® ­¥ 㬥­ìè ¥¬ ¨§®¡à ¦¥­¨¥
;      mov byte[map.drobn],0
      or tile_drobn,FRACT_TILE_10
; 1 ᦠ⨥ ª à⨭ª¨ ¯® ¢ëá®â¥
      stdcall img_rgb_hdiv2, tile_rgb_icon, TILE_LEN*TILE_LEN, TILE_LEN
; 2 ᦠ⨥ ª à⨭ª¨ ¯® è¨à¨­¥
      stdcall img_rgb_wdiv2, tile_rgb_icon, TILE_LEN*TILE_LEN/2
; 3 ª®¯¨à®¢ ­¨¥ 3-© ª à⨭ª¨ ¢ १¥à¢­ë© ¡ãä¥à
      mov eax,tile_drobn_icon
      add eax,RGB_TILE_SIZE_1N2 ; ᤢ¨£ ª à⨭ª¨ ¢­¨§ (â. ª. ®­  ­¨¦­ïï)
      stdcall mem_copy, tile_rgb_icon, eax, RGB_TILE_SIZE_1N4
    .no_tile_10:

    ;¥á«¨ ä ©« ­¥ ­ ©¤¥­, ¯®¯ ¤ ¥¬ á
    inc tile_coord_y
    stdcall get_tile_path,edi
    stdcall load_new_tile,edi

    cmp tile_err_f,0
    jne .no_tile_11 ;¥á«¨ ­¥ã¤ ç­® ®âªà뫨 â® ­¥ 㬥­ìè ¥¬ ¨§®¡à ¦¥­¨¥
;      mov byte[map.drobn],0
      or tile_drobn,FRACT_TILE_11
; 1 ᦠ⨥ ª à⨭ª¨ ¯® ¢ëá®â¥
      stdcall img_rgb_hdiv2, tile_rgb_icon, TILE_LEN*TILE_LEN, TILE_LEN
; 2 ᦠ⨥ ª à⨭ª¨ ¯® è¨à¨­¥
      stdcall img_rgb_wdiv2, tile_rgb_icon, TILE_LEN*TILE_LEN/2
; 3 ᤢ¨£ ª à⨭ª¨ ¢­¨§ ¡¥§ ª®¯¨à®¢ ­¨ï (â. ª. ®­  ­¨¦­ïï)
      stdcall img_rgb_hoffs, tile_rgb_icon, TILE_LEN*TILE_LEN/4, TILE_LEN/2, TILE_LEN*3/2

      ;ª®¯¨à㥬 ¯®á«¥¤­îî ¤à®¡­ãî ª à⨭ªã ¢ ¡ãä¥à drobn_icon
      ;¢ ª®â®à®¬ 㦥 ¡ë«¨ áä®à¬¨à®¢ ­ë 3 ¤à㣨¥
      mov eax,tile_drobn_icon
      add eax,RGB_TILE_SIZE_1N4*3
      stdcall mem_copy, tile_rgb_icon, eax, RGB_TILE_SIZE_1N4
    .no_tile_11:
  .opt_drobn_false:


	bt dword[ch1.flags],1 ;¯à®¢¥à塞 ®¯æ¨î '¡à âì ᢥàåã'
	jae @f
	cmp tile_drobn,FRACT_TILE_ALL ;¥á«¨ ᮡ࠭  ¢áï ª à⨭ª , ⮠ᢥàåã 㦥 ­¥ ¡¥à¥¬
	je @f
		;­¥ §­ ¥¬ ¡ë« -«¨ ¢ª«î祭  ®¯æ¨ï '¡à âì á­¨§ã', ¤«ï ª®â®à®© ¨§¬¥­ï«¨áì ª®®à¤¨­ âë
		;¯®â®¬ã ®¯ïâì ª®¯¨à㥬 ª®®à¤¨­ âë ªãàá®à  ¢ áâàãªâãàã â ©« 
		stdcall mem_copy, map,edi, 9
		movzx eax,tile_delta_x
		add tile_coord_x,eax ;ᤢ¨£ ¥¬ ª®®à¤¨­ âë â ©« , ®â­®á¨â¥«ì­® ªãàá®à 
		mov al,tile_delta_y
		add tile_coord_y,eax

		xor ecx,ecx
		.zoom:
		cmp tile_zoom,0
		je @f ;¯à®á¬ âਢ ¥âáï á ¬ ï ¢¥àå­ïï ª à⨭ª , ¢ë室¨¬
		call get_parent_tiles ;横«¨ç¥áª¨© ¯à®á¬®âà த¨â¥«ì᪨å â ©«®¢
		bt tile_drobn,SCALE_1BIT_USE
		jc @f
		cmp ecx,5 ;5 - ®£à ­¨ç¥­¨¥ ­  á⥯¥­ì à áâ¢ ­¨ï ª à⨭ª¨
		je @f
		jmp .zoom
	@@:

	ret
endp

;output:
;ecx - if not file = count parent levels else = 0
align 4
proc get_parent_tiles
  dec tile_zoom
  shr tile_coord_y,1
  shr tile_coord_x,1
  inc ecx

  stdcall get_tile_path,edi ;¡¥à¥¬ ¯ãâì ª â ©«ã
  stdcall load_new_tile,edi
  cmp tile_err_f,0
  jne @f ;¥á«¨ ­¥ã¤ ç­® ®âªà뫨 â® ­¥ à áâ¢ ¥¬ ¨§®¡à ¦¥­¨¥
    or tile_drobn,SCALE_1_USE
    mov ebx,ecx
  .zoom_cycle:
    xor eax,eax
    dec ebx
    bt tile_key_coord_y,ebx
    jae .scale_bot_l
      add eax,RGB_TILE_SIZE_1N2
    .scale_bot_l:
    bt tile_key_coord_x,ebx
    jae .scale_rig_l
      add eax,TILE_LEN_W*3/2
    .scale_rig_l:
    cmp eax,0
    je .scale_top_left
      add eax,tile_rgb_icon
      stdcall mem_copy, eax, tile_rgb_icon, RGB_TILE_SIZE_1N2
    .scale_top_left:

    ;à áâ¢ ­¨¥ ¢¥àå­¥© ª à⨭ª¨
    stdcall img_rgb_wcrop, tile_rgb_icon,TILE_LEN_W,TILE_LEN_W/2,TILE_LEN_H ;®â१ ¥¬ «¨è­¥¥
    stdcall img_rgb_wmul2, tile_rgb_icon,RGB_TILE_SIZE_1N4/3 ;à á⨣¨¢ ¥¬ ¯® è¨à¨­¥
    stdcall img_rgb_hmul2, tile_rgb_icon,RGB_TILE_SIZE_1N2/3,TILE_LEN_W ;à áâ¢ ¥¬ ¯® ¢ëá®â¥
  loop .zoom_cycle
  @@:
  ret
endp

;tile - 㪠§ â¥«ì ­  áâàãªâãàã â ©« 
align 4 ;§ £à㦠¥â ®¯à¥¤¥«¥­­ë© â ©«
proc load_new_tile, tile:dword
  pushad

  mov [run_file_70.Function], SSF_READ_FILE
  mov [run_file_70.Position], 0
  mov [run_file_70.Flags], 0
  mov [run_file_70.Count], RGB_TILE_SIZE+300 ;+54
  m2m [run_file_70.Buffer], [bmp_icon]
  mov byte[run_file_70+20], 0
  mov [run_file_70.FileName], file_name
  mcall SF_FILE,run_file_70
  mov edi,[tile]
;  cmp ebx,0xffffffff
;  je .no_load
  cmp al,6 ;®è¨¡ª  6 - ª®£¤  ä ©« § ª®­ç¨«áï à ­ìè¥ ç¥¬ ¡ãä¥à
  jne @f
    xor al,al ;­ ¬ íâ  ®è¨¡ª  ­¥ áâà è­ , ¯®â®¬ã áâ ¢¨¬ ª®¤ à ¢­ë¬ 0, ª ª ¯à¨ ®¡ëç­®¬ ®âªàë⨨
  @@:
  mov tile_err_f,al
  cmp al,0
  jne .no_load

  stdcall dword[img_decode], dword[bmp_icon],ebx,0
  mov dword[data_icon],eax
  stdcall dword[img_to_rgb2], dword[data_icon],tile_rgb_icon
  stdcall dword[img_destroy], dword[data_icon]
    jmp @f
  .no_load:

  mov al,byte[map.zoom]
  cmp tile_zoom,al
  jne @f
    mov eax,tile_rgb_icon
    mov bl,tile_err_f
    add bl,'0' ;bl - ª®¤ ®è¨¡ª¨ ®âªàëâ¨ï
    mov byte[eax],bl
    inc eax
    mov byte[eax],' '
    inc eax
    stdcall mem_copy, file_name,eax,250 ;ª®¯¨à㥬 ¯ãâì ª ä ©«ã ¯à¨ ®è¨¡®ç­®¬ ®âªàë⨨
  @@:
  popad
  ret
endp

;äã­ªæ¨ï £¥­¥à¨àã¥â ç áâì ¯ã⨠ª ä ©«ã ¢ ¢¨¤¥: \z?\?\x?\?\y?
align 4
proc get_tile_path uses eax ecx edi esi, tile:dword
	mov ecx,300
	mov edi,txt_tile_path
	@@:
		mov byte[edi],0 ;ç¨á⨬ áâபã
		inc edi
		loop @b

	mov esi,[tile] ;¡¥à¥¬ 㪠§ â¥«ì

	mov ecx,0x80000000 ;rol 0x80000000,1 = 1
	movzx eax,byte[esi+8] ;+8 = zoom
	.calc_coord:
		rol ecx,1 ;ecx = maximum coord
		dec ax
		cmp ax,0
		jg .calc_coord

	mov byte[txt_tile_path],'z'
	mov edi,txt_tile_path
	inc edi
	movzx eax,byte[esi+8] ;+8 = zoom
	stdcall convert_int_to_str, 300

	inc edi
	cmp byte[esi+8],9 ;+8 = zoom
	jle @f
		inc edi
	@@:
	mov byte[edi],'/'
	inc edi

	mov eax,[esi] ;coord_x
cmp eax,ecx
jge .out_of_rang ;coord_x ¡®«ìè¥ ¤®¯ãá⨬®£® §­ ç¥­¨ï ¯à¨ § ¤ ­­®¬ ¬ áèâ ¡¥
	shr eax,10
	stdcall convert_int_to_str, 300

	@@:
		inc edi
		cmp byte[edi],'0'
		jge @b
	mov byte[edi],'/'
	inc edi
	mov byte[edi],'x'
	inc edi

	mov eax,[esi] ;coord_x
	stdcall convert_int_to_str, 300

	@@:
		inc edi
		cmp byte[edi],'0'
		jl @f
		cmp byte[edi],'9'
		jg @f
		jmp @b
	@@:

	;coord y
	mov byte[edi],'/'
	inc edi

	mov eax,[esi+4] ;coord_y
cmp eax,ecx
jge .out_of_rang ;coord_y ¡®«ìè¥ ¤®¯ãá⨬®£® §­ ç¥­¨ï ¯à¨ § ¤ ­­®¬ ¬ áèâ ¡¥
	shr eax,10
	stdcall convert_int_to_str, 300

	@@:
		inc edi
		cmp byte[edi],'0'
		jge @b
	mov byte[edi],'/'
	inc edi
	mov byte[edi],'y'
	inc edi

	mov eax,[esi+4] ;coord_y]
	stdcall convert_int_to_str, 300

.out_of_rang:
	call get_all_path
	ret
endp

align 4
get_all_path: ;äã­ªæ¨ï ¤«ï ᮡ¨à ­¨ï ¯®«­®£® ¯ã⨠¨§ ¢á¥å ªã᪮¢
	mov ecx,[edit1.text]
	add ecx,[edit1.size]
	dec ecx

	stdcall [str_copy], file_name,4096-1, [edit1.text],[edit1.max]

	cmp byte[ecx],'/'
	je @f
		stdcall [str_catchar],file_name,4096-1, dword '/'
	@@:

	stdcall [str_cat], file_name,4096-1, txt_tile_path,300
	stdcall [str_cat], file_name,4096-1, [txt_tile_type],10
	ret