Jump to content

Script design help for picking locations by distance


Pikey

Recommended Posts

Hi, i dont need a script as such, i'm just after some design thoughts, it's more of a table problem than anything, so anyone with a maths brain could help!

 

 

It's a straight forward algorithm used by an AI commander to target something for attack.In words, i would like the script to ask, "what is the nearest Red airbase to a blue airbase 'pair' from all the existing 21 airbases on Caucasus?" For example if red had Sogunlog and blue had Tbilisi, it would always return that pair since they are the closest. But in a capture the map mission vs AI, blue is conquering airbases, and the airbases are changing ownership.

 

 

I've considered pre populating a lookup table for this. And it's getting a bit multidimensional for my brain now.

 

 

AirbaseMatrix = {Tbilisi={[sogunlug]=2, [Vaziani]=6.9, [beslan]=176}, Vaziani = {[Tbilisi]= 7, [soganlug]=8, [beslan]=180...etc}

 

 

Question is, how would I query that table in Lua to get the shortest pair and filter by coalition each time to provide the closest blue to a red airbase? New table > delete the keys that are blue, take smallest value from each of the rest > new table > order by smallest first, take top two and throw a dice?

 

 

Better ways? Missing something completely? It irks me that something my eyes can work out in an instant is so damn tough for a computer to do.

___________________________________________________________________________

SIMPLE SCENERY SAVING * SIMPLE GROUP SAVING * SIMPLE STATIC SAVING *

Link to comment
Share on other sites

Brute force it. Didn't test this, but might give you an idea of how to progress.

 

local ab = world.getAirbases()
local nearestPair = {dist = 1000000} -- assign large value to start so that almost anything will be smaller than it
for i = 1, #ab do
   local coa = (ab[i]):getCoalition()
   local pos = (ab[i]):getPoint()
   if coa ~= 0 then -- not neutral
       for j = 1, #ab do
           local oCoa = Airbase.getCoalition(ab[j])
           if i ~= j and (coa ~= oCoa and oCoa ~= 0 ) then -- not the same airbase and not the same coalition and not neutral
               local dist = mist.utils.get2DDist(pos, (ab[j]):getPoint()) -- whatever you wanted to use to get the distance between the two points
               if dist < nearestPair.dist then -- check against the shortest distance between opposing side airbases
                   nearestPair = {dist = dist, bases = {i, j}} -- newly defined entry
               end
           end
       end
   end
end

if not nearestPair.bases then
   -- no pair found somehow.
end

 

Basically iterate through all airbases and save the shortest distance to an enemy base.

The right man in the wrong place makes all the difference in the world.

Current Projects:  Grayflag ServerScripting Wiki

Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread)

 SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum

Link to comment
Share on other sites

And I spent an hour writing an associative table that couldn't be sorted with table.sort because ... LUA! (advice to all, if you ever learned any formal databases before seeing a LUA table, prepare to unlearn and unsee everything)

 

I'll test yours and see how the performance of all those vec2 to vec2 lookups goes, it scares me because, well 400 plus lookups of point to points sound scary but I'll likely be surprised again.

 

 

Either way will feedback here if the code is good and thank you very much for your time :)

___________________________________________________________________________

SIMPLE SCENERY SAVING * SIMPLE GROUP SAVING * SIMPLE STATIC SAVING *

Link to comment
Share on other sites

Absolutely insane. Worked perfectly and is instant.

 

 

ab[nearestPair.bases[1]]:getName() --Senaki

 

ab[nearestPair.bases[2]]:getName() --Kutaisi

 

 

I'm pretty humbled.

 

You've saved me a lot of time, sir. I'm genuinely very grateful, thank you.

___________________________________________________________________________

SIMPLE SCENERY SAVING * SIMPLE GROUP SAVING * SIMPLE STATIC SAVING *

Link to comment
Share on other sites

What I've been able to do with this is amazing to watch. I painted the map blue with a red airbase and used Grimes picker to launch helicopter landings and attacks. In a few hours they were well on their way to capturing the entire map, hopping from one base to the next always efficiently picking the fastest attack, then capturing airbases and using them to launch further attacks.

 

 

So cool to see this in action.

 

 

One note for anyone using this and taking the output to a function to do something, you will get either red or blue from the result in no particular order, so you need to check the return from Grimes code and sort it by Coalition again. I did also get one failure in about 50 calls and I have no idea why, but it's easy to handle. I think something changed coalition at exactly the wrong time

 

 

Another bonus (or hindrance depending on viewpoint) is that it will return Ships, Farps and Oil Rigs quite happily so you have to consider their placements at any given time.

___________________________________________________________________________

SIMPLE SCENERY SAVING * SIMPLE GROUP SAVING * SIMPLE STATIC SAVING *

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...