@rbxts/replecs 0.1.0-alpha7 → 0.1.0-alpha9-test5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,6 +9,7 @@ type Entity<T = any> = jecs.Entity<T>
9
9
 
10
10
  type Bitmask = utils.Bitmask
11
11
  type MaskGenerator = mask_generator.MaskGenerator
12
+ type MaskGeneratorWithBitmask = mask_generator.MaskGeneratorWithBitmask
12
13
 
13
14
  type GivenFilter = MemberFilter | any
14
15
  type MemberFilter = Set<Member>
@@ -48,6 +49,10 @@ export type EntityChanges = {
48
49
  component: { [Entity]: any },
49
50
  removed: { [Entity]: boolean },
50
51
  pairs: { [Entity]: Set<Entity> },
52
+ pairs_values: {
53
+ changed: { [Entity]: Map<Entity, any> },
54
+ removed: { [Entity]: Set<Entity> },
55
+ },
51
56
  }
52
57
 
53
58
  type PostponedList = ComponentIndex<{ filter: MemberFilter? }>
@@ -59,6 +64,10 @@ type LookupsList = {
59
64
  entities: Map<Entity, Lookup>,
60
65
  components: EntityComponentIndex<Lookup>,
61
66
  },
67
+ filter_deltas: {
68
+ entities: Map<Entity, Map<Entity, boolean>>,
69
+ components: EntityComponentIndex<Map<Entity, boolean>>,
70
+ },
62
71
 
63
72
  postponed: { [Entity]: PostponedList },
64
73
  }
@@ -102,20 +111,27 @@ type Storages = {
102
111
 
103
112
  export type MaskingController = {
104
113
  member_count: number,
114
+
115
+ client_indexes: { [Member]: number },
116
+ group_indexes: { [number]: number },
105
117
  member_indexes: { [Member]: number },
118
+ client_aliases: { [any]: Member },
119
+
106
120
  compact_count: number,
107
121
  unreplicated: Set<Member>,
108
122
 
109
- all_members_generator: MaskGenerator,
123
+ all_members_generator: MaskGeneratorWithBitmask,
110
124
  active_members_generator: MaskGenerator,
111
- unactive_members_generator: MaskGenerator,
125
+ unactive_members_generator: MaskGeneratorWithBitmask,
112
126
 
113
127
  storages: Storages,
114
128
  lookups: LookupsList,
115
129
 
116
- register_member: (self: MaskingController, member: Member) -> (),
117
- unregister_member: (self: MaskingController, member: Member) -> (),
118
- activate_member: (self: MaskingController, member: Member) -> (),
130
+ register_mask_group: (self: MaskingController, group: Member) -> (),
131
+ unregister_mask_group: (self: MaskingController, group: Member) -> (),
132
+ register_client: (self: MaskingController, member: Member) -> (),
133
+ unregister_client: (self: MaskingController, member: Member) -> (),
134
+ activate_client: (self: MaskingController, member: Member) -> (),
119
135
  member_is_active: (self: MaskingController, member: Member) -> boolean,
120
136
 
121
137
  register_stop_entity: (self: MaskingController, entity: Entity) -> (),
@@ -176,10 +192,19 @@ export type MaskingController = {
176
192
  ) -> (),
177
193
 
178
194
  allocate_component_change: (self: MaskingController, entity: Entity, component: Entity, value: any) -> (),
195
+ allocate_pair_value_change: (
196
+ self: MaskingController,
197
+ entity: Entity,
198
+ relation: Entity,
199
+ target: Entity,
200
+ value: any
201
+ ) -> (),
202
+
179
203
  allocate_tag_addition: (self: MaskingController, entity: Entity, tag: Entity) -> (),
180
204
  allocate_pair_addition: (self: MaskingController, entity: Entity, relation: Entity, target: Entity) -> (),
181
205
 
182
206
  allocate_component_removal: (self: MaskingController, entity: Entity, component: Entity) -> (),
207
+ allocate_pair_value_removal: (self: MaskingController, entity: Entity, relation: Entity, target: Entity) -> (),
183
208
  allocate_tag_removal: (self: MaskingController, entity: Entity, tag: Entity) -> (),
184
209
  allocate_pair_removal: (self: MaskingController, entity: Entity, relation: Entity, target: Entity) -> (),
185
210
  }
@@ -188,7 +213,8 @@ local COMPONENT_TYPES = {
188
213
  component = 1,
189
214
  tag = 2,
190
215
  pair = 3,
191
- unreliable = 4,
216
+ pair_value = 4,
217
+ unreliable = 5,
192
218
  }
193
219
 
194
220
  local masking_controller = {}
@@ -196,9 +222,9 @@ masking_controller.__index = masking_controller
196
222
 
197
223
  export type MaskingInternal = MaskingController & typeof(masking_controller)
198
224
 
199
- function masking_controller.get_members_from_bitmask(controller: MaskingInternal, bitmask: Bitmask): Array<Member>
225
+ function masking_controller.get_clients_from_bitmask(controller: MaskingInternal, bitmask: Bitmask): Array<Member>
200
226
  local members: { Member } = {}
201
- for player, index in controller.member_indexes do
227
+ for player, index in controller.client_indexes do
202
228
  if bitmask:get(index) then
203
229
  table.insert(members, player :: any)
204
230
  end
@@ -219,7 +245,7 @@ local function get_usable_filter(filter: (MemberFilter | any)?): MemberFilter?
219
245
  end
220
246
 
221
247
  local function create_component_indexes<T>()
222
- return { {}, {}, {}, {} } :: ComponentIndex<any>
248
+ return { {}, {}, {}, {}, {} } :: ComponentIndex<any>
223
249
  end
224
250
 
225
251
  local function create_entity_changes(): EntityChanges
@@ -228,19 +254,13 @@ local function create_entity_changes(): EntityChanges
228
254
  component = {},
229
255
  removed = {},
230
256
  pairs = {},
257
+ pairs_values = {
258
+ changed = {},
259
+ removed = {},
260
+ },
231
261
  }
