@rbxts/replecs 0.1.0-alpha8 → 0.1.0-alpha9

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,13 +927,18 @@ 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()
@@ -876,7 +947,7 @@ function masking_controller.update_entity_filter(masking: MaskingInternal, entit
876
947
  end
877
948
  local current_storage = lookup.storage_group
878
949
 
879
- local new_storage = masking:move_entity_storage(entity, current_storage, generator.bitmask)
950
+ local new_storage = masking:move_entity_storage(entity, current_storage, generator.result)
880
951
  lookup.storage_group = new_storage
881
952
  end
882
953
  generator.subscribed = subscribed
@@ -887,7 +958,7 @@ function masking_controller.start_entity(masking: MaskingInternal, entity: Entit
887
958
  local filter = get_usable_filter(given_filter)
888
959
  local generator = masking:update_entity_filter(entity, filter)
889
960
 
890
- local storage_group = masking:get_or_create_storage_group(generator.bitmask)
961
+ local storage_group = masking:get_or_create_storage_group(generator.result)
891
962
  local active = get_or_set_active(storage_group, entity)
892
963
  active.is_entity_mask = true
893
964
 
@@ -903,8 +974,8 @@ function masking_controller.start_entity(masking: MaskingInternal, entity: Entit
903
974
  local postponed = masking.lookups.postponed[entity]
904
975
  if postponed then
905
976
  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)
977
+ for component, postponed_info in components do
978
+ masking:start_component(entity, component, component_type, postponed_info.filter)
908
979
  end
909
980
  end
910
981
  masking.lookups.postponed[entity] = nil
@@ -954,6 +1025,15 @@ function masking_controller.stop_entity(masking: MaskingInternal, entity: Entity
954
1025
  postponed[component_type][component] = { filter = component_lookup.filter }
955
1026
  end
956
1027
  end
1028
+ for component_type, components in lookup.components do
1029
+ for component in components do
1030
+ if postponed == nil then
1031
+ postponed = create_component_indexes() :: PostponedList
1032
+ masking.lookups.postponed[entity] = postponed
1033
+ end
1034
+ postponed[component_type][component] = { filter = nil :: MemberFilter? }
1035
+ end
1036
+ end
957
1037
 
958
1038
  lookup.generator:destroy()
959
1039
  remove_active(lookup.storage_group, entity)
@@ -996,12 +1076,13 @@ function masking_controller.start_component(
996
1076
  end
997
1077
 
998
1078
  if filter == nil then
1079
+ lookup.components[component_type][component] = true
999
1080
  get_or_set_component_active(lookup.storage_group.active[entity], component, component_type)
1000
1081
  return
1001
1082
  end
1002
1083
 
1003
1084
  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)
1085
+ local storage_group = masking:get_or_create_storage_group(band_generator.result)
1005
1086
  local active = get_or_set_active(storage_group, entity)
1006
1087
 
1007
1088
  get_or_set_component_active(active, component, component_type)
@@ -1052,7 +1133,7 @@ function masking_controller.set_component(
1052
1133
 
1053
1134
  band_generator:run_subscribed()
1054
1135
  else
1055
- local desired_storage = masking:get_or_create_storage_group(band_generator.bitmask)
1136
+ local desired_storage = masking:get_or_create_storage_group(band_generator.result)
1056
1137
  local new_storage: StorageGroup
1057
1138
 
1058
1139
  if desired_storage and desired_storage == lookup.storage_group then
@@ -1064,7 +1145,7 @@ function masking_controller.set_component(
1064
1145
  component,
1065
1146
  component_type,
1066
1147
  lookup.storage_group,
1067
- band_generator.bitmask
1148
+ band_generator.result
1068
1149
  )
1069
1150
  end
1070
1151
  local new_lookup: ComponentLookup = {
@@ -1074,6 +1155,7 @@ function masking_controller.set_component(
1074
1155
  storage_group = new_storage,
1075
1156
  }
1076
1157
  lookup.filtered_components[component_type][component] = new_lookup
1158
+ lookup.components[component_type][component] = nil
1077
1159
  end
1078
1160
  end
1079
1161
 
@@ -1095,6 +1177,7 @@ function masking_controller.stop_component(
1095
1177
  if entity_lookup then
1096
1178
  remove_component_active(entity_lookup.storage_group, entity, component, component_type)
1097
1179
  end
1180
+ entity_lookup.components[component_type][component] = nil
1098
1181
  end
1099
1182
  end
1100
1183
 
@@ -1134,14 +1217,36 @@ function masking_controller.allocate_component_change(
1134
1217
  value: any
1135
1218
  )
1136
1219
  local storage = masking:get_component_storage_group(entity, component, COMPONENT_TYPES.component)
1137
- local changes = get_or_set_changes(storage, entity)
1220
+ local changes = get_or_set_changed(storage, entity)
1138
1221
  changes.component[component] = value
1139
1222
  changes.removed[component] = nil
1140
1223
  end
1141
1224
 
1225
+ function masking_controller.allocate_pair_value_change(
1226
+ masking: MaskingInternal,
1227
+ entity: Entity,
1228
+ relation: Entity,
1229
+ target: Entity,
1230
+ value: any
1231
+ )
1232
+ local storage = masking:get_component_storage_group(entity, relation, COMPONENT_TYPES.pair_value)
1233
+ local changes = get_or_set_changed(storage, entity)
1234
+ local values = changes.pairs_values.changed[relation]
1235
+ if values == nil then
1236
+ values = {}
1237
+ changes.pairs_values.changed[relation] = values
1238
+ end
1239
+ values[target] = value
1240
+
1241
+ local removed = changes.pairs_values.removed[relation]
1242
+ if removed ~= nil then
1243
+ removed[target] = nil
1244
+ end
1245
+ end
1246
+
1142
1247
  function masking_controller.allocate_tag_addition(masking: MaskingInternal, entity: Entity, tag: Entity)
1143
1248
  local storage = masking:get_component_storage_group(entity, tag, COMPONENT_TYPES.tag)
1144
- local changes = get_or_set_changes(storage, entity)
1249
+ local changes = get_or_set_changed(storage, entity)
1145
1250
  changes.tagged[tag] = true
1146
1251
  changes.removed[tag] = nil
1147
1252
  end
@@ -1153,7 +1258,7 @@ function masking_controller.allocate_pair_addition(
1153
1258
  target: Entity
1154
1259
  )
1155
1260
  local storage = masking:get_component_storage_group(entity, relation, COMPONENT_TYPES.pair)
1156
- local changes = get_or_set_changes(storage, entity)
1261
+ local changes = get_or_set_changed(storage, entity)
1157
1262
  local targets = changes.pairs[relation]
1158
1263
  if targets == nil then
1159
1264
  targets = {}
@@ -1164,7 +1269,7 @@ end
1164
1269
 
1165
1270
  function masking_controller.allocate_component_removal(masking: MaskingInternal, entity: Entity, component: Entity)
1166
1271
  local storage = masking:get_component_storage_group(entity, component, COMPONENT_TYPES.component)
1167
- local changes = get_or_set_changes(storage, entity)
1272
+ local changes = get_or_set_changed(storage, entity)
1168
1273
 
1169
1274
  changes.component[component] = nil
1170
1275
  changes.removed[component] = true
@@ -1172,7 +1277,7 @@ end
1172
1277
 
1173
1278
  function masking_controller.allocate_tag_removal(masking: MaskingInternal, entity: Entity, tag: Entity)
1174
1279
  local storage = masking:get_component_storage_group(entity, tag, COMPONENT_TYPES.tag)
1175
- local changes = get_or_set_changes(storage, entity)
1280
+ local changes = get_or_set_changed(storage, entity)
1176
1281
 
1177
1282
  changes.tagged[tag] = nil
1178
1283
  changes.removed[tag] = true
@@ -1185,7 +1290,7 @@ function masking_controller.allocate_pair_removal(
1185
1290
  target: Entity
1186
1291
  )
1187
1292
  local storage = masking:get_component_storage_group(entity, relation, COMPONENT_TYPES.pair)
1188
- local changes = get_or_set_changes(storage, entity)
1293
+ local changes = get_or_set_changed(storage, entity)
1189
1294
  local targets = changes.pairs[relation]
1190
1295
  if targets == nil then
1191
1296
  targets = {}
@@ -1194,6 +1299,27 @@ function masking_controller.allocate_pair_removal(
1194
1299
  targets[target] = false
1195
1300
  end
1196
1301
 
1302
+ function masking_controller.allocate_pair_value_removal(
1303
+ masking: MaskingInternal,
1304
+ entity: Entity,
1305
+ relation: Entity,
1306
+ target: Entity
1307
+ )
1308
+ local storage = masking:get_component_storage_group(entity, relation, COMPONENT_TYPES.pair_value)
1309
+ local changes = get_or_set_changed(storage, entity)
1310
+ local removed = changes.pairs_values.removed[relation]
1311
+ if removed == nil then
1312
+ removed = {}
1313
+ changes.pairs_values.removed[relation] = removed
1314
+ end
1315
+ removed[target] = true
1316
+
1317
+ local values = changes.pairs_values.changed[relation]
1318
+ if values ~= nil then
1319
+ values[target] = nil
1320
+ end
1321
+ end
1322
+
1197
1323
  local function remap_bitmask(
1198
1324
  bitmask: Bitmask,
1199
1325
  from: { [Member]: number },
@@ -1215,6 +1341,17 @@ local function remap_bitmask(
1215
1341
  return result
1216
1342
  end
1217
1343
 
1344
+ local function bitmask_from_set(masking: MaskingInternal, member_set: { [Member]: boolean }): utils.Bitmask
1345
+ local new_bitmask = utils.bitmask.create(masking.member_count)
1346
+ for member: Member in member_set do
1347
+ local index = masking.member_indexes[member] or masking.member_indexes[masking.client_aliases[member]]
1348
+ if index then
1349
+ new_bitmask:set(index)
1350
+ end
1351
+ end
1352
+ return new_bitmask
1353
+ end
1354
+
1218
1355
  function masking_controller.merge_storages(
1219
1356
  masking: MaskingInternal,
1220
1357
  old_storage: StorageGroup,
@@ -1225,7 +1362,7 @@ function masking_controller.merge_storages(
1225
1362
  end
1226
1363
  for entity, active in old_storage.active do
1227
1364
  local new_active = get_or_set_active(new_storage, entity)
1228
- merge_component_actives(new_active, active)
1365
+ merge_component_actives(active, new_active)
1229
1366
 
1230
1367
  if active.is_entity_mask then
1231
1368
  local lookup = masking.lookups.entities[entity]
@@ -1250,7 +1387,7 @@ function masking_controller.merge_storages(
1250
1387
  merge_component_additions(new_added_components, added_components)
1251
1388
  end
1252
1389
  for entity, changes in old_storage.changes.changed do
1253
- local new_changes = get_or_set_changes(new_storage, entity)
1390
+ local new_changes = get_or_set_changed(new_storage, entity)
1254
1391
  merge_component_changes(new_changes, changes)
1255
1392
  end
1256
1393
  for entity in old_storage.changes.added do
@@ -1277,22 +1414,44 @@ function masking_controller.compact_members(masking: MaskingInternal)
1277
1414
  local new_count = 0
1278
1415
  for member in old_indexes do
1279
1416
  new_indexes[member] = new_count
1417
+
1418
+ if masking.client_indexes[member] then
1419
+ masking.client_indexes[member] = new_count
1420
+ end
1421
+ if masking.group_indexes[new_count] then
1422
+ masking.group_indexes[new_count] = member
1423
+ end
1424
+
1280
1425
  new_count += 1
1281
1426
  end
1282
1427
  masking.member_indexes = new_indexes
1283
1428
  masking.member_count = new_count
1284
1429
 
1285
1430
  for _, entity_lookup in masking.lookups.entities do
1286
- local generator = entity_lookup.generator
1287
- generator.bitmask = remap_bitmask(generator.bitmask, old_indexes, new_indexes, new_count)
1431
+ local entity_generator = entity_lookup.generator
1432
+ local entity_bitmask = entity_generator.bitmask
1433
+ if entity_bitmask then
1434
+ entity_generator.bitmask = remap_bitmask(entity_bitmask, old_indexes, new_indexes, new_count)
1435
+ end
1436
+ entity_generator.result = remap_bitmask(entity_generator.result, old_indexes, new_indexes, new_count)
1288
1437
 
1289
1438
  for _, lookups in entity_lookup.filtered_components do
1290
1439
  for _, component_lookup in lookups do
1291
1440
  local component_generator = component_lookup.generator
1292
- generator.bitmask = remap_bitmask(component_generator.bitmask, old_indexes, new_indexes, new_count)
1441
+
1442
+ local component_bitmask = component_generator.bitmask
1443
+ if component_bitmask then
1444
+ component_generator.bitmask = remap_bitmask(component_bitmask, old_indexes, new_indexes, new_count)
1445
+ end
1446
+ component_generator.result =
1447
+ remap_bitmask(component_generator.result, old_indexes, new_indexes, new_count)
1293
1448
 
1294
1449
  local band_generator = component_lookup.band_generator
1295
- band_generator.bitmask = remap_bitmask(band_generator.bitmask, old_indexes, new_indexes, new_count)
1450
+ local band_bitmask = band_generator.bitmask
1451
+ if band_bitmask then
1452
+ band_generator.bitmask = remap_bitmask(band_bitmask, old_indexes, new_indexes, new_count)
1453
+ end
1454
+ band_generator.result = remap_bitmask(band_generator.result, old_indexes, new_indexes, new_count)
1296
1455
  end
1297
1456
  end
1298
1457
  end
@@ -1303,7 +1462,7 @@ function masking_controller.compact_members(masking: MaskingInternal)
1303
1462
 
1304
1463
  all.bitmask = remap_bitmask(all.bitmask, old_indexes, new_indexes, new_count)
1305
1464
  unactive.bitmask = remap_bitmask(unactive.bitmask, old_indexes, new_indexes, new_count)
1306
- active.bitmask = remap_bitmask(active.bitmask, old_indexes, new_indexes, new_count)
1465
+ active.result = remap_bitmask(active.result, old_indexes, new_indexes, new_count)
1307
1466
 
1308
1467
  local new_storages: Storages = {}
1309
1468
 
@@ -1312,7 +1471,7 @@ function masking_controller.compact_members(masking: MaskingInternal)
1312
1471
  storage.mask.bitmask = new_bitmask
1313
1472
  local new_hash = new_bitmask:tostring()
1314
1473
  storage.mask.hash = new_hash
1315
- storage.mask.members = masking:get_members_from_bitmask(new_bitmask)
1474
+ storage.mask.members = masking:get_clients_from_bitmask(new_bitmask)
1316
1475
 
1317
1476
  local existing = new_storages[new_hash]
1318
1477
  if existing then
@@ -1321,6 +1480,7 @@ function masking_controller.compact_members(masking: MaskingInternal)
1321
1480
  new_storages[new_hash] = storage
1322
1481
  end
1323
1482
  end
1483
+ masking.storages = new_storages
1324
1484
  end
1325
1485
 
1326
1486
  function masking_controller.create_generator_from_filter(masking: MaskingInternal, filter: MemberFilter): MaskGenerator
@@ -1332,49 +1492,51 @@ function masking_controller.create_generator_from_filter(masking: MaskingInterna
1332
1492
  end
1333
1493
  end
1334
1494
 
1335
- function masking_controller.create_exclude_generator(masking: MaskingInternal, exclude: MemberFilter): MaskGenerator
1336
- local bitmask = utils.bitmask.from_set(masking.member_indexes, exclude, masking.member_count)
1337
- local active = masking.active_members_generator
1495
+ function masking_controller.create_exclude_generator(masking: MaskingInternal, exclude: MemberFilter)
1496
+ local bitmask = bitmask_from_set(masking, exclude)
1497
+ local active_members = masking.active_members_generator
1338
1498
 
1339
- local generator = mask_generator.create(nil, function()
1340
- return active.bitmask:band(bitmask:bnot())
1499
+ local generator = mask_generator.create(bitmask, function(generator)
1500
+ local bitmask = generator.bitmask :: utils.Bitmask
1501
+ return active_members.result:band(bitmask:bnot())
1341
1502
  end)
1342
- generator:track(active)
1503
+ generator:track(active_members)
1343
1504
 
1344
1505
  return generator
1345
1506
  end
1346
1507
 
1347
1508
  -- TODO: check for players existing and register them if they don't
1348
- function masking_controller.create_include_generator(masking: MaskingInternal, filter: MemberFilter): MaskGenerator
1349
- local bitmask = utils.bitmask.from_set(masking.member_indexes, filter, masking.member_count)
1350
- local active = masking.active_members_generator
1509
+ function masking_controller.create_include_generator(masking: MaskingInternal, include: MemberFilter): MaskGenerator
1510
+ local bitmask = bitmask_from_set(masking, include)
1511
+ local active_members = masking.active_members_generator
1351
1512
 
1352
- local generator = mask_generator.create(nil, function()
1353
- return bitmask:band(active.bitmask)
1513
+ local generator = mask_generator.create(bitmask, function(generator)
1514
+ local bitmask = generator.bitmask :: utils.Bitmask
1515
+ return bitmask:band(active_members.result)
1354
1516
  end)
1355
- generator:track(active)
1517
+ generator:track(active_members)
1356
1518
 
1357
1519
  return generator
1358
1520
  end
1359
1521
 
1360
- function masking_controller.create_all_members_generator(masking: MaskingInternal): MaskGenerator
1522
+ function masking_controller.create_all_members_generator(masking: MaskingInternal)
1361
1523
  local filter: MemberFilter = {}
1362
1524
  for member in masking.member_indexes do
1363
1525
  filter[member] = true
1364
1526
  end
1365
1527
  local bitmask = utils.bitmask.from_set(masking.member_indexes, filter, masking.member_count)
1366
- return mask_generator.create(bitmask)
1528
+ return mask_generator.create(bitmask) :: MaskGeneratorWithBitmask
1367
1529
  end
1368
- function masking_controller.create_unactive_generator(masking: MaskingController): MaskGenerator
1530
+ function masking_controller.create_unactive_generator(masking: MaskingController)
1369
1531
  local bitmask = utils.bitmask.from_set(masking.member_indexes, masking.unreplicated, masking.member_count)
1370
- return mask_generator.create(bitmask)
1532
+ return mask_generator.create(bitmask) :: MaskGeneratorWithBitmask
1371
1533
  end
1372
1534
  function masking_controller.create_active_generator(masking: MaskingController): MaskGenerator
1373
1535
  local all = masking.all_members_generator
1374
1536
  local unactive = masking.unactive_members_generator
1375
1537
 
1376
1538
  local new_generator = mask_generator.create(nil, function()
1377
- return all.bitmask:band(unactive.bitmask:bnot())
1539
+ return all.result:band(unactive.result:bnot())
1378
1540
  end)
1379
1541
  new_generator:track(all)
1380
1542
  new_generator:track(unactive)
@@ -1382,12 +1544,17 @@ function masking_controller.create_active_generator(masking: MaskingController):
1382
1544
  return new_generator
1383
1545
  end
1384
1546
 
1385
- function masking_controller.register_member(masking: MaskingInternal, member: Member)
1547
+ function masking_controller.register_mask_group(masking: MaskingInternal, group: Member) end
1548
+
1549
+ function masking_controller.unregister_mask_group(masking: MaskingInternal, group: Member) end
1550
+
1551
+ function masking_controller.register_client(masking: MaskingInternal, member: Member)
1386
1552
  if masking.member_indexes[member] then
1387
1553
  return
1388
1554
  end
1389
1555
 
1390
1556
  local current = masking.member_count
1557
+ masking.client_indexes[member] = current
1391
1558
  masking.member_indexes[member] = current
1392
1559
  masking.member_count += 1
1393
1560
 
@@ -1401,22 +1568,22 @@ function masking_controller.register_member(masking: MaskingInternal, member: Me
1401
1568
  end
1402
1569
  end
1403
1570
 
1404
- function masking_controller.unregister_member(masking: MaskingInternal, member: Member)
1571
+ function masking_controller.unregister_client(masking: MaskingInternal, member: Member)
1405
1572
  local index = masking.member_indexes[member]
1406
1573
  if index == nil then
1407
1574
  return
1408
1575
  end
1576
+ masking.client_indexes[member] = nil
1409
1577
  masking.member_indexes[member] = nil
1410
1578
  masking.member_count -= 1
1411
1579
  masking.all_members_generator.bitmask:clear(index)
1412
1580
  masking.active_members_generator:compute()
1413
1581
  end
1414
1582
 
1415
- function masking_controller.activate_member(masking: MaskingInternal, member: Member)
1416
- local index = masking.member_indexes[member]
1583
+ function masking_controller.activate_client(masking: MaskingInternal, member: Member)
1584
+ local index = masking.client_indexes[member]
1417
1585
  if index == nil then
1418
- masking:register_member(member)
1419
- index = masking.member_indexes[member]
1586
+ index = masking.client_indexes[member]
1420
1587
  end
1421
1588
 
1422
1589
  masking.unreplicated[member] = nil
@@ -1425,18 +1592,21 @@ function masking_controller.activate_member(masking: MaskingInternal, member: Me
1425
1592
  end
1426
1593
 
1427
1594
  function masking_controller.member_is_active(masking: MaskingInternal, member: Member): boolean
1428
- local index = masking.member_indexes[member]
1595
+ local index = masking.client_indexes[member]
1429
1596
  if index == nil then
1430
1597
  return false
1431
1598
  end
1432
1599
 
1433
- return masking.active_members_generator.bitmask:get(index)
1600
+ return masking.active_members_generator.result:get(index)
1434
1601
  end
1435
1602
 
1436
1603
  local function create(): MaskingController
1437
1604
  local self = {} :: MaskingInternal
1438
1605
 
1439
1606
  self.member_indexes = {}
1607
+ self.client_indexes = {}
1608
+ self.group_indexes = {}
1609
+ self.client_aliases = {}
1440
1610
  self.member_count = 0
1441
1611
  self.compact_count = 100
1442
1612
  self.storages = {}
@@ -1447,6 +1617,10 @@ local function create(): MaskingController
1447
1617
  entities = {},
1448
1618
  components = {},
1449
1619
  },
1620
+ filter_deltas = {
1621
+ entities = {},
1622
+ components = {},
1623
+ },
1450
1624
  postponed = {},
1451
1625
  }
1452
1626
  self.unreplicated = {}