-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
160 lines (127 loc) · 4.39 KB
/
main.lua
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
--Licensed under MIT
--If you wan't to check out the API it's just a few lines below or take a ride to the the github repo (https://github.com/Kevadroz/tboiAchievementUnlocker).
--For how this works see the checkUnlocks() function, the workshop description and the python script on
--the github repo (https://github.com/Kevadroz/tboiAchievementUnlocker/blob/main/devScripts/genStatic.py)
local mod = RegisterMod("KZAchievementChecker", 1)
local json = require("json")
--mod.exportEnums = require("devScripts.exportEnums") -- Only works with --luadebug
local debugTab = {} -- Used to access local variables on mod development
local unlocks = {} -- Unlocked Achievements
debugTab.un = unlocks
local achievementToUnlockable = require("static.achievements") -- Used for achievement checking
debugTab.atu = achievementToUnlockable
-- Maps vanilla unlockables that are not ordinally chekable to achievements. Generated by a script
local playerUnlocks = require("static.players")
debugTab.pu = playerUnlocks
-- mod.debug = debugTab -- Commented out on production
-- For public access
if not KZLibs then
KZLibs = {}
end
KZLibs.AchievementChecker = mod
-- Call this [KZLibs.AchievementChecker.isAchievementUnlocked(x)] to get unlock status about achievement x, argument is a number between 1 and 637
function mod.isAchievementUnlocked(achievement)
if achievement > 0 then
return unlocks[achievement]
end
return true
end
-- Argument is a CollectibleType
-- Now just a ItemConfig wrapper
function mod.isCollectibleUnlocked(collectible)
if collectible ~= nil then
return Isaac:GetItemConfig():GetCollectible(collectible):IsAvailable()
end
end
-- Argument is a PlayerType
function mod.isPlayerUnlocked(player)
if player ~= nil then
if player == 0 or player == -1 then
return true
end
---@diagnostic disable-next-line: need-check-nil
local id = playerUnlocks[player]
if id ~= nil then
return mod.isAchievementUnlocked(id)
end
end
return nil
end
-- Argument is a TrinketType
-- Now just a ItemConfig wrapper
function mod.isTrinketUnlocked(trinket)
if trinket ~= nil then
return Isaac:GetItemConfig():GetTrinket(trinket):IsAvailable()
end
end
-- Argument is a Card
-- Now just a ItemConfig wrapper
function mod.isCardUnlocked(card)
if card ~= nil then
return Isaac:GetItemConfig():GetCard(card):IsAvailable()
end
end
-- Argument is a PillEffect
-- Now just a ItemConfig wrapper
function mod.isPillUnlocked(pill)
if pill ~= nil then
return Isaac:GetItemConfig():GetPillEffect(pill):IsAvailable()
end
end
local modCollectiblesStart = Isaac.GetItemIdByName("KZAchievementCheckerID1")
local modCollectiblesEnd = Isaac.GetItemIdByName("KZAchievementCheckerID637")
-- Get the mod item's ids and assign the achievements to them on the table
for key, value in pairs(achievementToUnlockable) do
if value[1] == 0 then
---@diagnostic disable-next-line: param-type-mismatch
value[2] = Isaac.GetItemIdByName(value[2])
value[1] = 1
end
end
-- Sets the entire table to false
local function clearUnlocks()
for a=1, 637 do
unlocks[a] = false
end
end
clearUnlocks()
-- Checks the unlockable type and uses the correct function to
local function getConfig(itemConfig, table)
if table[1] == 1 then
return itemConfig:GetCollectible(table[2]):IsAvailable()
elseif table[1] == 2 then
return itemConfig:GetTrinket(table[2]):IsAvailable()
elseif table[1] == 3 then
return itemConfig:GetCard(table[2]):IsAvailable()
elseif table[1] == 4 then
return itemConfig:GetPillEffect(table[2]):IsAvailable()
else
error('Invalid unlockable type!', 0)
end
end
-- This is called at the start of the run, on new floors and on run continues
local function checkUnlocks()
clearUnlocks()
local itemConfig = Isaac:GetItemConfig()
for achievement, unlockable in pairs(achievementToUnlockable) do
---@diagnostic disable-next-line: param-type-mismatch
if getConfig(itemConfig, unlockable) then
unlocks[achievement] = true
end
end
mod:SaveData(json.encode(unlocks))
end
-- This is the first callback on a run, thus it's the one we use
local function onPlayerInit(_, entityPlayer)
checkUnlocks()
end
-- We can update the unlocks table on every floor
local function onNewLevel(_)
checkUnlocks()
end
mod:AddCallback(ModCallbacks.MC_POST_PLAYER_INIT, onPlayerInit)
mod:AddCallback(ModCallbacks.MC_POST_NEW_LEVEL, onNewLevel)
-- Reload unlock table from disk when the mod is reloaded
if mod:HasData() then
unlocks = json.decode(mod:LoadData())
end