├── CMakeLists.txt ├── LICENSE ├── README.md ├── include ├── Rect.hpp ├── Rect.inl ├── Vector2.hpp ├── Vector2.inl ├── Vector3.hpp ├── Vector3.inl ├── Voronoi │ ├── .gitignore │ ├── CMakeCache.txt │ ├── CMakeLists.txt │ ├── LICENSE │ ├── README.md │ ├── bin │ │ └── .gitignore │ ├── cmake_install.cmake │ ├── examples │ │ └── SFML_Example.cpp │ ├── include │ │ ├── Cell.h │ │ ├── Diagram.h │ │ ├── Edge.h │ │ └── VoronoiDiagramGenerator.h │ ├── libvoronoi.a │ ├── sfvoronoi_example │ └── src │ │ ├── BeachLine.cpp │ │ ├── BeachLine.h │ │ ├── Cell.cpp │ │ ├── CircleEventQueue.cpp │ │ ├── CircleEventQueue.h │ │ ├── Diagram.cpp │ │ ├── Edge.cpp │ │ ├── Epsilon.h │ │ ├── MemoryPool │ │ ├── C-11 │ │ │ ├── MemoryPool.h │ │ │ └── MemoryPool.tcc │ │ ├── C-98 │ │ │ ├── MemoryPool.h │ │ │ └── MemoryPool.tcc │ │ ├── README.md │ │ ├── StackAlloc.h │ │ └── test.cpp │ │ ├── RBTree.h │ │ └── VoronoiDiagramGenerator.cpp ├── libnoise.dll ├── libnoise.lib ├── mapgen │ ├── Biom.hpp │ ├── City.hpp │ ├── Economy.hpp │ ├── Location.hpp │ ├── Map.hpp │ ├── MapGenerator.hpp │ ├── Package.hpp │ ├── Region.hpp │ ├── Report.hpp │ ├── River.hpp │ ├── Road.hpp │ ├── Simulator.hpp │ ├── State.hpp │ ├── WeatherManager.hpp │ ├── names.hpp │ └── utils.hpp ├── micropather.cpp ├── micropather.h ├── noise │ ├── basictypes.h │ ├── exception.h │ ├── interp.h │ ├── latlon.h │ ├── libnoise.dll │ ├── libnoise.lib │ ├── mathconsts.h │ ├── misc.h │ ├── model │ │ ├── cylinder.h │ │ ├── line.h │ │ ├── model.h │ │ ├── plane.h │ │ └── sphere.h │ ├── module │ │ ├── abs.h │ │ ├── add.h │ │ ├── billow.h │ │ ├── blend.h │ │ ├── cache.h │ │ ├── checkerboard.h │ │ ├── clamp.h │ │ ├── const.h │ │ ├── curve.h │ │ ├── cylinders.h │ │ ├── displace.h │ │ ├── exponent.h │ │ ├── invert.h │ │ ├── max.h │ │ ├── min.h │ │ ├── module.h │ │ ├── modulebase.h │ │ ├── multiply.h │ │ ├── perlin.h │ │ ├── power.h │ │ ├── ridgedmulti.h │ │ ├── rotatepoint.h │ │ ├── scalebias.h │ │ ├── scalepoint.h │ │ ├── select.h │ │ ├── spheres.h │ │ ├── terrace.h │ │ ├── translatepoint.h │ │ ├── turbulence.h │ │ └── voronoi.h │ ├── noise.h │ ├── noisegen.h │ ├── noiseutils.h │ └── vectortable.h ├── noiseutils.cpp └── rang.hpp └── src ├── Biom.cpp ├── City.cpp ├── Economy.cpp ├── GeneratorFacade.cpp ├── GeneratorFacade.cpp.meta ├── GeneratorFacade.h ├── GeneratorFacade.h.meta ├── Location.cpp ├── Map.cpp ├── MapGenerator.cpp ├── Package.cpp ├── Region.cpp ├── Report.cpp ├── Road.cpp ├── Simulator.cpp ├── State.cpp ├── WeatherManager.cpp ├── json.hpp ├── mask.frag ├── names.cpp └── utils.cpp /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8.6) 2 | project (generator) 3 | set (CMAKE_BUILD_TYPE Debug) 4 | 5 | #include *.h files under include folder and 6 | #the project's output folder e.g. Debug 7 | add_definitions(-D_USE_MATH_DEFINES) 8 | include_directories (include ${PROJECT_SOURCE_DIR} 9 | "${PROJECT_SOURCE_DIR}/include" 10 | "${PROJECT_SOURCE_DIR}/include/Voronoi/include" 11 | ) 12 | file(GLOB VLIB_SOURCE "${PROJECT_SOURCE_DIR}/include/Voronoi/src/*.cpp" "${PROJECT_SOURCE_DIR}/include/Voronoi/include/*.h") 13 | add_library(voronoi ${VLIB_SOURCE}) 14 | 15 | #compile all *.cpp source files under src folder 16 | file (GLOB SOURCES "src/*.cpp" "include/*.cpp") 17 | 18 | #output library as generator.* 19 | 20 | #output library export file *.lib and 21 | #output macro definitions include file 22 | include (GenerateExportHeader) 23 | add_library(generator SHARED ${SOURCES}) 24 | 25 | target_link_libraries(generator voronoi "${PROJECT_SOURCE_DIR}/include/libnoise.lib") 26 | GENERATE_EXPORT_HEADER (generator 27 | BASE_NAME generator 28 | EXPORT_MACRO_NAME generator_EXPORT 29 | EXPORT_FILE_NAME generator_Export.h 30 | STATIC_DEFINE generator_BUILT_AS_STATIC 31 | ) 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Alexey "Averrin" Nabrodov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mapgen for Unity 2 | 3 | This is gui-less version of my c++ project [mapgen](https://github.com/averrin/mapgen). In this repo i will develop better landscape generation and interface for c# side invoсation. 4 | 5 | [DEMO](https://streamable.com/n721g) 6 | 7 | # Usage 8 | 9 | You can use this lib as [Native plugin](https://docs.unity3d.com/Manual/NativePlugins.html). Signatures for calls are [here](https://github.com/averrin/mapgen-unity/blob/master/src/GeneratorFacade.h) 10 | -------------------------------------------------------------------------------- /include/Rect.inl: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | // 3 | // SFML - Simple and Fast Multimedia Library 4 | // Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) 5 | // 6 | // This software is provided 'as-is', without any express or implied warranty. 7 | // In no event will the authors be held liable for any damages arising from the use of this software. 8 | // 9 | // Permission is granted to anyone to use this software for any purpose, 10 | // including commercial applications, and to alter it and redistribute it freely, 11 | // subject to the following restrictions: 12 | // 13 | // 1. The origin of this software must not be misrepresented; 14 | // you must not claim that you wrote the original software. 15 | // If you use this software in a product, an acknowledgment 16 | // in the product documentation would be appreciated but is not required. 17 | // 18 | // 2. Altered source versions must be plainly marked as such, 19 | // and must not be misrepresented as being the original software. 20 | // 21 | // 3. This notice may not be removed or altered from any source distribution. 22 | // 23 | //////////////////////////////////////////////////////////// 24 | 25 | 26 | //////////////////////////////////////////////////////////// 27 | template 28 | Rect::Rect() : 29 | left (0), 30 | top (0), 31 | width (0), 32 | height(0) 33 | { 34 | 35 | } 36 | 37 | 38 | //////////////////////////////////////////////////////////// 39 | template 40 | Rect::Rect(T rectLeft, T rectTop, T rectWidth, T rectHeight) : 41 | left (rectLeft), 42 | top (rectTop), 43 | width (rectWidth), 44 | height(rectHeight) 45 | { 46 | 47 | } 48 | 49 | 50 | //////////////////////////////////////////////////////////// 51 | template 52 | Rect::Rect(const Vector2& position, const Vector2& size) : 53 | left (position.x), 54 | top (position.y), 55 | width (size.x), 56 | height(size.y) 57 | { 58 | 59 | } 60 | 61 | 62 | //////////////////////////////////////////////////////////// 63 | template 64 | template 65 | Rect::Rect(const Rect& rectangle) : 66 | left (static_cast(rectangle.left)), 67 | top (static_cast(rectangle.top)), 68 | width (static_cast(rectangle.width)), 69 | height(static_cast(rectangle.height)) 70 | { 71 | } 72 | 73 | 74 | //////////////////////////////////////////////////////////// 75 | template 76 | bool Rect::contains(T x, T y) const 77 | { 78 | // Rectangles with negative dimensions are allowed, so we must handle them correctly 79 | 80 | // Compute the real min and max of the rectangle on both axes 81 | T minX = std::min(left, static_cast(left + width)); 82 | T maxX = std::max(left, static_cast(left + width)); 83 | T minY = std::min(top, static_cast(top + height)); 84 | T maxY = std::max(top, static_cast(top + height)); 85 | 86 | return (x >= minX) && (x < maxX) && (y >= minY) && (y < maxY); 87 | } 88 | 89 | 90 | //////////////////////////////////////////////////////////// 91 | template 92 | bool Rect::contains(const Vector2& point) const 93 | { 94 | return contains(point.x, point.y); 95 | } 96 | 97 | 98 | //////////////////////////////////////////////////////////// 99 | template 100 | bool Rect::intersects(const Rect& rectangle) const 101 | { 102 | Rect intersection; 103 | return intersects(rectangle, intersection); 104 | } 105 | 106 | 107 | //////////////////////////////////////////////////////////// 108 | template 109 | bool Rect::intersects(const Rect& rectangle, Rect& intersection) const 110 | { 111 | // Rectangles with negative dimensions are allowed, so we must handle them correctly 112 | 113 | // Compute the min and max of the first rectangle on both axes 114 | T r1MinX = std::min(left, static_cast(left + width)); 115 | T r1MaxX = std::max(left, static_cast(left + width)); 116 | T r1MinY = std::min(top, static_cast(top + height)); 117 | T r1MaxY = std::max(top, static_cast(top + height)); 118 | 119 | // Compute the min and max of the second rectangle on both axes 120 | T r2MinX = std::min(rectangle.left, static_cast(rectangle.left + rectangle.width)); 121 | T r2MaxX = std::max(rectangle.left, static_cast(rectangle.left + rectangle.width)); 122 | T r2MinY = std::min(rectangle.top, static_cast(rectangle.top + rectangle.height)); 123 | T r2MaxY = std::max(rectangle.top, static_cast(rectangle.top + rectangle.height)); 124 | 125 | // Compute the intersection boundaries 126 | T interLeft = std::max(r1MinX, r2MinX); 127 | T interTop = std::max(r1MinY, r2MinY); 128 | T interRight = std::min(r1MaxX, r2MaxX); 129 | T interBottom = std::min(r1MaxY, r2MaxY); 130 | 131 | // If the intersection is valid (positive non zero area), then there is an intersection 132 | if ((interLeft < interRight) && (interTop < interBottom)) 133 | { 134 | intersection = Rect(interLeft, interTop, interRight - interLeft, interBottom - interTop); 135 | return true; 136 | } 137 | else 138 | { 139 | intersection = Rect(0, 0, 0, 0); 140 | return false; 141 | } 142 | } 143 | 144 | 145 | //////////////////////////////////////////////////////////// 146 | template 147 | inline bool operator ==(const Rect& left, const Rect& right) 148 | { 149 | return (left.left == right.left) && (left.width == right.width) && 150 | (left.top == right.top) && (left.height == right.height); 151 | } 152 | 153 | 154 | //////////////////////////////////////////////////////////// 155 | template 156 | inline bool operator !=(const Rect& left, const Rect& right) 157 | { 158 | return !(left == right); 159 | } 160 | -------------------------------------------------------------------------------- /include/Vector2.inl: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | // 3 | // SFML - Simple and Fast Multimedia Library 4 | // Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) 5 | // 6 | // This software is provided 'as-is', without any express or implied warranty. 7 | // In no event will the authors be held liable for any damages arising from the use of this software. 8 | // 9 | // Permission is granted to anyone to use this software for any purpose, 10 | // including commercial applications, and to alter it and redistribute it freely, 11 | // subject to the following restrictions: 12 | // 13 | // 1. The origin of this software must not be misrepresented; 14 | // you must not claim that you wrote the original software. 15 | // If you use this software in a product, an acknowledgment 16 | // in the product documentation would be appreciated but is not required. 17 | // 18 | // 2. Altered source versions must be plainly marked as such, 19 | // and must not be misrepresented as being the original software. 20 | // 21 | // 3. This notice may not be removed or altered from any source distribution. 22 | // 23 | //////////////////////////////////////////////////////////// 24 | 25 | 26 | //////////////////////////////////////////////////////////// 27 | template 28 | inline Vector2::Vector2() : 29 | x(0), 30 | y(0) 31 | { 32 | 33 | } 34 | 35 | 36 | //////////////////////////////////////////////////////////// 37 | template 38 | inline Vector2::Vector2(T X, T Y) : 39 | x(X), 40 | y(Y) 41 | { 42 | 43 | } 44 | 45 | 46 | //////////////////////////////////////////////////////////// 47 | template 48 | template 49 | inline Vector2::Vector2(const Vector2& vector) : 50 | x(static_cast(vector.x)), 51 | y(static_cast(vector.y)) 52 | { 53 | } 54 | 55 | 56 | //////////////////////////////////////////////////////////// 57 | template 58 | inline Vector2 operator -(const Vector2& right) 59 | { 60 | return Vector2(-right.x, -right.y); 61 | } 62 | 63 | 64 | //////////////////////////////////////////////////////////// 65 | template 66 | inline Vector2& operator +=(Vector2& left, const Vector2& right) 67 | { 68 | left.x += right.x; 69 | left.y += right.y; 70 | 71 | return left; 72 | } 73 | 74 | 75 | //////////////////////////////////////////////////////////// 76 | template 77 | inline Vector2& operator -=(Vector2& left, const Vector2& right) 78 | { 79 | left.x -= right.x; 80 | left.y -= right.y; 81 | 82 | return left; 83 | } 84 | 85 | 86 | //////////////////////////////////////////////////////////// 87 | template 88 | inline Vector2 operator +(const Vector2& left, const Vector2& right) 89 | { 90 | return Vector2(left.x + right.x, left.y + right.y); 91 | } 92 | 93 | 94 | //////////////////////////////////////////////////////////// 95 | template 96 | inline Vector2 operator -(const Vector2& left, const Vector2& right) 97 | { 98 | return Vector2(left.x - right.x, left.y - right.y); 99 | } 100 | 101 | 102 | //////////////////////////////////////////////////////////// 103 | template 104 | inline Vector2 operator *(const Vector2& left, T right) 105 | { 106 | return Vector2(left.x * right, left.y * right); 107 | } 108 | 109 | 110 | //////////////////////////////////////////////////////////// 111 | template 112 | inline Vector2 operator *(T left, const Vector2& right) 113 | { 114 | return Vector2(right.x * left, right.y * left); 115 | } 116 | 117 | 118 | //////////////////////////////////////////////////////////// 119 | template 120 | inline Vector2& operator *=(Vector2& left, T right) 121 | { 122 | left.x *= right; 123 | left.y *= right; 124 | 125 | return left; 126 | } 127 | 128 | 129 | //////////////////////////////////////////////////////////// 130 | template 131 | inline Vector2 operator /(const Vector2& left, T right) 132 | { 133 | return Vector2(left.x / right, left.y / right); 134 | } 135 | 136 | 137 | //////////////////////////////////////////////////////////// 138 | template 139 | inline Vector2& operator /=(Vector2& left, T right) 140 | { 141 | left.x /= right; 142 | left.y /= right; 143 | 144 | return left; 145 | } 146 | 147 | 148 | //////////////////////////////////////////////////////////// 149 | template 150 | inline bool operator ==(const Vector2& left, const Vector2& right) 151 | { 152 | return (left.x == right.x) && (left.y == right.y); 153 | } 154 | 155 | 156 | //////////////////////////////////////////////////////////// 157 | template 158 | inline bool operator !=(const Vector2& left, const Vector2& right) 159 | { 160 | return (left.x != right.x) || (left.y != right.y); 161 | } 162 | -------------------------------------------------------------------------------- /include/Vector3.inl: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | // 3 | // SFML - Simple and Fast Multimedia Library 4 | // Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) 5 | // 6 | // This software is provided 'as-is', without any express or implied warranty. 7 | // In no event will the authors be held liable for any damages arising from the use of this software. 8 | // 9 | // Permission is granted to anyone to use this software for any purpose, 10 | // including commercial applications, and to alter it and redistribute it freely, 11 | // subject to the following restrictions: 12 | // 13 | // 1. The origin of this software must not be misrepresented; 14 | // you must not claim that you wrote the original software. 15 | // If you use this software in a product, an acknowledgment 16 | // in the product documentation would be appreciated but is not required. 17 | // 18 | // 2. Altered source versions must be plainly marked as such, 19 | // and must not be misrepresented as being the original software. 20 | // 21 | // 3. This notice may not be removed or altered from any source distribution. 22 | // 23 | //////////////////////////////////////////////////////////// 24 | 25 | 26 | //////////////////////////////////////////////////////////// 27 | template 28 | inline Vector3::Vector3() : 29 | x(0), 30 | y(0), 31 | z(0) 32 | { 33 | 34 | } 35 | 36 | 37 | //////////////////////////////////////////////////////////// 38 | template 39 | inline Vector3::Vector3(T X, T Y, T Z) : 40 | x(X), 41 | y(Y), 42 | z(Z) 43 | { 44 | 45 | } 46 | 47 | 48 | //////////////////////////////////////////////////////////// 49 | template 50 | template 51 | inline Vector3::Vector3(const Vector3& vector) : 52 | x(static_cast(vector.x)), 53 | y(static_cast(vector.y)), 54 | z(static_cast(vector.z)) 55 | { 56 | } 57 | 58 | 59 | //////////////////////////////////////////////////////////// 60 | template 61 | inline Vector3 operator -(const Vector3& left) 62 | { 63 | return Vector3(-left.x, -left.y, -left.z); 64 | } 65 | 66 | 67 | //////////////////////////////////////////////////////////// 68 | template 69 | inline Vector3& operator +=(Vector3& left, const Vector3& right) 70 | { 71 | left.x += right.x; 72 | left.y += right.y; 73 | left.z += right.z; 74 | 75 | return left; 76 | } 77 | 78 | 79 | //////////////////////////////////////////////////////////// 80 | template 81 | inline Vector3& operator -=(Vector3& left, const Vector3& right) 82 | { 83 | left.x -= right.x; 84 | left.y -= right.y; 85 | left.z -= right.z; 86 | 87 | return left; 88 | } 89 | 90 | 91 | //////////////////////////////////////////////////////////// 92 | template 93 | inline Vector3 operator +(const Vector3& left, const Vector3& right) 94 | { 95 | return Vector3(left.x + right.x, left.y + right.y, left.z + right.z); 96 | } 97 | 98 | 99 | //////////////////////////////////////////////////////////// 100 | template 101 | inline Vector3 operator -(const Vector3& left, const Vector3& right) 102 | { 103 | return Vector3(left.x - right.x, left.y - right.y, left.z - right.z); 104 | } 105 | 106 | 107 | //////////////////////////////////////////////////////////// 108 | template 109 | inline Vector3 operator *(const Vector3& left, T right) 110 | { 111 | return Vector3(left.x * right, left.y * right, left.z * right); 112 | } 113 | 114 | 115 | //////////////////////////////////////////////////////////// 116 | template 117 | inline Vector3 operator *(T left, const Vector3& right) 118 | { 119 | return Vector3(right.x * left, right.y * left, right.z * left); 120 | } 121 | 122 | 123 | //////////////////////////////////////////////////////////// 124 | template 125 | inline Vector3& operator *=(Vector3& left, T right) 126 | { 127 | left.x *= right; 128 | left.y *= right; 129 | left.z *= right; 130 | 131 | return left; 132 | } 133 | 134 | 135 | //////////////////////////////////////////////////////////// 136 | template 137 | inline Vector3 operator /(const Vector3& left, T right) 138 | { 139 | return Vector3(left.x / right, left.y / right, left.z / right); 140 | } 141 | 142 | 143 | //////////////////////////////////////////////////////////// 144 | template 145 | inline Vector3& operator /=(Vector3& left, T right) 146 | { 147 | left.x /= right; 148 | left.y /= right; 149 | left.z /= right; 150 | 151 | return left; 152 | } 153 | 154 | 155 | //////////////////////////////////////////////////////////// 156 | template 157 | inline bool operator ==(const Vector3& left, const Vector3& right) 158 | { 159 | return (left.x == right.x) && (left.y == right.y) && (left.z == right.z); 160 | } 161 | 162 | 163 | //////////////////////////////////////////////////////////// 164 | template 165 | inline bool operator !=(const Vector3& left, const Vector3& right) 166 | { 167 | return (left.x != right.x) || (left.y != right.y) || (left.z != right.z); 168 | } 169 | -------------------------------------------------------------------------------- /include/Voronoi/.gitignore: -------------------------------------------------------------------------------- 1 | *.lastbuildstate 2 | *.opendb 3 | *.sdf 4 | *.lib 5 | *.tlog 6 | *.idb 7 | -------------------------------------------------------------------------------- /include/Voronoi/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(voronoi) 4 | 5 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 6 | 7 | list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/modules") 8 | 9 | find_package(SFML 2.4 COMPONENTS graphics system window) 10 | 11 | file(GLOB LIB_SOURCE "src/*.cpp" "include/*.h") 12 | 13 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${SFML_INCLUDE_DIR} include) 14 | 15 | SET(CMAKE_DEBUG_POSTFIX -d) 16 | 17 | add_library(voronoi ${LIB_SOURCE}) 18 | 19 | add_executable(sfvoronoi_example examples/SFML_Example.cpp) 20 | 21 | target_link_libraries(sfvoronoi_example debug voronoi-d optimized voronoi ${SFML_LIBRARIES} ${SFML_DEPENDENCIES}) 22 | -------------------------------------------------------------------------------- /include/Voronoi/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 mdally 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /include/Voronoi/README.md: -------------------------------------------------------------------------------- 1 | # Voronoi 2 | 3 | Windows: [![Build status](https://ci.appveyor.com/api/projects/status/l0yuxruih8f6rutc?svg=true)](https://ci.appveyor.com/project/JonnyPtn/voronoi) 4 | 5 | Linux: [ ![Codeship Status for JonnyPtn/Voronoi](https://app.codeship.com/projects/993e2000-bef9-0134-6367-0ef15c5d34cb/status?branch=sfvoronoi)](https://app.codeship.com/projects/196443) 6 | 7 | by Mark Dally 8 | 9 | https://github.com/mdally/Voronoi 10 | 11 | Modified for use with SFML by Jonny Paton 12 | 13 | http://www.sfml-dev.org/ 14 | 15 | ================================================ 16 | 17 | A simple library for computing Voronoi diagrams using Fortune's algorithm and performing Lloyd's relaxation. 18 | 19 | Cmake included, lib and example (in examples folder) should support all platforms supported by SFML 20 | 21 | ###Usage: 22 | //compute the diagram for a set of sites and a bounding box 23 | ``` 24 | VoronoiDiagramGenerator::compute(std::vector& sites, BoundingBox bbox) 25 | ``` 26 | 27 | //perform Lloyd's relaxation on the diagram last computed 28 | ``` 29 | VoronoiDiagramGenerator::relax() 30 | ``` 31 | 32 | ###Notes: 33 | * It is your responsibility to ensure that there are no duplicate sites or sites that fall outside or on the borders of the bounding box. 34 | 35 | * Performing Lloyd's relaxation returns a new diagram but does not delete the original. You must delete the old one in order to avoid memory leaks. 36 | -------------------------------------------------------------------------------- /include/Voronoi/bin/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/averrin/libmapgen/6b8dcaad87c9852f196e4ac16b14be2a1789fbcd/include/Voronoi/bin/.gitignore -------------------------------------------------------------------------------- /include/Voronoi/cmake_install.cmake: -------------------------------------------------------------------------------- 1 | # Install script for directory: /home/alexeynabrodov/projects/mapgen/Voronoi 2 | 3 | # Set the install prefix 4 | if(NOT DEFINED CMAKE_INSTALL_PREFIX) 5 | set(CMAKE_INSTALL_PREFIX "/usr/local") 6 | endif() 7 | string(REGEX REPLACE "/$" "" CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}") 8 | 9 | # Set the install configuration name. 10 | if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME) 11 | if(BUILD_TYPE) 12 | string(REGEX REPLACE "^[^A-Za-z0-9_]+" "" 13 | CMAKE_INSTALL_CONFIG_NAME "${BUILD_TYPE}") 14 | else() 15 | set(CMAKE_INSTALL_CONFIG_NAME "") 16 | endif() 17 | message(STATUS "Install configuration: \"${CMAKE_INSTALL_CONFIG_NAME}\"") 18 | endif() 19 | 20 | # Set the component getting installed. 21 | if(NOT CMAKE_INSTALL_COMPONENT) 22 | if(COMPONENT) 23 | message(STATUS "Install component: \"${COMPONENT}\"") 24 | set(CMAKE_INSTALL_COMPONENT "${COMPONENT}") 25 | else() 26 | set(CMAKE_INSTALL_COMPONENT) 27 | endif() 28 | endif() 29 | 30 | # Install shared libraries without execute permission? 31 | if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE) 32 | set(CMAKE_INSTALL_SO_NO_EXE "1") 33 | endif() 34 | 35 | if(CMAKE_INSTALL_COMPONENT) 36 | set(CMAKE_INSTALL_MANIFEST "install_manifest_${CMAKE_INSTALL_COMPONENT}.txt") 37 | else() 38 | set(CMAKE_INSTALL_MANIFEST "install_manifest.txt") 39 | endif() 40 | 41 | string(REPLACE ";" "\n" CMAKE_INSTALL_MANIFEST_CONTENT 42 | "${CMAKE_INSTALL_MANIFEST_FILES}") 43 | file(WRITE "/home/alexeynabrodov/projects/mapgen/Voronoi/${CMAKE_INSTALL_MANIFEST}" 44 | "${CMAKE_INSTALL_MANIFEST_CONTENT}") 45 | -------------------------------------------------------------------------------- /include/Voronoi/examples/SFML_Example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | // Window dimensions 10 | constexpr int windowSize(900); 11 | 12 | double normalize(double in, int dimension) 13 | { 14 | return in / (float)dimension*1.8 - 0.9; 15 | } 16 | //globals for use in giving relaxation commands 17 | int relax = 0; 18 | bool startOver = true; 19 | bool relaxForever = false; 20 | 21 | bool sitesOrdered(const sf::Vector2& s1, const sf::Vector2& s2) { 22 | if (s1.y < s2.y) 23 | return true; 24 | if (s1.y == s2.y && s1.x < s2.x) 25 | return true; 26 | 27 | return false; 28 | } 29 | 30 | void genRandomSites(std::vector>& sites, sf::Rect& bbox, unsigned int dimension, unsigned int numSites) { 31 | std::vector> tmpSites; 32 | 33 | tmpSites.reserve(numSites); 34 | sites.reserve(numSites); 35 | 36 | sf::Vector2 s; 37 | 38 | srand(std::clock()); 39 | for (unsigned int i = 0; i < numSites; ++i) { 40 | s.x = 1 + (rand() / (double)RAND_MAX)*(dimension - 2); 41 | s.y = 1 + (rand() / (double)RAND_MAX)*(dimension - 2); 42 | tmpSites.push_back(s); 43 | } 44 | 45 | //remove any duplicates that exist 46 | std::sort(tmpSites.begin(), tmpSites.end(), sitesOrdered); 47 | sites.push_back(tmpSites[0]); 48 | for (sf::Vector2& s : tmpSites) { 49 | if (s != sites.back()) sites.push_back(s); 50 | } 51 | } 52 | 53 | int main() 54 | { 55 | //number of points to generate 56 | unsigned int nPoints; 57 | unsigned int dimension = 900; 58 | 59 | //the generator 60 | VoronoiDiagramGenerator vdg = VoronoiDiagramGenerator(); 61 | 62 | //the generated diagram 63 | std::unique_ptr diagram; 64 | 65 | //sites used for generation 66 | std::vector>* sites; 67 | 68 | //maximum bounds for the diagram 69 | sf::Rect bbox(0,0,windowSize,windowSize); 70 | 71 | //used to measure generation time 72 | sf::Clock timer; 73 | 74 | //used to draw the diagram 75 | std::vector vertices; 76 | 77 | //update the visuals 78 | auto updateVisuals = [&]() 79 | { 80 | //clear first 81 | vertices.clear(); 82 | 83 | //then reserve the correct amount of verts 84 | // one point for each cell, and one line (2 verts) for each edge 85 | vertices.reserve(diagram->cells.size() + (diagram->edges.size() * 2)); 86 | for (auto c : diagram->cells) 87 | { 88 | if(c == nullptr) { 89 | continue; 90 | } 91 | //red point for each cell site 92 | sf::Vector2& p = c->site.p; 93 | vertices.push_back({{ static_cast(p.x),static_cast(p.y)}, sf::Color::Red}); 94 | } 95 | 96 | for (Edge* e : diagram->edges) 97 | { 98 | if (e->vertA && e->vertB) 99 | { 100 | sf::Vector2& p1 = *e->vertA; 101 | sf::Vector2& p2 = *e->vertB; 102 | 103 | //white line for each edge 104 | vertices.push_back({ { static_cast(p1.x),static_cast(p1.y) },sf::Color::White }); 105 | vertices.push_back({ { static_cast(p2.x),static_cast(p2.y) },sf::Color::White }); 106 | } 107 | } 108 | }; 109 | 110 | //generate a new diagram 111 | auto generateNewDiagram = [&]() 112 | { 113 | std::cout << "\tPress 'R' to perform Lloyd's relaxation once.\n" 114 | "\tPress 'T' to perform Lloyd's relaxation ten times.\n" 115 | "\tPress 'Y' to toggle continuous Lloyd's relaxation.\n" 116 | "\tPress 'X' to generate a new diagram with a different number of sites.\n" 117 | "\tPress 'Esc' to exit.\n\n"; 118 | startOver = false; 119 | relaxForever = false; 120 | relax = 0; 121 | sites = new std::vector>(); 122 | std::cout << "How many points? "; 123 | std::cin >> nPoints; 124 | genRandomSites(*sites, bbox, dimension, nPoints); 125 | timer.restart(); 126 | diagram.reset(vdg.compute(*sites, bbox)); 127 | auto duration = timer.getElapsedTime().asMilliseconds(); 128 | std::cout << "Computing a diagram of " << nPoints << " points took " << duration << "ms.\n"; 129 | delete sites; 130 | updateVisuals(); 131 | }; 132 | 133 | generateNewDiagram(); 134 | 135 | //now open the window 136 | sf::RenderWindow window; 137 | window.create(sf::VideoMode(windowSize, windowSize), "Voronoi"); 138 | window.setVerticalSyncEnabled(true); 139 | 140 | while (window.isOpen()) 141 | { 142 | sf::Event evt; 143 | while (window.pollEvent(evt)) 144 | { 145 | switch (evt.type) 146 | { 147 | case sf::Event::Closed: 148 | window.close(); 149 | break; 150 | 151 | case sf::Event::KeyPressed: 152 | switch (evt.key.code) 153 | { 154 | case sf::Keyboard::Escape: 155 | window.close(); 156 | break; 157 | 158 | case sf::Keyboard::R: 159 | ++relax; 160 | break; 161 | 162 | case sf::Keyboard::T: 163 | relax += 10; 164 | break; 165 | 166 | case sf::Keyboard::X: 167 | generateNewDiagram(); 168 | break; 169 | 170 | case sf::Keyboard::Y: 171 | if (relaxForever) 172 | relaxForever = false; 173 | else 174 | relaxForever = true; 175 | break; 176 | } 177 | break; 178 | } 179 | } 180 | 181 | if (relax || relaxForever) 182 | { 183 | timer.restart(); 184 | diagram.reset(vdg.relax()); 185 | auto duration = timer.getElapsedTime().asMilliseconds(); 186 | 187 | std::cout << "Computing a diagram of " << nPoints << " points took " << duration << "ms.\n"; 188 | --relax; 189 | if (relax < 0) 190 | relax = 0; 191 | 192 | updateVisuals(); 193 | } 194 | 195 | window.clear(); 196 | 197 | //draw points first 198 | auto pointCount = diagram->cells.size(); 199 | window.draw(vertices.data(), pointCount,sf::PrimitiveType::Points); 200 | 201 | //then lines, starting from the vert after the last point 202 | window.draw(vertices.data() + pointCount, vertices.size() - pointCount, sf::PrimitiveType::Lines); 203 | 204 | window.display(); 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /include/Voronoi/include/Cell.h: -------------------------------------------------------------------------------- 1 | #ifndef _CELL_H_ 2 | #define _CELL_H_ 3 | 4 | #include "Vector2.hpp" 5 | #include "Rect.hpp" 6 | #include 7 | 8 | struct Cell; 9 | struct Site { 10 | sf::Vector2 p; 11 | Cell* cell; 12 | 13 | Site() {}; 14 | Site(sf::Vector2 _p, Cell* _cell) : p(_p), cell(_cell) {}; 15 | }; 16 | 17 | struct HalfEdge; 18 | struct Cell { 19 | Site site; 20 | std::vector halfEdges; 21 | bool closeMe; 22 | 23 | Cell() : closeMe(false) {}; 24 | Cell(sf::Vector2 _site) : site(_site, this), closeMe(false) {}; 25 | 26 | std::vector getNeighbors(); 27 | sf::Rect getBoundingBox(); 28 | 29 | std::vector getEdges(); 30 | 31 | // Return whether a point is inside, on, or outside the cell: 32 | // -1: point is outside the perimeter of the cell 33 | // 0: point is on the perimeter of the cell 34 | // 1: point is inside the perimeter of the cell 35 | int pointIntersection(double x, double y); 36 | 37 | static bool edgesCCW(HalfEdge* a, HalfEdge* b); 38 | }; 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /include/Voronoi/include/Diagram.h: -------------------------------------------------------------------------------- 1 | #ifndef _DIAGRAM_H_ 2 | #define _DIAGRAM_H_ 3 | 4 | #include "../src/MemoryPool/C-11/MemoryPool.h" 5 | //#include "../src/MemoryPool/C-98/MemoryPool.h" //You will need to use this version instead of the one above if your compiler doesn't handle C++11's noexcept operator 6 | #include "Edge.h" 7 | #include "Cell.h" 8 | #include 9 | 10 | class Diagram { 11 | public: 12 | std::vector cells; 13 | std::vector edges; 14 | std::vector*> vertices; 15 | 16 | void printDiagram(); 17 | private: 18 | friend class VoronoiDiagramGenerator; 19 | 20 | std::set tmpCells; 21 | std::set tmpEdges; 22 | std::set*> tmpVertices; 23 | 24 | MemoryPool cellPool; 25 | MemoryPool edgePool; 26 | MemoryPool halfEdgePool; 27 | MemoryPool> vertexPool; 28 | 29 | sf::Vector2* createVertex(double x, double y); 30 | Cell* createCell(sf::Vector2 site); 31 | Edge* createEdge(Site* lSite, Site* rSite, sf::Vector2* vertA, sf::Vector2* vertB); 32 | Edge* createBorderEdge(Site* lSite, sf::Vector2* vertA, sf::Vector2* vertB); 33 | 34 | bool connectEdge(Edge* edge, sf::Rect bbox); 35 | bool clipEdge(Edge* edge, sf::Rect bbox); 36 | void clipEdges(sf::Rect bbox); 37 | void closeCells(sf::Rect bbox); 38 | void finalize(); 39 | }; 40 | 41 | #endif -------------------------------------------------------------------------------- /include/Voronoi/include/Edge.h: -------------------------------------------------------------------------------- 1 | #ifndef _EDGE_H_ 2 | #define _EDGE_H_ 3 | 4 | #include "Vector2.hpp" 5 | 6 | struct Site; 7 | 8 | struct Edge { 9 | Site* lSite; 10 | Site* rSite; 11 | sf::Vector2* vertA; 12 | sf::Vector2* vertB; 13 | 14 | Edge() : lSite(nullptr), rSite(nullptr), vertA(nullptr), vertB(nullptr) {}; 15 | Edge(Site* _lSite, Site* _rSite) : lSite(_lSite), rSite(_rSite), vertA(nullptr), vertB(nullptr) {}; 16 | Edge(Site* lS, Site* rS, sf::Vector2* vA, sf::Vector2* vB) : lSite(lS), rSite(rS), vertA(vA), vertB(vB) {}; 17 | 18 | void setStartPoint(Site* _lSite, Site* _rSite, sf::Vector2* vertex); 19 | void setEndPoint(Site* _lSite, Site* _rSite, sf::Vector2* vertex); 20 | }; 21 | 22 | struct HalfEdge { 23 | Site* site; 24 | Edge* edge; 25 | double angle; 26 | 27 | HalfEdge() : site(nullptr), edge(nullptr) {}; 28 | HalfEdge(Edge* e, Site* lSite, Site* rSite); 29 | 30 | inline sf::Vector2* startPoint(); 31 | inline sf::Vector2* endPoint(); 32 | }; 33 | 34 | inline sf::Vector2* HalfEdge::startPoint() { 35 | return (edge->lSite == site) ? edge->vertA : edge->vertB; 36 | } 37 | 38 | inline sf::Vector2 * HalfEdge::endPoint() { 39 | return (edge->lSite == site) ? edge->vertB : edge->vertA; 40 | } 41 | 42 | #endif -------------------------------------------------------------------------------- /include/Voronoi/include/VoronoiDiagramGenerator.h: -------------------------------------------------------------------------------- 1 | #ifndef _VORONOI_DIAGRAM_GENERATOR_H_ 2 | #define _VORONOI_DIAGRAM_GENERATOR_H_ 3 | 4 | #include "../src/RBTree.h" 5 | #include "../src/CircleEventQueue.h" 6 | #include "../src/BeachLine.h" 7 | #include "Diagram.h" 8 | #include 9 | 10 | class VoronoiDiagramGenerator { 11 | public: 12 | VoronoiDiagramGenerator() : circleEventQueue(nullptr), siteEventQueue(nullptr), beachLine(nullptr) {}; 13 | ~VoronoiDiagramGenerator() {}; 14 | 15 | Diagram* compute(std::vector>& sites, sf::Rect bbox); 16 | Diagram* relax(); 17 | private: 18 | Diagram* diagram; 19 | CircleEventQueue* circleEventQueue; 20 | std::vector*>* siteEventQueue; 21 | sf::Rect boundingBox; 22 | 23 | void printBeachLine(); 24 | 25 | //BeachLine 26 | RBTree* beachLine; 27 | treeNode* addBeachSection(Site* site); 28 | inline void detachBeachSection(treeNode* section); 29 | void removeBeachSection(treeNode* section); 30 | double leftBreakpoint(treeNode* section, double directrix); 31 | double rightBreakpoint(treeNode* section, double directrix); 32 | }; 33 | 34 | inline void VoronoiDiagramGenerator::detachBeachSection(treeNode* section) { 35 | circleEventQueue->removeCircleEvent(section); 36 | beachLine->removeNode(section); 37 | } 38 | 39 | #endif -------------------------------------------------------------------------------- /include/Voronoi/libvoronoi.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/averrin/libmapgen/6b8dcaad87c9852f196e4ac16b14be2a1789fbcd/include/Voronoi/libvoronoi.a -------------------------------------------------------------------------------- /include/Voronoi/sfvoronoi_example: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/averrin/libmapgen/6b8dcaad87c9852f196e4ac16b14be2a1789fbcd/include/Voronoi/sfvoronoi_example -------------------------------------------------------------------------------- /include/Voronoi/src/BeachLine.h: -------------------------------------------------------------------------------- 1 | #ifndef _BEACHLINE_H_ 2 | #define _BEACHLINE_H_ 3 | 4 | #include "RBTree.h" 5 | 6 | struct Site; 7 | struct Edge; 8 | struct CircleEvent; 9 | struct BeachSection { 10 | Site* site; 11 | Edge* edge; 12 | treeNode* circleEvent; 13 | 14 | BeachSection() : site(nullptr), edge(nullptr), circleEvent(nullptr) {}; 15 | ~BeachSection() {}; 16 | BeachSection(Site* _site) : site(_site), edge(nullptr), circleEvent(nullptr) {}; 17 | }; 18 | 19 | #endif -------------------------------------------------------------------------------- /include/Voronoi/src/Cell.cpp: -------------------------------------------------------------------------------- 1 | #include "../include/Cell.h" 2 | #include "../include/Edge.h" 3 | #include 4 | #include 5 | 6 | std::vector Cell::getNeighbors() { 7 | std::vector neighbors; 8 | Edge* e; 9 | 10 | size_t edgeCount = halfEdges.size(); 11 | while (edgeCount--) { 12 | e = halfEdges[edgeCount]->edge; 13 | if (e->lSite && e->lSite != &site) { 14 | neighbors.push_back(e->lSite->cell); 15 | } 16 | else if (e->rSite && e->rSite != &site) { 17 | neighbors.push_back(e->rSite->cell); 18 | } 19 | } 20 | 21 | return neighbors; 22 | } 23 | 24 | sf::Rect Cell::getBoundingBox() { 25 | size_t edgeCount = halfEdges.size(); 26 | double xmin = std::numeric_limits::infinity(); 27 | double ymin = xmin; 28 | double xmax = -xmin; 29 | double ymax = xmax; 30 | 31 | sf::Vector2* vert; 32 | while (edgeCount--) 33 | { 34 | vert = halfEdges[edgeCount]->startPoint(); 35 | 36 | double vx = vert->x; 37 | double vy = vert->y; 38 | 39 | if (vx < xmin) xmin = vx; 40 | if (vy < ymin) ymin = vy; 41 | if (vx > xmax) xmax = vx; 42 | if (vy > ymax) ymax = vy; 43 | } 44 | 45 | return{ xmin, ymin, xmax - xmin, ymax - ymin }; 46 | } 47 | 48 | std::vector Cell::getEdges() { 49 | return halfEdges; 50 | } 51 | 52 | // Return whether a point is inside, on, or outside the cell: 53 | // -1: point is outside the perimeter of the cell 54 | // 0: point is on the perimeter of the cell 55 | // 1: point is inside the perimeter of the cell 56 | // 57 | int Cell::pointIntersection(double x, double y) { 58 | // Check if point in polygon. Since all polygons of a Voronoi 59 | // diagram are convex, then: 60 | // http://paulbourke.net/geometry/polygonmesh/ 61 | // Solution 3 (2D): 62 | // "If the polygon is convex then one can consider the polygon 63 | // "as a 'path' from the first vertex. A point is on the interior 64 | // "of this polygons if it is always on the same side of all the 65 | // "line segments making up the path. ... 66 | // "(y - y0) (x1 - x0) - (x - x0) (y1 - y0) 67 | // "if it is less than 0 then P is to the right of the line segment, 68 | // "if greater than 0 it is to the left, if equal to 0 then it lies 69 | // "on the line segment" 70 | HalfEdge* he; 71 | size_t edgeCount = halfEdges.size(); 72 | sf::Vector2 p0; 73 | sf::Vector2 p1; 74 | double r; 75 | 76 | while (edgeCount--) { 77 | he = halfEdges[edgeCount]; 78 | p0 = *he->startPoint(); 79 | p1 = *he->endPoint(); 80 | r = (y - p0.y)*(p1.x - p0.x) - (x - p0.x)*(p1.y - p0.y); 81 | 82 | if (r == 0) { 83 | return 0; 84 | } 85 | if (r > 0) { 86 | return -1; 87 | } 88 | } 89 | return 1; 90 | } 91 | 92 | bool Cell::edgesCCW(HalfEdge* a, HalfEdge* b) { 93 | return a->angle > b->angle; 94 | } 95 | -------------------------------------------------------------------------------- /include/Voronoi/src/CircleEventQueue.cpp: -------------------------------------------------------------------------------- 1 | #include "Vector2.hpp" 2 | #include "CircleEventQueue.h" 3 | #include "../include/Cell.h" 4 | 5 | #include 6 | 7 | void CircleEventQueue::addCircleEvent(treeNode* section) { 8 | if (!section) return; 9 | treeNode* lSection = section->prev; 10 | treeNode* rSection = section->next; 11 | if (!lSection || !rSection) { return; } 12 | 13 | sf::Vector2 lSite = lSection->data.site->p; 14 | sf::Vector2 cSite = section->data.site->p; 15 | sf::Vector2 rSite = rSection->data.site->p; 16 | 17 | // If site of left beachsection is same as site of 18 | // right beachsection, there can't be convergence 19 | if (lSection == rSection) { return; } 20 | 21 | // Find the circumscribed circle for the three sites associated 22 | // with the beachsection triplet. 23 | // http://mathforum.org/library/drmath/view/55002.html 24 | // The bottom-most part of the circumcircle is our Fortune 'circle 25 | // event', and its center is a vertex potentially part of the final 26 | // Voronoi diagram. 27 | double bx = cSite.x; 28 | double by = cSite.y; 29 | double ax = lSite.x - bx; 30 | double ay = lSite.y - by; 31 | double cx = rSite.x - bx; 32 | double cy = rSite.y - by; 33 | 34 | // If points l->c->r are clockwise, then center beach section does not 35 | // collapse, hence it can't end up as a vertex (we reuse 'd' here, which 36 | // sign is reverse of the orientation, hence we reverse the test. 37 | // http://en.wikipedia.org/wiki/Curve_orientation#Orientation_of_a_simple_polygon 38 | double d = 2 * (ax*cy - ay*cx); 39 | if (d >= -2e-12) { return; } //handles finite precision error 40 | 41 | double ha = ax*ax + ay*ay; 42 | double hc = cx*cx + cy*cy; 43 | double x = (cy*ha - ay*hc) / d; 44 | double y = (ax*hc - cx*ha) / d; 45 | double ycenter = y + by; 46 | 47 | CircleEvent circleEvent(section->data.site, x + bx, ycenter + std::sqrt(x*x + y*y), ycenter, section); 48 | 49 | // find insertion point in RB-tree: 50 | // circle events are ordered from smallest to largest 51 | treeNode* predecessor = nullptr; 52 | treeNode* node = eventQueue.getRoot(); 53 | while (node) { 54 | CircleEvent& nodeEvent = node->data; 55 | if (circleEvent.y < nodeEvent.y || (circleEvent.y == nodeEvent.y && circleEvent.x <= nodeEvent.x)) { 56 | if (node->left) { 57 | node = node->left; 58 | } 59 | else { 60 | predecessor = node->prev; 61 | break; 62 | } 63 | } 64 | else { 65 | if (node->right) { 66 | node = node->right; 67 | } 68 | else { 69 | predecessor = node; 70 | break; 71 | } 72 | } 73 | } 74 | treeNode* newEvent = eventQueue.insertSuccessor(predecessor, circleEvent); 75 | section->data.circleEvent = newEvent; 76 | if (!predecessor) { 77 | firstEvent = newEvent; 78 | } 79 | } 80 | 81 | void CircleEventQueue::removeCircleEvent(treeNode* section) { 82 | treeNode* circleEvent = section->data.circleEvent; 83 | if (circleEvent) { 84 | if (!circleEvent->prev) { 85 | firstEvent = circleEvent->next; 86 | } 87 | eventQueue.removeNode(circleEvent); 88 | section->data.circleEvent = nullptr; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /include/Voronoi/src/CircleEventQueue.h: -------------------------------------------------------------------------------- 1 | #ifndef _CIRCLE_EVENT_QUEUE_H_ 2 | #define _CIRCLE_EVENT_QUEUE_H_ 3 | 4 | #include "RBTree.h" 5 | #include "BeachLine.h" 6 | 7 | struct Site; 8 | struct BeachSection; 9 | 10 | struct CircleEvent { 11 | Site* site; 12 | double x; 13 | double y; 14 | double yCenter; 15 | treeNode* beachSection; 16 | 17 | CircleEvent() {}; 18 | ~CircleEvent() {}; 19 | CircleEvent(Site* _site, double _x, double _y, double _yCenter, treeNode* _section) { 20 | site = _site; 21 | x = _x; 22 | y = _y; 23 | yCenter = _yCenter; 24 | beachSection = _section; 25 | } 26 | }; 27 | 28 | struct CircleEventQueue { 29 | treeNode* firstEvent; 30 | RBTree eventQueue; 31 | 32 | CircleEventQueue() : firstEvent(nullptr) {}; 33 | ~CircleEventQueue() {}; 34 | 35 | void addCircleEvent(treeNode* section); 36 | void removeCircleEvent(treeNode* section); 37 | }; 38 | 39 | #endif -------------------------------------------------------------------------------- /include/Voronoi/src/Edge.cpp: -------------------------------------------------------------------------------- 1 | #include "../include/Edge.h" 2 | #include "../include/Cell.h" 3 | #include 4 | 5 | HalfEdge::HalfEdge(Edge* e, Site* lSite, Site* rSite) { 6 | site = lSite; 7 | edge = e; 8 | // 'angle' is a value to be used for properly sorting the 9 | // halfsegments counterclockwise. By convention, we will 10 | // use the angle of the line defined by the 'site to the left' 11 | // to the 'site to the right'. 12 | // However, border edges have no 'site to the right': thus we 13 | // use the angle of line perpendicular to the halfsegment (the 14 | // edge should have both end points defined in such case.) 15 | if (rSite) { 16 | angle = atan2(rSite->p.y - lSite->p.y, rSite->p.x - lSite->p.x); 17 | } 18 | else { 19 | sf::Vector2& va = *(e->vertA); 20 | sf::Vector2& vb = *(e->vertB); 21 | 22 | angle = (e->lSite == lSite) ? atan2(vb.x - va.x, va.y - vb.y) : atan2(va.x - vb.x, vb.y - va.y); 23 | } 24 | } 25 | 26 | void Edge::setStartPoint(Site* _lSite, Site* _rSite, sf::Vector2* vertex) { 27 | if (!vertA && !vertB) { 28 | vertA = vertex; 29 | lSite = _lSite; 30 | rSite = _rSite; 31 | } 32 | else if (lSite == _rSite) { 33 | vertB = vertex; 34 | } 35 | else { 36 | vertA = vertex; 37 | } 38 | } 39 | 40 | void Edge::setEndPoint(Site* _lSite, Site* _rSite, sf::Vector2* vertex) { 41 | setStartPoint(_rSite, _lSite, vertex); 42 | } 43 | -------------------------------------------------------------------------------- /include/Voronoi/src/Epsilon.h: -------------------------------------------------------------------------------- 1 | #ifndef _EPSILON_H_ 2 | #define _EPSILON_H_ 3 | 4 | #include 5 | 6 | #define EPSILON 1e-9 7 | #define INV_EPSILON (1.0 / EPSILON); 8 | 9 | inline bool eq_withEpsilon(double a, double b) { return std::abs(a - b) < EPSILON; } 10 | inline bool gt_withEpsilon(double a, double b) { return a - b > EPSILON; } 11 | inline bool gteq_withEpsilon(double a, double b) { return b - a < EPSILON; } 12 | inline bool lt_withEpsilon(double a, double b) { return b - a > EPSILON; } 13 | inline bool lteq_withEpsilon(double a, double b) { return a - b < EPSILON; } 14 | 15 | #endif -------------------------------------------------------------------------------- /include/Voronoi/src/MemoryPool/C-11/MemoryPool.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2013 Cosku Acay, http://www.coskuacay.com 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef MEMORY_POOL_H 24 | #define MEMORY_POOL_H 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | template 33 | class MemoryPool 34 | { 35 | public: 36 | /* Member types */ 37 | typedef T value_type; 38 | typedef T* pointer; 39 | typedef T& reference; 40 | typedef const T* const_pointer; 41 | typedef const T& const_reference; 42 | typedef size_t size_type; 43 | typedef ptrdiff_t difference_type; 44 | typedef std::false_type propagate_on_container_copy_assignment; 45 | typedef std::true_type propagate_on_container_move_assignment; 46 | typedef std::true_type propagate_on_container_swap; 47 | 48 | template struct rebind { 49 | typedef MemoryPool other; 50 | }; 51 | 52 | /* Member functions */ 53 | MemoryPool() noexcept; 54 | MemoryPool(const MemoryPool& memoryPool) noexcept; 55 | MemoryPool(MemoryPool&& memoryPool) noexcept; 56 | template MemoryPool(const MemoryPool& memoryPool) noexcept; 57 | 58 | ~MemoryPool() noexcept; 59 | 60 | MemoryPool& operator=(const MemoryPool& memoryPool) = delete; 61 | MemoryPool& operator=(MemoryPool&& memoryPool) noexcept; 62 | 63 | pointer address(reference x) const noexcept; 64 | const_pointer address(const_reference x) const noexcept; 65 | 66 | // Can only allocate one object at a time. n and hint are ignored 67 | pointer allocate(size_type n = 1, const_pointer hint = 0); 68 | void deallocate(pointer p, size_type n = 1); 69 | 70 | size_type max_size() const noexcept; 71 | 72 | template void construct(U* p, Args&&... args); 73 | template void destroy(U* p); 74 | 75 | template pointer newElement(Args&&... args); 76 | void deleteElement(pointer p); 77 | 78 | private: 79 | union Slot_ { 80 | value_type element; 81 | Slot_* next; 82 | 83 | ~Slot_() {}; 84 | }; 85 | 86 | typedef char* data_pointer_; 87 | typedef Slot_ slot_type_; 88 | typedef Slot_* slot_pointer_; 89 | 90 | slot_pointer_ currentBlock_; 91 | slot_pointer_ currentSlot_; 92 | slot_pointer_ lastSlot_; 93 | slot_pointer_ freeSlots_; 94 | 95 | size_type padPointer(data_pointer_ p, size_type align) const noexcept; 96 | void allocateBlock(); 97 | 98 | static_assert(BlockSize >= 2 * sizeof(slot_type_), "BlockSize too small."); 99 | }; 100 | 101 | #include "MemoryPool.tcc" 102 | 103 | #endif // MEMORY_POOL_H 104 | -------------------------------------------------------------------------------- /include/Voronoi/src/MemoryPool/C-11/MemoryPool.tcc: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2013 Cosku Acay, http://www.coskuacay.com 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef MEMORY_BLOCK_TCC 24 | #define MEMORY_BLOCK_TCC 25 | 26 | 27 | 28 | template 29 | inline typename MemoryPool::size_type 30 | MemoryPool::padPointer(data_pointer_ p, size_type align) 31 | const noexcept 32 | { 33 | uintptr_t result = reinterpret_cast(p); 34 | return ((align - result) % align); 35 | } 36 | 37 | 38 | 39 | template 40 | MemoryPool::MemoryPool() 41 | noexcept 42 | { 43 | currentBlock_ = nullptr; 44 | currentSlot_ = nullptr; 45 | lastSlot_ = nullptr; 46 | freeSlots_ = nullptr; 47 | } 48 | 49 | 50 | 51 | template 52 | MemoryPool::MemoryPool(const MemoryPool& memoryPool) 53 | noexcept : 54 | MemoryPool() 55 | {} 56 | 57 | 58 | 59 | template 60 | MemoryPool::MemoryPool(MemoryPool&& memoryPool) 61 | noexcept 62 | { 63 | currentBlock_ = memoryPool.currentBlock_; 64 | memoryPool.currentBlock_ = nullptr; 65 | currentSlot_ = memoryPool.currentSlot_; 66 | lastSlot_ = memoryPool.lastSlot_; 67 | freeSlots_ = memoryPool.freeSlots; 68 | } 69 | 70 | 71 | template 72 | template 73 | MemoryPool::MemoryPool(const MemoryPool& memoryPool) 74 | noexcept : 75 | MemoryPool() 76 | {} 77 | 78 | 79 | 80 | template 81 | MemoryPool& 82 | MemoryPool::operator=(MemoryPool&& memoryPool) 83 | noexcept 84 | { 85 | if (this != &memoryPool) 86 | { 87 | std::swap(currentBlock_, memoryPool.currentBlock_); 88 | currentSlot_ = memoryPool.currentSlot_; 89 | lastSlot_ = memoryPool.lastSlot_; 90 | freeSlots_ = memoryPool.freeSlots; 91 | } 92 | return *this; 93 | } 94 | 95 | 96 | 97 | template 98 | MemoryPool::~MemoryPool() 99 | noexcept 100 | { 101 | slot_pointer_ curr = currentBlock_; 102 | while (curr != nullptr) { 103 | slot_pointer_ prev = curr->next; 104 | operator delete(reinterpret_cast(curr)); 105 | curr = prev; 106 | } 107 | } 108 | 109 | 110 | 111 | template 112 | inline typename MemoryPool::pointer 113 | MemoryPool::address(reference x) 114 | const noexcept 115 | { 116 | return &x; 117 | } 118 | 119 | 120 | 121 | template 122 | inline typename MemoryPool::const_pointer 123 | MemoryPool::address(const_reference x) 124 | const noexcept 125 | { 126 | return &x; 127 | } 128 | 129 | 130 | 131 | template 132 | void 133 | MemoryPool::allocateBlock() 134 | { 135 | // Allocate space for the new block and store a pointer to the previous one 136 | data_pointer_ newBlock = reinterpret_cast 137 | (operator new(BlockSize)); 138 | reinterpret_cast(newBlock)->next = currentBlock_; 139 | currentBlock_ = reinterpret_cast(newBlock); 140 | // Pad block body to staisfy the alignment requirements for elements 141 | data_pointer_ body = newBlock + sizeof(slot_pointer_); 142 | size_type bodyPadding = padPointer(body, alignof(slot_type_)); 143 | currentSlot_ = reinterpret_cast(body + bodyPadding); 144 | lastSlot_ = reinterpret_cast 145 | (newBlock + BlockSize - sizeof(slot_type_) + 1); 146 | } 147 | 148 | 149 | 150 | template 151 | inline typename MemoryPool::pointer 152 | MemoryPool::allocate(size_type n, const_pointer hint) 153 | { 154 | if (freeSlots_ != nullptr) { 155 | pointer result = reinterpret_cast(freeSlots_); 156 | freeSlots_ = freeSlots_->next; 157 | return result; 158 | } 159 | else { 160 | if (currentSlot_ >= lastSlot_) 161 | allocateBlock(); 162 | return reinterpret_cast(currentSlot_++); 163 | } 164 | } 165 | 166 | 167 | 168 | template 169 | inline void 170 | MemoryPool::deallocate(pointer p, size_type n) 171 | { 172 | if (p != nullptr) { 173 | reinterpret_cast(p)->next = freeSlots_; 174 | freeSlots_ = reinterpret_cast(p); 175 | } 176 | } 177 | 178 | 179 | 180 | template 181 | inline typename MemoryPool::size_type 182 | MemoryPool::max_size() 183 | const noexcept 184 | { 185 | size_type maxBlocks = -1 / BlockSize; 186 | return (BlockSize - sizeof(data_pointer_)) / sizeof(slot_type_) * maxBlocks; 187 | } 188 | 189 | 190 | 191 | template 192 | template 193 | inline void 194 | MemoryPool::construct(U* p, Args&&... args) 195 | { 196 | new (p) U (std::forward(args)...); 197 | } 198 | 199 | 200 | 201 | template 202 | template 203 | inline void 204 | MemoryPool::destroy(U* p) 205 | { 206 | p->~U(); 207 | } 208 | 209 | 210 | 211 | template 212 | template 213 | inline typename MemoryPool::pointer 214 | MemoryPool::newElement(Args&&... args) 215 | { 216 | pointer result = allocate(); 217 | construct(result, std::forward(args)...); 218 | return result; 219 | } 220 | 221 | 222 | 223 | template 224 | inline void 225 | MemoryPool::deleteElement(pointer p) 226 | { 227 | if (p != nullptr) { 228 | p->~value_type(); 229 | deallocate(p); 230 | } 231 | } 232 | 233 | 234 | 235 | #endif // MEMORY_BLOCK_TCC 236 | -------------------------------------------------------------------------------- /include/Voronoi/src/MemoryPool/C-98/MemoryPool.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2013 Cosku Acay, http://www.coskuacay.com 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef MEMORY_POOL_H 24 | #define MEMORY_POOL_H 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | template 32 | class MemoryPool 33 | { 34 | public: 35 | /* Member types */ 36 | typedef T value_type; 37 | typedef T* pointer; 38 | typedef T& reference; 39 | typedef const T* const_pointer; 40 | typedef const T& const_reference; 41 | typedef size_t size_type; 42 | typedef ptrdiff_t difference_type; 43 | 44 | template struct rebind { 45 | typedef MemoryPool other; 46 | }; 47 | 48 | /* Member functions */ 49 | MemoryPool() throw(); 50 | MemoryPool(const MemoryPool& memoryPool) throw(); 51 | template MemoryPool(const MemoryPool& memoryPool) throw(); 52 | 53 | ~MemoryPool() throw(); 54 | 55 | pointer address(reference x) const throw(); 56 | const_pointer address(const_reference x) const throw(); 57 | 58 | // Can only allocate one object at a time. n and hint are ignored 59 | pointer allocate(size_type n = 1, const_pointer hint = 0); 60 | void deallocate(pointer p, size_type n = 1); 61 | 62 | size_type max_size() const throw(); 63 | 64 | void construct(pointer p, const_reference val); 65 | void destroy(pointer p); 66 | 67 | pointer newElement(const_reference val); 68 | void deleteElement(pointer p); 69 | 70 | private: 71 | union Slot_ { 72 | value_type element; 73 | Slot_* next; 74 | 75 | ~Slot_() {} 76 | }; 77 | 78 | typedef char* data_pointer_; 79 | typedef Slot_ slot_type_; 80 | typedef Slot_* slot_pointer_; 81 | 82 | slot_pointer_ currentBlock_; 83 | slot_pointer_ currentSlot_; 84 | slot_pointer_ lastSlot_; 85 | slot_pointer_ freeSlots_; 86 | 87 | size_type padPointer(data_pointer_ p, size_type align) const throw(); 88 | void allocateBlock(); 89 | /* 90 | static_assert(BlockSize >= 2 * sizeof(slot_type_), "BlockSize too small."); 91 | */ 92 | }; 93 | 94 | #include "MemoryPool.tcc" 95 | 96 | #endif // MEMORY_POOL_H 97 | -------------------------------------------------------------------------------- /include/Voronoi/src/MemoryPool/C-98/MemoryPool.tcc: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2013 Cosku Acay, http://www.coskuacay.com 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef MEMORY_BLOCK_TCC 24 | #define MEMORY_BLOCK_TCC 25 | 26 | 27 | 28 | template 29 | inline typename MemoryPool::size_type 30 | MemoryPool::padPointer(data_pointer_ p, size_type align) 31 | const throw() 32 | { 33 | size_t result = reinterpret_cast(p); 34 | return ((align - result) % align); 35 | } 36 | 37 | 38 | 39 | template 40 | MemoryPool::MemoryPool() 41 | throw() 42 | { 43 | currentBlock_ = 0; 44 | currentSlot_ = 0; 45 | lastSlot_ = 0; 46 | freeSlots_ = 0; 47 | } 48 | 49 | 50 | 51 | template 52 | MemoryPool::MemoryPool(const MemoryPool& memoryPool) 53 | throw() 54 | { 55 | MemoryPool(); 56 | } 57 | 58 | 59 | 60 | template 61 | template 62 | MemoryPool::MemoryPool(const MemoryPool& memoryPool) 63 | throw() 64 | { 65 | MemoryPool(); 66 | } 67 | 68 | 69 | 70 | template 71 | MemoryPool::~MemoryPool() 72 | throw() 73 | { 74 | slot_pointer_ curr = currentBlock_; 75 | while (curr != 0) { 76 | slot_pointer_ prev = curr->next; 77 | operator delete(reinterpret_cast(curr)); 78 | curr = prev; 79 | } 80 | } 81 | 82 | 83 | 84 | template 85 | inline typename MemoryPool::pointer 86 | MemoryPool::address(reference x) 87 | const throw() 88 | { 89 | return &x; 90 | } 91 | 92 | 93 | 94 | template 95 | inline typename MemoryPool::const_pointer 96 | MemoryPool::address(const_reference x) 97 | const throw() 98 | { 99 | return &x; 100 | } 101 | 102 | 103 | 104 | template 105 | void 106 | MemoryPool::allocateBlock() 107 | { 108 | // Allocate space for the new block and store a pointer to the previous one 109 | data_pointer_ newBlock = reinterpret_cast 110 | (operator new(BlockSize)); 111 | reinterpret_cast(newBlock)->next = currentBlock_; 112 | currentBlock_ = reinterpret_cast(newBlock); 113 | // Pad block body to staisfy the alignment requirements for elements 114 | data_pointer_ body = newBlock + sizeof(slot_pointer_); 115 | size_type bodyPadding = padPointer(body, sizeof(slot_type_)); 116 | currentSlot_ = reinterpret_cast(body + bodyPadding); 117 | lastSlot_ = reinterpret_cast 118 | (newBlock + BlockSize - sizeof(slot_type_) + 1); 119 | } 120 | 121 | 122 | 123 | template 124 | inline typename MemoryPool::pointer 125 | MemoryPool::allocate(size_type, const_pointer) 126 | { 127 | if (freeSlots_ != 0) { 128 | pointer result = reinterpret_cast(freeSlots_); 129 | freeSlots_ = freeSlots_->next; 130 | return result; 131 | } 132 | else { 133 | if (currentSlot_ >= lastSlot_) 134 | allocateBlock(); 135 | return reinterpret_cast(currentSlot_++); 136 | } 137 | } 138 | 139 | 140 | 141 | template 142 | inline void 143 | MemoryPool::deallocate(pointer p, size_type) 144 | { 145 | if (p != 0) { 146 | reinterpret_cast(p)->next = freeSlots_; 147 | freeSlots_ = reinterpret_cast(p); 148 | } 149 | } 150 | 151 | 152 | 153 | template 154 | inline typename MemoryPool::size_type 155 | MemoryPool::max_size() 156 | const throw() 157 | { 158 | size_type maxBlocks = -1 / BlockSize; 159 | return (BlockSize - sizeof(data_pointer_)) / sizeof(slot_type_) * maxBlocks; 160 | } 161 | 162 | 163 | 164 | template 165 | inline void 166 | MemoryPool::construct(pointer p, const_reference val) 167 | { 168 | new (p) value_type (val); 169 | } 170 | 171 | 172 | 173 | template 174 | inline void 175 | MemoryPool::destroy(pointer p) 176 | { 177 | p->~value_type(); 178 | } 179 | 180 | 181 | 182 | template 183 | inline typename MemoryPool::pointer 184 | MemoryPool::newElement(const_reference val) 185 | { 186 | pointer result = allocate(); 187 | construct(result, val); 188 | return result; 189 | } 190 | 191 | 192 | 193 | template 194 | inline void 195 | MemoryPool::deleteElement(pointer p) 196 | { 197 | if (p != 0) { 198 | p->~value_type(); 199 | deallocate(p); 200 | } 201 | } 202 | 203 | 204 | 205 | #endif // MEMORY_BLOCK_TCC 206 | -------------------------------------------------------------------------------- /include/Voronoi/src/MemoryPool/StackAlloc.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2013 Cosku Acay, http://www.coskuacay.com 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | */ 22 | 23 | 24 | /*- 25 | * A template class that implements a simple stack structure. 26 | * This demostrates how to use alloctor_traits (specifically with MemoryPool) 27 | */ 28 | 29 | #ifndef STACK_ALLOC_H 30 | #define STACK_ALLOC_H 31 | 32 | #include 33 | 34 | template 35 | struct StackNode_ 36 | { 37 | T data; 38 | StackNode_* prev; 39 | }; 40 | 41 | /** T is the object to store in the stack, Alloc is the allocator to use */ 42 | template > 43 | class StackAlloc 44 | { 45 | public: 46 | typedef StackNode_ Node; 47 | typedef typename Alloc::template rebind::other allocator; 48 | 49 | /** Default constructor */ 50 | StackAlloc() {head_ = 0; } 51 | /** Default destructor */ 52 | ~StackAlloc() { clear(); } 53 | 54 | /** Returns true if the stack is empty */ 55 | bool empty() {return (head_ == 0);} 56 | 57 | /** Deallocate all elements and empty the stack */ 58 | void clear() { 59 | Node* curr = head_; 60 | while (curr != 0) 61 | { 62 | Node* tmp = curr->prev; 63 | allocator_.destroy(curr); 64 | allocator_.deallocate(curr, 1); 65 | curr = tmp; 66 | } 67 | head_ = 0; 68 | } 69 | 70 | /** Put an element on the top of the stack */ 71 | void push(T element) { 72 | Node* newNode = allocator_.allocate(1); 73 | allocator_.construct(newNode, Node()); 74 | newNode->data = element; 75 | newNode->prev = head_; 76 | head_ = newNode; 77 | } 78 | 79 | /** Remove and return the topmost element on the stack */ 80 | T pop() { 81 | T result = head_->data; 82 | Node* tmp = head_->prev; 83 | allocator_.destroy(head_); 84 | allocator_.deallocate(head_, 1); 85 | head_ = tmp; 86 | return result; 87 | } 88 | 89 | /** Return the topmost element */ 90 | T top() { return (head_->data); } 91 | 92 | private: 93 | allocator allocator_; 94 | Node* head_; 95 | }; 96 | 97 | #endif // STACK_ALLOC_H 98 | -------------------------------------------------------------------------------- /include/Voronoi/src/MemoryPool/test.cpp: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2013 Cosku Acay, http://www.coskuacay.com 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and associated documentation files (the "Software"), 6 | * to deal in the Software without restriction, including without limitation 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the 9 | * Software is furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | */ 22 | 23 | /*- 24 | * Provided to compare the default allocator to MemoryPool 25 | * 26 | * Check out StackAlloc.h for a stack implementation that takes an allocator as 27 | * a template argument. This may give you some idea about how to use MemoryPool. 28 | * 29 | * This code basically creates two stacks: one using the default allocator and 30 | * one using the MemoryPool allocator. It pushes a bunch of objects in them and 31 | * then pops them out. We repeat the process several times and time how long 32 | * this takes for each of the stacks. 33 | * 34 | * Do not forget to turn on optimizations (use -O2 or -O3 for GCC). This is a 35 | * benchmark, we want inlined code. 36 | */ 37 | 38 | #include 39 | #include 40 | #include 41 | #include 42 | 43 | #include "MemoryPool.h" 44 | #include "StackAlloc.h" 45 | 46 | /* Adjust these values depending on how much you trust your computer */ 47 | #define ELEMS 1000000 48 | #define REPS 50 49 | 50 | int main() 51 | { 52 | clock_t start; 53 | 54 | std::cout << "Copyright (c) 2013 Cosku Acay, http://www.coskuacay.com\n"; 55 | std::cout << "Provided to compare the default allocator to MemoryPool.\n\n"; 56 | 57 | /* Use the default allocator */ 58 | StackAlloc > stackDefault; 59 | start = clock(); 60 | for (int j = 0; j < REPS; j++) 61 | { 62 | assert(stackDefault.empty()); 63 | for (int i = 0; i < ELEMS / 4; i++) { 64 | // Unroll to time the actual code and not the loop 65 | stackDefault.push(i); 66 | stackDefault.push(i); 67 | stackDefault.push(i); 68 | stackDefault.push(i); 69 | } 70 | for (int i = 0; i < ELEMS / 4; i++) { 71 | // Unroll to time the actual code and not the loop 72 | stackDefault.pop(); 73 | stackDefault.pop(); 74 | stackDefault.pop(); 75 | stackDefault.pop(); 76 | } 77 | } 78 | std::cout << "Default Allocator Time: "; 79 | std::cout << (((double)clock() - start) / CLOCKS_PER_SEC) << "\n\n"; 80 | 81 | /* Use MemoryPool */ 82 | StackAlloc > stackPool; 83 | start = clock(); 84 | for (int j = 0; j < REPS; j++) 85 | { 86 | assert(stackPool.empty()); 87 | for (int i = 0; i < ELEMS / 4; i++) { 88 | // Unroll to time the actual code and not the loop 89 | stackPool.push(i); 90 | stackPool.push(i); 91 | stackPool.push(i); 92 | stackPool.push(i); 93 | } 94 | for (int i = 0; i < ELEMS / 4; i++) { 95 | // Unroll to time the actual code and not the loop 96 | stackPool.pop(); 97 | stackPool.pop(); 98 | stackPool.pop(); 99 | stackPool.pop(); 100 | } 101 | } 102 | std::cout << "MemoryPool Allocator Time: "; 103 | std::cout << (((double)clock() - start) / CLOCKS_PER_SEC) << "\n\n"; 104 | 105 | 106 | std::cout << "Here is a secret: the best way of implementing a stack" 107 | " is a dynamic array.\n"; 108 | 109 | /* Compare MemoryPool to std::vector */ 110 | std::vector stackVector; 111 | start = clock(); 112 | for (int j = 0; j < REPS; j++) 113 | { 114 | assert(stackVector.empty()); 115 | for (int i = 0; i < ELEMS / 4; i++) { 116 | // Unroll to time the actual code and not the loop 117 | stackVector.push_back(i); 118 | stackVector.push_back(i); 119 | stackVector.push_back(i); 120 | stackVector.push_back(i); 121 | } 122 | for (int i = 0; i < ELEMS / 4; i++) { 123 | // Unroll to time the actual code and not the loop 124 | stackVector.pop_back(); 125 | stackVector.pop_back(); 126 | stackVector.pop_back(); 127 | stackVector.pop_back(); 128 | } 129 | } 130 | std::cout << "Vector Time: "; 131 | std::cout << (((double)clock() - start) / CLOCKS_PER_SEC) << "\n\n"; 132 | 133 | std::cout << "The vector implementation will probably be faster.\n\n"; 134 | std::cout << "MemoryPool still has a lot of uses though. Any type of tree" 135 | " and when you have multiple linked lists are some examples (they" 136 | " can all share the same memory pool).\n"; 137 | 138 | return 0; 139 | } 140 | -------------------------------------------------------------------------------- /include/Voronoi/src/VoronoiDiagramGenerator.cpp: -------------------------------------------------------------------------------- 1 | #include "../include/VoronoiDiagramGenerator.h" 2 | #include "Vector2.hpp" 3 | #include "Epsilon.h" 4 | #include 5 | #include 6 | using std::cout; 7 | using std::cin; 8 | using std::endl; 9 | 10 | void VoronoiDiagramGenerator::printBeachLine() { 11 | treeNode* section = beachLine->getFirst(beachLine->getRoot()); 12 | 13 | while (section) { 14 | cout << section->data.site->p.x << " " << section->data.site->p.y << endl; 15 | section = section->next; 16 | } 17 | if(section) cout << section->data.site->p.x << " " << section->data.site->p.y << endl; 18 | cout << endl << endl; 19 | } 20 | 21 | bool pointComparator(sf::Vector2* a, sf::Vector2* b) { 22 | double r = b->y - a->y; 23 | if (r < 0) return true; 24 | else if (r == 0) { 25 | if (b->x - a->x < 0) return true; 26 | else return false; 27 | } 28 | else return false; 29 | } 30 | 31 | Diagram* VoronoiDiagramGenerator::compute(std::vector>& sites, sf::Rect bbox) { 32 | siteEventQueue = new std::vector*>(); 33 | boundingBox = bbox; 34 | 35 | for (size_t i = 0; i < sites.size(); ++i) { 36 | //sanitize sites by quantizing to integer multiple of epsilon 37 | sites[i].x = round(sites[i].x / EPSILON)*EPSILON; 38 | sites[i].y = round(sites[i].y / EPSILON)*EPSILON; 39 | 40 | siteEventQueue->push_back(&(sites[i])); 41 | } 42 | 43 | diagram = new Diagram(); 44 | circleEventQueue = new CircleEventQueue(); 45 | beachLine = new RBTree(); 46 | 47 | // Initialize site event queue 48 | std::sort(siteEventQueue->begin(), siteEventQueue->end(), pointComparator); 49 | 50 | // process queue 51 | sf::Vector2* site = siteEventQueue->empty() ? nullptr : siteEventQueue->back(); 52 | if (!siteEventQueue->empty()) siteEventQueue->pop_back(); 53 | treeNode* circle; 54 | 55 | // main loop 56 | for (;;) { 57 | // figure out whether to handle a site or circle event 58 | // for this we find out if there is a site event and if it is 59 | // 'earlier' than the circle event 60 | circle = circleEventQueue->firstEvent; 61 | 62 | // add beach section 63 | if (site && (!circle || site->y < circle->data.y || (site->y == circle->data.y && site->x < circle->data.x))) { 64 | // first create cell for new site 65 | Cell* cell = diagram->createCell(*site); 66 | // then create a beachsection for that site 67 | addBeachSection(&cell->site); 68 | 69 | site = siteEventQueue->empty() ? nullptr : siteEventQueue->back(); 70 | if (!siteEventQueue->empty()) siteEventQueue->pop_back(); 71 | } 72 | 73 | // remove beach section 74 | else if (circle) 75 | removeBeachSection(circle->data.beachSection); 76 | 77 | // all done, quit 78 | else 79 | break; 80 | } 81 | 82 | // wrapping-up: 83 | // connect dangling edges to bounding box 84 | // cut edges as per bounding box 85 | // discard edges completely outside bounding box 86 | // discard edges which are point-like 87 | diagram->clipEdges(boundingBox); 88 | 89 | // add missing edges in order to close open cells 90 | diagram->closeCells(boundingBox); 91 | 92 | diagram->finalize(); 93 | 94 | delete circleEventQueue; 95 | circleEventQueue = nullptr; 96 | 97 | delete siteEventQueue; 98 | siteEventQueue = nullptr; 99 | 100 | delete beachLine; 101 | beachLine = nullptr; 102 | 103 | return diagram; 104 | } 105 | 106 | bool halfEdgesCW(HalfEdge* e1, HalfEdge* e2) { 107 | return e1->angle < e2->angle; 108 | } 109 | 110 | Diagram* VoronoiDiagramGenerator::relax() { 111 | std::vector> sites; 112 | std::vector> verts; 113 | std::vector> vectors; 114 | //replace each site with its cell's centroid: 115 | // subdivide the cell into adjacent triangles 116 | // find those triangles' centroids (by averaging corners) 117 | // and areas (by computing vector cross product magnitude) 118 | // combine the triangles' centroids through weighted average 119 | // to get the whole cell's centroid 120 | for (Cell* c : diagram->cells) { 121 | size_t edgeCount = c->halfEdges.size(); 122 | verts.resize(edgeCount); 123 | vectors.resize(edgeCount); 124 | 125 | for (size_t i = 0; i < edgeCount; ++i) { 126 | verts[i] = *c->halfEdges[i]->startPoint(); 127 | vectors[i] = *c->halfEdges[i]->startPoint() - verts[0]; 128 | } 129 | 130 | sf::Vector2 centroid(0.0, 0.0); 131 | double totalArea = 0.0; 132 | for (size_t i = 1; i < edgeCount-1; ++i) { 133 | double area = (vectors[i+1].x*vectors[i].y - vectors[i+1].y*vectors[i].x)/2; 134 | totalArea += area; 135 | centroid.x += area*(verts[0].x + verts[i].x + verts[i + 1].x) / 3; 136 | centroid.y += area*(verts[0].y + verts[i].y + verts[i + 1].y) / 3; 137 | } 138 | centroid.x /= totalArea; 139 | centroid.y /= totalArea; 140 | sites.push_back(centroid); 141 | } 142 | 143 | //then recompute the diagram using the cells' centroids 144 | compute(sites, boundingBox); 145 | 146 | return diagram; 147 | } -------------------------------------------------------------------------------- /include/libnoise.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/averrin/libmapgen/6b8dcaad87c9852f196e4ac16b14be2a1789fbcd/include/libnoise.dll -------------------------------------------------------------------------------- /include/libnoise.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/averrin/libmapgen/6b8dcaad87c9852f196e4ac16b14be2a1789fbcd/include/libnoise.lib -------------------------------------------------------------------------------- /include/mapgen/Biom.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BIOM_H_ 2 | #define BIOM_H_ 3 | #include 4 | #include 5 | #include 6 | 7 | struct Biom { 8 | float border; 9 | std::string name; 10 | float feritlity; 11 | bool operator==(const Biom& b) const { 12 | return b.name == name; 13 | } 14 | bool operator!=(const Biom& b) const { 15 | return b.name != name; 16 | } 17 | friend bool operator<(const Biom& l, const Biom& r) { 18 | return l.name < r.name; 19 | } 20 | }; 21 | 22 | namespace biom { 23 | const float DEFAULT_HUMIDITY = 0.f; 24 | const float DEFAULT_TEMPERATURE = 30.f; 25 | 26 | const Biom ABYSS = {-2.0000f, "Abyss", 0.f}; 27 | const Biom DEEP = {-1.0000f, "Deep", 0.f}; 28 | const Biom SHALLOW = {-0.2500f, "Shallow", 0.f}; 29 | const Biom SHORE = {0.0000f, "Shore", 0.f}; 30 | // const Biom SAND = {0.0625, "Sand", 0}; 31 | const Biom SAND = {0.0625f, "Sand", 0.f}; 32 | const Biom GRASS = {0.1250f, "Grass", 0.8f}; 33 | const Biom FORREST = {0.3750f, "Forrest", 0.6f}; 34 | const Biom ROCK = {0.7500f, "Rock", 0.f}; 35 | const Biom SNOW = {1.0000f, "Snow", 0.f}; 36 | const Biom ICE = {1.2000f, "Ice", 0.f}; 37 | const Biom PRAIRIE = {999.000f, "Prairie", 0.6f}; 38 | const Biom MEADOW = {999.000f, "Meadow", 1.f}; 39 | const Biom DESERT = {999.000f, "Desert", 0.f}; 40 | const Biom CITY = {999.000f, "City", 0.f}; 41 | 42 | const Biom RAIN_FORREST = {0.3750f, "Rain forrest", 0.6f}; 43 | 44 | const std::vector BIOMS = { 45 | {ABYSS, DEEP, SHALLOW, SHORE, SAND, GRASS, FORREST, ROCK, SNOW, ICE}}; 46 | 47 | const Biom LAKE = {999.000f, "Lake", 0.f}; 48 | const Biom MARK = {999.000f, "Mark"}; 49 | const Biom MARK2 = {999.000f, "Mark"}; 50 | 51 | const Biom LAND = {0.500f, "Land", 0.f}; 52 | const Biom SEA = {-1.000f, "Sea", 0.f}; 53 | 54 | const std::vector> BIOMS_BY_HEIGHT = {{ 55 | {ABYSS}, 56 | {DEEP}, 57 | {SHALLOW}, 58 | {SHORE}, 59 | {SAND}, 60 | {MEADOW, GRASS, PRAIRIE, SAND}, 61 | {RAIN_FORREST, FORREST, GRASS, PRAIRIE, SAND}, 62 | {ROCK}, 63 | {SNOW, ROCK}, 64 | {ICE}, 65 | }}; 66 | 67 | const std::map BIOMS_BY_TEMP = {{{SAND.name, DESERT}, 68 | {PRAIRIE.name, DESERT}, 69 | {GRASS.name, PRAIRIE}, 70 | {MEADOW.name, GRASS}}}; 71 | } 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /include/mapgen/City.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CITY_H_ 2 | #define CITY_H_ 3 | 4 | #include "Region.hpp" 5 | #include "Location.hpp" 6 | #include "Road.hpp" 7 | #include "mapgen/Economy.hpp" 8 | 9 | class Package; 10 | class City : public Location { 11 | public: 12 | City(Region* r, std::string n, LocationType t); 13 | Package* makeGoods(int y); 14 | std::pair buyGoods(std::vector* goods); 15 | EconomyVars* economyVars = nullptr; 16 | 17 | bool isCapital = false; 18 | 19 | int population = 1000; 20 | float wealth = 1.f; 21 | std::vector roads; 22 | std::map cache; 23 | float getPrice(Package* p); 24 | private: 25 | friend std::ostream& operator<<(std::ostream &strm, const City &c); 26 | }; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /include/mapgen/Economy.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ECONOMY_H_ 2 | #define ECONOMY_H_ 3 | 4 | namespace Economy { 5 | const float POPULATION_GROWS = 0.2f; 6 | const float POPULATION_GROWS_WEALTH_MODIFIER = 0.15f; 7 | 8 | const float PACKAGES_PER_NICE = 20.f; 9 | const float PACKAGES_AGRO_POPULATION_MODIFIER = 0.04f; 10 | 11 | const float PACKAGES_PER_MINERALS = 15.f; 12 | const float PACKAGES_MINERALS_POPULATION_MODIFIER = 0.03f; 13 | 14 | const float CONSUME_AGRO_POPULATION_MODIFIER = 0.5f; 15 | const float CONSUME_MINERALS_POPULATION_MODIFIER = 0.5f; 16 | 17 | const float CONSUME_AGRO_WEALTH_MODIFIER = 0.1f; 18 | const float CONSUME_MINERALS_WEALTH_MODIFIER = 0.1f; 19 | 20 | const float CANT_BUY_AGRO = 0.3f; 21 | const float CANT_BUY_MINERALS = 0.3f; 22 | 23 | const float MINERALS_POPULATION_PRODUCE = 0.3f; 24 | const float AGRO_POPULATION_PRODUCE = 0.4f; 25 | 26 | const float PORT_FEE = 0.3f; 27 | const float PRICE_CORRECTION = 0.1f; 28 | } 29 | 30 | class EconomyVars { 31 | public: 32 | float POPULATION_GROWS; 33 | float POPULATION_GROWS_WEALTH_MODIFIER; 34 | 35 | float PACKAGES_PER_NICE; 36 | float PACKAGES_AGRO_POPULATION_MODIFIER; 37 | 38 | float PACKAGES_PER_MINERALS; 39 | float PACKAGES_MINERALS_POPULATION_MODIFIER; 40 | 41 | float CONSUME_AGRO_POPULATION_MODIFIER; 42 | float CONSUME_MINERALS_POPULATION_MODIFIER; 43 | 44 | float CONSUME_AGRO_WEALTH_MODIFIER; 45 | float CONSUME_MINERALS_WEALTH_MODIFIER; 46 | 47 | float CANT_BUY_AGRO; 48 | float CANT_BUY_MINERALS; 49 | 50 | float MINERALS_POPULATION_PRODUCE; 51 | float AGRO_POPULATION_PRODUCE; 52 | 53 | float PORT_FEE; 54 | float PRICE_CORRECTION; 55 | 56 | EconomyVars(); 57 | }; 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /include/mapgen/Location.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LOCATION_H_ 2 | #define LOCATION_H_ 3 | 4 | #include "Region.hpp" 5 | 6 | enum LocationType { 7 | CAPITAL, 8 | PORT, 9 | MINE, 10 | AGRO, 11 | TRADE, 12 | LIGHTHOUSE, 13 | CAVE, 14 | FORT 15 | }; 16 | 17 | class Location { 18 | public: 19 | Location(Region* r, std::string n, LocationType t); 20 | Region* region; 21 | std::string name; 22 | LocationType type; 23 | std::string typeName; 24 | }; 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /include/mapgen/Map.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MAP_H_ 2 | #define MAP_H_ 3 | #include "City.hpp" 4 | #include "Region.hpp" 5 | #include "River.hpp" 6 | #include "Road.hpp" 7 | #include "micropather.h" 8 | #include 9 | 10 | class Map : public micropather::Graph { 11 | public: 12 | ~Map(); 13 | 14 | std::vector megaClusters; 15 | std::vector clusters; 16 | std::vector stateClusters; 17 | 18 | std::vector states; 19 | std::vector regions; 20 | std::vector rivers; 21 | std::vector cities; 22 | std::vector locations; 23 | std::vector roads; 24 | std::map, Road*> roadMap; 25 | 26 | std::string status = ""; 27 | 28 | float getRegionDistance(Region *r, Region *r2); 29 | float LeastCostEstimate(void *stateStart, void *stateEnd); 30 | void AdjacentCost(void *state, MP_VECTOR *adjacent); 31 | void PrintStateInfo(void *state); 32 | 33 | }; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /include/mapgen/MapGenerator.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MAPGEN_H_ 2 | #define MAPGEN_H_ 3 | 4 | #include "noise/noise.h" 5 | #include "noise/noiseutils.h" 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "Region.hpp" 12 | #include "Simulator.hpp" 13 | #include "State.hpp" 14 | #include "WeatherManager.hpp" 15 | #include "micropather.h" 16 | 17 | typedef std::function sameFunc; 18 | typedef std::function assignFunc; 19 | typedef std::function *)> 21 | reassignFunc; 22 | typedef std::function createFunc; 23 | 24 | class MapGenerator { 25 | public: 26 | MapGenerator(int w, int h); 27 | 28 | void build(); 29 | void update(); 30 | void forceUpdate(); 31 | void relax(); 32 | void setSeed(int seed); 33 | void setOctaveCount(int octaveCount); 34 | void setSize(int w, int h); 35 | void setFrequency(float freq); 36 | void setPointCount(int count); 37 | int getPointCount(); 38 | int getOctaveCount(); 39 | int getRelax(); 40 | float getFrequency(); 41 | int getSeed(); 42 | Region *getRegion(Region* r, sf::Vector2f pos); 43 | void seed(); 44 | std::vector getRegions(); 45 | void setMapTemplate(const char *t); 46 | void startSimulation(); 47 | 48 | bool simpleRivers; 49 | bool ready; 50 | Map *map; 51 | Simulator *simulator; 52 | std::unique_ptr weather; 53 | 54 | template Iter select_randomly(Iter start, Iter end); 55 | 56 | std::mt19937 *_gen; 57 | 58 | private: 59 | void makeHeights(); 60 | void makeDiagram(); 61 | void makeRegions(); 62 | void makeFinalRegions(); 63 | void makeRivers(); 64 | void makeClusters(); 65 | void makeMegaClusters(); 66 | void makeRelax(); 67 | void makeRiver(Region *r); 68 | void calcHumidity(); 69 | void calcTemp(); 70 | void simplifyRivers(); 71 | void makeBorders(); 72 | void makeMinerals(); 73 | void makeCities(); 74 | void makeStates(); 75 | 76 | void getSea(std::vector *seas, Region *base, Region *r); 77 | int _seed; 78 | VoronoiDiagramGenerator _vdg; 79 | int _pointsCount; 80 | int _w; 81 | int _h; 82 | int _relax; 83 | int _octaves; 84 | float _freq; 85 | sf::Rect _bbox; 86 | std::vector> *_sites; 87 | std::map _cells; 88 | std::unique_ptr _diagram; 89 | Cell *_highestCell; 90 | std::map cellsMap; 91 | std::vector states; 92 | 93 | micropather::MicroPather *_pather; 94 | module::Perlin _perlin; 95 | utils::NoiseMap _heightMap; 96 | utils::NoiseMap _mineralsMap; 97 | std::string _terrainType; 98 | 99 | void genRandomSites(std::vector> &sites, 100 | sf::Rect &bbox, unsigned int dx, unsigned int dy, 101 | unsigned int numSites); 102 | 103 | std::vector clusterize(std::vector regions, 104 | sameFunc isSame, assignFunc assignCluster, 105 | reassignFunc reassignCluster, 106 | createFunc createCluster); 107 | }; 108 | 109 | #endif 110 | -------------------------------------------------------------------------------- /include/mapgen/Package.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PACKAGE_H_ 2 | #define PACKAGE_H_ 3 | 4 | #include "City.hpp" 5 | 6 | enum PackageType { 7 | MINERALS, 8 | AGROCULTURE 9 | }; 10 | 11 | class Package { 12 | public: 13 | Package (City* owner, PackageType type, unsigned int count); 14 | City* owner; 15 | std::vector ports; 16 | PackageType type; 17 | unsigned int count = 0; 18 | void buy(City* buyer, float price, unsigned int c); 19 | }; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /include/mapgen/Region.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REGION_H_ 2 | #define REGION_H_ 3 | 4 | #include 5 | #include "Biom.hpp" 6 | #include "State.hpp" 7 | #include 8 | 9 | typedef sf::Vector2* Point; 10 | typedef std::vector PointList; 11 | typedef std::map HeightMap; 12 | 13 | struct Cluster; 14 | typedef Cluster MegaCluster; 15 | typedef Cluster StateCluster; 16 | class City; 17 | class Location; 18 | class Region { 19 | public: 20 | Region(); 21 | Region(Biom b, PointList v, HeightMap h, Point s); 22 | PointList getPoints(); 23 | float getHeight(Point p); 24 | Biom biom; 25 | Point site; 26 | bool hasRiver = false; 27 | Cluster *cluster = nullptr; 28 | Cluster *stateCluster = nullptr; 29 | MegaCluster *megaCluster = nullptr; 30 | bool border = false; 31 | float humidity = 0.f; 32 | Cell* cell = nullptr; 33 | float temperature = 0.f; 34 | float minerals = 0.f; 35 | float nice = 0.f; 36 | float windForce = 0.f; 37 | City* city = nullptr; 38 | std::vector neighbors; 39 | bool hasRoad = false; 40 | int traffic = 0; 41 | Location* location = nullptr; 42 | State* state = nullptr; 43 | bool stateBorder = false; 44 | bool seaBorder = false; 45 | bool isCoast(); 46 | bool isLakeCoast(); 47 | 48 | Region* getRegionWithDirection(float angle, float force); 49 | 50 | private: 51 | PointList _verticies; 52 | HeightMap _heights; 53 | }; 54 | 55 | struct Cluster { 56 | std::string name = ""; 57 | std::vector regions; 58 | std::vector neighbors; 59 | MegaCluster* megaCluster = nullptr; 60 | Biom biom; 61 | bool hasRiver = false;; 62 | bool isLand = false;; 63 | Point* center = nullptr; 64 | PointList border; 65 | std::vector resourcePoints; 66 | std::vector goodPoints; 67 | std::vector cities; 68 | bool hasPort = false; 69 | std::vector states; 70 | }; 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /include/mapgen/Report.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REPORT_H_ 2 | #define REPORT_H_ 3 | #include 4 | #include 5 | 6 | class Report { 7 | public: 8 | Report(); 9 | std::vector population; 10 | std::vector wealth; 11 | int minPopulation = std::numeric_limits::max(); 12 | int maxPopulation = 0; 13 | float minWealth = std::numeric_limits::max(); 14 | float maxWealth = 0; 15 | }; 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /include/mapgen/River.hpp: -------------------------------------------------------------------------------- 1 | #ifndef RIVER_H_ 2 | #define RIVER_H_ 3 | 4 | #include "Region.hpp" 5 | 6 | struct River { 7 | std::string name; 8 | PointList* points; 9 | std::vector regions; 10 | }; 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /include/mapgen/Road.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ROAD_H_ 2 | #define ROAD_H_ 3 | 4 | #include "Region.hpp" 5 | #include "micropather.h" 6 | 7 | class Road { 8 | public: 9 | Road(); 10 | Road(micropather::MPVector* path, float c); 11 | std::vector regions; 12 | float cost; 13 | bool seaPath = false; 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /include/mapgen/Simulator.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SIM_H_ 2 | #define SIM_H_ 3 | #include "mapgen/Map.hpp" 4 | #include "mapgen/Economy.hpp" 5 | #include "mapgen/Report.hpp" 6 | #include 7 | 8 | class Simulator{ 9 | public: 10 | Simulator(Map* m, int s); 11 | void simulate(); 12 | void resetAll(); 13 | 14 | int years = 50; 15 | EconomyVars* vars; 16 | 17 | Report* report; 18 | 19 | private: 20 | void makeRoads(); 21 | void makeCaves(); 22 | void upgradeCities(); 23 | void removeBadPorts(); 24 | void makeLighthouses(); 25 | void makeLocationRoads(); 26 | void makeForts(); 27 | void fixRoads(); 28 | void removeCities(); 29 | 30 | void simulateEconomy(); 31 | void economyTick(int y); 32 | void populationTick(int y); 33 | void disasterTick(int y); 34 | 35 | template 36 | Iter select_randomly(Iter start, Iter end); 37 | bool plague = false; 38 | 39 | Map* map; 40 | std::mt19937* _gen; 41 | int _seed; 42 | micropather::MicroPather* _pather; 43 | }; 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /include/mapgen/State.hpp: -------------------------------------------------------------------------------- 1 | #ifndef STATE_H_ 2 | #define STATE_H_ 3 | #include 4 | 5 | class State { 6 | public: 7 | State(std::string n, Cell* c); 8 | std::string name; 9 | Cell* cell; 10 | }; 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /include/mapgen/WeatherManager.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "Region.hpp" 4 | #include "Biom.hpp" 5 | 6 | class WeatherManager { 7 | public: 8 | WeatherManager(); 9 | void calcTemp(std::vector r); 10 | void calcHumidity(std::vector r); 11 | void genWind(); 12 | 13 | 14 | float temperature = biom::DEFAULT_TEMPERATURE; 15 | float windForce; 16 | float windAngle; 17 | }; 18 | -------------------------------------------------------------------------------- /include/mapgen/names.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NAMES_HPP_ 2 | #define NAMES_HPP_ 3 | 4 | namespace names { 5 | std::string generateRiverName(std::mt19937 *gen); 6 | std::string generateLandName(std::mt19937 *gen); 7 | std::string generateSeaName(std::mt19937 *gen); 8 | std::string generateCityName(std::mt19937 *gen); 9 | }; 10 | #endif 11 | -------------------------------------------------------------------------------- /include/mapgen/utils.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_HPP_ 2 | #define UTILS_HPP_ 3 | #include 4 | #include "mapgen/City.hpp" 5 | 6 | #include 7 | namespace fs = std::experimental::filesystem; 8 | 9 | typedef sf::Vector2* Point; 10 | namespace mg { 11 | template using filterFunc = std::function; 12 | template using sortFunc = std::function; 13 | 14 | 15 | double getDistance(Point p, Point p2); 16 | 17 | template 18 | std::vector filterObjects(std::vector regions, 19 | std::function filter, std::function sort); 20 | 21 | void before(std::string method); 22 | void after(std::string method); 23 | void info(std::string prefix, std::string value); 24 | void info(std::string prefix, int value); 25 | void info(std::string prefix, City value); 26 | void info(std::string prefix, fs::path value); 27 | void warn(std::string prefix, std::string value); 28 | void warn(std::string prefix, int value); 29 | void warn(std::string prefix, City value); 30 | 31 | template< typename ContainerT, typename PredicateT > 32 | void erase_if( ContainerT& items, const PredicateT& predicate ) { 33 | for( auto it = items.begin(); it != items.end(); ) { 34 | if( predicate(*it) ) it = items.erase(it); 35 | else ++it; 36 | } 37 | }; 38 | }; 39 | #endif 40 | -------------------------------------------------------------------------------- /include/noise/basictypes.h: -------------------------------------------------------------------------------- 1 | // basictypes.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_BASICTYPES_H 24 | #define NOISE_BASICTYPES_H 25 | 26 | // You may need to modify these constants for your compiler or platform. 27 | 28 | namespace noise 29 | { 30 | 31 | /// @defgroup libnoise libnoise 32 | /// @addtogroup libnoise 33 | /// @{ 34 | 35 | /// Unsigned integer type. 36 | typedef unsigned int uint; 37 | 38 | /// 32-bit unsigned integer type. 39 | typedef unsigned int uint32; 40 | 41 | /// 16-bit unsigned integer type. 42 | typedef unsigned short uint16; 43 | 44 | /// 8-bit unsigned integer type. 45 | typedef unsigned char uint8; 46 | 47 | /// 32-bit signed integer type. 48 | typedef int int32; 49 | 50 | /// 16-bit signed integer type. 51 | typedef short int16; 52 | 53 | /// 8-bit signed integer type. 54 | typedef char int8; 55 | 56 | /// @} 57 | 58 | } 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /include/noise/exception.h: -------------------------------------------------------------------------------- 1 | // exception.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_EXCEPTION_H 24 | #define NOISE_EXCEPTION_H 25 | 26 | namespace noise 27 | { 28 | 29 | /// @addtogroup libnoise 30 | /// @{ 31 | 32 | /// Abstract base class for libnoise exceptions 33 | class Exception 34 | { 35 | }; 36 | 37 | /// Invalid parameter exception 38 | /// 39 | /// An invalid parameter was passed to a libnoise function or method. 40 | class ExceptionInvalidParam: public Exception 41 | { 42 | }; 43 | 44 | /// No module exception 45 | /// 46 | /// Could not retrieve a source module from a noise module. 47 | /// 48 | /// @note If one or more required source modules were not connected to a 49 | /// specific noise module, and its GetValue() method was called, that 50 | /// method will raise a debug assertion instead of this exception. This 51 | /// is done for performance reasons. 52 | class ExceptionNoModule: public Exception 53 | { 54 | }; 55 | 56 | /// Out of memory exception 57 | /// 58 | /// There was not enough memory to perform an action. 59 | class ExceptionOutOfMemory: public Exception 60 | { 61 | }; 62 | 63 | /// Unknown exception 64 | /// 65 | /// libnoise raised an unknown exception. 66 | class ExceptionUnknown: public Exception 67 | { 68 | }; 69 | 70 | /// @} 71 | 72 | } 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /include/noise/interp.h: -------------------------------------------------------------------------------- 1 | // interp.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_INTERP_H 24 | #define NOISE_INTERP_H 25 | 26 | namespace noise 27 | { 28 | 29 | /// @addtogroup libnoise 30 | /// @{ 31 | 32 | /// Performs cubic interpolation between two values bound between two other 33 | /// values. 34 | /// 35 | /// @param n0 The value before the first value. 36 | /// @param n1 The first value. 37 | /// @param n2 The second value. 38 | /// @param n3 The value after the second value. 39 | /// @param a The alpha value. 40 | /// 41 | /// @returns The interpolated value. 42 | /// 43 | /// The alpha value should range from 0.0 to 1.0. If the alpha value is 44 | /// 0.0, this function returns @a n1. If the alpha value is 1.0, this 45 | /// function returns @a n2. 46 | inline double CubicInterp (double n0, double n1, double n2, double n3, 47 | double a) 48 | { 49 | double p = (n3 - n2) - (n0 - n1); 50 | double q = (n0 - n1) - p; 51 | double r = n2 - n0; 52 | double s = n1; 53 | return p * a * a * a + q * a * a + r * a + s; 54 | } 55 | 56 | /// Performs linear interpolation between two values. 57 | /// 58 | /// @param n0 The first value. 59 | /// @param n1 The second value. 60 | /// @param a The alpha value. 61 | /// 62 | /// @returns The interpolated value. 63 | /// 64 | /// The alpha value should range from 0.0 to 1.0. If the alpha value is 65 | /// 0.0, this function returns @a n0. If the alpha value is 1.0, this 66 | /// function returns @a n1. 67 | inline double LinearInterp (double n0, double n1, double a) 68 | { 69 | return ((1.0 - a) * n0) + (a * n1); 70 | } 71 | 72 | /// Maps a value onto a cubic S-curve. 73 | /// 74 | /// @param a The value to map onto a cubic S-curve. 75 | /// 76 | /// @returns The mapped value. 77 | /// 78 | /// @a a should range from 0.0 to 1.0. 79 | /// 80 | /// The derivitive of a cubic S-curve is zero at @a a = 0.0 and @a a = 81 | /// 1.0 82 | inline double SCurve3 (double a) 83 | { 84 | return (a * a * (3.0 - 2.0 * a)); 85 | } 86 | 87 | /// Maps a value onto a quintic S-curve. 88 | /// 89 | /// @param a The value to map onto a quintic S-curve. 90 | /// 91 | /// @returns The mapped value. 92 | /// 93 | /// @a a should range from 0.0 to 1.0. 94 | /// 95 | /// The first derivitive of a quintic S-curve is zero at @a a = 0.0 and 96 | /// @a a = 1.0 97 | /// 98 | /// The second derivitive of a quintic S-curve is zero at @a a = 0.0 and 99 | /// @a a = 1.0 100 | inline double SCurve5 (double a) 101 | { 102 | double a3 = a * a * a; 103 | double a4 = a3 * a; 104 | double a5 = a4 * a; 105 | return (6.0 * a5) - (15.0 * a4) + (10.0 * a3); 106 | } 107 | 108 | // @} 109 | 110 | } 111 | 112 | #endif 113 | -------------------------------------------------------------------------------- /include/noise/latlon.h: -------------------------------------------------------------------------------- 1 | // latlon.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_LATLON_H 24 | #define NOISE_LATLON_H 25 | 26 | #include 27 | #include "mathconsts.h" 28 | 29 | namespace noise 30 | { 31 | 32 | /// @addtogroup libnoise 33 | /// @{ 34 | 35 | /// Converts latitude/longitude coordinates on a unit sphere into 3D 36 | /// Cartesian coordinates. 37 | /// 38 | /// @param lat The latitude, in degrees. 39 | /// @param lon The longitude, in degrees. 40 | /// @param x On exit, this parameter contains the @a x coordinate. 41 | /// @param y On exit, this parameter contains the @a y coordinate. 42 | /// @param z On exit, this parameter contains the @a z coordinate. 43 | /// 44 | /// @pre lat must range from @b -90 to @b +90. 45 | /// @pre lon must range from @b -180 to @b +180. 46 | void LatLonToXYZ (double lat, double lon, double& x, double& y, double& z); 47 | 48 | /// @} 49 | 50 | } 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /include/noise/libnoise.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/averrin/libmapgen/6b8dcaad87c9852f196e4ac16b14be2a1789fbcd/include/noise/libnoise.dll -------------------------------------------------------------------------------- /include/noise/libnoise.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/averrin/libmapgen/6b8dcaad87c9852f196e4ac16b14be2a1789fbcd/include/noise/libnoise.lib -------------------------------------------------------------------------------- /include/noise/mathconsts.h: -------------------------------------------------------------------------------- 1 | // mathconsts.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MATHCONSTS_H 24 | #define NOISE_MATHCONSTS_H 25 | 26 | // For whatever reason, I can't find the basic math consts in the MSVC version 27 | // of math.h. 28 | 29 | namespace noise 30 | { 31 | 32 | /// @addtogroup libnoise 33 | /// @{ 34 | 35 | /// Pi. 36 | const double PI = 3.1415926535897932385; 37 | 38 | /// Square root of 2. 39 | const double SQRT_2 = 1.4142135623730950488; 40 | 41 | /// Square root of 3. 42 | const double SQRT_3 = 1.7320508075688772935; 43 | 44 | /// Converts an angle from degrees to radians. 45 | const double DEG_TO_RAD = PI / 180.0; 46 | 47 | /// Converts an angle from radians to degrees. 48 | const double RAD_TO_DEG = 1.0 / DEG_TO_RAD; 49 | 50 | /// @} 51 | 52 | } 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /include/noise/misc.h: -------------------------------------------------------------------------------- 1 | // misc.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MISC_H 24 | #define NOISE_MISC_H 25 | 26 | namespace noise 27 | { 28 | 29 | /// Clamps a value onto a clamping range. 30 | /// 31 | /// @param value The value to clamp. 32 | /// @param lowerBound The lower bound of the clamping range. 33 | /// @param upperBound The upper bound of the clamping range. 34 | /// 35 | /// @returns 36 | /// - @a value if @a value lies between @a lowerBound and @a upperBound. 37 | /// - @a lowerBound if @a value is less than @a lowerBound. 38 | /// - @a upperBound if @a value is greater than @a upperBound. 39 | /// 40 | /// This function does not modify any parameters. 41 | inline int ClampValue (int value, int lowerBound, int upperBound) 42 | { 43 | if (value < lowerBound) { 44 | return lowerBound; 45 | } else if (value > upperBound) { 46 | return upperBound; 47 | } else { 48 | return value; 49 | } 50 | } 51 | 52 | /// @addtogroup libnoise 53 | /// @{ 54 | 55 | /// Returns the maximum of two values. 56 | /// 57 | /// @param a The first value. 58 | /// @param b The second value. 59 | /// 60 | /// @returns The maximum of the two values. 61 | template 62 | T GetMax (const T& a, const T& b) 63 | { 64 | return (a > b? a: b); 65 | } 66 | 67 | /// Returns the minimum of two values. 68 | /// 69 | /// @param a The first value. 70 | /// @param b The second value. 71 | /// 72 | /// @returns The minimum of the two values. 73 | template 74 | T GetMin (const T& a, const T& b) 75 | { 76 | return (a < b? a: b); 77 | } 78 | 79 | /// Swaps two values. 80 | /// 81 | /// @param a A variable containing the first value. 82 | /// @param b A variable containing the second value. 83 | /// 84 | /// @post The values within the the two variables are swapped. 85 | template 86 | void SwapValues (T& a, T& b) 87 | { 88 | T c = a; 89 | a = b; 90 | b = c; 91 | } 92 | 93 | /// @} 94 | 95 | } 96 | 97 | #endif 98 | -------------------------------------------------------------------------------- /include/noise/model/cylinder.h: -------------------------------------------------------------------------------- 1 | // cylinder.h 2 | // 3 | // Copyright 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODEL_CYLINDER_H 24 | #define NOISE_MODEL_CYLINDER_H 25 | 26 | #include 27 | #include 28 | #include 29 | #include "../module/modulebase.h" 30 | 31 | namespace noise 32 | { 33 | 34 | namespace model 35 | { 36 | 37 | /// @addtogroup libnoise 38 | /// @{ 39 | 40 | /// @defgroup models Models 41 | /// @addtogroup models 42 | /// @{ 43 | 44 | /// Model that defines the surface of a cylinder. 45 | /// 46 | /// @image html modelcylinder.png 47 | /// 48 | /// This model returns an output value from a noise module given the 49 | /// coordinates of an input value located on the surface of a cylinder. 50 | /// 51 | /// To generate an output value, pass the (angle, height) coordinates of 52 | /// an input value to the GetValue() method. 53 | /// 54 | /// This model is useful for creating: 55 | /// - seamless textures that can be mapped onto a cylinder 56 | /// 57 | /// This cylinder has a radius of 1.0 unit and has infinite height. It is 58 | /// oriented along the @a y axis. Its center is located at the origin. 59 | class Cylinder 60 | { 61 | 62 | public: 63 | 64 | /// Constructor. 65 | Cylinder (); 66 | 67 | /// Constructor 68 | /// 69 | /// @param module The noise module that is used to generate the output 70 | /// values. 71 | Cylinder (const module::Module& module); 72 | 73 | /// Returns the noise module that is used to generate the output 74 | /// values. 75 | /// 76 | /// @returns A reference to the noise module. 77 | /// 78 | /// @pre A noise module was passed to the SetModule() method. 79 | const module::Module& GetModule () const 80 | { 81 | assert (m_pModule != NULL); 82 | return *m_pModule; 83 | } 84 | 85 | /// Returns the output value from the noise module given the 86 | /// (angle, height) coordinates of the specified input value located 87 | /// on the surface of the cylinder. 88 | /// 89 | /// @param angle The angle around the cylinder's center, in degrees. 90 | /// @param height The height along the @a y axis. 91 | /// 92 | /// @returns The output value from the noise module. 93 | /// 94 | /// @pre A noise module was passed to the SetModule() method. 95 | /// 96 | /// This output value is generated by the noise module passed to the 97 | /// SetModule() method. 98 | /// 99 | /// This cylinder has a radius of 1.0 unit and has infinite height. 100 | /// It is oriented along the @a y axis. Its center is located at the 101 | /// origin. 102 | double GetValue (double angle, double height) const; 103 | 104 | /// Sets the noise module that is used to generate the output values. 105 | /// 106 | /// @param module The noise module that is used to generate the output 107 | /// values. 108 | /// 109 | /// This noise module must exist for the lifetime of this object, 110 | /// until you pass a new noise module to this method. 111 | void SetModule (const module::Module& module) 112 | { 113 | m_pModule = &module; 114 | } 115 | 116 | private: 117 | 118 | /// A pointer to the noise module used to generate the output values. 119 | const module::Module* m_pModule; 120 | 121 | }; 122 | 123 | /// @} 124 | 125 | /// @} 126 | 127 | } 128 | 129 | } 130 | 131 | #endif 132 | -------------------------------------------------------------------------------- /include/noise/model/line.h: -------------------------------------------------------------------------------- 1 | // line.h 2 | // 3 | // Copyright (C) 2004 Keith Davies 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | 20 | #ifndef NOISE_MODEL_LINE_H 21 | #define NOISE_MODEL_LINE_H 22 | 23 | #include 24 | #include 25 | #include 26 | #include "../module/modulebase.h" 27 | 28 | namespace noise 29 | { 30 | 31 | namespace model 32 | { 33 | 34 | /// @addtogroup libnoise 35 | /// @{ 36 | 37 | /// @addtogroup models 38 | /// @{ 39 | 40 | /// Model that defines the displacement of a line segment. 41 | /// 42 | /// This model returns an output value from a noise module given the 43 | /// one-dimensional coordinate of an input value located on a line 44 | /// segment, which can be used as displacements. 45 | /// 46 | /// This class is useful for creating: 47 | /// - roads and rivers 48 | /// - disaffected college students 49 | /// 50 | /// To generate an output value, pass an input value between 0.0 and 1.0 51 | /// to the GetValue() method. 0.0 represents the start position of the 52 | /// line segment and 1.0 represents the end position of the line segment. 53 | class Line 54 | { 55 | 56 | public: 57 | 58 | /// Constructor. 59 | Line (); 60 | 61 | /// Constructor 62 | /// 63 | /// @param module The noise module that is used to generate the output 64 | /// values. 65 | Line (const module::Module& module); 66 | 67 | /// Returns a flag indicating whether the output value is to be 68 | /// attenuated (moved toward 0.0) as the ends of the line segment are 69 | /// approached by the input value. 70 | /// 71 | /// @returns 72 | /// - @a true if the value is to be attenuated 73 | /// - @a false if not. 74 | bool GetAttenuate () const 75 | { 76 | return m_attenuate; 77 | } 78 | 79 | /// Returns the noise module that is used to generate the output 80 | /// values. 81 | /// 82 | /// @returns A reference to the noise module. 83 | /// 84 | /// @pre A noise module was passed to the SetModule() method. 85 | const module::Module& GetModule () const 86 | { 87 | assert (m_pModule != NULL); 88 | return *m_pModule; 89 | } 90 | 91 | /// Returns the output value from the noise module given the 92 | /// one-dimensional coordinate of the specified input value located 93 | /// on the line segment. 94 | /// 95 | /// @param p The distance along the line segment (ranges from 0.0 96 | /// to 1.0) 97 | /// 98 | /// @returns The output value from the noise module. 99 | /// 100 | /// @pre A noise module was passed to the SetModule() method. 101 | /// @pre The start and end points of the line segment were specified. 102 | /// 103 | /// The output value is generated by the noise module passed to the 104 | /// SetModule() method. This value may be attenuated (moved toward 105 | /// 0.0) as @a p approaches either end of the line segment; this is 106 | /// the default behavior. 107 | /// 108 | /// If the value is not to be attenuated, @a p can safely range 109 | /// outside the 0.0 to 1.0 range; the output value will be 110 | /// extrapolated along the line that this segment is part of. 111 | double GetValue (double p) const; 112 | 113 | /// Sets a flag indicating that the output value is to be attenuated 114 | /// (moved toward 0.0) as the ends of the line segment are approached. 115 | /// 116 | /// @param att A flag that specifies whether the output value is to be 117 | /// attenuated. 118 | void SetAttenuate (bool att) 119 | { 120 | m_attenuate = att; 121 | } 122 | 123 | /// Sets the position ( @a x, @a y, @a z ) of the end of the line 124 | /// segment to choose values along. 125 | /// 126 | /// @param x x coordinate of the end position. 127 | /// @param y y coordinate of the end position. 128 | /// @param z z coordinate of the end position. 129 | void SetEndPoint (double x, double y, double z) 130 | { 131 | m_x1 = x; 132 | m_y1 = y; 133 | m_z1 = z; 134 | } 135 | 136 | /// Sets the noise module that is used to generate the output values. 137 | /// 138 | /// @param module The noise module that is used to generate the output 139 | /// values. 140 | /// 141 | /// This noise module must exist for the lifetime of this object, 142 | /// until you pass a new noise module to this method. 143 | void SetModule (const module::Module& module) 144 | { 145 | m_pModule = &module; 146 | } 147 | 148 | /// Sets the position ( @a x, @a y, @a z ) of the start of the line 149 | /// segment to choose values along. 150 | /// 151 | /// @param x x coordinate of the start position. 152 | /// @param y y coordinate of the start position. 153 | /// @param z z coordinate of the start position. 154 | void SetStartPoint (double x, double y, double z) 155 | { 156 | m_x0 = x; 157 | m_y0 = y; 158 | m_z0 = z; 159 | } 160 | 161 | private: 162 | 163 | /// A flag that specifies whether the value is to be attenuated 164 | /// (moved toward 0.0) as the ends of the line segment are approached. 165 | bool m_attenuate; 166 | 167 | /// A pointer to the noise module used to generate the output values. 168 | const module::Module* m_pModule; 169 | 170 | /// @a x coordinate of the start of the line segment. 171 | double m_x0; 172 | 173 | /// @a x coordinate of the end of the line segment. 174 | double m_x1; 175 | 176 | /// @a y coordinate of the start of the line segment. 177 | double m_y0; 178 | 179 | /// @a y coordinate of the end of the line segment. 180 | double m_y1; 181 | 182 | /// @a z coordinate of the start of the line segment. 183 | double m_z0; 184 | 185 | /// @a z coordinate of the end of the line segment. 186 | double m_z1; 187 | 188 | }; 189 | 190 | /// @} 191 | 192 | /// @} 193 | 194 | } 195 | 196 | } 197 | 198 | #endif 199 | -------------------------------------------------------------------------------- /include/noise/model/model.h: -------------------------------------------------------------------------------- 1 | // model.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODEL_H 24 | #define NOISE_MODEL_H 25 | 26 | #include "cylinder.h" 27 | #include "line.h" 28 | #include "plane.h" 29 | #include "sphere.h" 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /include/noise/model/plane.h: -------------------------------------------------------------------------------- 1 | // plane.h 2 | // 3 | // Copyright (C) 2004 Owen Jacobson 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is ojacobson@lionsanctuary.net 20 | // 21 | 22 | #ifndef NOISE_MODEL_PLANE_H 23 | #define NOISE_MODEL_PLANE_H 24 | 25 | #include 26 | #include "../module/modulebase.h" 27 | 28 | namespace noise 29 | { 30 | 31 | namespace model 32 | { 33 | /// @addtogroup libnoise 34 | /// @{ 35 | 36 | /// @addtogroup models 37 | /// @{ 38 | 39 | /// Model that defines the surface of a plane. 40 | /// 41 | /// This model returns an output value from a noise module given the 42 | /// coordinates of an input value located on the surface of an ( @a x, 43 | /// @a z ) plane. 44 | /// 45 | /// To generate an output value, pass the ( @a x, @a z ) coordinates of 46 | /// an input value to the GetValue() method. 47 | /// 48 | /// This model is useful for creating: 49 | /// - two-dimensional textures 50 | /// - terrain height maps for local areas 51 | /// 52 | /// This plane extends infinitely in both directions. 53 | class Plane 54 | { 55 | 56 | public: 57 | 58 | /// Constructor. 59 | Plane (); 60 | 61 | /// Constructor 62 | /// 63 | /// @param module The noise module that is used to generate the output 64 | /// values. 65 | Plane (const module::Module& module); 66 | 67 | /// Returns the noise module that is used to generate the output 68 | /// values. 69 | /// 70 | /// @returns A reference to the noise module. 71 | /// 72 | /// @pre A noise module was passed to the SetModule() method. 73 | const module::Module& GetModule () const 74 | { 75 | assert (m_pModule != NULL); 76 | return *m_pModule; 77 | } 78 | 79 | /// Returns the output value from the noise module given the 80 | /// ( @a x, @a z ) coordinates of the specified input value located 81 | /// on the surface of the plane. 82 | /// 83 | /// @param x The @a x coordinate of the input value. 84 | /// @param z The @a z coordinate of the input value. 85 | /// 86 | /// @returns The output value from the noise module. 87 | /// 88 | /// @pre A noise module was passed to the SetModule() method. 89 | /// 90 | /// This output value is generated by the noise module passed to the 91 | /// SetModule() method. 92 | double GetValue (double x, double z) const; 93 | 94 | /// Sets the noise module that is used to generate the output values. 95 | /// 96 | /// @param module The noise module that is used to generate the output 97 | /// values. 98 | /// 99 | /// This noise module must exist for the lifetime of this object, 100 | /// until you pass a new noise module to this method. 101 | void SetModule (const module::Module& module) 102 | { 103 | m_pModule = &module; 104 | } 105 | 106 | private: 107 | 108 | /// A pointer to the noise module used to generate the output values. 109 | const module::Module* m_pModule; 110 | 111 | }; 112 | 113 | /// @} 114 | 115 | /// @} 116 | 117 | } 118 | 119 | } 120 | 121 | #endif 122 | -------------------------------------------------------------------------------- /include/noise/model/sphere.h: -------------------------------------------------------------------------------- 1 | // sphere.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODEL_SPHERE_H 24 | #define NOISE_MODEL_SPHERE_H 25 | 26 | #include 27 | #include "../module/modulebase.h" 28 | 29 | namespace noise 30 | { 31 | 32 | namespace model 33 | { 34 | 35 | /// @addtogroup libnoise 36 | /// @{ 37 | 38 | /// @addtogroup models 39 | /// @{ 40 | 41 | /// Model that defines the surface of a sphere. 42 | /// 43 | /// @image html modelsphere.png 44 | /// 45 | /// This model returns an output value from a noise module given the 46 | /// coordinates of an input value located on the surface of a sphere. 47 | /// 48 | /// To generate an output value, pass the (latitude, longitude) 49 | /// coordinates of an input value to the GetValue() method. 50 | /// 51 | /// This model is useful for creating: 52 | /// - seamless textures that can be mapped onto a sphere 53 | /// - terrain height maps for entire planets 54 | /// 55 | /// This sphere has a radius of 1.0 unit and its center is located at 56 | /// the origin. 57 | class Sphere 58 | { 59 | 60 | public: 61 | 62 | /// Constructor. 63 | Sphere (); 64 | 65 | /// Constructor 66 | /// 67 | /// @param module The noise module that is used to generate the output 68 | /// values. 69 | Sphere (const module::Module& module); 70 | 71 | /// Returns the noise module that is used to generate the output 72 | /// values. 73 | /// 74 | /// @returns A reference to the noise module. 75 | /// 76 | /// @pre A noise module was passed to the SetModule() method. 77 | const module::Module& GetModule () const 78 | { 79 | assert (m_pModule != NULL); 80 | return *m_pModule; 81 | } 82 | 83 | /// Returns the output value from the noise module given the 84 | /// (latitude, longitude) coordinates of the specified input value 85 | /// located on the surface of the sphere. 86 | /// 87 | /// @param lat The latitude of the input value, in degrees. 88 | /// @param lon The longitude of the input value, in degrees. 89 | /// 90 | /// @returns The output value from the noise module. 91 | /// 92 | /// @pre A noise module was passed to the SetModule() method. 93 | /// 94 | /// This output value is generated by the noise module passed to the 95 | /// SetModule() method. 96 | /// 97 | /// Use a negative latitude if the input value is located on the 98 | /// southern hemisphere. 99 | /// 100 | /// Use a negative longitude if the input value is located on the 101 | /// western hemisphere. 102 | double GetValue (double lat, double lon) const; 103 | 104 | /// Sets the noise module that is used to generate the output values. 105 | /// 106 | /// @param module The noise module that is used to generate the output 107 | /// values. 108 | /// 109 | /// This noise module must exist for the lifetime of this object, 110 | /// until you pass a new noise module to this method. 111 | void SetModule (const module::Module& module) 112 | { 113 | m_pModule = &module; 114 | } 115 | 116 | private: 117 | 118 | /// A pointer to the noise module used to generate the output values. 119 | const module::Module* m_pModule; 120 | 121 | }; 122 | 123 | /// @} 124 | 125 | /// @} 126 | 127 | } 128 | 129 | } 130 | 131 | #endif 132 | -------------------------------------------------------------------------------- /include/noise/module/abs.h: -------------------------------------------------------------------------------- 1 | // abs.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODULE_ABS_H 24 | #define NOISE_MODULE_ABS_H 25 | 26 | #include "modulebase.h" 27 | 28 | namespace noise 29 | { 30 | 31 | namespace module { 32 | 33 | /// @addtogroup libnoise 34 | /// @{ 35 | 36 | /// @addtogroup modules 37 | /// @{ 38 | 39 | /// @defgroup modifiermodules Modifier Modules 40 | /// @addtogroup modifiermodules 41 | /// @{ 42 | 43 | /// Noise module that outputs the absolute value of the output value from 44 | /// a source module. 45 | /// 46 | /// @image html moduleabs.png 47 | /// 48 | /// This noise module requires one source module. 49 | class Abs: public Module 50 | { 51 | 52 | public: 53 | 54 | /// Constructor. 55 | Abs (); 56 | 57 | virtual int GetSourceModuleCount () const 58 | { 59 | return 1; 60 | } 61 | 62 | virtual double GetValue (double x, double y, double z) const; 63 | 64 | }; 65 | 66 | /// @} 67 | 68 | /// @} 69 | 70 | /// @} 71 | 72 | } 73 | 74 | } 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /include/noise/module/add.h: -------------------------------------------------------------------------------- 1 | // add.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODULE_ADD_H 24 | #define NOISE_MODULE_ADD_H 25 | 26 | #include "modulebase.h" 27 | 28 | namespace noise 29 | { 30 | 31 | namespace module 32 | { 33 | 34 | /// @addtogroup libnoise 35 | /// @{ 36 | 37 | /// @addtogroup modules 38 | /// @{ 39 | 40 | /// @defgroup combinermodules Combiner Modules 41 | /// @addtogroup combinermodules 42 | /// @{ 43 | 44 | /// Noise module that outputs the sum of the two output values from two 45 | /// source modules. 46 | /// 47 | /// @image html moduleadd.png 48 | /// 49 | /// This noise module requires two source modules. 50 | class Add: public Module 51 | { 52 | 53 | public: 54 | 55 | /// Constructor. 56 | Add (); 57 | 58 | virtual int GetSourceModuleCount () const 59 | { 60 | return 2; 61 | } 62 | 63 | virtual double GetValue (double x, double y, double z) const; 64 | 65 | }; 66 | 67 | /// @} 68 | 69 | /// @} 70 | 71 | /// @} 72 | 73 | } 74 | 75 | } 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /include/noise/module/blend.h: -------------------------------------------------------------------------------- 1 | // blend.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODULE_BLEND_H 24 | #define NOISE_MODULE_BLEND_H 25 | 26 | #include "modulebase.h" 27 | 28 | namespace noise 29 | { 30 | 31 | namespace module 32 | { 33 | 34 | /// @addtogroup libnoise 35 | /// @{ 36 | 37 | /// @addtogroup modules 38 | /// @{ 39 | 40 | /// @defgroup selectormodules Selector Modules 41 | /// @addtogroup selectormodules 42 | /// @{ 43 | 44 | /// Noise module that outputs a weighted blend of the output values from 45 | /// two source modules given the output value supplied by a control module. 46 | /// 47 | /// @image html moduleblend.png 48 | /// 49 | /// Unlike most other noise modules, the index value assigned to a source 50 | /// module determines its role in the blending operation: 51 | /// - Source module 0 (upper left in the diagram) outputs one of the 52 | /// values to blend. 53 | /// - Source module 1 (lower left in the diagram) outputs one of the 54 | /// values to blend. 55 | /// - Source module 2 (bottom of the diagram) is known as the control 56 | /// module. The control module determines the weight of the 57 | /// blending operation. Negative values weigh the blend towards the 58 | /// output value from the source module with an index value of 0. 59 | /// Positive values weigh the blend towards the output value from the 60 | /// source module with an index value of 1. 61 | /// 62 | /// An application can pass the control module to the SetControlModule() 63 | /// method instead of the SetSourceModule() method. This may make the 64 | /// application code easier to read. 65 | /// 66 | /// This noise module uses linear interpolation to perform the blending 67 | /// operation. 68 | /// 69 | /// This noise module requires three source modules. 70 | class Blend: public Module 71 | { 72 | 73 | public: 74 | 75 | /// Constructor. 76 | Blend (); 77 | 78 | /// Returns the control module. 79 | /// 80 | /// @returns A reference to the control module. 81 | /// 82 | /// @pre A control module has been added to this noise module via a 83 | /// call to SetSourceModule() or SetControlModule(). 84 | /// 85 | /// @throw noise::ExceptionNoModule See the preconditions for more 86 | /// information. 87 | /// 88 | /// The control module determines the weight of the blending 89 | /// operation. Negative values weigh the blend towards the output 90 | /// value from the source module with an index value of 0. Positive 91 | /// values weigh the blend towards the output value from the source 92 | /// module with an index value of 1. 93 | const Module& GetControlModule () const 94 | { 95 | if (m_pSourceModule == NULL || m_pSourceModule[2] == NULL) { 96 | throw noise::ExceptionNoModule (); 97 | } 98 | return *(m_pSourceModule[2]); 99 | } 100 | 101 | virtual int GetSourceModuleCount () const 102 | { 103 | return 3; 104 | } 105 | 106 | virtual double GetValue (double x, double y, double z) const; 107 | 108 | /// Sets the control module. 109 | /// 110 | /// @param controlModule The control module. 111 | /// 112 | /// The control module determines the weight of the blending 113 | /// operation. Negative values weigh the blend towards the output 114 | /// value from the source module with an index value of 0. Positive 115 | /// values weigh the blend towards the output value from the source 116 | /// module with an index value of 1. 117 | /// 118 | /// This method assigns the control module an index value of 2. 119 | /// Passing the control module to this method produces the same 120 | /// results as passing the control module to the SetSourceModule() 121 | /// method while assigning that noise module an index value of 2. 122 | /// 123 | /// This control module must exist throughout the lifetime of this 124 | /// noise module unless another control module replaces that control 125 | /// module. 126 | void SetControlModule (const Module& controlModule) 127 | { 128 | assert (m_pSourceModule != NULL); 129 | m_pSourceModule[2] = &controlModule; 130 | } 131 | 132 | }; 133 | 134 | /// @} 135 | 136 | /// @} 137 | 138 | /// @} 139 | 140 | } 141 | 142 | } 143 | 144 | #endif 145 | -------------------------------------------------------------------------------- /include/noise/module/cache.h: -------------------------------------------------------------------------------- 1 | // cache.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODULE_CACHE_H 24 | #define NOISE_MODULE_CACHE_H 25 | 26 | #include "modulebase.h" 27 | 28 | namespace noise 29 | { 30 | 31 | namespace module 32 | { 33 | 34 | /// @addtogroup libnoise 35 | /// @{ 36 | 37 | /// @addtogroup modules 38 | /// @{ 39 | 40 | /// @defgroup miscmodules Miscellaneous Modules 41 | /// @addtogroup miscmodules 42 | /// @{ 43 | 44 | /// Noise module that caches the last output value generated by a source 45 | /// module. 46 | /// 47 | /// If an application passes an input value to the GetValue() method that 48 | /// differs from the previously passed-in input value, this noise module 49 | /// instructs the source module to calculate the output value. This 50 | /// value, as well as the ( @a x, @a y, @a z ) coordinates of the input 51 | /// value, are stored (cached) in this noise module. 52 | /// 53 | /// If the application passes an input value to the GetValue() method 54 | /// that is equal to the previously passed-in input value, this noise 55 | /// module returns the cached output value without having the source 56 | /// module recalculate the output value. 57 | /// 58 | /// If an application passes a new source module to the SetSourceModule() 59 | /// method, the cache is invalidated. 60 | /// 61 | /// Caching a noise module is useful if it is used as a source module for 62 | /// multiple noise modules. If a source module is not cached, the source 63 | /// module will redundantly calculate the same output value once for each 64 | /// noise module in which it is included. 65 | /// 66 | /// This noise module requires one source module. 67 | class Cache: public Module 68 | { 69 | 70 | public: 71 | 72 | /// Constructor. 73 | Cache (); 74 | 75 | virtual int GetSourceModuleCount () const 76 | { 77 | return 1; 78 | } 79 | 80 | virtual double GetValue (double x, double y, double z) const; 81 | 82 | virtual void SetSourceModule (int index, const Module& sourceModule) 83 | { 84 | Module::SetSourceModule (index, sourceModule); 85 | m_isCached = false; 86 | } 87 | 88 | protected: 89 | 90 | /// The cached output value at the cached input value. 91 | mutable double m_cachedValue; 92 | 93 | /// Determines if a cached output value is stored in this noise 94 | /// module. 95 | mutable double m_isCached; 96 | 97 | /// @a x coordinate of the cached input value. 98 | mutable double m_xCache; 99 | 100 | /// @a y coordinate of the cached input value. 101 | mutable double m_yCache; 102 | 103 | /// @a z coordinate of the cached input value. 104 | mutable double m_zCache; 105 | 106 | }; 107 | 108 | /// @} 109 | 110 | /// @} 111 | 112 | /// @} 113 | 114 | } 115 | 116 | } 117 | 118 | #endif 119 | -------------------------------------------------------------------------------- /include/noise/module/checkerboard.h: -------------------------------------------------------------------------------- 1 | // checkerboard.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODULE_CHECKERBOARD_H 24 | #define NOISE_MODULE_CHECKERBOARD_H 25 | 26 | #include "modulebase.h" 27 | 28 | namespace noise 29 | { 30 | 31 | namespace module 32 | { 33 | 34 | /// @addtogroup libnoise 35 | /// @{ 36 | 37 | /// @addtogroup modules 38 | /// @{ 39 | 40 | /// @addtogroup generatormodules 41 | /// @{ 42 | 43 | /// Noise module that outputs a checkerboard pattern. 44 | /// 45 | /// @image html modulecheckerboard.png 46 | /// 47 | /// This noise module outputs unit-sized blocks of alternating values. 48 | /// The values of these blocks alternate between -1.0 and +1.0. 49 | /// 50 | /// This noise module is not really useful by itself, but it is often used 51 | /// for debugging purposes. 52 | /// 53 | /// This noise module does not require any source modules. 54 | class Checkerboard: public Module 55 | { 56 | 57 | public: 58 | 59 | /// Constructor. 60 | Checkerboard (); 61 | 62 | virtual int GetSourceModuleCount () const 63 | { 64 | return 0; 65 | } 66 | 67 | virtual double GetValue (double x, double y, double z) const; 68 | 69 | }; 70 | 71 | /// @} 72 | 73 | /// @} 74 | 75 | /// @} 76 | 77 | } 78 | 79 | } 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /include/noise/module/clamp.h: -------------------------------------------------------------------------------- 1 | // clamp.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODULE_CLAMP_H 24 | #define NOISE_MODULE_CLAMP_H 25 | 26 | #include "modulebase.h" 27 | 28 | namespace noise 29 | { 30 | 31 | namespace module 32 | { 33 | 34 | /// @addtogroup libnoise 35 | /// @{ 36 | 37 | /// @addtogroup modules 38 | /// @{ 39 | 40 | /// @addtogroup modifiermodules 41 | /// @{ 42 | 43 | /// Default lower bound of the clamping range for the noise::module::Clamp 44 | /// noise module. 45 | const double DEFAULT_CLAMP_LOWER_BOUND = -1.0; 46 | 47 | /// Default upper bound of the clamping range for the noise::module::Clamp 48 | /// noise module. 49 | const double DEFAULT_CLAMP_UPPER_BOUND = 1.0; 50 | 51 | /// Noise module that clamps the output value from a source module to a 52 | /// range of values. 53 | /// 54 | /// @image html moduleclamp.png 55 | /// 56 | /// The range of values in which to clamp the output value is called the 57 | /// clamping range. 58 | /// 59 | /// If the output value from the source module is less than the lower 60 | /// bound of the clamping range, this noise module clamps that value to 61 | /// the lower bound. If the output value from the source module is 62 | /// greater than the upper bound of the clamping range, this noise module 63 | /// clamps that value to the upper bound. 64 | /// 65 | /// To specify the upper and lower bounds of the clamping range, call the 66 | /// SetBounds() method. 67 | /// 68 | /// This noise module requires one source module. 69 | class Clamp: public Module 70 | { 71 | 72 | public: 73 | 74 | /// Constructor. 75 | /// 76 | /// The default lower bound of the clamping range is set to 77 | /// noise::module::DEFAULT_CLAMP_LOWER_BOUND. 78 | /// 79 | /// The default upper bound of the clamping range is set to 80 | /// noise::module::DEFAULT_CLAMP_UPPER_BOUND. 81 | Clamp (); 82 | 83 | /// Returns the lower bound of the clamping range. 84 | /// 85 | /// @returns The lower bound. 86 | /// 87 | /// If the output value from the source module is less than the lower 88 | /// bound of the clamping range, this noise module clamps that value 89 | /// to the lower bound. 90 | double GetLowerBound () const 91 | { 92 | return m_lowerBound; 93 | } 94 | 95 | virtual int GetSourceModuleCount () const 96 | { 97 | return 1; 98 | } 99 | 100 | /// Returns the upper bound of the clamping range. 101 | /// 102 | /// @returns The upper bound. 103 | /// 104 | /// If the output value from the source module is greater than the 105 | /// upper bound of the clamping range, this noise module clamps that 106 | /// value to the upper bound. 107 | double GetUpperBound () const 108 | { 109 | return m_upperBound; 110 | } 111 | 112 | virtual double GetValue (double x, double y, double z) const; 113 | 114 | /// Sets the lower and upper bounds of the clamping range. 115 | /// 116 | /// @param lowerBound The lower bound. 117 | /// @param upperBound The upper bound. 118 | /// 119 | /// @pre The lower bound must be less than or equal to the 120 | /// upper bound. 121 | /// 122 | /// @throw noise::ExceptionInvalidParam An invalid parameter was 123 | /// specified; see the preconditions for more information. 124 | /// 125 | /// If the output value from the source module is less than the lower 126 | /// bound of the clamping range, this noise module clamps that value 127 | /// to the lower bound. If the output value from the source module 128 | /// is greater than the upper bound of the clamping range, this noise 129 | /// module clamps that value to the upper bound. 130 | void SetBounds (double lowerBound, double upperBound); 131 | 132 | protected: 133 | 134 | /// Lower bound of the clamping range. 135 | double m_lowerBound; 136 | 137 | /// Upper bound of the clamping range. 138 | double m_upperBound; 139 | 140 | }; 141 | 142 | /// @} 143 | 144 | /// @} 145 | 146 | /// @} 147 | 148 | } 149 | 150 | } 151 | 152 | #endif 153 | -------------------------------------------------------------------------------- /include/noise/module/const.h: -------------------------------------------------------------------------------- 1 | // const.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODULE_CONST_H 24 | #define NOISE_MODULE_CONST_H 25 | 26 | #include "modulebase.h" 27 | 28 | namespace noise 29 | { 30 | 31 | namespace module 32 | { 33 | 34 | /// @addtogroup libnoise 35 | /// @{ 36 | 37 | /// @addtogroup modules 38 | /// @{ 39 | 40 | /// @defgroup generatormodules Generator Modules 41 | /// @addtogroup generatormodules 42 | /// @{ 43 | 44 | /// Default constant value for the noise::module::Const noise module. 45 | const double DEFAULT_CONST_VALUE = 0.0; 46 | 47 | /// Noise module that outputs a constant value. 48 | /// 49 | /// @image html moduleconst.png 50 | /// 51 | /// To specify the constant value, call the SetConstValue() method. 52 | /// 53 | /// This noise module is not useful by itself, but it is often used as a 54 | /// source module for other noise modules. 55 | /// 56 | /// This noise module does not require any source modules. 57 | class Const: public Module 58 | { 59 | 60 | public: 61 | 62 | /// Constructor. 63 | /// 64 | /// The default constant value is set to 65 | /// noise::module::DEFAULT_CONST_VALUE. 66 | Const (); 67 | 68 | /// Returns the constant output value for this noise module. 69 | /// 70 | /// @returns The constant output value for this noise module. 71 | double GetConstValue () const 72 | { 73 | return m_constValue; 74 | } 75 | 76 | virtual int GetSourceModuleCount () const 77 | { 78 | return 0; 79 | } 80 | 81 | virtual double GetValue (double x, double y, double z) const 82 | { 83 | return m_constValue; 84 | } 85 | 86 | /// Sets the constant output value for this noise module. 87 | /// 88 | /// @param constValue The constant output value for this noise module. 89 | void SetConstValue (double constValue) 90 | { 91 | m_constValue = constValue; 92 | } 93 | 94 | protected: 95 | 96 | /// Constant value. 97 | double m_constValue; 98 | 99 | }; 100 | 101 | /// @} 102 | 103 | /// @} 104 | 105 | /// @} 106 | 107 | } 108 | 109 | } 110 | 111 | #endif 112 | -------------------------------------------------------------------------------- /include/noise/module/curve.h: -------------------------------------------------------------------------------- 1 | // curve.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODULE_CURVE_H 24 | #define NOISE_MODULE_CURVE_H 25 | 26 | #include "modulebase.h" 27 | 28 | namespace noise 29 | { 30 | 31 | namespace module 32 | { 33 | 34 | /// @addtogroup libnoise 35 | /// @{ 36 | 37 | /// This structure defines a control point. 38 | /// 39 | /// Control points are used for defining splines. 40 | struct ControlPoint 41 | { 42 | 43 | /// The input value. 44 | double inputValue; 45 | 46 | /// The output value that is mapped from the input value. 47 | double outputValue; 48 | 49 | }; 50 | 51 | /// @addtogroup modules 52 | /// @{ 53 | 54 | /// @addtogroup modifiermodules 55 | /// @{ 56 | 57 | /// Noise module that maps the output value from a source module onto an 58 | /// arbitrary function curve. 59 | /// 60 | /// @image html modulecurve.png 61 | /// 62 | /// This noise module maps the output value from the source module onto an 63 | /// application-defined curve. This curve is defined by a number of 64 | /// control points; each control point has an input value 65 | /// that maps to an output value. Refer to the following 66 | /// illustration: 67 | /// 68 | /// @image html curve.png 69 | /// 70 | /// To add the control points to this curve, call the AddControlPoint() 71 | /// method. 72 | /// 73 | /// Since this curve is a cubic spline, an application must add a minimum 74 | /// of four control points to the curve. If this is not done, the 75 | /// GetValue() method fails. Each control point can have any input and 76 | /// output value, although no two control points can have the same input 77 | /// value. There is no limit to the number of control points that can be 78 | /// added to the curve. 79 | /// 80 | /// This noise module requires one source module. 81 | class Curve: public Module 82 | { 83 | 84 | public: 85 | 86 | /// Constructor. 87 | Curve (); 88 | 89 | /// Destructor. 90 | ~Curve (); 91 | 92 | /// Adds a control point to the curve. 93 | /// 94 | /// @param inputValue The input value stored in the control point. 95 | /// @param outputValue The output value stored in the control point. 96 | /// 97 | /// @pre No two control points have the same input value. 98 | /// 99 | /// @throw noise::ExceptionInvalidParam An invalid parameter was 100 | /// specified; see the preconditions for more information. 101 | /// 102 | /// It does not matter which order these points are added. 103 | void AddControlPoint (double inputValue, double outputValue); 104 | 105 | /// Deletes all the control points on the curve. 106 | /// 107 | /// @post All points on the curve are deleted. 108 | void ClearAllControlPoints (); 109 | 110 | /// Returns a pointer to the array of control points on the curve. 111 | /// 112 | /// @returns A pointer to the array of control points. 113 | /// 114 | /// Before calling this method, call GetControlPointCount() to 115 | /// determine the number of control points in this array. 116 | /// 117 | /// It is recommended that an application does not store this pointer 118 | /// for later use since the pointer to the array may change if the 119 | /// application calls another method of this object. 120 | const ControlPoint* GetControlPointArray () const 121 | { 122 | return m_pControlPoints; 123 | } 124 | 125 | /// Returns the number of control points on the curve. 126 | /// 127 | /// @returns The number of control points on the curve. 128 | int GetControlPointCount () const 129 | { 130 | return m_controlPointCount; 131 | } 132 | 133 | virtual int GetSourceModuleCount () const 134 | { 135 | return 1; 136 | } 137 | 138 | virtual double GetValue (double x, double y, double z) const; 139 | 140 | protected: 141 | 142 | /// Determines the array index in which to insert the control point 143 | /// into the internal control point array. 144 | /// 145 | /// @param inputValue The input value of the control point. 146 | /// 147 | /// @returns The array index in which to insert the control point. 148 | /// 149 | /// @pre No two control points have the same input value. 150 | /// 151 | /// @throw noise::ExceptionInvalidParam An invalid parameter was 152 | /// specified; see the preconditions for more information. 153 | /// 154 | /// By inserting the control point at the returned array index, this 155 | /// class ensures that the control point array is sorted by input 156 | /// value. The code that maps a value onto the curve requires a 157 | /// sorted control point array. 158 | int FindInsertionPos (double inputValue); 159 | 160 | /// Inserts the control point at the specified position in the 161 | /// internal control point array. 162 | /// 163 | /// @param insertionPos The zero-based array position in which to 164 | /// insert the control point. 165 | /// @param inputValue The input value stored in the control point. 166 | /// @param outputValue The output value stored in the control point. 167 | /// 168 | /// To make room for this new control point, this method reallocates 169 | /// the control point array and shifts all control points occurring 170 | /// after the insertion position up by one. 171 | /// 172 | /// Because the curve mapping algorithm used by this noise module 173 | /// requires that all control points in the array must be sorted by 174 | /// input value, the new control point should be inserted at the 175 | /// position in which the order is still preserved. 176 | void InsertAtPos (int insertionPos, double inputValue, 177 | double outputValue); 178 | 179 | /// Number of control points on the curve. 180 | int m_controlPointCount; 181 | 182 | /// Array that stores the control points. 183 | ControlPoint* m_pControlPoints; 184 | 185 | }; 186 | 187 | /// @} 188 | 189 | /// @} 190 | 191 | /// @} 192 | 193 | } 194 | 195 | } 196 | 197 | #endif 198 | -------------------------------------------------------------------------------- /include/noise/module/cylinders.h: -------------------------------------------------------------------------------- 1 | // cylinders.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODULE_CYLINDERS_H 24 | #define NOISE_MODULE_CYLINDERS_H 25 | 26 | #include "modulebase.h" 27 | 28 | namespace noise 29 | { 30 | 31 | namespace module 32 | { 33 | 34 | /// @addtogroup libnoise 35 | /// @{ 36 | 37 | /// @addtogroup modules 38 | /// @{ 39 | 40 | /// @addtogroup generatormodules 41 | /// @{ 42 | 43 | /// Default frequency value for the noise::module::Cylinders noise module. 44 | const double DEFAULT_CYLINDERS_FREQUENCY = 1.0; 45 | 46 | /// Noise module that outputs concentric cylinders. 47 | /// 48 | /// @image html modulecylinders.png 49 | /// 50 | /// This noise module outputs concentric cylinders centered on the origin. 51 | /// These cylinders are oriented along the @a y axis similar to the 52 | /// concentric rings of a tree. Each cylinder extends infinitely along 53 | /// the @a y axis. 54 | /// 55 | /// The first cylinder has a radius of 1.0. Each subsequent cylinder has 56 | /// a radius that is 1.0 unit larger than the previous cylinder. 57 | /// 58 | /// The output value from this noise module is determined by the distance 59 | /// between the input value and the the nearest cylinder surface. The 60 | /// input values that are located on a cylinder surface are given the 61 | /// output value 1.0 and the input values that are equidistant from two 62 | /// cylinder surfaces are given the output value -1.0. 63 | /// 64 | /// An application can change the frequency of the concentric cylinders. 65 | /// Increasing the frequency reduces the distances between cylinders. To 66 | /// specify the frequency, call the SetFrequency() method. 67 | /// 68 | /// This noise module, modified with some low-frequency, low-power 69 | /// turbulence, is useful for generating wood-like textures. 70 | /// 71 | /// This noise module does not require any source modules. 72 | class Cylinders: public Module 73 | { 74 | 75 | public: 76 | 77 | /// Constructor. 78 | /// 79 | /// The default frequency is set to 80 | /// noise::module::DEFAULT_CYLINDERS_FREQUENCY. 81 | Cylinders (); 82 | 83 | /// Returns the frequency of the concentric cylinders. 84 | /// 85 | /// @returns The frequency of the concentric cylinders. 86 | /// 87 | /// Increasing the frequency increases the density of the concentric 88 | /// cylinders, reducing the distances between them. 89 | double GetFrequency () const 90 | { 91 | return m_frequency; 92 | } 93 | 94 | virtual int GetSourceModuleCount () const 95 | { 96 | return 0; 97 | } 98 | 99 | virtual double GetValue (double x, double y, double z) const; 100 | 101 | /// Sets the frequenct of the concentric cylinders. 102 | /// 103 | /// @param frequency The frequency of the concentric cylinders. 104 | /// 105 | /// Increasing the frequency increases the density of the concentric 106 | /// cylinders, reducing the distances between them. 107 | void SetFrequency (double frequency) 108 | { 109 | m_frequency = frequency; 110 | } 111 | 112 | protected: 113 | 114 | /// Frequency of the concentric cylinders. 115 | double m_frequency; 116 | 117 | }; 118 | 119 | /// @} 120 | 121 | /// @} 122 | 123 | /// @} 124 | 125 | } 126 | 127 | } 128 | 129 | #endif 130 | -------------------------------------------------------------------------------- /include/noise/module/exponent.h: -------------------------------------------------------------------------------- 1 | // exponent.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODULE_EXPONENT_H 24 | #define NOISE_MODULE_EXPONENT_H 25 | 26 | #include "modulebase.h" 27 | 28 | namespace noise 29 | { 30 | 31 | namespace module 32 | { 33 | 34 | /// @addtogroup libnoise 35 | /// @{ 36 | 37 | /// @addtogroup modules 38 | /// @{ 39 | 40 | /// @addtogroup modifiermodules 41 | /// @{ 42 | 43 | /// Default exponent for the noise::module::Exponent noise module. 44 | const double DEFAULT_EXPONENT = 1.0; 45 | 46 | /// Noise module that maps the output value from a source module onto an 47 | /// exponential curve. 48 | /// 49 | /// @image html moduleexponent.png 50 | /// 51 | /// Because most noise modules will output values that range from -1.0 to 52 | /// +1.0, this noise module first normalizes this output value (the range 53 | /// becomes 0.0 to 1.0), maps that value onto an exponential curve, then 54 | /// rescales that value back to the original range. 55 | /// 56 | /// This noise module requires one source module. 57 | class Exponent: public Module 58 | { 59 | 60 | public: 61 | 62 | /// Constructor. 63 | /// 64 | /// The default exponent is set to noise::module::DEFAULT_EXPONENT. 65 | Exponent (); 66 | 67 | /// Returns the exponent value to apply to the output value from the 68 | /// source module. 69 | /// 70 | /// @returns The exponent value. 71 | /// 72 | /// Because most noise modules will output values that range from -1.0 73 | /// to +1.0, this noise module first normalizes this output value (the 74 | /// range becomes 0.0 to 1.0), maps that value onto an exponential 75 | /// curve, then rescales that value back to the original range. 76 | double GetExponent () const 77 | { 78 | return m_exponent; 79 | } 80 | 81 | virtual int GetSourceModuleCount () const 82 | { 83 | return 1; 84 | } 85 | 86 | virtual double GetValue (double x, double y, double z) const; 87 | 88 | /// Sets the exponent value to apply to the output value from the 89 | /// source module. 90 | /// 91 | /// @param exponent The exponent value. 92 | /// 93 | /// Because most noise modules will output values that range from -1.0 94 | /// to +1.0, this noise module first normalizes this output value (the 95 | /// range becomes 0.0 to 1.0), maps that value onto an exponential 96 | /// curve, then rescales that value back to the original range. 97 | void SetExponent (double exponent) 98 | { 99 | m_exponent = exponent; 100 | } 101 | 102 | protected: 103 | 104 | /// Exponent to apply to the output value from the source module. 105 | double m_exponent; 106 | 107 | }; 108 | 109 | /// @} 110 | 111 | /// @} 112 | 113 | /// @} 114 | 115 | } 116 | 117 | } 118 | 119 | #endif 120 | -------------------------------------------------------------------------------- /include/noise/module/invert.h: -------------------------------------------------------------------------------- 1 | // invert.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODULE_INVERT_H 24 | #define NOISE_MODULE_INVERT_H 25 | 26 | #include "modulebase.h" 27 | 28 | namespace noise 29 | { 30 | 31 | namespace module 32 | { 33 | 34 | /// @addtogroup libnoise 35 | /// @{ 36 | 37 | /// @addtogroup modules 38 | /// @{ 39 | 40 | /// @addtogroup modifiermodules 41 | /// @{ 42 | 43 | /// Noise module that inverts the output value from a source module. 44 | /// 45 | /// @image html moduleinvert.png 46 | /// 47 | /// This noise module requires one source module. 48 | class Invert: public Module 49 | { 50 | 51 | public: 52 | 53 | /// Constructor. 54 | Invert (); 55 | 56 | virtual int GetSourceModuleCount () const 57 | { 58 | return 1; 59 | } 60 | 61 | virtual double GetValue (double x, double y, double z) const; 62 | 63 | }; 64 | 65 | /// @} 66 | 67 | /// @} 68 | 69 | /// @} 70 | 71 | } 72 | 73 | } 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /include/noise/module/max.h: -------------------------------------------------------------------------------- 1 | // max.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODULE_MAX_H 24 | #define NOISE_MODULE_MAX_H 25 | 26 | #include "modulebase.h" 27 | 28 | namespace noise 29 | { 30 | 31 | namespace module 32 | { 33 | 34 | /// @addtogroup libnoise 35 | /// @{ 36 | 37 | /// @addtogroup modules 38 | /// @{ 39 | 40 | /// @addtogroup combinermodules 41 | /// @{ 42 | 43 | /// Noise module that outputs the larger of the two output values from two 44 | /// source modules. 45 | /// 46 | /// @image html modulemax.png 47 | /// 48 | /// This noise module requires two source modules. 49 | class Max: public Module 50 | { 51 | 52 | public: 53 | 54 | /// Constructor. 55 | Max (); 56 | 57 | virtual int GetSourceModuleCount () const 58 | { 59 | return 2; 60 | } 61 | 62 | virtual double GetValue (double x, double y, double z) const; 63 | 64 | }; 65 | 66 | /// @} 67 | 68 | /// @} 69 | 70 | /// @} 71 | 72 | } 73 | 74 | } 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /include/noise/module/min.h: -------------------------------------------------------------------------------- 1 | // min.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODULE_MIN_H 24 | #define NOISE_MODULE_MIN_H 25 | 26 | #include "modulebase.h" 27 | 28 | namespace noise 29 | { 30 | 31 | namespace module 32 | { 33 | 34 | /// @addtogroup libnoise 35 | /// @{ 36 | 37 | /// @addtogroup modules 38 | /// @{ 39 | 40 | /// @addtogroup combinermodules 41 | /// @{ 42 | 43 | /// Noise module that outputs the smaller of the two output values from 44 | /// two source modules. 45 | /// 46 | /// @image html modulemin.png 47 | /// 48 | /// This noise module requires two source modules. 49 | class Min: public Module 50 | { 51 | 52 | public: 53 | 54 | /// Constructor. 55 | Min (); 56 | 57 | virtual int GetSourceModuleCount () const 58 | { 59 | return 2; 60 | } 61 | 62 | virtual double GetValue (double x, double y, double z) const; 63 | 64 | }; 65 | 66 | /// @} 67 | 68 | /// @} 69 | 70 | /// @} 71 | 72 | } 73 | 74 | } 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /include/noise/module/module.h: -------------------------------------------------------------------------------- 1 | // module.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODULE_H 24 | #define NOISE_MODULE_H 25 | 26 | #include "add.h" 27 | #include "abs.h" 28 | #include "billow.h" 29 | #include "blend.h" 30 | #include "cache.h" 31 | #include "checkerboard.h" 32 | #include "clamp.h" 33 | #include "const.h" 34 | #include "curve.h" 35 | #include "cylinders.h" 36 | #include "displace.h" 37 | #include "exponent.h" 38 | #include "invert.h" 39 | #include "max.h" 40 | #include "min.h" 41 | #include "multiply.h" 42 | #include "perlin.h" 43 | #include "power.h" 44 | #include "ridgedmulti.h" 45 | #include "rotatepoint.h" 46 | #include "scalebias.h" 47 | #include "scalepoint.h" 48 | #include "select.h" 49 | #include "spheres.h" 50 | #include "terrace.h" 51 | #include "translatepoint.h" 52 | #include "turbulence.h" 53 | #include "voronoi.h" 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /include/noise/module/multiply.h: -------------------------------------------------------------------------------- 1 | // multiply.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODULE_MULTIPLY_H 24 | #define NOISE_MODULE_MULTIPLY_H 25 | 26 | #include "modulebase.h" 27 | 28 | namespace noise 29 | { 30 | 31 | namespace module 32 | { 33 | 34 | /// @addtogroup libnoise 35 | /// @{ 36 | 37 | /// @addtogroup modules 38 | /// @{ 39 | 40 | /// @addtogroup combinermodules 41 | /// @{ 42 | 43 | /// Noise module that outputs the product of the two output values from 44 | /// two source modules. 45 | /// 46 | /// @image html modulemultiply.png 47 | /// 48 | /// This noise module requires two source modules. 49 | class Multiply: public Module 50 | { 51 | 52 | public: 53 | 54 | /// Constructor. 55 | Multiply (); 56 | 57 | virtual int GetSourceModuleCount () const 58 | { 59 | return 2; 60 | } 61 | 62 | virtual double GetValue (double x, double y, double z) const; 63 | 64 | }; 65 | 66 | /// @} 67 | 68 | /// @} 69 | 70 | /// @} 71 | 72 | } 73 | 74 | } 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /include/noise/module/power.h: -------------------------------------------------------------------------------- 1 | // power.h 2 | // 3 | // Copyright (C) 2004 Owen Jacobson 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is angstrom@lionsanctuary.net 20 | // 21 | 22 | #ifndef NOISE_MODULE_POWER_H 23 | #define NOISE_MODULE_POWER_H 24 | 25 | #include "modulebase.h" 26 | 27 | namespace noise 28 | { 29 | 30 | namespace module 31 | { 32 | 33 | /// @addtogroup libnoise 34 | /// @{ 35 | 36 | /// @addtogroup modules 37 | /// @{ 38 | 39 | /// @defgroup combinermodules Combiner Modules 40 | /// @addtogroup combinermodules 41 | /// @{ 42 | 43 | /// Noise module that raises the output value from a first source module 44 | /// to the power of the output value from a second source module. 45 | /// 46 | /// @image html modulepower.png 47 | /// 48 | /// The first source module must have an index value of 0. 49 | /// 50 | /// The second source module must have an index value of 1. 51 | /// 52 | /// This noise module requires two source modules. 53 | class Power: public Module 54 | { 55 | 56 | public: 57 | 58 | /// Constructor. 59 | Power (); 60 | 61 | virtual int GetSourceModuleCount () const 62 | { 63 | return 2; 64 | } 65 | 66 | virtual double GetValue (double x, double y, double z) const; 67 | 68 | }; 69 | 70 | /// @} 71 | 72 | /// @} 73 | 74 | /// @} 75 | 76 | } 77 | 78 | } 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /include/noise/module/scalebias.h: -------------------------------------------------------------------------------- 1 | // scalebias.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODULE_SCALEBIAS_H 24 | #define NOISE_MODULE_SCALEBIAS_H 25 | 26 | #include "modulebase.h" 27 | 28 | namespace noise 29 | { 30 | 31 | namespace module 32 | { 33 | 34 | /// @addtogroup libnoise 35 | /// @{ 36 | 37 | /// @addtogroup modules 38 | /// @{ 39 | 40 | /// @addtogroup modifiermodules 41 | /// @{ 42 | 43 | /// Default bias for the noise::module::ScaleBias noise module. 44 | const double DEFAULT_BIAS = 0.0; 45 | 46 | /// Default scale for the noise::module::ScaleBias noise module. 47 | const double DEFAULT_SCALE = 1.0; 48 | 49 | /// Noise module that applies a scaling factor and a bias to the output 50 | /// value from a source module. 51 | /// 52 | /// @image html modulescalebias.png 53 | /// 54 | /// The GetValue() method retrieves the output value from the source 55 | /// module, multiplies it with a scaling factor, adds a bias to it, then 56 | /// outputs the value. 57 | /// 58 | /// This noise module requires one source module. 59 | class ScaleBias: public Module 60 | { 61 | 62 | public: 63 | 64 | /// Constructor. 65 | /// 66 | /// The default bias is set to noise::module::DEFAULT_BIAS. 67 | /// 68 | /// The default scaling factor is set to noise::module::DEFAULT_SCALE. 69 | ScaleBias (); 70 | 71 | /// Returns the bias to apply to the scaled output value from the 72 | /// source module. 73 | /// 74 | /// @returns The bias to apply. 75 | /// 76 | /// The GetValue() method retrieves the output value from the source 77 | /// module, multiplies it with the scaling factor, adds the bias to 78 | /// it, then outputs the value. 79 | double GetBias () const 80 | { 81 | return m_bias; 82 | } 83 | 84 | /// Returns the scaling factor to apply to the output value from the 85 | /// source module. 86 | /// 87 | /// @returns The scaling factor to apply. 88 | /// 89 | /// The GetValue() method retrieves the output value from the source 90 | /// module, multiplies it with the scaling factor, adds the bias to 91 | /// it, then outputs the value. 92 | double GetScale () const 93 | { 94 | return m_scale; 95 | } 96 | 97 | virtual int GetSourceModuleCount () const 98 | { 99 | return 1; 100 | } 101 | 102 | virtual double GetValue (double x, double y, double z) const; 103 | 104 | /// Sets the bias to apply to the scaled output value from the source 105 | /// module. 106 | /// 107 | /// @param bias The bias to apply. 108 | /// 109 | /// The GetValue() method retrieves the output value from the source 110 | /// module, multiplies it with the scaling factor, adds the bias to 111 | /// it, then outputs the value. 112 | void SetBias (double bias) 113 | { 114 | m_bias = bias; 115 | } 116 | 117 | /// Sets the scaling factor to apply to the output value from the 118 | /// source module. 119 | /// 120 | /// @param scale The scaling factor to apply. 121 | /// 122 | /// The GetValue() method retrieves the output value from the source 123 | /// module, multiplies it with the scaling factor, adds the bias to 124 | /// it, then outputs the value. 125 | void SetScale (double scale) 126 | { 127 | m_scale = scale; 128 | } 129 | 130 | protected: 131 | 132 | /// Bias to apply to the scaled output value from the source module. 133 | double m_bias; 134 | 135 | /// Scaling factor to apply to the output value from the source 136 | /// module. 137 | double m_scale; 138 | 139 | }; 140 | 141 | /// @} 142 | 143 | /// @} 144 | 145 | /// @} 146 | 147 | } 148 | 149 | } 150 | 151 | #endif 152 | -------------------------------------------------------------------------------- /include/noise/module/spheres.h: -------------------------------------------------------------------------------- 1 | // spheres.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_MODULE_SPHERES_H 24 | #define NOISE_MODULE_SPHERES_H 25 | 26 | #include "modulebase.h" 27 | 28 | namespace noise 29 | { 30 | 31 | namespace module 32 | { 33 | 34 | /// @addtogroup libnoise 35 | /// @{ 36 | 37 | /// @addtogroup modules 38 | /// @{ 39 | 40 | /// @addtogroup generatormodules 41 | /// @{ 42 | 43 | /// Default frequency value for the noise::module::Spheres noise module. 44 | const double DEFAULT_SPHERES_FREQUENCY = 1.0; 45 | 46 | /// Noise module that outputs concentric spheres. 47 | /// 48 | /// @image html modulespheres.png 49 | /// 50 | /// This noise module outputs concentric spheres centered on the origin 51 | /// like the concentric rings of an onion. 52 | /// 53 | /// The first sphere has a radius of 1.0. Each subsequent sphere has a 54 | /// radius that is 1.0 unit larger than the previous sphere. 55 | /// 56 | /// The output value from this noise module is determined by the distance 57 | /// between the input value and the the nearest spherical surface. The 58 | /// input values that are located on a spherical surface are given the 59 | /// output value 1.0 and the input values that are equidistant from two 60 | /// spherical surfaces are given the output value -1.0. 61 | /// 62 | /// An application can change the frequency of the concentric spheres. 63 | /// Increasing the frequency reduces the distances between spheres. To 64 | /// specify the frequency, call the SetFrequency() method. 65 | /// 66 | /// This noise module, modified with some low-frequency, low-power 67 | /// turbulence, is useful for generating agate-like textures. 68 | /// 69 | /// This noise module does not require any source modules. 70 | class Spheres: public Module 71 | { 72 | 73 | public: 74 | 75 | /// Constructor. 76 | /// 77 | /// The default frequency is set to 78 | /// noise::module::DEFAULT_SPHERES_FREQUENCY. 79 | Spheres (); 80 | 81 | /// Returns the frequency of the concentric spheres. 82 | /// 83 | /// @returns The frequency of the concentric spheres. 84 | /// 85 | /// Increasing the frequency increases the density of the concentric 86 | /// spheres, reducing the distances between them. 87 | double GetFrequency () const 88 | { 89 | return m_frequency; 90 | } 91 | 92 | virtual int GetSourceModuleCount () const 93 | { 94 | return 0; 95 | } 96 | 97 | virtual double GetValue (double x, double y, double z) const; 98 | 99 | /// Sets the frequenct of the concentric spheres. 100 | /// 101 | /// @param frequency The frequency of the concentric spheres. 102 | /// 103 | /// Increasing the frequency increases the density of the concentric 104 | /// spheres, reducing the distances between them. 105 | void SetFrequency (double frequency) 106 | { 107 | m_frequency = frequency; 108 | } 109 | 110 | protected: 111 | 112 | /// Frequency of the concentric spheres. 113 | double m_frequency; 114 | 115 | }; 116 | 117 | /// @} 118 | 119 | /// @} 120 | 121 | /// @} 122 | 123 | } 124 | 125 | } 126 | 127 | #endif 128 | -------------------------------------------------------------------------------- /include/noise/noise.h: -------------------------------------------------------------------------------- 1 | // noise.h 2 | // 3 | // Copyright (C) 2003, 2004 Jason Bevins 4 | // 5 | // This library is free software; you can redistribute it and/or modify it 6 | // under the terms of the GNU Lesser General Public License as published by 7 | // the Free Software Foundation; either version 2.1 of the License, or (at 8 | // your option) any later version. 9 | // 10 | // This library is distributed in the hope that it will be useful, but WITHOUT 11 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 | // License (COPYING.txt) for more details. 14 | // 15 | // You should have received a copy of the GNU Lesser General Public License 16 | // along with this library; if not, write to the Free Software Foundation, 17 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | // 19 | // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 | // off every 'zig'.) 21 | // 22 | 23 | #ifndef NOISE_H 24 | #define NOISE_H 25 | 26 | /// @mainpage libnoise 27 | /// 28 | /// @section intro Introduction 29 | /// 30 | /// libnoise is a portable C++ library that is used to generate coherent 31 | /// noise, a type of smoothly-changing noise. libnoise can generate Perlin 32 | /// noise, ridged multifractal noise, and other types of coherent noise. 33 | /// 34 | /// Coherent noise is often used by graphics programmers to generate 35 | /// natural-looking textures, planetary terrain, and other things. It can 36 | /// also be used to move critters in a realistic way. 37 | /// 38 | /// libnoise is known to compile using the following compilers on the 39 | /// following platforms: 40 | /// - Microsoft Visual C++ 5.0 under Microsoft Windows 2000 Service Pack 4 41 | /// - gcc 3.3.4 under Gentoo Linux 10.0 (x86) 42 | /// 43 | /// It is not known if libnoise will compile on 64-bit platforms, although 44 | /// there is a good change that it will. 45 | /// 46 | /// @section noise Noise Modules 47 | /// 48 | /// In libnoise, coherent-noise generators are encapsulated in classes called 49 | /// noise modules. There are many different types of noise modules. 50 | /// Some noise modules can combine or modify the outputs of other noise 51 | /// modules in various ways; you can join these modules together to generate 52 | /// very complex coherent noise. 53 | /// 54 | /// A noise module receives a 3-dimensional input value from the application, 55 | /// computes the noise value given that input value, and returns the resulting 56 | /// value back to the application. 57 | /// 58 | /// If the application passes the same input value to a noise module, the 59 | /// noise module returns the same output value. 60 | /// 61 | /// All noise modules are derived from the noise::module::Module abstract 62 | /// base class. 63 | /// 64 | /// @section contact Contact 65 | /// 66 | /// Contact jas for questions about libnoise. The spam-resistant email 67 | /// address is jlbezigvins@gmzigail.com (For great email, take off every 68 | /// zig.) 69 | 70 | #include "module/module.h" 71 | #include "model/model.h" 72 | #include "misc.h" 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /src/Biom.cpp: -------------------------------------------------------------------------------- 1 | #include "mapgen/Biom.hpp" -------------------------------------------------------------------------------- /src/City.cpp: -------------------------------------------------------------------------------- 1 | #include "mapgen/City.hpp" 2 | #include "mapgen/Economy.hpp" 3 | #include "mapgen/Package.hpp" 4 | #include "mapgen/Region.hpp" 5 | #include "mapgen/utils.hpp" 6 | 7 | City::City(Region *r, std::string n, LocationType t) 8 | : Location::Location(r, n, t), isCapital(false) { 9 | region->city = this; 10 | } 11 | 12 | Package *City::makeGoods(int y) { 13 | Package *goods = nullptr; 14 | unsigned int p; 15 | switch (type) { 16 | case AGRO: 17 | p = region->nice * economyVars->PACKAGES_PER_NICE * population * 18 | economyVars->PACKAGES_AGRO_POPULATION_MODIFIER; 19 | goods = new Package(this, AGROCULTURE, p); 20 | break; 21 | case MINE: 22 | p = region->minerals * economyVars->PACKAGES_PER_MINERALS * population * 23 | economyVars->PACKAGES_MINERALS_POPULATION_MODIFIER; 24 | goods = new Package(this, MINERALS, p); 25 | break; 26 | } 27 | return goods; 28 | } 29 | 30 | std::pair City::buyGoods(std::vector *goods) { 31 | unsigned int mineralsNeeded = 32 | population * (economyVars->CONSUME_MINERALS_POPULATION_MODIFIER - 33 | region->minerals * economyVars->MINERALS_POPULATION_PRODUCE); 34 | unsigned int agroNeeded = 35 | population * (economyVars->CONSUME_AGRO_POPULATION_MODIFIER - 36 | region->nice * economyVars->AGRO_POPULATION_PRODUCE); 37 | 38 | std::vector mineralsCandidates; 39 | std::vector agroCandidates; 40 | int b = 0; 41 | 42 | std::copy_if( 43 | goods->begin(), goods->end(), std::back_inserter(mineralsCandidates), 44 | [&](Package *p) { return p->type == MINERALS && p->owner != this; }); 45 | std::sort(mineralsCandidates.begin(), mineralsCandidates.end(), 46 | [&](Package *p1, Package *p2) { 47 | if (getPrice(p1) < getPrice(p2)) { 48 | return true; 49 | } 50 | return false; 51 | }); 52 | std::copy_if( 53 | goods->begin(), goods->end(), std::back_inserter(agroCandidates), 54 | [&](Package *p) { return p->type == AGROCULTURE && p->owner != this; }); 55 | std::sort(agroCandidates.begin(), agroCandidates.end(), 56 | [&](Package *p1, Package *p2) { 57 | if (getPrice(p1) < getPrice(p2)) { 58 | return true; 59 | } 60 | return false; 61 | }); 62 | 63 | if (agroCandidates.size() > 0) { 64 | int n = 0; 65 | auto p = agroCandidates[n]; 66 | unsigned int c = 0; 67 | while (agroNeeded > 0 && n < int(agroCandidates.size())) { 68 | p = agroCandidates[n]; 69 | auto price = getPrice(p); 70 | c = std::min((int)agroNeeded, (int)p->count); 71 | if (price / population * c > wealth) { 72 | break; 73 | } 74 | b += c; 75 | agroNeeded -= c; 76 | p->buy(this, price, c); 77 | goods->erase(std::remove(goods->begin(), goods->end(), p)); 78 | n++; 79 | } 80 | } 81 | 82 | if (mineralsCandidates.size() > 0) { 83 | int n = 0; 84 | auto p = mineralsCandidates[n]; 85 | unsigned int c = 0; 86 | while (mineralsNeeded > 0 && n < int(mineralsCandidates.size())) { 87 | p = mineralsCandidates[n]; 88 | auto price = getPrice(p); 89 | c = std::min((int)mineralsNeeded, (int)p->count); 90 | if (price / population * c > wealth) { 91 | break; 92 | } 93 | b += c; 94 | mineralsNeeded -= c; 95 | p->buy(this, price, c); 96 | goods->erase(std::remove(goods->begin(), goods->end(), p)); 97 | n++; 98 | } 99 | } 100 | 101 | //this->wealth -= (float)(economyVars->CANT_BUY_AGRO * agroNeeded / this->population); 102 | //this->wealth -= (float)( 103 | // economyVars->CANT_BUY_MINERALS * mineralsNeeded / this->population); 104 | //wealth = std::max(wealth, 0.f); 105 | return std::make_pair(agroNeeded + mineralsNeeded, b); 106 | } 107 | 108 | float City::getPrice(Package *p) { 109 | float price = 1.f; 110 | if (cache.find(p->owner) != cache.end()) { 111 | price = cache[p->owner]; 112 | } else if (roads.size() != 0) { 113 | auto path = std::find_if(roads.begin(), roads.end(), [&](Road *r) { 114 | return r->regions.back()->city == p->owner || 115 | r->regions.front()->city == p->owner; 116 | }); 117 | 118 | if (path != roads.end()) { 119 | price *= 1 + ((*path)->cost / 10000.f); 120 | } else { 121 | mg::warn("Road not found: from ", *this); 122 | mg::warn("Road not found: to ", *p->owner); 123 | price *= 1.5; 124 | } 125 | if (p->owner->region->state != region->state) { 126 | price *= 1.5; 127 | } 128 | cache.insert(std::make_pair(p->owner, price)); 129 | } 130 | return price * economyVars->PRICE_CORRECTION; 131 | } 132 | 133 | std::ostream& operator<<(std::ostream &strm, const City &c) { 134 | return strm << "City: " << c.name << " [" << c.typeName << "]"; 135 | } 136 | -------------------------------------------------------------------------------- /src/Economy.cpp: -------------------------------------------------------------------------------- 1 | #include "mapgen/Economy.hpp" 2 | 3 | namespace Economy { 4 | } 5 | 6 | EconomyVars::EconomyVars() { 7 | POPULATION_GROWS = Economy::POPULATION_GROWS; 8 | POPULATION_GROWS_WEALTH_MODIFIER = Economy::POPULATION_GROWS_WEALTH_MODIFIER; 9 | 10 | PACKAGES_PER_NICE = Economy::PACKAGES_PER_NICE; 11 | PACKAGES_AGRO_POPULATION_MODIFIER = Economy::PACKAGES_AGRO_POPULATION_MODIFIER; 12 | 13 | PACKAGES_PER_MINERALS = Economy::PACKAGES_PER_MINERALS; 14 | PACKAGES_MINERALS_POPULATION_MODIFIER = Economy::PACKAGES_MINERALS_POPULATION_MODIFIER; 15 | 16 | CONSUME_AGRO_POPULATION_MODIFIER = Economy::CONSUME_AGRO_POPULATION_MODIFIER; 17 | CONSUME_MINERALS_POPULATION_MODIFIER = Economy::CONSUME_MINERALS_POPULATION_MODIFIER; 18 | 19 | CONSUME_AGRO_WEALTH_MODIFIER = Economy::CONSUME_AGRO_WEALTH_MODIFIER; 20 | CONSUME_MINERALS_WEALTH_MODIFIER = Economy::CONSUME_MINERALS_WEALTH_MODIFIER; 21 | 22 | CANT_BUY_AGRO = Economy::CANT_BUY_AGRO; 23 | CANT_BUY_MINERALS = Economy::CANT_BUY_MINERALS; 24 | 25 | MINERALS_POPULATION_PRODUCE = Economy::MINERALS_POPULATION_PRODUCE; 26 | AGRO_POPULATION_PRODUCE = Economy::AGRO_POPULATION_PRODUCE; 27 | 28 | PORT_FEE = Economy::PORT_FEE; 29 | PRICE_CORRECTION = Economy::PRICE_CORRECTION; 30 | }; 31 | -------------------------------------------------------------------------------- /src/GeneratorFacade.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "GeneratorFacade.h" 3 | #include 4 | #include 5 | #include "json.hpp" 6 | #include "mapgen/MapGenerator.hpp" 7 | 8 | using json = nlohmann::json; 9 | 10 | json world; 11 | char buff[10000000]; 12 | 13 | extern "C" { 14 | __declspec(dllexport) int createMap (int seed, int w, int h) { 15 | 16 | auto mapgen = new MapGenerator(w, h); 17 | mapgen->setSeed(seed); 18 | mapgen->update(); 19 | 20 | world = json({}); 21 | auto regions = json::array(); 22 | auto mClusters = json::array(); 23 | auto clusters = json::array(); 24 | int n = 0; 25 | for (auto c : mapgen->map->clusters) { 26 | auto mc = json({}); 27 | mc["id"] = n; 28 | mc["name"] = c->name; 29 | mc["biom"] = c->regions.front()->biom.name; 30 | mc["regions"] = c->regions.size(); 31 | clusters.push_back(mc); 32 | n++; 33 | } 34 | 35 | n = 0; 36 | for (auto c : mapgen->map->megaClusters) { 37 | auto mc = json({}); 38 | mc["id"] = n; 39 | mc["name"] = c->name; 40 | mc["clusters"] = json::array(); 41 | for (auto r : c->regions) 42 | { 43 | auto it = std::find(mapgen->map->clusters.begin(), mapgen->map->clusters.end(), r->cluster); 44 | mc["clusters"].push_back(std::distance(mapgen->map->clusters.begin(), it)); 45 | } 46 | 47 | auto v = mc["clusters"]; 48 | std::sort(v.begin(), v.end()); 49 | auto last = std::unique(v.begin(), v.end()); 50 | v.erase(last, v.end()); 51 | mc["clusters"] = v; 52 | mc["regions"] = c->regions.size(); 53 | mClusters.push_back(mc); 54 | n++; 55 | } 56 | 57 | 58 | int i = 0; 59 | for (auto r : mapgen->map->regions) { 60 | auto json_r = json({}); 61 | auto f_points = json::array(); 62 | auto points = r->getPoints(); 63 | 64 | int n = 0; 65 | for (PointList::iterator it2 = points.begin(); it2 < points.end(); 66 | it2++, n++) { 67 | sf::Vector2 *p = points[n]; 68 | f_points.push_back({ {"x", p->x}, {"y", p->y}, {"height", r->getHeight(p)} }); 69 | } 70 | json_r["points"] = f_points; 71 | 72 | json_r["site"] = { {"x", r->site->x}, {"y", r->site->y}, {"height", r->getHeight(r->site)} }; 73 | json_r["biom"] = r->biom.name; 74 | json_r["isLand"] = r->megaCluster->isLand; 75 | auto it = std::find(mapgen->map->megaClusters.begin(), mapgen->map->megaClusters.end(), r->megaCluster); 76 | json_r["megaCluster"] = std::distance(mapgen->map->megaClusters.begin(), it); 77 | 78 | it = std::find(mapgen->map->clusters.begin(), mapgen->map->clusters.end(), r->cluster); 79 | json_r["cluster"] = std::distance(mapgen->map->clusters.begin(), it); 80 | 81 | regions.push_back(json_r); 82 | i++; 83 | } 84 | 85 | world["regions"] = regions; 86 | world["megaClusters"] = mClusters; 87 | world["clusters"] = clusters; 88 | auto dump = world.dump(); 89 | std::strcpy(buff, dump.c_str()); 90 | return dump.length() + 1; 91 | } 92 | __declspec(dllexport) void getRegion(char *str, int n) 93 | { 94 | strcpy_s(str, n, buff); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/GeneratorFacade.cpp.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 25e3c7744fc065d4cbe7802a8f2eaa97 3 | timeCreated: 1512763632 4 | licenseType: Free 5 | PluginImporter: 6 | externalObjects: {} 7 | serializedVersion: 2 8 | iconMap: {} 9 | executionOrder: {} 10 | isPreloaded: 0 11 | isOverridable: 0 12 | platformData: 13 | - first: 14 | Any: 15 | second: 16 | enabled: 1 17 | settings: {} 18 | - first: 19 | Editor: Editor 20 | second: 21 | enabled: 0 22 | settings: 23 | DefaultValueInitialized: true 24 | userData: 25 | assetBundleName: 26 | assetBundleVariant: 27 | -------------------------------------------------------------------------------- /src/GeneratorFacade.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | extern "C" { 6 | __declspec(dllexport) void getRegion(char*, int); 7 | __declspec(dllexport) int createMap(int, int, int); 8 | } 9 | -------------------------------------------------------------------------------- /src/GeneratorFacade.h.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 803c32ccea14f434cbd2160571b4a758 3 | timeCreated: 1512763632 4 | licenseType: Free 5 | PluginImporter: 6 | externalObjects: {} 7 | serializedVersion: 2 8 | iconMap: {} 9 | executionOrder: {} 10 | isPreloaded: 0 11 | isOverridable: 0 12 | platformData: 13 | - first: 14 | Any: 15 | second: 16 | enabled: 1 17 | settings: {} 18 | - first: 19 | Editor: Editor 20 | second: 21 | enabled: 0 22 | settings: 23 | DefaultValueInitialized: true 24 | userData: 25 | assetBundleName: 26 | assetBundleVariant: 27 | -------------------------------------------------------------------------------- /src/Location.cpp: -------------------------------------------------------------------------------- 1 | #include "mapgen/Location.hpp" 2 | 3 | Location::Location(Region *r, std::string n, LocationType t) : region(r), name(n), type(t) { 4 | region->location = this; 5 | 6 | switch (this->type) { 7 | case CAPITAL: 8 | typeName = "Capital"; 9 | break; 10 | case PORT: 11 | typeName = "Port"; 12 | break; 13 | case MINE: 14 | typeName = "Mine"; 15 | break; 16 | case AGRO: 17 | typeName = "Agro"; 18 | break; 19 | case TRADE: 20 | typeName = "Trade post"; 21 | break; 22 | case LIGHTHOUSE: 23 | typeName = "Lighthouse"; 24 | break; 25 | case CAVE: 26 | typeName = "Cave"; 27 | break; 28 | case FORT: 29 | typeName = "Fort"; 30 | break; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/Map.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "mapgen/Map.hpp" 3 | 4 | Map::~Map(){}; 5 | 6 | float Map::getRegionDistance(Region *r, Region *r2) { 7 | Point p = r->site; 8 | Point p2 = r2->site; 9 | double distancex = (p2->x - p->x); 10 | double distancey = (p2->y - p->y); 11 | float d = std::sqrt(distancex * distancex + distancey * distancey); 12 | 13 | if (r->megaCluster->isLand) { 14 | float hd = (r->getHeight(r->site) - r2->getHeight(r2->site)); 15 | if (hd < 0) { 16 | d += 1000 * std::abs(hd); 17 | if (r2->city != nullptr && d >= 500) { 18 | d -= 500; 19 | } 20 | } 21 | if (r2->hasRiver) { 22 | d *= 0.6f; 23 | } 24 | if (r2->state != r->state) { 25 | d *= 1.2f; 26 | } 27 | if (r2->biom == biom::FORREST && r2->biom == biom::RAIN_FORREST) { 28 | d *= 1.1f; 29 | } 30 | if (r2->hasRoad) { 31 | d *= 0.2f; 32 | } 33 | } else { 34 | d *= 0.8f; 35 | } 36 | return d; 37 | } 38 | 39 | float Map::LeastCostEstimate(void *stateStart, void *stateEnd) { 40 | return getRegionDistance((Region *)stateStart, (Region *)stateEnd); 41 | }; 42 | 43 | void Map::AdjacentCost(void *state, 44 | MP_VECTOR *neighbors) { 45 | auto r = ((Region *)state); 46 | for (auto n : r->neighbors) { 47 | 48 | if (n->biom == biom::LAKE) { 49 | continue; 50 | } 51 | if (r->megaCluster->isLand) { 52 | if (!n->megaCluster->isLand) { 53 | if (r->city == nullptr || r->city->type != PORT) { 54 | continue; 55 | } 56 | } 57 | } else { 58 | if (n->megaCluster->isLand) { 59 | if (n->city == nullptr || n->city->type != PORT) { 60 | continue; 61 | } 62 | } 63 | } 64 | 65 | micropather::StateCost nodeCost = { 66 | (void *)n, getRegionDistance((Region *)state, (Region *)n)}; 67 | neighbors->push_back(nodeCost); 68 | } 69 | }; 70 | void Map::PrintStateInfo(void *state){}; 71 | 72 | -------------------------------------------------------------------------------- /src/Package.cpp: -------------------------------------------------------------------------------- 1 | #include "mapgen/Package.hpp" 2 | #include "mapgen/Economy.hpp" 3 | #include "mapgen/Road.hpp" 4 | #include "mapgen/utils.hpp" 5 | 6 | Package::Package(City *o, PackageType t, unsigned int c) : owner(o), type(t), count(c) {} 7 | 8 | void Package::buy(City *buyer, float price, unsigned int c) { 9 | count -= c; 10 | owner->wealth += (float)price / (float)owner->population * c; 11 | owner->wealth = std::max(owner->wealth, 0.f); 12 | 13 | buyer->wealth -= (float)price / (float)buyer->population * c; 14 | buyer->wealth = std::max(buyer->wealth, 0.f); 15 | auto path = 16 | std::find_if(owner->roads.begin(), owner->roads.end(), [&](Road *r) { 17 | return r->regions.back()->city == buyer || 18 | r->regions.front()->city == buyer; 19 | }); 20 | if (path == owner->roads.end()) { 21 | return; 22 | } 23 | for (auto r : (*path)->regions) { 24 | if (r->city != nullptr && r->city->type == PORT && r->city != owner && 25 | r->city != buyer) { 26 | r->city->wealth += price * Economy::PORT_FEE / r->city->population * c; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Region.cpp: -------------------------------------------------------------------------------- 1 | #include "mapgen/Region.hpp" 2 | #include 3 | #include 4 | 5 | Region::Region() {}; 6 | 7 | Region::Region(Biom b, PointList v, HeightMap h, Point s) 8 | : biom(b), _verticies(v), _heights(h), site(s) {} 9 | 10 | PointList Region::getPoints() { 11 | return _verticies; 12 | }; 13 | 14 | float Region::getHeight(Point p) { 15 | return _heights[p]; 16 | } 17 | 18 | bool Region::isCoast() { 19 | return std::count_if(neighbors.begin(), neighbors.end(), [&](Region* n) { 20 | return !n->megaCluster->isLand; 21 | }) != 0; 22 | } 23 | 24 | bool Region::isLakeCoast() { 25 | return std::count_if(neighbors.begin(), neighbors.end(), [&](Region* n) { 26 | return n->biom == biom::LAKE; 27 | }) != 0; 28 | } 29 | 30 | Region* Region::getRegionWithDirection(float angle, float force) { 31 | Region* nr = nullptr; 32 | float cabs = 360.0; 33 | float ar = 0.0; 34 | float mar = 0.0; 35 | for (auto n : neighbors) { 36 | if (n->cluster->isLand && cluster->isLand && n->getHeight(n->site) - getHeight(site) > 0.07 * force) { 37 | continue; 38 | } 39 | 40 | //TODO: fix angle > 180 41 | auto dx = n->site->x - site->x; 42 | auto dy = n->site->y - site->y; 43 | ar = atan2(dy, dx); 44 | ar *= 180.f / M_PI; 45 | if (nr == nullptr || abs(ar - angle) < cabs) { 46 | nr = n; 47 | mar = ar; 48 | cabs = abs(ar - angle); 49 | } 50 | } 51 | // std::cout << cabs << std::endl; 52 | if (cabs > 15 + 45.f * force) return nullptr; 53 | return nr; 54 | } 55 | -------------------------------------------------------------------------------- /src/Report.cpp: -------------------------------------------------------------------------------- 1 | #include "mapgen/Report.hpp" 2 | 3 | Report::Report(){}; 4 | -------------------------------------------------------------------------------- /src/Road.cpp: -------------------------------------------------------------------------------- 1 | #include "mapgen/Road.hpp" 2 | #include "mapgen/Region.hpp" 3 | #include "micropather.h" 4 | 5 | Road::Road() {}; 6 | 7 | Road::Road(micropather::MPVector* path, float c) : cost(c){ 8 | unsigned size = path->size(); 9 | for (int k = 0; k < size; ++k) { 10 | auto ptr = (*path)[k]; 11 | Region *r = (Region *)ptr; 12 | regions.push_back(r); 13 | r->hasRoad = true; 14 | r->traffic += 1; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /src/State.cpp: -------------------------------------------------------------------------------- 1 | #include "mapgen/State.hpp" 2 | 3 | State::State(std::string n, Cell* c) : name(n), cell(c) { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/WeatherManager.cpp: -------------------------------------------------------------------------------- 1 | #include "mapgen/WeatherManager.hpp" 2 | 3 | WeatherManager::WeatherManager() {} 4 | 5 | 6 | void WeatherManager::genWind() { 7 | windForce = rand() / (double)RAND_MAX; 8 | windAngle = rand() / (double)RAND_MAX * 270; 9 | } 10 | 11 | 12 | void WeatherManager::calcTemp(std::vector regions) { 13 | for (auto r : regions) { 14 | // TODO: adjust it 15 | r->temperature = temperature - (temperature / 5 * r->humidity) - 16 | (temperature / 1.2 * r->getHeight(r->site)); 17 | for (auto n : r->neighbors) { 18 | if (n->biom == biom::LAKE) { 19 | r->temperature += 2; 20 | r->biom.feritlity += 0.2f; 21 | } 22 | } 23 | } 24 | 25 | for (Region *region : regions) { 26 | if (!region->cluster->isLand) continue; 27 | auto r2 = region->getRegionWithDirection(windAngle, windForce); 28 | if (r2 != nullptr) { 29 | if (r2->temperature > region->temperature) { 30 | region->temperature += windForce * r2->temperature; 31 | } else { 32 | region->temperature -= 1 * windForce * r2->temperature; 33 | } 34 | } 35 | } 36 | for (Region *region : regions) { 37 | auto i = 1; 38 | auto h = 0.f; 39 | for (auto n: region->neighbors) { 40 | h += n->temperature; 41 | i++; 42 | } 43 | region->temperature = h/float(i); 44 | } 45 | } 46 | 47 | void WeatherManager::calcHumidity(std::vector regions) { 48 | for (auto r : regions) { 49 | r->humidity = biom::DEFAULT_HUMIDITY; 50 | if (!r->megaCluster->isLand) { 51 | r->humidity = 1; 52 | continue; 53 | } 54 | if (r->hasRiver) { 55 | r->humidity += 0.2f; 56 | } 57 | } 58 | 59 | auto calcRegionsHum = [&]() { 60 | for (auto r : regions) { 61 | if (!r->megaCluster->isLand || r->humidity >= 0.9) { 62 | continue; 63 | } 64 | for (auto rn : r->neighbors) { 65 | if (rn->hasRiver || rn->biom == biom::LAKE) { 66 | r->humidity += 0.05f; 67 | } 68 | float hd = rn->getHeight(rn->site) - r->getHeight(r->site); 69 | if (rn->humidity > r->humidity && r->humidity != 1 && hd < 0.04) { 70 | r->humidity += (rn->humidity - r->humidity) / (1.8f - (hd * 2)); 71 | } 72 | } 73 | //r->humidity = std::min(0.9f, float(r->humidity)); 74 | r->humidity = float(r->humidity); 75 | } 76 | }; 77 | 78 | calcRegionsHum(); 79 | std::reverse(regions.begin(), regions.end()); 80 | calcRegionsHum(); 81 | std::reverse(regions.begin(), regions.end()); 82 | 83 | 84 | for (Region *region : regions) { 85 | if (!region->cluster->isLand) continue; 86 | auto r2 = region->getRegionWithDirection(windAngle, windForce); 87 | if (r2 != nullptr) { 88 | if (r2->humidity > region->humidity) { 89 | region->humidity += windForce * r2->humidity; 90 | } else { 91 | region->humidity -= 0.2 * windForce * r2->humidity; 92 | } 93 | } 94 | } 95 | 96 | for (Region *region : regions) { 97 | auto i = 1; 98 | auto h = 0.f; 99 | for (auto n: region->neighbors) { 100 | h += n->humidity; 101 | i++; 102 | } 103 | region->humidity = h/float(i); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/mask.frag: -------------------------------------------------------------------------------- 1 | uniform sampler2D mask; 2 | uniform sampler2D texture; 3 | 4 | void main(void){ 5 | vec2 tex_coord = gl_TexCoord[0].xy; 6 | vec4 mask_color = texture2D(mask, tex_coord); 7 | vec4 pixel = texture2D(texture, tex_coord); 8 | if (mask_color[3] == 0.f) { 9 | if (pixel[3] > 0.f) { 10 | pixel[0] = 1.f; 11 | pixel[1] = 0.f; 12 | pixel[2] = 0.f; 13 | pixel[3] = 0.f; 14 | gl_FragColor = pixel; 15 | } 16 | } 17 | gl_FragColor = pixel; 18 | } 19 | -------------------------------------------------------------------------------- /src/utils.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "mapgen/utils.hpp" 3 | #include "rang.hpp" 4 | 5 | namespace mg { 6 | double getDistance(Point p, Point p2) { 7 | double distancex = (p2->x - p->x); 8 | double distancey = (p2->y - p->y); 9 | 10 | return std::sqrt(distancex * distancex + distancey * distancey); 11 | } 12 | 13 | template 14 | std::vector filterObjects(std::vector regions, 15 | filterFunc filter, sortFunc sort) { 16 | std::vector places; 17 | 18 | std::copy_if(regions.begin(), regions.end(), std::back_inserter(places), 19 | filter); 20 | std::sort(places.begin(), places.end(), sort); 21 | return places; 22 | } 23 | void before(std::string method) { 24 | std::cout << rang::fg::green << rang::style::bold << "[ -> ]\t" << rang::style::reset << method << std::endl << std::flush; 25 | 26 | }; 27 | void after(std::string method) { 28 | std::cout << rang::fg::red << rang::style::bold << "[ <- ]\t" << rang::style::reset << method << std::endl << std::flush; 29 | }; 30 | void info(std::string prefix, std::string value) { 31 | std::cout << rang::style::bold << rang::fg::black << "[info]\t" << rang::style::reset << prefix << " " << rang::fg::blue << value << rang::style::reset << std::endl << std::flush; 32 | }; 33 | void info(std::string prefix, int value) { 34 | std::cout << rang::style::bold << rang::fg::black << "[info]\t" << rang::style::reset << prefix << " " << rang::fg::blue << value << rang::style::reset << std::endl << std::flush; 35 | }; 36 | 37 | void info(std::string prefix, City value) { 38 | std::cout << rang::style::bold << rang::fg::black << "[info]\t" << rang::style::reset << prefix << " " << rang::fg::blue << value << rang::style::reset << std::endl << std::flush; 39 | }; 40 | 41 | void info(std::string prefix, fs::path value) { 42 | std::cout << rang::style::bold << rang::fg::black << "[info]\t" << rang::style::reset << prefix << " " << rang::fg::blue << value << rang::style::reset << std::endl << std::flush; 43 | }; 44 | 45 | 46 | void warn(std::string prefix, std::string value) { 47 | std::cout << rang::style::bold << rang::fg::yellow << "[warn]\t" << rang::style::reset << prefix << " " << rang::fg::blue << value << rang::style::reset << std::endl << std::flush; 48 | }; 49 | void warn(std::string prefix, int value) { 50 | std::cout << rang::style::bold << rang::fg::yellow << "[warn]\t" << rang::style::reset << prefix << " " << rang::fg::blue << value << rang::style::reset << std::endl << std::flush; 51 | }; 52 | void warn(std::string prefix, City value) { 53 | std::cout << rang::style::bold << rang::fg::yellow << "[warn]\t" << rang::style::reset << prefix << " " << rang::fg::blue << value << rang::style::reset << std::endl << std::flush; 54 | }; 55 | }; 56 | --------------------------------------------------------------------------------