//gate.txt - A gate. This is the default script type for gate objects (types 126,127).
// Default is for gate to be closed. If you want an open gate that can be closed and opened by a
// wheel, place object 126 or 127 and then set the proper memory cell below.

//Cell 0 -  If 1, start gate as open

// message 105 toggles it from open to closed
// message 106 just oepns it

beginobjectscript; // door lever

variables;
	short gate_is_open = 0;

body;

beginstate INIT_STATE;
//print_str("loading gate");
//print_num(object_type(ME));
	if ((object_type(ME) == 128) || (object_type(ME) == 129)) {
		gate_is_open = 1;
		}	
	if (get_memory_cell(0) > 0) {
		gate_is_open = 1;
		set_object_blockage(ME,0);
		if (object_type(ME) == 126)
			set_object_icon(ME,2);
		if (object_type(ME) == 127)
			set_object_icon(ME,3);
		}
	break;

beginstate DEAD_STATE;

break;

beginstate START_STATE; // door closed, waiting
//if (my_id() == 480) {
//print_nums(gate_is_open,my_current_message(),0);
//}
	if ((gate_is_open == 0) && ((my_current_message() == 105) || (my_current_message() == 106)) ) {
		if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
			set_flag(get_memory_cell(2),get_memory_cell(3),1);
		
		gate_is_open = 1;
		set_object_blockage(ME,0);
		if ((object_type(ME) == 126) || (object_type(ME) == 128))
			set_object_icon(ME,2);
		if ((object_type(ME) == 127) || (object_type(ME) == 129))
			set_object_icon(ME,3);
		}
		else if ((gate_is_open == 1) && (my_current_message() == 105)) { // close door mess
			if (creature_on_obj(ME) >= 0) {
				print_str("Something is keeping the gate from closing.");
				end();
				}
			gate_is_open = 0;
			set_object_blockage(ME,3);
			if ((object_type(ME) == 126) || (object_type(ME) == 128))
				set_object_icon(ME,0);
			if ((object_type(ME) == 127) || (object_type(ME) == 129))
				set_object_icon(ME,1);
			}
break;

beginstate USE_STATE;
break;

beginstate UNLOCK_STATE;
	print_str_color("Unlock Spell: Doesn't affect gates.",2);
break;
