Added extra checks to unit spawn events
Fixed stupid errors with mist.getNextUnitId and mist.getNextGroupId

ground vehicles spawns via mist.dynAdd will now have playerCanDrive set
to true if value not present.

Fixed typo in mist.getUnitsInPolyGon

added mist.utils.getQFE. Values returned have some error, need to figure
it out, but added it for testing purposes.

Added 2 extra values to mist.getRandPointInCircle to specify a min and
max angle in degrees. Funtion currently assumes both are positive
numbers. Need to add extra checks to it.

added mist.getRandomPointInPoly. Returns a random vec2 coordinate inside
a polyzone

Added checks to getLeadPos related functions to ensure the lead unit is
returned if it exists.
This commit is contained in:
mrSkortch 2017-02-08 04:17:00 -07:00
parent fa35a99872
commit ef56349efc

136
mist.lua
View File

@ -34,8 +34,8 @@ mist = {}
-- don't change these -- don't change these
mist.majorVersion = 4 mist.majorVersion = 4
mist.minorVersion = 3 mist.minorVersion = 4
mist.build = 74 mist.build = 76
-- forward declaration of log shorthand -- forward declaration of log shorthand
local log local log
@ -977,11 +977,15 @@ do -- the main scope
------- -------
if Object.getCategory(event.initiator) == 1 and not Unit.getPlayerName(event.initiator) then -- simple player check, will need to later check to see if unit was spawned with a player in a flight if Object.getCategory(event.initiator) == 1 and not Unit.getPlayerName(event.initiator) then -- simple player check, will need to later check to see if unit was spawned with a player in a flight
dbLog:info('Object is a Unit') dbLog:info('Object is a Unit')
dbLog:info(Unit.getGroup(event.initiator):getName()) if Unit.getGroup(event.initiator) then
if not tempSpawnedGroups[Unit.getGroup(event.initiator):getName()] then dbLog:info(Unit.getGroup(event.initiator):getName())
dbLog:info('added') if not tempSpawnedGroups[Unit.getGroup(event.initiator):getName()] then
tempSpawnedGroups[Unit.getGroup(event.initiator):getName()] = 'group' dbLog:info('added')
tempSpawnGroupsCounter = tempSpawnGroupsCounter + 1 tempSpawnedGroups[Unit.getGroup(event.initiator):getName()] = 'group'
tempSpawnGroupsCounter = tempSpawnGroupsCounter + 1
end
else
log:error('Group not accessible by unit in event handler. This is a DCS bug')
end end
elseif Object.getCategory(event.initiator) == 3 or Object.getCategory(event.initiator) == 6 then elseif Object.getCategory(event.initiator) == 3 or Object.getCategory(event.initiator) == 6 then
dbLog:info('Object is Static') dbLog:info('Object is Static')
@ -1187,8 +1191,8 @@ do -- the main scope
-- @treturn number next unit id. -- @treturn number next unit id.
function mist.getNextUnitId() function mist.getNextUnitId()
mist.nextUnitId = mist.nextUnitId + 1 mist.nextUnitId = mist.nextUnitId + 1
if mist.nextUnitId > 6900 then if mist.nextUnitId > 6900 and mist.nextUnitId < 30000 then
mist.nextUnitId = 14000 mist.nextUnitId = 30000
end end
return mist.nextUnitId return mist.nextUnitId
end end
@ -1197,8 +1201,8 @@ do -- the main scope
-- @treturn number next group id. -- @treturn number next group id.
function mist.getNextGroupId() function mist.getNextGroupId()
mist.nextGroupId = mist.nextGroupId + 1 mist.nextGroupId = mist.nextGroupId + 1
if mist.nextGroupId > 6900 then if mist.nextGroupId > 6900 and mist.nextGroupId < 30000 then
mist.nextGroupId = 14000 mist.nextGroupId = 30000
end end
return mist.nextGroupId return mist.nextGroupId
end end
@ -1418,24 +1422,6 @@ do -- the main scope
newGroup.units[unitIndex].skill = 'Random' newGroup.units[unitIndex].skill = 'Random'
end end
if not unitData.alt then
if newCat == 'AIRPLANE' then
newGroup.units[unitIndex].alt = 2000
newGroup.units[unitIndex].alt_type = 'RADIO'
newGroup.units[unitIndex].speed = 150
elseif newCat == 'HELICOPTER' then
newGroup.units[unitIndex].alt = 500
newGroup.units[unitIndex].alt_type = 'RADIO'
newGroup.units[unitIndex].speed = 60
else
--[[log:info('check height')
newGroup.units[unitIndex].alt = land.getHeight({x = newGroup.units[unitIndex].x, y = newGroup.units[unitIndex].y})
newGroup.units[unitIndex].alt_type = 'BARO']]
end
end
if newCat == 'AIRPLANE' or newCat == 'HELICOPTER' then if newCat == 'AIRPLANE' or newCat == 'HELICOPTER' then
if newGroup.units[unitIndex].alt_type and newGroup.units[unitIndex].alt_type ~= 'BARO' or not newGroup.units[unitIndex].alt_type then if newGroup.units[unitIndex].alt_type and newGroup.units[unitIndex].alt_type ~= 'BARO' or not newGroup.units[unitIndex].alt_type then
newGroup.units[unitIndex].alt_type = 'RADIO' newGroup.units[unitIndex].alt_type = 'RADIO'
@ -1450,6 +1436,23 @@ do -- the main scope
if not unitData.payload then if not unitData.payload then
newGroup.units[unitIndex].payload = mist.getPayload(originalName) newGroup.units[unitIndex].payload = mist.getPayload(originalName)
end end
if not unitData.alt then
if newCat == 'AIRPLANE' then
newGroup.units[unitIndex].alt = 2000
newGroup.units[unitIndex].alt_type = 'RADIO'
newGroup.units[unitIndex].speed = 150
elseif newCat == 'HELICOPTER' then
newGroup.units[unitIndex].alt = 500
newGroup.units[unitIndex].alt_type = 'RADIO'
newGroup.units[unitIndex].speed = 60
end
end
elseif newCat == 'GROUND_UNIT' then
if not unitData.playerCanDrive then
unitData.playerCanDrive = true
end
end end
mistAddedObjects[#mistAddedObjects + 1] = mist.utils.deepCopy(newGroup.units[unitIndex]) mistAddedObjects[#mistAddedObjects + 1] = mist.utils.deepCopy(newGroup.units[unitIndex])
end end
@ -2517,7 +2520,7 @@ function mist.getUnitsInPolygon(unit_names, polyZone, max_alt)
local inZoneUnits = {} local inZoneUnits = {}
for i =1, #units do for i =1, #units do
if units[i]:isActive() and mist.pointInPolygon(units[i]:getPosition().p, polyZone, max_alt) then if units[i]:isActive() and mist.pointInPolygon(units[i]:getPosition().p, polyZone, max_alt) then
inZoneUnits[inZoneUnits + 1] = units[i] inZoneUnits[#inZoneUnits + 1] = units[i]
end end
end end
@ -3698,6 +3701,16 @@ do -- mist.util scope
return kmph/3.6 return kmph/3.6
end end
function mist.utils.getQFE(point, inchHg)
local h = land.getHeight(mist.utils.makeVec2(point))/0.3048 -- convert to feet
local qnh = env.mission.weather.qnh
if inchHg then
return (qnh - (h/30)) * 0.0295299830714
else
return qnh - (h/30)
end
end
--- Converts a Vec3 to a Vec2. --- Converts a Vec3 to a Vec2.
-- @tparam Vec3 vec the 3D vector -- @tparam Vec3 vec the 3D vector
-- @return vector converted to Vec2 -- @return vector converted to Vec2
@ -4017,7 +4030,7 @@ function mist.utils.serialize(name, value, level)
local function serializeToTbl(name, value, level) local function serializeToTbl(name, value, level)
local var_str_tbl = {} local var_str_tbl = {}
if level == nil then level = "" end if level == nil then level = "" end
if level ~= "" then level = level.." " end if level ~= "" then level = level.." " end
table.insert(var_str_tbl, level .. name .. " = ") table.insert(var_str_tbl, level .. name .. " = ")
@ -4187,7 +4200,7 @@ function mist.utils.tableShow(tbl, loc, indent, tableshow_tbls) --based on seria
else else
tableshow_tbls[val] = loc .. '[' .. mist.utils.basicSerialize(ind) .. ']' tableshow_tbls[val] = loc .. '[' .. mist.utils.basicSerialize(ind) .. ']'
tbl_str[#tbl_str + 1] = tostring(val) .. ' ' tbl_str[#tbl_str + 1] = tostring(val) .. ' '
tbl_str[#tbl_str + 1] = mist.utils.tableShow(val, loc .. '[' .. mist.utils.basicSerialize(ind).. ']', indent .. ' ', tableshow_tbls) tbl_str[#tbl_str + 1] = mist.utils.tableShow(val, loc .. '[' .. mist.utils.basicSerialize(ind).. ']', indent .. ' ', tableshow_tbls)
tbl_str[#tbl_str + 1] = ',\n' tbl_str[#tbl_str + 1] = ',\n'
end end
elseif type(val) == 'function' then elseif type(val) == 'function' then
@ -6328,8 +6341,13 @@ do -- group tasks scope
end end
-- need to return a Vec3 or Vec2? -- need to return a Vec3 or Vec2?
function mist.getRandPointInCircle(point, radius, innerRadius) function mist.getRandPointInCircle(point, radius, innerRadius, maxA, minA)
local theta = 2*math.pi*math.random() local theta = 2*math.pi*math.random()
if maxA and not minA then
theta = math.rad(math.random(0, maxA))
elseif maxA and minA then
theta = math.rad(math.random(minA, maxA))
end
local rad = math.random() + math.random() local rad = math.random() + math.random()
if rad > 1 then if rad > 1 then
rad = 2 - rad rad = 2 - rad
@ -6341,7 +6359,7 @@ do -- group tasks scope
else else
radMult = radius*rad radMult = radius*rad
end end
-- radius = (maxR - minR)*math.sqrt(math.random()) + minR
if not point.z then --might as well work with vec2/3 if not point.z then --might as well work with vec2/3
point.z = point.y point.z = point.y
end end
@ -6362,6 +6380,32 @@ do -- group tasks scope
return false return false
end end
function mist.getRandomPointInPoly(zone)
local avg = mist.getAvgPoint(zone)
local radius = 0
local minR = math.huge
local newCoord = {}
for i = 1, #zone do
if mist.utils.get2DDist(avg, zone[i]) > radius then
radius = mist.utils.get2DDist(avg, zone[i])
end
if mist.utils.get2DDist(avg, zone[i]) < minR then
minR = mist.utils.get2DDist(avg, zone[i])
end
end
local lSpawnPos = {}
for j = 1, 100 do
newCoord = mist.getRandPointInCircle(avg, radius)
if mist.pointInPolygon(newCoord, zone) then
break
end
if j == 100 then
newCoord = mist.getRandPointInCircle(avg, 50000)
end
end
return newCoord
end
function mist.groupToRandomPoint(vars) function mist.groupToRandomPoint(vars)
local group = vars.group --Required local group = vars.group --Required
local point = vars.point --required local point = vars.point --required
@ -6394,20 +6438,20 @@ do -- group tasks scope
local offset = {} local offset = {}
local posStart = mist.getLeadPos(group) local posStart = mist.getLeadPos(group)
if posStart then
offset.x = mist.utils.round(math.sin(heading - (math.pi/2)) * 50 + rndCoord.x, 3) offset.x = mist.utils.round(math.sin(heading - (math.pi/2)) * 50 + rndCoord.x, 3)
offset.z = mist.utils.round(math.cos(heading + (math.pi/2)) * 50 + rndCoord.y, 3) offset.z = mist.utils.round(math.cos(heading + (math.pi/2)) * 50 + rndCoord.y, 3)
path[#path + 1] = mist.ground.buildWP(posStart, form, speed) path[#path + 1] = mist.ground.buildWP(posStart, form, speed)
if useRoads == true and ((point.x - posStart.x)^2 + (point.z - posStart.z)^2)^0.5 > radius * 1.3 then if useRoads == true and ((point.x - posStart.x)^2 + (point.z - posStart.z)^2)^0.5 > radius * 1.3 then
path[#path + 1] = mist.ground.buildWP({x = posStart.x + 11, z = posStart.z + 11}, 'off_road', speed) path[#path + 1] = mist.ground.buildWP({x = posStart.x + 11, z = posStart.z + 11}, 'off_road', speed)
path[#path + 1] = mist.ground.buildWP(posStart, 'on_road', speed) path[#path + 1] = mist.ground.buildWP(posStart, 'on_road', speed)
path[#path + 1] = mist.ground.buildWP(offset, 'on_road', speed) path[#path + 1] = mist.ground.buildWP(offset, 'on_road', speed)
else else
path[#path + 1] = mist.ground.buildWP({x = posStart.x + 25, z = posStart.z + 25}, form, speed) path[#path + 1] = mist.ground.buildWP({x = posStart.x + 25, z = posStart.z + 25}, form, speed)
end
end end
path[#path + 1] = mist.ground.buildWP(offset, form, speed) path[#path + 1] = mist.ground.buildWP(offset, form, speed)
path[#path + 1] = mist.ground.buildWP(rndCoord, form, speed) path[#path + 1] = mist.ground.buildWP(rndCoord, form, speed)
@ -6542,7 +6586,7 @@ do -- group tasks scope
local units = group:getUnits() local units = group:getUnits()
local leader = units[1] local leader = units[1]
if not Unit.isExist(leader) then -- SHOULD be good, but if there is a bug, this code future-proofs it then. if Unit.getLife(leader) == 0 or not Unit.isExist(leader) then -- SHOULD be good, but if there is a bug, this code future-proofs it then.
local lowestInd = math.huge local lowestInd = math.huge
for ind, unit in pairs(units) do for ind, unit in pairs(units) do
if Unit.isExist(unit) and ind < lowestInd then if Unit.isExist(unit) and ind < lowestInd then