-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathodometer.lua
More file actions
140 lines (115 loc) · 4.66 KB
/
Copy pathodometer.lua
File metadata and controls
140 lines (115 loc) · 4.66 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
-- Author: SentyFunBall
-- GitHub: https://github.com/SentyFunBall
-- Workshop:
--Code by STCorp. Do not reuse.--
--- Developed using LifeBoatAPI - Stormworks Lua plugin for VSCode - https://code.visualstudio.com/download (search "Stormworks Lua with LifeboatAPI" extension)
--- If you have any issues, please report them here: https://github.com/nameouschangey/STORMWORKS_VSCodeExtension/issues - by Nameous Changey
--[====[ HOTKEYS ]====]
-- Press F6 to simulate this file
-- Press F7 to build the project, copy the output from /_build/out/ into the game to use
-- Remember to set your Author name etc. in the settings: CTRL+COMMA
--[====[ EDITABLE SIMULATOR CONFIG - *automatically removed from the F7 build output ]====]
---@section __LB_SIMULATOR_ONLY__
do
---@type Simulator -- Set properties and screen sizes here - will run once when the script is loaded
simulator = simulator
simulator:setScreen(1, "3x3")
simulator:setProperty("Units", true)
-- Runs every tick just before onTick; allows you to simulate the inputs changing
---@param simulator Simulator Use simulator:<function>() to set inputs etc.
---@param ticks number Number of ticks since simulator started
function onLBSimulatorTick(simulator, ticks)
-- touchscreen defaults
simulator:setInputBool(1, simulator:getIsToggled(1))
simulator:setInputBool(2, not simulator:getIsToggled(1))
simulator:setInputNumber(1, simulator:getSlider(1) * 100)
simulator:setInputNumber(2, (-simulator:getSlider(2) * 50) + 50)
end
end
---@endsection
--[====[ IN-GAME CODE ]====]
-- try require("Folder.Filename") to include code from another file in this, so you can store code in libraries
-- the "LifeBoatAPI" is included by default in /_build/libs/ - you can use require("LifeBoatAPI") to get this, and use all the LifeBoatAPI.<functions>!
local maxBattery = 256000 -- ENSURE SAME AS IN BATTERY SCRIPT
local ticks = 0
local eng = false
local odometer = 0
local speeds = {}
local fuelStart = 0
local avgSpeed = 0
local distance = 0
local fuelEcon = 0
local econSamples = {}
local isEv = property.getBool("EV Mode (Do not change)")
function onTick()
local pulse = input.getBool(1) and not eng
eng = input.getBool(1)
local speed = input.getNumber(1)
local fuel = input.getNumber(2)
local Unit = input.getBool(32)
if pulse then --get the starting fuel when the car turns on
fuelStart = fuel -- or battery for ev
distance = 0
fuelEcon = 0
avgSpeed = 0
speeds = {}
econSamples = {}
end
if eng and ticks % 30 == 0 and speed > 0.2 then
local deltaDist = speed / 2 -- m per 0.5s
odometer = odometer + deltaDist
distance = distance + deltaDist
speeds[#speeds + 1] = speed
if #speeds > 240 then table.remove(speeds, 1) end -- 2min window
end
avgSpeed = 0
for i = 1, #speeds do
avgSpeed = (avgSpeed + speeds[i])
end
avgSpeed = #speeds > 0 and (avgSpeed / #speeds) or 0
fuelUsed = (fuelStart - fuel) * (isEv and maxBattery or 1)
if isEv then
if distance > 1 then
-- Swatts per km/mi
local econ = (fuelUsed / distance) * (Unit and 1609.34 or 1000)
-- Rolling average for smoothness
econSamples[#econSamples + 1] = econ
if #econSamples > 240 then table.remove(econSamples, 1) end
local sum = 0
for i = 1, #econSamples do sum = sum + econSamples[i] end
fuelEcon = sum / #econSamples
else
fuelEcon = 0
end
else
if Unit then --mpg
fuelEcon = (distance / 1609.34) / (fuelUsed / 3.785)--miles / gallon
else --l/100km
fuelEcon = distance/1000 / (fuelUsed*100) --idk blame nameous
end
end
if fuelEcon ~= fuelEcon then fuelEcon = 0 end
ticks = ticks + 1
if Unit then --miles
output.setNumber(1, odometer / 1609)
output.setNumber(2, fuelEcon) -- swatts/mi
output.setNumber(3, avgSpeed * 2.23)
if isEv then
output.setNumber(4, fuelUsed * 100 / maxBattery) --battery used in %
else
output.setNumber(4, fuelUsed/3.78)
end
output.setNumber(5, distance / 1609)
else --km
output.setNumber(1, odometer / 1000)
output.setNumber(3, avgSpeed * 3.6)
if isEv then
output.setNumber(4, fuelUsed * 100 / maxBattery) --battery used in %
output.setNumber(2, fuelEcon * 100) -- swatts/100km
else
output.setNumber(4, fuelUsed)
output.setNumber(2, fuelEcon)
end
output.setNumber(5, distance / 1000)
end
end