var literal_speed=3, speed=3
var mrgreen="#1FBE16", mrred="#D42222", mrgrey="#525352"
var curnt_digit=0, digit=[0,0] //digit[1]=10*n, digit[0]=n

var map=new Array()
var iteminfo=new Array()
var stage_code=new Array()
var now_stage=0
//map[x][y]

map=[	[0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0],
		[1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1],
		
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],
		[1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1],

		[1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1]
		
	]

iteminfo=[99,99,99,99,99]

var object=[0,"skalip.2083302","prodg81.문1","prodg81.창문","prodg81.종1","유리블록","유리블록","유리블록",8,"도착"]
var ringing=0

var item=["happy74virus.화살표1","happy74virus.화살표2","happy74virus.화살표4","happy74virus.화살표3","O","skalip.2083302","prodg81.문1","prodg81.창문","prodg81.종1","유리블록","유리블록","유리블록",8,"도착"]
var direction_list=[0,2,1,3,4]

var ur_item_number=0
var ur_item=item[ur_item_number]
var start_px=8, start_py=9, start_pd=0, end_px=24, end_py=9, wall_color=2
var game_stop=0, game_win=0, next_stage=0, jumped=0
var stop_message=""

var arrow=["→","↑","←","↓"]
var port_x=[[0,0],[0,0],[0,0]]
var port_y=[[0,0],[0,0],[0,0]]
var port_color=["red","yellow","green"]

var now_x, now_y, now_d

var wall=["skalip.2083302","skalip.2083304","skalip.2083301", "skalip.2083303","skalip.2083201"] //주황,초록,빨강,노랑,검정
var wall_color=2 

var mapcode=""
var now_state=0, num=-1
var temp_mapcode="", replace_start=0, dot_num=0, length=0
var iteminfo_input=[0,0,0,0,0]
var iteminfo_code=""
var temp_decode="", ix=1, iy=1, temp_arr=new Array()

var item_count=[0,0,0,0,0,""]

var gamemode="stage"

stage_code[0]="10aY-eZ10-aaaa-aa00-00"
stage_code[1]="f6g1-2Y7g-1bg1-bg53-Z82c-ccca-0000"
stage_code[2]="bag1-bg1b-g14Y-5g5Z-14g1-bg1b-gbcc-ccca-0000"
stage_code[3]="b8g2-g17g-2g17-g2g1-2Y3g-2g3Z-12g2-g17g-2g17-g2gb-accc-ca00-00"
stage_code[4]="7eg1-g18g-1g18-g1eg-18g1-g13Y-3g6Z-13g1-g1bg-18g1-bg1g-18g1-g81f-fffa-0000"
stage_code[5]="9cAg-1ag1-bg1b-g14Y-5g5Z-14g1-bg1b-g1aA-g9ea-aaab-0000"
stage_code[6]="b8g2-g17g-2g17-g2g1-2Y3g-2g4Z-11g2-g17g-2gd7-aaaa-c000-0"
stage_code[7]="49Ag-12i2-h0h1-9g13-i2h0-h18A-g4dY-5Z6a-h5i1-3g1g-1g14-g1i1-g12g-3g3g-11i4-h26h-hhhh-0000"
stage_code[8]="b9g1-bg6j-10g4-gfY4-g4g2-Z11g-4g15-g6j1-0gbd-bbbb-b000-0"
stage_code[9]="b1hj-8g2g-1g1g-1g1g-1g8g-2g1g-1g4g-1g8g-Y1g4-g1g3-Zg8g-5g4g-1g1g-8g2g-1g1g-1g1g-1g8h-jb2f-fffa-0000"
stage_code[10]="1g6g-2agb-geg7-Y2g5-k3gb-gegb-g10h-0h5g-4g1b-g1bg-1bg1-1n1g-2m4g-bgeg-bgah-eg3g-fg5g-3g1Z-cgag-aj0g-2m0g-fg1d-g11k-kkkk-0000"

stage_code[11]="dg1b-g1bg-1bg1-bg1b-g1bg-1bg1-bg1b-g15Y-3Ag3-Z15g-1bg1-bg1b-g1bg-1bg1-bg1b-gdaa-aac0-000"
stage_code[12]="9ao1-3g6g-13g2-Z2g1-3g6g-13g6-g13o-14g4-g15g-4g15-g1Y1-g15m-17Ag-19Ag-19i2-9ccc-cc00-30"
stage_code[13]="7ei1-9gZg-19Ag-16n1-4gY2-Ag14-j0Ag-17Bg-17Bg-17Bg-17g2-g17k-80bb-bbb0-000"
stage_code[14]="d2n1-4gY0-hZ0g-14h0-g2g1-5Ag2-g15A-g2g1-5Ag2-g15A-g2g1-5m80-cccc-d000-0"
stage_code[15]="b7m1-4g6g-13g1-i1g1-3Ah0-ZAg1-3Dg1-3AgY-0g1g-13g1-i1g1-3g6g-14m7-fbbb-bd00-00"
stage_code[16]="10bj-6jdg-Y0g6-AZgd-j6jd-1ccc-cc00-00"
stage_code[17]="d4m1-5g4g-15AY-0Z0g-15g4-g15m-33Ag-1g15-h0h1-9g2g-15h0-h0g1-6Ag1-g28h-hhhh-0000"
stage_code[18]="dfi8-hg2g-7AYb-h0Z0-g7hg-2g18-icfc-cccc-0000"
stage_code[19]="edgb-gch0-g9l8-gY1g-7h2Z-g8h0-g9la-gbgb-6bbb-bc00-00"
stage_code[20]="98g1-cBg1-6h0A-g13i-1Bg1-2gY7-h4Za-i1Bg-15h0-Ag17-Bg16-ga4c-accd-0000"

