戴森球计划 地基蓝图系统解析

算是前篇unity逆向01:c#代码篇的一个实战例子吧。

从代码层面上看,蓝图的序列化和逆序列化在类BlueprintData中实现,不涉及具体生成逻辑。而数据的提供方为BuildTool_BlueprintCopy,于是我们要啃的就是其中的生成逻辑RefreshBlueprintData:

1. 蓝图文件序列化/逆序列化#

class BlueprintData解析,序列化调用的接口为:LoadBlueprintData(string _fileName)

文件格式为:

1
2
3
4
5
${header}"${content}"${checksum}

header: 蓝图头
content: 蓝图内容(base64编码)
checksum: 蓝图md5签名

1.1 header#

1
2
3
4
5
6
7
8
9
10
11
12
13
固定值 BLUEPRINT:${version},${layout},${icon0},${icon1},${icon2},${icon3},${icon4},0,${ticks},${game_version},${short_desc},${author},${custom_version},${external_fields},${desc}

#0: 蓝图版本号,最新为1,旧版本为0(少了#12及后续字段)
#1 layout: 图标布局 (枚举详情参考enum EIconLayout)
#2~6 icon0 ~ icon4: 具体的图标id (最多5个)
#7: 固定为0,用途未知
#8: 创建时间(.net ticks)
#9: 创建游戏版本
#10: 蓝图短描述(蓝图图标下方文字)
#11: 蓝图作者
#12: 蓝图版本
#13: 蓝图属性(${key}:${value};字符串)
#14: 蓝图描述

对应接口:HeaderFromBase64String(string contentStr)

1.2 content#

base64 decode+gzip decompress之后就是跟存档一样的二进制数据,用 蓝图文件格式 解析即可,跟存档其实是类似的

对应接口:ContentFromBase64String(string strData)

1.3 checksum#

对应接口:CheckSignature(string _str64Data)

