-
Notifications
You must be signed in to change notification settings - Fork 32
/
test15.lua
183 lines (141 loc) · 2.53 KB
/
test15.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
180
-- group api
local ecs = require "ecs"
local w = ecs.world()
local function ADD(groupid, eid)
w:group_add(groupid, eid)
local t = w:group_get(groupid)
assert(eid == t[#t])
end
w:register {
name = "id",
type = "int",
}
w:register {
name = "group",
type = "int"
}
w:register {
name = "visible"
}
for i = 1, 100 do
local eid = w:new {
id = i,
group = i % 5
}
for i = 1, 4000 do
w:new()
end
ADD(i % 5, eid)
end
w:update()
for v in w:select "id:in group:in" do
if v.id % 3 == 0 then
print("REMOVE", v.id, v.group)
w:remove(v)
end
end
w:update()
-- enable all entities where group == 0 or group == 1
w:group_enable("visible", 0,2)
for v in w:select "visible id:in group:in" do
print(v.id, v.group)
end
local eid = w:new { id = 42 }
ADD (1000, eid)
ADD (1001, eid)
w:group_enable("visible", 1000, 1001)
for v in w:select "visible id:in" do
print(v.id)
end
local g = w:group_get(0)
for _, eid in ipairs(g) do
print("GROUP0", eid)
end
-------------------------
local e = {}
-- Random apply group
for i = 1, 1000 do
local eid = w:new()
local group = math.random(2000, 2010)
for j = 1, math.random(1, 1000) do
w:new()
end
e[eid] = group
ADD(group, eid)
end
-- Random remove
for k,v in pairs(e) do
if math.random(1, 3) == 1 then
w:remove(k)
e[k] = nil
end
end
w:update()
local function same_array(a, b)
local function errstr()
return table.concat(a, ",") .. "\n" .. table.concat(b, ",")
end
local n = #a
if n ~= #b then
error(errstr())
end
for i = 1, n do
if a[i] ~= b[i] then
error(errstr())
end
end
end
w:register {
name = "TEST"
}
local function insert_group(r, gid, ...)
if gid == nil then
return
end
for k,v in pairs(e) do
if v == gid then
table.insert(r, k)
end
end
insert_group(r, ...)
end
local function merge_group(...)
local r = {}
insert_group(r, ...)
table.sort(r)
return r
end
local function test(...)
local mg = merge_group(...)
w:group_enable("TEST", ...)
local r = {}
for v in w:select "TEST eid:in" do
table.insert(r, v.eid)
end
same_array(mg, r)
end
local function print_group(gid)
local g = w:group_get(gid)
print(table.concat(g, ","))
end
test(2000, 2001, 2005)
w:update()
local eid = { w:new(), w:new() }
ADD(3000, eid[1])
ADD(3000, eid[2])
w:group_enable("TEST", 3000)
local r = {}
for v in w:select "TEST eid:in" do
table.insert(r, v.eid)
end
same_array(eid, r)
w:remove(eid[2])
eid[2] = w:new()
w:update()
ADD(3000, eid[2])
w:group_enable("TEST", 3000)
local r = {}
for v in w:select "TEST eid:in" do
table.insert(r, v.eid)
end
same_array(eid, r)