-
Notifications
You must be signed in to change notification settings - Fork 2
/
OneWorldLineSaverToken.lua
144 lines (134 loc) · 4.3 KB
/
OneWorldLineSaverToken.lua
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
className = "OneWorldLineSaver"
versionNumber = "2.0.0"
oneWorldLines = nil
linesSaved = false
finishedLoading = false
function onload(saved_data)
if saved_data ~= "" then
local loaded_data = JSON.decode(saved_data)
if loaded_data.linesSaved ~= nil then
linesSaved = loaded_data.linesSaved
end
if loaded_data.oneWorldLines ~= nil then
oneWorldLines = loaded_data.oneWorldLines
end
end
revertToSave()
rebuildContextMenu()
self.setVar("className", "OneWorldLineSaver")
self.setVar("finishedLoading", true)
self.setName("LineSaver " .. versionNumber);
end
function rebuildContextMenu()
self.clearContextMenu()
if (linesSaved) then
self.addContextMenuItem("[X] Save Lines", saveLines)
else
self.addContextMenuItem("[ ] Save Lines", saveLines)
end
if (linesSaved and oneWorldLines ~= nil) then
self.addContextMenuItem("Revert To Save", revertToSave)
self.addContextMenuItem("Reset LineSaver", resetLineSaver)
end
end
function saveLines()
local validLines = getValidVectorLines(true)
if validLines == nil or #validLines == 0 then
print("No drawn lines to save! Only lines within the map area are saved.")
return
end
oneWorldLines = validLines
linesSaved = true
self.script_state = JSON.encode({
linesSaved = linesSaved,
oneWorldLines = oneWorldLines
});
print("Drawn lines saved! Only lines within the map area are saved.")
rebuildContextMenu()
end
function revertToSave()
if (linesSaved and oneWorldLines ~= nil) then
local newLines = getValidVectorLines(false)
for _,v in ipairs(oneWorldLines) do
table.insert(newLines, v)
end
Global.setVectorLines(newLines)
end
end
function onObjectDestroy(destroy_obj)
if destroy_obj == self and linesSaved and oneWorldLines ~= nil then
Global.setVectorLines(getValidVectorLines(false))
end
end
function getValidVectorLines(internal)
local globalLines = Global.getVectorLines()
-- Loop over the lines and find valid ones to save.
-- Use OneWorld map bounds.
local lineBounds = getMapBounds(true)
local minX = lineBounds.x / -2.0
local maxX = lineBounds.x / 2.0
local minZ = lineBounds.z / -2.0
local maxZ = lineBounds.z / 2.0
local validLines = {}
for i = 1, #globalLines do
local currentLine = globalLines[i]
-- Determine whether the line falls within bounds
-- Save the line if a single point falls within the bounds
local saveLine = false
for li = 1, #currentLine.points do
local currentPoint = currentLine.points[li]
if currentPoint.x > minX and
currentPoint.x < maxX and
currentPoint.z > minZ and
currentPoint.z < maxZ then
saveLine = true
break
end
end
if internal == saveLine then
table.insert(validLines, currentLine)
end
end
return validLines
end
function resetLineSaver()
if (linesSaved and oneWorldLines ~= nil) then
oneWorldLines = nil
linesSaved = false
self.script_state = JSON.encode({
linesSaved = linesSaved,
oneWorldLines = oneWorldLines
});
print("LineSaver reset! Saved lines forgotten!")
rebuildContextMenu()
end
end
function getOneWorldMap()
for _, obj in ipairs(getAllObjects()) do
if obj ~= self and obj ~= nil and obj.getName() == "_OW_vBase" then
return obj
end
end
return nil
end
function getMapBounds(debug)
local defaultBounds = {x = 88.07, y = 1, z = 52.02}
local oneWorldMap = getOneWorldMap()
if oneWorldMap ~= nil then
local oneWorldBounds = oneWorldMap.getBounds();
if oneWorldBounds.size.x > 10 then
if debuggingEnabled then
print("Using OneWorld map bounds.")
end
return oneWorldBounds.size
end
if debug or debuggingEnabled then
print("A OneWorld map is not deployed! Using default bounds.")
end
return defaultBounds
end
if debug or debuggingEnabled then
print("OneWorld is not available! Using default bounds.")
end
return defaultBounds
end