// Town 13: Central Bahssikava
begintownscript;

variables;

short choice,dummy,i,j,k,l,diversion,div_start;
string dlgstr;

body;

beginstate INIT_STATE;
turn_off_training(1);

i = 8;
while (i <= 19)
	{set_level(i,15);
	i = i + 1; }

add_range_to_group(20,24,1);
add_range_to_group(33,35,1);
set_name(7,"Mining Ghost");

if (get_flag(13,11) == 0)
	{change_blocked(20,27,1);
	change_blocked(20,28,1);
	change_blocked(24,29,1);
	change_blocked(25,29,1);
	change_blocked(21,27,1);
	change_blocked(22,27,1);
	change_blocked(23,27,1);
	change_blocked(24,27,1);
	change_blocked(25,27,1);
	change_blocked(21,28,1);
	change_blocked(22,28,1);
	change_blocked(23,28,1);
	change_blocked(24,28,1);
	change_blocked(25,28,1);
	}

if (get_flag(13,16))
	erase_char(7);

if (get_flag(13,6))
	if (get_flag(13,11) == 0)
		{i = 8;
		while (i <= 19)
			{erase_char(i);
			i = i + 1;
			}
		}

if (get_flag(13,0) != 0)
	end();

	reset_dialog();
	add_dialog_str(0,"You walk past the doorway and down a long, narrow passageway that begins to descend after short ways. The passage expands a little as you progress. The footing, meanwhile, grows more solid, and the path is more clearly marked out.",0);
	add_dialog_str(1,"Then you come to an opening. You look through it, and the view takes your breath away. You can see a vast city of lights with a river running through the middle, extending deep into the distance farther than you can see.",0);
	add_dialog_str(2,"Below you, you can see a whole swarm of activity. Glowing ghosts crawl over the city like ants, and you can see demons, too, as they traverse the ancient pathways of the slithzerikai city.",0);
	dummy = run_dialog(1);
	
	large_draw_pic_dialog(554,"Bahssikava!");

	activate_hidden_group(1);
	force_instant_terrain_redraw();
	message_dialog("You walk down from the tunnel, entering the large cavern. From here, you can see the devastation caused by the cavequake: the river that you've been following used to pass right in front of you.","Now right in front of you is a huge pile of rubble. The ceiling caved in right over the river and changed its course, simultaneously destroying all nearby houses and eliminating the Bahssikavans' means of survival.");
	
	if (get_flag(250,9)) {
		reset_dialog();
		add_dialog_str(0,"You have reached another cut scene, one in which a ghost describes what you must do in Bahssikava to free the ghosts. Would you like to skip it or see it?",0);
		add_dialog_choice(0,"Skip it.");
		add_dialog_choice(1,"See it.");
		if (run_dialog(1) == 1)
			set_state_continue(27);
		}
	
	pause(2 * get_flag(250,8));
	
	text_bubble_on_char(6,"You have returned!");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"We had feared that you wouldn't!");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"Listen carefully:");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"In our terrible arrogance,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"we Bahssikavans sought to open");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"the steel doors that have for so long");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"barred the way to our homeland.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"Sixty years ago,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"we attacked the doors");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"with every spell that we had.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"We threw fireballs, lightning,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"and all manner of force at the doors.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"They did not open.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"Worse, the gods of Bahssikava");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"chose to punish us for our arrogance.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"A great cavequake destroyed our city,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"and those who did not perish then");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"wished they had;");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"mighty and terrible demons attacked");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"our peaceful city");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"and enslaved our souls.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"For sixty years, we have served");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"our dark masters in horrific bondage,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"awaiting individuals powerful enough");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"to free us.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"If you would free us,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"you must travel west,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"where resides the demon lord,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"Nolagh-Khar,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"in his strong fortress,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"Khar-Grav.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"If you can defeat Nolagh-Khar,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"we will be free at last");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"from our terrible burden.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	force_instant_terrain_redraw();
	pause(5 * get_flag(250,8));
	
	text_bubble_on_char(6,"All our prayers go with you.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"You are our only hope.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	erase_char(6);
	
	if (char_ok(4))
		{text_bubble_on_char(4,"Interesting.");
		force_instant_terrain_redraw();
		pause(6 * get_flag(250,8));
		
		set_character_facing(first_group_member(0),6);
		text_bubble_on_char(4,"");
		text_bubble_on_char(first_group_member(0),"What?");
		force_instant_terrain_redraw();
		pause(6 * get_flag(250,8));
		
		text_bubble_on_char(first_group_member(0),"");
		text_bubble_on_char(4,"Nothing.");
		force_instant_terrain_redraw();
		pause(6 * get_flag(250,8));
		
		text_bubble_on_char(4,"");
		force_instant_terrain_redraw();
		pause(5 * get_flag(250,8));
		
		text_bubble_on_char(4,"Really, it's nothing.");
		force_instant_terrain_redraw();
		pause(6 * get_flag(250,8));
		
		text_bubble_on_char(4,"");
		text_bubble_on_char(first_group_member(0),"Whatever.");
		force_instant_terrain_redraw();
		pause(6 * get_flag(250,8));
		
		set_character_facing(first_group_member(0),4);
		text_bubble_on_char(first_group_member(0),"");
		force_instant_terrain_redraw();
		}
	
	message_dialog("The Bahssikavans' plight sounds dire. However, you'd better watch your step. If there is an army of demons nearby that is strong enough to subdue an entire city's worth of Bahssikavan sliths, then you are in dangerous territory for sure.","");
	
	set_flag(13,0,1);
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
if (get_flag(13,10) == 1)
	{div_start = get_current_tick();
	set_flag(13,10,2);
	}
	
if (get_flag(13,10) == 2)
	if (tick_difference(div_start,get_current_tick()) > 30)
		{set_flag(13,10,3);
		relocate_character(36,2,27);
		relocate_character(37,2,28);
		relocate_character(38,2,29);
		relocate_character(39,2,30);
		alert_char(36);
		alert_char(37);
		alert_char(38);
		alert_char(39);
		}
	
if (get_flag(13,2) != 1)
	end();
if (tick_difference(dummy,get_current_tick()) <= 1)
	end();
	
	dummy = get_current_tick();

	if (get_ran(1,1,4))
		{i = get_ran(1,20,24);
		j = get_ran(1,8,19);
		set_character_pose(i,1);
		put_boom_on_char(j,6,0);
		put_jagged_zap(char_loc_x(i),char_loc_y(i),char_loc_x(j),char_loc_y(j),5);
		run_animation_sound(-102);
		set_character_pose(i,0);
		}
	
	i = get_ran(1,8,10);
	set_character_pose(i,1);
	put_effect_on_space(char_loc_x(i) - 1,char_loc_y(i),9,1,2);
	run_animation_sound(-69);
	
	i = get_ran(1,11,13);
	set_character_pose(i,1);
	put_effect_on_space(char_loc_x(i),char_loc_y(i) - 1,9,1,2);
	run_animation_sound(-69);
	
	i = get_ran(1,14,16);
	set_character_pose(i,1);
	put_effect_on_space(char_loc_x(i) + 1,char_loc_y(i),9,1,2);
	run_animation_sound(-69);
	
	i = get_ran(1,17,19);
	set_character_pose(i,1);
	put_effect_on_space(char_loc_x(i) + 1,char_loc_y(i),9,1,2);
	run_animation_sound(-69);
break;

beginstate 10;
if ((get_flag(250,4) == 1) && (has_item(451) == 0))
	{message_dialog("You can't just leave Phaedra behind. You must take her with you to the guardpost immediately or she will die.","");
	block_entry(1);
	end();
	}

	reset_dialog();
	add_dialog_str(0,"There is a narrow, dark passage here. Do you wish to walk down it?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Walk down the passage.");
	choice = run_dialog(1);
	if (choice == 1)
		block_entry(1);
	if (choice == 2)
		move_to_new_town(12,27,42);
break;

beginstate 11;
if (get_flag(13,1) != 0)
	end();

	reset_dialog();
	add_dialog_str(0,"This room is evidently a storage room for the mine. All the crates and barrels here are filled to the brim with crystals, which is evidently what they are mining here.",0);
	add_dialog_str(1,"But why? This is very peculiar.",0);
	set_flag(13,1,run_dialog(1));
break;

beginstate 12;
if ((get_flag(13,3) == 0) && (get_flag(13,17) == 0))
	{set_attitude(1001,4);
	set_flag(13,2,1); }
break;

beginstate 13;
	set_attitude(1001,10);
	set_flag(13,2,0);
break;

beginstate 14;
if ((get_flag(13,4) != 0) || (get_flag(13,17) != 0))
	end();

	message_dialog("Through this window, you have a perfect view of the center of the mine. Bahssikavan ghosts work with the tireless energy of the dead digging crystals out of this pile of rubble. Their demon slavemasters tear into them if they slow down even for a moment.","It is a grim sight indeed. The demons cannot see you up here, but you get the feeling that if you went down to the mining area, you would be torn apart very quickly. There are an awful lot of haakai there.");
	set_flag(13,4,1);
break;

beginstate 15;
if (get_flag(13,5) != 0)
	end();

	message_dialog("In front of you is a mass grave. Heaped bodies rot here, overgrown with fungus, most still in the positions of terror in which they died, eyes gaping, mouths screaming, arms and legs in disjointed, agonized positions.","You feel bile rising in your throat and quickly look away. This must be the work of the demons. No other beings could be so callous or have such utter disregard for life.");
	set_flag(13,5,1);
break;

beginstate 16;
	block_entry(1);
	message_dialog("You can't walk in here among the dead. Even standing this close nearly makes you vomit.","");
break;

beginstate 17;
if (get_flag(250,4) == 1) // Phaedra dead
	{add_dialog_str(0,"There is a small passage here. However, you can't go this way: Phaedra needs you to bring her back to the guardpost.",0);
	dummy = run_dialog(1);
	block_entry(1);
	end();
	}

	reset_dialog();
	add_dialog_str(0,"There is a small passage here. Do you wish to walk down it?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Walk down the passage.");
	choice = run_dialog(1);
	if (choice == 1)
		block_entry(1);
	if (choice == 2)
		move_to_new_town(14,57,23);
break;

beginstate 18;
if (get_flag(13,7) != 0)
	end();

	message_dialog("This room is filled with broken machine parts. The apparatus around you must break down occasionally, and when it does, this is the place where the parts go to get repaired.","");
	set_flag(13,7,1);
break;

beginstate 19;
if (get_flag(13,8) != 0)
	end();

	message_dialog("These look like diagrams describing how to repair the machines. You can't make heads or tails of them, though. These demons must be more technically advanced than most people think demons can be.","");
	set_flag(13,8,1);
break;

beginstate 20;
if (get_flag(13,10))
	{message_dialog("The controls seem to be stuck. You can't use this control panel anymore.","");
	end(); }
	
	reset_dialog();
	add_dialog_str(0,"You have arrived at a very complicated control panel. It has a lot of buttons, switches, dials, and lights. It also has no instructions anywhere nearby, and you have no idea what any of this does.",0);
	if (get_flag(15,17))
		add_dialog_str(1,"The hidden ghost in the north of the demon holdings told you to create a diversion here, though, and this is surely the place to do it.",0);
	add_dialog_str(2,"What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Fiddle with the controls.");
	if (run_dialog(1) == 1)
		end();
	
	set_flag(13,10,1);
	message_dialog("You push a few buttons and turn a few dials. Some lights turn on, and some turn off. Then you push one particular button, and a very, VERY loud alarm goes off.","You'd better get out of here before anyone comes to find out what's going on.");
	activate_hidden_group(2);
	erase_char(20);
	erase_char(22);
	erase_char(24);
	erase_char(34);
break;

beginstate 21;
if (get_flag(13,9) != 0)
	end();

	message_dialog("In this room, a whole array of complex machines are at work. They appear to be processing the water from the river. It flows from the pumps in the north through a series of machines and eventually dumps out into the pool.","");
	set_flag(13,9,1);
break;

beginstate 22;
	message_dialog("These two wheels are presumably to close the gates to the west and east. They are very large and heavy. They were made for enormous demons to turn, not people your size. You can't do anything here.","");
break;

beginstate 23;
	block_entry(1);
	reset_dialog();
	add_dialog_str(0,"This part of the river is particularly shallow, and you may be able to wade across it. Do you wish to attempt to?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Wade across.");
	if (run_dialog(1) == 1)
		end();
		
	if (is_combat())
		{message_dialog("You try to hurry across still holding your weapons, but you get no farther than a few feet before stepping in an inopportune place and getting stuck. You have to turn back around. Rushing across is not a good idea.","(You can't cross the river in combat mode.)");
		end(); }
		
	i = 0;
	while (char_ok(i) == 0)
		{i = i + 1; }
	
	while (char_loc_x(i) > 37)
		{march_party(char_loc_x(i) - 1,char_loc_y(i));
		force_view_center(char_loc_x(i),char_loc_y(i));
		force_instant_terrain_redraw();
		play_sound(85);
		pause(1 * get_flag(250,8));
		}
		
	j = char_loc_y(i);
	while ((char_on_loc(38,j) > 0) && (char_loc_y(i) > 14))
		{march_party(37,char_loc_y(i) - 1);
		force_view_center(char_loc_x(i),char_loc_y(i));
		force_instant_terrain_redraw();
		play_sound(85);
		pause(1 * get_flag(250,8));
		}
		
	while (char_on_loc(38,j) > 0)
		{march_party(char_loc_x(i) - 1,14);
		force_view_center(char_loc_x(i),char_loc_y(i));
		force_instant_terrain_redraw();
		play_sound(85);
		pause(1 * get_flag(250,8));
		}
break;

beginstate 24;
if (get_flag(13,11) != 0)
	end();
	
	if ((get_flag(13,10) == 0) || (char_ok(24) == 1))
		message_dialog("You charge into the mining center ready to free the ghost slaves! However, to your dismay, they look up at you with fear. There are still a LOT of haakai around here, and they don't turn on their masters and help you.","That must mean that they don't think you'll win.");
	else
		{message_dialog("You charge into the mining center ready to free the ghost slaves! You look around, and there aren't very many haakai here. They must have gone off to investigate the alarm in the southeast.","When you arrive, the ghosts rise up against their masters and fight on your side! They are weakened by years of captivity, but they are still numerous.");
		set_flag(13,12,1);
		}
	change_blocked(20,27,0);
	change_blocked(20,28,0);
	change_blocked(24,29,0);
	change_blocked(25,29,0);
	change_blocked(21,27,0);
	change_blocked(22,27,0);
	change_blocked(23,27,0);
	change_blocked(24,27,0);
	change_blocked(25,27,0);
	change_blocked(21,28,0);
	change_blocked(22,28,0);
	change_blocked(23,28,0);
	change_blocked(24,28,0);
	change_blocked(25,28,0);
	set_flag(13,11,1);
break;

beginstate 25;
	inc_flag(13,13,1);
	
	j = 0;
	i = 20;
	while (i <= 24)
		{if (char_ok(i))
			j = j + 1;
		i = i + 1;
		}
	i = 33;
	while (i <= 35)
		{if (char_ok(i))
			j = j + 1;
		i = i + 1;
		}
	if (j > 1)
		end();
	
	message_dialog("The last haakai receives a terrible blow, finally killing it.","");
	set_flag(13,17,1);
	
	j = 0;
	i = 8;
	while (i <= 19)
		{if (char_ok(i))
			j = 1;
		i = i + 1;
		}
	
	if (j == 0)
		{message_dialog("Although you have defeated the haakai guarding the mine, all of the slith ghosts died in the battle. You have failed to free them.","");
		set_flag(13,14,1);
		end();
		}
	
	reset_dialog();
	add_dialog_str(0,"The remaining ghosts look upon you with approval. You have freed them from slavery. One begins to speak to you, and you can understand it!",0);
	add_dialog_str(1,"_We must disappear now,_ one says, _but you have done us a great service, and we will help you fight Nolagh-Khar. We will be with you in the final battle._",0);
	add_dialog_str(2,"_You can find other allies here, too. More slith spirits are imprisoned in crystals west of here. The room is heavily guarded, and they will not be easy to free, but if you can do so, they will help you._",0);
	if (diversion)
		add_dialog_str(3,"_Go now, and continue your good work._ With that, the spirits vanish, and you are alone in the room. You may not be alone very long, though, as your diversion has probably worn off by now.",0);
	else
		add_dialog_str(3,"_Go now, and continue your good work._ With that, the spirits vanish, and you are alone in the room.",0);
	set_flag(13,6,run_dialog(1)); 
	
	award_party_xp(100,50);
	
	i = 8;
	while (i <= 19)
		{erase_char(i);
		i = i + 1;
		}
break;

beginstate 26;
if (get_flag(13,15) == 0)
	{set_flag(13,15,1);
	message_dialog("The river ahead is fairly shallow, though wide. You think you could wade across if you needed to, although you'd get pretty wet.","");
	}
break;

beginstate 27;
	reset_dialog();
	add_dialog_str(0,"The ghost tells you about the demon lord who now rules Bahssikava, named Nolagh-Khar. To free the slith ghosts, you have to travel west, to Nolagh-Khar's stronghold, Khar-Grav, and kill him.",0);
	set_flag(13,0,run_dialog(1));
	
	erase_char(6);
break;