begintownscript;

variables;

	short i,j;
	string text;
body;

beginstate INIT_STATE;
	if(get_flag(1,4) == 2){
		set_terrain(1,10,0);
		set_terrain(13,10,0);
	}
	if(get_flag(1,5) == 2){
		set_terrain(25,13,0);
		set_terrain(37,13,0);
	}
	if(get_flag(1,6) == 2){
		set_terrain(25,34,0);
		set_terrain(37,34,0);
	}
break;

beginstate EXIT_STATE;
// Always called when the town is left.
break;

beginstate START_STATE;
	if(get_flag(1,9)==2){
		message_dialog("That was quite unexpected and frightening. The ring looks perfectly ordinary now, though, and you have a feeling that it must be important.","");
		set_flag(1,9,3);
	}
break;

beginstate 10;
	move_to_new_town(0,14,26);
break;

beginstate 11;
	i=2;
	while(i<=9){
		set_floor(10,i,71);
		set_terrain(10,i,1);
		set_height(10,i,1);
		i = i + 1;
	}
break;

beginstate 12;
	i=2;
	while(i<=9){
		set_floor(10,i,7);
		set_terrain(10,i,295);
		set_height(10,i,27 + i);
		i = i + 1;
	}
break;

beginstate 13;
	teleport_party(14,10,1);
break;

beginstate 14;
	teleport_party(1,9,1);
break;

beginstate 15;
	teleport_party(34,1,1);
break;

beginstate 16;
	teleport_party(22,2,1);
break;

beginstate 17;
	i=2;
	while(i<=9){
		set_floor(34,i,71);
		set_terrain(34,i,1);
		set_height(34,i,1);
		i = i + 1;
	}
break;

beginstate 18;
	i=2;
	while(i<=9){
		set_floor(34,i,7);
		set_terrain(34,i,295);
		set_height(34,i,27 + i);
		i = i + 1;
	}
break;

beginstate 19;
	teleport_party(37,10,1);
	set_character_facing(0,4);
break;

beginstate 20;
	teleport_party(25,9,1);
break;

beginstate 21;
	teleport_party(10,13,1);
break;

beginstate 22;
	teleport_party(46,2,1);
break;

beginstate 23;
	i=14;
	while(i<=21){
		set_floor(10,i,71);
		set_terrain(10,i,1);
		set_height(10,i,1);
		i = i + 1;
	}
break;

beginstate 24;
	i=14;
	while(i<=21){
		set_floor(10,i,7);
		set_terrain(10,i,295);
		set_height(10,i,15 + i);
		i = i + 1;
	}
break;

beginstate 25;
	teleport_party(13,22,1);
	set_character_facing(0,4);
break;

beginstate 26;
	teleport_party(1,21,1);
break;

beginstate 27;
	teleport_party(34,13,1);
break;

beginstate 28;
	teleport_party(22,14,1);
break;

beginstate 29;
	i=14;
	while(i<=21){
		set_floor(34,i,71);
		set_terrain(34,i,1);
		set_height(34,i,1);
		i = i + 1;
	}
break;

beginstate 30;
	i=14;
	while(i<=21){
		set_floor(34,i,7);
		set_terrain(34,i,295);
		set_height(34,i,15 + i);
		i = i + 1;
	}
break;

beginstate 31;
	teleport_party(37,22,1);
	set_character_facing(0,4);
break;

beginstate 32;
	teleport_party(25,21,1);
break;

beginstate 33;
	teleport_party(10,25,1);
break;

beginstate 34;
	teleport_party(46,14,1);
break;

beginstate 35;
	i=26;
	while(i<=33){
		set_floor(10,i,71);
		set_terrain(10,i,1);
		set_height(10,i,1);
		i = i + 1;
	}
break;

beginstate 36;
	i=26;
	while(i<=33){
		set_floor(10,i,7);
		set_terrain(10,i,295);
		set_height(10,i,3 + i);
		i = i + 1;
	}
break;

beginstate 37;
	teleport_party(13,34,1);
	set_character_facing(0,4);
break;

beginstate 38;
	teleport_party(1,33,1);
break;

beginstate 39;
	teleport_party(34,25,1);
break;

beginstate 40;
	teleport_party(22,26,1);
break;

beginstate 41;
	i=26;
	while(i<=33){
		set_floor(34,i,71);
		set_terrain(34,i,1);
		set_height(34,i,1);
		i = i + 1;
	}
