grid_map.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /*************************************************************************/
  2. /* grid_map.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.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. set_cell_size(p_value);
  45. } else if (name == "cell_octant_size") {
  46. set_octant_size(p_value);
  47. } else if (name == "cell_center_x") {
  48. set_center_x(p_value);
  49. } else if (name == "cell_center_y") {
  50. set_center_y(p_value);
  51. } else if (name == "cell_center_z") {
  52. set_center_z(p_value);
  53. } else if (name == "cell_scale") {
  54. set_cell_scale(p_value);
  55. /* } else if (name=="cells") {
  56. PoolVector<int> cells = p_value;
  57. int amount=cells.size();
  58. PoolVector<int>::Read r = cells.read();
  59. ERR_FAIL_COND_V(amount&1,false); // not even
  60. cell_map.clear();
  61. for(int i=0;i<amount/3;i++) {
  62. IndexKey ik;
  63. ik.key=decode_uint64(&r[i*3]);
  64. Cell cell;
  65. cell.cell=uint32_t(r[i*+1]);
  66. cell_map[ik]=cell;
  67. }
  68. _recreate_octant_data();*/
  69. } else if (name == "data") {
  70. Dictionary d = p_value;
  71. if (d.has("cells")) {
  72. PoolVector<int> cells = d["cells"];
  73. int amount = cells.size();
  74. PoolVector<int>::Read r = cells.read();
  75. ERR_FAIL_COND_V(amount % 3, false); // not even
  76. cell_map.clear();
  77. for (int i = 0; i < amount / 3; i++) {
  78. IndexKey ik;
  79. ik.key = decode_uint64((const uint8_t *)&r[i * 3]);
  80. Cell cell;
  81. cell.cell = decode_uint32((const uint8_t *)&r[i * 3 + 2]);
  82. cell_map[ik] = cell;
  83. }
  84. }
  85. _recreate_octant_data();
  86. } else
  87. return false;
  88. return true;
  89. }
  90. bool GridMap::_get(const StringName &p_name, Variant &r_ret) const {
  91. String name = p_name;
  92. if (name == "theme") {
  93. r_ret = get_theme();
  94. } else if (name == "cell_size") {
  95. r_ret = get_cell_size();
  96. } else if (name == "cell_octant_size") {
  97. r_ret = get_octant_size();
  98. } else if (name == "cell_center_x") {
  99. r_ret = get_center_x();
  100. } else if (name == "cell_center_y") {
  101. r_ret = get_center_y();
  102. } else if (name == "cell_center_z") {
  103. r_ret = get_center_z();
  104. } else if (name == "cell_scale") {
  105. r_ret = cell_scale;
  106. } else if (name == "data") {
  107. Dictionary d;
  108. PoolVector<int> cells;
  109. cells.resize(cell_map.size() * 3);
  110. {
  111. PoolVector<int>::Write w = cells.write();
  112. int i = 0;
  113. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next(), i++) {
  114. encode_uint64(E->key().key, (uint8_t *)&w[i * 3]);
  115. encode_uint32(E->get().cell, (uint8_t *)&w[i * 3 + 2]);
  116. }
  117. }
  118. d["cells"] = cells;
  119. r_ret = d;
  120. } else
  121. return false;
  122. return true;
  123. }
  124. void GridMap::_get_property_list(List<PropertyInfo> *p_list) const {
  125. p_list->push_back(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, "MeshLibrary"));
  126. p_list->push_back(PropertyInfo(Variant::NIL, "Cell", PROPERTY_HINT_NONE, "cell_", PROPERTY_USAGE_GROUP));
  127. p_list->push_back(PropertyInfo(Variant::REAL, "cell_size", PROPERTY_HINT_RANGE, "0.01,16384,0.01"));
  128. p_list->push_back(PropertyInfo(Variant::INT, "cell_octant_size", PROPERTY_HINT_RANGE, "1,1024,1"));
  129. p_list->push_back(PropertyInfo(Variant::BOOL, "cell_center_x"));
  130. p_list->push_back(PropertyInfo(Variant::BOOL, "cell_center_y"));
  131. p_list->push_back(PropertyInfo(Variant::BOOL, "cell_center_z"));
  132. p_list->push_back(PropertyInfo(Variant::REAL, "cell_scale"));
  133. p_list->push_back(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  134. }
  135. void GridMap::set_theme(const Ref<MeshLibrary> &p_theme) {
  136. if (!theme.is_null())
  137. theme->unregister_owner(this);
  138. theme = p_theme;
  139. if (!theme.is_null())
  140. theme->register_owner(this);
  141. _recreate_octant_data();
  142. _change_notify("theme");
  143. }
  144. Ref<MeshLibrary> GridMap::get_theme() const {
  145. return theme;
  146. }
  147. void GridMap::set_cell_size(float p_size) {
  148. cell_size = p_size;
  149. _recreate_octant_data();
  150. }
  151. float GridMap::get_cell_size() const {
  152. return cell_size;
  153. }
  154. void GridMap::set_octant_size(int p_size) {
  155. octant_size = p_size;
  156. _recreate_octant_data();
  157. }
  158. int GridMap::get_octant_size() const {
  159. return octant_size;
  160. }
  161. void GridMap::set_center_x(bool p_enable) {
  162. center_x = p_enable;
  163. _recreate_octant_data();
  164. }
  165. bool GridMap::get_center_x() const {
  166. return center_x;
  167. }
  168. void GridMap::set_center_y(bool p_enable) {
  169. center_y = p_enable;
  170. _recreate_octant_data();
  171. }
  172. bool GridMap::get_center_y() const {
  173. return center_y;
  174. }
  175. void GridMap::set_center_z(bool p_enable) {
  176. center_z = p_enable;
  177. _recreate_octant_data();
  178. }
  179. bool GridMap::get_center_z() const {
  180. return center_z;
  181. }
  182. void GridMap::set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_rot) {
  183. ERR_FAIL_INDEX(ABS(p_x), 1 << 20);
  184. ERR_FAIL_INDEX(ABS(p_y), 1 << 20);
  185. ERR_FAIL_INDEX(ABS(p_z), 1 << 20);
  186. IndexKey key;
  187. key.x = p_x;
  188. key.y = p_y;
  189. key.z = p_z;
  190. OctantKey ok;
  191. ok.x = p_x / octant_size;
  192. ok.y = p_y / octant_size;
  193. ok.z = p_z / octant_size;
  194. if (cell_map.has(key)) {
  195. int prev_item = cell_map[key].item;
  196. OctantKey octantkey = ok;
  197. ERR_FAIL_COND(!octant_map.has(octantkey));
  198. Octant &g = *octant_map[octantkey];
  199. ERR_FAIL_COND(!g.items.has(prev_item));
  200. ERR_FAIL_COND(!g.items[prev_item].cells.has(key));
  201. g.items[prev_item].cells.erase(key);
  202. if (g.items[prev_item].cells.size() == 0) {
  203. VS::get_singleton()->free(g.items[prev_item].multimesh_instance);
  204. g.items.erase(prev_item);
  205. }
  206. if (g.items.empty()) {
  207. PhysicsServer::get_singleton()->free(g.static_body);
  208. if (g.collision_debug.is_valid()) {
  209. PhysicsServer::get_singleton()->free(g.collision_debug);
  210. PhysicsServer::get_singleton()->free(g.collision_debug_instance);
  211. }
  212. memdelete(&g);
  213. octant_map.erase(octantkey);
  214. } else {
  215. g.dirty = true;
  216. }
  217. cell_map.erase(key);
  218. _queue_dirty_map();
  219. }
  220. if (p_item < 0)
  221. return;
  222. OctantKey octantkey = ok;
  223. //add later
  224. if (!octant_map.has(octantkey)) {
  225. Octant *g = memnew(Octant);
  226. g->dirty = true;
  227. g->static_body = PhysicsServer::get_singleton()->body_create(PhysicsServer::BODY_MODE_STATIC);
  228. PhysicsServer::get_singleton()->body_attach_object_instance_id(g->static_body, get_instance_id());
  229. if (is_inside_world())
  230. PhysicsServer::get_singleton()->body_set_space(g->static_body, get_world()->get_space());
  231. SceneTree *st = SceneTree::get_singleton();
  232. if (st && st->is_debugging_collisions_hint()) {
  233. g->collision_debug = VisualServer::get_singleton()->mesh_create();
  234. g->collision_debug_instance = VisualServer::get_singleton()->instance_create();
  235. VisualServer::get_singleton()->instance_set_base(g->collision_debug_instance, g->collision_debug);
  236. if (is_inside_world()) {
  237. VisualServer::get_singleton()->instance_set_scenario(g->collision_debug_instance, get_world()->get_scenario());
  238. VisualServer::get_singleton()->instance_set_transform(g->collision_debug_instance, get_global_transform());
  239. }
  240. }
  241. octant_map[octantkey] = g;
  242. }
  243. Octant &g = *octant_map[octantkey];
  244. if (!g.items.has(p_item)) {
  245. Octant::ItemInstances ii;
  246. if (theme.is_valid() && theme->has_item(p_item)) {
  247. ii.mesh = theme->get_item_mesh(p_item);
  248. ii.shape = theme->get_item_shape(p_item);
  249. ii.navmesh = theme->get_item_navmesh(p_item);
  250. }
  251. ii.multimesh = Ref<MultiMesh>(memnew(MultiMesh));
  252. ii.multimesh->set_color_format(MultiMesh::COLOR_NONE);
  253. ii.multimesh->set_transform_format(MultiMesh::TRANSFORM_3D);
  254. ii.multimesh->set_mesh(ii.mesh);
  255. ii.multimesh_instance = VS::get_singleton()->instance_create();
  256. VS::get_singleton()->instance_set_base(ii.multimesh_instance, ii.multimesh->get_rid());
  257. VS::get_singleton()->instance_geometry_set_flag(ii.multimesh_instance, VS::INSTANCE_FLAG_USE_BAKED_LIGHT, true);
  258. g.items[p_item] = ii;
  259. }
  260. Octant::ItemInstances &ii = g.items[p_item];
  261. ii.cells.insert(key);
  262. g.dirty = true;
  263. _queue_dirty_map();
  264. cell_map[key] = Cell();
  265. Cell &c = cell_map[key];
  266. c.item = p_item;
  267. c.rot = p_rot;
  268. }
  269. int GridMap::get_cell_item(int p_x, int p_y, int p_z) const {
  270. ERR_FAIL_INDEX_V(ABS(p_x), 1 << 20, INVALID_CELL_ITEM);
  271. ERR_FAIL_INDEX_V(ABS(p_y), 1 << 20, INVALID_CELL_ITEM);
  272. ERR_FAIL_INDEX_V(ABS(p_z), 1 << 20, INVALID_CELL_ITEM);
  273. IndexKey key;
  274. key.x = p_x;
  275. key.y = p_y;
  276. key.z = p_z;
  277. if (!cell_map.has(key))
  278. return INVALID_CELL_ITEM;
  279. return cell_map[key].item;
  280. }
  281. int GridMap::get_cell_item_orientation(int p_x, int p_y, int p_z) const {
  282. ERR_FAIL_INDEX_V(ABS(p_x), 1 << 20, -1);
  283. ERR_FAIL_INDEX_V(ABS(p_y), 1 << 20, -1);
  284. ERR_FAIL_INDEX_V(ABS(p_z), 1 << 20, -1);
  285. IndexKey key;
  286. key.x = p_x;
  287. key.y = p_y;
  288. key.z = p_z;
  289. if (!cell_map.has(key))
  290. return -1;
  291. return cell_map[key].rot;
  292. }
  293. void GridMap::_octant_enter_tree(const OctantKey &p_key) {
  294. ERR_FAIL_COND(!octant_map.has(p_key));
  295. if (navigation) {
  296. Octant &g = *octant_map[p_key];
  297. Vector3 ofs(cell_size * 0.5 * int(center_x), cell_size * 0.5 * int(center_y), cell_size * 0.5 * int(center_z));
  298. _octant_clear_navmesh(p_key);
  299. for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
  300. Octant::ItemInstances &ii = E->get();
  301. for (Set<IndexKey>::Element *F = ii.cells.front(); F; F = F->next()) {
  302. IndexKey ik = F->get();
  303. Map<IndexKey, Cell>::Element *C = cell_map.find(ik);
  304. ERR_CONTINUE(!C);
  305. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  306. Transform xform;
  307. if (clip && ((clip_above && cellpos[clip_axis] > clip_floor) || (!clip_above && cellpos[clip_axis] < clip_floor))) {
  308. xform.basis.set_zero();
  309. } else {
  310. xform.basis.set_orthogonal_index(C->get().rot);
  311. }
  312. xform.set_origin(cellpos * cell_size + ofs);
  313. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  314. // add the item's navmesh at given xform to GridMap's Navigation ancestor
  315. if (ii.navmesh.is_valid()) {
  316. int nm_id = navigation->navmesh_create(ii.navmesh, xform, this);
  317. Octant::NavMesh nm;
  318. nm.id = nm_id;
  319. nm.xform = xform;
  320. g.navmesh_ids[ik] = nm;
  321. }
  322. }
  323. }
  324. }
  325. }
  326. void GridMap::_octant_enter_world(const OctantKey &p_key) {
  327. ERR_FAIL_COND(!octant_map.has(p_key));
  328. Octant &g = *octant_map[p_key];
  329. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  330. PhysicsServer::get_singleton()->body_set_space(g.static_body, get_world()->get_space());
  331. //print_line("BODYPOS: "+get_global_transform());
  332. if (g.collision_debug_instance.is_valid()) {
  333. VS::get_singleton()->instance_set_scenario(g.collision_debug_instance, get_world()->get_scenario());
  334. VS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  335. }
  336. for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
  337. VS::get_singleton()->instance_set_scenario(E->get().multimesh_instance, get_world()->get_scenario());
  338. VS::get_singleton()->instance_set_transform(E->get().multimesh_instance, get_global_transform());
  339. //print_line("INSTANCEPOS: "+get_global_transform());
  340. }
  341. }
  342. void GridMap::_octant_transform(const OctantKey &p_key) {
  343. ERR_FAIL_COND(!octant_map.has(p_key));
  344. Octant &g = *octant_map[p_key];
  345. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  346. if (g.collision_debug_instance.is_valid()) {
  347. VS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  348. }
  349. for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
  350. VS::get_singleton()->instance_set_transform(E->get().multimesh_instance, get_global_transform());
  351. //print_line("UPDATEPOS: "+get_global_transform());
  352. }
  353. }
  354. void GridMap::_octant_clear_navmesh(const OctantKey &p_key) {
  355. Octant &g = *octant_map[p_key];
  356. if (navigation) {
  357. for (Map<IndexKey, Octant::NavMesh>::Element *E = g.navmesh_ids.front(); E; E = E->next()) {
  358. Octant::NavMesh *nvm = &E->get();
  359. if (nvm && nvm->id) {
  360. navigation->navmesh_remove(E->get().id);
  361. }
  362. }
  363. g.navmesh_ids.clear();
  364. }
  365. }
  366. void GridMap::_octant_update(const OctantKey &p_key) {
  367. ERR_FAIL_COND(!octant_map.has(p_key));
  368. Octant &g = *octant_map[p_key];
  369. if (!g.dirty)
  370. return;
  371. Ref<Mesh> mesh;
  372. _octant_clear_navmesh(p_key);
  373. PhysicsServer::get_singleton()->body_clear_shapes(g.static_body);
  374. if (g.collision_debug.is_valid()) {
  375. VS::get_singleton()->mesh_clear(g.collision_debug);
  376. }
  377. PoolVector<Vector3> col_debug;
  378. /*
  379. * foreach item in this octant,
  380. * set item's multimesh's instance count to number of cells which have this item
  381. * and set said multimesh bounding box to one containing all cells which have this item
  382. */
  383. for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
  384. Octant::ItemInstances &ii = E->get();
  385. ii.multimesh->set_instance_count(ii.cells.size());
  386. Rect3 aabb;
  387. Rect3 mesh_aabb = ii.mesh.is_null() ? Rect3() : ii.mesh->get_aabb();
  388. Vector3 ofs(cell_size * 0.5 * int(center_x), cell_size * 0.5 * int(center_y), cell_size * 0.5 * int(center_z));
  389. //print_line("OCTANT, CELLS: "+itos(ii.cells.size()));
  390. int idx = 0;
  391. // foreach cell containing this item type
  392. for (Set<IndexKey>::Element *F = ii.cells.front(); F; F = F->next()) {
  393. IndexKey ik = F->get();
  394. Map<IndexKey, Cell>::Element *C = cell_map.find(ik);
  395. ERR_CONTINUE(!C);
  396. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  397. Transform xform;
  398. if (clip && ((clip_above && cellpos[clip_axis] > clip_floor) || (!clip_above && cellpos[clip_axis] < clip_floor))) {
  399. xform.basis.set_zero();
  400. } else {
  401. xform.basis.set_orthogonal_index(C->get().rot);
  402. }
  403. xform.set_origin(cellpos * cell_size + ofs);
  404. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  405. ii.multimesh->set_instance_transform(idx, xform);
  406. //ii.multimesh->set_instance_transform(idx,Transform() );
  407. //ii.multimesh->set_instance_color(idx,Color(1,1,1,1));
  408. //print_line("MMINST: "+xform);
  409. if (idx == 0) {
  410. aabb = xform.xform(mesh_aabb);
  411. } else {
  412. aabb.merge_with(xform.xform(mesh_aabb));
  413. }
  414. // add the item's shape at given xform to octant's static_body
  415. if (ii.shape.is_valid()) {
  416. // add the item's shape
  417. PhysicsServer::get_singleton()->body_add_shape(g.static_body, ii.shape->get_rid(), xform);
  418. if (g.collision_debug.is_valid()) {
  419. ii.shape->add_vertices_to_array(col_debug, xform);
  420. }
  421. //print_line("PHIS x: "+xform);
  422. }
  423. // add the item's navmesh at given xform to GridMap's Navigation ancestor
  424. if (navigation) {
  425. if (ii.navmesh.is_valid()) {
  426. int nm_id = navigation->navmesh_create(ii.navmesh, xform, this);
  427. Octant::NavMesh nm;
  428. nm.id = nm_id;
  429. nm.xform = xform;
  430. g.navmesh_ids[ik] = nm;
  431. }
  432. }
  433. idx++;
  434. }
  435. //ii.multimesh->set_aabb(aabb);
  436. }
  437. if (col_debug.size()) {
  438. Array arr;
  439. arr.resize(VS::ARRAY_MAX);
  440. arr[VS::ARRAY_VERTEX] = col_debug;
  441. VS::get_singleton()->mesh_add_surface_from_arrays(g.collision_debug, VS::PRIMITIVE_LINES, arr);
  442. SceneTree *st = SceneTree::get_singleton();
  443. if (st) {
  444. VS::get_singleton()->mesh_surface_set_material(g.collision_debug, 0, st->get_debug_collision_material()->get_rid());
  445. }
  446. }
  447. g.dirty = false;
  448. }
  449. void GridMap::_octant_exit_world(const OctantKey &p_key) {
  450. ERR_FAIL_COND(!octant_map.has(p_key));
  451. Octant &g = *octant_map[p_key];
  452. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  453. PhysicsServer::get_singleton()->body_set_space(g.static_body, RID());
  454. if (g.collision_debug_instance.is_valid()) {
  455. VS::get_singleton()->instance_set_scenario(g.collision_debug_instance, RID());
  456. }
  457. for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
  458. VS::get_singleton()->instance_set_scenario(E->get().multimesh_instance, RID());
  459. //VS::get_singleton()->instance_set_transform(E->get().multimesh_instance,get_global_transform());
  460. }
  461. }
  462. void GridMap::_notification(int p_what) {
  463. switch (p_what) {
  464. case NOTIFICATION_ENTER_WORLD: {
  465. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  466. //IndexKey ik;
  467. //ik.key = E->key().indexkey;
  468. _octant_enter_world(E->key());
  469. _octant_update(E->key());
  470. }
  471. awaiting_update = false;
  472. last_transform = get_global_transform();
  473. } break;
  474. case NOTIFICATION_TRANSFORM_CHANGED: {
  475. Transform new_xform = get_global_transform();
  476. if (new_xform == last_transform)
  477. break;
  478. //update run
  479. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  480. _octant_transform(E->key());
  481. }
  482. last_transform = new_xform;
  483. } break;
  484. case NOTIFICATION_EXIT_WORLD: {
  485. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  486. _octant_exit_world(E->key());
  487. }
  488. //_queue_dirty_map(MAP_DIRTY_INSTANCES|MAP_DIRTY_TRANSFORMS);
  489. //_update_dirty_map_callback();
  490. //_update_area_instances();
  491. } break;
  492. case NOTIFICATION_ENTER_TREE: {
  493. Spatial *c = this;
  494. while (c) {
  495. navigation = Object::cast_to<Navigation>(c);
  496. if (navigation) {
  497. break;
  498. }
  499. c = Object::cast_to<Spatial>(c->get_parent());
  500. }
  501. if (navigation) {
  502. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  503. if (navigation) {
  504. _octant_enter_tree(E->key());
  505. }
  506. }
  507. }
  508. _queue_dirty_map();
  509. } break;
  510. case NOTIFICATION_EXIT_TREE: {
  511. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  512. if (navigation) {
  513. _octant_clear_navmesh(E->key());
  514. }
  515. }
  516. navigation = NULL;
  517. } break;
  518. }
  519. }
  520. void GridMap::_queue_dirty_map() {
  521. if (awaiting_update)
  522. return;
  523. if (is_inside_world()) {
  524. MessageQueue::get_singleton()->push_call(this, "_update_dirty_map_callback");
  525. awaiting_update = true;
  526. }
  527. }
  528. void GridMap::_recreate_octant_data() {
  529. Map<IndexKey, Cell> cell_copy = cell_map;
  530. _clear_internal();
  531. for (Map<IndexKey, Cell>::Element *E = cell_copy.front(); E; E = E->next()) {
  532. set_cell_item(E->key().x, E->key().y, E->key().z, E->get().item, E->get().rot);
  533. }
  534. }
  535. void GridMap::_clear_internal() {
  536. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  537. if (is_inside_world())
  538. _octant_exit_world(E->key());
  539. for (Map<int, Octant::ItemInstances>::Element *F = E->get()->items.front(); F; F = F->next()) {
  540. VS::get_singleton()->free(F->get().multimesh_instance);
  541. }
  542. if (E->get()->collision_debug.is_valid())
  543. VS::get_singleton()->free(E->get()->collision_debug);
  544. if (E->get()->collision_debug_instance.is_valid())
  545. VS::get_singleton()->free(E->get()->collision_debug_instance);
  546. PhysicsServer::get_singleton()->free(E->get()->static_body);
  547. memdelete(E->get());
  548. }
  549. octant_map.clear();
  550. cell_map.clear();
  551. }
  552. void GridMap::clear() {
  553. _clear_internal();
  554. }
  555. void GridMap::resource_changed(const RES &p_res) {
  556. _recreate_octant_data();
  557. }
  558. void GridMap::_update_dirty_map_callback() {
  559. if (!awaiting_update)
  560. return;
  561. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  562. _octant_update(E->key());
  563. }
  564. awaiting_update = false;
  565. }
  566. void GridMap::_bind_methods() {
  567. ClassDB::bind_method(D_METHOD("set_theme", "theme"), &GridMap::set_theme);
  568. ClassDB::bind_method(D_METHOD("get_theme"), &GridMap::get_theme);
  569. ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &GridMap::set_cell_size);
  570. ClassDB::bind_method(D_METHOD("get_cell_size"), &GridMap::get_cell_size);
  571. ClassDB::bind_method(D_METHOD("set_octant_size", "size"), &GridMap::set_octant_size);
  572. ClassDB::bind_method(D_METHOD("get_octant_size"), &GridMap::get_octant_size);
  573. ClassDB::bind_method(D_METHOD("set_cell_item", "x", "y", "z", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0));
  574. ClassDB::bind_method(D_METHOD("get_cell_item", "x", "y", "z"), &GridMap::get_cell_item);
  575. ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "x", "y", "z"), &GridMap::get_cell_item_orientation);
  576. //ClassDB::bind_method(D_METHOD("_recreate_octants"),&GridMap::_recreate_octants);
  577. ClassDB::bind_method(D_METHOD("_update_dirty_map_callback"), &GridMap::_update_dirty_map_callback);
  578. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed);
  579. ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &GridMap::set_center_x);
  580. ClassDB::bind_method(D_METHOD("get_center_x"), &GridMap::get_center_x);
  581. ClassDB::bind_method(D_METHOD("set_center_y", "enable"), &GridMap::set_center_y);
  582. ClassDB::bind_method(D_METHOD("get_center_y"), &GridMap::get_center_y);
  583. ClassDB::bind_method(D_METHOD("set_center_z", "enable"), &GridMap::set_center_z);
  584. ClassDB::bind_method(D_METHOD("get_center_z"), &GridMap::get_center_z);
  585. ClassDB::bind_method(D_METHOD("set_clip", "enabled", "clipabove", "floor", "axis"), &GridMap::set_clip, DEFVAL(true), DEFVAL(0), DEFVAL(Vector3::AXIS_X));
  586. ClassDB::bind_method(D_METHOD("clear"), &GridMap::clear);
  587. ClassDB::bind_method(D_METHOD("get_meshes"), &GridMap::get_meshes);
  588. BIND_CONSTANT(INVALID_CELL_ITEM);
  589. }
  590. void GridMap::set_clip(bool p_enabled, bool p_clip_above, int p_floor, Vector3::Axis p_axis) {
  591. if (!p_enabled && !clip)
  592. return;
  593. if (clip && p_enabled && clip_floor == p_floor && p_clip_above == clip_above && p_axis == clip_axis)
  594. return;
  595. clip = p_enabled;
  596. clip_floor = p_floor;
  597. clip_axis = p_axis;
  598. clip_above = p_clip_above;
  599. //make it all update
  600. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  601. Octant *g = E->get();
  602. g->dirty = true;
  603. }
  604. awaiting_update = true;
  605. _update_dirty_map_callback();
  606. }
  607. void GridMap::set_cell_scale(float p_scale) {
  608. cell_scale = p_scale;
  609. _queue_dirty_map();
  610. }
  611. float GridMap::get_cell_scale() const {
  612. return cell_scale;
  613. }
  614. Array GridMap::get_meshes() {
  615. if (theme.is_null())
  616. return Array();
  617. Vector3 ofs(cell_size * 0.5 * int(center_x), cell_size * 0.5 * int(center_y), cell_size * 0.5 * int(center_z));
  618. Array meshes;
  619. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
  620. int id = E->get().item;
  621. if (!theme->has_item(id))
  622. continue;
  623. Ref<Mesh> mesh = theme->get_item_mesh(id);
  624. if (mesh.is_null())
  625. continue;
  626. IndexKey ik = E->key();
  627. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  628. Transform xform;
  629. xform.basis.set_orthogonal_index(E->get().rot);
  630. xform.set_origin(cellpos * cell_size + ofs);
  631. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  632. meshes.push_back(xform);
  633. meshes.push_back(mesh);
  634. }
  635. return meshes;
  636. }
  637. GridMap::GridMap() {
  638. cell_size = 2;
  639. octant_size = 4;
  640. awaiting_update = false;
  641. _in_tree = false;
  642. center_x = true;
  643. center_y = true;
  644. center_z = true;
  645. clip = false;
  646. clip_floor = 0;
  647. clip_axis = Vector3::AXIS_Z;
  648. clip_above = true;
  649. cell_scale = 1.0;
  650. navigation = NULL;
  651. set_notify_transform(true);
  652. }
  653. GridMap::~GridMap() {
  654. if (!theme.is_null())
  655. theme->unregister_owner(this);
  656. clear();
  657. }