-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.lua
More file actions
298 lines (277 loc) · 8.37 KB
/
utils.lua
File metadata and controls
298 lines (277 loc) · 8.37 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
DIR_SEP = package.config:sub(1,1)
IS_WINDOWS = DIR_SEP == '\\'
IS_REPENTOGON = _VERSION ~= "Lua 5.3"
COLORS = {
RED = {
R = 255,
G = 0,
B = 0,
A = 1
},
GREEN = {
R = 0,
G = 255,
B = 0,
A = 1
},
BLUE = {
R = 0,
G = 0,
B = 255,
A = 1
},
YELLOW = {
R = 255,
G = 255,
B = 0,
A = 1
},
MAGENTA = {
R = 255,
G = 0,
B = 255,
A = 1
},
CYAN = {
R = 0,
G = 255,
B = 255,
A = 1
},
WHITE = {
R = 255,
G = 255,
B = 255,
A = 1
},
BLACK = {
R = 0,
G = 0,
B = 0,
A = 1
}
}
CHEST_TYPES = {PickupVariant.PICKUP_CHEST, PickupVariant.PICKUP_BOMBCHEST, PickupVariant.PICKUP_SPIKEDCHEST,
PickupVariant.PICKUP_ETERNALCHEST, PickupVariant.PICKUP_MIMICCHEST, PickupVariant.PICKUP_OLDCHEST,
PickupVariant.PICKUP_MEGACHEST, PickupVariant.PICKUP_HAUNTEDCHEST, PickupVariant.PICKUP_LOCKEDCHEST,
PickupVariant.PICKUP_REDCHEST, PickupVariant.PICKUP_MOMSCHEST}
PICKUP_TYPES = {PickupVariant.PICKUP_HEART, PickupVariant.PICKUP_COIN, PickupVariant.PICKUP_BOMB,
PickupVariant.PICKUP_KEY, PickupVariant.PICKUP_TAROTCARD, PickupVariant.PICKUP_PILL,
PickupVariant.PICKUP_TRINKET}
for _, v in ipairs(CHEST_TYPES) do
table.insert(PICKUP_TYPES, v)
end
function checkPos(pos, player)
local room = Game():GetRoom()
local playerPos = room:GetGridPosition(room:GetGridIndex(player.Position))
local clampedPos = room:GetGridPosition(room:GetGridIndex(pos))
local collision = room:GetGridCollisionAtPos(pos)
local girdEntity = room:GetGridEntityFromPos(pos)
--print("checkPos", 1, playerPos.X, clampedPos.X, playerPos.Y, clampedPos.Y, collision, girdEntity)
if (playerPos.X == clampedPos.X and playerPos.Y == clampedPos.Y) or collision ~= GridCollisionClass.COLLISION_NONE or girdEntity ~= nil then
return false
end
--print("checkPos", 2)
local entities = Isaac.GetRoomEntities()
for _, v in pairs(entities) do
if v.Type == EntityType.ENTITY_PICKUP then
local clampedEntityPos = room:GetGridPosition(room:GetGridIndex(v.Position))
if clampedEntityPos.X == clampedPos.X and clampedEntityPos.Y == clampedPos.Y then
--print("checkPos", 3)
return false
end
end
end
return true
end
function getCollectableIndex(collectable)
local level = Game():GetLevel()
local room = Game():GetRoom()
local roomDescriptor = level:GetCurrentRoomDesc()
local roomListIndex = roomDescriptor.ListIndex
local gridIndex = room:GetGridIndex(collectable.Position)
local subType = collectable.SubType
local initSeed = collectable.InitSeed
return "("..roomListIndex.."|"..gridIndex.."|"..initSeed..")"
end
-- from https://stackoverflow.com/questions/9168058/how-to-dump-a-table-to-console
function dump_table(o, depth)
if depth == nil then
depth = 0
end
if type(o) == 'table' then
local tabs = (' '):rep(depth)
local tabs2 = (' '):rep(depth + 1)
local s = '{\n'
for k, v in pairs(o) do
if type(k) ~= 'number' then
k = '"' .. k .. '"'
end
s = s .. tabs2 .. '[' .. k .. '] = ' .. dump_table(v, depth + 1) .. ',\n'
end
return s .. tabs .. '}'
else
return tostring(o)
end
end
function tbl_contains(list, value)
for _, v in pairs(list) do
if v == value then
return true
end
end
return false
end
function tbl_find_index(list, value)
for k, v in pairs(list) do
if v == value then
print("tbl_find_index", dump_table(list), value, k)
return k
end
end
return nil
end
function math.round(num, decimalPlaces)
return tonumber(string.format("%." .. (decimalPlaces or 0) .. "f", num))
end
function getKeysSortedByValue(tbl, sortFunction)
local keys = {}
for key in pairs(tbl) do
table.insert(keys, key)
end
table.sort(keys, function(a, b)
return sortFunction(tbl[a], tbl[b])
end)
return keys
end
-- used to dump collectables for AP item list
function dump_collectables(id_offset)
local temp = CollectibleType
-- remove unneeded keys
temp.COLLECTIBLE_NULL = nil
temp.NUM_COLLECTIBLES = nil
-- sort
local sortedKeys = getKeysSortedByValue(temp, function(a, b)
return a < b
end)
-- output
local ids = {}
require('io')
local file = io.open("collectables.txt", "w+")
for _, k in pairs(sortedKeys) do
local id = temp[k] + id_offset - 1
local name = string.sub(k, 13)
name = string.lower(name)
name = string.gsub(name, "_", " ")
name = string.upper(string.sub(name, 1, 1)) .. string.sub(name, 2)
local i = 0
while true do
i = string.find(name, " ", i + 1)
if i == nil then
break
end
name = string.sub(name, 1, i) .. string.upper(string.sub(name, i + 1, i + 1)) .. string.sub(name, i + 2)
end
if contains(ids, id) then
file:write(string.format("# %s need alias %s\n", id, name))
print('add alias notice', name, id)
else
file:write(string.format("\"%s\": %s,\n", name, id))
print(name, id)
table.insert(ids, id)
end
end
file:close()
end
function dump_table_to_file(table, filename)
require('io')
local file = io.open(filename, "w+")
file:write(dump_table(table))
file:close()
end
function get_simple_game_data(game_data)
if not game_data then
return nil
end
local result = {}
result.version = game_data.version
result.games = {}
--print(dump_table_to_file(game_data, "gamedata.txt"))
for k, v in pairs(game_data.games) do
if v.version ~= 0 then
result.games[k] = {}
result.games[k].item_name_to_id = v.item_name_to_id
result.games[k].location_name_to_id = v.location_name_to_id
result.games[k].version = v.version
end
end
return result
end
function deepcopy(orig, copies)
copies = copies or {}
local orig_type = type(orig)
local copy
if orig_type == 'table' then
if copies[orig] then
copy = copies[orig]
else
copy = {}
copies[orig] = copy
for orig_key, orig_value in next, orig, nil do
copy[deepcopy(orig_key, copies)] = deepcopy(orig_value, copies)
end
setmetatable(copy, deepcopy(getmetatable(orig), copies))
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
function dbg_log(str)
print("[AP] "..tostring(str))
Isaac.DebugString("[AP] "..tostring(str))
end
function split(str, sep)
local result = {}
local regex = ("([^%s]+)"):format(sep)
for each in str:gmatch(regex) do
table.insert(result, each)
end
return result
end
function debugPlayers()
local players = {}
for i=0, 8, 1 do
local player = Game():GetPlayer(i)
local found = false
for _, v in ipairs(players) do
if player.Index == v.Index then
found = true
break
end
end
if found then
break
end
players[i+1] = player
end
print("-------------------------------------------------")
for i,v in pairs(players) do
print(i-1, players[i].Index, players[i].Type, players[i].Variant, players[i].SubType, players[i]:GetPlayerType(),
players[i]:GetName(), players[i]:GetMainTwin().Index, players[i]:GetOtherTwin(), players[i]:GetSubPlayer(),
players[i]:IsCoopGhost(), players[i]:IsSubPlayer(), players[i]:GetEntityFlags(), players[i].Child, players[i].Parent)
if players[i].Parent then
print(players[i].Parent.Index)
end
print("-------------------------------------------------")
end
end
function script_path()
local str = debug.getinfo(2, "S").source:sub(2)
return str:match("(.*/)")
end