Untitled
unknown
lua
2 years ago
5.5 kB
4
Indexable
local Scenery_Exclusion_Table = {["POWER_TRANS_LINE_01"] = true, ["POWER_TRANS_HUB_1"] = true, ["POWER_TRANS_STATION"] = true} -- The typename of scenery objects that we want to filter out should be indexed in this table local function Scanner (Player_Object, Player_Grp, Player_Grp_ID) Building_Counter = 0 -- This is a building counter I added, it resets every time the function is called --- The following code is used to get a vec3 point offset from the client helo, which will be used as the center of a spherical scan zone. -- Vectors making up the coordinate system. local X = Player_Object:getPosition().x local Z = Player_Object:getPosition().z -- Offset vector: x meters ahead, z meters starboard (the y will be terrain elevation, which we'll find later). I set an offset 100 meters ahead, in this case local A = { x = 100, z = 0 } -- Scale components of orthonormal coordinate vectors local x = { x = X.x * A.x, z = X.z * A.x } local z = { x = Z.x * A.z, z = Z.z * A.z } -- Add up vectors in the unit coordinate system ==> this gives the offset vector relative to the origin of the map local a = { x = x.x + z.x, z = x.z + z.z } -- Vector from the origin of the map to the unit. local u = Player_Object:getPoint() -- Translate offset vector from map origin to the unit: v=u+a (v is our offset Vec2 point ). local v = { x = a.x + u.x, z = a.z + u.z } local h = land.getHeight({x= v.x , y = v.z}) -- Then we find the terrain elevation at v, in order to get the y value for the final offset vec3 point local Offset_Vec3_Point = {x = v.x , y = h, z = v.z} -- Finally, this is the offset vec3 point, which will be the center of the scan area trigger.action.smoke(Offset_Vec3_Point, 1) -- Optional smoke, for testing purposes local Search_Area = {id = world.VolumeType.SPHERE, params = {point = Offset_Vec3_Point , radius = 100}} -- I used a 100 meter radius sphere for the scan, but it can be changed local function Evaluate_Zone(Scenery_Object) if Scenery_Object then if Scenery_Object:getCategory() == Object.Category.SCENERY and Scenery_Object:getTypeName() ~= nil and Scenery_Object:getDesc() ~= nil then local Scenery_Type = Scenery_Object:getTypeName() local Scenery_Desc = Scenery_Object:getDesc() -- The description table needs to be accessed in order to get the scenery object's specific category if Scenery_Desc.attributes.Buildings == true and Scenery_Exclusion_Table[Scenery_Type] == nil then -- Here I filtered out non-problematic buildings which are indexed in the exclusion table (like electric posts, hubs, etc.), but it's optional Building_Counter = Building_Counter + 1 --trigger.action.outTextForGroup(Player_Grp_ID, "Detected building type: "..Scenery_Type, 10) end end end end world.searchObjects(Object.Category.SCENERY, Search_Area, Evaluate_Zone) if Building_Counter > 0 then trigger.action.outTextForGroup(Player_Grp_ID, Building_Counter.." building(s) in the way! FOB can't be built here!", 10) elseif Building_Counter == 0 then trigger.action.outTextForGroup(Player_Grp_ID, "No buildings in the way, FOB under construction!", 10) -- Add the code to deploy FOBs here end end --- The rest of the code is used to create/refresh the F10 menu I'm using for testing local Scenery_Scan_Menu = {} Menu_Flag = 0 -- This is a flag I used to manage F10 menu refreshing local function Refresh_Menu(Player_Object, Player_Grp_ID, Player_Grp) if Menu_Flag == 0 then Scenery_Scan_Menu["Menu_Root"] = missionCommands.addSubMenuForGroup(Player_Grp_ID , "Scenery scan", nil) Scenery_Scan_Menu["Scan"] = missionCommands.addCommandForGroup(Player_Grp_ID , "Scan!" , Scenery_Scan_Menu.Menu_Root , Scanner, Player_Object, Player_Grp, Player_Grp_ID) Menu_Flag = 1 elseif Menu_Flag == 1 then trigger.action.removeOtherCommand("Scenery scan") Scenery_Scan_Menu["Menu_Root"] = missionCommands.addSubMenuForGroup(Player_Grp_ID , "Scenery scan", nil) Scenery_Scan_Menu["Scan"] = missionCommands.addCommandForGroup(Player_Grp_ID , "Scan!" , Scenery_Scan_Menu.Menu_Root , Scanner, Player_Object, Player_Grp, Player_Grp_ID) end end Birth_Event_Handler = {} function Birth_Event_Handler:onEvent(event) if event.id == world.event.S_EVENT_BIRTH and event.initiator:isExist() ~= nil and event.initiator:getCategory() == Object.Category.UNIT and event.initiator:getPlayerName() ~= nil then local Player_Object = event.initiator local Player_Grp = Player_Object:getGroup() local Player_Grp_ID = Player_Grp:getID() Refresh_Menu(Player_Object, Player_Grp_ID, Player_Grp) end end world.addEventHandler(Birth_Event_Handler)
Editor is loading...