1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-18 03:21:47 +02:00

Merge pull request #49 from grilledham/feature-wells

rewrite of wells
This commit is contained in:
Valansch 2017-09-03 10:47:13 +02:00 committed by GitHub
commit ea540a4a62
4 changed files with 96 additions and 40 deletions

View File

@ -14,11 +14,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)
local player = game.players[event.player_index]

View File

@ -333,33 +333,6 @@ local function unfollow(cmd)
end
end
global.wells = {}
local function well(cmd)
if not game.player or 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
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
player_print("Usage: /well <item> <items per second>.")
end
end
global.tp_players = {}
local function built_entity(event)
local index = event.player_index
@ -411,5 +384,5 @@ commands.add_command("afk", 'Shows how long players have been afk.', afk)
commands.add_command("tag", '<player> <tag> Sets a players tag. (Admins only)', tag)
commands.add_command("follow", '<player> 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", '<item> <items per second> Spawns an item well. (Admins only)', well)
commands.add_command("well", '<item> <items per second> 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)

View File

@ -1,13 +1,3 @@
function refill_well()
for _,well in pairs(global.wells) do
if well.chest.valid then
well.chest.insert({name = well.item, count = well.items_per_second * 15})
else
table.remove_element(global.wells, well)
end
end
end
local function on_tick()
walk_on_tick()
if game.tick % 60 == 0 then
@ -25,5 +15,4 @@ local function on_tick()
end
end
Event.register(defines.events.on_tick, on_tick)

93
wells.lua Normal file
View File

@ -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 <item> <items per second>.")
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