// stickman.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 craps (dice).
// 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, done, dtwo, pnt, j;
string dline, sline;

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 stickman looks you up and down.", "'Get some clothes, then come back.' he states.");
		set_state(START_STATE);
	}
	else {
		set_state_continue(11);
	}
break;

beginstate 11;
	reset_dialog();
	add_dialog_str(0, "The man holding the dice, holds them out to you and says 'Do you wish to play craps?'", 0);
	add_dialog_str(1, "'If so, have a seat and let me know your pleasure.'", 0);
	add_dialog_str(2, "The stickman 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 stickman says.", 0);
	add_dialog_str(1, "'For first roll, 7 or 11 win, 2, 3 or 12 craps out.' he adds.", 0);
	add_dialog_str(2, "Do you:", 0);
	add_dialog_choice(0, "Roll Dice");
	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 stickman says.", "He then goes back to looking bored and playing with the dice.");
		set_state(START_STATE);
	}
break;

beginstate 13;
	if (coins_amount() >= 5) {
		done = 0;
		dtwo = 0;
		pnt = 0;
		set_state_continue(14);
	}
	else {
		message_dialog("The stickman looks annoyed 'You don't have enough money.'", "'Come back when you get enough.' he says as he starts playing with the dice in front of him.");
		set_state(START_STATE);
	}
break;

beginstate 14;
	i = 0;
	// roll die, check result
	done = get_ran(1, 1, 6);
	dtwo = get_ran(1, 1, 6);
	clear_buffer();
	append_string("You rolled a ");
	append_number(done);
	append_string(" and a ");
	append_number(dtwo);
	append_string(".");
	get_buffer_text(dline);
	if (pnt == 0) {
		pnt = done + dtwo;
		if ((pnt == 7) || (pnt == 11)) {
			message_dialog(dline, "'You've won.' the stickman says paying off your bet.");
			play_sound(38);
			change_coins(5);
			i = 1;
		}
		else if ((pnt < 4) || (pnt == 12)) {
			message_dialog(dline, "'You've crapped out.' the stickman says taking your bet.");
			change_coins(-5);
			i = 1;
		}
	}
	else {
		j = done + dtwo;
		if (j == pnt) {
			message_dialog(dline, "'You've made point and won.' the stickman says paying off your bet.");
			play_sound(38);
			change_coins(5);
			i = 1;
		}
		else if (j == 7) {
			message_dialog(dline, "'You rolled a 7 craps.' the stickman says taking your bet.");
			change_coins(-5);
			i = 1;
		}
	}
	if (i == 0) {
		// continue then
		set_state_continue(15);
	}
	else {
		set_state_continue(12);
	}
break;

beginstate 15;
	clear_buffer();
	append_string("Point is ");
	append_number(pnt);
	append_string(" for win, 7 craps out.");
	get_buffer_text(sline);
	reset_dialog();
	add_dialog_str(0, dline, 0);
	add_dialog_str(1, sline, 0);
	add_dialog_str(2, "The stickman pushes you back the dice 'Roll again when ready.' he says.", 0);
	add_dialog_str(3, "He awaits your move.", 0);
	add_dialog_choice(0, "Roll Dice");
	choice = run_dialog(0);
	set_state_continue(14);
break;
