Loading

Paste #pdrbr6mm6

  1. #!/usr/bin/env lua
  2. editor_sprites=
  3. {foliage={{sprite=192, height=2}, -- Regular European shrub 1
  4.           {sprite=193, height=2}, -- Ground plant, green 1
  5.           {sprite=194, height=2}, -- Bush
  6.           {sprite=195, height=2}, -- Ground plant, red flowers
  7.           {sprite=196, height=2}, -- Shrub
  8.           {sprite=197, height=4}, -- Dead tree (very high)
  9.           {sprite=198, height=2}, -- Low ground plant
  10.           {sprite=199, height=2}, -- Regular European shrub 2
  11.           {sprite=200, height=2}, -- Ground plant, flowers
  12.           {sprite=201, height=1}, -- Flowerbed East/South
  13.           {sprite=202, height=1}, -- Flowerbed West/South
  14.           {sprite=203, height=1}, -- Flowerbed West/North
  15.           {sprite=204, height=1}  -- Flowerbed North/South
  16.          },
  17.  hederow={{sprite=176, height=2}, -- Hedge West-North
  18.           {sprite=177, height=2}, -- Hedge West-South
  19.           {sprite=178, height=2}, -- Hedge East-South
  20.           {sprite=179, height=2}, -- Hedge East-North
  21.           {sprite=180, height=2}, -- Hedge West-East
  22.           {sprite=181, height=2}, -- Hedge North-South
  23.           {sprite=182, height=2}, -- Hedge East-West-South
  24.           {sprite=183, height=2}, -- Hedge East-West-North
  25.           {sprite=184, height=2}, -- Hedge West-North-South
  26.           {sprite=185, height=2}, -- Hedge East-North-South
  27.           {sprite=186, height=2}, -- Hedge East-West-North-South
  28.           {sprite=187, height=2}, -- Hedge North-South with shrub
  29.           {sprite=188, height=2}, -- Hedge North-South with holes
  30.           {sprite=189, height=2}, -- Hedge East-West with shrub 1
  31.           {sprite=190, height=2}, -- Hedge East-West with holes
  32.           {sprite=191, height=2}, -- Hedge East-West with shrub 2
  33.          },
  34.  pond={{sprite= 60, height=1}, -- South edge of a pond
  35.        {sprite= 65, height=1}, -- West edge of a pond
  36.        {sprite= 68, height=1}, -- North edge of a pond
  37.        {sprite= 78, height=1}, -- East edge of a pond
  38.        {sprite= 59, height=1}, -- South-West corner of a pond
  39.        {sprite= 77, height=1}, -- North-East corner of a pond
  40.        {sprite= 79, height=1}, -- South-East corner of a pond
  41.        {sprite= 80, height=1}, -- North-West corner of a pond
  42.        {sprite= 69, height=1}, -- Water tile of a pond
  43.        {sprite= 71, height=1}, -- Water tile of a pond with water lilies
  44.        {sprite= 72, height=2}, -- Water tile of a pond with water plant 1
  45.        {sprite= 73, height=2}  -- Water tile of a pond with water plant 2
  46.       },
  47.  inside={{sprite= 16, height=1}, -- Dark big checker pattern tile
  48.          {sprite= 17, height=1}, -- Dark blue/purple carpet tile
  49.          {sprite= 70, height=1}, -- Duplicate of 017
  50.          {sprite= 18, height=1}, -- Red-Blue floor tile 1
  51.          {sprite= 19, height=1}, -- Red-Blue floor tile 2
  52.          {sprite= 23, height=1}, -- Red-Blue floor tile 3
  53.          {sprite= 20, height=1}, -- Wooden floor tile
  54.          {sprite= 21, height=1}, -- Small checker pattern tile
  55.          {sprite= 22, height=1}, -- Big checker pattern tile
  56.          {sprite= 66, height=1}, -- Floor tile with light center
  57.          {sprite= 76, height=1}  -- Floor tile with light center and corners
  58.         },
  59.  outside={{sprite=  1, height=1}, -- Grass tile 1
  60.           {sprite=  2, height=1}, -- Grass tile 2
  61.           {sprite=  3, height=1}, -- Grass tile 3
  62.           {sprite=  4, height=1}, -- Light concrete tile
  63.           {sprite= 15, height=1}, -- Concrete tile
  64.           {sprite=  5, height=1}, -- Dark concrete tile
  65.           {sprite=  6, height=1}, -- Grass tile with South-East concrete corner
  66.           {sprite=  8, height=1}, -- Grass tile with South-West concrete corner
  67.           {sprite= 10, height=1}, -- Grass tile with North-West concrete corner
  68.           {sprite= 12, height=1}, -- Grass tile with North-East concrete corner
  69.           {sprite=  7, height=1}, -- Grass tile with South concrete edge
  70.           {sprite=  9, height=1}, -- Grass tile with West concrete edge
  71.           {sprite= 11, height=1}, -- Grass tile with North concrete edge
  72.           {sprite= 13, height=1}, -- Grass tile with East concrete edge
  73.           {sprite= 14, height=1}, -- Concrete tile with North-East grass corner
  74.           {sprite= 61, height=1}, -- Concrete tile with South-West grass corner
  75.           {sprite= 62, height=1}, -- Concrete tile with South-East grass corner
  76.           {sprite= 63, height=1}, -- Concrete tile with North-West grass corner
  77.           {sprite= 64, height=1}, -- Grass tile with rocks
  78.           {sprite=205, height=1}, -- Fully cracked garden marble tile
  79.           {sprite=206, height=1}, -- Broken garden marble tile
  80.           {sprite=207, height=1}, -- Partially cracked garden marble tile
  81.           {sprite=208, height=1}, -- Garden marble tile
  82.           {sprite=209, height=2}, -- Lamp post pointing East
  83.           {sprite=210, height=2}, -- Lamp post pointing South
  84.          },
  85.  road={{sprite= 41, height=1}, -- Road with white discontinuous line North-South
  86.        {sprite= 42, height=1}, -- Road with double yellow lines at West with black orthogonal lines
  87.        {sprite= 43, height=1}, -- Road with double yellow lines at West edge
  88.        {sprite= 44, height=1}, -- Road with double yellow lines at West edge merging at North
  89.        {sprite= 45, height=1}, -- Road with double yellow lines at West edge merging at South
  90.        {sprite= 46, height=1}, -- Duplicate of 045
  91.        {sprite= 47, height=1}, -- Road with red line at East linked to yellow discontinuous line at South
  92.        {sprite= 48, height=1}, -- Road with red line at East linked to yellow discontinuous line at North
  93.        {sprite= 49, height=1}, -- Road with red braking line at the East pointing to the West
  94.        {sprite= 50, height=1}, -- Road with grey edge at the East
  95.        {sprite= 51, height=1}, -- Road with double yellow lines at East edge
  96.        {sprite= 52, height=1}, -- Road with double yellow lines at East with black orthogonal lines
  97.        {sprite= 53, height=1}, -- Road with double yellow lines at East edge merging at the south
  98.        {sprite= 54, height=1}, -- Duplicate of 052
  99.        {sprite= 55, height=1}, -- Road with red braking line at the West pointing to the East
  100.        {sprite= 56, height=1}, -- Road with red line at West linked to yellow discontinuous line at North
  101.        {sprite= 57, height=1}, -- Road with red line at West linked to yellow discontinuous line at South
  102.        {sprite= 58, height=1}, -- Road with grey edge at the West
  103.       },
  104.  ["north wall"]={{sprites={{sprite=159, xpos=1, ypos=1}, -- External doorway North outside left part
  105.                            {sprite=157, xpos=3, ypos=1}, -- External doorway North outside right part
  106.                           },
  107.                   height=3},
  108.                  {sprites={{sprite=163, xpos=1, ypos=1}, -- External doorway North inside left part
  109.                            {sprite=161, xpos=3, ypos=1}, -- External doorway North inside right part
  110.                           },
  111.                   height=3},
  112.  
  113.                  {sprite=114, height=2}, -- External North wall outside
  114.                  {sprite=116, height=2}, -- External North wall outside left part of window
  115.                  {sprite=120, height=2}, -- External North wall with window glass
  116.                  {sprite=118, height=2}, -- External North wall outside right part of window
  117.                  {sprite=122, height=2}, -- External North wall inside
  118.                  {sprite=124, height=2}, -- External North wall inside left part of window
  119.                  {sprite=126, height=2}, -- External North wall inside right part of window
  120.                 },
  121.  ["west wall"]={{sprites={{sprite=158, xpos=1, ypos=3}, -- External doorway West outside left part
  122.                           {sprite=160, xpos=1, ypos=1}, -- External doorway West outside right part
  123.                          },
  124.                  height=3},
  125.                 {sprites={{sprite=162, xpos=1, ypos=3}, -- External doorway West inside left part
  126.                           {sprite=164, xpos=1, ypos=1}, -- External doorway West inside right part
  127.                          },
  128.                  height=3},
  129.  
  130.                 {sprite=115, height=2}, -- External West wall outside
  131.                 {sprite=119, height=2}, -- External West wall outside left part of window
  132.                 {sprite=121, height=2}, -- External West wall with window glass
  133.                 {sprite=117, height=2}, -- External West wall outside right part of window
  134.                 {sprite=123, height=2}, -- External West wall inside
  135.                 {sprite=127, height=2}, -- External West wall inside left part of window
  136.                 {sprite=125, height=2}, -- External West wall inside right part of window
  137.                },
  138.  helipad={{sprites={{sprite=5, xpos=1, ypos=1}, {sprite=5, xpos=2, ypos=1}, {sprite=5, xpos=3, ypos=1},
  139.                     {sprite=5, xpos=4, ypos=1}, {sprite=5, xpos=5, ypos=1}, {sprite=5, xpos=1, ypos=2},
  140.                     {sprite=5, xpos=1, ypos=3}, {sprite=5, xpos=1, ypos=4}, {sprite=5, xpos=1, ypos=5},
  141.                     {sprite=5, xpos=2, ypos=5}, {sprite=5, xpos=3, ypos=5}, {sprite=5, xpos=4, ypos=5},
  142.                     {sprite=5, xpos=5, ypos=5}, {sprite=5, xpos=5, ypos=2}, {sprite=5, xpos=5, ypos=3},
  143.                     {sprite=5, xpos=5, ypos=4},
  144.                     {sprite=5, xpos=3, ypos=2}, {sprite=5, xpos=3, ypos=4},
  145.  
  146.                     {sprite=4, xpos=2, ypos=2}, {sprite=4, xpos=2, ypos=3}, {sprite=4, xpos=2, ypos=4},
  147.                     {sprite=4, xpos=4, ypos=2}, {sprite=4, xpos=4, ypos=3}, {sprite=4, xpos=4, ypos=4},
  148.                     {sprite=4, xpos=3, ypos=3}
  149.                    },
  150.            height=3}
  151.           }
  152. }
  153. local MAX_HEIGHT = 4 -- Biggest height in above sprites
  154.  
  155. --! Normalize the editor sprite in the table to always have a 'sprites' field, as well as have sizes and a column width (for the display).
  156. --!param (table) Sprite from the editor_sprites table.
  157. --!return (table 'sprites', 'xsize', 'ysize', 'width', and 'height')
  158. function normalize_edit_sprite(spr)
  159.   assert(MAX_HEIGHT >= spr.height) -- Verify that sprite fits in the maximum height.
  160.  
  161.   if spr.sprites == nil then -- {spritex=xxx, height=y} case
  162.     return {sprites={sprite=spr.sprite, xpos=1, ypos=1},
  163.             xsize=1,
  164.             ysize=1,
  165.             width=2,
  166.             height=spr.height}
  167.   else
  168.     -- {sprites={...}, height=y} case, compute sizes, and width
  169.     local xsize=1
  170.     local ysize=1
  171.     for _, sp in ipairs(spr.sprites) do
  172.       if sp.xpos > xsize then xsize = sp.xpos end
  173.       if sp.ypos > ysize then ysize = sp.ypos end
  174.     end
  175.  
  176.     local width = xsize
  177.     if width < ysize then width = ysize end
  178.  
  179.     return {sprites=spr.sprites,
  180.             xsize=xsize,
  181.             ysize=ysize,
  182.             width = width + 1,
  183.             height=spr.height}
  184.   end
  185. end
  186.  
  187.  
  188. --!Decide the highest possible placement for 'width' columns.
  189. --!param cols (list int) First available position in each column, higher number is lower.
  190. --!param width (int) Required width as number of columns.
  191. --!return (int, int) Starting column and placement height.
  192. function get_highest_rowcol(cols, width)
  193.   local best = cols[1] + 100000
  194.   local best_col = 0
  195.  
  196.   for left = 1, #cols - width + 1 do -- Try starting in each column.
  197.     if cols[left] < best then -- First column is potentially better.
  198.       local top = cols[left]
  199.       for i = 1, width - 1 do
  200.         if top < cols[left + i] then
  201.           top = cols[left + i]
  202.           if top >= best then break end -- i-th column breaks improvement.
  203.         end
  204.       end
  205.       if top < best then
  206.         best = top
  207.         best_col = left
  208.       end
  209.     end
  210.   end
  211.   return best_col, best
  212. end
  213.  
  214. --! Layout buttons from a tab of editor sprites.
  215. --!param esprs (list) Editor sprite tab to layout.
  216. --!param num_cols (int) Number of columns available in the layout.
  217. --!return (list) Sprites with button positions in ('column', 'row').
  218. function layout_buttons(esprs, num_cols)
  219.   local buttons = {}
  220.  
  221.   local cols = {}
  222.   for i = 1, num_cols do
  223.     cols[i] = 1
  224.   end
  225.  
  226.   for hgt = MAX_HEIGHT, 1, -1 do -- Fit highest sprites first.
  227.     for _, espr in ipairs(esprs) do
  228.       espr = normalize_edit_sprite(espr)
  229.       if espr.height == hgt then
  230.         local spr_col, spr_height = get_highest_rowcol(cols, espr.width)
  231.         buttons[#buttons + 1] = espr
  232.         buttons[#buttons].column = spr_col
  233.         buttons[#buttons].row = spr_height
  234.  
  235.         for i = spr_col, spr_col + espr.width - 1 do -- Update height in the affected columns
  236.           assert(cols[i] <= spr_height)
  237.           cols[i] = spr_height + hgt
  238.         end
  239.  
  240.       end
  241.     end
  242.   end
  243.   return buttons
  244. end
  245.  
  246. for name, sprs in pairs(editor_sprites) do
  247.   local buttons = layout_buttons(sprs, 6)
  248.   print(name)
  249.   for _, button in ipairs(buttons) do
  250.     print("col " .. button.column .. " y " .. button.row .. " width " .. button.width .. " height " .. button.height)
  251.   end
  252. end

Comments