stage_code[21]="9dg1-bg18-U1g1-bg14-Y5S7-Z12g-1bg1-bg1b-g9fc-abba-0001"
stage_code[22]="d2U1-g1U1-g15g-4g10-Y3S4-S3Z1-0g4g-15g4-gd5f-fffa-0001"
stage_code[23]="9bg2-g14U-1AU0-g1U1-4g2g-3keg-2g3g-2g9Y-3S2T-3S0Z-0geg-2g3g-2geg-2g3k-eg2g-17g2-g9dk-kkka-0001"
stage_code[24]="b8g6-kfUg-4m15-h2h9-YbS0-Z0h1-5h2h-eUg4-meg6-kb2c-acbb-0001"
stage_code[25]="c2ie-gTST-g4kd-TU0U-T3h2-h8Y2-S0U0-S3SS-0Z0h-cTU0-UT3h-2hcg-TSTg-4k18-ib2i-iiii-0001"
stage_code[26]="10aY-eZ10-aaaa-aa00-00"
stage_code[27]="10aY-eZ10-aaaa-aa00-00"
stage_code[28]="10aY-eZ10-aaaa-aa00-00"
stage_code[29]="10aY-eZ10-aaaa-aa00-00"
stage_code[30]="dg1b-g1bg-1bg1-bg1b-g50o-12hY-3U0h-2Zei-Sk52-g1bg-1bg1-bg1b-g1bg-dbbb-bc00-01"

stage_code[31]="10aY-eZ10-aaaa-aa00-00"
stage_code[32]="10aY-eZ10-aaaa-aa00-00"
stage_code[33]="10aY-eZ10-aaaa-aa00-00"
stage_code[34]="10aY-eZ10-aaaa-aa00-00"
stage_code[35]="10aY-eZ10-aaaa-aa00-00"
stage_code[36]="10aY-eZ10-aaaa-aa00-00"
stage_code[37]="10aY-eZ10-aaaa-aa00-00"
stage_code[38]="10aY-eZ10-aaaa-aa00-00"
stage_code[39]="10aY-eZ10-aaaa-aa00-00"
stage_code[40]="10aY-eZ10-aaaa-aa00-00"

stage_code[41]="10aY-eZ10-aaaa-aa00-00"
stage_code[42]="10aY-eZ10-aaaa-aa00-00"
stage_code[43]="10aY-eZ10-aaaa-aa00-00"
stage_code[44]="10aY-eZ10-aaaa-aa00-00"
stage_code[45]="10aY-eZ10-aaaa-aa00-00"
stage_code[46]="10aY-eZ10-aaaa-aa00-00"
stage_code[47]="10aY-eZ10-aaaa-aa00-00"
stage_code[48]="10aY-eZ10-aaaa-aa00-00"
stage_code[49]="10aY-eZ10-aaaa-aa00-00"
stage_code[50]="10aY-eZ10-aaaa-aa00-00"

stage_code[51]="10aY-eZ10-aaaa-aa00-00"
stage_code[52]="10aY-eZ10-aaaa-aa00-00"
stage_code[53]="10aY-eZ10-aaaa-aa00-00"
stage_code[54]="10aY-eZ10-aaaa-aa00-00"
stage_code[55]="10aY-eZ10-aaaa-aa00-00"
stage_code[56]="10aY-eZ10-aaaa-aa00-00"
stage_code[57]="10aY-eZ10-aaaa-aa00-00"
stage_code[58]="10aY-eZ10-aaaa-aa00-00"
stage_code[59]="10aY-eZ10-aaaa-aa00-00"
stage_code[60]="10aY-eZ10-aaaa-aa00-00"

stage_code[61]="10aY-eZ10-aaaa-aa00-00"
stage_code[62]="10aY-eZ10-aaaa-aa00-00"
stage_code[63]="10aY-eZ10-aaaa-aa00-00"
stage_code[64]="10aY-eZ10-aaaa-aa00-00"
stage_code[65]="10aY-eZ10-aaaa-aa00-00"
stage_code[66]="10aY-eZ10-aaaa-aa00-00"
stage_code[67]="10aY-eZ10-aaaa-aa00-00"
stage_code[68]="10aY-eZ10-aaaa-aa00-00"
stage_code[69]="10aY-eZ10-aaaa-aa00-00"
stage_code[70]="10aY-eZ10-aaaa-aa00-00"

stage_code[71]="10aY-eZ10-aaaa-aa00-00"
stage_code[72]="10aY-eZ10-aaaa-aa00-00"
stage_code[73]="10aY-eZ10-aaaa-aa00-00"
stage_code[74]="10aY-eZ10-aaaa-aa00-00"
stage_code[75]="10aY-eZ10-aaaa-aa00-00"
stage_code[76]="10aY-eZ10-aaaa-aa00-00"
stage_code[77]="10aY-eZ10-aaaa-aa00-00"
stage_code[78]="10aY-eZ10-aaaa-aa00-00"
stage_code[79]="10aY-eZ10-aaaa-aa00-00"
stage_code[80]="10aY-eZ10-aaaa-aa00-00"

stage_code[81]="10aY-eZ10-aaaa-aa00-00"
stage_code[82]="10aY-eZ10-aaaa-aa00-00"
stage_code[83]="10aY-eZ10-aaaa-aa00-00"
stage_code[84]="10aY-eZ10-aaaa-aa00-00"
stage_code[85]="10aY-eZ10-aaaa-aa00-00"
stage_code[86]="10aY-eZ10-aaaa-aa00-00"
stage_code[87]="10aY-eZ10-aaaa-aa00-00"
stage_code[88]="10aY-eZ10-aaaa-aa00-00"
stage_code[89]="10aY-eZ10-aaaa-aa00-00"
stage_code[90]="10aY-eZ10-aaaa-aa00-00"

