begincreaturescript;

variables;

short i,target,last_abil_time,invcheck,invdone,statval,next,pcs;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);
	break;

beginstate DEAD_STATE;

	reset_dialog();
	add_dialog_str(0,"Shroud is damaged much more this time.  He spits blood out of his helmet in frustration and clenches both fists.",0);
	add_dialog_str(1,"_You will not survive!  Today, you burn in hell!_",0);
	add_dialog_str(2,"General Shroud focuses once more as he prepares to change forms.",0);
	next = run_dialog(1);

	pcs = 0;
	while(pcs < 4){
		change_spell_level(pcs,0,4,get_flag(8,(5 + pcs)));
		change_spell_level(pcs,0,18,get_flag(9,(5 + pcs)));
		change_spell_level(pcs,1,0,get_flag(10,(5 + pcs)));
		change_spell_level(pcs,1,1,get_flag(11,(5 + pcs)));
		change_spell_level(pcs,1,2,get_flag(12,(5 + pcs)));
		change_spell_level(pcs,1,7,get_flag(13,(5 + pcs)));
		change_spell_level(pcs,1,10,get_flag(14,(5 + pcs)));
		change_spell_level(pcs,1,11,get_flag(15,(5 + pcs)));
		change_spell_level(pcs,1,12,get_flag(16,(5 + pcs)));
		change_spell_level(pcs,1,17,get_flag(17,(5 + pcs)));

		pcs = (pcs + 1);
	}

	put_effect_on_space(my_loc_x(),my_loc_y(),4,4,0);
	activate_hidden_group(4);
	relocate_character(53,my_loc_x(),my_loc_y());
	erase_char(ME);
	force_instant_terrain_redraw();
	run_animation_sound(165);
	pause(7);

	pcs = 0;

	while(pcs < 4){

		set_flag(18,(5 + pcs),get_spell_level(pcs,0,2));
		change_spell_level(pcs,0,2,-100);

		set_flag(19,(5 + pcs),get_spell_level(pcs,0,14));
		change_spell_level(pcs,0,14,-100);

		set_flag(20,(5 + pcs),get_spell_level(pcs,0,17));
		change_spell_level(pcs,0,17,-100);

		set_flag(21,(5 + pcs),get_spell_level(pcs,0,18));
		change_spell_level(pcs,0,18,-100);

		set_flag(22,(5 + pcs),get_spell_level(pcs,1,6));
		change_spell_level(pcs,1,6,-100);

		set_flag(23,(5 + pcs),get_spell_level(pcs,1,7));
		change_spell_level(pcs,1,7,-100);

		set_flag(24,(5 + pcs),get_spell_level(pcs,1,12));
		change_spell_level(pcs,1,12,-100);

		set_flag(25,(5 + pcs),get_spell_level(pcs,1,19));
		change_spell_level(pcs,1,19,-100);

		pcs = (pcs + 1);
	}

	set_character_pose(53,1);
	pcs = 0;
	while(pcs < 4){
		if(char_ok(pcs) == TRUE){
put_sparkles_on_char(pcs,9,4);
put_straight_zap(char_loc_x(53),char_loc_y(53),char_loc_x(pcs),char_loc_y(pcs),4);
		}
		pcs = (pcs + 1);
	}
		force_instant_terrain_redraw();
	run_animation_sound(65);
	pause(7);
	set_character_pose(53,0);

break;

beginstate START_STATE; 
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state(3);
			else set_target(ME,-1);
		}
	
	// Look for a target, attack it if visible
	if (select_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
		
	// Have I been hit? Strike back!
	if (who_hit_me() >= 0) {
		set_target(ME,who_hit_me());
		do_attack();
		set_state(3);
		}
		
	// Otherwise, just peacefully move around. Go back to start, if I'm too far
	// from where I started.
	if ((my_dist_from_start() >= 6) || ((my_dist_from_start() > 0) && (get_memory_cell(0) > 0))) {
		if (get_ran(1,1,100) < 40) 
			return_to_start(ME,1);
		}
		else if (get_memory_cell(0) == 0) {
			fidget(ME,25);
			}

	// if we're in combat and the above didn't give me anything to do, just
	// stop now. Otherwise, game will keep running script, and that eats up CPU time.
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking

	if ((tick_difference(last_abil_time,get_current_tick()) >= 1)) {
		print_str("Ability Used");

		invcheck = 0;
		invdone = 0;

		pcs = 0;
		while(pcs < 4){
			if(char_ok(pcs) == TRUE){
	statval = (get_char_status(pcs,1) * -2);
	if(statval < 0){
		set_char_status(pcs,1,statval,1,0);
	}
	statval = (get_char_status(pcs,2) * -2);
	if(statval < 0){
		set_char_status(pcs,2,statval,1,0);
	}
	statval = (get_char_status(pcs,3) * -2);
	if(statval < 0){
		set_char_status(pcs,3,statval,1,0);
	}
	statval = (get_char_status(pcs,5) * -1);
	set_char_status(pcs,5,statval,1,0);
	statval = (get_char_status(pcs,14) * -1);
	set_char_status(pcs,14,statval,1,0);
	statval = (get_char_status(pcs,15) * -1);
	set_char_status(pcs,15,statval,1,0);
	statval = (get_char_status(pcs,16) * -1);
	set_char_status(pcs,16,statval,1,0);
	statval = (get_char_status(pcs,17) * -1);
	set_char_status(pcs,17,statval,1,0);
	statval = (get_char_status(pcs,22) * -1);
	set_char_status(pcs,22,statval,1,0);
			}
			pcs = (pcs + 1);
		}

		while(invcheck < 4){
			if(char_ok(invcheck) == TRUE){
				while(get_char_status(invcheck,4) >= 1){ 
	set_char_status(invcheck,4,-1,1,0);
	set_char_status(invcheck,2,1,1,0);
	invdone = 1;
				}
				statval = (get_health(invcheck) - get_max_health(invcheck));
				if(statval >= 1){
		statval = (statval * -2);
		change_char_health(invcheck,statval);
				}
			}
			invcheck = (invcheck + 1);
		}

		if(invdone == 1){
	print_str_color("Shroud reverses your invulnerability!",1);
		}

	last_abil_time = get_current_tick();
	}
	

	if (target_ok() == FALSE)
		set_state(START_STATE);
	do_attack();
break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {
		print_named_str(ME," doesn't respond.");
		end();
		}
	begin_talk_mode(get_memory_cell(3));
break;