-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAutoScaleInjector.lua
More file actions
178 lines (163 loc) · 5.97 KB
/
AutoScaleInjector.lua
File metadata and controls
178 lines (163 loc) · 5.97 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
--[[LUAStart
className = "AutoScaledObject"
versionNumber = "1.0.0"
scaleMultiplierX = 1.0
scaleMultiplierY = 1.0
scaleMultiplierZ = 1.0
finishedLoading = false
debuggingEnabled = false
onUpdateTriggerCount = 0
onUpdateGridSize = 1.0
function onLoad(save_state)
finishedLoading = true
end
function onUpdate()
onUpdateTriggerCount = onUpdateTriggerCount + 1
if onUpdateTriggerCount > 60 then
onUpdateTriggerCount = 0
if finishedLoading == true and onUpdateGridSize ~= Grid.sizeX then
resetScale()
end
end
end
function reloadMini()
self.reload()
end
function resetScale()
newScaleX = Grid.sizeX * scaleMultiplierX
newScaleY = Grid.sizeX * scaleMultiplierY
newScaleZ = Grid.sizeX * scaleMultiplierZ
if debuggingEnabled == true then
print(self.getName() .. ": Reset scale with reference to grid.")
end
scaleVector = vector(newScaleX, newScaleY, newScaleZ)
self.setScale(scaleVector)
onUpdateGridSize = Grid.sizeX
end
function onRotate(spin, flip, player_color, old_spin, old_flip)
if flip ~= old_flip then
destabilize()
local object = self
local timeWaiting = os.clock() + 0.26
local rotateWatch = function()
if object == nil or object.resting then
return true
end
local currentRotation = object.getRotation()
local rotationTarget = object.getRotationSmooth()
return os.clock() > timeWaiting and (rotationTarget == nil or currentRotation:angle(rotationTarget) < 0.5)
end
local rotateFunc = function()
if object == nil then
return
end
if debuggingEnabled == true then
print(self.getName() .. ": Stabilizing after rotation.")
end
stabilize()
end
Wait.condition(rotateFunc, rotateWatch)
end
end
function onPickUp(pcolor)
destabilize()
end
function onDrop(dcolor)
stabilize()
end
function stabilize()
if debuggingEnabled == true then
print(self.getName() .. ": stabilizing.")
end
local rb = self.getComponent("Rigidbody")
rb.set("freezeRotation", true)
end
function destabilize()
if debuggingEnabled == true then
print(self.getName() .. ": de-stabilizing.")
end
local rb = self.getComponent("Rigidbody")
rb.set("freezeRotation", false)
end
LUAStop--lua]]
className = "AutoScaleInjector"
versionNumber = "1.0.0"
finishedLoading = false
debuggingEnabled = false
onUpdateTriggerCount = 0
onUpdateGridSize = 1.0
injectedFrameLimiter = 0
collisionProcessing = {}
function onLoad(script_state)
finishedLoading = true
self.setVar("finishedLoading", true)
self.setName("Auto-Scale Injector " .. versionNumber)
end
function onCollisionEnter(collision_info)
table.insert(collisionProcessing, collision_info)
end
function onUpdate()
if injectedFrameLimiter > 0 then
injectedFrameLimiter = injectedFrameLimiter - 1
end
if injectedFrameLimiter == 0 and #collisionProcessing > 0 then
local collision_info = table.remove(collisionProcessing)
local object = collision_info.collision_object
if object ~= nil then
local hitList = Physics.cast({
origin = object.getBounds().center,
direction = {0,-1,0},
type = 1,
max_distance = 10,
debug = false,
})
local attemptCount = 1
for _, hitTable in ipairs(hitList) do
-- This hit makes sure the injector is the first object directly below the mini
if hitTable ~= nil and hitTable.hit_object == self then
if self.getRotationValue() == "[00ff00]INJECT[-]" then
objClassName = object.getVar("className")
if objClassName ~= "MiniInjector" and
objClassName ~= "MeasurementToken" and
objClassName ~= "MeasurementToken_Move" and
objClassName ~= "MeasurementTool" and
objClassName ~= "AutoScaledObject" and
objClassName ~= "AutoScaleInjector" then
print("[00ff00]Locking scale[-] for " .. object.getName() .. ".")
injectToken(object)
injectedFrameLimiter = 60
break
end
elseif self.getRotationValue() == "[ff0000]REMOVE[-]" then
if object.getVar("className") == "AutoScaledObject" then
print("[ff0000]Removing[-] scale lock for " .. object.getName() .. ".")
object.script_state = ""
object.script_code = ""
object.setLuaScript("")
object.reload()
break
end
else
error("Invalid rotation.")
break
end
else
attemptCount = attemptCount + 1
if (debuggingEnabled) then
print("Did not find injector, index "..tostring(attemptCount)..".")
end
end
end
end
end
end
function injectToken(object)
local script = self.getLuaScript()
local newScript = script:sub(script:find("LUAStart")+8, script:find("LUAStop")-1)
currentScale = object.getScale()
newScript = newScript:gsub("scaleMultiplierX = 1.0", "scaleMultiplierX = " .. (currentScale.x / Grid.sizeX))
newScript = newScript:gsub("scaleMultiplierY = 1.0", "scaleMultiplierY = " .. (currentScale.y / Grid.sizeX))
newScript = newScript:gsub("scaleMultiplierZ = 1.0", "scaleMultiplierZ = " .. (currentScale.z / Grid.sizeX))
object.setLuaScript(newScript)
object.reload()
end