script_name('Display Vehicle Components Example')
local mad = require 'MoonAdditions'
local ffi = require 'ffi'
require "socket"


local surface_props_offset = 12
local lastTime = socket.gettime()
local currTime = socket.gettime()

-- Constants
local POINTER_SIZE = ffi.sizeof("void*")
local RW_SURFACE_PROPERTIES_SIZE = ffi.sizeof("struct { float ambient; float specular; float diffuse; }")

			ffi.cdef[[
				typedef struct {
					float ambient;
					float specular;
					float diffuse;
				} RwSurfaceProperties;
			]]

function main()
	while true do
		wait(0)
		if isPlayerPlaying(PLAYER_HANDLE) then
			if isCharInAnyCar(PLAYER_PED) then
				local car = storeCarCharIsInNoSave(PLAYER_PED)
				local components = mad.get_all_vehicle_components(car)
				for i = 2, 2 do -- in ipairs(components) do
					draw_component(components[i], i)
				end
				currTime = socket.gettime()
				if currTime - lastTime > 2^12 then surface_props_offset = surface_props_offset + 4 lastTime = socket.gettime() end
				
			end
		end
	end
end

function draw_component(component, id)
	local x, y, z = component.matrix.pos:get()
	local sx, sy = convert3DCoordsToScreen(x, y, z)
	draw_text(string.format('Component #%d: ~y~%s', id, component.name), sx, sy, 255, 255, 255)
	sx = sx + 4
	-- draw objects
	local objs = component:get_objects()
	for i, obj in ipairs(objs) do
		local mats = obj:get_materials()
		sy = sy + 12
		draw_text('Object #' .. i .. ' total materials: ~y~' .. #mats, sx, sy, 255, 255, 255)
		-- draw materials
		sx = sx + 4
		for i, mat in ipairs(mats) do
			local tex = mat:get_texture()
		
			local raw_pointer = mat:get_pointer()
			local material_ptr = ffi.cast("uintptr_t", raw_pointer)

			local texture_offset = 0

			print(surface_props_offset)

			local texture_ptr = ffi.cast("void*", material_ptr + texture_offset)
			print("Texture pointer:", tostring(texture_ptr))

			local surface_props_ptr = ffi.cast("RwSurfaceProperties*", material_ptr + surface_props_offset)

			print("Surface Properties - Ambient: ", surface_props_ptr.ambient)
			print("Surface Properties - Specular: ", surface_props_ptr.specular)
			print("Surface Properties - Diffuse: ", surface_props_ptr.diffuse)
			
			local aaa = tostring(surface_props_ptr.ambient)
			local sss = tostring(surface_props_ptr.specular)
			local ddd = tostring(surface_props_ptr.diffuse)
			
			local tex_name = tex and tex.name
			
			if surface_props_ptr.ambient	and tex_name == 'vehiclelights128'	and i == 7	then surface_props_ptr.ambient 	= 8 end 
			if surface_props_ptr.specular	and tex_name == 'vehiclelights128'	and i == 7	then surface_props_ptr.specular	= 4.0 end 
			if surface_props_ptr.diffuse	and tex_name == 'vehiclelights128'	and i == 7	then surface_props_ptr.diffuse 	= 0.0 end 
			print(tex_name)
			
			--if surface_props_ptr.ambient	then surface_props_ptr.ambient 	= 1 end 
			--if surface_props_ptr.specular	then surface_props_ptr.specular	= 1.0 end 
			--if surface_props_ptr.diffuse	then surface_props_ptr.diffuse 	= 0.5 end 
		
		
			--local tex = mad.texture.from_memory(raw_pointer)

			print(tex)
			
			local tex_name = tex and ('~b~~h~~h~' .. tostring(tex.name)) or '~r~no texture'
			sy = sy + 12
			draw_text('Material #' .. i .. ' texture: ' .. tex_name .. "~w~ A: " .. aaa .. " S: " .. sss .. " D: " .. ddd , sx, sy, 255, 255, 255)
		end
					sy = sy + 12
			draw_text(tostring(surface_props_offset), sx, sy, 255, 255, 255)
		sx = sx - 4
	end
end

function draw_text(str, x, y, r, g, b)
	--mad.draw_text(str, x, y, mad.font_style.SUBTITLES, 0.3, 0.6, mad.font_align.LEFT, 2000, true, false, r, g, b, 255, 1, 0, 30, 30, 30, 120)
end

отрефактори, убери незадействованные блоки кода, наведи порядок в названиях переменных, комментариях, общей структуре