script_table:
	.dw	delay_return
		;arg1
	.dw	defer
		;arg1,*arg2
	.dw	change_tile
		;(arg1/16)+arg2, arg3
	.dw	change_tile_group
		;(arg1/16)+arg2, arg3, arg4, arg5
	.dw	prevent
		;arg1, arg2
	.dw	prevent_tile
		;(arg1/16)+arg2, arg3
	.dw	show_text
		;*arg1
	.dw	show_text_script
		;*arg1,*arg2
	.dw	show_double_text
		;*arg1
	.dw	scroll_screen
		;arg1,arg2,*arg3
	.dw	trigger_screen
		;arg1,arg2,*arg3
	.dw	trigger_screen_fast
		;arg1,arg2,*arg3
	.dw	move_screen
		;arg1,arg2,*arg3
	.dw	activate_animate
		;(arg1 * animate_width),arg2
	.dw	activate_object
		;(arg1 * object_width),arg2
	.dw	activate_enemy
		;(arg1 * enemy_width),arg2
	.dw	activate_misc
		;(arg1 * misc_width),arg2
	.dw	activate_overhead
		;(arg1 * misc_width),arg2
	.dw	create_enemy
		;arg1,arg2,arg3,arg4,arg5,arg6,*arg7,*arg8
	.dw	create_object
		;arg1,arg2,arg3,arg4,arg5,arg6,arg7,*arg8
	.dw	set_player_anim
		;arg1, *arg2
	.dw	set_enemy_attr
		;(arg1*enemy_width)+enemy_anim_ctr, arg2, *arg3
	.dw	set_object_attr
		;(arg1<<4)|arg2, arg3
	.dw	set_overhead_anim
		;(arg1*overhead_width)+overhead_anim_ctr, arg2, *arg3
	.dw	set_enemy_com
		;(arg1*enemy_width)+enemy_com_ctr, *arg2
	.dw	ex_update
		;null
	.dw	code
		;*arg1
	.dw	ll_new_pcom
		;arg1,*arg2
	.dw	ll_new_mcom
		;arg1,*arg2
	.dw	return
		;null
;end of definitions (keep this comment for external program)

;reserve some temporary label space for the arguments
arg1:\arg2:\arg3:\arg4:\arg5:\arg6:\arg7:\arg8:


scroll_script:
	ld	hl,(object_param1)
	PCALL(load_script)
;to run a script, copy the script to
;script_buffer, and call run_script	
run_script:
	ld	hl,script_buffer
	exx
	
script_loop:
	call	get_arg8
	add	a,a
	ld	d,0
	ld	e,a
	ld	hl,script_table
	add	hl,de
	ld	de,script_loop
	push	de
	ld	a,(hl)
	inc	hl
	ld	h,(hl)
	ld	l,a
	jp	(hl)
	
;returns 8-bit argument in a
get_arg8:
	exx
	ld	a,(hl)
	inc	hl
	exx
	ret
	
;returns 16-bit argument in hl
get_arg16:
	exx
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	inc	hl
	push	de
	exx
	pop	hl
	ret

;---------------------------------------
;---------------------------------------
delay_return:
	;ld	hl,no_action
	;ld	(focus_screen_com+15),hl
	ld	hl,invert_buffer+12+18
	ld	(p_com_ptr),hl
	call	get_arg8
	ld	hl,p_com_ctr
	add	a,(hl)
	ld	(hl),a
	ld	hl,m_com_ctr
	add	a,(hl)
	ld	(hl),a
	ret
;---------------------------------------
;---------------------------------------
defer:
	call	get_arg8
	call	get_arg16
	ld	(object_param1),hl
	ld	hl,_defer_com
	call	set_p_com_skip
	pop	hl
	set	f_input,(iy+flags)
	set	f_invunerable,(iy+flags)
	ret

_defer_com:
	.dw	delay_widen
	.db	1
	.dw	scroll_script
;---------------------------------------
;---------------------------------------
change_tile:
	call	get_arg8		;fetches offset
	ld	e,a
	ld	d,0
	ld	hl,map_data
	add	hl,de
	call	get_arg8		;fetches tile
	ld	(hl),a
	ret	
