Skip to content

Commit 5a30b62

Browse files
committed
Practicing what I preach: Removed all file-local variables from util/collection.lua
File local variables frustrated me to no end in the Don't Starve codebase; they have no real reason for existing and they only serve to limit mod-ability (their implementation would have to be copy+pasted for any other file to use it and the implementation can't be modified by other files), so they should be generally avoided.
1 parent f48571d commit 5a30b62

File tree

2 files changed

+58
-58
lines changed

2 files changed

+58
-58
lines changed

maps/includes/util/collection.lua

Lines changed: 52 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,9 @@
33
local Class = require "util.class"
44
require "util.utils"
55

6-
local function setup_items_key(self)
7-
-- keep a usable reference to the base metatable
8-
local real_obj = setmetatable({}, getmetatable(self))
9-
-- add special handling of the items key
10-
setmetatable(self, {
11-
-- the items key returns an iterator
12-
__index = function(t, k)
13-
if k == "items" then
14-
local i = 0
15-
local n = self:Count()
16-
return function()
17-
i = i + 1
18-
if i <= n then return self.entities[i] end
19-
end
20-
end
21-
return real_obj[k]
22-
end,
23-
-- protect the items key from being assigned
24-
__newindex = function(t, k, v)
25-
if k == "items" then
26-
return
27-
end
28-
real_obj[k] = v
29-
end
30-
})
31-
end
32-
33-
local function get_param_as_table(param)
34-
if param == nil then return {} end
35-
if type(param) == "table" then return param end
36-
return {param}
37-
end
38-
39-
-- lua tables start at index 1
40-
local function to_lua_index(collection_index)
41-
return collection_index+1
42-
end
43-
44-
-- Collection started at index 0
45-
local function to_collection_index(lua_index)
46-
return lua_index-1
47-
end
48-
496
local Collection = Class(function(self, entity_or_entities)
50-
setup_items_key(self)
51-
self.entities = get_param_as_table(entity_or_entities)
7+
self.entities = totable(entity_or_entities)
8+
self:SetupItemsKey()
529
end)
5310

