grid_map.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. /*************************************************************************/
  2. /* grid_map.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "grid_map.h"
  31. #include "message_queue.h"
  32. #include "scene/3d/light.h"
  33. #include "scene/resources/surface_tool.h"
  34. #include "servers/visual_server.h"
  35. #include "io/marshalls.h"
  36. #include "os/os.h"
  37. #include "scene/resources/mesh_library.h"
  38. #include "scene/scene_string_names.h"
  39. bool GridMap::_set(const StringName &p_name, const Variant &p_value) {
  40. String name = p_name;
  41. if (name == "theme") {
  42. set_theme(p_value);
  43. } else if (name == "cell_size") {
  44. if (p_value.get_type() == Variant::INT || p_value.get_type() == Variant::REAL) {
  45. //compatibility
  46. float cs = p_value;
  47. set_cell_size(Vector3(cs, cs, cs));
  48. } else {
  49. set_cell_size(p_value);
  50. }
  51. } else if (name == "cell_octant_size") {
  52. set_octant_size(p_value);
  53. } else if (name == "cell_center_x") {
  54. set_center_x(p_value);
  55. } else if (name == "cell_center_y") {
  56. set_center_y(p_value);
  57. } else if (name == "cell_center_z") {
  58. set_center_z(p_value);
  59. } else if (name == "cell_scale") {
  60. set_cell_scale(p_value);
  61. /* } else if (name=="cells") {
  62. PoolVector<int> cells = p_value;
  63. int amount=cells.size();
  64. PoolVector<int>::Read r = cells.read();
  65. ERR_FAIL_COND_V(amount&1,false); // not even
  66. cell_map.clear();
  67. for(int i=0;i<amount/3;i++) {
  68. IndexKey ik;
  69. ik.key=decode_uint64(&r[i*3]);
  70. Cell cell;
  71. cell.cell=uint32_t(r[i*+1]);
  72. cell_map[ik]=cell;
  73. }
  74. _recreate_octant_data();*/
  75. } else if (name == "data") {
  76. Dictionary d = p_value;
  77. if (d.has("cells")) {
  78. PoolVector<int> cells = d["cells"];
  79. int amount = cells.size();
  80. PoolVector<int>::Read r = cells.read();
  81. ERR_FAIL_COND_V(amount % 3, false); // not even
  82. cell_map.clear();
  83. for (int i = 0; i < amount / 3; i++) {
  84. IndexKey ik;
  85. ik.key = decode_uint64((const uint8_t *)&r[i * 3]);
  86. Cell cell;
  87. cell.cell = decode_uint32((const uint8_t *)&r[i * 3 + 2]);
  88. cell_map[ik] = cell;
  89. }
  90. }
  91. _recreate_octant_data();
  92. } else
  93. return false;
  94. return true;
  95. }
  96. bool GridMap::_get(const StringName &p_name, Variant &r_ret) const {
  97. String name = p_name;
  98. if (name == "theme") {
  99. r_ret = get_theme();
  100. } else if (name == "cell_size") {
  101. r_ret = get_cell_size();
  102. } else if (name == "cell_octant_size") {
  103. r_ret = get_octant_size();
  104. } else if (name == "cell_center_x") {
  105. r_ret = get_center_x();
  106. } else if (name == "cell_center_y") {
  107. r_ret = get_center_y();
  108. } else if (name == "cell_center_z") {
  109. r_ret = get_center_z();
  110. } else if (name == "cell_scale") {
  111. r_ret = cell_scale;
  112. } else if (name == "data") {
  113. Dictionary d;
  114. PoolVector<int> cells;
  115. cells.resize(cell_map.size() * 3);
  116. {
  117. PoolVector<int>::Write w = cells.write();
  118. int i = 0;
  119. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next(), i++) {
  120. encode_uint64(E->key().key, (uint8_t *)&w[i * 3]);
  121. encode_uint32(E->get().cell, (uint8_t *)&w[i * 3 + 2]);
  122. }
  123. }
  124. d["cells"] = cells;
  125. r_ret = d;
  126. } else
  127. return false;
  128. return true;
  129. }
  130. void GridMap::_get_property_list(List<PropertyInfo> *p_list) const {
  131. p_list->push_back(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, "MeshLibrary"));
  132. p_list->push_back(PropertyInfo(Variant::NIL, "Cell", PROPERTY_HINT_NONE, "cell_", PROPERTY_USAGE_GROUP));
  133. p_list->push_back(PropertyInfo(Variant::VECTOR3, "cell_size"));
  134. p_list->push_back(PropertyInfo(Variant::INT, "cell_octant_size", PROPERTY_HINT_RANGE, "1,1024,1"));
  135. p_list->push_back(PropertyInfo(Variant::BOOL, "cell_center_x"));
  136. p_list->push_back(PropertyInfo(Variant::BOOL, "cell_center_y"));
  137. p_list->push_back(PropertyInfo(Variant::BOOL, "cell_center_z"));
  138. p_list->push_back(PropertyInfo(Variant::REAL, "cell_scale"));
  139. p_list->push_back(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  140. }
  141. void GridMap::set_theme(const Ref<MeshLibrary> &p_theme) {
  142. if (!theme.is_null())
  143. theme->unregister_owner(this);
  144. theme = p_theme;
  145. if (!theme.is_null())
  146. theme->register_owner(this);
  147. _recreate_octant_data();
  148. _change_notify("theme");
  149. }
  150. Ref<MeshLibrary> GridMap::get_theme() const {
  151. return theme;
  152. }
  153. void GridMap::set_cell_size(const Vector3 &p_size) {
  154. ERR_FAIL_COND(p_size.x < 0.001 || p_size.y < 0.001 || p_size.z < 0.001);
  155. cell_size = p_size;
  156. _recreate_octant_data();
  157. }
  158. Vector3 GridMap::get_cell_size() const {
  159. return cell_size;
  160. }
  161. void GridMap::set_octant_size(int p_size) {
  162. octant_size = p_size;
  163. _recreate_octant_data();
  164. }
  165. int GridMap::get_octant_size() const {
  166. return octant_size;
  167. }
  168. void GridMap::set_center_x(bool p_enable) {
  169. center_x = p_enable;
  170. _recreate_octant_data();
  171. }
  172. bool GridMap::get_center_x() const {
  173. return center_x;
  174. }
  175. void GridMap::set_center_y(bool p_enable) {
  176. center_y = p_enable;
  177. _recreate_octant_data();
  178. }
  179. bool GridMap::get_center_y() const {
  180. return center_y;
  181. }
  182. void GridMap::set_center_z(bool p_enable) {
  183. center_z = p_enable;
  184. _recreate_octant_data();
  185. }
  186. bool GridMap::get_center_z() const {
  187. return center_z;
  188. }
  189. void GridMap::set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_rot) {
  190. ERR_FAIL_INDEX(ABS(p_x), 1 << 20);
  191. ERR_FAIL_INDEX(ABS(p_y), 1 << 20);
  192. ERR_FAIL_INDEX(ABS(p_z), 1 << 20);
  193. IndexKey key;
  194. key.x = p_x;
  195. key.y = p_y;
  196. key.z = p_z;
  197. OctantKey ok;
  198. ok.x = p_x / octant_size;
  199. ok.y = p_y / octant_size;
  200. ok.z = p_z / octant_size;
  201. if (p_item < 0) {
  202. //erase
  203. if (cell_map.has(key)) {
  204. OctantKey octantkey = ok;
  205. ERR_FAIL_COND(!octant_map.has(octantkey));
  206. Octant &g = *octant_map[octantkey];
  207. g.cells.erase(key);
  208. g.dirty = true;
  209. cell_map.erase(key);
  210. _queue_octants_dirty();
  211. }
  212. return;
  213. }
  214. OctantKey octantkey = ok;
  215. if (!octant_map.has(octantkey)) {
  216. //create octant because it does not exist
  217. Octant *g = memnew(Octant);
  218. g->dirty = true;
  219. g->static_body = PhysicsServer::get_singleton()->body_create(PhysicsServer::BODY_MODE_STATIC);
  220. PhysicsServer::get_singleton()->body_attach_object_instance_id(g->static_body, get_instance_id());
  221. SceneTree *st = SceneTree::get_singleton();
  222. if (st && st->is_debugging_collisions_hint()) {
  223. g->collision_debug = VisualServer::get_singleton()->mesh_create();
  224. g->collision_debug_instance = VisualServer::get_singleton()->instance_create();
  225. VisualServer::get_singleton()->instance_set_base(g->collision_debug_instance, g->collision_debug);
  226. }
  227. octant_map[octantkey] = g;
  228. if (is_inside_world()) {
  229. _octant_enter_world(octantkey);
  230. _octant_transform(octantkey);
  231. }
  232. }
  233. Octant &g = *octant_map[octantkey];
  234. g.cells.insert(key);
  235. g.dirty = true;
  236. _queue_octants_dirty();
  237. Cell c;
  238. c.item = p_item;
  239. c.rot = p_rot;
  240. cell_map[key] = c;
  241. }
  242. int GridMap::get_cell_item(int p_x, int p_y, int p_z) const {
  243. ERR_FAIL_INDEX_V(ABS(p_x), 1 << 20, INVALID_CELL_ITEM);
  244. ERR_FAIL_INDEX_V(ABS(p_y), 1 << 20, INVALID_CELL_ITEM);
  245. ERR_FAIL_INDEX_V(ABS(p_z), 1 << 20, INVALID_CELL_ITEM);
  246. IndexKey key;
  247. key.x = p_x;
  248. key.y = p_y;
  249. key.z = p_z;
  250. if (!cell_map.has(key))
  251. return INVALID_CELL_ITEM;
  252. return cell_map[key].item;
  253. }
  254. int GridMap::get_cell_item_orientation(int p_x, int p_y, int p_z) const {
  255. ERR_FAIL_INDEX_V(ABS(p_x), 1 << 20, -1);
  256. ERR_FAIL_INDEX_V(ABS(p_y), 1 << 20, -1);
  257. ERR_FAIL_INDEX_V(ABS(p_z), 1 << 20, -1);
  258. IndexKey key;
  259. key.x = p_x;
  260. key.y = p_y;
  261. key.z = p_z;
  262. if (!cell_map.has(key))
  263. return -1;
  264. return cell_map[key].rot;
  265. }
  266. Vector3 GridMap::world_to_map(const Vector3 &p_world_pos) const {
  267. Vector3 map_pos = p_world_pos / cell_size;
  268. map_pos.x = floor(map_pos.x);
  269. map_pos.y = floor(map_pos.y);
  270. map_pos.z = floor(map_pos.z);
  271. return map_pos;
  272. }
  273. Vector3 GridMap::map_to_world(int p_x, int p_y, int p_z) const {
  274. Vector3 offset = _get_offset();
  275. Vector3 world_pos(
  276. p_x * cell_size.x + offset.x,
  277. p_y * cell_size.y + offset.y,
  278. p_z * cell_size.z + offset.z);
  279. return world_pos;
  280. }
  281. void GridMap::_octant_transform(const OctantKey &p_key) {
  282. ERR_FAIL_COND(!octant_map.has(p_key));
  283. Octant &g = *octant_map[p_key];
  284. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  285. if (g.collision_debug_instance.is_valid()) {
  286. VS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  287. }
  288. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  289. VS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  290. }
  291. }
  292. bool GridMap::_octant_update(const OctantKey &p_key) {
  293. ERR_FAIL_COND_V(!octant_map.has(p_key), false);
  294. Octant &g = *octant_map[p_key];
  295. if (!g.dirty)
  296. return false;
  297. //erase body shapes
  298. PhysicsServer::get_singleton()->body_clear_shapes(g.static_body);
  299. //erase body shapes debug
  300. if (g.collision_debug.is_valid()) {
  301. VS::get_singleton()->mesh_clear(g.collision_debug);
  302. }
  303. //erase navigation
  304. if (navigation) {
  305. for (Map<IndexKey, Octant::NavMesh>::Element *E = g.navmesh_ids.front(); E; E = E->next()) {
  306. navigation->navmesh_remove(E->get().id);
  307. }
  308. g.navmesh_ids.clear();
  309. }
  310. //erase multimeshes
  311. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  312. VS::get_singleton()->free(g.multimesh_instances[i].instance);
  313. VS::get_singleton()->free(g.multimesh_instances[i].multimesh);
  314. }
  315. g.multimesh_instances.clear();
  316. if (g.cells.size() == 0) {
  317. //octant no longer needed
  318. _octant_clean_up(p_key);
  319. return true;
  320. }
  321. PoolVector<Vector3> col_debug;
  322. /*
  323. * foreach item in this octant,
  324. * set item's multimesh's instance count to number of cells which have this item
  325. * and set said multimesh bounding box to one containing all cells which have this item
  326. */
  327. Map<int, List<Pair<Transform, IndexKey> > > multimesh_items;
  328. for (Set<IndexKey>::Element *E = g.cells.front(); E; E = E->next()) {
  329. ERR_CONTINUE(!cell_map.has(E->get()));
  330. const Cell &c = cell_map[E->get()];
  331. if (!theme.is_valid() || !theme->has_item(c.item))
  332. continue;
  333. //print_line("OCTANT, CELLS: "+itos(ii.cells.size()));
  334. Vector3 cellpos = Vector3(E->get().x, E->get().y, E->get().z);
  335. Vector3 ofs = _get_offset();
  336. Transform xform;
  337. if (clip && ((clip_above && cellpos[clip_axis] > clip_floor) || (!clip_above && cellpos[clip_axis] < clip_floor))) {
  338. } else {
  339. }
  340. xform.basis.set_orthogonal_index(c.rot);
  341. xform.set_origin(cellpos * cell_size + ofs);
  342. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  343. if (theme->get_item_mesh(c.item).is_valid()) {
  344. if (!multimesh_items.has(c.item)) {
  345. multimesh_items[c.item] = List<Pair<Transform, IndexKey> >();
  346. }
  347. Pair<Transform, IndexKey> p;
  348. p.first = xform;
  349. p.second = E->get();
  350. multimesh_items[c.item].push_back(p);
  351. }
  352. Vector<MeshLibrary::ShapeData> shapes = theme->get_item_shapes(c.item);
  353. // add the item's shape at given xform to octant's static_body
  354. for (int i = 0; i < shapes.size(); i++) {
  355. // add the item's shape
  356. if (!shapes[i].shape.is_valid())
  357. continue;
  358. PhysicsServer::get_singleton()->body_add_shape(g.static_body, shapes[i].shape->get_rid(), xform * shapes[i].local_transform);
  359. if (g.collision_debug.is_valid()) {
  360. shapes[i].shape->add_vertices_to_array(col_debug, xform * shapes[i].local_transform);
  361. }
  362. //print_line("PHIS x: "+xform);
  363. }
  364. // add the item's navmesh at given xform to GridMap's Navigation ancestor
  365. Ref<NavigationMesh> navmesh = theme->get_item_navmesh(c.item);
  366. if (navmesh.is_valid()) {
  367. Octant::NavMesh nm;
  368. nm.xform = xform;
  369. if (navigation) {
  370. nm.id = navigation->navmesh_create(navmesh, xform, this);
  371. } else {
  372. nm.id = -1;
  373. }
  374. g.navmesh_ids[E->get()] = nm;
  375. }
  376. }
  377. //update multimeshes
  378. for (Map<int, List<Pair<Transform, IndexKey> > >::Element *E = multimesh_items.front(); E; E = E->next()) {
  379. Octant::MultimeshInstance mmi;
  380. RID mm = VS::get_singleton()->multimesh_create();
  381. VS::get_singleton()->multimesh_allocate(mm, E->get().size(), VS::MULTIMESH_TRANSFORM_3D, VS::MULTIMESH_COLOR_NONE);
  382. VS::get_singleton()->multimesh_set_mesh(mm, theme->get_item_mesh(E->key())->get_rid());
  383. int idx = 0;
  384. for (List<Pair<Transform, IndexKey> >::Element *F = E->get().front(); F; F = F->next()) {
  385. VS::get_singleton()->multimesh_instance_set_transform(mm, idx, F->get().first);
  386. #ifdef TOOLS_ENABLED
  387. Octant::MultimeshInstance::Item it;
  388. it.index = idx;
  389. it.transform = F->get().first;
  390. it.key = F->get().second;
  391. mmi.items.push_back(it);
  392. #endif
  393. idx++;
  394. }
  395. RID instance = VS::get_singleton()->instance_create();
  396. VS::get_singleton()->instance_set_base(instance, mm);
  397. if (is_inside_tree()) {
  398. VS::get_singleton()->instance_set_scenario(instance, get_world()->get_scenario());
  399. VS::get_singleton()->instance_set_transform(instance, get_global_transform());
  400. }
  401. mmi.multimesh = mm;
  402. mmi.instance = instance;
  403. g.multimesh_instances.push_back(mmi);
  404. }
  405. if (col_debug.size()) {
  406. Array arr;
  407. arr.resize(VS::ARRAY_MAX);
  408. arr[VS::ARRAY_VERTEX] = col_debug;
  409. VS::get_singleton()->mesh_add_surface_from_arrays(g.collision_debug, VS::PRIMITIVE_LINES, arr);
  410. SceneTree *st = SceneTree::get_singleton();
  411. if (st) {
  412. VS::get_singleton()->mesh_surface_set_material(g.collision_debug, 0, st->get_debug_collision_material()->get_rid());
  413. }
  414. }
  415. g.dirty = false;
  416. return false;
  417. }
  418. void GridMap::_octant_enter_world(const OctantKey &p_key) {
  419. ERR_FAIL_COND(!octant_map.has(p_key));
  420. Octant &g = *octant_map[p_key];
  421. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  422. PhysicsServer::get_singleton()->body_set_space(g.static_body, get_world()->get_space());
  423. //print_line("BODYPOS: "+get_global_transform());
  424. if (g.collision_debug_instance.is_valid()) {
  425. VS::get_singleton()->instance_set_scenario(g.collision_debug_instance, get_world()->get_scenario());
  426. VS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  427. }
  428. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  429. VS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, get_world()->get_scenario());
  430. VS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  431. }
  432. if (navigation && theme.is_valid()) {
  433. for (Map<IndexKey, Octant::NavMesh>::Element *F = g.navmesh_ids.front(); F; F = F->next()) {
  434. if (cell_map.has(F->key()) && F->get().id < 0) {
  435. Ref<NavigationMesh> nm = theme->get_item_navmesh(cell_map[F->key()].item);
  436. if (nm.is_valid()) {
  437. F->get().id = navigation->navmesh_create(nm, F->get().xform, this);
  438. }
  439. }
  440. }
  441. }
  442. }
  443. void GridMap::_octant_exit_world(const OctantKey &p_key) {
  444. ERR_FAIL_COND(!octant_map.has(p_key));
  445. Octant &g = *octant_map[p_key];
  446. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  447. PhysicsServer::get_singleton()->body_set_space(g.static_body, RID());
  448. if (g.collision_debug_instance.is_valid()) {
  449. VS::get_singleton()->instance_set_scenario(g.collision_debug_instance, RID());
  450. }
  451. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  452. VS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, RID());
  453. }
  454. if (navigation) {
  455. for (Map<IndexKey, Octant::NavMesh>::Element *F = g.navmesh_ids.front(); F; F = F->next()) {
  456. if (F->get().id >= 0) {
  457. navigation->navmesh_remove(F->get().id);
  458. F->get().id = -1;
  459. }
  460. }
  461. }
  462. }
  463. void GridMap::_octant_clean_up(const OctantKey &p_key) {
  464. ERR_FAIL_COND(!octant_map.has(p_key));
  465. Octant &g = *octant_map[p_key];
  466. if (g.collision_debug.is_valid())
  467. VS::get_singleton()->free(g.collision_debug);
  468. if (g.collision_debug_instance.is_valid())
  469. VS::get_singleton()->free(g.collision_debug_instance);
  470. PhysicsServer::get_singleton()->free(g.static_body);
  471. //erase navigation
  472. if (navigation) {
  473. for (Map<IndexKey, Octant::NavMesh>::Element *E = g.navmesh_ids.front(); E; E = E->next()) {
  474. navigation->navmesh_remove(E->get().id);
  475. }
  476. g.navmesh_ids.clear();
  477. }
  478. //erase multimeshes
  479. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  480. VS::get_singleton()->free(g.multimesh_instances[i].instance);
  481. VS::get_singleton()->free(g.multimesh_instances[i].multimesh);
  482. }
  483. g.multimesh_instances.clear();
  484. }
  485. void GridMap::_notification(int p_what) {
  486. switch (p_what) {
  487. case NOTIFICATION_ENTER_WORLD: {
  488. Spatial *c = this;
  489. while (c) {
  490. navigation = Object::cast_to<Navigation>(c);
  491. if (navigation) {
  492. break;
  493. }
  494. c = Object::cast_to<Spatial>(c->get_parent());
  495. }
  496. last_transform = get_global_transform();
  497. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  498. _octant_enter_world(E->key());
  499. }
  500. } break;
  501. case NOTIFICATION_TRANSFORM_CHANGED: {
  502. Transform new_xform = get_global_transform();
  503. if (new_xform == last_transform)
  504. break;
  505. //update run
  506. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  507. _octant_transform(E->key());
  508. }
  509. last_transform = new_xform;
  510. } break;
  511. case NOTIFICATION_EXIT_WORLD: {
  512. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  513. _octant_exit_world(E->key());
  514. }
  515. navigation = NULL;
  516. //_queue_octants_dirty(MAP_DIRTY_INSTANCES|MAP_DIRTY_TRANSFORMS);
  517. //_update_octants_callback();
  518. //_update_area_instances();
  519. } break;
  520. case NOTIFICATION_VISIBILITY_CHANGED: {
  521. _update_visibility();
  522. } break;
  523. }
  524. }
  525. void GridMap::_update_visibility() {
  526. if (!is_inside_tree())
  527. return;
  528. _change_notify("visible");
  529. for (Map<OctantKey, Octant *>::Element *e = octant_map.front(); e; e = e->next()) {
  530. Octant *octant = e->value();
  531. for (int i = 0; i < octant->multimesh_instances.size(); i++) {
  532. Octant::MultimeshInstance &mi = octant->multimesh_instances[i];
  533. VS::get_singleton()->instance_set_visible(mi.instance, is_visible());
  534. }
  535. }
  536. }
  537. void GridMap::_queue_octants_dirty() {
  538. if (awaiting_update)
  539. return;
  540. MessageQueue::get_singleton()->push_call(this, "_update_octants_callback");
  541. awaiting_update = true;
  542. }
  543. void GridMap::_recreate_octant_data() {
  544. Map<IndexKey, Cell> cell_copy = cell_map;
  545. _clear_internal();
  546. for (Map<IndexKey, Cell>::Element *E = cell_copy.front(); E; E = E->next()) {
  547. set_cell_item(E->key().x, E->key().y, E->key().z, E->get().item, E->get().rot);
  548. }
  549. }
  550. void GridMap::_clear_internal() {
  551. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  552. if (is_inside_world())
  553. _octant_exit_world(E->key());
  554. _octant_clean_up(E->key());
  555. memdelete(E->get());
  556. }
  557. octant_map.clear();
  558. cell_map.clear();
  559. }
  560. void GridMap::clear() {
  561. _clear_internal();
  562. }
  563. void GridMap::resource_changed(const RES &p_res) {
  564. _recreate_octant_data();
  565. }
  566. void GridMap::_update_octants_callback() {
  567. if (!awaiting_update)
  568. return;
  569. List<OctantKey> to_delete;
  570. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  571. if (_octant_update(E->key())) {
  572. to_delete.push_back(E->key());
  573. }
  574. }
  575. while (to_delete.front()) {
  576. octant_map.erase(to_delete.front()->get());
  577. to_delete.pop_back();
  578. }
  579. _update_visibility();
  580. awaiting_update = false;
  581. }
  582. void GridMap::_bind_methods() {
  583. ClassDB::bind_method(D_METHOD("set_theme", "theme"), &GridMap::set_theme);
  584. ClassDB::bind_method(D_METHOD("get_theme"), &GridMap::get_theme);
  585. ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &GridMap::set_cell_size);
  586. ClassDB::bind_method(D_METHOD("get_cell_size"), &GridMap::get_cell_size);
  587. ClassDB::bind_method(D_METHOD("set_octant_size", "size"), &GridMap::set_octant_size);
  588. ClassDB::bind_method(D_METHOD("get_octant_size"), &GridMap::get_octant_size);
  589. ClassDB::bind_method(D_METHOD("set_cell_item", "x", "y", "z", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0));
  590. ClassDB::bind_method(D_METHOD("get_cell_item", "x", "y", "z"), &GridMap::get_cell_item);
  591. ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "x", "y", "z"), &GridMap::get_cell_item_orientation);
  592. ClassDB::bind_method(D_METHOD("world_to_map", "pos"), &GridMap::world_to_map);
  593. ClassDB::bind_method(D_METHOD("map_to_world", "x", "y", "z"), &GridMap::map_to_world);
  594. //ClassDB::bind_method(D_METHOD("_recreate_octants"),&GridMap::_recreate_octants);
  595. ClassDB::bind_method(D_METHOD("_update_octants_callback"), &GridMap::_update_octants_callback);
  596. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed);
  597. ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &GridMap::set_center_x);
  598. ClassDB::bind_method(D_METHOD("get_center_x"), &GridMap::get_center_x);
  599. ClassDB::bind_method(D_METHOD("set_center_y", "enable"), &GridMap::set_center_y);
  600. ClassDB::bind_method(D_METHOD("get_center_y"), &GridMap::get_center_y);
  601. ClassDB::bind_method(D_METHOD("set_center_z", "enable"), &GridMap::set_center_z);
  602. ClassDB::bind_method(D_METHOD("get_center_z"), &GridMap::get_center_z);
  603. ClassDB::bind_method(D_METHOD("set_clip", "enabled", "clipabove", "floor", "axis"), &GridMap::set_clip, DEFVAL(true), DEFVAL(0), DEFVAL(Vector3::AXIS_X));
  604. ClassDB::bind_method(D_METHOD("clear"), &GridMap::clear);
  605. ClassDB::bind_method(D_METHOD("get_used_cells"), &GridMap::get_used_cells);
  606. ClassDB::bind_method(D_METHOD("get_meshes"), &GridMap::get_meshes);
  607. BIND_CONSTANT(INVALID_CELL_ITEM);
  608. }
  609. void GridMap::set_clip(bool p_enabled, bool p_clip_above, int p_floor, Vector3::Axis p_axis) {
  610. if (!p_enabled && !clip)
  611. return;
  612. if (clip && p_enabled && clip_floor == p_floor && p_clip_above == clip_above && p_axis == clip_axis)
  613. return;
  614. clip = p_enabled;
  615. clip_floor = p_floor;
  616. clip_axis = p_axis;
  617. clip_above = p_clip_above;
  618. //make it all update
  619. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  620. Octant *g = E->get();
  621. g->dirty = true;
  622. }
  623. awaiting_update = true;
  624. _update_octants_callback();
  625. }
  626. void GridMap::set_cell_scale(float p_scale) {
  627. cell_scale = p_scale;
  628. _recreate_octant_data();
  629. }
  630. float GridMap::get_cell_scale() const {
  631. return cell_scale;
  632. }
  633. Array GridMap::get_used_cells() const {
  634. Array a;
  635. a.resize(cell_map.size());
  636. int i = 0;
  637. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
  638. Vector3 p(E->key().x, E->key().y, E->key().z);
  639. a[i++] = p;
  640. }
  641. return a;
  642. }
  643. Array GridMap::get_meshes() {
  644. if (theme.is_null())
  645. return Array();
  646. Vector3 ofs = _get_offset();
  647. Array meshes;
  648. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
  649. int id = E->get().item;
  650. if (!theme->has_item(id))
  651. continue;
  652. Ref<Mesh> mesh = theme->get_item_mesh(id);
  653. if (mesh.is_null())
  654. continue;
  655. IndexKey ik = E->key();
  656. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  657. Transform xform;
  658. xform.basis.set_orthogonal_index(E->get().rot);
  659. xform.set_origin(cellpos * cell_size + ofs);
  660. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  661. meshes.push_back(xform);
  662. meshes.push_back(mesh);
  663. }
  664. return meshes;
  665. }
  666. Vector3 GridMap::_get_offset() const {
  667. return Vector3(
  668. cell_size.x * 0.5 * int(center_x),
  669. cell_size.y * 0.5 * int(center_y),
  670. cell_size.z * 0.5 * int(center_z));
  671. }
  672. GridMap::GridMap() {
  673. cell_size = Vector3(2, 2, 2);
  674. octant_size = 4;
  675. awaiting_update = false;
  676. _in_tree = false;
  677. center_x = true;
  678. center_y = true;
  679. center_z = true;
  680. clip = false;
  681. clip_floor = 0;
  682. clip_axis = Vector3::AXIS_Z;
  683. clip_above = true;
  684. cell_scale = 1.0;
  685. navigation = NULL;
  686. set_notify_transform(true);
  687. }
  688. GridMap::~GridMap() {
  689. if (!theme.is_null())
  690. theme->unregister_owner(this);
  691. clear();
  692. }