-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
180 lines (145 loc) · 5.22 KB
/
init.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package.path = package.path .. ";.\\mods\\Discord_rich_presence_noita\\lib\\?.lua"
package.cpath = package.cpath .. ";.\\mods\\Discord_rich_presence_noita\\bin\\?.dll"
dofile_once("mods/Discord_rich_presence_noita/lib/coroutines.lua")
local discordRPC = require("discordRPC")
local appId = "814077146904526858"
function discordRPC.ready(userId, username, discriminator, avatar)
print(string.format("Discord: ready (%s, %s, %s, %s)", userId, username, discriminator, avatar))
end
function discordRPC.disconnected(errorCode, message)
print(string.format("Discord: disconnected (%d: %s)", errorCode, message))
end
function discordRPC.errored(errorCode, message)
print(string.format("Discord: error (%d: %s)", errorCode, message))
end
function discordRPC.joinGame(joinSecret)
print(string.format("Discord: join (%s)", joinSecret))
end
function discordRPC.spectateGame(spectateSecret)
print(string.format("Discord: spectate (%s)", spectateSecret))
end
function discordRPC.joinRequest(userId, username, discriminator, avatar)
print(string.format("Discord: join request (%s, %s, %s, %s)", userId, username, discriminator, avatar))
discordRPC.respond(userId, "yes")
end
local presence = {
state = "Playing noita",
details = "This is a rich presence test.",
startTimestamp = os.time(),
largeImageKey = "coverimage",
smallImageKey = "goldnugget",
smallImageText = "",
largeImageText = "",
}
local old_presence = {}
local rpc_initialized = false
local function get_player()
local players = EntityGetWithTag("player_unit")
if players[1] ~= nil then
return players[1]
end
return nil
end
local function shallowcopy(orig)
if type(orig) == 'table' then
local copy = {}
for orig_key, orig_value in pairs(orig) do
copy[orig_key] = orig_value
end
return copy
end
-- number, string, boolean, etc
return orig
end
local function match_tables(table1, table2)
for k, v in pairs(table1)do
if table2[k] ~= v then
return false
end
end
return true
end
local function firstToUpper(str)
return str:gsub("^%l", string.upper)
end
local function get_biome_name(entity)
local x, y = EntityGetTransform(entity)
local current_biome = GameTextGetTranslatedOrNot( BiomeMapGetName(x, y) )
if current_biome == "_EMPTY_" then
current_biome = "Overworld"
end
if x < -17920 then
current_biome = "West "..current_biome
elseif x > 17920 then
current_biome = "East "..current_biome
end
return current_biome
end
function OnWorldPreUpdate()
wake_up_waiting_threads(1)
end
local function update_discord()
local newgame_n = tonumber( SessionNumbersGetValue("NEW_GAME_PLUS_COUNT") )
local t = os.date("*t")
local seed = string.format("%d%02d%d", t.year, t.month, t.day)
if StatsGetValue("world_seed") == seed and newgame_n == 0 then
presence.largeImageText = "Daily run"
elseif newgame_n == 0 then
presence.largeImageText = "Regular run"
else
presence.largeImageText = "NG+ Cycle: "..tostring(newgame_n)
end
local player = get_player()
if player ~= nil then
local biome_name = get_biome_name(player)
presence.details = "Biome: "..firstToUpper(biome_name)
local curbiome = biome_name:gsub("%s+", "_"):lower()
presence.largeImageKey = curbiome
local damagemodels = EntityGetComponent(player, "DamageModelComponent")
if damagemodels ~= nil then
for _, damagemodel in ipairs(damagemodels) do
local max_hp = math.floor(ComponentGetValue2(damagemodel, "max_hp") * 25)
local cur_hp = math.floor(ComponentGetValue2(damagemodel, "hp") * 25)
presence.state = string.format("Health: %d / %d", cur_hp, max_hp)
end
end
local wallet = EntityGetFirstComponent(player, "WalletComponent")
if wallet ~= nil then
local money = ComponentGetValue2(wallet, "money")
presence.smallImageText = "Gold: "..tostring(money)
end
end
if player == nil then
presence.state = "Dead!"
end
local polymorphed_entities = EntityGetWithTag("polymorphed") or {}
for _, entity_id in ipairs(polymorphed_entities) do
local is_player = false
local game_stats_comp = EntityGetFirstComponent(entity_id, "GameStatsComponent")
if game_stats_comp ~= nil then
is_player = ComponentGetValue2(game_stats_comp, "is_player")
end
if is_player then
--player is indeed polymorphed do your thing
presence.state = "Polymorphed!"
end
end
if rpc_initialized then
if not match_tables(presence, old_presence) then
discordRPC.updatePresence(presence)
old_presence = shallowcopy(presence)
end
discordRPC.runCallbacks()
end
end
function OnPlayerSpawned( player_entity )
discordRPC.initialize(appId, true)
rpc_initialized = true
async_loop(function()
local success, error_msg = pcall(update_discord)
if not success then
print_error("Lua script failed in coroutine: " .. error_msg)
end
wait(60) --885 --60
end)
end