break;

beginstate 42;
	i=26;
	while(i<=33){
		set_floor(34,i,7);
		set_terrain(34,i,295);
		set_height(34,i,3 + i);
		i = i + 1;
	}
break;

beginstate 43;
	teleport_party(38,34,1);
break;

beginstate 44;
	teleport_party(25,33,1);
break;

beginstate 45;
	teleport_party(10,37,1);
break;

beginstate 46;
	teleport_party(46,26,1);
break;

beginstate 47;
	i=38;
	while(i<=45){
		set_floor(10,i,71);
		set_terrain(10,i,1);
		set_height(10,i,1);
		i = i + 1;
	}
break;

beginstate 48;
	i=38;
	while(i<=45){
		set_floor(10,i,7);
		set_terrain(10,i,295);
		set_height(10,i,i - 9);
		i = i + 1;
	}
break;

beginstate 49;
	teleport_party(13,46,1);
	set_character_facing(0,4);
break;

beginstate 50;
	if(get_flag(1,9)==1){
		message_dialog("You can't ignore your enemy. You have to stand and fight.","");
		block_entry(1);
		end();
	}
	teleport_party(1,45,1);
break;

beginstate 51;
	if(get_flag(1,9)==1){
		message_dialog("You can't ignore your enemy. You have to stand and fight.","");
		block_entry(1);
		end();
	}
	teleport_party(34,37,1);
break;

beginstate 52;
	teleport_party(22,38,1);
break;

beginstate 53;
	i=38;
	while(i<=45){
		set_floor(34,i,71);
		set_terrain(34,i,1);
		set_height(34,i,1);
		i = i + 1;
	}
break;

beginstate 54;
	i=38;
	while(i<=45){
		set_floor(34,i,7);
		set_terrain(34,i,295);
		set_height(34,i,i - 9);
		i = i + 1;
	}
break;

beginstate 55;
	teleport_party(37,46,1);
	set_character_facing(0,4);
break;

beginstate 56;
	teleport_party(25,45,1);
break;

beginstate 57;
	move_to_new_town(2,10,1);
break;

beginstate 58;
	if(get_flag(1,4) < 2){
		reset_dialog_preset_options(0);
		if(get_flag(1,4) == 0){
			add_dialog_str(0,"There is another ampersand chalked on this bit of the wall. You have a strange urge to erase it.",0);
			set_flag(1,4,1);
		}
		else if(get_flag(1,4) == 1){
			clear_buffer();
			append_string("You walk past a chalked ampersand.");
			if(get_flag(1,14)<2)
				append_string(" You have a strange urge to erase it.");
			get_buffer_text(text);
			add_dialog_str(0,text,0);
		}
		add_dialog_choice(0,"Leave it alone");
		add_dialog_choice(1,"Erase it");
		i = run_dialog(1);
		if(i == 2){
			message_dialog("You rub out the symbol.","");
			set_flag(1,4,2);
			inc_flag(10,1,1);
			set_terrain(1,10,0);
			set_terrain(13,10,0);
			if(get_flag(10,1)>=3){
				i = get_current_tick() + get_ran(1,100,150);
				set_flag(10,2,i/256);
				set_flag(10,3,i%256);
			}
		}
	}
	else if((get_flag(1,4)==2) && (char_has_item(0,445))){
		reset_dialog_preset_options(0);
		add_dialog_str(0,"Since you have chalk now, you could restore this ampersand that you erased.",0);
		if(get_flag(1,14)>=2)
			add_dialog_str(1,"Given your suspicions about how these symbols relate to your fading out, you think you'd better draw it back.",0);
		add_dialog_choice(0,"Redraw the ampersand");
		add_dialog_choice(1,"Leave it alone");
		i = run_dialog(1);
		if(i == 1){
			message_dialog("You carefully redraw the ampersand.","");
			set_flag(1,4,1);
			inc_flag(10,1,-1);
			set_terrain(1,10,359);
			set_terrain(13,10,359);
			if(get_flag(10,1)==3)
				message_dialog("You feel a bit more solid now, as though you won't fade out completely. You're still not quite back to normal, though.","");
			else if(get_flag(10,1)==2)
				message_dialog("Now you finally feel fully solid again.","");
		}
	}
break;