232
262
  end
233
263
 
234
- local function get_or_set_changes(storage: StorageGroup, entity: Entity)
235
- local changes: EntityChanges = storage.changes.changed[entity]
236
- if changes == nil then
237
- changes = create_entity_changes()
238
- storage.changes.changed[entity] = changes
239
- end
240
-
241
- return changes
242
- end
243
-
244
264
  function masking_controller.create_new_storage(
245
265
  controller: MaskingInternal,
246
266
  bitmask: Bitmask,
@@ -251,7 +271,7 @@ function masking_controller.create_new_storage(
251
271
  mask = {
252
272
  bitmask = bitmask,
253
273
  hash = hash or bitmask:tostring(),
254
- members = members or controller:get_members_from_bitmask(bitmask),
274
+ members = members or controller:get_clients_from_bitmask(bitmask),
255
275
  },
256
276
  is_empty = true,
257
277
  shared_with = {},
@@ -291,15 +311,6 @@ local function create_new_active(): ActiveEntity
291
311
  }
292
312
  end
293
313
 
294
- local function create_new_changes(): EntityChanges
295
- return {
296
- tagged = {},
297
- component = {},
298
- removed = {},
299
- pairs = {},
300
- }
301
- end
302
-
303
314
  local function get_component_index_entry<T>(
304
315
  index: EntityComponentIndex<T>,
305
316
  entity: Entity,
@@ -343,14 +354,32 @@ local function remove_component_index_entry(
343
354
  entity_index[component_type][component] = nil
344
355
  end
345
356
 
357
+ local function get_bitmask_deltas(current: Bitmask, target: Bitmask): (Bitmask, Bitmask)
358
+ local added = target:band(current:bnot())
359
+ local removed = current:band(target:bnot())
360
+ return added, removed
361
+ end
362
+
363
+ local function merge_bitmask_deltas(added: Bitmask, removed: Bitmask, new_added: Bitmask, new_removed: Bitmask)
364
+ local merged_added = added:bor(new_added)
365
+ local merged_removed = removed:bor(new_removed)
366
+ local common = merged_added:band(merged_removed):bnot()
367
+
368
+ local added_final = merged_added:band(common)
369
+ local removed_final = merged_removed:band(common)
370
+ return added_final, removed_final
371
+ end
372
+
346
373
  masking_controller.get_component_index_entry = get_component_index_entry
347
374
  masking_controller.set_component_index_entry = set_component_index_entry
348
375
  masking_controller.remove_component_index_entry = remove_component_index_entry
376
+ masking_controller.get_bitmask_deltas = get_bitmask_deltas
377
+ masking_controller.merge_bitmask_deltas = merge_bitmask_deltas
349
378
 
350
379
  local function get_or_set_changed(storage: StorageGroup, entity: Entity, changed: EntityChanges?): EntityChanges
351
380
  local storage_changed = storage.changes.changed[entity]
352
381
  if storage_changed == nil then
353
- storage_changed = changed or create_new_changes()
382
+ storage_changed = changed or create_entity_changes()
354
383
  storage.changes.changed[entity] = storage_changed
355
384
  end
356
385
  return storage_changed
@@ -480,7 +509,7 @@ local function dissect_component_changes(changes: EntityChanges, shared_index: C
480
509
  for component, change in changes.component do
481
510
  local is_shared = shared_index[COMPONENT_TYPES.component] and shared_index[COMPONENT_TYPES.component][component]
482
511
  if is_shared then
483
- new_changes = new_changes or create_new_changes()
512
+ new_changes = new_changes or create_entity_changes()
484
513
  new_changes.component[component] = change
485
514
  changes.component[component] = nil
486
515
  end
@@ -488,19 +517,40 @@ local function dissect_component_changes(changes: EntityChanges, shared_index: C
488
517
  for entity, entity_changes in changes.tagged do
489
518
  local is_shared = shared_index[COMPONENT_TYPES.tag] and shared_index[COMPONENT_TYPES.tag][entity]
490
519
  if is_shared then
491
- new_changes = new_changes or create_new_changes()
520
+ new_changes = new_changes or create_entity_changes()
492
521
  new_changes.tagged[entity] = entity_changes
493
522
  changes.tagged[entity] = nil
494
523
  end
495
524
  end
496
- for entity, entity_changes in changes.pairs do
497
- local is_shared = shared_index[COMPONENT_TYPES.pair] and shared_index[COMPONENT_TYPES.pair][entity]
525
+ for relation, entity_changes in changes.pairs do
526
+ local is_shared = shared_index[COMPONENT_TYPES.pair] and shared_index[COMPONENT_TYPES.pair][relation]
498
527
  if is_shared then
499
- new_changes = new_changes or create_new_changes()
500
- new_changes.pairs[entity] = entity_changes
501
- changes.pairs[entity] = nil
528
+ new_changes = new_changes or create_entity_changes()
529
+ new_changes.pairs[relation] = entity_changes
530
+ changes.pairs[relation] = nil
502
531
  end
503
532
  end
533
+ for relation, entity_changes in changes.pairs_values.changed do
534
+ local is_shared = shared_index[COMPONENT_TYPES.pair_value]
535
+ and shared_index[COMPONENT_TYPES.pair_value][relation]
536
+
537
+ if is_shared then
538
+ new_changes = new_changes or create_entity_changes()
539
+ new_changes.pairs_values.changed[relation] = entity_changes
540
+ changes.pairs_values.changed[relation] = nil
541
+ end
542
+ end
543
+ for relation, entity_changes in changes.pairs_values.removed do
544
+ local is_shared = shared_index[COMPONENT_TYPES.pair_value]
545
+ and shared_index[COMPONENT_TYPES.pair_value][relation]
546
+
547
+ if is_shared then
548
+ new_changes = new_changes or create_entity_changes()
549
+ new_changes.pairs_values.removed[relation] = entity_changes
550
+ changes.pairs_values.removed[relation] = nil
551
+ end
552
+ end
553
+
504
554
  return new_changes
505
555
  end
506
556
 
@@ -549,13 +599,14 @@ function masking_controller.merge_component_filter(
549
599
  component,
550
600
  component_type,
551
601
  component_lookup.storage_group,
552
- entity_lookup.generator.bitmask
602
+ entity_lookup.generator.result
553
603
  )
554
604
  entity_lookup.filtered_components[component_type][component] = nil
555
605
  remove_component_index_entry(new_storage.shared_with, entity, component, component_type)
556
606
  else
557
607
  get_or_set_component_active(entity_lookup.storage_group.active[entity], component, component_type)
558
608
  end
609
+ entity_lookup.components[component_type][component] = true
559
610
  end
560
611
 
561
612
  function masking_controller.move_entity_storage(
@@ -679,6 +730,15 @@ function masking_controller.move_component_storage(
679
730
  elseif component_type == COMPONENT_TYPES.pair then
680
731
  target_changes.pairs[component] = changes.pairs[component]
681
732
  changes.pairs[component] = nil
733
+ elseif component_type == COMPONENT_TYPES.pair_value then
734
+ local target_pairs = target_changes.pairs_values
735
+ local pairs = changes.pairs_values
736
+
737
+ target_pairs.changed[component] = pairs.changed[component]
738
+ target_pairs.removed[component] = pairs.removed[component]
739
+
740
+ pairs.changed[component] = nil
741
+ pairs.removed[component] = nil
682
742
  end
683
743
  end
684
744
 
@@ -696,7 +756,7 @@ function masking_controller.bind_component_generator(
696
756
  )
697
757
  local component_lookup, entity_lookup = masking:get_component_lookup(entity, component, component_type)
698
758
  local generator = mask_generator.create(nil, function()
699
- return entity_lookup.generator.bitmask:band(base_generator.bitmask)
759
+ return entity_lookup.generator.result:band(base_generator.result)
700
760
  end)
701
761
 
702
762
  local function subscribed()
@@ -706,7 +766,7 @@ function masking_controller.bind_component_generator(
706
766
 
707
767
  local current_storage = component_lookup.storage_group
708
768
  local new_storage =
709
- masking:move_component_storage(entity, component, component_type, current_storage, generator.bitmask)
769
+ masking:move_component_storage(entity, component, component_type, current_storage, generator.result)
710
770
 
711
771
  component_lookup.storage_group = new_storage
712
772
  end
@@ -757,6 +817,9 @@ end
757
817
 
758
818
  function masking_controller.register_stop_entity(masking: MaskingInternal, entity: Entity)
759
819
  local lookup = masking.lookups.entities[entity]
820
+ if not lookup then
821
+ return
822
+ end
760
823
  local storage = lookup.storage_group
761
824
 
762
825
  if storage.deletions.entities[entity] then
@@ -773,6 +836,9 @@ function masking_controller.register_stop_component(
773
836
  component_type: number
774
837
  )
775
838
  local storage = masking:get_component_storage_group(entity, component, component_type) :: StorageGroup
839
+ if not storage then
840
+ return
841
+ end
776
842
 
777
843
  local deletions = storage.deletions.components[entity]
778
844
  if deletions == nil then
@@ -861,22 +927,28 @@ function masking_controller.propagate_entity_addition(masking: MaskingInternal,
861
927
  end
862
928
  end
863
929
 
864
- function masking_controller.update_entity_filter(masking: MaskingInternal, entity: Entity, filter: MemberFilter?)
930
+ function masking_controller.update_entity_filter(
931
+ masking: MaskingInternal,
932
+ entity: Entity,
933
+ filter: MemberFilter?
934
+ ): MaskGenerator
865
935
  local generator: MaskGenerator
866
936
  if filter then
867
937
  generator = masking:create_generator_from_filter(filter)
868
938
  else
869
939
  generator = mask_generator.follow(masking.active_members_generator)
870
940
  end
941
+
871
942
  local lookup = masking.lookups.entities[entity]
872
943
 
873
944
  local function subscribed()
874
945
  if not lookup then
875
946
  lookup = masking.lookups.entities[entity]
876
947
  end
948
+ local generator = generator :: MaskGenerator
877
949
  local current_storage = lookup.storage_group
878
950
 
879
- local new_storage = masking:move_entity_storage(entity, current_storage, generator.bitmask)
951
+ local new_storage = masking:move_entity_storage(entity, current_storage, generator.result)
880
952
  lookup.storage_group = new_storage
881
953
  end
882
954
  generator.subscribed = subscribed
@@ -887,7 +959,7 @@ function masking_controller.start_entity(masking: MaskingInternal, entity: Entit
887
959
  local filter = get_usable_filter(given_filter)
888
960
  local generator = masking:update_entity_filter(entity, filter)
889
961
 
890
- local storage_group = masking:get_or_create_storage_group(generator.bitmask)
962
+ local storage_group = masking:get_or_create_storage_group(generator.result)
891
963
  local active = get_or_set_active(storage_group, entity)
892
964
  active.is_entity_mask = true
893
965
 
@@ -903,8 +975,8 @@ function masking_controller.start_entity(masking: MaskingInternal, entity: Entit
903
975
  local postponed = masking.lookups.postponed[entity]
904
976
  if postponed then
905
977
  for component_type, components in postponed do
906
- for component, postoned_info in components do
907
- masking:start_component(entity, component, component_type, postoned_info.filter)
978
+ for component, postponed_info in components do
979
+ masking:start_component(entity, component, component_type, postponed_info.filter)
908
980
  end
909
981
  end
910
982
  masking.lookups.postponed[entity] = nil
@@ -954,6 +1026,15 @@ function masking_controller.stop_entity(masking: MaskingInternal, entity: Entity
954
1026
  postponed[component_type][component] = { filter = component_lookup.filter }
955
1027
  end
956
1028
  end
1029
+ for component_type, components in lookup.components do
1030
+ for component in components do
1031
+ if postponed == nil then
1032
+ postponed = create_component_indexes() :: PostponedList
1033
+ masking.lookups.postponed[entity] = postponed
1034
+ end
1035
+ postponed[component_type][component] = { filter = nil :: MemberFilter? }
1036
+ end
1037
+ end
957
1038
 
958
1039
  lookup.generator:destroy()
959
1040
  remove_active(lookup.storage_group, entity)
@@ -996,12 +1077,13 @@ function masking_controller.start_component(
996
1077
  end
997
1078
 
998
1079
  if filter == nil then
1080
+ lookup.components[component_type][component] = true
999
1081
  get_or_set_component_active(lookup.storage_group.active[entity], component, component_type)
1000
1082
  return
1001
1083
  end
1002
1084
 
1003
1085
  local generator, band_generator = masking:update_component_filter(entity, component, component_type, filter)
1004
- local storage_group = masking:get_or_create_storage_group(band_generator.bitmask)
1086
+ local storage_group = masking:get_or_create_storage_group(band_generator.result)
1005
1087
  local active = get_or_set_active(storage_group, entity)
1006
1088
 
1007
1089
  get_or_set_component_active(active, component, component_type)
@@ -1052,7 +1134,7 @@ function masking_controller.set_component(
1052
1134
 
1053
1135
  band_generator:run_subscribed()
1054
1136
  else
1055
- local desired_storage = masking:get_or_create_storage_group(band_generator.bitmask)
1137
+ local desired_storage = masking:get_or_create_storage_group(band_generator.result)
1056
1138
  local new_storage: StorageGroup
1057
1139
 
1058
1140
  if desired_storage and desired_storage == lookup.storage_group then
@@ -1064,7 +1146,7 @@ function masking_controller.set_component(
1064
1146
  component,
1065
1147
  component_type,
1066
1148
  lookup.storage_group,
1067
- band_generator.bitmask
1149
+ band_generator.result
1068
1150
  )
1069
1151
  end
1070
1152
  local new_lookup: ComponentLookup = {
@@ -1074,6 +1156,7 @@ function masking_controller.set_component(
1074
1156
  storage_group = new_storage,
1075
1157
  }
1076
1158
  lookup.filtered_components[component_type][component] = new_lookup
1159
+ lookup.components[component_type][component] = nil
1077
1160
  end
1078
1161
  end
1079
1162
 
@@ -1095,6 +1178,7 @@ function masking_controller.stop_component(
1095
1178
  if entity_lookup then
1096
1179
  remove_component_active(entity_lookup.storage_group, entity, component, component_type)
1097
1180
  end
1181
+ entity_lookup.components[component_type][component] = nil
1098
1182
  end
1099
1183
  end
1100
1184
 
@@ -1134,14 +1218,38 @@ function masking_controller.allocate_component_change(
1134
1218
  value: any
1135
1219
  )
1136
1220
  local storage = masking:get_component_storage_group(entity, component, COMPONENT_TYPES.component)
1137
- local changes = get_or_set_changes(storage, entity)
1221
+ local changes = get_or_set_changed(storage, entity)
1138
1222
  changes.component[component] = value
1223
+ changes.removed[component] = nil
1224
+ end
1225
+
1226
+ function masking_controller.allocate_pair_value_change(
1227
+ masking: MaskingInternal,
1228
+ entity: Entity,
1229
+ relation: Entity,
1230
+ target: Entity,
1231
+ value: any
1232
+ )
1233
+ local storage = masking:get_component_storage_group(entity, relation, COMPONENT_TYPES.pair_value)
1234
+ local changes = get_or_set_changed(storage, entity)
1235
+ local values = changes.pairs_values.changed[relation]
1236
+ if values == nil then
1237
+ values = {}
1238
+ changes.pairs_values.changed[relation] = values
1239
+ end
1240
+ values[target] = value
1241
+
1242
+ local removed = changes.pairs_values.removed[relation]
1243
+ if removed ~= nil then
1244
+ removed[target] = nil
1245
+ end
1139
1246
  end
1140
1247
 
1141
1248
  function masking_controller.allocate_tag_addition(masking: MaskingInternal, entity: Entity, tag: Entity)
1142
1249
  local storage = masking:get_component_storage_group(entity, tag, COMPONENT_TYPES.tag)
1143
- local changes = get_or_set_changes(storage, entity)
1250
+ local changes = get_or_set_changed(storage, entity)
1144
1251
  changes.tagged[tag] = true
1252
+ changes.removed[tag] = nil
1145
1253
  end
1146
1254
 
1147
1255
  function masking_controller.allocate_pair_addition(
@@ -1151,7 +1259,7 @@ function masking_controller.allocate_pair_addition(
1151
1259
  target: Entity
1152
1260
  )
1153
1261
  local storage = masking:get_component_storage_group(entity, relation, COMPONENT_TYPES.pair)
1154
- local changes = get_or_set_changes(storage, entity)
1262
+ local changes = get_or_set_changed(storage, entity)
1155
1263
  local targets = changes.pairs[relation]
1156
1264
  if targets == nil then
1157
1265
  targets = {}
@@ -1162,21 +1270,17 @@ end
1162
1270
 
1163
1271
  function masking_controller.allocate_component_removal(masking: MaskingInternal, entity: Entity, component: Entity)
1164
1272
  local storage = masking:get_component_storage_group(entity, component, COMPONENT_TYPES.component)
1165
- local changes = get_or_set_changes(storage, entity)
1273
+ local changes = get_or_set_changed(storage, entity)
1166
1274
 
1167
- if changes.component[component] then
1168
- changes.component[component] = nil
1169
- end
1275
+ changes.component[component] = nil
1170
1276
  changes.removed[component] = true
1171
1277
  end
1172
1278
 
1173
1279
  function masking_controller.allocate_tag_removal(masking: MaskingInternal, entity: Entity, tag: Entity)
1174
1280
  local storage = masking:get_component_storage_group(entity, tag, COMPONENT_TYPES.tag)
1175
- local changes = get_or_set_changes(storage, entity)
1281
+ local changes = get_or_set_changed(storage, entity)
1176
1282
 
1177
- if changes.tagged[tag] then
1178
- changes.tagged[tag] = nil
1179
- end
1283
+ changes.tagged[tag] = nil
1180
1284
  changes.removed[tag] = true
1181
1285
  end
1182
1286
 
@@ -1187,7 +1291,7 @@ function masking_controller.allocate_pair_removal(
1187
1291
  target: Entity
1188
1292
  )
1189
1293
  local storage = masking:get_component_storage_group(entity, relation, COMPONENT_TYPES.pair)
1190
- local changes = get_or_set_changes(storage, entity)
1294
+ local changes = get_or_set_changed(storage, entity)
1191
1295
  local targets = changes.pairs[relation]
1192
1296
  if targets == nil then
1193
1297
  targets = {}
@@ -1196,6 +1300,25 @@ function masking_controller.allocate_pair_removal(
1196
1300
  targets[target] = false
1197
1301
  end
1198
1302
 
1303
+ function masking_controller.allocate_pair_value_removal(
1304
+ masking: MaskingInternal,
1305
+ entity: Entity,
1306
+ relation: Entity,
1307
+ target: Entity
1308
+ )
1309
+ local storage = masking:get_component_storage_group(entity, relation, COMPONENT_TYPES.pair_value)
1310
+ local changes = get_or_set_changed(storage, entity)
1311
+ local removed = changes.pairs_values.removed[relation]
1312
+ if removed == nil then
1313
+ removed = {}
1314
+ changes.pairs_values.removed[relation] = removed
1315
+ end
1316
+ local values = changes.pairs_values.changed[relation]
1317
+ if values ~= nil then
1318
+ values[target] = nil
1319
+ end
1320
+ end
1321
+
1199
1322
  local function remap_bitmask(
1200
1323
  bitmask: Bitmask,
1201
1324
  from: { [Member]: number },
@@ -1217,6 +1340,17 @@ local function remap_bitmask(
1217
1340
  return result
1218
1341
  end
1219
1342
 
1343
+ local function bitmask_from_set(masking: MaskingInternal, member_set: { [Member]: boolean }): utils.Bitmask
1344
+ local new_bitmask = utils.bitmask.create(masking.member_count)
1345
+ for member: Member in member_set do
1346
+ local index = masking.member_indexes[member] or masking.member_indexes[masking.client_aliases[member]]
1347
+ if index then
1348
+ new_bitmask:set(index)
1349
+ end
1350
+ end
1351
+ return new_bitmask
1352
+ end
1353
+
1220
1354
  function masking_controller.merge_storages(
1221
1355
  masking: MaskingInternal,
1222
1356
  old_storage: StorageGroup,
@@ -1227,7 +1361,7 @@ function masking_controller.merge_storages(
1227
1361
  end
1228
1362
  for entity, active in old_storage.active do
1229
1363
  local new_active = get_or_set_active(new_storage, entity)
1230
- merge_component_actives(new_active, active)
1364
+ merge_component_actives(active, new_active)
1231
1365
 
1232
1366
  if active.is_entity_mask then
1233
1367
  local lookup = masking.lookups.entities[entity]
@@ -1252,7 +1386,7 @@ function masking_controller.merge_storages(
1252
1386
  merge_component_additions(new_added_components, added_components)
1253
1387
  end
1254
1388
  for entity, changes in old_storage.changes.changed do
1255
- local new_changes = get_or_set_changes(new_storage, entity)
1389
+ local new_changes = get_or_set_changed(new_storage, entity)
1256
1390
  merge_component_changes(new_changes, changes)
1257
1391
  end
1258
1392
  for entity in old_storage.changes.added do
@@ -1279,22 +1413,44 @@ function masking_controller.compact_members(masking: MaskingInternal)
1279
1413
  local new_count = 0
1280
1414
  for member in old_indexes do
1281
1415
  new_indexes[member] = new_count
1416
+
1417
+ if masking.client_indexes[member] then
1418
+ masking.client_indexes[member] = new_count
1419
+ end
1420
+ if masking.group_indexes[new_count] then
1421
+ masking.group_indexes[new_count] = member
1422
+ end
1423
+
1282
1424
  new_count += 1
1283
1425
  end
1284
1426
  masking.member_indexes = new_indexes
1285
1427
  masking.member_count = new_count
1286
1428
 
1287
1429
  for _, entity_lookup in masking.lookups.entities do
1288
- local generator = entity_lookup.generator
1289
- generator.bitmask = remap_bitmask(generator.bitmask, old_indexes, new_indexes, new_count)
1430
+ local entity_generator = entity_lookup.generator
1431
+ local entity_bitmask = entity_generator.bitmask
1432
+ if entity_bitmask then
1433
+ entity_generator.bitmask = remap_bitmask(entity_bitmask, old_indexes, new_indexes, new_count)
1434
+ end
1435
+ entity_generator.result = remap_bitmask(entity_generator.result, old_indexes, new_indexes, new_count)
1290
1436
 
1291
1437
  for _, lookups in entity_lookup.filtered_components do
1292
1438
  for _, component_lookup in lookups do
1293
1439
  local component_generator = component_lookup.generator
1294
- generator.bitmask = remap_bitmask(component_generator.bitmask, old_indexes, new_indexes, new_count)
1440
+
1441
+ local component_bitmask = component_generator.bitmask
1442
+ if component_bitmask then
1443
+ component_generator.bitmask = remap_bitmask(component_bitmask, old_indexes, new_indexes, new_count)
1444
+ end
1445
+ component_generator.result =
1446
+ remap_bitmask(component_generator.result, old_indexes, new_indexes, new_count)
1295
1447
 
1296
1448
  local band_generator = component_lookup.band_generator
1297
- band_generator.bitmask = remap_bitmask(band_generator.bitmask, old_indexes, new_indexes, new_count)
1449
+ local band_bitmask = band_generator.bitmask
1450
+ if band_bitmask then
1451
+ band_generator.bitmask = remap_bitmask(band_bitmask, old_indexes, new_indexes, new_count)
1452
+ end
1453
+ band_generator.result = remap_bitmask(band_generator.result, old_indexes, new_indexes, new_count)
1298
1454
  end
1299
1455
  end
1300
1456
  end
@@ -1305,7 +1461,7 @@ function masking_controller.compact_members(masking: MaskingInternal)
1305
1461
 
1306
1462
  all.bitmask = remap_bitmask(all.bitmask, old_indexes, new_indexes, new_count)
1307
1463
  unactive.bitmask = remap_bitmask(unactive.bitmask, old_indexes, new_indexes, new_count)
1308
- active.bitmask = remap_bitmask(active.bitmask, old_indexes, new_indexes, new_count)
1464
+ active.result = remap_bitmask(active.result, old_indexes, new_indexes, new_count)
1309
1465
 
1310
1466
  local new_storages: Storages = {}
1311
1467
 
@@ -1314,7 +1470,7 @@ function masking_controller.compact_members(masking: MaskingInternal)
1314
1470
  storage.mask.bitmask = new_bitmask
1315
1471
  local new_hash = new_bitmask:tostring()
1316
1472
  storage.mask.hash = new_hash
1317
- storage.mask.members = masking:get_members_from_bitmask(new_bitmask)
1473
+ storage.mask.members = masking:get_clients_from_bitmask(new_bitmask)
1318
1474
 
1319
1475
  local existing = new_storages[new_hash]
1320
1476
  if existing then
@@ -1323,6 +1479,7 @@ function masking_controller.compact_members(masking: MaskingInternal)
1323
1479
  new_storages[new_hash] = storage
1324
1480
  end
1325
1481
  end
1482
+ masking.storages = new_storages
1326
1483
  end
1327
1484
 
1328
1485
  function masking_controller.create_generator_from_filter(masking: MaskingInternal, filter: MemberFilter): MaskGenerator
@@ -1334,49 +1491,51 @@ function masking_controller.create_generator_from_filter(masking: MaskingInterna
1334
1491
  end
1335
1492
  end
1336
1493
 
1337
- function masking_controller.create_exclude_generator(masking: MaskingInternal, exclude: MemberFilter): MaskGenerator
1338
- local bitmask = utils.bitmask.from_set(masking.member_indexes, exclude, masking.member_count)
1339
- local active = masking.active_members_generator
1494
+ function masking_controller.create_exclude_generator(masking: MaskingInternal, exclude: MemberFilter)
1495
+ local bitmask = bitmask_from_set(masking, exclude)
1496
+ local active_members = masking.active_members_generator
1340
1497
 
1341
- local generator = mask_generator.create(nil, function()
1342
- return active.bitmask:band(bitmask:bnot())
1498
+ local generator = mask_generator.create(bitmask, function(generator)
1499
+ local bitmask = generator.bitmask :: utils.Bitmask
1500
+ return active_members.result:band(bitmask:bnot())
1343
1501
  end)
1344
- generator:track(active)
1502
+ generator:track(active_members)
1345
1503
 
1346
1504
  return generator
1347
1505
  end
1348
1506
 
1349
1507
  -- TODO: check for players existing and register them if they don't
1350
- function masking_controller.create_include_generator(masking: MaskingInternal, filter: MemberFilter): MaskGenerator
1351
- local bitmask = utils.bitmask.from_set(masking.member_indexes, filter, masking.member_count)
1352
- local active = masking.active_members_generator
1508
+ function masking_controller.create_include_generator(masking: MaskingInternal, include: MemberFilter): MaskGenerator
1509
+ local bitmask = bitmask_from_set(masking, include)
1510
+ local active_members = masking.active_members_generator
1353
1511
 
1354
- local generator = mask_generator.create(nil, function()
1355
- return bitmask:band(active.bitmask)
1512
+ local generator = mask_generator.create(bitmask, function(generator)
1513
+ local bitmask = generator.bitmask :: utils.Bitmask
1514
+ return bitmask:band(active_members.result)
1356
1515
  end)
1357
- generator:track(active)
1516
+ generator:track(active_members)
1358
1517
 
1359
1518
  return generator
1360
1519
  end
1361
1520
 
1362
- function masking_controller.create_all_members_generator(masking: MaskingInternal): MaskGenerator
1521
+ function masking_controller.create_all_members_generator(masking: MaskingInternal)
1363
1522
  local filter: MemberFilter = {}
1364
1523
  for member in masking.member_indexes do
1365
1524
  filter[member] = true
1366
1525
  end
1367
1526
  local bitmask = utils.bitmask.from_set(masking.member_indexes, filter, masking.member_count)
1368
- return mask_generator.create(bitmask)
1527
+ return mask_generator.create(bitmask) :: MaskGeneratorWithBitmask
1369
1528
  end
1370
- function masking_controller.create_unactive_generator(masking: MaskingController): MaskGenerator
1529
+ function masking_controller.create_unactive_generator(masking: MaskingController)
1371
1530
  local bitmask = utils.bitmask.from_set(masking.member_indexes, masking.unreplicated, masking.member_count)
1372
- return mask_generator.create(bitmask)
1531
+ return mask_generator.create(bitmask) :: MaskGeneratorWithBitmask
1373
1532
  end
1374
1533
  function masking_controller.create_active_generator(masking: MaskingController): MaskGenerator
1375
1534
  local all = masking.all_members_generator
1376
1535
  local unactive = masking.unactive_members_generator
1377
1536
 
1378
1537
  local new_generator = mask_generator.create(nil, function()
1379
- return all.bitmask:band(unactive.bitmask:bnot())
1538
+ return all.result:band(unactive.result:bnot())
1380
1539
  end)
1381
1540
  new_generator:track(all)
1382
1541
  new_generator:track(unactive)
@@ -1384,12 +1543,17 @@ function masking_controller.create_active_generator(masking: MaskingController):
1384
1543
  return new_generator
1385
1544
  end
1386
1545
 
1387
- function masking_controller.register_member(masking: MaskingInternal, member: Member)
1546
+ function masking_controller.register_mask_group(masking: MaskingInternal, group: Member) end
1547
+
1548
+ function masking_controller.unregister_mask_group(masking: MaskingInternal, group: Member) end
1549
+
1550
+ function masking_controller.register_client(masking: MaskingInternal, member: Member)
1388
1551
  if masking.member_indexes[member] then
1389
1552
  return
1390
1553
  end
1391
1554
 
1392
1555
  local current = masking.member_count
1556
+ masking.client_indexes[member] = current
1393
1557
  masking.member_indexes[member] = current
1394
1558
  masking.member_count += 1
1395
1559
 
@@ -1403,22 +1567,22 @@ function masking_controller.register_member(masking: MaskingInternal, member: Me
1403
1567
  end
1404
1568
  end
1405
1569
 
1406
- function masking_controller.unregister_member(masking: MaskingInternal, member: Member)
1570
+ function masking_controller.unregister_client(masking: MaskingInternal, member: Member)
1407
1571
  local index = masking.member_indexes[member]
1408
1572
  if index == nil then
1409
1573
  return
1410
1574
  end
1575
+ masking.client_indexes[member] = nil
1411
1576
  masking.member_indexes[member] = nil
1412
1577
  masking.member_count -= 1
1413
1578
  masking.all_members_generator.bitmask:clear(index)
1414
1579
  masking.active_members_generator:compute()
1415
1580
  end
1416
1581
 
1417
- function masking_controller.activate_member(masking: MaskingInternal, member: Member)
1418
- local index = masking.member_indexes[member]
1582
+ function masking_controller.activate_client(masking: MaskingInternal, member: Member)
1583
+ local index = masking.client_indexes[member]
1419
1584
  if index == nil then
1420
- masking:register_member(member)
1421
- index = masking.member_indexes[member]
1585
+ index = masking.client_indexes[member]
1422
1586
  end
1423
1587
 
1424
1588
  masking.unreplicated[member] = nil
@@ -1427,18 +1591,21 @@ function masking_controller.activate_member(masking: MaskingInternal, member: Me
1427
1591
  end
1428
1592
 
1429
1593
  function masking_controller.member_is_active(masking: MaskingInternal, member: Member): boolean
1430
- local index = masking.member_indexes[member]
1594
+ local index = masking.client_indexes[member]
1431
1595
  if index == nil then
1432
1596
  return false
1433
1597
  end
1434
1598
 
1435
- return masking.active_members_generator.bitmask:get(index)
1599
+ return masking.active_members_generator.result:get(index)
1436
1600
  end
1437
1601
 
1438
1602
  local function create(): MaskingController
1439
1603
  local self = {} :: MaskingInternal
1440
1604
 
1441
1605
  self.member_indexes = {}
1606
+ self.client_indexes = {}
1607
+ self.group_indexes = {}
1608
+ self.client_aliases = {}
1442
1609
  self.member_count = 0
1443
1610
  self.compact_count = 100
1444
1611
  self.storages = {}
@@ -1449,6 +1616,10 @@ local function create(): MaskingController
1449
1616
  entities = {},
1450
1617
  components = {},
1451
1618
  },
1619
+ filter_deltas = {
1620
+ entities = {},
1621
+ components = {},
1622
+ },
1452
1623
  postponed = {},
1453
1624
  }
1454
1625
  self.unreplicated = {}