grid_map.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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. void GridMap::_octant_transform(const OctantKey &p_key) {
  267. ERR_FAIL_COND(!octant_map.has(p_key));
  268. Octant &g = *octant_map[p_key];
  269. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  270. if (g.collision_debug_instance.is_valid()) {
  271. VS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  272. }
  273. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  274. VS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  275. }
  276. }
  277. bool GridMap::_octant_update(const OctantKey &p_key) {
  278. ERR_FAIL_COND_V(!octant_map.has(p_key), false);
  279. Octant &g = *octant_map[p_key];
  280. if (!g.dirty)
  281. return false;
  282. //erase body shapes
  283. PhysicsServer::get_singleton()->body_clear_shapes(g.static_body);
  284. //erase body shapes debug
  285. if (g.collision_debug.is_valid()) {
  286. VS::get_singleton()->mesh_clear(g.collision_debug);
  287. }
  288. //erase navigation
  289. if (navigation) {
  290. for (Map<IndexKey, Octant::NavMesh>::Element *E = g.navmesh_ids.front(); E; E = E->next()) {
  291. navigation->navmesh_remove(E->get().id);
  292. }
  293. g.navmesh_ids.clear();
  294. }
  295. //erase multimeshes
  296. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  297. VS::get_singleton()->free(g.multimesh_instances[i].instance);
  298. VS::get_singleton()->free(g.multimesh_instances[i].multimesh);
  299. }
  300. g.multimesh_instances.clear();
  301. if (g.cells.size() == 0) {
  302. //octant no longer needed
  303. _octant_clean_up(p_key);
  304. return true;
  305. }
  306. PoolVector<Vector3> col_debug;
  307. /*
  308. * foreach item in this octant,
  309. * set item's multimesh's instance count to number of cells which have this item
  310. * and set said multimesh bounding box to one containing all cells which have this item
  311. */
  312. Map<int, List<Pair<Transform, IndexKey> > > multimesh_items;
  313. for (Set<IndexKey>::Element *E = g.cells.front(); E; E = E->next()) {
  314. ERR_CONTINUE(!cell_map.has(E->get()));
  315. const Cell &c = cell_map[E->get()];
  316. if (!theme.is_valid() || !theme->has_item(c.item))
  317. continue;
  318. //print_line("OCTANT, CELLS: "+itos(ii.cells.size()));
  319. Vector3 cellpos = Vector3(E->get().x, E->get().y, E->get().z);
  320. Vector3 ofs(cell_size.x * 0.5 * int(center_x), cell_size.y * 0.5 * int(center_y), cell_size.z * 0.5 * int(center_z));
  321. Transform xform;
  322. if (clip && ((clip_above && cellpos[clip_axis] > clip_floor) || (!clip_above && cellpos[clip_axis] < clip_floor))) {
  323. } else {
  324. }
  325. xform.basis.set_orthogonal_index(c.rot);
  326. xform.set_origin(cellpos * cell_size + ofs);
  327. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  328. if (theme->get_item_mesh(c.item).is_valid()) {
  329. if (!multimesh_items.has(c.item)) {
  330. multimesh_items[c.item] = List<Pair<Transform, IndexKey> >();
  331. }
  332. Pair<Transform, IndexKey> p;
  333. p.first = xform;
  334. p.second = E->get();
  335. multimesh_items[c.item].push_back(p);
  336. }
  337. Vector<MeshLibrary::ShapeData> shapes = theme->get_item_shapes(c.item);
  338. // add the item's shape at given xform to octant's static_body
  339. for (int i = 0; i < shapes.size(); i++) {
  340. // add the item's shape
  341. if (!shapes[i].shape.is_valid())
  342. continue;
  343. PhysicsServer::get_singleton()->body_add_shape(g.static_body, shapes[i].shape->get_rid(), xform * shapes[i].local_transform);
  344. if (g.collision_debug.is_valid()) {
  345. shapes[i].shape->add_vertices_to_array(col_debug, xform * shapes[i].local_transform);
  346. }
  347. //print_line("PHIS x: "+xform);
  348. }
  349. // add the item's navmesh at given xform to GridMap's Navigation ancestor
  350. Ref<NavigationMesh> navmesh = theme->get_item_navmesh(c.item);
  351. if (navmesh.is_valid()) {
  352. Octant::NavMesh nm;
  353. nm.xform = xform;
  354. if (navigation) {
  355. nm.id = navigation->navmesh_create(navmesh, xform, this);
  356. } else {
  357. nm.id = -1;
  358. }
  359. g.navmesh_ids[E->get()] = nm;
  360. }
  361. }
  362. //update multimeshes
  363. for (Map<int, List<Pair<Transform, IndexKey> > >::Element *E = multimesh_items.front(); E; E = E->next()) {
  364. Octant::MultimeshInstance mmi;
  365. RID mm = VS::get_singleton()->multimesh_create();
  366. VS::get_singleton()->multimesh_allocate(mm, E->get().size(), VS::MULTIMESH_TRANSFORM_3D, VS::MULTIMESH_COLOR_NONE);
  367. VS::get_singleton()->multimesh_set_mesh(mm, theme->get_item_mesh(E->key())->get_rid());
  368. int idx = 0;
  369. for (List<Pair<Transform, IndexKey> >::Element *F = E->get().front(); F; F = F->next()) {
  370. VS::get_singleton()->multimesh_instance_set_transform(mm, idx, F->get().first);
  371. #ifdef TOOLS_ENABLED
  372. Octant::MultimeshInstance::Item it;
  373. it.index = idx;
  374. it.transform = F->get().first;
  375. it.key = F->get().second;
  376. mmi.items.push_back(it);
  377. #endif
  378. idx++;
  379. }
  380. RID instance = VS::get_singleton()->instance_create();
  381. VS::get_singleton()->instance_set_base(instance, mm);
  382. if (is_inside_tree()) {
  383. VS::get_singleton()->instance_set_scenario(instance, get_world()->get_scenario());
  384. VS::get_singleton()->instance_set_transform(instance, get_global_transform());
  385. }
  386. mmi.multimesh = mm;
  387. mmi.instance = instance;
  388. g.multimesh_instances.push_back(mmi);
  389. }
  390. if (col_debug.size()) {
  391. Array arr;
  392. arr.resize(VS::ARRAY_MAX);
  393. arr[VS::ARRAY_VERTEX] = col_debug;
  394. VS::get_singleton()->mesh_add_surface_from_arrays(g.collision_debug, VS::PRIMITIVE_LINES, arr);
  395. SceneTree *st = SceneTree::get_singleton();
  396. if (st) {
  397. VS::get_singleton()->mesh_surface_set_material(g.collision_debug, 0, st->get_debug_collision_material()->get_rid());
  398. }
  399. }
  400. g.dirty = false;
  401. return false;
  402. }
  403. void GridMap::_octant_enter_world(const OctantKey &p_key) {
  404. ERR_FAIL_COND(!octant_map.has(p_key));
  405. Octant &g = *octant_map[p_key];
  406. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  407. PhysicsServer::get_singleton()->body_set_space(g.static_body, get_world()->get_space());
  408. //print_line("BODYPOS: "+get_global_transform());
  409. if (g.collision_debug_instance.is_valid()) {
  410. VS::get_singleton()->instance_set_scenario(g.collision_debug_instance, get_world()->get_scenario());
  411. VS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  412. }
  413. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  414. VS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, get_world()->get_scenario());
  415. VS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  416. }
  417. if (navigation && theme.is_valid()) {
  418. for (Map<IndexKey, Octant::NavMesh>::Element *F = g.navmesh_ids.front(); F; F = F->next()) {
  419. if (cell_map.has(F->key()) && F->get().id < 0) {
  420. Ref<NavigationMesh> nm = theme->get_item_navmesh(cell_map[F->key()].item);
  421. if (nm.is_valid()) {
  422. F->get().id = navigation->navmesh_create(nm, F->get().xform, this);
  423. }
  424. }
  425. }
  426. }
  427. }
  428. void GridMap::_octant_exit_world(const OctantKey &p_key) {
  429. ERR_FAIL_COND(!octant_map.has(p_key));
  430. Octant &g = *octant_map[p_key];
  431. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  432. PhysicsServer::get_singleton()->body_set_space(g.static_body, RID());
  433. if (g.collision_debug_instance.is_valid()) {
  434. VS::get_singleton()->instance_set_scenario(g.collision_debug_instance, RID());
  435. }
  436. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  437. VS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, RID());
  438. }
  439. if (navigation) {
  440. for (Map<IndexKey, Octant::NavMesh>::Element *F = g.navmesh_ids.front(); F; F = F->next()) {
  441. if (F->get().id >= 0) {
  442. navigation->navmesh_remove(F->get().id);
  443. F->get().id = -1;
  444. }
  445. }
  446. }
  447. }
  448. void GridMap::_octant_clean_up(const OctantKey &p_key) {
  449. ERR_FAIL_COND(!octant_map.has(p_key));
  450. Octant &g = *octant_map[p_key];
  451. if (g.collision_debug.is_valid())
  452. VS::get_singleton()->free(g.collision_debug);
  453. if (g.collision_debug_instance.is_valid())
  454. VS::get_singleton()->free(g.collision_debug_instance);
  455. PhysicsServer::get_singleton()->free(g.static_body);
  456. //erase navigation
  457. if (navigation) {
  458. for (Map<IndexKey, Octant::NavMesh>::Element *E = g.navmesh_ids.front(); E; E = E->next()) {
  459. navigation->navmesh_remove(E->get().id);
  460. }
  461. g.navmesh_ids.clear();
  462. }
  463. //erase multimeshes
  464. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  465. VS::get_singleton()->free(g.multimesh_instances[i].instance);
  466. VS::get_singleton()->free(g.multimesh_instances[i].multimesh);
  467. }
  468. g.multimesh_instances.clear();
  469. }
  470. void GridMap::_notification(int p_what) {
  471. switch (p_what) {
  472. case NOTIFICATION_ENTER_WORLD: {
  473. Spatial *c = this;
  474. while (c) {
  475. navigation = Object::cast_to<Navigation>(c);
  476. if (navigation) {
  477. break;
  478. }
  479. c = Object::cast_to<Spatial>(c->get_parent());
  480. }
  481. last_transform = get_global_transform();
  482. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  483. _octant_enter_world(E->key());
  484. }
  485. } break;
  486. case NOTIFICATION_TRANSFORM_CHANGED: {
  487. Transform new_xform = get_global_transform();
  488. if (new_xform == last_transform)
  489. break;
  490. //update run
  491. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  492. _octant_transform(E->key());
  493. }
  494. last_transform = new_xform;
  495. } break;
  496. case NOTIFICATION_EXIT_WORLD: {
  497. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  498. _octant_exit_world(E->key());
  499. }
  500. navigation = NULL;
  501. //_queue_octants_dirty(MAP_DIRTY_INSTANCES|MAP_DIRTY_TRANSFORMS);
  502. //_update_octants_callback();
  503. //_update_area_instances();
  504. } break;
  505. }
  506. }
  507. void GridMap::_queue_octants_dirty() {
  508. if (awaiting_update)
  509. return;
  510. MessageQueue::get_singleton()->push_call(this, "_update_octants_callback");
  511. awaiting_update = true;
  512. }
  513. void GridMap::_recreate_octant_data() {
  514. Map<IndexKey, Cell> cell_copy = cell_map;
  515. _clear_internal();
  516. for (Map<IndexKey, Cell>::Element *E = cell_copy.front(); E; E = E->next()) {
  517. set_cell_item(E->key().x, E->key().y, E->key().z, E->get().item, E->get().rot);
  518. }
  519. }
  520. void GridMap::_clear_internal() {
  521. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  522. if (is_inside_world())
  523. _octant_exit_world(E->key());
  524. _octant_clean_up(E->key());
  525. memdelete(E->get());
  526. }
  527. octant_map.clear();
  528. cell_map.clear();
  529. }
  530. void GridMap::clear() {
  531. _clear_internal();
  532. }
  533. void GridMap::resource_changed(const RES &p_res) {
  534. _recreate_octant_data();
  535. }
  536. void GridMap::_update_octants_callback() {
  537. if (!awaiting_update)
  538. return;
  539. List<OctantKey> to_delete;
  540. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  541. if (_octant_update(E->key())) {
  542. to_delete.push_back(E->key());
  543. }
  544. }
  545. while (to_delete.front()) {
  546. memdelete(octant_map[to_delete.front()->get()]);
  547. octant_map.erase(to_delete.front()->get());
  548. to_delete.pop_back();
  549. }
  550. awaiting_update = false;
  551. }
  552. void GridMap::_bind_methods() {
  553. ClassDB::bind_method(D_METHOD("set_theme", "theme"), &GridMap::set_theme);
  554. ClassDB::bind_method(D_METHOD("get_theme"), &GridMap::get_theme);
  555. ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &GridMap::set_cell_size);
  556. ClassDB::bind_method(D_METHOD("get_cell_size"), &GridMap::get_cell_size);
  557. ClassDB::bind_method(D_METHOD("set_octant_size", "size"), &GridMap::set_octant_size);
  558. ClassDB::bind_method(D_METHOD("get_octant_size"), &GridMap::get_octant_size);
  559. ClassDB::bind_method(D_METHOD("set_cell_item", "x", "y", "z", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0));
  560. ClassDB::bind_method(D_METHOD("get_cell_item", "x", "y", "z"), &GridMap::get_cell_item);
  561. ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "x", "y", "z"), &GridMap::get_cell_item_orientation);
  562. //ClassDB::bind_method(D_METHOD("_recreate_octants"),&GridMap::_recreate_octants);
  563. ClassDB::bind_method(D_METHOD("_update_octants_callback"), &GridMap::_update_octants_callback);
  564. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed);
  565. ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &GridMap::set_center_x);
  566. ClassDB::bind_method(D_METHOD("get_center_x"), &GridMap::get_center_x);
  567. ClassDB::bind_method(D_METHOD("set_center_y", "enable"), &GridMap::set_center_y);
  568. ClassDB::bind_method(D_METHOD("get_center_y"), &GridMap::get_center_y);
  569. ClassDB::bind_method(D_METHOD("set_center_z", "enable"), &GridMap::set_center_z);
  570. ClassDB::bind_method(D_METHOD("get_center_z"), &GridMap::get_center_z);
  571. ClassDB::bind_method(D_METHOD("set_clip", "enabled", "clipabove", "floor", "axis"), &GridMap::set_clip, DEFVAL(true), DEFVAL(0), DEFVAL(Vector3::AXIS_X));
  572. ClassDB::bind_method(D_METHOD("clear"), &GridMap::clear);
  573. ClassDB::bind_method(D_METHOD("get_meshes"), &GridMap::get_meshes);
  574. BIND_CONSTANT(INVALID_CELL_ITEM);
  575. }
  576. void GridMap::set_clip(bool p_enabled, bool p_clip_above, int p_floor, Vector3::Axis p_axis) {
  577. if (!p_enabled && !clip)
  578. return;
  579. if (clip && p_enabled && clip_floor == p_floor && p_clip_above == clip_above && p_axis == clip_axis)
  580. return;
  581. clip = p_enabled;
  582. clip_floor = p_floor;
  583. clip_axis = p_axis;
  584. clip_above = p_clip_above;
  585. //make it all update
  586. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  587. Octant *g = E->get();
  588. g->dirty = true;
  589. }
  590. awaiting_update = true;
  591. _update_octants_callback();
  592. }
  593. void GridMap::set_cell_scale(float p_scale) {
  594. cell_scale = p_scale;
  595. _queue_octants_dirty();
  596. }
  597. float GridMap::get_cell_scale() const {
  598. return cell_scale;
  599. }
  600. Array GridMap::get_meshes() {
  601. if (theme.is_null())
  602. return Array();
  603. Vector3 ofs(cell_size.x * 0.5 * int(center_x), cell_size.y * 0.5 * int(center_y), cell_size.z * 0.5 * int(center_z));
  604. Array meshes;
  605. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
  606. int id = E->get().item;
  607. if (!theme->has_item(id))
  608. continue;
  609. Ref<Mesh> mesh = theme->get_item_mesh(id);
  610. if (mesh.is_null())
  611. continue;
  612. IndexKey ik = E->key();
  613. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  614. Transform xform;
  615. xform.basis.set_orthogonal_index(E->get().rot);
  616. xform.set_origin(cellpos * cell_size + ofs);
  617. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  618. meshes.push_back(xform);
  619. meshes.push_back(mesh);
  620. }
  621. return meshes;
  622. }
  623. GridMap::GridMap() {
  624. cell_size = Vector3(2, 2, 2);
  625. octant_size = 4;
  626. awaiting_update = false;
  627. _in_tree = false;
  628. center_x = true;
  629. center_y = true;
  630. center_z = true;
  631. clip = false;
  632. clip_floor = 0;
  633. clip_axis = Vector3::AXIS_Z;
  634. clip_above = true;
  635. cell_scale = 1.0;
  636. navigation = NULL;
  637. set_notify_transform(true);
  638. }
  639. GridMap::~GridMap() {
  640. if (!theme.is_null())
  641. theme->unregister_owner(this);
  642. clear();
  643. }