|
631 | 631 | local world_get: (world: World, entityId: i53, a: i53, b: i53?, c: i53?, d: i53?, e: i53?) -> (...any)
|
632 | 632 | do
|
633 | 633 | -- Keeping the function as small as possible to enable inlining
|
634 |
| - local function fetch(id: i24, records: { ArchetypeRecord }, columns, row): any |
| 634 | + local records |
| 635 | + local columns |
| 636 | + local row |
| 637 | + |
| 638 | + local function fetch(id): any |
635 | 639 | local tr = records[id]
|
636 | 640 |
|
637 | 641 | if not tr then
|
|
641 | 645 | return columns[tr.column][row]
|
642 | 646 | end
|
643 | 647 |
|
644 |
| - function world_get(world: World, entityId: i53, a: i53, b: i53?, c: i53?, d: i53?, e: i53?): ...any |
645 |
| - local id = entityId |
646 |
| - local record = world.entityIndex.sparse[id] |
| 648 | + function world_get(world: World, entity: i53, a: i53, b: i53?, c: i53?, d: i53?, e: i53?): ...any |
| 649 | + local record = world.entityIndex.sparse[entity] |
647 | 650 | if not record then
|
648 | 651 | return nil
|
649 | 652 | end
|
|
653 | 656 | return nil
|
654 | 657 | end
|
655 | 658 |
|
656 |
| - local tr = archetype.records |
657 |
| - local columns = archetype.columns |
658 |
| - local row = record.row |
| 659 | + records = archetype.records |
| 660 | + columns = archetype.columns |
| 661 | + row = record.row |
659 | 662 |
|
660 |
| - local va = fetch(a, tr, columns, row) |
| 663 | + local va = fetch(a) |
661 | 664 |
|
662 |
| - if b == nil then |
| 665 | + if not b then |
663 | 666 | return va
|
664 |
| - elseif c == nil then |
665 |
| - return va, fetch(b, tr, columns, row) |
666 |
| - elseif d == nil then |
667 |
| - return va, fetch(b, tr, columns, row), fetch(c, tr, columns, row) |
668 |
| - elseif e == nil then |
669 |
| - return va, fetch(b, tr, columns, row), fetch(c, tr, columns, row), fetch(d, tr, columns, row) |
| 667 | + elseif not c then |
| 668 | + return va, fetch(b) |
| 669 | + elseif not d then |
| 670 | + return va, fetch(b), fetch(c) |
| 671 | + elseif not e then |
| 672 | + return va, fetch(b), fetch(c), fetch(d) |
670 | 673 | else
|
671 | 674 | error("args exceeded")
|
672 | 675 | end
|
673 | 676 | end
|
674 | 677 | end
|
675 | 678 |
|
676 |
| -local world_has: (world: World, entity: number, ...i53) -> boolean |
677 |
| -do |
678 |
| - function world_has(world, entity, ...) |
679 |
| - local record = world.entityIndex.sparse[entity] |
680 |
| - if not record then |
681 |
| - return false |
682 |
| - end |
| 679 | +local function world_has(world: World, entity: number, ...: i53): boolean |
| 680 | + local record = world.entityIndex.sparse[entity] |
| 681 | + if not record then |
| 682 | + return false |
| 683 | + end |
683 | 684 |
|
684 |
| - local archetype = record.archetype |
685 |
| - if not archetype then |
686 |
| - return false |
687 |
| - end |
| 685 | + local archetype = record.archetype |
| 686 | + if not archetype then |
| 687 | + return false |
| 688 | + end |
688 | 689 |
|
689 |
| - local tr = archetype.records |
| 690 | + local records = archetype.records |
690 | 691 |
|
691 |
| - for i = 1, select("#", ...) do |
692 |
| - if not tr[select(i, ...)] then |
693 |
| - return false |
694 |
| - end |
| 692 | + for i = 1, select("#", ...) do |
| 693 | + if not records[select(i, ...)] then |
| 694 | + return false |
695 | 695 | end
|
696 |
| - |
697 |
| - return true |
698 | 696 | end
|
| 697 | + |
| 698 | + return true |
699 | 699 | end
|
700 | 700 |
|
701 | 701 | type Item = () -> (number, ...any)
|
|
734 | 734 | without = Arm,
|
735 | 735 | archetypes = function()
|
736 | 736 | return empty_list
|
737 |
| - end |
| 737 | + end, |
738 | 738 | }
|
739 | 739 |
|
740 | 740 | setmetatable(EmptyQuery, EmptyQuery)
|
|
776 | 776 |
|
777 | 777 | for archetype_id in idr.cache do
|
778 | 778 | local compatibleArchetype = archetypes[archetype_id]
|
779 |
| - local tr = compatibleArchetype.records |
| 779 | + local records = compatibleArchetype.records |
780 | 780 |
|
781 | 781 | local skip = false
|
782 | 782 |
|
783 | 783 | for i, id in ids do
|
784 |
| - local index = tr[id] |
785 |
| - if not index then |
| 784 | + local tr = records[id] |
| 785 | + if not tr then |
786 | 786 | skip = true
|
787 | 787 | break
|
788 | 788 | end
|
|
984 | 984 | return entityId, a[row], b[row], c[row], d[row], e[row], f[row], g[row], h[row]
|
985 | 985 | end
|
986 | 986 |
|
987 |
| - local field = archetype.records |
| 987 | + local records = archetype.records |
988 | 988 | for j, id in ids do
|
989 |
| - queryOutput[j] = columns[field[id].column][row] |
| 989 | + queryOutput[j] = columns[records[id].column][row] |
990 | 990 | end
|
991 | 991 |
|
992 | 992 | return entityId, unpack(queryOutput)
|
@@ -1065,108 +1065,108 @@ do
|
1065 | 1065 | end
|
1066 | 1066 |
|
1067 | 1067 | local function world_query_without(query, ...)
|
1068 |
| - local N = select("#", ...) |
1069 |
| - for i = #compatible_archetypes, 1, -1 do |
1070 |
| - local archetype = compatible_archetypes[i] |
1071 |
| - local tr = archetype.records |
1072 |
| - local shouldRemove = false |
1073 |
| - |
1074 |
| - for j = 1, N do |
1075 |
| - local id = select(i, ...) |
1076 |
| - if tr[id] then |
1077 |
| - shouldRemove = true |
1078 |
| - break |
1079 |
| - end |
1080 |
| - end |
| 1068 | + local N = select("#", ...) |
| 1069 | + for i = #compatible_archetypes, 1, -1 do |
| 1070 | + local archetype = compatible_archetypes[i] |
| 1071 | + local records = archetype.records |
| 1072 | + local shouldRemove = false |
| 1073 | + |
| 1074 | + for j = 1, N do |
| 1075 | + local id = select(j, ...) |
| 1076 | + if records[id] then |
| 1077 | + shouldRemove = true |
| 1078 | + break |
| 1079 | + end |
| 1080 | + end |
1081 | 1081 |
|
1082 |
| - if shouldRemove then |
1083 |
| - local last = #compatible_archetypes |
1084 |
| - if last ~= i then |
1085 |
| - compatible_archetypes[i] = compatible_archetypes[last] |
1086 |
| - end |
1087 |
| - compatible_archetypes[last] = nil |
1088 |
| - length -= 1 |
| 1082 | + if shouldRemove then |
| 1083 | + local last = #compatible_archetypes |
| 1084 | + if last ~= i then |
| 1085 | + compatible_archetypes[i] = compatible_archetypes[last] |
1089 | 1086 | end
|
1090 |
| - end |
1091 |
| - |
1092 |
| - if length == 0 then |
1093 |
| - return EmptyQuery |
| 1087 | + compatible_archetypes[last] = nil |
| 1088 | + length -= 1 |
1094 | 1089 | end
|
| 1090 | + end |
| 1091 | + |
| 1092 | + if length == 0 then |
| 1093 | + return EmptyQuery |
| 1094 | + end |
1095 | 1095 |
|
1096 |
| - return query |
| 1096 | + return query |
1097 | 1097 | end
|
1098 | 1098 |
|
1099 | 1099 | local function world_query_replace(query, fn: (...any) -> (...any))
|
1100 |
| - query_init(query) |
1101 |
| - |
1102 |
| - for i, archetype in compatible_archetypes do |
1103 |
| - local columns = archetype.columns |
1104 |
| - local tr = archetype.records |
1105 |
| - for row in archetype.entities do |
1106 |
| - if not B then |
1107 |
| - local va = columns[tr[A].column] |
1108 |
| - local pa = fn(va[row]) |
1109 |
| - |
1110 |
| - va[row] = pa |
1111 |
| - elseif not C then |
1112 |
| - local va = columns[tr[A].column] |
1113 |
| - local vb = columns[tr[B].column] |
1114 |
| - |
1115 |
| - va[row], vb[row] = fn(va[row], vb[row]) |
1116 |
| - elseif not D then |
1117 |
| - local va = columns[tr[A].column] |
1118 |
| - local vb = columns[tr[B].column] |
1119 |
| - local vc = columns[tr[C].column] |
1120 |
| - |
1121 |
| - va[row], vb[row], vc[row] = fn(va[row], vb[row], vc[row]) |
1122 |
| - elseif not E then |
1123 |
| - local va = columns[tr[A].column] |
1124 |
| - local vb = columns[tr[B].column] |
1125 |
| - local vc = columns[tr[C].column] |
1126 |
| - local vd = columns[tr[D].column] |
1127 |
| - |
1128 |
| - va[row], vb[row], vc[row], vd[row] = fn( |
1129 |
| - va[row], vb[row], vc[row], vd[row]) |
1130 |
| - else |
1131 |
| - local field = archetype.records |
1132 |
| - for j, id in ids do |
1133 |
| - queryOutput[j] = columns[field[id].column][row] |
1134 |
| - end |
1135 |
| - world_query_replace_values(row, columns, |
1136 |
| - fn(unpack(queryOutput))) |
| 1100 | + query_init(query) |
| 1101 | + |
| 1102 | + for i, archetype in compatible_archetypes do |
| 1103 | + local columns = archetype.columns |
| 1104 | + local records = archetype.records |
| 1105 | + for row in archetype.entities do |
| 1106 | + if not B then |
| 1107 | + local va = columns[records[A].column] |
| 1108 | + local pa = fn(va[row]) |
| 1109 | + |
| 1110 | + va[row] = pa |
| 1111 | + elseif not C then |
| 1112 | + local va = columns[records[A].column] |
| 1113 | + local vb = columns[records[B].column] |
| 1114 | + |
| 1115 | + va[row], vb[row] = fn(va[row], vb[row]) |
| 1116 | + elseif not D then |
| 1117 | + local va = columns[records[A].column] |
| 1118 | + local vb = columns[records[B].column] |
| 1119 | + local vc = columns[records[C].column] |
| 1120 | + |
| 1121 | + va[row], vb[row], vc[row] = fn(va[row], vb[row], vc[row]) |
| 1122 | + elseif not E then |
| 1123 | + local va = columns[records[A].column] |
| 1124 | + local vb = columns[records[B].column] |
| 1125 | + local vc = columns[records[C].column] |
| 1126 | + local vd = columns[records[D].column] |
| 1127 | + |
| 1128 | + va[row], vb[row], vc[row], vd[row] = fn( |
| 1129 | + va[row], vb[row], vc[row], vd[row]) |
| 1130 | + else |
| 1131 | + for j, id in ids do |
| 1132 | + local tr = records[id] |
| 1133 | + queryOutput[j] = columns[tr.column][row] |
1137 | 1134 | end
|
1138 |
| - end |
| 1135 | + world_query_replace_values(row, columns, |
| 1136 | + fn(unpack(queryOutput))) |
| 1137 | + end |
1139 | 1138 | end
|
| 1139 | + end |
1140 | 1140 | end
|
1141 | 1141 |
|
1142 | 1142 | local function world_query_with(query, ...)
|
1143 |
| - local N = select("#", ...) |
1144 |
| - for i = #compatible_archetypes, 1, -1 do |
1145 |
| - local archetype = compatible_archetypes[i] |
1146 |
| - local tr = archetype.records |
1147 |
| - local shouldRemove = false |
1148 |
| - |
1149 |
| - for j = 1, N do |
1150 |
| - local id = select(i, ...) |
1151 |
| - if not tr[id] then |
1152 |
| - shouldRemove = true |
1153 |
| - break |
1154 |
| - end |
1155 |
| - end |
1156 |
| - |
1157 |
| - if shouldRemove then |
1158 |
| - local last = #compatible_archetypes |
1159 |
| - if last ~= i then |
1160 |
| - compatible_archetypes[i] = compatible_archetypes[last] |
1161 |
| - end |
1162 |
| - compatible_archetypes[last] = nil |
1163 |
| - length -= 1 |
1164 |
| - end |
1165 |
| - end |
1166 |
| - if length == 0 then |
1167 |
| - return EmptyQuery |
| 1143 | + local N = select("#", ...) |
| 1144 | + for i = #compatible_archetypes, 1, -1 do |
| 1145 | + local archetype = compatible_archetypes[i] |
| 1146 | + local records = archetype.records |
| 1147 | + local shouldRemove = false |
| 1148 | + |
| 1149 | + for j = 1, N do |
| 1150 | + local id = select(j, ...) |
| 1151 | + if not records[id] then |
| 1152 | + shouldRemove = true |
| 1153 | + break |
| 1154 | + end |
1168 | 1155 | end
|
1169 |
| - return query |
| 1156 | + |
| 1157 | + if shouldRemove then |
| 1158 | + local last = #compatible_archetypes |
| 1159 | + if last ~= i then |
| 1160 | + compatible_archetypes[i] = compatible_archetypes[last] |
| 1161 | + end |
| 1162 | + compatible_archetypes[last] = nil |
| 1163 | + length -= 1 |
| 1164 | + end |
| 1165 | + end |
| 1166 | + if length == 0 then |
| 1167 | + return EmptyQuery |
| 1168 | + end |
| 1169 | + return query |
1170 | 1170 | end
|
1171 | 1171 |
|
1172 | 1172 | -- Meant for directly iterating over archetypes to minimize
|
|
0 commit comments