Unit:inArea() returns No when using ScenEdit_AddReferencePoint() generated reference points (Full Version)

All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Tech Support



Message


rneum -> Unit:inArea() returns No when using ScenEdit_AddReferencePoint() generated reference points (11/13/2021 11:31:51 AM)

I apologize for the multiple posts. I'm receiving an error of "You are not allowed to post links, emails or phone numbers for 7 days from the date of your tenth post." for posts with content such as the Command build and version numbers. I've determined the forum dislikes long numbers (lat and lon to 13 decimal places)

How do I use programmatically generated reference points with the Unit:inArea() function?

BLUF:
Command Version One dot Zero four, Build One-Thousand One-Hundred and forty-seven dot thirty-four
Lua_AddReferencepointError.scen
Test_InArea.lua.txt - Script to run in Lua Script Console

The Unit:inArea({'RP-6380','RP-6381','RP-6382','RP-6383'}) works as expected with manually created reference points, but does not produce a proper result with ScenEdit_AddReferencePoint() generated reference points. I'd thought the issue was a name format and added the ability to use mimicry (RP-...). that doesn't fix it either.

I attached the .scen file with the manual RPs and lua to run in the Lua script console. You can also create a new scenario, define the area manually area as N49, 37', 40"/W2,32',20" x N49,24',30/W2,18',40" and run the following test function.

Code to reproduce is attached in the Test_InArea.lua.txt.

This isn't a blocker, but it prevents a unit test for a function. It will be when I use actions to dynamically deploy randomized reference points.
==================================================================
==================================================================


function TestInArea()
-- Define area as N49, 37', 40"/W2,32',20" x N49,24',30/W2,18',40"
--Enter the RP Names below
local mrp1 = 'RP-6380'
local mrp2 = 'RP-6381'
local mrp3 = 'RP-6382'
local mrp4 = 'RP-6383'
local nameRp1 = 'PortsInAreaA'
local nameRp2 = 'PortsInAreaB'
local nameRp3 = 'PortsInAreaC'
local nameRp4 = 'PortsInAreaD'
mimicmanualnames = false
if mimicmanualnames then
nameRp1 = 'RP-9100'
nameRp2 = 'RP-9101'
nameRp3 = 'RP-9102'
nameRp4 = 'RP-9103'
end
--setup
testside = VP_GetSides ()[1].name
local port = { name = 'Saint Sampson', shore = { latitude = 49.48, longitude = -2.45 }, harbor = { latitude = 49.51, longitude = -2.42 }, country = 'Guernsey' }

local retval,shipT = pcall(ScenEdit_AddUnit,{type ='Ship', unitname =port.name .. " Buoy", dbid=347, side =VP_GetSides ()[1].name, Lat=port.harbor.latitude,Lon=port.harbor.longitude}) ;
local testship
if not retval or shipT ~= nil then
testship = ScenEdit_GetUnit({name=shipT.name , guid=shipT.guid});
end
size = 1
local rp1=ScenEdit_AddReferencePoint( {side=testside, name=nameRp1, lat=testship.latitude + (size/10), lon=testship.longitude + (size/10), highlighted=true})
local rp2=ScenEdit_AddReferencePoint( {side=testside, name=nameRp2, lat=testship.latitude + (size/10), lon=testship.longitude - (size/10), highlighted=true})
local rp3=ScenEdit_AddReferencePoint( {side=testside, name=nameRp3, lat=testship.latitude - (size/10), lon=testship.longitude + (size/10), highlighted=true})
local rp4=ScenEdit_AddReferencePoint( {side=testside, name=nameRp4, lat=testship.latitude - (size/10), lon=testship.longitude - (size/10), highlighted=true})
-- test
if testship ~= nil then
t = testship:inArea({'RP-6380','RP-6381','RP-6382','RP-6383'})
t = testship:inArea({mrp1,mrp2,mrp3,mrp4})
print("Manual RPs tested testship:inArea({'" .. mrp1 .. "','" .. mrp2 .. "','" .. mrp3 .. "','" .. mrp4 .. "'}) match:")
print(t)
t = testship:inArea({nameRp1,nameRp1,nameRp1,nameRp1})
print("Lua based RPs ceated using ScenEdit_AddReferencePoint tested testship:inArea({'" .. nameRp1 .. "','" .. nameRp2 .. "','" .. nameRp3 .. "','" .. nameRp4 .. "'}) match:")
print(t)
t = testship:inArea({rp1.name,rp2.name,rp3.name,rp4.name})
print("Lua based RPs ceated using ScenEdit_AddReferencePoint tested testship:inArea({rp1.name,rp2.name,rp3.name,rp4.name}) match:")
print(t)
t = testship:inArea({rp1.guid,rp2.guid,rp3.guid,rp4.guid})
print("Lua based RPs ceated using ScenEdit_AddReferencePoint tested testship:inArea({rp1.guid,rp2.guid,rp3.guid,rp4.guid}) match:")
print(t)