stage_code[91]="10aY-eZ10-aaaa-aa00-00"
stage_code[92]="10aY-eZ10-aaaa-aa00-00"
stage_code[93]="10aY-eZ10-aaaa-aa00-00"
stage_code[94]="10aY-eZ10-aaaa-aa00-00"
stage_code[95]="10aY-eZ10-aaaa-aa00-00"
stage_code[96]="10aY-eZ10-aaaa-aa00-00"
stage_code[97]="10aY-eZ10-aaaa-aa00-00"
stage_code[98]="10aY-eZ10-aaaa-aa00-00"
stage_code[99]="10aY-cZ14-T1aS-d9d-fi0-000"


stage_code[100]="g1ag-7fT4-fYeZ-30U0-V0W0-X2cj-Sn32-U0V0-W0X5-eg1a-gcdf-in00-00"

//■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■//

change_speed(-1)
hide_object_count()
//r2.hide()
r2.set_text(1,19,"ⓞ",mrgrey)
to_stage()

//■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■//

/*
■■■■■
■□□□■
■□□□■
■□□□■
■■■■■ : 인터페이스/시스템 구현 공간
*/

lclick_cell(1,12,"input_speed(-1)")
lclick_cell(1,10,"input_speed(1)")

lclick_cell(25,20,"to_stage()")
lclick_cell(26,20,"to_sandbox()")
lclick_cell(27,20,"to_custom(1)")

lclick_cell(1,1,"game_start()")

lclick_cell(14,20,"load_stage(digit[1]*10+digit[0])")

function input_speed(x) {
	if (x>0) literal_speed=up_lim(12,literal_speed+x)
	else literal_speed=dn_lim(1,literal_speed+x)
	
	if (literal_speed==11) speed=0, r2.set_text(1,11,"+")
	else if (literal_speed==12) speed=-1, r2.set_text(1,11,"∞")
	else speed=literal_speed, r2.set_text(1,11,speed)
}

function iteminforming(itemnumber) {
	if (gamemode=="sandbox") {
		r2.set_text(itemnumber*2+16,20,'X')
	} else {
		r2.set_text(itemnumber*2+16,20,iteminfo[itemnumber])
	}
}

function all_iteminforming() {
	for (k=0 ; k<=4 ; k++) iteminforming(k)
}



function to_stage() {
	gamemode="stage"
	decode(stage_code[now_stage])
	
	all_iteminforming()
	selecting(0)
	
	for (i=2 ; i<=11 ; i++) {
		r2.space_jump(i,20)
		r2.pick_object()
		r2.clear_color()
		r2.put_object("흰바둑알")
		r2.set_text(i-2)
	}
	if (r2.have_object("출발")==true) {
		r2.space_jump(14,20)
		r2.put_object("출발")
	}
	
	to_digit(1)
	
	for (i=1 ; i>=0 ; i--) {
		selecting_S_or_input_digit(digit[i]+2)
	}
	to_digit(1)
	
	gamemode_informing()
	return_place(r2)
}

function to_sandbox() {
	gamemode="sandbox"
	decode(stage_code[0])
	
	iteminfo=[99,99,99,99,99]
	all_iteminforming()
	r2.clear_text(1,19-ur_item_number)
	
	for (i=2 ; i<=11 ; i++) {
		r2.space_jump(i,20)
		r2.clear_text()
		r2.pick_object()
		if (i==2) {
			r2.put_object("흰바둑알")
			continue
		}
		if (i>=7 && i<=9) r2.set_color(port_color[i-7])
		if (i==10) {
			if(r2.have_object("출발")==0) {
				r2.space_jump(14,20)
				r2.pick_object()
				r2.space_jump(i,20)
			}
			r2.put_object("출발")
		}
		else r2.put_object(object[i-2])
	}
	
	ur_item_number=3
	selecting_S_or_input_digit(3)
	r2.set_text(2,20,arrow[start_pd],mrgreen)
	
	for(i=12 ; i<=13 ; i++) r2.set_text(i,20,0)
	r2.set_color(13,20,"skyblue")
	r2.clear_color(12,20)
	
	gamemode_informing()
	return_place(r2)
}

function to_custom(popup_on) {
	gamemode="custom"
	selecting(0)
	
	for (i=2 ; i<=11 ; i++) {
		r2.space_jump(i,20)
		r2.pick_object()
		r2.clear_color()
		r2.put_object("흰바둑알")
		r2.set_text(i-2)
	}
	
	if (r2.have_object("출발")==true) {
		r2.space_jump(14,20)
		r2.put_object("출발")
	}
	
	decode(stage_code[0])
	
	for(i=12 ; i<=13 ; i++) r2.set_text(i,20,0)
	r2.set_color(13,20,"skyblue")
	r2.clear_color(12,20)
	
	gamemode_informing()
	return_place(r2)
	
	if(popup_on==1) r1.say("아래에 맵 코드를 입력해주세요")
}

function load_stage(num) {
	if (gamemode=="stage") {
		now_stage=num
		decode(stage_code[now_stage])
		r2.set_text(1,4,(now_stage-(now_stage%10))/10)
		r2.set_text(1,3,now_stage%10)
	}
}

