Advertisement
xth

Dune Generator.lua

xth
Jun 13th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.50 KB | None | 0 0
  1. -- a script to automate dune making for random map scripts
  2.  
  3. -- WILL CREATE A NEW FILE NAMED "Project LuaDune.rms"
  4. -- IF THIS FILE ALREADY EXISTS IT WILL OVERWRITE IT
  5. --
  6. -- each dune requires N elevations from its max to lowest elevation
  7. -- a dune with max height 4 can take four create_land commands, each requires a shifting land_position, shifting borders, and steadily decreasing base elevation
  8.  
  9.  
  10.  
  11. --[[    sample dune
  12.  
  13.         terrain_type DIRT2
  14.         land_position 28 28
  15.         clumping_factor 100
  16.         land_percent 1
  17.         top_border 27
  18.         left_border 27
  19.         right_border 71
  20.         bottom_border 71
  21.         base_elevation 2
  22. ]]
  23. local dirs_kv = {
  24.         ["left"] = true,
  25.         ["right"] = true,
  26.         ["up"] = true,
  27.         ["down"] = true,
  28.         ["ne"] = true, -- equivalent
  29.         ["nw"] = true,
  30.         ["se"] = true,
  31.         ["sw"] = true;
  32. }
  33.  
  34. local dirs_random = {
  35.         "left",
  36.         "right",
  37.         "up",
  38.         "down",
  39.         "ne",
  40.         "nw",
  41.         "se",
  42.         "sw";
  43. }
  44.  
  45. function create_barkhan_dune(land_pos_x, land_pos_y, min_elevation, max_elevation, wind_direction)
  46.         assert(land_pos_x and land_pos_y and min_elevation and max_elevation and wind_direction)
  47.         assert(land_pos_x <= 100 and land_pos_x >= 0)
  48.         assert(land_pos_y <= 100 and land_pos_y >= 0)
  49.         assert(land_pos_x ~= 100 and land_pos_y ~= 100, "iirc (and im probably wrong) aoe2 doesn't allow land_position 100 100, use 100 99 or 99 199 instead?")
  50.  
  51.         assert(min_elevation < max_elevation, "min_elevation should be less than max_elevation")
  52.         assert(min_elevation >= 0, "I don't know if aoe2 allows negative elevation?")
  53.         assert(max_elevation < 16, "I don't know aoe2's max_elevation, i just assumed it is 16")
  54.  
  55.         assert(dirs_kv[wind_direction],
  56.         "Must use valid wind_direction" ..
  57.         "\npossible values:" ..
  58.         "\n\tleft     \tright    \tup       \tdown" ..
  59.         "\n\tne       \tnw       \tse       \tsw"
  60.         )
  61.         local strings = {}
  62.        
  63.  
  64.         local x = land_pos_x
  65.         local y = land_pos_y
  66.  
  67.         local function get_borders(x,y)
  68.                 local top_border = y > 0 and y - 1 or 0
  69.                 local left_border = x > 0 and x - 1 or 0
  70.                 local right_border = x <= 99 and 99 - x or 0
  71.                 local bottom_border = y <= 99 and 99 - y or 0
  72.                 return top_border, left_border, right_border, bottom_border
  73.         end
  74.  
  75.         local top_border, left_border, right_border, bottom_border;
  76.  
  77.         local idx = 0;
  78.         for e = max_elevation, min_elevation, -1 do
  79.                 idx = idx + 1
  80.                 top_border,
  81.                 left_border,
  82.                 right_border,
  83.                 bottom_border = get_borders(x,y);
  84.  
  85.                 strings[idx] =    
  86.                         "\ncreate_land {" ..                
  87.                         "\n\tbase_elevation " .. e ..
  88.                         "\n\tterrain_type DESERT" ..
  89.                         "\n\tland_position " .. x .. " " .. y ..
  90.                         "\n\tclumping_factor 100" ..
  91.                         "\n\tland_percent 1" ..
  92.                         "\n\ttop_border " .. top_border ..
  93.                         "\n\tleft_border " .. left_border ..
  94.                         "\n\tright_border " .. right_border ..
  95.                         "\n\tbottom_border " .. bottom_border ..
  96.                         "\n}";
  97.  
  98.                 if wind_direction == "left" then
  99.                         x = x - 1
  100.                         y = y - 1
  101.                 elseif wind_direction == "right" then
  102.                         x = x + 1
  103.                         y = y + 1
  104.                 elseif wind_direction == "up" then
  105.                         x = x + 1
  106.                         y = y - 1
  107.                 elseif wind_direction == "down" then
  108.                         x = x - 1
  109.                         y = y + 1
  110.                 elseif wind_direction == "ne" then
  111.                        
  112.                         x = x + 1
  113.                 elseif wind_direction == "nw" then
  114.                        
  115.                         y = y - 1
  116.                 elseif wind_direction == "se" then
  117.                      
  118.                         y = y + 1
  119.                 elseif wind_direction == "sw" then
  120.                        
  121.                         x = x - 1
  122.                 end
  123.         end
  124.         return strings
  125. end
  126.  
  127.  
  128.  
  129. local coordinates = {}
  130. for x = 10, 90, 20 do
  131.         for y = 10, 90, 20 do
  132.                 table.insert(coordinates, {x,y})
  133.         end
  134. end
  135.  
  136. file = io.open("Project LuaDune.rms", "w")
  137. -- WARNING there is a limit of 99 create_land calls.. just reminding
  138. io.output(file)
  139. io.write("/* THIS FILE GENERATED BY DUNE GENERATE.LUA */\n<ELEVATION_GENERATION>\n<PLAYER_SETUP>\n<LAND_GENERATION>")
  140. math.randomseed( os.time() )
  141.  
  142. for  i = 1, 8 do
  143.         local d = dirs_random[math.random(#dirs_random)]
  144.  
  145.         local math = {random = math.random}
  146.  
  147.         local c = table.remove(coordinates, math.random(#coordinates))
  148.         local x = c[1]
  149.         local y = c[2]
  150.         local min = math.random(1,2)
  151.         local max = math.random(4,5)
  152.  
  153.         local strings = create_barkhan_dune(
  154.                 x, -- land position x
  155.                 y, -- land position y
  156.                 min,  -- min elevation
  157.                 max,  -- max elevation
  158.                 d -- random direction
  159.                 )
  160.         for j,v in ipairs(strings) do
  161.                 io.write(v)
  162.         end
  163. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement