diff --git a/src/net/torvald/point/Point2d.kt b/src/net/torvald/point/Point2d.kt index 5cbee0ff6..cb6609af5 100644 --- a/src/net/torvald/point/Point2d.kt +++ b/src/net/torvald/point/Point2d.kt @@ -1,5 +1,7 @@ package net.torvald.point +import net.torvald.terrarum.gameactors.sqr +import net.torvald.terrarum.gameactors.sqrt import org.dyn4j.geometry.Vector2 /** @@ -46,4 +48,6 @@ class Point2d(var x: Double, var y: Double) : Cloneable { operator fun div(other: Point2d) = Point2d(x / other.x, y / other.y) fun toVector() = Vector2(x, y) + + fun length(other: Point2d) = ((this.x - other.x).sqr() + (this.y - other.y).sqr()).sqrt() } diff --git a/src/net/torvald/random/NoiseGenerator1D.kt b/src/net/torvald/random/NoiseGenerator1D.kt new file mode 100644 index 000000000..7871caf83 --- /dev/null +++ b/src/net/torvald/random/NoiseGenerator1D.kt @@ -0,0 +1,8 @@ +package net.torvald.random + +/** + * Created by minjaesong on 16-10-28. + */ +interface NoiseGenerator1D { + fun get(x: Double): Double +} \ No newline at end of file diff --git a/src/net/torvald/random/TileableValueNoise.kt b/src/net/torvald/random/TileableValueNoise.kt new file mode 100644 index 000000000..74407f50e --- /dev/null +++ b/src/net/torvald/random/TileableValueNoise.kt @@ -0,0 +1,18 @@ +package net.torvald.random + +/** + * Generate value noise that is always "tileably looped" every x in loopSize. + * + * Created by minjaesong on 16-10-28. + */ +class TileableValueNoise( + val octaves: Int, val persistency: Double, + val loopSize: Double = 1.0, val seed: Long? = null +) : NoiseGenerator1D { + + val rng = if (seed != null) HQRNG(seed) else HQRNG() + + override fun get(x: Double): Double { + TODO() + } +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/StateTestingSandbox.kt b/src/net/torvald/terrarum/StateTestingSandbox.kt index 114a86428..9d57e219f 100644 --- a/src/net/torvald/terrarum/StateTestingSandbox.kt +++ b/src/net/torvald/terrarum/StateTestingSandbox.kt @@ -2,6 +2,8 @@ package net.torvald.terrarum import com.jme3.math.FastMath +import net.torvald.point.Point2d +import net.torvald.random.HQRNG import net.torvald.terrarum.gameactors.floorInt import net.torvald.terrarum.gameactors.roundInt import net.torvald.terrarum.virtualcomputer.terminal.ALException @@ -9,6 +11,7 @@ import org.apache.commons.csv.CSVRecord import org.lwjgl.BufferUtils import org.lwjgl.openal.AL import org.lwjgl.openal.AL10 +import org.newdawn.slick.Color import org.newdawn.slick.GameContainer import org.newdawn.slick.Graphics import org.newdawn.slick.state.BasicGameState @@ -27,117 +30,57 @@ class StateTestingSandbox : BasicGameState() { override fun init(container: GameContainer?, game: StateBasedGame?) { - playTone() } - private val sampleRate = 22050 - private var beepSource: Int? = null - private var beepBuffer: Int? = null + val lightning_start = Point2d(50.0, 200.0) + val lightning_end = Point2d(750.0, 200.0) - /** - * @param duration : milliseconds - */ - private fun makeAudioData(duration: Int, freq: Float): ByteBuffer { - val audioData = BufferUtils.createByteBuffer(duration.times(sampleRate).div(1000)) - - val realDuration = duration * sampleRate / 1000 - val chopSize = freq * 2f / sampleRate - - val amp = Math.max(4600f / freq, 1f) - val nHarmonics = 4 - - val transitionThre = 1150f - - if (freq < transitionThre) { // chopper generator (for low freq) - for (x in 0..realDuration - 1) { - var sine: Float = amp * FastMath.cos(FastMath.PI * x * chopSize) - if (sine > 1f) sine = 1f - else if (sine < -1f) sine = -1f - audioData.put( - (0.5f + 0.5f * sine).times(0xFF).toByte() - ) - } - } - else { // harmonics generator (for high freq) - for (x in 0..realDuration - 1) { - var sine: Float = 0f - for (k in 0..nHarmonics) { // mix only odd harmonics to make squarewave - sine += (1f / (2*k + 1)) * - FastMath.sin((2*k + 1) * FastMath.PI * x * chopSize) - } - audioData.put( - (0.5f + 0.5f * sine).times(0xFF).toByte() - ) - } - } - - audioData.rewind() - - return audioData - } - - var audioData: ByteBuffer? = null - - private fun playTone() { - if (audioData == null) audioData = makeAudioData(5000, 27.5f) - - - if (!AL.isCreated()) AL.create() - - - // Clear error stack. - AL10.alGetError() - - beepBuffer = AL10.alGenBuffers() - checkALError() - - try { - AL10.alBufferData(beepBuffer!!, AL10.AL_FORMAT_MONO8, audioData, sampleRate) - checkALError() - - beepSource = AL10.alGenSources() - checkALError() - - try { - AL10.alSourceQueueBuffers(beepSource!!, beepBuffer!!) - checkALError() - - AL10.alSource3f(beepSource!!, AL10.AL_POSITION, 0f, 0f, 1f) - AL10.alSourcef(beepSource!!, AL10.AL_REFERENCE_DISTANCE, 1f) - AL10.alSourcef(beepSource!!, AL10.AL_MAX_DISTANCE, 1f) - AL10.alSourcef(beepSource!!, AL10.AL_GAIN, 0.3f) - checkALError() - - AL10.alSourcePlay(beepSource!!) - checkALError() - - } - catch (e: ALException) { - AL10.alDeleteSources(beepSource!!) - } - } - catch (e: ALException) { - if (beepSource != null) AL10.alDeleteSources(beepSource!!) - } - } + val bolt = LightingBolt(lightning_start, lightning_end, 20) override fun update(container: GameContainer?, game: StateBasedGame?, delta: Int) { } - // Custom implementation of Util.checkALError() that uses our custom exception. - private fun checkALError() { - val errorCode = AL10.alGetError() - if (errorCode != AL10.AL_NO_ERROR) { - throw ALException(errorCode) + override fun getID() = Terrarum.STATE_ID_TEST_SHIT + + override fun render(container: GameContainer, game: StateBasedGame, g: Graphics) { + g.color = Color.white + g.lineWidth = 3f + + //g.drawLine(lightning_start, lightning_end) + bolt.draw(g) + } + +} + +fun Graphics.drawLine(p1: Point2d, p2: Point2d) { + drawLine(p1.x.toFloat(), p1.y.toFloat(), p2.x.toFloat(), p2.y.toFloat()) +} + +class LightingBolt(val start: Point2d, val end: Point2d, val segments: Int) { + val mainBolt = LinkedList() //Pair + + val boltYDev = 20.0 + + init { + val length = start.length(end) + + for (i in 0..segments - 1) { + mainBolt.add( + Point2d( + start.x + length / segments * i, + start.y + HQRNG().nextFloat().times(2.0).minus(1.0).times(boltYDev) + ) + ) } } + fun draw(g: Graphics) { + for (i in 0..segments - 1) { + val startpoint = mainBolt[i] + val endpoint = if (i == segments - 1) end else mainBolt[i + 1] - override fun getID() = Terrarum.STATE_ID_TEST_SHIT - - override fun render(container: GameContainer?, game: StateBasedGame?, g: Graphics?) { - + g.drawLine(startpoint, endpoint) + } } - } \ No newline at end of file diff --git a/src/net/torvald/terrarum/gameactors/ActorWithBody.kt b/src/net/torvald/terrarum/gameactors/ActorWithBody.kt index ad9359c47..b58f9d884 100644 --- a/src/net/torvald/terrarum/gameactors/ActorWithBody.kt +++ b/src/net/torvald/terrarum/gameactors/ActorWithBody.kt @@ -23,8 +23,8 @@ open class ActorWithBody : Actor(), Visible { override var referenceID: Int = generateUniqueReferenceID() override var actorValue: ActorValue = ActorValue() - @Transient var sprite: SpriteAnimation? = null - @Transient var spriteGlow: SpriteAnimation? = null + @Transient internal var sprite: SpriteAnimation? = null + @Transient internal var spriteGlow: SpriteAnimation? = null @Transient private val world: GameWorld = Terrarum.ingame.world @@ -229,6 +229,16 @@ open class ActorWithBody : Actor(), Visible { // some initialiser goes here... } + fun makeNewSprite(w: Int, h: Int) { + sprite = SpriteAnimation() + sprite!!.setDimension(w, h) + } + + fun makeNewSpriteGlow(w: Int, h: Int) { + spriteGlow = SpriteAnimation() + spriteGlow!!.setDimension(w, h) + } + /** * @param w * @param h @@ -1016,6 +1026,7 @@ fun Double.roundInt(): Int = Math.round(this).toInt() fun Float.roundInt(): Int = Math.round(this).toInt() fun Double.abs() = Math.abs(this) fun Double.sqr() = this * this +fun Double.sqrt() = Math.sqrt(this) fun Int.abs() = if (this < 0) -this else this fun Double.bipolarClamp(limit: Double) = if (this > 0 && this > limit) limit diff --git a/src/net/torvald/terrarum/gameactors/PlayerBuilderCynthia.kt b/src/net/torvald/terrarum/gameactors/PlayerBuilderCynthia.kt index 6600a5254..1af2c77c4 100644 --- a/src/net/torvald/terrarum/gameactors/PlayerBuilderCynthia.kt +++ b/src/net/torvald/terrarum/gameactors/PlayerBuilderCynthia.kt @@ -17,8 +17,7 @@ object PlayerBuilderCynthia { p.actorValue["__selectedtile"] = 16 - p.sprite = SpriteAnimation() - p.sprite!!.setDimension(26, 42) + p.makeNewSprite(26, 42) p.sprite!!.setSpriteImage("assets/graphics/sprites/test_player_2.png") p.sprite!!.setDelay(200) p.sprite!!.setRowsAndFrames(1, 1) diff --git a/src/net/torvald/terrarum/gameactors/PlayerBuilderSigrid.kt b/src/net/torvald/terrarum/gameactors/PlayerBuilderSigrid.kt index 7eb11d733..cd23376fe 100644 --- a/src/net/torvald/terrarum/gameactors/PlayerBuilderSigrid.kt +++ b/src/net/torvald/terrarum/gameactors/PlayerBuilderSigrid.kt @@ -21,15 +21,13 @@ object PlayerBuilderSigrid { p.referenceID = 0x51621D // the only constant of this procedural universe - p.sprite = SpriteAnimation() - p.sprite!!.setDimension(28, 51) + p.makeNewSprite(28, 51) p.sprite!!.setSpriteImage("assets/graphics/sprites/test_player.png") p.sprite!!.setDelay(200) p.sprite!!.setRowsAndFrames(1, 1) p.sprite!!.setAsVisible() - p.spriteGlow = SpriteAnimation() - p.spriteGlow!!.setDimension(28, 51) + p.makeNewSpriteGlow(28, 51) p.spriteGlow!!.setSpriteImage("assets/graphics/sprites/test_player_glow.png") p.spriteGlow!!.setDelay(200) p.spriteGlow!!.setRowsAndFrames(1, 1) diff --git a/src/net/torvald/terrarum/mapgenerator/WorldGenerator.kt b/src/net/torvald/terrarum/mapgenerator/WorldGenerator.kt index 119c84f3d..628be05a5 100644 --- a/src/net/torvald/terrarum/mapgenerator/WorldGenerator.kt +++ b/src/net/torvald/terrarum/mapgenerator/WorldGenerator.kt @@ -258,6 +258,9 @@ object WorldGenerator { /** * http://accidentalnoise.sourceforge.net/minecraftworlds.html + * + * TODO have it seamless + * use MappingMode.SEAMLESS_XY ? */ private fun raise3(): Array { val noiseMap = Array(HEIGHT, { BitSet(WIDTH) })