#include <define.h>

//#define NAME                    SCRIPT_

#include <command.h>
//#include <ModReact.h>



procedure start;
#include <push.h>
procedure description_p_proc;
procedure look_at_p_proc;
procedure spatial_p_proc;
procedure use_skill_on_p_proc;
procedure deallocate;
procedure detonate;
procedure plasma_death;

variable initialized1;

import variable removal_ptr;


procedure start
begin
	if (not(initialized1)) then begin
		initialized1 := 1;
	end
	else begin
		if (script_action == description_proc) then begin
			call description_p_proc;
		end
		else begin
			if (script_action == look_at_proc) then begin
				call look_at_p_proc;
			end
			else begin
				if (script_action == use_skill_on_proc) then begin
					call use_skill_on_p_proc;
				end
				else begin
					if (script_action == spatial_proc) then begin
						call spatial_p_proc;
					end
				end
			end
		end
	end
end

procedure description_p_proc
begin
	variable LVar0 := 0;
	script_overrides;
	LVar0 := message_str(SCRIPT_VATALERT, 100);
	if (not(local_var(0))) then begin
		if (not(is_success(do_check(dude_obj, 1, has_trait(0, dude_obj, 0))))) then begin
			LVar0 := message_str(SCRIPT_VATALERT, 101);
		end
		else begin
			set_local_var(0, 1);
		end
	end
	display_msg(LVar0);
end

procedure look_at_p_proc
begin
	if (map_var(10) == 1) then begin
		script_overrides;
		call deallocate;
	end
end

procedure spatial_p_proc
begin
	variable LVar0 := 0;
	if (map_var(10) == 1) then begin
		call deallocate;
	end
	else begin
		if (source_obj == dude_obj) then begin
			if (map_var(7) == 1) then begin
				call detonate;
			end
			else begin
				if (is_success(do_check(dude_obj, 1, has_trait(0, dude_obj, 0) - 3))) then begin
					script_overrides;
					LVar0 := message_str(SCRIPT_VBOOM, 100);
					if (is_success(roll_vs_skill(dude_obj, 11, 10))) then begin
						LVar0 := LVar0 + message_str(SCRIPT_VBOOM, 101);
					end
					set_local_var(0, 1);
					display_msg(LVar0);
				end
			end
		end
	end
end

procedure use_skill_on_p_proc
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	if (action_being_used == 11) then begin
		if (local_var(0)) then begin
			script_overrides;
			LVar1 := roll_vs_skill(dude_obj, 11, 10);
			if (is_success(LVar1)) then begin
				display_msg(message_str(SCRIPT_VBOOM, 102));
				LVar0 := create_object_sid(26, 0, 0, -1);
				add_obj_to_inven(dude_obj, LVar0);
				call deallocate;
			end
			else begin
				if (is_critical(LVar1)) then begin
					display_msg(message_str(SCRIPT_VBOOM, 103));
					call detonate;
				end
				else begin
					display_msg(message_str(SCRIPT_VBOOM, random(104, 106)));
				end
			end
		end
	end
end

procedure deallocate
begin
	move_to(self_obj, 7000, 0);
	set_map_var(10, 1);
	removal_ptr := self_obj;
end

procedure detonate
begin
	variable LVar0 := 0;
	LVar0 := random(10, 20);
	explosion(tile_num(dude_obj), elevation(dude_obj), LVar0);
	call deallocate;
end

procedure plasma_death
begin
	explosion(tile_num(dude_obj), elevation(dude_obj), 0);
	reg_anim_func(2, dude_obj);
	reg_anim_func(1, 1);
	reg_anim_animate(dude_obj, 32, -1);
	reg_anim_func(3, 0);
	game_ui_disable;
	add_timer_event(dude_obj, game_ticks(5), 8);
end