@@ -104,7 +104,7 @@ class Terrain3DStorage : public Object {
104
104
TypedArray<Terrain3DRegion> get_regions_active (const bool p_copy = false , const bool p_deep = false ) const ;
105
105
Dictionary get_regions_all () const { return _regions; }
106
106
PackedInt32Array get_region_map () const { return _region_map; }
107
- int get_region_map_index (const Vector2i &p_region_loc) const ;
107
+ static int get_region_map_index (const Vector2i &p_region_loc);
108
108
109
109
bool has_region (const Vector2i &p_region_loc) const { return get_region_id (p_region_loc) != -1 ; }
110
110
bool has_regionp (const Vector3 &p_global_position) const { return get_region_idp (p_global_position) != -1 ; }
@@ -197,16 +197,18 @@ VARIANT_ENUM_CAST(Terrain3DStorage::HeightFilter);
197
197
198
198
// / Inline Region Functions
199
199
200
- // This function verifies the location is within the bounds of the _region_map array and
201
- // thus the world. It returns the _region_map index if valid, -1 if not
202
- inline int Terrain3DStorage::get_region_map_index (const Vector2i &p_region_loc) const {
203
- // Offset locations centered on (0,0) to positive only
204
- Vector2i loc = Vector2i (p_region_loc + (REGION_MAP_VSIZE / 2 ));
205
- int map_index = loc.y * REGION_MAP_SIZE + loc.x ;
206
- if (map_index < 0 || map_index >= REGION_MAP_SIZE * REGION_MAP_SIZE) {
200
+ // Verifies the location is within the bounds of the _region_map array and
201
+ // the world, returning the _region_map index, which contains the region_id.
202
+ // Valid region locations are -8, -8 to 7, 7, or when offset: 0, 0 to 15, 15
203
+ // If any bits other than 0xF are set, it's out of bounds and returns -1
204
+ inline int Terrain3DStorage::get_region_map_index (const Vector2i &p_region_loc) {
205
+ // Offset world to positive values only
206
+ Vector2i loc = p_region_loc + (REGION_MAP_VSIZE / 2 );
207
+ // Catch values > 15
208
+ if ((uint32_t (loc.x | loc.y ) & uint32_t (~0xF )) > 0 ) {
207
209
return -1 ;
208
210
}
209
- return map_index ;
211
+ return loc. y * REGION_MAP_SIZE + loc. x ;
210
212
}
211
213
212
214
// Returns a region location given a global position. No bounds checking nor data access.
0 commit comments