-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDial Set source.ttslua
171 lines (156 loc) · 5.12 KB
/
Dial Set source.ttslua
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
local dialLuaScript = [[
function setShip(shipTable)
assignedShip = shipTable[1]
end
function onPickedUp()
if assignedShip ~= nil then Global.call('DialPickedUp', {dial=self, ship=assignedShip}) end
end
function onDropped()
if assignedShip ~= nil then Global.call('DialDropped', {dial=self, ship=assignedShip}) end
end]]
local talSegPos = {1, 0, 4}
local koiPos = {{2, 0, 4}, {2, 0, 5}, {-2, 0, 4}, {-2, 0, 5}}
local koiCount = 1
function decodeDesc(desc)
dir = nil
speed = nil
type = nil
if desc:sub(-1, -1) == 's' then
type = 'seg'
speed = tonumber(desc:sub(-2, -2))
dir = desc:sub(-3, -3)
elseif desc:sub(-1, -1) == 't' then
type = 'tal'
speed = tonumber(desc:sub(-2, -2))
dir = desc:sub(-3, -3)
elseif desc:sub(-1,-1) == 'r' then
type = desc:sub(1, 1)
if desc:sub(2,2) == 'r' then
dir = 'r'
elseif desc:sub(2,2) == 'e' or desc:sub(2,2) == 'l' then
dir = 'l'
end
speed = 0
elseif desc:sub(1, 1) == 'k' then
type = 'koi'
speed = tonumber(desc:sub(2, 2))
dir = 's'
elseif desc:sub(1, 1) == 's' then
type = 's'
speed = tonumber(desc:sub(2, 2))
dir = 's'
else
type = desc:sub(1, 1)
dir = desc:sub(2, 2)
speed = tonumber(desc:sub(3, 3))
end
out = {0, 0, speed}
if type == 'seg' or type == 'tal' then
out = {talSegPos[1], talSegPos[2], talSegPos[3]}
elseif type == 'koi' then
out = koiPos[koiCount]
koiCount = koiCount+1
else
if type == 'b' then
out[1] = 1
elseif type == 't' then
out[1] = 2
end
end
if dir == 'e' or dir == 'l' then
out[1] = -1*out[1]
end
return out
end
function moveSpeed(desc)
local speed = nil
if desc:sub(-1, -1) == 's' then
speed = tonumber(desc:sub(-2, -2))
elseif desc:sub(-1, -1) == 't' then
speed = tonumber(desc:sub(-2, -2))
elseif desc:sub(-1,-1) == 'r' then
speed = 0
elseif desc:sub(1, 1) == 'k' then
speed = tonumber(desc:sub(2, 2))
elseif desc:sub(1, 1) == 's' then
speed = tonumber(desc:sub(2, 2))
else
speed = tonumber(desc:sub(3, 3))
end
if speed == nil then print(desc) end
return speed
end
function offset(table1, table2)
table2 = table2 or {0, 0, 0}
return {table1[1]+table2[1], table1[2]+table2[2], table1[3]+table2[3]}
end
function scale(table, value)
value = value or 1
return {table[1]*value, table[2]*value, table[3]*value}
end
function localPos(pos)
refOffset = self.getPosition()
refRot = math.rad(-1*self.getRotation()[2])
local posRot = {pos[3]*math.sin(refRot)-pos[1]*math.cos(refRot), pos[2], -1*pos[1]*math.sin(refRot) - pos[3]*math.cos(refRot)}
return {posRot[1]+refOffset[1], refOffset[2], posRot[3]+refOffset[3]}
end
-- FUNCTION FOR DIAL ASSIGNMENT UPON LAYOUT
function assignCallback(obj, params)
table.insert(spawnedDials, obj)
if #spawnedDials == dialCount then
if params.ship ~= nil then
Global.call('DialAPI_AssignSet', {set=spawnedDials, ship=params.ship})
end
if params.callbackObj ~= nil then
params.callbackObj.call(params.callbackFun, params.callbackParams)
end
end
end
-- LAY OUT DIALS, CALL ASSIGN IF NECCESARY, DESTROY SELF
function init(arg)
dialCount = #self.getObjects()-2
spawnedDials = {}
local callbackTable = arg or {}
local dialSpacing = (2/0.625)*(self.getScale()[1])
local stackDials = self.getObjects()
local minSpeed = 5
local maxSpeed = 0
for k,dial in pairs(stackDials) do
if dial.description ~= 'spec' then
local dialSpeed = moveSpeed(dial.description)
if dialSpeed < minSpeed then
minSpeed = dialSpeed
end
if dialSpeed > maxSpeed then
maxSpeed = dialSpeed
end
end
end
local posOffset = {0, 0.1, -1*0.5*(maxSpeed-minSpeed) - minSpeed}
for k, dial in pairs(stackDials) do
if dial['description'] ~= 'spec' then
local dialPos = decodeDesc(dial['description'])
dialPos = offset(dialPos, posOffset)
dialPos = scale(dialPos, dialSpacing)
dialPos = localPos(dialPos)
local dialRot = {0, self.getRotation()[2], 0}
newDial = self.takeObject({ position=dialPos, rotation=dialRot, guid=dial['guid'],
callback='assignCallback', callback_owner=self, params={ship=callbackTable.ship,
cb=callbackTable.callbackFun, cbArgs=callbackTable.callbackParams, cbObj=callbackTable.callbackObj}})
newDial.setPosition(dialPos)
newDial.setRotation(dialRot)
newDial.setLuaScript(dialLuaScript)
end
end
self.destruct()
return spawnedDials
end
function onLoad()
if self.getName() == 'Dial Set source' then
self.setPosition({0, -3, -7.5})
self.setRotation({0, 0, 0})
self.lock()
self.tooltip = false
self.interactable = false
end
end