begincreaturescript;

variables;

short i,target,last_abil_time,invcheck,invdone,next,pcs;
short summoncheck,desummonused,movex,rctx,wiz;

body;

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

beginstate DEAD_STATE;

	// Set COMBAT_DONE flag
	set_flag(99,9,1);

	erase_char(ME);
	reset_dialog();
	add_dialog_str(0,"Shroud coughs up blood.  It quickly dries and hisses on his burning red armor.  He grabs his various wounds as his knees buckle.  He shakes his head in disappointment.",0);
	add_dialog_str(1,"_I see it coming for me.  The land of the dead greets us all.  You..._  He coughs up a sizable portion of blood.  _You..._",0);
	add_dialog_str(2,"He stares up at you through his shadowy helmet.  You see his beady eyes staring back at you in horror.  _You are Ratatosk...!_",0);
	add_dialog_str(3,"Suddenly, a bright light shoots through your backpack.  It travels through your body and strikes General Shroud, causing him to instantly disintegrate.",0);
	add_dialog_str(4,"Once his remains are no more, you look into your backpack, only to find the Obispopiedra glowing gently.",0);
	add_dialog_str(5,"But what did he mean with his last words?  You almost thought for an instant that you knew, but the wheels within wheels keep turning.",0);
	next = run_dialog(1);
	award_party_xp(500,40);
	award_party_xp(500,40);
	award_party_xp(500,40);
	award_party_xp(500,40);
	award_party_xp(500,40);
	award_party_xp(500,40);
	award_party_xp(500,40);

	set_total_visibility(1);
	force_view_center(32,38);
	force_instant_terrain_redraw();
	pause(7);

	text_bubble_on_char(26,"Shroud is dead.");
	force_instant_terrain_redraw();
	pause(14);

	text_bubble_on_char(26,"");
	text_bubble_on_char(26,"Silva is dispatched.");
	force_instant_terrain_redraw();
	pause(14);

	text_bubble_on_char(26,"");
	text_bubble_on_char(26,"I might as well die");
	force_instant_terrain_redraw();
	pause(14);

	text_bubble_on_char(26,"");
	text_bubble_on_char(26,"with my honor.");
	force_instant_terrain_redraw();
	pause(14);

	text_bubble_on_char(26,"");
	text_bubble_on_char(26,"Unlock the gates.");
	force_instant_terrain_redraw();
	pause(14);

	text_bubble_on_char(26,"");
	text_bubble_on_char(26,"I fear not death.");
	force_instant_terrain_redraw();
	pause(14);

	text_bubble_on_char(26,"");
	force_view_center(49,26);
	activate_hidden_group(13);
	force_instant_terrain_redraw();
	pause(7);

	movex = 44;
	while(movex <= 49){
		relocate_character(30,movex,30);
		force_instant_terrain_redraw();
		pause(7);

		movex = (movex + 1);
	}

	set_character_facing(30,0);
	force_instant_terrain_redraw();
	pause(7);

	if(party_size() > 1)
		text_bubble_on_char(30,"Specialists!");
	if(party_size() == 1)
		text_bubble_on_char(30,"Specialist!");
	force_instant_terrain_redraw();
	pause(14);

	text_bubble_on_char(30,"");
	text_bubble_on_char(30,"Mistress Miraa");
	force_instant_terrain_redraw();
	pause(14);

	text_bubble_on_char(30,"");
	text_bubble_on_char(30,"wishes to see you.");
	force_instant_terrain_redraw();
	pause(14);

	rctx = 48;
	while(rctx <= 50){
		set_terrain(rctx,30,30);

		rctx = (rctx + 1);
	}
	force_instant_terrain_redraw();
	play_sound(99);
	pause(7);

	set_character_facing(30,2);
	force_instant_terrain_redraw();
	pause(7);

	movex = 48;
	while(movex >= 43){
		relocate_character(30,movex,30);
		force_instant_terrain_redraw();
		pause(7);

		movex = (movex - 1);
	}

	erase_char(30);
	set_flag(41,0,20);

	pcs = 0;
	while(pcs < 4){
		change_spell_level(pcs,0,2,get_flag(18,(5 + pcs)));
		change_spell_level(pcs,0,14,get_flag(19,(5 + pcs)));
		change_spell_level(pcs,0,17,get_flag(20,(5 + pcs)));
		change_spell_level(pcs,0,18,get_flag(21,(5 + pcs)));
		change_spell_level(pcs,1,6,get_flag(22,(5 + pcs)));
		change_spell_level(pcs,1,7,get_flag(23,(5 + pcs)));
		change_spell_level(pcs,1,12,get_flag(24,(5 + pcs)));
		change_spell_level(pcs,1,19,get_flag(25,(5 + pcs)));

		pcs = (pcs + 1);
	}

	wiz = 55;
	while(wiz < 61){
		set_attitude(wiz,4);
		wiz = (wiz + 1);
	}

	set_total_visibility(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");

		summoncheck = 86;
		desummonused = 0;
		while(summoncheck <= 119){
			if(char_ok(summoncheck) == TRUE){
				put_effect_on_char(summoncheck,4,4,1);
				erase_char(summoncheck);
				desummonused = 1;
			}
			summoncheck = (summoncheck + 1);
		}

		if(desummonused > 0){
			force_instant_terrain_redraw();
			run_animation_sound(42);
			print_str_color("Shroud dispels all summons!",1);
		}

		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(invcheck,-5);
	invdone = 1;
				}
			}
			invcheck = (invcheck + 1);
		}

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

		damage_char(1000,50,4);
		print_str_color("Shroud emits invisible fire!",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;