First, to use this, just put the "utilities.lua" file in with your mod files/folders. I put mine in a "library" folder but it can go anywhere near your control.lua. Then, just add
Code: Select all
require "library/utilities.lua"
getTime() ------ Returns the number of ticks that have passed in-game since this instance of utilities.lua (and by extention control.lua) was started.
EXAMPLE:
Code: Select all
if getTime()>60 then -dostuff- end
EXAMPLE:
Code: Select all
if game.real then -dostuff- end ------will throw an error and stop the game, since game.real does not exist
if notNil(game, "real") then -dostuff- end ------won't throw an error
EXAMPLE:
Code: Select all
message("Try Again", true) ------ prints to a dialog (pauses game): "Try Again"
message(entity) ------ prints: "Entity: Name = biter, Type = unit, Position: 56, -190"
message({1, 5, 10, 43, 81}) ------ prints: "Table: {1, 5, 10, ... 81}"
waitToDo(ticks, function) ------ After ticks number of ticks in-game, will do function. The easiest way to do this is with a temporary, unnamed function. NOTE: to use this properly, you need to put stepTimers() in with the ontick event so that every game tick, it steps the timers. Without this step, what you put in function will not run!
EXAMPLE:
Code: Select all
waitToDo(900, ------ will wait 15 seconds and then kill [entity]
function()
entity.die
end
--------------------------------
jerry = function() ------ once called, will check every 60 seconds if [entity] is at position [target] and if so, kill it and stop checking.
if entity.position = target then entity.die else waitToDo(60, jerry) end
end
EXAMPLE:
Code: Select all
if event.name == defines.events.ontick
then
stepTimers() ------ Steps timers every tick.
end
EXAMPLE:
Code: Select all
newpos = moveTowards(oldpos, defines.direction.east, 5) ------ returns a position 5 tiles east of oldpos
newpos = moveTowards(moveTowards(oldpos, defines.direction.east, 3), defines.direction.north, 4) ------ returns a new position 3 tiles east and 4 tiles north of oldpos
left(direction) ------ returns the direction 90 degrees to the left of direction
modPosition(position, modx, mody) ------ returns a new position starting at position, modx distance in the x axis and mody in the y axis.
areaAround(start, radius) ------ returns an area with the center at position start and of a radius of radius.
EXAMPLE:
Code: Select all
areaAround(entity.position, 10) ------ returns a 20x20 area centered on "entity"
positionInChunk(position) ------ returns what chunk position is in. chunks have x and y like position, starting at 0,0 and are 32x32 tiles
EXAMPLE:
Code: Select all
positionInChunk({x = 5, y = 1}) ------ returns 0,0
EXAMPLE:
Code: Select all
chunkArea({x=0,y=0}) ------ returns area = {{0,0},{32,32}}
EXAMPLE:
Code: Select all
mergeTables({"time", 5, true}, {"warp", 0, false}) ------ returns a table: {"time", 5, true, "warp", 0, false}
EXAMPLE:
Code: Select all
findEntitiesInGeneratedChunks({"assembling-machine-1", "assembling-machine-2", "assembling-machine-3"})
------ returns a table containing all the assembling machines of any tier in this world