Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added fast replace to build #9

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 73 additions & 65 deletions commands.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local util = require("utility_functions")

-- Initial definitions of shortcuts used by the TAScommands
local directions = {}
directions["STOP"] = {walking = false}
Expand All @@ -15,32 +17,32 @@ 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)
amt = myplayer.begin_crafting{recipe = tokens[2], count = tokens[3] or 1}
if amt ~= (tokens[3] or 1) then
errprint("Tried to craft with insufficient ingredients!")
errprint("You were trying to make " .. (tokens[3] or 1) .. "x" ..tokens[2])
util.errprint("Tried to craft with insufficient ingredients!")
util.errprint("You were trying to make " .. (tokens[3] or 1) .. "x" ..tokens[2])
else
debugprint("Crafting: " .. tokens[2] .. " x" .. (tokens[3] or 1))
util.debugprint("Crafting: " .. tokens[2] .. " x" .. (tokens[3] or 1))
end
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
Expand All @@ -51,43 +53,49 @@ 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] .. ")")

-- Check if we have the item
if myplayer.get_item_count(item) == 0 then
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")
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")
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 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 can_replace = util.can_fast_replace(item, position, myplayer)

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 = 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})
else
util.errprint("Build failed: Reason unknown.")
end
end

TAScommands["put"] = function (tokens, myplayer)
Expand All @@ -99,12 +107,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

Expand All @@ -113,36 +121,36 @@ 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

local inserted = otherinv.insert{name=item, count=toinsert}

--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

Expand All @@ -154,20 +162,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

Expand All @@ -178,29 +186,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)
Expand All @@ -210,7 +218,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 = {1000000,100000}, force="player", recipe=tokens[3]}
Expand All @@ -221,7 +229,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)
Expand All @@ -231,11 +239,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 .. ".")
myplayer.selected.direction = directions[direction]["direction"]
util.debugprint("Rotating " .. myplayer.selected.name .. " so that it faces " .. direction .. ".")
end

return TAScommands
16 changes: 8 additions & 8 deletions control.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require("util")
require("utility_functions")
local util = require("utility_functions")
require("silo-script")

-- Global variables initialization
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading