// apollyon.txt v1.0 (based off of basicnpc.txt)
// M. G. Slack - 2007
// (slack at attglobal dot net)
// A very simple, naive text. Creature attacks anything it hates nearby.
// Once it has won, it returns to its home.  Replaces the talk state with
// a custom talk state which says how you got here and why and starts a
// final riddle in order to get through (may be one of three if you fail
// to answer correctly).  If you fail all three, you won't be rewarded, but
// will finish the game.
// Uses: 3, 5 = solved one of the riddles.
//       3, 6 = riddle count.
//       3, 7 = counter to decrement before asking next riddle.
//       123, x = random riddle load (first time script used).
// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0, wander randomly. 
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this 
//     is killed, set to 1. (Example: If cell 1 is 3 and cell 2 is 5, when
//     creature is killed, sets SDF(3,5) to 1.)

begincreaturescript;

variables;

short i, j, k, m, target, choice, r;
string introln, msgln;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2) {
		set_mobility(ME, 0);
	}
	// set up riddles to be called
	if (get_flag(123, 0) == 0) {
//		print_str("Initializing riddles, please wait...");
		i = 17;
		m = 23;
		while (i <= m) {
			k = FALSE;
			while (k == FALSE) {
				j = get_ran(1, 0, 6);
				if (get_flag(123, j) == 0) {
					k = TRUE;
				}
			}
			set_flag(123, j, i);
			i = i + 1;
		}
		// -- debug ---
//		clear_buffer();
//		append_string("Order:");
//		i = 0;
//		while (i < 7) {
//			append_string(" ");
//			append_number(get_flag(123, i));
//			i = i + 1;
//		}
//		get_buffer_text(msgln);
//		print_str("Riddles initialized.");
//		print_str(msgln);
	}
break;

beginstate DEAD_STATE;
	// Set the appropriate stuff done flag for this character being dead
	if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
		set_flag(get_memory_cell(1), get_memory_cell(2), 1);
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 (target_ok() == FALSE)
		set_state(START_STATE);
	do_attack();
break;

beginstate TALKING_STATE;
	if (get_flag(3, 7) > 0) {
		print_str("Apollyon does not want to talk to you yet.");
		inc_flag(3, 7, -1);
		set_state(START_STATE);
	}
	else {
		set_state_continue(10);
	}
break;

beginstate 10;
	if (get_flag(3, 5) > 0) {
		message_dialog("Apollyon ignores you and your questions.", "It's like talking to a statue.");
		set_state(START_STATE);
	}
	else if (get_flag(3, 6) == 0) {
		inc_flag(3, 6, 1);
		set_state_continue(11);
	}
	else if (get_flag(3, 6) == 1) {
		inc_flag(3, 6, 1);
		set_state_continue(12);
	}
	else if (get_flag(3, 6) == 2) {
		inc_flag(3, 6, 1);
		set_state_continue(13);
	}
break;

beginstate 11;
	clear_buffer();
	append_string("The man speaks, brushing off your questions.  'Hello ");
	append_char_name(get_selected_char());
	append_string(".  I am Apollyon.'");
	get_buffer_text(introln);
	reset_dialog();
	add_dialog_str(0, introln, 0);
	add_dialog_str(1, "'I am the keeper of the gate.'", 0);
	add_dialog_str(2, "'I have brought you here because you have taken your intelligence, your skill, your abilities, for granted.'", 0);
	add_dialog_str(3, "'Because of that, I've decided to play a game.'", 0);
	add_dialog_str(4, "'I took from you all of your previous trappings leaving you with just your skills and abilities.'", 0);
	add_dialog_str(5, "'You have succeeded in reaching me which indicates you are ready to move on.'", 0);
	add_dialog_choice(0, "Continue");
	choice = run_dialog(1);
	reset_dialog();
	add_dialog_str(0, "'I will ask you a riddle, and if answered correctly, I will let you pass with a reward.'", 0);
	add_dialog_str(1, "'If you miss it, I'll give you 2 more chances at my own time to answer another riddle.'", 0);
	add_dialog_str(2, "He continues 'If you miss all, you get nothing.'", 0);
	add_dialog_str(3, "'If you are ready, I'll ask my riddle.'", 0);
	add_dialog_str(4, "You are getting nervous as he continues to stare, almost right through you.", 0);
	add_dialog_choice(0, "Get riddle");
	choice = run_dialog(1);
	set_state_continue(14);
break;

beginstate 12;
	reset_dialog();
	add_dialog_str(0, "Apollyon stops your questioning.", 0);
	add_dialog_str(1, "'As promised, I'll test you again.' he says.", 0);
	add_dialog_str(2, "'If you miss this one, you'll get one more chance when I am ready.'", 0);
	add_dialog_str(3, "'If you are ready, I'll ask my riddle.'", 0);
	add_dialog_str(4, "His eyes, oh his eyes are very piercing.", 0);
	add_dialog_choice(0, "Get riddle");
	choice = run_dialog(1);
	set_state_continue(14);
break;

