local U = require "scriptbank\\utillib" local lightNum = {} local attachTo = {} local offx,offy,offz,offax,offay,offaz = {},{},{},{},{},{} attachLightToMe = {} local _rad = math.rad function light_npc_init(e) lightNum[e] = GetEntityLightNumber(e)--change 2 to the entity number of the light --change 'nil' to the entity number of the npc --or place the light next to entity for automatic assignment --*make sure entity has 'attachLightToMe[e] = e' in the script* attachTo[e] = nil end function light_npc_main(e) if attachTo[e] == nil then local x1,y1,z1 = GetLightPosition(lightNum[e]) local dist1 = 999999 for a,_ in pairs (attachLightToMe) do local x2,y2,z2 = GetEntityPosAng(a) local dist2 = sqrd(x1,z1,x2,z2) if dist2 < dist1 then attachTo[e] = a dist1 = dist2 end end if attachTo[e] == nil then attachTo[e] = -1 return end --get the entity pos & angles values we are attaching too local x, y, z, Ax, Ay, Az = GetEntityPosAng(attachTo[e]) --get our own (light) values local lx,ly,lz = GetLightPosition(lightNum[e]) local lax,lay,laz = GetEntityAngleX(e),GetEntityAngleY(e),GetEntityAngleZ(e) --work out the offsets (done once at map start) offx[e] = lx-x offy[e] = ly-y offz[e] = lz-z --store the angle offset as a radian for vector use for later offax[e] = _rad(lax-Ax) offay[e] = _rad(lay-Ay) offaz[e] = _rad(laz-Az) else --this part is run during the update if attachTo[e] == -1 then return end --get the new values for our attached entity local x, y, z, Ax, Ay, Az = GetEntityPosAng(attachTo[e]) --place the light based on the new position + our offset SetLightPosition ( lightNum[e], x+offx[e], y+offy[e], z+offz[e] ) --work out the angle of the entity + our angle offset local xv, yv, zv = U.Rotate3D( 0, 0, 1, _rad(Ax)+offax[e], _rad(Ay)+offay[e], _rad(Az)+offaz[e] ) SetLightAngle( lightNum[e], xv, yv, zv ) --just to move the prompt SetPosition(e, x+offx[e], y+offy[e], z+offz[e]) ResetPosition(e, x+offx[e], y+offy[e], z+offz[e]) --shows the light's angle vectors PromptLocal(e,xv.." "..yv.." "..zv) end end function sqrd( x1, z1, x2, z2 ) local dx, dz = x1 - x2, z1 - z2 return dx*dx+dz*dz end