2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* grid_map.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:11:42 +02:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* 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
|
|
|
|
|
2016-04-20 20:19:05 +02:00
|
|
|
#include "scene/3d/navigation.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "scene/3d/spatial.h"
|
|
|
|
#include "scene/resources/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
|
|
|
|
|
2014-06-11 15:41:03 +02:00
|
|
|
class BakedLightInstance;
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
class GridMap : public Spatial {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
GDCLASS(GridMap, Spatial);
|
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;
|
|
|
|
};
|
|
|
|
uint64_t 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;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
IndexKey() { key = 0; }
|
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
|
|
|
};
|
|
|
|
uint32_t cell;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Cell() {
|
|
|
|
item = 0;
|
|
|
|
rot = 0;
|
|
|
|
layer = 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 {
|
|
|
|
|
2016-04-20 20:19:05 +02:00
|
|
|
struct NavMesh {
|
|
|
|
int id;
|
|
|
|
Transform xform;
|
|
|
|
};
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-04-20 20:19:05 +02:00
|
|
|
struct ItemInstances {
|
2014-02-10 02:10:30 +01:00
|
|
|
Set<IndexKey> cells;
|
|
|
|
Ref<Mesh> mesh;
|
|
|
|
Ref<Shape> shape;
|
|
|
|
Ref<MultiMesh> multimesh;
|
|
|
|
RID multimesh_instance;
|
2016-04-20 20:19:05 +02:00
|
|
|
Ref<NavigationMesh> navmesh;
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2015-09-20 18:03:46 +02:00
|
|
|
RID collision_debug;
|
|
|
|
RID collision_debug_instance;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
bool dirty;
|
|
|
|
RID static_body;
|
2017-03-05 16:44:50 +01:00
|
|
|
Map<int, ItemInstances> items;
|
|
|
|
Map<IndexKey, NavMesh> navmesh_ids;
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
union OctantKey {
|
|
|
|
|
|
|
|
struct {
|
|
|
|
int16_t x;
|
|
|
|
int16_t y;
|
|
|
|
int16_t z;
|
|
|
|
int16_t area;
|
|
|
|
};
|
|
|
|
|
|
|
|
uint64_t key;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ bool operator<(const OctantKey &p_key) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return key < p_key.key;
|
|
|
|
}
|
|
|
|
|
|
|
|
//OctantKey(const IndexKey& p_k, int p_item) { indexkey=p_k.key; item=p_item; }
|
2017-03-05 16:44:50 +01:00
|
|
|
OctantKey() { key = 0; }
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Transform last_transform;
|
|
|
|
|
|
|
|
bool _in_tree;
|
|
|
|
float cell_size;
|
|
|
|
int octant_size;
|
2017-03-05 16:44:50 +01:00
|
|
|
bool center_x, center_y, center_z;
|
2014-02-10 02:10:30 +01:00
|
|
|
float cell_scale;
|
2016-04-20 20:19:05 +02:00
|
|
|
Navigation *navigation;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
bool clip;
|
|
|
|
bool clip_above;
|
|
|
|
int clip_floor;
|
|
|
|
Vector3::Axis clip_axis;
|
|
|
|
|
2016-04-20 20:19:05 +02:00
|
|
|
/**
|
|
|
|
* @brief An Area is something like a room: it has doors, and Octants can choose to belong to it.
|
|
|
|
*/
|
2014-02-10 02:10:30 +01:00
|
|
|
struct Area {
|
|
|
|
|
|
|
|
String name;
|
|
|
|
RID base_portal;
|
|
|
|
RID instance;
|
|
|
|
IndexKey from;
|
|
|
|
IndexKey to;
|
|
|
|
struct Portal {
|
|
|
|
Transform xform;
|
|
|
|
RID instance;
|
|
|
|
~Portal();
|
|
|
|
};
|
|
|
|
Vector<Portal> portals;
|
|
|
|
float portal_disable_distance;
|
|
|
|
Color portal_disable_color;
|
|
|
|
bool exterior_portal;
|
|
|
|
|
|
|
|
Area();
|
|
|
|
~Area();
|
|
|
|
};
|
|
|
|
|
|
|
|
Ref<MeshLibrary> theme;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Map<OctantKey, Octant *> octant_map;
|
|
|
|
Map<IndexKey, Cell> cell_map;
|
|
|
|
Map<int, Area *> area_map;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
void _recreate_octant_data();
|
|
|
|
|
|
|
|
struct BakeLight {
|
|
|
|
|
|
|
|
VS::LightType type;
|
|
|
|
Vector3 pos;
|
|
|
|
Vector3 dir;
|
|
|
|
float param[VS::LIGHT_PARAM_MAX];
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ int _find_area(const IndexKey &p_pos) const;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void _octant_enter_world(const OctantKey &p_key);
|
2016-04-20 20:19:05 +02:00
|
|
|
void _octant_enter_tree(const OctantKey &p_key);
|
2014-02-10 02:10:30 +01:00
|
|
|
void _octant_exit_world(const OctantKey &p_key);
|
|
|
|
void _octant_update(const OctantKey &p_key);
|
|
|
|
void _octant_transform(const OctantKey &p_key);
|
2017-03-05 16:44:50 +01:00
|
|
|
void _octant_clear_navmesh(const GridMap::OctantKey &);
|
2014-02-10 02:10:30 +01:00
|
|
|
bool awaiting_update;
|
|
|
|
|
|
|
|
void _queue_dirty_map();
|
|
|
|
void _update_dirty_map_callback();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void resource_changed(const RES &p_res);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
void _update_areas();
|
|
|
|
void _update_area_instances();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void _clear_internal(bool p_keep_areas = false);
|
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);
|
|
|
|
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-03-05 16:44:50 +01:00
|
|
|
void set_theme(const Ref<MeshLibrary> &p_theme);
|
2014-02-10 02:10:30 +01:00
|
|
|
Ref<MeshLibrary> get_theme() const;
|
|
|
|
|
|
|
|
void set_cell_size(float p_size);
|
|
|
|
float 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;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_orientation = 0);
|
|
|
|
int get_cell_item(int p_x, int p_y, int p_z) const;
|
|
|
|
int get_cell_item_orientation(int p_x, int p_y, int p_z) const;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void set_clip(bool p_enabled, bool p_clip_above = true, int p_floor = 0, Vector3::Axis p_axis = Vector3::AXIS_X);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error create_area(int p_id, const Rect3 &p_area);
|
2017-01-11 04:52:51 +01:00
|
|
|
Rect3 area_get_bounds(int p_area) const;
|
2017-03-05 16:44:50 +01:00
|
|
|
void area_set_exterior_portal(int p_area, bool p_enable);
|
|
|
|
void area_set_name(int p_area, const String &p_name);
|
2014-02-10 02:10:30 +01:00
|
|
|
String area_get_name(int p_area) const;
|
|
|
|
bool area_is_exterior_portal(int p_area) const;
|
|
|
|
void area_set_portal_disable_distance(int p_area, float p_distance);
|
|
|
|
float area_get_portal_disable_distance(int p_area) const;
|
|
|
|
void area_set_portal_disable_color(int p_area, Color p_color);
|
|
|
|
Color area_get_portal_disable_color(int p_area) const;
|
|
|
|
void get_area_list(List<int> *p_areas) const;
|
|
|
|
void erase_area(int p_area);
|
|
|
|
int get_unused_area_id() const;
|
|
|
|
|
|
|
|
void set_cell_scale(float p_scale);
|
|
|
|
float get_cell_scale() const;
|
|
|
|
|
2017-02-15 12:29:46 +01:00
|
|
|
Array get_meshes();
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void clear();
|
|
|
|
|
|
|
|
GridMap();
|
|
|
|
~GridMap();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // CUBE_GRID_MAP_H
|