2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* grid_map.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifndef GRID_MAP_H
|
|
|
|
#define GRID_MAP_H
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
#include "scene/3d/node_3d.h"
|
2021-08-13 18:42:45 +02:00
|
|
|
#include "scene/resources/3d/mesh_library.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
#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
|
|
|
|
|
2021-12-17 01:47:17 +01:00
|
|
|
class PhysicsMaterial;
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
class GridMap : public Node3D {
|
|
|
|
GDCLASS(GridMap, Node3D);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
enum {
|
2017-03-05 16:44:50 +01:00
|
|
|
MAP_DIRTY_TRANSFORMS = 1,
|
|
|
|
MAP_DIRTY_INSTANCES = 2,
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
union IndexKey {
|
|
|
|
struct {
|
|
|
|
int16_t x;
|
|
|
|
int16_t y;
|
|
|
|
int16_t z;
|
|
|
|
};
|
2021-02-08 10:57:18 +01:00
|
|
|
uint64_t key = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-05-13 15:04:37 +02:00
|
|
|
static uint32_t hash(const IndexKey &p_key) {
|
|
|
|
return hash_one_uint64(p_key.key);
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ bool operator<(const IndexKey &p_key) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return key < p_key.key;
|
|
|
|
}
|
2022-05-13 15:04:37 +02:00
|
|
|
_FORCE_INLINE_ bool operator==(const IndexKey &p_key) const {
|
|
|
|
return key == p_key.key;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-06-29 10:52:01 +02:00
|
|
|
_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;
|
|
|
|
}
|
2021-02-08 10:57:18 +01:00
|
|
|
IndexKey() {}
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2016-04-20 20:19:05 +02:00
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2014-02-10 02:10:30 +01:00
|
|
|
union Cell {
|
|
|
|
struct {
|
|
|
|
unsigned int item : 16;
|
2017-03-05 16:44:50 +01:00
|
|
|
unsigned int rot : 5;
|
|
|
|
unsigned int layer : 8;
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
2021-02-08 10:57:18 +01:00
|
|
|
uint32_t cell = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2016-04-20 20:19:05 +02:00
|
|
|
/**
|
|
|
|
* @brief An Octant is a prism containing Cells, and possibly belonging to an Area.
|
|
|
|
* A GridMap can have multiple Octants.
|
|
|
|
*/
|
2014-02-10 02:10:30 +01:00
|
|
|
struct Octant {
|
2022-12-11 18:02:35 +01:00
|
|
|
struct NavigationCell {
|
2020-01-10 12:22:34 +01:00
|
|
|
RID region;
|
2020-10-17 07:08:21 +02:00
|
|
|
Transform3D xform;
|
2022-12-11 18:02:35 +01:00
|
|
|
RID navigation_mesh_debug_instance;
|
2022-11-29 20:01:31 +01:00
|
|
|
uint32_t navigation_layers = 1;
|
2016-04-20 20:19:05 +02:00
|
|
|
};
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-08-27 21:00:59 +02:00
|
|
|
struct MultimeshInstance {
|
|
|
|
RID instance;
|
|
|
|
RID multimesh;
|
|
|
|
struct Item {
|
2021-02-08 10:57:18 +01:00
|
|
|
int index = 0;
|
2020-10-17 07:08:21 +02:00
|
|
|
Transform3D transform;
|
2017-08-27 21:00:59 +02:00
|
|
|
IndexKey key;
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector<Item> items; //tools only, for changing visibility
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2017-08-27 21:00:59 +02:00
|
|
|
Vector<MultimeshInstance> multimesh_instances;
|
2022-05-19 17:00:06 +02:00
|
|
|
HashSet<IndexKey> cells;
|
2015-09-20 18:03:46 +02:00
|
|
|
RID collision_debug;
|
|
|
|
RID collision_debug_instance;
|
2022-08-09 20:13:47 +02:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
RID navigation_debug_edge_connections_instance;
|
|
|
|
Ref<ArrayMesh> navigation_debug_edge_connections_mesh;
|
|
|
|
#endif // DEBUG_ENABLED
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-02-08 10:57:18 +01:00
|
|
|
bool dirty = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
RID static_body;
|
2022-12-11 18:02:35 +01:00
|
|
|
HashMap<IndexKey, NavigationCell> navigation_cell_ids;
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
union OctantKey {
|
|
|
|
struct {
|
|
|
|
int16_t x;
|
|
|
|
int16_t y;
|
|
|
|
int16_t z;
|
2017-08-26 05:40:45 +02:00
|
|
|
int16_t empty;
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2021-02-08 10:57:18 +01:00
|
|
|
uint64_t key = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-05-13 15:04:37 +02:00
|
|
|
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;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//OctantKey(const IndexKey& p_k, int p_item) { indexkey=p_k.key; item=p_item; }
|
2021-02-08 10:57:18 +01:00
|
|
|
OctantKey() {}
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2021-02-08 10:57:18 +01:00
|
|
|
uint32_t collision_layer = 1;
|
|
|
|
uint32_t collision_mask = 1;
|
2022-12-19 19:01:27 +01:00
|
|
|
real_t collision_priority = 1.0;
|
2021-12-17 01:47:17 +01:00
|
|
|
Ref<PhysicsMaterial> physics_material;
|
2021-03-08 09:47:18 +01:00
|
|
|
bool bake_navigation = false;
|
2022-09-07 13:51:32 +02:00
|
|
|
RID map_override;
|
2017-12-28 16:13:45 +01:00
|
|
|
|
2020-10-17 07:08:21 +02:00
|
|
|
Transform3D last_transform;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-02-08 10:57:18 +01:00
|
|
|
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;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-02-08 10:57:18 +01:00
|
|
|
bool recreating_octants = false;
|
2017-12-18 04:34:48 +01:00
|
|
|
|
2018-08-22 08:10:54 +02:00
|
|
|
Ref<MeshLibrary> mesh_library;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-05-13 15:04:37 +02:00
|
|
|
HashMap<OctantKey, Octant *, OctantKey> octant_map;
|
|
|
|
HashMap<IndexKey, Cell, IndexKey> cell_map;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
void _recreate_octant_data();
|
|
|
|
|
|
|
|
struct BakeLight {
|
2021-02-08 10:57:18 +01:00
|
|
|
RS::LightType type = RS::LightType::LIGHT_DIRECTIONAL;
|
2014-02-10 02:10:30 +01:00
|
|
|
Vector3 pos;
|
|
|
|
Vector3 dir;
|
2021-02-08 10:57:18 +01:00
|
|
|
float param[RS::LIGHT_PARAM_MAX] = {};
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector3 _octant_get_offset(const OctantKey &p_key) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
return Vector3(p_key.x, p_key.y, p_key.z) * cell_size * octant_size;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-12-19 19:01:27 +01:00
|
|
|
void _update_physics_bodies_collision_properties();
|
2024-04-10 21:44:46 +02:00
|
|
|
void _update_physics_bodies_characteristics();
|
2014-02-10 02:10:30 +01:00
|
|
|
void _octant_enter_world(const OctantKey &p_key);
|
|
|
|
void _octant_exit_world(const OctantKey &p_key);
|
2017-08-27 21:00:59 +02:00
|
|
|
bool _octant_update(const OctantKey &p_key);
|
|
|
|
void _octant_clean_up(const OctantKey &p_key);
|
2014-02-10 02:10:30 +01:00
|
|
|
void _octant_transform(const OctantKey &p_key);
|
2022-08-09 20:13:47 +02:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
void _update_octant_navigation_debug_edge_connections_mesh(const OctantKey &p_key);
|
|
|
|
void _navigation_map_changed(RID p_map);
|
|
|
|
void _update_navigation_debug_edge_connections();
|
|
|
|
#endif // DEBUG_ENABLED
|
2021-02-08 10:57:18 +01:00
|
|
|
bool awaiting_update = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-08-27 21:00:59 +02:00
|
|
|
void _queue_octants_dirty();
|
|
|
|
void _update_octants_callback();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2023-07-03 21:29:37 +02:00
|
|
|
#ifndef DISABLE_DEPRECATED
|
2022-05-03 01:43:50 +02:00
|
|
|
void resource_changed(const Ref<Resource> &p_res);
|
2023-07-03 21:29:37 +02:00
|
|
|
#endif
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-08-26 05:40:45 +02:00
|
|
|
void _clear_internal();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-10-07 13:30:58 +02:00
|
|
|
Vector3 _get_offset() const;
|
|
|
|
|
2017-12-15 22:23:16 +01:00
|
|
|
struct BakedMesh {
|
|
|
|
Ref<Mesh> mesh;
|
2017-12-18 04:34:48 +01:00
|
|
|
RID instance;
|
2017-12-15 22:23:16 +01:00
|
|
|
};
|
|
|
|
|
2017-12-18 04:34:48 +01:00
|
|
|
Vector<BakedMesh> baked_meshes;
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
protected:
|
2017-03-05 16:44:50 +01:00
|
|
|
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;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
void _notification(int p_what);
|
2017-09-04 22:52:44 +02:00
|
|
|
void _update_visibility();
|
2014-02-10 02:10:30 +01:00
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
public:
|
|
|
|
enum {
|
2017-03-05 16:44:50 +01:00
|
|
|
INVALID_CELL_ITEM = -1
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2017-12-28 16:13:45 +01:00
|
|
|
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;
|
|
|
|
|
2021-08-12 01:01:38 +02:00
|
|
|
void set_collision_layer_value(int p_layer_number, bool p_value);
|
|
|
|
bool get_collision_layer_value(int p_layer_number) const;
|
2017-12-28 16:13:45 +01:00
|
|
|
|
2021-08-12 01:01:38 +02:00
|
|
|
void set_collision_mask_value(int p_layer_number, bool p_value);
|
|
|
|
bool get_collision_mask_value(int p_layer_number) const;
|
2017-12-28 16:13:45 +01:00
|
|
|
|
2022-12-19 19:01:27 +01:00
|
|
|
void set_collision_priority(real_t p_priority);
|
|
|
|
real_t get_collision_priority() const;
|
|
|
|
|
2021-12-17 01:47:17 +01:00
|
|
|
void set_physics_material(Ref<PhysicsMaterial> p_material);
|
|
|
|
Ref<PhysicsMaterial> get_physics_material() const;
|
|
|
|
|
2022-01-14 16:36:59 +01:00
|
|
|
Array get_collision_shapes() const;
|
|
|
|
|
2021-03-08 09:47:18 +01:00
|
|
|
void set_bake_navigation(bool p_bake_navigation);
|
|
|
|
bool is_baking_navigation();
|
|
|
|
|
2022-09-07 13:51:32 +02:00
|
|
|
void set_navigation_map(RID p_navigation_map);
|
|
|
|
RID get_navigation_map() const;
|
|
|
|
|
2018-08-22 08:10:54 +02:00
|
|
|
void set_mesh_library(const Ref<MeshLibrary> &p_mesh_library);
|
|
|
|
Ref<MeshLibrary> get_mesh_library() const;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-08-27 21:00:59 +02:00
|
|
|
void set_cell_size(const Vector3 &p_size);
|
|
|
|
Vector3 get_cell_size() const;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-06-29 10:52:01 +02:00
|
|
|
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;
|
2022-01-27 06:44:36 +01:00
|
|
|
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;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-08-20 18:39:05 +02:00
|
|
|
Vector3i local_to_map(const Vector3 &p_local_position) const;
|
|
|
|
Vector3 map_to_local(const Vector3i &p_map_position) const;
|
2017-10-07 13:30:58 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void set_cell_scale(float p_scale);
|
|
|
|
float get_cell_scale() const;
|
|
|
|
|
2022-08-05 20:35:08 +02:00
|
|
|
TypedArray<Vector3i> get_used_cells() const;
|
|
|
|
TypedArray<Vector3i> get_used_cells_by_item(int p_item) const;
|
2017-10-05 15:34:34 +02:00
|
|
|
|
2022-01-14 16:36:59 +01:00
|
|
|
Array get_meshes() const;
|
2017-02-15 12:29:46 +01:00
|
|
|
|
2017-12-18 04:34:48 +01:00
|
|
|
void clear_baked_meshes();
|
|
|
|
void make_baked_meshes(bool p_gen_lightmap_uv = false, float p_lightmap_uv_texel_size = 0.1);
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void clear();
|
|
|
|
|
2017-12-18 04:34:48 +01:00
|
|
|
Array get_bake_meshes();
|
|
|
|
RID get_bake_mesh_instance(int p_idx);
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
GridMap();
|
|
|
|
~GridMap();
|
|
|
|
};
|
|
|
|
|
2017-08-27 21:07:15 +02:00
|
|
|
#endif // GRID_MAP_H
|