beginstate 59;
	if(get_flag(1,5) < 2){
		reset_dialog_preset_options(0);
		if(get_flag(1,5) == 0){
			add_dialog_str(0,"There is another ampersand chalked on this bit of the wall. You have a strange urge to erase it.",0);
			set_flag(1,5,1);
		}
		else if(get_flag(1,5) == 1){
			clear_buffer();
			append_string("You walk past a chalked ampersand.");
			if(get_flag(1,14)<2)
				append_string(" You have a strange urge to erase it.");
			get_buffer_text(text);
			add_dialog_str(0,text,0);
		}
		add_dialog_choice(0,"Leave it alone");
		add_dialog_choice(1,"Erase it");
		i = run_dialog(1);
		if(i == 2){
			message_dialog("You rub out the symbol.","");
			set_flag(1,5,2);
			inc_flag(10,1,1);
			set_terrain(25,13,0);
			set_terrain(37,13,0);
			if(get_flag(10,1)>=3){
				i = get_current_tick() + get_ran(1,100,150);
				set_flag(10,2,i/256);
				set_flag(10,3,i%256);
			}
		}
	}
	else if((get_flag(1,5)==2) && (char_has_item(0,445))){
		reset_dialog_preset_options(0);
		add_dialog_str(0,"Since you have chalk now, you could restore this ampersand that you erased.",0);
		if(get_flag(1,14)>=2)
			add_dialog_str(1,"Given your suspicions about how these symbols relate to your fading out, you think you'd better draw it back.",0);
		add_dialog_choice(0,"Redraw the ampersand");
		add_dialog_choice(1,"Leave it alone");
		i = run_dialog(1);
		if(i == 1){
			message_dialog("You carefully redraw the ampersand.","");
			set_flag(1,5,1);
			inc_flag(10,1,-1);
			set_terrain(25,13,358);
			set_terrain(37,13,358);
			if(get_flag(10,1)==3)
				message_dialog("You feel a bit more solid now, as though you won't fade out completely. You're still not quite back to normal, though.","");
			else if(get_flag(10,1)==2)
				message_dialog("Now you finally feel fully solid again.","");
		}
	}
break;

beginstate 60;
	if(get_flag(1,6) < 2){
		reset_dialog_preset_options(0);
		if(get_flag(1,6) == 0){
			add_dialog_str(0,"There is another ampersand chalked on this bit of the wall. You have a strange urge to erase it.",0);
			set_flag(1,6,1);
		}
		else if(get_flag(1,6) == 1){
			clear_buffer();
			append_string("You walk past a chalked ampersand.");
			if(get_flag(1,14)<2)
				append_string(" You have a strange urge to erase it.");
			get_buffer_text(text);
			add_dialog_str(0,text,0);
		}
		add_dialog_choice(0,"Leave it alone");
		add_dialog_choice(1,"Erase it");
		i = run_dialog(1);
		if(i == 2){
			message_dialog("You rub out the symbol.","");
			set_flag(1,6,2);
			inc_flag(10,1,1);
			set_terrain(25,34,0);
			set_terrain(37,34,0);
			if(get_flag(10,1)>=3){
				i = get_current_tick() + get_ran(1,100,150);
				set_flag(10,2,i/256);
				set_flag(10,3,i%256);
			}
		}
	}
	else if((get_flag(1,6)==2) && (char_has_item(0,445))){
		reset_dialog_preset_options(0);
		add_dialog_str(0,"Since you have chalk now, you could restore this ampersand that you erased.",0);
		if(get_flag(1,14)>=2)
			add_dialog_str(1,"Given your suspicions about how these symbols relate to your fading out, you think you'd better draw it back.",0);
		add_dialog_choice(0,"Redraw the ampersand");
		add_dialog_choice(1,"Leave it alone");
		i = run_dialog(1);
		if(i == 1){
			message_dialog("You carefully redraw the ampersand.","");
			set_flag(1,6,1);
			inc_flag(10,1,-1);
			set_terrain(25,34,359);
			set_terrain(37,34,359);
			if(get_flag(10,1)==3)
				message_dialog("You feel a bit more solid now, as though you won't fade out completely. You're still not quite back to normal, though.","");
			else if(get_flag(10,1)==2)
				message_dialog("Now you finally feel fully solid again.","");
		}
	}
break;

