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
|
using System;
using System.IO;
using UnityEngine;
namespace XUtliPoolLib
{
public sealed class XGrid
{
public int Width;
public int Height;
public float XMin;
public float ZMin;
public float XMax;
public float ZMax;
public float Side;
public int[] IdxData = null;
public float[] ValueData = null;
public bool LoadFile(string FileName)
{
XBinaryReader xbinaryReader = XSingleton<XResourceLoaderMgr>.singleton.ReadBinary(FileName, ".bytes", false, true);
try
{
this.Height = xbinaryReader.ReadInt32();
this.Width = xbinaryReader.ReadInt32();
this.XMin = xbinaryReader.ReadSingle();
this.ZMin = xbinaryReader.ReadSingle();
this.XMax = xbinaryReader.ReadSingle();
this.ZMax = xbinaryReader.ReadSingle();
this.Side = xbinaryReader.ReadSingle();
int num = xbinaryReader.ReadInt32();
this.IdxData = new int[num];
for (int i = 0; i < num; i++)
{
this.IdxData[i] = xbinaryReader.ReadInt32();
}
this.ValueData = new float[num];
for (int j = 0; j < num; j++)
{
short num2 = xbinaryReader.ReadInt16();
bool flag = num2 < 0;
if (flag)
{
this.ValueData[j] = -100f;
bool flag2 = j + 1 < num;
if (flag2)
{
bool flag3 = num2 == short.MinValue;
if (flag3)
{
num2 = 0;
}
else
{
num2 = (short) -num2;
}
this.ValueData[j + 1] = (float)num2;
j++;
}
}
else
{
this.ValueData[j] = (float)num2;
}
}
}
catch (EndOfStreamException ex)
{
XSingleton<XDebug>.singleton.AddErrorLog("read file ", FileName, " not complete :", ex.Message, null, null);
return false;
}
finally
{
XSingleton<XResourceLoaderMgr>.singleton.ClearBinary(xbinaryReader, false);
}
return true;
}
public bool TryGetHeight(Vector3 pos, out float y)
{
y = 0f;
int num = (int)((pos.z - this.ZMin) / this.Side);
int num2 = (int)((pos.x - this.XMin) / this.Side);
int key = num * this.Width + num2;
int index = this.GetIndex(key);
bool flag = index >= 0 && index < this.ValueData.Length;
bool result;
if (flag)
{
y = this.ValueData[index] / 100f;
result = true;
}
else
{
result = false;
}
return result;
}
public float GetHeight(Vector3 pos)
{
int num = (int)((pos.z - this.ZMin) / this.Side);
int num2 = (int)((pos.x - this.XMin) / this.Side);
int key = num * this.Width + num2;
int index = this.GetIndex(key);
bool flag = index < 0 || index >= this.ValueData.Length;
float result;
if (flag)
{
XSingleton<XDebug>.singleton.AddErrorLog2("error grid index:{0} pos:{1}", new object[]
{
index,
pos
});
result = -1f;
}
else
{
result = this.ValueData[index] / 100f;
}
return result;
}
private int GetIndex(int key)
{
int num = this.IdxData.Length;
int i = 0;
int num2 = num - 1;
while (i <= num2)
{
int num3 = i + num2 >> 1;
int num4 = this.IdxData[num3];
bool flag = key == num4;
if (flag)
{
return num3;
}
bool flag2 = key < num4;
if (flag2)
{
num2 = num3 - 1;
}
else
{
bool flag3 = key > num4;
if (flag3)
{
i = num3 + 1;
}
}
}
return num2;
}
public void Update()
{
}
}
}
|