;Xe[WJn
@stage
pageid="stage"
playback=0
playback_auto=0
first=0

;gCf[^
for %i=1 to 10
	deletefile "savedata\"+str(stage*100+%i)+".sav"
next

;ʉ߃tO
operation_pass_format 0

;`
diff=""

stage_spset
sp "bg:bg",{name="img\panel\stage_"+st+"_0.png",a=255,x=0,y=0,z=0}
trans_stdp
character_body
if depth>1 then eventse
print #u,1000,"img\rule_00.png"

;fvXJn
@stage_depth

;fvXݒǍ
gosub "@depthdef_"+stdp

;aflݒǍ
gosub "@bgmdef_"+stdp

;`o
if first==0 then gosub @caption_enter

state_depth
state_mark_format
state_fw
turn=0
complete=0
if first==0 then
	sp "bg:bg",{name="img\panel\stage_"+st+"_1.png",a=255,x=0,y=0,z=0}
	caption_stage
	sp "orgasm:table",{name="img\panel\orgasm_table.png",a=255,x=28,y=996,z=1}
	hp=5
	shield=0
	hack=0
	hacks=4
	charge=0
	org=0
	orgnum=0
	orgmode=0
	state_hp
	state_hack
	state_hacks
	orgasm_orgnum
	if _debug<>0 then debug_btn
	print #u,1000,"img\rule_06.png"
	event "event","event_"+stdp
	spsetdelete "event"
	print #u,1000,"img\rule_06.png"
endif

;pl`
panel_format
se1 "121"
for %y=1 to 9
	for %x=1 to 9
		if panel[%x][%y][0]==1 then
			sp "panel:hex"+str(%y*10+%x),{name={"img\panel\panel_mark_0.png","img\panel\panel_mark_0_on.png"},a=255,x=909+91*%x,y=-353+56*%x+112*%y,z=2}
			for %i=0 to 26
				sp "command:hex"+str(%y*1000+%x*100+%i),{name="*"+str(62+%i*2)+","+str(108-%i*4)+",#00000000",a=255,x=935+91*%x-%i,y=-353+56*%x+112*%y+%i*2,z=0}
			next
		endif
		print #c
	next
next

;VXej[Ǎ
if first==0 then
	first=1
	system_menu
endif

;^[Jn
@stage_turn
turn=turn+1

;}jA|bvAbvP
if _manualpopup==1 and _manualpopup[1]==0 and stdp=="101" then system_2_popup 1

;}jA|bvAbvR
if _manualpopup==1 and _manualpopup[3]==0 and turn==2 then system_2_popup 3

;}jA|bvAbvV
if _manualpopup==1 and _manualpopup[7]==0 and org>0 then system_2_popup 7

;}jA|bvAbvX
if _manualpopup==1 and _manualpopup[9]==0 and depth==2 then system_2_popup 9

;}jA|bvAbvPO
if _manualpopup==1 and _manualpopup[10]==0 and depth==3 then system_2_popup 10

;}jA|bvAbvPQ
if _manualpopup==1 and _manualpopup[12]==0 then
	%flag=0
	for %y=1 to 9
		for %x=1 to 9
			if panel[%x][%y][0]==0 and panel[%x][%y][5]>0 and panel[%x][%y][6]==1 then %flag=1
		next
	next
	if %flag==1 then system_2_popup 12
endif

;{^`
@stage_btndef
savepoint
if turn==1 then save stage*100+depth
system_btndef
for %y=1 to 9
	for %x=1 to 9
		if panel[%x][%y][0]==1 then
			for %i=0 to 26
				btn "command:hex"+str(%y*1000+%x*100+%i),{on="P(panel:hex"+str(%y*10+%x)+",1)",off="P(panel:hex"+str(%y*10+%x)+",0)"}
			next
		endif
	next
next
if _debug<>0 then debug_btndef

;gC
if _retry==1 then
	_retry=0
	hp=0
	state_hp
	print #c
	wait 100
	for %i=1 to 5
		state_hp_update 1
		print #c
		wait 100
	next
endif

;{^
@stage_btnexec
btnid="stage"
btnexec btn_panel,"command",{func=1,wheel=1}
if mid(btn_panel,0,2)=="#F" then goto "@func_"+mid(btn_panel,2,2)
if btn_panel=="" or btn_panel==#wd then goto @stage_btnexec
btnclear "command"

;GXP[v
if btn_panel==#r and getkey("ESC")==1 then
	system 0,4
	goto @stage_btndef
endif

;VXe
if mid(btn_panel,0,3)=="sys" then
	system 0,num(mid(btn_panel,3,1))
	goto @stage_btndef
endif

;obNO
if btn_panel==#wu then
	system 0,1
	goto @stage_btndef
endif

;fobO
if mid(btn_panel,0,5)=="debug" then debug_com

;ENbN
if btn_panel==#r then goto @stage_mark

;T[`W
search_y=num(mid(btn_panel,3,1))
search_x=num(mid(btn_panel,4,1))

;󔒎J\񏉊
blank_reserve=0
for %i=1 to 99
	blank_reserve[%i]=0
next

;nbLO
hacking=0
critical=0
if panel[search_x][search_y][3]>0 then
	if hack<panel[search_x][search_y][3] then
		se1 "012"
		sp "effect:att",{name="img\panel\gauge_hack_att.png",a=255,x=973,y=906,z=0}
		print #c
		spdelete "effect:att"
		print #f,500
		spcell "command:hex"+str(search_y*10+search_x),0
		goto @stage_btndef
	endif
	panel_hacking search_x,search_y
endif

;
if turn==1 and hacking==0 and panel[search_x][search_y][1]>0 then
	panelset_fw panel[search_x][search_y][1]
	panel[search_x][search_y][1]=0
	if panel[search_x][search_y][2]==1 then
		panelset_securityhole
		panel[search_x][search_y][2]=0
	endif
	panel_format_adj
endif

;plJ
if hacking==0 then se1 "013"
panel_open search_x,search_y

;t@CAEH[
atk=0
if panel[search_x][search_y][1]>0 then
	if hacking>=panel[search_x][search_y][1] then
		atk=panel[search_x][search_y][1]
		panel_attack search_x,search_y
	else
		panel_firewall search_x,search_y
	endif
endif
hacking=0

;󔒎J
for %i=1 to 99
	if blank_reserve[%i]>0 then panel_blank blank_reserve[%i] mod 10,floor(blank_reserve[%i]/10)
next

;A^bN
if atk>0 then operation

;Q[I[o[
if hp==0 then goto @stage_gameover

;Xe[WNA
%panel=0
%hit=0
for %y=1 to 9
	for %x=1 to 9
		if panel[%x][%y][0]==1 then %panel=%panel+1
		if panel[%x][%y][0]==0 and panel[%x][%y][1]>0 then %hit=%hit+1
	next
next
%fw=0
for %i=1 to 5
	%fw=%fw+fw[%i]
next
if %panel+%hit==%fw then goto @stage_complete

goto @stage_turn

;}[N
@stage_mark
mark_x=0:mark_y=0
getmouse %mouse_x,%mouse_y
for %y=1 to 9
	for %x=1 to 9
		if panel[%x][%y][0]==1 then
			for %i=0 to 26
				%flag=sphitcheck("command:hex"+str(%y*1000+%x*100+%i),%mouse_x,%mouse_y)
				if %flag==1 then mark_x=%x:mark_y=%y
			next
		endif
	next
next
if mark_x==0 and mark_y==0 then goto @stage_btndef
se1 "014"
panel[mark_x][mark_y][3]=panel[mark_x][mark_y][3]+1
if panel[mark_x][mark_y][3]==6 then panel[mark_x][mark_y][3]=0
sp "panel:hex"+str(mark_y*10+mark_x),{name={"img\panel\panel_mark_"+str(panel[mark_x][mark_y][3])+".png","img\panel\panel_mark_"+str(panel[mark_x][mark_y][3])+"_on.png"},a=255,x=909+91*mark_x,y=-353+56*mark_x+112*mark_y,z=2}
state_mark_format
for %y=1 to 9
	for %x=1 to 9
		if panel[%x][%y][3]>0 then mark[panel[%x][%y][3]]=mark[panel[%x][%y][3]]+1
	next
next
state_fw

;}jA|bvAbvT
if _manualpopup==1 and _manualpopup[5]==0 and hack>0 then system_2_popup 5

goto @stage_btndef

;Q[I[o[
@stage_gameover
bgmfade 1000
wait 1000
se1 "113"
spset "system",810
sp "panel:gameover",{name="img\panel\dsp_gameover.png",a=255,x=993,y=33,z=0}
print #u,1000,"img\rule_06.png"
wait 500
sp "system:gameover",{name="img\panel\dsp_gameover.png",a=80,x=993,y=33,z=2}
system 3,4

;Rv[g
@stage_complete
complete=1
wait 500
sp "panel:complete",{name="img\panel\dsp_complete.png",a=255,x=993,y=33,z=0}
se1 "005"
se1 "123"
print #c
wait 500
spdelete "panel:complete"
for %y=1 to 9
	for %x=1 to 9
		panel[%x][%y][0]=0
		if panel[%x][%y][1]>0 then panel_open_fw %x,%y
	next
next
print #u,500,"img\rule_00.png"
wait 500
spdeletes "panel:"
spdeletes "command:hex"
spdeletes "state:fw"
panel_explosion

;CxgO
depth=depth+1
trans_stdp
if depth<11 then
	character_body
	eventse
	print #u,1000,"img\rule_06.png"
	wait 500
elseif depth==11 then
	if stage==5 and _clear==0 then _clear=1
	bgmfade 1000
	wait 1000
	orgasm_update 1,100-org
	wait 500
	bgm "902",1,0
	sp "panel:clear_0",{name="img\panel\dsp_clear_0.png",a=255,x=993,y=33,z=0}
	sp "panel:clear_1",{name="img\panel\dsp_clear_1.png",a=255,x=993,y=33,z=1}
	print #u,500,"img\rule_06.png"
	wait 2000
	spdelete "panel:clear_0"
	print #u,500,"img\rule_00.png"
	wait 500
	state_depth
	bgm "214",0,0
endif

;Cxg
event "event","event_"+stdp

;fvXis
if depth<11 then
	spsetdelete "event"
	print #u,1000,"img\rule_06.png"
	state_hack_update 1
	state_hp_update 1
	goto @stage_depth
elseif depth==11 then
	spdelete "event:btn0"
	print #c
	wait 500
	bgmfade 1000
	sp "effect:mask",{name="*#000000",a=200,x=0,y=0,z=1}
	print #f,500
	wait 1000
	sp "effect:noise",{name={"img\noise_sand0.jpg","img\noise_sand1.jpg","img\noise_sand2.jpg"},a=255,x=0,y=0,z=0,animtime=50,animtype="normal"}
	print #c
	se2 "920",0
	wait 1300
	stage_spsetdelete
	goto @title
endif