beginstate 13;
	reset_dialog();
	add_dialog_str(0, "Apollyon ignores your pleading bleats.", 0);
	add_dialog_str(1, "'You have failed twice to prove your abilities.' he says.", 0);
	add_dialog_str(2, "'You will not get another chance if you fail this time.' he adds.", 0);
	add_dialog_str(3, "'If you are ready, I'll ask my riddle.'", 0);
	add_dialog_str(4, "You feel hot and feverish under his gaze.", 0);
	add_dialog_choice(0, "Get riddle");
	choice = run_dialog(1);
	set_state_continue(14);
break;

beginstate 14;
	r = get_flag(3, 6) - 1;
	set_state_continue(get_flag(123, r));
break;

beginstate 15;
	// correctly solved riddle
	set_flag(3, 5, 1);
	set_flag(50, 1, 1);
	reset_dialog();
	add_dialog_str(0, "Apollyon looks satisfied.  'You have correctly answered and you may leave.' he says.", 0);
	add_dialog_str(1, "'Accept this skill increase with my blessing.' he adds.", 0);
	add_dialog_str(2, "'Remember, do not take your abilities and standing in life for granted.'", 0);
	add_dialog_str(3, "His eyes seem to glow with fire 'Otherwise you may be back.'", 0);
	add_dialog_str(4, "With that, he goes silent.", 0);
	add_dialog_choice(0, "Ok");
	choice = run_dialog(1);
	award_party_xp(500, 25);
	i = 0;
	while (i < 4) {
		if (char_ok(i) == TRUE) {
			change_pc_skill_pts(i, 50);
		}
		i = i + 1;
	}
	// reset door, no going back now...
	run_town_script(29);
	set_state(START_STATE);
break;

beginstate 16;
	if (get_flag(3, 6) < 3) {
		set_flag(3, 7, get_ran(1, 1, ((r + 1) * 10)));
		message_dialog("'That is incorrect.' Apollyon says.", "He goes silent and motionless.");
	}
	else {
		// failed to solve any of the riddles
		set_flag(3, 5, 1);
		set_flag(3, 6, 4);
		reset_dialog();
		add_dialog_str(0, "Apollyon looks upon you stonily, his eyes piercing you to your soul.", 0);
		add_dialog_str(1, "'You have failed to demonstrate your gift of intelligence.' Apollyon says.", 0);
		add_dialog_str(2, "'You may leave, but with this punishment.' he states, before going silent.", 0);
		add_dialog_str(3, "With that, lightning and fire flash from his eyes searing you deeply.", 0);
		add_dialog_choice(0, "Ok");
		choice = run_dialog(1);
		i = 0;
		while (i < 4) {
			if (char_ok(i) == TRUE) {
				put_boom_on_char(i, 0, 0);
				run_animation();
				damage_char(i, get_ran(1, 1, 20), 4);
			}
			i = i + 1;
		}
		// reset door, no going back now...
		run_town_script(29);
	}
	set_state(START_STATE);
break;

// --- riddles start ---

beginstate 17;
	reset_dialog();
	add_dialog_str(0, "'There are two men, Left and Right.  One always lies, one always tells the truth.'", 0);
	add_dialog_str(1, "'Right says -Left always lies.-.  Left says -I do not, I tell the truth.-.'", 0);
	add_dialog_str(2, "'Left is now asked -yes or no, would Right say he is telling the truth?-.'", 0);
	add_dialog_str(3, "'Left will answer -yes-.'", 0);
	add_dialog_str(4, "'Who always tells the truth, Left or Right?' Apollyon asks.", 0);
	add_dialog_choice(0, "Left");
	add_dialog_choice(1, "Right");
	choice = run_dialog(0);
	if (choice == 2) {
		set_state_continue(15);
	}
	else {
		set_state_continue(16);
	}
break;

beginstate 18;
	reset_dialog();
	add_dialog_str(0, "'Some months have thirty days, some months have thirty-one days.'", 0);
	add_dialog_str(1, "'My question is, how many months have twenty-eight days?' Apollyon asks.", 0);
	add_dialog_choice(0, "Give answer");
	choice = run_dialog(0);
	i = 0;
	j = FALSE;
	while (i == 0) {
		get_text_response("Apollyon awaits your response (1 - 12)?");
		check_text_response_match("1");
		if (got_text_match() > 0)
			i = 1;
		check_text_response_match("2");
		if (got_text_match() > 0)
			i = 1;
		check_text_response_match("3");
		if (got_text_match() > 0)
			i = 1;
		check_text_response_match("4");
		if (got_text_match() > 0)
			i = 1;
		check_text_response_match("5");
		if (got_text_match() > 0)
			i = 1;
		check_text_response_match("6");
		if (got_text_match() > 0)
			i = 1;
		check_text_response_match("7");
		if (got_text_match() > 0)
			i = 1;
		check_text_response_match("8");
		if (got_text_match() > 0)
			i = 1;
		check_text_response_match("9");
		if (got_text_match() > 0)
			i = 1;
		check_text_response_match("10");
		if (got_text_match() > 0)
			i = 1;
		check_text_response_match("11");
		if (got_text_match() > 0)
			i = 1;
		check_text_response_match("12");
		if (got_text_match() > 0) {
			i = 1;
			j = TRUE;
		}
		if (i == 0)
			message_dialog("Enter a number from 1 to 12.", "");
	}
	if (j == TRUE) {
		set_state_continue(15);
	}
	else {
		set_state_continue(16);
	}
