-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresearchVE.lua
313 lines (263 loc) · 6.78 KB
/
researchVE.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
-- This Source Code Form is subject to the terms of the bCDDL, v. 1.1.
-- If a copy of the bCDDL was not distributed with this
-- file, You can obtain one at http://beamng.com/bCDDL-1.1.txt
local M = {}
local logTag = 'ResearchVE'
local socket = require('libs/luasocket/socket.socket')
local rcom = require('utils/researchCommunication')
local sensorHandlers = {}
local skt = nil
local clients = {}
local host = nil
local port = nil
local conSleep = 60
local _log = log
local function log(level, message)
_log(level, logTag, message)
end
local function checkMessage()
local message, err = rcom.readMessage(clients)
if err ~= nil then
skt = nil
clients = {}
conSleep = 60
return
end
if message ~= nil then
local msgType = message['type']
if msgType ~= nil then
msgType = 'handle' .. msgType
local handler = M[msgType]
if handler ~= nil then
handler(message)
end
end
end
end
local function connect()
log('I', 'Trying to connect to: ' .. host .. ':' .. tostring(port))
skt = socket.connect(host, port)
if skt ~= nil then
log('I', 'Connected!')
table.insert(clients, skt)
else
log('I', 'Could not connect...')
end
end
M.startConnecting = function(targetHost, targetPort)
host = targetHost
port = targetPort
end
M.onDebugDraw = function()
if port ~= nil then
if skt == nil then
if conSleep <= 0 then
conSleep = 60
connect()
else
conSleep = conSleep - 1
end
end
end
if skt == nil then
return
end
checkMessage()
end
local function getVehicleState()
local vehicleState = {
pos = obj:getPosition(),
dir = obj:getDirectionVector(),
up = obj:getDirectionVectorUp(),
vel = obj:getVelocity(),
front = obj:getFrontPosition()
}
vehicleState['pos'] = {
vehicleState['pos'].x,
vehicleState['pos'].y,
vehicleState['pos'].z
}
vehicleState['dir'] = {
vehicleState['dir'].x,
vehicleState['dir'].y,
vehicleState['dir'].z
}
vehicleState['up'] = {
vehicleState['up'].x,
vehicleState['up'].y,
vehicleState['up'].z
}
vehicleState['vel'] = {
vehicleState['vel'].x,
vehicleState['vel'].y,
vehicleState['vel'].z
}
vehicleState['front'] = {
vehicleState['front'].x,
vehicleState['front'].y,
vehicleState['front'].z
}
return vehicleState
end
-- Handlers
local function submitInput(inputs, key)
local val = inputs[key]
if val ~= nil then
input.event(key, val, 1)
end
end
M.handleControl = function(msg)
submitInput(msg, 'throttle')
submitInput(msg, 'steering')
submitInput(msg, 'brake')
submitInput(msg, 'parkingbrake')
submitInput(msg, 'clutch')
local gear = msg['gear']
if gear ~= nil then
drivetrain.shiftToGear(gear)
end
rcom.sendACK(skt, 'Controlled')
end
M.handleSetShiftMode = function(msg)
drivetrain.setShifterMode(msg['mode'])
rcom.sendACK(skt, 'ShiftModeSet')
end
sensorHandlers.GForces = function(msg)
local resp = {type='GForces'}
resp['gx'] = sensors.gx
resp['gx2'] = sensors.gx2
resp['gxSmoothMax'] = sensors.gxSmoothMax
resp['gy'] = sensors.gy
resp['gy2'] = sensors.gy2
resp['gz'] = sensors.gz
resp['gz2'] = sensors.gz2
return resp
end
sensorHandlers.Electrics = function(msg)
local resp = {type = 'Electrics'}
resp['values'] = electrics.values
resp['values']['running'] = controller.mainController.engineInfo[18]
return resp
end
sensorHandlers.Damage = function(msg)
local resp = {type = 'Damage'}
resp['damageExt'] = beamstate.damageExt
resp['deformGroupDamage'] = beamstate.deformGroupDamage
resp['lowpressure'] = beamstate.lowpressure
resp['damage'] = beamstate.damage
return resp
end
local function getSensorData(request)
local response, sensor_type, handler
sensor_type = request['type']
handler = sensorHandlers[sensor_type]
if handler ~= nil then
response = handler(request)
return response
end
return nil
end
M.handleGetPartConfig = function(msg)
local cfg = partmgmt.getConfig()
local resp = {type = 'PartConfig', config = cfg}
rcom.sendMessage(skt, resp)
end
M.handleGetPartOptions = function(msg)
local options = v.slotMap
local resp = {type = 'PartOptions', options = options}
rcom.sendMessage(skt, resp)
end
M.handleSetPartConfig = function(msg)
local cfg = msg['config']
skt = nil
partmgmt.setConfig(cfg)
end
M.handleSensorRequest = function(msg)
local request, response, data
response = {}
request = msg['sensors']
for k, v in pairs(request) do
data = getSensorData(v)
if data == nil then
log('E', 'Could not get data for sensor: ' .. k)
end
response[k] = data
end
response = {type = 'SensorData', data = response}
response['state'] = getVehicleState()
rcom.sendMessage(skt, response)
end
M.handleSetColor = function(msg)
local cmd = 'Point4F(' .. msg['r'] .. ', ' .. msg['g'] .. ', ' .. msg['b'] .. ', ' .. msg['a'] .. ')'
cmd = 'be:getObjectByID(' .. obj:getID() .. '):setColor(' .. cmd .. ')'
obj:queueGameEngineLua(cmd)
rcom.sendACK(skt, 'ColorSet')
end
M.handleSetAiMode = function(msg)
ai.setMode(msg['mode'])
rcom.sendACK(skt, 'AiModeSet')
end
M.handleSetAiLine = function(msg)
local nodes = msg['line']
local fauxPath = {}
local cling = msg['cling']
local z = 0
local speedList = {}
for idx, n in ipairs(nodes) do
local pos = vec3(n['pos'][1], n['pos'][2], 10000)
if cling then
z = obj:getSurfaceHeightBelow(pos:toFloat3())
else
z = n['pos'][3]
end
pos.z = z
local fauxNode = {
pos = pos,
radius = 0,
radiusOrig = 0,
}
table.insert(speedList, n['speed'])
table.insert(fauxPath, fauxNode)
end
local arg = {
script = fauxPath,
wpSpeeds = speedList
}
ai.driveUsingPath(arg)
rcom.sendACK(skt, 'AiLineSet')
end
M.handleSetAiSpeed = function(msg)
ai.setSpeedMode(msg['mode'])
ai.setSpeed(msg['speed'])
rcom.sendACK(skt, 'AiSpeedSet')
end
M.handleSetAiTarget = function(msg)
local targetName = msg['target']
obj:queueGameEngineLua('scenetree.findObjectById(' .. obj:getID() .. '):queueLuaCommand("ai.setTargetObjectID(" .. scenetree.findObject(\'' .. targetName .. '\'):getID() .. ")")')
rcom.sendACK(skt, 'AiTargetSet')
end
M.handleSetAiWaypoint = function(msg)
local targetName = msg['target']
ai.setTarget(targetName)
rcom.sendACK(skt, 'AiWaypointSet')
end
M.handleSetAiSpan = function(msg)
if msg['span'] then
ai.spanMap(0)
else
ai.setMode('disabled')
end
rcom.sendACK(skt, 'AiSpanSet')
end
M.handleSetDriveInLane = function(msg)
ai.driveInLane = msg['lane']
rcom.sendACK(skt, 'AiDriveInLaneSet')
end
M.handleUpdateVehicle = function(msg)
local response = {type = 'VehicleUpdate'}
local vehicleState = getVehicleState()
response['state'] = vehicleState
rcom.sendMessage(skt, response)
return true
end
return M