Jump to content

Recommended Posts

Posted

Okay I need some help. I have very very very limited knowledge on LUA. I'm trying to score bombs dropped on a specific target but I cannot get a return on this script. Where did I go wrong or do I just need to scrap it.

-- Define target point
local targetPoint = {
    x = 451519,
    y = 623,
    z = 897725
}

-- Bounding box size in meters
local delta = 30 -- +/- 30 meters

local targetZone = {
    x_min = targetPoint.x - delta,
    x_max = targetPoint.x + delta,
    y_min = targetPoint.y - delta,
    y_max = targetPoint.y + delta,
    z_min = targetPoint.z - delta,
    z_max = targetPoint.z + delta
}

local score = 0
local bombsTracked = {}

-- Function to check if position is within target zone
local function isWithinTargetZone(pos)
    return pos.x >= targetZone.x_min and pos.x <= targetZone.x_max and
           pos.y >= targetZone.y_min and pos.y <= targetZone.y_max and
           pos.z >= targetZone.z_min and pos.z <= targetZone.z_max
end

-- Event handler for bomb impact
local function onEvent(event)
    if event.id == world.event.S_EVENT_SHOT then
        local initiator = event.initiator
        if initiator and initiator:getTypeName() == "MK-82" then
            local pos = initiator:getPosition().p
            local bombID = initiator:getID()

            -- Avoid scoring multiple times for the same bomb
            if not bombsTracked[bombID] then
                bombsTracked[bombID] = true

                if isWithinTargetZone(pos) then
                    score = score + 100
                    trigger.action.outText(string.format("MK-82 #%d hit the target! Score: %d", bombID, score), 10)
                else
                    trigger.action.outText(string.format("MK-82 #%d missed.", bombID), 10)
                end
            end
        end
    end
end

-- Register event handler
world.addEventHandler({onEvent = onEvent})

Posted

S_EVENT_SHOT triggers when firing a weapon, not when it impacts. Dunno whether S_EVENT_HIT will trigger when a bomb hits the ground without damaging any units. Also unsure whether your use of addEventHandler() is correct. See here for an exemplary use of S_EVENT_HIT to detect (H)ARMs that hit a ship: 

 

Posted (edited)

S_EVENT_HIT will tell you nothing about where the bomb hit unfortunately. Worse, by the time it triggers, the weapon object will not longer exists and you will be able to get any info about it.

The strategy is to detect a drop with S_EVENT_SHOT and track its position with a scheduled function every 0.001 seconds , stop when the bomb is not longer alive and return the position... I don't have an example without using MIST or MOOSE but attached is one of my scripts with MOOSE . If you look under my user files, you will find some of my range scripts using MIST (they are a bit more complicated to setup_

 

sbsa.lua

EDIT to add: This script depends on another (for TTS comms with the player). To use you will have to make the highlighted changes:

image.png

image.png

I'll post an example mission to the user files and when it is approved, I'll link it in this thread

Edited by Draken35
Posted

And to add to the last, checking for a bomb in a small zone is tricky/inconsistent if you don't do something like the last post suggests because the standard triggers are only checking about once per second.  So a bomb falling can pass into and out or into and blow up and therefore no longer exist between the 1 second intervals.

×
×
  • Create New...