grid_map.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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 if (name.begins_with("areas/")) {
  87. int which = name.get_slicec('/', 1).to_int();
  88. String what = name.get_slicec('/', 2);
  89. if (what == "bounds") {
  90. ERR_FAIL_COND_V(area_map.has(which), false);
  91. create_area(which, p_value);
  92. return true;
  93. }
  94. ERR_FAIL_COND_V(!area_map.has(which), false);
  95. if (what == "name")
  96. area_set_name(which, p_value);
  97. else if (what == "disable_distance")
  98. area_set_portal_disable_distance(which, p_value);
  99. else if (what == "exterior_portal")
  100. area_set_portal_disable_color(which, p_value);
  101. else
  102. return false;
  103. } else
  104. return false;
  105. return true;
  106. }
  107. bool GridMap::_get(const StringName &p_name, Variant &r_ret) const {
  108. String name = p_name;
  109. if (name == "theme") {
  110. r_ret = get_theme();
  111. } else if (name == "cell_size") {
  112. r_ret = get_cell_size();
  113. } else if (name == "cell_octant_size") {
  114. r_ret = get_octant_size();
  115. } else if (name == "cell_center_x") {
  116. r_ret = get_center_x();
  117. } else if (name == "cell_center_y") {
  118. r_ret = get_center_y();
  119. } else if (name == "cell_center_z") {
  120. r_ret = get_center_z();
  121. } else if (name == "cell_scale") {
  122. r_ret = cell_scale;
  123. } else if (name == "data") {
  124. Dictionary d;
  125. PoolVector<int> cells;
  126. cells.resize(cell_map.size() * 3);
  127. {
  128. PoolVector<int>::Write w = cells.write();
  129. int i = 0;
  130. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next(), i++) {
  131. encode_uint64(E->key().key, (uint8_t *)&w[i * 3]);
  132. encode_uint32(E->get().cell, (uint8_t *)&w[i * 3 + 2]);
  133. }
  134. }
  135. d["cells"] = cells;
  136. r_ret = d;
  137. } else if (name.begins_with("areas/")) {
  138. int which = name.get_slicec('/', 1).to_int();
  139. String what = name.get_slicec('/', 2);
  140. if (what == "bounds")
  141. r_ret = area_get_bounds(which);
  142. else if (what == "name")
  143. r_ret = area_get_name(which);
  144. else if (what == "disable_distance")
  145. r_ret = area_get_portal_disable_distance(which);
  146. else if (what == "exterior_portal")
  147. r_ret = area_is_exterior_portal(which);
  148. else
  149. return false;
  150. } else
  151. return false;
  152. return true;
  153. }
  154. void GridMap::_get_property_list(List<PropertyInfo> *p_list) const {
  155. p_list->push_back(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, "MeshLibrary"));
  156. p_list->push_back(PropertyInfo(Variant::NIL, "Cell", PROPERTY_HINT_NONE, "cell_", PROPERTY_USAGE_GROUP));
  157. p_list->push_back(PropertyInfo(Variant::REAL, "cell_size", PROPERTY_HINT_RANGE, "0.01,16384,0.01"));
  158. p_list->push_back(PropertyInfo(Variant::INT, "cell_octant_size", PROPERTY_HINT_RANGE, "1,1024,1"));
  159. p_list->push_back(PropertyInfo(Variant::BOOL, "cell_center_x"));
  160. p_list->push_back(PropertyInfo(Variant::BOOL, "cell_center_y"));
  161. p_list->push_back(PropertyInfo(Variant::BOOL, "cell_center_z"));
  162. p_list->push_back(PropertyInfo(Variant::REAL, "cell_scale"));
  163. p_list->push_back(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  164. for (const Map<int, Area *>::Element *E = area_map.front(); E; E = E->next()) {
  165. String base = "areas/" + itos(E->key()) + "/";
  166. p_list->push_back(PropertyInfo(Variant::RECT3, base + "bounds", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  167. p_list->push_back(PropertyInfo(Variant::STRING, base + "name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  168. p_list->push_back(PropertyInfo(Variant::REAL, base + "disable_distance", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  169. p_list->push_back(PropertyInfo(Variant::COLOR, base + "disable_color", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  170. p_list->push_back(PropertyInfo(Variant::BOOL, base + "exterior_portal", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  171. }
  172. }
  173. void GridMap::set_theme(const Ref<MeshLibrary> &p_theme) {
  174. if (!theme.is_null())
  175. theme->unregister_owner(this);
  176. theme = p_theme;
  177. if (!theme.is_null())
  178. theme->register_owner(this);
  179. _recreate_octant_data();
  180. _change_notify("theme");
  181. }
  182. Ref<MeshLibrary> GridMap::get_theme() const {
  183. return theme;
  184. }
  185. void GridMap::set_cell_size(float p_size) {
  186. cell_size = p_size;
  187. _recreate_octant_data();
  188. }
  189. float GridMap::get_cell_size() const {
  190. return cell_size;
  191. }
  192. void GridMap::set_octant_size(int p_size) {
  193. octant_size = p_size;
  194. _recreate_octant_data();
  195. }
  196. int GridMap::get_octant_size() const {
  197. return octant_size;
  198. }
  199. void GridMap::set_center_x(bool p_enable) {
  200. center_x = p_enable;
  201. _recreate_octant_data();
  202. }
  203. bool GridMap::get_center_x() const {
  204. return center_x;
  205. }
  206. void GridMap::set_center_y(bool p_enable) {
  207. center_y = p_enable;
  208. _recreate_octant_data();
  209. }
  210. bool GridMap::get_center_y() const {
  211. return center_y;
  212. }
  213. void GridMap::set_center_z(bool p_enable) {
  214. center_z = p_enable;
  215. _recreate_octant_data();
  216. }
  217. bool GridMap::get_center_z() const {
  218. return center_z;
  219. }
  220. int GridMap::_find_area(const IndexKey &p_pos) const {
  221. for (const Map<int, Area *>::Element *E = area_map.front(); E; E = E->next()) {
  222. //this should somehow be faster...
  223. const Area &a = *E->get();
  224. if (p_pos.x >= a.from.x && p_pos.x < a.to.x &&
  225. p_pos.y >= a.from.y && p_pos.y < a.to.y &&
  226. p_pos.z >= a.from.z && p_pos.z < a.to.z) {
  227. return E->key();
  228. }
  229. }
  230. return 0;
  231. }
  232. void GridMap::set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_rot) {
  233. ERR_FAIL_INDEX(ABS(p_x), 1 << 20);
  234. ERR_FAIL_INDEX(ABS(p_y), 1 << 20);
  235. ERR_FAIL_INDEX(ABS(p_z), 1 << 20);
  236. IndexKey key;
  237. key.x = p_x;
  238. key.y = p_y;
  239. key.z = p_z;
  240. OctantKey ok;
  241. ok.x = p_x / octant_size;
  242. ok.y = p_y / octant_size;
  243. ok.z = p_z / octant_size;
  244. ok.area = _find_area(key);
  245. if (cell_map.has(key)) {
  246. int prev_item = cell_map[key].item;
  247. OctantKey octantkey = ok;
  248. ERR_FAIL_COND(!octant_map.has(octantkey));
  249. Octant &g = *octant_map[octantkey];
  250. ERR_FAIL_COND(!g.items.has(prev_item));
  251. ERR_FAIL_COND(!g.items[prev_item].cells.has(key));
  252. g.items[prev_item].cells.erase(key);
  253. if (g.items[prev_item].cells.size() == 0) {
  254. VS::get_singleton()->free(g.items[prev_item].multimesh_instance);
  255. g.items.erase(prev_item);
  256. }
  257. if (g.items.empty()) {
  258. PhysicsServer::get_singleton()->free(g.static_body);
  259. if (g.collision_debug.is_valid()) {
  260. PhysicsServer::get_singleton()->free(g.collision_debug);
  261. PhysicsServer::get_singleton()->free(g.collision_debug_instance);
  262. }
  263. memdelete(&g);
  264. octant_map.erase(octantkey);
  265. } else {
  266. g.dirty = true;
  267. }
  268. cell_map.erase(key);
  269. _queue_dirty_map();
  270. }
  271. if (p_item < 0)
  272. return;
  273. OctantKey octantkey = ok;
  274. //add later
  275. if (!octant_map.has(octantkey)) {
  276. Octant *g = memnew(Octant);
  277. g->dirty = true;
  278. g->static_body = PhysicsServer::get_singleton()->body_create(PhysicsServer::BODY_MODE_STATIC);
  279. PhysicsServer::get_singleton()->body_attach_object_instance_ID(g->static_body, get_instance_ID());
  280. if (is_inside_world())
  281. PhysicsServer::get_singleton()->body_set_space(g->static_body, get_world()->get_space());
  282. SceneTree *st = SceneTree::get_singleton();
  283. if (st && st->is_debugging_collisions_hint()) {
  284. g->collision_debug = VisualServer::get_singleton()->mesh_create();
  285. g->collision_debug_instance = VisualServer::get_singleton()->instance_create();
  286. VisualServer::get_singleton()->instance_set_base(g->collision_debug_instance, g->collision_debug);
  287. if (is_inside_world()) {
  288. VisualServer::get_singleton()->instance_set_scenario(g->collision_debug_instance, get_world()->get_scenario());
  289. VisualServer::get_singleton()->instance_set_transform(g->collision_debug_instance, get_global_transform());
  290. }
  291. }
  292. octant_map[octantkey] = g;
  293. }
  294. Octant &g = *octant_map[octantkey];
  295. if (!g.items.has(p_item)) {
  296. Octant::ItemInstances ii;
  297. if (theme.is_valid() && theme->has_item(p_item)) {
  298. ii.mesh = theme->get_item_mesh(p_item);
  299. ii.shape = theme->get_item_shape(p_item);
  300. ii.navmesh = theme->get_item_navmesh(p_item);
  301. }
  302. ii.multimesh = Ref<MultiMesh>(memnew(MultiMesh));
  303. ii.multimesh->set_color_format(MultiMesh::COLOR_NONE);
  304. ii.multimesh->set_transform_format(MultiMesh::TRANSFORM_3D);
  305. ii.multimesh->set_mesh(ii.mesh);
  306. ii.multimesh_instance = VS::get_singleton()->instance_create();
  307. VS::get_singleton()->instance_set_base(ii.multimesh_instance, ii.multimesh->get_rid());
  308. VS::get_singleton()->instance_geometry_set_flag(ii.multimesh_instance, VS::INSTANCE_FLAG_USE_BAKED_LIGHT, true);
  309. g.items[p_item] = ii;
  310. }
  311. Octant::ItemInstances &ii = g.items[p_item];
  312. ii.cells.insert(key);
  313. g.dirty = true;
  314. _queue_dirty_map();
  315. cell_map[key] = Cell();
  316. Cell &c = cell_map[key];
  317. c.item = p_item;
  318. c.rot = p_rot;
  319. }
  320. int GridMap::get_cell_item(int p_x, int p_y, int p_z) const {
  321. ERR_FAIL_INDEX_V(ABS(p_x), 1 << 20, INVALID_CELL_ITEM);
  322. ERR_FAIL_INDEX_V(ABS(p_y), 1 << 20, INVALID_CELL_ITEM);
  323. ERR_FAIL_INDEX_V(ABS(p_z), 1 << 20, INVALID_CELL_ITEM);
  324. IndexKey key;
  325. key.x = p_x;
  326. key.y = p_y;
  327. key.z = p_z;
  328. if (!cell_map.has(key))
  329. return INVALID_CELL_ITEM;
  330. return cell_map[key].item;
  331. }
  332. int GridMap::get_cell_item_orientation(int p_x, int p_y, int p_z) const {
  333. ERR_FAIL_INDEX_V(ABS(p_x), 1 << 20, -1);
  334. ERR_FAIL_INDEX_V(ABS(p_y), 1 << 20, -1);
  335. ERR_FAIL_INDEX_V(ABS(p_z), 1 << 20, -1);
  336. IndexKey key;
  337. key.x = p_x;
  338. key.y = p_y;
  339. key.z = p_z;
  340. if (!cell_map.has(key))
  341. return -1;
  342. return cell_map[key].rot;
  343. }
  344. void GridMap::_octant_enter_tree(const OctantKey &p_key) {
  345. ERR_FAIL_COND(!octant_map.has(p_key));
  346. if (navigation) {
  347. Octant &g = *octant_map[p_key];
  348. Vector3 ofs(cell_size * 0.5 * int(center_x), cell_size * 0.5 * int(center_y), cell_size * 0.5 * int(center_z));
  349. _octant_clear_navmesh(p_key);
  350. for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
  351. Octant::ItemInstances &ii = E->get();
  352. for (Set<IndexKey>::Element *F = ii.cells.front(); F; F = F->next()) {
  353. IndexKey ik = F->get();
  354. Map<IndexKey, Cell>::Element *C = cell_map.find(ik);
  355. ERR_CONTINUE(!C);
  356. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  357. Transform xform;
  358. if (clip && ((clip_above && cellpos[clip_axis] > clip_floor) || (!clip_above && cellpos[clip_axis] < clip_floor))) {
  359. xform.basis.set_zero();
  360. } else {
  361. xform.basis.set_orthogonal_index(C->get().rot);
  362. }
  363. xform.set_origin(cellpos * cell_size + ofs);
  364. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  365. // add the item's navmesh at given xform to GridMap's Navigation ancestor
  366. if (ii.navmesh.is_valid()) {
  367. int nm_id = navigation->navmesh_create(ii.navmesh, xform, this);
  368. Octant::NavMesh nm;
  369. nm.id = nm_id;
  370. nm.xform = xform;
  371. g.navmesh_ids[ik] = nm;
  372. }
  373. }
  374. }
  375. }
  376. }
  377. void GridMap::_octant_enter_world(const OctantKey &p_key) {
  378. ERR_FAIL_COND(!octant_map.has(p_key));
  379. Octant &g = *octant_map[p_key];
  380. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  381. PhysicsServer::get_singleton()->body_set_space(g.static_body, get_world()->get_space());
  382. //print_line("BODYPOS: "+get_global_transform());
  383. if (g.collision_debug_instance.is_valid()) {
  384. VS::get_singleton()->instance_set_scenario(g.collision_debug_instance, get_world()->get_scenario());
  385. VS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  386. if (area_map.has(p_key.area)) {
  387. VS::get_singleton()->instance_set_room(g.collision_debug_instance, area_map[p_key.area]->instance);
  388. }
  389. }
  390. for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
  391. VS::get_singleton()->instance_set_scenario(E->get().multimesh_instance, get_world()->get_scenario());
  392. VS::get_singleton()->instance_set_transform(E->get().multimesh_instance, get_global_transform());
  393. //print_line("INSTANCEPOS: "+get_global_transform());
  394. if (area_map.has(p_key.area)) {
  395. VS::get_singleton()->instance_set_room(E->get().multimesh_instance, area_map[p_key.area]->instance);
  396. }
  397. }
  398. }
  399. void GridMap::_octant_transform(const OctantKey &p_key) {
  400. ERR_FAIL_COND(!octant_map.has(p_key));
  401. Octant &g = *octant_map[p_key];
  402. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  403. if (g.collision_debug_instance.is_valid()) {
  404. VS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  405. }
  406. for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
  407. VS::get_singleton()->instance_set_transform(E->get().multimesh_instance, get_global_transform());
  408. //print_line("UPDATEPOS: "+get_global_transform());
  409. }
  410. }
  411. void GridMap::_octant_clear_navmesh(const OctantKey &p_key) {
  412. Octant &g = *octant_map[p_key];
  413. if (navigation) {
  414. for (Map<IndexKey, Octant::NavMesh>::Element *E = g.navmesh_ids.front(); E; E = E->next()) {
  415. Octant::NavMesh *nvm = &E->get();
  416. if (nvm && nvm->id) {
  417. navigation->navmesh_remove(E->get().id);
  418. }
  419. }
  420. g.navmesh_ids.clear();
  421. }
  422. }
  423. void GridMap::_octant_update(const OctantKey &p_key) {
  424. ERR_FAIL_COND(!octant_map.has(p_key));
  425. Octant &g = *octant_map[p_key];
  426. if (!g.dirty)
  427. return;
  428. Ref<Mesh> mesh;
  429. _octant_clear_navmesh(p_key);
  430. PhysicsServer::get_singleton()->body_clear_shapes(g.static_body);
  431. if (g.collision_debug.is_valid()) {
  432. VS::get_singleton()->mesh_clear(g.collision_debug);
  433. }
  434. PoolVector<Vector3> col_debug;
  435. /*
  436. * foreach item in this octant,
  437. * set item's multimesh's instance count to number of cells which have this item
  438. * and set said multimesh bounding box to one containing all cells which have this item
  439. */
  440. for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
  441. Octant::ItemInstances &ii = E->get();
  442. ii.multimesh->set_instance_count(ii.cells.size());
  443. Rect3 aabb;
  444. Rect3 mesh_aabb = ii.mesh.is_null() ? Rect3() : ii.mesh->get_aabb();
  445. Vector3 ofs(cell_size * 0.5 * int(center_x), cell_size * 0.5 * int(center_y), cell_size * 0.5 * int(center_z));
  446. //print_line("OCTANT, CELLS: "+itos(ii.cells.size()));
  447. int idx = 0;
  448. // foreach cell containing this item type
  449. for (Set<IndexKey>::Element *F = ii.cells.front(); F; F = F->next()) {
  450. IndexKey ik = F->get();
  451. Map<IndexKey, Cell>::Element *C = cell_map.find(ik);
  452. ERR_CONTINUE(!C);
  453. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  454. Transform xform;
  455. if (clip && ((clip_above && cellpos[clip_axis] > clip_floor) || (!clip_above && cellpos[clip_axis] < clip_floor))) {
  456. xform.basis.set_zero();
  457. } else {
  458. xform.basis.set_orthogonal_index(C->get().rot);
  459. }
  460. xform.set_origin(cellpos * cell_size + ofs);
  461. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  462. ii.multimesh->set_instance_transform(idx, xform);
  463. //ii.multimesh->set_instance_transform(idx,Transform() );
  464. //ii.multimesh->set_instance_color(idx,Color(1,1,1,1));
  465. //print_line("MMINST: "+xform);
  466. if (idx == 0) {
  467. aabb = xform.xform(mesh_aabb);
  468. } else {
  469. aabb.merge_with(xform.xform(mesh_aabb));
  470. }
  471. // add the item's shape at given xform to octant's static_body
  472. if (ii.shape.is_valid()) {
  473. // add the item's shape
  474. PhysicsServer::get_singleton()->body_add_shape(g.static_body, ii.shape->get_rid(), xform);
  475. if (g.collision_debug.is_valid()) {
  476. ii.shape->add_vertices_to_array(col_debug, xform);
  477. }
  478. //print_line("PHIS x: "+xform);
  479. }
  480. // add the item's navmesh at given xform to GridMap's Navigation ancestor
  481. if (navigation) {
  482. if (ii.navmesh.is_valid()) {
  483. int nm_id = navigation->navmesh_create(ii.navmesh, xform, this);
  484. Octant::NavMesh nm;
  485. nm.id = nm_id;
  486. nm.xform = xform;
  487. g.navmesh_ids[ik] = nm;
  488. }
  489. }
  490. idx++;
  491. }
  492. //ii.multimesh->set_aabb(aabb);
  493. }
  494. if (col_debug.size()) {
  495. Array arr;
  496. arr.resize(VS::ARRAY_MAX);
  497. arr[VS::ARRAY_VERTEX] = col_debug;
  498. VS::get_singleton()->mesh_add_surface_from_arrays(g.collision_debug, VS::PRIMITIVE_LINES, arr);
  499. SceneTree *st = SceneTree::get_singleton();
  500. if (st) {
  501. VS::get_singleton()->mesh_surface_set_material(g.collision_debug, 0, st->get_debug_collision_material()->get_rid());
  502. }
  503. }
  504. g.dirty = false;
  505. }
  506. void GridMap::_octant_exit_world(const OctantKey &p_key) {
  507. ERR_FAIL_COND(!octant_map.has(p_key));
  508. Octant &g = *octant_map[p_key];
  509. PhysicsServer::get_singleton()->body_set_state(g.static_body, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  510. PhysicsServer::get_singleton()->body_set_space(g.static_body, RID());
  511. if (g.collision_debug_instance.is_valid()) {
  512. VS::get_singleton()->instance_set_room(g.collision_debug_instance, RID());
  513. VS::get_singleton()->instance_set_scenario(g.collision_debug_instance, RID());
  514. }
  515. for (Map<int, Octant::ItemInstances>::Element *E = g.items.front(); E; E = E->next()) {
  516. VS::get_singleton()->instance_set_scenario(E->get().multimesh_instance, RID());
  517. //VS::get_singleton()->instance_set_transform(E->get().multimesh_instance,get_global_transform());
  518. VS::get_singleton()->instance_set_room(E->get().multimesh_instance, RID());
  519. }
  520. }
  521. void GridMap::_notification(int p_what) {
  522. switch (p_what) {
  523. case NOTIFICATION_ENTER_WORLD: {
  524. _update_area_instances();
  525. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  526. //IndexKey ik;
  527. //ik.key = E->key().indexkey;
  528. _octant_enter_world(E->key());
  529. _octant_update(E->key());
  530. }
  531. awaiting_update = false;
  532. last_transform = get_global_transform();
  533. } break;
  534. case NOTIFICATION_TRANSFORM_CHANGED: {
  535. Transform new_xform = get_global_transform();
  536. if (new_xform == last_transform)
  537. break;
  538. //update run
  539. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  540. _octant_transform(E->key());
  541. }
  542. last_transform = new_xform;
  543. } break;
  544. case NOTIFICATION_EXIT_WORLD: {
  545. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  546. _octant_exit_world(E->key());
  547. }
  548. //_queue_dirty_map(MAP_DIRTY_INSTANCES|MAP_DIRTY_TRANSFORMS);
  549. //_update_dirty_map_callback();
  550. //_update_area_instances();
  551. } break;
  552. case NOTIFICATION_ENTER_TREE: {
  553. Spatial *c = this;
  554. while (c) {
  555. navigation = c->cast_to<Navigation>();
  556. if (navigation) {
  557. break;
  558. }
  559. c = c->get_parent()->cast_to<Spatial>();
  560. }
  561. if (navigation) {
  562. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  563. if (navigation) {
  564. _octant_enter_tree(E->key());
  565. }
  566. }
  567. }
  568. _queue_dirty_map();
  569. } break;
  570. case NOTIFICATION_EXIT_TREE: {
  571. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  572. if (navigation) {
  573. _octant_clear_navmesh(E->key());
  574. }
  575. }
  576. navigation = NULL;
  577. } break;
  578. }
  579. }
  580. void GridMap::_queue_dirty_map() {
  581. if (awaiting_update)
  582. return;
  583. if (is_inside_world()) {
  584. MessageQueue::get_singleton()->push_call(this, "_update_dirty_map_callback");
  585. awaiting_update = true;
  586. }
  587. }
  588. void GridMap::_recreate_octant_data() {
  589. Map<IndexKey, Cell> cell_copy = cell_map;
  590. _clear_internal(true);
  591. for (Map<IndexKey, Cell>::Element *E = cell_copy.front(); E; E = E->next()) {
  592. set_cell_item(E->key().x, E->key().y, E->key().z, E->get().item, E->get().rot);
  593. }
  594. }
  595. void GridMap::_clear_internal(bool p_keep_areas) {
  596. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  597. if (is_inside_world())
  598. _octant_exit_world(E->key());
  599. for (Map<int, Octant::ItemInstances>::Element *F = E->get()->items.front(); F; F = F->next()) {
  600. VS::get_singleton()->free(F->get().multimesh_instance);
  601. }
  602. if (E->get()->collision_debug.is_valid())
  603. VS::get_singleton()->free(E->get()->collision_debug);
  604. if (E->get()->collision_debug_instance.is_valid())
  605. VS::get_singleton()->free(E->get()->collision_debug_instance);
  606. PhysicsServer::get_singleton()->free(E->get()->static_body);
  607. memdelete(E->get());
  608. }
  609. octant_map.clear();
  610. cell_map.clear();
  611. if (p_keep_areas)
  612. return;
  613. for (Map<int, Area *>::Element *E = area_map.front(); E; E = E->next()) {
  614. VS::get_singleton()->free(E->get()->base_portal);
  615. VS::get_singleton()->free(E->get()->instance);
  616. for (int i = 0; i < E->get()->portals.size(); i++) {
  617. VS::get_singleton()->free(E->get()->portals[i].instance);
  618. }
  619. memdelete(E->get());
  620. }
  621. }
  622. void GridMap::clear() {
  623. _clear_internal();
  624. }
  625. void GridMap::resource_changed(const RES &p_res) {
  626. _recreate_octant_data();
  627. }
  628. void GridMap::_update_dirty_map_callback() {
  629. if (!awaiting_update)
  630. return;
  631. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  632. _octant_update(E->key());
  633. }
  634. awaiting_update = false;
  635. }
  636. void GridMap::_bind_methods() {
  637. ClassDB::bind_method(D_METHOD("set_theme", "theme:MeshLibrary"), &GridMap::set_theme);
  638. ClassDB::bind_method(D_METHOD("get_theme:MeshLibrary"), &GridMap::get_theme);
  639. ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &GridMap::set_cell_size);
  640. ClassDB::bind_method(D_METHOD("get_cell_size"), &GridMap::get_cell_size);
  641. ClassDB::bind_method(D_METHOD("set_octant_size", "size"), &GridMap::set_octant_size);
  642. ClassDB::bind_method(D_METHOD("get_octant_size"), &GridMap::get_octant_size);
  643. ClassDB::bind_method(D_METHOD("set_cell_item", "x", "y", "z", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0));
  644. ClassDB::bind_method(D_METHOD("get_cell_item", "x", "y", "z"), &GridMap::get_cell_item);
  645. ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "x", "y", "z"), &GridMap::get_cell_item_orientation);
  646. //ClassDB::bind_method(D_METHOD("_recreate_octants"),&GridMap::_recreate_octants);
  647. ClassDB::bind_method(D_METHOD("_update_dirty_map_callback"), &GridMap::_update_dirty_map_callback);
  648. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed);
  649. ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &GridMap::set_center_x);
  650. ClassDB::bind_method(D_METHOD("get_center_x"), &GridMap::get_center_x);
  651. ClassDB::bind_method(D_METHOD("set_center_y", "enable"), &GridMap::set_center_y);
  652. ClassDB::bind_method(D_METHOD("get_center_y"), &GridMap::get_center_y);
  653. ClassDB::bind_method(D_METHOD("set_center_z", "enable"), &GridMap::set_center_z);
  654. ClassDB::bind_method(D_METHOD("get_center_z"), &GridMap::get_center_z);
  655. ClassDB::bind_method(D_METHOD("set_clip", "enabled", "clipabove", "floor", "axis"), &GridMap::set_clip, DEFVAL(true), DEFVAL(0), DEFVAL(Vector3::AXIS_X));
  656. ClassDB::bind_method(D_METHOD("create_area", "id", "area"), &GridMap::create_area);
  657. ClassDB::bind_method(D_METHOD("area_get_bounds", "area", "bounds"), &GridMap::area_get_bounds);
  658. ClassDB::bind_method(D_METHOD("area_set_exterior_portal", "area", "enable"), &GridMap::area_set_exterior_portal);
  659. ClassDB::bind_method(D_METHOD("area_set_name", "area", "name"), &GridMap::area_set_name);
  660. ClassDB::bind_method(D_METHOD("area_get_name", "area"), &GridMap::area_get_name);
  661. ClassDB::bind_method(D_METHOD("area_is_exterior_portal", "area"), &GridMap::area_is_exterior_portal);
  662. ClassDB::bind_method(D_METHOD("area_set_portal_disable_distance", "area", "distance"), &GridMap::area_set_portal_disable_distance);
  663. ClassDB::bind_method(D_METHOD("area_get_portal_disable_distance", "area"), &GridMap::area_get_portal_disable_distance);
  664. ClassDB::bind_method(D_METHOD("area_set_portal_disable_color", "area", "color"), &GridMap::area_set_portal_disable_color);
  665. ClassDB::bind_method(D_METHOD("area_get_portal_disable_color", "area"), &GridMap::area_get_portal_disable_color);
  666. ClassDB::bind_method(D_METHOD("erase_area", "area"), &GridMap::erase_area);
  667. ClassDB::bind_method(D_METHOD("get_unused_area_id", "area"), &GridMap::get_unused_area_id);
  668. ClassDB::bind_method(D_METHOD("clear"), &GridMap::clear);
  669. ClassDB::bind_method(D_METHOD("get_meshes"), &GridMap::get_meshes);
  670. BIND_CONSTANT(INVALID_CELL_ITEM);
  671. }
  672. void GridMap::set_clip(bool p_enabled, bool p_clip_above, int p_floor, Vector3::Axis p_axis) {
  673. if (!p_enabled && !clip)
  674. return;
  675. if (clip && p_enabled && clip_floor == p_floor && p_clip_above == clip_above && p_axis == clip_axis)
  676. return;
  677. clip = p_enabled;
  678. clip_floor = p_floor;
  679. clip_axis = p_axis;
  680. clip_above = p_clip_above;
  681. //make it all update
  682. for (Map<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
  683. Octant *g = E->get();
  684. g->dirty = true;
  685. }
  686. awaiting_update = true;
  687. _update_dirty_map_callback();
  688. }
  689. void GridMap::_update_areas() {
  690. //clear the portals
  691. for (Map<int, Area *>::Element *E = area_map.front(); E; E = E->next()) {
  692. //this should somehow be faster...
  693. Area &a = *E->get();
  694. a.portals.clear();
  695. if (a.instance.is_valid()) {
  696. VisualServer::get_singleton()->free(a.instance);
  697. a.instance = RID();
  698. }
  699. }
  700. //test all areas against all areas and create portals - this sucks (slow :( )
  701. for (Map<int, Area *>::Element *E = area_map.front(); E; E = E->next()) {
  702. Area &a = *E->get();
  703. if (a.exterior_portal) //that's pretty much all it does... yes it is
  704. continue;
  705. Vector3 from_a(a.from.x, a.from.y, a.from.z);
  706. Vector3 to_a(a.to.x, a.to.y, a.to.z);
  707. for (Map<int, Area *>::Element *F = area_map.front(); F; F = F->next()) {
  708. Area &b = *F->get();
  709. Vector3 from_b(b.from.x, b.from.y, b.from.z);
  710. Vector3 to_b(b.to.x, b.to.y, b.to.z);
  711. // initially test intersection and discards
  712. int axis = -1;
  713. float sign = 0;
  714. bool valid = true;
  715. Vector3 axmin, axmax;
  716. for (int i = 0; i < 3; i++) {
  717. if (from_a[i] == to_b[i]) {
  718. if (axis != -1) {
  719. valid = false;
  720. break;
  721. }
  722. axis = i;
  723. sign = -1;
  724. } else if (from_b[i] == to_a[i]) {
  725. if (axis != -1) {
  726. valid = false;
  727. break;
  728. }
  729. axis = i;
  730. sign = +1;
  731. }
  732. if (from_a[i] > to_b[i] || to_a[i] < from_b[i]) {
  733. valid = false;
  734. break;
  735. } else {
  736. axmin[i] = (from_a[i] > from_b[i]) ? from_a[i] : from_b[i];
  737. axmax[i] = (to_a[i] < to_b[i]) ? to_a[i] : to_b[i];
  738. }
  739. }
  740. if (axis == -1 || !valid)
  741. continue;
  742. Transform xf;
  743. for (int i = 0; i < 3; i++) {
  744. int ax = (axis + i) % 3;
  745. Vector3 axis_vec;
  746. float scale = (i == 0) ? sign : ((axmax[ax] - axmin[ax]) * cell_size);
  747. axis_vec[ax] = scale;
  748. xf.basis.set_axis((2 + i) % 3, axis_vec);
  749. xf.origin[i] = axmin[i] * cell_size;
  750. }
  751. Area::Portal portal;
  752. portal.xform = xf;
  753. a.portals.push_back(portal);
  754. }
  755. }
  756. _update_area_instances();
  757. }
  758. void GridMap::_update_area_instances() {
  759. Transform base_xform;
  760. if (_in_tree)
  761. base_xform = get_global_transform();
  762. for (Map<int, Area *>::Element *E = area_map.front(); E; E = E->next()) {
  763. //this should somehow be faster...
  764. Area &a = *E->get();
  765. if (a.instance.is_valid() != _in_tree) {
  766. if (!_in_tree) {
  767. for (int i = 0; i < a.portals.size(); i++) {
  768. Area::Portal &p = a.portals[i];
  769. ERR_CONTINUE(!p.instance.is_valid());
  770. VisualServer::get_singleton()->free(p.instance);
  771. p.instance = RID();
  772. }
  773. VisualServer::get_singleton()->free(a.instance);
  774. a.instance = RID();
  775. } else {
  776. //a.instance = VisualServer::get_singleton()->instance_create2(base_room,get_world()->get_scenario());
  777. for (int i = 0; i < a.portals.size(); i++) {
  778. Area::Portal &p = a.portals[i];
  779. ERR_CONTINUE(p.instance.is_valid());
  780. p.instance = VisualServer::get_singleton()->instance_create2(a.base_portal, get_world()->get_scenario());
  781. VisualServer::get_singleton()->instance_set_room(p.instance, a.instance);
  782. }
  783. }
  784. }
  785. if (a.instance.is_valid()) {
  786. Transform xform;
  787. Vector3 from_a(a.from.x, a.from.y, a.from.z);
  788. Vector3 to_a(a.to.x, a.to.y, a.to.z);
  789. for (int i = 0; i < 3; i++) {
  790. xform.origin[i] = from_a[i] * cell_size;
  791. Vector3 s;
  792. s[i] = (to_a[i] - from_a[i]) * cell_size;
  793. xform.basis.set_axis(i, s);
  794. }
  795. VisualServer::get_singleton()->instance_set_transform(a.instance, base_xform * xform);
  796. for (int i = 0; i < a.portals.size(); i++) {
  797. Area::Portal &p = a.portals[i];
  798. ERR_CONTINUE(!p.instance.is_valid());
  799. VisualServer::get_singleton()->instance_set_transform(p.instance, base_xform * xform);
  800. }
  801. }
  802. }
  803. }
  804. Error GridMap::create_area(int p_id, const Rect3 &p_bounds) {
  805. ERR_FAIL_COND_V(area_map.has(p_id), ERR_ALREADY_EXISTS);
  806. ERR_EXPLAIN("ID 0 is taken as global area, start from 1");
  807. ERR_FAIL_COND_V(p_id == 0, ERR_INVALID_PARAMETER);
  808. ERR_FAIL_COND_V(p_bounds.has_no_area(), ERR_INVALID_PARAMETER);
  809. // FIRST VALIDATE AREA
  810. IndexKey from, to;
  811. from.x = p_bounds.position.x;
  812. from.y = p_bounds.position.y;
  813. from.z = p_bounds.position.z;
  814. to.x = p_bounds.position.x + p_bounds.size.x;
  815. to.y = p_bounds.position.y + p_bounds.size.y;
  816. to.z = p_bounds.position.z + p_bounds.size.z;
  817. for (Map<int, Area *>::Element *E = area_map.front(); E; E = E->next()) {
  818. //this should somehow be faster...
  819. Area &a = *E->get();
  820. //does it interset with anything else?
  821. if (from.x >= a.to.x ||
  822. to.x <= a.from.x ||
  823. from.y >= a.to.y ||
  824. to.y <= a.from.y ||
  825. from.z >= a.to.z ||
  826. to.z <= a.from.z) {
  827. // all good
  828. } else {
  829. return ERR_INVALID_PARAMETER;
  830. }
  831. }
  832. Area *area = memnew(Area);
  833. area->from = from;
  834. area->to = to;
  835. area->portal_disable_distance = 0;
  836. area->exterior_portal = false;
  837. area->name = "Area " + itos(p_id);
  838. area_map[p_id] = area;
  839. _recreate_octant_data();
  840. return OK;
  841. }
  842. Rect3 GridMap::area_get_bounds(int p_area) const {
  843. ERR_FAIL_COND_V(!area_map.has(p_area), Rect3());
  844. const Area *a = area_map[p_area];
  845. Rect3 aabb;
  846. aabb.position = Vector3(a->from.x, a->from.y, a->from.z);
  847. aabb.size = Vector3(a->to.x, a->to.y, a->to.z) - aabb.position;
  848. return aabb;
  849. }
  850. void GridMap::area_set_name(int p_area, const String &p_name) {
  851. ERR_FAIL_COND(!area_map.has(p_area));
  852. Area *a = area_map[p_area];
  853. a->name = p_name;
  854. }
  855. String GridMap::area_get_name(int p_area) const {
  856. ERR_FAIL_COND_V(!area_map.has(p_area), "");
  857. const Area *a = area_map[p_area];
  858. return a->name;
  859. }
  860. void GridMap::area_set_exterior_portal(int p_area, bool p_enable) {
  861. ERR_FAIL_COND(!area_map.has(p_area));
  862. Area *a = area_map[p_area];
  863. if (a->exterior_portal == p_enable)
  864. return;
  865. a->exterior_portal = p_enable;
  866. _recreate_octant_data();
  867. }
  868. bool GridMap::area_is_exterior_portal(int p_area) const {
  869. ERR_FAIL_COND_V(!area_map.has(p_area), false);
  870. const Area *a = area_map[p_area];
  871. return a->exterior_portal;
  872. }
  873. void GridMap::area_set_portal_disable_distance(int p_area, float p_distance) {
  874. ERR_FAIL_COND(!area_map.has(p_area));
  875. Area *a = area_map[p_area];
  876. a->portal_disable_distance = p_distance;
  877. }
  878. float GridMap::area_get_portal_disable_distance(int p_area) const {
  879. ERR_FAIL_COND_V(!area_map.has(p_area), 0);
  880. const Area *a = area_map[p_area];
  881. return a->portal_disable_distance;
  882. }
  883. void GridMap::area_set_portal_disable_color(int p_area, Color p_color) {
  884. ERR_FAIL_COND(!area_map.has(p_area));
  885. Area *a = area_map[p_area];
  886. a->portal_disable_color = p_color;
  887. }
  888. Color GridMap::area_get_portal_disable_color(int p_area) const {
  889. ERR_FAIL_COND_V(!area_map.has(p_area), Color());
  890. const Area *a = area_map[p_area];
  891. return a->portal_disable_color;
  892. }
  893. void GridMap::get_area_list(List<int> *p_areas) const {
  894. for (const Map<int, Area *>::Element *E = area_map.front(); E; E = E->next()) {
  895. p_areas->push_back(E->key());
  896. }
  897. }
  898. GridMap::Area::Portal::~Portal() {
  899. if (instance.is_valid())
  900. VisualServer::get_singleton()->free(instance);
  901. }
  902. GridMap::Area::Area() {
  903. base_portal = VisualServer::get_singleton()->portal_create();
  904. Vector<Point2> points;
  905. points.push_back(Point2(0, 1));
  906. points.push_back(Point2(1, 1));
  907. points.push_back(Point2(1, 0));
  908. points.push_back(Point2(0, 0));
  909. VisualServer::get_singleton()->portal_set_shape(base_portal, points);
  910. }
  911. GridMap::Area::~Area() {
  912. if (instance.is_valid())
  913. VisualServer::get_singleton()->free(instance);
  914. VisualServer::get_singleton()->free(base_portal);
  915. }
  916. void GridMap::erase_area(int p_area) {
  917. ERR_FAIL_COND(!area_map.has(p_area));
  918. Area *a = area_map[p_area];
  919. memdelete(a);
  920. area_map.erase(p_area);
  921. _recreate_octant_data();
  922. }
  923. int GridMap::get_unused_area_id() const {
  924. if (area_map.empty())
  925. return 1;
  926. else
  927. return area_map.back()->key() + 1;
  928. }
  929. void GridMap::set_cell_scale(float p_scale) {
  930. cell_scale = p_scale;
  931. _queue_dirty_map();
  932. }
  933. float GridMap::get_cell_scale() const {
  934. return cell_scale;
  935. }
  936. Array GridMap::get_meshes() {
  937. if (theme.is_null())
  938. return Array();
  939. Vector3 ofs(cell_size * 0.5 * int(center_x), cell_size * 0.5 * int(center_y), cell_size * 0.5 * int(center_z));
  940. Array meshes;
  941. for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
  942. int id = E->get().item;
  943. if (!theme->has_item(id))
  944. continue;
  945. Ref<Mesh> mesh = theme->get_item_mesh(id);
  946. if (mesh.is_null())
  947. continue;
  948. IndexKey ik = E->key();
  949. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  950. Transform xform;
  951. xform.basis.set_orthogonal_index(E->get().rot);
  952. xform.set_origin(cellpos * cell_size + ofs);
  953. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  954. meshes.push_back(xform);
  955. meshes.push_back(mesh);
  956. }
  957. return meshes;
  958. }
  959. GridMap::GridMap() {
  960. cell_size = 2;
  961. octant_size = 4;
  962. awaiting_update = false;
  963. _in_tree = false;
  964. center_x = true;
  965. center_y = true;
  966. center_z = true;
  967. clip = false;
  968. clip_floor = 0;
  969. clip_axis = Vector3::AXIS_Z;
  970. clip_above = true;
  971. cell_scale = 1.0;
  972. navigation = NULL;
  973. set_notify_transform(true);
  974. }
  975. GridMap::~GridMap() {
  976. if (!theme.is_null())
  977. theme->unregister_owner(this);
  978. clear();
  979. }