-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.lua
More file actions
203 lines (168 loc) · 5.29 KB
/
lib.lua
File metadata and controls
203 lines (168 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
require ("util")
-------------------------------------------------------------------------------
function new_class(members)
members = members or {}
local mt = {
__metatable = members;
__index = members;
}
local function new(_, init)
return setmetatable(init or {}, mt)
end
local function copy(obj, ...)
local newobj = obj:new(unpack(arg))
for n,v in pairs(obj) do newobj[n] = v end
return newobj
end
members.new = members.new or new
members.copy = members.copy or copy
return mt
end
-------------------------------------------------------------------------------
-- entity / object
function is_valid(entity)
return (entity ~= nil and entity.valid)
end
function is_valid_(chunk)
local f = load('return '..chunk)
local ok, ret = pcall(f)
return ok and ret or nil -- “a ? b : c”
end
-- https://wiki.factorio.com/World_generator#Maximum_Map_Size_and_used_Memory
-- The map limit is 2,000,000 x 2,000,000 tiles
-- https://stackoverflow.com/questions/7539810/saving-a-vector-as-a-single-number
-- V = fromXY(X, Y) = (y+65000)*130001+(x+65000)
-- (X,Y) = toXY(V) = (V%130001-65000,V/130001-65000) // <= / is integer division
-- (130001 is the number of distinct values for X or Y)
function get_eid(entity)
return (entity.position.y + 2000000) * 4000001 + (entity.position.x + 2000000)
end
function eid_to_position(eid)
local x = eid % 4000001 - 2000000
local y = math.floor(eid / 4000001) - 2000000
return x, y
end
function find_object(entity)
local eid = get_eid(entity)
return global.objects[eid], eid
end
function find_tag(tag)
for i, object in pairs(global.objects) do
if object.tag == tag then return i, object; end
end
end
function find_adjacent(entity)
entities = {}
search = entity.surface.find_entities({{entity.position.x - 1, entity.position.y - 1}, {entity.position.x + 1, entity.position.y + 1}})
for _, entry in pairs(search) do
if is_valid(entry) and (entry.position.x ~= entity.position.x or entry.position.y ~= entity.position.y) then
table.insert(entities, entry)
end
end
return entities
end
-- get entity in front direction
-- local target = entity.surface.find_entities_filtered({ area = position_area(position_move(entity.position, entity.direction, 1), 0.2),
-- type = "assembling-machine"})[1]
function position_area(position, distance) -- around
return {{x = position.x - distance, y = position.y - distance}, { x = position.x + distance, y = position.y + distance}}
end
function position_move(position, direction, distance)
local x, y = position.x, position.y
if direction == defines.direction.north then y = y - distance
elseif direction == defines.direction.south then y = y + distance
elseif direction == defines.direction.east then x = x + distance
elseif direction == defines.direction.west then x = x - distance
end
return {x=x, y=y}
end
--[[
>> This are the 8-way directions:
>> 3 4 5
>> \ | /
>> 2 -- -- 6
>> / | \
>> 1 0 7
]]--
function direction_rotate(direction, degrees)
local round = degrees > 0 and math.floor or math.ceil
steps = round((degrees % 360) / 45)
return ((direction + steps or 0) % 8)
end
-------------------------------------------------------------------------------
-- misc
function not_nil(class, var) -- if notNil(gametime_combinator, "position") then
value = false
pcall(function() if class[var] then value = true end end)
return value
end
function string.starts(string_in, string_starts)
return string.sub(string_in, 1, string.len(string_starts)) == string_starts
end
function gui_or_new(parent, name, new_element)
if parent[name] == nil then
parent.add(new_element)
end
return parent[name]
end
function table_or_new(table_a)
if table_a == nil then
return {}
else
return table_a
end
end
function table_merge(table1, table2)
for index, value in pairs(table2) do
if type(value) == "table" then
table_merge(table1[index], table2[index])
else
table1[index] = value
end
end
end
function say(player, message)
player.print(message or "")
end
function shout(message)
for _,player in pairs(game.players) do
player.print(message or "")
end
end
-------------------------------------------------------------------------------
-- debug
local cfg = config or {}
local debug_mode = cfg.debug or false
local debug_file = cfg.debug_file or "debug.log"
local debug_file_reset = cfg.debug_file_reset or true
function dump(thing, indent) -- http://stackoverflow.com/questions/9168058/how-to-dump-a-table-to-console
local dmp = ""
if not indent then indent = 0 end
for k, v in pairs(thing) do
if type(v) == "table" then
dmp = dmp .. string.rep(" ", indent) .. "[" .. tostring(k) .. "]:\n" .. dump(v, indent + 2)
else
dmp = dmp .. string.rep(" ", indent) .. tostring(k) .. " = '" .. tostring(v) .. "'\n"
end
end
return dmp
end
function debug_log(data)
if not debug_mode then return end
if debug_file_reset then
game.write_file(debug_file, game.tick .. ": Log started ...\n", false)
debug_file_reset = false
end
if type(data) == "table" then
data = "dump:\n" .. dump(data, 2) -- serpent.block(data, {nocode=true, comment=false})
else
data = tostring(data)
for _, player in pairs(game.players) do
player.print(game.tick .. ": " .. data)
end
end
game.write_file(debug_file, game.tick .. ": " .. data .. "\n", true)
end
function error(text)
error(text .. "\n" .. debug.traceback())
end