diff --git a/mist.lua b/mist.lua index ce94a48..204bf74 100644 --- a/mist.lua +++ b/mist.lua @@ -15,7 +15,7 @@ mist = {} -- don't change these mist.majorVersion = 4 mist.minorVersion = 0 -mist.build = 57 +mist.build = 60 -------------------------------------------------------------------------------------------------------------- -- the main area @@ -241,7 +241,7 @@ do end end end - + --mist.debug.writeData(mist.utils.serialize,{'msg', newTable}, timer.getAbsTime() ..'Group.lua') newTable['timeAdded'] = timer.getAbsTime() -- only on the dynGroupsAdded table. For other reference, see start time --mist.debug.dumpDBs() --end @@ -254,6 +254,7 @@ do local function checkSpawnedEvents() if #tempSpawnedUnits > 0 then local groupsToAdd = {} + local added = false local ltemp = tempSpawnedUnits local ltable = table @@ -265,7 +266,7 @@ do local spawnedObj = ltemp[x] if spawnedObj and spawnedObj:isExist() then local found = false - for index, name in pairs(groupsToAdd) do + for name, val in pairs(groupsToAdd) do if spawnedObj:getCategory() == 1 then -- normal groups if mist.stringMatch(spawnedObj:getGroup():getName(), name) == true then found = true @@ -280,26 +281,37 @@ do end -- for some reason cargo objects are returning as category == 6. if found == false then + added = true if spawnedObj:getCategory() == 1 then -- normal groups - groupsToAdd[#groupsToAdd + 1] = spawnedObj:getGroup():getName() + groupsToAdd[spawnedObj:getGroup():getName()] = true elseif spawnedObj:getCategory() == 3 or spawnedObj:getCategory() == 6 then -- static objects - groupsToAdd[#groupsToAdd + 1] = spawnedObj:getName() + groupsToAdd[spawnedObj:getName()] = true end + end end - table.remove(ltemp, x) if x%updatesPerRun == 0 then coroutine.yield() end - end - - - if #groupsToAdd > 0 then - for groupId, groupName in pairs(groupsToAdd) do - if not mist.DBs.groupsByName[groupName] or mist.DBs.groupsByName[groupName] and mist.DBs.groupsByName[groupName].startTime + 10 < timer.getAbsTime() then + + if added == true then + for groupName, val in pairs(groupsToAdd) do + local dataChanged = false + if mist.DBs.groupsByName[groupName] then + for _index, data in pairs(mist.DBs.groupsByName[groupName]) do + if data.unitName ~= spawnedObj:getName() and data.unitId ~= spawnedObj:getID() and data.type ~= spawnedObj:getTypeName() then + dataChanged = true + break + end + end + if dataChanged == false then + groupsToAdd[groupName] = false + end + end + if groupsToAdd[groupName] == true or not mist.DBs.groupsByName[groupName] then writeGroups[#writeGroups + 1] = dbUpdate(groupName) end end @@ -309,6 +321,7 @@ do local function updateDBTables() + local i = 0 for index, newTable in pairs(writeGroups) do i = i + 1 @@ -548,7 +561,7 @@ do if newObj.clone or not newObj.name then mistDynAddIndex = mistDynAddIndex + 1 - newObj.name = (newCountry .. ' static ' .. mistDynAddIndex) + newObj.name = (country.name[newCountry] .. ' static ' .. mistDynAddIndex) end if not newObj.dead then @@ -591,6 +604,7 @@ do -- validate data for countryId, countryName in pairs(country.name) do if type(cntry) == 'string' then + cntry = cntry:gsub("%s+", "_") if tostring(countryName) == string.upper(cntry) then newCountry = countryName end @@ -649,7 +663,7 @@ do end if newGroup.clone and mist.DBs.groupsByName[newGroup.name] or not newGroup.name then - newGroup['name'] = tostring(tostring(cntry) .. tostring(typeName) .. mistDynAddIndex) + newGroup['name'] = tostring(tostring(country.name[cntry]) .. tostring(typeName) .. mistDynAddIndex) end if not newGroup.hidden then @@ -921,7 +935,9 @@ end function mist.utils.makeVec3(Vec2, y) if not Vec2.z then - if not y then + if Vec2.alt and not y then + y = Vec2.alt + elseif not y then y = 0 end return {x = Vec2.x, y = y, z = Vec2.y} @@ -961,7 +977,9 @@ end -- gets heading-error corrected direction from point along vector vec. function mist.utils.getDir(vec, point) local dir = math.atan2(vec.z, vec.x) - dir = dir + mist.getNorthCorrection(point) + if point then + dir = dir + mist.getNorthCorrection(point) + end if dir < 0 then dir = dir + 2*math.pi -- put dir in range of 0 to 2*pi end @@ -980,7 +998,34 @@ function mist.utils.get3DDist(point1, point2) return mist.vec.mag({x = point1.x - point2.x, y = point1.y - point2.y, z = point1.z - point2.z}) end +function mist.utils.vecToWP(vec) + local newWP = {} + newWP.x = vec.x + newWP.y = vec.y + if vec.z then + newWP.alt = vec.y + newWP.y = vec.z + else + newWP.alt = land.getHeight({x = vec.x, y = vec.y}) + end + return newWP +end +function mist.utils.unitToWP(unit) + if type(unit) == 'string' then + if Unit.getByName(unit) then + unit = Unit.getByName(unit) + end + end + if unit:isExist() == true then + local new = mist.utils.vecToWP(unit:getPosition().p) + new.speed = mist.vec.mag(unit:getVelocity()) + new.alt_type = "BARO" + + return new + end + return false +end @@ -1010,6 +1055,15 @@ mist.utils.round = function(num, idp) return math.floor(num * mult + 0.5) / mult end +mist.utils.roundTbl = function(tbl, idp) + for id, val in pairs(tbl) do + if type(val) == 'number' then + tbl[id] = mist.utils.round(val, idp) + end + end + return tbl +end + -- porting in Slmod's dostring mist.utils.dostring = function(s) local f, err = loadstring(s) @@ -1526,7 +1580,8 @@ mist.tostringBR = function(az, dist, alt, metric) return s end -mist.getNorthCorrection = function(point) --gets the correction needed for true north +mist.getNorthCorrection = function(gPoint) --gets the correction needed for true north + local point = mist.utils.deepCopy(gPoint) if not point.z then --Vec2; convert to Vec3 point.z = point.y point.y = 0 @@ -3414,6 +3469,21 @@ mist.flagFunc.group_alive_more_than = function(vars) end end +mist.getAvgPoint = function(points) + local avgX, avgY, avgZ, totNum = 0, 0, 0, 0 + for i = 1, #points do + local nPoint = mist.utils.makeVec3(points[i]) + if nPoint.z then + avgX = avgX + nPoint.x + avgY = avgY + nPoint.y + avgZ = avgZ + nPoint.z + totNum = totNum + 1 + end + end + if totNum ~= 0 then + return {x = avgX/totNum, y = avgY/totNum, z = avgZ/totNum} + end +end --Gets the average position of a group of units (by name) diff --git a/rev changelog.txt b/rev changelog.txt index 7b5e9bb..72ee713 100644 --- a/rev changelog.txt +++ b/rev changelog.txt @@ -1,3 +1,15 @@ +v59 +-removed the new 3rd variable from mist.utils.getDir and just made the 2nd variable optional instead +-Added check to DB update functions to only update if data from the group has changed from what is currently in the DB. This fixes an issue I had where some data was lost if you spawned a group normally after mission start and then used a respawn, clone, or teleport function on said group +-fixed issue with dynAdd functions naming a group with the country ID insead of country name +-added mist.getAvgPoint(). Function is similar to mist.getAvgPos except it uses raw vec2/3 coordinates instead of units +-added mist.utils.roundTbl() Has the same input values as mist.utils.round except it accepts a table and it rounds all numbers in the first level of the table to the passed percision. + +v58 +-added optional variable to mist.utils.getDir() If present this function will return the "raw" heading, this is useful in assigning the heading variable within a task. +-mist.utils.makeVec3 now supports being given a table in WP format. Specifically it now coverts a table {x, y, alt) to vec3 + + v57 -fixed bug with groupRandomDistSelf improperly declaring the y variable -fixed flagFunc.units_in_moving_zones not checking for a unitNameTable for the zone_units