dungeongen.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2018 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. Copyright (C) 2015-2018 paramat
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include "dungeongen.h"
  18. #include <cmath>
  19. #include "mapgen.h"
  20. #include "voxel.h"
  21. #include "noise.h"
  22. #include "mapblock.h"
  23. #include "mapnode.h"
  24. #include "map.h"
  25. #include "nodedef.h"
  26. #include "settings.h"
  27. //#define DGEN_USE_TORCHES
  28. NoiseParams nparams_dungeon_density(0.9, 0.5, v3f(500.0, 500.0, 500.0), 0, 2, 0.8, 2.0);
  29. NoiseParams nparams_dungeon_alt_wall(-0.4, 1.0, v3f(40.0, 40.0, 40.0), 32474, 6, 1.1, 2.0);
  30. ///////////////////////////////////////////////////////////////////////////////
  31. DungeonGen::DungeonGen(const NodeDefManager *ndef,
  32. GenerateNotifier *gennotify, DungeonParams *dparams)
  33. {
  34. assert(ndef);
  35. this->ndef = ndef;
  36. this->gennotify = gennotify;
  37. #ifdef DGEN_USE_TORCHES
  38. c_torch = ndef->getId("default:torch");
  39. #endif
  40. if (dparams) {
  41. memcpy(&dp, dparams, sizeof(dp));
  42. } else {
  43. // Default dungeon parameters
  44. dp.seed = 0;
  45. dp.c_wall = ndef->getId("mapgen_cobble");
  46. dp.c_alt_wall = ndef->getId("mapgen_mossycobble");
  47. dp.c_stair = ndef->getId("mapgen_stair_cobble");
  48. dp.diagonal_dirs = false;
  49. dp.only_in_ground = true;
  50. dp.holesize = v3s16(1, 2, 1);
  51. dp.corridor_len_min = 1;
  52. dp.corridor_len_max = 13;
  53. dp.room_size_min = v3s16(4, 4, 4);
  54. dp.room_size_max = v3s16(8, 6, 8);
  55. dp.room_size_large_min = v3s16(8, 8, 8);
  56. dp.room_size_large_max = v3s16(16, 16, 16);
  57. dp.rooms_min = 2;
  58. dp.rooms_max = 16;
  59. dp.notifytype = GENNOTIFY_DUNGEON;
  60. dp.np_density = nparams_dungeon_density;
  61. dp.np_alt_wall = nparams_dungeon_alt_wall;
  62. }
  63. }
  64. void DungeonGen::generate(MMVManip *vm, u32 bseed, v3s16 nmin, v3s16 nmax)
  65. {
  66. assert(vm);
  67. //TimeTaker t("gen dungeons");
  68. float nval_density = NoisePerlin3D(&dp.np_density, nmin.X, nmin.Y, nmin.Z, dp.seed);
  69. if (nval_density < 1.0f)
  70. return;
  71. static const bool preserve_ignore = !g_settings->getBool("projecting_dungeons");
  72. this->vm = vm;
  73. this->blockseed = bseed;
  74. random.seed(bseed + 2);
  75. // Dungeon generator doesn't modify places which have this set
  76. vm->clearFlag(VMANIP_FLAG_DUNGEON_INSIDE | VMANIP_FLAG_DUNGEON_PRESERVE);
  77. if (dp.only_in_ground) {
  78. // Set all air and liquid drawtypes to be untouchable to make dungeons
  79. // open to air and liquids. Optionally set ignore to be untouchable to
  80. // prevent projecting dungeons.
  81. for (s16 z = nmin.Z; z <= nmax.Z; z++) {
  82. for (s16 y = nmin.Y; y <= nmax.Y; y++) {
  83. u32 i = vm->m_area.index(nmin.X, y, z);
  84. for (s16 x = nmin.X; x <= nmax.X; x++) {
  85. content_t c = vm->m_data[i].getContent();
  86. NodeDrawType dtype = ndef->get(c).drawtype;
  87. if (dtype == NDT_AIRLIKE || dtype == NDT_LIQUID ||
  88. (preserve_ignore && c == CONTENT_IGNORE))
  89. vm->m_flags[i] |= VMANIP_FLAG_DUNGEON_PRESERVE;
  90. i++;
  91. }
  92. }
  93. }
  94. }
  95. // Add them
  96. for (u32 i = 0; i < std::floor(nval_density); i++)
  97. makeDungeon(v3s16(1, 1, 1) * MAP_BLOCKSIZE);
  98. // Optionally convert some structure to alternative structure
  99. if (dp.c_alt_wall == CONTENT_IGNORE)
  100. return;
  101. for (s16 z = nmin.Z; z <= nmax.Z; z++)
  102. for (s16 y = nmin.Y; y <= nmax.Y; y++) {
  103. u32 i = vm->m_area.index(nmin.X, y, z);
  104. for (s16 x = nmin.X; x <= nmax.X; x++) {
  105. if (vm->m_data[i].getContent() == dp.c_wall) {
  106. if (NoisePerlin3D(&dp.np_alt_wall, x, y, z, blockseed) > 0.0f)
  107. vm->m_data[i].setContent(dp.c_alt_wall);
  108. }
  109. i++;
  110. }
  111. }
  112. //printf("== gen dungeons: %dms\n", t.stop());
  113. }
  114. void DungeonGen::makeDungeon(v3s16 start_padding)
  115. {
  116. const v3s16 &areasize = vm->m_area.getExtent();
  117. v3s16 roomsize;
  118. v3s16 roomplace;
  119. /*
  120. Find place for first room.
  121. There is a 1 in 4 chance of the first room being 'large',
  122. all other rooms are not 'large'.
  123. */
  124. bool fits = false;
  125. for (u32 i = 0; i < 100 && !fits; i++) {
  126. bool is_large_room = ((random.next() & 3) == 1);
  127. if (is_large_room) {
  128. roomsize.Z = random.range(
  129. dp.room_size_large_min.Z, dp.room_size_large_max.Z);
  130. roomsize.Y = random.range(
  131. dp.room_size_large_min.Y, dp.room_size_large_max.Y);
  132. roomsize.X = random.range(
  133. dp.room_size_large_min.X, dp.room_size_large_max.X);
  134. } else {
  135. roomsize.Z = random.range(dp.room_size_min.Z, dp.room_size_max.Z);
  136. roomsize.Y = random.range(dp.room_size_min.Y, dp.room_size_max.Y);
  137. roomsize.X = random.range(dp.room_size_min.X, dp.room_size_max.X);
  138. }
  139. // start_padding is used to disallow starting the generation of
  140. // a dungeon in a neighboring generation chunk
  141. roomplace = vm->m_area.MinEdge + start_padding;
  142. roomplace.Z += random.range(0, areasize.Z - roomsize.Z - start_padding.Z);
  143. roomplace.Y += random.range(0, areasize.Y - roomsize.Y - start_padding.Y);
  144. roomplace.X += random.range(0, areasize.X - roomsize.X - start_padding.X);
  145. /*
  146. Check that we're not putting the room to an unknown place,
  147. otherwise it might end up floating in the air
  148. */
  149. fits = true;
  150. for (s16 z = 0; z < roomsize.Z; z++)
  151. for (s16 y = 0; y < roomsize.Y; y++)
  152. for (s16 x = 0; x < roomsize.X; x++) {
  153. v3s16 p = roomplace + v3s16(x, y, z);
  154. u32 vi = vm->m_area.index(p);
  155. if ((vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE) ||
  156. vm->m_data[vi].getContent() == CONTENT_IGNORE) {
  157. fits = false;
  158. break;
  159. }
  160. }
  161. }
  162. // No place found
  163. if (!fits)
  164. return;
  165. /*
  166. Stores the center position of the last room made, so that
  167. a new corridor can be started from the last room instead of
  168. the new room, if chosen so.
  169. */
  170. v3s16 last_room_center = roomplace + v3s16(roomsize.X / 2, 1, roomsize.Z / 2);
  171. u32 room_count = random.range(dp.rooms_min, dp.rooms_max);
  172. for (u32 i = 0; i < room_count; i++) {
  173. // Make a room to the determined place
  174. makeRoom(roomsize, roomplace);
  175. v3s16 room_center = roomplace + v3s16(roomsize.X / 2, 1, roomsize.Z / 2);
  176. if (gennotify)
  177. gennotify->addEvent(dp.notifytype, room_center);
  178. #ifdef DGEN_USE_TORCHES
  179. // Place torch at room center (for testing)
  180. vm->m_data[vm->m_area.index(room_center)] = MapNode(c_torch);
  181. #endif
  182. // Quit if last room
  183. if (i == room_count - 1)
  184. break;
  185. // Determine walker start position
  186. bool start_in_last_room = (random.range(0, 2) != 0);
  187. v3s16 walker_start_place;
  188. if (start_in_last_room) {
  189. walker_start_place = last_room_center;
  190. } else {
  191. walker_start_place = room_center;
  192. // Store center of current room as the last one
  193. last_room_center = room_center;
  194. }
  195. // Create walker and find a place for a door
  196. v3s16 doorplace;
  197. v3s16 doordir;
  198. m_pos = walker_start_place;
  199. if (!findPlaceForDoor(doorplace, doordir))
  200. return;
  201. if (random.range(0, 1) == 0)
  202. // Make the door
  203. makeDoor(doorplace, doordir);
  204. else
  205. // Don't actually make a door
  206. doorplace -= doordir;
  207. // Make a random corridor starting from the door
  208. v3s16 corridor_end;
  209. v3s16 corridor_end_dir;
  210. makeCorridor(doorplace, doordir, corridor_end, corridor_end_dir);
  211. // Find a place for a random sized room
  212. roomsize.Z = random.range(dp.room_size_min.Z, dp.room_size_max.Z);
  213. roomsize.Y = random.range(dp.room_size_min.Y, dp.room_size_max.Y);
  214. roomsize.X = random.range(dp.room_size_min.X, dp.room_size_max.X);
  215. m_pos = corridor_end;
  216. m_dir = corridor_end_dir;
  217. if (!findPlaceForRoomDoor(roomsize, doorplace, doordir, roomplace))
  218. return;
  219. if (random.range(0, 1) == 0)
  220. // Make the door
  221. makeDoor(doorplace, doordir);
  222. else
  223. // Don't actually make a door
  224. roomplace -= doordir;
  225. }
  226. }
  227. void DungeonGen::makeRoom(v3s16 roomsize, v3s16 roomplace)
  228. {
  229. MapNode n_wall(dp.c_wall);
  230. MapNode n_air(CONTENT_AIR);
  231. // Make +-X walls
  232. for (s16 z = 0; z < roomsize.Z; z++)
  233. for (s16 y = 0; y < roomsize.Y; y++) {
  234. {
  235. v3s16 p = roomplace + v3s16(0, y, z);
  236. if (!vm->m_area.contains(p))
  237. continue;
  238. u32 vi = vm->m_area.index(p);
  239. if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
  240. continue;
  241. vm->m_data[vi] = n_wall;
  242. }
  243. {
  244. v3s16 p = roomplace + v3s16(roomsize.X - 1, y, z);
  245. if (!vm->m_area.contains(p))
  246. continue;
  247. u32 vi = vm->m_area.index(p);
  248. if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
  249. continue;
  250. vm->m_data[vi] = n_wall;
  251. }
  252. }
  253. // Make +-Z walls
  254. for (s16 x = 0; x < roomsize.X; x++)
  255. for (s16 y = 0; y < roomsize.Y; y++) {
  256. {
  257. v3s16 p = roomplace + v3s16(x, y, 0);
  258. if (!vm->m_area.contains(p))
  259. continue;
  260. u32 vi = vm->m_area.index(p);
  261. if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
  262. continue;
  263. vm->m_data[vi] = n_wall;
  264. }
  265. {
  266. v3s16 p = roomplace + v3s16(x, y, roomsize.Z - 1);
  267. if (!vm->m_area.contains(p))
  268. continue;
  269. u32 vi = vm->m_area.index(p);
  270. if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
  271. continue;
  272. vm->m_data[vi] = n_wall;
  273. }
  274. }
  275. // Make +-Y walls (floor and ceiling)
  276. for (s16 z = 0; z < roomsize.Z; z++)
  277. for (s16 x = 0; x < roomsize.X; x++) {
  278. {
  279. v3s16 p = roomplace + v3s16(x, 0, z);
  280. if (!vm->m_area.contains(p))
  281. continue;
  282. u32 vi = vm->m_area.index(p);
  283. if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
  284. continue;
  285. vm->m_data[vi] = n_wall;
  286. }
  287. {
  288. v3s16 p = roomplace + v3s16(x,roomsize. Y - 1, z);
  289. if (!vm->m_area.contains(p))
  290. continue;
  291. u32 vi = vm->m_area.index(p);
  292. if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
  293. continue;
  294. vm->m_data[vi] = n_wall;
  295. }
  296. }
  297. // Fill with air
  298. for (s16 z = 1; z < roomsize.Z - 1; z++)
  299. for (s16 y = 1; y < roomsize.Y - 1; y++)
  300. for (s16 x = 1; x < roomsize.X - 1; x++) {
  301. v3s16 p = roomplace + v3s16(x, y, z);
  302. if (!vm->m_area.contains(p))
  303. continue;
  304. u32 vi = vm->m_area.index(p);
  305. vm->m_flags[vi] |= VMANIP_FLAG_DUNGEON_UNTOUCHABLE;
  306. vm->m_data[vi] = n_air;
  307. }
  308. }
  309. void DungeonGen::makeFill(v3s16 place, v3s16 size,
  310. u8 avoid_flags, MapNode n, u8 or_flags)
  311. {
  312. for (s16 z = 0; z < size.Z; z++)
  313. for (s16 y = 0; y < size.Y; y++)
  314. for (s16 x = 0; x < size.X; x++) {
  315. v3s16 p = place + v3s16(x, y, z);
  316. if (!vm->m_area.contains(p))
  317. continue;
  318. u32 vi = vm->m_area.index(p);
  319. if (vm->m_flags[vi] & avoid_flags)
  320. continue;
  321. vm->m_flags[vi] |= or_flags;
  322. vm->m_data[vi] = n;
  323. }
  324. }
  325. void DungeonGen::makeHole(v3s16 place)
  326. {
  327. makeFill(place, dp.holesize, 0, MapNode(CONTENT_AIR),
  328. VMANIP_FLAG_DUNGEON_INSIDE);
  329. }
  330. void DungeonGen::makeDoor(v3s16 doorplace, v3s16 doordir)
  331. {
  332. makeHole(doorplace);
  333. #ifdef DGEN_USE_TORCHES
  334. // Place torch (for testing)
  335. vm->m_data[vm->m_area.index(doorplace)] = MapNode(c_torch);
  336. #endif
  337. }
  338. void DungeonGen::makeCorridor(v3s16 doorplace, v3s16 doordir,
  339. v3s16 &result_place, v3s16 &result_dir)
  340. {
  341. makeHole(doorplace);
  342. v3s16 p0 = doorplace;
  343. v3s16 dir = doordir;
  344. u32 length = random.range(dp.corridor_len_min, dp.corridor_len_max);
  345. u32 partlength = random.range(dp.corridor_len_min, dp.corridor_len_max);
  346. u32 partcount = 0;
  347. s16 make_stairs = 0;
  348. if (random.next() % 2 == 0 && partlength >= 3)
  349. make_stairs = random.next() % 2 ? 1 : -1;
  350. for (u32 i = 0; i < length; i++) {
  351. v3s16 p = p0 + dir;
  352. if (partcount != 0)
  353. p.Y += make_stairs;
  354. // Check segment of minimum size corridor is in voxelmanip
  355. if (vm->m_area.contains(p) && vm->m_area.contains(p + v3s16(0, 1, 0))) {
  356. if (make_stairs) {
  357. makeFill(p + v3s16(-1, -1, -1),
  358. dp.holesize + v3s16(2, 3, 2),
  359. VMANIP_FLAG_DUNGEON_UNTOUCHABLE,
  360. MapNode(dp.c_wall),
  361. 0);
  362. makeFill(p, dp.holesize, VMANIP_FLAG_DUNGEON_UNTOUCHABLE,
  363. MapNode(CONTENT_AIR), VMANIP_FLAG_DUNGEON_INSIDE);
  364. makeFill(p - dir, dp.holesize, VMANIP_FLAG_DUNGEON_UNTOUCHABLE,
  365. MapNode(CONTENT_AIR), VMANIP_FLAG_DUNGEON_INSIDE);
  366. // TODO: fix stairs code so it works 100%
  367. // (quite difficult)
  368. // exclude stairs from the bottom step
  369. // exclude stairs from diagonal steps
  370. if (((dir.X ^ dir.Z) & 1) &&
  371. (((make_stairs == 1) && i != 0) ||
  372. ((make_stairs == -1) && i != length - 1))) {
  373. // rotate face 180 deg if
  374. // making stairs backwards
  375. int facedir = dir_to_facedir(dir * make_stairs);
  376. v3s16 ps = p;
  377. u16 stair_width = (dir.Z != 0) ? dp.holesize.X : dp.holesize.Z;
  378. // Stair width direction vector
  379. v3s16 swv = (dir.Z != 0) ? v3s16(1, 0, 0) : v3s16(0, 0, 1);
  380. for (u16 st = 0; st < stair_width; st++) {
  381. if (make_stairs == -1) {
  382. u32 vi = vm->m_area.index(ps.X - dir.X, ps.Y - 1, ps.Z - dir.Z);
  383. if (vm->m_area.contains(ps + v3s16(-dir.X, -1, -dir.Z)) &&
  384. vm->m_data[vi].getContent() == dp.c_wall) {
  385. vm->m_flags[vi] |= VMANIP_FLAG_DUNGEON_UNTOUCHABLE;
  386. vm->m_data[vi] = MapNode(dp.c_stair, 0, facedir);
  387. }
  388. } else if (make_stairs == 1) {
  389. u32 vi = vm->m_area.index(ps.X, ps.Y - 1, ps.Z);
  390. if (vm->m_area.contains(ps + v3s16(0, -1, 0)) &&
  391. vm->m_data[vi].getContent() == dp.c_wall) {
  392. vm->m_flags[vi] |= VMANIP_FLAG_DUNGEON_UNTOUCHABLE;
  393. vm->m_data[vi] = MapNode(dp.c_stair, 0, facedir);
  394. }
  395. }
  396. ps += swv;
  397. }
  398. }
  399. } else {
  400. makeFill(p + v3s16(-1, -1, -1),
  401. dp.holesize + v3s16(2, 2, 2),
  402. VMANIP_FLAG_DUNGEON_UNTOUCHABLE,
  403. MapNode(dp.c_wall),
  404. 0);
  405. makeHole(p);
  406. }
  407. p0 = p;
  408. } else {
  409. // Can't go here, turn away
  410. dir = turn_xz(dir, random.range(0, 1));
  411. make_stairs = -make_stairs;
  412. partcount = 0;
  413. partlength = random.range(1, length);
  414. continue;
  415. }
  416. partcount++;
  417. if (partcount >= partlength) {
  418. partcount = 0;
  419. dir = random_turn(random, dir);
  420. partlength = random.range(1, length);
  421. make_stairs = 0;
  422. if (random.next() % 2 == 0 && partlength >= 3)
  423. make_stairs = random.next() % 2 ? 1 : -1;
  424. }
  425. }
  426. result_place = p0;
  427. result_dir = dir;
  428. }
  429. bool DungeonGen::findPlaceForDoor(v3s16 &result_place, v3s16 &result_dir)
  430. {
  431. for (u32 i = 0; i < 100; i++) {
  432. v3s16 p = m_pos + m_dir;
  433. v3s16 p1 = p + v3s16(0, 1, 0);
  434. if (!vm->m_area.contains(p) || !vm->m_area.contains(p1) || i % 4 == 0) {
  435. randomizeDir();
  436. continue;
  437. }
  438. if (vm->getNodeNoExNoEmerge(p).getContent() == dp.c_wall &&
  439. vm->getNodeNoExNoEmerge(p1).getContent() == dp.c_wall) {
  440. // Found wall, this is a good place!
  441. result_place = p;
  442. result_dir = m_dir;
  443. // Randomize next direction
  444. randomizeDir();
  445. return true;
  446. }
  447. /*
  448. Determine where to move next
  449. */
  450. // Jump one up if the actual space is there
  451. if (vm->getNodeNoExNoEmerge(p +
  452. v3s16(0, 0, 0)).getContent() == dp.c_wall &&
  453. vm->getNodeNoExNoEmerge(p +
  454. v3s16(0, 1, 0)).getContent() == CONTENT_AIR &&
  455. vm->getNodeNoExNoEmerge(p +
  456. v3s16(0, 2, 0)).getContent() == CONTENT_AIR)
  457. p += v3s16(0,1,0);
  458. // Jump one down if the actual space is there
  459. if (vm->getNodeNoExNoEmerge(p +
  460. v3s16(0, 1, 0)).getContent() == dp.c_wall &&
  461. vm->getNodeNoExNoEmerge(p +
  462. v3s16(0, 0, 0)).getContent() == CONTENT_AIR &&
  463. vm->getNodeNoExNoEmerge(p +
  464. v3s16(0, -1, 0)).getContent() == CONTENT_AIR)
  465. p += v3s16(0, -1, 0);
  466. // Check if walking is now possible
  467. if (vm->getNodeNoExNoEmerge(p).getContent() != CONTENT_AIR ||
  468. vm->getNodeNoExNoEmerge(p +
  469. v3s16(0, 1, 0)).getContent() != CONTENT_AIR) {
  470. // Cannot continue walking here
  471. randomizeDir();
  472. continue;
  473. }
  474. // Move there
  475. m_pos = p;
  476. }
  477. return false;
  478. }
  479. bool DungeonGen::findPlaceForRoomDoor(v3s16 roomsize, v3s16 &result_doorplace,
  480. v3s16 &result_doordir, v3s16 &result_roomplace)
  481. {
  482. for (s16 trycount = 0; trycount < 30; trycount++) {
  483. v3s16 doorplace;
  484. v3s16 doordir;
  485. bool r = findPlaceForDoor(doorplace, doordir);
  486. if (!r)
  487. continue;
  488. v3s16 roomplace;
  489. // X east, Z north, Y up
  490. if (doordir == v3s16(1, 0, 0)) // X+
  491. roomplace = doorplace +
  492. v3s16(0, -1, random.range(-roomsize.Z + 2, -2));
  493. if (doordir == v3s16(-1, 0, 0)) // X-
  494. roomplace = doorplace +
  495. v3s16(-roomsize.X + 1, -1, random.range(-roomsize.Z + 2, -2));
  496. if (doordir == v3s16(0, 0, 1)) // Z+
  497. roomplace = doorplace +
  498. v3s16(random.range(-roomsize.X + 2, -2), -1, 0);
  499. if (doordir == v3s16(0, 0, -1)) // Z-
  500. roomplace = doorplace +
  501. v3s16(random.range(-roomsize.X + 2, -2), -1, -roomsize.Z + 1);
  502. // Check fit
  503. bool fits = true;
  504. for (s16 z = 1; z < roomsize.Z - 1; z++)
  505. for (s16 y = 1; y < roomsize.Y - 1; y++)
  506. for (s16 x = 1; x < roomsize.X - 1; x++) {
  507. v3s16 p = roomplace + v3s16(x, y, z);
  508. if (!vm->m_area.contains(p)) {
  509. fits = false;
  510. break;
  511. }
  512. if (vm->m_flags[vm->m_area.index(p)] & VMANIP_FLAG_DUNGEON_INSIDE) {
  513. fits = false;
  514. break;
  515. }
  516. }
  517. if (!fits) {
  518. // Find new place
  519. continue;
  520. }
  521. result_doorplace = doorplace;
  522. result_doordir = doordir;
  523. result_roomplace = roomplace;
  524. return true;
  525. }
  526. return false;
  527. }
  528. v3s16 rand_ortho_dir(PseudoRandom &random, bool diagonal_dirs)
  529. {
  530. // Make diagonal directions somewhat rare
  531. if (diagonal_dirs && (random.next() % 4 == 0)) {
  532. v3s16 dir;
  533. int trycount = 0;
  534. do {
  535. trycount++;
  536. dir.Z = random.next() % 3 - 1;
  537. dir.Y = 0;
  538. dir.X = random.next() % 3 - 1;
  539. } while ((dir.X == 0 || dir.Z == 0) && trycount < 10);
  540. return dir;
  541. }
  542. if (random.next() % 2 == 0)
  543. return random.next() % 2 ? v3s16(-1, 0, 0) : v3s16(1, 0, 0);
  544. return random.next() % 2 ? v3s16(0, 0, -1) : v3s16(0, 0, 1);
  545. }
  546. v3s16 turn_xz(v3s16 olddir, int t)
  547. {
  548. v3s16 dir;
  549. if (t == 0) {
  550. // Turn right
  551. dir.X = olddir.Z;
  552. dir.Z = -olddir.X;
  553. dir.Y = olddir.Y;
  554. } else {
  555. // Turn left
  556. dir.X = -olddir.Z;
  557. dir.Z = olddir.X;
  558. dir.Y = olddir.Y;
  559. }
  560. return dir;
  561. }
  562. v3s16 random_turn(PseudoRandom &random, v3s16 olddir)
  563. {
  564. int turn = random.range(0, 2);
  565. v3s16 dir;
  566. if (turn == 0)
  567. // Go straight
  568. dir = olddir;
  569. else if (turn == 1)
  570. // Turn right
  571. dir = turn_xz(olddir, 0);
  572. else
  573. // Turn left
  574. dir = turn_xz(olddir, 1);
  575. return dir;
  576. }
  577. int dir_to_facedir(v3s16 d)
  578. {
  579. if (abs(d.X) > abs(d.Z))
  580. return d.X < 0 ? 3 : 1;
  581. return d.Z < 0 ? 2 : 0;
  582. }