Mass Battle Frameworkv1.20.0

Fragment 存值,Tag 给查 A Fragment Holds the Value, a Tag Makes It Queryable

QUERY & FILTERING

为什么队伍和子类型各有一套 fragment 加一套 tag,以及这套东西的意义由谁定义 · 2026-11-21Why team and subtype each ship a fragment and a set of tags, and who decides what they mean · 2026-11-21

系列这些文章写给人和他的 agent 一起看。这套文档怎么读 →SeriesThese posts are for you and your agent. How to read these docs →

两套东西,看着重复 Two Sets That Look Redundant

队伍这块有两套东西。一个 FTeam fragment,里面只装一个整数索引;还有一组空标签 FTeam0Tag … FTeam255Tag,256 个,每一个只承载一件事:"我是 7 号队"。子类型(subtype)同样配了两套:一个装整数的 fragment,一组按索引排好的空标签。

Team ships twice. There is an FTeam fragment holding a single integer index, and a family of empty tags FTeam0Tag … FTeam255Tag — 256 of them, each carrying one fact: "I am team seven". Subtype is the same pair: an integer-carrying fragment plus a tag family ordered by index.

第一次看的人大概都会问:"这不是重复吗"。它们服务的是两个不同的动作:fragment 管存,tag 管查。要理解这一点,得先说清两者之间那条像魔法一样的线。

The first reaction is usually "is that not redundant". The two serve different actions: the fragment holds, the tag is queried. To see why, start with the line between them that looks like magic.

那条线是我们写的 That Line Is Ours

改一个单位的队伍索引,它的队伍标签会跟着换,看上去像是 MassEntity 天生把两者绑在一起。其实没有。MassEntity 根本不知道这两个东西有关系:fragment 里那个整数,跟另一个完全独立的标签类型,在引擎眼里毫无关联。

Change a unit's team index and its team tag follows, as if MassEntity had joined the two. It has not. The engine has no idea these two are related: an integer inside a fragment and an entirely separate tag type mean nothing to each other.

真实情况是:fragment 里除了当前索引,还存了一个上一个索引。处理器每帧对每个单位比一次,两个值不同就撤掉旧标签、加上新索引对应的标签,然后把"上一个"改成当前值。整件事就这么多内容,一条我们每帧执行一次的规则,写在我们自己的处理器里。

The truth: besides the current index, the fragment keeps the previous one. Every frame the processor compares the two per unit; when they differ it removes the old tag, adds the tag for the new index, and records the current value as the previous. That is the whole mechanism, a rule of ours run once a frame inside our own processor.

几个容易被当成 bug 的现象也就说得通了。绕过设置函数直接改 fragment 里的索引,标签会在下一帧才跟上,比较发生在那个时候。用批量命令一次改一大批,那批标签的迁移也在同一刻成批发生,这就是"改 tag 会搬原型",代价写在写入端。

A few things that look like bugs make sense now. Write the index straight into the fragment and skip the setter, and the tag catches up on the next frame, when the comparison runs. Change a large batch through a deferred command and their tags migrate in a batch at that same moment: that is "changing a tag re-homes the entity", and the cost lands on the write side.

那为什么不只留索引 So Why Not Keep Only the Index

查询按类型过滤,从不看字段里的值。它能说"必须有这个片段(或这个标签)、必须没有那个",但没法说"这个片段里的整数等于 3"。要按队伍筛人,只有两条路:

Because a query filters by type; it never looks inside a field's value. It can say "must have this fragment (or this tag), must not have that one", never "the integer inside this fragment equals 3". Filtering by team leaves two roads:

做法Approach代价Cost
遍历所有单位,逐个读片段里的索引来比Walk every unit and compare the index read from its fragment 每个单位都得走一遍、取一次片段,查询的整块跳过完全用不上Every unit is visited and every fragment fetched; the query's whole-block skipping cannot help at all
每个取值给一个标签,查询里直接写"必须有 7 号队的标签"One tag per value, and the query says "must have the team-7 tag" 只有改队伍时才付一次原型迁移;平时查询可以整块跳过One archetype migration when the team changes; afterwards the query skips whole blocks

256 个空标签看着很丑,换来的就是上面第二行。"把值投影一份到标签空间"是个老做法:值的权威仍然只有一个,在 fragment 里;标签只是它的可查询投影,靠那条每帧比较的规则跟值同步。

256 empty tags look ugly, and they buy exactly that second row: projecting a value into tag space. The value keeps one authority, the fragment; the tag is a queryable projection of it, held in sync by the per-frame comparison.

这些标签,框架内部一个都不用 The Framework Itself Queries None of Them

有一点得说清楚,否则很容易把它们当成内部机制:MassBattle 自己的行为、移动、索敌、伤害,没有任何一处查询用到队伍标签或子类型标签。它们是留给你的过滤键。
(别把两者混起来:标签这边没人查,索引内部有人用——避障按队伍过滤邻居,渲染按子类型挑渲染器。局部判断读索引,大规模筛选读标签,两件事分工不同,「两个上限」那节还会回到这里。)

Say this plainly or the tags look like internal machinery: none of MassBattle's own behaviour, movement, targeting or damage queries reads the team or subtype tags. They are filter keys for you.
(Keep the two apart: nobody queries the tags, while the indexes are read inside — avoidance filters neighbours by team, the renderer picks by subtype. Local judgement reads the index, bulk filtering reads the tag. The section on the two ceilings comes back to this.)

所以它们是可选的。一个都不用,框架照样跑。只有碰上"找出 3 号队的所有弓箭手"这种大批筛选,它们才值回票价:那时候省下的是逐个取片段的那份成本,还有整块跳过的能力。

So they are optional. Use none of them and the framework runs. They earn their keep only when you need a bulk filter like "all archers of team 3", because there you stop paying the per-unit fragment read and start skipping whole blocks.

例外是两个:两个索引内部都在用 Two Exceptions: Both Indexes Are Used Inside

先说子类型。它的索引不纯粹是给你用的投影:渲染器按它去重,一个子类型对应一个渲染器,查找就靠这个索引。所以同一个值担着两件事:你用它筛人,我们用它决定"这批单位画在哪套渲染器上"。

Subtype first. Its index is not purely a projection for you: the renderer dedupes by it, one subtype maps to one renderer, and the lookup runs on that index. So the value carries two jobs — your filter, and our answer to "which renderer draws this batch".

队伍索引也一样,而且它落在一个很具体的功能上:「Ignore Other Teams」。打开之后,避障把"队伍索引跟自己不同"的邻居直接跳过。碰撞与 PBD 不受影响,但被过滤的是同一份邻居数组,交通规则、目标占位判定、目标偏转也就一起看不到这些邻居了。这个开关要工作,每个邻居都得知道自己的队伍索引,那份 1 字节记录就是为它存在的。

The team index is the same, and it lands in one concrete feature: "Ignore Other Teams". Turn it on and avoidance skips every neighbour whose team index differs from the agent's. Collision and PBD are untouched, but the filtered array is the same one traffic rules, goal-occupancy tests and goal deflection read, so they stop seeing those neighbours too. For the switch to work, every neighbour must know its own team index — that one-byte record exists for exactly this.

所以两个索引都有内部消费者,用法却跟标签毫无共同点:一个回答"这个单位画成什么样",一个回答"要不要避开它"。都是针对单个单位的局部判断,读一个整数就够。标签干的是另一件事,把一大批实体筛出来。同一个值投影两次,喂给两种访问模式。

Both indexes have internal consumers, and neither use resembles a tag: one answers "what does this unit look like", the other "should I avoid it". Both are local judgements about a single unit, settled by one integer. A tag does something else: it filters a large set. One value, projected twice, feeding two access patterns.

最常见的困惑也出在这里:两个单位用了同一个子类型,于是长得一模一样。它们的数据可以完全不同,体型、血量、伤害、动画各是各的,但子类型相同就落在同一个渲染器上,自然共用一套模型。这是逻辑与渲染分开的结果:数据住在片段里,长什么样由渲染器决定,渲染器按子类型索引去找。

That explains the most common confusion: two units share a subtype and look identical. Their data can differ completely (size, health, damage, animation), but the same subtype lands them on the same renderer, so they share one model. Logic and rendering are separate layers: data lives in fragments, the look is chosen by the renderer, and the renderer is found by subtype index.

想让它们看起来不同,办法很直接:给它们不同的子类型,各自配好渲染器。改数据等画面变化没用,画面在这一层不读数据。子类型的取值范围同样有上限(下一节的 0–255),不过你多半先撞上"一个子类型只能有一套渲染器"这条,轮不到上限。

Making them look different is direct: give them different subtypes and wire a renderer to each. Changing the data will not change the picture, because the picture does not read the data at this layer. The subtype index has a ceiling too (0–255, next section); you will hit "one subtype, one renderer" well before it.

两个上限:都是 0–255,而且原因不一样 Two Ceilings, Both 0–255, for Different Reasons

队伍和子类型的索引范围都是 0–255。越界的值设不进去,设置函数会拒绝并打一条警告,不会悄悄生效。原因有两条,第二条才是硬的:

