grid_map.cpp 24 KB

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