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

104
mist.lua
View File

@ -34,8 +34,8 @@ mist = {}
-- don't change these
mist.majorVersion = 4
mist.minorVersion = 3
mist.build = 74
mist.minorVersion = 4
mist.build = 76
-- forward declaration of log shorthand
local log
@ -977,12 +977,16 @@ 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
dbLog:info('Object is a Unit')
if Unit.getGroup(event.initiator) then
dbLog:info(Unit.getGroup(event.initiator):getName())
if not tempSpawnedGroups[Unit.getGroup(event.initiator):getName()] then
dbLog:info('added')
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
elseif Object.getCategory(event.initiator) == 3 or Object.getCategory(event.initiator) == 6 then
dbLog:info('Object is Static')
tempSpawnedGroups[StaticObject.getName(event.initiator)] = 'static'
@ -1187,8 +1191,8 @@ do -- the main scope
-- @treturn number next unit id.
function mist.getNextUnitId()
mist.nextUnitId = mist.nextUnitId + 1
if mist.nextUnitId > 6900 then
mist.nextUnitId = 14000
if mist.nextUnitId > 6900 and mist.nextUnitId < 30000 then
mist.nextUnitId = 30000
end
return mist.nextUnitId
end
@ -1197,8 +1201,8 @@ do -- the main scope
-- @treturn number next group id.
function mist.getNextGroupId()
mist.nextGroupId = mist.nextGroupId + 1
if mist.nextGroupId > 6900 then
mist.nextGroupId = 14000
if mist.nextGroupId > 6900 and mist.nextGroupId < 30000 then
mist.nextGroupId = 30000
end
return mist.nextGroupId
end
@ -1418,24 +1422,6 @@ do -- the main scope
newGroup.units[unitIndex].skill = 'Random'
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 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'
@ -1450,6 +1436,23 @@ do -- the main scope
if not unitData.payload then
newGroup.units[unitIndex].payload = mist.getPayload(originalName)
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
mistAddedObjects[#mistAddedObjects + 1] = mist.utils.deepCopy(newGroup.units[unitIndex])
end
@ -2517,7 +2520,7 @@ function mist.getUnitsInPolygon(unit_names, polyZone, max_alt)
local inZoneUnits = {}
for i =1, #units do
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
@ -3698,6 +3701,16 @@ do -- mist.util scope
return kmph/3.6
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.
-- @tparam Vec3 vec the 3D vector
-- @return vector converted to Vec2
@ -6328,8 +6341,13 @@ do -- group tasks scope
end
-- 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()
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()
if rad > 1 then
rad = 2 - rad
@ -6341,7 +6359,7 @@ do -- group tasks scope
else
radMult = radius*rad
end
-- radius = (maxR - minR)*math.sqrt(math.random()) + minR
if not point.z then --might as well work with vec2/3
point.z = point.y
end
@ -6362,6 +6380,32 @@ do -- group tasks scope
return false
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)
local group = vars.group --Required
local point = vars.point --required
@ -6394,7 +6438,7 @@ do -- group tasks scope
local offset = {}
local posStart = mist.getLeadPos(group)
if posStart then
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)
path[#path + 1] = mist.ground.buildWP(posStart, form, speed)
@ -6407,7 +6451,7 @@ do -- group tasks scope
else
path[#path + 1] = mist.ground.buildWP({x = posStart.x + 25, z = posStart.z + 25}, form, speed)
end
end
path[#path + 1] = mist.ground.buildWP(offset, form, speed)
path[#path + 1] = mist.ground.buildWP(rndCoord, form, speed)
@ -6542,7 +6586,7 @@ do -- group tasks scope
local units = group:getUnits()
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
for ind, unit in pairs(units) do
if Unit.isExist(unit) and ind < lowestInd then