// TOWN SCRIPT
//    Town 1: Nebulous

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documentation. States you write
// yourself should be numbered from 10-100.

// flags:

begintownscript;

variables;

short i, j, x, y, itm, slt, choice, fin, tg;

body;

beginstate INIT_STATE;
	turn_off_training(1);
	set_crime_tolerance(3);

	// set_name(6, "<name>");
	// set_char_dialogue_pic(6, xxxx, 0);
break;

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

beginstate START_STATE;
	if (get_flag(1, 0) == 0) {
		set_flag(1, 0, 1);
		play_sound(-96);
		reset_dialog();
		add_dialog_str(0, "You find yourself floating in blackness, are you even awake?", 0);
		add_dialog_str(1, "You strain your eyes, but see nothing around you.  Perhaps you are in an alley?", 0);
		add_dialog_str(2, "You feel perhaps, you can move, but move to where?", 0);
		add_dialog_choice(0, "Ok");
		choice = run_dialog(1);
		// take everything now
		tg = TRUE;
		set_state_continue(11);
	}
break;

beginstate 10;
	block_entry(1);
	set_incidental_sound(1);
	// put in alleyway now...
	move_to_new_town(0, 37, 7);
break;

beginstate 11;
	// take all equipment from user
	j = 0;
	// because item_type/take_item doesn't seem to work with one pass, do it several times
	while (j < 5) {
		i = 0;
		while (i < 4) {
			if (i == 0) {
				x = 51;
				y = 59;
			}
			else if (i == 1) {
				x = 54;
				y = 59;
			}
			else if (i == 2) {
				x = 51;
				y = 62;
			}
			else {
				x = 54;
				y = 62;
			}
			if (char_ok(i) == TRUE) {
				slt = 0;
				while (slt < 40) {
					itm = item_type_in_slot(i, slt);
					if (get_flag(50, 2) == 1) {
						// going through again, leave their clothes, other items
						if ((itm == 18) || (itm == 19) || (itm == 20) ||
						    (itm == 21) || (itm == 23) || 
						    ((itm >= 450) && (itm <= 457))) {
							itm = -1;
						}
					}
					if (itm != -1) {
						take_item_char_item(i, slt, x, y);
					}
					slt = slt + 1;
				}
			}
			i = i + 1;
		}
		j = j + 1;
	}
	if (tg == TRUE) {
		set_state_continue(12);
	}
	else {
		move_to_new_town(0, 37, 7);
	}
break;

beginstate 12;
	// take party gold, let 'em keep up to 50 (+ 5 per pc > 1)...
	j = 50 + ((party_size() - 1) * 5);
	i = coins_amount() - j;
	if (i > 0) {
		i = 0 - i;
		change_coins(i);
	}
	// strip spell energy at game start
	run_scenario_script(19);
break;

beginstate 13;
	// unused - was originally going to be used to strip spells.
	// ended up, taking away power every few turns was a better approach.
break;

beginstate 14;
	if (get_flag(1, 1) == 0) {
		set_flag(1, 1, 1);
		message_dialog("Ahead of you in a lighted room appears to be your stuff from your previous life.", "It all seems to be here, carefully stacked on some tables.");
	}
break;

beginstate 15;
	block_entry(1);
	move_to_new_town(0, 1, 45);
break;

beginstate 16;
	block_entry(1);
	fin = get_flag(50, 1);
	reset_dialog();
	add_dialog_str(0, "Ahead of you is a stairway out.", 0);
	if (fin == 0) {
		if (get_flag(3, 6) == 4) {
			add_dialog_str(1, "You failed to correctly answer the gate keepers riddles.", 0);
		}
		else {
			add_dialog_str(1, "You sniffed a branch to make it to here.", 0);
		}
	}
	else {
		add_dialog_str(1, "You feel ready to leave.", 0);
	}
	add_dialog_choice(0, "Stay here.");
	add_dialog_choice(1, "Leave for other adventures elsewhere.");
	if ((fin == 0) && (get_flag(3, 6) < 3)) {
		add_dialog_choice(2, "Stay here (return to alley).");
	}
	choice = run_dialog(0);
	if (choice == 2) {
		reset_dialog();
		if (fin > 0) {
			add_dialog_str(0, "You have successfully found your way out of Pergamos with the answers you sought.", 0);
			add_dialog_str(1, "You hope something like it never happens to you again.", 0);
		}
		else {
			add_dialog_str(0, "You have woken up from the nightmare of Pergamos.", 0);
			if (get_flag(3, 6) == 0) {
				add_dialog_str(1, "You still don't have all of the answers you were seeking or have a clue why you were there.", 0);
				add_dialog_str(2, "You leave it behind wondering if you should've stayed.", 0);
			}
			else {
				add_dialog_str(1, "You have failed to satisfy the gate keeper.", 0);
				add_dialog_str(2, "You know in leaving, you will not be able to rest wondering if he will be after you.", 0);
			}
		}
		add_dialog_choice(0, "Ok");
		choice = run_dialog(0);
		end_scenario(fin);
	}
	else if (choice == 3) {
		message_dialog("You find yourself being returned to the dirty alley in Pergamos.", "Perhaps now you can find the answers you seek.");
		// resteal all of the characters items again, leave gold...
		tg = FALSE;
		set_state_continue(11);
	}
break;