;---------------------------------------
;---------------------------------------
;for use with 2 identical tiles and up
change_tile_group:
	call	get_arg8		;offset into map_data
	ld	e,a
	ld	d,0
	ld	hl,map_data
	add	hl,de
	call	get_arg8		;offset for each tile
	ld	e,a
	bit	7,a
	jr	z,_pos_tile_offset
	dec	d			;$FF
_pos_tile_offset:
	call	get_arg8		;number
	ld	b,a
	call	get_arg8		;tile to change to
_tile_change_loop:
	ld	(hl),a
	add	hl,de
	djnz	_tile_change_loop
	ret
;---------------------------------------
;---------------------------------------
prevent:
	call	get_arg8		;type
	ld	c,a	
	call	get_arg8
	ld	b,a			;number
	jp	new_prev_entry
;---------------------------------------
;---------------------------------------
prevent_tile:
	call	get_arg8		;read in offset
	ld	c,a
	call	get_arg8		;read in tile change
	ld	b,a
	jp	new_tprev_entry
;---------------------------------------
;---------------------------------------	
show_text:
	call	get_arg16		;hl gets text
	jp	normal_text
;---------------------------------------
;---------------------------------------
show_text_script:
	call	get_arg16		;text
	ex	de,hl
	call	get_arg16
	ld	(object_param1),hl
	ex	de,hl
	ld	de,scroll_script
	jp	code_text
	
show_double_text:
	call	get_arg16
	jp	normal_text
;---------------------------------------
;---------------------------------------
scroll_screen:
	call	_generic_scroll
	PCALL(ready_focus)
	pop	hl
	exx
	ret
;---------------------------------------
;---------------------------------------
trigger_screen:
	call	_generic_scroll
	PCALL(trigger_focus)
	pop	hl
	exx
	ret
trigger_screen_fast:
	call	trigger_screen
	ld	hl,update_screen_focus_double
	ld	(invert_buffer+12+6),hl
	ret
;---------------------------------------
;---------------------------------------	
move_screen:
	call	_generic_scroll
	PCALL(ready_move)
	pop	hl
	exx
	ret
				
_generic_scroll:
	call	get_arg16
	ex	de,hl			;de - x,y
	call	get_arg16		;hl - code
	ld	(object_param1),hl
	ex	de,hl			;hl - x,y
	ld	de,scroll_script
	pop	ix
	exx
	push	hl
	exx
	jp	(ix)
;---------------------------------------
;---------------------------------------
activate_animate:
	ld	hl,animate_array
	jr	_act_finish	
activate_object:
	ld	hl,object_array
	jr	_act_finish
activate_enemy:
	ld	hl,enemy_array
	jr	_act_finish
activate_misc:
	ld	hl,misc_array
	jr	_act_finish
activate_overhead:
	ld	hl,overhead_array
	
_act_finish:
	ld	d,0
	call	get_arg8
	ld	e,a
	add	hl,de
	call	get_arg8
	ld	(hl),a
	ret
;---------------------------------------
;---------------------------------------
set_enemy_anim:
	ld	hl,enemy_array
	jr	_anim_finish
	
set_object_anim:
	ld	hl,object_array
	jr	_anim_finish
	
_anim_finish:
	ld	d,0
	call	get_arg8
	ld	e,a
	add	hl,de
	call	get_arg8
	ld	(hl),a
	inc	hl
	ex	de,hl
	call	get_arg16
	ex	de,hl
	ld	(hl),e
	inc	hl
	ld	(hl),d
	ret
;---------------------------------------
;---------------------------------------
ll_new_mcom:
	ld	hl,m_com_ctr
	call	get_arg8
	ld	(hl),a
	ex	de,hl
	call	get_arg16
	ex	de,hl
	ld	(hl),e
	inc	hl
	ld	(hl),d
	ret
;---------------------------------------
;---------------------------------------
return:
	pop	hl
	ret
	
ex_update:
	exx
	push	hl
	call	great_update
_scr_code_return:
	pop	hl
	exx
	ret
	
	;activate_enemy:
create_enemy:
create_object:
set_player_anim:
set_overhead_anim:
;---------------------------------------
;---------------------------------------
code:
	call	get_arg16
	ld	bc,_scr_code_return	;this is in ex_update
	push	bc
	exx
	push	hl
	exx
	jp	(hl)
;---------------------------------------
;---------------------------------------
ll_new_pcom:

.end