//Light
//Any npc
//Param1 - Duration
//PARAM2 - Radius
//PARAM3 - COLOR
{ [server]
	const EFFECT_ID			effect_light
	const EFFECT_NAME		"Light Emission"
	const EFFECT_ACTIONTYPE		normal
	const EFFECT_SCRIPT		$currentscript
}

{ [server] effect_die

	clientevent remove all effect.clientscript
}

{ game_duplicated

	//dbg temp Game Duplicated

	setvard NO_NEW_LIGHT 1
	setvard effect.duration 0

	callevent effect_die
}

#include effects/effect_base	allowduplicate

{ [server] game_activate

	setvard effect.duration 	PARAM1
	local GLOW_COLOR		PARAM3

	//dbg gotRadius PARAM2 gotcolor GLOW_COLOR ( PARAM3 )

	if ( GLOW_COLOR equals 'GLOW_COLOR' ) local GLOW_COLOR (255,255,128)	

	//dbg AdjustedColor GLOW_COLOR

	callexternal $get(ent_me,id) toggle_light

	if $get(ent_me,scriptvar,IR_GLOWING) == 1

	if ( !NO_NEW_LIGHT ) clientevent new all $currentscript effect.duration $get(ent_me,index) PARAM2 GLOW_COLOR PARAM4

	setvard effect.clientscript game.script.last_sent_id

	callevent effect.duration effect_die
}

// ===========
// Client Side
// ===========

{ [client]

	const OFS_POS			16
	const OFS_NEG			-16
	const OFSZ_POS			32
	const OFSZ_NEG			-10
	
	const LIGHT_PLAYER_SCALE	0.3
	const LIGHT_DROPPED_SCALE	0.5

	const SPRITE_1			blueflare1.spr
}

{ [client] client_activate

	setvard LIGHT_COLOR PARAM4

	//playermessage PARAM4


	setvard GLOWING 1
	setvard sfx.npcid PARAM2
	setvard sfx.radius PARAM3
	setvard ORIGINAL_CASTER PARAM5

	setvard USER_INDEX PARAM2 //attempts to tie light id

	callevent createlight
	setcallback render enable

	setvard TOTAL_OFS sfx.radius
	divide TOTAL_OFS 3

	setvard SPRITE_COUNT 0

	callevent PARAM1 effect_die

	////dbg temp $get(ent_me,index) vs $get(sfx.npcid,index)
	//if $get(ent_me,index) equals sfx.npcid
	callevent 0.1 spriteify
}

{ [client] effect_die
	setvard sfx.radius 0
	removescript
}

{ [client] createlight

	cleffect light new $getcl(sfx.npcid,origin) sfx.radius LIGHT_COLOR 5
	setvard effect.lightid game.script.last_light_id
}

{  [client] game_prerender

	//Update light position
	if sfx.radius > 0

	local l.pos $getcl(sfx.npcid,origin)
	local l.radius sfx.radius
	add l.radius $randf(-8,8)
	cleffect light effect.lightid l.pos l.radius LIGHT_COLOR 1.0
}


//Sprite

{ [client] spriteify

	setvard CYCLE_ANGLE 0

	calleventloop 18 createsprite
}

{ [client] createsprite

	//three rows
	local l.pos $getcl(sfx.npcid,origin)
	vectoradd l.pos $relpos($vec(0,CYCLE_ANGLE,0),$vec(0,TOTAL_OFS,-32))
	cleffect tempent sprite SPRITE_1 l.pos setup_sprite1_sparkle

	local l.pos $getcl(sfx.npcid,origin)
	vectoradd l.pos $relpos($vec(0,CYCLE_ANGLE,0),$vec(0,TOTAL_OFS,0))
	cleffect tempent sprite SPRITE_1 l.pos setup_sprite1_sparkle

	local l.pos $getcl(sfx.npcid,origin)
	vectoradd l.pos $relpos($vec(0,CYCLE_ANGLE,0),$vec(0,TOTAL_OFS,32))
	cleffect tempent sprite SPRITE_1 l.pos setup_sprite1_sparkle

	add CYCLE_ANGLE 20
}

{ [client] setup_sprite1_sparkle

	cleffect tempent set_current_prop death_delay 	3.0
	cleffect tempent set_current_prop framerate	30
	cleffect tempent set_current_prop frames	1
	cleffect tempent set_current_prop scale 	0.5
	cleffect tempent set_current_prop gravity 	-0.1
	cleffect tempent set_current_prop collide 	none
	cleffect tempent set_current_prop fadeout lifetime
}