// dealer.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 will start a simple game of 21.
// 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, target, choice, crd, dlr, plyr;
string dline;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_mobility(ME, 0);
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;
	set_state_continue(10);
break;

beginstate 10;
	if (get_flag(100, 0) == 1) {
		message_dialog("The dealer looks you up and down.", "'You need to put on clothes to play here.' he says finally.");
		set_state(START_STATE);
	}
	else {
		set_state_continue(11);
	}
break;

beginstate 11;
	reset_dialog();
	add_dialog_str(0, "The man shuffling cards at the table stops and says 'Do you wish to play 21?'", 0);
	add_dialog_str(1, "'If so, have a seat and let me know your pleasure.'", 0);
	add_dialog_str(2, "The card dealer points to the empty seat front of him.", 0);
	add_dialog_choice(0, "Ok");
	choice = run_dialog(0);
	set_state_continue(12);
break;

beginstate 12;
	reset_dialog();
	add_dialog_str(0, "'Bets are 5 coins a game.' the dealer says.", 0);
	add_dialog_str(1, "'I draw on anything under 16.' the dealer adds.", 0);
	add_dialog_str(2, "Do you:", 0);
	add_dialog_choice(0, "Play");
	add_dialog_choice(1, "Leave");
	choice = run_dialog(0);
	if (choice == 1) {
		set_state_continue(13);
	}
	else {
		message_dialog("'OK, come back again when you wish to play.' the dealer says.", "He then goes back to idly shuffling the card decks in front of him.");
		set_state(START_STATE);
	}
break;

beginstate 13;
	if (coins_amount() >= 5) {
		dlr = 0;
		plyr = 0;
		set_state_continue(14);
	}
	else {
		message_dialog("The dealer sneers at you 'You don't have enough money.'", "'Get otta here before I beat you to a pulp.' he says as he starts shuffling cards again.");
		set_state(START_STATE);
	}
break;

beginstate 14;
	// initial deal
	message_dialog("The dealer gives two cards to himself and two cards to you.", "");
	dlr = get_ran(1, 1, 13);
	if (dlr > 10) { dlr = 10; }
	if (dlr == 1) { dlr = 11; }
	crd = get_ran(1, 1, 13);
	if (crd > 10) { crd = 10; }
	if ((crd == 1) && (dlr < 11)) { crd = 11; }
	dlr = dlr + crd;
	plyr = get_ran(1, 1, 13);
	if (plyr > 10) { plyr = 10; }
	if (plyr == 1) { plyr = 11; }
	crd = get_ran(1, 1, 13);
	if (crd > 10) { crd = 10; }
	if ((crd == 1) && (plyr < 11)) { crd = 11; }
	plyr = plyr + crd;
	set_state_continue(15);
break;

beginstate 15;
	clear_buffer();
	append_string("You have ");
	append_number(plyr);
	append_string(" currently with your cards.");
	get_buffer_text(dline);
	reset_dialog();
	add_dialog_str(0, dline, 0);
	add_dialog_str(1, "Do you:", 0);
	add_dialog_choice(0, "Take a 'hit'");
	add_dialog_choice(1, "Stay");
	choice = run_dialog(0);
	if (choice == 1) {
		set_state_continue(16);
	}
	else {
		set_state_continue(17);
	}
break;

beginstate 16;
	// player gets another card
	crd = get_ran(1, 1, 13);
	if (crd > 10) { crd = 10; }
	if ((crd == 1) && ((11 + plyr) <= 21)) { crd = 11; }
	plyr = plyr + crd;
	if (plyr <= 21) {
		set_state_continue(15);
	}
	else {
		clear_buffer();
		append_string("The card dealer chuckles 'You have ");
		append_number(plyr);
		append_string(" and are over 21, you lose.'");
		get_buffer_text(dline);
		message_dialog(dline, "He gathers the cards from the table and starts shuffling again.");
		change_coins(-5);
		set_state_continue(12);
	}
break;

beginstate 17;
	i = 0;
	while (i == 0) {
		// finish off dealer
		clear_buffer();
		if (dlr < 16) {
			crd = get_ran(1, 1, 13);
			if (crd > 10) { crd = 10; }
			if ((crd == 1) && ((11 + dlr) <= 21)) { crd = 11; }
			dlr = dlr + crd;
			append_string("The dealer takes a card.  He has ");
		}
		else {
			append_string("The dealer stays.  He has ");
		}
		append_number(dlr);
		append_string(" currently.");
		get_buffer_text(dline);
		message_dialog(dline, "");
		if (dlr > 21) {
			message_dialog("The card dealer scowls a little.  'You win.' he says as he pays off the bet.", "He collects the cards and starts shuffling again.");
			play_sound(38);
			change_coins(5);
			i = 1;
		}
		else if (dlr >= 16) {
			i = 1;
			// compare cards
			clear_buffer();
			append_string("You have ");
			append_number(plyr);
			append_string(".  The dealer has ");
			append_number(dlr);
			append_string(".");
			get_buffer_text(dline);
			if (dlr == plyr) {
				message_dialog(dline, "'It's a push, no losers, no winners.' the dealer says leaving your bet on the table.");
			}
			else if (dlr > plyr) {
				message_dialog(dline, "'I win.' the dealers says gathering your bet.");
				change_coins(-5);
			}
			else {
				message_dialog(dline, "'You win.' the dealer says paying off your bet.");
				play_sound(38);
				change_coins(5);
			}
		}
	}
	set_state_continue(12);
break;
