-
Notifications
You must be signed in to change notification settings - Fork 22
/
unit.lua
150 lines (129 loc) · 4.59 KB
/
unit.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
--
-- RPC unit actions
-- https://wiki.hoggitworld.com/view/DCS_Class_Unit
--
GRPC.methods.getRadar = function(params)
local unit = Unit.getByName(params.name)
if unit == nil then
return GRPC.errorNotFound("Could not find unit with name '" .. params.name .. "'")
end
local active, object = unit:getRadar()
if object == nil then
return GRPC.success({
active = active
})
end
local category = object:getCategory()
local grpcTable = {}
if(category == Object.Category.UNIT) then
grpcTable["unit"] = GRPC.exporters.unit(object)
elseif(category == Object.Category.WEAPON) then
grpcTable["weapon"] = GRPC.exporters.weapon(object)
elseif(category == Object.Category.STATIC) then
grpcTable["static"] = GRPC.exporters.static(object)
elseif(category == Object.Category.BASE) then
grpcTable["airbase"] = GRPC.exporters.airbase(object)
elseif(category == Object.Category.SCENERY) then
grpcTable["scenery"] = GRPC.exporters.scenery(object)
elseif(category == Object.Category.Cargo) then
grpcTable["cargo"] = GRPC.exporters.cargo(object)
else
GRPC.logWarning(
"Could not determine object category of object with ID: " .. object:getID()
.. ", Category: " .. category
)
grpcTable["object"] = GRPC.exporters.object(object)
end
return GRPC.success({
active = active,
target = grpcTable
})
end
GRPC.methods.getUnitPosition = function(params)
-- https://wiki.hoggitworld.com/view/DCS_func_getByName
local unit = Unit.getByName(params.name)
if unit == nil then
return GRPC.errorNotFound("unit does not exist")
end
return GRPC.success({
-- https://wiki.hoggitworld.com/view/DCS_func_getPoint
position = GRPC.exporters.position(unit:getPoint()),
})
end
GRPC.methods.getUnitTransform = function(params)
-- https://wiki.hoggitworld.com/view/DCS_func_getByName
local unit = Unit.getByName(params.name)
if unit == nil then
return GRPC.errorNotFound("unit does not exist")
end
-- https://wiki.hoggitworld.com/view/DCS_func_getPosition
local position = unit:getPosition()
local coords = GRPC.exporters.position(position.p)
local coordsNorth = coord.LLtoLO(coords.lat + 1, coords.lon)
-- Response does not exactly match what the gRPC server will return since the gRPC response
-- will be calculated based on the information returned here.
return GRPC.success({
position = coords,
u = position.p.z,
v = position.p.x,
positionNorth = GRPC.exporters.vector(coordsNorth),
orientation = {
forward = GRPC.exporters.vector(position.x),
right = GRPC.exporters.vector(position.z),
up = GRPC.exporters.vector(position.y),
},
velocity = GRPC.exporters.vector(unit:getVelocity()),
time = timer.getTime(),
})
end
GRPC.methods.getUnitPlayerName = function(params)
-- https://wiki.hoggitworld.com/view/DCS_func_getByName
local unit = Unit.getByName(params.name)
if unit == nil then
return GRPC.errorNotFound("unit does not exist")
end
return GRPC.success({
-- https://wiki.hoggitworld.com/view/DCS_func_getPlayerName
playerName = unit:getPlayerName(),
})
end
GRPC.methods.getUnitDescriptor = function(params)
local unit = Unit.getByName(params.name)
if unit == nil then
return GRPC.errorNotFound("unit does not exist")
end
local desc = unit:getDesc()
local attrs = {}
for i in pairs(desc.attributes) do
table.insert(attrs, i)
end
return GRPC.success({
attributes = attrs
})
end
GRPC.methods.setEmission = function(params)
local unit = Unit.getByName(params.name)
if unit == nil then
return GRPC.errorNotFound("unit does not exist")
end unit:enableEmission(params.emitting)
return GRPC.success(nil)
end
GRPC.methods.getUnit = function(params)
local unit = Unit.getByName(params.name)
if unit == nil then
return GRPC.errorNotFound("unit `" .. tostring(params.name) .. "` does not exist")
end
return GRPC.success({unit = GRPC.exporters.unit(unit)})
end
-- This method is only used by the unit stream. It returns the subset of `getUnit` that can
-- change (like position, velocity, ...). It is not exposed to the gRPC service for now.
GRPC.methods.getUnitUpdate = function(params)
local unit = Unit.getByName(params.name)
if unit == nil then
return GRPC.errorNotFound("unit `" .. tostring(params.name) .. "` does not exist")
end
return GRPC.success({
position = GRPC.exporters.position(unit:getPoint()),
velocity = GRPC.exporters.vector(unit:getVelocity()),
})
end