From fb31bd3806c56b467dbaaf4dba26aaff4224fdc6 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sun, 15 May 2016 14:49:15 +0200 Subject: [PATCH 001/418] Continue with 0.4.14-dev --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 998cdc9a3..592feb997 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ set(VERSION_PATCH 14) set(VERSION_EXTRA "" CACHE STRING "Stuff to append to version string") # Change to false for releases -set(DEVELOPMENT_BUILD FALSE) +set(DEVELOPMENT_BUILD TRUE) set(VERSION_STRING "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}") if(VERSION_EXTRA) From 01a784bd11d833724fdf826d805429d3a6f7b1f5 Mon Sep 17 00:00:00 2001 From: paramat Date: Thu, 12 May 2016 04:39:05 +0100 Subject: [PATCH 002/418] Documentation: Remove incorrect and excessive mapgen flags text --- builtin/settingtypes.txt | 10 ---------- minetest.conf.example | 10 ---------- 2 files changed, 20 deletions(-) diff --git a/builtin/settingtypes.txt b/builtin/settingtypes.txt index ad269d8b0..93fb8e952 100644 --- a/builtin/settingtypes.txt +++ b/builtin/settingtypes.txt @@ -848,8 +848,6 @@ map_generation_limit (Map generation limit) int 31000 0 31000 # Global map generation attributes. # In Mapgen v6 the 'decorations' flag controls all decorations except trees # and junglegrass, in all other mapgens this flag controls all decorations. -# The default flags set in the engine are: caves, light, decorations -# The flags string modifies the engine defaults. # Flags that are not specified in the flag string are not modified from the default. # Flags starting with 'no' are used to explicitly disable them. mg_flags (Mapgen flags) flags caves,dungeons,light,decorations caves,dungeons,light,decorations,nocaves,nodungeons,nolight,nodecorations @@ -910,8 +908,6 @@ mgv5_np_cave2 (Mapgen v5 cave2 noise parameters) noise_params 0, 12, (50, 50, 50 # Map generation attributes specific to Mapgen v6. # When snowbiomes are enabled jungles are automatically enabled, the 'jungles' flag is ignored. -# The default flags set in the engine are: biomeblend, mudflow -# The flags string modifies the engine defaults. # Flags that are not specified in the flag string are not modified from the default. # Flags starting with 'no' are used to explicitly disable them. mgv6_spflags (Mapgen v6 flags) flags jungles,biomeblend,mudflow,snowbiomes,trees jungles,biomeblend,mudflow,snowbiomes,flat,trees,nojungles,nobiomeblend,nomudflow,nosnowbiomes,noflat,notrees @@ -937,8 +933,6 @@ mgv6_np_apple_trees (Mapgen v6 apple trees noise parameters) noise_params 0, 1, # Map generation attributes specific to Mapgen v7. # The 'ridges' flag controls the rivers. -# The default flags set in the engine are: mountains, ridges -# The flags string modifies the engine defaults. # Flags that are not specified in the flag string are not modified from the default. # Flags starting with 'no' are used to explicitly disable them. mgv7_spflags (Mapgen v7 flags) flags mountains,ridges mountains,ridges,nomountains,noridges @@ -962,8 +956,6 @@ mgv7_np_cave2 (Mapgen v7 cave2 noise parameters) noise_params 0, 12, (100, 100, # Map generation attributes specific to Mapgen flat. # Occasional lakes and hills can be added to the flat world. -# The default flags set in the engine are: none -# The flags string modifies the engine defaults. # Flags that are not specified in the flag string are not modified from the default. # Flags starting with 'no' are used to explicitly disable them. mgflat_spflags (Mapgen flat flags) flags lakes,hills,,nolakes,nohills @@ -1079,8 +1071,6 @@ mgfractal_np_cave2 (Mapgen fractal cave2 noise parameters) noise_params 0, 12, ( # 'altitude_chill' makes higher elevations colder, which may cause biome issues. # 'humid_rivers' modifies the humidity around rivers and in areas where water would tend to pool, # it may interfere with delicately adjusted biomes. -# The default flags set in the engine are: altitude_chill, humid_rivers -# The flags string modifies the engine defaults. # Flags that are not specified in the flag string are not modified from the default. # Flags starting with 'no' are used to explicitly disable them. mg_valleys_spflags (Valleys C Flags) flags altitude_chill,humid_rivers altitude_chill,noaltitude_chill,humid_rivers,nohumid_rivers diff --git a/minetest.conf.example b/minetest.conf.example index 5090a620b..a5ca16e51 100644 --- a/minetest.conf.example +++ b/minetest.conf.example @@ -1046,8 +1046,6 @@ # Global map generation attributes. # In Mapgen v6 the 'decorations' flag controls all decorations except trees # and junglegrass, in all other mapgens this flag controls all decorations. -# The default flags set in the engine are: caves, light, decorations -# The flags string modifies the engine defaults. # Flags that are not specified in the flag string are not modified from the default. # Flags starting with 'no' are used to explicitly disable them. # type: flags possible values: caves, dungeons, light, decorations, nocaves, nodungeons, nolight, nodecorations @@ -1121,8 +1119,6 @@ # Map generation attributes specific to Mapgen v6. # When snowbiomes are enabled jungles are automatically enabled, the 'jungles' flag is ignored. -# The default flags set in the engine are: biomeblend, mudflow -# The flags string modifies the engine defaults. # Flags that are not specified in the flag string are not modified from the default. # Flags starting with 'no' are used to explicitly disable them. # type: flags possible values: jungles, biomeblend, mudflow, snowbiomes, flat, trees, nojungles, nobiomeblend, nomudflow, nosnowbiomes, noflat, notrees @@ -1173,8 +1169,6 @@ # Map generation attributes specific to Mapgen v7. # The 'ridges' flag controls the rivers. -# The default flags set in the engine are: mountains, ridges -# The flags string modifies the engine defaults. # Flags that are not specified in the flag string are not modified from the default. # Flags starting with 'no' are used to explicitly disable them. # type: flags possible values: mountains, ridges, nomountains, noridges @@ -1221,8 +1215,6 @@ # Map generation attributes specific to Mapgen flat. # Occasional lakes and hills can be added to the flat world. -# The default flags set in the engine are: none -# The flags string modifies the engine defaults. # Flags that are not specified in the flag string are not modified from the default. # Flags starting with 'no' are used to explicitly disable them. # type: flags possible values: lakes, hills, , nolakes, nohills @@ -1367,8 +1359,6 @@ # 'altitude_chill' makes higher elevations colder, which may cause biome issues. # 'humid_rivers' modifies the humidity around rivers and in areas where water would tend to pool, # it may interfere with delicately adjusted biomes. -# The default flags set in the engine are: altitude_chill, humid_rivers -# The flags string modifies the engine defaults. # Flags that are not specified in the flag string are not modified from the default. # Flags starting with 'no' are used to explicitly disable them. # type: flags possible values: altitude_chill, noaltitude_chill, humid_rivers, nohumid_rivers From 7a828de1bc658938243c0ddf85b7aab7a1087ccd Mon Sep 17 00:00:00 2001 From: est31 Date: Fri, 13 May 2016 00:14:24 +0200 Subject: [PATCH 003/418] Android: enable parallelism for main target too This adds to the changes that commit 98d16e0d9a945f5f48462c05f26ae4bde2db5731 "Android: Tell make about sub-makes to speed up build" did, and enables parallel builds for minetest itself as well. --- build/android/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/android/Makefile b/build/android/Makefile index 4ac9760f8..380296993 100644 --- a/build/android/Makefile +++ b/build/android/Makefile @@ -752,7 +752,7 @@ clean_assets : apk: local.properties assets $(ICONV_LIB) $(IRRLICHT_LIB) $(CURL_LIB) $(GMP_LIB) $(LEVELDB_TARGET) \ $(OPENAL_LIB) $(OGG_LIB) prep_srcdir $(ANDR_ROOT)/jni/src/android_version.h \ $(ANDR_ROOT)/jni/src/android_version_githash.h sqlite3_download - @${ANDROID_NDK}/ndk-build NDK_MODULE_PATH=${NDK_MODULE_PATH} \ + + @${ANDROID_NDK}/ndk-build NDK_MODULE_PATH=${NDK_MODULE_PATH} \ GPROF=${GPROF} APP_ABI=${TARGET_ABI} HAVE_LEVELDB=${HAVE_LEVELDB} \ APP_PLATFORM=${APP_PLATFORM} \ TARGET_LIBDIR=${TARGET_LIBDIR} \ From decbd396df0855f0a356c836cf7c0c5b133964cf Mon Sep 17 00:00:00 2001 From: sfan5 Date: Mon, 16 May 2016 12:42:51 +0200 Subject: [PATCH 004/418] Really fix ncurses lookup on Arch Linux Commit 27ee8d8943080a5dd735c9faa47c726604bafdff forgot to add the paths without ncursesw/ to the find_path() call --- cmake/Modules/FindNcursesw.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/Modules/FindNcursesw.cmake b/cmake/Modules/FindNcursesw.cmake index b8c7f78f2..e572c704a 100644 --- a/cmake/Modules/FindNcursesw.cmake +++ b/cmake/Modules/FindNcursesw.cmake @@ -115,7 +115,7 @@ if(CURSES_USE_NCURSESW) get_filename_component(_cursesParentDir "${_cursesLibDir}" PATH) find_path(CURSES_INCLUDE_PATH - NAMES ncursesw/ncurses.h ncursesw/curses.h + NAMES ncursesw/ncurses.h ncursesw/curses.h ncurses.h curses.h HINTS "${_cursesParentDir}/include" ) From 143401451c457da5079b2970fe260acea45bd85a Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sat, 14 May 2016 12:23:15 +0200 Subject: [PATCH 005/418] DB::loadBlock copy removal & DB backend cleanup * Remove the copy from db::loadBlock by using a pointer to the destination * cleanup db backend, the child backend doesn't have to set their functions as virtual --- src/database-dummy.cpp | 11 +++++++---- src/database-dummy.h | 8 ++++---- src/database-leveldb.cpp | 7 ++----- src/database-leveldb.h | 8 ++++---- src/database-redis.cpp | 11 +++++++---- src/database-redis.h | 12 ++++++------ src/database-sqlite3.cpp | 11 ++++------- src/database-sqlite3.h | 18 +++++++++--------- src/database.h | 2 +- src/main.cpp | 3 ++- src/map.cpp | 3 +-- 11 files changed, 47 insertions(+), 47 deletions(-) diff --git a/src/database-dummy.cpp b/src/database-dummy.cpp index b38db1fb9..ef2148f70 100644 --- a/src/database-dummy.cpp +++ b/src/database-dummy.cpp @@ -30,13 +30,16 @@ bool Database_Dummy::saveBlock(const v3s16 &pos, const std::string &data) return true; } -std::string Database_Dummy::loadBlock(const v3s16 &pos) +void Database_Dummy::loadBlock(const v3s16 &pos, std::string *block) { s64 i = getBlockAsInteger(pos); std::map::iterator it = m_database.find(i); - if (it == m_database.end()) - return ""; - return it->second; + if (it == m_database.end()) { + *block = ""; + return; + } + + *block = it->second; } bool Database_Dummy::deleteBlock(const v3s16 &pos) diff --git a/src/database-dummy.h b/src/database-dummy.h index 0cf56928e..72100edd0 100644 --- a/src/database-dummy.h +++ b/src/database-dummy.h @@ -28,10 +28,10 @@ with this program; if not, write to the Free Software Foundation, Inc., class Database_Dummy : public Database { public: - virtual bool saveBlock(const v3s16 &pos, const std::string &data); - virtual std::string loadBlock(const v3s16 &pos); - virtual bool deleteBlock(const v3s16 &pos); - virtual void listAllLoadableBlocks(std::vector &dst); + bool saveBlock(const v3s16 &pos, const std::string &data); + void loadBlock(const v3s16 &pos, std::string *block); + bool deleteBlock(const v3s16 &pos); + void listAllLoadableBlocks(std::vector &dst); private: std::map m_database; diff --git a/src/database-leveldb.cpp b/src/database-leveldb.cpp index acd0fd1eb..f46f82b98 100644 --- a/src/database-leveldb.cpp +++ b/src/database-leveldb.cpp @@ -65,16 +65,13 @@ bool Database_LevelDB::saveBlock(const v3s16 &pos, const std::string &data) return true; } -std::string Database_LevelDB::loadBlock(const v3s16 &pos) +void Database_LevelDB::loadBlock(const v3s16 &pos, std::string *block) { std::string datastr; leveldb::Status status = m_database->Get(leveldb::ReadOptions(), i64tos(getBlockAsInteger(pos)), &datastr); - if(status.ok()) - return datastr; - else - return ""; + *block = (status.ok()) ? datastr : ""; } bool Database_LevelDB::deleteBlock(const v3s16 &pos) diff --git a/src/database-leveldb.h b/src/database-leveldb.h index 4afe2fdc7..3993db0c3 100644 --- a/src/database-leveldb.h +++ b/src/database-leveldb.h @@ -34,10 +34,10 @@ public: Database_LevelDB(const std::string &savedir); ~Database_LevelDB(); - virtual bool saveBlock(const v3s16 &pos, const std::string &data); - virtual std::string loadBlock(const v3s16 &pos); - virtual bool deleteBlock(const v3s16 &pos); - virtual void listAllLoadableBlocks(std::vector &dst); + bool saveBlock(const v3s16 &pos, const std::string &data); + void loadBlock(const v3s16 &pos, std::string *block); + bool deleteBlock(const v3s16 &pos); + void listAllLoadableBlocks(std::vector &dst); private: leveldb::DB *m_database; diff --git a/src/database-redis.cpp b/src/database-redis.cpp index 498e9d39a..01427b6f9 100644 --- a/src/database-redis.cpp +++ b/src/database-redis.cpp @@ -101,7 +101,7 @@ bool Database_Redis::saveBlock(const v3s16 &pos, const std::string &data) return true; } -std::string Database_Redis::loadBlock(const v3s16 &pos) +void Database_Redis::loadBlock(const v3s16 &pos, std::string *block) { std::string tmp = i64tos(getBlockAsInteger(pos)); redisReply *reply = static_cast(redisCommand(ctx, @@ -111,12 +111,13 @@ std::string Database_Redis::loadBlock(const v3s16 &pos) throw FileNotGoodException(std::string( "Redis command 'HGET %s %s' failed: ") + ctx->errstr); } + switch (reply->type) { case REDIS_REPLY_STRING: { - std::string str(reply->str, reply->len); + *block = std::string(reply->str, reply->len); // std::string copies the memory so this won't cause any problems freeReplyObject(reply); - return str; + return; } case REDIS_REPLY_ERROR: { std::string errstr(reply->str, reply->len); @@ -127,11 +128,13 @@ std::string Database_Redis::loadBlock(const v3s16 &pos) "Redis command 'HGET %s %s' errored: ") + errstr); } case REDIS_REPLY_NIL: { + *block = ""; // block not found in database freeReplyObject(reply); - return ""; + return; } } + errorstream << "loadBlock: loading block " << PP(pos) << " returned invalid reply type " << reply->type << ": " << std::string(reply->str, reply->len) << std::endl; diff --git a/src/database-redis.h b/src/database-redis.h index 45e702c83..3addaa20a 100644 --- a/src/database-redis.h +++ b/src/database-redis.h @@ -36,13 +36,13 @@ public: Database_Redis(Settings &conf); ~Database_Redis(); - virtual void beginSave(); - virtual void endSave(); + void beginSave(); + void endSave(); - virtual bool saveBlock(const v3s16 &pos, const std::string &data); - virtual std::string loadBlock(const v3s16 &pos); - virtual bool deleteBlock(const v3s16 &pos); - virtual void listAllLoadableBlocks(std::vector &dst); + bool saveBlock(const v3s16 &pos, const std::string &data); + void loadBlock(const v3s16 &pos, std::string *block); + bool deleteBlock(const v3s16 &pos); + void listAllLoadableBlocks(std::vector &dst); private: redisContext *ctx; diff --git a/src/database-sqlite3.cpp b/src/database-sqlite3.cpp index 56f937bf2..2a23e2ea0 100644 --- a/src/database-sqlite3.cpp +++ b/src/database-sqlite3.cpp @@ -237,7 +237,7 @@ bool Database_SQLite3::saveBlock(const v3s16 &pos, const std::string &data) return true; } -std::string Database_SQLite3::loadBlock(const v3s16 &pos) +void Database_SQLite3::loadBlock(const v3s16 &pos, std::string *block) { verifyDatabase(); @@ -245,20 +245,17 @@ std::string Database_SQLite3::loadBlock(const v3s16 &pos) if (sqlite3_step(m_stmt_read) != SQLITE_ROW) { sqlite3_reset(m_stmt_read); - return ""; + return; } + const char *data = (const char *) sqlite3_column_blob(m_stmt_read, 0); size_t len = sqlite3_column_bytes(m_stmt_read, 0); - std::string s; - if (data) - s = std::string(data, len); + *block = (data) ? std::string(data, len) : ""; sqlite3_step(m_stmt_read); // We should never get more than 1 row, so ok to reset sqlite3_reset(m_stmt_read); - - return s; } void Database_SQLite3::createDatabase() diff --git a/src/database-sqlite3.h b/src/database-sqlite3.h index 04a1825d9..debbc9d8b 100644 --- a/src/database-sqlite3.h +++ b/src/database-sqlite3.h @@ -31,17 +31,17 @@ class Database_SQLite3 : public Database { public: Database_SQLite3(const std::string &savedir); - - virtual void beginSave(); - virtual void endSave(); - - virtual bool saveBlock(const v3s16 &pos, const std::string &data); - virtual std::string loadBlock(const v3s16 &pos); - virtual bool deleteBlock(const v3s16 &pos); - virtual void listAllLoadableBlocks(std::vector &dst); - virtual bool initialized() const { return m_initialized; } ~Database_SQLite3(); + void beginSave(); + void endSave(); + + bool saveBlock(const v3s16 &pos, const std::string &data); + void loadBlock(const v3s16 &pos, std::string *block); + bool deleteBlock(const v3s16 &pos); + void listAllLoadableBlocks(std::vector &dst); + bool initialized() const { return m_initialized; } + private: // Open the database void openDatabase(); diff --git a/src/database.h b/src/database.h index cee7b6fd9..0cf75232f 100644 --- a/src/database.h +++ b/src/database.h @@ -38,7 +38,7 @@ public: virtual void endSave() {} virtual bool saveBlock(const v3s16 &pos, const std::string &data) = 0; - virtual std::string loadBlock(const v3s16 &pos) = 0; + virtual void loadBlock(const v3s16 &pos, std::string *block) = 0; virtual bool deleteBlock(const v3s16 &pos) = 0; static s64 getBlockAsInteger(const v3s16 &pos); diff --git a/src/main.cpp b/src/main.cpp index 1b95a9f1c..f63b6a986 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -948,7 +948,8 @@ static bool migrate_database(const GameParams &game_params, const Settings &cmd_ for (std::vector::const_iterator it = blocks.begin(); it != blocks.end(); ++it) { if (kill) return false; - const std::string &data = old_db->loadBlock(*it); + std::string data; + old_db->loadBlock(*it, &data); if (!data.empty()) { new_db->saveBlock(*it, data); } else { diff --git a/src/map.cpp b/src/map.cpp index 66fabaf87..848d2ef13 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -3442,8 +3442,7 @@ MapBlock* ServerMap::loadBlock(v3s16 blockpos) v2s16 p2d(blockpos.X, blockpos.Z); std::string ret; - - ret = dbase->loadBlock(blockpos); + dbase->loadBlock(blockpos, &ret); if (ret != "") { loadBlock(&ret, blockpos, createSector(p2d), false); return getBlockNoCreateNoEx(blockpos); From 8b940c005fd13db4e611ec49bc44881048b42cf7 Mon Sep 17 00:00:00 2001 From: Wayward One Date: Sat, 30 Apr 2016 10:31:11 -0400 Subject: [PATCH 006/418] Add on_punchnode callback --- builtin/game/falling.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/builtin/game/falling.lua b/builtin/game/falling.lua index 57bb98cfd..fa7ff24bc 100644 --- a/builtin/game/falling.lua +++ b/builtin/game/falling.lua @@ -258,3 +258,8 @@ function on_dignode(p, node) nodeupdate(p) end core.register_on_dignode(on_dignode) + +function on_punchnode(p, node) + nodeupdate(p) +end +core.register_on_punchnode(on_punchnode) From 8ba6d9f227398d2004d1fe7ae095f5e342dc6d7b Mon Sep 17 00:00:00 2001 From: nerzhul Date: Tue, 17 May 2016 09:59:02 +0200 Subject: [PATCH 007/418] Implement DatabaseException for databases --- src/database-leveldb.cpp | 2 +- src/database-redis.cpp | 20 ++++++++++---------- src/database-sqlite3.cpp | 2 +- src/exceptions.h | 5 +++++ 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/database-leveldb.cpp b/src/database-leveldb.cpp index f46f82b98..4a4904c6a 100644 --- a/src/database-leveldb.cpp +++ b/src/database-leveldb.cpp @@ -33,7 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #define ENSURE_STATUS_OK(s) \ if (!(s).ok()) { \ - throw FileNotGoodException(std::string("LevelDB error: ") + \ + throw DatabaseException(std::string("LevelDB error: ") + \ (s).ToString()); \ } diff --git a/src/database-redis.cpp b/src/database-redis.cpp index 01427b6f9..3bcedad9b 100644 --- a/src/database-redis.cpp +++ b/src/database-redis.cpp @@ -46,11 +46,11 @@ Database_Redis::Database_Redis(Settings &conf) int port = conf.exists("redis_port") ? conf.getU16("redis_port") : 6379; ctx = redisConnect(addr, port); if (!ctx) { - throw FileNotGoodException("Cannot allocate redis context"); + throw DatabaseException("Cannot allocate redis context"); } else if (ctx->err) { std::string err = std::string("Connection error: ") + ctx->errstr; redisFree(ctx); - throw FileNotGoodException(err); + throw DatabaseException(err); } } @@ -62,7 +62,7 @@ Database_Redis::~Database_Redis() void Database_Redis::beginSave() { redisReply *reply = static_cast(redisCommand(ctx, "MULTI")); if (!reply) { - throw FileNotGoodException(std::string( + throw DatabaseException(std::string( "Redis command 'MULTI' failed: ") + ctx->errstr); } freeReplyObject(reply); @@ -71,7 +71,7 @@ void Database_Redis::beginSave() { void Database_Redis::endSave() { redisReply *reply = static_cast(redisCommand(ctx, "EXEC")); if (!reply) { - throw FileNotGoodException(std::string( + throw DatabaseException(std::string( "Redis command 'EXEC' failed: ") + ctx->errstr); } freeReplyObject(reply); @@ -108,7 +108,7 @@ void Database_Redis::loadBlock(const v3s16 &pos, std::string *block) "HGET %s %s", hash.c_str(), tmp.c_str())); if (!reply) { - throw FileNotGoodException(std::string( + throw DatabaseException(std::string( "Redis command 'HGET %s %s' failed: ") + ctx->errstr); } @@ -124,7 +124,7 @@ void Database_Redis::loadBlock(const v3s16 &pos, std::string *block) freeReplyObject(reply); errorstream << "loadBlock: loading block " << PP(pos) << " failed: " << errstr << std::endl; - throw FileNotGoodException(std::string( + throw DatabaseException(std::string( "Redis command 'HGET %s %s' errored: ") + errstr); } case REDIS_REPLY_NIL: { @@ -139,7 +139,7 @@ void Database_Redis::loadBlock(const v3s16 &pos, std::string *block) << " returned invalid reply type " << reply->type << ": " << std::string(reply->str, reply->len) << std::endl; freeReplyObject(reply); - throw FileNotGoodException(std::string( + throw DatabaseException(std::string( "Redis command 'HGET %s %s' gave invalid reply.")); } @@ -150,7 +150,7 @@ bool Database_Redis::deleteBlock(const v3s16 &pos) redisReply *reply = static_cast(redisCommand(ctx, "HDEL %s %s", hash.c_str(), tmp.c_str())); if (!reply) { - throw FileNotGoodException(std::string( + throw DatabaseException(std::string( "Redis command 'HDEL %s %s' failed: ") + ctx->errstr); } else if (reply->type == REDIS_REPLY_ERROR) { warningstream << "deleteBlock: deleting block " << PP(pos) @@ -167,7 +167,7 @@ void Database_Redis::listAllLoadableBlocks(std::vector &dst) { redisReply *reply = static_cast(redisCommand(ctx, "HKEYS %s", hash.c_str())); if (!reply) { - throw FileNotGoodException(std::string( + throw DatabaseException(std::string( "Redis command 'HKEYS %s' failed: ") + ctx->errstr); } switch (reply->type) { @@ -179,7 +179,7 @@ void Database_Redis::listAllLoadableBlocks(std::vector &dst) } break; case REDIS_REPLY_ERROR: - throw FileNotGoodException(std::string( + throw DatabaseException(std::string( "Failed to get keys from database: ") + std::string(reply->str, reply->len)); } diff --git a/src/database-sqlite3.cpp b/src/database-sqlite3.cpp index 2a23e2ea0..07df976d4 100644 --- a/src/database-sqlite3.cpp +++ b/src/database-sqlite3.cpp @@ -47,7 +47,7 @@ SQLite format specification: #define SQLRES(s, r, m) \ if ((s) != (r)) { \ - throw FileNotGoodException(std::string(m) + ": " +\ + throw DatabaseException(std::string(m) + ": " +\ sqlite3_errmsg(m_database)); \ } #define SQLOK(s, m) SQLRES(s, SQLITE_OK, m) diff --git a/src/exceptions.h b/src/exceptions.h index 4f18f70d9..67a2d0df6 100644 --- a/src/exceptions.h +++ b/src/exceptions.h @@ -65,6 +65,11 @@ public: FileNotGoodException(const std::string &s): BaseException(s) {} }; +class DatabaseException : public BaseException { +public: + DatabaseException(const std::string &s): BaseException(s) {} +}; + class SerializationError : public BaseException { public: SerializationError(const std::string &s): BaseException(s) {} From 88acda02567d586820867059718cac551dc7a58b Mon Sep 17 00:00:00 2001 From: Craig Robbins Date: Fri, 20 May 2016 21:18:59 +1000 Subject: [PATCH 008/418] Fix tooltip height for versions of irrlicht < 1.8.2 Version 1.8.2 of irrlicht changed the way that IGUIStaticText::getTextHeight() works and since that release properly deals with newlines. From irrlicht changes.txt for 1.8.2, "IGUIStaticText::getTextHeight returns now the correct height for texts with newlines even WordWrap is not set." --- src/guiFormSpecMenu.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/guiFormSpecMenu.cpp b/src/guiFormSpecMenu.cpp index 2bf06c1d6..b0215a84a 100644 --- a/src/guiFormSpecMenu.cpp +++ b/src/guiFormSpecMenu.cpp @@ -2265,7 +2265,11 @@ void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase, this->bringToFront(m_tooltip_element); m_tooltip_element->setText(tooltip_text.c_str()); s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height; +#if IRRLICHT_VERSION_MAJOR >= 1 && IRRLICHT_VERSION_MINOR >= 8 && IRRLICHT_VERSION_REVISION >= 2 s32 tooltip_height = m_tooltip_element->getTextHeight() + 5; +#else + s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5; +#endif v2u32 screenSize = driver->getScreenSize(); int tooltip_offset_x = m_btn_height; int tooltip_offset_y = m_btn_height; From 643ac9dd7aa1852b24348d853e82aa267dade789 Mon Sep 17 00:00:00 2001 From: paramat Date: Tue, 17 May 2016 00:22:27 +0100 Subject: [PATCH 009/418] Item entities: Don't show description as infotext Partially reverts #3547 Infotext remains optional for objects, empty by default --- builtin/game/item_entity.lua | 4 ---- 1 file changed, 4 deletions(-) diff --git a/builtin/game/item_entity.lua b/builtin/game/item_entity.lua index a66bf33d0..be158c119 100644 --- a/builtin/game/item_entity.lua +++ b/builtin/game/item_entity.lua @@ -31,7 +31,6 @@ core.register_entity(":__builtin:item", { spritediv = {x = 1, y = 1}, initial_sprite_basepos = {x = 0, y = 0}, is_visible = false, - infotext = "", }, itemstring = '', @@ -51,7 +50,6 @@ core.register_entity(":__builtin:item", { local c = s local itemtable = stack:to_table() local itemname = nil - local description = "" if itemtable then itemname = stack:to_table().name end @@ -60,7 +58,6 @@ core.register_entity(":__builtin:item", { if core.registered_items[itemname] then item_texture = core.registered_items[itemname].inventory_image item_type = core.registered_items[itemname].type - description = core.registered_items[itemname].description end local prop = { is_visible = true, @@ -69,7 +66,6 @@ core.register_entity(":__builtin:item", { visual_size = {x = s, y = s}, collisionbox = {-c, -c, -c, c, c, c}, automatic_rotate = math.pi * 0.5, - infotext = description, } self.object:set_properties(prop) end, From 0f184d77c871b564b773b7fb81e7a3f16a197813 Mon Sep 17 00:00:00 2001 From: HybridDog Date: Fri, 20 May 2016 22:08:07 +0200 Subject: [PATCH 010/418] Gitignore: ignore idea and ninja files --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index f9c664da6..d7e0daab4 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ tags !tags/ gtags.files +.idea/* ## Files related to minetest development cycle /*.patch @@ -71,6 +72,8 @@ locale/ *.layout *.o *.a +*.ninja +.ninja* ## Android build files build/android/src/main/assets From ce42ff9cf74ebb8d4b68bc78c95e90ea3db02b78 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sat, 14 May 2016 11:00:42 +0200 Subject: [PATCH 011/418] Implement a PostgreSQL backend --- README.txt | 5 +- src/CMakeLists.txt | 37 +++++ src/cmake_config.h.in | 1 + src/database-postgresql.cpp | 286 ++++++++++++++++++++++++++++++++++ src/database-postgresql.h | 95 +++++++++++ src/map.cpp | 7 + util/travis/before_install.sh | 4 +- 7 files changed, 433 insertions(+), 2 deletions(-) create mode 100644 src/database-postgresql.cpp create mode 100644 src/database-postgresql.h diff --git a/README.txt b/README.txt index 9ca9b331e..bd2dadebb 100644 --- a/README.txt +++ b/README.txt @@ -169,7 +169,8 @@ ENABLE_CURSES - Build with (n)curses; Enables a server side terminal (comm ENABLE_FREETYPE - Build with FreeType2; Allows using TTF fonts ENABLE_GETTEXT - Build with Gettext; Allows using translations ENABLE_GLES - Search for Open GLES headers & libraries and use them -ENABLE_LEVELDB - Build with LevelDB; Enables use of LevelDB map backend (faster than SQLite3) +ENABLE_LEVELDB - Build with LevelDB; Enables use of LevelDB map backend +ENABLE_POSTGRESQL - Build with libpq; Enables use of PostgreSQL map backend (PostgreSQL 9.5 or greater required) ENABLE_REDIS - Build with libhiredis; Enables use of Redis map backend ENABLE_SPATIAL - Build with LibSpatial; Speeds up AreaStores ENABLE_SOUND - Build with OpenAL, libogg & libvorbis; in-game Sounds @@ -203,6 +204,8 @@ IRRLICHT_LIBRARY - Path to libIrrlicht.a/libIrrlicht.so/libIrrlic LEVELDB_INCLUDE_DIR - Only when building with LevelDB; directory that contains db.h LEVELDB_LIBRARY - Only when building with LevelDB; path to libleveldb.a/libleveldb.so/libleveldb.dll.a LEVELDB_DLL - Only when building with LevelDB on Windows; path to libleveldb.dll +POSTGRESQL_INCLUDE_DIR - Only when building with PostgreSQL; directory that contains libpq-fe.h +POSTGRESQL_LIBRARY - Only when building with PostgreSQL; path to libpq.a/libpq.so REDIS_INCLUDE_DIR - Only when building with Redis; directory that contains hiredis.h REDIS_LIBRARY - Only when building with Redis; path to libhiredis.a/libhiredis.so SPATIAL_INCLUDE_DIR - Only when building with LibSpatial; directory that contains spatialindex/SpatialIndex.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index feca199c1..ea1564eeb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -189,6 +189,36 @@ if(ENABLE_CURSES) endif() endif(ENABLE_CURSES) +option(ENABLE_POSTGRESQL "Enable PostgreSQL backend" TRUE) +set(USE_POSTGRESQL FALSE) + +if(ENABLE_POSTGRESQL) + find_program(POSTGRESQL_CONFIG_EXECUTABLE pg_config DOC "pg_config") + find_library(POSTGRESQL_LIBRARY pq) + if(POSTGRESQL_CONFIG_EXECUTABLE) + execute_process(COMMAND ${POSTGRESQL_CONFIG_EXECUTABLE} --includedir-server + OUTPUT_VARIABLE POSTGRESQL_SERVER_INCLUDE_DIRS + OUTPUT_STRIP_TRAILING_WHITESPACE) + execute_process(COMMAND ${POSTGRESQL_CONFIG_EXECUTABLE} + OUTPUT_VARIABLE POSTGRESQL_CLIENT_INCLUDE_DIRS + OUTPUT_STRIP_TRAILING_WHITESPACE) + # This variable is case sensitive for the cmake PostgreSQL module + set(PostgreSQL_ADDITIONAL_SEARCH_PATHS ${POSTGRESQL_SERVER_INCLUDE_DIRS} ${POSTGRESQL_CLIENT_INCLUDE_DIRS}) + endif() + + find_package("PostgreSQL") + + if(POSTGRESQL_FOUND) + set(USE_POSTGRESQL TRUE) + message(STATUS "PostgreSQL backend enabled") + # This variable is case sensitive, don't try to change it to POSTGRESQL_INCLUDE_DIR + message(STATUS "PostgreSQL includes: ${PostgreSQL_INCLUDE_DIR}") + include_directories(${PostgreSQL_INCLUDE_DIR}) + else() + message(STATUS "PostgreSQL not found!") + endif() +endif(ENABLE_POSTGRESQL) + option(ENABLE_LEVELDB "Enable LevelDB backend" TRUE) set(USE_LEVELDB FALSE) @@ -361,6 +391,7 @@ set(common_SRCS craftdef.cpp database-dummy.cpp database-leveldb.cpp + database-postgresql.cpp database-redis.cpp database-sqlite3.cpp database.cpp @@ -592,6 +623,9 @@ if(BUILD_CLIENT) if (USE_CURSES) target_link_libraries(${PROJECT_NAME} ${CURSES_LIBRARIES}) endif() + if (USE_POSTGRESQL) + target_link_libraries(${PROJECT_NAME} ${POSTGRESQL_LIBRARY}) + endif() if (USE_LEVELDB) target_link_libraries(${PROJECT_NAME} ${LEVELDB_LIBRARY}) endif() @@ -622,6 +656,9 @@ if(BUILD_SERVER) if (USE_CURSES) target_link_libraries(${PROJECT_NAME}server ${CURSES_LIBRARIES}) endif() + if (USE_POSTGRESQL) + target_link_libraries(${PROJECT_NAME}server ${POSTGRESQL_LIBRARY}) + endif() if (USE_LEVELDB) target_link_libraries(${PROJECT_NAME}server ${LEVELDB_LIBRARY}) endif() diff --git a/src/cmake_config.h.in b/src/cmake_config.h.in index 018532d13..50f34a0b8 100644 --- a/src/cmake_config.h.in +++ b/src/cmake_config.h.in @@ -22,6 +22,7 @@ #cmakedefine01 USE_CURSES #cmakedefine01 USE_LEVELDB #cmakedefine01 USE_LUAJIT +#cmakedefine01 USE_POSTGRESQL #cmakedefine01 USE_SPATIAL #cmakedefine01 USE_SYSTEM_GMP #cmakedefine01 USE_REDIS diff --git a/src/database-postgresql.cpp b/src/database-postgresql.cpp new file mode 100644 index 000000000..3b6b42aea --- /dev/null +++ b/src/database-postgresql.cpp @@ -0,0 +1,286 @@ +/* +Copyright (C) 2016 Loic Blot + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "config.h" + +#if USE_POSTGRESQL + +#include "database-postgresql.h" + +#ifdef _WIN32 + #ifndef WIN32_LEAN_AND_MEAN + #define WIN32_LEAN_AND_MEAN + #endif + // Without this some of the network functions are not found on mingw + #ifndef _WIN32_WINNT + #define _WIN32_WINNT 0x0501 + #endif + #include + #include +#else +#include +#endif + +#include "log.h" +#include "exceptions.h" +#include "settings.h" + +Database_PostgreSQL::Database_PostgreSQL(const Settings &conf) : + m_connect_string(""), + m_conn(NULL), + m_pgversion(0) +{ + if (!conf.getNoEx("pgsql_connection", m_connect_string)) { + throw SettingNotFoundException( + "Set pgsql_connection string in world.mt to " + "use the postgresql backend\n" + "Notes:\n" + "pgsql_connection has the following form: \n" + "\tpgsql_connection = host=127.0.0.1 port=5432 user=mt_user " + "password=mt_password dbname=minetest_world\n" + "mt_user should have CREATE TABLE, INSERT, SELECT, UPDATE and " + "DELETE rights on the database.\n" + "Don't create mt_user as a SUPERUSER!"); + } + + connectToDatabase(); +} + +Database_PostgreSQL::~Database_PostgreSQL() +{ + PQfinish(m_conn); +} + +void Database_PostgreSQL::connectToDatabase() +{ + m_conn = PQconnectdb(m_connect_string.c_str()); + + if (PQstatus(m_conn) != CONNECTION_OK) { + throw DatabaseException(std::string( + "PostgreSQL database error: ") + + PQerrorMessage(m_conn)); + } + + m_pgversion = PQserverVersion(m_conn); + + /* + * We are using UPSERT feature from PostgreSQL 9.5 + * to have the better performance, + * set the minimum version to 90500 + */ + if (m_pgversion < 90500) { + throw DatabaseException("PostgreSQL database error: " + "Server version 9.5 or greater required."); + } + + infostream << "PostgreSQL Database: Version " << m_pgversion + << " Connection made." << std::endl; + + createDatabase(); + initStatements(); +} + +void Database_PostgreSQL::verifyDatabase() +{ + if (PQstatus(m_conn) == CONNECTION_OK) + return; + + PQreset(m_conn); + ping(); +} + +void Database_PostgreSQL::ping() +{ + if (PQping(m_connect_string.c_str()) != PQPING_OK) { + throw DatabaseException(std::string( + "PostgreSQL database error: ") + + PQerrorMessage(m_conn)); + } +} + +bool Database_PostgreSQL::initialized() const +{ + return (PQstatus(m_conn) == CONNECTION_OK); +} + +void Database_PostgreSQL::initStatements() +{ + prepareStatement("read_block", + "SELECT data FROM blocks " + "WHERE posX = $1::int4 AND posY = $2::int4 AND " + "posZ = $3::int4"); + + prepareStatement("write_block", + "INSERT INTO blocks (posX, posY, posZ, data) VALUES " + "($1::int4, $2::int4, $3::int4, $4::bytea) " + "ON CONFLICT ON CONSTRAINT blocks_pkey DO " + "UPDATE SET data = $4::bytea"); + + prepareStatement("delete_block", "DELETE FROM blocks WHERE " + "posX = $1::int4 AND posY = $2::int4 AND posZ = $3::int4"); + + prepareStatement("list_all_loadable_blocks", + "SELECT posX, posY, posZ FROM blocks"); +} + +PGresult *Database_PostgreSQL::checkResults(PGresult *result, bool clear) +{ + ExecStatusType statusType = PQresultStatus(result); + + switch (statusType) { + case PGRES_COMMAND_OK: + case PGRES_TUPLES_OK: + break; + case PGRES_FATAL_ERROR: + default: + throw DatabaseException( + std::string("PostgreSQL database error: ") + + PQresultErrorMessage(result)); + } + + if (clear) + PQclear(result); + + return result; +} + +void Database_PostgreSQL::createDatabase() +{ + PGresult *result = checkResults(PQexec(m_conn, + "SELECT relname FROM pg_class WHERE relname='blocks';"), + false); + + // If table doesn't exist, create it + if (!PQntuples(result)) { + static const char* dbcreate_sql = "CREATE TABLE blocks (" + "posX INT NOT NULL," + "posY INT NOT NULL," + "posZ INT NOT NULL," + "data BYTEA," + "PRIMARY KEY (posX,posY,posZ)" + ");"; + checkResults(PQexec(m_conn, dbcreate_sql)); + } + + PQclear(result); + + infostream << "PostgreSQL: Game Database was inited." << std::endl; +} + + +void Database_PostgreSQL::beginSave() +{ + verifyDatabase(); + checkResults(PQexec(m_conn, "BEGIN;")); +} + +void Database_PostgreSQL::endSave() +{ + checkResults(PQexec(m_conn, "COMMIT;")); +} + +bool Database_PostgreSQL::saveBlock(const v3s16 &pos, + const std::string &data) +{ + // Verify if we don't overflow the platform integer with the mapblock size + if (data.size() > INT_MAX) { + errorstream << "Database_PostgreSQL::saveBlock: Data truncation! " + << "data.size() over 0xFFFF (== " << data.size() + << ")" << std::endl; + return false; + } + + verifyDatabase(); + + s32 x, y, z; + x = htonl(pos.X); + y = htonl(pos.Y); + z = htonl(pos.Z); + + const void *args[] = { &x, &y, &z, data.c_str() }; + const int argLen[] = { + sizeof(x), sizeof(y), sizeof(z), (int)data.size() + }; + const int argFmt[] = { 1, 1, 1, 1 }; + + execPrepared("write_block", ARRLEN(args), args, argLen, argFmt); + return true; +} + +void Database_PostgreSQL::loadBlock(const v3s16 &pos, + std::string *block) +{ + verifyDatabase(); + + s32 x, y, z; + x = htonl(pos.X); + y = htonl(pos.Y); + z = htonl(pos.Z); + + const void *args[] = { &x, &y, &z }; + const int argLen[] = { sizeof(x), sizeof(y), sizeof(z) }; + const int argFmt[] = { 1, 1, 1 }; + + PGresult *results = execPrepared("read_block", ARRLEN(args), args, + argLen, argFmt, false); + + *block = ""; + + if (PQntuples(results)) { + *block = std::string(PQgetvalue(results, 0, 0), + PQgetlength(results, 0, 0)); + } + + PQclear(results); +} + +bool Database_PostgreSQL::deleteBlock(const v3s16 &pos) +{ + verifyDatabase(); + + s32 x, y, z; + x = htonl(pos.X); + y = htonl(pos.Y); + z = htonl(pos.Z); + + const void *args[] = { &x, &y, &z }; + const int argLen[] = { sizeof(x), sizeof(y), sizeof(z) }; + const int argFmt[] = { 1, 1, 1 }; + + execPrepared("read_block", ARRLEN(args), args, argLen, argFmt); + + return true; +} + +void Database_PostgreSQL::listAllLoadableBlocks(std::vector &dst) +{ + verifyDatabase(); + + PGresult *results = execPrepared("list_all_loadable_blocks", 0, + NULL, NULL, NULL, false, false); + + int numrows = PQntuples(results); + + for (int row = 0; row < numrows; ++row) { + dst.push_back(pg_to_v3s16(results, 0, 0)); + } + + PQclear(results); +} + +#endif // USE_POSTGRESQL diff --git a/src/database-postgresql.h b/src/database-postgresql.h new file mode 100644 index 000000000..1cfa544e3 --- /dev/null +++ b/src/database-postgresql.h @@ -0,0 +1,95 @@ +/* +Minetest +Copyright (C) 2013 celeron55, Perttu Ahola + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef DATABASE_POSTGRESQL_HEADER +#define DATABASE_POSTGRESQL_HEADER + +#include +#include +#include "database.h" +#include "util/basic_macros.h" + +class Settings; + +class Database_PostgreSQL : public Database +{ +public: + Database_PostgreSQL(const Settings &conf); + ~Database_PostgreSQL(); + + void beginSave(); + void endSave(); + + bool saveBlock(const v3s16 &pos, const std::string &data); + void loadBlock(const v3s16 &pos, std::string *block); + bool deleteBlock(const v3s16 &pos); + void listAllLoadableBlocks(std::vector &dst); + bool initialized() const; + +private: + // Database initialization + void connectToDatabase(); + void initStatements(); + void createDatabase(); + + inline void prepareStatement(const std::string &name, const std::string &sql) + { + checkResults(PQprepare(m_conn, name.c_str(), sql.c_str(), 0, NULL)); + } + + // Database connectivity checks + void ping(); + void verifyDatabase(); + + // Database usage + PGresult *checkResults(PGresult *res, bool clear = true); + + inline PGresult *execPrepared(const char *stmtName, const int paramsNumber, + const void **params, + const int *paramsLengths = NULL, const int *paramsFormats = NULL, + bool clear = true, bool nobinary = true) + { + return checkResults(PQexecPrepared(m_conn, stmtName, paramsNumber, + (const char* const*) params, paramsLengths, paramsFormats, + nobinary ? 1 : 0), clear); + } + + // Conversion helpers + inline int pg_to_int(PGresult *res, int row, int col) + { + return atoi(PQgetvalue(res, row, col)); + } + + inline v3s16 pg_to_v3s16(PGresult *res, int row, int col) + { + return v3s16( + pg_to_int(res, row, col), + pg_to_int(res, row, col + 1), + pg_to_int(res, row, col + 2) + ); + } + + // Attributes + std::string m_connect_string; + PGconn *m_conn; + int m_pgversion; +}; + +#endif + diff --git a/src/map.cpp b/src/map.cpp index 848d2ef13..03daf4fa8 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -50,6 +50,9 @@ with this program; if not, write to the Free Software Foundation, Inc., #if USE_REDIS #include "database-redis.h" #endif +#if USE_POSTGRESQL +#include "database-postgresql.h" +#endif #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")" @@ -3240,6 +3243,10 @@ Database *ServerMap::createDatabase( else if (name == "redis") return new Database_Redis(conf); #endif + #if USE_POSTGRESQL + else if (name == "postgresql") + return new Database_PostgreSQL(conf); + #endif else throw BaseException(std::string("Database backend ") + name + " not supported."); } diff --git a/util/travis/before_install.sh b/util/travis/before_install.sh index 58dc42b17..70037389b 100755 --- a/util/travis/before_install.sh +++ b/util/travis/before_install.sh @@ -17,13 +17,15 @@ if [[ $PLATFORM == "Unix" ]]; then if [[ $TRAVIS_OS_NAME == "linux" ]]; then sudo apt-get install libirrlicht-dev cmake libbz2-dev libpng12-dev \ libjpeg-dev libxxf86vm-dev libgl1-mesa-dev libsqlite3-dev \ - libhiredis-dev libogg-dev libgmp-dev libvorbis-dev libopenal-dev gettext + libhiredis-dev libogg-dev libgmp-dev libvorbis-dev libopenal-dev \ + gettext libpq-dev postgresql-server-dev-all # Linking to LevelDB is broken, use a custom build wget http://minetest.kitsunemimi.pw/libleveldb-1.18-ubuntu12.04.7z sudo 7z x -o/usr libleveldb-1.18-ubuntu12.04.7z else brew update brew install freetype gettext hiredis irrlicht jpeg leveldb libogg libvorbis luajit + brew upgrade postgresql fi elif [[ $PLATFORM == "Win32" ]]; then wget http://minetest.kitsunemimi.pw/mingw_w64_i686_ubuntu12.04_4.9.1.7z -O mingw.7z From f64a6259b2b5e79103cfd4bc151304e6da39bca9 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sun, 22 May 2016 13:31:41 +0200 Subject: [PATCH 012/418] Fix a m_camera not used warning fix pointed by clang --- src/content_cao.cpp | 3 +-- src/content_cao.h | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/content_cao.cpp b/src/content_cao.cpp index d701e4f72..35ab1c508 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -546,7 +546,6 @@ GenericCAO::GenericCAO(IGameDef *gamedef, ClientEnvironment *env): // m_smgr(NULL), m_irr(NULL), - m_camera(NULL), m_gamedef(NULL), m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.), m_meshnode(NULL), @@ -804,7 +803,7 @@ void GenericCAO::removeFromScene(bool permanent) } } -void GenericCAO::addToScene(scene::ISceneManager *smgr, +void GenericCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc, IrrlichtDevice *irr) { m_smgr = smgr; diff --git a/src/content_cao.h b/src/content_cao.h index a166ff494..bf99fd3ba 100644 --- a/src/content_cao.h +++ b/src/content_cao.h @@ -68,7 +68,6 @@ private: // scene::ISceneManager *m_smgr; IrrlichtDevice *m_irr; - Camera* m_camera; IGameDef *m_gamedef; aabb3f m_selection_box; scene::IMeshSceneNode *m_meshnode; @@ -206,7 +205,7 @@ public: float time_from_last_punch=1000000); std::string debugInfoText(); - + std::string infoText() { return m_prop.infotext; From 423d8c1b0d3841f6ce9756ab0d6b2e10408fc89b Mon Sep 17 00:00:00 2001 From: est31 Date: Wed, 18 May 2016 06:18:08 +0200 Subject: [PATCH 013/418] Tolerate packet reordering in the early init process Fixes a bug where packet reordering made the server give the client two peer ids instead of one. This in turn confused reliable packet sending and made connecting to the server fail. The client usually sends three packets at init: one "dummy" packet consisting of two 0 bytes, and the init packet as well as its legacy counterpart. The last one can be turned off since commit af30183124d40a969040d7de4b3a487feec466e4, but this is of lower relevance for the bug. The relevant part here is that network packet reorder (which is a normal occurence) can make the packets reach the server in different order. If reorder puts the dummy packet further behind, the following would happen before the patch: 1. The server will get one of the init packets on channel 1 and assign the client a peer id, as the packet will have zero as peer id. 2. The server sends a CONTROLTYPE_SET_PEER_ID packet to inform the client of the peer id. 3. The next packet from the client will contain the peer id set by the server. 4. The server sets the m_has_sent_with_id member for the client's peer structure to true. 5. Now the dummy packet arrives. It has a peer id of zero, therefore the server searches whether it already has a peer id for the address the packet was sent from. The search fails because m_has_sent_with_id was set to true and the server only searched for peers with m_has_sent_with_id set to false. 6. In a working setup, the server would assign the dummy packet to the correct peer id. However the server instead now assigns a second peer id and peer structure to the peer, and assign the packet to that new peer. 7. In order to inform the peer of its peer id, the server sends a CONTROLTYPE_SET_PEER_ID command packet, reliably, to the peer. This packet uses the new peer id. 8. The client sends an ack to that packet, not with the new peer id but with the peer id sent in 2. 9. This packet reaches the server, but it drops the ACK as the peer id does not map to any un-ACK-ed packets with that seqnum. The same time, the server still waits for an ACK with the new peer id, which of course won't come. This causes the server to periodically re-try sending that packet, and the client ACKing it each time. Steps 7-9 cause annoyances and erroneous output, but don't cause the connection failure itself. The actual mistake that causes the connection failure happens in 6: The server does not assign the dummy packet to the correct peer, but to a newly created one. Therefore, all further packets sent by the client on channel 0 are now buffered by the server as it waits for the dummy packet to reach the peer, which of course doesn't happen as the server assigned that packet to the second peer it created for the client. This makes the connection code indefinitely buffer the TOSERVER_CLIENT_READY packet, not passing it to higher level code, which stalls the continuation of the further init process indefinitely and causes the actual bug. Maybe this can be caused by reordered init packets as well, the only studied case was where network has reliably reordered the dummy packet to get sent after the init packets. The patch fixes the bug by not ignoring peers where m_has_sent_with_id has been set anymore. The other changes of the patch are just cleanups of unused methods and fields and additional explanatory comments. One could think of alternate ways to fix the bug: * The client could simply take the new peer id and continue communicating with that. This is however worse than the fix as it requires the peer id set command to be sent reliably (which currently happens, but it cant be changed anymore). Also, such a change would require both server and client to be patched in order for the bug to be fixed, as right now the client ignores peer id set commands after the peer id is different from PEER_ID_INEXISTENT and the server requires modification too to change the peer id internally. And, most importantly, right now we guarantee higher level server code that the peer id for a certain peer does not change. This guarantee would have to be broken, and it would require much larger changes to the server than this patch means. * One could stop sending the dummy packet. One may be unsure whether this is a good idea, as the meaning of the dummy packet is not known (it might be there for something important), and as it is possible that the init packets may cause this problem as well (although it may be possible too that they can't cause this). Thanks to @auouymous who had originally reported this bug and who has helped patiently in finding its cause. --- src/network/connection.cpp | 13 ++++--------- src/network/connection.h | 19 ++++--------------- 2 files changed, 8 insertions(+), 24 deletions(-) diff --git a/src/network/connection.cpp b/src/network/connection.cpp index f7452d8e4..02c0aa165 100644 --- a/src/network/connection.cpp +++ b/src/network/connection.cpp @@ -2167,12 +2167,12 @@ void ConnectionReceiveThread::receive() throw InvalidIncomingDataException("Channel doesn't exist"); } - /* preserve original peer_id for later usage */ - u16 packet_peer_id = peer_id; - /* Try to identify peer by sender address (may happen on join) */ if (peer_id == PEER_ID_INEXISTENT) { peer_id = m_connection->lookupPeer(sender); + // We do not have to remind the peer of its + // peer id as the CONTROLTYPE_SET_PEER_ID + // command was sent reliably. } /* The peer was not found in our lists. Add it. */ @@ -2214,11 +2214,6 @@ void ConnectionReceiveThread::receive() } } - - /* mark peer as seen with id */ - if (!(packet_peer_id == PEER_ID_INEXISTENT)) - peer->setSentWithID(); - peer->ResetTimeout(); Channel *channel = 0; @@ -2756,7 +2751,7 @@ u16 Connection::lookupPeer(Address& sender) for(; j != m_peers.end(); ++j) { Peer *peer = j->second; - if (peer->isActive()) + if (peer->isPendingDeletion()) continue; Address tocheck; diff --git a/src/network/connection.h b/src/network/connection.h index fe2c9819d..5ee53b9d4 100644 --- a/src/network/connection.h +++ b/src/network/connection.h @@ -663,8 +663,7 @@ class Peer { m_last_rtt(-1.0), m_usage(0), m_timeout_counter(0.0), - m_last_timeout_check(porting::getTimeMs()), - m_has_sent_with_id(false) + m_last_timeout_check(porting::getTimeMs()) { m_rtt.avg_rtt = -1.0; m_rtt.jitter_avg = -1.0; @@ -687,21 +686,16 @@ class Peer { virtual void PutReliableSendCommand(ConnectionCommand &c, unsigned int max_packet_size) {}; - virtual bool isActive() { return false; }; - virtual bool getAddress(MTProtocols type, Address& toset) = 0; + bool isPendingDeletion() + { MutexAutoLock lock(m_exclusive_access_mutex); return m_pending_deletion; }; + void ResetTimeout() {MutexAutoLock lock(m_exclusive_access_mutex); m_timeout_counter=0.0; }; bool isTimedOut(float timeout); - void setSentWithID() - { MutexAutoLock lock(m_exclusive_access_mutex); m_has_sent_with_id = true; }; - - bool hasSentWithID() - { MutexAutoLock lock(m_exclusive_access_mutex); return m_has_sent_with_id; }; - unsigned int m_increment_packets_remaining; unsigned int m_increment_bytes_remaining; @@ -776,8 +770,6 @@ class Peer { float m_timeout_counter; u32 m_last_timeout_check; - - bool m_has_sent_with_id; }; class UDPPeer : public Peer @@ -795,9 +787,6 @@ public: void PutReliableSendCommand(ConnectionCommand &c, unsigned int max_packet_size); - bool isActive() - { return ((hasSentWithID()) && (!m_pending_deletion)); }; - bool getAddress(MTProtocols type, Address& toset); void setNonLegacyPeer(); From 725edc78b214f8ffa5494ed846755083295c3feb Mon Sep 17 00:00:00 2001 From: Ekdohibs Date: Sun, 22 May 2016 20:33:06 +0200 Subject: [PATCH 014/418] Move updateTextures and fillTileAttribs to ContentFeatures --- src/nodedef.cpp | 534 +++++++++++++++++++++++++----------------------- src/nodedef.h | 29 +++ 2 files changed, 302 insertions(+), 261 deletions(-) diff --git a/src/nodedef.cpp b/src/nodedef.cpp index 3a2cb00b1..1691289c2 100644 --- a/src/nodedef.cpp +++ b/src/nodedef.cpp @@ -247,6 +247,28 @@ static void deSerializeSimpleSoundSpec(SimpleSoundSpec &ss, std::istream &is) ss.gain = readF1000(is); } +void TextureSettings::readSettings() +{ + connected_glass = g_settings->getBool("connected_glass"); + opaque_water = g_settings->getBool("opaque_water"); + bool enable_shaders = g_settings->getBool("enable_shaders"); + bool enable_bumpmapping = g_settings->getBool("enable_bumpmapping"); + bool enable_parallax_occlusion = g_settings->getBool("enable_parallax_occlusion"); + enable_mesh_cache = g_settings->getBool("enable_mesh_cache"); + enable_minimap = g_settings->getBool("enable_minimap"); + std::string leaves_style_str = g_settings->get("leaves_style"); + + use_normal_texture = enable_shaders && + (enable_bumpmapping || enable_parallax_occlusion); + if (leaves_style_str == "fancy") { + leaves_style = LEAVES_FANCY; + } else if (leaves_style_str == "simple") { + leaves_style = LEAVES_SIMPLE; + } else { + leaves_style = LEAVES_OPAQUE; + } +} + /* ContentFeatures */ @@ -486,6 +508,254 @@ void ContentFeatures::deSerialize(std::istream &is) }catch(SerializationError &e) {}; } +#ifndef SERVER +void ContentFeatures::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile, + TileDef *tiledef, u32 shader_id, bool use_normal_texture, + bool backface_culling, u8 alpha, u8 material_type) +{ + tile->shader_id = shader_id; + tile->texture = tsrc->getTextureForMesh(tiledef->name, &tile->texture_id); + tile->alpha = alpha; + tile->material_type = material_type; + + // Normal texture and shader flags texture + if (use_normal_texture) { + tile->normal_texture = tsrc->getNormalTexture(tiledef->name); + } + tile->flags_texture = tsrc->getShaderFlagsTexture(tile->normal_texture ? true : false); + + // Material flags + tile->material_flags = 0; + if (backface_culling) + tile->material_flags |= MATERIAL_FLAG_BACKFACE_CULLING; + if (tiledef->animation.type == TAT_VERTICAL_FRAMES) + tile->material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES; + if (tiledef->tileable_horizontal) + tile->material_flags |= MATERIAL_FLAG_TILEABLE_HORIZONTAL; + if (tiledef->tileable_vertical) + tile->material_flags |= MATERIAL_FLAG_TILEABLE_VERTICAL; + + // Animation parameters + int frame_count = 1; + if (tile->material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES) { + // Get texture size to determine frame count by aspect ratio + v2u32 size = tile->texture->getOriginalSize(); + int frame_height = (float)size.X / + (float)tiledef->animation.aspect_w * + (float)tiledef->animation.aspect_h; + frame_count = size.Y / frame_height; + int frame_length_ms = 1000.0 * tiledef->animation.length / frame_count; + tile->animation_frame_count = frame_count; + tile->animation_frame_length_ms = frame_length_ms; + } + + if (frame_count == 1) { + tile->material_flags &= ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES; + } else { + std::ostringstream os(std::ios::binary); + tile->frames.resize(frame_count); + + for (int i = 0; i < frame_count; i++) { + + FrameSpec frame; + + os.str(""); + os << tiledef->name << "^[verticalframe:" + << frame_count << ":" << i; + + frame.texture = tsrc->getTextureForMesh(os.str(), &frame.texture_id); + if (tile->normal_texture) + frame.normal_texture = tsrc->getNormalTexture(os.str()); + frame.flags_texture = tile->flags_texture; + tile->frames[i] = frame; + } + } +} +#endif + +#ifndef SERVER +void ContentFeatures::updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc, + scene::ISceneManager *smgr, scene::IMeshManipulator *meshmanip, + IGameDef *gamedef, const TextureSettings &tsettings) +{ + // minimap pixel color - the average color of a texture + if (tsettings.enable_minimap && tiledef[0].name != "") + minimap_color = tsrc->getTextureAverageColor(tiledef[0].name); + + // Figure out the actual tiles to use + TileDef tdef[6]; + for (u32 j = 0; j < 6; j++) { + tdef[j] = tiledef[j]; + if (tdef[j].name == "") + tdef[j].name = "unknown_node.png"; + } + + bool is_liquid = false; + bool is_water_surface = false; + + u8 material_type = (alpha == 255) ? + TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA; + + switch (drawtype) { + default: + case NDT_NORMAL: + solidness = 2; + break; + case NDT_AIRLIKE: + solidness = 0; + break; + case NDT_LIQUID: + assert(liquid_type == LIQUID_SOURCE); + if (tsettings.opaque_water) + alpha = 255; + solidness = 1; + is_liquid = true; + break; + case NDT_FLOWINGLIQUID: + assert(liquid_type == LIQUID_FLOWING); + solidness = 0; + if (tsettings.opaque_water) + alpha = 255; + is_liquid = true; + break; + case NDT_GLASSLIKE: + solidness = 0; + visual_solidness = 1; + break; + case NDT_GLASSLIKE_FRAMED: + solidness = 0; + visual_solidness = 1; + break; + case NDT_GLASSLIKE_FRAMED_OPTIONAL: + solidness = 0; + visual_solidness = 1; + drawtype = tsettings.connected_glass ? NDT_GLASSLIKE_FRAMED : NDT_GLASSLIKE; + break; + case NDT_ALLFACES: + solidness = 0; + visual_solidness = 1; + break; + case NDT_ALLFACES_OPTIONAL: + if (tsettings.leaves_style == LEAVES_FANCY) { + drawtype = NDT_ALLFACES; + solidness = 0; + visual_solidness = 1; + } else if (tsettings.leaves_style == LEAVES_SIMPLE) { + for (u32 j = 0; j < 6; j++) { + if (tiledef_special[j].name != "") + tdef[j].name = tiledef_special[j].name; + } + drawtype = NDT_GLASSLIKE; + solidness = 0; + visual_solidness = 1; + } else { + drawtype = NDT_NORMAL; + solidness = 2; + for (u32 i = 0; i < 6; i++) + tdef[i].name += std::string("^[noalpha"); + } + if (waving == 1) + material_type = TILE_MATERIAL_WAVING_LEAVES; + break; + case NDT_PLANTLIKE: + solidness = 0; + if (waving == 1) + material_type = TILE_MATERIAL_WAVING_PLANTS; + break; + case NDT_FIRELIKE: + solidness = 0; + break; + case NDT_MESH: + solidness = 0; + break; + case NDT_TORCHLIKE: + case NDT_SIGNLIKE: + case NDT_FENCELIKE: + case NDT_RAILLIKE: + case NDT_NODEBOX: + solidness = 0; + break; + } + + if (is_liquid) { + material_type = (alpha == 255) ? + TILE_MATERIAL_LIQUID_OPAQUE : TILE_MATERIAL_LIQUID_TRANSPARENT; + if (name == "default:water_source") + is_water_surface = true; + } + + u32 tile_shader[6]; + for (u16 j = 0; j < 6; j++) { + tile_shader[j] = shdsrc->getShader("nodes_shader", + material_type, drawtype); + } + + if (is_water_surface) { + tile_shader[0] = shdsrc->getShader("water_surface_shader", + material_type, drawtype); + } + + // Tiles (fill in f->tiles[]) + for (u16 j = 0; j < 6; j++) { + fillTileAttribs(tsrc, &tiles[j], &tdef[j], tile_shader[j], + tsettings.use_normal_texture, + tiledef[j].backface_culling, alpha, material_type); + } + + // Special tiles (fill in f->special_tiles[]) + for (u16 j = 0; j < CF_SPECIAL_COUNT; j++) { + fillTileAttribs(tsrc, &special_tiles[j], &tiledef_special[j], + tile_shader[j], tsettings.use_normal_texture, + tiledef_special[j].backface_culling, alpha, material_type); + } + + if ((drawtype == NDT_MESH) && (mesh != "")) { + // Meshnode drawtype + // Read the mesh and apply scale + mesh_ptr[0] = gamedef->getMesh(mesh); + if (mesh_ptr[0]){ + v3f scale = v3f(1.0, 1.0, 1.0) * BS * visual_scale; + scaleMesh(mesh_ptr[0], scale); + recalculateBoundingBox(mesh_ptr[0]); + meshmanip->recalculateNormals(mesh_ptr[0], true, false); + } + } else if ((drawtype == NDT_NODEBOX) && + ((node_box.type == NODEBOX_REGULAR) || + (node_box.type == NODEBOX_FIXED)) && + (!node_box.fixed.empty())) { + //Convert regular nodebox nodes to meshnodes + //Change the drawtype and apply scale + drawtype = NDT_MESH; + mesh_ptr[0] = convertNodeboxesToMesh(node_box.fixed); + v3f scale = v3f(1.0, 1.0, 1.0) * visual_scale; + scaleMesh(mesh_ptr[0], scale); + recalculateBoundingBox(mesh_ptr[0]); + meshmanip->recalculateNormals(mesh_ptr[0], true, false); + } + + //Cache 6dfacedir and wallmounted rotated clones of meshes + if (tsettings.enable_mesh_cache && mesh_ptr[0] && (param_type_2 == CPT2_FACEDIR)) { + for (u16 j = 1; j < 24; j++) { + mesh_ptr[j] = cloneMesh(mesh_ptr[0]); + rotateMeshBy6dFacedir(mesh_ptr[j], j); + recalculateBoundingBox(mesh_ptr[j]); + meshmanip->recalculateNormals(mesh_ptr[j], true, false); + } + } else if (tsettings.enable_mesh_cache && mesh_ptr[0] && (param_type_2 == CPT2_WALLMOUNTED)) { + static const u8 wm_to_6d[6] = {20, 0, 16+1, 12+3, 8, 4+2}; + for (u16 j = 1; j < 6; j++) { + mesh_ptr[j] = cloneMesh(mesh_ptr[0]); + rotateMeshBy6dFacedir(mesh_ptr[j], wm_to_6d[j]); + recalculateBoundingBox(mesh_ptr[j]); + meshmanip->recalculateNormals(mesh_ptr[j], true, false); + } + rotateMeshBy6dFacedir(mesh_ptr[0], wm_to_6d[0]); + recalculateBoundingBox(mesh_ptr[0]); + meshmanip->recalculateNormals(mesh_ptr[0], true, false); + } +} +#endif + /* CNodeDefManager */ @@ -525,11 +795,6 @@ public: private: void addNameIdMapping(content_t i, std::string name); -#ifndef SERVER - void fillTileAttribs(ITextureSource *tsrc, TileSpec *tile, TileDef *tiledef, - u32 shader_id, bool use_normal_texture, bool backface_culling, - u8 alpha, u8 material_type); -#endif // Features indexed by id std::vector m_content_features; @@ -890,271 +1155,18 @@ void CNodeDefManager::updateTextures(IGameDef *gamedef, IShaderSource *shdsrc = gamedef->getShaderSource(); scene::ISceneManager* smgr = gamedef->getSceneManager(); scene::IMeshManipulator* meshmanip = smgr->getMeshManipulator(); - - bool connected_glass = g_settings->getBool("connected_glass"); - bool opaque_water = g_settings->getBool("opaque_water"); - bool enable_shaders = g_settings->getBool("enable_shaders"); - bool enable_bumpmapping = g_settings->getBool("enable_bumpmapping"); - bool enable_parallax_occlusion = g_settings->getBool("enable_parallax_occlusion"); - bool enable_mesh_cache = g_settings->getBool("enable_mesh_cache"); - bool enable_minimap = g_settings->getBool("enable_minimap"); - std::string leaves_style = g_settings->get("leaves_style"); - - bool use_normal_texture = enable_shaders && - (enable_bumpmapping || enable_parallax_occlusion); + TextureSettings tsettings; + tsettings.readSettings(); u32 size = m_content_features.size(); for (u32 i = 0; i < size; i++) { - ContentFeatures *f = &m_content_features[i]; - - // minimap pixel color - the average color of a texture - if (enable_minimap && f->tiledef[0].name != "") - f->minimap_color = tsrc->getTextureAverageColor(f->tiledef[0].name); - - // Figure out the actual tiles to use - TileDef tiledef[6]; - for (u32 j = 0; j < 6; j++) { - tiledef[j] = f->tiledef[j]; - if (tiledef[j].name == "") - tiledef[j].name = "unknown_node.png"; - } - - bool is_liquid = false; - bool is_water_surface = false; - - u8 material_type = (f->alpha == 255) ? - TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA; - - switch (f->drawtype) { - default: - case NDT_NORMAL: - f->solidness = 2; - break; - case NDT_AIRLIKE: - f->solidness = 0; - break; - case NDT_LIQUID: - assert(f->liquid_type == LIQUID_SOURCE); - if (opaque_water) - f->alpha = 255; - f->solidness = 1; - is_liquid = true; - break; - case NDT_FLOWINGLIQUID: - assert(f->liquid_type == LIQUID_FLOWING); - f->solidness = 0; - if (opaque_water) - f->alpha = 255; - is_liquid = true; - break; - case NDT_GLASSLIKE: - f->solidness = 0; - f->visual_solidness = 1; - break; - case NDT_GLASSLIKE_FRAMED: - f->solidness = 0; - f->visual_solidness = 1; - break; - case NDT_GLASSLIKE_FRAMED_OPTIONAL: - f->solidness = 0; - f->visual_solidness = 1; - f->drawtype = connected_glass ? NDT_GLASSLIKE_FRAMED : NDT_GLASSLIKE; - break; - case NDT_ALLFACES: - f->solidness = 0; - f->visual_solidness = 1; - break; - case NDT_ALLFACES_OPTIONAL: - if (leaves_style == "fancy") { - f->drawtype = NDT_ALLFACES; - f->solidness = 0; - f->visual_solidness = 1; - } else if (leaves_style == "simple") { - for (u32 j = 0; j < 6; j++) { - if (f->tiledef_special[j].name != "") - tiledef[j].name = f->tiledef_special[j].name; - } - f->drawtype = NDT_GLASSLIKE; - f->solidness = 0; - f->visual_solidness = 1; - } else { - f->drawtype = NDT_NORMAL; - f->solidness = 2; - for (u32 i = 0; i < 6; i++) - tiledef[i].name += std::string("^[noalpha"); - } - if (f->waving == 1) - material_type = TILE_MATERIAL_WAVING_LEAVES; - break; - case NDT_PLANTLIKE: - f->solidness = 0; - if (f->waving == 1) - material_type = TILE_MATERIAL_WAVING_PLANTS; - break; - case NDT_FIRELIKE: - f->solidness = 0; - break; - case NDT_MESH: - f->solidness = 0; - break; - case NDT_TORCHLIKE: - case NDT_SIGNLIKE: - case NDT_FENCELIKE: - case NDT_RAILLIKE: - case NDT_NODEBOX: - f->solidness = 0; - break; - } - - if (is_liquid) { - material_type = (f->alpha == 255) ? - TILE_MATERIAL_LIQUID_OPAQUE : TILE_MATERIAL_LIQUID_TRANSPARENT; - if (f->name == "default:water_source") - is_water_surface = true; - } - - u32 tile_shader[6]; - for (u16 j = 0; j < 6; j++) { - tile_shader[j] = shdsrc->getShader("nodes_shader", - material_type, f->drawtype); - } - - if (is_water_surface) { - tile_shader[0] = shdsrc->getShader("water_surface_shader", - material_type, f->drawtype); - } - - // Tiles (fill in f->tiles[]) - for (u16 j = 0; j < 6; j++) { - fillTileAttribs(tsrc, &f->tiles[j], &tiledef[j], tile_shader[j], - use_normal_texture, f->tiledef[j].backface_culling, f->alpha, material_type); - } - - // Special tiles (fill in f->special_tiles[]) - for (u16 j = 0; j < CF_SPECIAL_COUNT; j++) { - fillTileAttribs(tsrc, &f->special_tiles[j], &f->tiledef_special[j], - tile_shader[j], use_normal_texture, - f->tiledef_special[j].backface_culling, f->alpha, material_type); - } - - if ((f->drawtype == NDT_MESH) && (f->mesh != "")) { - // Meshnode drawtype - // Read the mesh and apply scale - f->mesh_ptr[0] = gamedef->getMesh(f->mesh); - if (f->mesh_ptr[0]){ - v3f scale = v3f(1.0, 1.0, 1.0) * BS * f->visual_scale; - scaleMesh(f->mesh_ptr[0], scale); - recalculateBoundingBox(f->mesh_ptr[0]); - meshmanip->recalculateNormals(f->mesh_ptr[0], true, false); - } - } else if ((f->drawtype == NDT_NODEBOX) && - ((f->node_box.type == NODEBOX_REGULAR) || - (f->node_box.type == NODEBOX_FIXED)) && - (!f->node_box.fixed.empty())) { - //Convert regular nodebox nodes to meshnodes - //Change the drawtype and apply scale - f->drawtype = NDT_MESH; - f->mesh_ptr[0] = convertNodeboxesToMesh(f->node_box.fixed); - v3f scale = v3f(1.0, 1.0, 1.0) * f->visual_scale; - scaleMesh(f->mesh_ptr[0], scale); - recalculateBoundingBox(f->mesh_ptr[0]); - meshmanip->recalculateNormals(f->mesh_ptr[0], true, false); - } - - //Cache 6dfacedir and wallmounted rotated clones of meshes - if (enable_mesh_cache && f->mesh_ptr[0] && (f->param_type_2 == CPT2_FACEDIR)) { - for (u16 j = 1; j < 24; j++) { - f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]); - rotateMeshBy6dFacedir(f->mesh_ptr[j], j); - recalculateBoundingBox(f->mesh_ptr[j]); - meshmanip->recalculateNormals(f->mesh_ptr[j], true, false); - } - } else if (enable_mesh_cache && f->mesh_ptr[0] && (f->param_type_2 == CPT2_WALLMOUNTED)) { - static const u8 wm_to_6d[6] = {20, 0, 16+1, 12+3, 8, 4+2}; - for (u16 j = 1; j < 6; j++) { - f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]); - rotateMeshBy6dFacedir(f->mesh_ptr[j], wm_to_6d[j]); - recalculateBoundingBox(f->mesh_ptr[j]); - meshmanip->recalculateNormals(f->mesh_ptr[j], true, false); - } - rotateMeshBy6dFacedir(f->mesh_ptr[0], wm_to_6d[0]); - recalculateBoundingBox(f->mesh_ptr[0]); - meshmanip->recalculateNormals(f->mesh_ptr[0], true, false); - } - + m_content_features[i].updateTextures(tsrc, shdsrc, smgr, meshmanip, gamedef, tsettings); progress_callback(progress_callback_args, i, size); } #endif } - -#ifndef SERVER -void CNodeDefManager::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile, - TileDef *tiledef, u32 shader_id, bool use_normal_texture, - bool backface_culling, u8 alpha, u8 material_type) -{ - tile->shader_id = shader_id; - tile->texture = tsrc->getTextureForMesh(tiledef->name, &tile->texture_id); - tile->alpha = alpha; - tile->material_type = material_type; - - // Normal texture and shader flags texture - if (use_normal_texture) { - tile->normal_texture = tsrc->getNormalTexture(tiledef->name); - } - tile->flags_texture = tsrc->getShaderFlagsTexture(tile->normal_texture ? true : false); - - // Material flags - tile->material_flags = 0; - if (backface_culling) - tile->material_flags |= MATERIAL_FLAG_BACKFACE_CULLING; - if (tiledef->animation.type == TAT_VERTICAL_FRAMES) - tile->material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES; - if (tiledef->tileable_horizontal) - tile->material_flags |= MATERIAL_FLAG_TILEABLE_HORIZONTAL; - if (tiledef->tileable_vertical) - tile->material_flags |= MATERIAL_FLAG_TILEABLE_VERTICAL; - - // Animation parameters - int frame_count = 1; - if (tile->material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES) { - // Get texture size to determine frame count by aspect ratio - v2u32 size = tile->texture->getOriginalSize(); - int frame_height = (float)size.X / - (float)tiledef->animation.aspect_w * - (float)tiledef->animation.aspect_h; - frame_count = size.Y / frame_height; - int frame_length_ms = 1000.0 * tiledef->animation.length / frame_count; - tile->animation_frame_count = frame_count; - tile->animation_frame_length_ms = frame_length_ms; - } - - if (frame_count == 1) { - tile->material_flags &= ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES; - } else { - std::ostringstream os(std::ios::binary); - tile->frames.resize(frame_count); - - for (int i = 0; i < frame_count; i++) { - - FrameSpec frame; - - os.str(""); - os << tiledef->name << "^[verticalframe:" - << frame_count << ":" << i; - - frame.texture = tsrc->getTextureForMesh(os.str(), &frame.texture_id); - if (tile->normal_texture) - frame.normal_texture = tsrc->getNormalTexture(os.str()); - frame.flags_texture = tile->flags_texture; - tile->frames[i] = frame; - } - } -} -#endif - - void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version) const { writeU8(os, 1); // version diff --git a/src/nodedef.h b/src/nodedef.h index 58d0faffa..1c2f792d8 100644 --- a/src/nodedef.h +++ b/src/nodedef.h @@ -112,6 +112,26 @@ struct NodeBox struct MapNode; class NodeMetadata; +enum LeavesStyle { + LEAVES_FANCY, + LEAVES_SIMPLE, + LEAVES_OPAQUE, +}; + +class TextureSettings { +public: + LeavesStyle leaves_style; + bool opaque_water; + bool connected_glass; + bool use_normal_texture; + bool enable_mesh_cache; + bool enable_minimap; + + TextureSettings() {} + + void readSettings(); +}; + enum NodeDrawType { NDT_NORMAL, // A basic solid block @@ -304,6 +324,15 @@ struct ContentFeatures if(!isLiquid() || !f.isLiquid()) return false; return (liquid_alternative_flowing == f.liquid_alternative_flowing); } + +#ifndef SERVER + void fillTileAttribs(ITextureSource *tsrc, TileSpec *tile, TileDef *tiledef, + u32 shader_id, bool use_normal_texture, bool backface_culling, + u8 alpha, u8 material_type); + void updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc, + scene::ISceneManager *smgr, scene::IMeshManipulator *meshmanip, + IGameDef *gamedef, const TextureSettings &tsettings); +#endif }; class INodeDefManager { From 22f78ea38e415c40ef445786b77788a40f4ddac3 Mon Sep 17 00:00:00 2001 From: Craig Robbins Date: Tue, 24 May 2016 00:27:11 +1000 Subject: [PATCH 015/418] Fix irrlicht version checking macro for tooltip_height calculation --- src/guiFormSpecMenu.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/guiFormSpecMenu.cpp b/src/guiFormSpecMenu.cpp index b0215a84a..99b1153c1 100644 --- a/src/guiFormSpecMenu.cpp +++ b/src/guiFormSpecMenu.cpp @@ -2265,10 +2265,10 @@ void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase, this->bringToFront(m_tooltip_element); m_tooltip_element->setText(tooltip_text.c_str()); s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height; -#if IRRLICHT_VERSION_MAJOR >= 1 && IRRLICHT_VERSION_MINOR >= 8 && IRRLICHT_VERSION_REVISION >= 2 - s32 tooltip_height = m_tooltip_element->getTextHeight() + 5; -#else +#if IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5; +#else + s32 tooltip_height = m_tooltip_element->getTextHeight() + 5; #endif v2u32 screenSize = driver->getScreenSize(); int tooltip_offset_x = m_btn_height; From ef100f12a1346f7bfd2a05dc606cb22e97179c02 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Fri, 12 Feb 2016 18:39:03 -0500 Subject: [PATCH 016/418] Fix rollback.txt migration Broken by b1965ac20922e3722392114bd63a22b403dcbe98. This also prepares the begin and commit statements only once. --- src/rollback.cpp | 41 +++++++++++++++++++++++++++-------------- src/rollback.h | 2 +- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/rollback.cpp b/src/rollback.cpp index 2367c3a21..ac4324cab 100644 --- a/src/rollback.cpp +++ b/src/rollback.cpp @@ -93,10 +93,10 @@ RollbackManager::RollbackManager(const std::string & world_path, std::string migrating_flag = txt_filename + ".migrating"; database_path = world_path + DIR_DELIM "rollback.sqlite"; - initDatabase(); + bool created = initDatabase(); - if (fs::PathExists(txt_filename) && (fs::PathExists(migrating_flag) || - !fs::PathExists(database_path))) { + if (fs::PathExists(txt_filename) && (created || + fs::PathExists(migrating_flag))) { std::ofstream of(migrating_flag.c_str()); of.close(); migrate(txt_filename); @@ -250,15 +250,15 @@ bool RollbackManager::createTables() } -void RollbackManager::initDatabase() +bool RollbackManager::initDatabase() { verbosestream << "RollbackManager: Database connection setup" << std::endl; - bool needsCreate = !fs::PathExists(database_path); + bool needs_create = !fs::PathExists(database_path); SQLOK(sqlite3_open_v2(database_path.c_str(), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)); - if (needsCreate) { + if (needs_create) { createTables(); } @@ -374,6 +374,8 @@ void RollbackManager::initDatabase() ); } SQLOK(sqlite3_reset(stmt_knownNode_select)); + + return needs_create; } @@ -672,23 +674,27 @@ void RollbackManager::migrate(const std::string & file_path) std::streampos file_size = fh.tellg(); - if (file_size > 10) { + if (file_size < 10) { errorstream << "Empty rollback log." << std::endl; return; } fh.seekg(0); + sqlite3_stmt *stmt_begin; + sqlite3_stmt *stmt_commit; + SQLOK(sqlite3_prepare_v2(db, "BEGIN", -1, &stmt_begin, NULL)); + SQLOK(sqlite3_prepare_v2(db, "COMMIT", -1, &stmt_commit, NULL)); + std::string bit; int i = 0; - int id = 1; time_t start = time(0); time_t t = start; - sqlite3_exec(db, "BEGIN", NULL, NULL, NULL); + SQLRES(sqlite3_step(stmt_begin), SQLITE_DONE); + sqlite3_reset(stmt_begin); do { ActionRow row; - - row.id = id; + row.id = 0; // Get the timestamp std::getline(fh, bit, ' '); @@ -758,17 +764,24 @@ void RollbackManager::migrate(const std::string & file_path) ++i; if (time(0) - t >= 1) { - sqlite3_exec(db, "COMMIT", NULL, NULL, NULL); + SQLRES(sqlite3_step(stmt_commit), SQLITE_DONE); + sqlite3_reset(stmt_commit); t = time(0); std::cout << " Done: " << static_cast((static_cast(fh.tellg()) / static_cast(file_size)) * 100) << "%" << " Speed: " << i / (t - start) << "/second \r" << std::flush; - sqlite3_exec(db, "BEGIN", NULL, NULL, NULL); + SQLRES(sqlite3_step(stmt_begin), SQLITE_DONE); + sqlite3_reset(stmt_begin); } } while (fh.good()); + SQLRES(sqlite3_step(stmt_commit), SQLITE_DONE); + sqlite3_reset(stmt_commit); + + SQLOK(sqlite3_finalize(stmt_begin)); + SQLOK(sqlite3_finalize(stmt_commit)); std::cout - << " Done: 100% " << std::endl + << " Done: 100% " << std::endl << "Now you can delete the old rollback.txt file." << std::endl; } diff --git a/src/rollback.h b/src/rollback.h index c57e38ab0..a05ef8b78 100644 --- a/src/rollback.h +++ b/src/rollback.h @@ -61,7 +61,7 @@ private: const char * getActorName(const int id); const char * getNodeName(const int id); bool createTables(); - void initDatabase(); + bool initDatabase(); bool registerRow(const ActionRow & row); const std::list actionRowsFromSelect(sqlite3_stmt * stmt); ActionRow actionRowFromRollbackAction(const RollbackAction & action); From fa6b21a15b415cd82dce6896b94a5341b7dd76f0 Mon Sep 17 00:00:00 2001 From: est31 Date: Sat, 14 May 2016 16:25:57 +0200 Subject: [PATCH 017/418] Tell irrlicht if we handle a key or not. We can remove the function in MtNativeActivity now as it serves precisely that purpose: to tell irrlicht that we handled the esc key. TODO for later: * Perhaps try to find a more performant container than KeyList --- .../MtNativeActivity.java | 5 --- src/client/inputhandler.h | 37 ++++++++++++++++--- src/game.cpp | 23 +++++++++++- src/game.h | 3 ++ 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/build/android/src/main/java/net.minetest.minetest/MtNativeActivity.java b/build/android/src/main/java/net.minetest.minetest/MtNativeActivity.java index 3173a71f4..159521a50 100644 --- a/build/android/src/main/java/net.minetest.minetest/MtNativeActivity.java +++ b/build/android/src/main/java/net.minetest.minetest/MtNativeActivity.java @@ -19,11 +19,6 @@ public class MtNativeActivity extends NativeActivity { public void onDestroy() { super.onDestroy(); } - - @Override - public void onBackPressed() { - } - public void copyAssets() { Intent intent = new Intent(this, MinetestAssetCopy.class); diff --git a/src/client/inputhandler.h b/src/client/inputhandler.h index 69e4b25fa..73e507fdc 100644 --- a/src/client/inputhandler.h +++ b/src/client/inputhandler.h @@ -42,11 +42,15 @@ public: // Remember whether each key is down or up if (event.EventType == irr::EET_KEY_INPUT_EVENT) { - if (event.KeyInput.PressedDown) { - keyIsDown.set(event.KeyInput); - keyWasDown.set(event.KeyInput); - } else { - keyIsDown.unset(event.KeyInput); + const KeyPress &keyCode = event.KeyInput; + if (keysListenedFor[keyCode]) { + if (event.KeyInput.PressedDown) { + keyIsDown.set(keyCode); + keyWasDown.set(keyCode); + } else { + keyIsDown.unset(keyCode); + } + return true; } } @@ -116,6 +120,15 @@ public: return b; } + void listenForKey(const KeyPress &keyCode) + { + keysListenedFor.set(keyCode); + } + void dontListenForKeys() + { + keysListenedFor.clear(); + } + s32 getMouseWheel() { s32 a = mouse_wheel; @@ -168,6 +181,12 @@ private: KeyList keyIsDown; // Whether a key has been pressed or not KeyList keyWasDown; + // List of keys we listen for + // TODO perhaps the type of this is not really + // performant as KeyList is designed for few but + // often changing keys, and keysListenedFor is expected + // to change seldomly but contain lots of keys. + KeyList keysListenedFor; }; @@ -192,6 +211,14 @@ public: { return m_receiver->WasKeyDown(keyCode); } + virtual void listenForKey(const KeyPress &keyCode) + { + m_receiver->listenForKey(keyCode); + } + virtual void dontListenForKeys() + { + m_receiver->dontListenForKeys(); + } virtual v2s32 getMousePos() { if (m_device->getCursorControl()) { diff --git a/src/game.cpp b/src/game.cpp index 23f261cfd..c5211a042 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1297,7 +1297,11 @@ static void updateChat(Client &client, f32 dtime, bool show_debug, */ struct KeyCache { - KeyCache() { populate(); } + KeyCache() + { + handler = NULL; + populate(); + } enum { // Player movement @@ -1349,6 +1353,7 @@ struct KeyCache { void populate(); KeyPress key[KEYMAP_INTERNAL_ENUM_COUNT]; + InputHandler *handler; }; void KeyCache::populate() @@ -1399,6 +1404,19 @@ void KeyCache::populate() key[KEYMAP_ID_QUICKTUNE_DEC] = getKeySetting("keymap_quicktune_dec"); key[KEYMAP_ID_DEBUG_STACKS] = getKeySetting("keymap_print_debug_stacks"); + + if (handler) { + // First clear all keys, then re-add the ones we listen for + handler->dontListenForKeys(); + for (size_t i = 0; i < KEYMAP_INTERNAL_ENUM_COUNT; i++) { + handler->listenForKey(key[i]); + } + handler->listenForKey(EscapeKey); + handler->listenForKey(CancelKey); + for (size_t i = 0; i < 10; i++) { + handler->listenForKey(NumberKey[i]); + } + } } @@ -1829,6 +1847,9 @@ bool Game::startup(bool *kill, this->chat_backend = chat_backend; this->simple_singleplayer_mode = simple_singleplayer_mode; + keycache.handler = input; + keycache.populate(); + driver = device->getVideoDriver(); smgr = device->getSceneManager(); diff --git a/src/game.h b/src/game.h index e1f4e9346..5465ecdc6 100644 --- a/src/game.h +++ b/src/game.h @@ -110,6 +110,9 @@ public: virtual bool isKeyDown(const KeyPress &keyCode) = 0; virtual bool wasKeyDown(const KeyPress &keyCode) = 0; + virtual void listenForKey(const KeyPress &keyCode) {} + virtual void dontListenForKeys() {} + virtual v2s32 getMousePos() = 0; virtual void setMousePos(s32 x, s32 y) = 0; From 76f485647983ebd7eb4c3abbca0869d13f76920b Mon Sep 17 00:00:00 2001 From: kwolekr Date: Thu, 28 Apr 2016 03:43:09 -0400 Subject: [PATCH 018/418] Move biome calculation to BiomeGen BiomeGen defines an interface that, given a set of BiomeParams, computes biomes for a given area using the algorithm implemented by that specific BiomeGen. This abstracts away the old system where each mapgen supplied the noises required for biome generation. --- src/emerge.cpp | 2 - src/mapgen.cpp | 38 ++++--- src/mapgen.h | 21 ++-- src/mapgen_flat.cpp | 48 +++------ src/mapgen_flat.h | 2 +- src/mapgen_fractal.cpp | 48 +++------ src/mapgen_fractal.h | 7 +- src/mapgen_v5.cpp | 46 +++----- src/mapgen_v5.h | 7 +- src/mapgen_v7.cpp | 61 +++-------- src/mapgen_v7.h | 8 +- src/mapgen_valleys.cpp | 70 ++++++------ src/mapgen_valleys.h | 11 +- src/mg_biome.cpp | 185 ++++++++++++++++++++++++-------- src/mg_biome.h | 155 ++++++++++++++++++++++++-- src/script/lua_api/l_mapgen.cpp | 16 +-- 16 files changed, 421 insertions(+), 304 deletions(-) diff --git a/src/emerge.cpp b/src/emerge.cpp index 93e8f2b30..a2a10a177 100644 --- a/src/emerge.cpp +++ b/src/emerge.cpp @@ -181,8 +181,6 @@ EmergeManager::~EmergeManager() delete oremgr; delete decomgr; delete schemmgr; - - delete params.sparams; } diff --git a/src/mapgen.cpp b/src/mapgen.cpp index b3c9380a0..e269bf454 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -76,10 +76,9 @@ Mapgen::Mapgen() vm = NULL; ndef = NULL; - heightmap = NULL; + biomegen = NULL; biomemap = NULL; - heatmap = NULL; - humidmap = NULL; + heightmap = NULL; } @@ -94,11 +93,10 @@ Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge) : csize = v3s16(1, 1, 1) * (params->chunksize * MAP_BLOCKSIZE); vm = NULL; - ndef = NULL; - heightmap = NULL; + ndef = emerge->ndef; + biomegen = NULL; biomemap = NULL; - heatmap = NULL; - humidmap = NULL; + heightmap = NULL; } @@ -444,6 +442,14 @@ void GenerateNotifier::getEvents( //// MapgenParams //// + +MapgenParams::~MapgenParams() +{ + delete bparams; + delete sparams; +} + + void MapgenParams::load(const Settings &settings) { std::string seed_str; @@ -458,10 +464,13 @@ void MapgenParams::load(const Settings &settings) settings.getS16NoEx("water_level", water_level); settings.getS16NoEx("chunksize", chunksize); settings.getFlagStrNoEx("mg_flags", flags, flagdesc_mapgen); - settings.getNoiseParams("mg_biome_np_heat", np_biome_heat); - settings.getNoiseParams("mg_biome_np_heat_blend", np_biome_heat_blend); - settings.getNoiseParams("mg_biome_np_humidity", np_biome_humidity); - settings.getNoiseParams("mg_biome_np_humidity_blend", np_biome_humidity_blend); + + delete bparams; + bparams = BiomeManager::createBiomeParams(BIOMEGEN_ORIGINAL); + if (bparams) { + bparams->readParams(&settings); + bparams->seed = seed; + } delete sparams; MapgenFactory *mgfactory = EmergeManager::getMapgenFactory(mg_name); @@ -479,10 +488,9 @@ void MapgenParams::save(Settings &settings) const settings.setS16("water_level", water_level); settings.setS16("chunksize", chunksize); settings.setFlagStr("mg_flags", flags, flagdesc_mapgen, U32_MAX); - settings.setNoiseParams("mg_biome_np_heat", np_biome_heat); - settings.setNoiseParams("mg_biome_np_heat_blend", np_biome_heat_blend); - settings.setNoiseParams("mg_biome_np_humidity", np_biome_humidity); - settings.setNoiseParams("mg_biome_np_humidity_blend", np_biome_humidity_blend); + + if (bparams) + bparams->writeParams(&settings); if (sparams) sparams->writeParams(&settings); diff --git a/src/mapgen.h b/src/mapgen.h index abc3d2e89..554ec6084 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -44,6 +44,8 @@ extern FlagDesc flagdesc_mapgen[]; extern FlagDesc flagdesc_gennotify[]; class Biome; +class BiomeGen; +struct BiomeParams; class EmergeManager; class MapBlock; class VoxelManipulator; @@ -115,11 +117,7 @@ struct MapgenParams { s16 water_level; u32 flags; - NoiseParams np_biome_heat; - NoiseParams np_biome_heat_blend; - NoiseParams np_biome_humidity; - NoiseParams np_biome_humidity_blend; - + BiomeParams *bparams; MapgenSpecificParams *sparams; MapgenParams() : @@ -128,12 +126,12 @@ struct MapgenParams { seed(0), water_level(1), flags(MG_CAVES | MG_LIGHT | MG_DECORATIONS), - np_biome_heat(NoiseParams(50, 50, v3f(750.0, 750.0, 750.0), 5349, 3, 0.5, 2.0)), - np_biome_heat_blend(NoiseParams(0, 1.5, v3f(8.0, 8.0, 8.0), 13, 2, 1.0, 2.0)), - np_biome_humidity(NoiseParams(50, 50, v3f(750.0, 750.0, 750.0), 842, 3, 0.5, 2.0)), - np_biome_humidity_blend(NoiseParams(0, 1.5, v3f(8.0, 8.0, 8.0), 90003, 2, 1.0, 2.0)), + bparams(NULL), sparams(NULL) - {} + { + } + + virtual ~MapgenParams(); void load(const Settings &settings); void save(Settings &settings) const; @@ -153,10 +151,9 @@ public: u32 blockseed; s16 *heightmap; u8 *biomemap; - float *heatmap; - float *humidmap; v3s16 csize; + BiomeGen *biomegen; GenerateNotifier gennotify; Mapgen(); diff --git a/src/mapgen_flat.cpp b/src/mapgen_flat.cpp index 4669f1716..cb9aa9a8b 100644 --- a/src/mapgen_flat.cpp +++ b/src/mapgen_flat.cpp @@ -61,10 +61,7 @@ MapgenFlat::MapgenFlat(int mapgenid, MapgenParams *params, EmergeManager *emerge // 1-down overgeneration this->zstride_1d = csize.X * (csize.Y + 1); - this->biomemap = new u8[csize.X * csize.Z]; - this->heightmap = new s16[csize.X * csize.Z]; - this->heatmap = NULL; - this->humidmap = NULL; + this->heightmap = new s16[csize.X * csize.Z]; MapgenFlatParams *sp = (MapgenFlatParams *)params->sparams; @@ -86,15 +83,12 @@ MapgenFlat::MapgenFlat(int mapgenid, MapgenParams *params, EmergeManager *emerge noise_cave1 = new Noise(&sp->np_cave1, seed, csize.X, csize.Y + 1, csize.Z); noise_cave2 = new Noise(&sp->np_cave2, seed, csize.X, csize.Y + 1, csize.Z); - //// Biome noise - noise_heat = new Noise(¶ms->np_biome_heat, seed, csize.X, csize.Z); - noise_humidity = new Noise(¶ms->np_biome_humidity, seed, csize.X, csize.Z); - noise_heat_blend = new Noise(¶ms->np_biome_heat_blend, seed, csize.X, csize.Z); - noise_humidity_blend = new Noise(¶ms->np_biome_humidity_blend, seed, csize.X, csize.Z); + //// Initialize biome generator + biomegen = emerge->biomemgr->createBiomeGen( + BIOMEGEN_ORIGINAL, params->bparams, csize); + biomemap = biomegen->biomemap; //// Resolve nodes to be used - INodeDefManager *ndef = emerge->ndef; - c_stone = ndef->getId("mapgen_stone"); c_water_source = ndef->getId("mapgen_water_source"); c_lava_source = ndef->getId("mapgen_lava_source"); @@ -128,13 +122,9 @@ MapgenFlat::~MapgenFlat() delete noise_cave1; delete noise_cave2; - delete noise_heat; - delete noise_humidity; - delete noise_heat_blend; - delete noise_humidity_blend; + delete biomegen; delete[] heightmap; - delete[] biomemap; } @@ -252,12 +242,10 @@ void MapgenFlat::makeChunk(BlockMakeData *data) // Create heightmap updateHeightmap(node_min, node_max); - // Create biomemap at heightmap surface - bmgr->calcBiomes(csize.X, csize.Z, noise_heat->result, - noise_humidity->result, heightmap, biomemap); - - // Actually place the biome-specific nodes - MgStoneType stone_type = generateBiomes(noise_heat->result, noise_humidity->result); + // Init biome generator, place biome-specific nodes, and build biomemap + biomegen->calcBiomeNoise(node_min); + biomegen->getBiomes(heightmap); + MgStoneType stone_type = generateBiomes(); if (flags & MG_CAVES) generateCaves(stone_surface_max_y); @@ -343,18 +331,7 @@ void MapgenFlat::calculateNoise() // only if solid terrain is present in mapchunk noise_filler_depth->perlinMap2D(x, z); - noise_heat->perlinMap2D(x, z); - noise_humidity->perlinMap2D(x, z); - noise_heat_blend->perlinMap2D(x, z); - noise_humidity_blend->perlinMap2D(x, z); - for (s32 i = 0; i < csize.X * csize.Z; i++) { - noise_heat->result[i] += noise_heat_blend->result[i]; - noise_humidity->result[i] += noise_humidity_blend->result[i]; - } - - heatmap = noise_heat->result; - humidmap = noise_humidity->result; //printf("calculateNoise: %dus\n", t.stop()); } @@ -406,7 +383,7 @@ s16 MapgenFlat::generateTerrain() } -MgStoneType MapgenFlat::generateBiomes(float *heat_map, float *humidity_map) +MgStoneType MapgenFlat::generateBiomes() { v3s16 em = vm->m_area.getExtent(); u32 index = 0; @@ -443,7 +420,8 @@ MgStoneType MapgenFlat::generateBiomes(float *heat_map, float *humidity_map) // 3. When stone or water is detected but biome has not yet been calculated. if ((c == c_stone && (air_above || water_above || !biome)) || (c == c_water_source && (air_above || !biome))) { - biome = bmgr->getBiome(heat_map[index], humidity_map[index], y); + biome = biomegen->getBiomeAtIndex(index, y); + depth_top = biome->depth_top; base_filler = MYMAX(depth_top + biome->depth_filler + noise_filler_depth->result[index], 0); diff --git a/src/mapgen_flat.h b/src/mapgen_flat.h index 8aed09be5..53fd66a67 100644 --- a/src/mapgen_flat.h +++ b/src/mapgen_flat.h @@ -104,7 +104,7 @@ public: int getSpawnLevelAtPoint(v2s16 p); void calculateNoise(); s16 generateTerrain(); - MgStoneType generateBiomes(float *heat_map, float *humidity_map); + MgStoneType generateBiomes(); void dustTopNodes(); void generateCaves(s16 max_stone_y); }; diff --git a/src/mapgen_fractal.cpp b/src/mapgen_fractal.cpp index e2e29f875..9671a2143 100644 --- a/src/mapgen_fractal.cpp +++ b/src/mapgen_fractal.cpp @@ -59,10 +59,7 @@ MapgenFractal::MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager * // 1-down overgeneration this->zstride_1d = csize.X * (csize.Y + 1); - this->biomemap = new u8[csize.X * csize.Z]; this->heightmap = new s16[csize.X * csize.Z]; - this->heatmap = NULL; - this->humidmap = NULL; MapgenFractalParams *sp = (MapgenFractalParams *)params->sparams; @@ -87,18 +84,15 @@ MapgenFractal::MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager * noise_cave1 = new Noise(&sp->np_cave1, seed, csize.X, csize.Y + 1, csize.Z); noise_cave2 = new Noise(&sp->np_cave2, seed, csize.X, csize.Y + 1, csize.Z); - //// Biome noise - noise_heat = new Noise(¶ms->np_biome_heat, seed, csize.X, csize.Z); - noise_humidity = new Noise(¶ms->np_biome_humidity, seed, csize.X, csize.Z); - noise_heat_blend = new Noise(¶ms->np_biome_heat_blend, seed, csize.X, csize.Z); - noise_humidity_blend = new Noise(¶ms->np_biome_humidity_blend, seed, csize.X, csize.Z); + //// Initialize biome generator + biomegen = emerge->biomemgr->createBiomeGen( + BIOMEGEN_ORIGINAL, params->bparams, csize); + biomemap = biomegen->biomemap; this->formula = fractal / 2 + fractal % 2; this->julia = fractal % 2 == 0; //// Resolve nodes to be used - INodeDefManager *ndef = emerge->ndef; - c_stone = ndef->getId("mapgen_stone"); c_water_source = ndef->getId("mapgen_water_source"); c_lava_source = ndef->getId("mapgen_lava_source"); @@ -132,13 +126,9 @@ MapgenFractal::~MapgenFractal() delete noise_cave1; delete noise_cave2; - delete noise_heat; - delete noise_humidity; - delete noise_heat_blend; - delete noise_humidity_blend; + delete biomegen; delete[] heightmap; - delete[] biomemap; } @@ -217,7 +207,7 @@ int MapgenFractal::getSpawnLevelAtPoint(v2s16 p) s16 search_start = MYMAX(seabed_level, water_level + 1); if (seabed_level > water_level) solid_below = true; - + for (s16 y = search_start; y <= search_start + 128; y++) { if (getFractalAtPoint(p.X, y, p.Y)) { // Fractal node solid_below = true; @@ -268,12 +258,10 @@ void MapgenFractal::makeChunk(BlockMakeData *data) // Create heightmap updateHeightmap(node_min, node_max); - // Create biomemap at heightmap surface - bmgr->calcBiomes(csize.X, csize.Z, noise_heat->result, - noise_humidity->result, heightmap, biomemap); - - // Actually place the biome-specific nodes - MgStoneType stone_type = generateBiomes(noise_heat->result, noise_humidity->result); + // Init biome generator, place biome-specific nodes, and build biomemap + biomegen->calcBiomeNoise(node_min); + biomegen->getBiomes(heightmap); + MgStoneType stone_type = generateBiomes(); if (flags & MG_CAVES) generateCaves(stone_surface_max_y); @@ -358,18 +346,7 @@ void MapgenFractal::calculateNoise() // only if solid terrain is present in mapchunk noise_filler_depth->perlinMap2D(x, z); - noise_heat->perlinMap2D(x, z); - noise_humidity->perlinMap2D(x, z); - noise_heat_blend->perlinMap2D(x, z); - noise_humidity_blend->perlinMap2D(x, z); - for (s32 i = 0; i < csize.X * csize.Z; i++) { - noise_heat->result[i] += noise_heat_blend->result[i]; - noise_humidity->result[i] += noise_humidity_blend->result[i]; - } - - heatmap = noise_heat->result; - humidmap = noise_humidity->result; //printf("calculateNoise: %dus\n", t.stop()); } @@ -530,7 +507,7 @@ s16 MapgenFractal::generateTerrain() } -MgStoneType MapgenFractal::generateBiomes(float *heat_map, float *humidity_map) +MgStoneType MapgenFractal::generateBiomes() { v3s16 em = vm->m_area.getExtent(); u32 index = 0; @@ -567,7 +544,8 @@ MgStoneType MapgenFractal::generateBiomes(float *heat_map, float *humidity_map) // 3. When stone or water is detected but biome has not yet been calculated. if ((c == c_stone && (air_above || water_above || !biome)) || (c == c_water_source && (air_above || !biome))) { - biome = bmgr->getBiome(heat_map[index], humidity_map[index], y); + biome = biomegen->getBiomeAtIndex(index, y); + depth_top = biome->depth_top; base_filler = MYMAX(depth_top + biome->depth_filler + noise_filler_depth->result[index], 0); diff --git a/src/mapgen_fractal.h b/src/mapgen_fractal.h index dd96045e2..a0e51b2c6 100644 --- a/src/mapgen_fractal.h +++ b/src/mapgen_fractal.h @@ -88,11 +88,6 @@ public: Noise *noise_cave1; Noise *noise_cave2; - Noise *noise_heat; - Noise *noise_humidity; - Noise *noise_heat_blend; - Noise *noise_humidity_blend; - content_t c_stone; content_t c_water_source; content_t c_lava_source; @@ -114,7 +109,7 @@ public: void calculateNoise(); bool getFractalAtPoint(s16 x, s16 y, s16 z); s16 generateTerrain(); - MgStoneType generateBiomes(float *heat_map, float *humidity_map); + MgStoneType generateBiomes(); void dustTopNodes(); void generateCaves(s16 max_stone_y); }; diff --git a/src/mapgen_v5.cpp b/src/mapgen_v5.cpp index b98acb928..829a1f432 100644 --- a/src/mapgen_v5.cpp +++ b/src/mapgen_v5.cpp @@ -57,10 +57,7 @@ MapgenV5::MapgenV5(int mapgenid, MapgenParams *params, EmergeManager *emerge) // 1-down overgeneration this->zstride_1d = csize.X * (csize.Y + 1); - this->biomemap = new u8[csize.X * csize.Z]; this->heightmap = new s16[csize.X * csize.Z]; - this->heatmap = NULL; - this->humidmap = NULL; MapgenV5Params *sp = (MapgenV5Params *)params->sparams; @@ -79,15 +76,12 @@ MapgenV5::MapgenV5(int mapgenid, MapgenParams *params, EmergeManager *emerge) noise_cave1 = new Noise(&sp->np_cave1, seed, csize.X, csize.Y + 1, csize.Z); noise_cave2 = new Noise(&sp->np_cave2, seed, csize.X, csize.Y + 1, csize.Z); - // Biome noise - noise_heat = new Noise(¶ms->np_biome_heat, seed, csize.X, csize.Z); - noise_humidity = new Noise(¶ms->np_biome_humidity, seed, csize.X, csize.Z); - noise_heat_blend = new Noise(¶ms->np_biome_heat_blend, seed, csize.X, csize.Z); - noise_humidity_blend = new Noise(¶ms->np_biome_humidity_blend, seed, csize.X, csize.Z); + //// Initialize biome generator + biomegen = emerge->biomemgr->createBiomeGen( + BIOMEGEN_ORIGINAL, params->bparams, csize); + biomemap = biomegen->biomemap; //// Resolve nodes to be used - INodeDefManager *ndef = emerge->ndef; - c_stone = ndef->getId("mapgen_stone"); c_water_source = ndef->getId("mapgen_water_source"); c_lava_source = ndef->getId("mapgen_lava_source"); @@ -123,13 +117,9 @@ MapgenV5::~MapgenV5() delete noise_cave2; delete noise_ground; - delete noise_heat; - delete noise_humidity; - delete noise_heat_blend; - delete noise_humidity_blend; + delete biomegen; delete[] heightmap; - delete[] biomemap; } @@ -248,12 +238,10 @@ void MapgenV5::makeChunk(BlockMakeData *data) // Create heightmap updateHeightmap(node_min, node_max); - // Create biomemap at heightmap surface - bmgr->calcBiomes(csize.X, csize.Z, noise_heat->result, - noise_humidity->result, heightmap, biomemap); - - // Actually place the biome-specific nodes - MgStoneType stone_type = generateBiomes(noise_heat->result, noise_humidity->result); + // Init biome generator, place biome-specific nodes, and build biomemap + biomegen->calcBiomeNoise(node_min); + biomegen->getBiomes(heightmap); + MgStoneType stone_type = generateBiomes(); // Generate caves if ((flags & MG_CAVES) && (stone_surface_max_y >= node_min.Y)) @@ -343,18 +331,7 @@ void MapgenV5::calculateNoise() // only if solid terrain is present in mapchunk noise_filler_depth->perlinMap2D(x, z); - noise_heat->perlinMap2D(x, z); - noise_humidity->perlinMap2D(x, z); - noise_heat_blend->perlinMap2D(x, z); - noise_humidity_blend->perlinMap2D(x, z); - for (s32 i = 0; i < csize.X * csize.Z; i++) { - noise_heat->result[i] += noise_heat_blend->result[i]; - noise_humidity->result[i] += noise_humidity_blend->result[i]; - } - - heatmap = noise_heat->result; - humidmap = noise_humidity->result; //printf("calculateNoise: %dus\n", t.stop()); } @@ -416,7 +393,7 @@ int MapgenV5::generateBaseTerrain() } -MgStoneType MapgenV5::generateBiomes(float *heat_map, float *humidity_map) +MgStoneType MapgenV5::generateBiomes() { v3s16 em = vm->m_area.getExtent(); u32 index = 0; @@ -452,7 +429,8 @@ MgStoneType MapgenV5::generateBiomes(float *heat_map, float *humidity_map) // 3. When stone or water is detected but biome has not yet been calculated. if ((c == c_stone && (air_above || water_above || !biome)) || (c == c_water_source && (air_above || !biome))) { - biome = bmgr->getBiome(heat_map[index], humidity_map[index], y); + biome = biomegen->getBiomeAtIndex(index, y); + depth_top = biome->depth_top; base_filler = MYMAX(depth_top + biome->depth_filler + noise_filler_depth->result[index], 0); diff --git a/src/mapgen_v5.h b/src/mapgen_v5.h index fd2f7f4d8..eef3da617 100644 --- a/src/mapgen_v5.h +++ b/src/mapgen_v5.h @@ -70,11 +70,6 @@ public: Noise *noise_cave2; Noise *noise_ground; - Noise *noise_heat; - Noise *noise_humidity; - Noise *noise_heat_blend; - Noise *noise_humidity_blend; - content_t c_stone; content_t c_water_source; content_t c_lava_source; @@ -95,7 +90,7 @@ public: int getSpawnLevelAtPoint(v2s16 p); void calculateNoise(); int generateBaseTerrain(); - MgStoneType generateBiomes(float *heat_map, float *humidity_map); + MgStoneType generateBiomes(); void generateCaves(int max_stone_y); void dustTopNodes(); }; diff --git a/src/mapgen_v7.cpp b/src/mapgen_v7.cpp index 9fb65f577..fddb34682 100644 --- a/src/mapgen_v7.cpp +++ b/src/mapgen_v7.cpp @@ -64,10 +64,7 @@ MapgenV7::MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge) // 1-down overgeneration this->zstride_1d = csize.X * (csize.Y + 1); - this->biomemap = new u8[csize.X * csize.Z]; this->heightmap = new s16[csize.X * csize.Z]; - this->heatmap = NULL; - this->humidmap = NULL; this->ridge_heightmap = new s16[csize.X * csize.Z]; MapgenV7Params *sp = (MapgenV7Params *)params->sparams; @@ -92,15 +89,13 @@ MapgenV7::MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge) noise_cave1 = new Noise(&sp->np_cave1, seed, csize.X, csize.Y + 1, csize.Z); noise_cave2 = new Noise(&sp->np_cave2, seed, csize.X, csize.Y + 1, csize.Z); - //// Biome noise - noise_heat = new Noise(¶ms->np_biome_heat, seed, csize.X, csize.Z); - noise_humidity = new Noise(¶ms->np_biome_humidity, seed, csize.X, csize.Z); - noise_heat_blend = new Noise(¶ms->np_biome_heat_blend, seed, csize.X, csize.Z); - noise_humidity_blend = new Noise(¶ms->np_biome_humidity_blend, seed, csize.X, csize.Z); + // TODO(hmmmm): should we have a way to disable biomemanager biomes? + //// Initialize biome generator + biomegen = emerge->biomemgr->createBiomeGen( + BIOMEGEN_ORIGINAL, params->bparams, csize); + biomemap = biomegen->biomemap; //// Resolve nodes to be used - INodeDefManager *ndef = emerge->ndef; - c_stone = ndef->getId("mapgen_stone"); c_water_source = ndef->getId("mapgen_water_source"); c_lava_source = ndef->getId("mapgen_lava_source"); @@ -141,14 +136,10 @@ MapgenV7::~MapgenV7() delete noise_cave1; delete noise_cave2; - delete noise_heat; - delete noise_humidity; - delete noise_heat_blend; - delete noise_humidity_blend; + delete biomegen; delete[] ridge_heightmap; delete[] heightmap; - delete[] biomemap; } @@ -279,12 +270,10 @@ void MapgenV7::makeChunk(BlockMakeData *data) // Update heightmap to include mountain terrain updateHeightmap(node_min, node_max); - // Create biomemap at heightmap surface - bmgr->calcBiomes(csize.X, csize.Z, noise_heat->result, - noise_humidity->result, heightmap, biomemap); - - // Actually place the biome-specific nodes - MgStoneType stone_type = generateBiomes(noise_heat->result, noise_humidity->result); + // Init biome generator, place biome-specific nodes, and build biomemap + biomegen->calcBiomeNoise(node_min); + biomegen->getBiomes(heightmap); + MgStoneType stone_type = generateBiomes(); if (flags & MG_CAVES) generateCaves(stone_surface_max_y); @@ -384,35 +373,10 @@ void MapgenV7::calculateNoise() // Cave noises are calculated in generateCaves() // only if solid terrain is present in mapchunk - noise_filler_depth->perlinMap2D(x, z); - noise_heat->perlinMap2D(x, z); - noise_humidity->perlinMap2D(x, z); - noise_heat_blend->perlinMap2D(x, z); - noise_humidity_blend->perlinMap2D(x, z); - - for (s32 i = 0; i < csize.X * csize.Z; i++) { - noise_heat->result[i] += noise_heat_blend->result[i]; - noise_humidity->result[i] += noise_humidity_blend->result[i]; - } - - heatmap = noise_heat->result; - humidmap = noise_humidity->result; //printf("calculateNoise: %dus\n", t.stop()); } -Biome *MapgenV7::getBiomeAtPoint(v3s16 p) -{ - float heat = NoisePerlin2D(&noise_heat->np, p.X, p.Z, seed) + - NoisePerlin2D(&noise_heat_blend->np, p.X, p.Z, seed); - float humidity = NoisePerlin2D(&noise_humidity->np, p.X, p.Z, seed) + - NoisePerlin2D(&noise_humidity_blend->np, p.X, p.Z, seed); - s16 groundlevel = baseTerrainLevelAtPoint(p.X, p.Z); - - return bmgr->getBiome(heat, humidity, groundlevel); -} - - float MapgenV7::baseTerrainLevelAtPoint(s16 x, s16 z) { float hselect = NoisePerlin2D(&noise_height_select->np, x, z, seed); @@ -553,7 +517,7 @@ void MapgenV7::generateRidgeTerrain() } -MgStoneType MapgenV7::generateBiomes(float *heat_map, float *humidity_map) +MgStoneType MapgenV7::generateBiomes() { v3s16 em = vm->m_area.getExtent(); u32 index = 0; @@ -589,7 +553,8 @@ MgStoneType MapgenV7::generateBiomes(float *heat_map, float *humidity_map) // 3. When stone or water is detected but biome has not yet been calculated. if ((c == c_stone && (air_above || water_above || !biome)) || (c == c_water_source && (air_above || !biome))) { - biome = bmgr->getBiome(heat_map[index], humidity_map[index], y); + biome = biomegen->getBiomeAtIndex(index, y); + depth_top = biome->depth_top; base_filler = MYMAX(depth_top + biome->depth_filler + noise_filler_depth->result[index], 0); diff --git a/src/mapgen_v7.h b/src/mapgen_v7.h index c25220646..a8dd9986d 100644 --- a/src/mapgen_v7.h +++ b/src/mapgen_v7.h @@ -84,11 +84,6 @@ public: Noise *noise_cave1; Noise *noise_cave2; - Noise *noise_heat; - Noise *noise_humidity; - Noise *noise_heat_blend; - Noise *noise_humidity_blend; - content_t c_stone; content_t c_water_source; content_t c_lava_source; @@ -107,7 +102,6 @@ public: virtual void makeChunk(BlockMakeData *data); int getSpawnLevelAtPoint(v2s16 p); - Biome *getBiomeAtPoint(v3s16 p); float baseTerrainLevelAtPoint(s16 x, s16 z); float baseTerrainLevelFromMap(int index); @@ -119,7 +113,7 @@ public: int generateTerrain(); void generateRidgeTerrain(); - MgStoneType generateBiomes(float *heat_map, float *humidity_map); + MgStoneType generateBiomes(); void dustTopNodes(); void generateCaves(s16 max_stone_y); diff --git a/src/mapgen_valleys.cpp b/src/mapgen_valleys.cpp index 0ec5409cb..44083d9d3 100644 --- a/src/mapgen_valleys.cpp +++ b/src/mapgen_valleys.cpp @@ -68,7 +68,7 @@ MapgenValleys::MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager * : Mapgen(mapgenid, params, emerge) { this->m_emerge = emerge; - this->bmgr = emerge->biomemgr; + this->bmgr = emerge->biomemgr; //// amount of elements to skip for the next index //// for noise/height/biome maps (not vmanip) @@ -77,15 +77,13 @@ MapgenValleys::MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager * // 1-down overgeneration this->zstride_1d = csize.X * (csize.Y + 1); - this->biomemap = new u8[csize.X * csize.Z]; this->heightmap = new s16[csize.X * csize.Z]; - this->heatmap = NULL; - this->humidmap = NULL; this->map_gen_limit = MYMIN(MAX_MAP_GENERATION_LIMIT, g_settings->getU16("map_generation_limit")); MapgenValleysParams *sp = (MapgenValleysParams *)params->sparams; + BiomeParamsOriginal *bp = (BiomeParamsOriginal *)params->bparams; this->spflags = sp->spflags; this->altitude_chill = sp->altitude_chill; @@ -113,15 +111,16 @@ MapgenValleys::MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager * noise_cave2 = new Noise(&sp->np_cave2, seed, csize.X, csize.Y + 1, csize.Z); noise_massive_caves = new Noise(&sp->np_massive_caves, seed, csize.X, csize.Y + 1, csize.Z); - //// Biome noise - noise_heat_blend = new Noise(¶ms->np_biome_heat_blend, seed, csize.X, csize.Z); - noise_heat = new Noise(¶ms->np_biome_heat, seed, csize.X, csize.Z); - noise_humidity_blend = new Noise(¶ms->np_biome_humidity_blend, seed, csize.X, csize.Z); - noise_humidity = new Noise(¶ms->np_biome_humidity, seed, csize.X, csize.Z); + //// Initialize biome generator + // NOTE: valleys mapgen can only use BiomeGenOriginal + biomegen = emerge->biomemgr->createBiomeGen( + BIOMEGEN_ORIGINAL, params->bparams, csize); + biomemap = biomegen->biomemap; + m_bgen = (BiomeGenOriginal *)biomegen; this->humid_rivers = (spflags & MGVALLEYS_HUMID_RIVERS); this->use_altitude_chill = (spflags & MGVALLEYS_ALT_CHILL); - this->humidity_adjust = params->np_biome_humidity.offset - 50.f; + this->humidity_adjust = bp->np_humidity.offset - 50.f; // a small chance of overflows if the settings are very high this->cave_water_max_height = water_level + MYMAX(0, water_features_lim - 4) * 50; @@ -130,8 +129,6 @@ MapgenValleys::MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager * tcave_cache = new float[csize.Y + 2]; //// Resolve nodes to be used - INodeDefManager *ndef = emerge->ndef; - c_cobble = ndef->getId("mapgen_cobble"); c_desert_stone = ndef->getId("mapgen_desert_stone"); c_dirt = ndef->getId("mapgen_dirt"); @@ -174,12 +171,8 @@ MapgenValleys::~MapgenValleys() delete noise_valley_depth; delete noise_valley_profile; - delete noise_heat; - delete noise_heat_blend; - delete noise_humidity; - delete noise_humidity_blend; + delete biomegen; - delete[] biomemap; delete[] heightmap; delete[] tcave_cache; } @@ -293,14 +286,19 @@ void MapgenValleys::makeChunk(BlockMakeData *data) // Generate noise maps and base terrain height. calculateNoise(); + // Generate biome noises. Note this must be executed strictly before + // generateTerrain, because generateTerrain depends on intermediate + // biome-related noises. + biomegen->calcBiomeNoise(node_min); + // Generate base terrain with initial heightmaps s16 stone_surface_max_y = generateTerrain(); - // Create biomemap at heightmap surface - bmgr->calcBiomes(csize.X, csize.Z, heatmap, humidmap, heightmap, biomemap); + // Build biomemap + biomegen->getBiomes(heightmap); - // Actually place the biome-specific nodes - MgStoneType stone_type = generateBiomes(heatmap, humidmap); + // Place biome-specific nodes + MgStoneType stone_type = generateBiomes(); // Cave creation. if (flags & MG_CAVES) @@ -391,10 +389,6 @@ void MapgenValleys::calculateNoise() //TimeTaker tcn("actualNoise"); noise_filler_depth->perlinMap2D(x, z); - noise_heat_blend->perlinMap2D(x, z); - noise_heat->perlinMap2D(x, z); - noise_humidity_blend->perlinMap2D(x, z); - noise_humidity->perlinMap2D(x, z); noise_inter_valley_slope->perlinMap2D(x, z); noise_rivers->perlinMap2D(x, z); noise_terrain_height->perlinMap2D(x, z); @@ -418,9 +412,8 @@ void MapgenValleys::calculateNoise() } for (s32 index = 0; index < csize.X * csize.Z; index++) { - noise_heat->result[index] += noise_heat_blend->result[index] + heat_offset; - noise_humidity->result[index] *= humidity_scale; - noise_humidity->result[index] += noise_humidity_blend->result[index]; + m_bgen->heatmap[index] += heat_offset; + m_bgen->humidmap[index] *= humidity_scale; } TerrainNoise tn; @@ -450,9 +443,6 @@ void MapgenValleys::calculateNoise() float mount = terrainLevelFromNoise(&tn); noise_terrain_height->result[index] = mount; } - - heatmap = noise_heat->result; - humidmap = noise_humidity->result; } @@ -596,7 +586,7 @@ int MapgenValleys::generateTerrain() float river_y = noise_rivers->result[index_2d]; float surface_y = noise_terrain_height->result[index_2d]; float slope = noise_inter_valley_slope->result[index_2d]; - float t_heat = noise_heat->result[index_2d]; + float t_heat = m_bgen->heatmap[index_2d]; heightmap[index_2d] = -MAX_MAP_GENERATION_LIMIT; @@ -610,7 +600,7 @@ int MapgenValleys::generateTerrain() t_heat -= alt_to_heat * MYMAX(surface_y, river_y) / altitude_chill; // If humidity is low or heat is high, lower the water table. - float delta = noise_humidity->result[index_2d] - 50.f; + float delta = m_bgen->humidmap[index_2d] - 50.f; if (delta < 0.f) { float t_evap = (t_heat - 32.f) / evaporation; river_y += delta * MYMAX(t_evap, 0.08f); @@ -672,7 +662,7 @@ int MapgenValleys::generateTerrain() // Use base ground (water table) in a riverbed, to // avoid an unnatural rise in humidity. float t_alt = MYMAX(noise_rivers->result[index_2d], (float)heightmap[index_2d]); - float humid = noise_humidity->result[index_2d]; + float humid = m_bgen->humidmap[index_2d]; float water_depth = (t_alt - river_y) / humidity_dropoff; humid *= 1.f + pow(0.5f, MYMAX(water_depth, 1.f)); @@ -683,7 +673,7 @@ int MapgenValleys::generateTerrain() if (t_alt > 0.f) humid -= alt_to_humid * t_alt / altitude_chill; - noise_humidity->result[index_2d] = humid; + m_bgen->humidmap[index_2d] = humid; } // Assign the heat adjusted by any changed altitudes. @@ -693,9 +683,9 @@ int MapgenValleys::generateTerrain() float t_alt = MYMAX(noise_rivers->result[index_2d], (float)heightmap[index_2d]); if (humid_rivers && heightmap[index_2d] == (s16)myround(surface_y)) // The altitude hasn't changed. Use the first result. - noise_heat->result[index_2d] = t_heat; + m_bgen->heatmap[index_2d] = t_heat; else if (t_alt > 0.f) - noise_heat->result[index_2d] -= alt_to_heat * t_alt / altitude_chill; + m_bgen->heatmap[index_2d] -= alt_to_heat * t_alt / altitude_chill; } } @@ -703,7 +693,7 @@ int MapgenValleys::generateTerrain() } -MgStoneType MapgenValleys::generateBiomes(float *heat_map, float *humidity_map) +MgStoneType MapgenValleys::generateBiomes() { v3s16 em = vm->m_area.getExtent(); u32 index = 0; @@ -739,9 +729,9 @@ MgStoneType MapgenValleys::generateBiomes(float *heat_map, float *humidity_map) // 3. When stone or water is detected but biome has not yet been calculated. if ((c == c_stone && (air_above || water_above || !biome)) || ((c == c_water_source || c == c_river_water_source) - && (air_above || !biome))) { + && (air_above || !biome))) { // Both heat and humidity have already been adjusted for altitude. - biome = bmgr->getBiome(heat_map[index], humidity_map[index], y); + biome = biomegen->getBiomeAtIndex(index, y); depth_top = biome->depth_top; base_filler = MYMAX(depth_top diff --git a/src/mapgen_valleys.h b/src/mapgen_valleys.h index 5224ea54b..0e268c1cf 100644 --- a/src/mapgen_valleys.h +++ b/src/mapgen_valleys.h @@ -39,6 +39,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #define MYCUBE(x) (x) * (x) * (x) class BiomeManager; +class BiomeGenOriginal; // Global profiler //class Profiler; @@ -98,6 +99,7 @@ public: private: EmergeManager *m_emerge; BiomeManager *bmgr; + BiomeGenOriginal *m_bgen; int ystride; int zstride; @@ -136,11 +138,6 @@ private: Noise *noise_valley_depth; Noise *noise_valley_profile; - Noise *noise_heat; - Noise *noise_heat_blend; - Noise *noise_humidity; - Noise *noise_humidity_blend; - content_t c_cobble; content_t c_desert_stone; content_t c_dirt; @@ -164,9 +161,7 @@ private: float terrainLevelFromNoise(TerrainNoise *tn); float adjustedTerrainLevelFromNoise(TerrainNoise *tn); - float humidityByTerrain(float humidity_base, float mount, float rivers, float valley); - - MgStoneType generateBiomes(float *heat_map, float *humidity_map); + MgStoneType generateBiomes(); void dustTopNodes(); void generateCaves(s16 max_stone_y); diff --git a/src/mg_biome.cpp b/src/mg_biome.cpp index 9ab8d06cc..dac0f7acc 100644 --- a/src/mg_biome.cpp +++ b/src/mg_biome.cpp @@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/numeric.h" #include "util/mathconstants.h" #include "porting.h" +#include "settings.h" /////////////////////////////////////////////////////////////////////////////// @@ -63,49 +64,11 @@ BiomeManager::BiomeManager(IGameDef *gamedef) : } - BiomeManager::~BiomeManager() { - //if (biomecache) - // delete[] biomecache; } - -// just a PoC, obviously needs optimization later on (precalculate this) -void BiomeManager::calcBiomes(s16 sx, s16 sy, float *heat_map, - float *humidity_map, s16 *height_map, u8 *biomeid_map) -{ - for (s32 i = 0; i != sx * sy; i++) { - Biome *biome = getBiome(heat_map[i], humidity_map[i], height_map[i]); - biomeid_map[i] = biome->index; - } -} - - -Biome *BiomeManager::getBiome(float heat, float humidity, s16 y) -{ - Biome *b, *biome_closest = NULL; - float dist_min = FLT_MAX; - - for (size_t i = 1; i < m_objects.size(); i++) { - b = (Biome *)m_objects[i]; - if (!b || y > b->y_max || y < b->y_min) - continue; - - float d_heat = heat - b->heat_point; - float d_humidity = humidity - b->humidity_point; - float dist = (d_heat * d_heat) + - (d_humidity * d_humidity); - if (dist < dist_min) { - dist_min = dist; - biome_closest = b; - } - } - - return biome_closest ? biome_closest : (Biome *)m_objects[0]; -} - void BiomeManager::clear() { EmergeManager *emerge = m_gamedef->getEmergeManager(); @@ -118,17 +81,153 @@ void BiomeManager::clear() } // Don't delete the first biome - for (size_t i = 1; i < m_objects.size(); i++) { - Biome *b = (Biome *)m_objects[i]; - delete b; - } + for (size_t i = 1; i < m_objects.size(); i++) + delete (Biome *)m_objects[i]; m_objects.resize(1); } +//////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// +void BiomeParamsOriginal::readParams(const Settings *settings) +{ + settings->getNoiseParams("mg_biome_np_heat", np_heat); + settings->getNoiseParams("mg_biome_np_heat_blend", np_heat_blend); + settings->getNoiseParams("mg_biome_np_humidity", np_humidity); + settings->getNoiseParams("mg_biome_np_humidity_blend", np_humidity_blend); +} + + +void BiomeParamsOriginal::writeParams(Settings *settings) const +{ + settings->setNoiseParams("mg_biome_np_heat", np_heat); + settings->setNoiseParams("mg_biome_np_heat_blend", np_heat_blend); + settings->setNoiseParams("mg_biome_np_humidity", np_humidity); + settings->setNoiseParams("mg_biome_np_humidity_blend", np_humidity_blend); +} + + +//////////////////////////////////////////////////////////////////////////////// + +BiomeGenOriginal::BiomeGenOriginal(BiomeManager *biomemgr, + BiomeParamsOriginal *params, v3s16 chunksize) +{ + m_bmgr = biomemgr; + m_params = params; + m_csize = chunksize; + + noise_heat = new Noise(¶ms->np_heat, + params->seed, m_csize.X, m_csize.Z); + noise_humidity = new Noise(¶ms->np_humidity, + params->seed, m_csize.X, m_csize.Z); + noise_heat_blend = new Noise(¶ms->np_heat_blend, + params->seed, m_csize.X, m_csize.Z); + noise_humidity_blend = new Noise(¶ms->np_humidity_blend, + params->seed, m_csize.X, m_csize.Z); + + heatmap = noise_heat->result; + humidmap = noise_humidity->result; + biomemap = new u8[m_csize.X * m_csize.Z]; +} + +BiomeGenOriginal::~BiomeGenOriginal() +{ + delete []biomemap; + + delete noise_heat; + delete noise_humidity; + delete noise_heat_blend; + delete noise_humidity_blend; +} + + +Biome *BiomeGenOriginal::calcBiomeAtPoint(v3s16 pos) const +{ + float heat = + NoisePerlin2D(&m_params->np_heat, pos.X, pos.Z, m_params->seed) + + NoisePerlin2D(&m_params->np_heat_blend, pos.X, pos.Z, m_params->seed); + float humidity = + NoisePerlin2D(&m_params->np_humidity, pos.X, pos.Z, m_params->seed) + + NoisePerlin2D(&m_params->np_humidity_blend, pos.X, pos.Z, m_params->seed); + + return calcBiomeFromNoise(heat, humidity, pos.Y); +} + + +void BiomeGenOriginal::calcBiomeNoise(v3s16 pmin) +{ + m_pmin = pmin; + + noise_heat->perlinMap2D(pmin.X, pmin.Z); + noise_humidity->perlinMap2D(pmin.X, pmin.Z); + noise_heat_blend->perlinMap2D(pmin.X, pmin.Z); + noise_humidity_blend->perlinMap2D(pmin.X, pmin.Z); + + for (s32 i = 0; i < m_csize.X * m_csize.Z; i++) { + noise_heat->result[i] += noise_heat_blend->result[i]; + noise_humidity->result[i] += noise_humidity_blend->result[i]; + } +} + + +u8 *BiomeGenOriginal::getBiomes(s16 *heightmap) +{ + for (s32 i = 0; i != m_csize.X * m_csize.Z; i++) { + Biome *biome = calcBiomeFromNoise( + noise_heat->result[i], + noise_humidity->result[i], + heightmap[i]); + + biomemap[i] = biome->index; + } + + return biomemap; +} + + +Biome *BiomeGenOriginal::getBiomeAtPoint(v3s16 pos) const +{ + return getBiomeAtIndex( + (pos.Z - m_pmin.Z) * m_csize.X + (pos.X - m_pmin.X), + pos.Y); +} + + +Biome *BiomeGenOriginal::getBiomeAtIndex(size_t index, s16 y) const +{ + return calcBiomeFromNoise( + noise_heat->result[index], + noise_humidity->result[index], + y); +} + + +Biome *BiomeGenOriginal::calcBiomeFromNoise(float heat, float humidity, s16 y) const +{ + Biome *b, *biome_closest = NULL; + float dist_min = FLT_MAX; + + for (size_t i = 1; i < m_bmgr->getNumObjects(); i++) { + b = (Biome *)m_bmgr->getRaw(i); + if (!b || y > b->y_max || y < b->y_min) + continue; + + float d_heat = heat - b->heat_point; + float d_humidity = humidity - b->humidity_point; + float dist = (d_heat * d_heat) + + (d_humidity * d_humidity); + if (dist < dist_min) { + dist_min = dist; + biome_closest = b; + } + } + + return biome_closest ? biome_closest : (Biome *)m_bmgr->getRaw(BIOME_NONE); +} + + +//////////////////////////////////////////////////////////////////////////////// void Biome::resolveNodeNames() { diff --git a/src/mg_biome.h b/src/mg_biome.h index 8d519f808..e9378fd79 100644 --- a/src/mg_biome.h +++ b/src/mg_biome.h @@ -22,6 +22,16 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "objdef.h" #include "nodedef.h" +#include "noise.h" + +class Settings; +class BiomeManager; + +//// +//// Biome +//// + +#define BIOME_NONE ((u8)0) enum BiomeType { @@ -56,10 +66,122 @@ public: virtual void resolveNodeNames(); }; + +//// +//// BiomeGen +//// + +enum BiomeGenType { + BIOMEGEN_ORIGINAL, +}; + +struct BiomeParams { + virtual void readParams(const Settings *settings) = 0; + virtual void writeParams(Settings *settings) const = 0; + virtual ~BiomeParams() {} + + int seed; +}; + +class BiomeGen { +public: + virtual ~BiomeGen() {} + virtual BiomeGenType getType() const = 0; + + // Calculates the biome at the exact position provided. This function can + // be called at any time, but may be less efficient than the latter methods, + // depending on implementation. + virtual Biome *calcBiomeAtPoint(v3s16 pos) const = 0; + + // Computes any intermediate results needed for biome generation. Must be + // called before using any of: getBiomes, getBiomeAtPoint, or getBiomeAtIndex. + // Calling this invalidates the previous results stored in biomemap. + virtual void calcBiomeNoise(v3s16 pmin) = 0; + + // Gets all biomes in current chunk using each corresponding element of + // heightmap as the y position, then stores the results by biome index in + // biomemap (also returned) + virtual u8 *getBiomes(s16 *heightmap) = 0; + + // Gets a single biome at the specified position, which must be contained + // in the region formed by m_pmin and (m_pmin + m_csize - 1). + virtual Biome *getBiomeAtPoint(v3s16 pos) const = 0; + + // Same as above, but uses a raw numeric index correlating to the (x,z) position. + virtual Biome *getBiomeAtIndex(size_t index, s16 y) const = 0; + + // Result of calcBiomes bulk computation. + u8 *biomemap; + +protected: + BiomeManager *m_bmgr; + v3s16 m_pmin; + v3s16 m_csize; +}; + + +//// +//// BiomeGen implementations +//// + +// +// Original biome algorithm (Whittaker's classification + surface height) +// + +struct BiomeParamsOriginal : public BiomeParams { + BiomeParamsOriginal() : + np_heat(50, 50, v3f(750.0, 750.0, 750.0), 5349, 3, 0.5, 2.0), + np_humidity(50, 50, v3f(750.0, 750.0, 750.0), 842, 3, 0.5, 2.0), + np_heat_blend(0, 1.5, v3f(8.0, 8.0, 8.0), 13, 2, 1.0, 2.0), + np_humidity_blend(0, 1.5, v3f(8.0, 8.0, 8.0), 90003, 2, 1.0, 2.0) + { + } + + virtual void readParams(const Settings *settings); + virtual void writeParams(Settings *settings) const; + + NoiseParams np_heat; + NoiseParams np_humidity; + NoiseParams np_heat_blend; + NoiseParams np_humidity_blend; +}; + +class BiomeGenOriginal : public BiomeGen { +public: + BiomeGenOriginal(BiomeManager *biomemgr, + BiomeParamsOriginal *params, v3s16 chunksize); + virtual ~BiomeGenOriginal(); + + BiomeGenType getType() const { return BIOMEGEN_ORIGINAL; } + + Biome *calcBiomeAtPoint(v3s16 pos) const; + void calcBiomeNoise(v3s16 pmin); + + u8 *getBiomes(s16 *heightmap); + Biome *getBiomeAtPoint(v3s16 pos) const; + Biome *getBiomeAtIndex(size_t index, s16 y) const; + + Biome *calcBiomeFromNoise(float heat, float humidity, s16 y) const; + + float *heatmap; + float *humidmap; + +private: + BiomeParamsOriginal *m_params; + + Noise *noise_heat; + Noise *noise_humidity; + Noise *noise_heat_blend; + Noise *noise_humidity_blend; +}; + + +//// +//// BiomeManager +//// + class BiomeManager : public ObjDefManager { public: - static const char *OBJECT_TITLE; - BiomeManager(IGameDef *gamedef); virtual ~BiomeManager(); @@ -73,15 +195,36 @@ public: return new Biome; } + BiomeGen *createBiomeGen(BiomeGenType type, BiomeParams *params, v3s16 chunksize) + { + switch (type) { + case BIOMEGEN_ORIGINAL: + return new BiomeGenOriginal(this, + (BiomeParamsOriginal *)params, chunksize); + default: + return NULL; + } + } + + static BiomeParams *createBiomeParams(BiomeGenType type) + { + switch (type) { + case BIOMEGEN_ORIGINAL: + return new BiomeParamsOriginal; + default: + return NULL; + } + } + virtual void clear(); - void calcBiomes(s16 sx, s16 sy, float *heat_map, float *humidity_map, - s16 *height_map, u8 *biomeid_map); - Biome *getBiome(float heat, float humidity, s16 y); + // Looks for pos in the biome cache, and if non-existent, looks up by noise + u8 getBiomeAtPoint(v3s16 pos); private: IGameDef *m_gamedef; + }; -#endif +#endif diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index fb839176b..405b93b86 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -528,24 +528,26 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L) return 1; } case MGOBJ_BIOMEMAP: { - if (!mg->biomemap) + if (!mg->biomegen) return 0; lua_newtable(L); for (size_t i = 0; i != maplen; i++) { - lua_pushinteger(L, mg->biomemap[i]); + lua_pushinteger(L, mg->biomegen->biomemap[i]); lua_rawseti(L, -2, i + 1); } return 1; } case MGOBJ_HEATMAP: { - if (!mg->heatmap) + if (!mg->biomegen || mg->biomegen->getType() != BIOMEGEN_ORIGINAL) return 0; + BiomeGenOriginal *bg = (BiomeGenOriginal *)mg->biomegen; + lua_newtable(L); for (size_t i = 0; i != maplen; i++) { - lua_pushnumber(L, mg->heatmap[i]); + lua_pushnumber(L, bg->heatmap[i]); lua_rawseti(L, -2, i + 1); } @@ -553,12 +555,14 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L) } case MGOBJ_HUMIDMAP: { - if (!mg->humidmap) + if (!mg->biomegen || mg->biomegen->getType() != BIOMEGEN_ORIGINAL) return 0; + BiomeGenOriginal *bg = (BiomeGenOriginal *)mg->biomegen; + lua_newtable(L); for (size_t i = 0; i != maplen; i++) { - lua_pushnumber(L, mg->humidmap[i]); + lua_pushnumber(L, bg->humidmap[i]); lua_rawseti(L, -2, i + 1); } From 87bc39dca733a2cc805bc1071794c4d5d7937115 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Mon, 2 May 2016 02:24:57 -0400 Subject: [PATCH 019/418] Mapgen: Combine generateBiomes, dustTopNodes, and generateCaves This commit condenses the above methods into a single implementation used by V7, V5, Flat, Fractal, and Valleys mapgens and introduces MapgenBasic. --- src/mapgen.cpp | 236 +++++++++++++++++++++++++++++++++++++++++ src/mapgen.h | 56 ++++++++++ src/mapgen_flat.cpp | 221 +------------------------------------- src/mapgen_flat.h | 29 +---- src/mapgen_fractal.cpp | 221 +------------------------------------- src/mapgen_fractal.h | 23 +--- src/mapgen_v5.cpp | 221 +------------------------------------- src/mapgen_v5.h | 24 +---- src/mapgen_v7.cpp | 230 ++------------------------------------- src/mapgen_v7.h | 25 +---- src/mapgen_valleys.cpp | 165 +--------------------------- src/mapgen_valleys.h | 26 +---- 12 files changed, 316 insertions(+), 1161 deletions(-) diff --git a/src/mapgen.cpp b/src/mapgen.cpp index e269bf454..f491ed9eb 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -39,6 +39,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/numeric.h" #include "filesys.h" #include "log.h" +#include "cavegen.h" FlagDesc flagdesc_mapgen[] = { {"trees", MG_TREES}, @@ -369,6 +370,241 @@ void Mapgen::spreadLight(v3s16 nmin, v3s16 nmax) } +//// +//// MapgenBasic +//// + +MapgenBasic::MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emerge) + : Mapgen(mapgenid, params, emerge) +{ + +} + +MgStoneType MapgenBasic::generateBiomes() +{ + v3s16 em = vm->m_area.getExtent(); + u32 index = 0; + MgStoneType stone_type = STONE; + + for (s16 z = node_min.Z; z <= node_max.Z; z++) + for (s16 x = node_min.X; x <= node_max.X; x++, index++) { + Biome *biome = NULL; + u16 depth_top = 0; + u16 base_filler = 0; + u16 depth_water_top = 0; + u32 vi = vm->m_area.index(x, node_max.Y, z); + + // Check node at base of mapchunk above, either a node of a previously + // generated mapchunk or if not, a node of overgenerated base terrain. + content_t c_above = vm->m_data[vi + em.X].getContent(); + bool air_above = c_above == CONTENT_AIR; + bool water_above = (c_above == c_water_source || c_above == c_river_water_source); + + // If there is air or water above enable top/filler placement, otherwise force + // nplaced to stone level by setting a number exceeding any possible filler depth. + u16 nplaced = (air_above || water_above) ? 0 : U16_MAX; + + for (s16 y = node_max.Y; y >= node_min.Y; y--) { + content_t c = vm->m_data[vi].getContent(); + + // Biome is recalculated each time an upper surface is detected while + // working down a column. The selected biome then remains in effect for + // all nodes below until the next surface and biome recalculation. + // Biome is recalculated: + // 1. At the surface of stone below air or water. + // 2. At the surface of water below air. + // 3. When stone or water is detected but biome has not yet been calculated. + if ((c == c_stone && (air_above || water_above || !biome)) + || ((c == c_water_source || c == c_river_water_source) + && (air_above || !biome))) { + biome = biomegen->getBiomeAtIndex(index, y); + + depth_top = biome->depth_top; + base_filler = MYMAX(depth_top + + biome->depth_filler + + noise_filler_depth->result[index], 0.f); + depth_water_top = biome->depth_water_top; + + // Detect stone type for dungeons during every biome calculation. + // This is more efficient than detecting per-node and will not + // miss any desert stone or sandstone biomes. + if (biome->c_stone == c_desert_stone) + stone_type = DESERT_STONE; + else if (biome->c_stone == c_sandstone) + stone_type = SANDSTONE; + } + + if (c == c_stone) { + content_t c_below = vm->m_data[vi - em.X].getContent(); + + // If the node below isn't solid, make this node stone, so that + // any top/filler nodes above are structurally supported. + // This is done by aborting the cycle of top/filler placement + // immediately by forcing nplaced to stone level. + if (c_below == CONTENT_AIR + || c_below == c_water_source + || c_below == c_river_water_source) + nplaced = U16_MAX; + + if (nplaced < depth_top) { + vm->m_data[vi] = MapNode(biome->c_top); + nplaced++; + } else if (nplaced < base_filler) { + vm->m_data[vi] = MapNode(biome->c_filler); + nplaced++; + } else { + vm->m_data[vi] = MapNode(biome->c_stone); + } + + air_above = false; + water_above = false; + } else if (c == c_water_source) { + vm->m_data[vi] = MapNode((y > (s32)(water_level - depth_water_top)) + ? biome->c_water_top : biome->c_water); + nplaced = 0; // Enable top/filler placement for next surface + air_above = false; + water_above = true; + } else if (c == c_river_water_source) { + vm->m_data[vi] = MapNode(biome->c_river_water); + nplaced = depth_top; // Enable filler placement for next surface + air_above = false; + water_above = true; + } else if (c == CONTENT_AIR) { + nplaced = 0; // Enable top/filler placement for next surface + air_above = true; + water_above = false; + } else { // Possible various nodes overgenerated from neighbouring mapchunks + nplaced = U16_MAX; // Disable top/filler placement + air_above = false; + water_above = false; + } + + vm->m_area.add_y(em, vi, -1); + } + } + + return stone_type; +} + + +void MapgenBasic::dustTopNodes() +{ + if (node_max.Y < water_level) + return; + + v3s16 em = vm->m_area.getExtent(); + u32 index = 0; + + for (s16 z = node_min.Z; z <= node_max.Z; z++) + for (s16 x = node_min.X; x <= node_max.X; x++, index++) { + Biome *biome = (Biome *)bmgr->getRaw(biomemap[index]); + + if (biome->c_dust == CONTENT_IGNORE) + continue; + + u32 vi = vm->m_area.index(x, full_node_max.Y, z); + content_t c_full_max = vm->m_data[vi].getContent(); + s16 y_start; + + if (c_full_max == CONTENT_AIR) { + y_start = full_node_max.Y - 1; + } else if (c_full_max == CONTENT_IGNORE) { + vi = vm->m_area.index(x, node_max.Y + 1, z); + content_t c_max = vm->m_data[vi].getContent(); + + if (c_max == CONTENT_AIR) + y_start = node_max.Y; + else + continue; + } else { + continue; + } + + vi = vm->m_area.index(x, y_start, z); + for (s16 y = y_start; y >= node_min.Y - 1; y--) { + if (vm->m_data[vi].getContent() != CONTENT_AIR) + break; + + vm->m_area.add_y(em, vi, -1); + } + + content_t c = vm->m_data[vi].getContent(); + if (!ndef->get(c).buildable_to && c != CONTENT_IGNORE && c != biome->c_dust) { + vm->m_area.add_y(em, vi, 1); + vm->m_data[vi] = MapNode(biome->c_dust); + } + } +} + + +void MapgenBasic::generateCaves(s16 max_stone_y, s16 large_cave_depth) +{ + if (max_stone_y < node_min.Y) + return; + + noise_cave1->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z); + noise_cave2->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z); + + v3s16 em = vm->m_area.getExtent(); + u32 index2d = 0; + + for (s16 z = node_min.Z; z <= node_max.Z; z++) + for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) { + bool column_is_open = false; // Is column open to overground + bool is_tunnel = false; // Is tunnel or tunnel floor + u32 vi = vm->m_area.index(x, node_max.Y, z); + u32 index3d = (z - node_min.Z) * zstride_1d + csize.Y * ystride + + (x - node_min.X); + // Biome of column + Biome *biome = (Biome *)bmgr->getRaw(biomemap[index2d]); + + // Don't excavate the overgenerated stone at node_max.Y + 1, + // this creates a 'roof' over the tunnel, preventing light in + // tunnels at mapchunk borders when generating mapchunks upwards. + // This 'roof' is removed when the mapchunk above is generated. + for (s16 y = node_max.Y; y >= node_min.Y - 1; y--, + index3d -= ystride, + vm->m_area.add_y(em, vi, -1)) { + + content_t c = vm->m_data[vi].getContent(); + if (c == CONTENT_AIR || c == biome->c_water_top || + c == biome->c_water) { + column_is_open = true; + continue; + } + // Ground + float d1 = contour(noise_cave1->result[index3d]); + float d2 = contour(noise_cave2->result[index3d]); + + if (d1 * d2 > cave_width && ndef->get(c).is_ground_content) { + // In tunnel and ground content, excavate + vm->m_data[vi] = MapNode(CONTENT_AIR); + is_tunnel = true; + } else { + // Not in tunnel or not ground content + if (is_tunnel && column_is_open && + (c == biome->c_filler || c == biome->c_stone)) + // Tunnel entrance floor + vm->m_data[vi] = MapNode(biome->c_top); + + column_is_open = false; + is_tunnel = false; + } + } + } + + if (node_max.Y > large_cave_depth) + return; + + PseudoRandom ps(blockseed + 21343); + u32 bruises_count = ps.range(0, 2); + for (u32 i = 0; i < bruises_count; i++) { + CaveV5 cave(this, &ps); ////caves version varies ---- todo- fix this! + cave.makeCave(node_min, node_max, max_stone_y); + } +} + + //// //// GenerateNotifier //// diff --git a/src/mapgen.h b/src/mapgen.h index 554ec6084..bee8e786a 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -46,6 +46,7 @@ extern FlagDesc flagdesc_gennotify[]; class Biome; class BiomeGen; struct BiomeParams; +class BiomeManager; class EmergeManager; class MapBlock; class VoxelManipulator; @@ -137,6 +138,16 @@ struct MapgenParams { void save(Settings &settings) const; }; + +/* + Generic interface for map generators. All mapgens must inherit this class. + If a feature exposed by a public member pointer is not supported by a + certain mapgen, it must be set to NULL. + + Apart from makeChunk, getGroundLevelAtPoint, and getSpawnLevelAtPoint, all + methods can be used by constructing a Mapgen base class and setting the + appropriate public members (e.g. vm, ndef, and so on). +*/ class Mapgen { public: int seed; @@ -189,6 +200,51 @@ private: DISABLE_CLASS_COPY(Mapgen); }; +/* + MapgenBasic is a Mapgen implementation that handles basic functionality + the majority of conventional mapgens will probably want to use, but isn't + generic enough to be included as part of the base Mapgen class (such as + generating biome terrain over terrain node skeletons, generating caves, + dungeons, etc.) + + Inherit MapgenBasic instead of Mapgen to add this basic functionality to + your mapgen without having to reimplement it. Feel free to override any of + these methods if you desire different or more advanced behavior. + + Note that you must still create your own generateTerrain implementation when + inheriting MapgenBasic. +*/ +class MapgenBasic : public Mapgen { +public: + EmergeManager *m_emerge; + BiomeManager *bmgr; + + Noise *noise_filler_depth; + Noise *noise_cave1; + Noise *noise_cave2; + + v3s16 node_min; + v3s16 node_max; + v3s16 full_node_min; + v3s16 full_node_max; + + content_t c_stone; + content_t c_water_source; + content_t c_river_water_source; + content_t c_desert_stone; + content_t c_sandstone; + + int ystride; + int zstride_1d; + float cave_width; + + MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emerge); + + virtual MgStoneType generateBiomes(); + virtual void dustTopNodes(); + virtual void generateCaves(s16 max_stone_y, s16 large_cave_depth); +}; + struct MapgenFactory { virtual Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) = 0; diff --git a/src/mapgen_flat.cpp b/src/mapgen_flat.cpp index cb9aa9a8b..97f1c4b2a 100644 --- a/src/mapgen_flat.cpp +++ b/src/mapgen_flat.cpp @@ -50,7 +50,7 @@ FlagDesc flagdesc_mapgen_flat[] = { MapgenFlat::MapgenFlat(int mapgenid, MapgenParams *params, EmergeManager *emerge) - : Mapgen(mapgenid, params, emerge) + : MapgenBasic(mapgenid, params, emerge) { this->m_emerge = emerge; this->bmgr = emerge->biomemgr; @@ -248,7 +248,7 @@ void MapgenFlat::makeChunk(BlockMakeData *data) MgStoneType stone_type = generateBiomes(); if (flags & MG_CAVES) - generateCaves(stone_surface_max_y); + generateCaves(stone_surface_max_y, large_cave_depth); if ((flags & MG_DUNGEONS) && (stone_surface_max_y >= node_min.Y)) { DungeonParams dp; @@ -381,220 +381,3 @@ s16 MapgenFlat::generateTerrain() return stone_surface_max_y; } - - -MgStoneType MapgenFlat::generateBiomes() -{ - v3s16 em = vm->m_area.getExtent(); - u32 index = 0; - MgStoneType stone_type = STONE; - - for (s16 z = node_min.Z; z <= node_max.Z; z++) - for (s16 x = node_min.X; x <= node_max.X; x++, index++) { - Biome *biome = NULL; - u16 depth_top = 0; - u16 base_filler = 0; - u16 depth_water_top = 0; - u32 vi = vm->m_area.index(x, node_max.Y, z); - - // Check node at base of mapchunk above, either a node of a previously - // generated mapchunk or if not, a node of overgenerated base terrain. - content_t c_above = vm->m_data[vi + em.X].getContent(); - bool air_above = c_above == CONTENT_AIR; - bool water_above = c_above == c_water_source; - - // If there is air or water above enable top/filler placement, otherwise force - // nplaced to stone level by setting a number exceeding any possible filler depth. - u16 nplaced = (air_above || water_above) ? 0 : U16_MAX; - - - for (s16 y = node_max.Y; y >= node_min.Y; y--) { - content_t c = vm->m_data[vi].getContent(); - - // Biome is recalculated each time an upper surface is detected while - // working down a column. The selected biome then remains in effect for - // all nodes below until the next surface and biome recalculation. - // Biome is recalculated: - // 1. At the surface of stone below air or water. - // 2. At the surface of water below air. - // 3. When stone or water is detected but biome has not yet been calculated. - if ((c == c_stone && (air_above || water_above || !biome)) || - (c == c_water_source && (air_above || !biome))) { - biome = biomegen->getBiomeAtIndex(index, y); - - depth_top = biome->depth_top; - base_filler = MYMAX(depth_top + biome->depth_filler - + noise_filler_depth->result[index], 0); - depth_water_top = biome->depth_water_top; - - // Detect stone type for dungeons during every biome calculation. - // This is more efficient than detecting per-node and will not - // miss any desert stone or sandstone biomes. - if (biome->c_stone == c_desert_stone) - stone_type = DESERT_STONE; - else if (biome->c_stone == c_sandstone) - stone_type = SANDSTONE; - } - - if (c == c_stone) { - content_t c_below = vm->m_data[vi - em.X].getContent(); - - // If the node below isn't solid, make this node stone, so that - // any top/filler nodes above are structurally supported. - // This is done by aborting the cycle of top/filler placement - // immediately by forcing nplaced to stone level. - if (c_below == CONTENT_AIR || c_below == c_water_source) - nplaced = U16_MAX; - - if (nplaced < depth_top) { - vm->m_data[vi] = MapNode(biome->c_top); - nplaced++; - } else if (nplaced < base_filler) { - vm->m_data[vi] = MapNode(biome->c_filler); - nplaced++; - } else { - vm->m_data[vi] = MapNode(biome->c_stone); - } - - air_above = false; - water_above = false; - } else if (c == c_water_source) { - vm->m_data[vi] = MapNode((y > (s32)(water_level - depth_water_top)) ? - biome->c_water_top : biome->c_water); - nplaced = 0; // Enable top/filler placement for next surface - air_above = false; - water_above = true; - } else if (c == CONTENT_AIR) { - nplaced = 0; // Enable top/filler placement for next surface - air_above = true; - water_above = false; - } else { // Possible various nodes overgenerated from neighbouring mapchunks - nplaced = U16_MAX; // Disable top/filler placement - air_above = false; - water_above = false; - } - - vm->m_area.add_y(em, vi, -1); - } - } - - return stone_type; -} - - -void MapgenFlat::dustTopNodes() -{ - if (node_max.Y < water_level) - return; - - v3s16 em = vm->m_area.getExtent(); - u32 index = 0; - - for (s16 z = node_min.Z; z <= node_max.Z; z++) - for (s16 x = node_min.X; x <= node_max.X; x++, index++) { - Biome *biome = (Biome *)bmgr->getRaw(biomemap[index]); - - if (biome->c_dust == CONTENT_IGNORE) - continue; - - u32 vi = vm->m_area.index(x, full_node_max.Y, z); - content_t c_full_max = vm->m_data[vi].getContent(); - s16 y_start; - - if (c_full_max == CONTENT_AIR) { - y_start = full_node_max.Y - 1; - } else if (c_full_max == CONTENT_IGNORE) { - vi = vm->m_area.index(x, node_max.Y + 1, z); - content_t c_max = vm->m_data[vi].getContent(); - - if (c_max == CONTENT_AIR) - y_start = node_max.Y; - else - continue; - } else { - continue; - } - - vi = vm->m_area.index(x, y_start, z); - for (s16 y = y_start; y >= node_min.Y - 1; y--) { - if (vm->m_data[vi].getContent() != CONTENT_AIR) - break; - - vm->m_area.add_y(em, vi, -1); - } - - content_t c = vm->m_data[vi].getContent(); - if (!ndef->get(c).buildable_to && c != CONTENT_IGNORE && c != biome->c_dust) { - vm->m_area.add_y(em, vi, 1); - vm->m_data[vi] = MapNode(biome->c_dust); - } - } -} - - -void MapgenFlat::generateCaves(s16 max_stone_y) -{ - if (max_stone_y < node_min.Y) - return; - - noise_cave1->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z); - noise_cave2->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z); - - v3s16 em = vm->m_area.getExtent(); - u32 index2d = 0; - - for (s16 z = node_min.Z; z <= node_max.Z; z++) - for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) { - bool column_is_open = false; // Is column open to overground - bool is_tunnel = false; // Is tunnel or tunnel floor - u32 vi = vm->m_area.index(x, node_max.Y, z); - u32 index3d = (z - node_min.Z) * zstride_1d + csize.Y * ystride + - (x - node_min.X); - // Biome of column - Biome *biome = (Biome *)bmgr->getRaw(biomemap[index2d]); - - // Don't excavate the overgenerated stone at node_max.Y + 1, - // this creates a 'roof' over the tunnel, preventing light in - // tunnels at mapchunk borders when generating mapchunks upwards. - // This 'roof' is removed when the mapchunk above is generated. - for (s16 y = node_max.Y; y >= node_min.Y - 1; y--, - index3d -= ystride, - vm->m_area.add_y(em, vi, -1)) { - - content_t c = vm->m_data[vi].getContent(); - if (c == CONTENT_AIR || c == biome->c_water_top || - c == biome->c_water) { - column_is_open = true; - continue; - } - // Ground - float d1 = contour(noise_cave1->result[index3d]); - float d2 = contour(noise_cave2->result[index3d]); - - if (d1 * d2 > cave_width && ndef->get(c).is_ground_content) { - // In tunnel and ground content, excavate - vm->m_data[vi] = MapNode(CONTENT_AIR); - is_tunnel = true; - } else { - // Not in tunnel or not ground content - if (is_tunnel && column_is_open && - (c == biome->c_filler || c == biome->c_stone)) - // Tunnel entrance floor - vm->m_data[vi] = MapNode(biome->c_top); - - column_is_open = false; - is_tunnel = false; - } - } - } - - if (node_max.Y > large_cave_depth) - return; - - PseudoRandom ps(blockseed + 21343); - u32 bruises_count = ps.range(0, 2); - for (u32 i = 0; i < bruises_count; i++) { - CaveV5 cave(this, &ps); - cave.makeCave(node_min, node_max, max_stone_y); - } -} diff --git a/src/mapgen_flat.h b/src/mapgen_flat.h index 53fd66a67..2b98c1f31 100644 --- a/src/mapgen_flat.h +++ b/src/mapgen_flat.h @@ -53,43 +53,19 @@ struct MapgenFlatParams : public MapgenSpecificParams { void writeParams(Settings *settings) const; }; -class MapgenFlat : public Mapgen { +class MapgenFlat : public MapgenBasic { public: - EmergeManager *m_emerge; - BiomeManager *bmgr; - - int ystride; - int zstride_1d; - - v3s16 node_min; - v3s16 node_max; - v3s16 full_node_min; - v3s16 full_node_max; - u32 spflags; s16 ground_level; s16 large_cave_depth; - float cave_width; float lake_threshold; float lake_steepness; float hill_threshold; float hill_steepness; Noise *noise_terrain; - Noise *noise_filler_depth; - Noise *noise_cave1; - Noise *noise_cave2; - Noise *noise_heat; - Noise *noise_humidity; - Noise *noise_heat_blend; - Noise *noise_humidity_blend; - - content_t c_stone; - content_t c_water_source; content_t c_lava_source; - content_t c_desert_stone; content_t c_ice; - content_t c_sandstone; content_t c_cobble; content_t c_stair_cobble; @@ -104,9 +80,6 @@ public: int getSpawnLevelAtPoint(v2s16 p); void calculateNoise(); s16 generateTerrain(); - MgStoneType generateBiomes(); - void dustTopNodes(); - void generateCaves(s16 max_stone_y); }; struct MapgenFactoryFlat : public MapgenFactory { diff --git a/src/mapgen_fractal.cpp b/src/mapgen_fractal.cpp index 9671a2143..81e162072 100644 --- a/src/mapgen_fractal.cpp +++ b/src/mapgen_fractal.cpp @@ -48,7 +48,7 @@ FlagDesc flagdesc_mapgen_fractal[] = { MapgenFractal::MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager *emerge) - : Mapgen(mapgenid, params, emerge) + : MapgenBasic(mapgenid, params, emerge) { this->m_emerge = emerge; this->bmgr = emerge->biomemgr; @@ -264,7 +264,7 @@ void MapgenFractal::makeChunk(BlockMakeData *data) MgStoneType stone_type = generateBiomes(); if (flags & MG_CAVES) - generateCaves(stone_surface_max_y); + generateCaves(stone_surface_max_y, MGFRACTAL_LARGE_CAVE_DEPTH); if ((flags & MG_DUNGEONS) && (stone_surface_max_y >= node_min.Y)) { DungeonParams dp; @@ -505,220 +505,3 @@ s16 MapgenFractal::generateTerrain() return stone_surface_max_y; } - - -MgStoneType MapgenFractal::generateBiomes() -{ - v3s16 em = vm->m_area.getExtent(); - u32 index = 0; - MgStoneType stone_type = STONE; - - for (s16 z = node_min.Z; z <= node_max.Z; z++) - for (s16 x = node_min.X; x <= node_max.X; x++, index++) { - Biome *biome = NULL; - u16 depth_top = 0; - u16 base_filler = 0; - u16 depth_water_top = 0; - u32 vi = vm->m_area.index(x, node_max.Y, z); - - // Check node at base of mapchunk above, either a node of a previously - // generated mapchunk or if not, a node of overgenerated base terrain. - content_t c_above = vm->m_data[vi + em.X].getContent(); - bool air_above = c_above == CONTENT_AIR; - bool water_above = c_above == c_water_source; - - // If there is air or water above enable top/filler placement, otherwise force - // nplaced to stone level by setting a number exceeding any possible filler depth. - u16 nplaced = (air_above || water_above) ? 0 : U16_MAX; - - - for (s16 y = node_max.Y; y >= node_min.Y; y--) { - content_t c = vm->m_data[vi].getContent(); - - // Biome is recalculated each time an upper surface is detected while - // working down a column. The selected biome then remains in effect for - // all nodes below until the next surface and biome recalculation. - // Biome is recalculated: - // 1. At the surface of stone below air or water. - // 2. At the surface of water below air. - // 3. When stone or water is detected but biome has not yet been calculated. - if ((c == c_stone && (air_above || water_above || !biome)) || - (c == c_water_source && (air_above || !biome))) { - biome = biomegen->getBiomeAtIndex(index, y); - - depth_top = biome->depth_top; - base_filler = MYMAX(depth_top + biome->depth_filler - + noise_filler_depth->result[index], 0); - depth_water_top = biome->depth_water_top; - - // Detect stone type for dungeons during every biome calculation. - // This is more efficient than detecting per-node and will not - // miss any desert stone or sandstone biomes. - if (biome->c_stone == c_desert_stone) - stone_type = DESERT_STONE; - else if (biome->c_stone == c_sandstone) - stone_type = SANDSTONE; - } - - if (c == c_stone) { - content_t c_below = vm->m_data[vi - em.X].getContent(); - - // If the node below isn't solid, make this node stone, so that - // any top/filler nodes above are structurally supported. - // This is done by aborting the cycle of top/filler placement - // immediately by forcing nplaced to stone level. - if (c_below == CONTENT_AIR || c_below == c_water_source) - nplaced = U16_MAX; - - if (nplaced < depth_top) { - vm->m_data[vi] = MapNode(biome->c_top); - nplaced++; - } else if (nplaced < base_filler) { - vm->m_data[vi] = MapNode(biome->c_filler); - nplaced++; - } else { - vm->m_data[vi] = MapNode(biome->c_stone); - } - - air_above = false; - water_above = false; - } else if (c == c_water_source) { - vm->m_data[vi] = MapNode((y > (s32)(water_level - depth_water_top)) ? - biome->c_water_top : biome->c_water); - nplaced = 0; // Enable top/filler placement for next surface - air_above = false; - water_above = true; - } else if (c == CONTENT_AIR) { - nplaced = 0; // Enable top/filler placement for next surface - air_above = true; - water_above = false; - } else { // Possible various nodes overgenerated from neighbouring mapchunks - nplaced = U16_MAX; // Disable top/filler placement - air_above = false; - water_above = false; - } - - vm->m_area.add_y(em, vi, -1); - } - } - - return stone_type; -} - - -void MapgenFractal::dustTopNodes() -{ - if (node_max.Y < water_level) - return; - - v3s16 em = vm->m_area.getExtent(); - u32 index = 0; - - for (s16 z = node_min.Z; z <= node_max.Z; z++) - for (s16 x = node_min.X; x <= node_max.X; x++, index++) { - Biome *biome = (Biome *)bmgr->getRaw(biomemap[index]); - - if (biome->c_dust == CONTENT_IGNORE) - continue; - - u32 vi = vm->m_area.index(x, full_node_max.Y, z); - content_t c_full_max = vm->m_data[vi].getContent(); - s16 y_start; - - if (c_full_max == CONTENT_AIR) { - y_start = full_node_max.Y - 1; - } else if (c_full_max == CONTENT_IGNORE) { - vi = vm->m_area.index(x, node_max.Y + 1, z); - content_t c_max = vm->m_data[vi].getContent(); - - if (c_max == CONTENT_AIR) - y_start = node_max.Y; - else - continue; - } else { - continue; - } - - vi = vm->m_area.index(x, y_start, z); - for (s16 y = y_start; y >= node_min.Y - 1; y--) { - if (vm->m_data[vi].getContent() != CONTENT_AIR) - break; - - vm->m_area.add_y(em, vi, -1); - } - - content_t c = vm->m_data[vi].getContent(); - if (!ndef->get(c).buildable_to && c != CONTENT_IGNORE && c != biome->c_dust) { - vm->m_area.add_y(em, vi, 1); - vm->m_data[vi] = MapNode(biome->c_dust); - } - } -} - - -void MapgenFractal::generateCaves(s16 max_stone_y) -{ - if (max_stone_y < node_min.Y) - return; - - noise_cave1->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z); - noise_cave2->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z); - - v3s16 em = vm->m_area.getExtent(); - u32 index2d = 0; - - for (s16 z = node_min.Z; z <= node_max.Z; z++) - for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) { - bool column_is_open = false; // Is column open to overground - bool is_tunnel = false; // Is tunnel or tunnel floor - u32 vi = vm->m_area.index(x, node_max.Y, z); - u32 index3d = (z - node_min.Z) * zstride_1d + csize.Y * ystride + - (x - node_min.X); - // Biome of column - Biome *biome = (Biome *)bmgr->getRaw(biomemap[index2d]); - - // Don't excavate the overgenerated stone at node_max.Y + 1, - // this creates a 'roof' over the tunnel, preventing light in - // tunnels at mapchunk borders when generating mapchunks upwards. - // This 'roof' is removed when the mapchunk above is generated. - for (s16 y = node_max.Y; y >= node_min.Y - 1; y--, - index3d -= ystride, - vm->m_area.add_y(em, vi, -1)) { - - content_t c = vm->m_data[vi].getContent(); - if (c == CONTENT_AIR || c == biome->c_water_top || - c == biome->c_water) { - column_is_open = true; - continue; - } - // Ground - float d1 = contour(noise_cave1->result[index3d]); - float d2 = contour(noise_cave2->result[index3d]); - - if (d1 * d2 > cave_width && ndef->get(c).is_ground_content) { - // In tunnel and ground content, excavate - vm->m_data[vi] = MapNode(CONTENT_AIR); - is_tunnel = true; - } else { - // Not in tunnel or not ground content - if (is_tunnel && column_is_open && - (c == biome->c_filler || c == biome->c_stone)) - // Tunnel entrance floor - vm->m_data[vi] = MapNode(biome->c_top); - - column_is_open = false; - is_tunnel = false; - } - } - } - - if (node_max.Y > MGFRACTAL_LARGE_CAVE_DEPTH) - return; - - PseudoRandom ps(blockseed + 21343); - u32 bruises_count = ps.range(0, 2); - for (u32 i = 0; i < bruises_count; i++) { - CaveV5 cave(this, &ps); - cave.makeCave(node_min, node_max, max_stone_y); - } -} diff --git a/src/mapgen_fractal.h b/src/mapgen_fractal.h index a0e51b2c6..176885d46 100644 --- a/src/mapgen_fractal.h +++ b/src/mapgen_fractal.h @@ -57,23 +57,12 @@ struct MapgenFractalParams : public MapgenSpecificParams { void writeParams(Settings *settings) const; }; -class MapgenFractal : public Mapgen { +class MapgenFractal : public MapgenBasic { public: - EmergeManager *m_emerge; - BiomeManager *bmgr; - - int ystride; - int zstride_1d; u16 formula; bool julia; - v3s16 node_min; - v3s16 node_max; - v3s16 full_node_min; - v3s16 full_node_max; - u32 spflags; - float cave_width; u16 fractal; u16 iterations; v3f scale; @@ -84,16 +73,9 @@ public: float julia_z; float julia_w; Noise *noise_seabed; - Noise *noise_filler_depth; - Noise *noise_cave1; - Noise *noise_cave2; - content_t c_stone; - content_t c_water_source; content_t c_lava_source; - content_t c_desert_stone; content_t c_ice; - content_t c_sandstone; content_t c_cobble; content_t c_stair_cobble; @@ -109,9 +91,6 @@ public: void calculateNoise(); bool getFractalAtPoint(s16 x, s16 y, s16 z); s16 generateTerrain(); - MgStoneType generateBiomes(); - void dustTopNodes(); - void generateCaves(s16 max_stone_y); }; struct MapgenFactoryFractal : public MapgenFactory { diff --git a/src/mapgen_v5.cpp b/src/mapgen_v5.cpp index 829a1f432..7698af825 100644 --- a/src/mapgen_v5.cpp +++ b/src/mapgen_v5.cpp @@ -46,7 +46,7 @@ FlagDesc flagdesc_mapgen_v5[] = { MapgenV5::MapgenV5(int mapgenid, MapgenParams *params, EmergeManager *emerge) - : Mapgen(mapgenid, params, emerge) + : MapgenBasic(mapgenid, params, emerge) { this->m_emerge = emerge; this->bmgr = emerge->biomemgr; @@ -245,7 +245,7 @@ void MapgenV5::makeChunk(BlockMakeData *data) // Generate caves if ((flags & MG_CAVES) && (stone_surface_max_y >= node_min.Y)) - generateCaves(stone_surface_max_y); + generateCaves(stone_surface_max_y, MGV5_LARGE_CAVE_DEPTH); // Generate dungeons and desert temples if ((flags & MG_DUNGEONS) && (stone_surface_max_y >= node_min.Y)) { @@ -391,220 +391,3 @@ int MapgenV5::generateBaseTerrain() return stone_surface_max_y; } - - -MgStoneType MapgenV5::generateBiomes() -{ - v3s16 em = vm->m_area.getExtent(); - u32 index = 0; - MgStoneType stone_type = STONE; - - for (s16 z = node_min.Z; z <= node_max.Z; z++) - for (s16 x = node_min.X; x <= node_max.X; x++, index++) { - Biome *biome = NULL; - u16 depth_top = 0; - u16 base_filler = 0; - u16 depth_water_top = 0; - u32 vi = vm->m_area.index(x, node_max.Y, z); - - // Check node at base of mapchunk above, either a node of a previously - // generated mapchunk or if not, a node of overgenerated base terrain. - content_t c_above = vm->m_data[vi + em.X].getContent(); - bool air_above = c_above == CONTENT_AIR; - bool water_above = c_above == c_water_source; - - // If there is air or water above enable top/filler placement, otherwise force - // nplaced to stone level by setting a number exceeding any possible filler depth. - u16 nplaced = (air_above || water_above) ? 0 : U16_MAX; - - for (s16 y = node_max.Y; y >= node_min.Y; y--) { - content_t c = vm->m_data[vi].getContent(); - - // Biome is recalculated each time an upper surface is detected while - // working down a column. The selected biome then remains in effect for - // all nodes below until the next surface and biome recalculation. - // Biome is recalculated: - // 1. At the surface of stone below air or water. - // 2. At the surface of water below air. - // 3. When stone or water is detected but biome has not yet been calculated. - if ((c == c_stone && (air_above || water_above || !biome)) || - (c == c_water_source && (air_above || !biome))) { - biome = biomegen->getBiomeAtIndex(index, y); - - depth_top = biome->depth_top; - base_filler = MYMAX(depth_top + biome->depth_filler - + noise_filler_depth->result[index], 0); - depth_water_top = biome->depth_water_top; - - // Detect stone type for dungeons during every biome calculation. - // This is more efficient than detecting per-node and will not - // miss any desert stone or sandstone biomes. - if (biome->c_stone == c_desert_stone) - stone_type = DESERT_STONE; - else if (biome->c_stone == c_sandstone) - stone_type = SANDSTONE; - } - - if (c == c_stone) { - content_t c_below = vm->m_data[vi - em.X].getContent(); - - // If the node below isn't solid, make this node stone, so that - // any top/filler nodes above are structurally supported. - // This is done by aborting the cycle of top/filler placement - // immediately by forcing nplaced to stone level. - if (c_below == CONTENT_AIR || c_below == c_water_source) - nplaced = U16_MAX; - - if (nplaced < depth_top) { - vm->m_data[vi] = MapNode(biome->c_top); - nplaced++; - } else if (nplaced < base_filler) { - vm->m_data[vi] = MapNode(biome->c_filler); - nplaced++; - } else { - vm->m_data[vi] = MapNode(biome->c_stone); - } - - air_above = false; - water_above = false; - } else if (c == c_water_source) { - vm->m_data[vi] = MapNode((y > (s32)(water_level - depth_water_top)) ? - biome->c_water_top : biome->c_water); - nplaced = 0; // Enable top/filler placement for next surface - air_above = false; - water_above = true; - } else if (c == CONTENT_AIR) { - nplaced = 0; // Enable top/filler placement for next surface - air_above = true; - water_above = false; - } else { // Possible various nodes overgenerated from neighbouring mapchunks - nplaced = U16_MAX; // Disable top/filler placement - air_above = false; - water_above = false; - } - - vm->m_area.add_y(em, vi, -1); - } - } - - return stone_type; -} - - -void MapgenV5::dustTopNodes() -{ - if (node_max.Y < water_level) - return; - - v3s16 em = vm->m_area.getExtent(); - u32 index = 0; - - for (s16 z = node_min.Z; z <= node_max.Z; z++) - for (s16 x = node_min.X; x <= node_max.X; x++, index++) { - Biome *biome = (Biome *)bmgr->getRaw(biomemap[index]); - - if (biome->c_dust == CONTENT_IGNORE) - continue; - - u32 vi = vm->m_area.index(x, full_node_max.Y, z); - content_t c_full_max = vm->m_data[vi].getContent(); - s16 y_start; - - if (c_full_max == CONTENT_AIR) { - y_start = full_node_max.Y - 1; - } else if (c_full_max == CONTENT_IGNORE) { - vi = vm->m_area.index(x, node_max.Y + 1, z); - content_t c_max = vm->m_data[vi].getContent(); - - if (c_max == CONTENT_AIR) - y_start = node_max.Y; - else - continue; - } else { - continue; - } - - vi = vm->m_area.index(x, y_start, z); - for (s16 y = y_start; y >= node_min.Y - 1; y--) { - if (vm->m_data[vi].getContent() != CONTENT_AIR) - break; - - vm->m_area.add_y(em, vi, -1); - } - - content_t c = vm->m_data[vi].getContent(); - if (!ndef->get(c).buildable_to && c != CONTENT_IGNORE && c != biome->c_dust) { - vm->m_area.add_y(em, vi, 1); - vm->m_data[vi] = MapNode(biome->c_dust); - } - } -} - - -void MapgenV5::generateCaves(int max_stone_y) -{ - if (max_stone_y < node_min.Y) - return; - - noise_cave1->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z); - noise_cave2->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z); - - v3s16 em = vm->m_area.getExtent(); - u32 index2d = 0; - - for (s16 z = node_min.Z; z <= node_max.Z; z++) - for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) { - bool column_is_open = false; // Is column open to overground - bool is_tunnel = false; // Is tunnel or tunnel floor - // Indexes at column top (node_max.Y) - u32 vi = vm->m_area.index(x, node_max.Y, z); - u32 index3d = (z - node_min.Z) * zstride_1d + csize.Y * ystride + - (x - node_min.X); - // Biome of column - Biome *biome = (Biome *)bmgr->getRaw(biomemap[index2d]); - - // Don't excavate the overgenerated stone at node_max.Y + 1, - // this creates a 'roof' over the tunnel, preventing light in - // tunnels at mapchunk borders when generating mapchunks upwards. - // This 'roof' is removed when the mapchunk above is generated. - for (s16 y = node_max.Y; y >= node_min.Y - 1; y--, - index3d -= ystride, - vm->m_area.add_y(em, vi, -1)) { - - content_t c = vm->m_data[vi].getContent(); - if (c == CONTENT_AIR || c == biome->c_water_top || - c == biome->c_water) { - column_is_open = true; - continue; - } - // Ground - float d1 = contour(noise_cave1->result[index3d]); - float d2 = contour(noise_cave2->result[index3d]); - - if (d1 * d2 > cave_width && ndef->get(c).is_ground_content) { - // In tunnel and ground content, excavate - vm->m_data[vi] = MapNode(CONTENT_AIR); - is_tunnel = true; - } else { - // Not in tunnel or not ground content - if (is_tunnel && column_is_open && - (c == biome->c_filler || c == biome->c_stone)) - // Tunnel entrance floor - vm->m_data[vi] = MapNode(biome->c_top); - - column_is_open = false; - is_tunnel = false; - } - } - } - - if (node_max.Y > MGV5_LARGE_CAVE_DEPTH) - return; - - PseudoRandom ps(blockseed + 21343); - u32 bruises_count = ps.range(0, 2); - for (u32 i = 0; i < bruises_count; i++) { - CaveV5 cave(this, &ps); - cave.makeCave(node_min, node_max, max_stone_y); - } -} diff --git a/src/mapgen_v5.h b/src/mapgen_v5.h index eef3da617..99836b23e 100644 --- a/src/mapgen_v5.h +++ b/src/mapgen_v5.h @@ -48,34 +48,15 @@ struct MapgenV5Params : public MapgenSpecificParams { }; -class MapgenV5 : public Mapgen { +class MapgenV5 : public MapgenBasic { public: - EmergeManager *m_emerge; - BiomeManager *bmgr; - - int ystride; - int zstride_1d; - - v3s16 node_min; - v3s16 node_max; - v3s16 full_node_min; - v3s16 full_node_max; - u32 spflags; - float cave_width; - Noise *noise_filler_depth; Noise *noise_factor; Noise *noise_height; - Noise *noise_cave1; - Noise *noise_cave2; Noise *noise_ground; - content_t c_stone; - content_t c_water_source; content_t c_lava_source; - content_t c_desert_stone; content_t c_ice; - content_t c_sandstone; content_t c_cobble; content_t c_stair_cobble; @@ -90,9 +71,6 @@ public: int getSpawnLevelAtPoint(v2s16 p); void calculateNoise(); int generateBaseTerrain(); - MgStoneType generateBiomes(); - void generateCaves(int max_stone_y); - void dustTopNodes(); }; diff --git a/src/mapgen_v7.cpp b/src/mapgen_v7.cpp index fddb34682..ee609bc95 100644 --- a/src/mapgen_v7.cpp +++ b/src/mapgen_v7.cpp @@ -51,7 +51,7 @@ FlagDesc flagdesc_mapgen_v7[] = { MapgenV7::MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge) - : Mapgen(mapgenid, params, emerge) + : MapgenBasic(mapgenid, params, emerge) { this->m_emerge = emerge; this->bmgr = emerge->biomemgr; @@ -276,7 +276,7 @@ void MapgenV7::makeChunk(BlockMakeData *data) MgStoneType stone_type = generateBiomes(); if (flags & MG_CAVES) - generateCaves(stone_surface_max_y); + generateCaves(stone_surface_max_y, water_level); if ((flags & MG_DUNGEONS) && (stone_surface_max_y >= node_min.Y)) { DungeonParams dp; @@ -517,225 +517,13 @@ void MapgenV7::generateRidgeTerrain() } -MgStoneType MapgenV7::generateBiomes() -{ - v3s16 em = vm->m_area.getExtent(); - u32 index = 0; - MgStoneType stone_type = STONE; - - for (s16 z = node_min.Z; z <= node_max.Z; z++) - for (s16 x = node_min.X; x <= node_max.X; x++, index++) { - Biome *biome = NULL; - u16 depth_top = 0; - u16 base_filler = 0; - u16 depth_water_top = 0; - u32 vi = vm->m_area.index(x, node_max.Y, z); - - // Check node at base of mapchunk above, either a node of a previously - // generated mapchunk or if not, a node of overgenerated base terrain. - content_t c_above = vm->m_data[vi + em.X].getContent(); - bool air_above = c_above == CONTENT_AIR; - bool water_above = c_above == c_water_source; - - // If there is air or water above enable top/filler placement, otherwise force - // nplaced to stone level by setting a number exceeding any possible filler depth. - u16 nplaced = (air_above || water_above) ? 0 : U16_MAX; - - for (s16 y = node_max.Y; y >= node_min.Y; y--) { - content_t c = vm->m_data[vi].getContent(); - - // Biome is recalculated each time an upper surface is detected while - // working down a column. The selected biome then remains in effect for - // all nodes below until the next surface and biome recalculation. - // Biome is recalculated: - // 1. At the surface of stone below air or water. - // 2. At the surface of water below air. - // 3. When stone or water is detected but biome has not yet been calculated. - if ((c == c_stone && (air_above || water_above || !biome)) || - (c == c_water_source && (air_above || !biome))) { - biome = biomegen->getBiomeAtIndex(index, y); - - depth_top = biome->depth_top; - base_filler = MYMAX(depth_top + biome->depth_filler - + noise_filler_depth->result[index], 0); - depth_water_top = biome->depth_water_top; - - // Detect stone type for dungeons during every biome calculation. - // This is more efficient than detecting per-node and will not - // miss any desert stone or sandstone biomes. - if (biome->c_stone == c_desert_stone) - stone_type = DESERT_STONE; - else if (biome->c_stone == c_sandstone) - stone_type = SANDSTONE; - } - - if (c == c_stone) { - content_t c_below = vm->m_data[vi - em.X].getContent(); - - // If the node below isn't solid, make this node stone, so that - // any top/filler nodes above are structurally supported. - // This is done by aborting the cycle of top/filler placement - // immediately by forcing nplaced to stone level. - if (c_below == CONTENT_AIR || c_below == c_water_source) - nplaced = U16_MAX; - - if (nplaced < depth_top) { - vm->m_data[vi] = MapNode(biome->c_top); - nplaced++; - } else if (nplaced < base_filler) { - vm->m_data[vi] = MapNode(biome->c_filler); - nplaced++; - } else { - vm->m_data[vi] = MapNode(biome->c_stone); - } - - air_above = false; - water_above = false; - } else if (c == c_water_source) { - vm->m_data[vi] = MapNode((y > (s32)(water_level - depth_water_top)) ? - biome->c_water_top : biome->c_water); - nplaced = 0; // Enable top/filler placement for next surface - air_above = false; - water_above = true; - } else if (c == CONTENT_AIR) { - nplaced = 0; // Enable top/filler placement for next surface - air_above = true; - water_above = false; - } else { // Possible various nodes overgenerated from neighbouring mapchunks - nplaced = U16_MAX; // Disable top/filler placement - air_above = false; - water_above = false; - } - - vm->m_area.add_y(em, vi, -1); - } - } - - return stone_type; -} - - -void MapgenV7::dustTopNodes() -{ - if (node_max.Y < water_level) - return; - - v3s16 em = vm->m_area.getExtent(); - u32 index = 0; - - for (s16 z = node_min.Z; z <= node_max.Z; z++) - for (s16 x = node_min.X; x <= node_max.X; x++, index++) { - Biome *biome = (Biome *)bmgr->getRaw(biomemap[index]); - - if (biome->c_dust == CONTENT_IGNORE) - continue; - - u32 vi = vm->m_area.index(x, full_node_max.Y, z); - content_t c_full_max = vm->m_data[vi].getContent(); - s16 y_start; - - if (c_full_max == CONTENT_AIR) { - y_start = full_node_max.Y - 1; - } else if (c_full_max == CONTENT_IGNORE) { - vi = vm->m_area.index(x, node_max.Y + 1, z); - content_t c_max = vm->m_data[vi].getContent(); - - if (c_max == CONTENT_AIR) - y_start = node_max.Y; - else - continue; - } else { - continue; - } - - vi = vm->m_area.index(x, y_start, z); - for (s16 y = y_start; y >= node_min.Y - 1; y--) { - if (vm->m_data[vi].getContent() != CONTENT_AIR) - break; - - vm->m_area.add_y(em, vi, -1); - } - - content_t c = vm->m_data[vi].getContent(); - if (!ndef->get(c).buildable_to && c != CONTENT_IGNORE && c != biome->c_dust) { - vm->m_area.add_y(em, vi, 1); - vm->m_data[vi] = MapNode(biome->c_dust); - } - } -} - - -void MapgenV7::generateCaves(s16 max_stone_y) -{ - if (max_stone_y < node_min.Y) - return; - - noise_cave1->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z); - noise_cave2->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z); - - v3s16 em = vm->m_area.getExtent(); - u32 index2d = 0; - - for (s16 z = node_min.Z; z <= node_max.Z; z++) - for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) { - bool column_is_open = false; // Is column open to overground - bool is_tunnel = false; // Is tunnel or tunnel floor - // Indexes at column top (node_max.Y) - u32 vi = vm->m_area.index(x, node_max.Y, z); - u32 index3d = (z - node_min.Z) * zstride_1d + csize.Y * ystride + - (x - node_min.X); - // Biome of column - Biome *biome = (Biome *)bmgr->getRaw(biomemap[index2d]); - - // Don't excavate the overgenerated stone at node_max.Y + 1, - // this creates a 'roof' over the tunnel, preventing light in - // tunnels at mapchunk borders when generating mapchunks upwards. - // This 'roof' is removed when the mapchunk above is generated. - for (s16 y = node_max.Y; y >= node_min.Y - 1; y--, - index3d -= ystride, - vm->m_area.add_y(em, vi, -1)) { - - content_t c = vm->m_data[vi].getContent(); - if (c == CONTENT_AIR || c == biome->c_water_top || - c == biome->c_water) { - column_is_open = true; - continue; - } - // Ground - float d1 = contour(noise_cave1->result[index3d]); - float d2 = contour(noise_cave2->result[index3d]); - - if (d1 * d2 > cave_width && ndef->get(c).is_ground_content) { - // In tunnel and ground content, excavate - vm->m_data[vi] = MapNode(CONTENT_AIR); - is_tunnel = true; - } else { - // Not in tunnel or not ground content - if (is_tunnel && column_is_open && - (c == biome->c_filler || c == biome->c_stone)) - // Tunnel entrance floor - vm->m_data[vi] = MapNode(biome->c_top); - - column_is_open = false; - is_tunnel = false; - } - } - } - - if (node_min.Y >= water_level) - return; - - PseudoRandom ps(blockseed + 21343); - u32 bruises_count = ps.range(0, 2); - for (u32 i = 0; i < bruises_count; i++) { - CaveV7 cave(this, &ps); - cave.makeCave(node_min, node_max, max_stone_y); - } -} - - -/////////////////////////////////////////////////////////////// - +//////////////////////////////////////////////////////////////////////////////// +//// Code Boneyard +//// +//// Much of the stuff here has potential to become useful again at some point +//// in the future, but we don't want it to get lost or forgotten in version +//// control. +//// #if 0 int MapgenV7::generateMountainTerrain(s16 ymax) diff --git a/src/mapgen_v7.h b/src/mapgen_v7.h index a8dd9986d..21beab2b4 100644 --- a/src/mapgen_v7.h +++ b/src/mapgen_v7.h @@ -54,42 +54,24 @@ struct MapgenV7Params : public MapgenSpecificParams { void writeParams(Settings *settings) const; }; -class MapgenV7 : public Mapgen { +class MapgenV7 : public MapgenBasic { public: - EmergeManager *m_emerge; - BiomeManager *bmgr; - - int ystride; int zstride_1u1d; - int zstride_1d; - - v3s16 node_min; - v3s16 node_max; - v3s16 full_node_min; - v3s16 full_node_max; s16 *ridge_heightmap; u32 spflags; - float cave_width; Noise *noise_terrain_base; Noise *noise_terrain_alt; Noise *noise_terrain_persist; Noise *noise_height_select; - Noise *noise_filler_depth; Noise *noise_mount_height; Noise *noise_ridge_uwater; Noise *noise_mountain; Noise *noise_ridge; - Noise *noise_cave1; - Noise *noise_cave2; - content_t c_stone; - content_t c_water_source; content_t c_lava_source; - content_t c_desert_stone; content_t c_ice; - content_t c_sandstone; content_t c_cobble; content_t c_stair_cobble; @@ -112,11 +94,6 @@ public: int generateTerrain(); void generateRidgeTerrain(); - - MgStoneType generateBiomes(); - void dustTopNodes(); - - void generateCaves(s16 max_stone_y); }; struct MapgenFactoryV7 : public MapgenFactory { diff --git a/src/mapgen_valleys.cpp b/src/mapgen_valleys.cpp index 44083d9d3..fa790677a 100644 --- a/src/mapgen_valleys.cpp +++ b/src/mapgen_valleys.cpp @@ -65,7 +65,7 @@ static FlagDesc flagdesc_mapgen_valleys[] = { MapgenValleys::MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager *emerge) - : Mapgen(mapgenid, params, emerge) + : MapgenBasic(mapgenid, params, emerge) { this->m_emerge = emerge; this->bmgr = emerge->biomemgr; @@ -302,7 +302,7 @@ void MapgenValleys::makeChunk(BlockMakeData *data) // Cave creation. if (flags & MG_CAVES) - generateCaves(stone_surface_max_y); + generateCaves(stone_surface_max_y, large_cave_depth); // Dungeon creation if ((flags & MG_DUNGEONS) && node_max.Y < 50 && (stone_surface_max_y >= node_min.Y)) { @@ -692,166 +692,7 @@ int MapgenValleys::generateTerrain() return surface_max_y; } - -MgStoneType MapgenValleys::generateBiomes() -{ - v3s16 em = vm->m_area.getExtent(); - u32 index = 0; - MgStoneType stone_type = STONE; - - for (s16 z = node_min.Z; z <= node_max.Z; z++) - for (s16 x = node_min.X; x <= node_max.X; x++, index++) { - Biome *biome = NULL; - u16 depth_top = 0; - u16 base_filler = 0; - u16 depth_water_top = 0; - u32 vi = vm->m_area.index(x, node_max.Y, z); - - // Check node at base of mapchunk above, either a node of a previously - // generated mapchunk or if not, a node of overgenerated base terrain. - content_t c_above = vm->m_data[vi + em.X].getContent(); - bool air_above = c_above == CONTENT_AIR; - bool water_above = (c_above == c_water_source || c_above == c_river_water_source); - - // If there is air or water above enable top/filler placement, otherwise force - // nplaced to stone level by setting a number exceeding any possible filler depth. - u16 nplaced = (air_above || water_above) ? 0 : U16_MAX; - - for (s16 y = node_max.Y; y >= node_min.Y; y--) { - content_t c = vm->m_data[vi].getContent(); - - // Biome is recalculated each time an upper surface is detected while - // working down a column. The selected biome then remains in effect for - // all nodes below until the next surface and biome recalculation. - // Biome is recalculated: - // 1. At the surface of stone below air or water. - // 2. At the surface of water below air. - // 3. When stone or water is detected but biome has not yet been calculated. - if ((c == c_stone && (air_above || water_above || !biome)) - || ((c == c_water_source || c == c_river_water_source) - && (air_above || !biome))) { - // Both heat and humidity have already been adjusted for altitude. - biome = biomegen->getBiomeAtIndex(index, y); - - depth_top = biome->depth_top; - base_filler = MYMAX(depth_top - + biome->depth_filler - + noise_filler_depth->result[index], 0.f); - depth_water_top = biome->depth_water_top; - - // Detect stone type for dungeons during every biome calculation. - // This is more efficient than detecting per-node and will not - // miss any desert stone or sandstone biomes. - if (biome->c_stone == c_desert_stone) - stone_type = DESERT_STONE; - else if (biome->c_stone == c_sandstone) - stone_type = SANDSTONE; - } - - if (c == c_stone) { - content_t c_below = vm->m_data[vi - em.X].getContent(); - - // If the node below isn't solid, make this node stone, so that - // any top/filler nodes above are structurally supported. - // This is done by aborting the cycle of top/filler placement - // immediately by forcing nplaced to stone level. - if (c_below == CONTENT_AIR - || c_below == c_water_source - || c_below == c_river_water_source) - nplaced = U16_MAX; - - if (nplaced < depth_top) { - vm->m_data[vi] = MapNode(biome->c_top); - nplaced++; - } else if (nplaced < base_filler) { - vm->m_data[vi] = MapNode(biome->c_filler); - nplaced++; - } else { - vm->m_data[vi] = MapNode(biome->c_stone); - } - - air_above = false; - water_above = false; - } else if (c == c_water_source) { - vm->m_data[vi] = MapNode((y > (s32)(water_level - depth_water_top)) - ? biome->c_water_top : biome->c_water); - nplaced = 0; // Enable top/filler placement for next surface - air_above = false; - water_above = true; - } else if (c == c_river_water_source) { - vm->m_data[vi] = MapNode(biome->c_river_water); - nplaced = depth_top; // Enable filler placement for next surface - air_above = false; - water_above = true; - } else if (c == CONTENT_AIR) { - nplaced = 0; // Enable top/filler placement for next surface - air_above = true; - water_above = false; - } else { // Possible various nodes overgenerated from neighbouring mapchunks - nplaced = U16_MAX; // Disable top/filler placement - air_above = false; - water_above = false; - } - - vm->m_area.add_y(em, vi, -1); - } - } - - return stone_type; -} - - -void MapgenValleys::dustTopNodes() -{ - if (node_max.Y < water_level) - return; - - v3s16 em = vm->m_area.getExtent(); - u32 index = 0; - - for (s16 z = node_min.Z; z <= node_max.Z; z++) - for (s16 x = node_min.X; x <= node_max.X; x++, index++) { - Biome *biome = (Biome *)bmgr->getRaw(biomemap[index]); - - if (biome->c_dust == CONTENT_IGNORE) - continue; - - u32 vi = vm->m_area.index(x, full_node_max.Y, z); - content_t c_full_max = vm->m_data[vi].getContent(); - s16 y_start; - - if (c_full_max == CONTENT_AIR) { - y_start = full_node_max.Y - 1; - } else if (c_full_max == CONTENT_IGNORE) { - vi = vm->m_area.index(x, node_max.Y + 1, z); - content_t c_max = vm->m_data[vi].getContent(); - - if (c_max == CONTENT_AIR) - y_start = node_max.Y; - else - continue; - } else { - continue; - } - - vi = vm->m_area.index(x, y_start, z); - for (s16 y = y_start; y >= node_min.Y - 1; y--) { - if (vm->m_data[vi].getContent() != CONTENT_AIR) - break; - - vm->m_area.add_y(em, vi, -1); - } - - content_t c = vm->m_data[vi].getContent(); - if (!ndef->get(c).buildable_to && c != CONTENT_IGNORE && c != biome->c_dust) { - vm->m_area.add_y(em, vi, 1); - vm->m_data[vi] = MapNode(biome->c_dust); - } - } -} - - -void MapgenValleys::generateCaves(s16 max_stone_y) +void MapgenValleys::generateCaves(s16 max_stone_y, s16 large_cave_depth) { if (max_stone_y < node_min.Y) return; diff --git a/src/mapgen_valleys.h b/src/mapgen_valleys.h index 0e268c1cf..2ab253430 100644 --- a/src/mapgen_valleys.h +++ b/src/mapgen_valleys.h @@ -85,7 +85,7 @@ struct TerrainNoise { float inter_valley_fill; }; -class MapgenValleys : public Mapgen { +class MapgenValleys : public MapgenBasic { public: MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager *emerge); @@ -97,13 +97,9 @@ public: s16 large_cave_depth; private: - EmergeManager *m_emerge; - BiomeManager *bmgr; BiomeGenOriginal *m_bgen; - int ystride; int zstride; - int zstride_1d; float map_gen_limit; @@ -113,11 +109,6 @@ private: s16 cave_water_max_height; s16 lava_max_height; - v3s16 node_min; - v3s16 node_max; - v3s16 full_node_min; - v3s16 full_node_max; - u32 spflags; float altitude_chill; s16 lava_features_lim; @@ -126,10 +117,6 @@ private: float river_size_factor; float *tcave_cache; s16 water_features_lim; - float cave_width; - Noise *noise_filler_depth; - Noise *noise_cave1; - Noise *noise_cave2; Noise *noise_inter_valley_fill; Noise *noise_inter_valley_slope; Noise *noise_rivers; @@ -139,19 +126,13 @@ private: Noise *noise_valley_profile; content_t c_cobble; - content_t c_desert_stone; content_t c_dirt; - content_t c_ice; content_t c_lava_source; content_t c_mossycobble; - content_t c_river_water_source; content_t c_sand; - content_t c_sandstone; content_t c_sandstonebrick; content_t c_stair_cobble; content_t c_stair_sandstonebrick; - content_t c_stone; - content_t c_water_source; float terrainLevelAtPoint(s16 x, s16 z); @@ -161,10 +142,7 @@ private: float terrainLevelFromNoise(TerrainNoise *tn); float adjustedTerrainLevelFromNoise(TerrainNoise *tn); - MgStoneType generateBiomes(); - void dustTopNodes(); - - void generateCaves(s16 max_stone_y); + virtual void generateCaves(s16 max_stone_y, s16 large_cave_depth); }; struct MapgenFactoryValleys : public MapgenFactory { From 9b5c492be57945c2df63e84ce8dbf057f45b2754 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Mon, 2 May 2016 02:45:59 -0400 Subject: [PATCH 020/418] Fix MgStoneType and BiomeType enum names --- src/mapgen.cpp | 6 +++--- src/mapgen.h | 6 +++--- src/mapgen_flat.cpp | 6 +++--- src/mapgen_fractal.cpp | 6 +++--- src/mapgen_v5.cpp | 6 +++--- src/mapgen_v7.cpp | 6 +++--- src/mapgen_valleys.cpp | 6 +++--- src/mg_biome.h | 14 +++++++------- src/script/lua_api/l_mapgen.cpp | 12 ++++++------ 9 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/mapgen.cpp b/src/mapgen.cpp index f491ed9eb..01593718b 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -384,7 +384,7 @@ MgStoneType MapgenBasic::generateBiomes() { v3s16 em = vm->m_area.getExtent(); u32 index = 0; - MgStoneType stone_type = STONE; + MgStoneType stone_type = MGSTONE_STONE; for (s16 z = node_min.Z; z <= node_max.Z; z++) for (s16 x = node_min.X; x <= node_max.X; x++, index++) { @@ -429,9 +429,9 @@ MgStoneType MapgenBasic::generateBiomes() // This is more efficient than detecting per-node and will not // miss any desert stone or sandstone biomes. if (biome->c_stone == c_desert_stone) - stone_type = DESERT_STONE; + stone_type = MGSTONE_DESERT_STONE; else if (biome->c_stone == c_sandstone) - stone_type = SANDSTONE; + stone_type = MGSTONE_SANDSTONE; } if (c == c_stone) { diff --git a/src/mapgen.h b/src/mapgen.h index bee8e786a..3ec148296 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -76,9 +76,9 @@ enum GenNotifyType { // TODO(hmmmm/paramat): make stone type selection dynamic enum MgStoneType { - STONE, - DESERT_STONE, - SANDSTONE, + MGSTONE_STONE, + MGSTONE_DESERT_STONE, + MGSTONE_SANDSTONE, }; struct GenNotifyEvent { diff --git a/src/mapgen_flat.cpp b/src/mapgen_flat.cpp index 97f1c4b2a..2cbf9ace0 100644 --- a/src/mapgen_flat.cpp +++ b/src/mapgen_flat.cpp @@ -257,7 +257,7 @@ void MapgenFlat::makeChunk(BlockMakeData *data) dp.np_density = nparams_dungeon_density; dp.np_wetness = nparams_dungeon_wetness; dp.c_water = c_water_source; - if (stone_type == STONE) { + if (stone_type == MGSTONE_STONE) { dp.c_cobble = c_cobble; dp.c_moss = c_mossycobble; dp.c_stair = c_stair_cobble; @@ -267,7 +267,7 @@ void MapgenFlat::makeChunk(BlockMakeData *data) dp.holesize = v3s16(1, 2, 1); dp.roomsize = v3s16(0, 0, 0); dp.notifytype = GENNOTIFY_DUNGEON; - } else if (stone_type == DESERT_STONE) { + } else if (stone_type == MGSTONE_DESERT_STONE) { dp.c_cobble = c_desert_stone; dp.c_moss = c_desert_stone; dp.c_stair = c_desert_stone; @@ -277,7 +277,7 @@ void MapgenFlat::makeChunk(BlockMakeData *data) dp.holesize = v3s16(2, 3, 2); dp.roomsize = v3s16(2, 5, 2); dp.notifytype = GENNOTIFY_TEMPLE; - } else if (stone_type == SANDSTONE) { + } else if (stone_type == MGSTONE_SANDSTONE) { dp.c_cobble = c_sandstonebrick; dp.c_moss = c_sandstonebrick; dp.c_stair = c_sandstonebrick; diff --git a/src/mapgen_fractal.cpp b/src/mapgen_fractal.cpp index 81e162072..3217d52e2 100644 --- a/src/mapgen_fractal.cpp +++ b/src/mapgen_fractal.cpp @@ -273,7 +273,7 @@ void MapgenFractal::makeChunk(BlockMakeData *data) dp.np_density = nparams_dungeon_density; dp.np_wetness = nparams_dungeon_wetness; dp.c_water = c_water_source; - if (stone_type == STONE) { + if (stone_type == MGSTONE_STONE) { dp.c_cobble = c_cobble; dp.c_moss = c_mossycobble; dp.c_stair = c_stair_cobble; @@ -283,7 +283,7 @@ void MapgenFractal::makeChunk(BlockMakeData *data) dp.holesize = v3s16(1, 2, 1); dp.roomsize = v3s16(0, 0, 0); dp.notifytype = GENNOTIFY_DUNGEON; - } else if (stone_type == DESERT_STONE) { + } else if (stone_type == MGSTONE_DESERT_STONE) { dp.c_cobble = c_desert_stone; dp.c_moss = c_desert_stone; dp.c_stair = c_desert_stone; @@ -293,7 +293,7 @@ void MapgenFractal::makeChunk(BlockMakeData *data) dp.holesize = v3s16(2, 3, 2); dp.roomsize = v3s16(2, 5, 2); dp.notifytype = GENNOTIFY_TEMPLE; - } else if (stone_type == SANDSTONE) { + } else if (stone_type == MGSTONE_SANDSTONE) { dp.c_cobble = c_sandstonebrick; dp.c_moss = c_sandstonebrick; dp.c_stair = c_sandstonebrick; diff --git a/src/mapgen_v5.cpp b/src/mapgen_v5.cpp index 7698af825..6c1441bc2 100644 --- a/src/mapgen_v5.cpp +++ b/src/mapgen_v5.cpp @@ -255,7 +255,7 @@ void MapgenV5::makeChunk(BlockMakeData *data) dp.np_density = nparams_dungeon_density; dp.np_wetness = nparams_dungeon_wetness; dp.c_water = c_water_source; - if (stone_type == STONE) { + if (stone_type == MGSTONE_STONE) { dp.c_cobble = c_cobble; dp.c_moss = c_mossycobble; dp.c_stair = c_stair_cobble; @@ -265,7 +265,7 @@ void MapgenV5::makeChunk(BlockMakeData *data) dp.holesize = v3s16(1, 2, 1); dp.roomsize = v3s16(0, 0, 0); dp.notifytype = GENNOTIFY_DUNGEON; - } else if (stone_type == DESERT_STONE) { + } else if (stone_type == MGSTONE_DESERT_STONE) { dp.c_cobble = c_desert_stone; dp.c_moss = c_desert_stone; dp.c_stair = c_desert_stone; @@ -275,7 +275,7 @@ void MapgenV5::makeChunk(BlockMakeData *data) dp.holesize = v3s16(2, 3, 2); dp.roomsize = v3s16(2, 5, 2); dp.notifytype = GENNOTIFY_TEMPLE; - } else if (stone_type == SANDSTONE) { + } else if (stone_type == MGSTONE_SANDSTONE) { dp.c_cobble = c_sandstonebrick; dp.c_moss = c_sandstonebrick; dp.c_stair = c_sandstonebrick; diff --git a/src/mapgen_v7.cpp b/src/mapgen_v7.cpp index ee609bc95..e6801a24b 100644 --- a/src/mapgen_v7.cpp +++ b/src/mapgen_v7.cpp @@ -285,7 +285,7 @@ void MapgenV7::makeChunk(BlockMakeData *data) dp.np_density = nparams_dungeon_density; dp.np_wetness = nparams_dungeon_wetness; dp.c_water = c_water_source; - if (stone_type == STONE) { + if (stone_type == MGSTONE_STONE) { dp.c_cobble = c_cobble; dp.c_moss = c_mossycobble; dp.c_stair = c_stair_cobble; @@ -295,7 +295,7 @@ void MapgenV7::makeChunk(BlockMakeData *data) dp.holesize = v3s16(1, 2, 1); dp.roomsize = v3s16(0, 0, 0); dp.notifytype = GENNOTIFY_DUNGEON; - } else if (stone_type == DESERT_STONE) { + } else if (stone_type == MGSTONE_DESERT_STONE) { dp.c_cobble = c_desert_stone; dp.c_moss = c_desert_stone; dp.c_stair = c_desert_stone; @@ -305,7 +305,7 @@ void MapgenV7::makeChunk(BlockMakeData *data) dp.holesize = v3s16(2, 3, 2); dp.roomsize = v3s16(2, 5, 2); dp.notifytype = GENNOTIFY_TEMPLE; - } else if (stone_type == SANDSTONE) { + } else if (stone_type == MGSTONE_SANDSTONE) { dp.c_cobble = c_sandstonebrick; dp.c_moss = c_sandstonebrick; dp.c_stair = c_sandstonebrick; diff --git a/src/mapgen_valleys.cpp b/src/mapgen_valleys.cpp index fa790677a..d424ca068 100644 --- a/src/mapgen_valleys.cpp +++ b/src/mapgen_valleys.cpp @@ -312,7 +312,7 @@ void MapgenValleys::makeChunk(BlockMakeData *data) dp.np_density = nparams_dungeon_density; dp.np_wetness = nparams_dungeon_wetness; dp.c_water = c_water_source; - if (stone_type == STONE) { + if (stone_type == MGSTONE_STONE) { dp.c_cobble = c_cobble; dp.c_moss = c_mossycobble; dp.c_stair = c_stair_cobble; @@ -322,7 +322,7 @@ void MapgenValleys::makeChunk(BlockMakeData *data) dp.holesize = v3s16(1, 2, 1); dp.roomsize = v3s16(0, 0, 0); dp.notifytype = GENNOTIFY_DUNGEON; - } else if (stone_type == DESERT_STONE) { + } else if (stone_type == MGSTONE_DESERT_STONE) { dp.c_cobble = c_desert_stone; dp.c_moss = c_desert_stone; dp.c_stair = c_desert_stone; @@ -332,7 +332,7 @@ void MapgenValleys::makeChunk(BlockMakeData *data) dp.holesize = v3s16(2, 3, 2); dp.roomsize = v3s16(2, 5, 2); dp.notifytype = GENNOTIFY_TEMPLE; - } else if (stone_type == SANDSTONE) { + } else if (stone_type == MGSTONE_SANDSTONE) { dp.c_cobble = c_sandstonebrick; dp.c_moss = c_sandstonebrick; dp.c_stair = c_sandstonebrick; diff --git a/src/mg_biome.h b/src/mg_biome.h index e9378fd79..e78e90e5f 100644 --- a/src/mg_biome.h +++ b/src/mg_biome.h @@ -33,13 +33,13 @@ class BiomeManager; #define BIOME_NONE ((u8)0) -enum BiomeType -{ - BIOME_NORMAL, - BIOME_LIQUID, - BIOME_NETHER, - BIOME_AETHER, - BIOME_FLAT +// TODO(hmmmm): Decide whether this is obsolete or will be used in the future +enum BiomeType { + BIOMETYPE_NORMAL, + BIOMETYPE_LIQUID, + BIOMETYPE_NETHER, + BIOMETYPE_AETHER, + BIOMETYPE_FLAT, }; class Biome : public ObjDef, public NodeResolver { diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 405b93b86..6baf217af 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -39,11 +39,11 @@ with this program; if not, write to the Free Software Foundation, Inc., struct EnumString ModApiMapgen::es_BiomeTerrainType[] = { - {BIOME_NORMAL, "normal"}, - {BIOME_LIQUID, "liquid"}, - {BIOME_NETHER, "nether"}, - {BIOME_AETHER, "aether"}, - {BIOME_FLAT, "flat"}, + {BIOMETYPE_NORMAL, "normal"}, + {BIOMETYPE_LIQUID, "liquid"}, + {BIOMETYPE_NETHER, "nether"}, + {BIOMETYPE_AETHER, "aether"}, + {BIOMETYPE_FLAT, "flat"}, {0, NULL}, }; @@ -371,7 +371,7 @@ Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef) return NULL; BiomeType biometype = (BiomeType)getenumfield(L, index, "type", - ModApiMapgen::es_BiomeTerrainType, BIOME_NORMAL); + ModApiMapgen::es_BiomeTerrainType, BIOMETYPE_NORMAL); Biome *b = BiomeManager::create(biometype); b->name = getstringfield_default(L, index, "name", ""); From 1bb5eb1da2c2e34c4413c01b45c63f6a67b214a2 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Tue, 10 May 2016 00:01:37 -0400 Subject: [PATCH 021/418] Cavegen: Merge CaveV5 and CaveV7 into CavesRandomWalk --- src/cavegen.cpp | 270 ++--------------------------------------- src/cavegen.h | 57 +-------- src/mapgen.cpp | 2 +- src/mapgen_valleys.cpp | 2 +- 4 files changed, 20 insertions(+), 311 deletions(-) diff --git a/src/cavegen.cpp b/src/cavegen.cpp index b8abfbca5..59274101e 100644 --- a/src/cavegen.cpp +++ b/src/cavegen.cpp @@ -31,7 +31,7 @@ NoiseParams nparams_caveliquids(0, 1, v3f(150.0, 150.0, 150.0), 776, 3, 0.6, 2.0 ///////////////////////////////////////// Caves V5 -CaveV5::CaveV5(Mapgen *mg, PseudoRandom *ps) +CavesRandomWalk::CavesRandomWalk(Mapgen *mg, PseudoRandom *ps) { this->mg = mg; this->vm = mg->vm; @@ -43,7 +43,8 @@ CaveV5::CaveV5(Mapgen *mg, PseudoRandom *ps) c_ice = ndef->getId("mapgen_ice"); this->np_caveliquids = &nparams_caveliquids; this->ystride = mg->csize.X; - + this->lava_depth = DEFAULT_LAVA_DEPTH; + if (c_ice == CONTENT_IGNORE) c_ice = CONTENT_AIR; @@ -59,7 +60,7 @@ CaveV5::CaveV5(Mapgen *mg, PseudoRandom *ps) } -void CaveV5::makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height) +void CavesRandomWalk::makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height) { node_min = nmin; node_max = nmax; @@ -84,12 +85,12 @@ void CaveV5::makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height) // Limit maximum to area route_y_max = rangelim(route_y_max, 0, ar.Y - 1); - s16 min = 0; - if (node_min.Y < water_level && node_max.Y > water_level) { - min = water_level - max_tunnel_diameter/3 - of.Y; - route_y_max = water_level + max_tunnel_diameter/3 - of.Y; - } - route_y_min = ps->range(min, min + max_tunnel_diameter); + s16 minpos = 0; + if (node_min.Y < water_level && node_max.Y > water_level) { + minpos = water_level - max_tunnel_diameter / 3 - of.Y; + route_y_max = water_level + max_tunnel_diameter / 3 - of.Y; + } + route_y_min = ps->range(minpos, minpos + max_tunnel_diameter); route_y_min = rangelim(route_y_min, 0, route_y_max); s16 route_start_y_min = route_y_min; @@ -121,7 +122,7 @@ void CaveV5::makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height) } -void CaveV5::makeTunnel(bool dirswitch) +void CavesRandomWalk::makeTunnel(bool dirswitch) { // Randomize size s16 min_d = min_tunnel_diameter; @@ -207,7 +208,7 @@ void CaveV5::makeTunnel(bool dirswitch) } -void CaveV5::carveRoute(v3f vec, float f, bool randomize_xz) +void CavesRandomWalk::carveRoute(v3f vec, float f, bool randomize_xz) { MapNode airnode(CONTENT_AIR); MapNode waternode(c_water_source); @@ -218,7 +219,7 @@ void CaveV5::carveRoute(v3f vec, float f, bool randomize_xz) float nval = NoisePerlin3D(np_caveliquids, startp.X, startp.Y, startp.Z, mg->seed); - MapNode liquidnode = (nval < 0.40 && node_max.Y < MGV5_LAVA_DEPTH) ? + MapNode liquidnode = (nval < 0.40 && node_max.Y < lava_depth) ? lavanode : waternode; v3f fp = orp + vec * f; @@ -567,248 +568,3 @@ void CaveV6::carveRoute(v3f vec, float f, bool randomize_xz, bool tunnel_above_g } } } - - -///////////////////////////////////////// Caves V7 - - -CaveV7::CaveV7(MapgenV7 *mg, PseudoRandom *ps) -{ - this->mg = mg; - this->vm = mg->vm; - this->ndef = mg->ndef; - this->water_level = mg->water_level; - this->ps = ps; - this->c_water_source = mg->c_water_source; - this->c_lava_source = mg->c_lava_source; - this->c_ice = mg->c_ice; - this->np_caveliquids = &nparams_caveliquids; - - dswitchint = ps->range(1, 14); - flooded = ps->range(1, 2) == 2; - - part_max_length_rs = ps->range(2, 4); - tunnel_routepoints = ps->range(5, ps->range(15, 30)); - min_tunnel_diameter = 5; - max_tunnel_diameter = ps->range(7, ps->range(8, 24)); - - large_cave_is_flat = (ps->range(0, 1) == 0); -} - - -void CaveV7::makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height) -{ - node_min = nmin; - node_max = nmax; - max_stone_y = max_stone_height; - main_direction = v3f(0, 0, 0); - - // Allowed route area size in nodes - ar = node_max - node_min + v3s16(1, 1, 1); - // Area starting point in nodes - of = node_min; - - // Allow a bit more - //(this should be more than the maximum radius of the tunnel) - s16 insure = 10; - s16 more = MYMAX(MAP_BLOCKSIZE - max_tunnel_diameter / 2 - insure, 1); - ar += v3s16(1,0,1) * more * 2; - of -= v3s16(1,0,1) * more; - - route_y_min = 0; - // Allow half a diameter + 7 over stone surface - route_y_max = -of.Y + max_stone_y + max_tunnel_diameter / 2 + 7; - - // Limit maximum to area - route_y_max = rangelim(route_y_max, 0, ar.Y - 1); - - s16 min = 0; - if (node_min.Y < water_level && node_max.Y > water_level) { - min = water_level - max_tunnel_diameter/3 - of.Y; - route_y_max = water_level + max_tunnel_diameter/3 - of.Y; - } - route_y_min = ps->range(min, min + max_tunnel_diameter); - route_y_min = rangelim(route_y_min, 0, route_y_max); - - s16 route_start_y_min = route_y_min; - s16 route_start_y_max = route_y_max; - - route_start_y_min = rangelim(route_start_y_min, 0, ar.Y - 1); - route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y - 1); - - // Randomize starting position - orp = v3f( - (float)(ps->next() % ar.X) + 0.5, - (float)(ps->range(route_start_y_min, route_start_y_max)) + 0.5, - (float)(ps->next() % ar.Z) + 0.5 - ); - - // Add generation notify begin event - v3s16 abs_pos(of.X + orp.X, of.Y + orp.Y, of.Z + orp.Z); - GenNotifyType notifytype = GENNOTIFY_LARGECAVE_BEGIN; - mg->gennotify.addEvent(notifytype, abs_pos); - - // Generate some tunnel starting from orp - for (u16 j = 0; j < tunnel_routepoints; j++) - makeTunnel(j % dswitchint == 0); - - // Add generation notify end event - abs_pos = v3s16(of.X + orp.X, of.Y + orp.Y, of.Z + orp.Z); - notifytype = GENNOTIFY_LARGECAVE_END; - mg->gennotify.addEvent(notifytype, abs_pos); -} - - -void CaveV7::makeTunnel(bool dirswitch) -{ - // Randomize size - s16 min_d = min_tunnel_diameter; - s16 max_d = max_tunnel_diameter; - rs = ps->range(min_d, max_d); - s16 rs_part_max_length_rs = rs * part_max_length_rs; - - v3s16 maxlen; - maxlen = v3s16( - rs_part_max_length_rs, - rs_part_max_length_rs / 2, - rs_part_max_length_rs - ); - - v3f vec; - // Jump downward sometimes - vec = v3f( - (float)(ps->next() % maxlen.X) - (float)maxlen.X / 2, - (float)(ps->next() % maxlen.Y) - (float)maxlen.Y / 2, - (float)(ps->next() % maxlen.Z) - (float)maxlen.Z / 2 - ); - - // Do not make caves that are above ground. - // It is only necessary to check the startpoint and endpoint. - v3s16 orpi(orp.X, orp.Y, orp.Z); - v3s16 veci(vec.X, vec.Y, vec.Z); - v3s16 p; - - p = orpi + veci + of + rs / 2; - if (p.Z >= node_min.Z && p.Z <= node_max.Z && - p.X >= node_min.X && p.X <= node_max.X) { - u32 index = (p.Z - node_min.Z) * mg->ystride + (p.X - node_min.X); - s16 h = mg->ridge_heightmap[index]; - if (h < p.Y) - return; - } else if (p.Y > water_level) { - return; // If it's not in our heightmap, use a simple heuristic - } - - p = orpi + of + rs / 2; - if (p.Z >= node_min.Z && p.Z <= node_max.Z && - p.X >= node_min.X && p.X <= node_max.X) { - u32 index = (p.Z - node_min.Z) * mg->ystride + (p.X - node_min.X); - s16 h = mg->ridge_heightmap[index]; - if (h < p.Y) - return; - } else if (p.Y > water_level) { - return; - } - - vec += main_direction; - - v3f rp = orp + vec; - if (rp.X < 0) - rp.X = 0; - else if (rp.X >= ar.X) - rp.X = ar.X - 1; - - if (rp.Y < route_y_min) - rp.Y = route_y_min; - else if (rp.Y >= route_y_max) - rp.Y = route_y_max - 1; - - if (rp.Z < 0) - rp.Z = 0; - else if (rp.Z >= ar.Z) - rp.Z = ar.Z - 1; - - vec = rp - orp; - - float veclen = vec.getLength(); - if (veclen < 0.05) - veclen = 1.0; - - // Every second section is rough - bool randomize_xz = (ps->range(1, 2) == 1); - - // Carve routes - for (float f = 0; f < 1.0; f += 1.0 / veclen) - carveRoute(vec, f, randomize_xz); - - orp = rp; -} - - -void CaveV7::carveRoute(v3f vec, float f, bool randomize_xz) -{ - MapNode airnode(CONTENT_AIR); - MapNode waternode(c_water_source); - MapNode lavanode(c_lava_source); - - v3s16 startp(orp.X, orp.Y, orp.Z); - startp += of; - - float nval = NoisePerlin3D(np_caveliquids, startp.X, - startp.Y, startp.Z, mg->seed); - MapNode liquidnode = (nval < 0.40 && node_max.Y < MGV7_LAVA_DEPTH) ? - lavanode : waternode; - - v3f fp = orp + vec * f; - fp.X += 0.1 * ps->range(-10, 10); - fp.Z += 0.1 * ps->range(-10, 10); - v3s16 cp(fp.X, fp.Y, fp.Z); - - s16 d0 = -rs/2; - s16 d1 = d0 + rs; - if (randomize_xz) { - d0 += ps->range(-1, 1); - d1 += ps->range(-1, 1); - } - - for (s16 z0 = d0; z0 <= d1; z0++) { - s16 si = rs / 2 - MYMAX(0, abs(z0) - rs / 7 - 1); - for (s16 x0 = -si - ps->range(0,1); x0 <= si - 1 + ps->range(0,1); x0++) { - s16 maxabsxz = MYMAX(abs(x0), abs(z0)); - - s16 si2 = rs / 2 - MYMAX(0, maxabsxz - rs / 7 - 1); - - for (s16 y0 = -si2; y0 <= si2; y0++) { - if (large_cave_is_flat) { - // Make large caves not so tall - if (rs > 7 && abs(y0) >= rs / 3) - continue; - } - - v3s16 p(cp.X + x0, cp.Y + y0, cp.Z + z0); - p += of; - - if (vm->m_area.contains(p) == false) - continue; - - u32 i = vm->m_area.index(p); - content_t c = vm->m_data[i].getContent(); - if (!ndef->get(c).is_ground_content) - continue; - - int full_ymin = node_min.Y - MAP_BLOCKSIZE; - int full_ymax = node_max.Y + MAP_BLOCKSIZE; - - if (flooded && full_ymin < water_level && - full_ymax > water_level) - vm->m_data[i] = (p.Y <= water_level) ? - waternode : airnode; - else if (flooded && full_ymax < water_level) - vm->m_data[i] = (p.Y < startp.Y - 4) ? - liquidnode : airnode; - else - vm->m_data[i] = airnode; - } - } - } -} diff --git a/src/cavegen.h b/src/cavegen.h index a1124711b..c907fe294 100644 --- a/src/cavegen.h +++ b/src/cavegen.h @@ -21,19 +21,18 @@ with this program; if not, write to the Free Software Foundation, Inc., #define CAVEGEN_HEADER #define VMANIP_FLAG_CAVE VOXELFLAG_CHECKED1 -#define MGV5_LAVA_DEPTH -256 -#define MGV7_LAVA_DEPTH -256 +#define DEFAULT_LAVA_DEPTH (-256) -class MapgenV5; class MapgenV6; -class MapgenV7; -class CaveV5 { +class CavesRandomWalk { public: Mapgen *mg; MMVManip *vm; INodeDefManager *ndef; + // variables + int lava_depth; NoiseParams *np_caveliquids; s16 min_tunnel_diameter; @@ -67,8 +66,7 @@ public: int water_level; int ystride; - CaveV5() {} - CaveV5(Mapgen *mg, PseudoRandom *ps); + CavesRandomWalk(Mapgen *mg, PseudoRandom *ps); void makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height); void makeTunnel(bool dirswitch); void carveRoute(v3f vec, float f, bool randomize_xz); @@ -118,49 +116,4 @@ public: void carveRoute(v3f vec, float f, bool randomize_xz, bool tunnel_above_ground); }; -class CaveV7 { -public: - MapgenV7 *mg; - MMVManip *vm; - INodeDefManager *ndef; - - NoiseParams *np_caveliquids; - - s16 min_tunnel_diameter; - s16 max_tunnel_diameter; - u16 tunnel_routepoints; - int dswitchint; - int part_max_length_rs; - - bool large_cave_is_flat; - bool flooded; - - s16 max_stone_y; - v3s16 node_min; - v3s16 node_max; - - v3f orp; // starting point, relative to caved space - v3s16 of; // absolute coordinates of caved space - v3s16 ar; // allowed route area - s16 rs; // tunnel radius size - v3f main_direction; - - s16 route_y_min; - s16 route_y_max; - - PseudoRandom *ps; - - content_t c_water_source; - content_t c_lava_source; - content_t c_ice; - - int water_level; - - CaveV7() {} - CaveV7(MapgenV7 *mg, PseudoRandom *ps); - void makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height); - void makeTunnel(bool dirswitch); - void carveRoute(v3f vec, float f, bool randomize_xz); -}; - #endif diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 01593718b..43ac6e51a 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -599,7 +599,7 @@ void MapgenBasic::generateCaves(s16 max_stone_y, s16 large_cave_depth) PseudoRandom ps(blockseed + 21343); u32 bruises_count = ps.range(0, 2); for (u32 i = 0; i < bruises_count; i++) { - CaveV5 cave(this, &ps); ////caves version varies ---- todo- fix this! + CavesRandomWalk cave(this, &ps); cave.makeCave(node_min, node_max, max_stone_y); } } diff --git a/src/mapgen_valleys.cpp b/src/mapgen_valleys.cpp index d424ca068..77fa1ed06 100644 --- a/src/mapgen_valleys.cpp +++ b/src/mapgen_valleys.cpp @@ -841,7 +841,7 @@ void MapgenValleys::generateCaves(s16 max_stone_y, s16 large_cave_depth) if (node_max.Y <= large_cave_depth && !made_a_big_one) { u32 bruises_count = ps.range(0, 2); for (u32 i = 0; i < bruises_count; i++) { - CaveV5 cave(this, &ps); + CavesRandomWalk cave(this, &ps); cave.makeCave(node_min, node_max, max_stone_y); } } From bf25837617541eee0317e1231abca65233edeb8e Mon Sep 17 00:00:00 2001 From: kwolekr Date: Tue, 10 May 2016 00:10:59 -0400 Subject: [PATCH 022/418] Cavegen: Rename CaveV6 to CavesV6 - Add comment explaining why it exists - Remove unused 'flooded' variable - Rename shadowed variable - Fix some code style --- src/cavegen.cpp | 49 ++++++++++++++++++++++++----------------------- src/cavegen.h | 19 ++++++++++++++---- src/mapgen_v6.cpp | 2 +- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/src/cavegen.cpp b/src/cavegen.cpp index 59274101e..e6e21be40 100644 --- a/src/cavegen.cpp +++ b/src/cavegen.cpp @@ -277,10 +277,12 @@ void CavesRandomWalk::carveRoute(v3f vec, float f, bool randomize_xz) } -///////////////////////////////////////// Caves V6 +//// +//// CavesV6 +//// - -CaveV6::CaveV6(MapgenV6 *mg, PseudoRandom *ps, PseudoRandom *ps2, bool is_large_cave) +CavesV6::CavesV6(MapgenV6 *mg, PseudoRandom *ps, PseudoRandom *ps2, + bool is_large_cave) { this->mg = mg; this->vm = mg->vm; @@ -295,23 +297,22 @@ CaveV6::CaveV6(MapgenV6 *mg, PseudoRandom *ps, PseudoRandom *ps2, bool is_large_ min_tunnel_diameter = 2; max_tunnel_diameter = ps->range(2, 6); dswitchint = ps->range(1, 14); - flooded = true; if (large_cave) { - part_max_length_rs = ps->range(2,4); - tunnel_routepoints = ps->range(5, ps->range(15,30)); + part_max_length_rs = ps->range(2, 4); + tunnel_routepoints = ps->range(5, ps->range(15, 30)); min_tunnel_diameter = 5; - max_tunnel_diameter = ps->range(7, ps->range(8,24)); + max_tunnel_diameter = ps->range(7, ps->range(8, 24)); } else { - part_max_length_rs = ps->range(2,9); - tunnel_routepoints = ps->range(10, ps->range(15,30)); + part_max_length_rs = ps->range(2, 9); + tunnel_routepoints = ps->range(10, ps->range(15, 30)); } - large_cave_is_flat = (ps->range(0,1) == 0); + large_cave_is_flat = (ps->range(0, 1) == 0); } -void CaveV6::makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height) +void CavesV6::makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height) { node_min = nmin; node_max = nmax; @@ -328,8 +329,8 @@ void CaveV6::makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height) const s16 max_spread_amount = MAP_BLOCKSIZE; s16 insure = 10; s16 more = MYMAX(max_spread_amount - max_tunnel_diameter / 2 - insure, 1); - ar += v3s16(1,0,1) * more * 2; - of -= v3s16(1,0,1) * more; + ar += v3s16(1, 0, 1) * more * 2; + of -= v3s16(1, 0, 1) * more; route_y_min = 0; // Allow half a diameter + 7 over stone surface @@ -339,20 +340,20 @@ void CaveV6::makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height) route_y_max = rangelim(route_y_max, 0, ar.Y - 1); if (large_cave) { - s16 min = 0; + s16 minpos = 0; if (node_min.Y < water_level && node_max.Y > water_level) { - min = water_level - max_tunnel_diameter/3 - of.Y; - route_y_max = water_level + max_tunnel_diameter/3 - of.Y; + minpos = water_level - max_tunnel_diameter / 3 - of.Y; + route_y_max = water_level + max_tunnel_diameter / 3 - of.Y; } - route_y_min = ps->range(min, min + max_tunnel_diameter); + route_y_min = ps->range(minpos, minpos + max_tunnel_diameter); route_y_min = rangelim(route_y_min, 0, route_y_max); } s16 route_start_y_min = route_y_min; s16 route_start_y_max = route_y_max; - route_start_y_min = rangelim(route_start_y_min, 0, ar.Y-1); - route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y-1); + route_start_y_min = rangelim(route_start_y_min, 0, ar.Y - 1); + route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y - 1); // Randomize starting position orp = v3f( @@ -379,7 +380,7 @@ void CaveV6::makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height) } -void CaveV6::makeTunnel(bool dirswitch) +void CavesV6::makeTunnel(bool dirswitch) { if (dirswitch && !large_cave) { main_direction = v3f( @@ -496,7 +497,8 @@ void CaveV6::makeTunnel(bool dirswitch) } -void CaveV6::carveRoute(v3f vec, float f, bool randomize_xz, bool tunnel_above_ground) +void CavesV6::carveRoute(v3f vec, float f, bool randomize_xz, + bool tunnel_above_ground) { MapNode airnode(CONTENT_AIR); MapNode waternode(c_water_source); @@ -547,11 +549,10 @@ void CaveV6::carveRoute(v3f vec, float f, bool randomize_xz, bool tunnel_above_g int full_ymin = node_min.Y - MAP_BLOCKSIZE; int full_ymax = node_max.Y + MAP_BLOCKSIZE; - if (flooded && full_ymin < water_level && - full_ymax > water_level) { + if (full_ymin < water_level && full_ymax > water_level) { vm->m_data[i] = (p.Y <= water_level) ? waternode : airnode; - } else if (flooded && full_ymax < water_level) { + } else if (full_ymax < water_level) { vm->m_data[i] = (p.Y < startp.Y - 2) ? lavanode : airnode; } else { diff --git a/src/cavegen.h b/src/cavegen.h index c907fe294..b50455c1c 100644 --- a/src/cavegen.h +++ b/src/cavegen.h @@ -72,7 +72,20 @@ public: void carveRoute(v3f vec, float f, bool randomize_xz); }; -class CaveV6 { +/* + CavesV6 is the original version of caves used with Mapgen V6. + + Though it uses the same fundamental algorithm as CavesRandomWalk, it is made + separate to preserve the exact sequence of PseudoRandom calls - any change + to this ordering results in the output being radically different. + Because caves in Mapgen V6 are responsible for a large portion of the basic + terrain shape, modifying this will break our contract of reverse + compatibility for a 'stable' mapgen such as V6. + + tl;dr, + *** DO NOT TOUCH THIS CLASS UNLESS YOU KNOW WHAT YOU ARE DOING *** +*/ +class CavesV6 { public: MapgenV6 *mg; MMVManip *vm; @@ -86,7 +99,6 @@ public: bool large_cave; bool large_cave_is_flat; - bool flooded; s16 max_stone_y; v3s16 node_min; @@ -109,8 +121,7 @@ public: int water_level; - CaveV6() {} - CaveV6(MapgenV6 *mg, PseudoRandom *ps, PseudoRandom *ps2, bool large_cave); + CavesV6(MapgenV6 *mg, PseudoRandom *ps, PseudoRandom *ps2, bool large_cave); void makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height); void makeTunnel(bool dirswitch); void carveRoute(v3f vec, float f, bool randomize_xz, bool tunnel_above_ground); diff --git a/src/mapgen_v6.cpp b/src/mapgen_v6.cpp index c389b2ed4..d143f12e9 100644 --- a/src/mapgen_v6.cpp +++ b/src/mapgen_v6.cpp @@ -1067,7 +1067,7 @@ void MapgenV6::generateCaves(int max_stone_y) for (u32 i = 0; i < caves_count + bruises_count; i++) { bool large_cave = (i >= caves_count); - CaveV6 cave(this, &ps, &ps2, large_cave); + CavesV6 cave(this, &ps, &ps2, large_cave); cave.makeCave(node_min, node_max, max_stone_y); } From a605d690921670008d605d067f1cf8cd1619def7 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Tue, 10 May 2016 13:21:13 -0400 Subject: [PATCH 023/418] Cavegen: Remove CavesV6 dependency on Mapgen --- src/cavegen.cpp | 113 +++++++++++++++++++++++++++------------------- src/cavegen.h | 39 +++++++++++----- src/mapgen_v6.cpp | 7 +-- 3 files changed, 97 insertions(+), 62 deletions(-) diff --git a/src/cavegen.cpp b/src/cavegen.cpp index e6e21be40..bc73b7206 100644 --- a/src/cavegen.cpp +++ b/src/cavegen.cpp @@ -27,9 +27,9 @@ with this program; if not, write to the Free Software Foundation, Inc., NoiseParams nparams_caveliquids(0, 1, v3f(150.0, 150.0, 150.0), 776, 3, 0.6, 2.0); - -///////////////////////////////////////// Caves V5 - +//// +//// CavesRandomWalk +//// CavesRandomWalk::CavesRandomWalk(Mapgen *mg, PseudoRandom *ps) { @@ -262,13 +262,10 @@ void CavesRandomWalk::carveRoute(v3f vec, float f, bool randomize_xz) int full_ymin = node_min.Y - MAP_BLOCKSIZE; int full_ymax = node_max.Y + MAP_BLOCKSIZE; - if (flooded && full_ymin < water_level && - full_ymax > water_level) - vm->m_data[i] = (p.Y <= water_level) ? - waternode : airnode; + if (flooded && full_ymin < water_level && full_ymax > water_level) + vm->m_data[i] = (p.Y <= water_level) ? waternode : airnode; else if (flooded && full_ymax < water_level) - vm->m_data[i] = (p.Y < startp.Y - 4) ? - liquidnode : airnode; + vm->m_data[i] = (p.Y < startp.Y - 4) ? liquidnode : airnode; else vm->m_data[i] = airnode; } @@ -281,23 +278,51 @@ void CavesRandomWalk::carveRoute(v3f vec, float f, bool randomize_xz) //// CavesV6 //// -CavesV6::CavesV6(MapgenV6 *mg, PseudoRandom *ps, PseudoRandom *ps2, - bool is_large_cave) +CavesV6::CavesV6(INodeDefManager *ndef, GenerateNotifier *gennotify, + int water_level, content_t water_source, content_t lava_source) { - this->mg = mg; - this->vm = mg->vm; - this->ndef = mg->ndef; - this->water_level = mg->water_level; - this->large_cave = is_large_cave; - this->ps = ps; - this->ps2 = ps2; - this->c_water_source = mg->c_water_source; - this->c_lava_source = mg->c_lava_source; + assert(ndef); + this->ndef = ndef; + this->gennotify = gennotify; + this->water_level = water_level; + + c_water_source = water_source; + if (c_water_source == CONTENT_IGNORE) + c_water_source = ndef->getId("mapgen_water_source"); + if (c_water_source == CONTENT_IGNORE) + c_water_source = CONTENT_AIR; + + c_lava_source = lava_source; + if (c_lava_source == CONTENT_IGNORE) + c_lava_source = ndef->getId("mapgen_lava_source"); + if (c_lava_source == CONTENT_IGNORE) + c_lava_source = CONTENT_AIR; +} + + +void CavesV6::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, + PseudoRandom *ps, PseudoRandom *ps2, + bool is_large_cave, int max_stone_height, s16 *heightmap) +{ + assert(vm); + assert(ps); + assert(ps2); + + this->vm = vm; + this->ps = ps; + this->ps2 = ps2; + this->node_min = nmin; + this->node_max = nmax; + this->heightmap = heightmap; + this->large_cave = is_large_cave; + + this->ystride = nmax.X - nmin.X + 1; + + // Set initial parameters from randomness min_tunnel_diameter = 2; max_tunnel_diameter = ps->range(2, 6); dswitchint = ps->range(1, 14); - if (large_cave) { part_max_length_rs = ps->range(2, 4); tunnel_routepoints = ps->range(5, ps->range(15, 30)); @@ -307,16 +332,8 @@ CavesV6::CavesV6(MapgenV6 *mg, PseudoRandom *ps, PseudoRandom *ps2, part_max_length_rs = ps->range(2, 9); tunnel_routepoints = ps->range(10, ps->range(15, 30)); } - large_cave_is_flat = (ps->range(0, 1) == 0); -} - -void CavesV6::makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height) -{ - node_min = nmin; - node_max = nmax; - max_stone_y = max_stone_height; main_direction = v3f(0, 0, 0); // Allowed route area size in nodes @@ -334,7 +351,7 @@ void CavesV6::makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height) route_y_min = 0; // Allow half a diameter + 7 over stone surface - route_y_max = -of.Y + max_stone_y + max_tunnel_diameter / 2 + 7; + route_y_max = -of.Y + max_stone_height + max_tunnel_diameter / 2 + 7; // Limit maximum to area route_y_max = rangelim(route_y_max, 0, ar.Y - 1); @@ -363,20 +380,24 @@ void CavesV6::makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height) ); // Add generation notify begin event - v3s16 abs_pos(of.X + orp.X, of.Y + orp.Y, of.Z + orp.Z); - GenNotifyType notifytype = large_cave ? - GENNOTIFY_LARGECAVE_BEGIN : GENNOTIFY_CAVE_BEGIN; - mg->gennotify.addEvent(notifytype, abs_pos); + if (gennotify != NULL) { + v3s16 abs_pos(of.X + orp.X, of.Y + orp.Y, of.Z + orp.Z); + GenNotifyType notifytype = large_cave ? + GENNOTIFY_LARGECAVE_BEGIN : GENNOTIFY_CAVE_BEGIN; + gennotify->addEvent(notifytype, abs_pos); + } // Generate some tunnel starting from orp for (u16 j = 0; j < tunnel_routepoints; j++) makeTunnel(j % dswitchint == 0); // Add generation notify end event - abs_pos = v3s16(of.X + orp.X, of.Y + orp.Y, of.Z + orp.Z); - notifytype = large_cave ? - GENNOTIFY_LARGECAVE_END : GENNOTIFY_CAVE_END; - mg->gennotify.addEvent(notifytype, abs_pos); + if (gennotify != NULL) { + v3s16 abs_pos = v3s16(of.X + orp.X, of.Y + orp.Y, of.Z + orp.Z); + GenNotifyType notifytype = large_cave ? + GENNOTIFY_LARGECAVE_END : GENNOTIFY_CAVE_END; + gennotify->addEvent(notifytype, abs_pos); + } } @@ -438,9 +459,9 @@ void CavesV6::makeTunnel(bool dirswitch) v3s16 p1 = orpi + veci + of + rs / 2; if (p1.Z >= node_min.Z && p1.Z <= node_max.Z && p1.X >= node_min.X && p1.X <= node_max.X) { - u32 index1 = (p1.Z - node_min.Z) * mg->ystride + + u32 index1 = (p1.Z - node_min.Z) * ystride + (p1.X - node_min.X); - h1 = mg->heightmap[index1]; + h1 = heightmap[index1]; } else { h1 = water_level; // If not in heightmap } @@ -448,9 +469,9 @@ void CavesV6::makeTunnel(bool dirswitch) v3s16 p2 = orpi + of + rs / 2; if (p2.Z >= node_min.Z && p2.Z <= node_max.Z && p2.X >= node_min.X && p2.X <= node_max.X) { - u32 index2 = (p2.Z - node_min.Z) * mg->ystride + + u32 index2 = (p2.Z - node_min.Z) * ystride + (p2.X - node_min.X); - h2 = mg->heightmap[index2]; + h2 = heightmap[index2]; } else { h2 = water_level; } @@ -512,7 +533,7 @@ void CavesV6::carveRoute(v3f vec, float f, bool randomize_xz, fp.Z += 0.1 * ps->range(-10, 10); v3s16 cp(fp.X, fp.Y, fp.Z); - s16 d0 = -rs/2; + s16 d0 = -rs / 2; s16 d1 = d0 + rs; if (randomize_xz) { d0 += ps->range(-1, 1); @@ -550,11 +571,9 @@ void CavesV6::carveRoute(v3f vec, float f, bool randomize_xz, int full_ymax = node_max.Y + MAP_BLOCKSIZE; if (full_ymin < water_level && full_ymax > water_level) { - vm->m_data[i] = (p.Y <= water_level) ? - waternode : airnode; + vm->m_data[i] = (p.Y <= water_level) ? waternode : airnode; } else if (full_ymax < water_level) { - vm->m_data[i] = (p.Y < startp.Y - 2) ? - lavanode : airnode; + vm->m_data[i] = (p.Y < startp.Y - 2) ? lavanode : airnode; } else { vm->m_data[i] = airnode; } diff --git a/src/cavegen.h b/src/cavegen.h index b50455c1c..74cd0b416 100644 --- a/src/cavegen.h +++ b/src/cavegen.h @@ -23,13 +23,14 @@ with this program; if not, write to the Free Software Foundation, Inc., #define VMANIP_FLAG_CAVE VOXELFLAG_CHECKED1 #define DEFAULT_LAVA_DEPTH (-256) -class MapgenV6; +class GenerateNotifier; class CavesRandomWalk { public: Mapgen *mg; MMVManip *vm; INodeDefManager *ndef; + s16 *heightmap; // variables int lava_depth; @@ -64,7 +65,7 @@ public: content_t c_ice; int water_level; - int ystride; + u16 ystride; CavesRandomWalk(Mapgen *mg, PseudoRandom *ps); void makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height); @@ -87,9 +88,18 @@ public: */ class CavesV6 { public: - MapgenV6 *mg; MMVManip *vm; INodeDefManager *ndef; + GenerateNotifier *gennotify; + PseudoRandom *ps; + PseudoRandom *ps2; + + s16 *heightmap; + content_t c_water_source; + content_t c_lava_source; + int water_level; + + u16 ystride; s16 min_tunnel_diameter; s16 max_tunnel_diameter; @@ -100,7 +110,6 @@ public: bool large_cave; bool large_cave_is_flat; - s16 max_stone_y; v3s16 node_min; v3s16 node_max; @@ -113,16 +122,22 @@ public: s16 route_y_min; s16 route_y_max; - PseudoRandom *ps; - PseudoRandom *ps2; + // ndef is a mandatory parameter. + // If gennotify is NULL, generation events are not logged. + CavesV6(INodeDefManager *ndef, + GenerateNotifier *gennotify = NULL, + int water_level = 1, + content_t water_source = CONTENT_IGNORE, + content_t lava_source = CONTENT_IGNORE); - content_t c_water_source; - content_t c_lava_source; + // vm, ps, and ps2 are mandatory parameters. + // If heightmap is NULL, the surface level at all points is assumed to + // be water_level. + void makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, + PseudoRandom *ps, PseudoRandom *ps2, + bool is_large_cave, int max_stone_height, s16 *heightmap = NULL); - int water_level; - - CavesV6(MapgenV6 *mg, PseudoRandom *ps, PseudoRandom *ps2, bool large_cave); - void makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height); +private: void makeTunnel(bool dirswitch); void carveRoute(v3f vec, float f, bool randomize_xz, bool tunnel_above_ground); }; diff --git a/src/mapgen_v6.cpp b/src/mapgen_v6.cpp index d143f12e9..65acfaf93 100644 --- a/src/mapgen_v6.cpp +++ b/src/mapgen_v6.cpp @@ -1066,9 +1066,10 @@ void MapgenV6::generateCaves(int max_stone_y) } for (u32 i = 0; i < caves_count + bruises_count; i++) { - bool large_cave = (i >= caves_count); - CavesV6 cave(this, &ps, &ps2, large_cave); + CavesV6 cave(ndef, &gennotify, water_level, c_water_source, c_lava_source); - cave.makeCave(node_min, node_max, max_stone_y); + bool large_cave = (i >= caves_count); + cave.makeCave(vm, node_min, node_max, &ps, &ps2, + large_cave, max_stone_y, heightmap); } } From 68b1cd8d1bda704dc174c2e39a03276f68984164 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Tue, 10 May 2016 13:35:39 -0400 Subject: [PATCH 024/418] Cavegen: Merge instances of repetitive surface level-finding code --- src/cavegen.cpp | 97 +++++++++++++++++++++---------------------------- src/cavegen.h | 5 +++ 2 files changed, 47 insertions(+), 55 deletions(-) diff --git a/src/cavegen.cpp b/src/cavegen.cpp index bc73b7206..59878c890 100644 --- a/src/cavegen.cpp +++ b/src/cavegen.cpp @@ -147,31 +147,10 @@ void CavesRandomWalk::makeTunnel(bool dirswitch) // Do not make caves that are above ground. // It is only necessary to check the startpoint and endpoint. - v3s16 orpi(orp.X, orp.Y, orp.Z); - v3s16 veci(vec.X, vec.Y, vec.Z); - v3s16 p; - - p = orpi + veci + of + rs / 2; - if (p.Z >= node_min.Z && p.Z <= node_max.Z && - p.X >= node_min.X && p.X <= node_max.X) { - u32 index = (p.Z - node_min.Z) * ystride + (p.X - node_min.X); - s16 h = mg->heightmap[index]; - if (h < p.Y) - return; - } else if (p.Y > water_level) { - return; // If it's not in our heightmap, use a simple heuristic - } - - p = orpi + of + rs / 2; - if (p.Z >= node_min.Z && p.Z <= node_max.Z && - p.X >= node_min.X && p.X <= node_max.X) { - u32 index = (p.Z - node_min.Z) * ystride + (p.X - node_min.X); - s16 h = mg->heightmap[index]; - if (h < p.Y) - return; - } else if (p.Y > water_level) { + v3s16 p1 = v3s16(orp.X, orp.Y, orp.Z) + of + rs / 2; + v3s16 p2 = v3s16(vec.X, vec.Y, vec.Z) + p1; + if (isPosAboveSurface(p1) || isPosAboveSurface(p2)) return; - } vec += main_direction; @@ -274,6 +253,22 @@ void CavesRandomWalk::carveRoute(v3f vec, float f, bool randomize_xz) } +inline bool CavesRandomWalk::isPosAboveSurface(v3s16 p) +{ + if (heightmap != NULL && + p.Z >= node_min.Z && p.Z <= node_max.Z && + p.X >= node_min.X && p.X <= node_max.X) { + u32 index = (p.Z - node_min.Z) * ystride + (p.X - node_min.X); + if (heightmap[index] < p.Y) + return true; + } else if (p.Y > water_level) { + return true; + } + + return false; +} + + //// //// CavesV6 //// @@ -448,39 +443,18 @@ void CavesV6::makeTunnel(bool dirswitch) ); } - // Do not make caves that are entirely above ground, to fix - // shadow bugs caused by overgenerated large caves. + // Do not make caves that are entirely above ground, to fix shadow bugs + // caused by overgenerated large caves. // It is only necessary to check the startpoint and endpoint. - v3s16 orpi(orp.X, orp.Y, orp.Z); - v3s16 veci(vec.X, vec.Y, vec.Z); - s16 h1; - s16 h2; + v3s16 p1 = v3s16(orp.X, orp.Y, orp.Z) + of + rs / 2; + v3s16 p2 = v3s16(vec.X, vec.Y, vec.Z) + p1; - v3s16 p1 = orpi + veci + of + rs / 2; - if (p1.Z >= node_min.Z && p1.Z <= node_max.Z && - p1.X >= node_min.X && p1.X <= node_max.X) { - u32 index1 = (p1.Z - node_min.Z) * ystride + - (p1.X - node_min.X); - h1 = heightmap[index1]; - } else { - h1 = water_level; // If not in heightmap - } - - v3s16 p2 = orpi + of + rs / 2; - if (p2.Z >= node_min.Z && p2.Z <= node_max.Z && - p2.X >= node_min.X && p2.X <= node_max.X) { - u32 index2 = (p2.Z - node_min.Z) * ystride + - (p2.X - node_min.X); - h2 = heightmap[index2]; - } else { - h2 = water_level; - } - - // If startpoint and endpoint are above ground, - // disable placing of nodes in carveRoute while - // still running all pseudorandom calls to ensure - // caves consistent with existing worlds. - bool tunnel_above_ground = p1.Y > h1 && p2.Y > h2; + // If startpoint and endpoint are above ground, disable placement of nodes + // in carveRoute while still running all PseudoRandom calls to ensure caves + // are consistent with existing worlds. + bool tunnel_above_ground = + p1.Y > getSurfaceFromHeightmap(p1) && + p2.Y > getSurfaceFromHeightmap(p2); vec += main_direction; @@ -588,3 +562,16 @@ void CavesV6::carveRoute(v3f vec, float f, bool randomize_xz, } } } + + +inline s16 CavesV6::getSurfaceFromHeightmap(v3s16 p) +{ + if (heightmap != NULL && + p.Z >= node_min.Z && p.Z <= node_max.Z && + p.X >= node_min.X && p.X <= node_max.X) { + u32 index = (p.Z - node_min.Z) * ystride + (p.X - node_min.X); + return heightmap[index]; + } else { + return water_level; + } +} diff --git a/src/cavegen.h b/src/cavegen.h index 74cd0b416..5a41966fe 100644 --- a/src/cavegen.h +++ b/src/cavegen.h @@ -71,6 +71,9 @@ public: void makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height); void makeTunnel(bool dirswitch); void carveRoute(v3f vec, float f, bool randomize_xz); + +private: + inline bool isPosAboveSurface(v3s16 p); }; /* @@ -140,6 +143,8 @@ public: private: void makeTunnel(bool dirswitch); void carveRoute(v3f vec, float f, bool randomize_xz, bool tunnel_above_ground); + + inline s16 getSurfaceFromHeightmap(v3s16 p); }; #endif From db1b4dc8908c83aef5560718af7466d95d86e44f Mon Sep 17 00:00:00 2001 From: kwolekr Date: Tue, 10 May 2016 22:56:03 -0400 Subject: [PATCH 025/418] Cavegen: Remove CavesRandomWalk dependency on Mapgen --- src/cavegen.cpp | 79 ++++++++++++++++++++++++++++-------------- src/cavegen.h | 45 +++++++++++++++++++----- src/mapgen.cpp | 6 ++-- src/mapgen_valleys.cpp | 6 ++-- 4 files changed, 98 insertions(+), 38 deletions(-) diff --git a/src/cavegen.cpp b/src/cavegen.cpp index 59878c890..25e8473bc 100644 --- a/src/cavegen.cpp +++ b/src/cavegen.cpp @@ -31,23 +31,52 @@ NoiseParams nparams_caveliquids(0, 1, v3f(150.0, 150.0, 150.0), 776, 3, 0.6, 2.0 //// CavesRandomWalk //// -CavesRandomWalk::CavesRandomWalk(Mapgen *mg, PseudoRandom *ps) +CavesRandomWalk::CavesRandomWalk( + INodeDefManager *ndef, + GenerateNotifier *gennotify, + int seed, + int water_level, + content_t water_source, + content_t lava_source) { - this->mg = mg; - this->vm = mg->vm; - this->ndef = mg->ndef; - this->water_level = mg->water_level; - this->ps = ps; - c_water_source = ndef->getId("mapgen_water_source"); - c_lava_source = ndef->getId("mapgen_lava_source"); - c_ice = ndef->getId("mapgen_ice"); + assert(ndef); + + this->ndef = ndef; + this->gennotify = gennotify; + this->seed = seed; + this->water_level = water_level; this->np_caveliquids = &nparams_caveliquids; - this->ystride = mg->csize.X; this->lava_depth = DEFAULT_LAVA_DEPTH; - if (c_ice == CONTENT_IGNORE) - c_ice = CONTENT_AIR; + c_water_source = water_source; + if (c_water_source == CONTENT_IGNORE) + c_water_source = ndef->getId("mapgen_water_source"); + if (c_water_source == CONTENT_IGNORE) + c_water_source = CONTENT_AIR; + c_lava_source = lava_source; + if (c_lava_source == CONTENT_IGNORE) + c_lava_source = ndef->getId("mapgen_lava_source"); + if (c_lava_source == CONTENT_IGNORE) + c_lava_source = CONTENT_AIR; +} + + +void CavesRandomWalk::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, + PseudoRandom *ps, int max_stone_height, s16 *heightmap) +{ + assert(vm); + assert(ps); + + this->vm = vm; + this->ps = ps; + this->node_min = nmin; + this->node_max = nmax; + this->heightmap = heightmap; + + this->ystride = nmax.X - nmin.X + 1; + + // Set initial parameters from randomness dswitchint = ps->range(1, 14); flooded = ps->range(1, 2) == 2; @@ -57,13 +86,7 @@ CavesRandomWalk::CavesRandomWalk(Mapgen *mg, PseudoRandom *ps) max_tunnel_diameter = ps->range(7, ps->range(8, 24)); large_cave_is_flat = (ps->range(0, 1) == 0); -} - -void CavesRandomWalk::makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height) -{ - node_min = nmin; - node_max = nmax; main_direction = v3f(0, 0, 0); // Allowed route area size in nodes @@ -107,18 +130,22 @@ void CavesRandomWalk::makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height) ); // Add generation notify begin event - v3s16 abs_pos(of.X + orp.X, of.Y + orp.Y, of.Z + orp.Z); - GenNotifyType notifytype = GENNOTIFY_LARGECAVE_BEGIN; - mg->gennotify.addEvent(notifytype, abs_pos); + if (gennotify) { + v3s16 abs_pos(of.X + orp.X, of.Y + orp.Y, of.Z + orp.Z); + GenNotifyType notifytype = GENNOTIFY_LARGECAVE_BEGIN; + gennotify->addEvent(notifytype, abs_pos); + } // Generate some tunnel starting from orp for (u16 j = 0; j < tunnel_routepoints; j++) makeTunnel(j % dswitchint == 0); // Add generation notify end event - abs_pos = v3s16(of.X + orp.X, of.Y + orp.Y, of.Z + orp.Z); - notifytype = GENNOTIFY_LARGECAVE_END; - mg->gennotify.addEvent(notifytype, abs_pos); + if (gennotify) { + v3s16 abs_pos(of.X + orp.X, of.Y + orp.Y, of.Z + orp.Z); + GenNotifyType notifytype = GENNOTIFY_LARGECAVE_END; + gennotify->addEvent(notifytype, abs_pos); + } } @@ -197,7 +224,7 @@ void CavesRandomWalk::carveRoute(v3f vec, float f, bool randomize_xz) startp += of; float nval = NoisePerlin3D(np_caveliquids, startp.X, - startp.Y, startp.Z, mg->seed); + startp.Y, startp.Z, seed); MapNode liquidnode = (nval < 0.40 && node_max.Y < lava_depth) ? lavanode : waternode; @@ -388,7 +415,7 @@ void CavesV6::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, // Add generation notify end event if (gennotify != NULL) { - v3s16 abs_pos = v3s16(of.X + orp.X, of.Y + orp.Y, of.Z + orp.Z); + v3s16 abs_pos(of.X + orp.X, of.Y + orp.Y, of.Z + orp.Z); GenNotifyType notifytype = large_cave ? GENNOTIFY_LARGECAVE_END : GENNOTIFY_CAVE_END; gennotify->addEvent(notifytype, abs_pos); diff --git a/src/cavegen.h b/src/cavegen.h index 5a41966fe..2db5e9d4f 100644 --- a/src/cavegen.h +++ b/src/cavegen.h @@ -25,17 +25,35 @@ with this program; if not, write to the Free Software Foundation, Inc., class GenerateNotifier; +/* + CavesRandomWalk is an implementation of a cave-digging algorithm that + operates on the principle of a "random walk" to approximate the stochiastic + activity of cavern development. + + In summary, this algorithm works by carving a randomly sized tunnel in a + random direction a random amount of times, randomly varying in width. + All randomness here is uniformly distributed; alternative distributions have + not yet been implemented. + + This algorithm is very fast, executing in less than 1ms on average for an + 80x80x80 chunk of map on a modern processor. +*/ class CavesRandomWalk { public: - Mapgen *mg; MMVManip *vm; INodeDefManager *ndef; + GenerateNotifier *gennotify; s16 *heightmap; - // variables + // configurable parameters + int seed; + int water_level; int lava_depth; NoiseParams *np_caveliquids; + // intermediate state variables + u16 ystride; + s16 min_tunnel_diameter; s16 max_tunnel_diameter; u16 tunnel_routepoints; @@ -62,17 +80,26 @@ public: content_t c_water_source; content_t c_lava_source; - content_t c_ice; - int water_level; - u16 ystride; + // ndef is a mandatory parameter. + // If gennotify is NULL, generation events are not logged. + CavesRandomWalk(INodeDefManager *ndef, + GenerateNotifier *gennotify = NULL, + int seed = 0, + int water_level = 1, + content_t water_source = CONTENT_IGNORE, + content_t lava_source = CONTENT_IGNORE); - CavesRandomWalk(Mapgen *mg, PseudoRandom *ps); - void makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height); + // vm and ps are mandatory parameters. + // If heightmap is NULL, the surface level at all points is assumed to + // be water_level. + void makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, + PseudoRandom *ps, int max_stone_height, s16 *heightmap); + +private: void makeTunnel(bool dirswitch); void carveRoute(v3f vec, float f, bool randomize_xz); -private: inline bool isPosAboveSurface(v3s16 p); }; @@ -97,11 +124,13 @@ public: PseudoRandom *ps; PseudoRandom *ps2; + // configurable parameters s16 *heightmap; content_t c_water_source; content_t c_lava_source; int water_level; + // intermediate state variables u16 ystride; s16 min_tunnel_diameter; diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 43ac6e51a..7e74ad642 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -599,8 +599,10 @@ void MapgenBasic::generateCaves(s16 max_stone_y, s16 large_cave_depth) PseudoRandom ps(blockseed + 21343); u32 bruises_count = ps.range(0, 2); for (u32 i = 0; i < bruises_count; i++) { - CavesRandomWalk cave(this, &ps); - cave.makeCave(node_min, node_max, max_stone_y); + CavesRandomWalk cave(ndef, &gennotify, seed, water_level, + c_water_source, CONTENT_IGNORE); + + cave.makeCave(vm, node_min, node_max, &ps, max_stone_y, heightmap); } } diff --git a/src/mapgen_valleys.cpp b/src/mapgen_valleys.cpp index 77fa1ed06..af7a8b153 100644 --- a/src/mapgen_valleys.cpp +++ b/src/mapgen_valleys.cpp @@ -841,8 +841,10 @@ void MapgenValleys::generateCaves(s16 max_stone_y, s16 large_cave_depth) if (node_max.Y <= large_cave_depth && !made_a_big_one) { u32 bruises_count = ps.range(0, 2); for (u32 i = 0; i < bruises_count; i++) { - CavesRandomWalk cave(this, &ps); - cave.makeCave(node_min, node_max, max_stone_y); + CavesRandomWalk cave(ndef, &gennotify, seed, water_level, + c_water_source, c_lava_source); + + cave.makeCave(vm, node_min, node_max, &ps, max_stone_y, heightmap); } } } From 5b05f75a272ede582d7f86d8dbbbf7bc81e81970 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Wed, 11 May 2016 00:00:05 -0400 Subject: [PATCH 026/418] Cavegen: Re-add small caves to CavesRandomWalk --- src/cavegen.cpp | 121 +++++++++++++++++++++++++++++------------ src/cavegen.h | 5 +- src/mapgen.cpp | 2 +- src/mapgen_valleys.cpp | 2 +- 4 files changed, 91 insertions(+), 39 deletions(-) diff --git a/src/cavegen.cpp b/src/cavegen.cpp index 25e8473bc..1beac6f27 100644 --- a/src/cavegen.cpp +++ b/src/cavegen.cpp @@ -63,16 +63,17 @@ CavesRandomWalk::CavesRandomWalk( void CavesRandomWalk::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, - PseudoRandom *ps, int max_stone_height, s16 *heightmap) + PseudoRandom *ps, bool is_large_cave, int max_stone_height, s16 *heightmap) { assert(vm); assert(ps); - this->vm = vm; - this->ps = ps; - this->node_min = nmin; - this->node_max = nmax; - this->heightmap = heightmap; + this->vm = vm; + this->ps = ps; + this->node_min = nmin; + this->node_max = nmax; + this->heightmap = heightmap; + this->large_cave = is_large_cave; this->ystride = nmax.X - nmin.X + 1; @@ -80,10 +81,17 @@ void CavesRandomWalk::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, dswitchint = ps->range(1, 14); flooded = ps->range(1, 2) == 2; - part_max_length_rs = ps->range(2, 4); - tunnel_routepoints = ps->range(5, ps->range(15, 30)); - min_tunnel_diameter = 5; - max_tunnel_diameter = ps->range(7, ps->range(8, 24)); + if (large_cave) { + part_max_length_rs = ps->range(2, 4); + tunnel_routepoints = ps->range(5, ps->range(15, 30)); + min_tunnel_diameter = 5; + max_tunnel_diameter = ps->range(7, ps->range(8, 24)); + } else { + part_max_length_rs = ps->range(2, 9); + tunnel_routepoints = ps->range(10, ps->range(15, 30)); + min_tunnel_diameter = 2; + max_tunnel_diameter = ps->range(2, 6); + } large_cave_is_flat = (ps->range(0, 1) == 0); @@ -108,13 +116,15 @@ void CavesRandomWalk::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, // Limit maximum to area route_y_max = rangelim(route_y_max, 0, ar.Y - 1); - s16 minpos = 0; - if (node_min.Y < water_level && node_max.Y > water_level) { - minpos = water_level - max_tunnel_diameter / 3 - of.Y; - route_y_max = water_level + max_tunnel_diameter / 3 - of.Y; + if (large_cave) { + s16 minpos = 0; + if (node_min.Y < water_level && node_max.Y > water_level) { + minpos = water_level - max_tunnel_diameter / 3 - of.Y; + route_y_max = water_level + max_tunnel_diameter / 3 - of.Y; + } + route_y_min = ps->range(minpos, minpos + max_tunnel_diameter); + route_y_min = rangelim(route_y_min, 0, route_y_max); } - route_y_min = ps->range(minpos, minpos + max_tunnel_diameter); - route_y_min = rangelim(route_y_min, 0, route_y_max); s16 route_start_y_min = route_y_min; s16 route_start_y_max = route_y_max; @@ -132,7 +142,8 @@ void CavesRandomWalk::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, // Add generation notify begin event if (gennotify) { v3s16 abs_pos(of.X + orp.X, of.Y + orp.Y, of.Z + orp.Z); - GenNotifyType notifytype = GENNOTIFY_LARGECAVE_BEGIN; + GenNotifyType notifytype = large_cave ? + GENNOTIFY_LARGECAVE_BEGIN : GENNOTIFY_CAVE_BEGIN; gennotify->addEvent(notifytype, abs_pos); } @@ -143,7 +154,8 @@ void CavesRandomWalk::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, // Add generation notify end event if (gennotify) { v3s16 abs_pos(of.X + orp.X, of.Y + orp.Y, of.Z + orp.Z); - GenNotifyType notifytype = GENNOTIFY_LARGECAVE_END; + GenNotifyType notifytype = large_cave ? + GENNOTIFY_LARGECAVE_END : GENNOTIFY_CAVE_END; gennotify->addEvent(notifytype, abs_pos); } } @@ -151,6 +163,15 @@ void CavesRandomWalk::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, void CavesRandomWalk::makeTunnel(bool dirswitch) { + if (dirswitch && !large_cave) { + main_direction = v3f( + ((float)(ps->next() % 20) - (float)10) / 10, + ((float)(ps->next() % 20) - (float)10) / 30, + ((float)(ps->next() % 20) - (float)10) / 10 + ); + main_direction *= (float)ps->range(0, 10) / 10; + } + // Randomize size s16 min_d = min_tunnel_diameter; s16 max_d = max_tunnel_diameter; @@ -158,19 +179,35 @@ void CavesRandomWalk::makeTunnel(bool dirswitch) s16 rs_part_max_length_rs = rs * part_max_length_rs; v3s16 maxlen; - maxlen = v3s16( - rs_part_max_length_rs, - rs_part_max_length_rs / 2, - rs_part_max_length_rs - ); + if (large_cave) { + maxlen = v3s16( + rs_part_max_length_rs, + rs_part_max_length_rs / 2, + rs_part_max_length_rs + ); + } else { + maxlen = v3s16( + rs_part_max_length_rs, + ps->range(1, rs_part_max_length_rs), + rs_part_max_length_rs + ); + } v3f vec; // Jump downward sometimes - vec = v3f( - (float)(ps->next() % maxlen.X) - (float)maxlen.X / 2, - (float)(ps->next() % maxlen.Y) - (float)maxlen.Y / 2, - (float)(ps->next() % maxlen.Z) - (float)maxlen.Z / 2 - ); + if (!large_cave && ps->range(0, 12) == 0) { + vec = v3f( + (float)(ps->next() % (maxlen.X * 1)) - (float)maxlen.X / 2, + (float)(ps->next() % (maxlen.Y * 2)) - (float)maxlen.Y, + (float)(ps->next() % (maxlen.Z * 1)) - (float)maxlen.Z / 2 + ); + } else { + vec = v3f( + (float)(ps->next() % (maxlen.X * 1)) - (float)maxlen.X / 2, + (float)(ps->next() % (maxlen.Y * 1)) - (float)maxlen.Y / 2, + (float)(ps->next() % (maxlen.Z * 1)) - (float)maxlen.Z / 2 + ); + } // Do not make caves that are above ground. // It is only necessary to check the startpoint and endpoint. @@ -240,6 +277,8 @@ void CavesRandomWalk::carveRoute(v3f vec, float f, bool randomize_xz) d1 += ps->range(-1, 1); } + bool flat_cave_floor = !large_cave && ps->range(0, 2) == 2; + for (s16 z0 = d0; z0 <= d1; z0++) { s16 si = rs / 2 - MYMAX(0, abs(z0) - rs / 7 - 1); for (s16 x0 = -si - ps->range(0,1); x0 <= si - 1 + ps->range(0,1); x0++) { @@ -248,6 +287,10 @@ void CavesRandomWalk::carveRoute(v3f vec, float f, bool randomize_xz) s16 si2 = rs / 2 - MYMAX(0, maxabsxz - rs / 7 - 1); for (s16 y0 = -si2; y0 <= si2; y0++) { + // Make better floors in small caves + if (flat_cave_floor && y0 <= -rs / 2 && rs <= 7) + continue; + if (large_cave_is_flat) { // Make large caves not so tall if (rs > 7 && abs(y0) >= rs / 3) @@ -265,15 +308,23 @@ void CavesRandomWalk::carveRoute(v3f vec, float f, bool randomize_xz) if (!ndef->get(c).is_ground_content) continue; - int full_ymin = node_min.Y - MAP_BLOCKSIZE; - int full_ymax = node_max.Y + MAP_BLOCKSIZE; + if (large_cave) { + int full_ymin = node_min.Y - MAP_BLOCKSIZE; + int full_ymax = node_max.Y + MAP_BLOCKSIZE; + + if (flooded && full_ymin < water_level && full_ymax > water_level) + vm->m_data[i] = (p.Y <= water_level) ? waternode : airnode; + else if (flooded && full_ymax < water_level) + vm->m_data[i] = (p.Y < startp.Y - 4) ? liquidnode : airnode; + else + vm->m_data[i] = airnode; + } else { + if (c == CONTENT_IGNORE) + continue; - if (flooded && full_ymin < water_level && full_ymax > water_level) - vm->m_data[i] = (p.Y <= water_level) ? waternode : airnode; - else if (flooded && full_ymax < water_level) - vm->m_data[i] = (p.Y < startp.Y - 4) ? liquidnode : airnode; - else vm->m_data[i] = airnode; + vm->m_flags[i] |= VMANIP_FLAG_CAVE; + } } } } diff --git a/src/cavegen.h b/src/cavegen.h index 2db5e9d4f..e250ad452 100644 --- a/src/cavegen.h +++ b/src/cavegen.h @@ -60,6 +60,7 @@ public: int dswitchint; int part_max_length_rs; + bool large_cave; bool large_cave_is_flat; bool flooded; @@ -93,8 +94,8 @@ public: // vm and ps are mandatory parameters. // If heightmap is NULL, the surface level at all points is assumed to // be water_level. - void makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, - PseudoRandom *ps, int max_stone_height, s16 *heightmap); + void makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, PseudoRandom *ps, + bool is_large_cave, int max_stone_height, s16 *heightmap); private: void makeTunnel(bool dirswitch); diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 7e74ad642..09671d845 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -602,7 +602,7 @@ void MapgenBasic::generateCaves(s16 max_stone_y, s16 large_cave_depth) CavesRandomWalk cave(ndef, &gennotify, seed, water_level, c_water_source, CONTENT_IGNORE); - cave.makeCave(vm, node_min, node_max, &ps, max_stone_y, heightmap); + cave.makeCave(vm, node_min, node_max, &ps, true, max_stone_y, heightmap); } } diff --git a/src/mapgen_valleys.cpp b/src/mapgen_valleys.cpp index af7a8b153..699554ecd 100644 --- a/src/mapgen_valleys.cpp +++ b/src/mapgen_valleys.cpp @@ -844,7 +844,7 @@ void MapgenValleys::generateCaves(s16 max_stone_y, s16 large_cave_depth) CavesRandomWalk cave(ndef, &gennotify, seed, water_level, c_water_source, c_lava_source); - cave.makeCave(vm, node_min, node_max, &ps, max_stone_y, heightmap); + cave.makeCave(vm, node_min, node_max, &ps, true, max_stone_y, heightmap); } } } From b1eb757e60c155707b8fc1b2497b632246819c7b Mon Sep 17 00:00:00 2001 From: kwolekr Date: Wed, 11 May 2016 03:47:45 -0400 Subject: [PATCH 027/418] Fix undefined evaluation order when constructing random vectors MSVC and GCC evaluate parameters in right-to-left order, whereas Clang evaluates in left-to-right order, and of course, an optimization could leave the order of evaluation completely indeterminate. This commit fixes all instances of the error by explicitly assigning the results of expressions that use PseudoRandom::next() or range() to their respective vector components. The right-to-left evaluation behavior is preserved since Clang is much less commonly used to compile Minetest than GCC and MSVC combined, and would therefore cause the least harm. --- src/cavegen.cpp | 67 +++++++++++++++++++--------------------------- src/dungeongen.cpp | 29 +++++++++++++------- 2 files changed, 47 insertions(+), 49 deletions(-) diff --git a/src/cavegen.cpp b/src/cavegen.cpp index 1beac6f27..5728c1337 100644 --- a/src/cavegen.cpp +++ b/src/cavegen.cpp @@ -133,11 +133,9 @@ void CavesRandomWalk::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y - 1); // Randomize starting position - orp = v3f( - (float)(ps->next() % ar.X) + 0.5, - (float)(ps->range(route_start_y_min, route_start_y_max)) + 0.5, - (float)(ps->next() % ar.Z) + 0.5 - ); + orp.Z = (float)(ps->next() % ar.Z) + 0.5; + orp.Y = (float)(ps->range(route_start_y_min, route_start_y_max)) + 0.5; + orp.X = (float)(ps->next() % ar.X) + 0.5; // Add generation notify begin event if (gennotify) { @@ -164,11 +162,10 @@ void CavesRandomWalk::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, void CavesRandomWalk::makeTunnel(bool dirswitch) { if (dirswitch && !large_cave) { - main_direction = v3f( - ((float)(ps->next() % 20) - (float)10) / 10, - ((float)(ps->next() % 20) - (float)10) / 30, - ((float)(ps->next() % 20) - (float)10) / 10 - ); + main_direction.Z = ((float)(ps->next() % 20) - (float)10) / 10; + main_direction.Y = ((float)(ps->next() % 20) - (float)10) / 30; + main_direction.X = ((float)(ps->next() % 20) - (float)10) / 10; + main_direction *= (float)ps->range(0, 10) / 10; } @@ -196,17 +193,13 @@ void CavesRandomWalk::makeTunnel(bool dirswitch) v3f vec; // Jump downward sometimes if (!large_cave && ps->range(0, 12) == 0) { - vec = v3f( - (float)(ps->next() % (maxlen.X * 1)) - (float)maxlen.X / 2, - (float)(ps->next() % (maxlen.Y * 2)) - (float)maxlen.Y, - (float)(ps->next() % (maxlen.Z * 1)) - (float)maxlen.Z / 2 - ); + vec.Z = (float)(ps->next() % (maxlen.Z * 1)) - (float)maxlen.Z / 2; + vec.Y = (float)(ps->next() % (maxlen.Y * 2)) - (float)maxlen.Y; + vec.X = (float)(ps->next() % (maxlen.X * 1)) - (float)maxlen.X / 2; } else { - vec = v3f( - (float)(ps->next() % (maxlen.X * 1)) - (float)maxlen.X / 2, - (float)(ps->next() % (maxlen.Y * 1)) - (float)maxlen.Y / 2, - (float)(ps->next() % (maxlen.Z * 1)) - (float)maxlen.Z / 2 - ); + vec.Z = (float)(ps->next() % (maxlen.Z * 1)) - (float)maxlen.Z / 2; + vec.Y = (float)(ps->next() % (maxlen.Y * 1)) - (float)maxlen.Y / 2; + vec.X = (float)(ps->next() % (maxlen.X * 1)) - (float)maxlen.X / 2; } // Do not make caves that are above ground. @@ -446,11 +439,9 @@ void CavesV6::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y - 1); // Randomize starting position - orp = v3f( - (float)(ps->next() % ar.X) + 0.5, - (float)(ps->range(route_start_y_min, route_start_y_max)) + 0.5, - (float)(ps->next() % ar.Z) + 0.5 - ); + orp.Z = (float)(ps->next() % ar.Z) + 0.5; + orp.Y = (float)(ps->range(route_start_y_min, route_start_y_max)) + 0.5; + orp.X = (float)(ps->next() % ar.X) + 0.5; // Add generation notify begin event if (gennotify != NULL) { @@ -477,11 +468,10 @@ void CavesV6::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, void CavesV6::makeTunnel(bool dirswitch) { if (dirswitch && !large_cave) { - main_direction = v3f( - ((float)(ps->next() % 20) - (float)10) / 10, - ((float)(ps->next() % 20) - (float)10) / 30, - ((float)(ps->next() % 20) - (float)10) / 10 - ); + main_direction.Z = ((float)(ps->next() % 20) - (float)10) / 10; + main_direction.Y = ((float)(ps->next() % 20) - (float)10) / 30; + main_direction.X = ((float)(ps->next() % 20) - (float)10) / 10; + main_direction *= (float)ps->range(0, 10) / 10; } @@ -506,19 +496,16 @@ void CavesV6::makeTunnel(bool dirswitch) ); } - v3f vec( - (float)(ps->next() % maxlen.X) - (float)maxlen.X / 2, - (float)(ps->next() % maxlen.Y) - (float)maxlen.Y / 2, - (float)(ps->next() % maxlen.Z) - (float)maxlen.Z / 2 - ); + v3f vec; + vec.Z = (float)(ps->next() % maxlen.Z) - (float)maxlen.Z / 2; + vec.Y = (float)(ps->next() % maxlen.Y) - (float)maxlen.Y / 2; + vec.X = (float)(ps->next() % maxlen.X) - (float)maxlen.X / 2; // Jump downward sometimes if (!large_cave && ps->range(0, 12) == 0) { - vec = v3f( - (float)(ps->next() % maxlen.X) - (float)maxlen.X / 2, - (float)(ps->next() % (maxlen.Y * 2)) - (float)maxlen.Y, - (float)(ps->next() % maxlen.Z) - (float)maxlen.Z / 2 - ); + vec.Z = (float)(ps->next() % maxlen.Z) - (float)maxlen.Z / 2; + vec.Y = (float)(ps->next() % (maxlen.Y * 2)) - (float)maxlen.Y; + vec.X = (float)(ps->next() % maxlen.X) - (float)maxlen.X / 2; } // Do not make caves that are entirely above ground, to fix shadow bugs diff --git a/src/dungeongen.cpp b/src/dungeongen.cpp index 5bfc320c9..f7f9b8b50 100644 --- a/src/dungeongen.cpp +++ b/src/dungeongen.cpp @@ -135,17 +135,23 @@ void DungeonGen::makeDungeon(v3s16 start_padding) bool fits = false; for (u32 i = 0; i < 100 && !fits; i++) { bool is_large_room = ((random.next() & 3) == 1); - roomsize = is_large_room ? - v3s16(random.range(8, 16), random.range(8, 16), random.range(8, 16)) : - v3s16(random.range(4, 8), random.range(4, 6), random.range(4, 8)); + if (is_large_room) { + roomsize.Z = random.range(8, 16); + roomsize.Y = random.range(8, 16); + roomsize.X = random.range(8, 16); + } else { + roomsize.Z = random.range(4, 8); + roomsize.Y = random.range(4, 6); + roomsize.X = random.range(4, 8); + } roomsize += dp.roomsize; // start_padding is used to disallow starting the generation of // a dungeon in a neighboring generation chunk - roomplace = vm->m_area.MinEdge + start_padding + v3s16( - random.range(0, areasize.X - roomsize.X - start_padding.X), - random.range(0, areasize.Y - roomsize.Y - start_padding.Y), - random.range(0, areasize.Z - roomsize.Z - start_padding.Z)); + roomplace = vm->m_area.MinEdge + start_padding; + roomplace.Z += random.range(0, areasize.Z - roomsize.Z - start_padding.Z); + roomplace.Y += random.range(0, areasize.Y - roomsize.Y - start_padding.Y); + roomplace.X += random.range(0, areasize.X - roomsize.X - start_padding.X); /* Check that we're not putting the room to an unknown place, @@ -227,7 +233,9 @@ void DungeonGen::makeDungeon(v3s16 start_padding) makeCorridor(doorplace, doordir, corridor_end, corridor_end_dir); // Find a place for a random sized room - roomsize = v3s16(random.range(4, 8), random.range(4, 6), random.range(4, 8)); + roomsize.Z = random.range(4, 8); + roomsize.Y = random.range(4, 6); + roomsize.X = random.range(4, 8); roomsize += dp.roomsize; m_pos = corridor_end; @@ -587,7 +595,10 @@ v3s16 rand_ortho_dir(PseudoRandom &random, bool diagonal_dirs) do { trycount++; - dir = v3s16(random.next() % 3 - 1, 0, random.next() % 3 - 1); + + dir.Z = random.next() % 3 - 1; + dir.Y = 0; + dir.X = random.next() % 3 - 1; } while ((dir.X == 0 && dir.Z == 0) && trycount < 10); return dir; From 6151f7bc4b32c2576035bd3381bd81ae287c57eb Mon Sep 17 00:00:00 2001 From: kwolekr Date: Sat, 14 May 2016 22:46:50 -0400 Subject: [PATCH 028/418] Cavegen: Minor misc. fixes - Convert instances of numeric literal doubles to floats - Move dswitchint to a local variable now that being a member is unnecessary - Improve const correctness - Indentation fixes --- src/cavegen.cpp | 50 ++++++++++++++++++++++++------------------------- src/cavegen.h | 4 +--- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/cavegen.cpp b/src/cavegen.cpp index 5728c1337..5f0af9699 100644 --- a/src/cavegen.cpp +++ b/src/cavegen.cpp @@ -78,8 +78,8 @@ void CavesRandomWalk::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, this->ystride = nmax.X - nmin.X + 1; // Set initial parameters from randomness - dswitchint = ps->range(1, 14); - flooded = ps->range(1, 2) == 2; + int dswitchint = ps->range(1, 14); + flooded = ps->range(1, 2) == 2; if (large_cave) { part_max_length_rs = ps->range(2, 4); @@ -104,10 +104,10 @@ void CavesRandomWalk::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, // Allow a bit more //(this should be more than the maximum radius of the tunnel) - s16 insure = 10; + const s16 insure = 10; s16 more = MYMAX(MAP_BLOCKSIZE - max_tunnel_diameter / 2 - insure, 1); - ar += v3s16(1,0,1) * more * 2; - of -= v3s16(1,0,1) * more; + ar += v3s16(1, 0, 1) * more * 2; + of -= v3s16(1, 0, 1) * more; route_y_min = 0; // Allow half a diameter + 7 over stone surface @@ -133,9 +133,9 @@ void CavesRandomWalk::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y - 1); // Randomize starting position - orp.Z = (float)(ps->next() % ar.Z) + 0.5; - orp.Y = (float)(ps->range(route_start_y_min, route_start_y_max)) + 0.5; - orp.X = (float)(ps->next() % ar.X) + 0.5; + orp.Z = (float)(ps->next() % ar.Z) + 0.5f; + orp.Y = (float)(ps->range(route_start_y_min, route_start_y_max)) + 0.5f; + orp.X = (float)(ps->next() % ar.X) + 0.5f; // Add generation notify begin event if (gennotify) { @@ -230,14 +230,14 @@ void CavesRandomWalk::makeTunnel(bool dirswitch) vec = rp - orp; float veclen = vec.getLength(); - if (veclen < 0.05) - veclen = 1.0; + if (veclen < 0.05f) + veclen = 1.0f; // Every second section is rough bool randomize_xz = (ps->range(1, 2) == 1); // Carve routes - for (float f = 0; f < 1.0; f += 1.0 / veclen) + for (float f = 0.f; f < 1.0f; f += 1.0f / veclen) carveRoute(vec, f, randomize_xz); orp = rp; @@ -255,15 +255,15 @@ void CavesRandomWalk::carveRoute(v3f vec, float f, bool randomize_xz) float nval = NoisePerlin3D(np_caveliquids, startp.X, startp.Y, startp.Z, seed); - MapNode liquidnode = (nval < 0.40 && node_max.Y < lava_depth) ? + MapNode liquidnode = (nval < 0.40f && node_max.Y < lava_depth) ? lavanode : waternode; v3f fp = orp + vec * f; - fp.X += 0.1 * ps->range(-10, 10); - fp.Z += 0.1 * ps->range(-10, 10); + fp.X += 0.1f * ps->range(-10, 10); + fp.Z += 0.1f * ps->range(-10, 10); v3s16 cp(fp.X, fp.Y, fp.Z); - s16 d0 = -rs/2; + s16 d0 = -rs / 2; s16 d1 = d0 + rs; if (randomize_xz) { d0 += ps->range(-1, 1); @@ -388,7 +388,7 @@ void CavesV6::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, // Set initial parameters from randomness min_tunnel_diameter = 2; max_tunnel_diameter = ps->range(2, 6); - dswitchint = ps->range(1, 14); + int dswitchint = ps->range(1, 14); if (large_cave) { part_max_length_rs = ps->range(2, 4); tunnel_routepoints = ps->range(5, ps->range(15, 30)); @@ -410,7 +410,7 @@ void CavesV6::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, // Allow a bit more //(this should be more than the maximum radius of the tunnel) const s16 max_spread_amount = MAP_BLOCKSIZE; - s16 insure = 10; + const s16 insure = 10; s16 more = MYMAX(max_spread_amount - max_tunnel_diameter / 2 - insure, 1); ar += v3s16(1, 0, 1) * more * 2; of -= v3s16(1, 0, 1) * more; @@ -439,9 +439,9 @@ void CavesV6::makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y - 1); // Randomize starting position - orp.Z = (float)(ps->next() % ar.Z) + 0.5; - orp.Y = (float)(ps->range(route_start_y_min, route_start_y_max)) + 0.5; - orp.X = (float)(ps->next() % ar.X) + 0.5; + orp.Z = (float)(ps->next() % ar.Z) + 0.5f; + orp.Y = (float)(ps->range(route_start_y_min, route_start_y_max)) + 0.5f; + orp.X = (float)(ps->next() % ar.X) + 0.5f; // Add generation notify begin event if (gennotify != NULL) { @@ -543,14 +543,14 @@ void CavesV6::makeTunnel(bool dirswitch) float veclen = vec.getLength(); // As odd as it sounds, veclen is *exactly* 0.0 sometimes, causing a FPE - if (veclen < 0.05) - veclen = 1.0; + if (veclen < 0.05f) + veclen = 1.0f; // Every second section is rough bool randomize_xz = (ps2->range(1, 2) == 1); // Carve routes - for (float f = 0; f < 1.0; f += 1.0 / veclen) + for (float f = 0.f; f < 1.0f; f += 1.0f / veclen) carveRoute(vec, f, randomize_xz, tunnel_above_ground); orp = rp; @@ -568,8 +568,8 @@ void CavesV6::carveRoute(v3f vec, float f, bool randomize_xz, startp += of; v3f fp = orp + vec * f; - fp.X += 0.1 * ps->range(-10, 10); - fp.Z += 0.1 * ps->range(-10, 10); + fp.X += 0.1f * ps->range(-10, 10); + fp.Z += 0.1f * ps->range(-10, 10); v3s16 cp(fp.X, fp.Y, fp.Z); s16 d0 = -rs / 2; diff --git a/src/cavegen.h b/src/cavegen.h index e250ad452..68e0c8bc5 100644 --- a/src/cavegen.h +++ b/src/cavegen.h @@ -57,7 +57,6 @@ public: s16 min_tunnel_diameter; s16 max_tunnel_diameter; u16 tunnel_routepoints; - int dswitchint; int part_max_length_rs; bool large_cave; @@ -95,7 +94,7 @@ public: // If heightmap is NULL, the surface level at all points is assumed to // be water_level. void makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, PseudoRandom *ps, - bool is_large_cave, int max_stone_height, s16 *heightmap); + bool is_large_cave, int max_stone_height, s16 *heightmap); private: void makeTunnel(bool dirswitch); @@ -137,7 +136,6 @@ public: s16 min_tunnel_diameter; s16 max_tunnel_diameter; u16 tunnel_routepoints; - int dswitchint; int part_max_length_rs; bool large_cave; From 548d99bb456931192609e8c6fa1eb4c80679af42 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Thu, 19 May 2016 03:40:22 -0400 Subject: [PATCH 029/418] Cavegen: Move V5-style caves to CavesNoiseIntersection --- src/cavegen.cpp | 96 ++++++++++++++++++++++++++++++++++++++++++ src/cavegen.h | 37 ++++++++++++++++ src/mapgen.cpp | 52 ++--------------------- src/mapgen.h | 5 +-- src/mapgen_flat.cpp | 10 +---- src/mapgen_fractal.cpp | 10 +---- src/mapgen_v5.cpp | 10 ++--- src/mapgen_v7.cpp | 10 ++--- src/mapgen_valleys.h | 3 ++ 9 files changed, 151 insertions(+), 82 deletions(-) diff --git a/src/cavegen.cpp b/src/cavegen.cpp index 5f0af9699..51309ad60 100644 --- a/src/cavegen.cpp +++ b/src/cavegen.cpp @@ -23,10 +23,106 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "mapgen_v5.h" #include "mapgen_v6.h" #include "mapgen_v7.h" +#include "mg_biome.h" #include "cavegen.h" NoiseParams nparams_caveliquids(0, 1, v3f(150.0, 150.0, 150.0), 776, 3, 0.6, 2.0); + +//// +//// CavesNoiseIntersection +//// + +CavesNoiseIntersection::CavesNoiseIntersection( + INodeDefManager *nodedef, BiomeManager *biomemgr, v3s16 chunksize, + NoiseParams *np_cave1, NoiseParams *np_cave2, int seed, float cave_width) +{ + assert(nodedef); + assert(biomemgr); + + m_ndef = nodedef; + m_bmgr = biomemgr; + + m_csize = chunksize; + m_cave_width = cave_width; + + m_ystride = m_csize.X; + m_zstride_1d = m_csize.X * (m_csize.Y + 1); + + // Noises are created using 1-down overgeneration + // A Nx-by-1-by-Nz-sized plane is at the bottom of the desired for + // re-carving the solid overtop placed for blocking sunlight + noise_cave1 = new Noise(np_cave1, seed, m_csize.X, m_csize.Y + 1, m_csize.Z); + noise_cave2 = new Noise(np_cave2, seed, m_csize.X, m_csize.Y + 1, m_csize.Z); +} + + +CavesNoiseIntersection::~CavesNoiseIntersection() +{ + delete noise_cave1; + delete noise_cave2; +} + + +void CavesNoiseIntersection::generateCaves(MMVManip *vm, + v3s16 nmin, v3s16 nmax, u8 *biomemap) +{ + assert(vm); + assert(biomemap); + + noise_cave1->perlinMap3D(nmin.X, nmin.Y - 1, nmin.Z); + noise_cave2->perlinMap3D(nmin.X, nmin.Y - 1, nmin.Z); + + v3s16 em = vm->m_area.getExtent(); + u32 index2d = 0; + + for (s16 z = nmin.Z; z <= nmax.Z; z++) + for (s16 x = nmin.X; x <= nmax.X; x++, index2d++) { + bool column_is_open = false; // Is column open to overground + bool is_tunnel = false; // Is tunnel or tunnel floor + u32 vi = vm->m_area.index(x, nmax.Y, z); + u32 index3d = (z - nmin.Z) * m_zstride_1d + m_csize.Y * m_ystride + + (x - nmin.X); + // Biome of column + Biome *biome = (Biome *)m_bmgr->getRaw(biomemap[index2d]); + + // Don't excavate the overgenerated stone at nmax.Y + 1, + // this creates a 'roof' over the tunnel, preventing light in + // tunnels at mapchunk borders when generating mapchunks upwards. + // This 'roof' is removed when the mapchunk above is generated. + for (s16 y = nmax.Y; y >= nmin.Y - 1; y--, + index3d -= m_ystride, + vm->m_area.add_y(em, vi, -1)) { + + content_t c = vm->m_data[vi].getContent(); + if (c == CONTENT_AIR || c == biome->c_water_top || + c == biome->c_water) { + column_is_open = true; + continue; + } + // Ground + float d1 = contour(noise_cave1->result[index3d]); + float d2 = contour(noise_cave2->result[index3d]); + + if (d1 * d2 > m_cave_width && m_ndef->get(c).is_ground_content) { + // In tunnel and ground content, excavate + vm->m_data[vi] = MapNode(CONTENT_AIR); + is_tunnel = true; + } else { + // Not in tunnel or not ground content + if (is_tunnel && column_is_open && + (c == biome->c_filler || c == biome->c_stone)) + // Tunnel entrance floor + vm->m_data[vi] = MapNode(biome->c_top); + + column_is_open = false; + is_tunnel = false; + } + } + } +} + + //// //// CavesRandomWalk //// diff --git a/src/cavegen.h b/src/cavegen.h index 68e0c8bc5..da0894635 100644 --- a/src/cavegen.h +++ b/src/cavegen.h @@ -25,6 +25,43 @@ with this program; if not, write to the Free Software Foundation, Inc., class GenerateNotifier; +/* + CavesNoiseIntersection is a cave digging algorithm that carves smooth, + web-like, continuous tunnels at points where the density of the intersection + between two separate 3d noises is above a certain value. This value, + cave_width, can be modified to set the effective width of these tunnels. + + This algorithm is relatively heavyweight, taking ~80ms to generate an + 80x80x80 chunk of map on a modern processor. Use sparingly! + + TODO(hmmmm): Remove dependency on biomes + TODO(hmmmm): Find alternative to overgeneration as solution for sunlight issue +*/ +class CavesNoiseIntersection { +public: + CavesNoiseIntersection(INodeDefManager *nodedef, BiomeManager *biomemgr, + v3s16 chunksize, NoiseParams *np_cave1, NoiseParams *np_cave2, + int seed, float cave_width); + ~CavesNoiseIntersection(); + + void generateCaves(MMVManip *vm, v3s16 nmin, v3s16 nmax, u8 *biomemap); + +private: + INodeDefManager *m_ndef; + BiomeManager *m_bmgr; + + // configurable parameters + v3s16 m_csize; + float m_cave_width; + + // intermediate state variables + u16 m_ystride; + u16 m_zstride_1d; + + Noise *noise_cave1; + Noise *noise_cave2; +}; + /* CavesRandomWalk is an implementation of a cave-digging algorithm that operates on the principle of a "random walk" to approximate the stochiastic diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 09671d845..7e959533d 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -542,56 +542,10 @@ void MapgenBasic::generateCaves(s16 max_stone_y, s16 large_cave_depth) if (max_stone_y < node_min.Y) return; - noise_cave1->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z); - noise_cave2->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z); + CavesNoiseIntersection caves_noise(ndef, bmgr, csize, + &np_cave1, &np_cave2, seed, cave_width); - v3s16 em = vm->m_area.getExtent(); - u32 index2d = 0; - - for (s16 z = node_min.Z; z <= node_max.Z; z++) - for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) { - bool column_is_open = false; // Is column open to overground - bool is_tunnel = false; // Is tunnel or tunnel floor - u32 vi = vm->m_area.index(x, node_max.Y, z); - u32 index3d = (z - node_min.Z) * zstride_1d + csize.Y * ystride + - (x - node_min.X); - // Biome of column - Biome *biome = (Biome *)bmgr->getRaw(biomemap[index2d]); - - // Don't excavate the overgenerated stone at node_max.Y + 1, - // this creates a 'roof' over the tunnel, preventing light in - // tunnels at mapchunk borders when generating mapchunks upwards. - // This 'roof' is removed when the mapchunk above is generated. - for (s16 y = node_max.Y; y >= node_min.Y - 1; y--, - index3d -= ystride, - vm->m_area.add_y(em, vi, -1)) { - - content_t c = vm->m_data[vi].getContent(); - if (c == CONTENT_AIR || c == biome->c_water_top || - c == biome->c_water) { - column_is_open = true; - continue; - } - // Ground - float d1 = contour(noise_cave1->result[index3d]); - float d2 = contour(noise_cave2->result[index3d]); - - if (d1 * d2 > cave_width && ndef->get(c).is_ground_content) { - // In tunnel and ground content, excavate - vm->m_data[vi] = MapNode(CONTENT_AIR); - is_tunnel = true; - } else { - // Not in tunnel or not ground content - if (is_tunnel && column_is_open && - (c == biome->c_filler || c == biome->c_stone)) - // Tunnel entrance floor - vm->m_data[vi] = MapNode(biome->c_top); - - column_is_open = false; - is_tunnel = false; - } - } - } + caves_noise.generateCaves(vm, node_min, node_max, biomemap); if (node_max.Y > large_cave_depth) return; diff --git a/src/mapgen.h b/src/mapgen.h index 3ec148296..8db01f5c1 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -220,8 +220,6 @@ public: BiomeManager *bmgr; Noise *noise_filler_depth; - Noise *noise_cave1; - Noise *noise_cave2; v3s16 node_min; v3s16 node_max; @@ -235,7 +233,8 @@ public: content_t c_sandstone; int ystride; - int zstride_1d; + NoiseParams np_cave1; + NoiseParams np_cave2; float cave_width; MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emerge); diff --git a/src/mapgen_flat.cpp b/src/mapgen_flat.cpp index 2cbf9ace0..a541a54f3 100644 --- a/src/mapgen_flat.cpp +++ b/src/mapgen_flat.cpp @@ -58,8 +58,6 @@ MapgenFlat::MapgenFlat(int mapgenid, MapgenParams *params, EmergeManager *emerge //// amount of elements to skip for the next index //// for noise/height/biome maps (not vmanip) this->ystride = csize.X; - // 1-down overgeneration - this->zstride_1d = csize.X * (csize.Y + 1); this->heightmap = new s16[csize.X * csize.Z]; @@ -78,10 +76,8 @@ MapgenFlat::MapgenFlat(int mapgenid, MapgenParams *params, EmergeManager *emerge noise_terrain = new Noise(&sp->np_terrain, seed, csize.X, csize.Z); noise_filler_depth = new Noise(&sp->np_filler_depth, seed, csize.X, csize.Z); - //// 3D noise - // 1-down overgeneraion - noise_cave1 = new Noise(&sp->np_cave1, seed, csize.X, csize.Y + 1, csize.Z); - noise_cave2 = new Noise(&sp->np_cave2, seed, csize.X, csize.Y + 1, csize.Z); + MapgenBasic::np_cave1 = sp->np_cave1; + MapgenBasic::np_cave2 = sp->np_cave2; //// Initialize biome generator biomegen = emerge->biomemgr->createBiomeGen( @@ -119,8 +115,6 @@ MapgenFlat::~MapgenFlat() { delete noise_terrain; delete noise_filler_depth; - delete noise_cave1; - delete noise_cave2; delete biomegen; diff --git a/src/mapgen_fractal.cpp b/src/mapgen_fractal.cpp index 3217d52e2..dca10d253 100644 --- a/src/mapgen_fractal.cpp +++ b/src/mapgen_fractal.cpp @@ -56,8 +56,6 @@ MapgenFractal::MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager * //// amount of elements to skip for the next index //// for noise/height/biome maps (not vmanip) this->ystride = csize.X; - // 1-down overgeneration - this->zstride_1d = csize.X * (csize.Y + 1); this->heightmap = new s16[csize.X * csize.Z]; @@ -79,10 +77,8 @@ MapgenFractal::MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager * noise_seabed = new Noise(&sp->np_seabed, seed, csize.X, csize.Z); noise_filler_depth = new Noise(&sp->np_filler_depth, seed, csize.X, csize.Z); - //// 3D terrain noise - // 1-down overgeneraion - noise_cave1 = new Noise(&sp->np_cave1, seed, csize.X, csize.Y + 1, csize.Z); - noise_cave2 = new Noise(&sp->np_cave2, seed, csize.X, csize.Y + 1, csize.Z); + MapgenBasic::np_cave1 = sp->np_cave1; + MapgenBasic::np_cave2 = sp->np_cave2; //// Initialize biome generator biomegen = emerge->biomemgr->createBiomeGen( @@ -123,8 +119,6 @@ MapgenFractal::~MapgenFractal() { delete noise_seabed; delete noise_filler_depth; - delete noise_cave1; - delete noise_cave2; delete biomegen; diff --git a/src/mapgen_v5.cpp b/src/mapgen_v5.cpp index 6c1441bc2..0c063dc6f 100644 --- a/src/mapgen_v5.cpp +++ b/src/mapgen_v5.cpp @@ -54,8 +54,6 @@ MapgenV5::MapgenV5(int mapgenid, MapgenParams *params, EmergeManager *emerge) // amount of elements to skip for the next index // for noise/height/biome maps (not vmanip) this->ystride = csize.X; - // 1-down overgeneration - this->zstride_1d = csize.X * (csize.Y + 1); this->heightmap = new s16[csize.X * csize.Z]; @@ -72,9 +70,9 @@ MapgenV5::MapgenV5(int mapgenid, MapgenParams *params, EmergeManager *emerge) // 3D terrain noise // 1-up 1-down overgeneration noise_ground = new Noise(&sp->np_ground, seed, csize.X, csize.Y + 2, csize.Z); - // 1-down overgeneraion - noise_cave1 = new Noise(&sp->np_cave1, seed, csize.X, csize.Y + 1, csize.Z); - noise_cave2 = new Noise(&sp->np_cave2, seed, csize.X, csize.Y + 1, csize.Z); + + MapgenBasic::np_cave1 = sp->np_cave1; + MapgenBasic::np_cave2 = sp->np_cave2; //// Initialize biome generator biomegen = emerge->biomemgr->createBiomeGen( @@ -113,8 +111,6 @@ MapgenV5::~MapgenV5() delete noise_filler_depth; delete noise_factor; delete noise_height; - delete noise_cave1; - delete noise_cave2; delete noise_ground; delete biomegen; diff --git a/src/mapgen_v7.cpp b/src/mapgen_v7.cpp index e6801a24b..e297a1d3e 100644 --- a/src/mapgen_v7.cpp +++ b/src/mapgen_v7.cpp @@ -61,8 +61,6 @@ MapgenV7::MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge) this->ystride = csize.X; // 1-up 1-down overgeneration this->zstride_1u1d = csize.X * (csize.Y + 2); - // 1-down overgeneration - this->zstride_1d = csize.X * (csize.Y + 1); this->heightmap = new s16[csize.X * csize.Z]; this->ridge_heightmap = new s16[csize.X * csize.Z]; @@ -85,9 +83,9 @@ MapgenV7::MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge) // 1-up 1-down overgeneration noise_mountain = new Noise(&sp->np_mountain, seed, csize.X, csize.Y + 2, csize.Z); noise_ridge = new Noise(&sp->np_ridge, seed, csize.X, csize.Y + 2, csize.Z); - // 1-down overgeneraion - noise_cave1 = new Noise(&sp->np_cave1, seed, csize.X, csize.Y + 1, csize.Z); - noise_cave2 = new Noise(&sp->np_cave2, seed, csize.X, csize.Y + 1, csize.Z); + + MapgenBasic::np_cave1 = sp->np_cave1; + MapgenBasic::np_cave2 = sp->np_cave2; // TODO(hmmmm): should we have a way to disable biomemanager biomes? //// Initialize biome generator @@ -133,8 +131,6 @@ MapgenV7::~MapgenV7() delete noise_ridge_uwater; delete noise_mountain; delete noise_ridge; - delete noise_cave1; - delete noise_cave2; delete biomegen; diff --git a/src/mapgen_valleys.h b/src/mapgen_valleys.h index 2ab253430..4f87be6b6 100644 --- a/src/mapgen_valleys.h +++ b/src/mapgen_valleys.h @@ -100,6 +100,7 @@ private: BiomeGenOriginal *m_bgen; int zstride; + int zstride_1d; float map_gen_limit; @@ -120,6 +121,8 @@ private: Noise *noise_inter_valley_fill; Noise *noise_inter_valley_slope; Noise *noise_rivers; + Noise *noise_cave1; + Noise *noise_cave2; Noise *noise_massive_caves; Noise *noise_terrain_height; Noise *noise_valley_depth; From c5968049bbf73ceff08a2b1d35bb34192fa3f315 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Thu, 19 May 2016 14:05:51 -0400 Subject: [PATCH 030/418] Mapgen V7: Remove now-unused ridge heightmap --- src/mapgen_v7.cpp | 10 ++-------- src/mapgen_v7.h | 2 -- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/mapgen_v7.cpp b/src/mapgen_v7.cpp index e297a1d3e..ee2c5f45f 100644 --- a/src/mapgen_v7.cpp +++ b/src/mapgen_v7.cpp @@ -62,8 +62,7 @@ MapgenV7::MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge) // 1-up 1-down overgeneration this->zstride_1u1d = csize.X * (csize.Y + 2); - this->heightmap = new s16[csize.X * csize.Z]; - this->ridge_heightmap = new s16[csize.X * csize.Z]; + this->heightmap = new s16[csize.X * csize.Z]; MapgenV7Params *sp = (MapgenV7Params *)params->sparams; @@ -134,7 +133,6 @@ MapgenV7::~MapgenV7() delete biomegen; - delete[] ridge_heightmap; delete[] heightmap; } @@ -440,8 +438,7 @@ int MapgenV7::generateTerrain() for (s16 z = node_min.Z; z <= node_max.Z; z++) for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) { s16 surface_y = baseTerrainLevelFromMap(index2d); - heightmap[index2d] = surface_y; // Create base terrain heightmap - ridge_heightmap[index2d] = surface_y; + heightmap[index2d] = surface_y; // Create base terrain heightmap if (surface_y > stone_surface_max_y) stone_surface_max_y = surface_y; @@ -504,9 +501,6 @@ void MapgenV7::generateRidgeTerrain() if (nridge + width_mod * height_mod < 0.6) continue; - if (y < ridge_heightmap[j]) - ridge_heightmap[j] = y - 1; - vm->m_data[vi] = (y > water_level) ? n_air : n_water; } } diff --git a/src/mapgen_v7.h b/src/mapgen_v7.h index 21beab2b4..eb89d5978 100644 --- a/src/mapgen_v7.h +++ b/src/mapgen_v7.h @@ -58,8 +58,6 @@ class MapgenV7 : public MapgenBasic { public: int zstride_1u1d; - s16 *ridge_heightmap; - u32 spflags; Noise *noise_terrain_base; Noise *noise_terrain_alt; From 0df5c01a8ce927c33ae9b67f459365505b980c33 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Fri, 20 May 2016 03:37:31 -0400 Subject: [PATCH 031/418] Mapgen: Remove calculateNoise from most mapgens This commit moves noise calculation to the functions where the noise is actually required, increasing the separation of concerns and level of interdependency for each mapgen method. Valleys Mapgen is left unmodified. --- src/mapgen.cpp | 2 ++ src/mapgen_flat.cpp | 30 ++++------------------- src/mapgen_flat.h | 1 - src/mapgen_fractal.cpp | 22 ++--------------- src/mapgen_fractal.h | 1 - src/mapgen_v5.cpp | 27 ++++----------------- src/mapgen_v5.h | 1 - src/mapgen_v7.cpp | 54 ++++++++++++++---------------------------- src/mapgen_v7.h | 3 --- src/mapgen_valleys.cpp | 1 - 10 files changed, 31 insertions(+), 111 deletions(-) diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 7e959533d..009a81f0c 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -386,6 +386,8 @@ MgStoneType MapgenBasic::generateBiomes() u32 index = 0; MgStoneType stone_type = MGSTONE_STONE; + noise_filler_depth->perlinMap2D(node_min.X, node_min.Z); + for (s16 z = node_min.Z; z <= node_max.Z; z++) for (s16 x = node_min.X; x <= node_max.X; x++, index++) { Biome *biome = NULL; diff --git a/src/mapgen_flat.cpp b/src/mapgen_flat.cpp index a541a54f3..ded859f65 100644 --- a/src/mapgen_flat.cpp +++ b/src/mapgen_flat.cpp @@ -227,9 +227,6 @@ void MapgenFlat::makeChunk(BlockMakeData *data) blockseed = getBlockSeed2(full_node_min, seed); - // Make some noise - calculateNoise(); - // Generate base terrain, mountains, and ridges with initial heightmaps s16 stone_surface_max_y = generateTerrain(); @@ -312,24 +309,6 @@ void MapgenFlat::makeChunk(BlockMakeData *data) } -void MapgenFlat::calculateNoise() -{ - //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO); - s16 x = node_min.X; - s16 z = node_min.Z; - - if ((spflags & MGFLAT_LAKES) || (spflags & MGFLAT_HILLS)) - noise_terrain->perlinMap2D(x, z); - - // Cave noises are calculated in generateCaves() - // only if solid terrain is present in mapchunk - - noise_filler_depth->perlinMap2D(x, z); - - //printf("calculateNoise: %dus\n", t.stop()); -} - - s16 MapgenFlat::generateTerrain() { MapNode n_air(CONTENT_AIR); @@ -340,13 +319,14 @@ s16 MapgenFlat::generateTerrain() s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT; u32 ni2d = 0; + bool use_noise = (spflags & MGFLAT_LAKES) || (spflags & MGFLAT_HILLS); + if (use_noise) + noise_terrain->perlinMap2D(node_min.X, node_min.Z); + for (s16 z = node_min.Z; z <= node_max.Z; z++) for (s16 x = node_min.X; x <= node_max.X; x++, ni2d++) { s16 stone_level = ground_level; - float n_terrain = 0.0f; - - if ((spflags & MGFLAT_LAKES) || (spflags & MGFLAT_HILLS)) - n_terrain = noise_terrain->result[ni2d]; + float n_terrain = use_noise ? noise_terrain->result[ni2d] : 0.0f; if ((spflags & MGFLAT_LAKES) && n_terrain < lake_threshold) { s16 depress = (lake_threshold - n_terrain) * lake_steepness; diff --git a/src/mapgen_flat.h b/src/mapgen_flat.h index 2b98c1f31..39da6e025 100644 --- a/src/mapgen_flat.h +++ b/src/mapgen_flat.h @@ -78,7 +78,6 @@ public: virtual void makeChunk(BlockMakeData *data); int getSpawnLevelAtPoint(v2s16 p); - void calculateNoise(); s16 generateTerrain(); }; diff --git a/src/mapgen_fractal.cpp b/src/mapgen_fractal.cpp index dca10d253..f54de6275 100644 --- a/src/mapgen_fractal.cpp +++ b/src/mapgen_fractal.cpp @@ -243,9 +243,6 @@ void MapgenFractal::makeChunk(BlockMakeData *data) blockseed = getBlockSeed2(full_node_min, seed); - // Make some noise - calculateNoise(); - // Generate base terrain, mountains, and ridges with initial heightmaps s16 stone_surface_max_y = generateTerrain(); @@ -328,23 +325,6 @@ void MapgenFractal::makeChunk(BlockMakeData *data) } -void MapgenFractal::calculateNoise() -{ - //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO); - s16 x = node_min.X; - s16 z = node_min.Z; - - noise_seabed->perlinMap2D(x, z); - - // Cave noises are calculated in generateCaves() - // only if solid terrain is present in mapchunk - - noise_filler_depth->perlinMap2D(x, z); - - //printf("calculateNoise: %dus\n", t.stop()); -} - - bool MapgenFractal::getFractalAtPoint(s16 x, s16 y, s16 z) { float cx, cy, cz, cw, ox, oy, oz, ow; @@ -474,6 +454,8 @@ s16 MapgenFractal::generateTerrain() s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT; u32 index2d = 0; + noise_seabed->perlinMap2D(node_min.X, node_min.Z); + for (s16 z = node_min.Z; z <= node_max.Z; z++) { for (s16 y = node_min.Y - 1; y <= node_max.Y + 1; y++) { u32 vi = vm->m_area.index(node_min.X, y, z); diff --git a/src/mapgen_fractal.h b/src/mapgen_fractal.h index 176885d46..e30550405 100644 --- a/src/mapgen_fractal.h +++ b/src/mapgen_fractal.h @@ -88,7 +88,6 @@ public: virtual void makeChunk(BlockMakeData *data); int getSpawnLevelAtPoint(v2s16 p); - void calculateNoise(); bool getFractalAtPoint(s16 x, s16 y, s16 z); s16 generateTerrain(); }; diff --git a/src/mapgen_v5.cpp b/src/mapgen_v5.cpp index 0c063dc6f..85df34353 100644 --- a/src/mapgen_v5.cpp +++ b/src/mapgen_v5.cpp @@ -225,9 +225,6 @@ void MapgenV5::makeChunk(BlockMakeData *data) // Create a block-specific seed blockseed = getBlockSeed2(full_node_min, seed); - // Make some noise - calculateNoise(); - // Generate base terrain s16 stone_surface_max_y = generateBaseTerrain(); @@ -312,26 +309,6 @@ void MapgenV5::makeChunk(BlockMakeData *data) } -void MapgenV5::calculateNoise() -{ - //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO); - s16 x = node_min.X; - s16 y = node_min.Y - 1; - s16 z = node_min.Z; - - noise_factor->perlinMap2D(x, z); - noise_height->perlinMap2D(x, z); - noise_ground->perlinMap3D(x, y, z); - - // Cave noises are calculated in generateCaves() - // only if solid terrain is present in mapchunk - - noise_filler_depth->perlinMap2D(x, z); - - //printf("calculateNoise: %dus\n", t.stop()); -} - - //bool is_cave(u32 index) { // double d1 = contour(noise_cave1->result[index]); // double d2 = contour(noise_cave2->result[index]); @@ -355,6 +332,10 @@ int MapgenV5::generateBaseTerrain() u32 index2d = 0; int stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT; + noise_factor->perlinMap2D(node_min.X, node_min.Z); + noise_height->perlinMap2D(node_min.X, node_min.Z); + noise_ground->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z); + for (s16 z=node_min.Z; z<=node_max.Z; z++) { for (s16 y=node_min.Y - 1; y<=node_max.Y + 1; y++) { u32 vi = vm->m_area.index(node_min.X, y, z); diff --git a/src/mapgen_v5.h b/src/mapgen_v5.h index 99836b23e..dd5ca0a31 100644 --- a/src/mapgen_v5.h +++ b/src/mapgen_v5.h @@ -69,7 +69,6 @@ public: virtual void makeChunk(BlockMakeData *data); int getSpawnLevelAtPoint(v2s16 p); - void calculateNoise(); int generateBaseTerrain(); }; diff --git a/src/mapgen_v7.cpp b/src/mapgen_v7.cpp index ee2c5f45f..5c5f13b88 100644 --- a/src/mapgen_v7.cpp +++ b/src/mapgen_v7.cpp @@ -252,9 +252,6 @@ void MapgenV7::makeChunk(BlockMakeData *data) blockseed = getBlockSeed2(full_node_min, seed); - // Make some noise - calculateNoise(); - // Generate terrain and ridges with initial heightmaps s16 stone_surface_max_y = generateTerrain(); @@ -340,37 +337,6 @@ void MapgenV7::makeChunk(BlockMakeData *data) } -void MapgenV7::calculateNoise() -{ - //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO); - s16 x = node_min.X; - s16 y = node_min.Y - 1; - s16 z = node_min.Z; - - noise_terrain_persist->perlinMap2D(x, z); - float *persistmap = noise_terrain_persist->result; - - noise_terrain_base->perlinMap2D(x, z, persistmap); - noise_terrain_alt->perlinMap2D(x, z, persistmap); - noise_height_select->perlinMap2D(x, z); - - if (spflags & MGV7_MOUNTAINS) { - noise_mountain->perlinMap3D(x, y, z); - noise_mount_height->perlinMap2D(x, z); - } - - if ((spflags & MGV7_RIDGES) && node_max.Y >= water_level) { - noise_ridge->perlinMap3D(x, y, z); - noise_ridge_uwater->perlinMap2D(x, z); - } - - // Cave noises are calculated in generateCaves() - // only if solid terrain is present in mapchunk - - //printf("calculateNoise: %dus\n", t.stop()); -} - - float MapgenV7::baseTerrainLevelAtPoint(s16 x, s16 z) { float hselect = NoisePerlin2D(&noise_height_select->np, x, z, seed); @@ -430,10 +396,23 @@ int MapgenV7::generateTerrain() MapNode n_stone(c_stone); MapNode n_water(c_water_source); + //// Calculate noise for terrain generation + noise_terrain_persist->perlinMap2D(node_min.X, node_min.Z); + float *persistmap = noise_terrain_persist->result; + + noise_terrain_base->perlinMap2D(node_min.X, node_min.Z, persistmap); + noise_terrain_alt->perlinMap2D(node_min.X, node_min.Z, persistmap); + noise_height_select->perlinMap2D(node_min.X, node_min.Z); + + if (spflags & MGV7_MOUNTAINS) { + noise_mountain->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z); + noise_mount_height->perlinMap2D(node_min.X, node_min.Z); + } + + //// Place nodes v3s16 em = vm->m_area.getExtent(); s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT; u32 index2d = 0; - bool mountain_flag = spflags & MGV7_MOUNTAINS; for (s16 z = node_min.Z; z <= node_max.Z; z++) for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) { @@ -450,7 +429,7 @@ int MapgenV7::generateTerrain() if (vm->m_data[vi].getContent() == CONTENT_IGNORE) { if (y <= surface_y) { vm->m_data[vi] = n_stone; // Base terrain - } else if (mountain_flag && + } else if ((spflags & MGV7_MOUNTAINS) && getMountainTerrainFromMap(index3d, index2d, y)) { vm->m_data[vi] = n_stone; // Mountain terrain if (y > stone_surface_max_y) @@ -475,6 +454,9 @@ void MapgenV7::generateRidgeTerrain() if (node_max.Y < water_level - 16) return; + noise_ridge->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z); + noise_ridge_uwater->perlinMap2D(node_min.X, node_min.Z); + MapNode n_water(c_water_source); MapNode n_air(CONTENT_AIR); u32 index = 0; diff --git a/src/mapgen_v7.h b/src/mapgen_v7.h index eb89d5978..723f1217f 100644 --- a/src/mapgen_v7.h +++ b/src/mapgen_v7.h @@ -87,9 +87,6 @@ public: float baseTerrainLevelFromMap(int index); bool getMountainTerrainAtPoint(s16 x, s16 y, s16 z); bool getMountainTerrainFromMap(int idx_xyz, int idx_xz, s16 y); - - void calculateNoise(); - int generateTerrain(); void generateRidgeTerrain(); }; diff --git a/src/mapgen_valleys.cpp b/src/mapgen_valleys.cpp index 699554ecd..3a4f984f9 100644 --- a/src/mapgen_valleys.cpp +++ b/src/mapgen_valleys.cpp @@ -388,7 +388,6 @@ void MapgenValleys::calculateNoise() //TimeTaker tcn("actualNoise"); - noise_filler_depth->perlinMap2D(x, z); noise_inter_valley_slope->perlinMap2D(x, z); noise_rivers->perlinMap2D(x, z); noise_terrain_height->perlinMap2D(x, z); From 081090176666e849d01f9a02b15279bdf72570bc Mon Sep 17 00:00:00 2001 From: kwolekr Date: Sun, 22 May 2016 02:17:19 -0400 Subject: [PATCH 032/418] Mapgen: Deduplicate common constructor code --- src/mapgen.cpp | 55 ++++++++++++++++++++++++++++++++++++++++-- src/mapgen.h | 22 +++++++++++------ src/mapgen_flat.cpp | 30 ++--------------------- src/mapgen_flat.h | 19 ++++++--------- src/mapgen_fractal.cpp | 30 ++--------------------- src/mapgen_fractal.h | 21 +++++++--------- src/mapgen_v5.cpp | 30 ++--------------------- src/mapgen_v5.h | 25 +++++++++---------- src/mapgen_v7.cpp | 33 ++----------------------- src/mapgen_v7.h | 37 ++++++++++++---------------- src/mapgen_valleys.cpp | 42 ++++++-------------------------- src/mapgen_valleys.h | 5 ---- 12 files changed, 128 insertions(+), 221 deletions(-) diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 009a81f0c..a2d207bff 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -377,9 +377,60 @@ void Mapgen::spreadLight(v3s16 nmin, v3s16 nmax) MapgenBasic::MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emerge) : Mapgen(mapgenid, params, emerge) { + this->m_emerge = emerge; + this->m_bmgr = emerge->biomemgr; + //// Here, 'stride' refers to the number of elements needed to skip to index + //// an adjacent element for that coordinate in noise/height/biome maps + //// (*not* vmanip content map!) + + // Note there is no X stride explicitly defined. Items adjacent in the X + // coordinate are assumed to be adjacent in memory as well (i.e. stride of 1). + + // Number of elements to skip to get to the next Y coordinate + this->ystride = csize.X; + + // Number of elements to skip to get to the next Z coordinate + this->zstride = csize.X * csize.Y; + + // Z-stride value for maps oversized for 1-down overgeneration + this->zstride_1d = csize.X * (csize.Y + 1); + + // Z-stride value for maps oversized for 1-up 1-down overgeneration + this->zstride_1u1d = csize.X * (csize.Y + 2); + + //// Allocate heightmap + this->heightmap = new s16[csize.X * csize.Z]; + + //// Initialize biome generator + // TODO(hmmmm): should we have a way to disable biomemanager biomes? + biomegen = m_bmgr->createBiomeGen(BIOMEGEN_ORIGINAL, params->bparams, csize); + biomemap = biomegen->biomemap; + + //// Look up some commonly used content + c_stone = ndef->getId("mapgen_stone"); + c_water_source = ndef->getId("mapgen_water_source"); + c_desert_stone = ndef->getId("mapgen_desert_stone"); + c_sandstone = ndef->getId("mapgen_sandstone"); + c_river_water_source = ndef->getId("mapgen_river_water_source"); + + //// Fall back to more basic content if not defined + if (c_desert_stone == CONTENT_IGNORE) + c_desert_stone = c_stone; + if (c_sandstone == CONTENT_IGNORE) + c_sandstone = c_stone; + if (c_river_water_source == CONTENT_IGNORE) + c_river_water_source = c_water_source; } + +MapgenBasic::~MapgenBasic() +{ + delete biomegen; + delete []heightmap; +} + + MgStoneType MapgenBasic::generateBiomes() { v3s16 em = vm->m_area.getExtent(); @@ -499,7 +550,7 @@ void MapgenBasic::dustTopNodes() for (s16 z = node_min.Z; z <= node_max.Z; z++) for (s16 x = node_min.X; x <= node_max.X; x++, index++) { - Biome *biome = (Biome *)bmgr->getRaw(biomemap[index]); + Biome *biome = (Biome *)m_bmgr->getRaw(biomemap[index]); if (biome->c_dust == CONTENT_IGNORE) continue; @@ -544,7 +595,7 @@ void MapgenBasic::generateCaves(s16 max_stone_y, s16 large_cave_depth) if (max_stone_y < node_min.Y) return; - CavesNoiseIntersection caves_noise(ndef, bmgr, csize, + CavesNoiseIntersection caves_noise(ndef, m_bmgr, csize, &np_cave1, &np_cave2, seed, cave_width); caves_noise.generateCaves(vm, node_min, node_max, biomemap); diff --git a/src/mapgen.h b/src/mapgen.h index 8db01f5c1..0b552e508 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -216,8 +216,16 @@ private: */ class MapgenBasic : public Mapgen { public: + MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emerge); + virtual ~MapgenBasic(); + + virtual MgStoneType generateBiomes(); + virtual void dustTopNodes(); + virtual void generateCaves(s16 max_stone_y, s16 large_cave_depth); + +protected: EmergeManager *m_emerge; - BiomeManager *bmgr; + BiomeManager *m_bmgr; Noise *noise_filler_depth; @@ -233,15 +241,15 @@ public: content_t c_sandstone; int ystride; + int zstride; + int zstride_1d; + int zstride_1u1d; + + u32 spflags; + NoiseParams np_cave1; NoiseParams np_cave2; float cave_width; - - MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emerge); - - virtual MgStoneType generateBiomes(); - virtual void dustTopNodes(); - virtual void generateCaves(s16 max_stone_y, s16 large_cave_depth); }; struct MapgenFactory { diff --git a/src/mapgen_flat.cpp b/src/mapgen_flat.cpp index ded859f65..cc43684c4 100644 --- a/src/mapgen_flat.cpp +++ b/src/mapgen_flat.cpp @@ -52,15 +52,6 @@ FlagDesc flagdesc_mapgen_flat[] = { MapgenFlat::MapgenFlat(int mapgenid, MapgenParams *params, EmergeManager *emerge) : MapgenBasic(mapgenid, params, emerge) { - this->m_emerge = emerge; - this->bmgr = emerge->biomemgr; - - //// amount of elements to skip for the next index - //// for noise/height/biome maps (not vmanip) - this->ystride = csize.X; - - this->heightmap = new s16[csize.X * csize.Z]; - MapgenFlatParams *sp = (MapgenFlatParams *)params->sparams; this->spflags = sp->spflags; @@ -79,27 +70,14 @@ MapgenFlat::MapgenFlat(int mapgenid, MapgenParams *params, EmergeManager *emerge MapgenBasic::np_cave1 = sp->np_cave1; MapgenBasic::np_cave2 = sp->np_cave2; - //// Initialize biome generator - biomegen = emerge->biomemgr->createBiomeGen( - BIOMEGEN_ORIGINAL, params->bparams, csize); - biomemap = biomegen->biomemap; - - //// Resolve nodes to be used - c_stone = ndef->getId("mapgen_stone"); - c_water_source = ndef->getId("mapgen_water_source"); - c_lava_source = ndef->getId("mapgen_lava_source"); - c_desert_stone = ndef->getId("mapgen_desert_stone"); - c_ice = ndef->getId("mapgen_ice"); - c_sandstone = ndef->getId("mapgen_sandstone"); - + // Content used for dungeon generation c_cobble = ndef->getId("mapgen_cobble"); c_stair_cobble = ndef->getId("mapgen_stair_cobble"); c_mossycobble = ndef->getId("mapgen_mossycobble"); c_sandstonebrick = ndef->getId("mapgen_sandstonebrick"); c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick"); - if (c_ice == CONTENT_IGNORE) - c_ice = CONTENT_AIR; + // Fall back to more basic content if not defined if (c_mossycobble == CONTENT_IGNORE) c_mossycobble = c_cobble; if (c_stair_cobble == CONTENT_IGNORE) @@ -115,10 +93,6 @@ MapgenFlat::~MapgenFlat() { delete noise_terrain; delete noise_filler_depth; - - delete biomegen; - - delete[] heightmap; } diff --git a/src/mapgen_flat.h b/src/mapgen_flat.h index 39da6e025..ffec4b924 100644 --- a/src/mapgen_flat.h +++ b/src/mapgen_flat.h @@ -55,7 +55,14 @@ struct MapgenFlatParams : public MapgenSpecificParams { class MapgenFlat : public MapgenBasic { public: - u32 spflags; + MapgenFlat(int mapgenid, MapgenParams *params, EmergeManager *emerge); + ~MapgenFlat(); + + virtual void makeChunk(BlockMakeData *data); + int getSpawnLevelAtPoint(v2s16 p); + s16 generateTerrain(); + +private: s16 ground_level; s16 large_cave_depth; float lake_threshold; @@ -64,21 +71,11 @@ public: float hill_steepness; Noise *noise_terrain; - content_t c_lava_source; - content_t c_ice; - content_t c_cobble; content_t c_stair_cobble; content_t c_mossycobble; content_t c_sandstonebrick; content_t c_stair_sandstonebrick; - - MapgenFlat(int mapgenid, MapgenParams *params, EmergeManager *emerge); - ~MapgenFlat(); - - virtual void makeChunk(BlockMakeData *data); - int getSpawnLevelAtPoint(v2s16 p); - s16 generateTerrain(); }; struct MapgenFactoryFlat : public MapgenFactory { diff --git a/src/mapgen_fractal.cpp b/src/mapgen_fractal.cpp index f54de6275..50e10d128 100644 --- a/src/mapgen_fractal.cpp +++ b/src/mapgen_fractal.cpp @@ -50,15 +50,6 @@ FlagDesc flagdesc_mapgen_fractal[] = { MapgenFractal::MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager *emerge) : MapgenBasic(mapgenid, params, emerge) { - this->m_emerge = emerge; - this->bmgr = emerge->biomemgr; - - //// amount of elements to skip for the next index - //// for noise/height/biome maps (not vmanip) - this->ystride = csize.X; - - this->heightmap = new s16[csize.X * csize.Z]; - MapgenFractalParams *sp = (MapgenFractalParams *)params->sparams; this->spflags = sp->spflags; @@ -80,30 +71,17 @@ MapgenFractal::MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager * MapgenBasic::np_cave1 = sp->np_cave1; MapgenBasic::np_cave2 = sp->np_cave2; - //// Initialize biome generator - biomegen = emerge->biomemgr->createBiomeGen( - BIOMEGEN_ORIGINAL, params->bparams, csize); - biomemap = biomegen->biomemap; - this->formula = fractal / 2 + fractal % 2; this->julia = fractal % 2 == 0; - //// Resolve nodes to be used - c_stone = ndef->getId("mapgen_stone"); - c_water_source = ndef->getId("mapgen_water_source"); - c_lava_source = ndef->getId("mapgen_lava_source"); - c_desert_stone = ndef->getId("mapgen_desert_stone"); - c_ice = ndef->getId("mapgen_ice"); - c_sandstone = ndef->getId("mapgen_sandstone"); - + // Content used for dungeon generation c_cobble = ndef->getId("mapgen_cobble"); c_stair_cobble = ndef->getId("mapgen_stair_cobble"); c_mossycobble = ndef->getId("mapgen_mossycobble"); c_sandstonebrick = ndef->getId("mapgen_sandstonebrick"); c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick"); - if (c_ice == CONTENT_IGNORE) - c_ice = CONTENT_AIR; + // Fall back to more basic content if not defined if (c_mossycobble == CONTENT_IGNORE) c_mossycobble = c_cobble; if (c_stair_cobble == CONTENT_IGNORE) @@ -119,10 +97,6 @@ MapgenFractal::~MapgenFractal() { delete noise_seabed; delete noise_filler_depth; - - delete biomegen; - - delete[] heightmap; } diff --git a/src/mapgen_fractal.h b/src/mapgen_fractal.h index e30550405..de0ccaa2f 100644 --- a/src/mapgen_fractal.h +++ b/src/mapgen_fractal.h @@ -59,10 +59,18 @@ struct MapgenFractalParams : public MapgenSpecificParams { class MapgenFractal : public MapgenBasic { public: + MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager *emerge); + ~MapgenFractal(); + + virtual void makeChunk(BlockMakeData *data); + int getSpawnLevelAtPoint(v2s16 p); + bool getFractalAtPoint(s16 x, s16 y, s16 z); + s16 generateTerrain(); + +private: u16 formula; bool julia; - u32 spflags; u16 fractal; u16 iterations; v3f scale; @@ -74,22 +82,11 @@ public: float julia_w; Noise *noise_seabed; - content_t c_lava_source; - content_t c_ice; - content_t c_cobble; content_t c_stair_cobble; content_t c_mossycobble; content_t c_sandstonebrick; content_t c_stair_sandstonebrick; - - MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager *emerge); - ~MapgenFractal(); - - virtual void makeChunk(BlockMakeData *data); - int getSpawnLevelAtPoint(v2s16 p); - bool getFractalAtPoint(s16 x, s16 y, s16 z); - s16 generateTerrain(); }; struct MapgenFactoryFractal : public MapgenFactory { diff --git a/src/mapgen_v5.cpp b/src/mapgen_v5.cpp index 85df34353..167a38282 100644 --- a/src/mapgen_v5.cpp +++ b/src/mapgen_v5.cpp @@ -48,15 +48,6 @@ FlagDesc flagdesc_mapgen_v5[] = { MapgenV5::MapgenV5(int mapgenid, MapgenParams *params, EmergeManager *emerge) : MapgenBasic(mapgenid, params, emerge) { - this->m_emerge = emerge; - this->bmgr = emerge->biomemgr; - - // amount of elements to skip for the next index - // for noise/height/biome maps (not vmanip) - this->ystride = csize.X; - - this->heightmap = new s16[csize.X * csize.Z]; - MapgenV5Params *sp = (MapgenV5Params *)params->sparams; this->spflags = sp->spflags; @@ -74,27 +65,14 @@ MapgenV5::MapgenV5(int mapgenid, MapgenParams *params, EmergeManager *emerge) MapgenBasic::np_cave1 = sp->np_cave1; MapgenBasic::np_cave2 = sp->np_cave2; - //// Initialize biome generator - biomegen = emerge->biomemgr->createBiomeGen( - BIOMEGEN_ORIGINAL, params->bparams, csize); - biomemap = biomegen->biomemap; - - //// Resolve nodes to be used - c_stone = ndef->getId("mapgen_stone"); - c_water_source = ndef->getId("mapgen_water_source"); - c_lava_source = ndef->getId("mapgen_lava_source"); - c_desert_stone = ndef->getId("mapgen_desert_stone"); - c_ice = ndef->getId("mapgen_ice"); - c_sandstone = ndef->getId("mapgen_sandstone"); - + // Content used for dungeon generation c_cobble = ndef->getId("mapgen_cobble"); c_stair_cobble = ndef->getId("mapgen_stair_cobble"); c_mossycobble = ndef->getId("mapgen_mossycobble"); c_sandstonebrick = ndef->getId("mapgen_sandstonebrick"); c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick"); - if (c_ice == CONTENT_IGNORE) - c_ice = CONTENT_AIR; + // Fall back to more basic content if not defined if (c_mossycobble == CONTENT_IGNORE) c_mossycobble = c_cobble; if (c_stair_cobble == CONTENT_IGNORE) @@ -112,10 +90,6 @@ MapgenV5::~MapgenV5() delete noise_factor; delete noise_height; delete noise_ground; - - delete biomegen; - - delete[] heightmap; } diff --git a/src/mapgen_v5.h b/src/mapgen_v5.h index dd5ca0a31..e7e5b2327 100644 --- a/src/mapgen_v5.h +++ b/src/mapgen_v5.h @@ -50,26 +50,23 @@ struct MapgenV5Params : public MapgenSpecificParams { class MapgenV5 : public MapgenBasic { public: - u32 spflags; - Noise *noise_factor; - Noise *noise_height; - Noise *noise_ground; - - content_t c_lava_source; - content_t c_ice; - - content_t c_cobble; - content_t c_stair_cobble; - content_t c_mossycobble; - content_t c_sandstonebrick; - content_t c_stair_sandstonebrick; - MapgenV5(int mapgenid, MapgenParams *params, EmergeManager *emerge); ~MapgenV5(); virtual void makeChunk(BlockMakeData *data); int getSpawnLevelAtPoint(v2s16 p); int generateBaseTerrain(); + +private: + Noise *noise_factor; + Noise *noise_height; + Noise *noise_ground; + + content_t c_cobble; + content_t c_stair_cobble; + content_t c_mossycobble; + content_t c_sandstonebrick; + content_t c_stair_sandstonebrick; }; diff --git a/src/mapgen_v7.cpp b/src/mapgen_v7.cpp index 5c5f13b88..29c81e8a7 100644 --- a/src/mapgen_v7.cpp +++ b/src/mapgen_v7.cpp @@ -53,17 +53,6 @@ FlagDesc flagdesc_mapgen_v7[] = { MapgenV7::MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge) : MapgenBasic(mapgenid, params, emerge) { - this->m_emerge = emerge; - this->bmgr = emerge->biomemgr; - - //// amount of elements to skip for the next index - //// for noise/height/biome maps (not vmanip) - this->ystride = csize.X; - // 1-up 1-down overgeneration - this->zstride_1u1d = csize.X * (csize.Y + 2); - - this->heightmap = new s16[csize.X * csize.Z]; - MapgenV7Params *sp = (MapgenV7Params *)params->sparams; this->spflags = sp->spflags; @@ -86,28 +75,14 @@ MapgenV7::MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge) MapgenBasic::np_cave1 = sp->np_cave1; MapgenBasic::np_cave2 = sp->np_cave2; - // TODO(hmmmm): should we have a way to disable biomemanager biomes? - //// Initialize biome generator - biomegen = emerge->biomemgr->createBiomeGen( - BIOMEGEN_ORIGINAL, params->bparams, csize); - biomemap = biomegen->biomemap; - - //// Resolve nodes to be used - c_stone = ndef->getId("mapgen_stone"); - c_water_source = ndef->getId("mapgen_water_source"); - c_lava_source = ndef->getId("mapgen_lava_source"); - c_desert_stone = ndef->getId("mapgen_desert_stone"); - c_ice = ndef->getId("mapgen_ice"); - c_sandstone = ndef->getId("mapgen_sandstone"); - + // Content used for dungeon generation c_cobble = ndef->getId("mapgen_cobble"); c_stair_cobble = ndef->getId("mapgen_stair_cobble"); c_mossycobble = ndef->getId("mapgen_mossycobble"); c_sandstonebrick = ndef->getId("mapgen_sandstonebrick"); c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick"); - if (c_ice == CONTENT_IGNORE) - c_ice = CONTENT_AIR; + // Fall back to more basic content if not defined if (c_mossycobble == CONTENT_IGNORE) c_mossycobble = c_cobble; if (c_stair_cobble == CONTENT_IGNORE) @@ -130,10 +105,6 @@ MapgenV7::~MapgenV7() delete noise_ridge_uwater; delete noise_mountain; delete noise_ridge; - - delete biomegen; - - delete[] heightmap; } diff --git a/src/mapgen_v7.h b/src/mapgen_v7.h index 723f1217f..aee171b48 100644 --- a/src/mapgen_v7.h +++ b/src/mapgen_v7.h @@ -56,27 +56,6 @@ struct MapgenV7Params : public MapgenSpecificParams { class MapgenV7 : public MapgenBasic { public: - int zstride_1u1d; - - u32 spflags; - Noise *noise_terrain_base; - Noise *noise_terrain_alt; - Noise *noise_terrain_persist; - Noise *noise_height_select; - Noise *noise_mount_height; - Noise *noise_ridge_uwater; - Noise *noise_mountain; - Noise *noise_ridge; - - content_t c_lava_source; - content_t c_ice; - - content_t c_cobble; - content_t c_stair_cobble; - content_t c_mossycobble; - content_t c_sandstonebrick; - content_t c_stair_sandstonebrick; - MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge); ~MapgenV7(); @@ -89,6 +68,22 @@ public: bool getMountainTerrainFromMap(int idx_xyz, int idx_xz, s16 y); int generateTerrain(); void generateRidgeTerrain(); + +private: + Noise *noise_terrain_base; + Noise *noise_terrain_alt; + Noise *noise_terrain_persist; + Noise *noise_height_select; + Noise *noise_mount_height; + Noise *noise_ridge_uwater; + Noise *noise_mountain; + Noise *noise_ridge; + + content_t c_cobble; + content_t c_stair_cobble; + content_t c_mossycobble; + content_t c_sandstonebrick; + content_t c_stair_sandstonebrick; }; struct MapgenFactoryV7 : public MapgenFactory { diff --git a/src/mapgen_valleys.cpp b/src/mapgen_valleys.cpp index 3a4f984f9..c0094eb3b 100644 --- a/src/mapgen_valleys.cpp +++ b/src/mapgen_valleys.cpp @@ -67,17 +67,8 @@ static FlagDesc flagdesc_mapgen_valleys[] = { MapgenValleys::MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager *emerge) : MapgenBasic(mapgenid, params, emerge) { - this->m_emerge = emerge; - this->bmgr = emerge->biomemgr; - - //// amount of elements to skip for the next index - //// for noise/height/biome maps (not vmanip) - this->ystride = csize.X; - this->zstride = csize.X * (csize.Y + 2); - // 1-down overgeneration - this->zstride_1d = csize.X * (csize.Y + 1); - - this->heightmap = new s16[csize.X * csize.Z]; + // NOTE: MapgenValleys has a hard dependency on BiomeGenOriginal + this->m_bgen = (BiomeGenOriginal *)biomegen; this->map_gen_limit = MYMIN(MAX_MAP_GENERATION_LIMIT, g_settings->getU16("map_generation_limit")); @@ -111,13 +102,6 @@ MapgenValleys::MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager * noise_cave2 = new Noise(&sp->np_cave2, seed, csize.X, csize.Y + 1, csize.Z); noise_massive_caves = new Noise(&sp->np_massive_caves, seed, csize.X, csize.Y + 1, csize.Z); - //// Initialize biome generator - // NOTE: valleys mapgen can only use BiomeGenOriginal - biomegen = emerge->biomemgr->createBiomeGen( - BIOMEGEN_ORIGINAL, params->bparams, csize); - biomemap = biomegen->biomemap; - m_bgen = (BiomeGenOriginal *)biomegen; - this->humid_rivers = (spflags & MGVALLEYS_HUMID_RIVERS); this->use_altitude_chill = (spflags & MGVALLEYS_ALT_CHILL); this->humidity_adjust = bp->np_humidity.offset - 50.f; @@ -128,25 +112,18 @@ MapgenValleys::MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager * tcave_cache = new float[csize.Y + 2]; - //// Resolve nodes to be used + // Resolve content to be used c_cobble = ndef->getId("mapgen_cobble"); - c_desert_stone = ndef->getId("mapgen_desert_stone"); - c_dirt = ndef->getId("mapgen_dirt"); c_lava_source = ndef->getId("mapgen_lava_source"); c_mossycobble = ndef->getId("mapgen_mossycobble"); - c_river_water_source = ndef->getId("mapgen_river_water_source"); c_sand = ndef->getId("mapgen_sand"); c_sandstonebrick = ndef->getId("mapgen_sandstonebrick"); - c_sandstone = ndef->getId("mapgen_sandstone"); c_stair_cobble = ndef->getId("mapgen_stair_cobble"); c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick"); - c_stone = ndef->getId("mapgen_stone"); - c_water_source = ndef->getId("mapgen_water_source"); + // Fall back to more basic content if not defined if (c_mossycobble == CONTENT_IGNORE) c_mossycobble = c_cobble; - if (c_river_water_source == CONTENT_IGNORE) - c_river_water_source = c_water_source; if (c_sand == CONTENT_IGNORE) c_sand = c_stone; if (c_sandstonebrick == CONTENT_IGNORE) @@ -171,9 +148,6 @@ MapgenValleys::~MapgenValleys() delete noise_valley_depth; delete noise_valley_profile; - delete biomegen; - - delete[] heightmap; delete[] tcave_cache; } @@ -289,13 +263,13 @@ void MapgenValleys::makeChunk(BlockMakeData *data) // Generate biome noises. Note this must be executed strictly before // generateTerrain, because generateTerrain depends on intermediate // biome-related noises. - biomegen->calcBiomeNoise(node_min); + m_bgen->calcBiomeNoise(node_min); // Generate base terrain with initial heightmaps s16 stone_surface_max_y = generateTerrain(); // Build biomemap - biomegen->getBiomes(heightmap); + m_bgen->getBiomes(heightmap); // Place biome-specific nodes MgStoneType stone_type = generateBiomes(); @@ -606,7 +580,7 @@ int MapgenValleys::generateTerrain() } } - u32 index_3d = (z - node_min.Z) * zstride + (x - node_min.X); + u32 index_3d = (z - node_min.Z) * zstride_1u1d + (x - node_min.X); u32 index_data = vm->m_area.index(x, node_min.Y - 1, z); // Mapgens concern themselves with stone and water. @@ -755,7 +729,7 @@ void MapgenValleys::generateCaves(s16 max_stone_y, s16 large_cave_depth) u32 index_2d = 0; for (s16 z = node_min.Z; z <= node_max.Z; z++) for (s16 x = node_min.X; x <= node_max.X; x++, index_2d++) { - Biome *biome = (Biome *)bmgr->getRaw(biomemap[index_2d]); + Biome *biome = (Biome *)m_bmgr->getRaw(biomemap[index_2d]); bool tunnel_air_above = false; bool underground = false; u32 index_data = vm->m_area.index(x, node_max.Y, z); diff --git a/src/mapgen_valleys.h b/src/mapgen_valleys.h index 4f87be6b6..bab3a756e 100644 --- a/src/mapgen_valleys.h +++ b/src/mapgen_valleys.h @@ -99,9 +99,6 @@ public: private: BiomeGenOriginal *m_bgen; - int zstride; - int zstride_1d; - float map_gen_limit; bool humid_rivers; @@ -110,7 +107,6 @@ private: s16 cave_water_max_height; s16 lava_max_height; - u32 spflags; float altitude_chill; s16 lava_features_lim; s16 massive_cave_depth; @@ -129,7 +125,6 @@ private: Noise *noise_valley_profile; content_t c_cobble; - content_t c_dirt; content_t c_lava_source; content_t c_mossycobble; content_t c_sand; From fd0efb21c3d40a963a813d73a09bb80df625b247 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Sun, 22 May 2016 16:27:31 -0400 Subject: [PATCH 033/418] Mapgen: Combine dungeon generation code --- src/mapgen.cpp | 73 +++++++++++++++++++++++++++++++++++++++++- src/mapgen.h | 11 ++++++- src/mapgen_flat.cpp | 61 ++--------------------------------- src/mapgen_flat.h | 6 ---- src/mapgen_fractal.cpp | 61 ++--------------------------------- src/mapgen_fractal.h | 6 ---- src/mapgen_v5.cpp | 61 ++--------------------------------- src/mapgen_v5.h | 6 ---- src/mapgen_v7.cpp | 61 ++--------------------------------- src/mapgen_v7.h | 6 ---- src/mapgen_valleys.cpp | 61 +++-------------------------------- src/mapgen_valleys.h | 5 --- 12 files changed, 94 insertions(+), 324 deletions(-) diff --git a/src/mapgen.cpp b/src/mapgen.cpp index a2d207bff..56fe34e16 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -40,6 +40,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "filesys.h" #include "log.h" #include "cavegen.h" +#include "dungeongen.h" FlagDesc flagdesc_mapgen[] = { {"trees", MG_TREES}, @@ -414,13 +415,30 @@ MapgenBasic::MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emer c_sandstone = ndef->getId("mapgen_sandstone"); c_river_water_source = ndef->getId("mapgen_river_water_source"); - //// Fall back to more basic content if not defined + // Fall back to more basic content if not defined if (c_desert_stone == CONTENT_IGNORE) c_desert_stone = c_stone; if (c_sandstone == CONTENT_IGNORE) c_sandstone = c_stone; if (c_river_water_source == CONTENT_IGNORE) c_river_water_source = c_water_source; + + //// Content used for dungeon generation + c_cobble = ndef->getId("mapgen_cobble"); + c_stair_cobble = ndef->getId("mapgen_stair_cobble"); + c_mossycobble = ndef->getId("mapgen_mossycobble"); + c_sandstonebrick = ndef->getId("mapgen_sandstonebrick"); + c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick"); + + // Fall back to more basic content if not defined + if (c_mossycobble == CONTENT_IGNORE) + c_mossycobble = c_cobble; + if (c_stair_cobble == CONTENT_IGNORE) + c_stair_cobble = c_cobble; + if (c_sandstonebrick == CONTENT_IGNORE) + c_sandstonebrick = c_sandstone; + if (c_stair_sandstonebrick == CONTENT_IGNORE) + c_stair_sandstonebrick = c_sandstone; } @@ -614,6 +632,59 @@ void MapgenBasic::generateCaves(s16 max_stone_y, s16 large_cave_depth) } +void MapgenBasic::generateDungeons(s16 max_stone_y, MgStoneType stone_type) +{ + if (max_stone_y < node_min.Y) + return; + + DungeonParams dp; + + dp.np_rarity = nparams_dungeon_rarity; + dp.np_density = nparams_dungeon_density; + dp.np_wetness = nparams_dungeon_wetness; + dp.c_water = c_water_source; + switch (stone_type) { + default: + case MGSTONE_STONE: + dp.c_cobble = c_cobble; + dp.c_moss = c_mossycobble; + dp.c_stair = c_stair_cobble; + + dp.diagonal_dirs = false; + dp.mossratio = 3.0; + dp.holesize = v3s16(1, 2, 1); + dp.roomsize = v3s16(0, 0, 0); + dp.notifytype = GENNOTIFY_DUNGEON; + break; + case MGSTONE_DESERT_STONE: + dp.c_cobble = c_desert_stone; + dp.c_moss = c_desert_stone; + dp.c_stair = c_desert_stone; + + dp.diagonal_dirs = true; + dp.mossratio = 0.0; + dp.holesize = v3s16(2, 3, 2); + dp.roomsize = v3s16(2, 5, 2); + dp.notifytype = GENNOTIFY_TEMPLE; + break; + case MGSTONE_SANDSTONE: + dp.c_cobble = c_sandstonebrick; + dp.c_moss = c_sandstonebrick; + dp.c_stair = c_sandstonebrick; + + dp.diagonal_dirs = false; + dp.mossratio = 0.0; + dp.holesize = v3s16(2, 2, 2); + dp.roomsize = v3s16(2, 0, 2); + dp.notifytype = GENNOTIFY_DUNGEON; + break; + } + + DungeonGen dgen(this, &dp); + dgen.generate(blockseed, full_node_min, full_node_max); +} + + //// //// GenerateNotifier //// diff --git a/src/mapgen.h b/src/mapgen.h index 0b552e508..10595fafc 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -219,9 +219,10 @@ public: MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emerge); virtual ~MapgenBasic(); + virtual void generateCaves(s16 max_stone_y, s16 large_cave_depth); + virtual void generateDungeons(s16 max_stone_y, MgStoneType stone_type); virtual MgStoneType generateBiomes(); virtual void dustTopNodes(); - virtual void generateCaves(s16 max_stone_y, s16 large_cave_depth); protected: EmergeManager *m_emerge; @@ -234,12 +235,20 @@ protected: v3s16 full_node_min; v3s16 full_node_max; + // Content required for generateBiomes content_t c_stone; content_t c_water_source; content_t c_river_water_source; content_t c_desert_stone; content_t c_sandstone; + // Content required for generateDungeons + content_t c_cobble; + content_t c_stair_cobble; + content_t c_mossycobble; + content_t c_sandstonebrick; + content_t c_stair_sandstonebrick; + int ystride; int zstride; int zstride_1d; diff --git a/src/mapgen_flat.cpp b/src/mapgen_flat.cpp index cc43684c4..724f06825 100644 --- a/src/mapgen_flat.cpp +++ b/src/mapgen_flat.cpp @@ -69,23 +69,6 @@ MapgenFlat::MapgenFlat(int mapgenid, MapgenParams *params, EmergeManager *emerge MapgenBasic::np_cave1 = sp->np_cave1; MapgenBasic::np_cave2 = sp->np_cave2; - - // Content used for dungeon generation - c_cobble = ndef->getId("mapgen_cobble"); - c_stair_cobble = ndef->getId("mapgen_stair_cobble"); - c_mossycobble = ndef->getId("mapgen_mossycobble"); - c_sandstonebrick = ndef->getId("mapgen_sandstonebrick"); - c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick"); - - // Fall back to more basic content if not defined - if (c_mossycobble == CONTENT_IGNORE) - c_mossycobble = c_cobble; - if (c_stair_cobble == CONTENT_IGNORE) - c_stair_cobble = c_cobble; - if (c_sandstonebrick == CONTENT_IGNORE) - c_sandstonebrick = c_sandstone; - if (c_stair_sandstonebrick == CONTENT_IGNORE) - c_stair_sandstonebrick = c_sandstone; } @@ -215,48 +198,8 @@ void MapgenFlat::makeChunk(BlockMakeData *data) if (flags & MG_CAVES) generateCaves(stone_surface_max_y, large_cave_depth); - if ((flags & MG_DUNGEONS) && (stone_surface_max_y >= node_min.Y)) { - DungeonParams dp; - - dp.np_rarity = nparams_dungeon_rarity; - dp.np_density = nparams_dungeon_density; - dp.np_wetness = nparams_dungeon_wetness; - dp.c_water = c_water_source; - if (stone_type == MGSTONE_STONE) { - dp.c_cobble = c_cobble; - dp.c_moss = c_mossycobble; - dp.c_stair = c_stair_cobble; - - dp.diagonal_dirs = false; - dp.mossratio = 3.0; - dp.holesize = v3s16(1, 2, 1); - dp.roomsize = v3s16(0, 0, 0); - dp.notifytype = GENNOTIFY_DUNGEON; - } else if (stone_type == MGSTONE_DESERT_STONE) { - dp.c_cobble = c_desert_stone; - dp.c_moss = c_desert_stone; - dp.c_stair = c_desert_stone; - - dp.diagonal_dirs = true; - dp.mossratio = 0.0; - dp.holesize = v3s16(2, 3, 2); - dp.roomsize = v3s16(2, 5, 2); - dp.notifytype = GENNOTIFY_TEMPLE; - } else if (stone_type == MGSTONE_SANDSTONE) { - dp.c_cobble = c_sandstonebrick; - dp.c_moss = c_sandstonebrick; - dp.c_stair = c_sandstonebrick; - - dp.diagonal_dirs = false; - dp.mossratio = 0.0; - dp.holesize = v3s16(2, 2, 2); - dp.roomsize = v3s16(2, 0, 2); - dp.notifytype = GENNOTIFY_DUNGEON; - } - - DungeonGen dgen(this, &dp); - dgen.generate(blockseed, full_node_min, full_node_max); - } + if (flags & MG_DUNGEONS) + generateDungeons(stone_surface_max_y, stone_type); // Generate the registered decorations if (flags & MG_DECORATIONS) diff --git a/src/mapgen_flat.h b/src/mapgen_flat.h index ffec4b924..afe67b04e 100644 --- a/src/mapgen_flat.h +++ b/src/mapgen_flat.h @@ -70,12 +70,6 @@ private: float hill_threshold; float hill_steepness; Noise *noise_terrain; - - content_t c_cobble; - content_t c_stair_cobble; - content_t c_mossycobble; - content_t c_sandstonebrick; - content_t c_stair_sandstonebrick; }; struct MapgenFactoryFlat : public MapgenFactory { diff --git a/src/mapgen_fractal.cpp b/src/mapgen_fractal.cpp index 50e10d128..b613bf358 100644 --- a/src/mapgen_fractal.cpp +++ b/src/mapgen_fractal.cpp @@ -73,23 +73,6 @@ MapgenFractal::MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager * this->formula = fractal / 2 + fractal % 2; this->julia = fractal % 2 == 0; - - // Content used for dungeon generation - c_cobble = ndef->getId("mapgen_cobble"); - c_stair_cobble = ndef->getId("mapgen_stair_cobble"); - c_mossycobble = ndef->getId("mapgen_mossycobble"); - c_sandstonebrick = ndef->getId("mapgen_sandstonebrick"); - c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick"); - - // Fall back to more basic content if not defined - if (c_mossycobble == CONTENT_IGNORE) - c_mossycobble = c_cobble; - if (c_stair_cobble == CONTENT_IGNORE) - c_stair_cobble = c_cobble; - if (c_sandstonebrick == CONTENT_IGNORE) - c_sandstonebrick = c_sandstone; - if (c_stair_sandstonebrick == CONTENT_IGNORE) - c_stair_sandstonebrick = c_sandstone; } @@ -231,48 +214,8 @@ void MapgenFractal::makeChunk(BlockMakeData *data) if (flags & MG_CAVES) generateCaves(stone_surface_max_y, MGFRACTAL_LARGE_CAVE_DEPTH); - if ((flags & MG_DUNGEONS) && (stone_surface_max_y >= node_min.Y)) { - DungeonParams dp; - - dp.np_rarity = nparams_dungeon_rarity; - dp.np_density = nparams_dungeon_density; - dp.np_wetness = nparams_dungeon_wetness; - dp.c_water = c_water_source; - if (stone_type == MGSTONE_STONE) { - dp.c_cobble = c_cobble; - dp.c_moss = c_mossycobble; - dp.c_stair = c_stair_cobble; - - dp.diagonal_dirs = false; - dp.mossratio = 3.0; - dp.holesize = v3s16(1, 2, 1); - dp.roomsize = v3s16(0, 0, 0); - dp.notifytype = GENNOTIFY_DUNGEON; - } else if (stone_type == MGSTONE_DESERT_STONE) { - dp.c_cobble = c_desert_stone; - dp.c_moss = c_desert_stone; - dp.c_stair = c_desert_stone; - - dp.diagonal_dirs = true; - dp.mossratio = 0.0; - dp.holesize = v3s16(2, 3, 2); - dp.roomsize = v3s16(2, 5, 2); - dp.notifytype = GENNOTIFY_TEMPLE; - } else if (stone_type == MGSTONE_SANDSTONE) { - dp.c_cobble = c_sandstonebrick; - dp.c_moss = c_sandstonebrick; - dp.c_stair = c_sandstonebrick; - - dp.diagonal_dirs = false; - dp.mossratio = 0.0; - dp.holesize = v3s16(2, 2, 2); - dp.roomsize = v3s16(2, 0, 2); - dp.notifytype = GENNOTIFY_DUNGEON; - } - - DungeonGen dgen(this, &dp); - dgen.generate(blockseed, full_node_min, full_node_max); - } + if (flags & MG_DUNGEONS) + generateDungeons(stone_surface_max_y, stone_type); // Generate the registered decorations if (flags & MG_DECORATIONS) diff --git a/src/mapgen_fractal.h b/src/mapgen_fractal.h index de0ccaa2f..065142b84 100644 --- a/src/mapgen_fractal.h +++ b/src/mapgen_fractal.h @@ -81,12 +81,6 @@ private: float julia_z; float julia_w; Noise *noise_seabed; - - content_t c_cobble; - content_t c_stair_cobble; - content_t c_mossycobble; - content_t c_sandstonebrick; - content_t c_stair_sandstonebrick; }; struct MapgenFactoryFractal : public MapgenFactory { diff --git a/src/mapgen_v5.cpp b/src/mapgen_v5.cpp index 167a38282..3e10ec2a5 100644 --- a/src/mapgen_v5.cpp +++ b/src/mapgen_v5.cpp @@ -64,23 +64,6 @@ MapgenV5::MapgenV5(int mapgenid, MapgenParams *params, EmergeManager *emerge) MapgenBasic::np_cave1 = sp->np_cave1; MapgenBasic::np_cave2 = sp->np_cave2; - - // Content used for dungeon generation - c_cobble = ndef->getId("mapgen_cobble"); - c_stair_cobble = ndef->getId("mapgen_stair_cobble"); - c_mossycobble = ndef->getId("mapgen_mossycobble"); - c_sandstonebrick = ndef->getId("mapgen_sandstonebrick"); - c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick"); - - // Fall back to more basic content if not defined - if (c_mossycobble == CONTENT_IGNORE) - c_mossycobble = c_cobble; - if (c_stair_cobble == CONTENT_IGNORE) - c_stair_cobble = c_cobble; - if (c_sandstonebrick == CONTENT_IGNORE) - c_sandstonebrick = c_sandstone; - if (c_stair_sandstonebrick == CONTENT_IGNORE) - c_stair_sandstonebrick = c_sandstone; } @@ -215,48 +198,8 @@ void MapgenV5::makeChunk(BlockMakeData *data) generateCaves(stone_surface_max_y, MGV5_LARGE_CAVE_DEPTH); // Generate dungeons and desert temples - if ((flags & MG_DUNGEONS) && (stone_surface_max_y >= node_min.Y)) { - DungeonParams dp; - - dp.np_rarity = nparams_dungeon_rarity; - dp.np_density = nparams_dungeon_density; - dp.np_wetness = nparams_dungeon_wetness; - dp.c_water = c_water_source; - if (stone_type == MGSTONE_STONE) { - dp.c_cobble = c_cobble; - dp.c_moss = c_mossycobble; - dp.c_stair = c_stair_cobble; - - dp.diagonal_dirs = false; - dp.mossratio = 3.0; - dp.holesize = v3s16(1, 2, 1); - dp.roomsize = v3s16(0, 0, 0); - dp.notifytype = GENNOTIFY_DUNGEON; - } else if (stone_type == MGSTONE_DESERT_STONE) { - dp.c_cobble = c_desert_stone; - dp.c_moss = c_desert_stone; - dp.c_stair = c_desert_stone; - - dp.diagonal_dirs = true; - dp.mossratio = 0.0; - dp.holesize = v3s16(2, 3, 2); - dp.roomsize = v3s16(2, 5, 2); - dp.notifytype = GENNOTIFY_TEMPLE; - } else if (stone_type == MGSTONE_SANDSTONE) { - dp.c_cobble = c_sandstonebrick; - dp.c_moss = c_sandstonebrick; - dp.c_stair = c_sandstonebrick; - - dp.diagonal_dirs = false; - dp.mossratio = 0.0; - dp.holesize = v3s16(2, 2, 2); - dp.roomsize = v3s16(2, 0, 2); - dp.notifytype = GENNOTIFY_DUNGEON; - } - - DungeonGen dgen(this, &dp); - dgen.generate(blockseed, full_node_min, full_node_max); - } + if (flags & MG_DUNGEONS) + generateDungeons(stone_surface_max_y, stone_type); // Generate the registered decorations if (flags & MG_DECORATIONS) diff --git a/src/mapgen_v5.h b/src/mapgen_v5.h index e7e5b2327..4e1772a64 100644 --- a/src/mapgen_v5.h +++ b/src/mapgen_v5.h @@ -61,12 +61,6 @@ private: Noise *noise_factor; Noise *noise_height; Noise *noise_ground; - - content_t c_cobble; - content_t c_stair_cobble; - content_t c_mossycobble; - content_t c_sandstonebrick; - content_t c_stair_sandstonebrick; }; diff --git a/src/mapgen_v7.cpp b/src/mapgen_v7.cpp index 29c81e8a7..9ff2fbe66 100644 --- a/src/mapgen_v7.cpp +++ b/src/mapgen_v7.cpp @@ -74,23 +74,6 @@ MapgenV7::MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge) MapgenBasic::np_cave1 = sp->np_cave1; MapgenBasic::np_cave2 = sp->np_cave2; - - // Content used for dungeon generation - c_cobble = ndef->getId("mapgen_cobble"); - c_stair_cobble = ndef->getId("mapgen_stair_cobble"); - c_mossycobble = ndef->getId("mapgen_mossycobble"); - c_sandstonebrick = ndef->getId("mapgen_sandstonebrick"); - c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick"); - - // Fall back to more basic content if not defined - if (c_mossycobble == CONTENT_IGNORE) - c_mossycobble = c_cobble; - if (c_stair_cobble == CONTENT_IGNORE) - c_stair_cobble = c_cobble; - if (c_sandstonebrick == CONTENT_IGNORE) - c_sandstonebrick = c_sandstone; - if (c_stair_sandstonebrick == CONTENT_IGNORE) - c_stair_sandstonebrick = c_sandstone; } @@ -240,48 +223,8 @@ void MapgenV7::makeChunk(BlockMakeData *data) if (flags & MG_CAVES) generateCaves(stone_surface_max_y, water_level); - if ((flags & MG_DUNGEONS) && (stone_surface_max_y >= node_min.Y)) { - DungeonParams dp; - - dp.np_rarity = nparams_dungeon_rarity; - dp.np_density = nparams_dungeon_density; - dp.np_wetness = nparams_dungeon_wetness; - dp.c_water = c_water_source; - if (stone_type == MGSTONE_STONE) { - dp.c_cobble = c_cobble; - dp.c_moss = c_mossycobble; - dp.c_stair = c_stair_cobble; - - dp.diagonal_dirs = false; - dp.mossratio = 3.0; - dp.holesize = v3s16(1, 2, 1); - dp.roomsize = v3s16(0, 0, 0); - dp.notifytype = GENNOTIFY_DUNGEON; - } else if (stone_type == MGSTONE_DESERT_STONE) { - dp.c_cobble = c_desert_stone; - dp.c_moss = c_desert_stone; - dp.c_stair = c_desert_stone; - - dp.diagonal_dirs = true; - dp.mossratio = 0.0; - dp.holesize = v3s16(2, 3, 2); - dp.roomsize = v3s16(2, 5, 2); - dp.notifytype = GENNOTIFY_TEMPLE; - } else if (stone_type == MGSTONE_SANDSTONE) { - dp.c_cobble = c_sandstonebrick; - dp.c_moss = c_sandstonebrick; - dp.c_stair = c_sandstonebrick; - - dp.diagonal_dirs = false; - dp.mossratio = 0.0; - dp.holesize = v3s16(2, 2, 2); - dp.roomsize = v3s16(2, 0, 2); - dp.notifytype = GENNOTIFY_DUNGEON; - } - - DungeonGen dgen(this, &dp); - dgen.generate(blockseed, full_node_min, full_node_max); - } + if (flags & MG_DUNGEONS) + generateDungeons(stone_surface_max_y, stone_type); // Generate the registered decorations if (flags & MG_DECORATIONS) diff --git a/src/mapgen_v7.h b/src/mapgen_v7.h index aee171b48..9ed89a511 100644 --- a/src/mapgen_v7.h +++ b/src/mapgen_v7.h @@ -78,12 +78,6 @@ private: Noise *noise_ridge_uwater; Noise *noise_mountain; Noise *noise_ridge; - - content_t c_cobble; - content_t c_stair_cobble; - content_t c_mossycobble; - content_t c_sandstonebrick; - content_t c_stair_sandstonebrick; }; struct MapgenFactoryV7 : public MapgenFactory { diff --git a/src/mapgen_valleys.cpp b/src/mapgen_valleys.cpp index c0094eb3b..2cd733d36 100644 --- a/src/mapgen_valleys.cpp +++ b/src/mapgen_valleys.cpp @@ -113,25 +113,12 @@ MapgenValleys::MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager * tcave_cache = new float[csize.Y + 2]; // Resolve content to be used - c_cobble = ndef->getId("mapgen_cobble"); - c_lava_source = ndef->getId("mapgen_lava_source"); - c_mossycobble = ndef->getId("mapgen_mossycobble"); - c_sand = ndef->getId("mapgen_sand"); - c_sandstonebrick = ndef->getId("mapgen_sandstonebrick"); - c_stair_cobble = ndef->getId("mapgen_stair_cobble"); - c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick"); + c_lava_source = ndef->getId("mapgen_lava_source"); + c_sand = ndef->getId("mapgen_sand"); // Fall back to more basic content if not defined - if (c_mossycobble == CONTENT_IGNORE) - c_mossycobble = c_cobble; if (c_sand == CONTENT_IGNORE) c_sand = c_stone; - if (c_sandstonebrick == CONTENT_IGNORE) - c_sandstonebrick = c_sandstone; - if (c_stair_cobble == CONTENT_IGNORE) - c_stair_cobble = c_cobble; - if (c_stair_sandstonebrick == CONTENT_IGNORE) - c_stair_sandstonebrick = c_sandstone; } @@ -279,48 +266,8 @@ void MapgenValleys::makeChunk(BlockMakeData *data) generateCaves(stone_surface_max_y, large_cave_depth); // Dungeon creation - if ((flags & MG_DUNGEONS) && node_max.Y < 50 && (stone_surface_max_y >= node_min.Y)) { - DungeonParams dp; - - dp.np_rarity = nparams_dungeon_rarity; - dp.np_density = nparams_dungeon_density; - dp.np_wetness = nparams_dungeon_wetness; - dp.c_water = c_water_source; - if (stone_type == MGSTONE_STONE) { - dp.c_cobble = c_cobble; - dp.c_moss = c_mossycobble; - dp.c_stair = c_stair_cobble; - - dp.diagonal_dirs = false; - dp.mossratio = 3.f; - dp.holesize = v3s16(1, 2, 1); - dp.roomsize = v3s16(0, 0, 0); - dp.notifytype = GENNOTIFY_DUNGEON; - } else if (stone_type == MGSTONE_DESERT_STONE) { - dp.c_cobble = c_desert_stone; - dp.c_moss = c_desert_stone; - dp.c_stair = c_desert_stone; - - dp.diagonal_dirs = true; - dp.mossratio = 0.f; - dp.holesize = v3s16(2, 3, 2); - dp.roomsize = v3s16(2, 5, 2); - dp.notifytype = GENNOTIFY_TEMPLE; - } else if (stone_type == MGSTONE_SANDSTONE) { - dp.c_cobble = c_sandstonebrick; - dp.c_moss = c_sandstonebrick; - dp.c_stair = c_sandstonebrick; - - dp.diagonal_dirs = false; - dp.mossratio = 0.f; - dp.holesize = v3s16(2, 2, 2); - dp.roomsize = v3s16(2, 0, 2); - dp.notifytype = GENNOTIFY_DUNGEON; - } - - DungeonGen dgen(this, &dp); - dgen.generate(blockseed, full_node_min, full_node_max); - } + if ((flags & MG_DUNGEONS) && node_max.Y < 50) + generateDungeons(stone_surface_max_y, stone_type); // Generate the registered decorations if (flags & MG_DECORATIONS) diff --git a/src/mapgen_valleys.h b/src/mapgen_valleys.h index bab3a756e..a7369ea92 100644 --- a/src/mapgen_valleys.h +++ b/src/mapgen_valleys.h @@ -124,13 +124,8 @@ private: Noise *noise_valley_depth; Noise *noise_valley_profile; - content_t c_cobble; content_t c_lava_source; - content_t c_mossycobble; content_t c_sand; - content_t c_sandstonebrick; - content_t c_stair_cobble; - content_t c_stair_sandstonebrick; float terrainLevelAtPoint(s16 x, s16 z); From c8fd232678698b8be469b3792e7acc9418231a38 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Sun, 22 May 2016 16:50:43 -0400 Subject: [PATCH 034/418] Dungeongen: Remove dependency on Mapgen --- src/dungeongen.cpp | 37 +++++++++++++++++++++++-------------- src/dungeongen.h | 19 +++++++++++++------ src/mapgen.cpp | 7 +++++-- src/mapgen_v6.cpp | 6 ++++-- 4 files changed, 45 insertions(+), 24 deletions(-) diff --git a/src/dungeongen.cpp b/src/dungeongen.cpp index f7f9b8b50..6be7e9086 100644 --- a/src/dungeongen.cpp +++ b/src/dungeongen.cpp @@ -38,22 +38,27 @@ NoiseParams nparams_dungeon_density(0.0, 1.0, v3f(2.5, 2.5, 2.5), 0, 2, 1.4, 2.0 /////////////////////////////////////////////////////////////////////////////// -DungeonGen::DungeonGen(Mapgen *mapgen, DungeonParams *dparams) +DungeonGen::DungeonGen(INodeDefManager *ndef, + GenerateNotifier *gennotify, DungeonParams *dparams) { - this->mg = mapgen; - this->vm = mapgen->vm; + assert(ndef); + + this->ndef = ndef; + this->gennotify = gennotify; #ifdef DGEN_USE_TORCHES - c_torch = mg->ndef->getId("default:torch"); + c_torch = ndef->getId("default:torch"); #endif if (dparams) { memcpy(&dp, dparams, sizeof(dp)); } else { - dp.c_water = mg->ndef->getId("mapgen_water_source"); - dp.c_cobble = mg->ndef->getId("mapgen_cobble"); - dp.c_moss = mg->ndef->getId("mapgen_mossycobble"); - dp.c_stair = mg->ndef->getId("mapgen_stair_cobble"); + dp.seed = 0; + + dp.c_water = ndef->getId("mapgen_water_source"); + dp.c_cobble = ndef->getId("mapgen_cobble"); + dp.c_moss = ndef->getId("mapgen_mossycobble"); + dp.c_stair = ndef->getId("mapgen_stair_cobble"); dp.diagonal_dirs = false; dp.mossratio = 3.0; @@ -67,18 +72,21 @@ DungeonGen::DungeonGen(Mapgen *mapgen, DungeonParams *dparams) } // For mapgens using river water - dp.c_river_water = mg->ndef->getId("mapgen_river_water_source"); + dp.c_river_water = ndef->getId("mapgen_river_water_source"); if (dp.c_river_water == CONTENT_IGNORE) - dp.c_river_water = mg->ndef->getId("mapgen_water_source"); + dp.c_river_water = ndef->getId("mapgen_water_source"); } -void DungeonGen::generate(u32 bseed, v3s16 nmin, v3s16 nmax) +void DungeonGen::generate(MMVManip *vm, u32 bseed, v3s16 nmin, v3s16 nmax) { + assert(vm); + //TimeTaker t("gen dungeons"); - if (NoisePerlin3D(&dp.np_rarity, nmin.X, nmin.Y, nmin.Z, mg->seed) < 0.2) + if (NoisePerlin3D(&dp.np_rarity, nmin.X, nmin.Y, nmin.Z, dp.seed) < 0.2) return; + this->vm = vm; this->blockseed = bseed; random.seed(bseed + 2); @@ -109,7 +117,7 @@ void DungeonGen::generate(u32 bseed, v3s16 nmin, v3s16 nmax) u32 i = vm->m_area.index(nmin.X, y, z); for (s16 x = nmin.X; x <= nmax.X; x++) { if (vm->m_data[i].getContent() == dp.c_cobble) { - float wetness = NoisePerlin3D(&dp.np_wetness, x, y, z, mg->seed); + float wetness = NoisePerlin3D(&dp.np_wetness, x, y, z, dp.seed); float density = NoisePerlin3D(&dp.np_density, x, y, z, blockseed); if (density < wetness / dp.mossratio) vm->m_data[i].setContent(dp.c_moss); @@ -187,7 +195,8 @@ void DungeonGen::makeDungeon(v3s16 start_padding) makeRoom(roomsize, roomplace); v3s16 room_center = roomplace + v3s16(roomsize.X / 2, 1, roomsize.Z / 2); - mg->gennotify.addEvent(dp.notifytype, room_center); + if (gennotify) + gennotify->addEvent(dp.notifytype, room_center); #ifdef DGEN_USE_TORCHES // Place torch at room center (for testing) diff --git a/src/dungeongen.h b/src/dungeongen.h index d209dd4bf..f0a5e5ba8 100644 --- a/src/dungeongen.h +++ b/src/dungeongen.h @@ -39,6 +39,8 @@ int dir_to_facedir(v3s16 d); struct DungeonParams { + int seed; + content_t c_water; content_t c_river_water; content_t c_cobble; @@ -59,7 +61,9 @@ struct DungeonParams { class DungeonGen { public: MMVManip *vm; - Mapgen *mg; + INodeDefManager *ndef; + GenerateNotifier *gennotify; + u32 blockseed; PseudoRandom random; v3s16 csize; @@ -67,17 +71,20 @@ public: content_t c_torch; DungeonParams dp; - //RoomWalker + // RoomWalker v3s16 m_pos; v3s16 m_dir; - DungeonGen(Mapgen *mg, DungeonParams *dparams); - void generate(u32 bseed, v3s16 full_node_min, v3s16 full_node_max); + DungeonGen(INodeDefManager *ndef, + GenerateNotifier *gennotify, DungeonParams *dparams); + + void generate(MMVManip *vm, u32 bseed, + v3s16 full_node_min, v3s16 full_node_max); void makeDungeon(v3s16 start_padding); void makeRoom(v3s16 roomsize, v3s16 roomplace); void makeCorridor(v3s16 doorplace, v3s16 doordir, - v3s16 &result_place, v3s16 &result_dir); + v3s16 &result_place, v3s16 &result_dir); void makeDoor(v3s16 doorplace, v3s16 doordir); void makeFill(v3s16 place, v3s16 size, u8 avoid_flags, MapNode n, u8 or_flags); void makeHole(v3s16 place); @@ -86,7 +93,7 @@ public: bool findPlaceForRoomDoor(v3s16 roomsize, v3s16 &result_doorplace, v3s16 &result_doordir, v3s16 &result_roomplace); - void randomizeDir() + inline void randomizeDir() { m_dir = rand_ortho_dir(random, dp.diagonal_dirs); } diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 56fe34e16..041356e3d 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -639,10 +639,13 @@ void MapgenBasic::generateDungeons(s16 max_stone_y, MgStoneType stone_type) DungeonParams dp; + dp.seed = seed; + dp.np_rarity = nparams_dungeon_rarity; dp.np_density = nparams_dungeon_density; dp.np_wetness = nparams_dungeon_wetness; dp.c_water = c_water_source; + switch (stone_type) { default: case MGSTONE_STONE: @@ -680,8 +683,8 @@ void MapgenBasic::generateDungeons(s16 max_stone_y, MgStoneType stone_type) break; } - DungeonGen dgen(this, &dp); - dgen.generate(blockseed, full_node_min, full_node_max); + DungeonGen dgen(ndef, &gennotify, &dp); + dgen.generate(vm, blockseed, full_node_min, full_node_max); } diff --git a/src/mapgen_v6.cpp b/src/mapgen_v6.cpp index 65acfaf93..103120b6a 100644 --- a/src/mapgen_v6.cpp +++ b/src/mapgen_v6.cpp @@ -559,6 +559,8 @@ void MapgenV6::makeChunk(BlockMakeData *data) if ((flags & MG_DUNGEONS) && (stone_surface_max_y >= node_min.Y)) { DungeonParams dp; + dp.seed = seed; + dp.np_rarity = nparams_dungeon_rarity; dp.np_density = nparams_dungeon_density; dp.np_wetness = nparams_dungeon_wetness; @@ -585,8 +587,8 @@ void MapgenV6::makeChunk(BlockMakeData *data) dp.notifytype = GENNOTIFY_DUNGEON; } - DungeonGen dgen(this, &dp); - dgen.generate(blockseed, full_node_min, full_node_max); + DungeonGen dgen(ndef, &gennotify, &dp); + dgen.generate(vm, blockseed, full_node_min, full_node_max); } // Add top and bottom side of water to transforming_liquid queue From c4e083f7e142d8e25e906bb19f9b1f03bb7b430d Mon Sep 17 00:00:00 2001 From: kwolekr Date: Thu, 26 May 2016 23:23:05 -0400 Subject: [PATCH 035/418] Disallow external linkage for default Cavegen NoiseParams --- src/cavegen.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cavegen.cpp b/src/cavegen.cpp index 51309ad60..1c25ce711 100644 --- a/src/cavegen.cpp +++ b/src/cavegen.cpp @@ -26,7 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "mg_biome.h" #include "cavegen.h" -NoiseParams nparams_caveliquids(0, 1, v3f(150.0, 150.0, 150.0), 776, 3, 0.6, 2.0); +static NoiseParams nparams_caveliquids(0, 1, v3f(150.0, 150.0, 150.0), 776, 3, 0.6, 2.0); //// From 62d15ac7c1fcd7214a9e45d46bbc560f998edb95 Mon Sep 17 00:00:00 2001 From: red-001 Date: Sat, 28 May 2016 04:37:28 +0100 Subject: [PATCH 036/418] Add base64 encoding and decoding to the lua api. (#3919) --- doc/lua_api.txt | 4 ++++ doc/menu_lua_api.txt | 4 ++++ src/script/lua_api/l_util.cpp | 35 +++++++++++++++++++++++++++++++++++ src/script/lua_api/l_util.h | 6 ++++++ 4 files changed, 49 insertions(+) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 593e0c438..01763fd73 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -2438,6 +2438,10 @@ These functions return the leftover itemstack. * See documentation on `minetest.compress()` for supported compression methods. * currently supported. * `...` indicates method-specific arguments. Currently, no methods use this. +* `minetest.encode_base64(string)`: returns string encoded in base64 + * Encodes a string in base64. +* `minetest.decode_base64(string)`: returns string + * Decodes a string encoded in base64. * `minetest.is_protected(pos, name)`: returns boolean * Returns true, if player `name` shouldn't be abled to dig at `pos` or do other actions, defineable by mods, due to some mod-defined ownership-like concept. diff --git a/doc/menu_lua_api.txt b/doc/menu_lua_api.txt index ac8713a32..479880266 100644 --- a/doc/menu_lua_api.txt +++ b/doc/menu_lua_api.txt @@ -210,6 +210,10 @@ string:trim() ^ eg. string.trim("\n \t\tfoo bar\t ") == "foo bar" core.is_yes(arg) (possible in async calls) ^ returns whether arg can be interpreted as yes +minetest.encode_base64(string) (possible in async calls) +^ Encodes a string in base64. +minetest.decode_base64(string) (possible in async calls) +^ Decodes a string encoded in base64. Version compat: core.get_min_supp_proto() diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index c3e6c8964..e90b7fbcf 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "filesys.h" #include "settings.h" #include "util/auth.h" +#include "util/base64.h" #include // log([level,] text) @@ -320,6 +321,34 @@ int ModApiUtil::l_decompress(lua_State *L) return 1; } +// encode_base64(string) +int ModApiUtil::l_encode_base64(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + size_t size; + const char *data = luaL_checklstring(L, 1, &size); + + std::string out = base64_encode((const unsigned char *)(data), size); + + lua_pushlstring(L, out.data(), out.size()); + return 1; +} + +// decode_base64(string) +int ModApiUtil::l_decode_base64(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + size_t size; + const char *data = luaL_checklstring(L, 1, &size); + + std::string out = base64_decode(std::string(data, size)); + + lua_pushlstring(L, out.data(), out.size()); + return 1; +} + // mkdir(path) int ModApiUtil::l_mkdir(lua_State *L) { @@ -433,6 +462,9 @@ void ModApiUtil::Initialize(lua_State *L, int top) API_FCT(get_dir_list); API_FCT(request_insecure_environment); + + API_FCT(encode_base64); + API_FCT(decode_base64); } void ModApiUtil::InitializeAsync(AsyncEngine& engine) @@ -459,5 +491,8 @@ void ModApiUtil::InitializeAsync(AsyncEngine& engine) ASYNC_API_FCT(mkdir); ASYNC_API_FCT(get_dir_list); + + ASYNC_API_FCT(encode_base64); + ASYNC_API_FCT(decode_base64); } diff --git a/src/script/lua_api/l_util.h b/src/script/lua_api/l_util.h index 6fac7e7eb..779dbe281 100644 --- a/src/script/lua_api/l_util.h +++ b/src/script/lua_api/l_util.h @@ -95,6 +95,12 @@ private: // request_insecure_environment() static int l_request_insecure_environment(lua_State *L); + // encode_base64(string) + static int l_encode_base64(lua_State *L); + + // decode_base64(string) + static int l_decode_base64(lua_State *L); + public: static void Initialize(lua_State *L, int top); From d499ec483837fa7210176ef39beba2d5a3a5a61d Mon Sep 17 00:00:00 2001 From: Auke Kok Date: Fri, 27 May 2016 21:08:23 -0700 Subject: [PATCH 037/418] Particles: Add option to remove particles on collision Adds the particle option `collision_removal = bool` Some particles are hard to use right now since they either go through solid blocks (without collision detection), and with collision detection enabled they (e.g. raindrops) would just stop dead on the floor and sit there until they expire, or worse, scrape along a wall or ceiling. We can solve the problem by adding a boolean flag that tells the particle to be removed if it ever collides with something. This will make it easier to add rain that doesn't fall through your roof or stick on the top of it. Or clouds and smoke that don't go through trees. Particles that collide with this flag are marked expired unconditionally, causing them to be treated like normal expired particles and cleaned up normally. Documentation is adjusted accordingly. An added bonus of this patch is that particles can potentially collide many times with nodes, and this reduces the amount of collisions to 1 (max), which may end up reducing particle load on the client. --- doc/lua_api.txt | 6 +++++ src/client.h | 2 ++ src/network/clientpackethandler.cpp | 7 ++++++ src/network/networkprotocol.h | 2 ++ src/particles.cpp | 39 ++++++++++++++++++----------- src/particles.h | 5 ++++ src/script/lua_api/l_particles.cpp | 24 ++++++++++++------ src/server.cpp | 19 +++++++++----- src/server.h | 12 ++++++--- 9 files changed, 84 insertions(+), 32 deletions(-) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 01763fd73..03f2dad32 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -3885,6 +3885,9 @@ Definition tables size = 1, collisiondetection = false, -- ^ collisiondetection: if true collides with physical objects + collision_removal = false, + -- ^ collision_removal: if true then particle is removed when it collides, + -- ^ requires collisiondetection = true to have any effect vertical = false, -- ^ vertical: if true faces player using y axis only texture = "image.png", @@ -3914,6 +3917,9 @@ Definition tables -- ^ minsize/maxsize, minexptime/maxexptime (expirationtime) collisiondetection = false, -- ^ collisiondetection: if true uses collision detection + collision_removal = false, + -- ^ collision_removal: if true then particle is removed when it collides, + -- ^ requires collisiondetection = true to have any effect vertical = false, -- ^ vertical: if true faces player using y axis only texture = "image.png", diff --git a/src/client.h b/src/client.h index cdadb9d3e..a7eb22ad9 100644 --- a/src/client.h +++ b/src/client.h @@ -182,6 +182,7 @@ struct ClientEvent f32 expirationtime; f32 size; bool collisiondetection; + bool collision_removal; bool vertical; std::string *texture; } spawn_particle; @@ -199,6 +200,7 @@ struct ClientEvent f32 minsize; f32 maxsize; bool collisiondetection; + bool collision_removal; bool vertical; std::string *texture; u32 id; diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index 0498f4048..48c573da5 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -898,8 +898,10 @@ void Client::handleCommand_SpawnParticle(NetworkPacket* pkt) bool collisiondetection = readU8(is); std::string texture = deSerializeLongString(is); bool vertical = false; + bool collision_removal = false; try { vertical = readU8(is); + collision_removal = readU8(is); } catch (...) {} ClientEvent event; @@ -910,6 +912,7 @@ void Client::handleCommand_SpawnParticle(NetworkPacket* pkt) event.spawn_particle.expirationtime = expirationtime; event.spawn_particle.size = size; event.spawn_particle.collisiondetection = collisiondetection; + event.spawn_particle.collision_removal = collision_removal; event.spawn_particle.vertical = vertical; event.spawn_particle.texture = new std::string(texture); @@ -942,8 +945,11 @@ void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt) *pkt >> id; bool vertical = false; + bool collision_removal = false; try { *pkt >> vertical; + *pkt >> collision_removal; + } catch (...) {} ClientEvent event; @@ -961,6 +967,7 @@ void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt) event.add_particlespawner.minsize = minsize; event.add_particlespawner.maxsize = maxsize; event.add_particlespawner.collisiondetection = collisiondetection; + event.add_particlespawner.collision_removal = collision_removal; event.add_particlespawner.vertical = vertical; event.add_particlespawner.texture = new std::string(texture); event.add_particlespawner.id = id; diff --git a/src/network/networkprotocol.h b/src/network/networkprotocol.h index 177b97680..3a9483efb 100644 --- a/src/network/networkprotocol.h +++ b/src/network/networkprotocol.h @@ -474,6 +474,7 @@ enum ToClientCommand u8 bool vertical u32 len u8[len] texture + u8 collision_removal */ TOCLIENT_ADD_PARTICLESPAWNER = 0x47, @@ -495,6 +496,7 @@ enum ToClientCommand u32 len u8[len] texture u32 id + u8 collision_removal */ TOCLIENT_DELETE_PARTICLESPAWNER_LEGACY = 0x48, diff --git a/src/particles.cpp b/src/particles.cpp index 525258a25..ccca691d1 100644 --- a/src/particles.cpp +++ b/src/particles.cpp @@ -54,6 +54,7 @@ Particle::Particle( float expirationtime, float size, bool collisiondetection, + bool collision_removal, bool vertical, video::ITexture *texture, v2f texpos, @@ -85,6 +86,7 @@ Particle::Particle( m_player = player; m_size = size; m_collisiondetection = collisiondetection; + m_collision_removal = collision_removal; m_vertical = vertical; // Irrlicht stuff @@ -126,20 +128,21 @@ void Particle::render() void Particle::step(float dtime) { m_time += dtime; - if (m_collisiondetection) - { + if (m_collisiondetection) { aabb3f box = m_collisionbox; - v3f p_pos = m_pos*BS; - v3f p_velocity = m_velocity*BS; - collisionMoveSimple(m_env, m_gamedef, - BS*0.5, box, - 0, dtime, - &p_pos, &p_velocity, m_acceleration * BS); - m_pos = p_pos/BS; - m_velocity = p_velocity/BS; - } - else - { + v3f p_pos = m_pos * BS; + v3f p_velocity = m_velocity * BS; + collisionMoveResult r = collisionMoveSimple(m_env, + m_gamedef, BS * 0.5, box, 0, dtime, &p_pos, + &p_velocity, m_acceleration * BS); + if (m_collision_removal && r.collides) { + // force expiration of the particle + m_expiration = -1.0; + } else { + m_pos = p_pos / BS; + m_velocity = p_velocity / BS; + } + } else { m_velocity += m_acceleration * dtime; m_pos += m_velocity * dtime; } @@ -210,8 +213,8 @@ ParticleSpawner::ParticleSpawner(IGameDef* gamedef, scene::ISceneManager *smgr, u16 amount, float time, v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime, float minsize, float maxsize, - bool collisiondetection, bool vertical, video::ITexture *texture, u32 id, - ParticleManager *p_manager) : + bool collisiondetection, bool collision_removal, bool vertical, + video::ITexture *texture, u32 id, ParticleManager *p_manager) : m_particlemanager(p_manager) { m_gamedef = gamedef; @@ -230,6 +233,7 @@ ParticleSpawner::ParticleSpawner(IGameDef* gamedef, scene::ISceneManager *smgr, m_minsize = minsize; m_maxsize = maxsize; m_collisiondetection = collisiondetection; + m_collision_removal = collision_removal; m_vertical = vertical; m_texture = texture; m_time = 0; @@ -277,6 +281,7 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env) exptime, size, m_collisiondetection, + m_collision_removal, m_vertical, m_texture, v2f(0.0, 0.0), @@ -317,6 +322,7 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env) exptime, size, m_collisiondetection, + m_collision_removal, m_vertical, m_texture, v2f(0.0, 0.0), @@ -446,6 +452,7 @@ void ParticleManager::handleParticleEvent(ClientEvent *event, IGameDef *gamedef, event->add_particlespawner.minsize, event->add_particlespawner.maxsize, event->add_particlespawner.collisiondetection, + event->add_particlespawner.collision_removal, event->add_particlespawner.vertical, texture, event->add_particlespawner.id, @@ -480,6 +487,7 @@ void ParticleManager::handleParticleEvent(ClientEvent *event, IGameDef *gamedef, event->spawn_particle.expirationtime, event->spawn_particle.size, event->spawn_particle.collisiondetection, + event->spawn_particle.collision_removal, event->spawn_particle.vertical, texture, v2f(0.0, 0.0), @@ -555,6 +563,7 @@ void ParticleManager::addNodeParticle(IGameDef* gamedef, scene::ISceneManager* s visual_size, true, false, + false, texture, texpos, texsize); diff --git a/src/particles.h b/src/particles.h index dda84385c..bc3ca53b7 100644 --- a/src/particles.h +++ b/src/particles.h @@ -30,6 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc., struct ClientEvent; class ParticleManager; +class ClientEnvironment; class Particle : public scene::ISceneNode { @@ -45,6 +46,7 @@ class Particle : public scene::ISceneNode float expirationtime, float size, bool collisiondetection, + bool collision_removal, bool vertical, video::ITexture *texture, v2f texpos, @@ -97,6 +99,7 @@ private: float m_size; u8 m_light; bool m_collisiondetection; + bool m_collision_removal; bool m_vertical; v3s16 m_camera_offset; }; @@ -115,6 +118,7 @@ class ParticleSpawner float minexptime, float maxexptime, float minsize, float maxsize, bool collisiondetection, + bool collision_removal, bool vertical, video::ITexture *texture, u32 id, @@ -148,6 +152,7 @@ class ParticleSpawner video::ITexture *m_texture; std::vector m_spawntimes; bool m_collisiondetection; + bool m_collision_removal; bool m_vertical; }; diff --git a/src/script/lua_api/l_particles.cpp b/src/script/lua_api/l_particles.cpp index f6c1725de..263e35407 100644 --- a/src/script/lua_api/l_particles.cpp +++ b/src/script/lua_api/l_particles.cpp @@ -21,13 +21,16 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "lua_api/l_internal.h" #include "common/c_converter.h" #include "server.h" +#include "particles.h" // add_particle({pos=, velocity=, acceleration=, expirationtime=, -// size=, collisiondetection=, vertical=, texture=, player=}) +// size=, collisiondetection=, collision_removal=, vertical=, +// texture=, player=}) // pos/velocity/acceleration = {x=num, y=num, z=num} // expirationtime = num (seconds) // size = num // collisiondetection = bool +// collision_removal = bool // vertical = bool // texture = e.g."default_wood.png" int ModApiParticles::l_add_particle(lua_State *L) @@ -41,8 +44,8 @@ int ModApiParticles::l_add_particle(lua_State *L) float expirationtime, size; expirationtime = size = 1; - bool collisiondetection, vertical; - collisiondetection = vertical = false; + bool collisiondetection, vertical, collision_removal; + collisiondetection = vertical = collision_removal = false; std::string texture = ""; std::string playername = ""; @@ -94,12 +97,14 @@ int ModApiParticles::l_add_particle(lua_State *L) size = getfloatfield_default(L, 1, "size", 1); collisiondetection = getboolfield_default(L, 1, "collisiondetection", collisiondetection); + collision_removal = getboolfield_default(L, 1, + "collision_removal", collision_removal); vertical = getboolfield_default(L, 1, "vertical", vertical); texture = getstringfield_default(L, 1, "texture", ""); playername = getstringfield_default(L, 1, "playername", ""); } - getServer(L)->spawnParticle(playername, pos, vel, acc, - expirationtime, size, collisiondetection, vertical, texture); + getServer(L)->spawnParticle(playername, pos, vel, acc, expirationtime, size, + collisiondetection, collision_removal, vertical, texture); return 1; } @@ -110,6 +115,7 @@ int ModApiParticles::l_add_particle(lua_State *L) // minexptime=, maxexptime=, // minsize=, maxsize=, // collisiondetection=, +// collision_removal=, // vertical=, // texture=, // player=}) @@ -117,6 +123,7 @@ int ModApiParticles::l_add_particle(lua_State *L) // minexptime/maxexptime = num (seconds) // minsize/maxsize = num // collisiondetection = bool +// collision_removal = bool // vertical = bool // texture = e.g."default_wood.png" int ModApiParticles::l_add_particlespawner(lua_State *L) @@ -129,8 +136,8 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) minpos= maxpos= minvel= maxvel= minacc= maxacc= v3f(0, 0, 0); float time, minexptime, maxexptime, minsize, maxsize; time= minexptime= maxexptime= minsize= maxsize= 1; - bool collisiondetection, vertical; - collisiondetection= vertical= false; + bool collisiondetection, vertical, collision_removal; + collisiondetection = vertical = collision_removal = false; std::string texture = ""; std::string playername = ""; @@ -189,6 +196,8 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) maxsize = getfloatfield_default(L, 1, "maxsize", maxsize); collisiondetection = getboolfield_default(L, 1, "collisiondetection", collisiondetection); + collision_removal = getboolfield_default(L, 1, + "collision_removal", collision_removal); vertical = getboolfield_default(L, 1, "vertical", vertical); texture = getstringfield_default(L, 1, "texture", ""); playername = getstringfield_default(L, 1, "playername", ""); @@ -201,6 +210,7 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) minexptime, maxexptime, minsize, maxsize, collisiondetection, + collision_removal, vertical, texture, playername); lua_pushnumber(L, id); diff --git a/src/server.cpp b/src/server.cpp index a3b686c25..ada45dc68 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1673,7 +1673,8 @@ void Server::SendShowFormspecMessage(u16 peer_id, const std::string &formspec, // Spawns a particle on peer with peer_id void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, bool collisiondetection, - bool vertical, std::string texture) + bool collision_removal, + bool vertical, const std::string &texture) { DSTACK(FUNCTION_NAME); @@ -1683,6 +1684,7 @@ void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f accelerat << size << collisiondetection; pkt.putLongString(texture); pkt << vertical; + pkt << collision_removal; if (peer_id != PEER_ID_INEXISTENT) { Send(&pkt); @@ -1695,7 +1697,8 @@ void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f accelerat // Adds a ParticleSpawner on peer with peer_id void Server::SendAddParticleSpawner(u16 peer_id, u16 amount, float spawntime, v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime, - float minsize, float maxsize, bool collisiondetection, bool vertical, std::string texture, u32 id) + float minsize, float maxsize, bool collisiondetection, bool collision_removal, + bool vertical, const std::string &texture, u32 id) { DSTACK(FUNCTION_NAME); @@ -1708,6 +1711,7 @@ void Server::SendAddParticleSpawner(u16 peer_id, u16 amount, float spawntime, v3 pkt.putLongString(texture); pkt << id << vertical; + pkt << collision_removal; if (peer_id != PEER_ID_INEXISTENT) { Send(&pkt); @@ -3160,7 +3164,8 @@ void Server::notifyPlayers(const std::wstring &msg) void Server::spawnParticle(const std::string &playername, v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, bool - collisiondetection, bool vertical, const std::string &texture) + collisiondetection, bool collision_removal, + bool vertical, const std::string &texture) { // m_env will be NULL if the server is initializing if (!m_env) @@ -3175,13 +3180,15 @@ void Server::spawnParticle(const std::string &playername, v3f pos, } SendSpawnParticle(peer_id, pos, velocity, acceleration, - expirationtime, size, collisiondetection, vertical, texture); + expirationtime, size, collisiondetection, + collision_removal, vertical, texture); } u32 Server::addParticleSpawner(u16 amount, float spawntime, v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime, float minsize, float maxsize, - bool collisiondetection, bool vertical, const std::string &texture, + bool collisiondetection, bool collision_removal, + bool vertical, const std::string &texture, const std::string &playername) { // m_env will be NULL if the server is initializing @@ -3200,7 +3207,7 @@ u32 Server::addParticleSpawner(u16 amount, float spawntime, SendAddParticleSpawner(peer_id, amount, spawntime, minpos, maxpos, minvel, maxvel, minacc, maxacc, minexptime, maxexptime, minsize, maxsize, - collisiondetection, vertical, texture, id); + collisiondetection, collision_removal, vertical, texture, id); return id; } diff --git a/src/server.h b/src/server.h index daf51dee1..7ee15a463 100644 --- a/src/server.h +++ b/src/server.h @@ -275,7 +275,8 @@ public: void spawnParticle(const std::string &playername, v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, - bool collisiondetection, bool vertical, const std::string &texture); + bool collisiondetection, bool collision_removal, + bool vertical, const std::string &texture); u32 addParticleSpawner(u16 amount, float spawntime, v3f minpos, v3f maxpos, @@ -283,7 +284,8 @@ public: v3f minacc, v3f maxacc, float minexptime, float maxexptime, float minsize, float maxsize, - bool collisiondetection, bool vertical, const std::string &texture, + bool collisiondetection, bool collision_removal, + bool vertical, const std::string &texture, const std::string &playername); void deleteParticleSpawner(const std::string &playername, u32 id); @@ -456,7 +458,8 @@ private: v3f minacc, v3f maxacc, float minexptime, float maxexptime, float minsize, float maxsize, - bool collisiondetection, bool vertical, std::string texture, u32 id); + bool collisiondetection, bool collision_removal, + bool vertical, const std::string &texture, u32 id); void SendDeleteParticleSpawner(u16 peer_id, u32 id); @@ -464,7 +467,8 @@ private: void SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, - bool collisiondetection, bool vertical, std::string texture); + bool collisiondetection, bool collision_removal, + bool vertical, const std::string &texture); u32 SendActiveObjectRemoveAdd(u16 peer_id, const std::string &datas); void SendActiveObjectMessages(u16 peer_id, const std::string &datas, bool reliable = true); From a9bc7dc405df04d5a3cb5fe5de43190200dc692d Mon Sep 17 00:00:00 2001 From: Zeno- Date: Mon, 30 May 2016 22:37:40 +1000 Subject: [PATCH 038/418] Remove unused code in s_security.cpp (#4172) Note that the macro CHECK_FILE_ERR implements the code removed --- src/script/cpp_api/s_security.cpp | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/script/cpp_api/s_security.cpp b/src/script/cpp_api/s_security.cpp index 730235c7b..e117a4811 100644 --- a/src/script/cpp_api/s_security.cpp +++ b/src/script/cpp_api/s_security.cpp @@ -249,8 +249,8 @@ bool ScriptApiSecurity::isSecure(lua_State *L) #define CHECK_FILE_ERR(ret, fp) \ if (ret) { \ - if (fp) std::fclose(fp); \ lua_pushfstring(L, "%s: %s", path, strerror(errno)); \ + if (fp) std::fclose(fp); \ return false; \ } @@ -291,20 +291,12 @@ bool ScriptApiSecurity::safeLoadFile(lua_State *L, const char *path) // Read the file int ret = std::fseek(fp, 0, SEEK_END); CHECK_FILE_ERR(ret, fp); - if (ret) { - std::fclose(fp); - lua_pushfstring(L, "%s: %s", path, strerror(errno)); - return false; - } + size_t size = std::ftell(fp) - start; char *code = new char[size]; ret = std::fseek(fp, start, SEEK_SET); CHECK_FILE_ERR(ret, fp); - if (ret) { - std::fclose(fp); - lua_pushfstring(L, "%s: %s", path, strerror(errno)); - return false; - } + size_t num_read = std::fread(code, 1, size, fp); if (path) { std::fclose(fp); From 4134d8ad13c371820a485a9752b1197a8d7007f3 Mon Sep 17 00:00:00 2001 From: Sokomine Date: Mon, 30 May 2016 14:40:32 +0200 Subject: [PATCH 039/418] protection_bypass priv can now be revoked in singleplayer (#4169) --- builtin/game/privileges.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/builtin/game/privileges.lua b/builtin/game/privileges.lua index bd5ead624..5838b9374 100644 --- a/builtin/game/privileges.lua +++ b/builtin/game/privileges.lua @@ -32,7 +32,10 @@ core.register_privilege("settime", "Can use /time") core.register_privilege("privs", "Can modify privileges") core.register_privilege("basic_privs", "Can modify 'shout' and 'interact' privileges") core.register_privilege("server", "Can do server maintenance stuff") -core.register_privilege("protection_bypass", "Can bypass node protection in the world") +core.register_privilege("protection_bypass", { + description = "Can bypass node protection in the world", + give_to_singleplayer = false, +}) core.register_privilege("shout", "Can speak in chat") core.register_privilege("ban", "Can ban and unban players") core.register_privilege("kick", "Can kick players") From 27db92925261ed6646d0a1c0512711ab3aeb5fb8 Mon Sep 17 00:00:00 2001 From: est31 Date: Mon, 30 May 2016 23:27:48 +1000 Subject: [PATCH 040/418] Add minetest.check_password_entry callback Gives a convenient way to check a player's password. This entirely bypasses the SRP protocol, so should be used with great care. This function is not intended to be used in-game, but solely by external protocols, where no authentication of the minetest engine is provided, and also only for protocols, in which the user already gives the server the plaintext password. Examples for good use are the classical http form, or irc, an example for a bad use is a password change dialog inside formspec. Users should be aware that they lose the advantages of the SRP protocol if they enter their passwords for servers outside the normal entry box, like in in-game formspec menus, or through irc /msg s, This patch also fixes an auth.h mistake which has mixed up the order of params inside the decode_srp_verifier_and_salt function. Zeno-: Added errorstream message for invalid format when I committed --- doc/lua_api.txt | 9 +++++++++ src/script/lua_api/l_util.cpp | 30 ++++++++++++++++++++++++++++++ src/script/lua_api/l_util.h | 3 +++ src/util/auth.h | 2 +- 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 03f2dad32..82a0acbee 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -1951,12 +1951,21 @@ Call these functions only at load time! * `minetest.notify_authentication_modified(name)` * Should be called by the authentication handler if privileges changes. * To report everybody, set `name=nil`. +* `minetest.check_password_entry(name, entry, password)` + * Returns true if the "db entry" for a player with name matches given + * password, false otherwise. + * The "db entry" is the usually player-individual value that is derived + * from the player's chosen password and stored on the server in order to allow + * authentication whenever the player desires to log in. + * Only use this function for making it possible to log in via the password from + * via protocols like IRC, other uses for inside the game are frowned upon. * `minetest.get_password_hash(name, raw_password)` * Convert a name-password pair to a password hash that Minetest can use. * The returned value alone is not a good basis for password checks based * on comparing the password hash in the database with the password hash * from the function, with an externally provided password, as the hash * in the db might use the new SRP verifier format. + * For this purpose, use minetest.check_password_entry instead. * `minetest.string_to_privs(str)`: returns `{priv1=true,...}` * `minetest.privs_to_string(privs)`: returns `"priv1,priv2,..."` * Convert between two privilege representations diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index e90b7fbcf..d090fc91c 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -246,6 +246,35 @@ int ModApiUtil::l_get_hit_params(lua_State *L) return 1; } +// check_password_entry(name, entry, password) +int ModApiUtil::l_check_password_entry(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + std::string name = luaL_checkstring(L, 1); + std::string entry = luaL_checkstring(L, 2); + std::string password = luaL_checkstring(L, 3); + + if (base64_is_valid(entry)) { + std::string hash = translate_password(name, password); + lua_pushboolean(L, hash == entry); + return 1; + } + + std::string salt; + std::string verifier; + + if (!decode_srp_verifier_and_salt(entry, &verifier, &salt)) { + // invalid format + warningstream << "Invalid password format for " << name << std::endl; + lua_pushboolean(L, false); + return 1; + } + std::string gen_verifier = generate_srp_verifier(name, password, salt); + + lua_pushboolean(L, gen_verifier == verifier); + return 1; +} + // get_password_hash(name, raw_password) int ModApiUtil::l_get_password_hash(lua_State *L) { @@ -449,6 +478,7 @@ void ModApiUtil::Initialize(lua_State *L, int top) API_FCT(get_dig_params); API_FCT(get_hit_params); + API_FCT(check_password_entry); API_FCT(get_password_hash); API_FCT(is_yes); diff --git a/src/script/lua_api/l_util.h b/src/script/lua_api/l_util.h index 779dbe281..3012d55aa 100644 --- a/src/script/lua_api/l_util.h +++ b/src/script/lua_api/l_util.h @@ -71,6 +71,9 @@ private: // get_hit_params(groups, tool_capabilities[, time_from_last_punch]) static int l_get_hit_params(lua_State *L); + // check_password_entry(name, entry, password) + static int l_check_password_entry(lua_State *L); + // get_password_hash(name, raw_password) static int l_get_password_hash(lua_State *L); diff --git a/src/util/auth.h b/src/util/auth.h index 1fd6ab453..7cdc7d74d 100644 --- a/src/util/auth.h +++ b/src/util/auth.h @@ -45,6 +45,6 @@ std::string encode_srp_verifier(const std::string &verifier, /// Reads the DB-formatted SRP verifier and gets the verifier /// and salt components. bool decode_srp_verifier_and_salt(const std::string &encoded, - std::string *salt, std::string *bytes_v); + std::string *verifier, std::string *salt); #endif From 3842c3de656c8eebe46c63b67ecc50f1afdee981 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Wed, 11 May 2016 04:34:34 +0200 Subject: [PATCH 041/418] Translated using Weblate (German) Currently translated at 100.0% (887 of 887 strings) --- po/de/minetest.po | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/po/de/minetest.po b/po/de/minetest.po index b9db120e5..25d49957c 100644 --- a/po/de/minetest.po +++ b/po/de/minetest.po @@ -8,7 +8,7 @@ msgstr "" "Project-Id-Version: 0.0.0\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2016-05-05 16:13+0200\n" -"PO-Revision-Date: 2016-05-06 02:25+0000\n" +"PO-Revision-Date: 2016-05-11 04:34+0000\n" "Last-Translator: Wuzzy \n" "Language-Team: German " "\n" @@ -2944,7 +2944,7 @@ msgstr "" #: src/settings_translation_file.cpp msgid "Liquid fluidity" -msgstr "Flüssigkeitswiederstand" +msgstr "Flüssigkeitswiderstand" #: src/settings_translation_file.cpp msgid "Liquid fluidity smoothing" From 526c978f9691f2d7d389ed50d511bf9a788baffd Mon Sep 17 00:00:00 2001 From: Vasily Pavlov Date: Wed, 11 May 2016 22:36:17 +0200 Subject: [PATCH 042/418] Translated using Weblate (Russian) Currently translated at 57.1% (507 of 887 strings) --- po/ru/minetest.po | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/po/ru/minetest.po b/po/ru/minetest.po index d28193911..425a1ee0c 100644 --- a/po/ru/minetest.po +++ b/po/ru/minetest.po @@ -8,10 +8,10 @@ msgstr "" "Project-Id-Version: minetest\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2016-05-05 16:13+0200\n" -"PO-Revision-Date: 2016-04-18 23:08+0000\n" -"Last-Translator: Stas Kies \n" -"Language-Team: Russian \n" +"PO-Revision-Date: 2016-05-11 22:36+0000\n" +"Last-Translator: Vasily Pavlov \n" +"Language-Team: Russian " +"\n" "Language: ru\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" @@ -3767,9 +3767,8 @@ msgid "Serverlist URL" msgstr "Список публичных серверов" #: src/settings_translation_file.cpp -#, fuzzy msgid "Serverlist file" -msgstr "Список публичных серверов" +msgstr "Файл списка серверов" #: src/settings_translation_file.cpp #, fuzzy From dc33ecad6d0f8651e0e3cd0716f7e3c49138f3ed Mon Sep 17 00:00:00 2001 From: Sergey Date: Wed, 11 May 2016 22:34:38 +0200 Subject: [PATCH 043/418] Translated using Weblate (Russian) Currently translated at 57.0% (506 of 887 strings) This is a merger of three commits. --- po/ru/minetest.po | 111 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 88 insertions(+), 23 deletions(-) diff --git a/po/ru/minetest.po b/po/ru/minetest.po index 425a1ee0c..fdb2539db 100644 --- a/po/ru/minetest.po +++ b/po/ru/minetest.po @@ -8,17 +8,17 @@ msgstr "" "Project-Id-Version: minetest\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2016-05-05 16:13+0200\n" -"PO-Revision-Date: 2016-05-11 22:36+0000\n" -"Last-Translator: Vasily Pavlov \n" +"PO-Revision-Date: 2016-05-13 13:42+0000\n" +"Last-Translator: Sergey \n" "Language-Team: Russian " "\n" "Language: ru\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -"Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n" -"%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\n" -"X-Generator: Weblate 2.6-dev\n" +"Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=" +"4 && (n%100<10 || n%100>=20) ? 1 : 2;\n" +"X-Generator: Weblate 2.7-dev\n" #: builtin/fstk/ui.lua msgid "An error occured in a Lua script, such as a mod:" @@ -98,6 +98,7 @@ msgid "Enable all" msgstr "Включить всё" #: builtin/mainmenu/dlg_config_world.lua +#, fuzzy msgid "" "Failed to enable mod \"$1\" as it contains disallowed characters. Only " "chararacters [a-z0-9_] are allowed." @@ -425,6 +426,7 @@ msgid "Creative mode" msgstr "Режим творчества" #: builtin/mainmenu/tab_multiplayer.lua builtin/mainmenu/tab_simple_main.lua +#, fuzzy msgid "Damage enabled" msgstr "Разрешить увечья" @@ -1620,16 +1622,23 @@ msgid "Colored fog" msgstr "Цветной туман" #: src/settings_translation_file.cpp +#, fuzzy msgid "" "Comma-separated list of trusted mods that are allowed to access insecure\n" "functions even when mod security is on (via request_insecure_environment())." msgstr "" +"Список доверенных модов, через запятую, которым разрешён доступ к " +"небезопасным функциям, даже в безопасном режиме (с помощью " +"request_insecure_environment())." #: src/settings_translation_file.cpp +#, fuzzy msgid "" "Comma-seperated list of mods that are allowed to access HTTP APIs, which\n" "allow them to upload and download data to/from the internet." msgstr "" +"Список доверенных модов, через запятую, которым разрешён доступ к HTTP API, " +"что позволяет им отправлять и принимать данные в/из сети Интернет." #: src/settings_translation_file.cpp msgid "Command key" @@ -1800,7 +1809,7 @@ msgstr "" #: src/settings_translation_file.cpp msgid "Delay showing tooltips, stated in milliseconds." -msgstr "" +msgstr "Задержка показа подсказок, указанная в миллисекундах." #: src/settings_translation_file.cpp #, fuzzy @@ -1923,8 +1932,9 @@ msgid "" msgstr "" #: src/settings_translation_file.cpp +#, fuzzy msgid "Enables animation of inventory items." -msgstr "" +msgstr "Включить анимацию предметов в инвентаре." #: src/settings_translation_file.cpp msgid "" @@ -1979,18 +1989,22 @@ msgid "Fall bobbing" msgstr "" #: src/settings_translation_file.cpp +#, fuzzy msgid "Fallback font" msgstr "Fallback шрифт" #: src/settings_translation_file.cpp +#, fuzzy msgid "Fallback font shadow" -msgstr "" +msgstr "Fallback тень шрифта" #: src/settings_translation_file.cpp +#, fuzzy msgid "Fallback font shadow alpha" -msgstr "" +msgstr "Fallback прозрачность тени шрифта" #: src/settings_translation_file.cpp +#, fuzzy msgid "Fallback font size" msgstr "Fallback размер шрифта" @@ -2016,7 +2030,7 @@ msgid "" "Fast movement (via use key).\n" "This requires the \"fast\" privilege on the server." msgstr "" -"Быстрое перемещение (с использование клавиши).\n" +"Быстрое перемещение (с использованием клавиши).\n" "Это требует привилегию \"fast\" на сервере." #: src/settings_translation_file.cpp @@ -2056,8 +2070,9 @@ msgid "Filtering" msgstr "Фильтрация" #: src/settings_translation_file.cpp +#, fuzzy msgid "Fixed map seed" -msgstr "Конкретное семя мира" +msgstr "Фиксированное зерно мира" #: src/settings_translation_file.cpp msgid "Fly key" @@ -2101,13 +2116,12 @@ msgid "Font size" msgstr "Размер шрифта" #: src/settings_translation_file.cpp -#, fuzzy msgid "Format of screenshots." -msgstr "Путь для сохранения скриншотов." +msgstr "Формат скриншотов." #: src/settings_translation_file.cpp msgid "Forward key" -msgstr "Вперед" +msgstr "Клавиша вперёд" #: src/settings_translation_file.cpp msgid "Freetype fonts" @@ -2166,6 +2180,7 @@ msgid "Generate normalmaps" msgstr "Генерировать карты нормалей" #: src/settings_translation_file.cpp +#, fuzzy msgid "" "Global map generation attributes.\n" "In Mapgen v6 the 'decorations' flag controls all decorations except trees\n" @@ -2176,6 +2191,16 @@ msgid "" "default.\n" "Flags starting with 'no' are used to explicitly disable them." msgstr "" +"Параметры глобального генератора карты.\n" +"В генераторе карт v6 флаг 'decorations' управляет всем оформлением за " +"исключением деревьев\n" +" и junglegras(травы джунглей?), в остальных генераторах этот флаг управляет " +"всем оформлением.\n" +"Стандартные флаги, установленные в engine(инструменте?) следующие: caves, " +"light, decorations\n" +"Строка флагов изменяет стандартные настройки engine(инструмента?).\n" +"Флаги, не указанные в строке флагов, по умолчанию не изменяются.\n" +"Флаги, начинающиеся с 'no' используются для их явного отключения." #: src/settings_translation_file.cpp msgid "Graphics" @@ -2201,18 +2226,26 @@ msgid "" "- log: mimic and log backtrace of deprecated call (default for debug).\n" "- error: abort on usage of deprecated call (suggested for mod developers)." msgstr "" +"Обработка для устаревших вызовов lua api:\n" +"- legacy: (иногда успешно) имитирует прежнее поведение (по умолчанию для " +"релиза).\n" +"- log: имитирует и журналирует backtrace(трассировку?след?) устаревших " +"вызовов (по умолчанию для отладки).\n" +"- error: прерывание при использовании устаревших вызовов (рекомендовано " +"для разработчиков модов)." #: src/settings_translation_file.cpp +#, fuzzy msgid "Height component of the initial window size." -msgstr "" +msgstr "Высота содержимого (относительно?) исходного размера окна" #: src/settings_translation_file.cpp msgid "Height on which clouds are appearing." -msgstr "" +msgstr "Высота, на которой появляются облака." #: src/settings_translation_file.cpp msgid "High-precision FPU" -msgstr "" +msgstr "Высокоточный FPU" #: src/settings_translation_file.cpp msgid "Homepage of server, to be displayed in the serverlist." @@ -2220,33 +2253,43 @@ msgstr "Домашняя страница сервера, отображаема #: src/settings_translation_file.cpp msgid "How deep to make rivers" -msgstr "" +msgstr "Глубина рек" #: src/settings_translation_file.cpp +#, fuzzy msgid "" "How large area of blocks are subject to the active block stuff, stated in " "mapblocks (16 nodes).\n" "In active blocks objects are loaded and ABMs run." msgstr "" +"Размер площади из блоков, составляющей активный блок, расположенный в " +"mapblocks (16 nodes).↵\n" +"В активных блоках объекты загружаются и ABMs запускается." #: src/settings_translation_file.cpp +#, fuzzy msgid "" "How many blocks are flying in the wire simultaneously for the whole server." -msgstr "" +msgstr "Количество блоков, передаваемых одновременно для всего сервера." #: src/settings_translation_file.cpp +#, fuzzy msgid "How many blocks are flying in the wire simultaneously per client." -msgstr "" +msgstr "Количество блоков, передаваемых одновременно для каждого клиента." #: src/settings_translation_file.cpp +#, fuzzy msgid "" "How much the server will wait before unloading unused mapblocks.\n" "Higher value is smoother, but will use more RAM." msgstr "" +"Время ожидания сервера до выгрузки неиспользуемых mapblocks.\n" +"Высокие значения более плавные, но используют больше RAM." #: src/settings_translation_file.cpp +#, fuzzy msgid "How wide to make rivers" -msgstr "" +msgstr "Установка ширины рек" #: src/settings_translation_file.cpp #, fuzzy @@ -2262,39 +2305,57 @@ msgid "IPv6 support." msgstr "IPv6 поддержка." #: src/settings_translation_file.cpp +#, fuzzy msgid "" "If FPS would go higher than this, limit it by sleeping\n" "to not waste CPU power for no benefit." msgstr "" +"Если FPS превысит это значение, ограничить его простоем,\n" +"чтобы не тратить мощность процессора впустую." #: src/settings_translation_file.cpp +#, fuzzy msgid "" "If disabled \"use\" key is used to fly fast if both fly and fast mode are " "enabled." msgstr "" +"Если выключено, кнопка \"use\"(\"использовать\"?) используется для быстрого " +"полёта, если одновременно включены быстрый режим и режим полёта." #: src/settings_translation_file.cpp +#, fuzzy msgid "" "If enabled together with fly mode, player is able to fly through solid " "nodes.\n" "This requires the \"noclip\" privilege on the server." msgstr "" +"Если включено одновременно с режимом полёта, игрок может летать через " +"твердые nodes(узлы?вершины?). Это требует привилегий \"noclip\" на сервере." #: src/settings_translation_file.cpp +#, fuzzy msgid "" "If enabled, \"use\" key instead of \"sneak\" key is used for climbing down " "and descending." msgstr "" +"Если включено, клавиша \"use\"(\"использовать\"?) вместо \"sneak\"(\"" +"подкрасться\"?) будет использоваться для climbing down and descending." #: src/settings_translation_file.cpp +#, fuzzy msgid "" "If enabled, actions are recorded for rollback.\n" "This option is only read when server starts." msgstr "" +"Если включено, действия записываются для отката.\n" +"Этот параметр считывается только при запуске сервера." #: src/settings_translation_file.cpp +#, fuzzy msgid "If enabled, disable cheat prevention in multiplayer." msgstr "" +"Если включено, отключается prevention(предупреждение?предотвращение?) " +"читерства в мультиплеере." #: src/settings_translation_file.cpp msgid "" @@ -3700,19 +3761,23 @@ msgstr "Каталог со скриншотами" #: src/settings_translation_file.cpp #, fuzzy msgid "Screenshot format" -msgstr "Каталог со скриншотами" +msgstr "Формат скриншота" #: src/settings_translation_file.cpp #, fuzzy msgid "Screenshot quality" -msgstr "Cкриншот" +msgstr "Качество скриншота" #: src/settings_translation_file.cpp +#, fuzzy msgid "" "Screenshot quality. Only used for JPEG format.\n" "1 means worst quality; 100 means best quality.\n" "Use 0 for default quality." msgstr "" +"Качество скриншота. Используется только для изображений в формате JPEG.\n" +"1 означает плохое качество; 100 означает хорошее качество.\n" +"Используйте 0 для настроек по умолчанию." #: src/settings_translation_file.cpp #, fuzzy From 30cd77a30ba5d8086947fe4abbc958d3cb8a9415 Mon Sep 17 00:00:00 2001 From: Fixer Date: Thu, 12 May 2016 01:10:28 +0200 Subject: [PATCH 044/418] Translated using Weblate (Ukrainian) Currently translated at 39.3% (349 of 887 strings) --- po/uk/minetest.po | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/po/uk/minetest.po b/po/uk/minetest.po index 4a2101d76..1ae1094a3 100644 --- a/po/uk/minetest.po +++ b/po/uk/minetest.po @@ -8,7 +8,7 @@ msgstr "" "Project-Id-Version: minetest\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2016-05-05 16:13+0200\n" -"PO-Revision-Date: 2016-05-10 00:06+0000\n" +"PO-Revision-Date: 2016-05-12 01:10+0000\n" "Last-Translator: Fixer \n" "Language-Team: Ukrainian " "\n" @@ -87,14 +87,12 @@ msgid "Depends:" msgstr "Залежить від:" #: builtin/mainmenu/dlg_config_world.lua -#, fuzzy msgid "Disable MP" -msgstr "Вимкнути МП" +msgstr "Вимкнути модпак" #: builtin/mainmenu/dlg_config_world.lua -#, fuzzy msgid "Enable MP" -msgstr "Увімкнути МП" +msgstr "Увімкнути модпак" #: builtin/mainmenu/dlg_config_world.lua msgid "Enable all" @@ -113,13 +111,12 @@ msgid "Hide Game" msgstr "Приховати гру" #: builtin/mainmenu/dlg_config_world.lua -#, fuzzy msgid "Hide mp content" -msgstr "Приховати мп контент" +msgstr "Сховати вміст модпаку" #: builtin/mainmenu/dlg_config_world.lua msgid "Mod:" -msgstr "Модифікація:" +msgstr "Мод:" #: builtin/mainmenu/dlg_config_world.lua #: builtin/mainmenu/dlg_settings_advanced.lua src/guiKeyChangeMenu.cpp @@ -466,7 +463,7 @@ msgstr "Творчість" #: builtin/mainmenu/tab_server.lua builtin/mainmenu/tab_simple_main.lua #: builtin/mainmenu/tab_singleplayer.lua msgid "Enable Damage" -msgstr "Увімкнути поранення" +msgstr "Поранення" #: builtin/mainmenu/tab_server.lua msgid "Name/Password" @@ -538,9 +535,8 @@ msgid "Bilinear Filter" msgstr "Білінійна фільтрація" #: builtin/mainmenu/tab_settings.lua -#, fuzzy msgid "Bump Mapping" -msgstr "Бамп-маппінг" +msgstr "Бамп маппінг" #: builtin/mainmenu/tab_settings.lua msgid "Change keys" @@ -548,11 +544,11 @@ msgstr "Змінити клавіші" #: builtin/mainmenu/tab_settings.lua msgid "Connected Glass" -msgstr "З'єднане скло" +msgstr "З'єднувати скло" #: builtin/mainmenu/tab_settings.lua msgid "Fancy Leaves" -msgstr "Гарні листя" +msgstr "Гарне листя" #: builtin/mainmenu/tab_settings.lua msgid "Mipmap" @@ -568,7 +564,7 @@ msgstr "Ні" #: builtin/mainmenu/tab_settings.lua msgid "No Filter" -msgstr "Без фільтрування" +msgstr "Без фільтрації" #: builtin/mainmenu/tab_settings.lua msgid "No Mipmap" @@ -896,7 +892,7 @@ msgstr "Далі" #: src/guiKeyChangeMenu.cpp msgid "\"Use\" = climb down" -msgstr "\"Використовувати\" = підніматися в гору" +msgstr "\"Використовувати\" = підніматися вгору" #: src/guiKeyChangeMenu.cpp msgid "Backward" @@ -1564,7 +1560,7 @@ msgstr "" #: src/settings_translation_file.cpp msgid "Client and Server" -msgstr "" +msgstr "Клієнт і сервер" #: src/settings_translation_file.cpp msgid "Climbing speed" @@ -3261,7 +3257,7 @@ msgstr "" #: src/settings_translation_file.cpp msgid "Network" -msgstr "" +msgstr "Мережа" #: src/settings_translation_file.cpp msgid "" @@ -3534,7 +3530,7 @@ msgstr "" #: src/settings_translation_file.cpp msgid "Security" -msgstr "" +msgstr "Безпека" #: src/settings_translation_file.cpp msgid "See http://www.sqlite.org/pragma.html#pragma_synchronous" @@ -3665,7 +3661,7 @@ msgstr "Крастися" #: src/settings_translation_file.cpp msgid "Sound" -msgstr "" +msgstr "Звук" #: src/settings_translation_file.cpp msgid "" From 5e9663d3d4cdfe2fea540a0c7dc5b4f06c666440 Mon Sep 17 00:00:00 2001 From: Emon Omen Date: Sat, 14 May 2016 13:34:53 +0200 Subject: [PATCH 045/418] Translated using Weblate (Italian) Currently translated at 100.0% (887 of 887 strings) --- po/it/minetest.po | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/po/it/minetest.po b/po/it/minetest.po index 83ff7c1ab..bd6a22e32 100644 --- a/po/it/minetest.po +++ b/po/it/minetest.po @@ -7,7 +7,7 @@ msgstr "" "Project-Id-Version: Minetest 0.4.9\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2016-05-05 16:13+0200\n" -"PO-Revision-Date: 2016-05-08 17:01+0000\n" +"PO-Revision-Date: 2016-05-14 13:34+0000\n" "Last-Translator: Emon Omen \n" "Language-Team: Italian " "\n" @@ -2831,7 +2831,7 @@ msgstr "Tasto \"Usare\" per arrampicarsi/scendere" #: src/settings_translation_file.cpp msgid "Language" -msgstr "Linua" +msgstr "Lingua" #: src/settings_translation_file.cpp msgid "Large cave depth" From 6792447dfa6ae4f13c0166c2c85016fbb557a8ee Mon Sep 17 00:00:00 2001 From: Andrey K Date: Fri, 20 May 2016 15:18:33 +0000 Subject: [PATCH 046/418] Translated using Weblate (Russian) Currently translated at 57.2% (508 of 887 strings) --- po/ru/minetest.po | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/po/ru/minetest.po b/po/ru/minetest.po index fdb2539db..8c48a2c2e 100644 --- a/po/ru/minetest.po +++ b/po/ru/minetest.po @@ -8,8 +8,8 @@ msgstr "" "Project-Id-Version: minetest\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2016-05-05 16:13+0200\n" -"PO-Revision-Date: 2016-05-13 13:42+0000\n" -"Last-Translator: Sergey \n" +"PO-Revision-Date: 2016-05-20 15:18+0000\n" +"Last-Translator: Andrey K. \n" "Language-Team: Russian " "\n" "Language: ru\n" @@ -104,7 +104,8 @@ msgid "" "chararacters [a-z0-9_] are allowed." msgstr "" "Ошибка при попытке включения мода \"$1\" поскольку он содержит недопустимые " -"символы. Допускается использование символов от Aa-Zz и от 0-9." +"символы. Допускается использование строчных букв латинского алфавита, цифр, " +"и знака подчёркивания." #: builtin/mainmenu/dlg_config_world.lua msgid "Hide Game" From 0e44af9f7056a78a8e561f708751acceacd149c1 Mon Sep 17 00:00:00 2001 From: yuval hreman Date: Thu, 26 May 2016 21:02:00 +0000 Subject: [PATCH 047/418] Translated using Weblate (Hebrew) Currently translated at 7.8% (70 of 887 strings) --- po/he/minetest.po | 56 +++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/po/he/minetest.po b/po/he/minetest.po index f4229208c..f3e1e6500 100644 --- a/po/he/minetest.po +++ b/po/he/minetest.po @@ -8,20 +8,20 @@ msgstr "" "Project-Id-Version: minetest\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2016-05-05 16:13+0200\n" -"PO-Revision-Date: 2015-10-26 16:22+0200\n" -"Last-Translator: ChaosWormz \n" -"Language-Team: Hebrew \n" +"PO-Revision-Date: 2016-05-26 21:01+0000\n" +"Last-Translator: yuval hreman \n" +"Language-Team: Hebrew " +"\n" "Language: he\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=n != 1;\n" -"X-Generator: Weblate 2.5-dev\n" +"X-Generator: Weblate 2.7-dev\n" #: builtin/fstk/ui.lua msgid "An error occured in a Lua script, such as a mod:" -msgstr "" +msgstr "אירעה שגיאה בקוד לואה (Lua), כנראה באחד המודים:" #: builtin/fstk/ui.lua msgid "An error occured:" @@ -41,7 +41,7 @@ msgstr "התחבר מחדש" #: builtin/fstk/ui.lua msgid "The server has requested a reconnect:" -msgstr "" +msgstr "השרת מבקש שתתחבר מחדש:" #: builtin/mainmenu/common.lua src/game.cpp msgid "Loading..." @@ -49,27 +49,27 @@ msgstr "טוען..." #: builtin/mainmenu/common.lua msgid "Protocol version mismatch. " -msgstr "" +msgstr "שגיאה בגרסאות הפרוטוקול. " #: builtin/mainmenu/common.lua msgid "Server enforces protocol version $1. " -msgstr "" +msgstr "השרת יפעיל את פרוטוקול גרסה $1. בכוח " #: builtin/mainmenu/common.lua msgid "Server supports protocol versions between $1 and $2. " -msgstr "" +msgstr "השרת תומך בפרוטוקולים בין גרסה $1 וגרסה $2. " #: builtin/mainmenu/common.lua msgid "Try reenabling public serverlist and check your internet connection." -msgstr "" +msgstr "נסה לצאת והכנס מחדש לרשימת השרתים ובדוק את חיבור האינטרנט שלך." #: builtin/mainmenu/common.lua msgid "We only support protocol version $1." -msgstr "" +msgstr "אנו תומכים רק בגירסה 1$ של הפרוטוקול." #: builtin/mainmenu/common.lua msgid "We support protocol versions between version $1 and $2." -msgstr "" +msgstr "אנו תומכים בגרסאות בין 1$ ל-2$ של הפרוטוקול." #: builtin/mainmenu/dlg_config_world.lua builtin/mainmenu/dlg_create_world.lua #: builtin/mainmenu/dlg_delete_mod.lua builtin/mainmenu/dlg_delete_world.lua @@ -93,13 +93,15 @@ msgstr "" #: builtin/mainmenu/dlg_config_world.lua msgid "Enable all" -msgstr "אפשר בכל" +msgstr "אפשר הכל" #: builtin/mainmenu/dlg_config_world.lua msgid "" "Failed to enable mod \"$1\" as it contains disallowed characters. Only " "chararacters [a-z0-9_] are allowed." msgstr "" +"טעינת המוד \"1$\" נכשלה מכיוון שהוא מכיל תווים לא חוקיים. רק התווים [a-z0-9_]" +" מותרים." #: builtin/mainmenu/dlg_config_world.lua msgid "Hide Game" @@ -128,7 +130,7 @@ msgstr "מופעל" #: builtin/mainmenu/dlg_create_world.lua msgid "A world named \"$1\" already exists" -msgstr "" +msgstr "עולם בשם \"1$\" כבר קיים" #: builtin/mainmenu/dlg_create_world.lua msgid "Create" @@ -136,11 +138,11 @@ msgstr "ליצור" #: builtin/mainmenu/dlg_create_world.lua msgid "Download a subgame, such as minetest_game, from minetest.net" -msgstr "" +msgstr "הורד מפעיל משחק, למשל \"minetest_game\", מהאתר: minetest.net" #: builtin/mainmenu/dlg_create_world.lua msgid "Download one from minetest.net" -msgstr "" +msgstr "הורד אחד מ-\"minetest.net\"" #: builtin/mainmenu/dlg_create_world.lua src/settings_translation_file.cpp msgid "Game" @@ -148,11 +150,11 @@ msgstr "משחק" #: builtin/mainmenu/dlg_create_world.lua src/settings_translation_file.cpp msgid "Mapgen" -msgstr "" +msgstr "מנוע מפות" #: builtin/mainmenu/dlg_create_world.lua msgid "No worldname given or no game selected" -msgstr "" +msgstr "לא נבחר שם לעולם או שאף מפעיל משחק לא נבחר" #: builtin/mainmenu/dlg_create_world.lua msgid "Seed" @@ -160,7 +162,7 @@ msgstr "" #: builtin/mainmenu/dlg_create_world.lua msgid "Warning: The minimal development test is meant for developers." -msgstr "" +msgstr "אזהרה: מצב המפתחים נועד למפתחים!." #: builtin/mainmenu/dlg_create_world.lua msgid "World name" @@ -168,7 +170,7 @@ msgstr "שם העולם" #: builtin/mainmenu/dlg_create_world.lua msgid "You have no subgames installed." -msgstr "" +msgstr "אין לך אף מפעיל משחק מותקן." #: builtin/mainmenu/dlg_delete_mod.lua msgid "Are you sure you want to delete \"$1\"?" @@ -397,9 +399,8 @@ msgid "Uninstall selected modpack" msgstr "" #: builtin/mainmenu/tab_multiplayer.lua -#, fuzzy msgid "Address / Port" -msgstr "כתובת / פורט :" +msgstr "כתובת / פורט" #: builtin/mainmenu/tab_multiplayer.lua src/settings_translation_file.cpp msgid "Client" @@ -426,9 +427,8 @@ msgid "Favorite" msgstr "" #: builtin/mainmenu/tab_multiplayer.lua builtin/mainmenu/tab_simple_main.lua -#, fuzzy msgid "Name / Password" -msgstr "שם/סיסמה :" +msgstr "שם/סיסמה" #: builtin/mainmenu/tab_multiplayer.lua builtin/mainmenu/tab_simple_main.lua msgid "PvP enabled" @@ -505,9 +505,8 @@ msgid "8x" msgstr "" #: builtin/mainmenu/tab_settings.lua -#, fuzzy msgid "Advanced Settings" -msgstr "הגדרות" +msgstr "הגדרות מתקדמות" #: builtin/mainmenu/tab_settings.lua msgid "Antialiasing:" @@ -587,9 +586,8 @@ msgid "Parallax Occlusion" msgstr "" #: builtin/mainmenu/tab_settings.lua -#, fuzzy msgid "Particles" -msgstr "אפשר בכל" +msgstr "חלקיקים" #: builtin/mainmenu/tab_settings.lua msgid "Settings" From 1d40385d4aacf0cbea4b19ff06940e8c9bebaf47 Mon Sep 17 00:00:00 2001 From: TriBlade9 Date: Fri, 16 Jan 2015 14:54:26 +0800 Subject: [PATCH 048/418] Colored chat working as expected for both freetype and non-freetype builds. @nerzhul improvements * Add unit tests * Fix coding style * move guiChatConsole.hpp to client/ --- builtin/game/chatcommands.lua | 2 +- builtin/game/misc.lua | 17 + src/chat.cpp | 29 +- src/chat.h | 6 +- src/client/CMakeLists.txt | 1 + src/{ => client}/guiChatConsole.cpp | 31 +- src/{ => client}/guiChatConsole.h | 0 src/game.cpp | 14 +- src/util/CMakeLists.txt | 10 + src/util/coloredstring.cpp | 68 +++ src/util/coloredstring.h | 44 ++ src/util/statictext.cpp | 654 ++++++++++++++++++++++++++++ src/util/statictext.h | 150 +++++++ 13 files changed, 998 insertions(+), 28 deletions(-) rename src/{ => client}/guiChatConsole.cpp (96%) rename src/{ => client}/guiChatConsole.h (100%) create mode 100644 src/util/coloredstring.cpp create mode 100644 src/util/coloredstring.h create mode 100644 src/util/statictext.cpp create mode 100644 src/util/statictext.h diff --git a/builtin/game/chatcommands.lua b/builtin/game/chatcommands.lua index 3350140ee..2627559a5 100644 --- a/builtin/game/chatcommands.lua +++ b/builtin/game/chatcommands.lua @@ -102,7 +102,7 @@ core.register_chatcommand("help", { description = "Get help for commands or list privileges", func = function(name, param) local function format_help_line(cmd, def) - local msg = "/"..cmd + local msg = core.colorize("00ffff", "/"..cmd) if def.params and def.params ~= "" then msg = msg .. " " .. def.params end diff --git a/builtin/game/misc.lua b/builtin/game/misc.lua index de41cfc91..8d5c80216 100644 --- a/builtin/game/misc.lua +++ b/builtin/game/misc.lua @@ -197,3 +197,20 @@ function core.http_add_fetch(httpenv) return httpenv end + +function core.get_color_escape_sequence(color) + --if string.len(color) == 3 then + -- local r = string.sub(color, 1, 1) + -- local g = string.sub(color, 2, 2) + -- local b = string.sub(color, 3, 3) + -- color = r .. r .. g .. g .. b .. b + --end + + --assert(#color == 6, "Color must be six characters in length.") + --return "\v" .. color + return "\v(color;" .. color .. ")" +end + +function core.colorize(color, message) + return core.get_color_escape_sequence(color) .. message .. core.get_color_escape_sequence("ffffff") +end diff --git a/src/chat.cpp b/src/chat.cpp index cebe31225..958389df5 100644 --- a/src/chat.cpp +++ b/src/chat.cpp @@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "chat.h" #include "debug.h" +#include "config.h" #include "util/strfnd.h" #include #include @@ -251,8 +252,7 @@ u32 ChatBuffer::formatChatLine(const ChatLine& line, u32 cols, u32 hanging_indentation = 0; // Format the sender name and produce fragments - if (!line.name.empty()) - { + if (!line.name.empty()) { temp_frag.text = L"<"; temp_frag.column = 0; //temp_frag.bold = 0; @@ -267,28 +267,28 @@ u32 ChatBuffer::formatChatLine(const ChatLine& line, u32 cols, next_frags.push_back(temp_frag); } + std::wstring name_sanitized = removeEscapes(line.name); + // Choose an indentation level - if (line.name.empty()) - { + if (line.name.empty()) { // Server messages hanging_indentation = 0; } - else if (line.name.size() + 3 <= cols/2) - { + else if (name_sanitized.size() + 3 <= cols/2) { // Names shorter than about half the console width hanging_indentation = line.name.size() + 3; } - else - { + else { // Very long names hanging_indentation = 2; } + ColoredString line_text(line.text); next_line.first = true; bool text_processing = false; // Produce fragments and layout them into lines - while (!next_frags.empty() || in_pos < line.text.size()) + while (!next_frags.empty() || in_pos < line_text.size()) { // Layout fragments into lines while (!next_frags.empty()) @@ -326,9 +326,9 @@ u32 ChatBuffer::formatChatLine(const ChatLine& line, u32 cols, } // Produce fragment - if (in_pos < line.text.size()) + if (in_pos < line_text.size()) { - u32 remaining_in_input = line.text.size() - in_pos; + u32 remaining_in_input = line_text.size() - in_pos; u32 remaining_in_output = cols - out_column; // Determine a fragment length <= the minimum of @@ -338,14 +338,14 @@ u32 ChatBuffer::formatChatLine(const ChatLine& line, u32 cols, while (frag_length < remaining_in_input && frag_length < remaining_in_output) { - if (isspace(line.text[in_pos + frag_length])) + if (isspace(line_text[in_pos + frag_length])) space_pos = frag_length; ++frag_length; } if (space_pos != 0 && frag_length < remaining_in_input) frag_length = space_pos + 1; - temp_frag.text = line.text.substr(in_pos, frag_length); + temp_frag.text = line_text.substr(in_pos, frag_length); temp_frag.column = 0; //temp_frag.bold = 0; next_frags.push_back(temp_frag); @@ -686,9 +686,6 @@ ChatBackend::~ChatBackend() void ChatBackend::addMessage(std::wstring name, std::wstring text) { - name = unescape_enriched(name); - text = unescape_enriched(text); - // Note: A message may consist of multiple lines, for example the MOTD. WStrfnd fnd(text); while (!fnd.at_end()) diff --git a/src/chat.h b/src/chat.h index db4146d35..661cafc82 100644 --- a/src/chat.h +++ b/src/chat.h @@ -20,11 +20,13 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef CHAT_HEADER #define CHAT_HEADER -#include "irrlichttypes.h" #include #include #include +#include "irrlichttypes.h" +#include "util/coloredstring.h" + // Chat console related classes struct ChatLine @@ -34,7 +36,7 @@ struct ChatLine // name of sending player, or empty if sent by server std::wstring name; // message text - std::wstring text; + ColoredString text; ChatLine(std::wstring a_name, std::wstring a_text): age(0.0), diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index a1ec37fe3..bcf114760 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -1,5 +1,6 @@ set(client_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/clientlauncher.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/guiChatConsole.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tile.cpp PARENT_SCOPE ) diff --git a/src/guiChatConsole.cpp b/src/client/guiChatConsole.cpp similarity index 96% rename from src/guiChatConsole.cpp rename to src/client/guiChatConsole.cpp index 17a1689c7..d8837556a 100644 --- a/src/guiChatConsole.cpp +++ b/src/client/guiChatConsole.cpp @@ -32,7 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #if USE_FREETYPE -#include "xCGUITTFont.h" + #include "xCGUITTFont.h" #endif inline u32 clamp_u8(s32 value) @@ -340,13 +340,28 @@ void GUIChatConsole::drawText() s32 x = (fragment.column + 1) * m_fontsize.X; core::rect destrect( x, y, x + m_fontsize.X * fragment.text.size(), y + m_fontsize.Y); - m_font->draw( - fragment.text.c_str(), - destrect, - video::SColor(255, 255, 255, 255), - false, - false, - &AbsoluteClippingRect); + + + #if USE_FREETYPE + // Draw colored text if FreeType is enabled + irr::gui::CGUITTFont *tmp = static_cast(m_font); + tmp->draw( + fragment.text.c_str(), + destrect, + fragment.text.getColors(), + false, + false, + &AbsoluteClippingRect); + #else + // Otherwise use standard text + m_font->draw( + fragment.text.c_str(), + destrect, + video::SColor(255, 255, 255, 255), + false, + false, + &AbsoluteClippingRect); + #endif } } } diff --git a/src/guiChatConsole.h b/src/client/guiChatConsole.h similarity index 100% rename from src/guiChatConsole.h rename to src/client/guiChatConsole.h diff --git a/src/game.cpp b/src/game.cpp index c5211a042..71a04aef5 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -34,7 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "log.h" #include "filesys.h" #include "gettext.h" -#include "guiChatConsole.h" +#include "client/guiChatConsole.h" #include "guiFormSpecMenu.h" #include "guiKeyChangeMenu.h" #include "guiPasswordChange.h" @@ -59,6 +59,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "minimap.h" #include "mapblock_mesh.h" +#if USE_FREETYPE + #include "util/statictext.h" +#endif + #include "sound.h" #if USE_SOUND @@ -2239,12 +2243,20 @@ bool Game::initGui() false, false, guiroot); guitext_status->setVisible(false); +#if USE_FREETYPE + // Colored chat support when using FreeType + guitext_chat = new gui::StaticText(L"", false, guienv, guiroot, -1, core::rect(0, 0, 0, 0), false); + guitext_chat->setWordWrap(true); + guitext_chat->drop(); +#else + // Standard chat when FreeType is disabled // Chat text guitext_chat = guienv->addStaticText( L"", core::rect(0, 0, 0, 0), //false, false); // Disable word wrap as of now false, true, guiroot); +#endif // Remove stale "recent" chat messages from previous connections chat_backend->clearRecentChat(); diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 0e7cbad07..e028a0435 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -1,7 +1,16 @@ +if(USE_FREETYPE) + set(UTIL_FREETYPEDEP_SRCS + ${CMAKE_CURRENT_SOURCE_DIR}/statictext.cpp + ) +else() + set(UTIL_FREETYPEDEP_SRCS ) +endif(USE_FREETYPE) + set(UTIL_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/areastore.cpp ${CMAKE_CURRENT_SOURCE_DIR}/auth.cpp ${CMAKE_CURRENT_SOURCE_DIR}/base64.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/coloredstring.cpp ${CMAKE_CURRENT_SOURCE_DIR}/directiontables.cpp ${CMAKE_CURRENT_SOURCE_DIR}/numeric.cpp ${CMAKE_CURRENT_SOURCE_DIR}/pointedthing.cpp @@ -11,5 +20,6 @@ set(UTIL_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/string.cpp ${CMAKE_CURRENT_SOURCE_DIR}/srp.cpp ${CMAKE_CURRENT_SOURCE_DIR}/timetaker.cpp + ${UTIL_FREETYPEDEP_SRCS} PARENT_SCOPE) diff --git a/src/util/coloredstring.cpp b/src/util/coloredstring.cpp new file mode 100644 index 000000000..7db586550 --- /dev/null +++ b/src/util/coloredstring.cpp @@ -0,0 +1,68 @@ +/* +Copyright (C) 2013 xyz, Ilya Zhuravlev + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "coloredstring.h" +#include "util/string.h" + +ColoredString::ColoredString() +{} + +ColoredString::ColoredString(const std::wstring &string, const std::vector &colors): + m_string(string), + m_colors(colors) +{} + +ColoredString::ColoredString(const std::wstring &s) { + m_string = colorizeText(s, m_colors, SColor(255, 255, 255, 255)); +} + +void ColoredString::operator=(const wchar_t *str) { + m_string = colorizeText(str, m_colors, SColor(255, 255, 255, 255)); +} + +size_t ColoredString::size() const { + return m_string.size(); +} + +ColoredString ColoredString::substr(size_t pos, size_t len) const { + if (pos == m_string.length()) + return ColoredString(); + if (len == std::string::npos || pos + len > m_string.length()) { + return ColoredString( + m_string.substr(pos, std::string::npos), + std::vector(m_colors.begin() + pos, m_colors.end()) + ); + } else { + return ColoredString( + m_string.substr(pos, len), + std::vector(m_colors.begin() + pos, m_colors.begin() + pos + len) + ); + } +} + +const wchar_t *ColoredString::c_str() const { + return m_string.c_str(); +} + +const std::vector &ColoredString::getColors() const { + return m_colors; +} + +const std::wstring &ColoredString::getString() const { + return m_string; +} diff --git a/src/util/coloredstring.h b/src/util/coloredstring.h new file mode 100644 index 000000000..a6d98db30 --- /dev/null +++ b/src/util/coloredstring.h @@ -0,0 +1,44 @@ +/* +Copyright (C) 2013 xyz, Ilya Zhuravlev + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef COLOREDSTRING_HEADER +#define COLOREDSTRING_HEADER + +#include +#include +#include + +using namespace irr::video; + +class ColoredString { +public: + ColoredString(); + ColoredString(const std::wstring &s); + ColoredString(const std::wstring &string, const std::vector &colors); + void operator=(const wchar_t *str); + size_t size() const; + ColoredString substr(size_t pos = 0, size_t len = std::string::npos) const; + const wchar_t *c_str() const; + const std::vector &getColors() const; + const std::wstring &getString() const; +private: + std::wstring m_string; + std::vector m_colors; +}; + +#endif diff --git a/src/util/statictext.cpp b/src/util/statictext.cpp new file mode 100644 index 000000000..b534b560e --- /dev/null +++ b/src/util/statictext.cpp @@ -0,0 +1,654 @@ +// Copyright (C) 2002-2012 Nikolaus Gebhardt +// This file is part of the "Irrlicht Engine". +// For conditions of distribution and use, see copyright notice in irrlicht.h + +#include "statictext.h" +#ifdef _IRR_COMPILE_WITH_GUI_ + +//Only compile this if freetype is enabled. + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cguittfont/xCGUITTFont.h" +#include "util/string.h" + +namespace irr +{ +namespace gui +{ +//! constructor +StaticText::StaticText(const wchar_t* text, bool border, + IGUIEnvironment* environment, IGUIElement* parent, + s32 id, const core::rect& rectangle, + bool background) +: IGUIStaticText(environment, parent, id, rectangle), + HAlign(EGUIA_UPPERLEFT), VAlign(EGUIA_UPPERLEFT), + Border(border), OverrideColorEnabled(false), OverrideBGColorEnabled(false), WordWrap(false), Background(background), + RestrainTextInside(true), RightToLeft(false), + OverrideColor(video::SColor(101,255,255,255)), BGColor(video::SColor(101,210,210,210)), + OverrideFont(0), LastBreakFont(0) +{ + #ifdef _DEBUG + setDebugName("StaticText"); + #endif + + Text = text; + if (environment && environment->getSkin()) + { + BGColor = environment->getSkin()->getColor(gui::EGDC_3D_FACE); + } +} + + +//! destructor +StaticText::~StaticText() +{ + if (OverrideFont) + OverrideFont->drop(); +} + + +//! draws the element and its children +void StaticText::draw() +{ + if (!IsVisible) + return; + + IGUISkin* skin = Environment->getSkin(); + if (!skin) + return; + video::IVideoDriver* driver = Environment->getVideoDriver(); + + core::rect frameRect(AbsoluteRect); + + // draw background + + if (Background) + { + if ( !OverrideBGColorEnabled ) // skin-colors can change + BGColor = skin->getColor(gui::EGDC_3D_FACE); + + driver->draw2DRectangle(BGColor, frameRect, &AbsoluteClippingRect); + } + + // draw the border + + if (Border) + { + skin->draw3DSunkenPane(this, 0, true, false, frameRect, &AbsoluteClippingRect); + frameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X); + } + + // draw the text + if (Text.size()) + { + IGUIFont* font = getActiveFont(); + + if (font) + { + if (!WordWrap) + { + // TODO: add colors here + if (VAlign == EGUIA_LOWERRIGHT) + { + frameRect.UpperLeftCorner.Y = frameRect.LowerRightCorner.Y - + font->getDimension(L"A").Height - font->getKerningHeight(); + } + if (HAlign == EGUIA_LOWERRIGHT) + { + frameRect.UpperLeftCorner.X = frameRect.LowerRightCorner.X - + font->getDimension(Text.c_str()).Width; + } + + font->draw(Text.c_str(), frameRect, + OverrideColorEnabled ? OverrideColor : skin->getColor(isEnabled() ? EGDC_BUTTON_TEXT : EGDC_GRAY_TEXT), + HAlign == EGUIA_CENTER, VAlign == EGUIA_CENTER, (RestrainTextInside ? &AbsoluteClippingRect : NULL)); + } + else + { + if (font != LastBreakFont) + breakText(); + + core::rect r = frameRect; + s32 height = font->getDimension(L"A").Height + font->getKerningHeight(); + s32 totalHeight = height * BrokenText.size(); + if (VAlign == EGUIA_CENTER) + { + r.UpperLeftCorner.Y = r.getCenter().Y - (totalHeight / 2); + } + else if (VAlign == EGUIA_LOWERRIGHT) + { + r.UpperLeftCorner.Y = r.LowerRightCorner.Y - totalHeight; + } + + irr::video::SColor previous_color(255, 255, 255, 255); + for (u32 i=0; igetDimension(BrokenText[i].c_str()).Width; + } + + std::vector colors; + std::wstring str; + + str = colorizeText(BrokenText[i].c_str(), colors, previous_color); + if (!colors.empty()) + previous_color = colors[colors.size() - 1]; + + irr::gui::CGUITTFont *tmp = static_cast(font); + tmp->draw(str.c_str(), r, + colors, + HAlign == EGUIA_CENTER, false, (RestrainTextInside ? &AbsoluteClippingRect : NULL)); + + r.LowerRightCorner.Y += height; + r.UpperLeftCorner.Y += height; + } + } + } + } + + IGUIElement::draw(); +} + + +//! Sets another skin independent font. +void StaticText::setOverrideFont(IGUIFont* font) +{ + if (OverrideFont == font) + return; + + if (OverrideFont) + OverrideFont->drop(); + + OverrideFont = font; + + if (OverrideFont) + OverrideFont->grab(); + + breakText(); +} + +//! Gets the override font (if any) +IGUIFont * StaticText::getOverrideFont() const +{ + return OverrideFont; +} + +//! Get the font which is used right now for drawing +IGUIFont* StaticText::getActiveFont() const +{ + if ( OverrideFont ) + return OverrideFont; + IGUISkin* skin = Environment->getSkin(); + if (skin) + return skin->getFont(); + return 0; +} + +//! Sets another color for the text. +void StaticText::setOverrideColor(video::SColor color) +{ + OverrideColor = color; + OverrideColorEnabled = true; +} + + +//! Sets another color for the text. +void StaticText::setBackgroundColor(video::SColor color) +{ + BGColor = color; + OverrideBGColorEnabled = true; + Background = true; +} + + +//! Sets whether to draw the background +void StaticText::setDrawBackground(bool draw) +{ + Background = draw; +} + + +//! Gets the background color +video::SColor StaticText::getBackgroundColor() const +{ + return BGColor; +} + + +//! Checks if background drawing is enabled +bool StaticText::isDrawBackgroundEnabled() const +{ + _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; + return Background; +} + + +//! Sets whether to draw the border +void StaticText::setDrawBorder(bool draw) +{ + Border = draw; +} + + +//! Checks if border drawing is enabled +bool StaticText::isDrawBorderEnabled() const +{ + _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; + return Border; +} + + +void StaticText::setTextRestrainedInside(bool restrainTextInside) +{ + RestrainTextInside = restrainTextInside; +} + + +bool StaticText::isTextRestrainedInside() const +{ + return RestrainTextInside; +} + + +void StaticText::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical) +{ + HAlign = horizontal; + VAlign = vertical; +} + + +#if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 7 +const video::SColor& StaticText::getOverrideColor() const +#else +video::SColor StaticText::getOverrideColor() const +#endif +{ + return OverrideColor; +} + + +//! Sets if the static text should use the overide color or the +//! color in the gui skin. +void StaticText::enableOverrideColor(bool enable) +{ + OverrideColorEnabled = enable; +} + + +bool StaticText::isOverrideColorEnabled() const +{ + _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; + return OverrideColorEnabled; +} + + +//! Enables or disables word wrap for using the static text as +//! multiline text control. +void StaticText::setWordWrap(bool enable) +{ + WordWrap = enable; + breakText(); +} + + +bool StaticText::isWordWrapEnabled() const +{ + _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; + return WordWrap; +} + + +void StaticText::setRightToLeft(bool rtl) +{ + if (RightToLeft != rtl) + { + RightToLeft = rtl; + breakText(); + } +} + + +bool StaticText::isRightToLeft() const +{ + return RightToLeft; +} + + +//! Breaks the single text line. +void StaticText::breakText() +{ + if (!WordWrap) + return; + + BrokenText.clear(); + + IGUISkin* skin = Environment->getSkin(); + IGUIFont* font = getActiveFont(); + if (!font) + return; + + LastBreakFont = font; + + core::stringw line; + core::stringw word; + core::stringw whitespace; + s32 size = Text.size(); + s32 length = 0; + s32 elWidth = RelativeRect.getWidth(); + if (Border) + elWidth -= 2*skin->getSize(EGDS_TEXT_DISTANCE_X); + wchar_t c; + + std::vector colors; + + // We have to deal with right-to-left and left-to-right differently + // However, most parts of the following code is the same, it's just + // some order and boundaries which change. + if (!RightToLeft) + { + // regular (left-to-right) + for (s32 i=0; igetDimension(whitespace.c_str()).Width; + const std::wstring sanitized = removeEscapes(word.c_str()); + const s32 wordlgth = font->getDimension(sanitized.c_str()).Width; + + if (wordlgth > elWidth) + { + // This word is too long to fit in the available space, look for + // the Unicode Soft HYphen (SHY / 00AD) character for a place to + // break the word at + int where = word.findFirst( wchar_t(0x00AD) ); + if (where != -1) + { + core::stringw first = word.subString(0, where); + core::stringw second = word.subString(where, word.size() - where); + BrokenText.push_back(line + first + L"-"); + const s32 secondLength = font->getDimension(second.c_str()).Width; + + length = secondLength; + line = second; + } + else + { + // No soft hyphen found, so there's nothing more we can do + // break to next line + if (length) + BrokenText.push_back(line); + length = wordlgth; + line = word; + } + } + else if (length && (length + wordlgth + whitelgth > elWidth)) + { + // break to next line + BrokenText.push_back(line); + length = wordlgth; + line = word; + } + else + { + // add word to line + line += whitespace; + line += word; + length += whitelgth + wordlgth; + } + + word = L""; + whitespace = L""; + } + + if ( isWhitespace ) + { + whitespace += c; + } + + // compute line break + if (lineBreak) + { + line += whitespace; + line += word; + BrokenText.push_back(line); + line = L""; + word = L""; + whitespace = L""; + length = 0; + } + } + } + + line += whitespace; + line += word; + BrokenText.push_back(line); + } + else + { + // right-to-left + for (s32 i=size; i>=0; --i) + { + c = Text[i]; + bool lineBreak = false; + + if (c == L'\r') // Mac or Windows breaks + { + lineBreak = true; + if ((i>0) && Text[i-1] == L'\n') // Windows breaks + { + Text.erase(i-1); + --size; + } + c = '\0'; + } + else if (c == L'\n') // Unix breaks + { + lineBreak = true; + c = '\0'; + } + + if (c==L' ' || c==0 || i==0) + { + if (word.size()) + { + // here comes the next whitespace, look if + // we must break the last word to the next line. + const s32 whitelgth = font->getDimension(whitespace.c_str()).Width; + const s32 wordlgth = font->getDimension(word.c_str()).Width; + + if (length && (length + wordlgth + whitelgth > elWidth)) + { + // break to next line + BrokenText.push_back(line); + length = wordlgth; + line = word; + } + else + { + // add word to line + line = whitespace + line; + line = word + line; + length += whitelgth + wordlgth; + } + + word = L""; + whitespace = L""; + } + + if (c != 0) + whitespace = core::stringw(&c, 1) + whitespace; + + // compute line break + if (lineBreak) + { + line = whitespace + line; + line = word + line; + BrokenText.push_back(line); + line = L""; + word = L""; + whitespace = L""; + length = 0; + } + } + else + { + // yippee this is a word.. + word = core::stringw(&c, 1) + word; + } + } + + line = whitespace + line; + line = word + line; + BrokenText.push_back(line); + } +} + + +//! Sets the new caption of this element. +void StaticText::setText(const wchar_t* text) +{ + IGUIElement::setText(text); + breakText(); +} + + +void StaticText::updateAbsolutePosition() +{ + IGUIElement::updateAbsolutePosition(); + breakText(); +} + + +//! Returns the height of the text in pixels when it is drawn. +s32 StaticText::getTextHeight() const +{ + IGUIFont* font = getActiveFont(); + if (!font) + return 0; + + s32 height = font->getDimension(L"A").Height + font->getKerningHeight(); + + if (WordWrap) + height *= BrokenText.size(); + + return height; +} + + +s32 StaticText::getTextWidth() const +{ + IGUIFont * font = getActiveFont(); + if(!font) + return 0; + + if(WordWrap) + { + s32 widest = 0; + + for(u32 line = 0; line < BrokenText.size(); ++line) + { + s32 width = font->getDimension(BrokenText[line].c_str()).Width; + + if(width > widest) + widest = width; + } + + return widest; + } + else + { + return font->getDimension(Text.c_str()).Width; + } +} + + +//! Writes attributes of the element. +//! Implement this to expose the attributes of your element for +//! scripting languages, editors, debuggers or xml serialization purposes. +void StaticText::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const +{ + IGUIStaticText::serializeAttributes(out,options); + + out->addBool ("Border", Border); + out->addBool ("OverrideColorEnabled",OverrideColorEnabled); + out->addBool ("OverrideBGColorEnabled",OverrideBGColorEnabled); + out->addBool ("WordWrap", WordWrap); + out->addBool ("Background", Background); + out->addBool ("RightToLeft", RightToLeft); + out->addBool ("RestrainTextInside", RestrainTextInside); + out->addColor ("OverrideColor", OverrideColor); + out->addColor ("BGColor", BGColor); + out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames); + out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames); + + // out->addFont ("OverrideFont", OverrideFont); +} + + +//! Reads attributes of the element +void StaticText::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0) +{ + IGUIStaticText::deserializeAttributes(in,options); + + Border = in->getAttributeAsBool("Border"); + enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled")); + OverrideBGColorEnabled = in->getAttributeAsBool("OverrideBGColorEnabled"); + setWordWrap(in->getAttributeAsBool("WordWrap")); + Background = in->getAttributeAsBool("Background"); + RightToLeft = in->getAttributeAsBool("RightToLeft"); + RestrainTextInside = in->getAttributeAsBool("RestrainTextInside"); + OverrideColor = in->getAttributeAsColor("OverrideColor"); + BGColor = in->getAttributeAsColor("BGColor"); + + setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames), + (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames)); + + // OverrideFont = in->getAttributeAsFont("OverrideFont"); +} + +} // end namespace gui +} // end namespace irr + + +#endif // _IRR_COMPILE_WITH_GUI_ diff --git a/src/util/statictext.h b/src/util/statictext.h new file mode 100644 index 000000000..8d2f879e7 --- /dev/null +++ b/src/util/statictext.h @@ -0,0 +1,150 @@ +// Copyright (C) 2002-2012 Nikolaus Gebhardt +// This file is part of the "Irrlicht Engine". +// For conditions of distribution and use, see copyright notice in irrlicht.h + +#ifndef __C_GUI_STATIC_TEXT_H_INCLUDED__ +#define __C_GUI_STATIC_TEXT_H_INCLUDED__ + +#include "IrrCompileConfig.h" +#ifdef _IRR_COMPILE_WITH_GUI_ + +#include "IGUIStaticText.h" +#include "irrArray.h" + +#include + +namespace irr +{ +namespace gui +{ + class StaticText : public IGUIStaticText + { + public: + + //! constructor + StaticText(const wchar_t* text, bool border, IGUIEnvironment* environment, + IGUIElement* parent, s32 id, const core::rect& rectangle, + bool background = false); + + //! destructor + virtual ~StaticText(); + + //! draws the element and its children + virtual void draw(); + + //! Sets another skin independent font. + virtual void setOverrideFont(IGUIFont* font=0); + + //! Gets the override font (if any) + virtual IGUIFont* getOverrideFont() const; + + //! Get the font which is used right now for drawing + virtual IGUIFont* getActiveFont() const; + + //! Sets another color for the text. + virtual void setOverrideColor(video::SColor color); + + //! Sets another color for the background. + virtual void setBackgroundColor(video::SColor color); + + //! Sets whether to draw the background + virtual void setDrawBackground(bool draw); + + //! Gets the background color + virtual video::SColor getBackgroundColor() const; + + //! Checks if background drawing is enabled + virtual bool isDrawBackgroundEnabled() const; + + //! Sets whether to draw the border + virtual void setDrawBorder(bool draw); + + //! Checks if border drawing is enabled + virtual bool isDrawBorderEnabled() const; + + //! Sets alignment mode for text + virtual void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical); + + //! Gets the override color + #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 7 + virtual const video::SColor& getOverrideColor() const; + #else + virtual video::SColor getOverrideColor() const; + #endif + + //! Sets if the static text should use the overide color or the + //! color in the gui skin. + virtual void enableOverrideColor(bool enable); + + //! Checks if an override color is enabled + virtual bool isOverrideColorEnabled() const; + + //! Set whether the text in this label should be clipped if it goes outside bounds + virtual void setTextRestrainedInside(bool restrainedInside); + + //! Checks if the text in this label should be clipped if it goes outside bounds + virtual bool isTextRestrainedInside() const; + + //! Enables or disables word wrap for using the static text as + //! multiline text control. + virtual void setWordWrap(bool enable); + + //! Checks if word wrap is enabled + virtual bool isWordWrapEnabled() const; + + //! Sets the new caption of this element. + virtual void setText(const wchar_t* text); + + //! Returns the height of the text in pixels when it is drawn. + virtual s32 getTextHeight() const; + + //! Returns the width of the current text, in the current font + virtual s32 getTextWidth() const; + + //! Updates the absolute position, splits text if word wrap is enabled + virtual void updateAbsolutePosition(); + + //! Set whether the string should be interpreted as right-to-left (RTL) text + /** \note This component does not implement the Unicode bidi standard, the + text of the component should be already RTL if you call this. The + main difference when RTL is enabled is that the linebreaks for multiline + elements are performed starting from the end. + */ + virtual void setRightToLeft(bool rtl); + + //! Checks if the text should be interpreted as right-to-left text + virtual bool isRightToLeft() const; + + //! Writes attributes of the element. + virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const; + + //! Reads attributes of the element + virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options); + + private: + + //! Breaks the single text line. + void breakText(); + + EGUI_ALIGNMENT HAlign, VAlign; + bool Border; + bool OverrideColorEnabled; + bool OverrideBGColorEnabled; + bool WordWrap; + bool Background; + bool RestrainTextInside; + bool RightToLeft; + + video::SColor OverrideColor, BGColor; + gui::IGUIFont* OverrideFont; + gui::IGUIFont* LastBreakFont; // stored because: if skin changes, line break must be recalculated. + + core::array< core::stringw > BrokenText; + }; + +} // end namespace gui +} // end namespace irr + +#endif // _IRR_COMPILE_WITH_GUI_ + +#endif // C_GUI_STATIC_TEXT_H_INCLUDED From 14ef2b445adcec770defe1abf83af9d22ccf39d8 Mon Sep 17 00:00:00 2001 From: Ekdohibs Date: Tue, 31 May 2016 17:30:11 +0200 Subject: [PATCH 049/418] Add colored text (not only colored chat). Add documentation, move files to a proper place and avoid memory leaks. Make it work with most kind of texts, and allow backgrounds too. --- builtin/game/chatcommands.lua | 2 +- builtin/game/misc.lua | 41 +++-- builtin/settingtypes.txt | 5 + doc/lua_api.txt | 18 ++ src/CMakeLists.txt | 2 + src/cguittfont/CGUITTFont.cpp | 30 +++- src/cguittfont/CGUITTFont.h | 7 + src/chat.cpp | 37 ++-- src/chat.h | 17 +- src/client/CMakeLists.txt | 1 - src/defaultsettings.cpp | 2 + src/game.cpp | 53 +++--- src/{client => }/guiChatConsole.cpp | 4 +- src/{client => }/guiChatConsole.h | 0 src/guiEngine.cpp | 23 +-- src/guiEngine.h | 3 + src/guiFormSpecMenu.cpp | 59 ++----- src/guiFormSpecMenu.h | 13 +- src/irrlicht_changes/CMakeLists.txt | 7 + .../static_text.cpp} | 139 +++++++++------ .../static_text.h} | 122 ++++++++++++- src/terminal_chat_console.cpp | 10 +- src/util/CMakeLists.txt | 11 +- src/util/coloredstring.cpp | 68 ------- src/util/coloredstring.h | 44 ----- src/util/enriched_string.cpp | 166 ++++++++++++++++++ src/util/enriched_string.h | 91 ++++++++++ src/util/string.h | 32 ++++ 28 files changed, 689 insertions(+), 318 deletions(-) rename src/{client => }/guiChatConsole.cpp (99%) rename src/{client => }/guiChatConsole.h (100%) create mode 100644 src/irrlicht_changes/CMakeLists.txt rename src/{util/statictext.cpp => irrlicht_changes/static_text.cpp} (83%) rename src/{util/statictext.h => irrlicht_changes/static_text.h} (54%) delete mode 100644 src/util/coloredstring.cpp delete mode 100644 src/util/coloredstring.h create mode 100644 src/util/enriched_string.cpp create mode 100644 src/util/enriched_string.h diff --git a/builtin/game/chatcommands.lua b/builtin/game/chatcommands.lua index 2627559a5..22755386b 100644 --- a/builtin/game/chatcommands.lua +++ b/builtin/game/chatcommands.lua @@ -102,7 +102,7 @@ core.register_chatcommand("help", { description = "Get help for commands or list privileges", func = function(name, param) local function format_help_line(cmd, def) - local msg = core.colorize("00ffff", "/"..cmd) + local msg = core.colorize("#00ffff", "/"..cmd) if def.params and def.params ~= "" then msg = msg .. " " .. def.params end diff --git a/builtin/game/misc.lua b/builtin/game/misc.lua index 8d5c80216..918315656 100644 --- a/builtin/game/misc.lua +++ b/builtin/game/misc.lua @@ -198,19 +198,34 @@ function core.http_add_fetch(httpenv) return httpenv end -function core.get_color_escape_sequence(color) - --if string.len(color) == 3 then - -- local r = string.sub(color, 1, 1) - -- local g = string.sub(color, 2, 2) - -- local b = string.sub(color, 3, 3) - -- color = r .. r .. g .. g .. b .. b - --end +if minetest.setting_getbool("disable_escape_sequences") then + + function core.get_color_escape_sequence(color) + return "" + end + + function core.get_background_escape_sequence(color) + return "" + end + + function core.colorize(color, message) + return message + end + +else + + local ESCAPE_CHAR = string.char(0x1b) + function core.get_color_escape_sequence(color) + return ESCAPE_CHAR .. "(c@" .. color .. ")" + end + + function core.get_background_escape_sequence(color) + return ESCAPE_CHAR .. "(b@" .. color .. ")" + end + + function core.colorize(color, message) + return core.get_color_escape_sequence(color) .. message .. core.get_color_escape_sequence("#ffffff") + end - --assert(#color == 6, "Color must be six characters in length.") - --return "\v" .. color - return "\v(color;" .. color .. ")" end -function core.colorize(color, message) - return core.get_color_escape_sequence(color) .. message .. core.get_color_escape_sequence("ffffff") -end diff --git a/builtin/settingtypes.txt b/builtin/settingtypes.txt index 93fb8e952..538a04f33 100644 --- a/builtin/settingtypes.txt +++ b/builtin/settingtypes.txt @@ -615,6 +615,11 @@ server_announce (Announce server) bool false # If you want to announce your ipv6 address, use serverlist_url = v6.servers.minetest.net. serverlist_url (Serverlist URL) string servers.minetest.net +# Disable escape sequences, e.g. chat coloring. +# Use this if you want to run a server with pre-0.4.14 clients and you want to disable +# the escape sequences generated by mods. +disable_escape_sequences (Disable escape sequences) bool false + [*Network] # Network port to listen (UDP). diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 82a0acbee..aa0d7e45d 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -1701,6 +1701,24 @@ numerical form, the raw integer value of an ARGB8 quad: or string form, a ColorString (defined above): `colorspec = "green"` +Escape sequences +---------------- +Most text can contain escape sequences, that can for example color the text. +There are a few exceptions: tab headers, dropdowns and vertical labels can't. +The following functions provide escape sequences: +* `core.get_color_escape_sequence(color)`: + * `color` is a ColorString + * The escape sequence sets the text color to `color` +* `core.colorize(color, message)`: + * Equivalent to: + `core.get_color_escape_sequence(color) .. + message .. + core.get_color_escape_sequence("#ffffff")` +* `color.get_background_escape_sequence(color)` + * `color` is a ColorString + * The escape sequence sets the background of the whole text element to + `color`. Only defined for item descriptions and tooltips. + Spatial Vectors --------------- * `vector.new(a[, b, c])`: returns a vector: diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ea1564eeb..f02812415 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -376,6 +376,7 @@ add_subdirectory(network) add_subdirectory(script) add_subdirectory(unittest) add_subdirectory(util) +add_subdirectory(irrlicht_changes) set(common_SRCS ban.cpp @@ -493,6 +494,7 @@ set(client_SRCS ${common_SRCS} ${sound_SRCS} ${client_network_SRCS} + ${client_irrlicht_changes_SRCS} camera.cpp client.cpp clientmap.cpp diff --git a/src/cguittfont/CGUITTFont.cpp b/src/cguittfont/CGUITTFont.cpp index 2342eb748..c2d37c6c0 100644 --- a/src/cguittfont/CGUITTFont.cpp +++ b/src/cguittfont/CGUITTFont.cpp @@ -1,6 +1,7 @@ /* CGUITTFont FreeType class for Irrlicht Copyright (c) 2009-2010 John Norman + Copyright (c) 2016 Nathanaël Courant This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any @@ -545,6 +546,13 @@ void CGUITTFont::setFontHinting(const bool enable, const bool enable_auto_hintin void CGUITTFont::draw(const core::stringw& text, const core::rect& position, video::SColor color, bool hcenter, bool vcenter, const core::rect* clip) { + draw(EnrichedString(std::wstring(text.c_str()), color), position, color, hcenter, vcenter, clip); +} + +void CGUITTFont::draw(const EnrichedString &text, const core::rect& position, video::SColor color, bool hcenter, bool vcenter, const core::rect* clip) +{ + std::vector colors = text.getColors(); + if (!Driver) return; @@ -572,7 +580,7 @@ void CGUITTFont::draw(const core::stringw& text, const core::rect& position } // Convert to a unicode string. - core::ustring utext(text); + core::ustring utext = text.getString(); // Set up our render map. core::map Render_Map; @@ -581,6 +589,7 @@ void CGUITTFont::draw(const core::stringw& text, const core::rect& position u32 n; uchar32_t previousChar = 0; core::ustring::const_iterator iter(utext); + std::vector applied_colors; while (!iter.atEnd()) { uchar32_t currentChar = *iter; @@ -590,7 +599,7 @@ void CGUITTFont::draw(const core::stringw& text, const core::rect& position if (currentChar == L'\r') // Mac or Windows breaks { lineBreak = true; - if (*(iter + 1) == (uchar32_t)'\n') // Windows line breaks. + if (*(iter + 1) == (uchar32_t)'\n') // Windows line breaks. currentChar = *(++iter); } else if (currentChar == (uchar32_t)'\n') // Unix breaks @@ -627,6 +636,9 @@ void CGUITTFont::draw(const core::stringw& text, const core::rect& position page->render_positions.push_back(core::position2di(offset.X + offx, offset.Y + offy)); page->render_source_rects.push_back(glyph.source_rect); Render_Map.set(glyph.glyph_page, page); + u32 current_color = iter.getPos(); + if (current_color < colors.size()) + applied_colors.push_back(colors[current_color]); } offset.X += getWidthFromCharacter(currentChar); @@ -645,8 +657,6 @@ void CGUITTFont::draw(const core::stringw& text, const core::rect& position CGUITTGlyphPage* page = n->getValue(); - if (!use_transparency) color.color |= 0xff000000; - if (shadow_offset) { for (size_t i = 0; i < page->render_positions.size(); ++i) page->render_positions[i] += core::vector2di(shadow_offset, shadow_offset); @@ -654,7 +664,17 @@ void CGUITTFont::draw(const core::stringw& text, const core::rect& position for (size_t i = 0; i < page->render_positions.size(); ++i) page->render_positions[i] -= core::vector2di(shadow_offset, shadow_offset); } - Driver->draw2DImageBatch(page->texture, page->render_positions, page->render_source_rects, clip, color, true); + for (size_t i = 0; i < page->render_positions.size(); ++i) { + irr::video::SColor col; + if (!applied_colors.empty()) { + col = applied_colors[i < applied_colors.size() ? i : 0]; + } else { + col = irr::video::SColor(255, 255, 255, 255); + } + if (!use_transparency) + col.color |= 0xff000000; + Driver->draw2DImage(page->texture, page->render_positions[i], page->render_source_rects[i], clip, col, true); + } } } diff --git a/src/cguittfont/CGUITTFont.h b/src/cguittfont/CGUITTFont.h index e24d8f18b..0aa540c5c 100644 --- a/src/cguittfont/CGUITTFont.h +++ b/src/cguittfont/CGUITTFont.h @@ -1,6 +1,7 @@ /* CGUITTFont FreeType class for Irrlicht Copyright (c) 2009-2010 John Norman + Copyright (c) 2016 Nathanaël Courant This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any @@ -33,6 +34,8 @@ #include #include +#include +#include "util/enriched_string.h" #include FT_FREETYPE_H namespace irr @@ -258,6 +261,10 @@ namespace gui virtual void draw(const core::stringw& text, const core::rect& position, video::SColor color, bool hcenter=false, bool vcenter=false, const core::rect* clip=0); + + virtual void draw(const EnrichedString& text, const core::rect& position, + video::SColor color, bool hcenter=false, bool vcenter=false, + const core::rect* clip=0); //! Returns the dimension of a character produced by this font. virtual core::dimension2d getCharDimension(const wchar_t ch) const; diff --git a/src/chat.cpp b/src/chat.cpp index 958389df5..46555b3dc 100644 --- a/src/chat.cpp +++ b/src/chat.cpp @@ -267,28 +267,26 @@ u32 ChatBuffer::formatChatLine(const ChatLine& line, u32 cols, next_frags.push_back(temp_frag); } - std::wstring name_sanitized = removeEscapes(line.name); + std::wstring name_sanitized = line.name.c_str(); // Choose an indentation level if (line.name.empty()) { // Server messages hanging_indentation = 0; - } - else if (name_sanitized.size() + 3 <= cols/2) { + } else if (name_sanitized.size() + 3 <= cols/2) { // Names shorter than about half the console width hanging_indentation = line.name.size() + 3; - } - else { + } else { // Very long names hanging_indentation = 2; } - ColoredString line_text(line.text); + //EnrichedString line_text(line.text); next_line.first = true; bool text_processing = false; // Produce fragments and layout them into lines - while (!next_frags.empty() || in_pos < line_text.size()) + while (!next_frags.empty() || in_pos < line.text.size()) { // Layout fragments into lines while (!next_frags.empty()) @@ -326,9 +324,9 @@ u32 ChatBuffer::formatChatLine(const ChatLine& line, u32 cols, } // Produce fragment - if (in_pos < line_text.size()) + if (in_pos < line.text.size()) { - u32 remaining_in_input = line_text.size() - in_pos; + u32 remaining_in_input = line.text.size() - in_pos; u32 remaining_in_output = cols - out_column; // Determine a fragment length <= the minimum of @@ -338,14 +336,14 @@ u32 ChatBuffer::formatChatLine(const ChatLine& line, u32 cols, while (frag_length < remaining_in_input && frag_length < remaining_in_output) { - if (isspace(line_text[in_pos + frag_length])) + if (isspace(line.text.getString()[in_pos + frag_length])) space_pos = frag_length; ++frag_length; } if (space_pos != 0 && frag_length < remaining_in_input) frag_length = space_pos + 1; - temp_frag.text = line_text.substr(in_pos, frag_length); + temp_frag.text = line.text.substr(in_pos, frag_length); temp_frag.column = 0; //temp_frag.bold = 0; next_frags.push_back(temp_frag); @@ -729,19 +727,22 @@ ChatBuffer& ChatBackend::getRecentBuffer() return m_recent_buffer; } -std::wstring ChatBackend::getRecentChat() +EnrichedString ChatBackend::getRecentChat() { - std::wostringstream stream; + EnrichedString result; for (u32 i = 0; i < m_recent_buffer.getLineCount(); ++i) { const ChatLine& line = m_recent_buffer.getLine(i); if (i != 0) - stream << L"\n"; - if (!line.name.empty()) - stream << L"<" << line.name << L"> "; - stream << line.text; + result += L"\n"; + if (!line.name.empty()) { + result += L"<"; + result += line.name; + result += L"> "; + } + result += line.text; } - return stream.str(); + return result; } ChatPrompt& ChatBackend::getPrompt() diff --git a/src/chat.h b/src/chat.h index 661cafc82..11061fd39 100644 --- a/src/chat.h +++ b/src/chat.h @@ -25,7 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include "irrlichttypes.h" -#include "util/coloredstring.h" +#include "util/enriched_string.h" // Chat console related classes @@ -34,9 +34,9 @@ struct ChatLine // age in seconds f32 age; // name of sending player, or empty if sent by server - std::wstring name; + EnrichedString name; // message text - ColoredString text; + EnrichedString text; ChatLine(std::wstring a_name, std::wstring a_text): age(0.0), @@ -44,12 +44,19 @@ struct ChatLine text(a_text) { } + + ChatLine(EnrichedString a_name, EnrichedString a_text): + age(0.0), + name(a_name), + text(a_text) + { + } }; struct ChatFormattedFragment { // text string - std::wstring text; + EnrichedString text; // starting column u32 column; // formatting @@ -262,7 +269,7 @@ public: // Get the recent messages buffer ChatBuffer& getRecentBuffer(); // Concatenate all recent messages - std::wstring getRecentChat(); + EnrichedString getRecentChat(); // Get the console prompt ChatPrompt& getPrompt(); diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index bcf114760..a1ec37fe3 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -1,6 +1,5 @@ set(client_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/clientlauncher.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/guiChatConsole.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tile.cpp PARENT_SCOPE ) diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index e8b652c17..632ec0df9 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -202,6 +202,8 @@ void set_default_settings(Settings *settings) settings->setDefault("server_name", ""); settings->setDefault("server_description", ""); + settings->setDefault("disable_escape_sequences", "false"); + #if USE_FREETYPE settings->setDefault("freetype", "true"); settings->setDefault("font_path", porting::getDataPath("fonts" DIR_DELIM "liberationsans.ttf")); diff --git a/src/game.cpp b/src/game.cpp index 71a04aef5..def202fe5 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -34,7 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "log.h" #include "filesys.h" #include "gettext.h" -#include "client/guiChatConsole.h" +#include "guiChatConsole.h" #include "guiFormSpecMenu.h" #include "guiKeyChangeMenu.h" #include "guiPasswordChange.h" @@ -55,14 +55,11 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "tool.h" #include "util/directiontables.h" #include "util/pointedthing.h" +#include "irrlicht_changes/static_text.h" #include "version.h" #include "minimap.h" #include "mapblock_mesh.h" -#if USE_FREETYPE - #include "util/statictext.h" -#endif - #include "sound.h" #if USE_SOUND @@ -541,7 +538,7 @@ void update_profiler_gui(gui::IGUIStaticText *guitext_profiler, FontEngine *fe, std::ostringstream os(std::ios_base::binary); g_profiler->printPage(os, show_profiler, show_profiler_max); std::wstring text = utf8_to_wide(os.str()); - guitext_profiler->setText(text.c_str()); + setStaticText(guitext_profiler, text.c_str()); guitext_profiler->setVisible(true); s32 w = fe->getTextWidth(text.c_str()); @@ -1244,7 +1241,11 @@ static void updateChat(Client &client, f32 dtime, bool show_debug, // Get new messages from error log buffer while (!chat_log_error_buf.empty()) { - chat_backend.addMessage(L"", utf8_to_wide(chat_log_error_buf.get())); + std::wstring error_message = utf8_to_wide(chat_log_error_buf.get()); + if (!g_settings->getBool("disable_escape_sequences")) { + error_message = L"\x1b(c@red)" + error_message + L"\x1b(c@white)"; + } + chat_backend.addMessage(L"", error_message); } // Get new messages from client @@ -1259,10 +1260,10 @@ static void updateChat(Client &client, f32 dtime, bool show_debug, // Display all messages in a static text element unsigned int recent_chat_count = chat_backend.getRecentBuffer().getLineCount(); - std::wstring recent_chat = chat_backend.getRecentChat(); + EnrichedString recent_chat = chat_backend.getRecentChat(); unsigned int line_height = g_fontengine->getLineHeight(); - guitext_chat->setText(recent_chat.c_str()); + setStaticText(guitext_chat, recent_chat); // Update gui element size and position s32 chat_y = 5 + line_height; @@ -1271,7 +1272,7 @@ static void updateChat(Client &client, f32 dtime, bool show_debug, chat_y += line_height; // first pass to calculate height of text to be set - s32 width = std::min(g_fontengine->getTextWidth(recent_chat) + 10, + s32 width = std::min(g_fontengine->getTextWidth(recent_chat.c_str()) + 10, porting::getWindowSize().X - 20); core::rect rect(10, chat_y, width, chat_y + porting::getWindowSize().Y); guitext_chat->setRelativePosition(rect); @@ -2218,45 +2219,39 @@ bool Game::createClient(const std::string &playername, bool Game::initGui() { // First line of debug text - guitext = guienv->addStaticText( + guitext = addStaticText(guienv, utf8_to_wide(PROJECT_NAME_C).c_str(), core::rect(0, 0, 0, 0), false, false, guiroot); // Second line of debug text - guitext2 = guienv->addStaticText( + guitext2 = addStaticText(guienv, L"", core::rect(0, 0, 0, 0), false, false, guiroot); // At the middle of the screen // Object infos are shown in this - guitext_info = guienv->addStaticText( + guitext_info = addStaticText(guienv, L"", core::rect(0, 0, 400, g_fontengine->getTextHeight() * 5 + 5) + v2s32(100, 200), false, true, guiroot); // Status text (displays info when showing and hiding GUI stuff, etc.) - guitext_status = guienv->addStaticText( + guitext_status = addStaticText(guienv, L"", core::rect(0, 0, 0, 0), false, false, guiroot); guitext_status->setVisible(false); -#if USE_FREETYPE - // Colored chat support when using FreeType - guitext_chat = new gui::StaticText(L"", false, guienv, guiroot, -1, core::rect(0, 0, 0, 0), false); - guitext_chat->setWordWrap(true); - guitext_chat->drop(); -#else - // Standard chat when FreeType is disabled // Chat text - guitext_chat = guienv->addStaticText( + guitext_chat = addStaticText( + guienv, L"", core::rect(0, 0, 0, 0), //false, false); // Disable word wrap as of now false, true, guiroot); -#endif + // Remove stale "recent" chat messages from previous connections chat_backend->clearRecentChat(); @@ -2270,7 +2265,7 @@ bool Game::initGui() } // Profiler text (size is updated when text is updated) - guitext_profiler = guienv->addStaticText( + guitext_profiler = addStaticText(guienv, L"", core::rect(0, 0, 0, 0), false, false, guiroot); @@ -4308,12 +4303,12 @@ void Game::updateGui(float *statustext_time, const RunStats &stats, << ", v_range = " << draw_control->wanted_range << std::setprecision(3) << ", RTT = " << client->getRTT(); - guitext->setText(utf8_to_wide(os.str()).c_str()); + setStaticText(guitext, utf8_to_wide(os.str()).c_str()); guitext->setVisible(true); } else if (flags.show_hud || flags.show_chat) { std::ostringstream os(std::ios_base::binary); os << PROJECT_NAME_C " " << g_version_hash; - guitext->setText(utf8_to_wide(os.str()).c_str()); + setStaticText(guitext, utf8_to_wide(os.str()).c_str()); guitext->setVisible(true); } else { guitext->setVisible(false); @@ -4350,7 +4345,7 @@ void Game::updateGui(float *statustext_time, const RunStats &stats, } } - guitext2->setText(utf8_to_wide(os.str()).c_str()); + setStaticText(guitext2, utf8_to_wide(os.str()).c_str()); guitext2->setVisible(true); core::rect rect( @@ -4362,7 +4357,7 @@ void Game::updateGui(float *statustext_time, const RunStats &stats, guitext2->setVisible(false); } - guitext_info->setText(infotext.c_str()); + setStaticText(guitext_info, infotext.c_str()); guitext_info->setVisible(flags.show_hud && g_menumgr.menuCount() == 0); float statustext_time_max = 1.5; @@ -4376,7 +4371,7 @@ void Game::updateGui(float *statustext_time, const RunStats &stats, } } - guitext_status->setText(statustext.c_str()); + setStaticText(guitext_status, statustext.c_str()); guitext_status->setVisible(!statustext.empty()); if (!statustext.empty()) { diff --git a/src/client/guiChatConsole.cpp b/src/guiChatConsole.cpp similarity index 99% rename from src/client/guiChatConsole.cpp rename to src/guiChatConsole.cpp index d8837556a..bb58d1305 100644 --- a/src/client/guiChatConsole.cpp +++ b/src/guiChatConsole.cpp @@ -346,9 +346,9 @@ void GUIChatConsole::drawText() // Draw colored text if FreeType is enabled irr::gui::CGUITTFont *tmp = static_cast(m_font); tmp->draw( - fragment.text.c_str(), + fragment.text, destrect, - fragment.text.getColors(), + video::SColor(255, 255, 255, 255), false, false, &AbsoluteClippingRect); diff --git a/src/client/guiChatConsole.h b/src/guiChatConsole.h similarity index 100% rename from src/client/guiChatConsole.h rename to src/guiChatConsole.h diff --git a/src/guiEngine.cpp b/src/guiEngine.cpp index ba286a91c..96de7a4f7 100644 --- a/src/guiEngine.cpp +++ b/src/guiEngine.cpp @@ -37,6 +37,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "log.h" #include "fontengine.h" #include "guiscalingfilter.h" +#include "irrlicht_changes/static_text.h" #ifdef __ANDROID__ #include "client/tile.h" @@ -172,15 +173,16 @@ GUIEngine::GUIEngine( irr::IrrlichtDevice* dev, m_sound_manager = &dummySoundManager; //create topleft header - std::wstring t = utf8_to_wide(std::string(PROJECT_NAME_C " ") + + m_toplefttext = utf8_to_wide(std::string(PROJECT_NAME_C " ") + g_version_hash); - core::rect rect(0, 0, g_fontengine->getTextWidth(t), g_fontengine->getTextHeight()); + core::rect rect(0, 0, g_fontengine->getTextWidth(m_toplefttext.c_str()), + g_fontengine->getTextHeight()); rect += v2s32(4, 0); m_irr_toplefttext = - m_device->getGUIEnvironment()->addStaticText(t.c_str(), - rect,false,true,0,-1); + addStaticText(m_device->getGUIEnvironment(), m_toplefttext, + rect, false, true, 0, -1); //create formspecsource m_formspecgui = new FormspecFormSource(""); @@ -578,7 +580,7 @@ void GUIEngine::setTopleftText(std::string append) toset += utf8_to_wide(append); } - m_irr_toplefttext->setText(toset.c_str()); + m_toplefttext = toset; updateTopLeftTextSize(); } @@ -586,15 +588,14 @@ void GUIEngine::setTopleftText(std::string append) /******************************************************************************/ void GUIEngine::updateTopLeftTextSize() { - std::wstring text = m_irr_toplefttext->getText(); - - core::rect rect(0, 0, g_fontengine->getTextWidth(text), g_fontengine->getTextHeight()); - rect += v2s32(4, 0); + core::rect rect(0, 0, g_fontengine->getTextWidth(m_toplefttext.c_str()), + g_fontengine->getTextHeight()); + rect += v2s32(4, 0); m_irr_toplefttext->remove(); m_irr_toplefttext = - m_device->getGUIEnvironment()->addStaticText(text.c_str(), - rect,false,true,0,-1); + addStaticText(m_device->getGUIEnvironment(), m_toplefttext, + rect, false, true, 0, -1); } /******************************************************************************/ diff --git a/src/guiEngine.h b/src/guiEngine.h index d527f7222..fa98a21e4 100644 --- a/src/guiEngine.h +++ b/src/guiEngine.h @@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "guiFormSpecMenu.h" #include "sound.h" #include "client/tile.h" +#include "util/enriched_string.h" /******************************************************************************/ /* Typedefs and macros */ @@ -275,6 +276,8 @@ private: /** pointer to gui element shown at topleft corner */ irr::gui::IGUIStaticText* m_irr_toplefttext; + /** and text that is in it */ + EnrichedString m_toplefttext; /** initialize cloud subsystem */ void cloudInit(); diff --git a/src/guiFormSpecMenu.cpp b/src/guiFormSpecMenu.cpp index 99b1153c1..cf01dc38c 100644 --- a/src/guiFormSpecMenu.cpp +++ b/src/guiFormSpecMenu.cpp @@ -50,6 +50,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/hex.h" #include "util/numeric.h" #include "util/string.h" // for parseColorString() +#include "irrlicht_changes/static_text.h" #include "guiscalingfilter.h" #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9 @@ -249,37 +250,6 @@ std::vector* GUIFormSpecMenu::getDropDownValues(const std::string & return NULL; } -static std::vector split(const std::string &s, char delim) -{ - std::vector tokens; - - std::string current = ""; - bool last_was_escape = false; - for (unsigned int i = 0; i < s.size(); i++) { - char si = s.c_str()[i]; - if (last_was_escape) { - current += '\\'; - current += si; - last_was_escape = false; - } else { - if (si == delim) { - tokens.push_back(current); - current = ""; - last_was_escape = false; - } else if (si == '\\') { - last_was_escape = true; - } else { - current += si; - last_was_escape = false; - } - } - } - //push last element - tokens.push_back(current); - - return tokens; -} - void GUIFormSpecMenu::parseSize(parserData* data,std::string element) { std::vector parts = split(element,','); @@ -966,7 +936,7 @@ void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element) int font_height = g_fontengine->getTextHeight(); rect.UpperLeftCorner.Y -= font_height; rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height; - Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0); + addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0); } e->setPasswordBox(true,L'*'); @@ -1021,7 +991,7 @@ void GUIFormSpecMenu::parseSimpleField(parserData* data, if (name == "") { // spec field id to 0, this stops submit searching for a value that isn't there - Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid); + addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, spec.fid); } else { @@ -1056,7 +1026,7 @@ void GUIFormSpecMenu::parseSimpleField(parserData* data, int font_height = g_fontengine->getTextHeight(); rect.UpperLeftCorner.Y -= font_height; rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height; - Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0); + addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0); } } @@ -1117,7 +1087,7 @@ void GUIFormSpecMenu::parseTextArea(parserData* data, if (name == "") { // spec field id to 0, this stops submit searching for a value that isn't there - Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, spec.fid); + addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, spec.fid); } else { @@ -1161,7 +1131,7 @@ void GUIFormSpecMenu::parseTextArea(parserData* data, int font_height = g_fontengine->getTextHeight(); rect.UpperLeftCorner.Y -= font_height; rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height; - Environment->addStaticText(spec.flabel.c_str(), rect, false, true, this, 0); + addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0); } } m_fields.push_back(spec); @@ -1230,7 +1200,7 @@ void GUIFormSpecMenu::parseLabel(parserData* data,std::string element) 258+m_fields.size() ); gui::IGUIStaticText *e = - Environment->addStaticText(spec.flabel.c_str(), + addStaticText(Environment, spec.flabel.c_str(), rect, false, false, this, spec.fid); e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER); @@ -1284,7 +1254,7 @@ void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element) 258+m_fields.size() ); gui::IGUIStaticText *t = - Environment->addStaticText(spec.flabel.c_str(), rect, false, false, this, spec.fid); + addStaticText(Environment, spec.flabel.c_str(), rect, false, false, this, spec.fid); t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER); m_fields.push_back(spec); return; @@ -1910,7 +1880,7 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize) { assert(m_tooltip_element == NULL); // Note: parent != this so that the tooltip isn't clipped by the menu rectangle - m_tooltip_element = Environment->addStaticText(L"",core::rect(0,0,110,18)); + m_tooltip_element = addStaticText(Environment, L"",core::rect(0,0,110,18)); m_tooltip_element->enableOverrideColor(true); m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor); m_tooltip_element->setDrawBackground(true); @@ -2255,7 +2225,6 @@ void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase, std::wstring tooltip_text = L""; if (hovering && !m_selected_item) { tooltip_text = utf8_to_wide(item.getDefinition(m_gamedef->idef()).description); - tooltip_text = unescape_enriched(tooltip_text); } if (tooltip_text != L"") { std::vector tt_rows = str_split(tooltip_text, L'\n'); @@ -2263,7 +2232,7 @@ void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase, m_tooltip_element->setOverrideColor(m_default_tooltip_color); m_tooltip_element->setVisible(true); this->bringToFront(m_tooltip_element); - m_tooltip_element->setText(tooltip_text.c_str()); + setStaticText(m_tooltip_element, tooltip_text.c_str()); s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height; #if IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2 s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5; @@ -2535,8 +2504,10 @@ void GUIFormSpecMenu::drawMenu() iter != m_fields.end(); ++iter) { if (iter->fid == id && m_tooltips[iter->fname].tooltip != L"") { if (m_old_tooltip != m_tooltips[iter->fname].tooltip) { + m_tooltip_element->setBackgroundColor(m_tooltips[iter->fname].bgcolor); + m_tooltip_element->setOverrideColor(m_tooltips[iter->fname].color); m_old_tooltip = m_tooltips[iter->fname].tooltip; - m_tooltip_element->setText(m_tooltips[iter->fname].tooltip.c_str()); + setStaticText(m_tooltip_element, m_tooltips[iter->fname].tooltip.c_str()); std::vector tt_rows = str_split(m_tooltips[iter->fname].tooltip, L'\n'); s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height; s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5; @@ -2558,8 +2529,6 @@ void GUIFormSpecMenu::drawMenu() core::position2d(tooltip_x, tooltip_y), core::dimension2d(tooltip_width, tooltip_height))); } - m_tooltip_element->setBackgroundColor(m_tooltips[iter->fname].bgcolor); - m_tooltip_element->setOverrideColor(m_tooltips[iter->fname].color); m_tooltip_element->setVisible(true); this->bringToFront(m_tooltip_element); break; @@ -2568,6 +2537,8 @@ void GUIFormSpecMenu::drawMenu() } } + m_tooltip_element->draw(); + /* Draw dragged item stack */ diff --git a/src/guiFormSpecMenu.h b/src/guiFormSpecMenu.h index ef230c81c..4122b1f56 100644 --- a/src/guiFormSpecMenu.h +++ b/src/guiFormSpecMenu.h @@ -30,6 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "guiTable.h" #include "network/networkprotocol.h" #include "util/string.h" +#include "util/enriched_string.h" class IGameDef; class InventoryManager; @@ -202,7 +203,8 @@ class GUIFormSpecMenu : public GUIModalMenu fname(name), fid(id) { - flabel = unescape_enriched(label); + //flabel = unescape_enriched(label); + flabel = label; fdefault = unescape_enriched(default_text); send = false; ftype = f_Unknown; @@ -239,7 +241,8 @@ class GUIFormSpecMenu : public GUIModalMenu bgcolor(a_bgcolor), color(a_color) { - tooltip = unescape_enriched(utf8_to_wide(a_tooltip)); + //tooltip = unescape_enriched(utf8_to_wide(a_tooltip)); + tooltip = utf8_to_wide(a_tooltip); } std::wstring tooltip; irr::video::SColor bgcolor; @@ -256,7 +259,8 @@ class GUIFormSpecMenu : public GUIModalMenu rect(a_rect), parent_button(NULL) { - text = unescape_enriched(a_text); + //text = unescape_enriched(a_text); + text = a_text; } StaticTextSpec(const std::wstring &a_text, const core::rect &a_rect, @@ -264,7 +268,8 @@ class GUIFormSpecMenu : public GUIModalMenu rect(a_rect), parent_button(a_parent_button) { - text = unescape_enriched(a_text); + //text = unescape_enriched(a_text); + text = a_text; } std::wstring text; core::rect rect; diff --git a/src/irrlicht_changes/CMakeLists.txt b/src/irrlicht_changes/CMakeLists.txt new file mode 100644 index 000000000..3a265c99d --- /dev/null +++ b/src/irrlicht_changes/CMakeLists.txt @@ -0,0 +1,7 @@ +if (BUILD_CLIENT) + set(client_irrlicht_changes_SRCS + ${CMAKE_CURRENT_SOURCE_DIR}/static_text.cpp + PARENT_SCOPE + ) +endif() + diff --git a/src/util/statictext.cpp b/src/irrlicht_changes/static_text.cpp similarity index 83% rename from src/util/statictext.cpp rename to src/irrlicht_changes/static_text.cpp index b534b560e..703287eb3 100644 --- a/src/util/statictext.cpp +++ b/src/irrlicht_changes/static_text.cpp @@ -1,12 +1,12 @@ // Copyright (C) 2002-2012 Nikolaus Gebhardt +// Copyright (C) 2016 Nathanaël Courant: +// Modified the functions to use EnrichedText instead of string. // This file is part of the "Irrlicht Engine". // For conditions of distribution and use, see copyright notice in irrlicht.h -#include "statictext.h" +#include "static_text.h" #ifdef _IRR_COMPILE_WITH_GUI_ -//Only compile this if freetype is enabled. - #include #include #include @@ -17,15 +17,21 @@ #include #include -#include "cguittfont/xCGUITTFont.h" +#if USE_FREETYPE + #include "cguittfont/xCGUITTFont.h" +#endif + #include "util/string.h" namespace irr { + +#if USE_FREETYPE + namespace gui { //! constructor -StaticText::StaticText(const wchar_t* text, bool border, +StaticText::StaticText(const EnrichedString &text, bool border, IGUIEnvironment* environment, IGUIElement* parent, s32 id, const core::rect& rectangle, bool background) @@ -40,7 +46,8 @@ StaticText::StaticText(const wchar_t* text, bool border, setDebugName("StaticText"); #endif - Text = text; + Text = text.c_str(); + cText = text; if (environment && environment->getSkin()) { BGColor = environment->getSkin()->getColor(gui::EGDC_3D_FACE); @@ -55,7 +62,6 @@ StaticText::~StaticText() OverrideFont->drop(); } - //! draws the element and its children void StaticText::draw() { @@ -88,7 +94,7 @@ void StaticText::draw() } // draw the text - if (Text.size()) + if (cText.size()) { IGUIFont* font = getActiveFont(); @@ -105,10 +111,11 @@ void StaticText::draw() if (HAlign == EGUIA_LOWERRIGHT) { frameRect.UpperLeftCorner.X = frameRect.LowerRightCorner.X - - font->getDimension(Text.c_str()).Width; + font->getDimension(cText.c_str()).Width; } - font->draw(Text.c_str(), frameRect, + irr::gui::CGUITTFont *tmp = static_cast(font); + tmp->draw(cText, frameRect, OverrideColorEnabled ? OverrideColor : skin->getColor(isEnabled() ? EGDC_BUTTON_TEXT : EGDC_GRAY_TEXT), HAlign == EGUIA_CENTER, VAlign == EGUIA_CENTER, (RestrainTextInside ? &AbsoluteClippingRect : NULL)); } @@ -138,16 +145,17 @@ void StaticText::draw() font->getDimension(BrokenText[i].c_str()).Width; } - std::vector colors; - std::wstring str; + //std::vector colors; + //std::wstring str; + EnrichedString str = BrokenText[i]; - str = colorizeText(BrokenText[i].c_str(), colors, previous_color); - if (!colors.empty()) - previous_color = colors[colors.size() - 1]; + //str = colorizeText(BrokenText[i].c_str(), colors, previous_color); + //if (!colors.empty()) + // previous_color = colors[colors.size() - 1]; irr::gui::CGUITTFont *tmp = static_cast(font); - tmp->draw(str.c_str(), r, - colors, + tmp->draw(str, r, + previous_color, // FIXME HAlign == EGUIA_CENTER, false, (RestrainTextInside ? &AbsoluteClippingRect : NULL)); r.LowerRightCorner.Y += height; @@ -340,17 +348,17 @@ void StaticText::breakText() LastBreakFont = font; - core::stringw line; - core::stringw word; - core::stringw whitespace; - s32 size = Text.size(); + EnrichedString line; + EnrichedString word; + EnrichedString whitespace; + s32 size = cText.size(); s32 length = 0; s32 elWidth = RelativeRect.getWidth(); if (Border) elWidth -= 2*skin->getSize(EGDS_TEXT_DISTANCE_X); wchar_t c; - std::vector colors; + //std::vector colors; // We have to deal with right-to-left and left-to-right differently // However, most parts of the following code is the same, it's just @@ -360,17 +368,17 @@ void StaticText::breakText() // regular (left-to-right) for (s32 i=0; igetDimension(whitespace.c_str()).Width; - const std::wstring sanitized = removeEscapes(word.c_str()); - const s32 wordlgth = font->getDimension(sanitized.c_str()).Width; + //const std::wstring sanitized = removeEscapes(word.c_str()); + const s32 wordlgth = font->getDimension(word.c_str()).Width; if (wordlgth > elWidth) { // This word is too long to fit in the available space, look for // the Unicode Soft HYphen (SHY / 00AD) character for a place to // break the word at - int where = word.findFirst( wchar_t(0x00AD) ); + int where = core::stringw(word.c_str()).findFirst( wchar_t(0x00AD) ); if (where != -1) { - core::stringw first = word.subString(0, where); - core::stringw second = word.subString(where, word.size() - where); - BrokenText.push_back(line + first + L"-"); + EnrichedString first = word.substr(0, where); + EnrichedString second = word.substr(where, word.size() - where); + first.addCharNoColor(L'-'); + BrokenText.push_back(line + first); const s32 secondLength = font->getDimension(second.c_str()).Width; length = secondLength; @@ -437,13 +447,13 @@ void StaticText::breakText() length += whitelgth + wordlgth; } - word = L""; - whitespace = L""; + word.clear(); + whitespace.clear(); } - if ( isWhitespace ) + if ( isWhitespace && c != 0) { - whitespace += c; + whitespace.addChar(cText, i); } // compute line break @@ -452,9 +462,9 @@ void StaticText::breakText() line += whitespace; line += word; BrokenText.push_back(line); - line = L""; - word = L""; - whitespace = L""; + line.clear(); + word.clear(); + whitespace.clear(); length = 0; } } @@ -469,17 +479,17 @@ void StaticText::breakText() // right-to-left for (s32 i=size; i>=0; --i) { - c = Text[i]; + c = cText.getString()[i]; bool lineBreak = false; if (c == L'\r') // Mac or Windows breaks { lineBreak = true; - if ((i>0) && Text[i-1] == L'\n') // Windows breaks - { - Text.erase(i-1); - --size; - } + //if ((i>0) && Text[i-1] == L'\n') // Windows breaks + //{ + // Text.erase(i-1); + // --size; + //} c = '\0'; } else if (c == L'\n') // Unix breaks @@ -512,12 +522,13 @@ void StaticText::breakText() length += whitelgth + wordlgth; } - word = L""; - whitespace = L""; + word.clear(); + whitespace.clear(); } if (c != 0) - whitespace = core::stringw(&c, 1) + whitespace; + // whitespace = core::stringw(&c, 1) + whitespace; + whitespace = cText.substr(i, 1) + whitespace; // compute line break if (lineBreak) @@ -525,16 +536,17 @@ void StaticText::breakText() line = whitespace + line; line = word + line; BrokenText.push_back(line); - line = L""; - word = L""; - whitespace = L""; + line.clear(); + word.clear(); + whitespace.clear(); length = 0; } } else { // yippee this is a word.. - word = core::stringw(&c, 1) + word; + //word = core::stringw(&c, 1) + word; + word = cText.substr(i, 1) + word; } } @@ -548,7 +560,17 @@ void StaticText::breakText() //! Sets the new caption of this element. void StaticText::setText(const wchar_t* text) { - IGUIElement::setText(text); + setText(EnrichedString(text)); +} + +//! Sets the new caption of this element. +void StaticText::setText(const EnrichedString &text) +{ + IGUIElement::setText(text.c_str()); + cText = text; + if (text.hasBackground()) { + setBackgroundColor(text.getBackground()); + } breakText(); } @@ -598,7 +620,7 @@ s32 StaticText::getTextWidth() const } else { - return font->getDimension(Text.c_str()).Width; + return font->getDimension(cText.c_str()).Width; } } @@ -648,6 +670,9 @@ void StaticText::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWr } } // end namespace gui + +#endif // USE_FREETYPE + } // end namespace irr diff --git a/src/util/statictext.h b/src/irrlicht_changes/static_text.h similarity index 54% rename from src/util/statictext.h rename to src/irrlicht_changes/static_text.h index 8d2f879e7..408a12784 100644 --- a/src/util/statictext.h +++ b/src/irrlicht_changes/static_text.h @@ -1,4 +1,6 @@ // Copyright (C) 2002-2012 Nikolaus Gebhardt +// Copyright (C) 2016 Nathanaël Courant +// Modified this class to work with EnrichedStrings too // This file is part of the "Irrlicht Engine". // For conditions of distribution and use, see copyright notice in irrlicht.h @@ -11,18 +13,30 @@ #include "IGUIStaticText.h" #include "irrArray.h" +#include "log.h" + #include +#include "util/enriched_string.h" +#include "config.h" +#include + +#if USE_FREETYPE + namespace irr { + namespace gui { + + const EGUI_ELEMENT_TYPE EGUIET_ENRICHED_STATIC_TEXT = (EGUI_ELEMENT_TYPE)(0x1000); + class StaticText : public IGUIStaticText { public: //! constructor - StaticText(const wchar_t* text, bool border, IGUIEnvironment* environment, + StaticText(const EnrichedString &text, bool border, IGUIEnvironment* environment, IGUIElement* parent, s32 id, const core::rect& rectangle, bool background = false); @@ -121,6 +135,16 @@ namespace gui //! Reads attributes of the element virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options); + virtual bool hasType(EGUI_ELEMENT_TYPE t) const { + return (t == EGUIET_ENRICHED_STATIC_TEXT) || (t == EGUIET_STATIC_TEXT); + }; + + virtual bool hasType(EGUI_ELEMENT_TYPE t) { + return (t == EGUIET_ENRICHED_STATIC_TEXT) || (t == EGUIET_STATIC_TEXT); + }; + + void setText(const EnrichedString &text); + private: //! Breaks the single text line. @@ -139,12 +163,106 @@ namespace gui gui::IGUIFont* OverrideFont; gui::IGUIFont* LastBreakFont; // stored because: if skin changes, line break must be recalculated. - core::array< core::stringw > BrokenText; + EnrichedString cText; + core::array< EnrichedString > BrokenText; }; + } // end namespace gui + } // end namespace irr +inline irr::gui::IGUIStaticText *addStaticText( + irr::gui::IGUIEnvironment *guienv, + const EnrichedString &text, + const core::rect< s32 > &rectangle, + bool border = false, + bool wordWrap = true, + irr::gui::IGUIElement *parent = NULL, + s32 id = -1, + bool fillBackground = false) +{ + if (parent == NULL) { + // parent is NULL, so we must find one, or we need not to drop + // result, but then there will be a memory leak. + // + // What Irrlicht does is to use guienv as a parent, but the problem + // is that guienv is here only an IGUIEnvironment, while it is a + // CGUIEnvironment in Irrlicht, which inherits from both IGUIElement + // and IGUIEnvironment. + // + // A solution would be to dynamic_cast guienv to a + // IGUIElement*, but Irrlicht is shipped without rtti support + // in some distributions, causing the dymanic_cast to segfault. + // + // Thus, to find the parent, we create a dummy StaticText and ask + // for its parent, and then remove it. + irr::gui::IGUIStaticText *dummy_text = + guienv->addStaticText(L"", rectangle, border, wordWrap, + parent, id, fillBackground); + parent = dummy_text->getParent(); + dummy_text->remove(); + } + irr::gui::IGUIStaticText *result = new irr::gui::StaticText( + text, border, guienv, parent, + id, rectangle, fillBackground); + + result->setWordWrap(wordWrap); + result->drop(); + return result; +} + +inline void setStaticText(irr::gui::IGUIStaticText *static_text, const EnrichedString &text) +{ + // dynamic_cast not possible due to some distributions shipped + // without rtti support in irrlicht + if (static_text->hasType(irr::gui::EGUIET_ENRICHED_STATIC_TEXT)) { + irr::gui::StaticText* stext = static_cast(static_text); + stext->setText(text); + } else { + static_text->setText(text.c_str()); + } +} + +#else // USE_FREETYPE + +inline irr::gui::IGUIStaticText *addStaticText( + irr::gui::IGUIEnvironment *guienv, + const EnrichedString &text, + const core::rect< s32 > &rectangle, + bool border = false, + bool wordWrap = true, + irr::gui::IGUIElement *parent = NULL, + s32 id = -1, + bool fillBackground = false) +{ + return guienv->addStaticText(text.c_str(), rectangle, border, wordWrap, parent, id, fillBackground); +} + +inline void setStaticText(irr::gui::IGUIStaticText *static_text, const EnrichedString &text) +{ + static_text->setText(text.c_str()); +} + +#endif + +inline irr::gui::IGUIStaticText *addStaticText( + irr::gui::IGUIEnvironment *guienv, + const wchar_t *text, + const core::rect< s32 > &rectangle, + bool border = false, + bool wordWrap = true, + irr::gui::IGUIElement *parent = NULL, + s32 id = -1, + bool fillBackground = false) { + return addStaticText(guienv, EnrichedString(text), rectangle, border, wordWrap, parent, id, fillBackground); +} + +inline void setStaticText(irr::gui::IGUIStaticText *static_text, const wchar_t *text) +{ + setStaticText(static_text, EnrichedString(text)); +} + #endif // _IRR_COMPILE_WITH_GUI_ #endif // C_GUI_STATIC_TEXT_H_INCLUDED diff --git a/src/terminal_chat_console.cpp b/src/terminal_chat_console.cpp index c86a960fa..a8c4ebaef 100644 --- a/src/terminal_chat_console.cpp +++ b/src/terminal_chat_console.cpp @@ -345,9 +345,11 @@ void TerminalChatConsole::step(int ch) if (p.first > m_log_level) continue; - m_chat_backend.addMessage( - utf8_to_wide(Logger::getLevelLabel(p.first)), - utf8_to_wide(p.second)); + std::wstring error_message = utf8_to_wide(Logger::getLevelLabel(p.first)); + if (!g_settings->getBool("disable_escape_sequences")) { + error_message = L"\x1b(c@red)" + error_message + L"\x1b(c@white)"; + } + m_chat_backend.addMessage(error_message, utf8_to_wide(p.second)); } // handle input @@ -438,7 +440,7 @@ void TerminalChatConsole::draw_text() continue; for (u32 i = 0; i < line.fragments.size(); ++i) { const ChatFormattedFragment& fragment = line.fragments[i]; - addstr(wide_to_utf8(fragment.text).c_str()); + addstr(wide_to_utf8(fragment.text.getString()).c_str()); } } } diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index e028a0435..f571ab22c 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -1,17 +1,9 @@ -if(USE_FREETYPE) - set(UTIL_FREETYPEDEP_SRCS - ${CMAKE_CURRENT_SOURCE_DIR}/statictext.cpp - ) -else() - set(UTIL_FREETYPEDEP_SRCS ) -endif(USE_FREETYPE) - set(UTIL_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/areastore.cpp ${CMAKE_CURRENT_SOURCE_DIR}/auth.cpp ${CMAKE_CURRENT_SOURCE_DIR}/base64.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/coloredstring.cpp ${CMAKE_CURRENT_SOURCE_DIR}/directiontables.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/enriched_string.cpp ${CMAKE_CURRENT_SOURCE_DIR}/numeric.cpp ${CMAKE_CURRENT_SOURCE_DIR}/pointedthing.cpp ${CMAKE_CURRENT_SOURCE_DIR}/serialize.cpp @@ -20,6 +12,5 @@ set(UTIL_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/string.cpp ${CMAKE_CURRENT_SOURCE_DIR}/srp.cpp ${CMAKE_CURRENT_SOURCE_DIR}/timetaker.cpp - ${UTIL_FREETYPEDEP_SRCS} PARENT_SCOPE) diff --git a/src/util/coloredstring.cpp b/src/util/coloredstring.cpp deleted file mode 100644 index 7db586550..000000000 --- a/src/util/coloredstring.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/* -Copyright (C) 2013 xyz, Ilya Zhuravlev - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Lesser General Public License for more details. - -You should have received a copy of the GNU Lesser General Public License along -with this program; if not, write to the Free Software Foundation, Inc., -51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -*/ - -#include "coloredstring.h" -#include "util/string.h" - -ColoredString::ColoredString() -{} - -ColoredString::ColoredString(const std::wstring &string, const std::vector &colors): - m_string(string), - m_colors(colors) -{} - -ColoredString::ColoredString(const std::wstring &s) { - m_string = colorizeText(s, m_colors, SColor(255, 255, 255, 255)); -} - -void ColoredString::operator=(const wchar_t *str) { - m_string = colorizeText(str, m_colors, SColor(255, 255, 255, 255)); -} - -size_t ColoredString::size() const { - return m_string.size(); -} - -ColoredString ColoredString::substr(size_t pos, size_t len) const { - if (pos == m_string.length()) - return ColoredString(); - if (len == std::string::npos || pos + len > m_string.length()) { - return ColoredString( - m_string.substr(pos, std::string::npos), - std::vector(m_colors.begin() + pos, m_colors.end()) - ); - } else { - return ColoredString( - m_string.substr(pos, len), - std::vector(m_colors.begin() + pos, m_colors.begin() + pos + len) - ); - } -} - -const wchar_t *ColoredString::c_str() const { - return m_string.c_str(); -} - -const std::vector &ColoredString::getColors() const { - return m_colors; -} - -const std::wstring &ColoredString::getString() const { - return m_string; -} diff --git a/src/util/coloredstring.h b/src/util/coloredstring.h deleted file mode 100644 index a6d98db30..000000000 --- a/src/util/coloredstring.h +++ /dev/null @@ -1,44 +0,0 @@ -/* -Copyright (C) 2013 xyz, Ilya Zhuravlev - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Lesser General Public License for more details. - -You should have received a copy of the GNU Lesser General Public License along -with this program; if not, write to the Free Software Foundation, Inc., -51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -*/ - -#ifndef COLOREDSTRING_HEADER -#define COLOREDSTRING_HEADER - -#include -#include -#include - -using namespace irr::video; - -class ColoredString { -public: - ColoredString(); - ColoredString(const std::wstring &s); - ColoredString(const std::wstring &string, const std::vector &colors); - void operator=(const wchar_t *str); - size_t size() const; - ColoredString substr(size_t pos = 0, size_t len = std::string::npos) const; - const wchar_t *c_str() const; - const std::vector &getColors() const; - const std::wstring &getString() const; -private: - std::wstring m_string; - std::vector m_colors; -}; - -#endif diff --git a/src/util/enriched_string.cpp b/src/util/enriched_string.cpp new file mode 100644 index 000000000..a7fc3a828 --- /dev/null +++ b/src/util/enriched_string.cpp @@ -0,0 +1,166 @@ +/* +Copyright (C) 2013 xyz, Ilya Zhuravlev +Copyright (C) 2016 Nore, Nathanaël Courant + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "enriched_string.h" +#include "util/string.h" +#include "log.h" +using namespace irr::video; + +EnrichedString::EnrichedString() +{ + clear(); +} + +EnrichedString::EnrichedString(const std::wstring &string, + const std::vector &colors): + m_string(string), + m_colors(colors), + m_has_background(false) +{} + +EnrichedString::EnrichedString(const std::wstring &s, const SColor &color) +{ + clear(); + addAtEnd(s, color); +} + +EnrichedString::EnrichedString(const wchar_t *str, const SColor &color) +{ + clear(); + addAtEnd(std::wstring(str), color); +} + +void EnrichedString::operator=(const wchar_t *str) +{ + clear(); + addAtEnd(std::wstring(str), SColor(255, 255, 255, 255)); +} + +void EnrichedString::addAtEnd(const std::wstring &s, const SColor &initial_color) +{ + SColor color(initial_color); + size_t i = 0; + while (i < s.length()) { + if (s[i] != L'\x1b') { + m_string += s[i]; + m_colors.push_back(color); + ++i; + continue; + } + ++i; + size_t start_index = i; + size_t length; + if (i == s.length()) { + break; + } + if (s[i] == L'(') { + ++i; + ++start_index; + while (i < s.length() && s[i] != L')') { + if (s[i] == L'\\') { + ++i; + } + ++i; + } + length = i - start_index; + ++i; + } else { + ++i; + length = 1; + } + std::wstring escape_sequence(s, start_index, length); + std::vector parts = split(escape_sequence, L'@'); + if (parts[0] == L"c") { + if (parts.size() < 2) { + continue; + } + parseColorString(wide_to_utf8(parts[1]), color, true); + } else if (parts[0] == L"b") { + if (parts.size() < 2) { + continue; + } + parseColorString(wide_to_utf8(parts[1]), m_background, true); + m_has_background = true; + } + continue; + } +} + +void EnrichedString::addChar(const EnrichedString &source, size_t i) +{ + m_string += source.m_string[i]; + m_colors.push_back(source.m_colors[i]); +} + +void EnrichedString::addCharNoColor(wchar_t c) +{ + m_string += c; + if (m_colors.empty()) { + m_colors.push_back(SColor(255, 255, 255, 255)); + } else { + m_colors.push_back(m_colors[m_colors.size() - 1]); + } +} + +EnrichedString EnrichedString::operator+(const EnrichedString &other) const +{ + std::vector result; + result.insert(result.end(), m_colors.begin(), m_colors.end()); + result.insert(result.end(), other.m_colors.begin(), other.m_colors.end()); + return EnrichedString(m_string + other.m_string, result); +} + +void EnrichedString::operator+=(const EnrichedString &other) +{ + m_string += other.m_string; + m_colors.insert(m_colors.end(), other.m_colors.begin(), other.m_colors.end()); +} + +EnrichedString EnrichedString::substr(size_t pos, size_t len) const +{ + if (pos == m_string.length()) { + return EnrichedString(); + } + if (len == std::string::npos || pos + len > m_string.length()) { + return EnrichedString( + m_string.substr(pos, std::string::npos), + std::vector(m_colors.begin() + pos, m_colors.end()) + ); + } else { + return EnrichedString( + m_string.substr(pos, len), + std::vector(m_colors.begin() + pos, m_colors.begin() + pos + len) + ); + } +} + +const wchar_t *EnrichedString::c_str() const +{ + return m_string.c_str(); +} + +const std::vector &EnrichedString::getColors() const +{ + return m_colors; +} + +const std::wstring &EnrichedString::getString() const +{ + return m_string; +} diff --git a/src/util/enriched_string.h b/src/util/enriched_string.h new file mode 100644 index 000000000..1aca8948a --- /dev/null +++ b/src/util/enriched_string.h @@ -0,0 +1,91 @@ +/* +Copyright (C) 2013 xyz, Ilya Zhuravlev +Copyright (C) 2016 Nore, Nathanaël Courant + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef ENRICHEDSTRING_HEADER +#define ENRICHEDSTRING_HEADER + +#include +#include +#include + +class EnrichedString { +public: + EnrichedString(); + EnrichedString(const std::wstring &s, + const irr::video::SColor &color = irr::video::SColor(255, 255, 255, 255)); + EnrichedString(const wchar_t *str, + const irr::video::SColor &color = irr::video::SColor(255, 255, 255, 255)); + EnrichedString(const std::wstring &string, + const std::vector &colors); + void operator=(const wchar_t *str); + void addAtEnd(const std::wstring &s, const irr::video::SColor &color); + + // Adds the character source[i] at the end. + // An EnrichedString should always be able to be copied + // to the end of an existing EnrichedString that way. + void addChar(const EnrichedString &source, size_t i); + + // Adds a single character at the end, without specifying its + // color. The color used will be the one from the last character. + void addCharNoColor(wchar_t c); + + EnrichedString substr(size_t pos = 0, size_t len = std::string::npos) const; + EnrichedString operator+(const EnrichedString &other) const; + void operator+=(const EnrichedString &other); + const wchar_t *c_str() const; + const std::vector &getColors() const; + const std::wstring &getString() const; + inline bool operator==(const EnrichedString &other) const + { + return (m_string == other.m_string && m_colors == other.m_colors); + } + inline bool operator!=(const EnrichedString &other) const + { + return !(*this == other); + } + inline void clear() + { + m_string.clear(); + m_colors.clear(); + m_has_background = false; + } + inline bool empty() const + { + return m_string.empty(); + } + inline size_t size() const + { + return m_string.size(); + } + inline bool hasBackground() const + { + return m_has_background; + } + inline irr::video::SColor getBackground() const + { + return m_background; + } +private: + std::wstring m_string; + std::vector m_colors; + bool m_has_background; + irr::video::SColor m_background; +}; + +#endif diff --git a/src/util/string.h b/src/util/string.h index 40ef3e4d3..c77c5a6f9 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -519,6 +519,38 @@ std::basic_string unescape_enriched(const std::basic_string &s) return output; } +template +std::vector > split(const std::basic_string &s, T delim) +{ + std::vector > tokens; + + std::basic_string current; + bool last_was_escape = false; + for (size_t i = 0; i < s.length(); i++) { + T si = s[i]; + if (last_was_escape) { + current += '\\'; + current += si; + last_was_escape = false; + } else { + if (si == delim) { + tokens.push_back(current); + current = std::basic_string(); + last_was_escape = false; + } else if (si == '\\') { + last_was_escape = true; + } else { + current += si; + last_was_escape = false; + } + } + } + //push last element + tokens.push_back(current); + + return tokens; +} + /** * Checks that all characters in \p to_check are a decimal digits. * From 569998011e0a9cb6179f1e79fb15b7a3ce80977f Mon Sep 17 00:00:00 2001 From: paramat Date: Mon, 30 May 2016 13:15:14 +0100 Subject: [PATCH 050/418] Mgv7: Always carve river channels in mountain terrain Previously, rivers were sometimes blocked by vertical walls of mountain terrain due to river carving being disabled when base terrain height was below water_level - 16 Remove now unused base terrain heightmap created in generateTerrain() --- src/mapgen_v7.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/mapgen_v7.cpp b/src/mapgen_v7.cpp index 9ff2fbe66..7327facae 100644 --- a/src/mapgen_v7.cpp +++ b/src/mapgen_v7.cpp @@ -206,13 +206,15 @@ void MapgenV7::makeChunk(BlockMakeData *data) blockseed = getBlockSeed2(full_node_min, seed); - // Generate terrain and ridges with initial heightmaps + // Generate base and mountain terrain + // An initial heightmap is no longer created here for use in generateRidgeTerrain() s16 stone_surface_max_y = generateTerrain(); + // Generate rivers if (spflags & MGV7_RIDGES) generateRidgeTerrain(); - // Update heightmap to include mountain terrain + // Create heightmap updateHeightmap(node_min, node_max); // Init biome generator, place biome-specific nodes, and build biomemap @@ -331,7 +333,6 @@ int MapgenV7::generateTerrain() for (s16 z = node_min.Z; z <= node_max.Z; z++) for (s16 x = node_min.X; x <= node_max.X; x++, index2d++) { s16 surface_y = baseTerrainLevelFromMap(index2d); - heightmap[index2d] = surface_y; // Create base terrain heightmap if (surface_y > stone_surface_max_y) stone_surface_max_y = surface_y; @@ -382,9 +383,6 @@ void MapgenV7::generateRidgeTerrain() for (s16 x = node_min.X; x <= node_max.X; x++, index++, vi++) { int j = (z - node_min.Z) * csize.X + (x - node_min.X); - if (heightmap[j] < water_level - 16) // Use base terrain heightmap - continue; - float uwatern = noise_ridge_uwater->result[j] * 2; if (fabs(uwatern) > width) continue; From 7ea4a03c835d68a6fb58aa55aa6a6315ec80b79f Mon Sep 17 00:00:00 2001 From: Robert Kiraly Date: Fri, 3 Jun 2016 00:50:21 -0700 Subject: [PATCH 051/418] Sapier's fix for the RESEND RELIABLE problem (#4170) --- src/network/connection.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/network/connection.cpp b/src/network/connection.cpp index 02c0aa165..b711cae11 100644 --- a/src/network/connection.cpp +++ b/src/network/connection.cpp @@ -71,6 +71,9 @@ static inline float CALC_DTIME(unsigned int lasttime, unsigned int curtime) { #define PING_TIMEOUT 5.0 +/* maximum number of retries for reliable packets */ +#define MAX_RELIABLE_RETRY 5 + static u16 readPeerId(u8 *packetdata) { return readU16(&packetdata[4]); @@ -1399,6 +1402,7 @@ void ConnectionSendThread::runTimeouts(float dtime) } float resend_timeout = dynamic_cast(&peer)->getResendTimeout(); + bool retry_count_exceeded = false; for(u16 i=0; i timed_outs; @@ -1438,6 +1442,13 @@ void ConnectionSendThread::runTimeouts(float dtime) channel->UpdateBytesLost(k->data.getSize()); k->resend_count++; + if (k-> resend_count > MAX_RELIABLE_RETRY) { + retry_count_exceeded = true; + timeouted_peers.push_back(peer->id); + /* no need to check additional packets if a single one did timeout*/ + break; + } + LOG(derr_con<getDesc() <<"RE-SENDING timed-out RELIABLE to " << k->address.serializeString() @@ -1452,9 +1463,18 @@ void ConnectionSendThread::runTimeouts(float dtime) // do not handle rtt here as we can't decide if this packet was // lost or really takes more time to transmit } + + if (retry_count_exceeded) { + break; /* no need to check other channels if we already did timeout */ + } + channel->UpdateTimers(dtime,dynamic_cast(&peer)->getLegacyPeer()); } + /* skip to next peer if we did timeout */ + if (retry_count_exceeded) + continue; + /* send ping if necessary */ if (dynamic_cast(&peer)->Ping(dtime,data)) { LOG(dout_con<getDesc() From 1e86c89f3614cf298916149a8f13d44ea671da64 Mon Sep 17 00:00:00 2001 From: est31 Date: Wed, 25 May 2016 09:22:20 +0200 Subject: [PATCH 052/418] Input related generalisations * Move key types into own file * Use Generalized input methods in game.cpp --- src/client/keys.h | 79 +++++++++++++ src/game.cpp | 289 ++++++++++++++++++++++------------------------ src/game.h | 1 + 3 files changed, 221 insertions(+), 148 deletions(-) create mode 100644 src/client/keys.h diff --git a/src/client/keys.h b/src/client/keys.h new file mode 100644 index 000000000..08b1c4123 --- /dev/null +++ b/src/client/keys.h @@ -0,0 +1,79 @@ +/* +Minetest +Copyright (C) 2016 est31, + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef KEYS_HEADER +#define KEYS_HEADER + +#include + +class KeyType { +public: + enum T { + // Player movement + FORWARD, + BACKWARD, + LEFT, + RIGHT, + JUMP, + SPECIAL1, + SNEAK, + AUTORUN, + + ESC, + + // Other + DROP, + INVENTORY, + CHAT, + CMD, + CONSOLE, + MINIMAP, + FREEMOVE, + FASTMOVE, + NOCLIP, + CINEMATIC, + SCREENSHOT, + TOGGLE_HUD, + TOGGLE_CHAT, + TOGGLE_FORCE_FOG_OFF, + TOGGLE_UPDATE_CAMERA, + TOGGLE_DEBUG, + TOGGLE_PROFILER, + CAMERA_MODE, + INCREASE_VIEWING_RANGE, + DECREASE_VIEWING_RANGE, + RANGESELECT, + + QUICKTUNE_NEXT, + QUICKTUNE_PREV, + QUICKTUNE_INC, + QUICKTUNE_DEC, + + DEBUG_STACKS, + + // Fake keycode for array size and internal checks + INTERNAL_ENUM_COUNT + + }; +}; + +typedef KeyType::T GameKeyType; + + +#endif diff --git a/src/game.cpp b/src/game.cpp index def202fe5..ac1c0fe6b 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "camera.h" #include "client.h" #include "client/tile.h" // For TextureSource +#include "client/keys.h" #include "clientmap.h" #include "clouds.h" #include "config.h" @@ -1306,114 +1307,76 @@ struct KeyCache { { handler = NULL; populate(); + populate_nonchanging(); } - enum { - // Player movement - KEYMAP_ID_FORWARD, - KEYMAP_ID_BACKWARD, - KEYMAP_ID_LEFT, - KEYMAP_ID_RIGHT, - KEYMAP_ID_JUMP, - KEYMAP_ID_SPECIAL1, - KEYMAP_ID_SNEAK, - KEYMAP_ID_AUTORUN, - - // Other - KEYMAP_ID_DROP, - KEYMAP_ID_INVENTORY, - KEYMAP_ID_CHAT, - KEYMAP_ID_CMD, - KEYMAP_ID_CONSOLE, - KEYMAP_ID_MINIMAP, - KEYMAP_ID_FREEMOVE, - KEYMAP_ID_FASTMOVE, - KEYMAP_ID_NOCLIP, - KEYMAP_ID_CINEMATIC, - KEYMAP_ID_SCREENSHOT, - KEYMAP_ID_TOGGLE_HUD, - KEYMAP_ID_TOGGLE_CHAT, - KEYMAP_ID_TOGGLE_FORCE_FOG_OFF, - KEYMAP_ID_TOGGLE_UPDATE_CAMERA, - KEYMAP_ID_TOGGLE_DEBUG, - KEYMAP_ID_TOGGLE_PROFILER, - KEYMAP_ID_CAMERA_MODE, - KEYMAP_ID_INCREASE_VIEWING_RANGE, - KEYMAP_ID_DECREASE_VIEWING_RANGE, - KEYMAP_ID_RANGESELECT, - - KEYMAP_ID_QUICKTUNE_NEXT, - KEYMAP_ID_QUICKTUNE_PREV, - KEYMAP_ID_QUICKTUNE_INC, - KEYMAP_ID_QUICKTUNE_DEC, - - KEYMAP_ID_DEBUG_STACKS, - - // Fake keycode for array size and internal checks - KEYMAP_INTERNAL_ENUM_COUNT - - - }; - void populate(); - KeyPress key[KEYMAP_INTERNAL_ENUM_COUNT]; + // Keys that are not settings dependent + void populate_nonchanging(); + + KeyPress key[KeyType::INTERNAL_ENUM_COUNT]; InputHandler *handler; }; +void KeyCache::populate_nonchanging() +{ + key[KeyType::ESC] = EscapeKey; +} + void KeyCache::populate() { - key[KEYMAP_ID_FORWARD] = getKeySetting("keymap_forward"); - key[KEYMAP_ID_BACKWARD] = getKeySetting("keymap_backward"); - key[KEYMAP_ID_LEFT] = getKeySetting("keymap_left"); - key[KEYMAP_ID_RIGHT] = getKeySetting("keymap_right"); - key[KEYMAP_ID_JUMP] = getKeySetting("keymap_jump"); - key[KEYMAP_ID_SPECIAL1] = getKeySetting("keymap_special1"); - key[KEYMAP_ID_SNEAK] = getKeySetting("keymap_sneak"); + key[KeyType::FORWARD] = getKeySetting("keymap_forward"); + key[KeyType::BACKWARD] = getKeySetting("keymap_backward"); + key[KeyType::LEFT] = getKeySetting("keymap_left"); + key[KeyType::RIGHT] = getKeySetting("keymap_right"); + key[KeyType::JUMP] = getKeySetting("keymap_jump"); + key[KeyType::SPECIAL1] = getKeySetting("keymap_special1"); + key[KeyType::SNEAK] = getKeySetting("keymap_sneak"); - key[KEYMAP_ID_AUTORUN] = getKeySetting("keymap_autorun"); + key[KeyType::AUTORUN] = getKeySetting("keymap_autorun"); - key[KEYMAP_ID_DROP] = getKeySetting("keymap_drop"); - key[KEYMAP_ID_INVENTORY] = getKeySetting("keymap_inventory"); - key[KEYMAP_ID_CHAT] = getKeySetting("keymap_chat"); - key[KEYMAP_ID_CMD] = getKeySetting("keymap_cmd"); - key[KEYMAP_ID_CONSOLE] = getKeySetting("keymap_console"); - key[KEYMAP_ID_MINIMAP] = getKeySetting("keymap_minimap"); - key[KEYMAP_ID_FREEMOVE] = getKeySetting("keymap_freemove"); - key[KEYMAP_ID_FASTMOVE] = getKeySetting("keymap_fastmove"); - key[KEYMAP_ID_NOCLIP] = getKeySetting("keymap_noclip"); - key[KEYMAP_ID_CINEMATIC] = getKeySetting("keymap_cinematic"); - key[KEYMAP_ID_SCREENSHOT] = getKeySetting("keymap_screenshot"); - key[KEYMAP_ID_TOGGLE_HUD] = getKeySetting("keymap_toggle_hud"); - key[KEYMAP_ID_TOGGLE_CHAT] = getKeySetting("keymap_toggle_chat"); - key[KEYMAP_ID_TOGGLE_FORCE_FOG_OFF] + key[KeyType::DROP] = getKeySetting("keymap_drop"); + key[KeyType::INVENTORY] = getKeySetting("keymap_inventory"); + key[KeyType::CHAT] = getKeySetting("keymap_chat"); + key[KeyType::CMD] = getKeySetting("keymap_cmd"); + key[KeyType::CONSOLE] = getKeySetting("keymap_console"); + key[KeyType::MINIMAP] = getKeySetting("keymap_minimap"); + key[KeyType::FREEMOVE] = getKeySetting("keymap_freemove"); + key[KeyType::FASTMOVE] = getKeySetting("keymap_fastmove"); + key[KeyType::NOCLIP] = getKeySetting("keymap_noclip"); + key[KeyType::CINEMATIC] = getKeySetting("keymap_cinematic"); + key[KeyType::SCREENSHOT] = getKeySetting("keymap_screenshot"); + key[KeyType::TOGGLE_HUD] = getKeySetting("keymap_toggle_hud"); + key[KeyType::TOGGLE_CHAT] = getKeySetting("keymap_toggle_chat"); + key[KeyType::TOGGLE_FORCE_FOG_OFF] = getKeySetting("keymap_toggle_force_fog_off"); - key[KEYMAP_ID_TOGGLE_UPDATE_CAMERA] + key[KeyType::TOGGLE_UPDATE_CAMERA] = getKeySetting("keymap_toggle_update_camera"); - key[KEYMAP_ID_TOGGLE_DEBUG] + key[KeyType::TOGGLE_DEBUG] = getKeySetting("keymap_toggle_debug"); - key[KEYMAP_ID_TOGGLE_PROFILER] + key[KeyType::TOGGLE_PROFILER] = getKeySetting("keymap_toggle_profiler"); - key[KEYMAP_ID_CAMERA_MODE] + key[KeyType::CAMERA_MODE] = getKeySetting("keymap_camera_mode"); - key[KEYMAP_ID_INCREASE_VIEWING_RANGE] + key[KeyType::INCREASE_VIEWING_RANGE] = getKeySetting("keymap_increase_viewing_range_min"); - key[KEYMAP_ID_DECREASE_VIEWING_RANGE] + key[KeyType::DECREASE_VIEWING_RANGE] = getKeySetting("keymap_decrease_viewing_range_min"); - key[KEYMAP_ID_RANGESELECT] + key[KeyType::RANGESELECT] = getKeySetting("keymap_rangeselect"); - key[KEYMAP_ID_QUICKTUNE_NEXT] = getKeySetting("keymap_quicktune_next"); - key[KEYMAP_ID_QUICKTUNE_PREV] = getKeySetting("keymap_quicktune_prev"); - key[KEYMAP_ID_QUICKTUNE_INC] = getKeySetting("keymap_quicktune_inc"); - key[KEYMAP_ID_QUICKTUNE_DEC] = getKeySetting("keymap_quicktune_dec"); + key[KeyType::QUICKTUNE_NEXT] = getKeySetting("keymap_quicktune_next"); + key[KeyType::QUICKTUNE_PREV] = getKeySetting("keymap_quicktune_prev"); + key[KeyType::QUICKTUNE_INC] = getKeySetting("keymap_quicktune_inc"); + key[KeyType::QUICKTUNE_DEC] = getKeySetting("keymap_quicktune_dec"); - key[KEYMAP_ID_DEBUG_STACKS] = getKeySetting("keymap_print_debug_stacks"); + key[KeyType::DEBUG_STACKS] = getKeySetting("keymap_print_debug_stacks"); if (handler) { // First clear all keys, then re-add the ones we listen for handler->dontListenForKeys(); - for (size_t i = 0; i < KEYMAP_INTERNAL_ENUM_COUNT; i++) { + for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) { handler->listenForKey(key[i]); } handler->listenForKey(EscapeKey); @@ -1575,9 +1538,10 @@ protected: f32 dtime); void updateStats(RunStats *stats, const FpsControl &draw_times, f32 dtime); + // Input related void processUserInput(VolatileRunFlags *flags, GameRunData *runData, f32 dtime); - void processKeyboardInput(VolatileRunFlags *flags, + void processKeyInput(VolatileRunFlags *flags, float *statustext_time, float *jump_timer, bool *reset_jump_timer, @@ -1647,6 +1611,36 @@ protected: static void settingChangedCallback(const std::string &setting_name, void *data); void readSettings(); + bool getLeftClicked() + { + return input->getLeftClicked(); + } + bool getRightClicked() + { + return input->getRightClicked(); + } + bool isLeftPressed() + { + return input->getLeftState(); + } + bool isRightPressed() + { + return input->getRightState(); + } + bool getLeftReleased() + { + return input->getLeftReleased(); + } + + bool isKeyDown(GameKeyType k) + { + return input->isKeyDown(keycache.key[k]); + } + bool wasKeyDown(GameKeyType k) + { + return input->wasKeyDown(keycache.key[k]); + } + #ifdef __ANDROID__ void handleAndroidChatInput(); #endif @@ -2379,7 +2373,7 @@ bool Game::connectToServer(const std::string &playername, break; } - if (input->wasKeyDown(EscapeKey) || input->wasKeyDown(CancelKey)) { + if (wasKeyDown(KeyType::ESC) || input->wasKeyDown(CancelKey)) { *aborted = true; infostream << "Connect aborted [Escape]" << std::endl; break; @@ -2440,7 +2434,7 @@ bool Game::getServerContent(bool *aborted) return false; } - if (input->wasKeyDown(EscapeKey) || input->wasKeyDown(CancelKey)) { + if (wasKeyDown(KeyType::ESC) || input->wasKeyDown(CancelKey)) { *aborted = true; infostream << "Connect aborted [Escape]" << std::endl; return false; @@ -2702,7 +2696,7 @@ void Game::processUserInput(VolatileRunFlags *flags, if (m_cache_doubletap_jump && runData->jump_timer <= 0.2) runData->jump_timer += dtime; - processKeyboardInput( + processKeyInput( flags, &runData->statustext_time, &runData->jump_timer, @@ -2714,7 +2708,7 @@ void Game::processUserInput(VolatileRunFlags *flags, } -void Game::processKeyboardInput(VolatileRunFlags *flags, +void Game::processKeyInput(VolatileRunFlags *flags, float *statustext_time, float *jump_timer, bool *reset_jump_timer, @@ -2724,66 +2718,66 @@ void Game::processKeyboardInput(VolatileRunFlags *flags, //TimeTaker tt("process kybd input", NULL, PRECISION_NANO); - if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_DROP])) { + if (wasKeyDown(KeyType::DROP)) { dropSelectedItem(); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_AUTORUN])) { + } else if (wasKeyDown(KeyType::AUTORUN)) { toggleAutorun(statustext_time); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_INVENTORY])) { + } else if (wasKeyDown(KeyType::INVENTORY)) { openInventory(); - } else if (input->wasKeyDown(EscapeKey) || input->wasKeyDown(CancelKey)) { + } else if (wasKeyDown(KeyType::ESC) || input->wasKeyDown(CancelKey)) { if (!gui_chat_console->isOpenInhibited()) { show_pause_menu(¤t_formspec, client, gamedef, texture_src, device, simple_singleplayer_mode); } - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_CHAT])) { + } else if (wasKeyDown(KeyType::CHAT)) { openConsole(0.2, L""); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_CMD])) { + } else if (wasKeyDown(KeyType::CMD)) { openConsole(0.2, L"/"); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_CONSOLE])) { + } else if (wasKeyDown(KeyType::CONSOLE)) { openConsole(1); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_FREEMOVE])) { + } else if (wasKeyDown(KeyType::FREEMOVE)) { toggleFreeMove(statustext_time); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_JUMP])) { + } else if (wasKeyDown(KeyType::JUMP)) { toggleFreeMoveAlt(statustext_time, jump_timer); *reset_jump_timer = true; - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_FASTMOVE])) { + } else if (wasKeyDown(KeyType::FASTMOVE)) { toggleFast(statustext_time); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_NOCLIP])) { + } else if (wasKeyDown(KeyType::NOCLIP)) { toggleNoClip(statustext_time); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_CINEMATIC])) { + } else if (wasKeyDown(KeyType::CINEMATIC)) { toggleCinematic(statustext_time); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_SCREENSHOT])) { + } else if (wasKeyDown(KeyType::SCREENSHOT)) { client->makeScreenshot(device); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_TOGGLE_HUD])) { + } else if (wasKeyDown(KeyType::TOGGLE_HUD)) { toggleHud(statustext_time, &flags->show_hud); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_MINIMAP])) { + } else if (wasKeyDown(KeyType::MINIMAP)) { toggleMinimap(statustext_time, &flags->show_minimap, flags->show_hud, - input->isKeyDown(keycache.key[KeyCache::KEYMAP_ID_SNEAK])); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_TOGGLE_CHAT])) { + isKeyDown(KeyType::SNEAK)); + } else if (wasKeyDown(KeyType::TOGGLE_CHAT)) { toggleChat(statustext_time, &flags->show_chat); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_TOGGLE_FORCE_FOG_OFF])) { + } else if (wasKeyDown(KeyType::TOGGLE_FORCE_FOG_OFF)) { toggleFog(statustext_time, &flags->force_fog_off); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_TOGGLE_UPDATE_CAMERA])) { + } else if (wasKeyDown(KeyType::TOGGLE_UPDATE_CAMERA)) { toggleUpdateCamera(statustext_time, &flags->disable_camera_update); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_TOGGLE_DEBUG])) { + } else if (wasKeyDown(KeyType::TOGGLE_DEBUG)) { toggleDebug(statustext_time, &flags->show_debug, &flags->show_profiler_graph); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_TOGGLE_PROFILER])) { + } else if (wasKeyDown(KeyType::TOGGLE_PROFILER)) { toggleProfiler(statustext_time, profiler_current_page, profiler_max_page); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_INCREASE_VIEWING_RANGE])) { + } else if (wasKeyDown(KeyType::INCREASE_VIEWING_RANGE)) { increaseViewRange(statustext_time); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_DECREASE_VIEWING_RANGE])) { + } else if (wasKeyDown(KeyType::DECREASE_VIEWING_RANGE)) { decreaseViewRange(statustext_time); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_RANGESELECT])) { + } else if (wasKeyDown(KeyType::RANGESELECT)) { toggleFullViewRange(statustext_time); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_QUICKTUNE_NEXT])) { + } else if (wasKeyDown(KeyType::QUICKTUNE_NEXT)) { quicktune->next(); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_QUICKTUNE_PREV])) { + } else if (wasKeyDown(KeyType::QUICKTUNE_PREV)) { quicktune->prev(); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_QUICKTUNE_INC])) { + } else if (wasKeyDown(KeyType::QUICKTUNE_INC)) { quicktune->inc(); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_QUICKTUNE_DEC])) { + } else if (wasKeyDown(KeyType::QUICKTUNE_DEC)) { quicktune->dec(); - } else if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_DEBUG_STACKS])) { + } else if (wasKeyDown(KeyType::DEBUG_STACKS)) { // Print debug stacks dstream << "-----------------------------------------" << std::endl; @@ -2793,7 +2787,7 @@ void Game::processKeyboardInput(VolatileRunFlags *flags, debug_stacks_print(); } - if (!input->isKeyDown(getKeySetting("keymap_jump")) && *reset_jump_timer) { + if (!isKeyDown(KeyType::JUMP) && *reset_jump_timer) { *reset_jump_timer = false; *jump_timer = 0.0; } @@ -2807,7 +2801,6 @@ void Game::processKeyboardInput(VolatileRunFlags *flags, } } - void Game::processItemSelection(u16 *new_playeritem) { LocalPlayer *player = client->getEnv().getLocalPlayer(); @@ -3228,13 +3221,13 @@ void Game::updatePlayerControl(const CameraOrientation &cam) //TimeTaker tt("update player control", NULL, PRECISION_NANO); PlayerControl control( - input->isKeyDown(keycache.key[KeyCache::KEYMAP_ID_FORWARD]), - input->isKeyDown(keycache.key[KeyCache::KEYMAP_ID_BACKWARD]), - input->isKeyDown(keycache.key[KeyCache::KEYMAP_ID_LEFT]), - input->isKeyDown(keycache.key[KeyCache::KEYMAP_ID_RIGHT]), - input->isKeyDown(keycache.key[KeyCache::KEYMAP_ID_JUMP]), - input->isKeyDown(keycache.key[KeyCache::KEYMAP_ID_SPECIAL1]), - input->isKeyDown(keycache.key[KeyCache::KEYMAP_ID_SNEAK]), + input->isKeyDown(keycache.key[KeyType::FORWARD]), + input->isKeyDown(keycache.key[KeyType::BACKWARD]), + input->isKeyDown(keycache.key[KeyType::LEFT]), + input->isKeyDown(keycache.key[KeyType::RIGHT]), + input->isKeyDown(keycache.key[KeyType::JUMP]), + input->isKeyDown(keycache.key[KeyType::SPECIAL1]), + input->isKeyDown(keycache.key[KeyType::SNEAK]), input->getLeftState(), input->getRightState(), cam.camera_pitch, @@ -3242,13 +3235,13 @@ void Game::updatePlayerControl(const CameraOrientation &cam) ); u32 keypress_bits = - ( (u32)(input->isKeyDown(keycache.key[KeyCache::KEYMAP_ID_FORWARD]) & 0x1) << 0) | - ( (u32)(input->isKeyDown(keycache.key[KeyCache::KEYMAP_ID_BACKWARD]) & 0x1) << 1) | - ( (u32)(input->isKeyDown(keycache.key[KeyCache::KEYMAP_ID_LEFT]) & 0x1) << 2) | - ( (u32)(input->isKeyDown(keycache.key[KeyCache::KEYMAP_ID_RIGHT]) & 0x1) << 3) | - ( (u32)(input->isKeyDown(keycache.key[KeyCache::KEYMAP_ID_JUMP]) & 0x1) << 4) | - ( (u32)(input->isKeyDown(keycache.key[KeyCache::KEYMAP_ID_SPECIAL1]) & 0x1) << 5) | - ( (u32)(input->isKeyDown(keycache.key[KeyCache::KEYMAP_ID_SNEAK]) & 0x1) << 6) | + ( (u32)(input->isKeyDown(keycache.key[KeyType::FORWARD]) & 0x1) << 0) | + ( (u32)(input->isKeyDown(keycache.key[KeyType::BACKWARD]) & 0x1) << 1) | + ( (u32)(input->isKeyDown(keycache.key[KeyType::LEFT]) & 0x1) << 2) | + ( (u32)(input->isKeyDown(keycache.key[KeyType::RIGHT]) & 0x1) << 3) | + ( (u32)(input->isKeyDown(keycache.key[KeyType::JUMP]) & 0x1) << 4) | + ( (u32)(input->isKeyDown(keycache.key[KeyType::SPECIAL1]) & 0x1) << 5) | + ( (u32)(input->isKeyDown(keycache.key[KeyType::SNEAK]) & 0x1) << 6) | ( (u32)(input->getLeftState() & 0x1) << 7) | ( (u32)(input->getRightState() & 0x1) << 8 ); @@ -3522,7 +3515,7 @@ void Game::updateCamera(VolatileRunFlags *flags, u32 busy_time, v3s16 old_camera_offset = camera->getOffset(); - if (input->wasKeyDown(keycache.key[KeyCache::KEYMAP_ID_CAMERA_MODE])) { + if (wasKeyDown(KeyType::CAMERA_MODE)) { GenericCAO *playercao = player->getCAO(); // If playercao not loaded, don't change camera @@ -3665,7 +3658,7 @@ void Game::processPlayerInteraction(GameRunData *runData, - pointing away from node */ if (runData->digging) { - if (input->getLeftReleased()) { + if (getLeftReleased()) { infostream << "Left button released" << " (stopped digging)" << std::endl; runData->digging = false; @@ -3691,7 +3684,7 @@ void Game::processPlayerInteraction(GameRunData *runData, } } - if (!runData->digging && runData->ldown_for_dig && !input->getLeftState()) { + if (!runData->digging && runData->ldown_for_dig && !isLeftPressed()) { runData->ldown_for_dig = false; } @@ -3699,13 +3692,13 @@ void Game::processPlayerInteraction(GameRunData *runData, soundmaker->m_player_leftpunch_sound.name = ""; - if (input->getRightState()) + if (isRightPressed()) runData->repeat_rightclick_timer += dtime; else runData->repeat_rightclick_timer = 0; - if (playeritem_def.usable && input->getLeftState()) { - if (input->getLeftClicked()) + if (playeritem_def.usable && isLeftPressed()) { + if (getLeftClicked()) client->interact(4, pointed); } else if (pointed.type == POINTEDTHING_NODE) { ToolCapabilities playeritem_toolcap = @@ -3715,16 +3708,16 @@ void Game::processPlayerInteraction(GameRunData *runData, } else if (pointed.type == POINTEDTHING_OBJECT) { handlePointingAtObject(runData, pointed, playeritem, player_position, show_debug); - } else if (input->getLeftState()) { + } else if (isLeftPressed()) { // When button is held down in air, show continuous animation runData->left_punch = true; - } else if (input->getRightClicked()) { + } else if (getRightClicked()) { handlePointingAtNothing(runData, playeritem); } runData->pointed_old = pointed; - if (runData->left_punch || input->getLeftClicked()) + if (runData->left_punch || getLeftClicked()) camera->setDigging(0); // left click animation input->resetLeftClicked(); @@ -3769,19 +3762,19 @@ void Game::handlePointingAtNode(GameRunData *runData, } } - if (runData->nodig_delay_timer <= 0.0 && input->getLeftState() + if (runData->nodig_delay_timer <= 0.0 && isLeftPressed() && client->checkPrivilege("interact")) { handleDigging(runData, pointed, nodepos, playeritem_toolcap, dtime); } - if ((input->getRightClicked() || + if ((getRightClicked() || runData->repeat_rightclick_timer >= m_repeat_right_click_time) && client->checkPrivilege("interact")) { runData->repeat_rightclick_timer = 0; infostream << "Ground right-clicked" << std::endl; if (meta && meta->getString("formspec") != "" && !random_input - && !input->isKeyDown(getKeySetting("keymap_sneak"))) { + && !isKeyDown(KeyType::SNEAK)) { infostream << "Launching custom inventory view" << std::endl; InventoryLocation inventoryloc; @@ -3846,7 +3839,7 @@ void Game::handlePointingAtObject(GameRunData *runData, runData->selected_object->debugInfoText())); } - if (input->getLeftState()) { + if (isLeftPressed()) { bool do_punch = false; bool do_punch_damage = false; @@ -3856,7 +3849,7 @@ void Game::handlePointingAtObject(GameRunData *runData, runData->object_hit_delay_timer = object_hit_delay; } - if (input->getLeftClicked()) + if (getLeftClicked()) do_punch = true; if (do_punch) { @@ -3876,7 +3869,7 @@ void Game::handlePointingAtObject(GameRunData *runData, if (!disable_send) client->interact(0, pointed); } - } else if (input->getRightClicked()) { + } else if (getRightClicked()) { infostream << "Right-clicked object" << std::endl; client->interact(3, pointed); // place } diff --git a/src/game.h b/src/game.h index 5465ecdc6..d8c2f78fc 100644 --- a/src/game.h +++ b/src/game.h @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "irrlichttypes_extrabloated.h" #include +#include "client/keys.h" #include "keycode.h" #include From 2060fd9cbe587d7e8ffe0cecdd67925f13a56c05 Mon Sep 17 00:00:00 2001 From: est31 Date: Fri, 27 May 2016 08:35:07 +0200 Subject: [PATCH 053/418] Initial Gamepad support Adds initial ingame gamepad support to minetest. Full Formspec support is not implemented yet and can be added by a later change. --- builtin/settingtypes.txt | 11 ++ src/client/CMakeLists.txt | 1 + src/client/clientlauncher.cpp | 20 +++- src/client/inputhandler.h | 13 +++ src/client/joystick_controller.cpp | 179 +++++++++++++++++++++++++++++ src/client/joystick_controller.h | 163 ++++++++++++++++++++++++++ src/client/keys.h | 6 + src/content_cao.cpp | 4 +- src/defaultsettings.cpp | 3 + src/game.cpp | 167 +++++++++++++++++---------- src/game.h | 3 + src/guiEngine.cpp | 2 + src/guiEngine.h | 3 +- src/guiFormSpecMenu.cpp | 42 +++++-- src/guiFormSpecMenu.h | 13 ++- src/localplayer.cpp | 17 ++- src/player.h | 10 +- 17 files changed, 577 insertions(+), 80 deletions(-) create mode 100644 src/client/joystick_controller.cpp create mode 100644 src/client/joystick_controller.h diff --git a/builtin/settingtypes.txt b/builtin/settingtypes.txt index 538a04f33..b53e35a85 100644 --- a/builtin/settingtypes.txt +++ b/builtin/settingtypes.txt @@ -104,6 +104,17 @@ random_input (Random input) bool false # Continuous forward movement (only used for testing). continuous_forward (Continuous forward) bool false +# Enable Joysticks +enable_joysticks (Enable Joysticks) bool true + +# The time in seconds it takes between repeated events +# when holding down a joystick button combination. +repeat_joystick_button_time (Joystick button repetition invterval) float 0.17 + +# The sensitivity of the joystick axes for moving the +# ingame view frustum around. +joystick_frustum_sensitivity (Joystick frustum sensitivity) float 170 + # Key for moving the player forward. # See http://irrlicht.sourceforge.net/docu/namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3 keymap_forward (Forward key) key KEY_KEY_W diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index a1ec37fe3..5faa186a7 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -1,6 +1,7 @@ set(client_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/clientlauncher.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tile.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/joystick_controller.cpp PARENT_SCOPE ) diff --git a/src/client/clientlauncher.cpp b/src/client/clientlauncher.cpp index 404a16310..ee8662ed6 100644 --- a/src/client/clientlauncher.cpp +++ b/src/client/clientlauncher.cpp @@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "guiEngine.h" #include "player.h" #include "fontengine.h" +#include "joystick_controller.h" #include "clientlauncher.h" /* mainmenumanager.h @@ -499,7 +500,8 @@ void ClientLauncher::main_menu(MainMenuData *menudata) #endif /* show main menu */ - GUIEngine mymenu(device, guiroot, &g_menumgr, smgr, menudata, *kill); + GUIEngine mymenu(device, &input->joystick, guiroot, + &g_menumgr, smgr, menudata, *kill); smgr->clear(); /* leave scene manager in a clean state */ } @@ -558,6 +560,22 @@ bool ClientLauncher::create_engine_device() device = createDeviceEx(params); if (device) { + if (g_settings->getBool("enable_joysticks")) { + irr::core::array infos; + std::vector joystick_infos; + // Make sure this is called maximum once per + // irrlicht device, otherwise it will give you + // multiple events for the same joystick. + if (device->activateJoysticks(infos)) { + infostream << "Joystick support enabled" << std::endl; + joystick_infos.reserve(infos.size()); + for (u32 i = 0; i < infos.size(); i++) { + joystick_infos.push_back(infos[i]); + } + } else { + errorstream << "Could not activate joystick support." << std::endl; + } + } porting::initIrrlicht(device); } diff --git a/src/client/inputhandler.h b/src/client/inputhandler.h index 73e507fdc..824b0da2e 100644 --- a/src/client/inputhandler.h +++ b/src/client/inputhandler.h @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #define INPUT_HANDLER_H #include "irrlichttypes_extrabloated.h" +#include "joystick_controller.h" class MyEventReceiver : public IEventReceiver { @@ -62,6 +63,14 @@ public: return true; } #endif + + if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) { + /* TODO add a check like: + if (event.JoystickEvent != joystick_we_listen_for) + return false; + */ + return joystick->handleEvent(event.JoystickEvent); + } // handle mouse events if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) { if (noMenuActive() == false) { @@ -172,6 +181,8 @@ public: s32 mouse_wheel; + JoystickController *joystick; + #ifdef HAVE_TOUCHSCREENGUI TouchScreenGUI* m_touchscreengui; #endif @@ -202,6 +213,7 @@ public: m_receiver(receiver), m_mousepos(0,0) { + m_receiver->joystick = &joystick; } virtual bool isKeyDown(const KeyPress &keyCode) { @@ -288,6 +300,7 @@ public: void clear() { + joystick.clear(); m_receiver->clearInput(); } private: diff --git a/src/client/joystick_controller.cpp b/src/client/joystick_controller.cpp new file mode 100644 index 000000000..ef8d18ab0 --- /dev/null +++ b/src/client/joystick_controller.cpp @@ -0,0 +1,179 @@ +/* +Minetest +Copyright (C) 2016 est31, + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "joystick_controller.h" +#include "irrlichttypes_extrabloated.h" +#include "keys.h" +#include "settings.h" +#include "gettime.h" + +bool JoystickButtonCmb::isTriggered(const irr::SEvent::SJoystickEvent &ev) const +{ + u32 buttons = ev.ButtonStates; + + buttons &= filter_mask; + return buttons == compare_mask; +} + +bool JoystickAxisCmb::isTriggered(const irr::SEvent::SJoystickEvent &ev) const +{ + s16 ax_val = ev.Axis[axis_to_compare]; + + return (ax_val * direction < 0) && (thresh * direction > ax_val * direction); +} + +// spares many characters +#define JLO_B_PB(A, B, C) jlo.button_keys.push_back(JoystickButtonCmb(A, B, C)) +#define JLO_A_PB(A, B, C, D) jlo.axis_keys.push_back(JoystickAxisCmb(A, B, C, D)) + +static JoystickLayout create_default_layout() +{ + JoystickLayout jlo; + + jlo.axes_dead_border = 1024; + + const JoystickAxisLayout axes[JA_COUNT] = { + {0, 1}, // JA_SIDEWARD_MOVE + {1, 1}, // JA_FORWARD_MOVE + {3, 1}, // JA_FRUSTUM_HORIZONTAL + {4, 1}, // JA_FRUSTUM_VERTICAL + }; + memcpy(jlo.axes, axes, sizeof(jlo.axes)); + + u32 sb = 1 << 7; // START button mask + u32 fb = 1 << 3; // FOUR button mask + u32 bm = sb | fb; // Mask for Both Modifiers + + // The back button means "ESC". + JLO_B_PB(KeyType::ESC, 1 << 6, 1 << 6); + + // The start button counts as modifier as well as use key. + // JLO_B_PB(KeyType::USE, sb, sb)); + + // Accessible without start modifier button pressed + // regardless whether four is pressed or not + JLO_B_PB(KeyType::SNEAK, sb | 1 << 2, 1 << 2); + + // Accessible without four modifier button pressed + // regardless whether start is pressed or not + JLO_B_PB(KeyType::MOUSE_L, fb | 1 << 4, 1 << 4); + JLO_B_PB(KeyType::MOUSE_R, fb | 1 << 5, 1 << 5); + + // Accessible without any modifier pressed + JLO_B_PB(KeyType::JUMP, bm | 1 << 0, 1 << 0); + JLO_B_PB(KeyType::SPECIAL1, bm | 1 << 1, 1 << 1); + + // Accessible with start button not pressed, but four pressed + // TODO find usage for button 0 + JLO_B_PB(KeyType::DROP, bm | 1 << 1, fb | 1 << 1); + JLO_B_PB(KeyType::SCROLL_UP, bm | 1 << 4, fb | 1 << 4); + JLO_B_PB(KeyType::SCROLL_DOWN,bm | 1 << 5, fb | 1 << 5); + + // Accessible with start button and four pressed + // TODO find usage for buttons 0, 1 and 4, 5 + + // Now about the buttons simulated by the axes + + // Movement buttons, important for vessels + JLO_A_PB(KeyType::FORWARD, 1, 1, 1024); + JLO_A_PB(KeyType::BACKWARD, 1, -1, 1024); + JLO_A_PB(KeyType::LEFT, 0, 1, 1024); + JLO_A_PB(KeyType::RIGHT, 0, -1, 1024); + + // Scroll buttons + JLO_A_PB(KeyType::SCROLL_UP, 2, -1, 1024); + JLO_A_PB(KeyType::SCROLL_DOWN, 5, -1, 1024); + + return jlo; +} + +static const JoystickLayout default_layout = create_default_layout(); + +JoystickController::JoystickController() +{ + m_layout = &default_layout; + doubling_dtime = g_settings->getFloat("repeat_joystick_button_time"); + + for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) { + m_past_pressed_time[i] = 0; + } + clear(); +} + +bool JoystickController::handleEvent(const irr::SEvent::SJoystickEvent &ev) +{ + m_internal_time = getTimeMs() / 1000.f; + + std::bitset keys_pressed; + + // First generate a list of keys pressed + + for (size_t i = 0; i < m_layout->button_keys.size(); i++) { + if (m_layout->button_keys[i].isTriggered(ev)) { + keys_pressed.set(m_layout->button_keys[i].key); + } + } + + for (size_t i = 0; i < m_layout->axis_keys.size(); i++) { + if (m_layout->axis_keys[i].isTriggered(ev)) { + keys_pressed.set(m_layout->axis_keys[i].key); + } + } + + // Then update the values + + for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) { + if (keys_pressed[i]) { + if (!m_past_pressed_keys[i] && + m_past_pressed_time[i] < m_internal_time - doubling_dtime) { + m_past_pressed_keys[i] = true; + m_past_pressed_time[i] = m_internal_time; + } + } else if (m_pressed_keys[i]) { + m_past_released_keys[i] = true; + } + + m_pressed_keys[i] = keys_pressed[i]; + } + + for (size_t i = 0; i < JA_COUNT; i++) { + const JoystickAxisLayout &ax_la = m_layout->axes[i]; + m_axes_vals[i] = ax_la.invert * ev.Axis[ax_la.axis_id]; + } + + + return true; +} + +void JoystickController::clear() +{ + m_pressed_keys.reset(); + m_past_pressed_keys.reset(); + m_past_released_keys.reset(); + memset(m_axes_vals, 0, sizeof(m_axes_vals)); +} + +s16 JoystickController::getAxisWithoutDead(JoystickAxis axis) +{ + s16 v = m_axes_vals[axis]; + if (((v > 0) && (v < m_layout->axes_dead_border)) || + ((v < 0) && (v > -m_layout->axes_dead_border))) + return 0; + return v; +} diff --git a/src/client/joystick_controller.h b/src/client/joystick_controller.h new file mode 100644 index 000000000..ed0ee4068 --- /dev/null +++ b/src/client/joystick_controller.h @@ -0,0 +1,163 @@ +/* +Minetest +Copyright (C) 2016 est31, + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef JOYSTICK_HEADER +#define JOYSTICK_HEADER + +#include "irrlichttypes_extrabloated.h" +#include "keys.h" +#include +#include + +enum JoystickAxis { + JA_SIDEWARD_MOVE, + JA_FORWARD_MOVE, + + JA_FRUSTUM_HORIZONTAL, + JA_FRUSTUM_VERTICAL, + + // To know the count of enum values + JA_COUNT, +}; + +struct JoystickAxisLayout { + u16 axis_id; + // -1 if to invert, 1 if to keep it. + int invert; +}; + + +struct JoystickCombination { + + virtual bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const=0; + + GameKeyType key; +}; + +struct JoystickButtonCmb : public JoystickCombination { + + JoystickButtonCmb() {} + JoystickButtonCmb(GameKeyType key, u32 filter_mask, u32 compare_mask) : + filter_mask(filter_mask), + compare_mask(compare_mask) + { + this->key = key; + } + + virtual bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const; + + u32 filter_mask; + u32 compare_mask; +}; + +struct JoystickAxisCmb : public JoystickCombination { + + JoystickAxisCmb() {} + JoystickAxisCmb(GameKeyType key, u16 axis_to_compare, int direction, s16 thresh) : + axis_to_compare(axis_to_compare), + direction(direction), + thresh(thresh) + { + this->key = key; + } + + virtual bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const; + + u16 axis_to_compare; + + // if -1, thresh must be smaller than the axis value in order to trigger + // if 1, thresh must be bigger than the axis value in order to trigger + int direction; + s16 thresh; +}; + +struct JoystickLayout { + std::vector button_keys; + std::vector axis_keys; + JoystickAxisLayout axes[JA_COUNT]; + s16 axes_dead_border; +}; + +class JoystickController { + +public: + JoystickController(); + bool handleEvent(const irr::SEvent::SJoystickEvent &ev); + void clear(); + + bool wasKeyDown(GameKeyType b) + { + bool r = m_past_pressed_keys[b]; + m_past_pressed_keys[b] = false; + return r; + } + bool getWasKeyDown(GameKeyType b) + { + return m_past_pressed_keys[b]; + } + void clearWasKeyDown(GameKeyType b) + { + m_past_pressed_keys[b] = false; + } + + bool wasKeyReleased(GameKeyType b) + { + bool r = m_past_released_keys[b]; + m_past_released_keys[b] = false; + return r; + } + bool getWasKeyReleased(GameKeyType b) + { + return m_past_pressed_keys[b]; + } + void clearWasKeyReleased(GameKeyType b) + { + m_past_pressed_keys[b] = false; + } + + bool isKeyDown(GameKeyType b) + { + return m_pressed_keys[b]; + } + + s16 getAxis(JoystickAxis axis) + { + return m_axes_vals[axis]; + } + + s16 getAxisWithoutDead(JoystickAxis axis); + + f32 doubling_dtime; + +private: + const JoystickLayout *m_layout; + + s16 m_axes_vals[JA_COUNT]; + + std::bitset m_pressed_keys; + + f32 m_internal_time; + + f32 m_past_pressed_time[KeyType::INTERNAL_ENUM_COUNT]; + + std::bitset m_past_pressed_keys; + std::bitset m_past_released_keys; +}; + +#endif diff --git a/src/client/keys.h b/src/client/keys.h index 08b1c4123..0921bc166 100644 --- a/src/client/keys.h +++ b/src/client/keys.h @@ -67,6 +67,12 @@ public: DEBUG_STACKS, + // joystick specific keys + MOUSE_L, + MOUSE_R, + SCROLL_UP, + SCROLL_DOWN, + // Fake keycode for array size and internal checks INTERNAL_ENUM_COUNT diff --git a/src/content_cao.cpp b/src/content_cao.cpp index 35ab1c508..eeb85c8a6 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -1056,7 +1056,9 @@ void GenericCAO::step(float dtime, ClientEnvironment *env) PlayerControl controls = player->getPlayerControl(); bool walking = false; - if(controls.up || controls.down || controls.left || controls.right) + if (controls.up || controls.down || controls.left || controls.right || + controls.forw_move_joystick_axis != 0.f || + controls.sidew_move_joystick_axis != 0.f) walking = true; f32 new_speed = player->local_animation_speed; diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index 632ec0df9..43a7a7c00 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -116,6 +116,9 @@ void set_default_settings(Settings *settings) settings->setDefault("free_move", "false"); settings->setDefault("noclip", "false"); settings->setDefault("continuous_forward", "false"); + settings->setDefault("enable_joysticks", "true"); + settings->setDefault("repeat_joystick_button_time", "0.17"); + settings->setDefault("joystick_frustum_sensitivity", "170"); settings->setDefault("cinematic", "false"); settings->setDefault("camera_smoothing", "0"); settings->setDefault("cinematic_camera_smoothing", "0.7"); diff --git a/src/game.cpp b/src/game.cpp index ac1c0fe6b..ba77d299a 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "client.h" #include "client/tile.h" // For TextureSource #include "client/keys.h" +#include "client/joystick_controller.h" #include "clientmap.h" #include "clouds.h" #include "config.h" @@ -1108,12 +1109,14 @@ bool nodePlacementPrediction(Client &client, static inline void create_formspec_menu(GUIFormSpecMenu **cur_formspec, InventoryManager *invmgr, IGameDef *gamedef, IWritableTextureSource *tsrc, IrrlichtDevice *device, + JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest, Client *client) { if (*cur_formspec == 0) { - *cur_formspec = new GUIFormSpecMenu(device, guiroot, -1, &g_menumgr, - invmgr, gamedef, tsrc, fs_src, txt_dest, client); + *cur_formspec = new GUIFormSpecMenu(device, joystick, + guiroot, -1, &g_menumgr, invmgr, gamedef, tsrc, + fs_src, txt_dest, client); (*cur_formspec)->doPause = false; /* @@ -1138,7 +1141,8 @@ static inline void create_formspec_menu(GUIFormSpecMenu **cur_formspec, static void show_deathscreen(GUIFormSpecMenu **cur_formspec, InventoryManager *invmgr, IGameDef *gamedef, - IWritableTextureSource *tsrc, IrrlichtDevice *device, Client *client) + IWritableTextureSource *tsrc, IrrlichtDevice *device, + JoystickController *joystick, Client *client) { std::string formspec = std::string(FORMSPEC_VERSION_STRING) + @@ -1154,14 +1158,15 @@ static void show_deathscreen(GUIFormSpecMenu **cur_formspec, FormspecFormSource *fs_src = new FormspecFormSource(formspec); LocalFormspecHandler *txt_dst = new LocalFormspecHandler("MT_DEATH_SCREEN", client); - create_formspec_menu(cur_formspec, invmgr, gamedef, tsrc, device, fs_src, txt_dst, NULL); + create_formspec_menu(cur_formspec, invmgr, gamedef, tsrc, device, + joystick, fs_src, txt_dst, NULL); } /******************************************************************************/ static void show_pause_menu(GUIFormSpecMenu **cur_formspec, InventoryManager *invmgr, IGameDef *gamedef, IWritableTextureSource *tsrc, IrrlichtDevice *device, - bool singleplayermode) + JoystickController *joystick, bool singleplayermode) { #ifdef __ANDROID__ std::string control_text = strgettext("Default Controls:\n" @@ -1226,7 +1231,8 @@ static void show_pause_menu(GUIFormSpecMenu **cur_formspec, FormspecFormSource *fs_src = new FormspecFormSource(os.str()); LocalFormspecHandler *txt_dst = new LocalFormspecHandler("MT_PAUSE_MENU"); - create_formspec_menu(cur_formspec, invmgr, gamedef, tsrc, device, fs_src, txt_dst, NULL); + create_formspec_menu(cur_formspec, invmgr, gamedef, tsrc, device, + joystick, fs_src, txt_dst, NULL); std::string con("btn_continue"); (*cur_formspec)->setFocus(con); (*cur_formspec)->doPause = true; @@ -1574,9 +1580,10 @@ protected: void decreaseViewRange(float *statustext_time); void toggleFullViewRange(float *statustext_time); - void updateCameraDirection(CameraOrientation *cam, VolatileRunFlags *flags); + void updateCameraDirection(CameraOrientation *cam, VolatileRunFlags *flags, + float dtime); void updateCameraOrientation(CameraOrientation *cam, - const VolatileRunFlags &flags); + const VolatileRunFlags &flags, float dtime); void updatePlayerControl(const CameraOrientation &cam); void step(f32 *dtime); void processClientEvents(CameraOrientation *cam, float *damage_flash); @@ -1611,34 +1618,39 @@ protected: static void settingChangedCallback(const std::string &setting_name, void *data); void readSettings(); - bool getLeftClicked() + inline bool getLeftClicked() { - return input->getLeftClicked(); + return input->getLeftClicked() || + input->joystick.getWasKeyDown(KeyType::MOUSE_L); } - bool getRightClicked() + inline bool getRightClicked() { - return input->getRightClicked(); + return input->getRightClicked() || + input->joystick.getWasKeyDown(KeyType::MOUSE_R); } - bool isLeftPressed() + inline bool isLeftPressed() { - return input->getLeftState(); + return input->getLeftState() || + input->joystick.isKeyDown(KeyType::MOUSE_L); } - bool isRightPressed() + inline bool isRightPressed() { - return input->getRightState(); + return input->getRightState() || + input->joystick.isKeyDown(KeyType::MOUSE_R); } - bool getLeftReleased() + inline bool getLeftReleased() { - return input->getLeftReleased(); + return input->getLeftReleased() || + input->joystick.wasKeyReleased(KeyType::MOUSE_L); } - bool isKeyDown(GameKeyType k) + inline bool isKeyDown(GameKeyType k) { - return input->isKeyDown(keycache.key[k]); + return input->isKeyDown(keycache.key[k]) || input->joystick.isKeyDown(k); } - bool wasKeyDown(GameKeyType k) + inline bool wasKeyDown(GameKeyType k) { - return input->wasKeyDown(keycache.key[k]); + return input->wasKeyDown(keycache.key[k]) || input->joystick.wasKeyDown(k); } #ifdef __ANDROID__ @@ -1724,9 +1736,11 @@ private: */ bool m_cache_doubletap_jump; bool m_cache_enable_clouds; + bool m_cache_enable_joysticks; bool m_cache_enable_particles; bool m_cache_enable_fog; f32 m_cache_mouse_sensitivity; + f32 m_cache_joystick_frustum_sensitivity; f32 m_repeat_right_click_time; #ifdef __ANDROID__ @@ -1762,12 +1776,16 @@ Game::Game() : &settingChangedCallback, this); g_settings->registerChangedCallback("enable_clouds", &settingChangedCallback, this); + g_settings->registerChangedCallback("doubletap_joysticks", + &settingChangedCallback, this); g_settings->registerChangedCallback("enable_particles", &settingChangedCallback, this); g_settings->registerChangedCallback("enable_fog", &settingChangedCallback, this); g_settings->registerChangedCallback("mouse_sensitivity", &settingChangedCallback, this); + g_settings->registerChangedCallback("joystick_frustum_sensitivity", + &settingChangedCallback, this); g_settings->registerChangedCallback("repeat_rightclick_time", &settingChangedCallback, this); @@ -1930,7 +1948,7 @@ void Game::run() updateProfilers(runData, stats, draw_times, dtime); processUserInput(&flags, &runData, dtime); // Update camera before player movement to avoid camera lag of one frame - updateCameraDirection(&cam_view_target, &flags); + updateCameraDirection(&cam_view_target, &flags, dtime); float cam_smoothing = 0; if (g_settings->getBool("cinematic")) cam_smoothing = 1 - g_settings->getFloat("cinematic_camera_smoothing"); @@ -2727,7 +2745,8 @@ void Game::processKeyInput(VolatileRunFlags *flags, } else if (wasKeyDown(KeyType::ESC) || input->wasKeyDown(CancelKey)) { if (!gui_chat_console->isOpenInhibited()) { show_pause_menu(¤t_formspec, client, gamedef, - texture_src, device, simple_singleplayer_mode); + texture_src, device, &input->joystick, + simple_singleplayer_mode); } } else if (wasKeyDown(KeyType::CHAT)) { openConsole(0.2, L""); @@ -2813,12 +2832,21 @@ void Game::processItemSelection(u16 *new_playeritem) u16 max_item = MYMIN(PLAYER_INVENTORY_SIZE - 1, player->hud_hotbar_itemcount - 1); - if (wheel < 0) - *new_playeritem = *new_playeritem < max_item ? *new_playeritem + 1 : 0; - else if (wheel > 0) - *new_playeritem = *new_playeritem > 0 ? *new_playeritem - 1 : max_item; - // else wheel == 0 + s32 dir = wheel; + if (input->joystick.wasKeyDown(KeyType::SCROLL_DOWN)) { + dir = -1; + } + + if (input->joystick.wasKeyDown(KeyType::SCROLL_UP)) { + dir = 1; + } + + if (dir < 0) + *new_playeritem = *new_playeritem < max_item ? *new_playeritem + 1 : 0; + else if (dir > 0) + *new_playeritem = *new_playeritem > 0 ? *new_playeritem - 1 : max_item; + // else dir == 0 /* Item selection using keyboard */ @@ -2868,7 +2896,7 @@ void Game::openInventory() TextDest *txt_dst = new TextDestPlayerInventory(client); create_formspec_menu(¤t_formspec, client, gamedef, texture_src, - device, fs_src, txt_dst, client); + device, &input->joystick, fs_src, txt_dst, client); InventoryLocation inventoryloc; inventoryloc.setCurrentPlayer(); @@ -3154,7 +3182,7 @@ void Game::toggleFullViewRange(float *statustext_time) void Game::updateCameraDirection(CameraOrientation *cam, - VolatileRunFlags *flags) + VolatileRunFlags *flags, float dtime) { if ((device->isWindowActive() && noMenuActive()) || random_input) { @@ -3169,7 +3197,7 @@ void Game::updateCameraDirection(CameraOrientation *cam, if (flags->first_loop_after_window_activation) flags->first_loop_after_window_activation = false; else - updateCameraOrientation(cam, *flags); + updateCameraOrientation(cam, *flags, dtime); input->setMousePos((driver->getScreenSize().Width / 2), (driver->getScreenSize().Height / 2)); @@ -3187,9 +3215,8 @@ void Game::updateCameraDirection(CameraOrientation *cam, } } - void Game::updateCameraOrientation(CameraOrientation *cam, - const VolatileRunFlags &flags) + const VolatileRunFlags &flags, float dtime) { #ifdef HAVE_TOUCHSCREENGUI if (g_touchscreengui) { @@ -3197,6 +3224,7 @@ void Game::updateCameraOrientation(CameraOrientation *cam, cam->camera_pitch = g_touchscreengui->getPitch(); } else { #endif + s32 dx = input->getMousePos().X - (driver->getScreenSize().Width / 2); s32 dy = input->getMousePos().Y - (driver->getScreenSize().Height / 2); @@ -3212,6 +3240,14 @@ void Game::updateCameraOrientation(CameraOrientation *cam, } #endif + if (m_cache_enable_joysticks) { + f32 c = m_cache_joystick_frustum_sensitivity * (1.f / 32767.f) * dtime; + cam->camera_yaw -= input->joystick.getAxisWithoutDead(JA_FRUSTUM_HORIZONTAL) * + c; + cam->camera_pitch += input->joystick.getAxisWithoutDead(JA_FRUSTUM_VERTICAL) * + c; + } + cam->camera_pitch = rangelim(cam->camera_pitch, -89.5, 89.5); } @@ -3220,30 +3256,36 @@ void Game::updatePlayerControl(const CameraOrientation &cam) { //TimeTaker tt("update player control", NULL, PRECISION_NANO); + // DO NOT use the isKeyDown method for the forward, backward, left, right + // buttons, as the code that uses the controls needs to be able to + // distinguish between the two in order to know when to use joysticks. + PlayerControl control( input->isKeyDown(keycache.key[KeyType::FORWARD]), input->isKeyDown(keycache.key[KeyType::BACKWARD]), input->isKeyDown(keycache.key[KeyType::LEFT]), input->isKeyDown(keycache.key[KeyType::RIGHT]), - input->isKeyDown(keycache.key[KeyType::JUMP]), - input->isKeyDown(keycache.key[KeyType::SPECIAL1]), - input->isKeyDown(keycache.key[KeyType::SNEAK]), - input->getLeftState(), - input->getRightState(), + isKeyDown(KeyType::JUMP), + isKeyDown(KeyType::SPECIAL1), + isKeyDown(KeyType::SNEAK), + isLeftPressed(), + isRightPressed(), cam.camera_pitch, - cam.camera_yaw + cam.camera_yaw, + input->joystick.getAxisWithoutDead(JA_SIDEWARD_MOVE), + input->joystick.getAxisWithoutDead(JA_FORWARD_MOVE) ); u32 keypress_bits = - ( (u32)(input->isKeyDown(keycache.key[KeyType::FORWARD]) & 0x1) << 0) | - ( (u32)(input->isKeyDown(keycache.key[KeyType::BACKWARD]) & 0x1) << 1) | - ( (u32)(input->isKeyDown(keycache.key[KeyType::LEFT]) & 0x1) << 2) | - ( (u32)(input->isKeyDown(keycache.key[KeyType::RIGHT]) & 0x1) << 3) | - ( (u32)(input->isKeyDown(keycache.key[KeyType::JUMP]) & 0x1) << 4) | - ( (u32)(input->isKeyDown(keycache.key[KeyType::SPECIAL1]) & 0x1) << 5) | - ( (u32)(input->isKeyDown(keycache.key[KeyType::SNEAK]) & 0x1) << 6) | - ( (u32)(input->getLeftState() & 0x1) << 7) | - ( (u32)(input->getRightState() & 0x1) << 8 + ( (u32)(isKeyDown(KeyType::FORWARD) & 0x1) << 0) | + ( (u32)(isKeyDown(KeyType::BACKWARD) & 0x1) << 1) | + ( (u32)(isKeyDown(KeyType::LEFT) & 0x1) << 2) | + ( (u32)(isKeyDown(KeyType::RIGHT) & 0x1) << 3) | + ( (u32)(isKeyDown(KeyType::JUMP) & 0x1) << 4) | + ( (u32)(isKeyDown(KeyType::SPECIAL1) & 0x1) << 5) | + ( (u32)(isKeyDown(KeyType::SNEAK) & 0x1) << 6) | + ( (u32)(isLeftPressed() & 0x1) << 7) | + ( (u32)(isRightPressed() & 0x1) << 8 ); #ifdef ANDROID @@ -3312,7 +3354,7 @@ void Game::processClientEvents(CameraOrientation *cam, float *damage_flash) cam->camera_pitch = event.player_force_move.pitch; } else if (event.type == CE_DEATHSCREEN) { show_deathscreen(¤t_formspec, client, gamedef, texture_src, - device, client); + device, &input->joystick, client); chat_backend->addMessage(L"", L"You died."); @@ -3328,7 +3370,8 @@ void Game::processClientEvents(CameraOrientation *cam, float *damage_flash) new TextDestPlayerInventory(client, *(event.show_formspec.formname)); create_formspec_menu(¤t_formspec, client, gamedef, - texture_src, device, fs_src, txt_dst, client); + texture_src, device, &input->joystick, + fs_src, txt_dst, client); delete(event.show_formspec.formspec); delete(event.show_formspec.formname); @@ -3723,8 +3766,14 @@ void Game::processPlayerInteraction(GameRunData *runData, input->resetLeftClicked(); input->resetRightClicked(); + input->joystick.clearWasKeyDown(KeyType::MOUSE_L); + input->joystick.clearWasKeyDown(KeyType::MOUSE_R); + input->resetLeftReleased(); input->resetRightReleased(); + + input->joystick.clearWasKeyReleased(KeyType::MOUSE_L); + input->joystick.clearWasKeyReleased(KeyType::MOUSE_R); } @@ -3785,7 +3834,7 @@ void Game::handlePointingAtNode(GameRunData *runData, TextDest *txt_dst = new TextDestNodeMetadata(nodepos, client); create_formspec_menu(¤t_formspec, client, gamedef, - texture_src, device, fs_src, txt_dst, client); + texture_src, device, &input->joystick, fs_src, txt_dst, client); current_formspec->setFormSpec(meta->getString("formspec"), inventoryloc); } else { @@ -4468,12 +4517,14 @@ void Game::settingChangedCallback(const std::string &setting_name, void *data) void Game::readSettings() { - m_cache_doubletap_jump = g_settings->getBool("doubletap_jump"); - m_cache_enable_clouds = g_settings->getBool("enable_clouds"); - m_cache_enable_particles = g_settings->getBool("enable_particles"); - m_cache_enable_fog = g_settings->getBool("enable_fog"); - m_cache_mouse_sensitivity = g_settings->getFloat("mouse_sensitivity"); - m_repeat_right_click_time = g_settings->getFloat("repeat_rightclick_time"); + m_cache_doubletap_jump = g_settings->getBool("doubletap_jump"); + m_cache_enable_clouds = g_settings->getBool("enable_clouds"); + m_cache_enable_joysticks = g_settings->getBool("enable_joysticks"); + m_cache_enable_particles = g_settings->getBool("enable_particles"); + m_cache_enable_fog = g_settings->getBool("enable_fog"); + m_cache_mouse_sensitivity = g_settings->getFloat("mouse_sensitivity"); + m_cache_joystick_frustum_sensitivity = g_settings->getFloat("joystick_frustum_sensitivity"); + m_repeat_right_click_time = g_settings->getFloat("repeat_rightclick_time"); m_cache_mouse_sensitivity = rangelim(m_cache_mouse_sensitivity, 0.001, 100.0); } diff --git a/src/game.h b/src/game.h index d8c2f78fc..df32e3397 100644 --- a/src/game.h +++ b/src/game.h @@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "irrlichttypes_extrabloated.h" #include #include "client/keys.h" +#include "client/joystick_controller.h" #include "keycode.h" #include @@ -135,6 +136,8 @@ public: virtual void step(float dtime) {} virtual void clear() {} + + JoystickController joystick; }; class ChatBackend; /* to avoid having to include chat.h */ diff --git a/src/guiEngine.cpp b/src/guiEngine.cpp index 96de7a4f7..b9d796ccb 100644 --- a/src/guiEngine.cpp +++ b/src/guiEngine.cpp @@ -131,6 +131,7 @@ void MenuMusicFetcher::fetchSounds(const std::string &name, /** GUIEngine */ /******************************************************************************/ GUIEngine::GUIEngine( irr::IrrlichtDevice* dev, + JoystickController *joystick, gui::IGUIElement* parent, IMenuManager *menumgr, scene::ISceneManager* smgr, @@ -189,6 +190,7 @@ GUIEngine::GUIEngine( irr::IrrlichtDevice* dev, /* Create menu */ m_menu = new GUIFormSpecMenu(m_device, + joystick, m_parent, -1, m_menumanager, diff --git a/src/guiEngine.h b/src/guiEngine.h index fa98a21e4..a59436953 100644 --- a/src/guiEngine.h +++ b/src/guiEngine.h @@ -149,7 +149,8 @@ public: * @param smgr scene manager to add scene elements to * @param data struct to transfer data to main game handling */ - GUIEngine( irr::IrrlichtDevice* dev, + GUIEngine(irr::IrrlichtDevice* dev, + JoystickController *joystick, gui::IGUIElement* parent, IMenuManager *menumgr, scene::ISceneManager* smgr, diff --git a/src/guiFormSpecMenu.cpp b/src/guiFormSpecMenu.cpp index cf01dc38c..a9cbb6254 100644 --- a/src/guiFormSpecMenu.cpp +++ b/src/guiFormSpecMenu.cpp @@ -79,6 +79,7 @@ static unsigned int font_line_height(gui::IGUIFont *font) } GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev, + JoystickController *joystick, gui::IGUIElement* parent, s32 id, IMenuManager *menumgr, InventoryManager *invmgr, IGameDef *gamedef, ISimpleTextureSource *tsrc, IFormSource* fsrc, TextDest* tdst, @@ -102,6 +103,7 @@ GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev, m_text_dst(tdst), m_formspec_version(0), m_focused_element(""), + m_joystick(joystick), m_font(NULL), m_remap_dbl_click(remap_dbl_click) #ifdef __ANDROID__ @@ -2459,7 +2461,7 @@ void GUIFormSpecMenu::drawMenu() Draw static text elements */ for (u32 i = 0; i < m_static_texts.size(); i++) { - const StaticTextSpec &spec = m_static_texts[i]; + const StaticTextSpec &spec = m_static_texts[i]; core::rect rect = spec.rect; if (spec.parent_button && spec.parent_button->isPressed()) { #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8) @@ -3024,6 +3026,25 @@ bool GUIFormSpecMenu::preprocessEvent(const SEvent& event) } #endif + if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) { + /* TODO add a check like: + if (event.JoystickEvent != joystick_we_listen_for) + return false; + */ + bool handled = m_joystick->handleEvent(event.JoystickEvent); + if (handled) { + if (m_joystick->wasKeyDown(KeyType::ESC)) { + tryClose(); + } else if (m_joystick->wasKeyDown(KeyType::JUMP)) { + if (m_allowclose) { + acceptInput(quit_mode_accept); + quitMenu(); + } + } + } + return handled; + } + return false; } @@ -3085,19 +3106,24 @@ bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event) return false; } +void GUIFormSpecMenu::tryClose() +{ + if (m_allowclose) { + doPause = false; + acceptInput(quit_mode_cancel); + quitMenu(); + } else { + m_text_dst->gotText(L"MenuQuit"); + } +} + bool GUIFormSpecMenu::OnEvent(const SEvent& event) { if (event.EventType==EET_KEY_INPUT_EVENT) { KeyPress kp(event.KeyInput); if (event.KeyInput.PressedDown && ( (kp == EscapeKey) || (kp == getKeySetting("keymap_inventory")) || (kp == CancelKey))) { - if (m_allowclose) { - doPause = false; - acceptInput(quit_mode_cancel); - quitMenu(); - } else { - m_text_dst->gotText(L"MenuQuit"); - } + tryClose(); return true; } else if (m_client != NULL && event.KeyInput.PressedDown && (kp == getKeySetting("keymap_screenshot"))) { diff --git a/src/guiFormSpecMenu.h b/src/guiFormSpecMenu.h index 4122b1f56..2fb55070d 100644 --- a/src/guiFormSpecMenu.h +++ b/src/guiFormSpecMenu.h @@ -29,6 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "modalMenu.h" #include "guiTable.h" #include "network/networkprotocol.h" +#include "client/joystick_controller.h" #include "util/string.h" #include "util/enriched_string.h" @@ -278,6 +279,7 @@ class GUIFormSpecMenu : public GUIModalMenu public: GUIFormSpecMenu(irr::IrrlichtDevice* dev, + JoystickController *joystick, gui::IGUIElement* parent, s32 id, IMenuManager *menumgr, InventoryManager *invmgr, @@ -433,10 +435,11 @@ protected: video::SColor m_default_tooltip_color; private: - IFormSource *m_form_src; - TextDest *m_text_dst; - unsigned int m_formspec_version; - std::string m_focused_element; + IFormSource *m_form_src; + TextDest *m_text_dst; + unsigned int m_formspec_version; + std::string m_focused_element; + JoystickController *m_joystick; typedef struct { bool explicit_size; @@ -494,6 +497,8 @@ private: bool parseSizeDirect(parserData* data, std::string element); void parseScrollBar(parserData* data, std::string element); + void tryClose(); + /** * check if event is part of a double click * @param event event to evaluate diff --git a/src/localplayer.cpp b/src/localplayer.cpp index 507f31980..732ca8acf 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -528,18 +528,23 @@ void LocalPlayer::applyControl(float dtime) speedH += move_direction; } } - if(control.down) - { + if (control.down) { speedH -= move_direction; } - if(control.left) - { + if (!control.up && !control.down) { + speedH -= move_direction * + (control.forw_move_joystick_axis / 32767.f); + } + if (control.left) { speedH += move_direction.crossProduct(v3f(0,1,0)); } - if(control.right) - { + if (control.right) { speedH += move_direction.crossProduct(v3f(0,-1,0)); } + if (!control.left && !control.right) { + speedH -= move_direction.crossProduct(v3f(0,1,0)) * + (control.sidew_move_joystick_axis / 32767.f); + } if(control.jump) { if (free_move) { diff --git a/src/player.h b/src/player.h index b317cda4f..6687ca86e 100644 --- a/src/player.h +++ b/src/player.h @@ -46,6 +46,8 @@ struct PlayerControl RMB = false; pitch = 0; yaw = 0; + sidew_move_joystick_axis = .0f; + forw_move_joystick_axis = .0f; } PlayerControl( bool a_up, @@ -58,7 +60,9 @@ struct PlayerControl bool a_LMB, bool a_RMB, float a_pitch, - float a_yaw + float a_yaw, + float a_sidew_move_joystick_axis, + float a_forw_move_joystick_axis ) { up = a_up; @@ -72,6 +76,8 @@ struct PlayerControl RMB = a_RMB; pitch = a_pitch; yaw = a_yaw; + sidew_move_joystick_axis = a_sidew_move_joystick_axis; + forw_move_joystick_axis = a_forw_move_joystick_axis; } bool up; bool down; @@ -84,6 +90,8 @@ struct PlayerControl bool RMB; float pitch; float yaw; + float sidew_move_joystick_axis; + float forw_move_joystick_axis; }; class Map; From dfbdb5bcd7bc48efb21d585d5c22454a9d5f0f1e Mon Sep 17 00:00:00 2001 From: kwolekr Date: Sat, 4 Jun 2016 01:35:37 -0400 Subject: [PATCH 054/418] Change internal type for seeds to s32 This fixes value truncation (and therefore incompatibility) on platforms with an LP32 data model, such as VAX or MS-DOS. --- src/cavegen.cpp | 4 ++-- src/cavegen.h | 6 +++--- src/dungeongen.h | 2 +- src/mapgen.cpp | 20 +++++++++++++++++--- src/mapgen.h | 6 +++--- src/mg_biome.h | 2 +- src/noise.cpp | 26 +++++++++++++------------- src/noise.h | 32 ++++++++++++++++---------------- src/script/lua_api/l_env.cpp | 2 +- src/script/lua_api/l_noise.cpp | 2 +- src/script/lua_api/l_noise.h | 4 ++-- src/treegen.cpp | 8 ++++---- src/treegen.h | 8 ++++---- 13 files changed, 68 insertions(+), 54 deletions(-) diff --git a/src/cavegen.cpp b/src/cavegen.cpp index 1c25ce711..dc7355fe0 100644 --- a/src/cavegen.cpp +++ b/src/cavegen.cpp @@ -35,7 +35,7 @@ static NoiseParams nparams_caveliquids(0, 1, v3f(150.0, 150.0, 150.0), 776, 3, 0 CavesNoiseIntersection::CavesNoiseIntersection( INodeDefManager *nodedef, BiomeManager *biomemgr, v3s16 chunksize, - NoiseParams *np_cave1, NoiseParams *np_cave2, int seed, float cave_width) + NoiseParams *np_cave1, NoiseParams *np_cave2, s32 seed, float cave_width) { assert(nodedef); assert(biomemgr); @@ -130,7 +130,7 @@ void CavesNoiseIntersection::generateCaves(MMVManip *vm, CavesRandomWalk::CavesRandomWalk( INodeDefManager *ndef, GenerateNotifier *gennotify, - int seed, + s32 seed, int water_level, content_t water_source, content_t lava_source) diff --git a/src/cavegen.h b/src/cavegen.h index da0894635..2bf503d47 100644 --- a/src/cavegen.h +++ b/src/cavegen.h @@ -41,7 +41,7 @@ class CavesNoiseIntersection { public: CavesNoiseIntersection(INodeDefManager *nodedef, BiomeManager *biomemgr, v3s16 chunksize, NoiseParams *np_cave1, NoiseParams *np_cave2, - int seed, float cave_width); + s32 seed, float cave_width); ~CavesNoiseIntersection(); void generateCaves(MMVManip *vm, v3s16 nmin, v3s16 nmax, u8 *biomemap); @@ -83,7 +83,7 @@ public: s16 *heightmap; // configurable parameters - int seed; + s32 seed; int water_level; int lava_depth; NoiseParams *np_caveliquids; @@ -122,7 +122,7 @@ public: // If gennotify is NULL, generation events are not logged. CavesRandomWalk(INodeDefManager *ndef, GenerateNotifier *gennotify = NULL, - int seed = 0, + s32 seed = 0, int water_level = 1, content_t water_source = CONTENT_IGNORE, content_t lava_source = CONTENT_IGNORE); diff --git a/src/dungeongen.h b/src/dungeongen.h index f0a5e5ba8..898627483 100644 --- a/src/dungeongen.h +++ b/src/dungeongen.h @@ -39,7 +39,7 @@ int dir_to_facedir(v3s16 d); struct DungeonParams { - int seed; + s32 seed; content_t c_water; content_t c_river_water; diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 041356e3d..32f7e29eb 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -89,11 +89,25 @@ Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge) : { generating = false; id = mapgenid; - seed = (int)params->seed; water_level = params->water_level; flags = params->flags; csize = v3s16(1, 1, 1) * (params->chunksize * MAP_BLOCKSIZE); + /* + We are losing half our entropy by doing this, but it is necessary to + preserve reverse compatibility. If the top half of our current 64 bit + seeds ever starts getting used, existing worlds will break due to a + different hash outcome and no way to differentiate between versions. + + A solution could be to add a new bit to designate that the top half of + the seed value should be used, essentially a 1-bit version code, but + this would require increasing the total size of a seed to 9 bytes (yuck) + + It's probably okay if this never gets fixed. 4.2 billion possibilities + ought to be enough for anyone. + */ + seed = (s32)params->seed; + vm = NULL; ndef = emerge->ndef; biomegen = NULL; @@ -107,7 +121,7 @@ Mapgen::~Mapgen() } -u32 Mapgen::getBlockSeed(v3s16 p, int seed) +u32 Mapgen::getBlockSeed(v3s16 p, s32 seed) { return (u32)seed + p.Z * 38134234 + @@ -116,7 +130,7 @@ u32 Mapgen::getBlockSeed(v3s16 p, int seed) } -u32 Mapgen::getBlockSeed2(v3s16 p, int seed) +u32 Mapgen::getBlockSeed2(v3s16 p, s32 seed) { u32 n = 1619 * p.X + 31337 * p.Y + 52591 * p.Z + 1013 * seed; n = (n >> 13) ^ n; diff --git a/src/mapgen.h b/src/mapgen.h index 10595fafc..0342fd46e 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -150,7 +150,7 @@ struct MapgenParams { */ class Mapgen { public: - int seed; + s32 seed; int water_level; u32 flags; bool generating; @@ -171,8 +171,8 @@ public: Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge); virtual ~Mapgen(); - static u32 getBlockSeed(v3s16 p, int seed); - static u32 getBlockSeed2(v3s16 p, int seed); + static u32 getBlockSeed(v3s16 p, s32 seed); + static u32 getBlockSeed2(v3s16 p, s32 seed); s16 findGroundLevelFull(v2s16 p2d); s16 findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax); s16 findLiquidSurface(v2s16 p2d, s16 ymin, s16 ymax); diff --git a/src/mg_biome.h b/src/mg_biome.h index e78e90e5f..389b7664f 100644 --- a/src/mg_biome.h +++ b/src/mg_biome.h @@ -80,7 +80,7 @@ struct BiomeParams { virtual void writeParams(Settings *settings) const = 0; virtual ~BiomeParams() {} - int seed; + s32 seed; }; class BiomeGen { diff --git a/src/noise.cpp b/src/noise.cpp index 2ddc3926f..c57c98ccb 100644 --- a/src/noise.cpp +++ b/src/noise.cpp @@ -156,7 +156,7 @@ s32 PcgRandom::randNormalDist(s32 min, s32 max, int num_trials) /////////////////////////////////////////////////////////////////////////////// -float noise2d(int x, int y, int seed) +float noise2d(int x, int y, s32 seed) { unsigned int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y + NOISE_MAGIC_SEED * seed) & 0x7fffffff; @@ -166,7 +166,7 @@ float noise2d(int x, int y, int seed) } -float noise3d(int x, int y, int z, int seed) +float noise3d(int x, int y, int z, s32 seed) { unsigned int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y + NOISE_MAGIC_Z * z + NOISE_MAGIC_SEED * seed) & 0x7fffffff; @@ -235,7 +235,7 @@ float triLinearInterpolationNoEase( return linearInterpolation(u, v, z); } -float noise2d_gradient(float x, float y, int seed, bool eased) +float noise2d_gradient(float x, float y, s32 seed, bool eased) { // Calculate the integer coordinates int x0 = myfloor(x); @@ -256,7 +256,7 @@ float noise2d_gradient(float x, float y, int seed, bool eased) } -float noise3d_gradient(float x, float y, float z, int seed, bool eased) +float noise3d_gradient(float x, float y, float z, s32 seed, bool eased) { // Calculate the integer coordinates int x0 = myfloor(x); @@ -290,7 +290,7 @@ float noise3d_gradient(float x, float y, float z, int seed, bool eased) } -float noise2d_perlin(float x, float y, int seed, +float noise2d_perlin(float x, float y, s32 seed, int octaves, float persistence, bool eased) { float a = 0; @@ -306,7 +306,7 @@ float noise2d_perlin(float x, float y, int seed, } -float noise2d_perlin_abs(float x, float y, int seed, +float noise2d_perlin_abs(float x, float y, s32 seed, int octaves, float persistence, bool eased) { float a = 0; @@ -321,7 +321,7 @@ float noise2d_perlin_abs(float x, float y, int seed, } -float noise3d_perlin(float x, float y, float z, int seed, +float noise3d_perlin(float x, float y, float z, s32 seed, int octaves, float persistence, bool eased) { float a = 0; @@ -336,7 +336,7 @@ float noise3d_perlin(float x, float y, float z, int seed, } -float noise3d_perlin_abs(float x, float y, float z, int seed, +float noise3d_perlin_abs(float x, float y, float z, s32 seed, int octaves, float persistence, bool eased) { float a = 0; @@ -363,7 +363,7 @@ float contour(float v) ///////////////////////// [ New noise ] //////////////////////////// -float NoisePerlin2D(NoiseParams *np, float x, float y, int seed) +float NoisePerlin2D(NoiseParams *np, float x, float y, s32 seed) { float a = 0; float f = 1.0; @@ -389,7 +389,7 @@ float NoisePerlin2D(NoiseParams *np, float x, float y, int seed) } -float NoisePerlin3D(NoiseParams *np, float x, float y, float z, int seed) +float NoisePerlin3D(NoiseParams *np, float x, float y, float z, s32 seed) { float a = 0; float f = 1.0; @@ -416,7 +416,7 @@ float NoisePerlin3D(NoiseParams *np, float x, float y, float z, int seed) } -Noise::Noise(NoiseParams *np_, int seed, u32 sx, u32 sy, u32 sz) +Noise::Noise(NoiseParams *np_, s32 seed, u32 sx, u32 sy, u32 sz) { memcpy(&np, np_, sizeof(np)); this->seed = seed; @@ -543,7 +543,7 @@ void Noise::resizeNoiseBuf(bool is3d) void Noise::gradientMap2D( float x, float y, float step_x, float step_y, - int seed) + s32 seed) { float v00, v01, v10, v11, u, v, orig_u; u32 index, i, j, noisex, noisey; @@ -607,7 +607,7 @@ void Noise::gradientMap2D( void Noise::gradientMap3D( float x, float y, float z, float step_x, float step_y, float step_z, - int seed) + s32 seed) { float v000, v010, v100, v110; float v001, v011, v101, v111; diff --git a/src/noise.h b/src/noise.h index 0e4252dd4..41b93ae01 100644 --- a/src/noise.h +++ b/src/noise.h @@ -148,7 +148,7 @@ struct NoiseParams { class Noise { public: NoiseParams np; - int seed; + s32 seed; u32 sx; u32 sy; u32 sz; @@ -157,7 +157,7 @@ public: float *persist_buf; float *result; - Noise(NoiseParams *np, int seed, u32 sx, u32 sy, u32 sz=1); + Noise(NoiseParams *np, s32 seed, u32 sx, u32 sy, u32 sz=1); ~Noise(); void setSize(u32 sx, u32 sy, u32 sz=1); @@ -167,11 +167,11 @@ public: void gradientMap2D( float x, float y, float step_x, float step_y, - int seed); + s32 seed); void gradientMap3D( float x, float y, float z, float step_x, float step_y, float step_z, - int seed); + s32 seed); float *perlinMap2D(float x, float y, float *persistence_map=NULL); float *perlinMap3D(float x, float y, float z, float *persistence_map=NULL); @@ -202,11 +202,11 @@ private: }; -float NoisePerlin2D(NoiseParams *np, float x, float y, int seed); -float NoisePerlin3D(NoiseParams *np, float x, float y, float z, int seed); +float NoisePerlin2D(NoiseParams *np, float x, float y, s32 seed); +float NoisePerlin3D(NoiseParams *np, float x, float y, float z, s32 seed); inline float NoisePerlin2D_PO(NoiseParams *np, float x, float xoff, - float y, float yoff, int seed) + float y, float yoff, s32 seed) { return NoisePerlin2D(np, x + xoff * np->spread.X, @@ -215,7 +215,7 @@ inline float NoisePerlin2D_PO(NoiseParams *np, float x, float xoff, } inline float NoisePerlin3D_PO(NoiseParams *np, float x, float xoff, - float y, float yoff, float z, float zoff, int seed) + float y, float yoff, float z, float zoff, s32 seed) { return NoisePerlin3D(np, x + xoff * np->spread.X, @@ -225,22 +225,22 @@ inline float NoisePerlin3D_PO(NoiseParams *np, float x, float xoff, } // Return value: -1 ... 1 -float noise2d(int x, int y, int seed); -float noise3d(int x, int y, int z, int seed); +float noise2d(int x, int y, s32 seed); +float noise3d(int x, int y, int z, s32 seed); -float noise2d_gradient(float x, float y, int seed, bool eased=true); -float noise3d_gradient(float x, float y, float z, int seed, bool eased=false); +float noise2d_gradient(float x, float y, s32 seed, bool eased=true); +float noise3d_gradient(float x, float y, float z, s32 seed, bool eased=false); -float noise2d_perlin(float x, float y, int seed, +float noise2d_perlin(float x, float y, s32 seed, int octaves, float persistence, bool eased=true); -float noise2d_perlin_abs(float x, float y, int seed, +float noise2d_perlin_abs(float x, float y, s32 seed, int octaves, float persistence, bool eased=true); -float noise3d_perlin(float x, float y, float z, int seed, +float noise3d_perlin(float x, float y, float z, s32 seed, int octaves, float persistence, bool eased=false); -float noise3d_perlin_abs(float x, float y, float z, int seed, +float noise3d_perlin_abs(float x, float y, float z, s32 seed, int octaves, float persistence, bool eased=false); inline float easeCurve(float t) diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 8284c3fcb..ebdea09e4 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -758,7 +758,7 @@ int ModApiEnvMod::l_get_perlin_map(lua_State *L) return 0; v3s16 size = read_v3s16(L, 2); - int seed = (int)(env->getServerMap().getSeed()); + s32 seed = (s32)(env->getServerMap().getSeed()); LuaPerlinNoiseMap *n = new LuaPerlinNoiseMap(&np, seed, size); *(void **)(lua_newuserdata(L, sizeof(void *))) = n; luaL_getmetatable(L, "PerlinNoiseMap"); diff --git a/src/script/lua_api/l_noise.cpp b/src/script/lua_api/l_noise.cpp index 04dc6048f..e0039371f 100644 --- a/src/script/lua_api/l_noise.cpp +++ b/src/script/lua_api/l_noise.cpp @@ -146,7 +146,7 @@ const luaL_reg LuaPerlinNoise::methods[] = { LuaPerlinNoiseMap */ -LuaPerlinNoiseMap::LuaPerlinNoiseMap(NoiseParams *params, int seed, v3s16 size) +LuaPerlinNoiseMap::LuaPerlinNoiseMap(NoiseParams *params, s32 seed, v3s16 size) { m_is3d = size.Z > 1; np = *params; diff --git a/src/script/lua_api/l_noise.h b/src/script/lua_api/l_noise.h index 492eb7550..40bfd1315 100644 --- a/src/script/lua_api/l_noise.h +++ b/src/script/lua_api/l_noise.h @@ -79,7 +79,7 @@ class LuaPerlinNoiseMap : public ModApiBase { static int l_getMapSlice(lua_State *L); public: - LuaPerlinNoiseMap(NoiseParams *np, int seed, v3s16 size); + LuaPerlinNoiseMap(NoiseParams *np, s32 seed, v3s16 size); ~LuaPerlinNoiseMap(); @@ -111,7 +111,7 @@ private: static int l_next(lua_State *L); public: - LuaPseudoRandom(int seed) : + LuaPseudoRandom(s32 seed) : m_pseudo(seed) {} // LuaPseudoRandom(seed) diff --git a/src/treegen.cpp b/src/treegen.cpp index 208f34552..36d387c57 100644 --- a/src/treegen.cpp +++ b/src/treegen.cpp @@ -31,7 +31,7 @@ namespace treegen { void make_tree(MMVManip &vmanip, v3s16 p0, - bool is_apple_tree, INodeDefManager *ndef, int seed) + bool is_apple_tree, INodeDefManager *ndef, s32 seed) { /* NOTE: Tree-placing code is currently duplicated in the engine @@ -149,7 +149,7 @@ treegen::error make_ltree(MMVManip &vmanip, v3s16 p0, INodeDefManager *ndef, TreeDef tree_definition) { MapNode dirtnode(ndef->getId("mapgen_dirt")); - int seed; + s32 seed; if (tree_definition.explicit_seed) seed = tree_definition.seed + 14002; else @@ -649,7 +649,7 @@ v3f transposeMatrix(irr::core::matrix4 M, v3f v) } -void make_jungletree(MMVManip &vmanip, v3s16 p0, INodeDefManager *ndef, int seed) +void make_jungletree(MMVManip &vmanip, v3s16 p0, INodeDefManager *ndef, s32 seed) { /* NOTE: Tree-placing code is currently duplicated in the engine @@ -748,7 +748,7 @@ void make_jungletree(MMVManip &vmanip, v3s16 p0, INodeDefManager *ndef, int seed } -void make_pine_tree(MMVManip &vmanip, v3s16 p0, INodeDefManager *ndef, int seed) +void make_pine_tree(MMVManip &vmanip, v3s16 p0, INodeDefManager *ndef, s32 seed) { /* NOTE: Tree-placing code is currently duplicated in the engine diff --git a/src/treegen.h b/src/treegen.h index 4b0089d1e..4e6f95e67 100644 --- a/src/treegen.h +++ b/src/treegen.h @@ -54,19 +54,19 @@ namespace treegen { bool thin_branches; MapNode fruitnode; int fruit_chance; - int seed; + s32 seed; bool explicit_seed; }; // Add default tree void make_tree(MMVManip &vmanip, v3s16 p0, - bool is_apple_tree, INodeDefManager *ndef, int seed); + bool is_apple_tree, INodeDefManager *ndef, s32 seed); // Add jungle tree void make_jungletree(MMVManip &vmanip, v3s16 p0, - INodeDefManager *ndef, int seed); + INodeDefManager *ndef, s32 seed); // Add pine tree void make_pine_tree(MMVManip &vmanip, v3s16 p0, - INodeDefManager *ndef, int seed); + INodeDefManager *ndef, s32 seed); // Add L-Systems tree (used by engine) treegen::error make_ltree(MMVManip &vmanip, v3s16 p0, INodeDefManager *ndef, From 8ed467d438634ffe45806a6a6a325bb00774d651 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Sat, 4 Jun 2016 02:16:06 -0400 Subject: [PATCH 055/418] PcgRandom: Fix/improve documentation --- doc/lua_api.txt | 4 +++- src/noise.cpp | 31 ++++++++++++++++++++----------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index aa0d7e45d..f348f5103 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -2865,7 +2865,9 @@ It can be created via `PcgRandom(seed)` or `PcgRandom(seed, sequence)`. * `next()`: return next integer random number [`-2147483648`...`2147483647`] * `next(min, max)`: return next integer random number [`min`...`max`] * `rand_normal_dist(min, max, num_trials=6)`: return normally distributed random number [`min`...`max`] - * This is only a rough approximation of a normal distribution with mean=(max-min)/2 and variance=1 + * This is only a rough approximation of a normal distribution with: + * mean = (max - min) / 2, and + * variance = (((max - min + 1) ^ 2) - 1) / (12 * num_trials) * Increasing num_trials improves accuracy of the approximation ### `SecureRandom` diff --git a/src/noise.cpp b/src/noise.cpp index c57c98ccb..b918c9936 100644 --- a/src/noise.cpp +++ b/src/noise.cpp @@ -93,22 +93,31 @@ u32 PcgRandom::range(u32 bound) // If the bound is 0, we cover the whole RNG's range if (bound == 0) return next(); - /* - If the bound is not a multiple of the RNG's range, it may cause bias, - e.g. a RNG has a range from 0 to 3 and we take want a number 0 to 2. - Using rand() % 3, the number 0 would be twice as likely to appear. - With a very large RNG range, the effect becomes less prevalent but - still present. This can be solved by modifying the range of the RNG - to become a multiple of bound by dropping values above the a threshold. - In our example, threshold == 4 - 3 = 1 % 3 == 1, so reject 0, thus - making the range 3 with no bias. - This loop looks dangerous, but will always terminate due to the - RNG's property of uniformity. + /* + This is an optimization of the expression: + 0x100000000ull % bound + since 64-bit modulo operations typically much slower than 32. */ u32 threshold = -bound % bound; u32 r; + /* + If the bound is not a multiple of the RNG's range, it may cause bias, + e.g. a RNG has a range from 0 to 3 and we take want a number 0 to 2. + Using rand() % 3, the number 0 would be twice as likely to appear. + With a very large RNG range, the effect becomes less prevalent but + still present. + + This can be solved by modifying the range of the RNG to become a + multiple of bound by dropping values above the a threshold. + + In our example, threshold == 4 % 3 == 1, so reject values < 1 + (that is, 0), thus making the range == 3 with no bias. + + This loop may look dangerous, but will always terminate due to the + RNG's property of uniformity. + */ while ((r = next()) < threshold) ; From 109c7e334920f859068aeda31463f644e6b69895 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Sat, 4 Jun 2016 03:00:45 -0400 Subject: [PATCH 056/418] Biomes: Define and use biome_t for biome IDs --- src/mapgen.h | 4 +++- src/mg_biome.cpp | 4 ++-- src/mg_biome.h | 13 ++++++------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/mapgen.h b/src/mapgen.h index 0342fd46e..f673007b5 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -36,6 +36,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #define MG_LIGHT 0x10 #define MG_DECORATIONS 0x20 +typedef u8 biome_t; // copy from mg_biome.h to avoid an unnecessary include + class Settings; class MMVManip; class INodeDefManager; @@ -161,7 +163,7 @@ public: u32 blockseed; s16 *heightmap; - u8 *biomemap; + biome_t *biomemap; v3s16 csize; BiomeGen *biomegen; diff --git a/src/mg_biome.cpp b/src/mg_biome.cpp index dac0f7acc..df728af33 100644 --- a/src/mg_biome.cpp +++ b/src/mg_biome.cpp @@ -128,7 +128,7 @@ BiomeGenOriginal::BiomeGenOriginal(BiomeManager *biomemgr, heatmap = noise_heat->result; humidmap = noise_humidity->result; - biomemap = new u8[m_csize.X * m_csize.Z]; + biomemap = new biome_t[m_csize.X * m_csize.Z]; } BiomeGenOriginal::~BiomeGenOriginal() @@ -171,7 +171,7 @@ void BiomeGenOriginal::calcBiomeNoise(v3s16 pmin) } -u8 *BiomeGenOriginal::getBiomes(s16 *heightmap) +biome_t *BiomeGenOriginal::getBiomes(s16 *heightmap) { for (s32 i = 0; i != m_csize.X * m_csize.Z; i++) { Biome *biome = calcBiomeFromNoise( diff --git a/src/mg_biome.h b/src/mg_biome.h index 389b7664f..568d0b1ac 100644 --- a/src/mg_biome.h +++ b/src/mg_biome.h @@ -31,7 +31,9 @@ class BiomeManager; //// Biome //// -#define BIOME_NONE ((u8)0) +typedef u8 biome_t; + +#define BIOME_NONE ((biome_t)0) // TODO(hmmmm): Decide whether this is obsolete or will be used in the future enum BiomeType { @@ -101,7 +103,7 @@ public: // Gets all biomes in current chunk using each corresponding element of // heightmap as the y position, then stores the results by biome index in // biomemap (also returned) - virtual u8 *getBiomes(s16 *heightmap) = 0; + virtual biome_t *getBiomes(s16 *heightmap) = 0; // Gets a single biome at the specified position, which must be contained // in the region formed by m_pmin and (m_pmin + m_csize - 1). @@ -111,7 +113,7 @@ public: virtual Biome *getBiomeAtIndex(size_t index, s16 y) const = 0; // Result of calcBiomes bulk computation. - u8 *biomemap; + biome_t *biomemap; protected: BiomeManager *m_bmgr; @@ -157,7 +159,7 @@ public: Biome *calcBiomeAtPoint(v3s16 pos) const; void calcBiomeNoise(v3s16 pmin); - u8 *getBiomes(s16 *heightmap); + biome_t *getBiomes(s16 *heightmap); Biome *getBiomeAtPoint(v3s16 pos) const; Biome *getBiomeAtIndex(size_t index, s16 y) const; @@ -218,9 +220,6 @@ public: virtual void clear(); - // Looks for pos in the biome cache, and if non-existent, looks up by noise - u8 getBiomeAtPoint(v3s16 pos); - private: IGameDef *m_gamedef; From 7841f1c5098d73b7538623cf898ca1e32ac6773d Mon Sep 17 00:00:00 2001 From: MillersMan Date: Sun, 5 Jun 2016 01:30:36 +0200 Subject: [PATCH 057/418] Mapgen: Performance improvement and fixes for updateLiquid (#4065) - Adds only ~100 nodes per chunk to trans_liquid with similar processing time - Adds liquid nodes themselves instead of potential solid nodes below them - CONTENT_IGNORE nodes are interpreted as if they continue their neighborhood - This allows liquid columns to span multiple chunks without being interrupted - NOTE: Expects an one-node border in generation chunk without liquid changes --- src/mapgen.cpp | 83 ++++++++++++++++++++++++++++++++++++++++++-------- src/mapgen.h | 4 +++ 2 files changed, 75 insertions(+), 12 deletions(-) diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 32f7e29eb..b5c48a471 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -213,27 +213,86 @@ void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax) //printf("updateHeightmap: %dus\n", t.stop()); } +inline bool Mapgen::isLiquidHorizontallyFlowable(u32 vi, v3s16 em) +{ + u32 vi_neg_x = vi; + vm->m_area.add_x(em, vi_neg_x, -1); + if (vm->m_data[vi_neg_x].getContent() != CONTENT_IGNORE) { + const ContentFeatures &c_nx = ndef->get(vm->m_data[vi_neg_x]); + if (c_nx.floodable && !c_nx.isLiquid()) + return true; + } + u32 vi_pos_x = vi; + vm->m_area.add_x(em, vi_pos_x, +1); + if (vm->m_data[vi_pos_x].getContent() != CONTENT_IGNORE) { + const ContentFeatures &c_px = ndef->get(vm->m_data[vi_pos_x]); + if (c_px.floodable && !c_px.isLiquid()) + return true; + } + u32 vi_neg_z = vi; + vm->m_area.add_z(em, vi_neg_z, -1); + if (vm->m_data[vi_neg_z].getContent() != CONTENT_IGNORE) { + const ContentFeatures &c_nz = ndef->get(vm->m_data[vi_neg_z]); + if (c_nz.floodable && !c_nz.isLiquid()) + return true; + } + u32 vi_pos_z = vi; + vm->m_area.add_z(em, vi_pos_z, +1); + if (vm->m_data[vi_pos_z].getContent() != CONTENT_IGNORE) { + const ContentFeatures &c_pz = ndef->get(vm->m_data[vi_pos_z]); + if (c_pz.floodable && !c_pz.isLiquid()) + return true; + } + return false; +} void Mapgen::updateLiquid(UniqueQueue *trans_liquid, v3s16 nmin, v3s16 nmax) { - bool isliquid, wasliquid; + bool isignored, isliquid, wasignored, wasliquid, waschecked, waspushed; v3s16 em = vm->m_area.getExtent(); - for (s16 z = nmin.Z; z <= nmax.Z; z++) { - for (s16 x = nmin.X; x <= nmax.X; x++) { - wasliquid = true; + for (s16 z = nmin.Z + 1; z <= nmax.Z - 1; z++) + for (s16 x = nmin.X + 1; x <= nmax.X - 1; x++) { + wasignored = true; + wasliquid = false; + waschecked = false; + waspushed = false; - u32 i = vm->m_area.index(x, nmax.Y, z); - for (s16 y = nmax.Y; y >= nmin.Y; y--) { - isliquid = ndef->get(vm->m_data[i]).isLiquid(); + u32 vi = vm->m_area.index(x, nmax.Y, z); + for (s16 y = nmax.Y; y >= nmin.Y; y--) { + isignored = vm->m_data[vi].getContent() == CONTENT_IGNORE; + isliquid = ndef->get(vm->m_data[vi]).isLiquid(); - // there was a change between liquid and nonliquid, add to queue. - if (isliquid != wasliquid) + if (isignored || wasignored || isliquid == wasliquid) { + // Neither topmost node of liquid column nor topmost node below column + waschecked = false; + waspushed = false; + } else if (isliquid) { + // This is the topmost node in the column + bool ispushed = false; + if (isLiquidHorizontallyFlowable(vi, em)) { trans_liquid->push_back(v3s16(x, y, z)); - - wasliquid = isliquid; - vm->m_area.add_y(em, i, -1); + ispushed = true; + } + // Remember waschecked and waspushed to avoid repeated + // checks/pushes in case the column consists of only this node + waschecked = true; + waspushed = ispushed; + } else { + // This is the topmost node below a liquid column + u32 vi_above = vi; + vm->m_area.add_y(em, vi_above, 1); + if (!waspushed && (ndef->get(vm->m_data[vi]).floodable || + (!waschecked && isLiquidHorizontallyFlowable(vi_above, em)))) { + // Push back the lowest node in the column which is one + // node above this one + trans_liquid->push_back(v3s16(x, y + 1, z)); + } } + + wasliquid = isliquid; + wasignored = isignored; + vm->m_area.add_y(em, vi, -1); } } } diff --git a/src/mapgen.h b/src/mapgen.h index f673007b5..90ac84bd8 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -199,6 +199,10 @@ public: virtual int getSpawnLevelAtPoint(v2s16 p) { return 0; } private: + // isLiquidHorizontallyFlowable() is a helper function for updateLiquid() + // that checks whether there are floodable nodes without liquid beneath + // the node at index vi. + inline bool isLiquidHorizontallyFlowable(u32 vi, v3s16 em); DISABLE_CLASS_COPY(Mapgen); }; From d24f3841740b471eff384c8bd6e8bbfdfd03a3e2 Mon Sep 17 00:00:00 2001 From: paramat Date: Fri, 3 Jun 2016 12:58:50 +0100 Subject: [PATCH 058/418] Biome API: Add per-biome riverbed material and depth Mgvalleys: Remove riverbed sand placement from base terrain generation Riverbed material placement moved to MapgenBasic::generateBiomes() Document fields and add note that the biome API is still unstable --- doc/lua_api.txt | 6 ++++++ src/mapgen.cpp | 24 ++++++++++++++++++------ src/mapgen_valleys.cpp | 13 ++----------- src/mg_biome.cpp | 3 +++ src/mg_biome.h | 2 ++ src/script/lua_api/l_mapgen.cpp | 2 ++ 6 files changed, 33 insertions(+), 17 deletions(-) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index f348f5103..30f4d87df 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -3723,6 +3723,9 @@ Definition tables ### Biome definition (`register_biome`) +**Note** +The biome API is still in an experimental phase and subject to change. + { name = "tundra", node_dust = "default:snow", @@ -3742,6 +3745,9 @@ Definition tables -- ^ Node that replaces all seawater nodes not in the defined surface layer. node_river_water = "default:ice", -- ^ Node that replaces river water in mapgens that use default:river_water. + node_riverbed = "default:gravel", + depth_riverbed = 2, + -- ^ Node placed under river water and thickness of this layer. y_min = 1, y_max = 31000, -- ^ Lower and upper limits for biome. diff --git a/src/mapgen.cpp b/src/mapgen.cpp index b5c48a471..66892a574 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -536,13 +536,15 @@ MgStoneType MapgenBasic::generateBiomes() u16 depth_top = 0; u16 base_filler = 0; u16 depth_water_top = 0; + u16 depth_riverbed = 0; u32 vi = vm->m_area.index(x, node_max.Y, z); // Check node at base of mapchunk above, either a node of a previously // generated mapchunk or if not, a node of overgenerated base terrain. content_t c_above = vm->m_data[vi + em.X].getContent(); bool air_above = c_above == CONTENT_AIR; - bool water_above = (c_above == c_water_source || c_above == c_river_water_source); + bool river_water_above = c_above == c_river_water_source; + bool water_above = c_above == c_water_source || river_water_above; // If there is air or water above enable top/filler placement, otherwise force // nplaced to stone level by setting a number exceeding any possible filler depth. @@ -564,10 +566,11 @@ MgStoneType MapgenBasic::generateBiomes() biome = biomegen->getBiomeAtIndex(index, y); depth_top = biome->depth_top; - base_filler = MYMAX(depth_top - + biome->depth_filler - + noise_filler_depth->result[index], 0.f); + base_filler = MYMAX(depth_top + + biome->depth_filler + + noise_filler_depth->result[index], 0.f); depth_water_top = biome->depth_water_top; + depth_riverbed = biome->depth_riverbed; // Detect stone type for dungeons during every biome calculation. // This is more efficient than detecting per-node and will not @@ -590,7 +593,15 @@ MgStoneType MapgenBasic::generateBiomes() || c_below == c_river_water_source) nplaced = U16_MAX; - if (nplaced < depth_top) { + if (river_water_above) { + if (nplaced < depth_riverbed) { + vm->m_data[vi] = MapNode(biome->c_riverbed); + nplaced++; + } else { + nplaced = U16_MAX; // Disable top/filler placement + river_water_above = false; + } + } else if (nplaced < depth_top) { vm->m_data[vi] = MapNode(biome->c_top); nplaced++; } else if (nplaced < base_filler) { @@ -610,9 +621,10 @@ MgStoneType MapgenBasic::generateBiomes() water_above = true; } else if (c == c_river_water_source) { vm->m_data[vi] = MapNode(biome->c_river_water); - nplaced = depth_top; // Enable filler placement for next surface + nplaced = 0; // Enable riverbed placement for next surface air_above = false; water_above = true; + river_water_above = true; } else if (c == CONTENT_AIR) { nplaced = 0; // Enable top/filler placement for next surface air_above = true; diff --git a/src/mapgen_valleys.cpp b/src/mapgen_valleys.cpp index 2cd733d36..6581b792f 100644 --- a/src/mapgen_valleys.cpp +++ b/src/mapgen_valleys.cpp @@ -114,11 +114,6 @@ MapgenValleys::MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager * // Resolve content to be used c_lava_source = ndef->getId("mapgen_lava_source"); - c_sand = ndef->getId("mapgen_sand"); - - // Fall back to more basic content if not defined - if (c_sand == CONTENT_IGNORE) - c_sand = c_stone; } @@ -493,7 +488,6 @@ int MapgenValleys::generateTerrain() MapNode n_air(CONTENT_AIR); MapNode n_river_water(c_river_water_source); - MapNode n_sand(c_sand); MapNode n_stone(c_stone); MapNode n_water(c_water_source); @@ -537,10 +531,7 @@ int MapgenValleys::generateTerrain() float surface_delta = (float)y - surface_y; bool river = y + 1 < river_y; - if (fabs(surface_delta) <= 0.5f && y > water_level && river) { - // river bottom - vm->m_data[index_data] = n_sand; - } else if (slope * fill > surface_delta) { + if (slope * fill > surface_delta) { // ground vm->m_data[index_data] = n_stone; if (y > heightmap[index_2d]) @@ -553,7 +544,7 @@ int MapgenValleys::generateTerrain() } else if (river) { // river vm->m_data[index_data] = n_river_water; - } else { + } else { // air vm->m_data[index_data] = n_air; } } diff --git a/src/mg_biome.cpp b/src/mg_biome.cpp index df728af33..78034bf6c 100644 --- a/src/mg_biome.cpp +++ b/src/mg_biome.cpp @@ -46,6 +46,7 @@ BiomeManager::BiomeManager(IGameDef *gamedef) : b->depth_top = 0; b->depth_filler = -MAX_MAP_GENERATION_LIMIT; b->depth_water_top = 0; + b->depth_riverbed = 0; b->y_min = -MAX_MAP_GENERATION_LIMIT; b->y_max = MAX_MAP_GENERATION_LIMIT; b->heat_point = 0.0; @@ -57,6 +58,7 @@ BiomeManager::BiomeManager(IGameDef *gamedef) : b->m_nodenames.push_back("mapgen_water_source"); b->m_nodenames.push_back("mapgen_water_source"); b->m_nodenames.push_back("mapgen_river_water_source"); + b->m_nodenames.push_back("mapgen_stone"); b->m_nodenames.push_back("ignore"); m_ndef->pendNodeResolve(b); @@ -237,5 +239,6 @@ void Biome::resolveNodeNames() getIdFromNrBacklog(&c_water_top, "mapgen_water_source", CONTENT_AIR); getIdFromNrBacklog(&c_water, "mapgen_water_source", CONTENT_AIR); getIdFromNrBacklog(&c_river_water, "mapgen_river_water_source", CONTENT_AIR); + getIdFromNrBacklog(&c_riverbed, "mapgen_stone", CONTENT_AIR); getIdFromNrBacklog(&c_dust, "ignore", CONTENT_IGNORE); } diff --git a/src/mg_biome.h b/src/mg_biome.h index 568d0b1ac..eb0a18a2f 100644 --- a/src/mg_biome.h +++ b/src/mg_biome.h @@ -54,11 +54,13 @@ public: content_t c_water_top; content_t c_water; content_t c_river_water; + content_t c_riverbed; content_t c_dust; s16 depth_top; s16 depth_filler; s16 depth_water_top; + s16 depth_riverbed; s16 y_min; s16 y_max; diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 6baf217af..dc188f8a4 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -378,6 +378,7 @@ Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef) b->depth_top = getintfield_default(L, index, "depth_top", 0); b->depth_filler = getintfield_default(L, index, "depth_filler", -31000); b->depth_water_top = getintfield_default(L, index, "depth_water_top", 0); + b->depth_riverbed = getintfield_default(L, index, "depth_riverbed", 0); b->y_min = getintfield_default(L, index, "y_min", -31000); b->y_max = getintfield_default(L, index, "y_max", 31000); b->heat_point = getfloatfield_default(L, index, "heat_point", 0.f); @@ -391,6 +392,7 @@ Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef) nn.push_back(getstringfield_default(L, index, "node_water_top", "")); nn.push_back(getstringfield_default(L, index, "node_water", "")); nn.push_back(getstringfield_default(L, index, "node_river_water", "")); + nn.push_back(getstringfield_default(L, index, "node_riverbed", "")); nn.push_back(getstringfield_default(L, index, "node_dust", "")); ndef->pendNodeResolve(b); From d4457ef42061435a1ccf6f5e056f37bac9b30c02 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Sat, 4 Jun 2016 20:24:07 -0400 Subject: [PATCH 059/418] Mapgen: Optimize biomemap creation by combining with generateBiomes --- src/mapgen.cpp | 20 +++++++++++++++++--- src/mapgen_flat.cpp | 1 - src/mapgen_fractal.cpp | 1 - src/mapgen_v5.cpp | 1 - src/mapgen_v7.cpp | 1 - src/mapgen_valleys.cpp | 5 +---- 6 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 66892a574..576fb219a 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -524,6 +524,10 @@ MapgenBasic::~MapgenBasic() MgStoneType MapgenBasic::generateBiomes() { + // can't generate biomes without a biome generator! + assert(biomegen); + assert(biomemap); + v3s16 em = vm->m_area.getExtent(); u32 index = 0; MgStoneType stone_type = MGSTONE_STONE; @@ -546,6 +550,8 @@ MgStoneType MapgenBasic::generateBiomes() bool river_water_above = c_above == c_river_water_source; bool water_above = c_above == c_water_source || river_water_above; + biomemap[index] = BIOME_NONE; + // If there is air or water above enable top/filler placement, otherwise force // nplaced to stone level by setting a number exceeding any possible filler depth. u16 nplaced = (air_above || water_above) ? 0 : U16_MAX; @@ -560,11 +566,19 @@ MgStoneType MapgenBasic::generateBiomes() // 1. At the surface of stone below air or water. // 2. At the surface of water below air. // 3. When stone or water is detected but biome has not yet been calculated. - if ((c == c_stone && (air_above || water_above || !biome)) - || ((c == c_water_source || c == c_river_water_source) - && (air_above || !biome))) { + bool is_stone_surface = (c == c_stone) && + (air_above || water_above || !biome); + + bool is_water_surface = + (c == c_water_source || c == c_river_water_source) && + (air_above || !biome); + + if (is_stone_surface || is_water_surface) { biome = biomegen->getBiomeAtIndex(index, y); + if (biomemap[index] == BIOME_NONE && is_stone_surface) + biomemap[index] = biome->index; + depth_top = biome->depth_top; base_filler = MYMAX(depth_top + biome->depth_filler + diff --git a/src/mapgen_flat.cpp b/src/mapgen_flat.cpp index 724f06825..7cc6aad5c 100644 --- a/src/mapgen_flat.cpp +++ b/src/mapgen_flat.cpp @@ -192,7 +192,6 @@ void MapgenFlat::makeChunk(BlockMakeData *data) // Init biome generator, place biome-specific nodes, and build biomemap biomegen->calcBiomeNoise(node_min); - biomegen->getBiomes(heightmap); MgStoneType stone_type = generateBiomes(); if (flags & MG_CAVES) diff --git a/src/mapgen_fractal.cpp b/src/mapgen_fractal.cpp index b613bf358..c47a7bfdc 100644 --- a/src/mapgen_fractal.cpp +++ b/src/mapgen_fractal.cpp @@ -208,7 +208,6 @@ void MapgenFractal::makeChunk(BlockMakeData *data) // Init biome generator, place biome-specific nodes, and build biomemap biomegen->calcBiomeNoise(node_min); - biomegen->getBiomes(heightmap); MgStoneType stone_type = generateBiomes(); if (flags & MG_CAVES) diff --git a/src/mapgen_v5.cpp b/src/mapgen_v5.cpp index 3e10ec2a5..74d5e1ee3 100644 --- a/src/mapgen_v5.cpp +++ b/src/mapgen_v5.cpp @@ -190,7 +190,6 @@ void MapgenV5::makeChunk(BlockMakeData *data) // Init biome generator, place biome-specific nodes, and build biomemap biomegen->calcBiomeNoise(node_min); - biomegen->getBiomes(heightmap); MgStoneType stone_type = generateBiomes(); // Generate caves diff --git a/src/mapgen_v7.cpp b/src/mapgen_v7.cpp index 7327facae..35dcdcd94 100644 --- a/src/mapgen_v7.cpp +++ b/src/mapgen_v7.cpp @@ -219,7 +219,6 @@ void MapgenV7::makeChunk(BlockMakeData *data) // Init biome generator, place biome-specific nodes, and build biomemap biomegen->calcBiomeNoise(node_min); - biomegen->getBiomes(heightmap); MgStoneType stone_type = generateBiomes(); if (flags & MG_CAVES) diff --git a/src/mapgen_valleys.cpp b/src/mapgen_valleys.cpp index 6581b792f..bbf20719d 100644 --- a/src/mapgen_valleys.cpp +++ b/src/mapgen_valleys.cpp @@ -250,10 +250,7 @@ void MapgenValleys::makeChunk(BlockMakeData *data) // Generate base terrain with initial heightmaps s16 stone_surface_max_y = generateTerrain(); - // Build biomemap - m_bgen->getBiomes(heightmap); - - // Place biome-specific nodes + // Place biome-specific nodes and build biomemap MgStoneType stone_type = generateBiomes(); // Cave creation. From c7297e552be9c265eba77e58dbd8f69be23b4131 Mon Sep 17 00:00:00 2001 From: paramat Date: Tue, 7 Jun 2016 03:23:23 +0100 Subject: [PATCH 060/418] Lua_api.txt: Split long lines. Capitalise 'Biome API'. Minor edits --- doc/lua_api.txt | 129 ++++++++++++++++++++++++++++-------------------- 1 file changed, 76 insertions(+), 53 deletions(-) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 30f4d87df..ea47d0044 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -65,15 +65,19 @@ e.g. The game directory can contain the file minetest.conf, which will be used to set default settings when running the particular game. It can also contain a settingtypes.txt in the same format as the one in builtin. -This settingtypes.txt will be parsed by the menu and the settings will be displayed in the "Games" category in the settings tab. +This settingtypes.txt will be parsed by the menu and the settings will be displayed +in the "Games" category in the settings tab. ### Menu images -Games can provide custom main menu images. They are put inside a `menu` directory inside the game directory. +Games can provide custom main menu images. They are put inside a `menu` directory +inside the game directory. -The images are named `$identifier.png`, where `$identifier` is one of `overlay,background,footer,header`. -If you want to specify multiple images for one identifier, add additional images named like `$identifier.$n.png`, with an ascending number $n starting with 1, -and a random image will be chosen from the provided ones. +The images are named `$identifier.png`, where `$identifier` is +one of `overlay,background,footer,header`. +If you want to specify multiple images for one identifier, add additional images named +like `$identifier.$n.png`, with an ascending number $n starting with 1, and a random +image will be chosen from the provided ones. Mod load path @@ -243,7 +247,8 @@ Example: default_dirt.png^default_grass_side.png `default_grass_side.png` is overlayed over `default_dirt.png`. -The texture with the lower resolution will be automatically upscaled to the higher resolution texture. +The texture with the lower resolution will be automatically upscaled to +the higher resolution texture. ### Texture grouping Textures can be grouped together by enclosing them in `(` and `)`. @@ -468,8 +473,8 @@ the global `minetest.registered_*` tables. * added to `minetest.registered_schematic` with the key of `schematic.name` * if `schematic.name` is nil, the key is the returned ID * if the schematic is loaded from a file, schematic.name is set to the filename - * if the function is called when loading the mod, and schematic.name is a relative path, - * then the current mod path will be prepended to the schematic filename + * if the function is called when loading the mod, and schematic.name is a relative + path, then the current mod path will be prepended to the schematic filename * `minetest.clear_registered_biomes()` * clears all biomes currently registered @@ -1416,13 +1421,13 @@ examples. #### `listring[;]` * Allows to create a ring of inventory lists * Shift-clicking on items in one element of the ring -* will send them to the next inventory list inside the ring + will send them to the next inventory list inside the ring * The first occurrence of an element inside the ring will -* determine the inventory where items will be sent to + determine the inventory where items will be sent to #### `listring[]` * Shorthand for doing `listring[;]` -* for the last two inventory lists added by list[...] + for the last two inventory lists added by list[...] #### `listcolors[;]` * Sets background color of slots as `ColorString` @@ -1475,7 +1480,7 @@ examples. * Textual password style field; will be sent to server when a button is clicked * `x` and `y` position the field relative to the top left of the menu * `w` and `h` are the size of the field -* fields are a set height, but will be vertically centred on `h` +* Fields are a set height, but will be vertically centred on `h` * Position and size units are inventory slots * `name` is the name of the field as returned in fields to `on_receive_fields` * `label`, if not blank, will be text printed on the top left above the field @@ -1484,7 +1489,7 @@ examples. * Textual field; will be sent to server when a button is clicked * `x` and `y` position the field relative to the top left of the menu * `w` and `h` are the size of the field -* fields are a set height, but will be vertically centred on `h` +* Fields are a set height, but will be vertically centred on `h` * Position and size units are inventory slots * `name` is the name of the field as returned in fields to `on_receive_fields` * `label`, if not blank, will be text printed on the top left above the field @@ -1494,13 +1499,13 @@ examples. * **Note**: no extra text or more than a single variable is supported ATM. #### `field[;