bullet_name = "bullet-splitworm"
dofile_lua( bullet_name )

-- null part
-- leave me
	local actor = ACTOR_preload()
-- leave me

	actor[cp_actor_name]		= "worm_chain_split_null"
	actor[cp_model_dir]			= "beasties"
	actor[cp_model_name]		= "worm-null"

	actor[cp_move_type]			= cmt_random  -- + cmt_repelled

	actor[cp_i_collide_with_beasties]	= 0
	actor[cp_these_collide_with_me]		= 0

	actor[cp_score]				= 500

	-- movement

--	actor[cp_i_am_repelled_by]		= cat_block + cat_blocker + cat_webblob
--	actor[cp_repel_dist]			= 8.0

	actor[cp_move_speed_min]		= 0.3
	actor[cp_move_speed_max]		= 0.3
	actor[cp_move_acceleration]		= 0.1
	actor[cp_move_deceleration]		= 0.0
	actor[cp_move_inertia_accn]		= 0.1
	actor[cp_move_inertia_decn]		= 0.0

	actor[cp_target_stop_dist]		= 10.0

--	actor[cp_turn_lock_angle_inner]		= 115.0
--	actor[cp_turn_lock_angle_outer]		= 125.0
--	actor[cp_turn_speed_still]			= 1.0
--	actor[cp_turn_acceleration_still]	= 0.5
--	actor[cp_turn_inertia_still]		= 0.5
	actor[cp_turn_speed_moving]			= 2.0
	actor[cp_turn_acceleration_moving]	= 1.0
	actor[cp_turn_inertia_moving]		= 0.0


	actor[cp_random_edge_inner_percent]	= 0.0
	actor[cp_random_edge_outer_percent]	= 80.0

	actor[cp_random_offset_x]			= 5.0
	actor[cp_random_offset_z]			= 5.0

	actor[cp_random_trajectory_offset]				= 60.0
	actor[cp_random_trajectory_offset_delay_min]	= 0.1
	actor[cp_random_trajectory_offset_delay_max]	= 0.5

	actor[cp_geopus_style]			= 4+32

	actor[cp_difficulty_adjust_speed]	= 10.0

-- leave me
	c_preload_model( actor )
-- leave me

	-- set new stuff
	actor[cp_move_type]			= cmt_track
	actor[cp_move_speed_min]		= 0.40
	actor[cp_move_speed_max]		= 0.40

--	actor[cp_i_am_repelled_by]		= cat_block + cat_blocker + cat_webblob
--	actor[cp_repel_dist]			= 8.0

	-- send out
	c_set_trigger_state( "count_extended_children", 5.0, 5.0, actor )
	-- new state


-- head part
-- leave me
	local actor = ACTOR_preload()
-- leave me





	actor[cp_actor_name]		= "worm_chain_split_head"
	actor[cp_model_dir]			= "beasties"
	actor[cp_model_name]		= "worm-split_1"

	actor[cp_i_collide_with_beasties]	= cat_player
	actor[cp_these_collide_with_me]		= cat_player + cat_bullet

	actor[cp_health_percent]		= 30.0
	actor[cp_score]				= 150

	actor[cp_hit_power]			= 5.0
	actor[cp_hit_delay]			= 3.0

	actor[cp_geopus_style]			= 8

	actor[cp_launch_scale_min]		= 70.0
	actor[cp_launch_scale_max]		= 70.0

--	actor[cp_mutate_hit_delay]		= 1.0

	actor[cp_launch_hue_shift] = color

-- leave me
	c_preload_model( actor )
-- leave me


	c_add_particle_to_anim( "emerge", "mainbone", "worm-split_emerge" )
	c_add_particle_to_anim( "emerge", "mainbone", "worm-split_emergepad" )
	c_add_sound_to_anim( "emerge", "worm-split-emerge" )


	c_set_random_anim( "walk", 350.0, 400.0, 0, 0 )
	local seq = { 0.0 , 0.175}
	c_set_use_explosion_for( "bye_bye", "worm-split_flash", exp_no_align, 30.0, 0.0, 2000.0, 2000.0, seq )
	c_add_particle_to_explosion( "worm-split_flash", "explode", "", "worm-split_flash" )
	c_add_sound_to_explosion( "worm-split_flash", "explode", "worm-split-split" )

	local boo = WEAPON_preload( bullet_name )
	boo[w_gun_bone_name]		= "sphere01"
	boo[w_shaft_bone_name]		= "sphere01"
	boo[w_fire_anim_name]		= "fire"
	boo[w_flare_exp_name]		= "robo_yellow_flare"
	boo[w_flare_exp_scale]		= 600.0
	boo[w_gun_type]				= "no_bone_aim"
	boo[w_aim_delay]			= 1.0
	boo[w_bullet_scale]			= 70.0
	boo[w_barrel_offset]		= 2.0
	boo[w_no_shoot_dist]		= 9.0
	boo[w_no_shoot_angle]		= 0.0
	boo[w_reload_random]		= 100.0
	local seq = { 2.0 }
	WEAPON_add( boo, seq )

	-- new state
	c_set_trigger_mutate( "worm_chain_split_head", "my_health__change_child__via_null_actor_and_check", "worm_chain_split_body", 0.0, 0.0, 1, 1, 95.0, 105.0 )
	-- new state


