-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroundAI_events.lua
More file actions
111 lines (104 loc) · 4.21 KB
/
groundAI_events.lua
File metadata and controls
111 lines (104 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
-- groundAI_events.lua - Event tracking for GroundAI
GroundAI_Events = {}
-- Rolling logs for shots and impacts (30s window)
GroundAI_Events.shotLog = {}
GroundAI_Events.impactLog = {}
GroundAI_Events.window = 30 -- seconds
-- Helper: Prune old events from a log
local function pruneLog(log, now, window)
local i = 1
while i <= #log do
if now - log[i].timestamp > window then
table.remove(log, i)
else
i = i + 1
end
end
end
-- Track projectile until it no longer exists, then log impact
function GroundAI_Events.trackProjectile(weapon, initiatorName, weaponType)
local lastPos = nil
local function poll()
if weapon and weapon:isExist() then
lastPos = weapon:getPosition()
return poll, timer.getTime() + 0.05 -- poll every 0.05s
else
-- Weapon no longer exists, log impact
table.insert(GroundAI_Events.impactLog, {
unitName = initiatorName or "unknown",
weaponType = weaponType or "unknown",
timestamp = timer.getTime(),
impactPos = lastPos
})
if GroundAI and GroundAI.log then
GroundAI.log("[GroundAI_Events] Impact logged for " .. (initiatorName or "unknown"), 4)
end
return nil
end
end
poll()
end
-- DCS event handler
function GroundAI_Events:onEvent(event)
local now = timer.getTime()
if event and event.id == world.event.S_EVENT_SHOT then
if GroundAI and GroundAI.log then
GroundAI.log("[GroundAI_Events] SHOT by " .. (event.initiator and event.initiator.getName and event.initiator:getName() or "nil"), 4)
end
table.insert(GroundAI_Events.shotLog, {
unitName = event.initiator and event.initiator.getName and event.initiator:getName() or "unknown",
weaponType = event.weapon and event.weapon.getTypeName and event.weapon:getTypeName() or "unknown",
timestamp = now,
weapon = event.weapon
})
pruneLog(GroundAI_Events.shotLog, now, GroundAI_Events.window)
if event.weapon and event.weapon.isExist then
GroundAI_Events.trackProjectile(event.weapon, event.initiator and event.initiator.getName and event.initiator:getName() or "unknown", event.weapon.getTypeName and event.weapon:getTypeName() or "unknown")
end
end
end
-- Initialization: register event handler
function GroundAI_Events.init(main)
if GroundAI_Events._initialized then return end
GroundAI_Events._initialized = true
if GroundAI and GroundAI.log then
GroundAI.log("[GroundAI_Events] Initializing event handler", 4)
end
world.addEventHandler(GroundAI_Events)
end
-- Query: has unit fired in last N seconds?
function GroundAI_Events.hasFiredRecently(unitName, seconds)
local now = timer.getTime()
if GroundAI and GroundAI.log then
GroundAI.log("[GroundAI_Events] Query hasFiredRecently for " .. tostring(unitName), 4)
end
for _, shot in ipairs(GroundAI_Events.shotLog) do
if shot.unitName == unitName and now - shot.timestamp <= (seconds or GroundAI_Events.window) then
if GroundAI and GroundAI.log then
GroundAI.log("[GroundAI_Events] hasFiredRecently TRUE for " .. tostring(unitName), 4)
end
return true
end
end
if GroundAI and GroundAI.log then
GroundAI.log("[GroundAI_Events] hasFiredRecently FALSE for " .. tostring(unitName), 4)
end
return false
end
-- Query: get recent impacts for a unit
function GroundAI_Events.getRecentImpacts(unitName, seconds)
local now = timer.getTime()
if GroundAI and GroundAI.log then
GroundAI.log("[GroundAI_Events] Query getRecentImpacts for " .. tostring(unitName), 4)
end
local impacts = {}
for _, impact in ipairs(GroundAI_Events.impactLog) do
if impact.unitName == unitName and now - impact.timestamp <= (seconds or GroundAI_Events.window) then
table.insert(impacts, impact)
end
end
if GroundAI and GroundAI.log then
GroundAI.log("[GroundAI_Events] getRecentImpacts found " .. tostring(#impacts) .. " for " .. tostring(unitName), 4)
end
return impacts
end