// barrier.txt - A magic barrier, dispelled by the Dispel Barrier spell
// Cell 0 - The strength of the spell needed to dispel it.
//  0 - level 1, 1 - level 2, 2 - level 3
// Call 1,2 - The stuff done flag to set to 1 when the barrier gone

beginobjectscript;

variables;
short last_abil;
short damage_count = 0;

body;

beginstate INIT_STATE;
	last_abil = get_current_tick();

	break;

beginstate DEAD_STATE;

break;

beginstate START_STATE; 
	if (gf(59,21) != 1) // event not going
		last_abil = get_current_tick();
		else if (damage_count > 0) { // event going crystal broke
			if (tick_difference(last_abil,get_current_tick()) > 0) {
				damage_count = damage_count - 1;
				last_abil = get_current_tick();
				
				if (damage_count == 0) {
					sf(59,get_memory_cell(0),0);
					print_str_color("One of the crystals has repaired itself!",2);
					if (gf(59,28) == 0) {
						sf(59,28,1);
						ok_dlog(86,1);
						}
					}
				end();
				
				}
			}
		else if (gf(59,get_memory_cell(0)) == 0) { // event going crystal working
			if ((tick_difference(last_abil,get_current_tick()) > 0) && (get_ran(1,0,100) < 20 + (difficulty() * 10))) {
				last_abil = get_current_tick();
		
				if (char_ok(get_memory_cell(1)) == FALSE) {
					spawn_creature(get_memory_cell(1));
					place_particle_num(get_memory_cell(1),1,1,8);
					run_text_on_char(get_memory_cell(1),"Poof!",0);
					}
					else if (char_ok(get_memory_cell(2)) == FALSE) {
						spawn_creature(get_memory_cell(2));
						place_particle_num(get_memory_cell(2),1,1,8);
						run_text_on_char(get_memory_cell(2),"Poof!",0);
						}
		
				}
			}

break;

beginstate RUN_ANIM_STATE; 
	if ((gf(59,21) == 1) && (get_ran(1,0,2) == 1)) {
		if (gf(59,get_memory_cell(0)) == 0)
			run_sparkles_on_object(ME,17,6,0);
			else run_sparkles_on_object(ME,7,6,0);
			
		}

	break;
	
beginstate USE_STATE;
	if (gf(59,21) != 1) // not going
		ok_dlog(82,1);
		else if (gf(59,get_memory_cell(0)) > 0) { // cryst damaged
				play_sound(89);
			damage_count = 14 - difficulty();
			ok_dlog(84,1);
			}
			else { // break cryst
				damage_count = 14 - difficulty();
				sf(59,get_memory_cell(0),1);
				play_sound(89);
					
				print_str_color("You break one of the crystals! It starts to slowly reform.",2);
				if (gf(59,29) == 0) {
					sf(59,29,1);
					ok_dlog(77,2);
					}
				end();
				}
break;