// Script for an alarm in bandit lair

// Memory cells:
// 0 and 1 = SDF for this alarm.  0 = not set off. 1 = destroyed.  2 = activated.
// 2 - hidden group to summon when detonated.
beginterrainscript; 

variables;
	short my_state;
	short choice;
	short msg;
body;

beginstate INIT_STATE;
	set_script_mode(2); // Per turn, only when near
	my_state = get_flag(get_memory_cell(0), get_memory_cell(1));
	if (my_state == 1) {
		set_terrain(my_loc_x(), my_loc_y(), 423);
	}
	
break;

beginstate START_STATE;
	msg = my_current_message();
	if ((msg == 1) && (my_state == 0)) {
		// Set off alarm
		message_dialog("An alarm goes off, and a group of bandits come running.","");
		activate_hidden_group(get_memory_cell(2));
		set_flag(get_memory_cell(0), get_memory_cell(1), 2);
		my_state = 2;
		play_sound(25);
		set_terrain(my_loc_x(), my_loc_y(), 421);
	}
break;

beginstate SEARCH_STATE;
	my_state = get_flag(get_memory_cell(0), get_memory_cell(1));
	choice = -1;
	if (my_state == 0) {
		reset_dialog();
		add_dialog_str(0, "This strange devive looks like an alarm.  The glowing ball can be pressed down, presumably to set it off." ,0);
		add_dialog_str(1, "Alternatively, you could smash the ball and hopefully make the alarm unusable.", 0);
		add_dialog_choice(1, "Set it off");
		add_dialog_choice(0, "Smash it");
		add_dialog_choice(2, "Leave it alone");
		choice = run_dialog(0);
	}
	if (my_state == 2) {
		reset_dialog();
		add_dialog_str(0, "This strange device is clearly an alarm.  It is pulsating and making a horrible sound.",0);
		add_dialog_str(1, "You could smash it, and hope it stops.",0);
		add_dialog_choice(0, "Smash it");
		add_dialog_choice(1, "Leave it alone");
		choice = run_dialog(0);
		if (choice == 2) {
			choice = 3;
		}
	}
	if (choice == 1) { // Destroy alarm
		set_flag(get_memory_cell(0), get_memory_cell(1), 1);
		my_state = 1;
		set_terrain(my_loc_x(), my_loc_y(), 423);
		play_sound(-108);
	}
	if (choice == 2) {  //Set alarm off for some weird reason

		message_dialog("An alarm goes off, and a group of bandits come running.","");
		activate_hidden_group(get_memory_cell(2));
		set_flag(get_memory_cell(0), get_memory_cell(1), 2);
		my_state = 2;
		play_sound(25);
		set_terrain(my_loc_x(), my_loc_y(), 421);
	}
		
break;