forked from slime73/TargetTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
executable file
·354 lines (303 loc) · 9.77 KB
/
main.lua
File metadata and controls
executable file
·354 lines (303 loc) · 9.77 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
-- written by slime
-- extended by ARF
local gkpc = gkinterface.GKProcessCommand
local getradar = radar.GetRadarSelectionID
local setradar = radar.SetRadarSelection
local objectpos = Game.GetObjectAtScreenPos
local floor = math.floor
-- Targetless compatability
local function targetless_exists()
return pcall(function() return targetless end)
end
local function targetless_off()
if targetless_exists() then
targetless.var.scanlock = true
targetless.api.radarlock = true
targetless.var.lock = true
return true
end
return false
end
local function targetless_on()
if targetless_exists() then
targetless.api.radarlock = false
targetless.var.lock = false
targetless.var.scanlock = false
return true
end
return false
end
declare("TargetTools", {})
TargetTools.ReTarget = {
target={0,0},
active=gkini.ReadString("targettools", "retarget", "ON") == "ON",
}
function TargetTools.ReTarget:OnEvent(event, type)
if not self.active then return end
if event == "TARGET_CHANGED" and not PlayerInStation() then
self.target = {getradar()}
elseif event == "HUD_SHOW" then
setradar(self.target[1], self.target[2])
end
end
RegisterEvent(TargetTools.ReTarget, "TARGET_CHANGED")
RegisterEvent(TargetTools.ReTarget, "HUD_SHOW")
TargetTools.CallSnoop = {
location=0,
target={0,0}
}
function TargetTools.CallSnoop:OnEvent(event, args)
if args.location == GetCurrentSectorid() then
self.location = args.location
local nodeid, objectid = args.msg:match("{(%d+), (%d+)} at")
if nodeid and objectid then
self.target = { nodeid, objectid }
end
end
end
function TargetTools.CallSnoop.CalledTarget(args)
local self = TargetTools.CallSnoop
if self.location == GetCurrentSectorid() then
setradar(unpack(self.target))
end
end
RegisterEvent(TargetTools.CallSnoop, "CHAT_MSG_GROUP")
RegisterEvent(TargetTools.CallSnoop, "CHAT_MSG_GUILD")
RegisterUserCommand("CalledTarget", TargetTools.CallSnoop.CalledTarget)
function TargetTools.SendTarget(channel)
if GetTargetInfo() then
local formatstr = "Targeting %s (%d%%, %s), \"%s\", {%d, %d} at %dm"
local shieldformatstr = "Targeting %s (%d%% : %d%%, %s), \"%s\" {%d, %d} at %dm"
local nohealthformatstr = "Targeting %s {%d, %d} at %dm"
local name, health, distance, factionid, guild, ship = GetTargetInfo()
local _, shield = GetPlayerHealth(GetCharacterIDByName(name))
local nodeid, objectid = radar.GetRadarSelectionID()
if guild and guild ~= "" then
name = "["..guild.."] "..name
end
local str
if health and ship and factionid then
if shield then
str = shieldformatstr:format(Article(ship), floor(shield),
floor(health*100),
FactionName[factionid],
name,
nodeid,
objectid,
floor(distance)
)
else
str = formatstr:format(Article(ship), floor(health*100),
FactionName[factionid],
name,
nodeid,
objectid,
floor(distance)
)
end
else
str = nohealthformatstr:format(name, nodeid, objectid, floor(distance))
end
SendChat(str, channel:upper())
end
end
function TargetTools.ReadyAtDist(channel)
if GetTargetInfo() then
local name, health, distance, factionid, guild, ship = GetTargetInfo()
SendChat("Ready at ".. floor(distance) .."m from \""..name.."\"", channel:upper())
end
end
function TargetTools.AttackedBy(channel)
if GetLastAggressor() then
local node, object = GetLastAggressor()
local charid = GetCharacterID(node)
if charid == GetCharacterID() then return end
SendChat("Under attack by "..Article(GetPrimaryShipNameOfPlayer(charid))..", \""..GetPlayerName(charid).."\" !", channel)
end
end
function TargetTools.GroupOrGuild(fn)
if GetGroupOwnerID() ~= 0 then
fn("GROUP")
else
fn("GUILD")
end
end
RegisterUserCommand("GroupTarget", function() TargetTools.SendTarget("GROUP") end)
RegisterUserCommand("GuildTarget", function() TargetTools.SendTarget("GUILD") end)
RegisterUserCommand("GTarget", function() TargetTools.GroupOrGuild(TargetTools.SendTarget) end)
RegisterUserCommand("GroupReady", function() TargetTools.ReadyAtDist("GROUP") end)
RegisterUserCommand("GuildReady", function() TargetTools.ReadyAtDist("GUILD") end)
RegisterUserCommand("GReady", function() TargetTools.GroupOrGuild(TargetTools.ReadyAtDist) end)
RegisterUserCommand("GroupAttacked", function() TargetTools.AttackedBy("GROUP") end)
RegisterUserCommand("GuildAttacked", function() TargetTools.AttackedBy("GUILD") end)
RegisterUserCommand("GAttacked", function() TargetTools.GroupOrGuild(TargetTools.AttackedBy) end)
function TargetTools.GetPlayerIDs(charid)
if not charid then charid = RequestTargetStats() end
if not charid then return end
local nodeid = GetPlayerNodeID(charid)
local objectid = GetPrimaryShipIDOfPlayer(charid)
return nodeid, objectid, charid
end
function TargetTools.TargetParent(charid)
setradar(TargetTools.GetPlayerIDs(charid))
end
RegisterUserCommand("TargetParent", TargetTools.TargetParent)
function TargetTools.TargetTurret(rev) -- this way isn't the best
local skip = 1
local nodeid, objectid = getradar()
if not nodeid then
-- We can't target an object's turrets if we aren't targeting an object.
TargetTools.TargetFront()
return
end
-- Disable targetless functionality while we scan.
targetless_off()
local childnode, childobject
local repmin, repmax = 1, 400
if rev then
-- Reverse the scan.
repmin, repmax = repmin*-1, repmax*-1
end
for rep = repmin, repmax, skip do
setradar(nodeid, objectid+rep)
childnode, childobject = getradar()
-- Stop at the first valid object (getradar returns a different objectid from our original target.)
if childobject ~= objectid then
break
end
end
-- Re-enable targetless functionality when we're done scanning.
targetless_on()
if childobject == objectid then
TargetTools.TargetParent()
end
end
RegisterUserCommand("TargetNextTurret", TargetTools.TargetTurret)
RegisterUserCommand("TargetPrevTurret", function() TargetTools.TargetTurret(true) end)
function TargetTools.GetLocalObjects(charid)
if not charid then return end
local nodeid, objectid = GetPlayerNodeID(charid), GetPrimaryShipIDOfPlayer(charid)
if not nodeid or not objectid then return end
local localobjects = {}
targetless_off()
setradar(nodeid, objectid)
while true do
gkpc("LocalRadarNext")
local nextnode, nextobject = getradar()
if not nextnode then break end
local name, health, dist = GetTargetInfo()
table.insert(localobjects, {nodeid=nextnode, objectid=nextobject, dist=dist})
end
targetless_on()
return localobjects
end
function TargetTools.TargetFront(targetturret, reverse)
local xstep = 1 / gkinterface.GetXResolution()
local ystep = 1 / gkinterface.GetYResolution()
local xmin = 0.5 - 15 * xstep
local xmax = 0.5 + 15 * xstep
local ymin = 0.5 - 15 * ystep
local ymax = 0.5 + 15 * ystep
gkpc("RadarNone")
--TODO: rewrite this to spiral outwards from the center
-- Narrow scan for small stuff
for y=ymin, ymax, ystep do
for x=xmin, xmax, xstep do
local node,obj = objectpos(x,y)
if node and obj then setradar(node,obj) return x,y end
end
end
-- Wider fast scan for big things
for y=0.45, 0.55, 0.005 do
for x=0.45, 0.55, 0.005 do
local node,obj = objectpos(x,y)
if node and obj then setradar(node,obj) return x,y end
end
end
if targetturret then
TargetTools.TargetTurret(reverse, true)
end
end
RegisterUserCommand("TargetFront", TargetTools.TargetFront)
function TargetTools.TargetShipType(type)
type = type:lower()
local ships = {}
local function IsShipType(charid)
if GetPrimaryShipNameOfPlayer(charid) and GetPrimaryShipNameOfPlayer(charid):lower():match(type) then
local distance = GetPlayerDistance(charid)
if charid == GetCharacterID() then distance = 10000 end
if distance then
table.insert(ships, {distance=distance, node=GetPlayerNodeID(charid), object=GetPrimaryShipIDOfPlayer(charid)})
end
end
end
ForEachPlayer(IsShipType)
if not ships[1] then return end
table.sort(ships, function(a,b) return a.distance < b.distance end)
setradar(ships[1].node, ships[1].object)
end
RegisterUserCommand("TargetRag", function() TargetTools.TargetShipType("ragnarok") end)
RegisterUserCommand("TargetShip", function(unused, data)
if data then
TargetTools.TargetShipType(table.concat(data, " "))
else
TargetTools.TargetShipType(".")
end
end)
function TargetTools.TargetPlayer(name)
name = name:lower()
local ships = {}
local function IsPlayerName(charid)
if GetPlayerName(charid):lower():match(name) then
local distance = GetPlayerDistance(charid)
if charid == GetCharacterID() then distance = 10000 end -- sort last
if distance then
table.insert(ships, {distance=distance, node=GetPlayerNodeID(charid), object=GetPrimaryShipIDOfPlayer(charid)})
end
end
end
ForEachPlayer(IsPlayerName)
if not ships[1] then return end
table.sort(ships, function(a,b) return a.distance < b.distance end)
setradar(ships[1].node, ships[1].object)
end
RegisterUserCommand("TargetPlayer", function(_, data)
if data then
TargetTools.TargetPlayer(table.concat(data, " "))
else
TargetTools.TargetPlayer(".")
end
end)
function TargetTools.TargetCargo(ctype)
ctype = ctype:lower()
local _name = GetTargetInfo()
if _name and not _name:lower():match(ctype) then gkpc("RadarNone") end
targetless_off()
local found = false
repeat
gkpc("RadarNext")
local name = GetTargetInfo()
if not name then
print("\127ff0000\""..ctype.."\" is not in range")
break
end
local nodeid, objectid = getradar()
if nodeid <= 2
and name ~= "Asteroid"
and name ~= "Ice Crystal"
and name ~= "Hulk"
and name ~= "Hulk debris"
and name:lower():match(ctype) then
found = true
end
until found
targetless_on()
end
RegisterUserCommand("TargetCargo", function(_, data)
if data then
TargetTools.TargetCargo(table.concat(data, " "))
else
TargetTools.TargetCargo(".")
end
end)
dofile("ui.lua")