function gamemode_informing() {
	set_direction(r2,3)
	
	for (i=0 ; i<8 ; i++) {
		r2.space_jump(1,9-i)
		r2.clear_text()
		if (i==7) continue
		r2.open_door()
	}

	if (gamemode=="stage") {
		for (i=0 ; i<5 ; i++) {
			r2.set_text(1,9-i,"STAGE".charAt(i))
		}
		r2.set_text(1,4,(now_stage-(now_stage%10))/10)
		r2.set_text(1,3,now_stage%10)
		r2.space_jump(1,4)
		set_direction(r2,1)
		r2.close_door()
	} else if (gamemode=="sandbox") {
		for (i=0 ; i<7 ; i++) {
			r2.set_text(1,9-i,"SANDBOX".charAt(i))
		}
	} else if (gamemode=="custom") {
		for (i=0 ; i<6 ; i++) {
			r2.set_text(1,8-i,"CUSTOM".charAt(i))
		}
		r2.space_jump(1,8)
		set_direction(r2,1)
		r2.close_door()
	}
	
	r2.space_jump(1,3)
	set_direction(r2,3)
	r2.close_door()
	set_direction(r2,0)
}
		

function game_start() {
	r1.space_jump(start_px,start_py)
	set_direction(r1,start_pd)
	r2.set_color(1,1,mrgreen)
	
	game_stop=0, game_win=0, next_stage=0, jumped=0, ringing=0
	
	playbot_on_move()
	
	if (game_stop==1) {
		delay()
		r1.say(stop_message)
		set_direction(r1,start_pd)
		r1.space_jump(start_px,start_py)
	} 
	
	if (game_win==1) {
		popup("승리!")
		if(gamemode=="stage") {
			next_stage=1
		}
	}
	
	set_direction(r1,start_pd)
	r1.space_jump(start_px,start_py)
	
	if (ringing%2==1) open_n_close()
	
	r2.set_color(1,1,mrred)
	
	if (next_stage==1 && now_stage!=99) {
		next_stage=0
		now_stage++
		all_clear()
		decode(stage_code[now_stage])
		r2.set_text(1,4,(now_stage-(now_stage%10))/10)
		r2.set_text(1,3,now_stage%10)
		digit[0]=now_stage%10
		digit[1]=(now_stage-digit[0])/10
		r2.set_text(12,20,digit[1])
		r2.set_text(13,20,digit[0])
	}
	
	set_direction(r2,0)
}

//■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■//

/*
□□□□■
□□□□■
□□□□■
□□□□■
□□□□■ : 맵/아이템 구현 공간
*/

for (j=1 ; j<=19 ; j++) {
	for (i=2 ; i<=30 ; i++) {
		lclick_cell(i,j,"putting(CX,CY)")
		rclick_cell(i,j,"changing()")
	}
}

for (j=15 ; j<=19 ; j++) {
	lclick_cell(1,j,"selecting(19-CY)")
}

for (i=15 ; i<=23 ; i+=2) {
	lclick_cell(i,20,"selecting_iteminfo((CX-15)/2)")
}

for (i=2 ; i<=11 ; i++) {
	lclick_cell(i,20,"selecting_S_or_input_digit(CX)")
}

lclick_cell(12,20,"to_digit(1)")
lclick_cell(13,20,"to_digit(0)")

lclick_cell(29,20,"all_clear()")

function putting(x,y) {
	r2.space_jump(x,y)
	
	if (r2.on_object()==false) {
		if (ur_item_number!=12) {
			if (ur_item_number<5) {
				put_arrow(x,y,ur_item_number)
				iteminforming(ur_item_number)
			} else {
				r2.put_object(item[ur_item_number])
			}
		}
		
		if (ur_item_number>=5) {
			map[x][y]=ur_item_number-4
		}
		
		if (ur_item_number==5) {
			for (k=0 ; k<4 ; k++) {
				if (r2.front_is_wall()==false) {
					r2.close_door()
				}
				r2.turn_left()
			}
		} else if (ur_item_number>=9 && ur_item_number<=11) {
			r2.set_color(port_color[ur_item_number-9])
			port_x[ur_item_number-9].push(x)
			port_y[ur_item_number-9].push(y)
			if(arrlen(port_x[ur_item_number-9])>2) {
				r2.space_jump(port_x[ur_item_number-9][0],port_y[ur_item_number-9][0])
				r2.pick_object()
				r2.clear_color()
				map[port_x[ur_item_number-9][0]][port_y[ur_item_number-9][0]]=0
				port_x[ur_item_number-9].shift()
				port_y[ur_item_number-9].shift()
			}
		} else if (ur_item_number==12) {
			r2.clear_color(start_px,start_py)
			map[start_px][start_py]=0
			map[x][y]=8
			start_px=x
			start_py=y
			r1.space_jump(start_px,start_py)
			r2.set_color(start_px,start_py,mrgreen)
		} else if (ur_item_number==13) {
			r2.space_jump(end_px,end_py)
			r2.pick_object()
			r2.clear_color()
			map[end_px][end_py]=0
			map[x][y]=9
			end_px=x
			end_py=y
			r2.set_color(end_px,end_py,mrred)
		}
	} else {
		r2wt_ob=r2.what_object()
		for (k=0 ; k<=4 ; k++) {
			if(r2wt_ob==item[k]) {
				pick_arrow(x,y,k)
				iteminforming(k)
			}
		}
		
		if (gamemode=="sandbox") {
			if(map[r2.get_x()][r2.get_y()]!=8 && map[r2.get_x()][r2.get_y()]!=9) {
				if(r2.on_object()==1) r2.pick_object()
				map[x][y]=0
			}
			
			if (r2wt_ob==object[1]) {
				for (k=0 ; k<=3 ; k++) {
					if (front_is_what_item(x,y,k,1)!=1) {
						r2.open_door()
					}
					r2.turn_left()
				}
			} else if (r2wt_ob==object[5]) {
				for (k=0 ; k<=2 ; k++) {
					if(r2.get_color()==port_color[k]) {
						if (x==port_x[k][0] && y==port_y[k][0]) {
							port_x[k].shift()
							port_y[k].shift()
						} else {
							port_x[k].pop()
							port_y[k].pop()
						}
					}
				}
				r2.clear_color()
			}
		}
	}
	return_place(r2)
}