5411
Collection.filters = {
@@ -98,7 +55,7 @@ end
9855

9956
function Collection.PassesFilters(entity, filters)
10057
if not filters then return true end
101-
filters = get_param_as_table(filters)
58+
filters = totable(filters)
10259
for _,filter in ipairs(filters) do
10360
if not Collection.PassesFilter(entity, filter) then
10461
return false
@@ -107,16 +64,53 @@ function Collection.PassesFilters(entity, filters)
10764
return true
10865
end
10966

67+
function Collection:SetupItemsKey()
68+
-- keep a usable reference to the base metatable
69+
local real_obj = setmetatable({}, getmetatable(self))
70+
-- add special handling of the items key
71+
setmetatable(self, {
72+
-- the items key returns an iterator
73+
__index = function(t, k)
74+
if k == "items" then
75+
local i = 0
76+
local n = self:Count()
77+
return function()
78+
i = i + 1
79+
if i <= n then return self.entities[i] end
80+
end
81+
end
82+
return real_obj[k]
83+
end,
84+
-- protect the items key from being assigned
85+
__newindex = function(t, k, v)
86+
if k == "items" then
87+
return
88+
end
89+
real_obj[k] = v
90+
end
91+
})
92+
end
93+
94+
-- lua tables start at index 1
95+
function Collection.ToLuaIndex(collection_index)
96+
return collection_index+1
97+
end
98+
99+
-- Collection started at index 0
100+
function Collection.ToCollectionIndex(lua_index)
101+
return lua_index-1
102+
end
103+
110104
function Collection:AddItem(entity_or_entities)
111-
local entities_to_add = get_param_as_table(entity_or_entities)
105+
local entities_to_add = totable(entity_or_entities)
112106

113107
for i,entity_to_add in ipairs(entities_to_add) do
114108
table.insert(self.entities, entity_to_add)
115109
end
116110
end
117111

118112
function Collection:AddFiltered(entity_or_entities, filters)
119-
local entities_to_add = get_param_as_table(entity_or_entities)
113+
local entities_to_add = totable(entity_or_entities)
120114

121115
for i,entity_to_add in ipairs(entities_to_add) do
122116
if Collection.PassesFilters(entity_to_add, filters) then
@@ -126,12 +120,12 @@ function Collection:AddFiltered(entity_or_entities, filters)
126120
end
127121

128122
function Collection:RemoveItem(entity_or_entities)
129-
local entities_to_find = get_param_as_table(entity_or_entities)
123+
local entities_to_find = totable(entity_or_entities)
130124

131125
for i,entity_to_find in ipairs(entities_to_find) do
132126
local i = self:FindItemIndex(entity_to_find)
133127
if i then
134-
table.remove(self.entities, to_lua_index(i))
128+
table.remove(self.entities, Collection.ToLuaIndex(i))
135129
end
136130
end
137131
end
@@ -153,7 +147,7 @@ function Collection:IsEmpty()
153147
end
154148

155149
function Collection:HasItem(entity_or_entities)
156-
local entities_to_find = get_param_as_table(entity_or_entities)
150+
local entities_to_find = totable(entity_or_entities)
157151

158152
for i,entity_to_find in ipairs(entities_to_find) do
159153
if self:FindItemIndex(entity_to_find) then
@@ -165,7 +159,7 @@ end
165159

166160
-- this is a strange function
167161
function Collection:GetItem(entity_or_entities)
168-
local entities_to_find = get_param_as_table(entity_or_entities)
162+
local entities_to_find = totable(entity_or_entities)
169163

170164
for i,entity_to_find in ipairs(entities_to_find) do
171165
local i = self:FindItemIndex(entity_to_find)
@@ -178,18 +172,18 @@ end
178172
function Collection:FindItemIndex(entity_to_find)
179173
for i,entity in ipairs(self.entities) do
180174
if entity:GetId() == entity_to_find:GetId() then
181-
return to_collection_index(i)
175+
return Collection.ToCollectionIndex(i)
182176
end
183177
end
184178
return nil
185179
end
186180

187181
function Collection:Element(i)
188-
return self.entities[to_lua_index(i)]
182+
return self.entities[Collection.ToLuaIndex(i)]
189183
end
190184

191185
function Collection:GetByFilter(filters)
192-
filters = get_param_as_table(filters)
186+
filters = totable(filters)
193187

194188
-- optimization for players
195189
local players_only = false
@@ -210,16 +204,16 @@ function Collection:GetByFilter(filters)
210204
end
211205

212206
function Collection:GetByName(name_or_names, filters)
213-
local names_to_find = get_param_as_table(name_or_names)
214-
filters = get_param_as_table(filters)
207+
local names_to_find = totable(name_or_names)
208+
filters = totable(filters)
215209

216210
for i,name_to_find in ipairs(names_to_find) do
217211
self:AddFiltered(GetEntitiesByName(name_to_find), filters)
218212
end
219213
end
220214

221215
function Collection:GetInSphere(entity_or_origin, radius, filters)
222-
filters = get_param_as_table(filters)
216+
filters = totable(filters)
223217
local origin = IsEntity(entity_or_origin) and entity_or_origin:GetOrigin() or entity_or_origin
224218
local ignore_walls = not table.contains(filters, CF.kTraceBlockWalls)
225219
self:AddFiltered(GetEntitiesInSphere(origin, radius, ignore_walls), filters)

maps/includes/util/utils.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@ function table.contains_any(tbl, elements)
2525
end
2626
end
2727
return false
28+
end
29+
30+
function totable(obj)
31+
if obj == nil then return {} end
32+
if type(obj) == "table" then return obj end
33+
return {obj}
2834
end

0 commit comments

Comments
 (0)