用 奇怪的md5算法 校验数据段(第2个双引号前,即${header}"${content})的Md5是否与后面的${checksum}一致。因为戴森球用的是自己手搓的md5(用的不是常规初始值的),直接用python调hashlib库算出来的还对不上,不要问为什么,对应的代码见buggy_md5.py。

想当年没看dsp代码的时候,本着随便碰点运气的念头,结果吃了大憋:试遍了${header}、"以及${content}的各种排列组合,都没得到预期的md5。最后才低下头去翻dsp的MD5F是怎么写的

1.4 读取流程#

DSP读取蓝图步骤基本上就是把前面几步串起来:

  1. 解析header
  2. 验证内容checksum
  3. 解析content

2. 蓝图生成流程#

2.1 游戏的经纬度&球面坐标转换#

在谈代码之前,先要弄懂数学问题:经纬度跟三维球面坐标是怎么转换的。在翻阅代码时,看到了一个BlueprintUtils的类,里面有两个这样的方法。做下简单的阅读理解:

于是很快就能得出以下结论:纬度为y轴,N为正方向。经度为xz平面,0度为z=-1,180度为z=+1,东为x轴正方向:

没错,不是右手系,但也不是不能用。

2.2 RefreshBlueprintData#

一共2步,一个是GenerateGratBox,另一个是GenerateBlueprintData

2.2.1 GenerateGratBox#

跟纬度相关的区域划分代码,从上往下一个个看

2.2.1.1 DetermineDivideLine#

第一部分,跟_building_divideline_marks相关的代码:

从第1节可知,整个球面赤道地区有200个大格(每个大格包含5小格),下标为0=180° W到下标1000=180° E。在赤道地区,如果某个经度格子里有建筑被选中,则对应下标位置标为true。其中num2就是选中建筑的纬度(rad),num3则为经度(rad)。

举例来说,如果你在经纬度都为0°的地方,树了根电线杆,加到蓝图里,那么就会有_building_divideline_marks[500] = true这么个结果。

而其他非赤道地区(过了一条回归线,绕球一圈不足1000格的纬度),调BlueprintUtils.GetLongitudeSegmentCount时,会返回一个小于200的值,导致num4不为0。体现在后面的for循环里,原本只会占1格的,现在除了自己那格外,相邻的num4格也会被设置为true,简单来说就是更占地方了。

翻译过来就是这个意思:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
foreach (int objId in this.selectedObjIds) // 在蓝图选中的object id(建筑带子啥的)
{
if (base.GetPrefabDesc(objId) != null)
{
Vector3 normalized = base.GetObjectPose(objId).position.normalized;
// objId的单位坐标,position对应 factory(class PlanetFactory).entityPool(class EntityData)[objId].pos(struct Vector3)
// 对应的存档数据路径为root.game_data.factories[i].entity_pool[objId].pos
float latRad = Mathf.Asin(normalized.y); // y -> 纬度轴(rad),N正S负 取值[-π/2, +π/2]
float lonRad = Mathf.Atan2(normalized.x, -normalized.z); // x,z -> 经度轴(rad),0°=(x=0, z=-1),±180°(±π)=(x=0,z=1),90°E(+π/2)=(x=1,z=0),90°W(-π/2)=(x=-1,z=0) 取值[-π, π]
int longitudeSegmentCount = BlueprintUtils.GetLongitudeSegmentCount(latRad, this.segment); // 绕该纬度一圈的大格子数,从赤道到两极递减:200->160->120->100->80->60->40->32->20->16->8->4

// 高纬度时,每个格子的经度宽度相比于赤道是会略微放大的,在那里1格宽相当于赤道的X格宽(向上取整)。比如第一条纬度线往南/北后,经度格子数就从1000格变成800格,在这个区域放1格相当于赤道的1.25格,这里会向上取整按2格宽设置对应的mark
int collidedBlocksNum = Mathf.CeilToInt((float)(Mathf.CeilToInt((float)this.segment / (float)longitudeSegmentCount) - 1) * 0.5f);
// 这里的下标是从西半球→东半球递增的,分1000格,0°下标刚好在中间500
int lonBlockIdx = (int)Math.Round((double)(lonRad + 3.1415927f) / 0.0062831854820251465);
for (int i = -collidedBlocksNum; i <= collidedBlocksNum; i++)
{
int idx = (int)Mathf.Repeat((float)(lonBlockIdx + i), 1000f);
this._building_divideline_marks[idx] = true;
}
}
}

不过看了下,GetLongitudeSegmentCount里面的逻辑。这段代码对南北半球的坐标做了差异,可以间接推断出以整数$i$作为纬度格子索引时,对应的纬度范围是 $[(i-1)/\pi*250, i/\pi*250]$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// class BlueprintUtils
// Token: 0x06001561 RID: 5473 RVA: 0x00174008 File Offset: 0x00172208
public static int GetLongitudeSegmentCount(float _latitudeRad, int _segmentCnt = 200)
{
int latIdx = BlueprintUtils.GetSnappedLatitudeGridIdx(_latitudeRad, 200); // 格子下标,范围[-250, 250],N正S负
if (latIdx < 0) // 南半球纬度(<0)
{
latIdx = -latIdx;
}
if (latIdx > 0) // 北半球纬度(>0)
{
latIdx--;
}
// 从南北半球的处理差异来看,latIdx应该只有(-250, 250],latIdx号格子的纬度范围应该是[(latIdx-1)/π*250, latIdx/π*250],比如0号格子实际上的纬度范围是[-π/250 (rad) S, 0 (rad)]
// 然后获取latIdx靠南端的那个纬度,绕经度一圈的大格子数(segment),实际上每个大格子里面还有5个小格子
return PlanetGrid.DetermineLongitudeSegmentCount(latIdx / 5, _segmentCnt);
}

然后就是第二部分,_divideline_marks相关的地方,跟上面类似。

this.equatorWidth是常量1000,this.selectedReformIds是一个uint的flag,索引方式就是上面for循环里的那个if。相当于0-999存赤道以南第一格的地基,1000-1999存赤道以北第一格(注意上面的说明,0号格是赤道~赤道以南的),依此类推,存完北半球才接着从赤道往南存南半球的数据。按LittleEndian的方式差不多就长这样:

同样的,对于非赤道地区,会按比例四舍五入缩放到对应的格子,设置为true。唯一区别是这个只会设置单格,不会跟建筑一样在for循环里设置一片。

翻译过来就是这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int halfEquatorWidth = this.equatorWidth / 2; // =500
int quadEquatorWidth = halfEquatorWidth / 2; // =250
for (int j = 0; j < halfEquatorWidth; j++)
{
// DetermineLongitudeSegmentCount的调用上面解释过了,调用参数和返回值随j的变化为:
// latitudeIndex: [0/5, 1/5, ..., 249/5, 250/5, -1/5, -2/5 ..., -249/5]
// segment: 200
// returns: [200 x5 -> ... -> 4 x5 -> ... -> 200 x5]
int longitudeSegmentCount = PlanetGrid.DetermineLongitudeSegmentCount(((j > quadEquatorWidth) ? (quadEquatorWidth - j) : j) / 5, this.segment) * 5;
int halfLongitudeSegmentCount = longitudeSegmentCount / 2;

// 从下面代码能看出selectedReformIds的存储和索引方式
int longitudeOffsetBegin = j * this.equatorWidth; // 当前的一圈纬度方块对应selectedReformIds的起始偏移量
int longitudeOffsetEnd = longitudeOffsetBegin + longitudeSegmentCount - 1; // 当前的一圈纬度方块对应selectedReformIds的结束偏移量
for (int k = longitudeOffsetBegin; k <= longitudeOffsetEnd; k++)
{
if ((this.selectedReformIds[k >> 5] & (1U << k)) > 0U)
{
int longitudeIndex = k - longitudeOffsetBegin;
// 跟建筑不一样的是,地基的marker只会设置到离地基最近的一个mark位置,不会跟建筑那样先scale再向上取整设置多个
int idx = Mathf.RoundToInt((float)(((longitudeIndex >= halfLongitudeSegmentCount) ? (halfLongitudeSegmentCount - longitudeIndex) : longitudeIndex) + halfLongitudeSegmentCount) * (float)this.equatorWidth / (float)longitudeSegmentCount);
idx = ((idx >= 1001) ? (idx - 1001 - 1) : idx); // 越界保护
this._divideline_marks[idx] = true;
}
}
}

第三部分的flag判断:简单来说,就是判断是否是全部格子都设置成true这种全局蓝图,是的话走里面的if (flag)分支,把上面地基设置的_divideline_marks覆盖掉,只用回建筑的,不知道具体对应哪种用途。


最后是一段比较抽象的代码,没有变量名看的还是挺折腾的。简单来说,这地方是用来判断是不跨越180°(包括经线在180° W/E)的蓝图,对应的是最后的if (num21 > num19)的两种情况。

简单来说下各个num的作用:

  • num15:mark为true的最小下标
  • num20:mark当前的true离上一个true的下标差
  • num19:mark中两true间隔的最大下标差
  • num18:mark中两true间隔最大时,首个true的下标(靠前的那个)
  • num16/num17:mark为true的最大下标

代码翻译一下就是这样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int minMarkIdx = -1; // mark为true的最小下标
int maxMarkIdx = -1; // mark为true的最大下标
int maxMarkIdxFlag = -1; // 作用跟上面是一样的,不知道为什么多搞一个变量作区分
int maxDistFirstIdx = -1; // mark中两true间隔最大时,首个true的下标(靠前的那个)
int maxDist = 0; // mark当前的true离上一个true的下标差
for (int i = 0; i < 1001; i++)
{
if (this._divideline_marks[i])
{
if (maxMarkIdxFlag == -1)
{
maxMarkIdxFlag = i;
minMarkIdx = i;
maxMarkIdx = i;
}
else
{
int curDist = i - maxMarkIdxFlag;
if (curDist > maxDist)
{
maxDistFirstIdx = maxMarkIdxFlag;
maxDist = curDist;
}
maxMarkIdxFlag = i;
maxMarkIdx = i;
}
}
}

光说其实没用,还是画个图好点:左边是蓝图不跨越180°线的场景(即mark在[0,1000]区间是连续的),而右边是跨越180°线的场景(对应的mark会

最后的divideLineRad就是把num22和num23从[0,1000]按比例缩放到[-π, π]而已。而这个值的几何意义为:该经度线到蓝图最左边的端点和最右边的端点等长。

2.2.1.2 GetBoundingRange#

又臭又长的代码,要晕了。

调用参数为BlueprintUtils.GetBoundingRange(this.planet, base.actionBuild.planetAux, this._tmp_selected_ids, this._tmp_selected_cnt, this.divideLineRad, this.selectedReformIds);

整体看下来就是算出一个蓝图最小容纳box的经纬度,经度范围是[-π, π],纬度范围是[-π/2, π/2]。

详细一点就是:

  1. 首先遍历蓝图选中的物体,用物体坐标做最值运算,转换为以经度分割线为基准的蓝图经纬度(原本在行星上的标准经纬度坐标系还是称为经纬度。跟标准经纬度相比,蓝图经度没有东西半球概念,只有0°到360°,也就是0到2π)
  2. 然后再遍历蓝图选中的地基,同样做最值运算
  3. 最后将蓝图经纬度重新转回标准经纬度

根据用途替换下变量名就是这样了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
int segment = 200;
if (_planet.aux != null && _planet.aux.activeGrid != null)
{
segment = _planet.aux.activeGrid.segment;
}
float globalMinLonRad = float.MaxValue;
float globalMinLatRad = float.MaxValue;
float globalMaxLonRad = float.MinValue;
float globalMaxLatRad = float.MinValue;
bool isGlobalBP = true;
int blockCnt = PlanetGrid.DetermineLongitudeSegmentCount(0, segment) * 5; // 1000
for (int i = 0; i < _objCount; i++)
{
int objId = _objIds[i];
Vector3 vector = ((objId > 0) ? entityPool[objId].pos.normalized : prebuildPool[-objId].pos.normalized);
Vector3 normalized = _auxData.Snap(vector, true).normalized; // 跟上面区别在于,这里的坐标会通过Snap离散到最近的网格坐标
bool isPole = normalized.x * normalized.x + normalized.z * normalized.z < 5E-07f; // 判断是否为极点坐标,如北极(0, 1, 0)
float latRadOrigin = Mathf.Asin(Mathf.Clamp(vector.y, -0.99999f, 0.99999f));
float blockRad = 6.2831855f / (float)blockCnt; // π/500
float latRad = Mathf.Asin(normalized.y);
float lonRad = Mathf.Atan2(normalized.x, -normalized.z);
float maxLatRad;
float minLatRad = (maxLatRad = latRad);
if (latRadOrigin > latRad + 0.2f * blockRad) // 小格占用向上取整,占了0.2格=把这格都给占了
{
maxLatRad = latRad + blockRad;
}
else if (latRadOrigin < latRad - 0.2f * blockRad)
{
minLatRad = latRad - blockRad;
}
globalMinLatRad = ((globalMinLatRad < maxLatRad) ? globalMinLatRad : maxLatRad); // min
globalMaxLatRad = ((globalMaxLatRad > minLatRad) ? globalMaxLatRad : minLatRad); // max
if (!isPole)
{
isGlobalBP = false;
float bpLonRad = lonRad - _divideLongitude; // 对原本的经度做线性变换,以上面提到的经度分割线divideLongitude为0,右正左负
if (bpLonRad <= -1.8E-05f)
{
bpLonRad += 6.2831855f;
}
if (bpLonRad < 0f)
{
bpLonRad = 0f;
} // 经过2次操作后,蓝图经度为:以经度分割线往右开始线性递增,绕一圈取值[0, 2π] 小型蓝图的中央区域位于π附近
globalMinLonRad = ((globalMinLonRad < bpLonRad) ? globalMinLonRad : bpLonRad); // min
globalMaxLonRad = ((globalMaxLonRad > bpLonRad) ? globalMaxLonRad : bpLonRad); // max
}
}

if (_reformIds != null)
{
int halfBlockCnt = blockCnt / 2; // 500
int quadBlockCnt = halfBlockCnt / 2; // 250
float divideLonOffsetRatio = _divideLongitude / 6.2831855f; // [-0.5, 0.5]
divideLonOffsetRatio = ((divideLonOffsetRatio < 0f) ? (0.5f - divideLonOffsetRatio) : divideLonOffsetRatio); // [0, 1] >0取值(0, 0.5] <0取值(0.5, 1]
// 这里计算的是经度分割线在同纬度地基数组的位置比例,因为地基是按0->E再0->W的顺序存的……东半球地基按从左往右存到数组前半位置,而再到西半球地基按从右往左存到数组后半位置。之前的图里已经说明过这种存储顺序了
float blockRad = 6.2831855f / (float)blockCnt; // π/500
for (int j = 0; j < halfBlockCnt; j++)
{
// 跟之前类似的位置
int longitudeSegmentCount = PlanetGrid.DetermineLongitudeSegmentCount(((j >= quadBlockCnt) ? (quadBlockCnt - j) : j) / 5, num) * 5;
int halfLongitudeSegmentCount = longitudeSegmentCount / 2;
float longitudeSegmentRad = 6.2831855f / (float)longitudeSegmentCount;
int divideLonOffset = (int)(divideLonOffsetRatio * (float)longitudeSegmentCount);
if (divideLonOffset >= longitudeSegmentCount)
{
divideLonOffset = longitudeSegmentCount - 1;
}
int offset = j * blockCnt + divideLonOffset; // 参考之前的地基存储顺序图
if (((ulong)_reformIds[offset >> 5] & (ulong)(1L << (offset & 31 & 31))) > 0UL)
{
// 在经度分割线上有地基的情况,对应全球蓝图
// 没懂max为啥要减 min不减……
float reformLatRad = (float)((j >= quadBlockCnt) ? (quadBlockCnt - j) : (j + 1)) * blockRad;
globalMaxLatRad = ((globalMaxLatRad > reformLatRad - blockRad) ? globalMaxLatRad : (reformLatRad - blockRad)); // max
globalMinLatRad = ((globalMinLatRad < reformLatRad) ? globalMinLatRad : reformLatRad); // min
globalMinLonRad = 0f;
globalMaxLonRad = 6.2831855f;
}
// 下面这段代码都是去找地基的经度最值,先最小值再最大值
bool foundReform = false;
if (divideLonOffset >= halfLongitudeSegmentCount) // 经度分割线位于西半球
{
for (int k = divideLonOffset - 1; k >= halfLongitudeSegmentCount; k--) // 经度分割线~0° 从西向东扫地基数据
{
offset = j * blockCnt + k;
if (_reformIds[offset >> 5] == 0U) // 整个32bit都没地基,会快速跳过这4bytes数据,不再每个bit都在循环里检查一遍
{
k = (offset & -32) - j * blockCnt;
}
else if (((ulong)_reformIds[offset >> 5] & (ulong)(1L << (offset & 31 & 31))) > 0UL)
{
foundReform = true;
float reformLatRad2 = (float)((j >= quadBlockCnt) ? (quadBlockCnt - j) : (j + 1)) * blockRad;
globalMaxLatRad = ((globalMaxLatRad > reformLatRad2 - blockRad) ? globalMaxLatRad : (reformLatRad2 - blockRad));
globalMinLatRad = ((globalMinLatRad < reformLatRad2) ? globalMinLatRad : reformLatRad2);
// 转换成蓝图经度 同上
float reformLonRad = (float)(halfLongitudeSegmentCount - k - 1) * longitudeSegmentRad - _divideLongitude;
if (reformLonRad <= -1.8E-05f)
{
reformLonRad += 6.2831855f;
}
if (globalMinLonRad > reformLonRad)
{
globalMinLonRad = reformLonRad;
break;
}
break;
}
}
if (!foundReform) // 西半球找不到地基,去东半球找
{
for (int l = 0; l < halfLongitudeSegmentCount; l++) // 0°~180°E 从西向东扫 代码结构跟上面是类似的
{
offset = j * blockCnt + l;
if (_reformIds[offset >> 5] == 0U)
{
l = (offset | 31) - j * blockCnt;
}
else if (((ulong)_reformIds[offset >> 5] & (ulong)(1L << (offset & 31 & 31))) > 0UL)
{
foundReform = true;
float reformLatRad3 = (float)((j >= quadBlockCnt) ? (quadBlockCnt - j) : (j + 1)) * blockRad;
globalMaxLatRad = ((globalMaxLatRad > reformLatRad3 - blockRad) ? globalMaxLatRad : (reformLatRad3 - blockRad));
globalMinLatRad = ((globalMinLatRad < reformLatRad3) ? globalMinLatRad : reformLatRad3);
float reformLonRad2 = (float)l * longitudeSegmentRad - _divideLongitude;
if (reformLonRad2 <= -1.8E-05f)
{
reformLonRad2 += 6.2831855f;
}
if (globalMinLonRad > reformLonRad2)
{
globalMinLonRad = reformLonRad2;
break;
}
break;
}
}
}
if (!foundReform) // 还是找不到……再折返西半球
{
for (int m = longitudeSegmentCount - 1; m > divideLonOffset; m--) // 180°W~经度分割线 从西向东扫
{
offset = j * blockCnt + m;
if (_reformIds[offset >> 5] == 0U)
{
m = (offset & -32) - j * blockCnt;
}
else if (((ulong)_reformIds[offset >> 5] & (ulong)(1L << (offset & 31 & 31))) > 0UL)
{
foundReform = true;
float reformLatRad4 = (float)((j >= quadBlockCnt) ? (quadBlockCnt - j) : (j + 1)) * blockRad;
globalMaxLatRad = ((globalMaxLatRad > reformLatRad4 - blockRad) ? globalMaxLatRad : (reformLatRad4 - blockRad));
globalMinLatRad = ((globalMinLatRad < reformLatRad4) ? globalMinLatRad : reformLatRad4);
float reformLonRad3 = (float)(halfLongitudeSegmentCount - m - 1) * longitudeSegmentRad - _divideLongitude;
if (reformLonRad3 <= -1.8E-05f)
{
reformLonRad3 += 6.2831855f;
}
if (globalMinLonRad > reformLonRad3)
{
globalMinLonRad = reformLonRad3;
break;
}
break;
}
}
}
if (foundReform) // 找到地基的情况,再找最大值
{
isGlobalBP = false;
foundReform = false;
for (int n = divideLonOffset + 1; n < longitudeSegmentCount; n++) // 经度分割线~180°W 从东向西扫 (蓝图经度2π的位置开始往前扫)
{
offset = j * blockCnt + n;
if (_reformIds[offset >> 5] == 0U)
{
n = (offset | 31) - j * blockCnt;
}
else if (((ulong)_reformIds[offset >> 5] & (ulong)(1L << (offset & 31 & 31))) > 0UL)
{
foundReform = true;
float reformLonRad4 = (float)(halfLongitudeSegmentCount - n) * longitudeSegmentRad - _divideLongitude;
if (reformLonRad4 <= -1.8E-05f)
{
reformLonRad4 += 6.2831855f;
}
if (globalMaxLonRad < reformLonRad4)
{
globalMaxLonRad = reformLonRad4;
break;
}
break;
}
}
if (!foundReform) // 找不到 去东半球找
{
for (int o = halfLongitudeSegmentCount - 1; o >= 0; o--) // 180°E~0° 从东向西扫
{
offset = j * blockCnt + o;
if (_reformIds[offset >> 5] == 0U)
{
o = (offset & -32) - j * blockCnt;
}
else if (((ulong)_reformIds[offset >> 5] & (ulong)(1L << (offset & 31 & 31))) > 0UL)
{
float reformLonRad5 = (float)(o + 1) * longitudeSegmentRad - _divideLongitude;
if (reformLonRad5 <= -1.8E-05f)
{
reformLonRad5 += 6.2831855f;
}
if (globalMaxLonRad < reformLonRad5)
{
globalMaxLonRad = reformLonRad5;
break;
}
break;
}
}
}
if (!foundReform) // 找不到 再回去西半球找
{
for (int p = halfLongitudeSegmentCount; p < divideLonOffset; p++) // 0°~经度分割线 从东向西扫
{
offset = j * blockCnt + p;
if (_reformIds[offset >> 5] == 0U)
{
p = (offset | 31) - j * blockCnt;
}
else if (((ulong)_reformIds[offset >> 5] & (ulong)(1L << (offset & 31 & 31))) > 0UL)
{
float reformLonRad6 = (float)(halfLongitudeSegmentCount - p) * longitudeSegmentRad - _divideLongitude;
if (reformLonRad6 <= -1.8E-05f)
{
reformLonRad6 += 6.2831855f;
}
if (globalMaxLonRad < reformLonRad6)
{
globalMaxLonRad = reformLonRad6;
break;
}
break;
}
}
}
}
}
else // 经度分割线位于东半球,跟上面就是一个对称情况,原理是一样的就不详细讲了
{
for (int num36 = divideLonOffset + 1; num36 < halfLongitudeSegmentCount; num36++)
{
offset = j * blockCnt + num36;
if (_reformIds[offset >> 5] == 0U)
{
num36 = (offset | 31) - j * blockCnt;
}
else if (((ulong)_reformIds[offset >> 5] & (ulong)(1L << (offset & 31 & 31))) > 0UL)
{
foundReform = true;
float num37 = (float)((j >= quadBlockCnt) ? (quadBlockCnt - j) : (j + 1)) * blockRad;
globalMaxLatRad = ((globalMaxLatRad > num37 - blockRad) ? globalMaxLatRad : (num37 - blockRad));
globalMinLatRad = ((globalMinLatRad < num37) ? globalMinLatRad : num37);
float num38 = (float)num36 * longitudeSegmentRad - _divideLongitude;
if (num38 <= -1.8E-05f)
{
num38 += 6.2831855f;
}
if (globalMinLonRad > num38)
{
globalMinLonRad = num38;
break;
}
break;
}
}
if (!foundReform)
{
for (int num39 = longitudeSegmentCount - 1; num39 >= halfLongitudeSegmentCount; num39--)
{
offset = j * blockCnt + num39;
if (_reformIds[offset >> 5] == 0U)
{
num39 = (offset & -32) - j * blockCnt;
}
else if (((ulong)_reformIds[offset >> 5] & (ulong)(1L << (offset & 31 & 31))) > 0UL)
{
foundReform = true;
float num40 = (float)((j >= quadBlockCnt) ? (quadBlockCnt - j) : (j + 1)) * blockRad;
globalMaxLatRad = ((globalMaxLatRad > num40 - blockRad) ? globalMaxLatRad : (num40 - blockRad));
globalMinLatRad = ((globalMinLatRad < num40) ? globalMinLatRad : num40);
float num41 = (float)(halfLongitudeSegmentCount - num39 - 1) * longitudeSegmentRad - _divideLongitude;
if (num41 <= -1.8E-05f)
{
num41 += 6.2831855f;
}
if (globalMinLonRad > num41)
{
globalMinLonRad = num41;
break;
}
break;
}
}
}
if (!foundReform)
{
for (int num42 = 0; num42 < divideLonOffset; num42++)
{
offset = j * blockCnt + num42;
if (_reformIds[offset >> 5] == 0U)
{
num42 = (offset | 31) - j * blockCnt;
}
else if (((ulong)_reformIds[offset >> 5] & (ulong)(1L << (offset & 31 & 31))) > 0UL)
{
foundReform = true;
float num43 = (float)((j >= quadBlockCnt) ? (quadBlockCnt - j) : (j + 1)) * blockRad;
globalMaxLatRad = ((globalMaxLatRad > num43 - blockRad) ? globalMaxLatRad : (num43 - blockRad));
globalMinLatRad = ((globalMinLatRad < num43) ? globalMinLatRad : num43);
float num44 = (float)num42 * longitudeSegmentRad - _divideLongitude;
if (num44 <= -1.8E-05f)
{
num44 += 6.2831855f;
}
if (globalMinLonRad > num44)
{
globalMinLonRad = num44;
break;
}
break;
}
}
}
if (foundReform)
{
isGlobalBP = false;
foundReform = false;
for (int num45 = divideLonOffset - 1; num45 >= 0; num45--)
{
offset = j * blockCnt + num45;
if (_reformIds[offset >> 5] == 0U)
{
num45 = (offset & -32) - j * blockCnt;
}
else if (((ulong)_reformIds[offset >> 5] & (ulong)(1L << (offset & 31 & 31))) > 0UL)
{
foundReform = true;
float num46 = (float)(num45 + 1) * longitudeSegmentRad - _divideLongitude;
if (num46 <= -1.8E-05f)
{
num46 += 6.2831855f;
}
if (globalMaxLonRad < num46)
{
globalMaxLonRad = num46;
break;
}
break;
}
}
if (!foundReform)
{
for (int num47 = halfLongitudeSegmentCount; num47 < longitudeSegmentCount; num47++)
{
offset = j * blockCnt + num47;
if (_reformIds[offset >> 5] == 0U)
{
num47 = (offset | 31) - j * blockCnt;
}
else if (((ulong)_reformIds[offset >> 5] & (ulong)(1L << (offset & 31 & 31))) > 0UL)
{
float num48 = (float)(halfLongitudeSegmentCount - num47) * longitudeSegmentRad - _divideLongitude;
if (num48 <= -1.8E-05f)
{
num48 += 6.2831855f;
}
if (globalMaxLonRad < num48)
{
globalMaxLonRad = num48;
break;
}
break;
}
}
}
if (!foundReform)
{
for (int num49 = halfLongitudeSegmentCount - 1; num49 > divideLonOffset; num49--)
{
offset = j * blockCnt + num49;
if (_reformIds[offset >> 5] == 0U)
{
num49 = (offset & -32) - j * blockCnt;
}
else if (((ulong)_reformIds[offset >> 5] & (ulong)(1L << (offset & 31 & 31))) > 0UL)
{
float num50 = (float)(num49 + 1) * longitudeSegmentRad - _divideLongitude;
if (num50 <= -1.8E-05f)
{
num50 += 6.2831855f;
}
if (globalMaxLonRad < num50)
{
globalMaxLonRad = num50;
break;
}
break;
}
}
}
}
}
}
if (globalMinLatRad > globalMaxLatRad)
{
if (globalMinLatRad < 0f)
{
globalMinLatRad = globalMaxLatRad;
}
else
{
globalMaxLatRad = globalMinLatRad;
}
}
}
// 限制蓝图经度最值在[0, 2π]区间
if (globalMinLonRad < 0f)
{
globalMinLonRad = 0f;
}
else if (globalMinLonRad > 6.2831674f)
{
globalMinLonRad = 6.2831674f;
}
if (globalMaxLonRad < 0f)
{
globalMaxLonRad = 0f;
}
else if (globalMaxLonRad > 6.2831674f)
{
globalMaxLonRad = 6.2831674f;
}
if (isGlobalBP)
{
globalMinLonRad = 0f;
globalMaxLonRad = 6.2831674f;
}
// 最后再转成全局(行星上的)经度,区间为[-π, π]
globalMinLonRad += _divideLongitude;
globalMaxLonRad += _divideLongitude;
if (globalMinLonRad < -3.1415927f)
{
globalMinLonRad += 6.2831855f;
}
else if (globalMinLonRad > 3.1415927f)
{
globalMinLonRad -= 6.2831855f;
}
if (globalMaxLonRad < -3.1415927f)
{
globalMaxLonRad += 6.2831855f;
}
else if (globalMaxLonRad > 3.1415927f)
{
globalMaxLonRad -= 6.2831855f;
}
return new BPGratBox(globalMinLonRad, globalMinLatRad, globalMaxLonRad, globalMaxLatRad);

虽然变量名多了点确实有点恐怖,但静下心来看的话还是不算难理解的。

2.2.1.3 GetLongitudeRadPerGrid#

算的是蓝图box最小纬度(上面的globalMinLatRad)的小格的经度宽,赤道附近是π/500,其他也没啥好说的

2.2.1.4 GetLatitudeRadPerGrid#

算的是小格的纬度宽,固定是π/500

2.2.1.5 GetExtendedGratBox#

将蓝图box的四周拓展1~3格宽,蓝图范围越小拓展格数越多(3格),格子当前蓝图中的按最小格宽算(主要是取越接近赤道那边的格宽)

调用的代码也没啥好说的,比较简单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Token: 0x06001571 RID: 5489 RVA: 0x00174360 File Offset: 0x00172560
public static BPGratBox GetExtendedGratBox(BPGratBox gratbox, float extend_lng_grid = 0.5f, float extend_lat_grid = 0.5f)
{
float num; // 小格经度宽,横跨多区时按格子最多的那区算宽度(越接近赤道的区域)
if (gratbox.y * gratbox.w > 0f && gratbox.y > 0f) // 蓝图范围在北半球
{
num = BlueprintUtils.GetLongitudeRadPerGrid(gratbox.y, 200);
}
else if (gratbox.y * gratbox.w > 0f && gratbox.y < 0f) // 蓝图范围在南半球
{
num = BlueprintUtils.GetLongitudeRadPerGrid(gratbox.w, 200);
}
else // 蓝图横跨赤道
{
num = BlueprintUtils.GetLongitudeRadPerGrid(0f, 200);
}
gratbox.Extend(num * extend_lng_grid, BlueprintUtils.GetLatitudeRadPerGrid(200) * extend_lat_grid);
return gratbox;
}

// Token: 0x0600159F RID: 5535 RVA: 0x0017A7C0 File Offset: 0x001789C0
public void Extend(float longitude, float latitude)
{
float minLonRad = this.x;
float maxLonRad = this.z;
if (maxLonRad <= minLonRad + 1E-05f)
{
if (maxLonRad < minLonRad - 1E-05f)
{
maxLonRad += 6.2831855f;
}
else
{
maxLonRad = minLonRad;
}
}
minLonRad -= longitude;
maxLonRad += longitude;
float lonWidth = maxLonRad - minLonRad;
if (lonWidth < 0f)
{
maxLonRad = (minLonRad = (minLonRad + maxLonRad) / 2f);
}
else if (lonWidth > 6.2831693f)
{
float num4 = (lonWidth - 6.2831855f) / 2f;
minLonRad += num4;
maxLonRad = minLonRad;
minLonRad += 7E-06f;
maxLonRad -= 7E-06f;
}
if (minLonRad < -3.1415927f)
{
minLonRad += 6.2831855f;
}
else if (minLonRad >= 3.1415927f)
{
minLonRad -= 6.2831855f;
}
if (maxLonRad < -3.1415927f)
{
maxLonRad += 6.2831855f;
}
else if (maxLonRad >= 3.1415927f)
{
maxLonRad -= 6.2831855f;
}
this.x = minLonRad;
this.z = maxLonRad;
this.y -= latitude;
this.w += latitude;
}

2.2.1.6 SplitGratBoxInTropicAreas#

整个代码的作用就是把前面计算得出的一整个蓝图box,按纬度回归线(黄线)拆分成若干个区域,存到this.tropicGratBoxRadRangeArr里,而对应的数组数量则放到this.curActiveAreaGratBoxCursor里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Token: 0x0600157E RID: 5502 RVA: 0x0017522C File Offset: 0x0017342C
public static int SplitGratBoxInTropicAreas(BPGratBox _gratbox, BPGratBox[] _areaBoxes, BPGratBox[] _areaBoxesEx, int _segmentCnt = 200)
{
float minLonRad = _gratbox.x;
float minLatRad = _gratbox.y;
float maxLonRad = _gratbox.z;
float maxLatRad = _gratbox.w;
if (maxLonRad < minLonRad - 1E-05f)
{
maxLonRad += 6.2831855f;
}
int maxLatGridIdx = BlueprintUtils.GetSnappedLatitudeGridIdx(maxLatRad, _segmentCnt); // 格子下标,范围[-250, 250]
int minLatGridIdx = BlueprintUtils.GetSnappedLatitudeGridIdx(minLatRad, _segmentCnt);
float latitudeRadPerGrid = BlueprintUtils.GetLatitudeRadPerGrid(_segmentCnt); // π/500
int areaBoxIdx = 0;
int lastLonUpdateAreaBoxIdx = -1; // 没更新当前区块经度时,取值为areaBoxIdx-1,更新后设置成areaBoxIdx
float nextLatRad = _gratbox.y;
_areaBoxes[areaBoxIdx] = BPGratBox.zero;
_areaBoxes[areaBoxIdx].y = (_areaBoxes[areaBoxIdx].w = nextLatRad); // {.x=0, .y=_gratbox.y, .z=0, .w=_gratbox.y}
int lonGridCnt = BlueprintUtils.GetLongitudeSegmentCount(minLatGridIdx, _segmentCnt); // 蓝图box最南端的经度格数(大格,小格要*5)
for (int i = minLatGridIdx; i <= maxLatGridIdx; i++)
{
int nextLatGridIdx = ((i + 1 > maxLatGridIdx) ? maxLatGridIdx : (i + 1)); // min(i+1, maxLatGridIdx) 下一格(靠北端)的格子下标
nextLatRad = (float)nextLatGridIdx * latitudeRadPerGrid;
int curGridCnt = lonGridCnt;
int nextLonGridCnt = BlueprintUtils.GetLongitudeSegmentCount(nextLatGridIdx, _segmentCnt);
lonGridCnt = nextLonGridCnt;
bool isGridCntChanged = curGridCnt != nextLonGridCnt;
if (lastLonUpdateAreaBoxIdx < areaBoxIdx)
{
// 对当前区域蓝图box做经度离散化,舍入到最近大格的经度,写入当前区域的最小/最大经度
float longitudeRadPerGrid = BlueprintUtils.GetLongitudeRadPerGrid(curGridCnt, _segmentCnt);
float minGridIdx = minLonRad / longitudeRadPerGrid;
float maxGridIdx = maxLonRad / longitudeRadPerGrid;
float tmpMinGridIdx = BlueprintUtils._round(minGridIdx);
maxGridIdx = BlueprintUtils._round(maxGridIdx);
float minGridLonRad = tmpMinGridIdx * longitudeRadPerGrid;
float maxGridLonRad = maxGridIdx * longitudeRadPerGrid;
if (maxGridLonRad > minGridLonRad + 6.2831855f - 1E-05f - 4E-06f)
{
maxGridLonRad = minGridLonRad + 6.2831855f - longitudeRadPerGrid;
}
if (minGridLonRad < -3.1415927f)
{
minGridLonRad += 6.2831855f;
}
if (minGridLonRad > 3.1415927f)
{
minGridLonRad -= 6.2831855f;
}
if (maxGridLonRad < -3.1415927f)
{
maxGridLonRad += 6.2831855f;
}
if (maxGridLonRad > 3.1415927f)
{
maxGridLonRad -= 6.2831855f;
}
_areaBoxes[areaBoxIdx].x = minGridLonRad;
_areaBoxes[areaBoxIdx].z = maxGridLonRad;
lastLonUpdateAreaBoxIdx = areaBoxIdx;
}
if (isGridCntChanged)
{
areaBoxIdx++;
_areaBoxes[areaBoxIdx] = BPGratBox.zero;
_areaBoxes[areaBoxIdx].minLatRad = (_areaBoxes[areaBoxIdx].maxLatRad = nextLatRad);
}
else
{
_areaBoxes[areaBoxIdx].maxLatRad = nextLatRad;
}
}
int areaBoxCnt = areaBoxIdx + 1;
for (int j = 0; j < areaBoxCnt; j++)
{
_areaBoxesEx[j] = BlueprintUtils.GetExtendedGratBox(_areaBoxes[j], 0.5f);
}
return areaBoxCnt;
}

2.2.2 GenerateBlueprintData#

这里直接调用BlueprintUtils.GenerateBlueprintData,跟着往下走:

看着算蓝图关键区域的代码就是GenerateBlueprintData这个调用了,首先进去看一下内容