break;

beginstate 19;
	reset_dialog();
	add_dialog_str(0, "'I start two clocks at the same time.'", 0);
	add_dialog_str(1, "'One of them is two minutes per hour too slow and the other is one minute per hour too fast.'", 0);
	add_dialog_str(2, "'When I next looked at the clocks, the faster one was exactly one hour ahead.'", 0);
	add_dialog_str(3, "'How long have the clocks been running?' Apollyon asks.", 0);
	add_dialog_choice(0, "Give answer");
	choice = run_dialog(0);
	// only can be one response, so just check for it, all others are wrong
	get_text_response("Your response (# in hours)?");
	check_text_response_match("20");
	if (got_text_match() > 0) {
		set_state_continue(15);
	}
	else {
		set_state_continue(16);
	}
break;

beginstate 20;
	reset_dialog();
	add_dialog_str(0, "'I am the center of gravity, hold a capital situation in Vienna, and as I am foremost in every victory, am allowed by all to be invaluable.'", 0);
	add_dialog_str(1, "'Though I am invisible, I am clearly seen in the midst of a river.'", 0);
	add_dialog_str(2, "'I could name three who are in love with me and have three associates in vice.'", 0);
	add_dialog_str(3, "'It is vain that you seek me for I have long been in heaven yet even now lie embalmed in the grave.'", 0);
	add_dialog_str(4, "'What am I?' asks Apollyon.", 0);
	add_dialog_choice(0, "Give answer");
	choice = run_dialog(0);
	// can be one of three responses, so just check for them, all others are wrong
	get_text_response("Your response?");
	i = 0;
	check_text_response_match("THE LETTER V");
	if (got_text_match() > 0)
		i = 1;
	check_text_response_match("LETTER V");
	if (got_text_match() > 0)
		i = 1;
	check_text_response_match("V");
	if (got_text_match() > 0)
		i = 1;
	if (i > 0) {
		set_state_continue(15);
	}
	else {
		set_state_continue(16);
	}
break;

beginstate 21;
	reset_dialog();
	add_dialog_str(0, "'I stand aloft and regulate everyone's daily movements, yet feel no interest in your concerns.'", 0);
	add_dialog_str(1, "'I direct you when to go and when to come, yet care not whether you attend or not.'", 0);
	add_dialog_str(2, "'Still, I indifferent to your fate, often strike a blow to urge you on.'", 0);
	add_dialog_str(3, "'You feel no resentment of me when a reproof is given.'", 0);
	add_dialog_str(4, "'What am I?' asks Apollyon.", 0);
	add_dialog_choice(0, "Give answer");
	choice = run_dialog(0);
	// can be one of two responses, so just check for them, all others are wrong
	get_text_response("Apollyon awaits your response?");
	i = 0;
	check_text_response_match("A CLOCK");
	if (got_text_match() > 0)
		i = 1;
	check_text_response_match("CLOCK");
	if (got_text_match() > 0)
		i = 1;
	if (i > 0) {
		set_state_continue(15);
	}
	else {
		set_state_continue(16);
	}
break;

beginstate 22;
	reset_dialog();
	add_dialog_str(0, "'All but one of the following words require clarification as to their exact meaning and usage.'", 0);
	add_dialog_str(1, "'Listen to these words and choose the odd one of the group.'", 0);
	add_dialog_str(2, "'suspect, drive, dive, race, borrow, taste'", 0);
	add_dialog_str(3, "'Which is the odd word in the group?' asks Apollyon.", 0);
	add_dialog_choice(0, "Give answer");
	choice = run_dialog(0);
	// only can be one response, so just check for it, all others are wrong
	get_text_response("Odd word is?");
	check_text_response_match("BORROW");
	if (got_text_match() > 0) {
		set_state_continue(15);
	}
	else {
		set_state_continue(16);
	}
break;

beginstate 23;
	reset_dialog();
	add_dialog_str(0, "Apollyon hands you a piece of paper with a table on numbers on it.", 0);
	add_dialog_str(1, "The table of number looks like:", 0);
	add_dialog_str(2, " 4   5   6   7   8   9", 20);
	add_dialog_str(3, "61 52 63 94 46 ??", 20);
	add_dialog_str(4, "'What is the missing number in the table?' Apollyon asks.", 0);
	add_dialog_choice(0, "23");
	add_dialog_choice(1, "12");
	add_dialog_choice(2, "18");
	choice = run_dialog(0);
	if (choice == 3) {
		set_state_continue(15);
	}
	else {
		set_state_continue(16);
	}
break;
