source: main.lua

Last change on this file was 129, checked in by drazed, 16 months ago

fix loss-of-control on initial login if not docked issue

File size: 19.6 KB
Line 
1declare("targetless", {})
2targetless.start = {}
3targetless.stop = {}
4targetless.retarget = {}
5targetless.logout = {}
6targetless.init = {}
7targetless.re_attach = {}
8targetless.update = {}
9targetless.targetchange = {}
10targetless.sectorupdate = {}
11targetless.gothit = {}
12targetless.scanevent = {}
13targetless.scanevent.lock = false
14targetless.scan = {}
15targetless.addroid = {}
16targetless.oretbl = {}
17targetless.notelabel = nil
18targetless.orelabel = nil
19
20dofile('var.lua')
21dofile('api.lua')
22dofile('lists/Controller.lua')
23dofile('ui/ui.lua')
24
25targetless.playercaps = {
26    ["Trident Type M"] = true,
27    ["Goliath"] = true,
28    ["Capella"] = true,
29}
30
31function targetless.scancaps()
32    targetless.var.mycaps = {}
33    local _, name, shiptype, containerid
34    for itemid, _ in PlayerInventoryPairs() do
35        _, name, _, _, shiptype, _, _, containerid, _, _ = GetInventoryItemInfo(itemid)
36        if targetless.playercaps[shiptype] and containerid > 0 then -- found a capship!
37            local stationid = GetInventoryItemLocation(itemid)
38            local sectorid = GetSectorIDOfStation(stationid)
39            local mycap = {
40                itemid=itemid,
41                containerid=containerid,
42                stationid=stationid,
43                sectorid=sectorid,
44                name=name,
45                shiptype=shiptype,
46                ship=nil,
47            }
48            targetless.var.mycaps[name] = mycap
49        end
50    end
51end
52
53function targetless.start:OnEvent(eventname, ...)
54    -- scan for your capships
55    targetless.scancaps()
56    targetless.var.state = true
57    targetless.Controller:update()
58end
59
60function targetless.stop:OnEvent(eventname, ...)
61    if(GetCurrentStationType() ~= 1) then
62        targetless.var.state = false
63    end
64end
65
66function targetless.logout:OnEvent(eventname, ...)
67    targetless.var.state = false
68    ReloadInterface()
69end
70
71function targetless.retarget:OnEvent(eventname, ...)
72    -- TODO we should probably be checking if the actual player change
73    -- turret event rather then a turret-guest
74    -- END TODO
75
76    -- re-aquire target after short delay, so missles can lock
77    -- we de-target first, so target actually gets required
78    local timer = Timer()
79        timer:SetTimeout(1000, function()
80        local objecttype,objectid = radar.GetRadarSelectionID()
81        gkinterface.GKProcessCommand("RadarNone")
82        radar.SetRadarSelection(objecttype, objectid)
83    end)
84end
85
86function targetless.re_attach()
87    -- when VO reloads the HUD it deletes all our lists thinking they're
88    -- the selfinfo holder box =(
89    targetless.Controller.currentbuffer:reset()
90    targetless.Controller.rebuildbuffer:reset()
91
92    while(targetless.RoidList[1]) do table.remove(targetless.RoidList, 1) end
93    targetless.Controller.iup = nil
94    targetless.Controller.selfinfo = nil
95    targetless.Controller.centerHUD = nil
96    targetless.Controller.totals.iup = nil
97    targetless.Controller.currentbuffer.iupobj = nil
98    targetless.appendiups()
99    if GetStationLocation() == nil then targetless.Controller:update() end
100    targetless.RoidList:load(GetCurrentSectorid())
101end
102
103function targetless.strsplit(pattern,text)
104    local result = {}
105    local theStart = 1
106    local theSplitStart, theSplitEnd = string.find(text,pattern,theStart)
107    while theSplitStart do
108        table.insert(result,string.sub(text,theStart,theSplitStart-1))
109        theStart = theSplitEnd + 1
110        theSplitStart, theSplitEnd = string.find(text,pattern,theStart)
111    end
112    table.insert(result, string.sub(text,theStart))
113    return result
114end
115
116function targetless.scanevent:OnEvent(eventname, data)
117    if self.lock then return end
118    targetless.scanevent.objectid = targetless.scan(data)
119    if (targetless.var.scanall == "ON") then targetless.addroid() end
120end
121
122function targetless.scan(data)
123    local objecttype,objectid = radar.GetRadarSelectionID()
124    if(data ~= nil and data ~= "" and objecttype == 2) then
125        local ores = {}
126        string.gsub(data,"(.-)\n", function(a) if(not string.find(a, "Temperature:")) then table.insert(ores, a) end end)
127        if(#ores >= 1 and ores[1] ~= "Object too far to scan") then
128            for i,v in ipairs(ores) do
129                if v then
130                    v = string.gsub(v, "\r", "")
131                    v = string.gsub(v, "\n", "")
132                    v = string.gsub(v, "%%", "")
133                    v = string.gsub(v, " Ore", "")
134                    local ore = targetless.strsplit(": ",v)
135                    targetless.oretbl[ore[1]] = ore[2]
136                end
137            end
138            if (not iup.GetNextChild(targetless.orelabel)) then
139                local roid = targetless.Roid:new()
140                roid.ore = targetless.oretbl
141                targetless.notelabel = iup.label{title="(Not saved)", font=targetless.var.font}
142                targetless.orelabel = roid:getlabelbytag("<ore>")
143                iup.Append(iup.GetParent(HUD.targethealth), targetless.notelabel)
144                iup.Append(iup.GetParent(HUD.targetshiptype), targetless.orelabel)
145            end
146            targetless.scanevent.lock = true
147        end
148    end
149
150    return objectid
151end
152
153function targetless.addroid()
154    if(targetless.scanevent.lock) then
155        if targetless.RoidList:add(targetless.scanevent.objectid,"",targetless.oretbl) then
156            targetless.RoidList:save()
157            targetless.RoidList:load(GetCurrentSectorid())
158            HUD:PrintSecondaryMsg("\127ffffffOre Saved!\127o")
159            targetless.Controller:update()
160            iup.Detach(targetless.notelabel)
161            iup.Destroy(targetless.notelabel)
162            targetless.notelabel = iup.hbox{}
163            iup.Append(iup.GetParent(HUD.targetname), targetless.notelabel)
164        end
165    end
166end
167
168function targetless.unroid()
169    -- unroid freezes up really bad if the ore-list is open during operation
170    -- if current list is roidlist do an lsswitch before running unroid
171    if targetless.Controller.mode == "Ore" then targetless.Controller:switch() end
172
173    local roids = targetless.RoidList:ids()
174    local objecttype = 2;
175    local max = 0
176    local numroids = 0
177    for i,v in pairs(roids) do
178        numroids = numroids+1 -- #roids is not sequential doesn't have size
179        if(tonumber(i) > max) then max = tonumber(i) end
180    end
181    local estimate = "No new roids in range.  "..numroids.." of at least "..max.." scanned!"
182    if(numroids == max) then estimate = "Estimate sector scanning completed!!" end
183    if(numroids == 0) then estimate = "No scanned/new roids in range!" end
184    local name = ""
185    local nearestdistance = 5000
186    local nearestid = 0
187    targetless.api.radarlock = true
188    gkinterface.GKProcessCommand("RadarNone")
189    targetless.var.lock = true
190    local endtype,endid = radar.GetRadarSelectionID()
191    local ttype,tid = radar.GetRadarSelectionID()
192    local endid = tid
193    repeat
194        if(tid) then
195            name = HUD.targetname.title
196            if(name == "Asteroid" or name == "Ice Crystal") then
197                if(roids[tonumber(tid)] ~= true) then
198                    local distance = HUD.targetdistance.title
199                    distance = distance:gsub(",","")
200                    distance = distance:gsub("m","")
201                    distance = tonumber(distance)
202                    if(distance <= nearestdistance) then
203                        nearestdistance = distance
204                        nearestid = tid
205                    end
206                end
207            end
208        end
209        gkinterface.GKProcessCommand("RadarNext")
210        ttype,tid = radar.GetRadarSelectionID()
211    until(tid == endid)
212    targetless.var.lock = false
213    targetless.api.radarlock = false
214
215    if(nearestdistance < 5000) then
216        radar.SetRadarSelection(objecttype, nearestid)
217        HUD:PrintSecondaryMsg("\127ffffffRoid found "..nearestdistance.."m away!  "..numroids.." of at least "..max.." scanned!" .."\127o")
218    else
219        -- no unscanned found
220        HUD:PrintSecondaryMsg("\127ffffff"..estimate.."\127o")
221
222        radar.SetRadarSelection(GetPlayerNodeID(GetCharacterID()), GetPrimaryShipIDOfPlayer(GetCharacterID()))
223    end
224end
225
226
227function targetless.sectorupdate:OnEvent(eventname, ...)
228    -- refresh mycaps
229    targetless.scancaps()
230
231    targetless.RoidList:save()
232    targetless.RoidList:load(GetCurrentSectorid())
233
234    -- re-aquire target after short delay, so missles can lock
235    local timer = Timer()
236        timer:SetTimeout(1000, function()
237        local objecttype,objectid = radar.GetRadarSelectionID()
238        radar.SetRadarSelection(objecttype, objectid)
239    end)
240end
241
242function targetless.update:OnEvent(eventname, ...)
243    local scaninfo = HUD.scaninfo.title
244    targetless.targetchange()
245    HUD:OnEvent(eventname, ...)
246    if targetless.api.radarlock then
247        HUD.scaninfo.title = scaninfo
248    end
249    targetless.Controller:update()
250end
251
252function targetless.gothit:OnEvent(eventname,myid,id)
253    if targetless.var.autopin.damage == "ON" then
254        targetless.Controller.pin[id] = 1
255    end
256end
257
258function targetless.targetchange()
259    -- don't run if radar is locked
260    if targetless.api.radarlock then return end
261
262    targetless.scanevent.lock = false
263    targetless.oretbl = {}
264    targetless.scanevent.objectid = nil
265
266    local objecttype,objectid = radar.GetRadarSelectionID()
267    if objecttype and objectid then
268        targetless.var.lasttarget = objectid
269        targetless.var.lasttype = objecttype
270    end
271    if(targetless.notelabel) then
272        iup.Detach(targetless.notelabel)
273        iup.Destroy(targetless.notelabel)
274        targetless.notelabel = nil -- iup.hbox{}
275    end
276    if(targetless.orelabel) then
277        iup.Detach(targetless.orelabel)
278        iup.Destroy(targetless.orelabel)
279        targetless.orelabel = nil -- iup.hbox{}
280    end
281    if(tonumber(objecttype) == 2) then
282        for i,v in ipairs(targetless.RoidList) do
283            if(tonumber(v.id)==objectid) then
284                targetless.notelabel = iup.label{title=v.note, font=targetless.var.font}
285                targetless.orelabel = v:getlabelbytag("<ore>")
286            end
287        end
288    end
289    if not targetless.notelabel then targetless.notelabel = iup.hbox{} end
290    if not targetless.orelabel then targetless.orelabel = iup.hbox{} end
291    iup.Append(iup.GetParent(HUD.targetname), targetless.notelabel)
292    iup.Append(iup.GetParent(HUD.targetshiptype), targetless.orelabel)
293end
294
295function targetless.printinfo()
296    print("\127ffffff" .. "Target List " .. targetless.var.version .. " loaded...\n\t\127ff77ff/targetless\127o  ->  display options")
297end
298
299function targetless.usercmd(cmd)
300    targetless.ui.show()
301end
302
303function targetless.init:OnEvent(eventname, ...)
304    targetless.RoidList.allroids = unspickle(LoadSystemNotes(targetless.var.noteoffset) or "") or {}
305    targetless.RoidList.sector = GetCurrentSectorid()
306    targetless.appendiups()
307    targetless.RoidList:load(GetCurrentSectorid())
308    if GetStationLocation() == nil then
309        -- scan for your capships
310        targetless.scancaps()
311        targetless.var.state = true
312        targetless.Controller:update()
313    end
314    UnregisterEvent(self, "PLAYER_ENTERED_GAME")
315    UnregisterEvent(HUD, "TARGET_CHANGED")
316
317    RegisterEvent(targetless.re_attach, "HUD_RELOADED")
318    RegisterEvent(targetless.retarget, "TURRET_OCCUPIED")
319    RegisterEvent(targetless.retarget, "TURRET_EMPTY")
320    RegisterEvent(targetless.start, "LEAVING_STATION")
321    RegisterEvent(targetless.stop, "ENTERING_STATION")
322    RegisterEvent(targetless.logout, "PLAYER_LOGGED_OUT")
323    RegisterEvent(targetless.scanevent, "TARGET_SCANNED")
324    RegisterEvent(targetless.sectorupdate, "SECTOR_CHANGED")
325    RegisterEvent(targetless.update, "TARGET_CHANGED")
326    RegisterEvent(targetless.gothit, "PLAYER_GOT_HIT")
327    RegisterEvent(targetless.hudtoggle, "HUD_TOGGLE")
328
329    -- ideally this would be triggered off SHIP_CHANGED event, but something is up with that one
330    -- that causes complete loss of control if called on initial load while in an undocked ship
331    RegisterEvent(targetless.scancaps, "SHIP_SPAWN_CINEMATIC_FINISHED")
332end
333
334function targetless.appendiups()
335    -- set sizes back to quarter for targetinfo and license watch
336    --HUD.licensewatchframe.size="QUARTER"
337    --HUD.missiontimerframe.size="QUARTER"
338    --HUD.targetframe.size="QUARTER"
339
340    iup.Append(iup.GetParent(HUD.targetframe), iup.hbox{iup.fill{size="QUARTER"}})
341
342    targetless.Controller:generatetotals()
343    targetless.var.iuplists = iup.vbox {}
344    targetless.var.selfship = iup.vbox {}
345
346    local licensewatchframe
347    if(HUD.visibility.license=="YES" or HUD.visibility.missiontimers=="YES") then
348        iup.Detach(HUD.licensewatchframe)
349        iup.Detach(HUD.missiontimerframe)
350        licensewatchframe = iup.zbox{
351            HUD.licensewatchframe,
352            HUD.missiontimerframe,
353        }
354    else
355        licensewatchframe = iup.hbox{}
356    end
357
358    local targetinfoframe
359    if(HUD.visibility.targetinfo=="YES") then
360        iup.Detach(HUD.targetframe)
361        targetinfoframe = iup.zbox{
362            HUD.targetframe,
363        }
364    else
365        targetinfoframe = iup.hbox{iup.fill{size="QUARTER"}}
366    end
367
368    local hudinfo = iup.GetParent(HUD.selfinfoframe)
369    local addons = iup.GetParent(HUD.addonframe)
370    local schat = iup.GetParent(HUD.secondarychatarea)
371    iup.Detach(hudinfo)
372    iup.Detach(addons)
373    iup.Detach(schat)
374    iup.Detach(HUD.restcargoframe)
375
376    local padding = 0
377    local xres = gkinterface.GetXResolution()
378    local yres = gkinterface.GetYResolution()
379    local threequarter = xres*0.75
380    local quarter = xres*0.25
381    if HUD.Centered then
382        local usex = yres*(4/3)
383        if usex < xres then
384            padding = (xres - usex)
385            threequarter = usex*0.75
386            quarter = usex*0.25
387        end
388    end
389
390    targetless.var.PlayerData = iup.hbox
391    {
392        iup.hbox{
393            iup.hbox{iup.fill{size=padding/2,},},
394            iup.vbox{
395                iup.hbox{iup.fill{size=threequarter,},},
396                iup.fill{size="%22",},
397                iup.hbox{
398                    iup.fill{size=10},
399                    hudinfo,
400                    addons,
401                    schat,
402                    iup.fill{},
403                    gap="4",
404                },
405                iup.hbox{
406                    iup.fill{size=10},
407                    HUD.restcargoframe,
408                },
409            },
410            iup.vbox
411            {
412                iup.hbox {
413                    iup.vbox{
414                        iup.zbox{
415                            licensewatchframe,
416                        },
417                        iup.zbox{
418                            targetinfoframe,
419                        },
420                    },
421                },
422                targetless.var.selfship,
423                targetless.Controller.totals.iup,
424                targetless.var.iuplists,
425                iup.hbox{iup.fill{size=quarter,},},
426                expand="VERTICAL",
427                margin="4x4",
428                gap="4",
429            },
430        },
431    }
432
433    local yres = gkinterface.GetYResolution()*HUD_SCALE
434    targetless.var.centerHUD = iup.vbox{}
435    local centerHUDinfo = iup.hbox
436    {
437        iup.fill{},
438        iup.vbox{
439            iup.fill{size="HALF"},
440            targetless.var.centerHUD,
441            iup.fill{},
442            alignment="ACENTER",
443        },
444        iup.fill{},
445        alignment="ACENTER",
446    }
447    iup.Append(HUD.pluginlayer, centerHUDinfo)
448
449    local bsinfo = iup.GetParent(HUD.BSinfo.enemylabel)
450    targetless.var.PlayerData.expand = "NO"
451    targetless.var.centerHUD.expand = "NO"
452    iup.Detach(bsinfo)
453    iup.Append(iup.GetParent(iup.GetParent(HUD.locationtext)), bsinfo)
454    iup.Append(HUD.pluginlayer, targetless.var.PlayerData)
455
456    if(targetless.notelabel) then
457        iup.Detach(targetless.notelabel)
458        iup.Destroy(targetless.notelabel)
459        targetless.notelabel = nil
460    end
461    if(targetless.orelabel) then
462        iup.Detach(targetless.orelabel)
463        iup.Destroy(targetless.orelabel)
464        targetless.orelabel = nil
465    end
466    targetless.notelabel = iup.hbox{}
467    targetless.orelabel = iup.hbox{}
468    iup.Append(iup.GetParent(HUD.targetname), targetless.notelabel)
469    iup.Append(iup.GetParent(HUD.targetshiptype), targetless.orelabel)
470
471    -- fix for ticket #45 and mission-update crashing
472    -- debugged/fixed by Chocoleteer
473    HUD.dlg:map()
474end
475
476function targetless.hudtoggle()
477    targetless.var.lock = not targetless.var.lock
478    targetless.var.state = not targetless.var.state
479    targetless.Controller:update()
480end
481
482RegisterEvent(targetless.init, "PLAYER_ENTERED_GAME")
483RegisterUserCommand("targetselect", function(data, args) targetless.Controller:settarget(args[1]) end) -- set binds using aliases for any target number
484RegisterUserCommand("targetmycap",function()
485    targetless.var.targetnum = 0
486    targetless.Controller:settarget(0)
487end)
488RegisterUserCommand("lsswitch",function() targetless.Controller:switch() end)
489RegisterUserCommand("lsback",function() targetless.Controller:switchback() end)
490RegisterUserCommand("lssort",function() targetless.Controller:sort() end)
491RegisterUserCommand("nextLS",function() targetless.Controller:targetnext() end)
492RegisterUserCommand("prevLS",function() targetless.Controller:targetprev() end)
493RegisterUserCommand("pin",function() targetless.Controller:pinfunc() end)
494RegisterUserCommand("clearpin",function()
495    targetless.Controller.pin = {}
496    targetless.Controller.currentbuffer.pin = targetless.Controller.pin
497    targetless.Controller.rebuildbuffer.pin = targetless.Controller.pin
498    targetless.Controller:update()
499end)
500RegisterUserCommand("cyclestatus",function() targetless.Controller:cyclestatus() end)
501RegisterUserCommand("addroid",targetless.addroid)
502RegisterUserCommand("editroid",function()
503    local objecttype,objectid = radar.GetRadarSelectionID()
504    local note = false
505    local ore = ""
506    if(tonumber(objecttype) == 2) then
507        for i,v in ipairs(targetless.RoidList) do
508            if(tonumber(v.id)==objectid) then
509                note = v.note
510                for k,v in pairs(v.ore) do
511                    ore = ore..targetless.Roid.colorore(k)..":"..v.."%  "
512                end
513            end
514        end
515    end
516    if note then
517        targetless.ui.ore.edit.element.sector = GetCurrentSectorid()
518        targetless.ui.ore.edit.element.id = objectid
519        targetless.ui.ore.edit.element.ore.title = ore
520        targetless.ui.ore.edit.element.note.value = note
521        targetless.ui.ore.edit.dlg:show()
522    else
523        HUD:PrintSecondaryMsg("\127ffffffYou need to scan it first!\127o")
524    end
525end)
526
527
528RegisterUserCommand("unroid",targetless.unroid)
529RegisterUserCommand("importconfigroids",function() targetless.ui.ore.importconfig() end)
530RegisterUserCommand("selecttarget1",function() targetless.Controller:settarget(1) end)
531RegisterUserCommand("selecttarget2",function() targetless.Controller:settarget(2) end)
532RegisterUserCommand("selecttarget3",function() targetless.Controller:settarget(3) end)
533RegisterUserCommand("selecttarget4",function() targetless.Controller:settarget(4) end)
534RegisterUserCommand("selecttarget5",function() targetless.Controller:settarget(5) end)
535RegisterUserCommand("selecttarget6",function() targetless.Controller:settarget(6) end)
536RegisterUserCommand("selecttarget7",function() targetless.Controller:settarget(7) end)
537RegisterUserCommand("selecttarget8",function() targetless.Controller:settarget(8) end)
538RegisterUserCommand("selecttarget9",function() targetless.Controller:settarget(9) end)
539RegisterUserCommand("selecttarget10",function() targetless.Controller:settarget(10) end)
540RegisterUserCommand("targetnone",function()
541    targetless.var.lasttype = nil
542    targetless.var.lasttarget = nil
543    gkinterface.GKProcessCommand("RadarNone")
544end)
545RegisterUserCommand("targetless",function(data, args) targetless.usercmd(args) end)
546
547-- load android stuff last, only if on android
548if gkinterface.IsTouchModeEnabled() then
549    dofile('touchless.lua')
550end
Note: See TracBrowser for help on using the repository browser.