-- DESCRIPTION: Glue this entity to another local U = require "scriptbank\\utillib" local Q = require "scriptbank\\quatlib" local deg = math.deg local rad = math.rad local glueEnts = {} function glue_entity_init_name( e, name ) glueEnts[ e ] = { state = 'init', obj = g_Entity[ e ].obj } end -- creates list entry by calculating offsets between entities local function getOffs( bobj, gobj ) local x1, y1, z1, ax1, ay1, az1 = GetObjectPosAng( bobj ) local x2, y2, z2, ax2, ay2, az2 = GetObjectPosAng( gobj ) local q1 = Q.FromEuler( rad( ax1 ), rad( ay1 ), rad( az1 ) ) local q2 = Q.FromEuler( rad( ax2 ), rad( ay2 ), rad( az2 ) ) local xo, yo, zo = x2 - x1, y2 - y1, z2 - z1 -- now need to rotate the offets to what they would be if the -- base object was not rotated q1 = Q.Conjugate( q1 ) xo, yo, zo = U.Rotate3D( xo, yo, zo, Q.ToEuler( q1 ) ) -- return rotated offsets and angular difference return { xo = xo, yo = yo, zo = zo, angQ = Q.Mul( q1 , q2 ) } end local function repositionGE( ge ) local x, y, z, xa, ya, za = GetObjectPosAng( ge.gluedTo ) xa, ya, za = rad( xa ), rad( ya ), rad( za ) local xo, yo, zo = U.Rotate3D( ge.offs.xo, ge.offs.yo, ge.offs.zo, xa, ya, za ) local nquat = Q.FromEuler( xa, ya, za ) xa, ya, za = Q.ToEuler( Q.Mul( nquat, ge.offs.angQ ) ) PositionObject( ge.obj, x + xo, y + yo, z + zo ) RotateObject( ge.obj, deg( xa ), deg( ya ), deg( za ) ) end local function getEntityLinks( e ) local list = {} for i = 0, 9 do local elink = GetEntityRelationshipID( e, i ) if elink > 0 then list[ #list + 1 ] = elink end end return list end function glue_entity_main( e ) local ge = glueEnts[ e ] if ge == nil then return end if ge.state == 'init' then local links = getEntityLinks( e ) if #links > 1 then ge.state = 'too many' elseif #links == 0 then ge.state = 'unlinked' else local obj = g_Entity[ links[ 1 ] ].obj ge.gluedTo = obj ge.offs = getOffs( obj, ge.obj ) ge.state = 'ready' end elseif ge.state == 'too many' then PromptLocal( e, 'Can only be glued to one entity' ) elseif ge.state == 'unlinked' then PromptLocal( e, 'Not linked to anything' ) elseif ge.state == 'ready' then repositionGE( ge ) end end