begincreaturescript;

variables;

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

body;

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

beginstate DEAD_STATE;

	statval = (get_char_status(pcs,1) * -1);
	if(statval < 0){
		set_char_status(pcs,1,statval,1,0);
	}
	statval = (get_char_status(pcs,2) * -1);
	if(statval < 0){
		set_char_status(pcs,2,statval,1,0);
	}
	statval = (get_char_status(pcs,3) * -1);
	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);

	reset_dialog();
	add_dialog_str(0,"General Shroud seems to be damaged, but not as much as you might think.  You put in nearly enough damage into him to defeat Trahison, and yet he's even tougher!",0);
	add_dialog_str(1,"_This is only the beginning!  I will have you dead yet!  Prepare yourselves if you can._",0);
	add_dialog_str(2,"General Shroud concentrates intensely.  The power emanating from him is almost palpable.",0);
	next = run_dialog(1);

	pcs = 0;
	while(pcs < 4){
		change_spell_level(pcs,0,0,get_flag(0,(5 + pcs)));
		change_spell_level(pcs,0,3,get_flag(1,(5 + pcs)));
		change_spell_level(pcs,0,6,get_flag(2,(5 + pcs)));
		change_spell_level(pcs,0,16,get_flag(3,(5 + pcs)));
		change_spell_level(pcs,1,5,get_flag(4,(5 + pcs)));
		change_spell_level(pcs,1,13,get_flag(5,(5 + pcs)));
		change_spell_level(pcs,1,17,get_flag(7,(5 + pcs)));

		pcs = (pcs + 1);
	}

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

	// Ban Support Spells
	pcs = 0;
	while(pcs < 4){

		set_flag(8,(5 + pcs),get_spell_level(pcs,0,4));
		change_spell_level(pcs,0,4,-100);

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

		set_flag(10,(5 + pcs),get_spell_level(pcs,1,0));
		change_spell_level(pcs,1,0,-100);

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

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

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

		set_flag(14,(5 + pcs),get_spell_level(pcs,1,10));
		change_spell_level(pcs,1,10,-100);

		set_flag(15,(5 + pcs),get_spell_level(pcs,1,11));
		change_spell_level(pcs,1,11,-100);

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

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

		pcs = (pcs + 1);
	}

	set_character_pose(52,1);
	pcs = 0;
	while(pcs < 4){
		if(char_ok(pcs) == TRUE){
put_sparkles_on_char(pcs,9,4);
put_straight_zap(char_loc_x(52),char_loc_y(52),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(52,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");
		change_char_health(ME,25);

		invcheck = 0;
		invdone = 0;
		while(invcheck < 4){
			if(char_ok(invcheck) == TRUE){
				while(get_char_status(invcheck,4) >= 1){ 
	set_char_status(invcheck,4,-1,1,0);
	change_char_health(ME,10);
	invdone = 1;
				}
			}
			invcheck = (invcheck + 1);
		}

		if(invdone == 1){
	print_str_color("Shroud absorbs 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;