beginstate 61;
	if(get_flag(1,7) == 0){
		message_dialog("For some time now the feeling that you are being watched has been growing, but now you're sure something must be there. ","You look around nervously but still see nothing.");
		set_flag(1,7,1);
	}
break;

beginstate 62;
	if((get_flag(1,9)==0) && (char_has_item(0,206))){
		reset_dialog_preset_options(0);
		add_dialog_str(0,"You suddenly trip on a step. You catch yourself and don't fall, but the silver ring drops out of your pocket.",0);
		add_dialog_choice(0,"Oops");
		run_dialog(0);
		i=0;
		while(i<40){
			if(item_type_in_slot(0,i)==206)
				j=i;
			i = i + 1;
		}
		take_item_char_item(0,j,22,44);
		set_terrain(22,44,378);
		force_instant_terrain_redraw();
		pause(1);
		move_item_on_spot(22,44,-1,-1);
		put_item_on_spot(22,44,207);
		set_terrain(22,44,377);
		force_instant_terrain_redraw();
		pause(1);
		move_item_on_spot(22,44,-1,-1);
		put_item_on_spot(22,44,206);
		set_terrain(22,44,295);
		force_instant_terrain_redraw();
		play_sound(69);
		//move_item_on_spot(22,44,22,43);
		move_item_on_spot(22,44,-1,-1);
		put_item_on_spot(22,43,207);
		set_terrain(22,43,378);
		force_instant_terrain_redraw();
		pause(1);
		move_item_on_spot(22,43,-1,-1);
		put_item_on_spot(22,43,206);
		set_terrain(22,43,377);
		force_instant_terrain_redraw();
		pause(1);
		move_item_on_spot(22,43,-1,-1);
		put_item_on_spot(22,43,207);
		set_terrain(22,43,295);
		force_instant_terrain_redraw();
		play_sound(69);
		//move_item_on_spot(22,43,22,42);
		move_item_on_spot(22,43,-1,-1);
		put_item_on_spot(22,42,206);
		set_terrain(22,42,378);
		force_instant_terrain_redraw();
		pause(1);
		move_item_on_spot(22,42,-1,-1);
		put_item_on_spot(22,42,207);
		set_terrain(22,42,377);
		force_instant_terrain_redraw();
		pause(1);
		move_item_on_spot(22,42,-1,-1);
		put_item_on_spot(22,42,206);
		set_terrain(22,42,295);
		force_instant_terrain_redraw();
		play_sound(69);
		//move_item_on_spot(22,42,22,41);
		move_item_on_spot(22,42,-1,-1);
		put_item_on_spot(22,41,207);
		set_terrain(22,41,377);
		force_instant_terrain_redraw();
		pause(1);
		move_item_on_spot(22,41,-1,-1);
		move_item_on_spot(48,1,22,41);
		//put_item_on_spot(22,41,206);
		set_terrain(22,41,295);
		force_instant_terrain_redraw();
		play_sound(69);
		reset_dialog_preset_options(0);
		add_dialog_str(0,"The ring comes to a rest a few steps down from you. You think everything is fine until gray mist begins to rise up from the ring.",0);
		add_dialog_choice(0,"Uh oh");
		run_dialog(0);
		put_effect_on_space(22,41,5,5,1);
		run_animation_sound(52);
		activate_hidden_group(1);
		set_flag(1,9,1);
	}
break;

beginstate 63;
	if(get_flag(1,18)==0){
		message_dialog("You're beginning to feel that this place goes on forever, and you're not learning anything more about where it is or why you might be here.","You stop for a moment and strain to think of anything that you might remember from before waking up on the ledge, but you still cannot come up with anything.");
		message_dialog("Desperately, you reconsider what you already know, but it seems useless. You have ordinary possessions that don't suggest any profession, and you don't seem to have any particular skills. You are in a huge featureless shaft that seems to be underground.","None of it makes any sense or reminds you of anything. After a while you give up and decide to continue onward. At least then there's the slight possiblity of seeing something new.");
		set_flag(1,18,1);
	}
break;

beginstate 64;
	if(get_flag(1,21)==0){
		message_dialog("You hear strange faint music floating up from the depths. You wonder if there's somebody down there.","");
		//play_sound(-174);
		set_flag(10,5,get_current_tick()/256);
		set_flag(10,6,get_current_tick()%256);
		set_flag(1,21,1);
	}
break;