diff --git a/.github/workflows/autofix.yml b/.github/workflows/autofix.yml index 680daf2fde0e..560f7391f95d 100644 --- a/.github/workflows/autofix.yml +++ b/.github/workflows/autofix.yml @@ -25,10 +25,13 @@ jobs: - name: format C++ files run: make astyle - - name: format markdown and typescript files + - name: format Markdown and TypeScript files run: deno fmt - - name: json formatting + - name: format Lua files + run: deno task dprint fmt + + - name: format JSON run: make style-all-json-parallel RELEASE=1 - uses: autofix-ci/action@bee19d72e71787c12ca0f29de72f2833e437e4c9 diff --git a/data/mods/saveload_lua_test/main.lua b/data/mods/saveload_lua_test/main.lua index e97c53ea9566..94deabd0e983 100644 --- a/data/mods/saveload_lua_test/main.lua +++ b/data/mods/saveload_lua_test/main.lua @@ -1,13 +1,14 @@ gdebug.log_info("SLT: main") -local mod = game.mod_runtime[ game.current_mod ] -local storage = game.mod_storage[ game.current_mod ] +local mod = game.mod_runtime[game.current_mod] +local storage = game.mod_storage[game.current_mod] --[[ If we keep all our data simple and in mod.storage, we won't even have to register save/load hooks, it'll be saved/loaded automatically. -]]-- +]] +-- mod.storage = storage --[[ @@ -19,33 +20,35 @@ storage.num = 12.3 -- Usertype storage.tri = Tripoint.new(3, 4, 5) -- String -storage.tri_as_str = tostring( Tripoint.new(3, 4, 5) ) +storage.tri_as_str = tostring(Tripoint.new(3, 4, 5)) --[[ If we want to build complex state out of loaded data we may create a hook that would read loaded data from mod_storage and create our complex state in the mod_runtime. -]]-- +]] +-- mod.on_game_load_hook = function() - gdebug.log_info("SLT: on_load") - - if storage.num then - gdebug.log_info( "Data found! num = ", storage.num ) - end - if storage.tri then - gdebug.log_info( "Data found! tri = ", storage.tri ) - end + gdebug.log_info("SLT: on_load") + + if storage.num then + gdebug.log_info("Data found! num = ", storage.num) + end + if storage.tri then + gdebug.log_info("Data found! tri = ", storage.tri) + end end --[[ If we have complex enough state (e.g. recursive tables, or with custom metatables) we may create a hook that would write a simplified version into mod_storage, so the hardcoded JSON serializer would be able to handle it. -]]-- +]] +-- mod.on_game_save_hook = function() - gdebug.log_info("SLT: on_save") + gdebug.log_info("SLT: on_save") - if storage.num then - gdebug.log_info("Saving NUM value = ", storage.num) - end + if storage.num then + gdebug.log_info("Saving NUM value = ", storage.num) + end end diff --git a/data/mods/saveload_lua_test/preload.lua b/data/mods/saveload_lua_test/preload.lua index fa895d2770a1..d72681d46b09 100644 --- a/data/mods/saveload_lua_test/preload.lua +++ b/data/mods/saveload_lua_test/preload.lua @@ -1,11 +1,11 @@ gdebug.log_info("SLT: preload") -local mod = game.mod_runtime[ game.current_mod ] +local mod = game.mod_runtime[game.current_mod] -game.hooks.on_game_load[ #game.hooks.on_game_load + 1 ] = function( ... ) - return mod.on_game_load_hook( ... ) +game.hooks.on_game_load[#game.hooks.on_game_load + 1] = function(...) + return mod.on_game_load_hook(...) end -game.hooks.on_game_save[ #game.hooks.on_game_save + 1 ] = function( ... ) - return mod.on_game_save_hook( ... ) +game.hooks.on_game_save[#game.hooks.on_game_save + 1] = function(...) + return mod.on_game_save_hook(...) end diff --git a/data/mods/smart_house_remotes/main.lua b/data/mods/smart_house_remotes/main.lua index 8d675e25b3d2..493add2a1186 100644 --- a/data/mods/smart_house_remotes/main.lua +++ b/data/mods/smart_house_remotes/main.lua @@ -5,9 +5,10 @@ gdebug.log_info("SHR: main.") This file can be loaded multiple times (e.g. when you press a key to hot-reload Lua code), so ideally we shouldn't modify here any state defined in earlier load stages. -]]-- +]] +-- -local mod = game.mod_runtime[ game.current_mod ] +local mod = game.mod_runtime[game.current_mod] --[[ When we export Lua function, Lua is smart enough not to garbage collect @@ -32,132 +33,132 @@ mod.remote_wireless_range = 24 -- Range of remote, vertical mod.remote_wireless_range_z = 2 --- Get abs omt of remote's base -mod.get_remote_base_omt = function( item ) - return item:get_var_tri( mod.var_base, Tripoint.new(0,0,0) ) +-- Get abs omt of remote's base +mod.get_remote_base_omt = function(item) + return item:get_var_tri(mod.var_base, Tripoint.new(0, 0, 0)) end -- Get abs ms of remote's base -mod.get_remote_base_abs_ms = function( item ) - local p_omt = mod.get_remote_base_omt( item ) - return coords.omt_to_ms( p_omt ) + Point.new( const.OMT_MS_SIZE // 2, const.OMT_MS_SIZE // 2 ) +mod.get_remote_base_abs_ms = function(item) + local p_omt = mod.get_remote_base_omt(item) + return coords.omt_to_ms(p_omt) + Point.new(const.OMT_MS_SIZE // 2, const.OMT_MS_SIZE // 2) end -- Set remote's base abs omt -mod.set_remote_base = function( item, p_omt ) - item:set_var_tri(mod.var_base, p_omt) +mod.set_remote_base = function(item, p_omt) + item:set_var_tri(mod.var_base, p_omt) end -- Look for spawned remotes and bind them to given omt -mod.on_mapgen_postprocess_hook = function( map, p_omt, when ) - local mapsize = map:get_map_size() - local item_id = mod.item_id - for y = 0, mapsize-1 do - for x = 0,mapsize-1 do - local p = Tripoint.new(x, y, 0) - -- TODO: Check whether using has_items_at() gives a speedup in Lua. - -- In C++, it's supposed to be faster then !i_at( p ).empty() - if map:has_items_at( p ) then - local items = map:get_items_at( p ):as_item_stack() - for _, item in pairs(items) do - if item:get_type():str() == item_id then - mod.set_remote_base( item, p_omt ) - end - end - end +mod.on_mapgen_postprocess_hook = function(map, p_omt, when) + local mapsize = map:get_map_size() + local item_id = mod.item_id + for y = 0, mapsize - 1 do + for x = 0, mapsize - 1 do + local p = Tripoint.new(x, y, 0) + -- TODO: Check whether using has_items_at() gives a speedup in Lua. + -- In C++, it's supposed to be faster then !i_at( p ).empty() + if map:has_items_at(p) then + local items = map:get_items_at(p):as_item_stack() + for _, item in pairs(items) do + if item:get_type():str() == item_id then + mod.set_remote_base(item, p_omt) + end end + end end + end end -- List of terrain transformations supported by the mod mod.get_transform_list = function() - return { - { - -- terrain id when open - o = 't_window_domestic', - -- terrain id when closed - c = 't_curtains', - -- terrain id that belongs to this group, but can't be closed/opened - -- TODO: some 'inert' tiles can belong to multiple transforms - i = { - 't_window', - 't_window_frame', - 't_window_open', - 't_window_empty', - 't_window_no_curtains', - 't_window_no_curtains_open' - }, - -- action name in UI - name_open = locale.gettext('Open curtains'), - name_close = locale.gettext('Close curtains'), - -- power required to open or close - power = 5, - }, - { - -- TODO: garage doors should check whether there's something obstructing them. - o = 't_door_metal_locked_o', - c = 't_door_metal_locked', - name_open = locale.gettext('Raise garage door'), - name_close = locale.gettext('Lower garage door'), - power = 20, - } - } + return { + { + -- terrain id when open + o = "t_window_domestic", + -- terrain id when closed + c = "t_curtains", + -- terrain id that belongs to this group, but can't be closed/opened + -- TODO: some 'inert' tiles can belong to multiple transforms + i = { + "t_window", + "t_window_frame", + "t_window_open", + "t_window_empty", + "t_window_no_curtains", + "t_window_no_curtains_open", + }, + -- action name in UI + name_open = locale.gettext("Open curtains"), + name_close = locale.gettext("Close curtains"), + -- power required to open or close + power = 5, + }, + { + -- TODO: garage doors should check whether there's something obstructing them. + o = "t_door_metal_locked_o", + c = "t_door_metal_locked", + name_open = locale.gettext("Raise garage door"), + name_close = locale.gettext("Lower garage door"), + power = 20, + }, + } end -- Caches transforms list for lookups mod.cache_transforms = function(tlist) - local to_close_list = {} - local to_open_list = {} - local idx = 0 - for _,v in pairs(tlist) do - idx = idx + 1 - to_open_list[v.c] = idx - to_close_list[v.o] = idx - end - return to_close_list, to_open_list + local to_close_list = {} + local to_open_list = {} + local idx = 0 + for _, v in pairs(tlist) do + idx = idx + 1 + to_open_list[v.c] = idx + to_close_list[v.o] = idx + end + return to_close_list, to_open_list end -- These 2 functions use breadth-first search to find all tiles belonging to given block -mod.get_neighbours_at = function( opts, block, p ) - local k = tostring(p) - local v = opts[k] - if v ~= nil and v.idx == block.idx then - opts[k] = nil - block.points[#block.points + 1] = p - if v.can_open then - block.can_open_num = block.can_open_num + 1 - end - if v.can_close then - block.can_close_num = block.can_close_num + 1 - end - mod.get_neighbours( opts, block, p ) +mod.get_neighbours_at = function(opts, block, p) + local k = tostring(p) + local v = opts[k] + if v ~= nil and v.idx == block.idx then + opts[k] = nil + block.points[#block.points + 1] = p + if v.can_open then + block.can_open_num = block.can_open_num + 1 end + if v.can_close then + block.can_close_num = block.can_close_num + 1 + end + mod.get_neighbours(opts, block, p) + end end -mod.get_neighbours = function( opts, block, p ) - mod.get_neighbours_at( opts, block, p + Tripoint.new(1, 0, 0) ) - mod.get_neighbours_at( opts, block, p + Tripoint.new(-1, 0, 0) ) - mod.get_neighbours_at( opts, block, p + Tripoint.new(0, 1, 0) ) - mod.get_neighbours_at( opts, block, p + Tripoint.new(0, -1, 0) ) +mod.get_neighbours = function(opts, block, p) + mod.get_neighbours_at(opts, block, p + Tripoint.new(1, 0, 0)) + mod.get_neighbours_at(opts, block, p + Tripoint.new(-1, 0, 0)) + mod.get_neighbours_at(opts, block, p + Tripoint.new(0, 1, 0)) + mod.get_neighbours_at(opts, block, p + Tripoint.new(0, -1, 0)) end -- Helper func to check whether value is in array -local find_in_array = function( arr, val ) - for k, v in pairs( arr ) do - if v == val then - return k - end +local find_in_array = function(arr, val) + for k, v in pairs(arr) do + if v == val then + return k end - return nil + end + return nil end -- Helper func to check whether tile is considered as inert for one of the blocks -local check_is_tile_inert = function( tlist, val ) - for idx, transform in pairs(tlist) do - if transform.i and find_in_array( transform.i, val ) then - return idx - end +local check_is_tile_inert = function(tlist, val) + for idx, transform in pairs(tlist) do + if transform.i and find_in_array(transform.i, val) then + return idx end - return nil + end + return nil end --[[ @@ -171,158 +172,159 @@ end can_open = bool, --Whether block can be opened can_close = bool, --Whether block can be closed } -]]-- -mod.build_target_list = function( map, pos_omt ) - -- First, find all tiles that can be opened, closed or belong to 'inert' group - local act_tiles = {} - local tlist = mod.get_transform_list() - local to_close_list, to_open_list = mod.cache_transforms(tlist) - local p_zero = map:get_local_ms( coords.omt_to_ms( pos_omt ) ) - local iter_max = const.OMT_MS_SIZE - 1 - for y = 0, iter_max do - for x = 0, iter_max do - local p = p_zero + Tripoint.new(x, y, 0) - local t = map:get_ter_at( p ):str_id() - - local idx_found = to_close_list[t:str()] - local can_open = false - local can_close = false - if idx_found then - can_close = true - else - idx_found = to_open_list[t:str()] - if idx_found then - can_open = true - else - idx_found = check_is_tile_inert( tlist, t:str() ) - end - end - - if idx_found then - act_tiles[tostring(p)] = { p = p, idx = idx_found, can_open = can_open, can_close = can_close } - end +]] +-- +mod.build_target_list = function(map, pos_omt) + -- First, find all tiles that can be opened, closed or belong to 'inert' group + local act_tiles = {} + local tlist = mod.get_transform_list() + local to_close_list, to_open_list = mod.cache_transforms(tlist) + local p_zero = map:get_local_ms(coords.omt_to_ms(pos_omt)) + local iter_max = const.OMT_MS_SIZE - 1 + for y = 0, iter_max do + for x = 0, iter_max do + local p = p_zero + Tripoint.new(x, y, 0) + local t = map:get_ter_at(p):str_id() + + local idx_found = to_close_list[t:str()] + local can_open = false + local can_close = false + if idx_found then + can_close = true + else + idx_found = to_open_list[t:str()] + if idx_found then + can_open = true + else + idx_found = check_is_tile_inert(tlist, t:str()) end - end + end - -- Use breadth-first to sort tiles into multitile blocks - local ret = {} - - local next = next - -- While (table has entries) do - while next(act_tiles) ~= nil do - -- Remove first available entry from table - local k, v = next(act_tiles) - act_tiles[k] = nil - - -- Build block of neighboring tiles with same idx - local block = { - points = { v.p }, - idx = v.idx, - can_open_num = v.can_open and 1 or 0, - can_close_num = v.can_close and 1 or 0 - } - mod.get_neighbours( act_tiles, block, v.p ) - block.can_open = block.can_open_num > 0 - block.can_close = block.can_close_num > 0 - - -- Add block to list - ret[#ret + 1] = block + if idx_found then + act_tiles[tostring(p)] = { p = p, idx = idx_found, can_open = can_open, can_close = can_close } + end end + end + + -- Use breadth-first to sort tiles into multitile blocks + local ret = {} + + local next = next + -- While (table has entries) do + while next(act_tiles) ~= nil do + -- Remove first available entry from table + local k, v = next(act_tiles) + act_tiles[k] = nil + + -- Build block of neighboring tiles with same idx + local block = { + points = { v.p }, + idx = v.idx, + can_open_num = v.can_open and 1 or 0, + can_close_num = v.can_close and 1 or 0, + } + mod.get_neighbours(act_tiles, block, v.p) + block.can_open = block.can_open_num > 0 + block.can_close = block.can_close_num > 0 - return ret + -- Add block to list + ret[#ret + 1] = block + end + + return ret end -- Close all closable tiles in block -mod.block_close = function( block, transform ) - for _, p in pairs(block.points) do - local ter_at_p = gapi.get_map():get_ter_at( p ):str_id() - if ter_at_p:str() == transform.o then - gapi.get_map():set_ter_at( p, TerId.new( transform.c ):int_id() ) - end +mod.block_close = function(block, transform) + for _, p in pairs(block.points) do + local ter_at_p = gapi.get_map():get_ter_at(p):str_id() + if ter_at_p:str() == transform.o then + gapi.get_map():set_ter_at(p, TerId.new(transform.c):int_id()) end + end end -- Open all openable tiles in block -mod.block_open = function( block, transform ) - for _, p in pairs(block.points) do - local ter_at_p = gapi.get_map():get_ter_at( p ):str_id() - if ter_at_p:str() == transform.c then - gapi.get_map():set_ter_at( p, TerId.new( transform.o ):int_id() ) - end +mod.block_open = function(block, transform) + for _, p in pairs(block.points) do + local ter_at_p = gapi.get_map():get_ter_at(p):str_id() + if ter_at_p:str() == transform.c then + gapi.get_map():set_ter_at(p, TerId.new(transform.o):int_id()) end + end end -- Show 'not enough power' error mod.show_low_power_error = function() - local pp = QueryPopup.new() - --~ Message on the remote, stylized as calculator led display. - --~ Shown when there's not enough grid charge. - pp:message(locale.gettext("Low Current At Endpoint")) - -- This color is awful, but it's a cheap LCD display, what did you expect? - pp:message_color( Color.i_green ) - pp:allow_any_key( true ) - pp:query() + local pp = QueryPopup.new() + --~ Message on the remote, stylized as calculator led display. + --~ Shown when there's not enough grid charge. + pp:message(locale.gettext("Low Current At Endpoint")) + -- This color is awful, but it's a cheap LCD display, what did you expect? + pp:message_color(Color.i_green) + pp:allow_any_key(true) + pp:query() end -- Show 'no signal' error mod.show_no_signal_error = function() - local pp = QueryPopup.new() - --~ Message on the remote, stylized as calculator led display. - --~ Shown when player is too far away from the area. - pp:message(locale.gettext("No Signal")) - pp:message_color( Color.i_green ) - pp:allow_any_key( true ) - pp:query() + local pp = QueryPopup.new() + --~ Message on the remote, stylized as calculator led display. + --~ Shown when player is too far away from the area. + pp:message(locale.gettext("No Signal")) + pp:message_color(Color.i_green) + pp:allow_any_key(true) + pp:query() end -- Show 'no valid blocks' error mod.show_no_endpoints_error = function() - local pp = QueryPopup.new() - --~ Message on the remote, stylized as calculator led display. - --~ Shown when there's nothing to activate. - pp:message(locale.gettext("No Endpoints Available")) - pp:message_color( Color.i_green ) - pp:allow_any_key( true ) - pp:query() + local pp = QueryPopup.new() + --~ Message on the remote, stylized as calculator led display. + --~ Shown when there's nothing to activate. + pp:message(locale.gettext("No Endpoints Available")) + pp:message_color(Color.i_green) + pp:allow_any_key(true) + pp:query() end -- Add message indicating the remote works mod.show_msg_remote_working = function() - gapi.add_msg(locale.gettext("The remote beeps quietly.")) + gapi.add_msg(locale.gettext("The remote beeps quietly.")) end -- Open or close all tiles in block -mod.invoke_block = function( block, grid ) - local tlist = mod.get_transform_list() - local transform = tlist[ block.idx ] - local power_available = grid:get_resource( true ) - - if block.can_open then - local power_needed = transform.power * block.can_open_num - if power_needed > power_available then - mod.show_low_power_error() - return 0 - end - grid:mod_resource( -power_needed, true ) - mod.block_open( block, transform ) - elseif block.can_close then - local power_needed = transform.power * block.can_close_num - if power_needed > power_available then - mod.show_low_power_error() - return 0 - end - grid:mod_resource( -power_needed, true ) - mod.block_close( block, transform ) +mod.invoke_block = function(block, grid) + local tlist = mod.get_transform_list() + local transform = tlist[block.idx] + local power_available = grid:get_resource(true) + + if block.can_open then + local power_needed = transform.power * block.can_open_num + if power_needed > power_available then + mod.show_low_power_error() + return 0 + end + grid:mod_resource(-power_needed, true) + mod.block_open(block, transform) + elseif block.can_close then + local power_needed = transform.power * block.can_close_num + if power_needed > power_available then + mod.show_low_power_error() + return 0 end - return 1 + grid:mod_resource(-power_needed, true) + mod.block_close(block, transform) + end + return 1 end -- Main iuse function. Returns amount of charges consumed from item. -mod.iuse_function = function( who, item, pos ) - local user_pos = gapi.get_map():get_abs_ms( pos ) +mod.iuse_function = function(who, item, pos) + local user_pos = gapi.get_map():get_abs_ms(pos) - -- Uncomment this so on activation the remote reconfigures itself to work in user's omt - --[[ + -- Uncomment this so on activation the remote reconfigures itself to work in user's omt + --[[ mod.set_remote_base( item, coords.ms_to_omt( user_pos ) ) gapi.add_msg(locale.gettext("Remote reconfigured!")) --if true then @@ -330,78 +332,80 @@ mod.iuse_function = function( who, item, pos ) --end ]] - local base_pos = mod.get_remote_base_abs_ms( item ) - - -- Check distance to wireless base the remote is bound to. - -- The base does not physically exist in game world, but we imagine - -- it's tucked away into a hoouse wall or something. - if math.abs( user_pos.z - base_pos.z ) > mod.remote_wireless_range_z or - coords.rl_dist( user_pos, base_pos ) > mod.remote_wireless_range then - mod.show_no_signal_error() - return 0 - end - - local base_pos_omt = mod.get_remote_base_omt( item ) - local grid = gapi.get_distribution_grid_tracker():get_grid_at_abs_ms( base_pos ); - local power_available = grid:get_resource( true ) - - -- If house has no power, the wireless base also has no power and can't emit signal. - if power_available == 0 then - mod.show_no_signal_error() - return 0 - end - - -- Get list of all available multitile formations in base's omt - local targets = mod.build_target_list( gapi.get_map(), base_pos_omt ) - - -- Ignore ones that are completely inert (e.g. window got all its curtains torn down). - local sel_list = {} - local transforms = mod.get_transform_list() - for block_idx, block in pairs(targets) do - if block.can_open then - sel_list[#sel_list + 1] = { - block = block, - do_open = true, - text = transforms[block.idx].name_open.." #"..tostring(block_idx) - } - elseif block.can_close then - sel_list[#sel_list + 1] = { - block = block, - do_open = false, - text = transforms[block.idx].name_close.." #"..tostring(block_idx) - } - end - end - - -- No available blocks? Too bad. - if next(sel_list) == nil then - mod.show_no_endpoints_error() - return 0 - end - - -- If only 1 is available, don't ask - if #sel_list == 1 then - mod.show_msg_remote_working() - return mod.invoke_block( sel_list[1].block, grid ) + local base_pos = mod.get_remote_base_abs_ms(item) + + -- Check distance to wireless base the remote is bound to. + -- The base does not physically exist in game world, but we imagine + -- it's tucked away into a hoouse wall or something. + if + math.abs(user_pos.z - base_pos.z) > mod.remote_wireless_range_z + or coords.rl_dist(user_pos, base_pos) > mod.remote_wireless_range + then + mod.show_no_signal_error() + return 0 + end + + local base_pos_omt = mod.get_remote_base_omt(item) + local grid = gapi.get_distribution_grid_tracker():get_grid_at_abs_ms(base_pos) + local power_available = grid:get_resource(true) + + -- If house has no power, the wireless base also has no power and can't emit signal. + if power_available == 0 then + mod.show_no_signal_error() + return 0 + end + + -- Get list of all available multitile formations in base's omt + local targets = mod.build_target_list(gapi.get_map(), base_pos_omt) + + -- Ignore ones that are completely inert (e.g. window got all its curtains torn down). + local sel_list = {} + local transforms = mod.get_transform_list() + for block_idx, block in pairs(targets) do + if block.can_open then + sel_list[#sel_list + 1] = { + block = block, + do_open = true, + text = transforms[block.idx].name_open .. " #" .. tostring(block_idx), + } + elseif block.can_close then + sel_list[#sel_list + 1] = { + block = block, + do_open = false, + text = transforms[block.idx].name_close .. " #" .. tostring(block_idx), + } end + end - -- Query which block to activate - local ui = UiList.new() - --~ Title of the menu prompting player to select what to activate with the remote. - --~ Typically it's doors, garage doors or window curtains. - ui:title(locale.gettext("Select endpoint")) - for i = 1, #sel_list do - ui:add( i, sel_list[i].text ) - end - local eidx = ui:query() + -- No available blocks? Too bad. + if next(sel_list) == nil then + mod.show_no_endpoints_error() + return 0 + end - -- Canceled by player - if eidx < 1 then - gapi.add_msg(locale.gettext("Nevermind.")) - return 0 - end - - -- Activate desired block + -- If only 1 is available, don't ask + if #sel_list == 1 then mod.show_msg_remote_working() - return mod.invoke_block( sel_list[eidx].block, grid ) + return mod.invoke_block(sel_list[1].block, grid) + end + + -- Query which block to activate + local ui = UiList.new() + --~ Title of the menu prompting player to select what to activate with the remote. + --~ Typically it's doors, garage doors or window curtains. + ui:title(locale.gettext("Select endpoint")) + for i = 1, #sel_list do + ui:add(i, sel_list[i].text) + end + local eidx = ui:query() + + -- Canceled by player + if eidx < 1 then + gapi.add_msg(locale.gettext("Nevermind.")) + return 0 + end + + -- Activate desired block + mod.show_msg_remote_working() + return mod.invoke_block(sel_list[eidx].block, grid) end diff --git a/data/mods/smart_house_remotes/preload.lua b/data/mods/smart_house_remotes/preload.lua index ba915e074821..94a984e27895 100644 --- a/data/mods/smart_house_remotes/preload.lua +++ b/data/mods/smart_house_remotes/preload.lua @@ -3,14 +3,14 @@ gdebug.log_info("SHR: preload.") -- Have to register iuse before data loading. -- Actual implementation (function mod.iuse_function) will be defined later. -local mod = game.mod_runtime[ game.current_mod ] +local mod = game.mod_runtime[game.current_mod] -- Register our map post-process hook -game.hooks.on_mapgen_postprocess[ #game.hooks.on_mapgen_postprocess + 1 ] = function(...) - return mod.on_mapgen_postprocess_hook(...) +game.hooks.on_mapgen_postprocess[#game.hooks.on_mapgen_postprocess + 1] = function(...) + return mod.on_mapgen_postprocess_hook(...) end -- Register our item use function -game.iuse_functions[ "SMART_HOUSE_REMOTE" ] = function(...) - return mod.iuse_function(...) +game.iuse_functions["SMART_HOUSE_REMOTE"] = function(...) + return mod.iuse_function(...) end diff --git a/data/mods/teleportation_mod/main.lua b/data/mods/teleportation_mod/main.lua index 7e3928f71be8..e169cf243a54 100644 --- a/data/mods/teleportation_mod/main.lua +++ b/data/mods/teleportation_mod/main.lua @@ -1,685 +1,675 @@ -gdebug.log_info("Teleporter: main") - ---[[ CONFIG ]]-- - --- how many energy units a station gets per hour, default = 1, 1 charge teleports you one tile -charge_multiplier = 1 --- how much power in kj is needed for 1 teleport energy unit when charging from grid -power_charge_kj_multiplier = 1000 - -local mod = game.mod_runtime[ game.current_mod ] -local storage = game.mod_storage[ game.current_mod ] - ---Item id (static) - -mod.storage = storage - --- Variable ids - --- Table of anchor positions -mod.anchor_list = {} --- Table of station tables - [1] = time, [2] = charge, [3] = position(omt) -mod.station_list = {} --- saved variables... these are separate from the active variables so I didn't mess with the saved tables constantly... also less chances of saves breaking on alt-f4, not that you'd do that ;) -storage.anchor_omt = {} -storage.station_placement_time = {} -storage.station_charge = {} -storage.station_pos = {} --- also the default values -storage.charge_multiplier = 1 -storage.power_charge_kj_multiplier = 1000 --- NOTE: due to an engine bug, the saved table keys need to be strings, not ints, that's why there's a mishmash of keys (tostring and tonumber functions used)... while this will be fixed in the foreseeable future (ty Olanti), there's no need to change this in the mod - everything should still work - - --- helper function for counting tables with non-integer keys, #table does not work for those -mod.count_table = function(save_table) - local count = 0 - for _, _ in pairs(save_table) do - count = count + 1 - end - return count -end - --- LOADING - -mod.load_saved_anchors = function () - if mod.count_table(storage.anchor_omt) == 0 then - print("Data not loaded - no anchors are placed.") - else - for i in pairs(storage.anchor_omt) do - mod.anchor_list[tonumber(i)] = storage.anchor_omt[i] - print( "Data found! anchor = ".. tostring(mod.anchor_list[tonumber(i)]) ) - end - end -end - -mod.load_saved_station_charge = function() - - if mod.count_table(storage.station_charge) == 0 then - print("Data not loaded - no stations are placed. (charge)") - else - for i in pairs(storage.station_charge) do - mod.station_list[tonumber(i)] = {} - mod.station_list[tonumber(i)][2] = storage.station_charge[i] - print( "Data found! station charge = ".. tostring(mod.station_list[tonumber(i)][2]) ) - end - end -end - -mod.load_saved_station_placement_time = function() - - if mod.count_table(storage.station_placement_time) == 0 then - print("Data not loaded - no stations are placed. (time)") - else - for i in pairs(storage.station_placement_time) do - mod.station_list[tonumber(i)][1] = storage.station_placement_time[i] - print( "Data found! station time = ".. tostring(mod.station_list[tonumber(i)][1]) ) - end - end -end - -mod.load_saved_station_pos = function() - - if mod.count_table(storage.station_pos) == 0 then - print("Data not loaded - no stations are placed. (pos)") - else - for i in pairs(storage.station_pos) do - mod.station_list[tonumber(i)][3] = storage.station_pos[i] - print( "Data found! station pos = ".. tostring(mod.station_list[tonumber(i)][3]) ) - end - end -end - -mod.on_game_load_hook = function() - - gdebug.log_info("Teleporters: on_load") - - mod.load_saved_anchors() - - -- order below is important, since the data tables on load are initialized in the mod.load_saved_station_charge() function - - mod.load_saved_station_charge() - mod.load_saved_station_placement_time() - mod.load_saved_station_pos() - charge_multiplier = storage.charge_multiplier - power_charge_kj_multiplier = storage.power_charge_kj_multiplier -end - --- SAVING - -mod.save_station_charge = function(num) - storage.station_charge[tostring(num)] = mod.station_list[num][2] - print("Saved charge to list".. tostring(mod.station_list[num][2])) -end - -mod.save_station_placement_time = function(num) - storage.station_placement_time[tostring(num)] = mod.station_list[num][1] - print("Saved time to list".. tostring(mod.station_list[num][1])) -end - -mod.save_station_pos = function(num) - storage.station_pos[tostring(num)] = mod.station_list[num][3] - print("Saved pos to list".. tostring(mod.station_list[num][3])) -end - -mod.save_anchor_omt = function() - for i in pairs(mod.anchor_list) do - storage.anchor_omt[tostring(i)] = mod.anchor_list[i] - print("Saved to anchor list".. tostring(mod.anchor_list[i])) - end -end - -mod.on_game_save_hook = function() - - gdebug.log_info("Teleporters: on_save") - - mod.save_anchor_omt() - - mod.update_station_charge() - - for i in pairs(mod.station_list) do - mod.save_station_charge(i) - mod.save_station_placement_time(i) - mod.save_station_pos(i) - end - -end - --- main function - -mod.add_anchor_to_list = function (pos) - --print("adding to list") - --print(pos) - mod.anchor_list[#mod.anchor_list+1] = pos - --print(#mod.anchor_list, pos) - mod.save_anchor_omt() -end - - - -mod.iuse_function_anchor = function (who, item, pos) - - local a = {"teleporter_anchor_deployed"} - - local player_abs_pos = gapi.get_map():get_abs_ms( pos ) - --print(player_abs_pos) - - local player_map_pos = gapi.get_map():get_local_ms(player_abs_pos) - --print(player_map_pos) - local player_omt = coords.ms_to_omt(player_abs_pos) - - local no_furn = gapi.get_map():get_furn_at(player_map_pos) - b = tostring(no_furn) - --print(b) - anchor_omt = tostring(player_omt) - - if b == "FurnIntId[0][f_null]" then - --print (FurnId.new("teleporter_anchor_deployed"):int_id()) - gapi.get_map():set_furn_at( player_map_pos, FurnId.new(a[1]):int_id() ) - - - - mod.add_anchor_to_list(player_omt) - - gapi.add_msg("The teleporter anchor was placed at ") - gapi.add_msg(anchor_omt) - else - gapi.add_msg("Can only be placed on a square with no existing furniture.") - return 0 - end - return 1 - -end - -mod.add_station_to_list = function (pos) - local num = #mod.station_list + 1 - --print(num) - local turn = gapi.current_turn() - local charge = 0 - - mod.station_list[num] = {} - mod.station_list[num][1] = turn - mod.station_list[num][2] = charge - mod.station_list[num][3] = pos - - mod.save_station_placement_time(num) - mod.save_station_charge(num) - mod.save_station_pos(num) - -end - -mod.iuse_function_station = function (who, item, pos) - - local a = {"teleporter_station_deployed"} - local player_abs_pos = gapi.get_map():get_abs_ms( pos ) - --print(player_abs_pos) - - local player_map_pos = gapi.get_map():get_local_ms(player_abs_pos) - --print(player_map_pos) - - local player_omt = coords.ms_to_omt(player_abs_pos) - --print(player_omt) - - local no_furn = gapi.get_map():get_furn_at(player_map_pos) - b = tostring(no_furn) - --print(b) - station_omt = tostring(player_omt) - - if b == "FurnIntId[0][f_null]" then - gapi.get_map():set_furn_at( player_map_pos, FurnId.new(a[1]):int_id() ) - mod.add_station_to_list(player_omt) - else - gapi.add_msg("Can only be placed on a square with no existing furniture.") - return 0 - end - - return 1 - --end - -end - -mod.update_station_charge = function() - -- go through station list, compare times, change charge depending on time passed - if mod.count_table(mod.station_list) > 0 then - for i in pairs(mod.station_list) do - local station_time = mod.station_list[i][1] - local current_time = gapi.current_turn() - local time_difference_TimeDuration = TimePoint.__sub(current_time, station_time) - local time_difference = TimeDuration.to_seconds(time_difference_TimeDuration) - --print (time_difference) - if time_difference > 60 then - local added_charge = time_difference * charge_multiplier / 3600 - mod.station_list[i][2] = mod.station_list[i][2] + added_charge - mod.station_list[i][1] = current_time - end - end - else - print("No stations in network!") - end -end - -mod.do_station_charge = function (choose, grid, power_available, chosen_station_list) - local available_units = (tonumber(power_available) / power_charge_kj_multiplier) - --print(available_units) - if available_units < 1 then - gapi.add_msg(locale.gettext("Power too low for charging.")) - return 0 - end - local uic = UiList.new() - uic:title(locale.gettext("How much power do you want to charge the station with?")) - for i = 1, available_units do - uic:add(i, tostring(i)) - end - local pick = uic:query() - if pick < 1 then - gapi.add_msg(locale.gettext("Nevermind.")) - return 0 - else - --print(mod.station_list[chosen_station_list[choose]][2]) - mod.station_list[chosen_station_list[choose]][2] = mod.station_list[chosen_station_list[choose]][2] + pick - --print(mod.station_list[chosen_station_list[choose]][2]) - grid:mod_resource( -(pick * power_charge_kj_multiplier), true ) - gapi.add_msg(locale.gettext("The station was charged.")) - return 1 - end - -end - -mod.charge_stations_from_grid = function (pos) - - local abs_pos = gapi.get_map():get_abs_ms( pos ) - local abs_omt = coords.ms_to_omt(abs_pos) - local grid = gapi.get_distribution_grid_tracker():get_grid_at_abs_ms(abs_pos); - local power_available = grid:get_resource( true ) - local chosen_station_list = {} - - if power_available == 0 then - gapi.add_msg(locale.gettext("No grid or no power in grid")) - return 0 - end - if power_available > 0 then - local ui_charge = UiList.new() - ui_charge:title(locale.gettext("Charge which station?")) - local j = 1 - for i in pairs(mod.station_list) do - if mod.station_list[i][3] == abs_omt then - local a = tostring(j).. "Station with charge ".. tostring(mod.station_list[i][2]) - ui_charge:add( j, a ) - chosen_station_list[j] = i - j = j + 1 - end - if i == mod.count_table(mod.station_list) then - if j == 1 then - gapi.add_msg(locale.gettext("No stations found on your tile.")) - return 0 - end - end - end - local choose = ui_charge:query() - -- Canceled by player - if choose < 1 then - gapi.add_msg(locale.gettext("Nevermind.")) - return 0 - end - return mod.do_station_charge(choose, grid, power_available, chosen_station_list) - end -end - -mod.teleport_to_target = function(who, anchor, distance, teleporter_list_key, picked_teleporter) - local a = teleporter_list_key[picked_teleporter] - print(a) - local removed_charge = -distance - mod.station_list[a][2] = mod.station_list[a][2] + removed_charge - - gapi.add_msg("Teleporting Player to ".. tostring(anchor)) - gapi.add_msg("Station at: ".. tostring(mod.station_list[teleporter_list_key[picked_teleporter]][3]).. "now has ".. mod.station_list[teleporter_list_key[picked_teleporter]][2].. " units of charge left") - --need exposed debug teleport for this to function - gapi.place_player_overmap_at(anchor) - return 1 -end - -mod.pick_teleporter = function (who, eidx, pos) - local abs_pos = gapi.get_map():get_abs_ms( pos ) - local abs_omt = coords.ms_to_omt(abs_pos) - local anchor = mod.anchor_list[eidx] - local distance = coords.rl_dist(abs_omt, anchor) - local teleporter_list_key = {} - local ui_pick_teleporter = UiList.new() - ui_pick_teleporter:title(locale.gettext("Use which station?")) - - local j = 1 - for i in pairs(mod.station_list) do - - if mod.station_list[i][2] > distance then - local a = "Teleporter at ".. tostring(mod.station_list[i][3]).. "with charge ".. tostring(mod.station_list[i][2]) - ui_pick_teleporter:add(j, a) - teleporter_list_key[j] = i - - j = j + 1 - end - if i == mod.count_table(mod.station_list) then - if j == 1 then - gapi.add_msg(locale.gettext("No stations with enough charge. The required charge is ".. tostring(distance).. " energy units.")) - return 0 - end - end - end - local picked_teleporter = ui_pick_teleporter:query() - if picked_teleporter < 1 then - gapi.add_msg(locale.gettext("Nevermind.")) - return 0 - else - - for i in pairs(teleporter_list_key) do - print(tostring(teleporter_list_key[i])) - end - - return mod.teleport_to_target(who, anchor, distance, teleporter_list_key, picked_teleporter) - end - -end - -mod.pick_teleport_destination = function (who, pos) - local pos = pos - local ui_teleport = UiList.new() - local abs_pos = gapi.get_map():get_abs_ms( pos ) - local abs_omt = coords.ms_to_omt(abs_pos) - ui_teleport:title(locale.gettext("Select teleportation target")) - - for i in pairs(mod.anchor_list) do - local anchor = mod.anchor_list[i] - local distance = coords.rl_dist(abs_omt, anchor) - local a = "Coordinates: ".. tostring(mod.anchor_list[i]).. " Distance: ".. tostring(distance) - ui_teleport:add( i, a ) - end - - local eidx = ui_teleport:query() - if eidx < 1 then - gapi.add_msg(locale.gettext("Nevermind.")) - return 0 - end - - return mod.pick_teleporter (who, eidx, pos) -end - -mod.get_anchor_distance = function(pos, i) - local abs_pos = gapi.get_map():get_abs_ms( pos ) - local abs_omt = coords.ms_to_omt(abs_pos) - local anchor = mod.anchor_list[i] - local distance = coords.rl_dist(abs_omt, anchor) - return distance -end - -mod.create_network_message = function(pos) - b = [[Stations: -]] - for i in pairs(mod.station_list) do - local a = "Station: " .. tostring(i) - a = a .. " Last updated: ".. tostring(mod.station_list[i][1]) - a = a .. " Charge: ".. tostring(mod.station_list[i][2]) - a = a .. " Position: ".. tostring(mod.station_list[i][3]) - a = a .. [[ -]] - print(a) - b = b .. a - end - b = b.. [[------------------------------ - -Anchors: -]] - for i in pairs(mod.anchor_list) do - local a = "" - a = a .. " Position: ".. tostring(mod.anchor_list[i]) - a = a .. " Distance: ".. mod.get_anchor_distance(pos, i) - a = a .. [[ -]] - print(a) - b = b .. a - end - return b -end - -mod.network_info = function (pos) - -- spam out all stations and anchors - - local ui_network_info = QueryPopup.new() - local message_text = mod.create_network_message(pos) - ui_network_info:message(message_text) - ui_network_info:message_color( Color.c_white ) - ui_network_info:allow_any_key( true ) - ui_network_info:query() - return 1 -end - -mod.show_readme_info = function () - local ui_mod_info = QueryPopup.new() - local modinfo = [[ -Teleporters - the future, today! - -Teleporter stations provide the teleportation service, -while anchors function as targets. -This controller is used to teleport to an anchor from -anywhere, provided you have enough power in a station. - -The stations fill passively, the current rate is -]].. charge_multiplier.. [[ units per hour. - -You can also fill the stations from the grid, -using this controller, by standing on the same overmap -tile as the station you wish to fill, and drawing -from the grid on that specific overmap. If multiple -stations are present, you can pick which one to fill. - -The current rate is ]].. power_charge_kj_multiplier..[[kJ per 1 unit -of charge. Both rates can be changed in the config. ]] - ui_mod_info:message(modinfo) - ui_mod_info:message_color( Color.c_white ) - ui_mod_info:allow_any_key( true ) - ui_mod_info:query() - return 0 -end - -mod.config_set_variables = function () - local ui_pick_vars = UiList.new() - ui_pick_vars:title(locale.gettext("Change which variable?")) - ui_pick_vars:add(1, locale.gettext("Passive charge per hour")) - ui_pick_vars:add(2, locale.gettext("kJ to charge ratio")) - local pick_chargekjmult_action = ui_pick_vars:query() - if pick_chargekjmult_action < 1 then - gapi.add_msg(locale.gettext("Nevermind.")) - return 0 - elseif pick_chargekjmult_action == 1 then - return mod.setvar_charge_multiplier() - elseif pick_chargekjmult_action == 2 then - return mod.setvar_power_charge_kj_multiplier() - end -end - -mod.setvar_charge_multiplier = function () - local ui_pick_chargemult = UiList.new() - ui_pick_chargemult:title(locale.gettext("Set passive charge to:")) - ui_pick_chargemult:add(1, locale.gettext("Default: ").. tostring(1)) - ui_pick_chargemult:add(2, locale.gettext("Half: ").. tostring(storage.charge_multiplier*0.5)) - ui_pick_chargemult:add(3, locale.gettext("Double: ").. tostring(storage.charge_multiplier*2)) - local pick_chargemult_action = ui_pick_chargemult:query() - if pick_chargemult_action < 1 then - gapi.add_msg(locale.gettext("Nevermind.")) - return 0 - elseif pick_chargemult_action == 1 then - storage.charge_multiplier = 1 - charge_multiplier = storage.charge_multiplier - return 0 - elseif pick_chargemult_action == 2 then - storage.charge_multiplier = storage.charge_multiplier*0.5 - charge_multiplier = storage.charge_multiplier - return 0 - elseif pick_chargemult_action == 3 then - storage.charge_multiplier = storage.charge_multiplier*2 - charge_multiplier = storage.charge_multiplier - return 0 - end -end - -mod.setvar_power_charge_kj_multiplier = function () - local ui_pick_kjmult = UiList.new() - ui_pick_kjmult:title(locale.gettext("Set kJ to charge to:")) - ui_pick_kjmult:add(1, locale.gettext("Default: ").. tostring(1000)) - ui_pick_kjmult:add(2, locale.gettext("Half: ").. tostring(storage.power_charge_kj_multiplier*0.5)) - ui_pick_kjmult:add(3, locale.gettext("Double: ").. tostring(storage.power_charge_kj_multiplier*2)) - local pick_kjmult_action = ui_pick_kjmult:query() - if pick_kjmult_action < 1 then - gapi.add_msg(locale.gettext("Nevermind.")) - return 0 - elseif pick_kjmult_action == 1 then - storage.power_charge_kj_multiplier = 1000 - power_charge_kj_multiplier = storage.power_charge_kj_multiplier - return 0 - elseif pick_kjmult_action == 2 then - storage.power_charge_kj_multiplier = storage.power_charge_kj_multiplier*0.5 - power_charge_kj_multiplier = storage.power_charge_kj_multiplier - return 0 - elseif pick_kjmult_action == 3 then - storage.power_charge_kj_multiplier = storage.power_charge_kj_multiplier*2 - power_charge_kj_multiplier = storage.power_charge_kj_multiplier - return 0 - end -end - -mod.scan_omt_remove_furn = function (abs_omt, ftype) - local furniture_name = "" - local spawn_item = "" - if ftype == "station" then - furniture_name = tostring(FurnId.new("teleporter_station_deployed"):int_id()) - spawn_item = "teleporter_station_undeployed" - --print(furniture_name) - elseif ftype == "anchor" then - furniture_name = tostring(FurnId.new("teleporter_anchor_deployed"):int_id()) - spawn_item = "teleporter_station_undeployed" - --print(furniture_name) - end - local x, y = 0, 0 - local scantile = Point.new(x, y) - local scansquare = coords.omt_to_ms(abs_omt, scantile) - local mapsize = const.OMT_MS_SIZE - local furn_at_tile = "" - - for i = 1,const.OMT_MS_SIZE do - for j = 1,const.OMT_MS_SIZE do - x = i-1 - y = j-1 - scantile = Point.new(x, y) - scansquare = coords.omt_to_ms(abs_omt, scantile) - local xyz = gapi.get_map():get_local_ms(scansquare) - furn_at_tile = tostring(gapi.get_map():get_furn_at(xyz)) - --print(tostring(xyz) .. tostring(furn_at_tile)) - --gapi.get_map():set_furn_at( xyz, FurnId.new("f_fridge"):int_id() ) - if furn_at_tile == furniture_name then - gapi.get_map():set_furn_at( xyz, FurnId.new("f_null"):int_id() ) - gapi.add_msg(locale.gettext("You should get the".. tostring(spawn_item).. "item back at this point, but not yet available in LUA, sorry! Feel free to debug spawn the appropriate item.")) - return 0 - elseif i == const.OMT_MS_SIZE and j == const.OMT_MS_SIZE then - gapi.add_msg(locale.gettext("Appropriate furniture not found. Something went terribly wrong!")) - return 0 - end - end - end - -end - -mod.update_station_table = function() - for j in pairs(mod.station_list) do - if j ~= 1 then - if mod.station_list[j-1] == nil then - mod.station_list[j-1] = {} - for k in pairs(mod.station_list[j]) do - mod.station_list[j-1][k] = mod.station_list[j][k] - end - mod.station_list[j] = nil - end - end - end -end - -mod.remove_placed_furniture = function (pos) - - local abs_pos = gapi.get_map():get_abs_ms( pos ) - local abs_omt = coords.ms_to_omt(abs_pos) - local ui_remove_furn = UiList.new() - ui_remove_furn:title(locale.gettext("Remove station or anchor?")) - ui_remove_furn:add(1, locale.gettext("Station")) - ui_remove_furn:add(2, locale.gettext("Anchor")) - local pick_removefurn_action = ui_remove_furn:query() - if pick_removefurn_action < 1 then - gapi.add_msg(locale.gettext("Nevermind.")) - return 0 - elseif pick_removefurn_action == 1 then - for i in pairs(mod.station_list) do - if mod.station_list[i][3] == abs_omt then - mod.station_list[i] = nil - mod.update_station_table() - mod.scan_omt_remove_furn(abs_omt, "station") - return 0 - elseif i == mod.count_table(mod.station_list) then - gapi.add_msg(locale.gettext("No stations found on this tile.")) - return 0 - end - end - - elseif pick_removefurn_action == 2 then - for i in pairs(mod.anchor_list) do - if mod.anchor_list[i] == abs_omt then - mod.anchor_list[i] = nil - for j in pairs(mod.anchor_list) do - if j ~= 1 then - if mod.anchor_list[j-1] == nil then - mod.anchor_list[j-1] = mod.anchor_list[j] - mod.anchor_list[j] = nil - end - end - end - mod.scan_omt_remove_furn(abs_omt, "anchor") - return 0 - elseif i == mod.count_table(mod.anchor_list) then - gapi.add_msg(locale.gettext("No anchors found on this tile.")) - return 0 - end - end - - end -end - -mod.iuse_function_controller = function (who, item, pos) - - --print("used teleporter remote") - mod.update_station_charge() - - - - local ui_action = UiList.new() - ui_action:title(locale.gettext("Do what?")) - ui_action:add(1, locale.gettext("Teleport")) - ui_action:add(2, locale.gettext("Charge stations")) - ui_action:add(3, locale.gettext("Check network status")) - ui_action:add(4, locale.gettext("Remove anchors and stations")) - ui_action:add(5, locale.gettext("Info")) - ui_action:add(6, locale.gettext("Config")) - local pick_action = ui_action:query() - if pick_action < 1 then - gapi.add_msg(locale.gettext("Nevermind.")) - return 0 - elseif pick_action == 1 then - return mod.pick_teleport_destination(who, pos) - elseif pick_action == 2 then - return mod.charge_stations_from_grid(pos) - elseif pick_action == 3 then - return mod.network_info(pos) - elseif pick_action == 4 then - return mod.remove_placed_furniture(pos) - elseif pick_action == 5 then - return mod.show_readme_info() - elseif pick_action == 6 then - return mod.config_set_variables() - end - -end - +gdebug.log_info("Teleporter: main") + +--[[ CONFIG ]] +-- + +-- how many energy units a station gets per hour, default = 1, 1 charge teleports you one tile +charge_multiplier = 1 +-- how much power in kj is needed for 1 teleport energy unit when charging from grid +power_charge_kj_multiplier = 1000 + +local mod = game.mod_runtime[game.current_mod] +local storage = game.mod_storage[game.current_mod] + +--Item id (static) + +mod.storage = storage + +-- Variable ids + +-- Table of anchor positions +mod.anchor_list = {} +-- Table of station tables - [1] = time, [2] = charge, [3] = position(omt) +mod.station_list = {} +-- saved variables... these are separate from the active variables so I didn't mess with the saved tables constantly... also less chances of saves breaking on alt-f4, not that you'd do that ;) +storage.anchor_omt = {} +storage.station_placement_time = {} +storage.station_charge = {} +storage.station_pos = {} +-- also the default values +storage.charge_multiplier = 1 +storage.power_charge_kj_multiplier = 1000 +-- NOTE: due to an engine bug, the saved table keys need to be strings, not ints, that's why there's a mishmash of keys (tostring and tonumber functions used)... while this will be fixed in the foreseeable future (ty Olanti), there's no need to change this in the mod - everything should still work + +-- helper function for counting tables with non-integer keys, #table does not work for those +mod.count_table = function(save_table) + local count = 0 + for _, _ in pairs(save_table) do + count = count + 1 + end + return count +end + +-- LOADING + +mod.load_saved_anchors = function() + if mod.count_table(storage.anchor_omt) == 0 then + print("Data not loaded - no anchors are placed.") + else + for i in pairs(storage.anchor_omt) do + mod.anchor_list[tonumber(i)] = storage.anchor_omt[i] + print("Data found! anchor = " .. tostring(mod.anchor_list[tonumber(i)])) + end + end +end + +mod.load_saved_station_charge = function() + if mod.count_table(storage.station_charge) == 0 then + print("Data not loaded - no stations are placed. (charge)") + else + for i in pairs(storage.station_charge) do + mod.station_list[tonumber(i)] = {} + mod.station_list[tonumber(i)][2] = storage.station_charge[i] + print("Data found! station charge = " .. tostring(mod.station_list[tonumber(i)][2])) + end + end +end + +mod.load_saved_station_placement_time = function() + if mod.count_table(storage.station_placement_time) == 0 then + print("Data not loaded - no stations are placed. (time)") + else + for i in pairs(storage.station_placement_time) do + mod.station_list[tonumber(i)][1] = storage.station_placement_time[i] + print("Data found! station time = " .. tostring(mod.station_list[tonumber(i)][1])) + end + end +end + +mod.load_saved_station_pos = function() + if mod.count_table(storage.station_pos) == 0 then + print("Data not loaded - no stations are placed. (pos)") + else + for i in pairs(storage.station_pos) do + mod.station_list[tonumber(i)][3] = storage.station_pos[i] + print("Data found! station pos = " .. tostring(mod.station_list[tonumber(i)][3])) + end + end +end + +mod.on_game_load_hook = function() + gdebug.log_info("Teleporters: on_load") + + mod.load_saved_anchors() + + -- order below is important, since the data tables on load are initialized in the mod.load_saved_station_charge() function + + mod.load_saved_station_charge() + mod.load_saved_station_placement_time() + mod.load_saved_station_pos() + charge_multiplier = storage.charge_multiplier + power_charge_kj_multiplier = storage.power_charge_kj_multiplier +end + +-- SAVING + +mod.save_station_charge = function(num) + storage.station_charge[tostring(num)] = mod.station_list[num][2] + print("Saved charge to list" .. tostring(mod.station_list[num][2])) +end + +mod.save_station_placement_time = function(num) + storage.station_placement_time[tostring(num)] = mod.station_list[num][1] + print("Saved time to list" .. tostring(mod.station_list[num][1])) +end + +mod.save_station_pos = function(num) + storage.station_pos[tostring(num)] = mod.station_list[num][3] + print("Saved pos to list" .. tostring(mod.station_list[num][3])) +end + +mod.save_anchor_omt = function() + for i in pairs(mod.anchor_list) do + storage.anchor_omt[tostring(i)] = mod.anchor_list[i] + print("Saved to anchor list" .. tostring(mod.anchor_list[i])) + end +end + +mod.on_game_save_hook = function() + gdebug.log_info("Teleporters: on_save") + + mod.save_anchor_omt() + + mod.update_station_charge() + + for i in pairs(mod.station_list) do + mod.save_station_charge(i) + mod.save_station_placement_time(i) + mod.save_station_pos(i) + end +end + +-- main function + +mod.add_anchor_to_list = function(pos) + --print("adding to list") + --print(pos) + mod.anchor_list[#mod.anchor_list + 1] = pos + --print(#mod.anchor_list, pos) + mod.save_anchor_omt() +end + +mod.iuse_function_anchor = function(who, item, pos) + local a = { "teleporter_anchor_deployed" } + + local player_abs_pos = gapi.get_map():get_abs_ms(pos) + --print(player_abs_pos) + + local player_map_pos = gapi.get_map():get_local_ms(player_abs_pos) + --print(player_map_pos) + local player_omt = coords.ms_to_omt(player_abs_pos) + + local no_furn = gapi.get_map():get_furn_at(player_map_pos) + b = tostring(no_furn) + --print(b) + anchor_omt = tostring(player_omt) + + if b == "FurnIntId[0][f_null]" then + --print (FurnId.new("teleporter_anchor_deployed"):int_id()) + gapi.get_map():set_furn_at(player_map_pos, FurnId.new(a[1]):int_id()) + + mod.add_anchor_to_list(player_omt) + + gapi.add_msg("The teleporter anchor was placed at ") + gapi.add_msg(anchor_omt) + else + gapi.add_msg("Can only be placed on a square with no existing furniture.") + return 0 + end + return 1 +end + +mod.add_station_to_list = function(pos) + local num = #mod.station_list + 1 + --print(num) + local turn = gapi.current_turn() + local charge = 0 + + mod.station_list[num] = {} + mod.station_list[num][1] = turn + mod.station_list[num][2] = charge + mod.station_list[num][3] = pos + + mod.save_station_placement_time(num) + mod.save_station_charge(num) + mod.save_station_pos(num) +end + +mod.iuse_function_station = function(who, item, pos) + local a = { "teleporter_station_deployed" } + local player_abs_pos = gapi.get_map():get_abs_ms(pos) + --print(player_abs_pos) + + local player_map_pos = gapi.get_map():get_local_ms(player_abs_pos) + --print(player_map_pos) + + local player_omt = coords.ms_to_omt(player_abs_pos) + --print(player_omt) + + local no_furn = gapi.get_map():get_furn_at(player_map_pos) + b = tostring(no_furn) + --print(b) + station_omt = tostring(player_omt) + + if b == "FurnIntId[0][f_null]" then + gapi.get_map():set_furn_at(player_map_pos, FurnId.new(a[1]):int_id()) + mod.add_station_to_list(player_omt) + else + gapi.add_msg("Can only be placed on a square with no existing furniture.") + return 0 + end + + return 1 + --end +end + +mod.update_station_charge = function() + -- go through station list, compare times, change charge depending on time passed + if mod.count_table(mod.station_list) > 0 then + for i in pairs(mod.station_list) do + local station_time = mod.station_list[i][1] + local current_time = gapi.current_turn() + local time_difference_TimeDuration = TimePoint.__sub(current_time, station_time) + local time_difference = TimeDuration.to_seconds(time_difference_TimeDuration) + --print (time_difference) + if time_difference > 60 then + local added_charge = time_difference * charge_multiplier / 3600 + mod.station_list[i][2] = mod.station_list[i][2] + added_charge + mod.station_list[i][1] = current_time + end + end + else + print("No stations in network!") + end +end + +mod.do_station_charge = function(choose, grid, power_available, chosen_station_list) + local available_units = (tonumber(power_available) / power_charge_kj_multiplier) + --print(available_units) + if available_units < 1 then + gapi.add_msg(locale.gettext("Power too low for charging.")) + return 0 + end + local uic = UiList.new() + uic:title(locale.gettext("How much power do you want to charge the station with?")) + for i = 1, available_units do + uic:add(i, tostring(i)) + end + local pick = uic:query() + if pick < 1 then + gapi.add_msg(locale.gettext("Nevermind.")) + return 0 + else + --print(mod.station_list[chosen_station_list[choose]][2]) + mod.station_list[chosen_station_list[choose]][2] = mod.station_list[chosen_station_list[choose]][2] + pick + --print(mod.station_list[chosen_station_list[choose]][2]) + grid:mod_resource(-(pick * power_charge_kj_multiplier), true) + gapi.add_msg(locale.gettext("The station was charged.")) + return 1 + end +end + +mod.charge_stations_from_grid = function(pos) + local abs_pos = gapi.get_map():get_abs_ms(pos) + local abs_omt = coords.ms_to_omt(abs_pos) + local grid = gapi.get_distribution_grid_tracker():get_grid_at_abs_ms(abs_pos) + local power_available = grid:get_resource(true) + local chosen_station_list = {} + + if power_available == 0 then + gapi.add_msg(locale.gettext("No grid or no power in grid")) + return 0 + end + if power_available > 0 then + local ui_charge = UiList.new() + ui_charge:title(locale.gettext("Charge which station?")) + local j = 1 + for i in pairs(mod.station_list) do + if mod.station_list[i][3] == abs_omt then + local a = tostring(j) .. "Station with charge " .. tostring(mod.station_list[i][2]) + ui_charge:add(j, a) + chosen_station_list[j] = i + j = j + 1 + end + if i == mod.count_table(mod.station_list) then + if j == 1 then + gapi.add_msg(locale.gettext("No stations found on your tile.")) + return 0 + end + end + end + local choose = ui_charge:query() + -- Canceled by player + if choose < 1 then + gapi.add_msg(locale.gettext("Nevermind.")) + return 0 + end + return mod.do_station_charge(choose, grid, power_available, chosen_station_list) + end +end + +mod.teleport_to_target = function(who, anchor, distance, teleporter_list_key, picked_teleporter) + local a = teleporter_list_key[picked_teleporter] + print(a) + local removed_charge = -distance + mod.station_list[a][2] = mod.station_list[a][2] + removed_charge + + gapi.add_msg("Teleporting Player to " .. tostring(anchor)) + gapi.add_msg( + "Station at: " + .. tostring(mod.station_list[teleporter_list_key[picked_teleporter]][3]) + .. "now has " + .. mod.station_list[teleporter_list_key[picked_teleporter]][2] + .. " units of charge left" + ) + --need exposed debug teleport for this to function + gapi.place_player_overmap_at(anchor) + return 1 +end + +mod.pick_teleporter = function(who, eidx, pos) + local abs_pos = gapi.get_map():get_abs_ms(pos) + local abs_omt = coords.ms_to_omt(abs_pos) + local anchor = mod.anchor_list[eidx] + local distance = coords.rl_dist(abs_omt, anchor) + local teleporter_list_key = {} + local ui_pick_teleporter = UiList.new() + ui_pick_teleporter:title(locale.gettext("Use which station?")) + + local j = 1 + for i in pairs(mod.station_list) do + if mod.station_list[i][2] > distance then + local a = "Teleporter at " + .. tostring(mod.station_list[i][3]) + .. "with charge " + .. tostring(mod.station_list[i][2]) + ui_pick_teleporter:add(j, a) + teleporter_list_key[j] = i + + j = j + 1 + end + if i == mod.count_table(mod.station_list) then + if j == 1 then + gapi.add_msg( + locale.gettext( + "No stations with enough charge. The required charge is " .. tostring(distance) .. " energy units." + ) + ) + return 0 + end + end + end + local picked_teleporter = ui_pick_teleporter:query() + if picked_teleporter < 1 then + gapi.add_msg(locale.gettext("Nevermind.")) + return 0 + else + for i in pairs(teleporter_list_key) do + print(tostring(teleporter_list_key[i])) + end + + return mod.teleport_to_target(who, anchor, distance, teleporter_list_key, picked_teleporter) + end +end + +mod.pick_teleport_destination = function(who, pos) + local pos = pos + local ui_teleport = UiList.new() + local abs_pos = gapi.get_map():get_abs_ms(pos) + local abs_omt = coords.ms_to_omt(abs_pos) + ui_teleport:title(locale.gettext("Select teleportation target")) + + for i in pairs(mod.anchor_list) do + local anchor = mod.anchor_list[i] + local distance = coords.rl_dist(abs_omt, anchor) + local a = "Coordinates: " .. tostring(mod.anchor_list[i]) .. " Distance: " .. tostring(distance) + ui_teleport:add(i, a) + end + + local eidx = ui_teleport:query() + if eidx < 1 then + gapi.add_msg(locale.gettext("Nevermind.")) + return 0 + end + + return mod.pick_teleporter(who, eidx, pos) +end + +mod.get_anchor_distance = function(pos, i) + local abs_pos = gapi.get_map():get_abs_ms(pos) + local abs_omt = coords.ms_to_omt(abs_pos) + local anchor = mod.anchor_list[i] + local distance = coords.rl_dist(abs_omt, anchor) + return distance +end + +mod.create_network_message = function(pos) + b = [[Stations: +]] + for i in pairs(mod.station_list) do + local a = "Station: " .. tostring(i) + a = a .. " Last updated: " .. tostring(mod.station_list[i][1]) + a = a .. " Charge: " .. tostring(mod.station_list[i][2]) + a = a .. " Position: " .. tostring(mod.station_list[i][3]) + a = a .. [[ +]] + print(a) + b = b .. a + end + b = b .. [[------------------------------ + +Anchors: +]] + for i in pairs(mod.anchor_list) do + local a = "" + a = a .. " Position: " .. tostring(mod.anchor_list[i]) + a = a .. " Distance: " .. mod.get_anchor_distance(pos, i) + a = a .. [[ +]] + print(a) + b = b .. a + end + return b +end + +mod.network_info = function(pos) + -- spam out all stations and anchors + + local ui_network_info = QueryPopup.new() + local message_text = mod.create_network_message(pos) + ui_network_info:message(message_text) + ui_network_info:message_color(Color.c_white) + ui_network_info:allow_any_key(true) + ui_network_info:query() + return 1 +end + +mod.show_readme_info = function() + local ui_mod_info = QueryPopup.new() + local modinfo = [[ +Teleporters - the future, today! + +Teleporter stations provide the teleportation service, +while anchors function as targets. +This controller is used to teleport to an anchor from +anywhere, provided you have enough power in a station. + +The stations fill passively, the current rate is +]] .. charge_multiplier .. [[ units per hour. + +You can also fill the stations from the grid, +using this controller, by standing on the same overmap +tile as the station you wish to fill, and drawing +from the grid on that specific overmap. If multiple +stations are present, you can pick which one to fill. + +The current rate is ]] .. power_charge_kj_multiplier .. [[kJ per 1 unit +of charge. Both rates can be changed in the config. ]] + ui_mod_info:message(modinfo) + ui_mod_info:message_color(Color.c_white) + ui_mod_info:allow_any_key(true) + ui_mod_info:query() + return 0 +end + +mod.config_set_variables = function() + local ui_pick_vars = UiList.new() + ui_pick_vars:title(locale.gettext("Change which variable?")) + ui_pick_vars:add(1, locale.gettext("Passive charge per hour")) + ui_pick_vars:add(2, locale.gettext("kJ to charge ratio")) + local pick_chargekjmult_action = ui_pick_vars:query() + if pick_chargekjmult_action < 1 then + gapi.add_msg(locale.gettext("Nevermind.")) + return 0 + elseif pick_chargekjmult_action == 1 then + return mod.setvar_charge_multiplier() + elseif pick_chargekjmult_action == 2 then + return mod.setvar_power_charge_kj_multiplier() + end +end + +mod.setvar_charge_multiplier = function() + local ui_pick_chargemult = UiList.new() + ui_pick_chargemult:title(locale.gettext("Set passive charge to:")) + ui_pick_chargemult:add(1, locale.gettext("Default: ") .. tostring(1)) + ui_pick_chargemult:add(2, locale.gettext("Half: ") .. tostring(storage.charge_multiplier * 0.5)) + ui_pick_chargemult:add(3, locale.gettext("Double: ") .. tostring(storage.charge_multiplier * 2)) + local pick_chargemult_action = ui_pick_chargemult:query() + if pick_chargemult_action < 1 then + gapi.add_msg(locale.gettext("Nevermind.")) + return 0 + elseif pick_chargemult_action == 1 then + storage.charge_multiplier = 1 + charge_multiplier = storage.charge_multiplier + return 0 + elseif pick_chargemult_action == 2 then + storage.charge_multiplier = storage.charge_multiplier * 0.5 + charge_multiplier = storage.charge_multiplier + return 0 + elseif pick_chargemult_action == 3 then + storage.charge_multiplier = storage.charge_multiplier * 2 + charge_multiplier = storage.charge_multiplier + return 0 + end +end + +mod.setvar_power_charge_kj_multiplier = function() + local ui_pick_kjmult = UiList.new() + ui_pick_kjmult:title(locale.gettext("Set kJ to charge to:")) + ui_pick_kjmult:add(1, locale.gettext("Default: ") .. tostring(1000)) + ui_pick_kjmult:add(2, locale.gettext("Half: ") .. tostring(storage.power_charge_kj_multiplier * 0.5)) + ui_pick_kjmult:add(3, locale.gettext("Double: ") .. tostring(storage.power_charge_kj_multiplier * 2)) + local pick_kjmult_action = ui_pick_kjmult:query() + if pick_kjmult_action < 1 then + gapi.add_msg(locale.gettext("Nevermind.")) + return 0 + elseif pick_kjmult_action == 1 then + storage.power_charge_kj_multiplier = 1000 + power_charge_kj_multiplier = storage.power_charge_kj_multiplier + return 0 + elseif pick_kjmult_action == 2 then + storage.power_charge_kj_multiplier = storage.power_charge_kj_multiplier * 0.5 + power_charge_kj_multiplier = storage.power_charge_kj_multiplier + return 0 + elseif pick_kjmult_action == 3 then + storage.power_charge_kj_multiplier = storage.power_charge_kj_multiplier * 2 + power_charge_kj_multiplier = storage.power_charge_kj_multiplier + return 0 + end +end + +mod.scan_omt_remove_furn = function(abs_omt, ftype) + local furniture_name = "" + local spawn_item = "" + if ftype == "station" then + furniture_name = tostring(FurnId.new("teleporter_station_deployed"):int_id()) + spawn_item = "teleporter_station_undeployed" + --print(furniture_name) + elseif ftype == "anchor" then + furniture_name = tostring(FurnId.new("teleporter_anchor_deployed"):int_id()) + spawn_item = "teleporter_station_undeployed" + --print(furniture_name) + end + local x, y = 0, 0 + local scantile = Point.new(x, y) + local scansquare = coords.omt_to_ms(abs_omt, scantile) + local mapsize = const.OMT_MS_SIZE + local furn_at_tile = "" + + for i = 1, const.OMT_MS_SIZE do + for j = 1, const.OMT_MS_SIZE do + x = i - 1 + y = j - 1 + scantile = Point.new(x, y) + scansquare = coords.omt_to_ms(abs_omt, scantile) + local xyz = gapi.get_map():get_local_ms(scansquare) + furn_at_tile = tostring(gapi.get_map():get_furn_at(xyz)) + --print(tostring(xyz) .. tostring(furn_at_tile)) + --gapi.get_map():set_furn_at( xyz, FurnId.new("f_fridge"):int_id() ) + if furn_at_tile == furniture_name then + gapi.get_map():set_furn_at(xyz, FurnId.new("f_null"):int_id()) + gapi.add_msg( + locale.gettext( + "You should get the" + .. tostring(spawn_item) + .. "item back at this point, but not yet available in LUA, sorry! Feel free to debug spawn the appropriate item." + ) + ) + return 0 + elseif i == const.OMT_MS_SIZE and j == const.OMT_MS_SIZE then + gapi.add_msg(locale.gettext("Appropriate furniture not found. Something went terribly wrong!")) + return 0 + end + end + end +end + +mod.update_station_table = function() + for j in pairs(mod.station_list) do + if j ~= 1 then + if mod.station_list[j - 1] == nil then + mod.station_list[j - 1] = {} + for k in pairs(mod.station_list[j]) do + mod.station_list[j - 1][k] = mod.station_list[j][k] + end + mod.station_list[j] = nil + end + end + end +end + +mod.remove_placed_furniture = function(pos) + local abs_pos = gapi.get_map():get_abs_ms(pos) + local abs_omt = coords.ms_to_omt(abs_pos) + local ui_remove_furn = UiList.new() + ui_remove_furn:title(locale.gettext("Remove station or anchor?")) + ui_remove_furn:add(1, locale.gettext("Station")) + ui_remove_furn:add(2, locale.gettext("Anchor")) + local pick_removefurn_action = ui_remove_furn:query() + if pick_removefurn_action < 1 then + gapi.add_msg(locale.gettext("Nevermind.")) + return 0 + elseif pick_removefurn_action == 1 then + for i in pairs(mod.station_list) do + if mod.station_list[i][3] == abs_omt then + mod.station_list[i] = nil + mod.update_station_table() + mod.scan_omt_remove_furn(abs_omt, "station") + return 0 + elseif i == mod.count_table(mod.station_list) then + gapi.add_msg(locale.gettext("No stations found on this tile.")) + return 0 + end + end + elseif pick_removefurn_action == 2 then + for i in pairs(mod.anchor_list) do + if mod.anchor_list[i] == abs_omt then + mod.anchor_list[i] = nil + for j in pairs(mod.anchor_list) do + if j ~= 1 then + if mod.anchor_list[j - 1] == nil then + mod.anchor_list[j - 1] = mod.anchor_list[j] + mod.anchor_list[j] = nil + end + end + end + mod.scan_omt_remove_furn(abs_omt, "anchor") + return 0 + elseif i == mod.count_table(mod.anchor_list) then + gapi.add_msg(locale.gettext("No anchors found on this tile.")) + return 0 + end + end + end +end + +mod.iuse_function_controller = function(who, item, pos) + --print("used teleporter remote") + mod.update_station_charge() + + local ui_action = UiList.new() + ui_action:title(locale.gettext("Do what?")) + ui_action:add(1, locale.gettext("Teleport")) + ui_action:add(2, locale.gettext("Charge stations")) + ui_action:add(3, locale.gettext("Check network status")) + ui_action:add(4, locale.gettext("Remove anchors and stations")) + ui_action:add(5, locale.gettext("Info")) + ui_action:add(6, locale.gettext("Config")) + local pick_action = ui_action:query() + if pick_action < 1 then + gapi.add_msg(locale.gettext("Nevermind.")) + return 0 + elseif pick_action == 1 then + return mod.pick_teleport_destination(who, pos) + elseif pick_action == 2 then + return mod.charge_stations_from_grid(pos) + elseif pick_action == 3 then + return mod.network_info(pos) + elseif pick_action == 4 then + return mod.remove_placed_furniture(pos) + elseif pick_action == 5 then + return mod.show_readme_info() + elseif pick_action == 6 then + return mod.config_set_variables() + end +end diff --git a/data/mods/teleportation_mod/preload.lua b/data/mods/teleportation_mod/preload.lua index cd01a52dc665..9284bc3ab0d6 100644 --- a/data/mods/teleportation_mod/preload.lua +++ b/data/mods/teleportation_mod/preload.lua @@ -3,25 +3,25 @@ gdebug.log_info("Tele: preload.") -- Have to register iuse before data loading. -- Actual implementation (function mod.iuse_function) will be defined later. -local mod = game.mod_runtime[ game.current_mod ] +local mod = game.mod_runtime[game.current_mod] -- Register our item use function -game.iuse_functions[ "teleporter_controller_use" ] = function(...) - return mod.iuse_function_controller(...) +game.iuse_functions["teleporter_controller_use"] = function(...) + return mod.iuse_function_controller(...) end -game.iuse_functions[ "teleporter_anchor_use" ] = function(...) - return mod.iuse_function_anchor(...) +game.iuse_functions["teleporter_anchor_use"] = function(...) + return mod.iuse_function_anchor(...) end -game.iuse_functions[ "teleporter_station_use" ] = function(...) - return mod.iuse_function_station(...) +game.iuse_functions["teleporter_station_use"] = function(...) + return mod.iuse_function_station(...) end -game.hooks.on_game_load[ #game.hooks.on_game_load + 1 ] = function( ... ) - return mod.on_game_load_hook( ... ) +game.hooks.on_game_load[#game.hooks.on_game_load + 1] = function(...) + return mod.on_game_load_hook(...) end -game.hooks.on_game_save[ #game.hooks.on_game_save + 1 ] = function( ... ) - return mod.on_game_save_hook( ... ) -end \ No newline at end of file +game.hooks.on_game_save[#game.hooks.on_game_save + 1] = function(...) + return mod.on_game_save_hook(...) +end diff --git a/data/raw/generate_docs.lua b/data/raw/generate_docs.lua index d0732e35996e..ef48c0dfef3d 100644 --- a/data/raw/generate_docs.lua +++ b/data/raw/generate_docs.lua @@ -4,143 +4,148 @@ Sort by key by default, but may also use provided sort function f(a, b) where a and b - tables of type {k=k, v=v} (the function must return whether a is less than b). -]]-- +]] +-- local sorted_by = function(t, f) - if not f then - f = function(a,b) return (a.k < b.k) end - end - local sorted = {} - for k, v in pairs(t) do - sorted[#sorted + 1] = {k=k, v=v} + if not f then + f = function(a, b) + return (a.k < b.k) end - table.sort( sorted, f ) - return sorted + end + local sorted = {} + for k, v in pairs(t) do + sorted[#sorted + 1] = { k = k, v = v } + end + table.sort(sorted, f) + return sorted end local remove_hidden_args = function(arg_list) - local ret = {} - for _, arg in pairs(arg_list) do - if arg == "" then - -- sol::this_state is only visible to C++ side - else - ret[#ret + 1] = arg - end + local ret = {} + for _, arg in pairs(arg_list) do + if arg == "" then + -- sol::this_state is only visible to C++ side + else + ret[#ret + 1] = arg end - return ret + end + return ret end local fmt_arg_list = function(arg_list) - local ret = "" - local arg_list = remove_hidden_args(arg_list) - if #arg_list == 0 then - return ret - end - local is_first = true - for _, arg in pairs(arg_list) do - if not is_first then - ret=ret.."," - end - ret=ret.." "..arg - is_first = false + local ret = "" + local arg_list = remove_hidden_args(arg_list) + if #arg_list == 0 then + return ret + end + local is_first = true + for _, arg in pairs(arg_list) do + if not is_first then + ret = ret .. "," end - return ret.." " + ret = ret .. " " .. arg + is_first = false + end + return ret .. " " end local fmt_one_constructor = function(typename, ctor) - return typename..".new("..fmt_arg_list(ctor)..")" + return typename .. ".new(" .. fmt_arg_list(ctor) .. ")" end local fmt_constructors = function(typename, ctors) - if #ctors == 0 then - return " No constructors.\n" - else - local ret = "" - for k,v in pairs(ctors) do - ret=ret.."#### `"..fmt_one_constructor(typename, v).."`\n" - end - return ret + if #ctors == 0 then + return " No constructors.\n" + else + local ret = "" + for k, v in pairs(ctors) do + ret = ret .. "#### `" .. fmt_one_constructor(typename, v) .. "`\n" end + return ret + end end local fmt_one_member = function(typename, member) - local ret = "#### "..tostring(member.name).."\n"; + local ret = "#### " .. tostring(member.name) .. "\n" - if member.comment then - ret=ret..member.comment.."\n" - end + if member.comment then + ret = ret .. member.comment .. "\n" + end - if member.type == "var" then - ret=ret.." Variable of type `"..member.vartype.."`" - if member.hasval then - ret=ret.." value: `"..tostring(member.varval).."`" - end - ret=ret.."\n" - elseif member.type == "func" then - for _,overload in pairs(member.overloads) do - ret=ret.." Function `("..fmt_arg_list(overload.args)..")" - if overload.retval ~= "nil" then - ret=ret.." -> "..overload.retval - end - ret=ret.."`\n" - end - else - error("Unknown member type "..tostring(member.type)) + if member.type == "var" then + ret = ret .. " Variable of type `" .. member.vartype .. "`" + if member.hasval then + ret = ret .. " value: `" .. tostring(member.varval) .. "`" + end + ret = ret .. "\n" + elseif member.type == "func" then + for _, overload in pairs(member.overloads) do + ret = ret .. " Function `(" .. fmt_arg_list(overload.args) .. ")" + if overload.retval ~= "nil" then + ret = ret .. " -> " .. overload.retval + end + ret = ret .. "`\n" end + else + error("Unknown member type " .. tostring(member.type)) + end - return ret + return ret end local fmt_members = function(typename, members) - if #members == 0 then - return " No members.\n" - else - local ret = "" + if #members == 0 then + return " No members.\n" + else + local ret = "" - local members_sorted = sorted_by( members ) + local members_sorted = sorted_by(members) - for _,it in pairs(members_sorted) do - ret=ret..fmt_one_member(typename, it.v).."\n" - end - return ret + for _, it in pairs(members_sorted) do + ret = ret .. fmt_one_member(typename, it.v) .. "\n" end + return ret + end end local fmt_bases = function(typename, bases) - if #bases == 0 then - return " No base classes.\n" - else - local ret = "" - for k,v in pairs(bases) do - ret=ret.."- `"..v.."`\n" - end - return ret + if #bases == 0 then + return " No base classes.\n" + else + local ret = "" + for k, v in pairs(bases) do + ret = ret .. "- `" .. v .. "`\n" end + return ret + end end local fmt_enum_entries = function(typename, entries) - if next(entries) == nil then - return " No entries.\n" - else - local ret = "" - - local entries_filtered = {} - for k,v in pairs(entries) do - -- TODO: this should not be needed - if type(v) ~= "table" and type(v) ~= "function" then - entries_filtered[k] = v; - end - end - - local entries_sorted = sorted_by(entries_filtered, function(a,b) return a.v < b.v end) - for _,it in pairs(entries_sorted) do - ret=ret.."- `"..tostring(it.k).."` = `"..tostring(it.v).."`\n" - end - return ret + if next(entries) == nil then + return " No entries.\n" + else + local ret = "" + + local entries_filtered = {} + for k, v in pairs(entries) do + -- TODO: this should not be needed + if type(v) ~= "table" and type(v) ~= "function" then + entries_filtered[k] = v + end + end + + local entries_sorted = sorted_by(entries_filtered, function(a, b) + return a.v < b.v + end) + for _, it in pairs(entries_sorted) do + ret = ret .. "- `" .. tostring(it.k) .. "` = `" .. tostring(it.v) .. "`\n" end + return ret + end end doc_gen_func.impl = function() - local ret = [[--- + local ret = [[--- title: Lua API reference editUrl: false sidebar: @@ -160,77 +165,71 @@ and should not be edited directly. ]] - local dt = catadoc - - local types_table = dt["#types"] - - local types_sorted = sorted_by(types_table) - for _,it in pairs(types_sorted) do - local typename = it.k - local dt_type = it.v - local type_comment = dt_type.type_comment - ret = ret.."## "..typename.."\n" - - if type_comment then - ret = ret..type_comment.."\n" - end - - local bases = dt_type["#bases"] - local ctors = dt_type["#construct"] - local members = dt_type["#member"] - - ret = ret - .."### Bases\n" - ..fmt_bases( typename, bases ) - .."\n" - .."### Constructors\n" - ..fmt_constructors( typename, ctors ) - .."\n" - .."### Members\n" - ..fmt_members( typename, members ) - .."\n" + local dt = catadoc + + local types_table = dt["#types"] + + local types_sorted = sorted_by(types_table) + for _, it in pairs(types_sorted) do + local typename = it.k + local dt_type = it.v + local type_comment = dt_type.type_comment + ret = ret .. "## " .. typename .. "\n" + + if type_comment then + ret = ret .. type_comment .. "\n" end - ret = ret.."# Enums\n\n" + local bases = dt_type["#bases"] + local ctors = dt_type["#construct"] + local members = dt_type["#member"] - local enums_table = dt["#enums"] + ret = ret + .. "### Bases\n" + .. fmt_bases(typename, bases) + .. "\n" + .. "### Constructors\n" + .. fmt_constructors(typename, ctors) + .. "\n" + .. "### Members\n" + .. fmt_members(typename, members) + .. "\n" + end - local enums_sorted = sorted_by(enums_table) - for _,it in pairs(enums_sorted) do - local typename = it.k - local dt_type = it.v - ret = ret.."## "..typename.."\n" + ret = ret .. "# Enums\n\n" - local entries = dt_type["entries"] + local enums_table = dt["#enums"] - ret = ret - .."### Entries\n" - ..fmt_enum_entries( typename, entries ) - .."\n" - end + local enums_sorted = sorted_by(enums_table) + for _, it in pairs(enums_sorted) do + local typename = it.k + local dt_type = it.v + ret = ret .. "## " .. typename .. "\n" - ret = ret.."# Libraries\n\n" + local entries = dt_type["entries"] - local libs_table = dt["#libs"] + ret = ret .. "### Entries\n" .. fmt_enum_entries(typename, entries) .. "\n" + end - local libs_sorted = sorted_by( libs_table ) - for _,it in pairs(libs_sorted) do - local typename = it.k - local dt_lib = it.v - local lib_comment = dt_lib.lib_comment - ret = ret.."## "..typename.."\n" + ret = ret .. "# Libraries\n\n" - if lib_comment then - ret = ret..lib_comment.."\n" - end + local libs_table = dt["#libs"] - local members = dt_lib["#member"] + local libs_sorted = sorted_by(libs_table) + for _, it in pairs(libs_sorted) do + local typename = it.k + local dt_lib = it.v + local lib_comment = dt_lib.lib_comment + ret = ret .. "## " .. typename .. "\n" - ret = ret - .."### Members\n" - ..fmt_members( nil, members ) - .."\n" + if lib_comment then + ret = ret .. lib_comment .. "\n" end - return ret + local members = dt_lib["#member"] + + ret = ret .. "### Members\n" .. fmt_members(nil, members) .. "\n" + end + + return ret end diff --git a/data/raw/on_game_start.lua b/data/raw/on_game_start.lua index 041496574929..ffb152cb3c1a 100644 --- a/data/raw/on_game_start.lua +++ b/data/raw/on_game_start.lua @@ -1 +1 @@ -print('Lua online.') +print("Lua online.") diff --git a/deno.jsonc b/deno.jsonc index fafc79660c7f..366ecffb2d44 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -3,7 +3,8 @@ "doc": "deno task doc:cli & deno task doc:lua && deno fmt", "doc:cli": "deno run --allow-run=./cataclysm-tiles --allow-write=doc/src/content/docs ./scripts/gen_cli_docs.ts", "doc:lua": "./cataclysm-tiles --lua-doc && cp config/lua_doc.md doc/src/content/docs/en/mod/lua/reference/lua.md", - "migrate-unit": "deno run -A scripts/migrate_legacy_unit.ts --path data/json; deno run -A scripts/migrate_legacy_unit.ts --path data/mods; make style-all-json-parallel" + "migrate-unit": "deno run -A scripts/migrate_legacy_unit.ts --path data/json; deno run -A scripts/migrate_legacy_unit.ts --path data/mods; make style-all-json-parallel", + "dprint": "deno run -A npm:dprint" }, "test": { "include": ["scripts"] }, "lint": { "include": ["scripts"] }, diff --git a/deno.lock b/deno.lock index e0870f7bcd5d..ea7c3d1fc897 100644 --- a/deno.lock +++ b/deno.lock @@ -2,9 +2,50 @@ "version": "3", "packages": { "specifiers": { + "npm:dprint": "npm:dprint@0.43.2", "npm:ts-pattern@5.0.5": "npm:ts-pattern@5.0.5" }, "npm": { + "@dprint/darwin-arm64@0.43.2": { + "integrity": "sha512-EPBM3my8L2Wq+UPjfRepKLsZZe30mipvgT6HdpsT8FXqrvVaHcE5BxQdyu0TH/V7GogmeMXOaiAgH9f5IBkdTg==", + "dependencies": {} + }, + "@dprint/darwin-x64@0.43.2": { + "integrity": "sha512-zXabg6PF8g3d0Zc7KS3gj6/w38jO6GqIwHG0fvJh+E1UiSs36xLjLAsyZh7/6wrDu7LfD/g5WOUz1QAfrnO3zQ==", + "dependencies": {} + }, + "@dprint/linux-arm64-glibc@0.43.2": { + "integrity": "sha512-XV6pXgyT8iKHf/VOB9JVZlU73mTX6vjJ+fLY0c+r9j7Zgjp6kb4JX6mTfeEwTZYBsZUzOmxKj+kno1mfw6dUHg==", + "dependencies": {} + }, + "@dprint/linux-arm64-musl@0.43.2": { + "integrity": "sha512-aaWBIOMncDb0YrRmmNKCs7tf6H26VB0izX2bPIbMiOc50hPuCI28lz1lY+zdwdFKG+1aRkKHkLJtpOjgvmEqCw==", + "dependencies": {} + }, + "@dprint/linux-x64-glibc@0.43.2": { + "integrity": "sha512-OBJlQCvPCQOgSZvQhTVJbF6mD9Bpz3hhecTZNnU/n83bHX1L3cwu/laws0mQPXOxJvBKpYVgN5ceZelIO0ZKWA==", + "dependencies": {} + }, + "@dprint/linux-x64-musl@0.43.2": { + "integrity": "sha512-WVnivencONHhRVfgbKPf95P3ES8DYowBCk80tROg41BBRdP/WJoPWc5Ocm6mhEGEeIpcHol+xvwKkqZkkMxJcg==", + "dependencies": {} + }, + "@dprint/win32-x64@0.43.2": { + "integrity": "sha512-ZboeX4lGZ+MtFyWV/Z9wxV3Re2zb/t6u79V38aKgCWDuy1SzGdxZcCPjEEY17lxwGpzgD5T0VBVdiBQLIy08CQ==", + "dependencies": {} + }, + "dprint@0.43.2": { + "integrity": "sha512-9p/dmiFSkTl3hEvCfNbc/jwVwZp4JE5Bgm0z148QEXU7v+FA2dv0xQooAagWEnCPAQgxYcQhecTxrLanOsyNoA==", + "dependencies": { + "@dprint/darwin-arm64": "@dprint/darwin-arm64@0.43.2", + "@dprint/darwin-x64": "@dprint/darwin-x64@0.43.2", + "@dprint/linux-arm64-glibc": "@dprint/linux-arm64-glibc@0.43.2", + "@dprint/linux-arm64-musl": "@dprint/linux-arm64-musl@0.43.2", + "@dprint/linux-x64-glibc": "@dprint/linux-x64-glibc@0.43.2", + "@dprint/linux-x64-musl": "@dprint/linux-x64-musl@0.43.2", + "@dprint/win32-x64": "@dprint/win32-x64@0.43.2" + } + }, "ts-pattern@5.0.5": { "integrity": "sha512-tL0w8U/pgaacOmkb9fRlYzWEUDCfVjjv9dD4wHTgZ61MjhuMt46VNWTG747NqW6vRzoWIKABVhFSOJ82FvXrfA==", "dependencies": {} diff --git a/doc/src/content/docs/en/contribute/contributing.md b/doc/src/content/docs/en/contribute/contributing.md index e59902936b10..b0b0a4047e90 100644 --- a/doc/src/content/docs/en/contribute/contributing.md +++ b/doc/src/content/docs/en/contribute/contributing.md @@ -83,6 +83,12 @@ VSCode, you can set following configuration to auto-format markdown files on sav } ``` +### Lua + +Lua files are formatted using [`dprint`](https://dprint.dev)'s built-in formatter. Run +[`deno task dprint fmt`](https://dprint.dev/plugins/lua) anywhere to format Lua files. For details, +see [Lua Style Guide](../mod/lua/explanation/lua_style.md). + ## Translations The translation of Cataclysm: BN is done using Transifex. Look at the diff --git a/doc/src/content/docs/en/mod/json/explanation/json_style.md b/doc/src/content/docs/en/mod/json/explanation/json_style.md index a2e26ae01202..1be5fd2ae04d 100644 --- a/doc/src/content/docs/en/mod/json/explanation/json_style.md +++ b/doc/src/content/docs/en/mod/json/explanation/json_style.md @@ -10,8 +10,14 @@ Like in [C++ Code Style](../../../dev/explanation/code_style.md), the JSON styli update JSON as it is added or edited, and in relatively small chunks otherwise in order to prevent undue disruption to development. -We haven't been able to find a decent JSON styling tool, so we wrote our own. It lives in -tools/format/format.cpp and it leverages src/json.cpp to parse and emit JSON. +## Why do we have a homegrown JSON formatter? + +DDA wrote their own JSON parser. It lives in `tools/format/format.cpp` and it leverages +`src/json.cpp`to parse and emit JSON. + +This isn't optimal solution as it makes using existing JSON formatters (e.g `deno fmt`) impossible, +but [last attempt](https://github.com/cataclysmbnteam/Cataclysm-BN/pull/3118) proved that the +disadvantages outweighed possible benefits. ## JSON Example diff --git a/doc/src/content/docs/en/mod/lua/explanation/lua_style.md b/doc/src/content/docs/en/mod/lua/explanation/lua_style.md new file mode 100644 index 000000000000..02b4c5d17603 --- /dev/null +++ b/doc/src/content/docs/en/mod/lua/explanation/lua_style.md @@ -0,0 +1,31 @@ +--- +title: Lua Style Guide +--- + +As Lua ecosystem is a new addition to the project, we only have formatting guidelines. + +## Formatting + +We format Lua files with [dprint-plugin-stylua](https://github.com/RubixDev/dprint-plugin-stylua) +with default configuration. + +To format lua files, run: + +```sh +deno task dprint fmt +``` + +### Formatting Lua files in VSCode + +add following lines on `.vscode/settings.json`: + +```json +{ + "[lua]": { + "editor.formatOnSave": true, + "editor.defaultFormatter": "dprint.dprint" + } +} +``` + +now saving your lua files will also format them. diff --git a/dprint.json b/dprint.json new file mode 100644 index 000000000000..dc8023482546 --- /dev/null +++ b/dprint.json @@ -0,0 +1,13 @@ +{ + "includes": [ + "data/raw/**/*.lua", + "data/mods/**/*.lua", + "tests/**/*.lua" + ], + "excludes": [ + + ], + "plugins": [ + "https://plugins.dprint.dev/RubixDev/stylua-v0.2.1.wasm" + ] +} diff --git a/tests/catalua_test.cpp b/tests/catalua_test.cpp index 6d21217e838d..a381c91365db 100644 --- a/tests/catalua_test.cpp +++ b/tests/catalua_test.cpp @@ -127,9 +127,9 @@ TEST_CASE( "lua_runtime_error", "[lua]" ) const std::string expected = "Script runtime error in tests/lua/runtime_error.lua: " - "tests/lua/runtime_error.lua:3: attempt to index a nil value (global 'table_with_typo')\n" + "tests/lua/runtime_error.lua:2: attempt to index a nil value (global 'table_with_typo')\n" "stack traceback:\n" - "\ttests/lua/runtime_error.lua:3: in main chunk"; + "\ttests/lua/runtime_error.lua:2: in main chunk"; REQUIRE_THROWS_MATCHES( run_lua_test_script( lua, "runtime_error.lua" ), @@ -147,10 +147,10 @@ TEST_CASE( "lua_called_error_on_lua_side", "[lua]" ) const std::string expected = "Script runtime error in tests/lua/called_error_on_lua_side.lua: " - "tests/lua/called_error_on_lua_side.lua:3: Error called on Lua side!\n" + "tests/lua/called_error_on_lua_side.lua:2: Error called on Lua side!\n" "stack traceback:\n" "\t[C]: in function 'base.error'\n" - "\ttests/lua/called_error_on_lua_side.lua:3: in main chunk"; + "\ttests/lua/called_error_on_lua_side.lua:2: in main chunk"; REQUIRE_THROWS_MATCHES( run_lua_test_script( lua, "called_error_on_lua_side.lua" ), @@ -175,10 +175,10 @@ TEST_CASE( "lua_called_error_on_cpp_side", "[lua]" ) const std::string expected = "Script runtime error in tests/lua/called_error_on_cpp_side.lua: " - "tests/lua/called_error_on_cpp_side.lua:3: Error called on Cpp side!\n" + "tests/lua/called_error_on_cpp_side.lua:2: Error called on Cpp side!\n" "stack traceback:\n" "\t[C]: in function 'base.cpp_call_error'\n" - "\ttests/lua/called_error_on_cpp_side.lua:3: in main chunk"; + "\ttests/lua/called_error_on_cpp_side.lua:2: in main chunk"; REQUIRE_THROWS_MATCHES( run_lua_test_script( lua, "called_error_on_cpp_side.lua" ), @@ -207,7 +207,7 @@ TEST_CASE( "lua_called_cpp_func_throws", "[lua]" ) "Exception thrown on Cpp side!\n" "stack traceback:\n" "\t[C]: in function 'base.cpp_throw_exception'\n" - "\ttests/lua/called_cpp_func_throws.lua:3: in main chunk"; + "\ttests/lua/called_cpp_func_throws.lua:2: in main chunk"; REQUIRE_THROWS_MATCHES( run_lua_test_script( lua, "called_cpp_func_throws.lua" ), diff --git a/tests/lua/called_cpp_func_throws.lua b/tests/lua/called_cpp_func_throws.lua index 70d91db17286..214ada06b276 100644 --- a/tests/lua/called_cpp_func_throws.lua +++ b/tests/lua/called_cpp_func_throws.lua @@ -1,3 +1,2 @@ - -- Calling this C++ function will throw std::runtime_error cpp_throw_exception() diff --git a/tests/lua/called_error_on_cpp_side.lua b/tests/lua/called_error_on_cpp_side.lua index e60547981ed0..f6225c3ae4ba 100644 --- a/tests/lua/called_error_on_cpp_side.lua +++ b/tests/lua/called_error_on_cpp_side.lua @@ -1,3 +1,2 @@ - -- This C++ function will call Lua error() function cpp_call_error() diff --git a/tests/lua/called_error_on_lua_side.lua b/tests/lua/called_error_on_lua_side.lua index 3a7edcb6e9c5..25064dfee33c 100644 --- a/tests/lua/called_error_on_lua_side.lua +++ b/tests/lua/called_error_on_lua_side.lua @@ -1,3 +1,2 @@ - -- Just plain Lua error, C++ must be able to handle it error("Error called on Lua side!") diff --git a/tests/lua/called_from_cpp_test.lua b/tests/lua/called_from_cpp_test.lua index 416ed81f1625..45105a04697f 100644 --- a/tests/lua/called_from_cpp_test.lua +++ b/tests/lua/called_from_cpp_test.lua @@ -1,18 +1,18 @@ -- Initialize function output table test_data["out"] = { - s = "", - i = 0, + s = "", + i = 0, } local func = function(i, s) - -- Add to existing value - test_data.out.i = test_data.out.i + i - -- Append string, but only if string is passed - if s ~= nil then - test_data.out.s = test_data.out.s .. s - end - -- Return some new integer value - return i * 2 + -- Add to existing value + test_data.out.i = test_data.out.i + i + -- Append string, but only if string is passed + if s ~= nil then + test_data.out.s = test_data.out.s .. s + end + -- Return some new integer value + return i * 2 end -- Put function into table to be used by C++ diff --git a/tests/lua/class_members_test.lua b/tests/lua/class_members_test.lua index 02d11719ad50..665db2678ce0 100644 --- a/tests/lua/class_members_test.lua +++ b/tests/lua/class_members_test.lua @@ -1,12 +1,11 @@ - -- We receive Point as input local val_in = test_data["in"] -- We do some C++-defined operations on it -local p = val_in:abs() + Point.new( 2, 3 ) +local p = val_in:abs() + Point.new(2, 3) -- We use direct member access to turn it into a string -local val_out = "result is Point("..tostring(p.x)..","..tostring(p.y)..")" +local val_out = "result is Point(" .. tostring(p.x) .. "," .. tostring(p.y) .. ")" -- We output single string as test result test_data["out"] = val_out diff --git a/tests/lua/global_functions_test.lua b/tests/lua/global_functions_test.lua index 9dcf412632f0..ce5ab4d55d7e 100644 --- a/tests/lua/global_functions_test.lua +++ b/tests/lua/global_functions_test.lua @@ -1,11 +1,10 @@ - -- Get global avatar instance local av = gapi.get_avatar() -- Get avatar name. -- Original C++ function accepts Character reference, -- but we don't need to care about that, inheritance works. -local av_name = gapi.get_character_name( av ) +local av_name = gapi.get_character_name(av) -- We output single string as test result test_data["out"] = av_name diff --git a/tests/lua/id_conversions.lua b/tests/lua/id_conversions.lua index 258f4a1e2f5b..6d8e1e826d3d 100644 --- a/tests/lua/id_conversions.lua +++ b/tests/lua/id_conversions.lua @@ -6,22 +6,21 @@ local func_raw = test_data.func_raw local func_str_id = test_data.func_str_id local func_int_id = test_data.func_int_id -assert( str_id:implements_int_id() ) +assert(str_id:implements_int_id()) -- Functions directly accept their expected result -func_raw( raw_ptr ) -func_str_id( str_id ) -func_int_id( int_id ) +func_raw(raw_ptr) +func_str_id(str_id) +func_int_id(int_id) -- Converting raw -> id -func_raw( str_id:obj() ) -func_raw( int_id:obj() ) +func_raw(str_id:obj()) +func_raw(int_id:obj()) -- Converting id -> raw -func_int_id( raw_ptr:int_id() ) -func_str_id( raw_ptr:str_id() ) +func_int_id(raw_ptr:int_id()) +func_str_id(raw_ptr:str_id()) -- Converting str_id <-> int_id -func_int_id( str_id:int_id() ) -func_str_id( int_id:str_id() ) - +func_int_id(str_id:int_id()) +func_str_id(int_id:str_id()) diff --git a/tests/lua/id_conversions_no_int_id.lua b/tests/lua/id_conversions_no_int_id.lua index 0d3517fa62a3..4b853342f7ba 100644 --- a/tests/lua/id_conversions_no_int_id.lua +++ b/tests/lua/id_conversions_no_int_id.lua @@ -4,15 +4,14 @@ local str_id = test_data.str_id local func_raw = test_data.func_raw local func_str_id = test_data.func_str_id -assert( not str_id:implements_int_id() ) +assert(not str_id:implements_int_id()) -- Functions directly accept their expected result -func_raw( raw_ptr ) -func_str_id( str_id ) +func_raw(raw_ptr) +func_str_id(str_id) -- Converting raw -> id -func_raw( str_id:obj() ) +func_raw(str_id:obj()) -- Converting id -> raw -func_str_id( raw_ptr:str_id() ) - +func_str_id(raw_ptr:str_id()) diff --git a/tests/lua/regression_sol_1444.lua b/tests/lua/regression_sol_1444.lua index 974106b68327..119fef57eb23 100644 --- a/tests/lua/regression_sol_1444.lua +++ b/tests/lua/regression_sol_1444.lua @@ -1,3 +1,2 @@ - -assert( tests_lib.my_awesome_lambda_1() == 1 ) -assert( tests_lib.my_awesome_lambda_2() == 2 ) +assert(tests_lib.my_awesome_lambda_1() == 1) +assert(tests_lib.my_awesome_lambda_2() == 2) diff --git a/tests/lua/runtime_error.lua b/tests/lua/runtime_error.lua index a9ab2e55ff6a..1c4483480e25 100644 --- a/tests/lua/runtime_error.lua +++ b/tests/lua/runtime_error.lua @@ -1,3 +1,2 @@ - -- Let's try to write into a non-existent table table_with_typo.status = -1