end
-- cleanup
strnum = ScenEdit_InputBox ('pause before cleanup')
if testship ~= nil and testship:delete() then end
ScenEdit_DeleteReferencePoint ({side=testside, guid=rp1.guid})
ScenEdit_DeleteReferencePoint ({side=testside, guid=rp2.guid})
ScenEdit_DeleteReferencePoint ({side=testside, guid=rp3.guid})
ScenEdit_DeleteReferencePoint ({side=testside, guid=rp4.guid})
end
TestInArea()




KnightHawk75 -> RE: Unit:inArea() returns No when using ScenEdit_AddReferencePoint() generated reference points (11/14/2021 7:06:15 AM)

See my attached testbed sample for a functional example.
You can either look at the special actions or to test under 'event' conditions enable the regularly timed event.
Both execute the following for the auto-generation test (that also include a test of manually placed tanker in the scene).
It creates the rp's and then uses them in a unit:inArea() test, that should be true based on it's generate location.
local function TestAutoRPS() 
  local autolats = {26.077119, 26.092939, 23.935986, 23.917725}
  local autolons = {-95.459756, -91.126452, -91.078016, -95.414230}
  local autonamebase = "Auto-";
  local autosidename = "Green";
  local autorps = {}
  local u = SE_GetUnit({side="Green",name="Tanker VLCC #1"})
  local retvalu2,u2 = pcall(ScenEdit_AddUnit,{side=autosidename,type="Ship",dbid=259,name="AutoGen Tanker VLCC",latitude=25.0849,longitude=-93.0443,heading=0,speed=0})
  print('adding auto ref points.');
  for i = 1,4 do
    autorps[i] = ScenEdit_AddReferencePoint({side=autosidename, name=autonamebase .. tostring(i), latitude=autolats[i], longitude=autolons[i]})
    if autorps[i] ~= nil then print('refpoint '.. tostring(autorps.name) .. ' added.') end
  end
  print("-- Sanity checking. --")
  print("verifying autorps returns");
  for k,v in ipairs(autorps) do
    print(v.name .. ":  lat: " ..v.latitude .. " lon: " .. v.longitude )
  end
  print("verifying autorps with side lookup and getrefpoint calls")
  local s = VP_GetSide({side="Green"})
  for k,v in ipairs(s.rps) do
    if string.find(v.name,'Auto-',1,true) ~= nil then --filter for only Auto-xx rps
      print("getting refference point for " .. v.name)
      local rp = ScenEdit_GetReferencePoint({side="Green",guid=v.guid})
      print("rp obtained - " ..rp.name .. ":  lat: " ..rp.latitude .. " lon: " .. rp.longitude )
      rp = nil;
    end
  end
  print("-- End sanity checking. --");
  if #autorps == 4 then --Do we have list and right number of autogen rps?
    local retval = u:inArea({'Auto-1','Auto-2','Auto-3','Auto-4'})
    print('Manual tanker is in the Auto-X area? ' .. tostring(retval));
    ScenEdit_MsgBox("Manual tanker is in the Auto-X rp Area? : " .. tostring(retval),1)
  end
  if (retvalu2 ==true) and u2 ~=nil then --was unit creation successful?
    local retval = u2:inArea({'Auto-1','Auto-2','Auto-3','Auto-4'})
    print('Auto generated tanker is in the Auto-X area? ' .. tostring(retval));
    ScenEdit_MsgBox("Auto generated tanker is in the Auto-X Area? : " .. tostring(retval),1)
  end
  --cleanup
  if u2 ~=nil then --if unit exists remove it.
    print("removing Auto generated unit.");
    u2:delete();
  end
  print("removing Auto-X ref points.")
  for i=1,#autorps do
    if autorps[i] ~= nil then --skip if nil.
      local strsuccess = "Removed RP " .. tostring(autorps[i].name) .. "  by guid:".. tostring(autorps[i].guid);
      local retval1 = ScenEdit_DeleteReferencePoint({side=autosidename,guid=autorps[i].guid});
      if retval1 then print(strsuccess); else print('failed to remove'); end
    end
  end
