From a6bb32870d1df08813251bb0c2ebca6e9c02d26e Mon Sep 17 00:00:00 2001 From: Song Minjae Date: Thu, 22 Dec 2016 02:02:13 +0900 Subject: [PATCH] noise pattern generator (loops x and y) Former-commit-id: 548612468f454aaf037544e5c3a86deb4f30fc63 Former-commit-id: 1026b3d7420fd5a14117e4c295009dc5831862ad --- src/net/torvald/terrarum/COPYING.md | 45 ++---- src/net/torvald/terrarum/StateNoiseTexGen.kt | 142 ++++++++++++++++++ src/net/torvald/terrarum/Terrarum.kt | 11 +- src/net/torvald/terrarum/console/ExportMap.kt | 81 +++++----- 4 files changed, 198 insertions(+), 81 deletions(-) create mode 100644 src/net/torvald/terrarum/StateNoiseTexGen.kt diff --git a/src/net/torvald/terrarum/COPYING.md b/src/net/torvald/terrarum/COPYING.md index 26ac5e040..e72bb613f 100644 --- a/src/net/torvald/terrarum/COPYING.md +++ b/src/net/torvald/terrarum/COPYING.md @@ -58,7 +58,7 @@ All rights reserved. Kotlin translated and modified code Copyright (C) 2016 Minjaesong (Torvald) ---- +---- Amazing ambient sound recordings: @@ -72,7 +72,7 @@ Amazing ambient sound recordings: Copyright (C) 2012, 2013, 2015, 2016 Klankbeeld Sound from http://www.freesound.org/people/klankbeeld/ ---- +---- *OpenComputers* @@ -96,39 +96,7 @@ Copyright (c) 2013-2015 Florian "Sangar" Nücke OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ---- - -Modified JNLua from *OpenComputers* - -Copyright (c) 2013-2015 Florian "Sangar" Nücke - - OpenComputers uses the JNLua library, with a patch to allow memory limits: - - http://code.google.com/p/jnlua/issues/detail?id=9 - - JNLua is copyrighted and licensed as follows: - - Copyright (C) 2008,2012 Andre Naef - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - ---- +---- LuaJ @@ -151,3 +119,10 @@ Copyright (c) 2007 LuaJ. All rights reserved. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +---- + +*FREE Seamless-Textures-Generator-2* +Version 2.0 +Photoshop Extension Panel and Actions Set +©2015 the-orange-box.com \ No newline at end of file diff --git a/src/net/torvald/terrarum/StateNoiseTexGen.kt b/src/net/torvald/terrarum/StateNoiseTexGen.kt new file mode 100644 index 000000000..c3ad24eba --- /dev/null +++ b/src/net/torvald/terrarum/StateNoiseTexGen.kt @@ -0,0 +1,142 @@ +package net.torvald.terrarum + +import com.sudoplay.joise.Joise +import com.sudoplay.joise.module.* +import net.torvald.terrarum.Terrarum.Companion.STATE_ID_TOOL_NOISEGEN +import org.newdawn.slick.Color +import org.newdawn.slick.GameContainer +import org.newdawn.slick.Graphics +import org.newdawn.slick.Image +import org.newdawn.slick.state.BasicGameState +import org.newdawn.slick.state.StateBasedGame +import java.util.* + +/** + * Created by SKYHi14 on 2016-12-21. + */ +class StateNoiseTexGen : BasicGameState() { + + private val imagesize = 512 + + private val noiseImage = Image(imagesize, imagesize) + + private val sampleDensity = 4.0 + + override fun init(p0: GameContainer?, p1: StateBasedGame?) { + generateNoiseImage() + + println("Press SPACE to generate new noise") + } + + private fun noiseRidged(): Joise { + val ridged = ModuleFractal() + ridged.setType(ModuleFractal.FractalType.RIDGEMULTI) + ridged.setAllSourceInterpolationTypes(ModuleBasisFunction.InterpolationType.QUINTIC) + ridged.setNumOctaves(4) + ridged.setFrequency(1.0) + ridged.seed = Random().nextLong() + + val ridged_autocorrect = ModuleAutoCorrect() + ridged_autocorrect.setRange(0.0, 1.0) + ridged_autocorrect.setSource(ridged) + + val ridged_scale = ModuleScaleDomain() + ridged_scale.setScaleX(1.0) + ridged_scale.setScaleY(1.0) + ridged_scale.setSource(ridged_autocorrect) + + return Joise(ridged_scale) + } + + private fun noiseBlobs(): Joise { + val gradval = ModuleBasisFunction() + gradval.seed = Random().nextLong() + gradval.setType(ModuleBasisFunction.BasisType.GRADVAL) + gradval.setInterpolation(ModuleBasisFunction.InterpolationType.QUINTIC) + + val gradval_scale = ModuleScaleDomain() + gradval_scale.setScaleX(1.0) + gradval_scale.setScaleY(1.0) + gradval_scale.setSource(gradval) + + return Joise(gradval_scale) + } + + private fun noiseSimplex(): Joise { + val simplex = ModuleFractal() + simplex.seed = Random().nextLong() + simplex.setAllSourceBasisTypes(ModuleBasisFunction.BasisType.SIMPLEX) + simplex.setAllSourceInterpolationTypes(ModuleBasisFunction.InterpolationType.LINEAR) + simplex.setNumOctaves(2) + simplex.setFrequency(1.0) + + val simplex_scale = ModuleScaleDomain() + simplex_scale.setScaleX(1.0) + simplex_scale.setScaleY(1.0) + simplex_scale.setSource(simplex) + + return Joise(simplex_scale) + } + + private fun noiseCellular(): Joise { + val cellgen = ModuleCellGen() + cellgen.seed = Random().nextLong() + + val cellular = ModuleCellular() + cellular.setCellularSource(cellgen) + + return Joise(cellular) + } + + fun generateNoiseImage() { + val noiseModule = noiseCellular() + + noiseImage.graphics.background = Color.black + + for (sy in 0..imagesize - 1) { + for (sx in 0..imagesize - 1) { + val y = sy.toDouble() / imagesize + val x = sx.toDouble() / imagesize + + val sampleOffset = sampleDensity + // 4-D toroidal sampling (looped H and V) + val sampleTheta1 = x * Math.PI * 2.0 + val sampleTheta2 = y * Math.PI * 2.0 + val sampleX = Math.sin(sampleTheta1) * sampleDensity + sampleDensity + val sampleY = Math.cos(sampleTheta1) * sampleDensity + sampleDensity + val sampleZ = Math.sin(sampleTheta2) * sampleDensity + sampleDensity + val sampleW = Math.cos(sampleTheta2) * sampleDensity + sampleDensity + + val noise = noiseModule.get( + sampleX, sampleY, sampleZ, sampleW + ).plus(1.0).div(2.0) + + noiseImage.graphics.color = Color(noise.toFloat(), noise.toFloat(), noise.toFloat()) + noiseImage.graphics.fillRect(sx.toFloat(), sy.toFloat(), 1f, 1f) + } + } + + noiseImage.graphics.flush() + } + + override fun update(p0: GameContainer?, p1: StateBasedGame?, p2: Int) { + + } + + override fun getID() = STATE_ID_TOOL_NOISEGEN + + override fun render(gc: GameContainer, sbg: StateBasedGame, g: Graphics) { + g.background = Color.cyan + g.drawImage(noiseImage, + Terrarum.WIDTH.minus(imagesize).div(2).toFloat(), + Terrarum.HEIGHT.minus(imagesize).div(2).toFloat() + ) + } + + override fun keyPressed(key: Int, c: Char) { + if (c == ' ') { + println("Generating noise, may take a while") + generateNoiseImage() + } + } +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/Terrarum.kt b/src/net/torvald/terrarum/Terrarum.kt index e40af8051..4a8e752d7 100644 --- a/src/net/torvald/terrarum/Terrarum.kt +++ b/src/net/torvald/terrarum/Terrarum.kt @@ -112,9 +112,10 @@ constructor(gamename: String) : StateBasedGame(gamename) { //addState(StateSplash()) //addState(StateMonitorCheck()) //addState(StateFontTester()) + addState(StateNoiseTexGen()) - ingame = StateInGame() - addState(ingame) + //ingame = StateInGame() + //addState(ingame) } companion object { @@ -210,8 +211,10 @@ constructor(gamename: String) : StateBasedGame(gamename) { val STATE_ID_CONFIG_CALIBRATE = 0x11 val STATE_ID_TEST_FONT = 0x100 - val STATE_ID_TEST_TTY = 0x110 - val STATE_ID_TEST_SHIT = 0x5617 + val STATE_ID_TEST_SHIT = 0x101 + val STATE_ID_TEST_TTY = 0x102 + + val STATE_ID_TOOL_NOISEGEN = 0x200 var hasController = false val CONTROLLER_DEADZONE = 0.1f diff --git a/src/net/torvald/terrarum/console/ExportMap.kt b/src/net/torvald/terrarum/console/ExportMap.kt index 02843925a..652e580b3 100644 --- a/src/net/torvald/terrarum/console/ExportMap.kt +++ b/src/net/torvald/terrarum/console/ExportMap.kt @@ -18,9 +18,47 @@ internal object ExportMap : ConsoleCommand { private val colorTable = HashMap() + init { + colorTable.put(Tile.AIR, Col4096(0xCEF)) + colorTable.put(Tile.STONE, Col4096(0x888)) + colorTable.put(Tile.DIRT, Col4096(0x753)) + colorTable.put(Tile.GRASS, Col4096(0x472)) + + colorTable.put(Tile.ORE_COPPER, Col4096(0x6A8)) + colorTable.put(Tile.ORE_IRON, Col4096(0xC75)) + colorTable.put(Tile.ORE_GOLD, Col4096(0xA87)) + colorTable.put(Tile.ORE_ILMENITE, Col4096(0x8AB)) + colorTable.put(Tile.ORE_AURICHALCUM, Col4096(0xD92)) + colorTable.put(Tile.ORE_SILVER, Col4096(0xDDD)) + + colorTable.put(Tile.RAW_DIAMOND, Col4096(0x2BF)) + colorTable.put(Tile.RAW_RUBY, Col4096(0xB10)) + colorTable.put(Tile.RAW_EMERALD, Col4096(0x0B1)) + colorTable.put(Tile.RAW_SAPPHIRE, Col4096(0x01B)) + colorTable.put(Tile.RAW_TOPAZ, Col4096(0xC70)) + colorTable.put(Tile.RAW_AMETHYST, Col4096(0x70C)) + + colorTable.put(Tile.WATER, Col4096(0x038)) + colorTable.put(Tile.LAVA, Col4096(0xF50)) + + colorTable.put(Tile.SAND, Col4096(0xDDB)) + colorTable.put(Tile.SAND_WHITE, Col4096(0xFFD)) + colorTable.put(Tile.SAND_RED, Col4096(0xA32)) + colorTable.put(Tile.SAND_DESERT, Col4096(0xEDB)) + colorTable.put(Tile.SAND_BLACK, Col4096(0x444)) + colorTable.put(Tile.SAND_GREEN, Col4096(0x9A6)) + + colorTable.put(Tile.GRAVEL, Col4096(0x664)) + colorTable.put(Tile.GRAVEL_GREY, Col4096(0x999)) + + colorTable.put(Tile.ICE_NATURAL, Col4096(0x9AB)) + colorTable.put(Tile.ICE_MAGICAL, Col4096(0x7AC)) + colorTable.put(Tile.ICE_FRAGILE, Col4096(0x6AF)) + colorTable.put(Tile.SNOW, Col4096(0xCDE)) + } + override fun execute(args: Array) { if (args.size == 2) { - buildColorTable() var mapData = ByteArray(Terrarum.ingame.world.width * Terrarum.ingame.world.height * 3) var mapDataPointer = 0 @@ -70,47 +108,6 @@ internal object ExportMap : ConsoleCommand { Echo("Exports current map into echo image.") Echo("The image can be found at %appdata%/terrarum/Exports") } - - private fun buildColorTable() { - colorTable.put(Tile.AIR, Col4096(0xCEF)) - colorTable.put(Tile.STONE, Col4096(0x888)) - colorTable.put(Tile.DIRT, Col4096(0x753)) - colorTable.put(Tile.GRASS, Col4096(0x472)) - - colorTable.put(Tile.ORE_COPPER, Col4096(0x6A8)) - colorTable.put(Tile.ORE_IRON, Col4096(0xC75)) - colorTable.put(Tile.ORE_GOLD, Col4096(0xA87)) - colorTable.put(Tile.ORE_ILMENITE, Col4096(0x8AB)) - colorTable.put(Tile.ORE_AURICHALCUM, Col4096(0xD92)) - colorTable.put(Tile.ORE_SILVER, Col4096(0xDDD)) - - colorTable.put(Tile.RAW_DIAMOND, Col4096(0x2BF)) - colorTable.put(Tile.RAW_RUBY, Col4096(0xB10)) - colorTable.put(Tile.RAW_EMERALD, Col4096(0x0B1)) - colorTable.put(Tile.RAW_SAPPHIRE, Col4096(0x01B)) - colorTable.put(Tile.RAW_TOPAZ, Col4096(0xC70)) - colorTable.put(Tile.RAW_AMETHYST, Col4096(0x70C)) - - colorTable.put(Tile.WATER, Col4096(0x038)) - colorTable.put(Tile.LAVA, Col4096(0xF50)) - - colorTable.put(Tile.SAND, Col4096(0xDDB)) - colorTable.put(Tile.SAND_WHITE, Col4096(0xFFD)) - colorTable.put(Tile.SAND_RED, Col4096(0xA32)) - colorTable.put(Tile.SAND_DESERT, Col4096(0xEDB)) - colorTable.put(Tile.SAND_BLACK, Col4096(0x444)) - colorTable.put(Tile.SAND_GREEN, Col4096(0x9A6)) - - colorTable.put(Tile.GRAVEL, Col4096(0x664)) - colorTable.put(Tile.GRAVEL_GREY, Col4096(0x999)) - - colorTable.put(Tile.ICE_NATURAL, Col4096(0x9AB)) - colorTable.put(Tile.ICE_MAGICAL, Col4096(0x7AC)) - colorTable.put(Tile.ICE_FRAGILE, Col4096(0x6AF)) - colorTable.put(Tile.SNOW, Col4096(0xCDE)) - - - } }