*=======================================================*
*	Lighting module: latest update 12/11/95		*
*=======================================================*
*	The contents of this module are temporary,	*
*	and should not be taken seriously. I just 	*
*	wanted to see what lighting would look like!	*
*=======================================================*

*-------------------------------------------------------*
*	Dodgey colour-mixing macros			*
*-------------------------------------------------------*

getcol	macro
	and.w		#$FF,\1
	move.w		(colours.l,\1.w*2),\1
	endm

fixcol	macro
	push.w		d7
	bfextu		\1{16:5},\3
	mulu		\2,\3
	and.w		#$FF00,\3
	lsl.w		#3,\3
	bfextu		\1{21:6},d7
	mulu		\2,d7
	lsr.w		#8,d7
	lsl.w		#5,d7
	or.w		d7,\3
	bfextu		\1{27:5},d7
	mulu		\2,d7
	lsr.w		#8,d7
	or.w		d7,\3
	pop.w		d7
	endm

*-------------------------------------------------------*
*	Dodgey hacks to set up sector lighting		*
*-------------------------------------------------------*

set_lighting:
	move.w		frame,framelock
	rts

process_lighting:
	pushall
	move.l		this_sector,a0
	move.w		sector_special(a0),d2
	move.w		sector_light(a0),d1
	lsr.w		d1
	cmp.w		#1,d2
	beq.s		.off
	cmp.w		#2,d2
	beq.s		.on
	cmp.w		#3,d2
	beq.s		.on
	cmp.w		#4,d2
	beq.s		.on
	cmp.w		#8,d2
	beq.s		.off
	cmp.w		#12,d2
	beq.s		.sync
	cmp.w		#13,d2
	beq.s		.sync
	cmp.w		#17,d2
	beq.s		.off
.end:	move.w		d1,sector_tag(a0)
	popall
	rts
.on:	move.w		framelock,d2
	lsr.w		#4,d2
	and.w		#8-1,d2
	bne.s		.end
	move.w		#255,d1
	bra		.end
.off:	move.w		framelock,d2
	lsr.w		#4,d2
	addq.w		#3,d2
	and.w		#8-1,d2
	beq		.end
	move.w		#255,d1
	bra		.end
.sync:	move.l		a0,d2
	add.w		framelock,d2
	lsr.l		#4,d2
	and.w		#8-1,d2
	bne		.end
	move.w		#255,d1
	bra		.end

*-------------------------------------------------------*
*	Even more dodgey hack to work out colours	*
*-------------------------------------------------------*
select_colours:	
*-------------------------------------------------------*
	swap		d0
	sub.w		d0,d0
	swap		d0
	move.l		d0,a0
	move.l		Sector_Array,a1 
	lea		(a0,a0.l*2),a5
	move.l		a5,d0 
	lea		(a0,d0.l*4),a0
	move.l		a0,d0 
	ifd		lighting_effects
	moveq		#0,d1
	move.w		sector_tag(a1,d0.w*2),d1
	endc
	move.w		sector_colour,d0
	getcol		d0
	ifd		lighting_effects
	fixcol		d0,d1,d2
	move.w		d2,addwall_tex(a6)
	elseif
	move.w		d0,addwall_tex(a6)
	endc
	move.w		floor_ht,d0
	getcol		d0
	ifd		lighting_effects
	fixcol		d0,d1,d2
	move.w		d2,addwall_fc(a6)
	elseif
	move.w		d0,addwall_fc(a6)
	endc
	move.w		ceiling_ht,d0
	getcol		d0
	ifd		lighting_effects
	fixcol		d0,d1,d2
	move.w		d2,addwall_cc(a6)
	elseif
	move.w		d0,addwall_cc(a6)
	endc
	rts

*-------------------------------------------------------*
			bss
*-------------------------------------------------------*

frame:			ds.w	1			; some timing variables
framelock:		ds.w	1			; for hacked lighting effects

floor_col:		ds.w	1			; floor texture-id
ceiling_col:		ds.w	1			; ceiling texture-id

*-------------------------------------------------------*
			text
*-------------------------------------------------------*