function put_arrow(x,y,num) {
	if (gamemode=="sandbox" || iteminfo[num]>0) {
		r2.space_jump(1,19-num)
		if (r2.count_object()>1) {
			r2.pick_object()
			if (gamemode!="sandbox") {
				iteminfo[num]--
			}
			r2.space_jump(x,y)
			r2.put_object(item[num])
		} else {
			if (gamemode=="sandbox") {
				r1.say("화살표/점프대는 최대 99개까지 밖에 놓을 수 없습니다")
			}
			r2.space_jump(x,y)
		}
	}
	
}

function pick_arrow(x,y,num) {
	r2.pick_object()
	r2.space_jump(1,19-num)
	r2.put_object(item[num])
	if (gamemode!="sandbox") {
		iteminfo[num]++
	}
	r2.space_jump(x,y)
}

function changing() {
	if (ur_item_number<5) {
		r2.clear_text(1,19-ur_item_number)
	} else {
		r2.clear_text(ur_item_number-2,20)
	}
	
	if (gamemode=="sandbox") {
		ur_item_number=mod(0,13,ur_item_number+1)
	} else {
		ur_item_number=mod(0,4,ur_item_number+1)
	}
	
	ur_item=item[ur_item_number]
	
	if (ur_item_number<5) {
		r2.set_text(1,19-ur_item_number,"ⓞ",mrgrey)
	} else {
		if (ur_item_number==5 || ur_item_number==9 || ur_item_number==11) {
			r2.set_text(ur_item_number-2,20,"ⓞ","silver")
		} else { 
			r2.set_text(ur_item_number-2,20,"ⓞ",mrgrey)
		}
	}
	
}

function selecting(x) {
	if (ur_item_number<5) {
		r2.clear_text(1,19-ur_item_number)
	} else {
		r2.clear_text(ur_item_number-2,20)
	}
	ur_item_number=x
	ur_item=item[ur_item_number]
	r2.set_text(1,19-ur_item_number,"ⓞ",mrgrey)
}

function selecting_iteminfo(x) {
	if (ur_item_number<5) {
		r2.clear_text(1,19-ur_item_number)
	} else {
		r2.clear_text(ur_item_number-2,20)
	}
	ur_item_number=x
	ur_item=item[ur_item_number]
	r2.set_text(1,19-ur_item_number,"ⓞ",mrgrey)
}

function selecting_S_or_input_digit(x) {
	if (gamemode=="sandbox") {
		if (x==2) {
			change_start_direction()
		} else if (x==3 && ur_item_number==5) {
			wall_color=mod(0,4,wall_color+1)
			change_wall_color(wall_color)
			r2.space_jump(3,20)
			r2.pick_object()
			r2.put_object(object[1])
			return_place(r2)
		} else {
			if (ur_item_number<5) {
				r2.clear_text(1,19-ur_item_number)
			} else {
				r2.clear_text(ur_item_number-2,20)
			}
			ur_item_number=x+2
			if (x==3 || x==7 || x==9) {
				r2.set_text(ur_item_number-2,20,"ⓞ","silver")
			} else {
				r2.set_text(ur_item_number-2,20,"ⓞ",mrgrey)
			}
		}
	} else if (gamemode=="stage") {
		digit[curnt_digit]=x-2
		r2.set_text(13-curnt_digit,20,digit[curnt_digit])
		if (curnt_digit==1) {
			to_digit(0)
		}
	}
}

function to_digit(x) {
	if (gamemode=="stage") {
		curnt_digit=x
		r2.set_color(13-x,20,"skyblue")
		r2.clear_color(x+12,20)
	}
}

function change_start_direction() {
	start_pd=mod(0,3,start_pd+1)
	r2.set_text(2,20,arrow[start_pd],mrgreen)
	r1.turn_left()
}

function change_wall_color(color_num) {
	object[1]=wall[color_num]
	item[5]=wall[color_num]
	
	for (l=1 ; l<=19 ; l++) {
		for (k=2 ; k<=30 ; k++) {
			if (map[k][l]==1) {
				r2.space_jump(k,l)
				r2.pick_object()
				r2.put_object(object[1])
			}
		}
	}
	return_place(r2)
}


function all_mapping() {
	start_px=8, start_py=9, end_px=24, end_py=9
	for (i=0 ; i<=2 ; i++) {
		repeat("port_x[i].shift()",2)
		repeat("port_y[i].shift()",2)
	}
	
	for (j=1 ; j<=19 ; j++) {
		for (i=2 ; i<=30 ; i++) {
			mapping(i,j)
		}
	}
	
	change_wall_color(wall_color)
	return_place(r2)
}


function all_clear() {
	if (gamemode=="sandbox") {
		for (j=1 ; j<=19 ; j++) {
			for (i=2 ; i<=30 ; i++) {
				if (map[i][j]!=8 && map[i][j]!=9) {
					map[i][j]=0
				}
			}
		}
		all_mapping()
	} else {
		for (j=1 ; j<=19 ; j++) {
			for (i=2 ; i<=30 ; i++) {
				r2.space_jump(i,j)
				for (k=0 ; k<5 ; k++) {
					if (r2.what_object()==item[k]) {
						pick_arrow(i,j,k)
					}
				}
			}
		}
		all_iteminforming()
		return_place(r2)
	}
}

