266 lines
8.1 KiB
Plaintext
266 lines
8.1 KiB
Plaintext
--@name RaceLogic
|
|
--@author Opti1337
|
|
--@server
|
|
--@model models/sprops/cuboids/height06/size_1/cube_6x6x6.mdl
|
|
|
|
local digitsColor = Color(255, 128, 0)
|
|
local backgroundColor = Color(0, 0, 0)
|
|
local bodyColor = Color(40, 40, 40)
|
|
|
|
local holdDuration = 2
|
|
|
|
local reachSoundPath = "buttons/blip2.wav"--"garrysmod/ui_click.wav"
|
|
local resetSoundPath = "buttons/combine_button7.wav"
|
|
|
|
local lastResultsCount = 3
|
|
local printResultBeforeReset = true
|
|
|
|
------------------------------------
|
|
|
|
local METER = 39.37
|
|
|
|
local speedometerNumbers = {}
|
|
local timerNumbers = {}
|
|
local timerDot = {}
|
|
local active = false
|
|
local hold = false
|
|
local startTime = 0
|
|
local holdTime = 0
|
|
local currentResult = {
|
|
triggers = {
|
|
[30] = {
|
|
reached = false,
|
|
reachTime = 0,
|
|
time = 0
|
|
},
|
|
[60] = {
|
|
reached = false,
|
|
reachTime = 0,
|
|
time = 0
|
|
},
|
|
[100] = {
|
|
reached = false,
|
|
reachTime = 0,
|
|
time = 0
|
|
},
|
|
[200] = {
|
|
reached = false,
|
|
reachTime = 0,
|
|
time = 0
|
|
}
|
|
},
|
|
distance = 0,
|
|
quarter = 0,
|
|
maxSpeed = 0
|
|
}
|
|
local results = {}
|
|
|
|
function buildBody()
|
|
local background = holograms.create(chip():getPos(), chip():localToWorldAngles(Angle(90, 0, 0)), "models/holograms/plane.mdl")
|
|
background:setSize(Vector(METER * 0.04, METER * 0.2, 1))
|
|
background:setMaterial("models/debug/debugwhite")
|
|
background:setColor(backgroundColor)
|
|
background:suppressEngineLighting(true)
|
|
background:setParent(chip())
|
|
|
|
local body = holograms.create(chip():getPos(), chip():localToWorldAngles(Angle(90, 0, 0)), "models/props_junk/PlasticCrate01a.mdl")
|
|
body:setSize(Vector(METER * 0.042, METER * 0.21, 1))
|
|
body:setMaterial("models/debug/debugwhite")
|
|
body:setColor(bodyColor)
|
|
body:setParent(chip())
|
|
end
|
|
|
|
function buildSpeedometer()
|
|
local xOffset = -METER * 0.085
|
|
for i = 0, 2 do
|
|
local number = holograms.create(chip():localToWorld(Vector(0, xOffset + i * METER * 0.03)), chip():localToWorldAngles(Angle(0, -90, 0)), "models/sprops/misc/alphanum/alphanum_0.mdl")
|
|
number:setSize(Vector(METER * 0.03, METER * 0.005, METER * 0.03))
|
|
number:setBodygroup(0, 3)
|
|
number:setMaterial("models/debug/debugwhite")
|
|
number:setColor(digitsColor)
|
|
number:suppressEngineLighting(true)
|
|
number:setParent(chip())
|
|
|
|
table.insert(speedometerNumbers, number)
|
|
end
|
|
end
|
|
|
|
function buildTimer()
|
|
local xOffset = METER * 0.025
|
|
for i = 0, 2 do
|
|
local number = holograms.create(chip():localToWorld(Vector(0, xOffset + i * METER * 0.03)), chip():localToWorldAngles(Angle(0, -90, 0)), "models/sprops/misc/alphanum/alphanum_0.mdl")
|
|
number:setSize(Vector(METER * 0.03, METER * 0.005, METER * 0.03))
|
|
number:setBodygroup(0, 3)
|
|
number:setMaterial("models/debug/debugwhite")
|
|
number:setColor(digitsColor)
|
|
number:suppressEngineLighting(true)
|
|
number:setParent(chip())
|
|
|
|
table.insert(timerNumbers, number)
|
|
|
|
if i == 1 then
|
|
local dot = holograms.create(chip():localToWorld(Vector(0, xOffset + i * METER * 0.03 + METER * 0.012)), chip():localToWorldAngles(Angle(0, -90, 0)), "models/sprops/misc/alphanum/alphanum_prd.mdl")
|
|
dot:setSize(Vector(METER * 0.03, METER * 0.005, METER * 0.03))
|
|
dot:setBodygroup(0, 3)
|
|
dot:setMaterial("models/debug/debugwhite")
|
|
dot:setColor(digitsColor)
|
|
dot:suppressEngineLighting(true)
|
|
dot:setParent(chip())
|
|
|
|
timerDot = dot
|
|
end
|
|
end
|
|
end
|
|
|
|
function setSpeed(value)
|
|
value = math.clamp(value, 0, 999)
|
|
value = math.floor(value)
|
|
|
|
local str = string.rep("0", 3 - #tostring(value)) .. value
|
|
|
|
for k, v in pairs(speedometerNumbers) do
|
|
v:setModel("models/sprops/misc/alphanum/alphanum_" .. string.sub(str, k, k) ..".mdl")
|
|
end
|
|
end
|
|
|
|
function setTime(value)
|
|
value = math.clamp(value, 0, 99.9)
|
|
|
|
local i, f = math.modf(value)
|
|
f = math.floor(f * 10)
|
|
|
|
local str = string.rep("0", 2 - #tostring(i)) .. i .. f
|
|
|
|
for k, v in pairs(timerNumbers) do
|
|
v:setModel("models/sprops/misc/alphanum/alphanum_" .. string.sub(str, k, k) ..".mdl")
|
|
end
|
|
end
|
|
|
|
function hasResult()
|
|
return currentResult.triggers[math.min(unpack(table.getKeys(currentResult.triggers)))].reached
|
|
end
|
|
|
|
function reset()
|
|
active = false
|
|
|
|
if hasResult() then
|
|
chip():emitSound(resetSoundPath)
|
|
end
|
|
|
|
for k, v in pairs(currentResult.triggers) do
|
|
v.reached = false
|
|
v.reachTime = 0
|
|
v.time = 0
|
|
end
|
|
|
|
currentResult.maxSpeed = 0
|
|
currentResult.quarter = 0
|
|
currentResult.distance = 0
|
|
|
|
setTime(0)
|
|
setSpeed(0)
|
|
end
|
|
|
|
function saveResult()
|
|
table.remove(results, lastResultsCount)
|
|
table.insert(results, 1, table.copy(currentResult))
|
|
end
|
|
|
|
function printResult(result)
|
|
local format = "%d-%d km/h - %.02fs"
|
|
local orderedTriggerKeys = table.getKeys(result.triggers)
|
|
|
|
table.sort(orderedTriggerKeys)
|
|
|
|
for _, v in pairs(orderedTriggerKeys) do
|
|
local trigger = result.triggers[v]
|
|
|
|
if trigger.reached then
|
|
print(Color(255, 255, 255), string.format(format, 0, v, trigger.time))
|
|
end
|
|
end
|
|
|
|
if result.triggers[200].reached then
|
|
print(Color(255, 255, 255), string.format(format, 100, 200, math.abs(result.triggers[200].time - result.triggers[100].time)))
|
|
end
|
|
|
|
if (result.quarter > 0) then
|
|
print(Color(255, 255, 255), string.format("Quarter: %.02fs", result.quarter))
|
|
end
|
|
|
|
print(Color(200, 200, 200), string.format("Distance: %.02fm", result.distance))
|
|
print(Color(200, 200, 200), string.format("Max speed: %.02f km/h", result.maxSpeed))
|
|
end
|
|
|
|
hook.add("tick", "_tick", function()
|
|
local weldedTo = chip():isWeldedTo()
|
|
|
|
if isValid(weldedTo) and not weldedTo:isPlayerHolding() then
|
|
local localVelocity = weldedTo:worldToLocal(weldedTo:getVelocity() + weldedTo:getPos())
|
|
local localVelocityLength = localVelocity:getLength()
|
|
local MPH = localVelocityLength * (15 / 352)
|
|
local KPH = MPH * 1.609
|
|
local roundedSpeed = math.floor(KPH)
|
|
|
|
if roundedSpeed < 1 and active then
|
|
if hasResult() then
|
|
saveResult()
|
|
|
|
if printResultBeforeReset then
|
|
print(Color(255, 128, 0), "Result:")
|
|
printResult(currentResult)
|
|
end
|
|
end
|
|
|
|
reset()
|
|
elseif not active and roundedSpeed >= 1 then
|
|
active = true
|
|
startTime = timer.systime()
|
|
end
|
|
|
|
if active then
|
|
currentResult.maxSpeed = math.max(currentResult.maxSpeed, KPH)
|
|
|
|
for k, v in pairs(currentResult.triggers) do
|
|
if KPH >= k and not v.reached then
|
|
local time = timer.systime()
|
|
|
|
v.reached = true
|
|
v.reachTime = time
|
|
v.time = time - startTime
|
|
hold = true
|
|
holdTime = time
|
|
|
|
chip():emitSound(reachSoundPath)
|
|
|
|
setSpeed(k)
|
|
setTime(time - startTime)
|
|
end
|
|
end
|
|
|
|
currentResult.distance = currentResult.distance + KPH * game.getTickInterval() / 3.6
|
|
|
|
if (currentResult.quarter == 0 and currentResult.distance >= 402.33) then
|
|
currentResult.quarter = timer.systime() - startTime
|
|
|
|
chip():emitSound(reachSoundPath)
|
|
end
|
|
|
|
if hold and holdTime + holdDuration <= timer.systime() then
|
|
hold = false
|
|
end
|
|
|
|
if not hold then
|
|
setSpeed(KPH)
|
|
setTime(timer.systime() - startTime)
|
|
end
|
|
end
|
|
end
|
|
end)
|
|
|
|
chip():setColor(Color(0, 0, 0, 0))
|
|
|
|
buildBody()
|
|
buildSpeedometer()
|
|
buildTimer()
|