diff --git a/control.lua b/control.lua index f9142bbb..0d1312a8 100644 --- a/control.lua +++ b/control.lua @@ -13,12 +13,12 @@ require "fish_market" require "train_station_names" require "score" require "map_layout" -require "custom_commands" require "nuke_control" require "walk_distance" require "on_tick" require "follow" - +require "wells" +require "custom_commands" function player_joined(event) diff --git a/custom_commands.lua b/custom_commands.lua index 4f3ac049..4c095e4b 100644 --- a/custom_commands.lua +++ b/custom_commands.lua @@ -302,33 +302,6 @@ local function unfollow(cmd) end end -global.wells = {} -local function well(cmd) - if not game.player.admin then - cant_run(cmd.name) - return - end - - if cmd.parameter == nil then - return - end - - local params = {} - for param in string.gmatch(cmd.parameter, "%S+") do table.insert(params, param) end - if game.item_prototypes[params[1]] and params[2] and tonumber(params[2]) then - local ips = tonumber(params[2]) - if ips < 1 then - game.player.print("Items per second must be at least 1.") - return - end - local chest = game.surfaces[1].create_entity({name = "steel-chest", force = game.player.force, position = game.player.position}) - table.insert(global.wells, {chest = chest, item = params[1], items_per_second = math.floor(ips)}) - refill_well() - else - game.player.print("Usage: /well .") - end -end - global.tp_players = {} --global.tp_players_count = 0 @@ -403,6 +376,6 @@ commands.add_command("afktime", 'Shows how long players have been afk.', afk) commands.add_command("tag", ' Sets a players tag. (Admins only)', tag) commands.add_command("follow", ' makes you follow the player. Use /unfollow to stop following a player.', follow) commands.add_command("unfollow", 'stops following a player.', unfollow) -commands.add_command("well", ' Spawns an item well. (Admins only)', well) +commands.add_command("well", ' Spawns an item well. (Admins only)', well_command) commands.add_command("tpmode", "Toggles tp mode. When on place a ghost entity to teleport there (Admins and moderators)", toggle_tp_mode) diff --git a/on_tick.lua b/on_tick.lua index e9bab9c8..e4f3a342 100644 --- a/on_tick.lua +++ b/on_tick.lua @@ -1,9 +1,3 @@ -function refill_well() - for _,well in pairs(global.wells) do - well.chest.insert({name = well.item, count = well.items_per_second * 15}) - end -end - local function on_tick() walk_on_tick() if game.tick % 60 == 0 then @@ -21,5 +15,4 @@ local function on_tick() end end - Event.register(defines.events.on_tick, on_tick) diff --git a/wells.lua b/wells.lua new file mode 100644 index 00000000..d8da1ae4 --- /dev/null +++ b/wells.lua @@ -0,0 +1,93 @@ +global.wells = {} + +function refill_well() + local current_tick = game.tick + + local wells = global.wells + -- iterate backwards to allow for removals. + for i = #wells, 1, -1 do + local well = wells[i] + local entity = well.entity + if not entity.valid then + table.remove(wells, i) + else + local items_per_tick = well.items_per_tick + local diff = current_tick - well.last_tick + local count = diff * items_per_tick + + if count >= 1 then + local whole = math.floor(count) + entity.insert({ name = well.item, count = whole }) + + local frac = count - whole + well.last_tick = current_tick - frac / items_per_tick + end + end + end +end + +local function validate(item, items_per_second) + if not game.item_prototypes[item] then + return "item is not valid" + end + + if type(items_per_second) ~= "number" or items_per_second <= 0 then + return "items per second must be a number and greater than 0" + end +end + +local function non_validating_create_well(entity, item, items_per_second) + local well = + { + entity = entity, + item = item, + items_per_tick = items_per_second / 60, + last_tick = game.tick + } + + table.insert(global.wells, well) +end + +function create_well(entity, item, items_per_second) + if not entity or entity.type ~= "container" then + return "entity must be a container" + end + + local error = validate(item, items_per_second) + if error then + return error + end + + non_validating_create_well(entity, item, items_per_second) +end + +function well_command(cmd) + if not game.player.admin then + cant_run(cmd.name) + return + end + + if cmd.parameter == nil then + return + end + + local params = {} + for param in string.gmatch(cmd.parameter, "%S+") do table.insert(params, param) end + + if #params ~= 2 then + game.player.print("Usage: /well .") + return + end + + local error = validate(params[1], tonumber(params[2])) + if error then + game.player.print(error) + return + end + + local chest = game.player.surface.create_entity({name = "steel-chest", force = game.player.force, position = game.player.position}) + chest.minable = false; + chest.destructible = false; + + non_validating_create_well(chest, params[1], tonumber(params[2])) +end \ No newline at end of file