end
TestAutoRPS();

Can also just plop the above into the console after loading the scene.
I'll have to dig into yours to see where things go wrong, probably something simple but not obvious at first glance.




KnightHawk75 -> RE: Unit:inArea() returns No when using ScenEdit_AddReferencePoint() generated reference points (11/14/2021 8:18:03 AM)

rneum,

Two problems in your original code:
first (primarily):
local rp1=ScenEdit_AddReferencePoint( {side=testside, name=nameRp1, lat=testship.latitude + (size/10), lon=testship.longitude + (size/10), highlighted=true})
local rp2=ScenEdit_AddReferencePoint( {side=testside, name=nameRp2, lat=testship.latitude + (size/10), lon=testship.longitude - (size/10), highlighted=true}) 

These two need to be reversed, the location calculated for #2 needed to be first, and the first one needs to be second, if you want a 4pt poly box shape. (this would fail normal geometric validation without it... on the map before cleanup you can see A is to the right of B). As it was unit actually was not really inside given the order of submission.

Second:
    t = testship:inArea({nameRp1,[b]nameRp1,nameRp1,nameRp1[/b]}) <--you forgot to change the last 3 varnames.


Attached is your .txt file updated (working) with my changes (some vars weren't 'local' and I tweaked a few others, but the crux was what was already mentioned.)




rneum -> RE: Unit:inArea() returns No when using ScenEdit_AddReferencePoint() generated reference points (12/10/2021 10:29:50 AM)

Thank you, the order of reference points. SMH




jannas34 -> RE: Unit:inArea() returns No when using ScenEdit_AddReferencePoint() generated reference points (12/21/2021 8:14:01 AM)

lololol this is not getting fixed




KnightHawk75 -> RE: Unit:inArea() returns No when using ScenEdit_AddReferencePoint() generated reference points (12/22/2021 6:09:10 AM)


quote:

ORIGINAL: jannas34

lololol this is not getting fixed

Jannas34 - Did you read the thread? There was nothing to fix, it was just user error.




thewood1 -> RE: Unit:inArea() returns No when using ScenEdit_AddReferencePoint() generated reference points (12/22/2021 10:44:18 AM)

What do you think the answer to that is? Does no one realize this guy posted in over 20 threads with similar messages. Why are wasting the effort in responding?




KnightHawk75 -> RE: Unit:inArea() returns No when using ScenEdit_AddReferencePoint() generated reference points (12/22/2021 4:28:49 PM)


quote:

ORIGINAL: thewood1

What do you think the answer to that is? Does no one realize this guy posted in over 20 threads with similar messages. Why are wasting the effort in responding?

Answer to what is?
Yes I realized, _after_ this and a few other posts




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.5625