local script = globalObject;

local player = nil;

local commandMap = {};

local mgun, mgun_b, rlauncher, mines = nil, nil, nil, nil;


local function ExecuteCommands()
	while(SetNextCommand(script))
	do
		local command = GetCurrentCommand(script);
		if (commandMap[command])
		then
--			CheckString(command);
			commandMap[command]();
			
		end
	end
end

local function ShowPart(n, show)
	if (n == 1)
	then
		SetObjectVisible(mgun_b, show);
	elseif (n == 2)
	then
		SetObjectVisible(rlauncher, show);
	elseif (n == 3)
	then
		SetObjectVisible(mines, show);
	end	
end

local function ShowPart2()
	local n, show = GetCurrentParams(script);
	n = tonumber(n);
	show = tonumber(show);
	local show2 = false;
	if (show == 1)
	then
		show2 = true;
	end
	ShowPart(n, show2);
end

local function UpdateParts()
	if (IsWeaponMounted(3, 1))
	then
		ShowPart(1, true);
	end
	if (IsWeaponMounted(3, 2))
	then
		ShowPart(2, true);
	end
	if (IsWeaponMounted(3, 3))
	then
		ShowPart(3, true);
	end
end

local function AddObject()
	player = StringToPointer(GetCurrentParams(script));

--~ 	mgun = FindObject(player, "mgun");
--~ 	SetObjectVisible(mgun, false);
	mgun_b = FindObject(player, "mgun_b");
	SetObjectVisible(mgun_b, false);
	rlauncher = FindObject(player, "rlauncher");
	SetObjectVisible(rlauncher, false);
	mines = FindObject(player, "mines");
	SetObjectVisible(mines, false);
	
	UpdateParts();
end

local function DeleteObject()
	Release(player);
	Release(mgun_b);
	Release(rlauncher);
	Release(mines);
	player = nil;
	StopScript(script);
end

local function ReleaseAll()
	DeactivateInterface(player);
	player = nil;
end

local function SetActive()
	local x, y, z = GetPosition(player);
	SetDirectParameters2(x, y, z);
end

commandMap["add_object"] = AddObject;
commandMap["delete_object"] = DeleteObject;
commandMap["release_all"] = ReleaseAll;
commandMap["set_active"] = SetActive;
commandMap["update_parts"] = UpdateParts;
commandMap["show_part"] = ShowPart2;

while(true)
do
	ExecuteCommands();
	
	Update();
end
