← Back to team overview

widelands-dev team mailing list archive

Re: [Merge] lp:~widelands-dev/widelands/bug-1810062-territorial-calculations into lp:widelands

 

Next try I have done is starting from the starting field and add all fields that can be conquered.


function get_valuable_fields()
   local fields = {}
   local check = {}
   local map = wl.Game().map
   local sf = map.player_slots[1].starting_field
   -- initilaize with the region around the startign field of P1
   for idx, fg in ipairs(sf:region(9)) do
      local index = fg.x * 1000 + fg.y
      fields[index] = fg
      check[index] = fg
   end
      local loop = true
      while loop do
         loop = false
         local new = {}
         -- checking the check region for buildcaps and add fields that can be conquered
         for idx, f in pairs(check) do 
            if f:has_max_caps("big") then
               local radius = f:region(10)
               for idx, fg in ipairs(radius) do
                  local index = fg.x * 1000 + fg.y
                  if fields[index] == nil and check[index] == nil then 
                     -- if new fields are discovered add them to the fields table and mark them for checking next loop
                     new[index] = fg
                     fields[index] = fg
                     loop = true
                  end
               end
            elseif f:has_max_caps("medium") then
               local radius = f:region(8)
               for idx, fg in ipairs(radius) do
                  local index = fg.x * 1000 + fg.y
                  if fields[index] == nil and check[index] == nil then 
                     new[index] = fg
                     fields[index] = fg
                     loop = true
                  end
               end
            elseif f:has_max_caps("small") then
               local radius = f:region(6)
               for idx, fg in ipairs(radius) do
                  local index = fg.x * 1000 + fg.y
                  if fields[index] == nil and check[index] == nil then 
                     new[index] = fg
                     fields[index] = fg
                     loop = true
                  end
               end
            end
         end
         check = new
      end
   local result = {}
   for idx,f in pairs(fields) do
      table.insert(result, f)
   end
   return result
end


Only thing left is to adapt this for seafaring i.e. doing the same thing for all port spaces as well.
Speed is still good I think. 
-- 
https://code.launchpad.net/~widelands-dev/widelands/bug-1810062-territorial-calculations/+merge/361366
Your team Widelands Developers is subscribed to branch lp:~widelands-dev/widelands/bug-1810062-territorial-calculations.


References