diff --git a/src/net/torvald/Clipboard.kt b/src/net/torvald/Clipboard.kt new file mode 100644 index 000000000..1e82ea209 --- /dev/null +++ b/src/net/torvald/Clipboard.kt @@ -0,0 +1,19 @@ +package net.torvald + +import java.awt.Toolkit +import java.awt.datatransfer.DataFlavor +import java.awt.datatransfer.StringSelection + +/** + * Created by minjaesong on 16-07-31. + */ +object Clipboard { + fun fetch(): String = + Toolkit.getDefaultToolkit().systemClipboard.getData(DataFlavor.stringFlavor) as String + + fun paste(s: String) { + val selection = StringSelection(s) + val clipboard = Toolkit.getDefaultToolkit().systemClipboard + clipboard.setContents(selection, selection) + } +} \ No newline at end of file diff --git a/src/net/torvald/IntArrayStack.kt b/src/net/torvald/IntArrayStack.kt new file mode 100644 index 000000000..1c587cb43 --- /dev/null +++ b/src/net/torvald/IntArrayStack.kt @@ -0,0 +1,100 @@ +package net.torvald + +import java.util.* + +class IntArrayStack { + /** + * Number of elements in the stack + */ + var depth: Int = 0 + private set + + var size: Int + get() = data.size + set(newSize) { + if (newSize > depth) inflate(newSize - data.size) + else deflate(data.size - newSize) + } + + private lateinit var data: IntArray + + constructor(stackSize: Int) { + data = IntArray(stackSize) + } + + constructor(arr: IntArray) { + data = arr.copyOf() + depth = size + } + + fun push(v: Int) { + if (depth >= data.size) throw StackOverflowError() + data[depth++] = v + } + + fun pop(): Int { + if (depth == 0) throw EmptyStackException() + return data[--depth] + } + + fun peek(): Int? { + if (depth == 0) return null + return data[depth - 1] + } + + fun dup() { + if (depth == 0) throw EmptyStackException() + if (depth == data.size) throw StackOverflowError() + push(peek()!!) + } + + fun swap() { + if (depth < 2) throw UnsupportedOperationException("Stack is empty or has only one element.") + val up = pop() + val dn = pop() + push(up) + push(dn) + } + + fun drop() { + if (depth == 0) throw EmptyStackException() + --depth + } + + fun defineFromArray(arr: IntArray) { data = arr.copyOf() } + + /** + * Increase the stack size by a factor. + */ + fun inflate(sizeToAdd: Int) { + if (sizeToAdd < 0) throw UnsupportedOperationException("$sizeToAdd: Cannot deflate the stack with this function. Use deflate(int) instead.") + size += sizeToAdd + val oldStack = this.asArray() + data = IntArray(size, { if (it < oldStack.size) oldStack[it] else 0 }) + } + + /** + * Decrease the stack size by a factor. Overflowing data will be removed. + */ + fun deflate(sizeToTake: Int) { + if (size - sizeToTake < 1) throw UnsupportedOperationException("$sizeToTake: Cannot deflate the stack to the size of zero or negative.") + size -= sizeToTake + val oldStack = this.asArray() + data = IntArray(size, { oldStack[it] }) + if (depth > data.size) depth = data.size + } + + /** + * Convert stack as array. Index zero is the bottommost element. + * @return array of data, with array size equivalent to the stack depth. + */ + fun asArray() = data.copyOfRange(0, depth - 1) + + fun equalTo(other: IntArrayStack) = (this.asArray() == other.asArray()) + + fun plus() { data[depth - 2] += pop() } + fun minus() { data[depth - 2] -= pop() } + fun times() { data[depth - 2] *= pop() } + fun div() { data[depth - 2] /= pop() } + fun mod() { data[depth - 2] %= pop() } +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/console/CodexEdictis.kt b/src/net/torvald/terrarum/console/CodexEdictis.kt index fb0cc233d..8ea05877e 100644 --- a/src/net/torvald/terrarum/console/CodexEdictis.kt +++ b/src/net/torvald/terrarum/console/CodexEdictis.kt @@ -18,7 +18,7 @@ class CodexEdictis : ConsoleCommand { } else { try { - val commandObj = CommandDict.getCommand(args[1].toLowerCase()) + val commandObj = CommandDict.get(args[1].toLowerCase()) commandObj.printUsage() } catch (e: NullPointerException) { diff --git a/src/net/torvald/terrarum/console/CommandDict.kt b/src/net/torvald/terrarum/console/CommandDict.kt index 36c4f51f6..ca241f491 100644 --- a/src/net/torvald/terrarum/console/CommandDict.kt +++ b/src/net/torvald/terrarum/console/CommandDict.kt @@ -46,7 +46,7 @@ object CommandDict { Pair("langtest", LangTest()) ) - fun getCommand(commandName: String): ConsoleCommand { + operator fun get(commandName: String): ConsoleCommand { return dict[commandName]!! } } diff --git a/src/net/torvald/terrarum/console/CommandInterpreter.kt b/src/net/torvald/terrarum/console/CommandInterpreter.kt index 78a73bdd9..65362863a 100644 --- a/src/net/torvald/terrarum/console/CommandInterpreter.kt +++ b/src/net/torvald/terrarum/console/CommandInterpreter.kt @@ -39,13 +39,15 @@ object CommandInterpreter { for (single_command in cmd) { var commandObj: ConsoleCommand? = null try { - if (commandsNoAuth.contains(single_command!!.name.toLowerCase())) { - commandObj = CommandDict.getCommand(single_command.name.toLowerCase()) + if (single_command!!.name.toLowerCase().startsWith("qqq")) { + commandObj = CommandDict["qqq"] + } + else if (commandsNoAuth.contains(single_command.name.toLowerCase())) { + commandObj = CommandDict[single_command.name.toLowerCase()] } else { if (Terrarum.ingame.auth.b()) { - commandObj = CommandDict.getCommand( - single_command.name.toLowerCase()) + commandObj = CommandDict[single_command.name.toLowerCase()] } else { // System.out.println("ee1"); diff --git a/src/net/torvald/terrarum/gameactors/AVKey.kt b/src/net/torvald/terrarum/gameactors/AVKey.kt index fbc5ab982..17a849e2b 100644 --- a/src/net/torvald/terrarum/gameactors/AVKey.kt +++ b/src/net/torvald/terrarum/gameactors/AVKey.kt @@ -12,12 +12,12 @@ object AVKey { * walking/running speed */ const val SPEED = "speed" - const val SPEEDMULT = "speed$MULT" + const val SPEEDMULT = "$SPEED$MULT" /** pixels per frame squared * acceleration of the movement (e.g. running, flying, driving, etc.) */ const val ACCEL = "accel" - const val ACCELMULT = "accel$MULT" + const val ACCELMULT = "$ACCEL$MULT" const val SCALE = "scale" /** pixels */ const val BASEHEIGHT = "baseheight" @@ -25,7 +25,7 @@ object AVKey { const val BASEMASS = "basemass" /** pixels per frame */ const val JUMPPOWER = "jumppower" - const val JUMPPOWERMULT = "jumppower$MULT" + const val JUMPPOWERMULT = "$JUMPPOWER$MULT" /** Int * "Default" value of 1 000 @@ -72,7 +72,10 @@ object AVKey { * current defence point of worn armour(s) */ const val ARMOURDEFENCE = "armourdefence" - const val ARMOURDEFENCEMULT = "armourdefence$MULT" + const val ARMOURDEFENCEMULT = "$ARMOURDEFENCE$MULT" + + const val MAGICREGENRATE = "magicregenrate" + const val MAGICREGENRATEMULT = "$MAGICREGENRATE$MULT" diff --git a/src/net/torvald/terrarum/gameactors/ActorWithBody.kt b/src/net/torvald/terrarum/gameactors/ActorWithBody.kt index 3cab5b1ac..5e82ebe3b 100644 --- a/src/net/torvald/terrarum/gameactors/ActorWithBody.kt +++ b/src/net/torvald/terrarum/gameactors/ActorWithBody.kt @@ -331,8 +331,8 @@ open class ActorWithBody : Actor(), Visible { } // cheap solution for sticking into the wall while Left or Right is held - walledLeft = false//isTouchingSide(hitbox, COLLIDING_LEFT) - walledRight = false//isTouchingSide(hitbox, COLLIDING_RIGHT) + walledLeft = isTouchingSide(nextHitbox, COLLIDING_LEFT) + walledRight = isTouchingSide(nextHitbox, COLLIDING_RIGHT) } } @@ -421,10 +421,10 @@ open class ActorWithBody : Actor(), Visible { } } // axis X - if (isTouchingSide(nextHitbox, COLLIDING_LEFT) && isTouchingSide(nextHitbox, COLLIDING_RIGHT) + if (isTouchingSide(nextHitbox, COLLIDING_LEFT) || isTouchingSide(nextHitbox, COLLIDING_RIGHT) && moveDelta.x != 0.0) { // check right and left // the actor is hitting the wall - //hitAndReflectX() + hitAndReflectX() } } } @@ -444,11 +444,6 @@ open class ActorWithBody : Actor(), Visible { if (ccdDelta.x != 0.0 || ccdDelta.y != 0.0) ccdDelta.set(ccdDelta.setMagnitude(CCD_TICK)) - //////TEST////// - ccdDelta.x = 0.0 - //////TEST////// - // Result: player CAN WALK with ccdDelta.x of zero, which means previous method is a shit. - //println("deltaMax: $deltaMax") //println("ccdDelta: $ccdDelta") diff --git a/src/net/torvald/terrarum/gameactors/Player.kt b/src/net/torvald/terrarum/gameactors/Player.kt index d0f2edbe3..7afaeed43 100644 --- a/src/net/torvald/terrarum/gameactors/Player.kt +++ b/src/net/torvald/terrarum/gameactors/Player.kt @@ -143,7 +143,7 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L * @author minjaesong */ private fun walkHorizontal(left: Boolean, absAxisVal: Float) { - if (true) {//(!walledLeft && left) || (!walledRight && !left)) { + if ((!walledLeft && left) || (!walledRight && !left)) { readonly_totalX = absMax( // keyboard actorValue.getAsDouble(AVKey.ACCEL)!! * @@ -156,6 +156,7 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L actorValue.getAsDouble(AVKey.ACCELMULT)!! * Math.sqrt(scale) * (if (left) -1f else 1f) * absAxisVal + // do not add applyVelo(walkCounterY) here, as it prevents player from moving with gamepad ) //applyForce(Vector2(readonly_totalX, 0.0)) @@ -192,7 +193,6 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L actorValue.getAsDouble(AVKey.ACCEL)!! * actorValue.getAsDouble(AVKey.ACCELMULT)!! * Math.sqrt(scale) * - applyVelo(walkCounterY) * (if (up) -1f else 1f) * absAxisVal ) @@ -275,10 +275,20 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L * TODO linear function (play Super Mario Bros. and you'll get what I'm talking about) */ private fun jump() { - if (jumping) { - val len = MAX_JUMP_LENGTH.toFloat() - val pwr = actorValue.getAsDouble(AVKey.JUMPPOWER)!! * (actorValue.getAsDouble(AVKey.JUMPPOWERMULT) ?: 1.0) + val len = MAX_JUMP_LENGTH.toFloat() + val pwr = actorValue.getAsDouble(AVKey.JUMPPOWER)!! * (actorValue.getAsDouble(AVKey.JUMPPOWERMULT) ?: 1.0) + val jumpLinearThre = 0.08 + + fun jumpFunc(x: Int): Double { + if (x >= len) return 0.0 + val ret = pwr - 0.02 * x + + if (ret < jumpLinearThre) return jumpLinearThre + else return ret + } + + if (jumping) { // increment jump counter if (jumpCounter < len) jumpCounter += 1 @@ -287,6 +297,9 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L var timedJumpCharge = init - init / len * jumpCounter if (timedJumpCharge < 0) timedJumpCharge = 0.0 + // one that uses jumpFunc(x) + //val timedJumpCharge = jumpFunc(jumpCounter) + jumpAcc = -pwr * timedJumpCharge * JUMP_ACCELERATION_MOD * Math.sqrt(scale) // positive value applyForce(Vector2(0.0, jumpAcc)) @@ -390,7 +403,7 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L if (noClip) { if (Terrarum.hasController) { if (axisY != 0f) { - walkVertical(axisY > 0, axisY.abs()) + walkVertical(axisY < 0, axisY.abs()) } } // ↑E, ↓D diff --git a/src/net/torvald/terrarum/mapgenerator/RoguelikeRandomiser.kt b/src/net/torvald/terrarum/mapgenerator/RoguelikeRandomiser.kt index 9f8bdf621..980ffc48e 100644 --- a/src/net/torvald/terrarum/mapgenerator/RoguelikeRandomiser.kt +++ b/src/net/torvald/terrarum/mapgenerator/RoguelikeRandomiser.kt @@ -1,5 +1,6 @@ package net.torvald.terrarum.mapgenerator +import net.torvald.IntArrayStack import net.torvald.colourutil.Col4096 import net.torvald.random.HQRNG import org.newdawn.slick.Color @@ -60,102 +61,4 @@ object RoguelikeRandomiser { ar[i] = a; } } - - class IntArrayStack { - /** - * Number of elements in the stack - */ - var depth: Int = 0 - private set - - var size: Int - get() = data.size - set(newSize) { - if (newSize > depth) inflate(newSize - data.size) - else deflate(data.size - newSize) - } - - private lateinit var data: IntArray - - constructor(stackSize: Int) { - data = IntArray(stackSize) - } - - constructor(arr: IntArray) { - data = arr.copyOf() - depth = size - } - - fun push(v: Int) { - if (depth >= data.size) throw StackOverflowError() - data[depth++] = v - } - - fun pop(): Int { - if (depth == 0) throw EmptyStackException() - return data[--depth] - } - - fun peek(): Int? { - if (depth == 0) return null - return data[depth - 1] - } - - fun dup() { - if (depth == 0) throw EmptyStackException() - if (depth == data.size) throw StackOverflowError() - push(peek()!!) - } - - fun swap() { - if (depth < 2) throw UnsupportedOperationException("Stack is empty or has only one element.") - val up = pop() - val dn = pop() - push(up) - push(dn) - } - - fun drop() { - if (depth == 0) throw EmptyStackException() - --depth - } - - fun defineFromArray(arr: IntArray) { data = arr.copyOf() } - - /** - * Increase the stack size by a factor. - */ - fun inflate(sizeToAdd: Int) { - if (sizeToAdd < 0) throw UnsupportedOperationException("$sizeToAdd: Cannot deflate the stack with this function. Use deflate(int) instead.") - size += sizeToAdd - val oldStack = this.asArray() - data = IntArray(size, { if (it < oldStack.size) oldStack[it] else 0 }) - } - - /** - * Decrease the stack size by a factor. Overflowing data will be removed. - */ - fun deflate(sizeToTake: Int) { - if (size - sizeToTake < 1) throw UnsupportedOperationException("$sizeToTake: Cannot deflate the stack to the size of zero or negative.") - size -= sizeToTake - val oldStack = this.asArray() - data = IntArray(size, { oldStack[it] }) - if (depth > data.size) depth = data.size - } - - /** - * Convert stack as array. Index zero is the bottommost element. - * @return array of data, with array size equivalent to the stack depth. - */ - fun asArray() = data.copyOfRange(0, depth - 1) - - fun equalTo(other: IntArrayStack) = (this.asArray() == other.asArray()) - - fun plus() { data[depth - 2] += pop() } - fun minus() { data[depth - 2] -= pop() } - fun times() { data[depth - 2] *= pop() } - fun div() { data[depth - 2] /= pop() } - fun mod() { data[depth - 2] %= pop() } - } - } \ No newline at end of file