-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodmain.lua
More file actions
311 lines (265 loc) · 8.92 KB
/
Copy pathmodmain.lua
File metadata and controls
311 lines (265 loc) · 8.92 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
299
300
301
302
303
304
305
306
307
308
309
310
311
local _G = GLOBAL
local ACTIONS = _G.ACTIONS
local FRAMES = _G.FRAMES
local TheNet = _G.TheNet
local function getKeyFromConfig(name)
local k = GetModConfigData(name)
return (type(k) == "string") and _G[k] or k
end
local FILTER_ITEM_KEY = getKeyFromConfig("FILTER_ITEM_KEY")
local TOGGLE_PICKUP_FILTER_KEY = getKeyFromConfig("TOGGLE_PICKUP_FILTER_KEY")
local ALLOW_MOUSE_PICKUP_BOOL = getKeyFromConfig("ALLOW_MOUSE_PICKUP_THROUGH_FILTER_BOOL")
local REMOVE_INTERACTIONS_BOOL = getKeyFromConfig("REMOVE_INTERACTIONS_FROM_FILTERED_BOOL")
local RED_TINT_ENABLED_BOOL = getKeyFromConfig("RED_TINT_ENABLED_BOOL")
local PERSISTENCE_MODE = GetModConfigData("PERSISTENCE_MODE") or "game"
local function IsFiltered(ent)
return ent and ent._pf_filtered
end
local function GetSaveFile()
if PERSISTENCE_MODE == "disabled" then
return nil
elseif PERSISTENCE_MODE == "world" then
local id = TheNet and TheNet.GetSessionIdentifier and TheNet:GetSessionIdentifier() or "unknown"
return string.format("pickup_filter_data_%s.txt", id)
else
return "pickup_filter_data.txt"
end
end
local function saveFilter(tbl)
local file = GetSaveFile()
if not file then
return
end
local out, n = {}, 0
for prefab in pairs(tbl) do
n = n + 1
out[n] = prefab
end
_G.TheSim:SetPersistentString(file, table.concat(out, "\n"), false)
end
local function loadFilter(cb)
local file = GetSaveFile()
if not file then
cb({})
return
end
_G.TheSim:GetPersistentString(
file,
function(ok, data)
local filter = {}
if ok and data then
for prefab in data:gmatch("[^\r\n]+") do
filter[prefab] = true
end
end
cb(filter)
end
)
end
local filterEnabled = true
local pickupFilter = {prefabs = {}}
loadFilter(
function(filter)
pickupFilter.prefabs = filter
end
)
local function tintEntity(ent, on)
if not (ent and ent.AnimState) or not RED_TINT_ENABLED_BOOL then
return
end
ent._pf_filtered = on or nil
if filterEnabled and on then
ent.AnimState:SetMultColour(1, 0, 0, 1)
else
ent.AnimState:SetMultColour(1, 1, 1, 1)
end
end
local function talk(msg)
local ply = _G.ThePlayer
if ply and ply.components and ply.components.talker then
ply.components.talker:Say(msg)
else
print("[Pickup Filter] " .. msg)
end
end
local function canBeFiltered(ent)
return (ent and ent.replica and ent.replica.inventoryitem and ent.replica.inventoryitem:CanBePickedUp()) or
(ent and ent:HasTag("pickable"))
end
local function PatchCanMouseThrough(inst)
if inst._pf_can_mouse_wrapped then
return
end
inst._pf_can_mouse_wrapped = true
inst._pf_original_can_mouse = inst.CanMouseThrough
inst.CanMouseThrough = function(self, ...)
if filterEnabled and IsFiltered(self) then
return true, true
end
if self._pf_original_can_mouse then
return self._pf_original_can_mouse(self, ...)
end
end
end
AddPrefabPostInitAny(
function(inst)
if not inst then
return
end
if REMOVE_INTERACTIONS_BOOL then
PatchCanMouseThrough(inst)
end
if inst.prefab and pickupFilter.prefabs[inst.prefab] then
tintEntity(inst, true)
end
end
)
AddClassPostConstruct("components/playeractionpicker", function(self)
local function addPickupIfAllowed(picker, actions, target)
if not (ALLOW_MOUSE_PICKUP_BOOL and filterEnabled) then
return actions
end
if target == nil
or target.replica == nil
or target.replica.inventoryitem == nil
or not pickupFilter.prefabs[target.prefab]
then
return actions
end
for _, act in ipairs(actions) do
if (act.action or act) == ACTIONS.PICKUP then
return actions
end
end
local ba = _G.BufferedAction(picker.inst, target, ACTIONS.PICKUP)
table.insert(actions, 1, ba)
return actions
end
local function filterActions(actions, inst)
if not (filterEnabled and inst == _G.ThePlayer) then
return actions
end
for i = #actions, 1, -1 do
local act = actions[i]
if act and act.target and pickupFilter.prefabs[act.target.prefab] then
local isFilteredAction =
not ALLOW_MOUSE_PICKUP_BOOL
and (act.action == ACTIONS.PICK or act.action == ACTIONS.PICKUP)
local isExamineOrWalkTo =
REMOVE_INTERACTIONS_BOOL
and (act.action == ACTIONS.LOOKAT or act.action == ACTIONS.WALKTO)
if isFilteredAction or isExamineOrWalkTo then
table.remove(actions, i)
end
end
end
return actions
end
local old_left = self.GetLeftClickActions
self.GetLeftClickActions = function(picker, position, target, ...)
local actions = old_left(picker, position, target, ...)
actions = filterActions(actions, picker.inst)
actions = addPickupIfAllowed(picker, actions, target)
return actions
end
local old_right = self.GetRightClickActions
self.GetRightClickActions = function(picker, position, target, ...)
local actions = old_right(picker, position, target, ...)
actions = filterActions(actions, picker.inst)
actions = addPickupIfAllowed(picker, actions, target)
return actions
end
end)
AddClassPostConstruct(
"components/playercontroller",
function(pc)
local old = pc.GetActionButtonAction
function pc:GetActionButtonAction(force_target, ...)
local act = old(self, force_target, ...)
if
act and act.target and (act.action == ACTIONS.PICK or act.action == ACTIONS.PICKUP) and filterEnabled and
pickupFilter.prefabs[act.target.prefab] and
self.inst == _G.ThePlayer
then
return nil
end
return act
end
end
)
AddClassPostConstruct(
"components/inventoryitem_replica",
function(replica)
local old = replica.CanBePickedUp
function replica:CanBePickedUp(picker)
if filterEnabled and picker == _G.ThePlayer and IsFiltered(self.inst) then
return false
end
return old(self, picker)
end
end
)
_G.TheInput:AddKeyDownHandler(
FILTER_ITEM_KEY,
function()
if _G.IsPaused() or not _G.ThePlayer then
return
end
local touched = {}
if REMOVE_INTERACTIONS_BOOL then
for _, inst in pairs(_G.Ents) do
if IsFiltered(inst) then
touched[inst] = inst.CanMouseThrough
inst.CanMouseThrough = nil
end
end
end
_G.ThePlayer:DoTaskInTime(
FRAMES,
function()
local ent = _G.TheInput:GetWorldEntityUnderMouse()
for inst, wrapped in pairs(touched) do
inst.CanMouseThrough = function(self, ...)
if filterEnabled and IsFiltered(self) then
return true, true
end
return wrapped and wrapped(self, ...)
end
end
touched = nil
if not (ent and ent.prefab and canBeFiltered(ent)) then
talk("I can't filter that.")
return
end
local prefab = ent.prefab
local now_filtered = not pickupFilter.prefabs[prefab]
pickupFilter.prefabs[prefab] = now_filtered or nil
saveFilter(pickupFilter.prefabs)
talk(
now_filtered and string.format("Okay! I'll ignore “%s” from now on.", ent.name or prefab) or
string.format("Got it! I'll pick up “%s” again.", ent.name or prefab)
)
for _, inst in pairs(_G.Ents) do
if inst and inst.prefab == prefab then
tintEntity(inst, now_filtered and filterEnabled)
end
end
end
)
end
)
_G.TheInput:AddKeyDownHandler(
TOGGLE_PICKUP_FILTER_KEY,
function()
if _G.IsPaused() or not _G.ThePlayer then
return
end
filterEnabled = not filterEnabled
talk(filterEnabled and "Pickup filter enabled." or "Pickup filter temporarily disabled.")
for _, inst in pairs(_G.Ents) do
if inst and pickupFilter.prefabs[inst.prefab] then
tintEntity(inst, filterEnabled)
end
end
end
)