//■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■//

/*
■■■■■
□□□□■
■■■■■
■□□□□
■■■■■ : 플레이봇 구현 공간
*/

function playbot_on_move() {
	while (1) {
		now_x=r1.get_x()
		now_y=r1.get_y()
		now_d=r1.get_direction()
	
		if (r1.on_object()==true) {
			interacting(r1.what_object(),now_x,now_y,now_d)
		}
		
		if (game_stop==1 || game_win==1) {
			break
		}
		
		if (jumped==1) {
			jumped=0
			continue
		}
		
		if (front_is_unmovable_item(now_x,now_y,now_d,1)==1) {
			game_stop=1
			stop_message="앞으로 갈 수 없음!"
		} else {
			delay()
			r1.move()
		}
		
		if (game_stop==1 || game_win==1) {
			break
		}
	}
}

function interacting(itemname,x,y,d) {
	switch (itemname) {
			case(item[0]):
			case(item[1]):
			case(item[2]):
			case(item[3]):
				for (i=0 ; i<=3 ; i++) {
					if (item[direction_list[i]]==itemname) {
						if(d!=i) {
							delay()
							set_direction(r1,i)
							now_d=i
						}
					}
				}
				break
			case(item[4]):
				if (front_is_unmovable_item(x,y,d,2)==true) {
					game_stop=1
					stop_message="점프할 수 없음!"
				} else {
					delay()
					jump(x,y,d)
					jumped=1;
				}
				break
			case(object[4]):
				smart_delay(up_lim(10,speed+3))
				open_n_close()
				ringing++
				break
			case(object[5]):
			case(object[6]):
			case(object[7]):
				for (l=0 ; l<=2 ; l++) {
					if (r1.get_color()==port_color[l]) itemname=l+5
				}
				for (l=1 ; l<=19 ; l++) {
					for (k=2 ; k<=30 ; k++) {
						if (map[k][l]==itemname) {
							if (k!=x || l!=y) {
								now_x=k , now_y=l
								r1.space_jump(now_x,now_y)
								delay()
							}
						}
					}
				}
				break
			case(object[9]):
				game_win=1
				break
		}	
}

function front_is_what_item(x,y,d,r) {
	return map[x+(d+1)%2*(!((d-d%2)/2)*2-1)*r][y+d%2*(!((d-d%2)/2)*2-1)*r]
}

function front_is_unmovable_item(x,y,d,r) {
	fiui=front_is_what_item(x,y,d,r)
	if(fiui==1 || fiui==2) return 1
	else return 0
}

function jump(x,y,d) {
	r1.space_jump(x+(d+1)%2*(!((d-d%2)/2)*2-1)*2,y+d%2*(!((d-d%2)/2)*2-1)*2)
	now_x=r1.get_x()
	now_y=r1.get_y()
}

function delay() {
	change_speed(speed)
	r2.space_jump(1,20)
	change_speed(-1)
}

function smart_delay(spd) {
	change_speed(spd)
	r2.space_jump(1,20)
	change_speed(-1)
}

function open_n_close() {
	for (o=1 ; o<=19 ; o++) {
		for (p=2 ; p<=30 ; p++) {
			if (map[p][o]==2) {
				map[p][o]=3
				mapping(p,o)
			} else if (map[p][o]==3) { 
				map[p][o]=2
				mapping(p,o)
			}
		}
	}
	return_place(r2)
}
//■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■//

/*
■■■■■
□□□□■
■■■■■
□□□□■
■■■■■ : 코드화 구현 공간
*/

lclick_cell(28,20,"encode()")

textbox("mapcode_input","맵의 코드를 입력해주세요(샌드박스,커스텀 전용) ")
button("mapcode_decode","디코딩하기","decode1()")
textbox("mapcode_arrow","인코딩 전용: 맵에 넣을 화살표와 점프대의 개수(최대 99개) ")
button("arrow_count","맵에 있는 화살표/점프대 세기","count_arrow()")
button("mapcode_incode","인코딩하기","encode()")
textbox("mapcode_output","코드화된 맵의 코드 ")
set_form("mapcode_arrow","(예시:2,3,5,8,13)")

function encode() {
	mapcode=""
	now_state=0, num=-1
	temp_mapcode="", replace_start=0, dot_num=0, length=0
	now_state=map[2][1]
	iteminfo_input=[0,0,0,0,0]
	iteminfo_code=""
	
	for (j=1 ; j<=19 ; j++) {
		for (i=2 ; i<=30 ; i++) {
			encode_segm(map[i][j])
		}
	}
	end_of_encode()
	mapcode=replace(mapcode,"g0",".")
	dot_to_Aex()
	mapcode+=iteminfo_encode()
	mapcode+=wall_color
	mapcode=dashing(mapcode,strlen(mapcode))
	
	set_form("mapcode_output",mapcode)
}

function encode_segm(x) {
	if (now_state==x && now_state<2) num++
	else {
		if (now_state==0) {
			mapcode+=to_hexstr(num)
		} else if (now_state==1) { 			
			mapcode+=to_gexstr(to_hexstr(num),strlen(to_hexstr(num)),0)
		} else {
			mapcode+=to_specialstr(now_state)
		}
		now_state=x
		num=0
	}
}

