{"action":"created","issue":{"url":"https://api.github.com/repos/wesnoth/wesnoth/issues/3195","repository_url":"https://api.github.com/repos/wesnoth/wesnoth","labels_url":"https://api.github.com/repos/wesnoth/wesnoth/issues/3195/labels{/name}","comments_url":"https://api.github.com/repos/wesnoth/wesnoth/issues/3195/comments","events_url":"https://api.github.com/repos/wesnoth/wesnoth/issues/3195/events","html_url":"https://github.com/wesnoth/wesnoth/issues/3195","id":327817352,"node_id":"MDU6SXNzdWUzMjc4MTczNTI=","number":3195,"title":"upgrade to luajit","user":{"login":"gfgtdf","id":4893136,"node_id":"MDQ6VXNlcjQ4OTMxMzY=","avatar_url":"https://avatars3.githubusercontent.com/u/4893136?v=4","gravatar_id":"","url":"https://api.github.com/users/gfgtdf","html_url":"https://github.com/gfgtdf","followers_url":"https://api.github.com/users/gfgtdf/followers","following_url":"https://api.github.com/users/gfgtdf/following{/other_user}","gists_url":"https://api.github.com/users/gfgtdf/gists{/gist_id}","starred_url":"https://api.github.com/users/gfgtdf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gfgtdf/subscriptions","organizations_url":"https://api.github.com/users/gfgtdf/orgs","repos_url":"https://api.github.com/users/gfgtdf/repos","events_url":"https://api.github.com/users/gfgtdf/events{/privacy}","received_events_url":"https://api.github.com/users/gfgtdf/received_events","type":"User","site_admin":false},"labels":[{"id":902995638,"node_id":"MDU6TGFiZWw5MDI5OTU2Mzg=","url":"https://api.github.com/repos/wesnoth/wesnoth/labels/Engine","name":"Engine","color":"d455f9","default":false},{"id":35865190,"node_id":"MDU6TGFiZWwzNTg2NTE5MA==","url":"https://api.github.com/repos/wesnoth/wesnoth/labels/Enhancement","name":"Enhancement","color":"84b6eb","default":false},{"id":597475146,"node_id":"MDU6TGFiZWw1OTc0NzUxNDY=","url":"https://api.github.com/repos/wesnoth/wesnoth/labels/Lua%20API","name":"Lua API","color":"d455f9","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":8,"created_at":"2018-05-30T17:01:13Z","updated_at":"2018-06-01T00:49:54Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"we should upgrade to luajit at some point, the current lua intepreter is just too slow, the concrete example why i want this is that i tried to port the mainline mapgen to lua. it gave the same result as the cpp mapgen but was just way to slow to actually use it . the critucal part is the generate_heightmap function:\r\n\r\n```\r\nlocal width=213\r\nlocal height=213 \r\nlocal iterations=52489 \r\nlocal hill_size=8 \r\nlocal island_size=0\r\n\r\nfunction create_array(n, a)\r\n\ta = a or 0\r\n\tlocal r={}\r\n\tfor i=1, n do\r\n\t\tr[i] = a\r\n\tend\r\n\treturn r\r\nend\r\n\r\nfunction generate_height_map(width, height, iterations, hill_size, island_size, island_off_center)\r\n\t\r\n\tlocal math_max = math.max\r\n\tlocal math_min = math.min\r\n\tlocal math_floor = math.floor\r\n\tlocal math_sqrt = math.sqrt\r\n\tlocal math_random = math.random -- use wesnoth.random in the actual mapgen code.\r\n\tlocal res = create_array(width * height)\r\n\tlocal center_x = math_floor((width + 2) / 2)\r\n\tlocal center_y = math_floor((height + 2) / 2)\r\n\tfor i=1, iterations do\r\n\t\tlocal is_valley = false\r\n\r\n\t\tlocal x1 = island_size > 0 and (center_x - island_size + math_random(island_size*2) - 1) or (math.random(width))\r\n\t\tlocal y1 = island_size > 0 and (center_y - island_size + math_random(island_size*2) - 1) or (math.random(height))\r\n\t\tlocal radius = math_random(hill_size)\r\n\r\n\t\tif island_size ~= 0 then\r\n\t\t\tlocal diffx = x1 - center_x\r\n\t\t\tlocal diffy = y1 - center_y\r\n\t\t\tlocal dist = math_floor(math_sqrt(diffx*diffx + diffy*diffy))\r\n\t\t\tis_valley = dist > island_size\t\t\t\r\n\t\tend\r\n\t\t-- std_print(\"placing hill at \" .. x1 .. \",\" .. y1 .. \" radius=\" .. radius .. \" is_valley=\" .. tostring(is_valley))\r\n\t\tlocal min_x = math_max(1, x1 - radius)\r\n\t\tlocal max_x = math_min(width + 1, x1 + radius)\r\n\t\tlocal min_y = math_max(1, y1 - radius)\r\n\t\tlocal max_y = math_min(height + 1, y1 + radius)\r\n\t\tfor x2 = min_x, max_x - 1 do\r\n\t\t\tfor y2 = min_y, max_y - 1 do\r\n\t\t\t\tlocal xdiff = (x2-x1)\r\n\t\t\t\tlocal ydiff = (y2-y1)\r\n\t\t\t\tlocal hill_height = radius - math_floor(math_sqrt(xdiff*xdiff + ydiff*ydiff))\r\n\t\t\t\tif hill_height > 0 then\r\n\t\t\t\t\tlocal old_h = res[width * (y2 - 1) + x2]\r\n\t\t\t\t\tif is_valley then\r\n\t\t\t\t\t\tres[width * (y2 - 1) + x2] = math_max(0, old_h - hill_height)\r\n\t\t\t\t\telse\r\n\t\t\t\t\t\tres[width * (y2 - 1) + x2] = old_h + hill_height\r\n\t\t\t\t\tend\r\n\t\t\t\tend\r\n\t\t\tend\r\n\t\tend\r\n\tend\r\n\r\n\t--normalize to 0-max_wanted\r\n\tlocal max_wanted = 1000\r\n\tlocal h_max = res[1]\r\n\tlocal h_min = res[1]\r\n\tfor i, h in ipairs(res) do\r\n\t\tif h > h_max then\r\n\t\t\th_max = h\r\n\t\tend\r\n\t\tif h < h_min then\r\n\t\t\th_min = h\r\n\t\tend\r\n\tend\r\n\r\n\tprint(\"h_max=\" .. h_max .. \" h_min=\" .. h_min)\r\n\tlocal norm_factor = max_wanted / (h_max - h_min)\r\n\tfor i= 1, #res  do\r\n\t\tres[i] = math_floor((res[i] - h_min) * norm_factor)\r\n\tend\r\n\r\n\treturn res\r\nend\r\n\r\ngenerate_height_map(width, height, iterations, hill_size, island_size, island_off_center)\r\n```\r\n\r\nwhere the parameters in the code above are taken form the last scenario of the popular addon 'world conquest 2'.\r\n\r\nbenchmarking gives that the corresponding c++ code runs in ~0.2 second while the wesnoth lua interpreter takes ~3.5 second to execute the code above. benchmarking with a standalone luajit.exe also gives a speed similar to the c++ code. (actually the luajit doesn't have a precise clock function to timestamp, but commandline returned instantly with the correct result when i ran this code).\r\n\r\nSo using luajit ofc not only allow use to port the mainline mapgen to lua but it would also allow umc author to implement more custom mapgens in lua (bases on the default mapgen or not).\r\n\r\nI also think that other codes in paerticular the ai code would also profit from it.\r\n\r\nit's no easy though, the obviousl issues are:\r\n1) it's unclear whether your c++/lua exception workaround woudl still work as-is and how difuculat it's be to adapt it.\r\n2) luajit is basd on lua 5.1/5.2 (but it has some of the 5.3 function aswell), some lua code that use lua 5.3 feature might give compiler erros."},"comment":{"url":"https://api.github.com/repos/wesnoth/wesnoth/issues/comments/393725976","html_url":"https://github.com/wesnoth/wesnoth/issues/3195#issuecomment-393725976","issue_url":"https://api.github.com/repos/wesnoth/wesnoth/issues/3195","id":393725976,"node_id":"MDEyOklzc3VlQ29tbWVudDM5MzcyNTk3Ng==","user":{"login":"singalen","id":151199,"node_id":"MDQ6VXNlcjE1MTE5OQ==","avatar_url":"https://avatars1.githubusercontent.com/u/151199?v=4","gravatar_id":"","url":"https://api.github.com/users/singalen","html_url":"https://github.com/singalen","followers_url":"https://api.github.com/users/singalen/followers","following_url":"https://api.github.com/users/singalen/following{/other_user}","gists_url":"https://api.github.com/users/singalen/gists{/gist_id}","starred_url":"https://api.github.com/users/singalen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/singalen/subscriptions","organizations_url":"https://api.github.com/users/singalen/orgs","repos_url":"https://api.github.com/users/singalen/repos","events_url":"https://api.github.com/users/singalen/events{/privacy}","received_events_url":"https://api.github.com/users/singalen/received_events","type":"User","site_admin":false},"created_at":"2018-06-01T00:49:54Z","updated_at":"2018-06-01T00:49:54Z","author_association":"CONTRIBUTOR","body":"[LuaJIT work on iOS, it will just do no JITting](https://news.ycombinator.com/item?id=9877311)."}}
{"actor":{"display_login":"singalen"}}