utilities = {} --Object Utilities function spawnPlacedLoot( lootType, lootPos, value ) print("spawnPlacedLoot",lootType,lootPos) if not lootType or not object(lootPos) then print("spawnPlacedLoot called with invalid paramters:",lootType,lootPos,object(lootPos)) end if not value then value = 1 end if type(lootPos) == "string" then --loot.SpawnAndLaunchLoot( lootType , {object(lootPos).position()} , 0.0 , 0.0 , -90.0 , 0 , 20 , 0 , -29.8 , 0.98 , 0, value ) return loot.SpawnAndLaunchLoot( lootType , {object(lootPos).position()} , 0.0 , 0.0 , -90.0 , 0 , 20 , 0 , -29.8 , 0 , 0, value ) elseif type(lootPos) == "table" then --loot.SpawnAndLaunchLoot( lootType , lootPos , 0.0 , 0.0 , -90.0 , 0 , 20 , 0 , -29.8 , 0.98 , 0, value ) return loot.SpawnAndLaunchLoot( lootType , lootPos , 0.0 , 0.0 , -90.0 , 0 , 20 , 0 , -29.8 , 0 , 0, value ) end end function spawnThrownLoot( lootType, lootPos , value , throwType , count ) print("spawnThrownLoot",lootType, lootPos , value , throwType , count , type(lootPos) ) if not lootType or not lootPos or not type(lootPos) == "string" or not type(lookPos) == "table" then print("spawnThrownLoot called with invalid parameters:",lootType,lootPos,object(lootPos)) return nil else if not count then count = 1 end if not value then value = 1 end if throwType and throwType == 1 then --enemy throw settings fFacingRandFrom = 0.0 fFacingRandRange = 360.0 fPitchRandFrom = 80.0 fPitchRandRange = 10.0 fSpeedRandFrom = 20.0 fSpeedRandRange = 5.0 fZGravity = -29.8 fDamping = 9.98 fBounciness = 0.5 else --prop settings fFacingRandFrom = 0.0 fFacingRandRange = 360.0 fPitchRandFrom = 65.0 fPitchRandRange = 25.0 fSpeedRandFrom = 20.0 fSpeedRandRange = 5.0 fZGravity = -29.8 fDamping = 9.98 fBounciness = 1.0 end for i=1,count do --print("throw",i,count) if type(lootPos) == "string" then loot.SpawnAndLaunchLoot( lootType , {object(lootPos).position()} , fFacingRandFrom , fFacingRandRange , fPitchRandFrom , fPitchRandRange , fSpeedRandFrom , fSpeedRandRange , fZGravity , fDamping , fBounciness, value ) elseif type(lootPos) == "table" then loot.SpawnAndLaunchLoot( lootType , lootPos , fFacingRandFrom , fFacingRandRange , fPitchRandFrom , fPitchRandRange , fSpeedRandFrom , fSpeedRandRange , fZGravity , fDamping , fBounciness, value ) end end end end -- Table Utilities function tableContains(table, element) for _, value in pairs(table) do if value == element then return true end end return false end -- Vector Utilities function getCross(vA,vB) x = (vA[2]*vB[3]) - (vA[3]*vB[2]) y = (vA[3]*vB[1]) - (vA[1]*vB[3]) z = (vA[1]*vB[2]) - (vA[2]*vB[1]) --print(x,y,z) return x,y,z end -- Position Utilities function getDistance(posA,posB) return math.sqrt( math.pow(posA[1] - posB[1],2) + math.pow(posA[2] - posB[2],2) + math.pow(posA[3] - posB[3],2) ) end function getDistance2D(posA,posB) return math.sqrt( math.pow(posA[1] - posB[1],2) + math.pow(posA[2] - posB[2],2) + 0 ) end utilities.player1 = nil utilities.player2 = nil function getNearestPlayer(pos) -- input: pos: 3 index table; X,Y,Z position -- output: if valid player is available return player_obj, player_obj's distance from pos (3D), player_obj's distance from pos (2D) ; -- otherwise return nil , math.huge , math.huge -- (returning math.huge is a bit of a cheat but it allow for pure-number functions -- that only care about distance value, if you are really worried about if the player -- actually exists or not you can check the first return value) if not pos or not (type(pos) == "table") then --wasn't provided a valid position, bail print("called getNearestPlayer without a valid position") return nil end if not utilities.player1 or not utilities.player1.alive or not utilities.player1.alive() then --print("updating player1") utilities.player1 = player_obj(0) end if not utilities.player2 or not utilities.player2.alive or not utilities.player2.alive() then --print("updating player2") utilities.player2 = player_obj(1) end player1 = utilities.player1 player2 = utilities.player2 if (player1 and player1.alive and player1.alive()) and (not player2 or not player2.alive or not player2.alive()) then --only Player 1, return Player 1 --print("only Player 1, return Player 1") player1pos = {player1.position()} player1d = getDistance( player1pos , pos ) player1d2d = getDistance2D( player1pos , pos ) return player1,player1d,player1d2d elseif (player2 and player2.alive and player2.alive()) and (not player1 or not player1.alive or not player1.alive()) then --only Player 2, return Player 2 --print("only Player 2, return Player 2") player2pos = {player2.position()} player2d = getDistance( player2pos , pos ) player2d2d = getDistance2D( player2pos , pos ) return player2,player2d,player2d2d elseif (player1 and player1.alive and player1.alive()) and (player2 and player2.alive and player2.alive()) then --both Players, find nearest and return it --print("both Players, find nearest and return it") player1pos = {player1.position()} player2pos = {player2.position()} player1d = getDistance( player1pos , pos ) player2d = getDistance( player2pos , pos ) player1d2d = getDistance2D( player1pos , pos ) player2d2d = getDistance2D( player2pos , pos ) --print(player1pos) --print(player2pos) --print(player1d,player2d) --print(player1d2d,player2d2d) if player1d < player2d then --print("player 1 closer") return player1,player1d,player1d2d else --print("player 2 closer") return player2,player2d,player2d2d end else --no valid players --print("no valid players") return nil , math.huge , math.huge end end function moveTo(origin, destination) if not origin or not destination then print("moveTo called with invalid parameters",origin,destination) else print("moveTo") origin.position( destination.position() ) origin.rotation( destination.rotation() ) origin.trigger.send(1,"updatePosition") end end function moveToFlip(origin, destination) if not origin or not destination then print("moveToFlip called with invalid parameters",origin,destination) elseif not origin.position or not destination.position then print("moveToFlip called with invalid parameters - no position",origin,destination) else print("moveToFlip",origin,destination) --[[ print("Pos:",origin.position()) print("Pos:",destination.position()) print("Rot:",origin.rotation(),destination.rotation()) ]] origin.position( destination.position() ) rx, ry , rz = origin.rotation( destination.rotation() ) rz = (rz+180) origin.rotation(rx,ry,rz) origin.trigger.send(1,"updatePosition") end end function isThereAPlayer() if player_obj(0) or player_obj(1) then return true , player_obj(0) or player_obj(1) else return false , nil end end --Math Utilities --Convert feet (TFB unit) into meter (XPEC unit) function ft2m(i) return (i * 0.3048) end --Convert meter (XPEC unit) into feet (TFB unit) function m2ft(i) return (i * (1200/3937) ) end --Convert 0-255 value (TFB unit) into 0-1 value (XPEC unit) function convert255to1(i) return (i/255) end