function end_of_encode() {
	if (now_state==0) {
		mapcode+=to_hexstr(num)
	} else if (now_state==1) { 			
		mapcode+=to_gexstr(to_hexstr(num),strlen(to_hexstr(num)),0)
	} else {
		mapcode+=to_specialstr(now_state)
	}
}

function dot_to_Aex() {
	length=strlen(mapcode)
	for (i=0 ; i<length ; i++) {
		if (midstr(mapcode,i,1)=='.') {
			if (replace_start==0) {
				replace_start=1
			}
			dot_num++
		} else {
			if (replace_start==1) {
				temp_mapcode+=leftstr(mapcode,i-dot_num)
				mapcode=rightstr(mapcode,strlen(mapcode)-i)
				i=0
				length=strlen(mapcode)
				
				dot_num=to_hexstr(dot_num-1)
				dot_num=to_Aexstr(dot_num,strlen(dot_num),0)
				temp_mapcode+=dot_num
				
				replace_start=0
				dot_num=0
			}
		}		
	}
	if (replace_start==1) {
		temp_mapcode+=leftstr(mapcode,i-dot_num)
		mapcode=rightstr(mapcode,strlen(mapcode)-i)
		i=0
		length=strlen(mapcode)
		
		dot_num=to_hexstr(dot_num-1)
		dot_num=to_Aexstr(dot_num,strlen(dot_num),0)
		temp_mapcode+=dot_num
		
		replace_start=0
		dot_num=0
	}
	temp_mapcode+=mapcode
	mapcode=temp_mapcode
}

function iteminfo_encode() {
	iteminfo_input=get_form("mapcode_arrow").split(",")
	
	for (i=0 ; i<=4 ; i++) {
		if (mod(0,49,iteminfo_input[i])<25) {
			iteminfo_code+=chr(mod(0,49,iteminfo_input[i])+97)
		} else {
			iteminfo_code+=chr(mod(0,49,iteminfo_input[i])+40) //65-25
		}
	}
	iteminfo_code+=((iteminfo_input[0]>=50)*2+(iteminfo_input[1]>=50)*1)
	iteminfo_code+=((iteminfo_input[2]>=50)*4+(iteminfo_input[3]>=50)*2+(iteminfo_input[4]>=50)*1)
	iteminfo_code+=start_pd
	
	return iteminfo_code
}

function dashing(str,len) {
	for(i=0 ; i<len/4-1 ; i++) {
		str=leftstr(str,(i+1)*4+i)+"-"+rightstr(str,strlen(str)-(i+1)*4-i)
	}
	return str;
}
			
function decode(str) {
	temp_decode="", ix=2, iy=1
	now_state=0
	temp_arr=str.split('-')
	str=""
	for(i=0 ; i<arrlen(temp_arr) ; i++) {
		str+=temp_arr[i]
	}
	
	iteminfo_code=rightstr(str,9)
	str=leftstr(str,strlen(str)-9)

	for(i=0 ; i<strlen(str) ; i++) {
		decode_segm(str.charAt(i))
	}
	end_of_decode()
	iteminfocode_decode(iteminfo_code)
	all_mapping()
	all_iteminforming()
}

function decode1() {
	if (gamemode=="stage") {
		to_custom(0)
	}
	decode(get_form("mapcode_input"))
	if (gamemode=="sandbox") {
		r2.space_jump(2,20)
		r2.clear_text()
		r2.set_text(arrow[start_pd])
		r2.space_jump(3,20)
		r2.pick_object()
		r2.put_object(object[1])
		return_place(r2)
	}
}

function decode_segm(char) {
	if (now_state==decode_identifying(asc(char))) temp_decode+=char
	else {
		if (now_state==0) {
			for(k=0 ; k<=parseInt(temp_decode,16) ; k++) {
				map[ix][iy]=0
				ix++
				if (ix==31) ix=2, iy++
			}
		} else if (now_state==1) {
			temp_decode=degexnate(temp_decode)
			for(k=0 ; k<=parseInt(temp_decode,16) ; k++) {
				map[ix][iy]=1
				ix++
				if (ix==31) ix=2, iy++
			}
		} else if (now_state==2) {
			temp_decode=deAexnate(temp_decode)
			for(k=0 ; k<=parseInt(temp_decode,16) ; k++) {
				map[ix][iy]=1
				ix++
				if (ix==31) ix=2, iy++
				map[ix][iy]=0
				ix++
				if (ix==31) ix=2, iy++
			}
		} else if (now_state==3) {
			temp_arr=temp_decode.split("")
			for (k=0 ; k<arrlen(temp_arr) ; k++) {
				map[ix][iy]=asc(temp_arr[k])-81
				ix++
				if (ix==31) ix=2, iy++
			}
		}
		now_state=decode_identifying(asc(char))
		temp_decode=""
		temp_decode+=char
	}
}

function decode_identifying(num) {
	if (num>=48 && num<=57) return 0;
	if (num>=97 && num<=102) return 0;
	if (num>=103 && num<=118) return 1;
	if (num>=65 && num<=80) return 2;
	if (num>=83 && num<=93) return 3;
}

function degexnate(str) {
	temp_arr=str.split("")
	str=""
	for (k=0 ; k<arrlen(temp_arr) ; k++) {
		str+=to_hexstr(asc(temp_arr[k])-103)
	}
	return str
}

function deAexnate(str) {
	temp_arr=str.split("")
	str=""
	for (k=0 ; k<arrlen(temp_arr) ; k++) {
		str+=to_hexstr(asc(temp_arr[k])-65)
	}
	return str
}

