Guest User

Untitled

a guest
Feb 5th, 2021
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.69 KB | None | 0 0
  1. --get cells you're between
  2.     local top = math.floor(y)
  3.     local bottom = (top + 1)
  4.     local left = math.floor(x)
  5.     local right = (left + 1)
  6.     --get codes
  7.     local c_top_left = get_world_tile_code_raw(left, top)
  8.     local c_top_right = get_world_tile_code_raw(right, top)
  9.     local c_bottom_left = get_world_tile_code_raw(left, bottom)
  10.     local c_bottom_right = get_world_tile_code_raw(right, bottom)
  11.     if(c_top_left == c_top_right == c_bottom_left == c_bottom_right) then
  12.         --shortcut before heavy operations it's applied often enough, that it's worth of additional if.
  13.         return terrain_codes[c_top_left]
  14.     end
  15.     --calc weights
  16.     local sqrt2 = math.sqrt(2)
  17.     local w_top_left = 1 - math.sqrt((top - y)*(top - y) + (left - x)*(left - x)) / sqrt2
  18.     local w_top_right = 1 - math.sqrt((top - y)*(top - y) + (right - x)*(right - x)) / sqrt2
  19.     local w_bottom_left = 1 - math.sqrt((bottom - y)*(bottom - y) + (left - x)*(left - x)) / sqrt2
  20.     local w_bottom_right = 1 - math.sqrt((bottom - y)*(bottom - y) + (right - x)*(right - x)) / sqrt2
  21.     w_top_left = w_top_left * w_top_left + math.random() / math.max(scale / 2, 10)
  22.     w_top_right = w_top_right * w_top_right + math.random() / math.max(scale / 2, 10)
  23.     w_bottom_left = w_bottom_left * w_bottom_left + math.random() / math.max(scale / 2, 10)
  24.     w_bottom_right = w_bottom_right * w_bottom_right + math.random() / math.max(scale / 2, 10)
  25.     --calculate total weights for codes
  26.     local totals = {}
  27.     add_to_total(totals, w_top_left, c_top_left)
  28.     add_to_total(totals, w_top_right, c_top_right)
  29.     add_to_total(totals, w_bottom_left, c_bottom_left)
  30.     add_to_total(totals, w_bottom_right, c_bottom_right)
Advertisement
Add Comment
Please, Sign In to add comment