File: proximity.lua

package info (click to toggle)
naev 0.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 386,084 kB
  • sloc: ansic: 93,149; xml: 87,292; python: 2,347; sh: 904; makefile: 654; lisp: 162; awk: 4
file content (122 lines) | stat: -rw-r--r-- 4,314 bytes parent folder | download | duplicates (3)
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
112
113
114
115
116
117
118
119
120
121
122
-- Poll for player proximity to a point in space.
-- argument trigger: a table containing:
-- location: The location, OR
-- anchor: the pilot to use as the anchor for the trigger area
-- radius: The radius around the location or anchor
-- focus: The pilot that's polled for. If omitted, defaults to the player.
-- funcname: The name of the function to be called when the player is in proximity.
-- 
-- Example usage: hook.timer(500, "proximity", {location = vec2.new(0, 0), radius = 500, funcname = "function"})
-- Example usage: hook.timer(500, "proximity", {anchor = mypilot, radius = 500, funcname = "function"})
-- Example usage: hook.timer(500, "proximity", {anchor = mypilot, radius = 500, funcname = "function", focus = theirpilot})
function proximity( trigger )
   if trigger.focus == nil then
      trigger.focus = player.pilot()
   end
   _proximity(trigger)
end

-- This variant assumes a proximity hook between two ships, and trigger when the anchor ship scans the focus ship (fuzzy detection doesn't trigger).
-- argument trigger: a table containing:
-- anchor: The pilot to use as the anchor for the trigger area.  If omitted, defaults to the player.
-- focus: The pilot that's polled for.
-- funcname: The name of the function to be called when the player is in proximity.
--
-- Example usage: hook.timer(500, "proximityScan", {focus = theirpilot, funcname = "function"}) -- Triggers when theirpilot is detected by the player
-- Example usage: hook.timer(500, "proximityScan", {anchor = theirpilot, focus = mypilot, funcname = "function"}) -- Triggers when mypilot is detected by theirpilot
function proximityScan( trigger )
   if trigger.anchor == nil then
      trigger.anchor = player.pilot()
   end
   _proximityScan(trigger)
end

function _proximity( trigger )
    if trigger.location ~= nil then
        if vec2.dist(trigger.focus:pos(), trigger.location) <= trigger.radius then
            _G[trigger.funcname]()
            return
        end
    elseif trigger.anchor ~= nil then
        if trigger.anchor:exists() and trigger.focus:exists() and vec2.dist(trigger.focus:pos(), trigger.anchor:pos()) <= trigger.radius then
            _G[trigger.funcname]()
            return
        end
    end

    -- Check global proxmitiy table
    if __proximity_tbl == nil then
       __proximity_tbl = {}
       __proximity_tbl["__save"] = true
       __proximity_tbl["id"] = 0
    end

    -- Assign ID if necessary
    if trigger.__id == nil then
       __proximity_tbl["id"] = __proximity_tbl["id"]+1
       trigger.__id = __proximity_tbl["id"]
       __proximity_tbl[ trigger.__id ] = trigger
    end

    -- First time hook is set
    if trigger.hook_tbl == nil then
       trigger.hook_tbl = {}
       hook.enter("proximityCancel", trigger)
    end

    -- Set new timer hook
    local hook_id = hook.timer(500, "_proximity", trigger)
    trigger.hook_tbl[1] = hook_id
end

function _proximityScan( trigger )
    if trigger.focus ~= nil and trigger.anchor:exists() and trigger.focus:exists() then
        seen, scanned = trigger.anchor:inrange(trigger.focus)
        if scanned then
            _G[trigger.funcname]()
            return
        end
    end

    -- Check global proxmitiy table
    if __proximity_tbl == nil then
       __proximity_tbl = {}
       __proximity_tbl["__save"] = true
       __proximity_tbl["id"] = 0
    end

    -- Assign ID if necessary
    if trigger.__id == nil then
       __proximity_tbl["id"] = __proximity_tbl["id"]+1
       trigger.__id = __proximity_tbl["id"]
       __proximity_tbl[ trigger.__id ] = trigger
    end

    -- First time hook is set
    if trigger.hook_tbl == nil then
       trigger.hook_tbl = {}
       hook.enter("proximityCancel", trigger)
    end

    -- Set new timer hook
    local hook_id = hook.timer(500, "_proximityScan", trigger)
    trigger.hook_tbl[1] = hook_id
end

-- Make sure the proximity timer shuts itself off on land or jumpout.
function proximityCancel( trigger )
   if trigger ~= nil then
       hook.rm( trigger.hook_tbl[1] )
       __proximity_tbl[ trigger.__id ] = nil
    end
end

-- Make sure the proximity timer shuts itself off on land or jumpout.
function proximityCancel( trigger )
   if trigger ~= nil then
       hook.rm( trigger.hook_tbl[1] )
       __proximity_tbl[ trigger.__id ] = nil
    end
end