-- LUA Script - precede every function and global member with lowercase name of script + '_main' local U = require "scriptbank\\utillib" local rad = math.rad local deg = math.deg local rnd = math.random math.randomseed(os.time()) rnd(); rnd(); rnd() local dice_list = {} local controlDice = nil local EkeyPressed = false function sldice_init( e ) Include( "utillib.lua" ) end local thrower = nil function SLThrowDice( e ) for k, _ in pairs(dice_list) do CollisionOff(k) local Ent = g_Entity[k] local yo = rnd(100) SetPosition(k, Ent.x, Ent.y + 100 + yo, Ent.z) nxa, nya, nza = rnd(-179, 179), rnd(-89, 89), rnd(-179,179) SetRotation(k, nxa, nya, nza) ResetPosition(k, Ent.x, Ent.y + 100 + yo, Ent.z) ResetRotation(k, nxa, nya, nza) CollisionOn(k) dice_list[k].state = 'thrown' dice_list[k].oldAngs = nil dice_list[k].timer = g_Time + 200 thrower = e end end function sldice_main( e ) local dice = dice_list[e] local Ent = g_Entity[e] if dice == nil then dice_list[e] = { state = 'idle', timer = math.huge } if controlDice == nil then controlDice = e end return end if dice.state == 'thrown' and g_Time > dice.timer then -- get new Euler angles local xa, ya, za = rad( GetEntityAngleX( e ) ), rad( GetEntityAngleY( e ) ), rad( GetEntityAngleZ( e ) ) local x, y, z = Ent.x, Ent.y, Ent.z if dice.oldPos == nil then dice.oldPos = { x = 0, y = 0, z = 0, xa = 0, ya = 0, za = 0 } dice_list[e].timer = g_Time + 500 return elseif math.abs( ( x - dice.oldPos.x ) + ( y - dice.oldPos.y ) + ( z - dice.oldPos.z ) ) < 0.001 and math.abs( ( xa - dice.oldPos.xa ) + ( ya - dice.oldPos.ya ) + ( za - dice.oldPos.za ) ) < 0.001 then local vx,vy,vz = U.Rotate3D(0,0,1, xa, ya, za) local throw = 0 if vy > 0.5 then throw = 4 elseif vy < -0.5 then throw = 3 end if throw == 0 then vx,vy,vz = U.Rotate3D(0,1,0, xa, ya, za) if vy > 0.5 then throw = 6 elseif vy < -0.5 then throw = 1 end end if throw == 0 then vx,vy,vz = U.Rotate3D(1,0,0, xa, ya, za) if vy > 0.5 then throw = 2 elseif vy < -0.5 then throw = 5 end end SLDiceThrown( thrower, throw ) dice.state = 'idle' else dice.oldPos = { x = x, y = y, z = z, xa = xa, ya = ya, za = za } dice_list[e].timer = g_Time + 200 end end end