-- tail part
-- leave me
--	local actor = ACTOR_preload()
-- leave me

	actor[cp_actor_name]		= "worm_chain_split_tail"
	actor[cp_model_dir]			= "beasties"
	actor[cp_model_name]		= "worm-split_3"

	actor[cp_health_percent]		= 30.0
	actor[cp_score]				= 150

-- leave me
	c_preload_model( actor )
-- leave me

	c_set_random_anim( "walk", 350.0, 400.0, 0, 0 )
	local seq = { 0.0 , 0.175}
	c_set_use_explosion_for( "bye_bye", "worm-split_flash", exp_no_align, 30.0, 0.0, 2000.0, 2000.0, seq )
	c_add_particle_to_explosion( "worm-split_flash", "explode", "", "worm-split_flash" )

	-- new state
	c_set_trigger_mutate( "worm_chain_split_tail", "my_health__change_parent__via_null_actor_and_check", "worm_chain_split_body", 0.0, 0.0, 1, 1, 95.0, 105.0 )
	-- new state


-- body part
-- leave me
--	local actor = ACTOR_preload()
-- leave me




	actor[cp_actor_name]		= "worm_chain_split_body"
	actor[cp_model_dir]			= "beasties"
	actor[cp_model_name]		= "worm-split_2"

	actor[cp_health_percent]		= 45.0
	actor[cp_score]				= 150

-- leave me
	c_preload_model( actor )
-- leave me

	c_set_random_anim( "walk", 350.0, 400.0, 0, 0 )
	local seq = { 0.0 , 0.175}
	c_set_use_explosion_for( "bye_bye", "worm-split_flash", exp_no_align, 30.0, 0.0, 2000.0, 2000.0, seq )
	c_add_particle_to_explosion( "worm-split_flash", "explode", "", "worm-split_flash" )

	-- new state
	c_set_trigger_mutate( "worm_chain_split_tail", "my_health__change_parent__via_null_actor_and_check", "worm_chain_split_body", 0.0, 0.0, 1, 1, 95.0, 105.0 )
	c_set_trigger_mutate( "worm_chain_split_head", "my_health__change_child__via_null_actor_and_check", "worm_chain_split_body", 0.0, 0.0, 1, 1, 95.0, 105.0 )
	-- new state


function worm_chain_split_plop( act, num_segments, posx, posy, posz, rot, siz, hue, path )
	local cur_unique_name = ""
	local last_unique_name = ""
	local act_part_name = ""
--	local size = siz
	local launch_delay = 0

	for i = 1, num_segments, 1 do
		last_unique_name = cur_unique_name
		act_part_name = format( "%s_null", act )
		cur_unique_name = format( "%s_%d", act_part_name, actor_unique_id )

		local apo = ACTOR_PreLaunch( act_part_name, cur_unique_name )
		apo:setall( posy, posz, -posx, rot, siz, hue, 0.0, path )
		apo:launch()

		posx = 0
		posy = 0
		posz = 0
		rot = 0
--		path = nil

		if i > 1 then
			c_link_to_parent( last_unique_name, "mainbone_x17", last_unique_name )
		end

		if i == 1 then
			act_part_name = format( "%s_head", act )
		elseif i == num_segments then
			act_part_name = format( "%s_tail", act )
		else
			act_part_name = format( "%s_body", act )
		end

		apo = ACTOR_PreLaunch( act_part_name )
--		size = c_zrand(0.85,1.15) * siz
		apo:setall( posy, posz, -posx, rot, siz, hue, launch_delay )
		apo:launch()

		c_link_to_parent( cur_unique_name, "mainbone_x" )

		launch_delay = launch_delay + 0.1
	end
end
