From 504d62d9991eb7fa876e37d57120efaacc21d95c Mon Sep 17 00:00:00 2001 From: Bilka Date: Thu, 26 Oct 2017 10:47:21 +0200 Subject: [PATCH 1/5] Utilities returns table --- commands.lua | 76 ++++++++++++++++++++++--------------------- control.lua | 16 ++++----- utility_functions.lua | 17 ++++++---- 3 files changed, 57 insertions(+), 52 deletions(-) diff --git a/commands.lua b/commands.lua index 2f69cf0..a340443 100644 --- a/commands.lua +++ b/commands.lua @@ -1,3 +1,5 @@ +local util = require("utility_functions") + -- Initial definitions of shortcuts used by the TAScommands local directions = {} directions["STOP"] = {walking = false} @@ -15,27 +17,27 @@ local TAScommands = {} -- Definitions of the TAScommands TAScommands["move"] = function (tokens, myplayer) - debugprint("Moving: " .. tokens[2]) + util.debugprint("Moving: " .. tokens[2]) global.walkstate = directions[tokens[2]] if tokens[2] == "STOP" then - debugprint("Stopped at: (" .. myplayer.position.x .. "," .. myplayer.position.y .. ")") + util.debugprint("Stopped at: (" .. myplayer.position.x .. "," .. myplayer.position.y .. ")") end end TAScommands["craft"] = function (tokens, myplayer) myplayer.begin_crafting{recipe = tokens[2], count = tokens[3] or 1} - debugprint("Crafting: " .. tokens[2] .. " x" .. (tokens[3] or 1)) + util.debugprint("Crafting: " .. tokens[2] .. " x" .. (tokens[3] or 1)) end TAScommands["stopcraft"] = function (tokens, myplayer) myplayer.cancel_crafting{index = tokens[2], count = tokens[3] or 1} - debugprint("Craft abort: Index " .. tokens[2] .. " x" .. (tokens[3] or 1)) + util.debugprint("Craft abort: Index " .. tokens[2] .. " x" .. (tokens[3] or 1)) end TAScommands["mine"] = function (tokens, myplayer) local position = tokens[2] if position then - if position[1] ~= roundn(position[1]) or position[2] ~= roundn(position[2]) then + if position[1] ~= util.roundn(position[1]) or position[2] ~= util.roundn(position[2]) then hasdecimals = true else hasdecimals = false @@ -46,33 +48,33 @@ TAScommands["mine"] = function (tokens, myplayer) else global.minestate = {position[1] + 0.5, position[2] + 0.5} end if position then - if hasdecimals then debugprint("Mining: Coordinates (" .. position[1] .. "," .. position[2] .. ")") - else debugprint("Mining: Tile (" .. position[1] .. "," .. position[2] .. ")") end - else debugprint("Mining: STOP") end + if hasdecimals then util.debugprint("Mining: Coordinates (" .. position[1] .. "," .. position[2] .. ")") + else util.debugprint("Mining: Tile (" .. position[1] .. "," .. position[2] .. ")") end + else util.debugprint("Mining: STOP") end end TAScommands["build"] = function (tokens, myplayer) local item = tokens[2] local position = tokens[3] local direction = tokens[4] - debugprint("Building: " .. item .. " on tile (" .. position[1] .. "," .. position[2] .. ")") + util.debugprint("Building: " .. item .. " on tile (" .. position[1] .. "," .. position[2] .. ")") -- Check if we have the item if myplayer.get_item_count(item) == 0 then - errprint("Build failed: No item available") + util.errprint("Build failed: No item available") return end -- Check if we are in reach of this tile - if not inrange(position, myplayer) then - errprint("Build failed: You are trying to place beyond realistic reach") + if not util.inrange(position, myplayer) then + util.errprint("Build failed: You are trying to place beyond realistic reach") return end -- Check if we can actually place the item at this tile local canplace = myplayer.surface.can_place_entity{name = item, position = position, direction = direction, force = "player"} if not canplace then - errprint("Build failed: Something is in the way") + util.errprint("Build failed: Something is in the way") return end @@ -81,7 +83,7 @@ TAScommands["build"] = function (tokens, myplayer) asm = myplayer.surface.create_entity{name = item, position = position, direction = direction, force="player"} -- Remove the placed item from the player (since he has now spent it) if asm then myplayer.remove_item({name = item, count = 1}) - else errprint("Build failed: Reason unknown.") end + else util.errprint("Build failed: Reason unknown.") end end @@ -94,12 +96,12 @@ TAScommands["put"] = function (tokens, myplayer) myplayer.update_selected_entity(position) if not myplayer.selected then - errprint("Put failed: No object at position {" .. position[1] .. "," .. position[2] .. "}.") + util.errprint("Put failed: No object at position {" .. position[1] .. "," .. position[2] .. "}.") return end - if not inrange(position, myplayer) then - errprint("Put failed: You are trying to reach too far.") + if not util.inrange(position, myplayer) then + util.errprint("Put failed: You are trying to reach too far.") return end @@ -108,11 +110,11 @@ TAScommands["put"] = function (tokens, myplayer) local toinsert = math.min(amountininventory, amount) if toinsert == 0 then - errprint("Put failed: No items") + util.errprint("Put failed: No items") return end if not otherinv then - errprint("Put failed : Target doesn't have an inventory at {" .. position[1] .. "," .. position[2] .. "}.") + util.errprint("Put failed : Target doesn't have an inventory at {" .. position[1] .. "," .. position[2] .. "}.") return end @@ -120,24 +122,24 @@ TAScommands["put"] = function (tokens, myplayer) --if we already failed for trying to insert no items, then if no items were inserted, it must be because it is full if inserted == 0 then - errprint("Put failed: No space at {" .. position[1] .. "," .. position[2] .. "}.") + util.errprint("Put failed: No space at {" .. position[1] .. "," .. position[2] .. "}.") return end myplayer.remove_item{name=item, count = inserted} if inserted < amount then - errprint("Put sub-optimal: Only put " .. inserted .. "x " .. item .. " instead of " .. amount .. "x " .. item .. " at {" .. position[1] .. "," .. position[2] .. "}.") + util.errprint("Put sub-optimal: Only put " .. inserted .. "x " .. item .. " instead of " .. amount .. "x " .. item .. " at {" .. position[1] .. "," .. position[2] .. "}.") end - debugprint("Put " .. inserted .. "x " .. item .. " into " .. myplayer.selected.name .. " at {" .. position[1] .. "," .. position[2] .. "}.") + util.debugprint("Put " .. inserted .. "x " .. item .. " into " .. myplayer.selected.name .. " at {" .. position[1] .. "," .. position[2] .. "}.") end TAScommands["speed"] = function (tokens, myplayer) if global.allowspeed then game.speed = tokens[2] - debugprint("Speed: " .. tokens[2]) + util.debugprint("Speed: " .. tokens[2]) else - errprint("Speed failed : Changing the speed of the run is not allowed. ") + util.errprint("Speed failed : Changing the speed of the run is not allowed. ") end end @@ -149,20 +151,20 @@ TAScommands["take"] = function (tokens, myplayer) myplayer.update_selected_entity(position) if not myplayer.selected then - errprint("Take failed: No object at position {" .. position[1] .. "," .. position[2] .. "}.") + util.errprint("Take failed: No object at position {" .. position[1] .. "," .. position[2] .. "}.") return end -- Check if we are in reach of this tile - if not inrange(position, myplayer) then - errprint("Take failed: You are trying to reach too far.") + if not util.inrange(position, myplayer) then + util.errprint("Take failed: You are trying to reach too far.") return end local otherinv = myplayer.selected.get_inventory(slot) if not otherinv then - errprint("Take failed: Unable to access inventories") + util.errprint("Take failed: Unable to access inventories") return end @@ -173,29 +175,29 @@ TAScommands["take"] = function (tokens, myplayer) end if amountintarget == 0 then - errprint("Take failed: No items at {" .. position[1] .. "," .. position[2] .. "}.") + util.errprint("Take failed: No items at {" .. position[1] .. "," .. position[2] .. "}.") return end local taken = myplayer.insert{name=item, count=totake} - debugprint("Took " .. taken .. "x " .. item .. " from " .. myplayer.selected.name .. " at {" .. position[1] .. "," .. position[2] .. "}.") + util.debugprint("Took " .. taken .. "x " .. item .. " from " .. myplayer.selected.name .. " at {" .. position[1] .. "," .. position[2] .. "}.") if taken == 0 then - errprint("Take failed: No space at {" .. position[1] .. "," .. position[2] .. "}.") + util.errprint("Take failed: No space at {" .. position[1] .. "," .. position[2] .. "}.") return end otherinv.remove{name=item, count=taken} if amount ~= "all" and taken < amount then - errprint("Take sub-optimal: Only took " .. taken .. " at {" .. position[1] .. "," .. position[2] .. "}.") + util.errprint("Take sub-optimal: Only took " .. taken .. " at {" .. position[1] .. "," .. position[2] .. "}.") end end TAScommands["tech"] = function (tokens, myplayer) myplayer.force.current_research = tokens[2] - debugprint("Research: " .. tokens[2]) + util.debugprint("Research: " .. tokens[2]) end TAScommands["print"] = function (tokens, myplayer) @@ -205,7 +207,7 @@ end TAScommands["recipe"] = function (tokens, myplayer) myplayer.update_selected_entity(tokens[2]) if not myplayer.selected then - errprint("Setting recipe: Entity at position {" .. tokens[2][1] .. "," .. tokens[2][2] .. "} could not be selected.") + util.errprint("Setting recipe: Entity at position {" .. tokens[2][1] .. "," .. tokens[2][2] .. "} could not be selected.") return end local ent = myplayer.surface.create_entity{name = myplayer.selected.name, position = {100000,100000}, force="player", recipe=tokens[3]} @@ -216,7 +218,7 @@ TAScommands["recipe"] = function (tokens, myplayer) myplayer.insert{name=name, count=count} end end - debugprint("Setting recipe: " .. tokens[3] .. " at position {" .. tokens[2][1] .. "," .. tokens[2][2] .. "}.") + util.debugprint("Setting recipe: " .. tokens[3] .. " at position {" .. tokens[2][1] .. "," .. tokens[2][2] .. "}.") end TAScommands["rotate"] = function (tokens, myplayer) @@ -226,11 +228,11 @@ TAScommands["rotate"] = function (tokens, myplayer) myplayer.update_selected_entity(position) if not myplayer.selected then - errprint ("Rotate failed, no object at position {" .. position[1] .. "," .. position[2] .. "}") + util.errprint ("Rotate failed, no object at position {" .. position[1] .. "," .. position[2] .. "}") end myplayer.selected.direction = directions[direction] - debugprint("Rotating " .. myplayer.selected.name .. " so that it faces " .. direction .. ".") + util.debugprint("Rotating " .. myplayer.selected.name .. " so that it faces " .. direction .. ".") end return TAScommands diff --git a/control.lua b/control.lua index 2ee50b6..7a3dad9 100644 --- a/control.lua +++ b/control.lua @@ -1,5 +1,5 @@ require("util") -require("utility_functions") +local util = require("utility_functions") require("silo-script") -- Global variables initialization @@ -52,15 +52,15 @@ end -- This function initializes the run's clock and a few properties local function init_run(myplayer_index) - debugprint("Initializing the run") + util.debugprint("Initializing the run") -- Examine the command queue for errors. if not commandqueue then - errprint("The command queue is empty! No point in starting.") + util.errprint("The command queue is empty! No point in starting.") return end - debugprint("Command queue size is " .. table_size(commandqueue)) --includes settings "field" + util.debugprint("Command queue size is " .. table_size(commandqueue)) --includes settings "field" if max_tick == 0 then - errprint("The command queue is empty! No point in starting.") + util.errprint("The command queue is empty! No point in starting.") return end if not commandqueue.settings then @@ -69,7 +69,7 @@ local function init_run(myplayer_index) end -- Applying command queue settings global.allowspeed = commandqueue.settings.allowspeed - debugprint("Changing the speed of the run through commands is " .. ((global.allowspeed and "allowed") or "forbidden") .. ".") + util.debugprint("Changing the speed of the run through commands is " .. ((global.allowspeed and "allowed") or "forbidden") .. ".") -- Initiating the game: -- Prepare the players: -- Prepare the runner @@ -95,7 +95,7 @@ local function init_run(myplayer_index) end global.start_tick = game.tick - debugprint("Starting tick is " .. global.start_tick) + util.debugprint("Starting tick is " .. global.start_tick) global.running = true end @@ -165,7 +165,7 @@ script.on_event(defines.events.on_player_joined_game, function (event) end) script.on_event(defines.events.on_research_finished, function (event) - debugprint("Researched " .. event.research.name) + util.debugprint("Researched " .. event.research.name) end) -- Create the interface and command that allow to launch a run diff --git a/utility_functions.lua b/utility_functions.lua index aaace9a..cecfd06 100644 --- a/utility_functions.lua +++ b/utility_functions.lua @@ -1,14 +1,15 @@ -- Utility functions +local util = {} -function roundn(x) +function util.roundn(x) return x + 0.5 - (x + 0.5) % 1 end -function inrange(position, myplayer) +function util.inrange(position, myplayer) return ((position[1]-myplayer.position.x)^2+(position[2]-myplayer.position.y)^2) < 36 end -function prettytime() +function util.prettytime() local tick = game.tick - (global.start_tick or 0) if settings.global["tas-pretty-time"].value then local hours = string.format("%02.f", math.floor(tick / 216000)) @@ -24,14 +25,16 @@ function prettytime() return "[" .. tick .. "] " end -function debugprint(msg) +function util.debugprint(msg) for _, player in pairs(game.connected_players) do if player.mod_settings["tas-verbose-logging"].value then - player.print(prettytime() .. msg) + player.print(util.prettytime() .. msg) end end end -function errprint(msg) - game.print(prettytime() .. " ___WARNING___ " .. msg) +function util.errprint(msg) + game.print(util.prettytime() .. " ___WARNING___ " .. msg) end + +return util From dcf23c4995899d1ecc82fbc5dd3670768638a49a Mon Sep 17 00:00:00 2001 From: Bilka Date: Thu, 26 Oct 2017 11:41:02 +0200 Subject: [PATCH 2/5] Added fast replace to build --- commands.lua | 77 ++++++++++++++++++++++++++----------------- utility_functions.lua | 40 +++++++++++++--------- 2 files changed, 71 insertions(+), 46 deletions(-) diff --git a/commands.lua b/commands.lua index a340443..036d75e 100644 --- a/commands.lua +++ b/commands.lua @@ -54,37 +54,52 @@ TAScommands["mine"] = function (tokens, myplayer) end TAScommands["build"] = function (tokens, myplayer) - local item = tokens[2] - local position = tokens[3] - local direction = tokens[4] - util.debugprint("Building: " .. item .. " on tile (" .. position[1] .. "," .. position[2] .. ")") - - -- Check if we have the item - if myplayer.get_item_count(item) == 0 then - util.errprint("Build failed: No item available") - return - end - - -- Check if we are in reach of this tile - if not util.inrange(position, myplayer) then - util.errprint("Build failed: You are trying to place beyond realistic reach") - return - end - - -- Check if we can actually place the item at this tile - local canplace = myplayer.surface.can_place_entity{name = item, position = position, direction = direction, force = "player"} - if not canplace then - util.errprint("Build failed: Something is in the way") - return - end - - -- If no errors, proceed to actually building things - -- Place the item - asm = myplayer.surface.create_entity{name = item, position = position, direction = direction, force="player"} - -- Remove the placed item from the player (since he has now spent it) - if asm then myplayer.remove_item({name = item, count = 1}) - else util.errprint("Build failed: Reason unknown.") end - + local item = tokens[2] + local position = tokens[3] + local direction = tokens[4] + util.debugprint("Building: " .. item .. " on tile (" .. position[1] .. "," .. position[2] .. ")") + + -- Check if we have the item + if myplayer.get_item_count(item) == 0 then + util.errprint("Build failed: No item available") + return + end + + -- Check if we are in reach of this tile + if not util.inrange(position, myplayer) then + util.errprint("Build failed: You are trying to place beyond realistic reach") + return + end + + -- Check if we can actually place the item at this tile + local canplace = myplayer.surface.can_place_entity{name = item, position = position, direction = direction, force = "player"} + + -- Check if we can fast replace + local fast_replace_entity = util.can_fast_replace(item, position, myplayer) + local rplc = false + local return_item + if fast_replace_entity then + return_item = fast_replace_entity.name + rplc = true + end + + if (not canplace) and (not fast_replace_entity) then + util.errprint("Build failed: Something that can't be fast replaced is in the way") + return + end + + -- If no errors, proceed to actually building things + -- Place the item + local created = myplayer.surface.create_entity{name = item, position = position, direction = direction, force="player", fast_replace = rplc} + -- Remove the placed item from the player (since he has now spent it) + if created and created.valid then + myplayer.remove_item({name = item, count = 1}) + if return_item then + myplayer.insert{name = return_item, count = 1} + end + else + util.errprint("Build failed: Reason unknown.") + end end TAScommands["put"] = function (tokens, myplayer) diff --git a/utility_functions.lua b/utility_functions.lua index cecfd06..295c064 100644 --- a/utility_functions.lua +++ b/utility_functions.lua @@ -2,27 +2,37 @@ local util = {} function util.roundn(x) - return x + 0.5 - (x + 0.5) % 1 + return x + 0.5 - (x + 0.5) % 1 end function util.inrange(position, myplayer) - return ((position[1]-myplayer.position.x)^2+(position[2]-myplayer.position.y)^2) < 36 + return ((position[1]-myplayer.position.x)^2+(position[2]-myplayer.position.y)^2) < 36 end +function util.can_fast_replace(name, position, myplayer) + local entity_prototypes = game.entity_prototypes + local blocking_entity = myplayer.surface.find_entities_filtered{position = position, force=myplayer.force}[1] + + if blocking_entity and entity_prototypes[blocking_entity.name].fast_replaceable_group == entity_prototypes[name].fast_replaceable_group and name ~= blocking_entity.name then + return blocking_entity + end +end + + function util.prettytime() - local tick = game.tick - (global.start_tick or 0) - if settings.global["tas-pretty-time"].value then - local hours = string.format("%02.f", math.floor(tick / 216000)) - local minutes = string.format("%02.f", math.floor(tick / 3600) - hours * 60) - local seconds = string.format("%02.f", math.floor(tick / 60) - hours * 3600 - minutes * 60) - local ticks = string.format("%02.f", tick - hours * 216000 - minutes * 3600 - seconds * 60) - if hours == "00" then - return "[" .. minutes .. ":" .. seconds .. ":" .. ticks .. "] " - else - return "[" .. hours .. ":" .. minutes .. ":" .. seconds .. ":" .. ticks .. "] " - end - end - return "[" .. tick .. "] " + local tick = game.tick - (global.start_tick or 0) + if settings.global["tas-pretty-time"].value then + local hours = string.format("%02.f", math.floor(tick / 216000)) + local minutes = string.format("%02.f", math.floor(tick / 3600) - hours * 60) + local seconds = string.format("%02.f", math.floor(tick / 60) - hours * 3600 - minutes * 60) + local ticks = string.format("%02.f", tick - hours * 216000 - minutes * 3600 - seconds * 60) + if hours == "00" then + return "[" .. minutes .. ":" .. seconds .. ":" .. ticks .. "] " + else + return "[" .. hours .. ":" .. minutes .. ":" .. seconds .. ":" .. ticks .. "] " + end + end + return "[" .. tick .. "] " end function util.debugprint(msg) From e4ca02ef8cf31753c58a22ea6c1d056f1852ca7f Mon Sep 17 00:00:00 2001 From: Bilka Date: Thu, 26 Oct 2017 12:08:25 +0200 Subject: [PATCH 3/5] Reading the api is good Missed the player param in create_entity lol --- commands.lua | 15 +++------------ utility_functions.lua | 3 ++- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/commands.lua b/commands.lua index 036d75e..82ddab5 100644 --- a/commands.lua +++ b/commands.lua @@ -75,28 +75,19 @@ TAScommands["build"] = function (tokens, myplayer) local canplace = myplayer.surface.can_place_entity{name = item, position = position, direction = direction, force = "player"} -- Check if we can fast replace - local fast_replace_entity = util.can_fast_replace(item, position, myplayer) - local rplc = false - local return_item - if fast_replace_entity then - return_item = fast_replace_entity.name - rplc = true - end + local can_replace = util.can_fast_replace(item, position, myplayer) - if (not canplace) and (not fast_replace_entity) then + if (not canplace) and (not can_replace) then util.errprint("Build failed: Something that can't be fast replaced is in the way") return end -- If no errors, proceed to actually building things -- Place the item - local created = myplayer.surface.create_entity{name = item, position = position, direction = direction, force="player", fast_replace = rplc} + local created = myplayer.surface.create_entity{name = item, position = position, direction = direction, force="player", fast_replace = can_replace, player = myplayer} -- Remove the placed item from the player (since he has now spent it) if created and created.valid then myplayer.remove_item({name = item, count = 1}) - if return_item then - myplayer.insert{name = return_item, count = 1} - end else util.errprint("Build failed: Reason unknown.") end diff --git a/utility_functions.lua b/utility_functions.lua index 295c064..c6125bd 100644 --- a/utility_functions.lua +++ b/utility_functions.lua @@ -14,8 +14,9 @@ function util.can_fast_replace(name, position, myplayer) local blocking_entity = myplayer.surface.find_entities_filtered{position = position, force=myplayer.force}[1] if blocking_entity and entity_prototypes[blocking_entity.name].fast_replaceable_group == entity_prototypes[name].fast_replaceable_group and name ~= blocking_entity.name then - return blocking_entity + return true end + return false end From 2d20cba0659d399daca003f14c643252d3bc7359 Mon Sep 17 00:00:00 2001 From: Bilka Date: Thu, 26 Oct 2017 12:55:45 +0200 Subject: [PATCH 4/5] Update commands.lua --- commands.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/commands.lua b/commands.lua index 82ddab5..1fd15c3 100644 --- a/commands.lua +++ b/commands.lua @@ -25,8 +25,13 @@ TAScommands["move"] = function (tokens, myplayer) end TAScommands["craft"] = function (tokens, myplayer) - myplayer.begin_crafting{recipe = tokens[2], count = tokens[3] or 1} - util.debugprint("Crafting: " .. tokens[2] .. " x" .. (tokens[3] or 1)) + amt = myplayer.begin_crafting{recipe = tokens[2], count = tokens[3] or 1} + if amt ~= (tokens[3] or 1) then + util.errprint("Tried to craft with insufficient ingredients!") + util.errprint("You were trying to make " .. (tokens[3] or 1) .. "x" ..tokens[2]) + else + util.debugprint("Crafting: " .. tokens[2] .. " x" .. (tokens[3] or 1)) + end end TAScommands["stopcraft"] = function (tokens, myplayer) From 6a07ca613f7765e60ce1ae946f77f59c800264d7 Mon Sep 17 00:00:00 2001 From: Bilka Date: Fri, 27 Oct 2017 21:13:53 +0200 Subject: [PATCH 5/5] Fix rotate command --- commands.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands.lua b/commands.lua index 424dec8..dd35cbc 100644 --- a/commands.lua +++ b/commands.lua @@ -242,7 +242,7 @@ TAScommands["rotate"] = function (tokens, myplayer) util.errprint ("Rotate failed, no object at position {" .. position[1] .. "," .. position[2] .. "}") end - myplayer.selected.direction = directions[direction] + myplayer.selected.direction = directions[direction]["direction"] util.debugprint("Rotating " .. myplayer.selected.name .. " so that it faces " .. direction .. ".") end