#Conditional Hooks $Application: FS2_Open $On Gameplay Start: [ WeaponHit = {} WeaponHit.hitDefs = {} function WeaponHit:resolveDef(weapon_class_name, ship_name, subsys_name) if (type(weapon_class_name) ~= "string") then ba.warning("Illegal type '" .. type(weapon_class_name) .. "'!") return nil end if (ship_name ~= nil and type(ship_name) ~= "string") then ba.warning("Illegal type '" .. type(ship_name) .. "'!") return nil end if (subsys_name ~= nil and type(subsys_name) ~= "string") then ba.warning("Illegal type '" .. type(subsys_name) .. "'!") return nil end local weapon_class = tb.WeaponClasses[weapon_class_name] if (not weapon_class:isValid()) then ba.warning("Illegal weapon class '" .. weapon_class_name .. "'!") return nil end local ship = nil if (ship_name ~= nil) then ship = mn.Ships[ship_name] if (not ship:isValid()) then return nil end end local subsys = nil if (subsys_name ~= nil) then if (ship == nil) then ba.warning("Cannot check a subsystem for an unrestricted ship!") else subsys = ship[subsys_name] if (not subsys:isValid()) then ba.warning("Illegal subsys '" .. subsys_name .. "'!") return nil end end end local def = { weapon_class = weapon_class, ship = ship, subsys_name = subsys_name, lastTime = -1, x = 0, y = 0, z = 0, } return def end function WeaponHit:processHit(weapon, ship) local colInfo = weapon:getCollisionInformation() if (colInfo ~= nil and colInfo:isValid()) then for _, v in ipairs(self.hitDefs) do if (weapon.Class == v.weapon_class and ship == v.ship) then local impactPos = colInfo:getCollisionPoint() if (v.subsys_name == nil) then v.lastTime = mn.getMissionTime() v.x = impactPos.x v.y = impactPos.y v.z = impactPos.z else local subsys = v.ship[v.subsys_name] local subsysPos = v.ship.Position + v.ship.Orientation:unrotateVector(subsys.Position) local dist = impactPos:getDistance(subsysPos) if (dist <= subsys.Radius) then v.lastTime = mn.getMissionTime() v.x = impactPos.x v.y = impactPos.y v.z = impactPos.z end end end end end end function WeaponHit:addHitDef(weapon_class_name, ship_name, subsys_name) local def = WeaponHit:resolveDef(weapon_class_name, ship_name, subsys_name) if (def ~= nil) then if (def.ship == nil) then ba.warning("Cannot add a def with an unrestricted ship! Do that in checkHit.") return end table.insert(self.hitDefs, def) end end function WeaponHit:checkHit(variables, weapon_class_name, ship_name, subsys_name) local age = nil local x = nil local y = nil local z = nil if (variables.age ~= nil) then if (type(variables.age) == "string") then if (mn.SEXPVariables[variables.age]:isValid()) then age = mn.SEXPVariables[variables.age] else ba.warning("Illegal variable '" .. variables.age .. "'!") end else ba.warning("Illegal type '" .. type(variables.age) .. "'!") end end if (variables.x ~= nil) then if (type(variables.x) == "string") then if (mn.SEXPVariables[variables.x]:isValid()) then x = mn.SEXPVariables[variables.x] else ba.warning("Illegal variable '" .. variables.x .. "'!") end else ba.warning("Illegal type '" .. type(variables.x) .. "'!") end end if (variables.y ~= nil) then if (type(variables.y) == "string") then if (mn.SEXPVariables[variables.y]:isValid()) then y = mn.SEXPVariables[variables.y] else ba.warning("Illegal variable '" .. variables.y .. "'!") end else ba.warning("Illegal type '" .. type(variables.y) .. "'!") end end if (variables.z ~= nil) then if (type(variables.z) == "string") then if (mn.SEXPVariables[variables.z]:isValid()) then z = mn.SEXPVariables[variables.z] else ba.warning("Illegal variable '" .. variables.z .. "'!") end else ba.warning("Illegal type '" .. type(variables.z) .. "'!") end end local def = WeaponHit:resolveDef(weapon_class_name, ship_name, subsys_name) if (def == nil) then return end for _, v in ipairs(self.hitDefs) do -- there is a subtle distinction here: checking with a nil ship means "any ship", whereas checking with a nil subsys means "the hull", not "any subsystem" if (def.weapon_class == v.weapon_class and (def.ship == nil or def.ship == v.ship) and def.subsys_name == v.subsys_name) then if (v.lastTime >= 0) then if (age ~= nil) then age.Value = (mn.getMissionTime() - v.lastTime) end if (x ~= nil) then x.Value = v.x end if (y ~= nil) then y.Value = v.y end if (z ~= nil) then z.Value = v.z end end end end end ] $On Weapon Collision: [ if (hv.Self:getBreedName() == "Ship") then WeaponHit:processHit(hv.Weapon, hv.Self) end ] #End