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
|
/*************************************************************************/
/* grid_map.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef GRID_MAP_H
#define GRID_MAP_H
#include "scene/3d/node_3d.h"
#include "scene/resources/mesh_library.h"
#include "scene/resources/multimesh.h"
//heh heh, godotsphir!! this shares no code and the design is completely different with previous projects i've done..
//should scale better with hardware that supports instancing
class PhysicsMaterial;
class GridMap : public Node3D {
GDCLASS(GridMap, Node3D);
enum {
MAP_DIRTY_TRANSFORMS = 1,
MAP_DIRTY_INSTANCES = 2,
};
union IndexKey {
struct {
int16_t x;
int16_t y;
int16_t z;
};
uint64_t key = 0;
static uint32_t hash(const IndexKey &p_key) {
return hash_one_uint64(p_key.key);
}
_FORCE_INLINE_ bool operator<(const IndexKey &p_key) const {
return key < p_key.key;
}
_FORCE_INLINE_ bool operator==(const IndexKey &p_key) const {
return key == p_key.key;
}
_FORCE_INLINE_ operator Vector3i() const {
return Vector3i(x, y, z);
}
IndexKey(Vector3i p_vector) {
x = (int16_t)p_vector.x;
y = (int16_t)p_vector.y;
z = (int16_t)p_vector.z;
}
IndexKey() {}
};
/**
* @brief A Cell is a single cell in the cube map space; it is defined by its coordinates and the populating Item, identified by int id.
*/
union Cell {
struct {
unsigned int item : 16;
unsigned int rot : 5;
unsigned int layer : 8;
};
uint32_t cell = 0;
};
/**
* @brief An Octant is a prism containing Cells, and possibly belonging to an Area.
* A GridMap can have multiple Octants.
*/
struct Octant {
struct NavMesh {
RID region;
Transform3D xform;
RID navmesh_debug_instance;
};
struct MultimeshInstance {
RID instance;
RID multimesh;
struct Item {
int index = 0;
Transform3D transform;
IndexKey key;
};
Vector<Item> items; //tools only, for changing visibility
};
Vector<MultimeshInstance> multimesh_instances;
HashSet<IndexKey> cells;
RID collision_debug;
RID collision_debug_instance;
bool dirty = false;
RID static_body;
HashMap<IndexKey, NavMesh> navmesh_ids;
};
union OctantKey {
struct {
int16_t x;
int16_t y;
int16_t z;
int16_t empty;
};
uint64_t key = 0;
static uint32_t hash(const OctantKey &p_key) {
return hash_one_uint64(p_key.key);
}
_FORCE_INLINE_ bool operator==(const OctantKey &p_key) const {
return key == p_key.key;
}
//OctantKey(const IndexKey& p_k, int p_item) { indexkey=p_k.key; item=p_item; }
OctantKey() {}
};
uint32_t collision_layer = 1;
uint32_t collision_mask = 1;
Ref<PhysicsMaterial> physics_material;
bool bake_navigation = false;
uint32_t navigation_layers = 1;
Transform3D last_transform;
bool _in_tree = false;
Vector3 cell_size = Vector3(2, 2, 2);
int octant_size = 8;
bool center_x = true;
bool center_y = true;
bool center_z = true;
float cell_scale = 1.0;
bool recreating_octants = false;
Ref<MeshLibrary> mesh_library;
HashMap<OctantKey, Octant *, OctantKey> octant_map;
HashMap<IndexKey, Cell, IndexKey> cell_map;
void _recreate_octant_data();
struct BakeLight {
RS::LightType type = RS::LightType::LIGHT_DIRECTIONAL;
Vector3 pos;
Vector3 dir;
float param[RS::LIGHT_PARAM_MAX] = {};
};
_FORCE_INLINE_ Vector3 _octant_get_offset(const OctantKey &p_key) const {
return Vector3(p_key.x, p_key.y, p_key.z) * cell_size * octant_size;
}
void _reset_physic_bodies_collision_filters();
void _octant_enter_world(const OctantKey &p_key);
void _octant_exit_world(const OctantKey &p_key);
bool _octant_update(const OctantKey &p_key);
void _octant_clean_up(const OctantKey &p_key);
void _octant_transform(const OctantKey &p_key);
bool awaiting_update = false;
void _queue_octants_dirty();
void _update_octants_callback();
void resource_changed(const Ref<Resource> &p_res);
void _clear_internal();
Vector3 _get_offset() const;
struct BakedMesh {
Ref<Mesh> mesh;
RID instance;
};
Vector<BakedMesh> baked_meshes;
protected:
bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const;
void _get_property_list(List<PropertyInfo> *p_list) const;
void _notification(int p_what);
void _update_visibility();
static void _bind_methods();
public:
enum {
INVALID_CELL_ITEM = -1
};
void set_collision_layer(uint32_t p_layer);
uint32_t get_collision_layer() const;
void set_collision_mask(uint32_t p_mask);
uint32_t get_collision_mask() const;
void set_collision_layer_value(int p_layer_number, bool p_value);
bool get_collision_layer_value(int p_layer_number) const;
void set_collision_mask_value(int p_layer_number, bool p_value);
bool get_collision_mask_value(int p_layer_number) const;
void set_physics_material(Ref<PhysicsMaterial> p_material);
Ref<PhysicsMaterial> get_physics_material() const;
Array get_collision_shapes() const;
void set_bake_navigation(bool p_bake_navigation);
bool is_baking_navigation();
void set_navigation_layers(uint32_t p_navigation_layers);
uint32_t get_navigation_layers() const;
void set_navigation_layer_value(int p_layer_number, bool p_value);
bool get_navigation_layer_value(int p_layer_number) const;
void set_mesh_library(const Ref<MeshLibrary> &p_mesh_library);
Ref<MeshLibrary> get_mesh_library() const;
void set_cell_size(const Vector3 &p_size);
Vector3 get_cell_size() const;
void set_octant_size(int p_size);
int get_octant_size() const;
void set_center_x(bool p_enable);
bool get_center_x() const;
void set_center_y(bool p_enable);
bool get_center_y() const;
void set_center_z(bool p_enable);
bool get_center_z() const;
void set_cell_item(const Vector3i &p_position, int p_item, int p_rot = 0);
int get_cell_item(const Vector3i &p_position) const;
int get_cell_item_orientation(const Vector3i &p_position) const;
Basis get_cell_item_basis(const Vector3i &p_position) const;
Basis get_basis_with_orthogonal_index(int p_index) const;
int get_orthogonal_index_from_basis(const Basis &p_basis) const;
Vector3i world_to_map(const Vector3 &p_world_position) const;
Vector3 map_to_world(const Vector3i &p_map_position) const;
void set_cell_scale(float p_scale);
float get_cell_scale() const;
Array get_used_cells() const;
Array get_used_cells_by_item(int p_item) const;
Array get_meshes() const;
void clear_baked_meshes();
void make_baked_meshes(bool p_gen_lightmap_uv = false, float p_lightmap_uv_texel_size = 0.1);
void clear();
Array get_bake_meshes();
RID get_bake_mesh_instance(int p_idx);
GridMap();
~GridMap();
};
#endif // GRID_MAP_H
|