function end_of_decode() {
	if (now_state==0) {
		for(k=0 ; k<=parseInt(temp_decode,16) ; k++) {
			map[ix][iy]=0
			ix++
			if (ix==31) ix=2, iy++
		}
	} else if (now_state==1) {
		temp_decode=degexnate(temp_decode)
		for(k=0 ; k<=parseInt(temp_decode,16) ; k++) {
			map[ix][iy]=1
			ix++
			if (ix==31) ix=2, iy++
		}
	} else if (now_state==2) {
		temp_decode=deAexnate(temp_decode)
		for(k=0 ; k<=parseInt(temp_decode,16) ; k++) {
			map[ix][iy]=1
			ix++
			if (ix==31) ix=2, iy++
			map[ix][iy]=0
			ix++
			if (ix==31) ix=2, iy++
		}
	} else if (now_state==3) {
		temp_arr=temp_decode.split("")
		for (k=0 ; k<arrlen(temp_arr) ; k++) {
			map[ix][iy]=asc(temp_arr[k])-81
			ix++
			if (ix==31) ix=2, iy++
		}
	}
}

function iteminfocode_decode(str) {
	temp_arr=str.split("")
	temp_arr[5]=Number(temp_arr[5]).toString(2)
	temp_arr[6]=Number(temp_arr[6]).toString(2)
	for (i=0 ; i<=1 ; i++) {
		iteminfo[i]=dealphexnate(temp_arr[i])+50*temp_arr[5].charAt(i)
	}
	for(i=2 ; i<=4 ; i++) {
		iteminfo[i]=dealphexnate(temp_arr[i])+50*temp_arr[6].charAt(i-2)
	}
	start_pd=temp_arr[7]
	wall_color=temp_arr[8]
}

function dealphexnate(char) {
	if (asc(char)>90) {
		return asc(char)-97
	} else {
		return asc(char)-40//-65+25
	}
}

function count_arrow() {
	item_count=[0,0,0,0,0,""]
	
	for(j=1 ; j<=19 ; j++) {
		for (i=2 ; i<=30 ; i++) {
			r2.space_jump(i,j)
			if (r2.on_object()==1) {
				for (k=0 ; k<=4 ; k++) {
					if (r2.what_object()==item[k]) {
						item_count[k]++
					}
				}
			}
		}
	}
	
	for (i=0 ; i<=4 ; i++) {
		item_count[5]+=item_count[i]
		if (i==4) continue
		item_count[5]+=","
	}
	
	set_form("mapcode_arrow",item_count[5])
	return_place(r2)
}

function to_hexstr(x) {
	return String(x.toString(16))
} //27->1b->"1b"

function str_hexnate(str) {
	if(str<=10) return parseInt(str)
	else return asc(str)-87
} //"1"->1 "a"->11

function to_gexstr(str,len,num) {
	if(len==0) return ""
	len--
	return chr(str_hexnate(midstr(str,num,1))+103) + to_gexstr(str,len,num+1)
} //0~15 -> g~v

function to_Aexstr(str,len,num) {
	if(len==0) return""
	len--
	return chr(str_hexnate(midstr(str,num,1))+65) + to_Aexstr(str,len,num+1)
} //0~15 -> A~P

function to_specialstr(x) {
	return chr(x+81)
}

//■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■//

/*
■□□□■
■□□□■
■■■■■
□□□□■
□□□□■ : 다목적 함수 구현 공간
*/

function mod(ini,end,x) {
	return x%(end+1)+ini
}

function up_lim(lim,x) {
	return (x<lim)*(x-lim)+lim
}

function dn_lim(lim,x){
	return (x>lim)*(x-lim)+lim
}

function turn_right(name){
	name.turn_left()
	name.turn_left()
	name.turn_left()
}

function set_direction(name,direction) {
	while(name.get_direction()!=direction) name.turn_left()
}

function return_place(name) {
	name.space_jump(1,20)
}

function cube(x,num) {
	if (num==0) return 1
	return x*cube(x,num-1)
}

function mapping(x,y) {
	r2.space_jump(x,y)
	if (r2.on_object()==1) {
		if(r2.what_object()==object[1]) {
			for (k=0 ; k<=3 ; k++) {
				if (front_is_what_item(x,y,k,1)!=1) {
					r2.open_door()
				}
				r2.turn_left()
			}
		}
		
		for (k=0 ; k<5 ; k++) {
			if (r2.what_object()==item[k]) {
				r2.pick_object()
				r2.space_jump(1,19-k)
				r2.put_object(item[k])
				r2.space_jump(x,y)
				break
			}
		}
		if (r2.on_object()==1) r2.pick_object()
	}
	r2.clear_color()
	
	switch (map[x][y]) {
		case(0): 
			break
		case(1):
			for (k=0 ; k<=3 ; k++) {
				if (r2.front_is_wall()==0) {
					r2.close_door()
				}
				r2.turn_left()
			}
			r2.put_object(object[1])
			break
		case(5):
		case(6):
		case(7):
			r2.set_color(port_color[map[x][y]-5])
			r2.put_object(object[map[x][y]])
			port_x[map[x][y]-5].push(x)
			port_y[map[x][y]-5].push(y)
			break
		case(8):
			r2.set_color(mrgreen)
			r1.space_jump(x,y)
			set_direction(r1,start_pd)
			start_px=x
			start_py=y
			break
		case(9):
			r2.set_color(mrred)
			end_px=x
			end_py=y
		default:
			r2.put_object(object[map[x][y]])
	}
}

/* function print_map(mapname) {
	var mapstr=""
	for(j=19; j>=1 ; j--) {
		for(i=2 ; i<=30 ; i++) {
			mapstr+=mapname[i][j]
		}
		mapstr+="\n" 
	}
	print(mapstr)
} */

//■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■//