Both indexes run 0–255. An out-of-range value never takes effect silently: the setter refuses it and logs a warning. Two reasons, and the second is the hard one:

一、标签是逐个写出来的。"一个取值对应一个标签",标签族就得是一堆具体的类型:队伍 256 个,子类型 256 个。要支持更大的索引,只能再写更多空类型。这是枚举方案的固有成本,也是不把标签做成"带名称的位"的原因。

One: the tags are written out one by one. One value, one tag, so the family is a list of concrete types: 256 for team, 256 for subtype. Larger indexes mean writing more empty types — the inherent cost of the enumeration approach, and the reason the tags are not a named bit field.

二、队伍索引在网格里只占一个字节。每个单位在网格里有一份压到极限的记录,避障逐邻居读它,每个字节都是要付钱的。队伍索引就放在其中一个字节里,注册时从片段发布过去,天生只能装 0–255。这一条比标签更硬:你有办法写出更多标签也没用,网格这份记录装不下。

Two: the team index costs exactly one byte in the grid. Every unit has a tightly packed record there, because avoidance reads it per neighbour and every byte is paid for. The team index sits in one of those bytes (published from the fragment at registration) and can never hold more than 0–255. This is the harder limit: define all the tags you like, that grid record still cannot store the value.

子类型不受第二条约束,网格里不存它;但第一条照样适用,所以也是 0–255,加不到无穷。实际用起来两边都绰绰有余:256 支队伍配 256 种子类型,组合出来六万多种分类。

Subtype escapes the second constraint (the grid does not store it) and still gets the first, so it is 0–255 as well. In practice both are plenty: 256 teams against 256 subtypes gives more than sixty thousand categories.

这也回答了"为什么索引内部有人用、标签内部没人用"。内部问的是"这个邻居跟我是同队吗",逐邻居比一次,一个字节就够,用不上标签。你问的是"把 7 号队的人全挑出来",那是一次大规模筛选,标签才划算。同一个值的两种投影,喂给两种访问模式。

That answers "why do the indexes have internal consumers, and the tags none": inside, the question is per neighbour, "is this one on my team", and one byte settles it; no tag needed. Your question is "pick everyone on team 7", a bulk filter, which is where a tag pays. Two projections of one value, serving two different access patterns.

顺便破一个观念:并不存在"队伍"这个对象 Breaking an Object-Minded Assumption: There Is No Team Object

在面向对象的世界里,"这个单位属于某支队伍"通常意味一个队伍对象、一份成员列表、一根引用。这里一样都没有,只有标签空间里的一个取值。我们把它叫"队伍",是这个用途最常见,底层并没有"队伍"这个东西。

In an object world, "this unit belongs to a squad" means a squad object, a member list, a reference. None of that exists here. There is only a value in tag space, and we call it "team" because that is the common use, not because a team exists underneath.

推论很好用:你定义的标签叫什么都行,功能完全一样,阵营、兵种组、刷怪波次、所属城池、有没有被玩家选中、是不是这场战斗的主角。机制上它们完全平权:都是一个空标签,改一次搬一次原型,查询时都能整块跳过。

The practical corollary: a tag with any name can take the same job — faction, unit class group, spawn wave, home city, whether the player selected it, whether it is the hero of this fight. Mechanically all equal: an empty tag, an archetype move when it changes, whole-block skipping when queried.

这意味着查询的意义完全由你定义。框架自带的标签只是一份"常用词表",不构成规则:别因为标签叫队伍,就以为单位只能按队伍组织;也别因为你的组织方式没有现成的标签,就以为做不到。加一个自己的标签,机制一模一样。

Which means the meaning of a query is yours to define. The tags we ship are a vocabulary of common words, not a rulebook: do not assume units may only be organised by team because a tag is called team, and do not assume your own scheme is unsupported because there is no tag for it. Add one, and the mechanism is the same.

一句话 In One Line

值住在片段里,可查性住在标签里,两者我们每帧同步;框架内部一个标签都不依赖;标签叫什么、代表什么,由你定。这一层理清之后,剩下的就是设计你自己的词表。查询真正灵活的地方在那儿。

The value lives in the fragment, queryability lives in the tag, and we sync the two every frame; the framework itself depends on none of those tags; what they are called and what they mean is up to you. Once that is clear, what is left is your own vocabulary. That is where a query gets flexible.

Leroy Works · Mass Battle Framework 作者
联系:Discord · QQ 群 916358710 · Fab · 全部联系方式

Leroy Works — author of Mass Battle Framework
Contact: Discord · QQ group 916358710 · Fab · all channels