← Back to team overview

widelands-dev team mailing list archive

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

 

@ Gun: I tried to reply your comment, although I am not sure whether I understood it correctly.

@ kaputtnik: Yeah the values of Astoria might imply we should do it in a coroutine.

I just did some testing with trident of fire and it didn't crash although I hardly managed to conquer some land and did not see any map editing. will continue to do so.

Diff comments:

> === modified file 'data/scripting/win_conditions/territorial_functions.lua'
> --- data/scripting/win_conditions/territorial_functions.lua	2019-02-12 17:30:21 +0000
> +++ data/scripting/win_conditions/territorial_functions.lua	2019-02-19 07:34:52 +0000
> @@ -14,24 +14,65 @@
>  local wc_had_territory = _"%1$s had %2$3.0f%% of the land (%3$i of %4$i)."
>  
>  -- RST
> --- .. function:: get_buildable_fields()
> ---
> ---    Collects all fields that are buildable
> ---
> ---    :returns: a table with the map's buildable fields
> ---
> -function get_buildable_fields()
> +-- .. function:: get_valuable_fields()
> +--
> +--    Collects all fields that are valuable
> +--
> +--    :returns: a table with the map's valuable fields
> +--
> +function get_valuable_fields()
>     local fields = {}
> +   local check = {}
>     local map = wl.Game().map
> -   for x=0, map.width-1 do
> -      for y=0, map.height-1 do
> -         local f = map:get_field(x,y)
> -         if f.buildable then
> -            table.insert(fields, f)
> -         end
> -      end
> -   end
> -   return fields
> +   local plrs = wl.Game().players
> +   for idx, player in ipairs(plrs) do
> +      local sf = map.player_slots[idx].starting_field
> +   -- initialize the fields table and the check area with the regions around the starting field of each Player
> +      for idx, fg in ipairs(sf:region(9)) do

I understand what you mean, but it isn't quite as simple as this. I am adding the regions that will be conquered by a HQ because this is what a player has in the beginning. As we started with a conquer radius of 10 for big caps this is less than was in the loop. Additionally the Hq region does contain water and the loop doesn't so basically with our new value of 9 for big caps we could skip the sf regions but they are doing no harm to us. Other thing for a port space cause a port only conquers a radius of 5. so we definitly can't start with a region of 9 for the port spaces.

regarding the check table this is just a trick to just go only over the fields discovered in the last loop and not all of them which speeds up the process a lot.

> +         fields[fg.__hash] = fg
> +         check[fg.__hash] = fg
> +      end
> +   end
> +   if map.allows_seafaring == true then
> +      -- add port spaces to the starting check area
> +      for idx, spaces in ipairs(map.port_spaces) do
> +         local f = map:get_field(spaces["x"],spaces["y"])
> +         for idx, fg in ipairs(f:region(5)) do
> +            fields[fg.__hash] = fg
> +            check[fg.__hash] = fg
> +         end
> +      end
> +   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
> +         local radius ={}
> +         if f:has_max_caps("big") then
> +            radius = f:region(9)
> +         elseif f:has_max_caps("medium") then
> +            radius = f:region(7)
> +         elseif f:has_max_caps("small") then
> +            radius = f:region(5)
> +         end
> +         for idx, fg in ipairs(radius) do
> +            if fields[fg.__hash] == nil and check[fg.__hash] == nil and fg:has_max_caps("walkable") then
> +               new[fg.__hash] = fg
> +               fields[fg.__hash] = fg
> +               loop = true
> +            end
> +         end
> +      end
> +      check = new
> +   end
> +   local result = {}
> +   -- as our fields table is not continuosly indexed we need to build a properly indexed table
> +   for idx,f in pairs(fields) do
> +      table.insert(result, f)
> +   end
> +   return result
>  end
>  
>  -- RST


-- 
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