mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-07 12:21:52 +09:00
new console command 'error' (stderr equivalent of console window)
Former-commit-id: ab54663fd64f9ae9c758f53b3f5800a5894f0db3 Former-commit-id: 7aba1585ffa45195622bb25e1c62cace474420c9
This commit is contained in:
@@ -3,7 +3,7 @@ package net.torvald.terrarum
|
||||
import com.google.gson.JsonObject
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 16-03-19.
|
||||
* Created by minjaesong on 16-03-12.
|
||||
*/
|
||||
object DefaultConfig {
|
||||
fun fetch(): JsonObject {
|
||||
|
||||
@@ -31,7 +31,7 @@ import java.lang.management.ManagementFactory
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 16-03-19.
|
||||
* Created by minjaesong on 15-12-30.
|
||||
*/
|
||||
class Game @Throws(SlickException::class)
|
||||
constructor() : BasicGameState() {
|
||||
@@ -42,7 +42,7 @@ constructor() : BasicGameState() {
|
||||
lateinit var map: GameMap
|
||||
|
||||
/**
|
||||
* Linked list of Actors that is sorted by Actors' referenceID
|
||||
* list of Actors that is sorted by Actors' referenceID
|
||||
*/
|
||||
val ACTORCONTAINER_INITIAL_SIZE = 128
|
||||
val actorContainer = ArrayList<Actor>(ACTORCONTAINER_INITIAL_SIZE)
|
||||
@@ -337,7 +337,7 @@ constructor() : BasicGameState() {
|
||||
val actor = actorContainer[i]
|
||||
val actorIndex = i
|
||||
if (actor is Visible && !actor.inUpdateRange()) {
|
||||
actorContainerInactive.add(actor)
|
||||
actorContainerInactive.add(actor) // duplicates are checked when the actor is re-activated
|
||||
actorContainer.removeAt(actorIndex)
|
||||
actorContainerSize -= 1
|
||||
i-- // array removed 1 elem, so also decrement counter by 1
|
||||
@@ -352,9 +352,6 @@ constructor() : BasicGameState() {
|
||||
private val globalLightByTime: Int
|
||||
get() = getGradientColour(2).getRGB24().rgb24ExpandToRgb30()
|
||||
|
||||
/**
|
||||
* extension function for org.newdawn.slick.Color
|
||||
*/
|
||||
fun Color.getRGB24(): Int = (this.redByte shl 16) or (this.greenByte shl 8) or (this.blueByte)
|
||||
fun Int.rgb24ExpandToRgb30(): Int = (this and 0xff) or
|
||||
(this and 0xff00).ushr(8).shl(10) or
|
||||
@@ -364,12 +361,14 @@ constructor() : BasicGameState() {
|
||||
fun Int.sqr() = this * this
|
||||
private fun distToActorSqr(a: Visible, p: Player): Float =
|
||||
(a.hitbox.centeredX - p.hitbox.centeredX).sqr() + (a.hitbox.centeredY - p.hitbox.centeredY).sqr()
|
||||
private fun Visible.inScreen() = distToActorSqr(this, player) <= (Terrarum.WIDTH.plus(this.hitbox.width.div(2)).times(1 / Terrarum.game.screenZoom).sqr() +
|
||||
Terrarum.HEIGHT.plus(this.hitbox.height.div(2)).times(1 / Terrarum.game.screenZoom).sqr())
|
||||
private fun Visible.inScreen() = distToActorSqr(this, player) <=
|
||||
(Terrarum.WIDTH.plus(this.hitbox.width.div(2)).times(1 / Terrarum.game.screenZoom).sqr() +
|
||||
Terrarum.HEIGHT.plus(this.hitbox.height.div(2)).times(1 / Terrarum.game.screenZoom).sqr())
|
||||
private fun Visible.inUpdateRange() = distToActorSqr(this, player) <= ACTOR_UPDATE_RANGE.sqr()
|
||||
/**
|
||||
* actorContainer extensions
|
||||
*/
|
||||
fun hasActor(actor: Actor) = hasActor(actor.referenceID)
|
||||
fun hasActor(ID: Int): Boolean =
|
||||
if (actorContainer.size == 0)
|
||||
false
|
||||
@@ -377,9 +376,8 @@ constructor() : BasicGameState() {
|
||||
actorContainer.binarySearch(ID) >= 0
|
||||
|
||||
fun removeActor(actor: Actor) = removeActor(actor.referenceID)
|
||||
|
||||
fun removeActor(ID: Int) {
|
||||
if (ID == player.referenceID) throw IllegalArgumentException("Attemped to remove player.")
|
||||
if (ID == player.referenceID) throw RuntimeException("Attempted to remove player.")
|
||||
// get index of the actor and delete by the index.
|
||||
// we can do this as the list is guaranteed to be sorted
|
||||
// and only contains unique values
|
||||
@@ -388,12 +386,13 @@ constructor() : BasicGameState() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add actor and sort the list
|
||||
* Check for duplicates, append actor and sort the list
|
||||
*/
|
||||
fun addActor(actor: Actor): Boolean {
|
||||
if (hasActor(actor.referenceID)) throw IllegalArgumentException("Actor with ID ${actor.referenceID} already exists.")
|
||||
if (hasActor(actor.referenceID))
|
||||
throw RuntimeException("Actor with ID ${actor.referenceID} already exists.")
|
||||
actorContainer.add(actor)
|
||||
insertionSortLastElem(actorContainer) // we can do this as we only add one actor
|
||||
insertionSortLastElem(actorContainer) // we can do this as we are only adding single actor
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -413,7 +412,6 @@ constructor() : BasicGameState() {
|
||||
}
|
||||
|
||||
private fun insertionSortLastElem(arr: ArrayList<Actor>) {
|
||||
|
||||
// 'insertion sort' last element
|
||||
var x: Actor
|
||||
var j: Int
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package net.torvald.terrarum
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 16-03-19.
|
||||
* Created by minjaesong on 15-12-30.
|
||||
*/
|
||||
class GameConfig : KVHashMap()
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.google.gson.JsonPrimitive
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 16-03-19.
|
||||
* Created by minjaesong on 15-12-30.
|
||||
*/
|
||||
open class KVHashMap {
|
||||
|
||||
|
||||
@@ -19,7 +19,6 @@ import java.util.logging.SimpleFormatter
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 15-12-30.
|
||||
* Kotlin code: Created by minjaesong on 16-03-19.
|
||||
*/
|
||||
class Terrarum @Throws(SlickException::class)
|
||||
constructor(gamename: String) : StateBasedGame(gamename) {
|
||||
|
||||
@@ -19,6 +19,6 @@ class Batch : ConsoleCommand {
|
||||
}
|
||||
|
||||
override fun printUsage() {
|
||||
Echo().execute("batch path/to/batch.txt")
|
||||
Echo().execute("Usage: batch path/to/batch.txt")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ object CommandDict {
|
||||
|
||||
internal var dict: HashMap<String, ConsoleCommand> = hashMapOf(
|
||||
Pair("echo", Echo()),
|
||||
Pair("error", Error()),
|
||||
Pair("setav", SetAV()),
|
||||
Pair("qqq", QuitApp()),
|
||||
Pair("codex", CodexEdictis()),
|
||||
|
||||
@@ -25,6 +25,7 @@ object CommandInterpreter {
|
||||
fun execute(command: String) {
|
||||
val cmd: Array<CommandInput?> = parse(command)
|
||||
val echo = Echo()
|
||||
val error = Error()
|
||||
|
||||
for (single_command in cmd) {
|
||||
var commandObj: ConsoleCommand? = null
|
||||
@@ -61,7 +62,7 @@ object CommandInterpreter {
|
||||
catch (e: Exception) {
|
||||
System.err.print("[CommandInterpreter] ")
|
||||
e.printStackTrace()
|
||||
echo.error(Lang["ERROR_GENERIC_TEXT"])
|
||||
error.execute(Lang["ERROR_GENERIC_TEXT"])
|
||||
}
|
||||
|
||||
}
|
||||
@@ -103,7 +104,7 @@ object CommandInterpreter {
|
||||
val sb = StringBuilder()
|
||||
val formatter = Formatter(sb)
|
||||
|
||||
Echo().error(
|
||||
Error().execute(
|
||||
formatter.format(Lang["DEV_MESSAGE_CONSOLE_COMMAND_UNKNOWN"], cmdname).toString())
|
||||
}
|
||||
|
||||
|
||||
@@ -12,19 +12,13 @@ import java.util.Arrays
|
||||
internal class Echo : ConsoleCommand {
|
||||
override fun execute(args: Array<String>) {
|
||||
val argsWoHeader = Array<String>(args.size - 1, {it -> args[it + 1]})
|
||||
|
||||
argsWoHeader.forEach(
|
||||
{ (Terrarum.game.consoleHandler.UI as ConsoleWindow).sendMessage(it) })
|
||||
argsWoHeader.forEach { execute(it) }
|
||||
}
|
||||
|
||||
fun execute(single_line: String) {
|
||||
(Terrarum.game.consoleHandler.UI as ConsoleWindow).sendMessage(single_line)
|
||||
}
|
||||
|
||||
fun error(single_line: String) {
|
||||
(Terrarum.game.consoleHandler.UI as ConsoleWindow).sendMessage("${GameFontBase.colToCode["r"]}$single_line")
|
||||
}
|
||||
|
||||
override fun printUsage() {
|
||||
|
||||
}
|
||||
|
||||
23
src/net/torvald/terrarum/console/Error.kt
Normal file
23
src/net/torvald/terrarum/console/Error.kt
Normal file
@@ -0,0 +1,23 @@
|
||||
package net.torvald.terrarum.console
|
||||
|
||||
import net.torvald.imagefont.GameFontBase
|
||||
import net.torvald.terrarum.Terrarum
|
||||
import net.torvald.terrarum.ui.ConsoleWindow
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 16-04-25.
|
||||
*/
|
||||
class Error : ConsoleCommand {
|
||||
override fun execute(args: Array<String>) {
|
||||
val argsWoHeader = Array<String>(args.size - 1, {it -> args[it + 1]})
|
||||
argsWoHeader.forEach { execute(it) }
|
||||
}
|
||||
|
||||
fun execute(single_line: String) {
|
||||
(Terrarum.game.consoleHandler.UI as ConsoleWindow).sendMessage("${GameFontBase.colToCode["r"]}$single_line")
|
||||
}
|
||||
|
||||
override fun printUsage() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ class GetAV : ConsoleCommand {
|
||||
|
||||
override fun execute(args: Array<String>) {
|
||||
val echo = Echo()
|
||||
val error = Error()
|
||||
|
||||
try {
|
||||
if (args.size == 1) {
|
||||
@@ -87,11 +88,11 @@ class GetAV : ConsoleCommand {
|
||||
}
|
||||
catch (e: NullPointerException) {
|
||||
if (args.size == 2) {
|
||||
echo.error(args[1] + ": actor value does not exist.")
|
||||
error.execute(args[1] + ": actor value does not exist.")
|
||||
System.err.println("[GetAV] ${args[1]}: actor value does not exist.")
|
||||
}
|
||||
else if (args.size == 3) {
|
||||
echo.error(args[2] + ": actor value does not exist.")
|
||||
error.execute(args[2] + ": actor value does not exist.")
|
||||
System.err.println("[GetAV] ${args[2]}: actor value does not exist.")
|
||||
}
|
||||
else {
|
||||
@@ -99,7 +100,7 @@ class GetAV : ConsoleCommand {
|
||||
}
|
||||
}
|
||||
catch (e1: IllegalArgumentException) {
|
||||
echo.error("${args[1]}: no actor with this ID.")
|
||||
error.execute("${args[1]}: no actor with this ID.")
|
||||
System.err.println("[GetAV] ${args[1]}: no actor with this ID.")
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ class GetFactioning : ConsoleCommand {
|
||||
|
||||
override fun execute(args: Array<String>) {
|
||||
val echo = Echo()
|
||||
val error = Error()
|
||||
|
||||
fun printOutFactioning(id: Int) {
|
||||
val a = Terrarum.game.getActorByID(id)
|
||||
@@ -75,7 +76,7 @@ class GetFactioning : ConsoleCommand {
|
||||
}
|
||||
}
|
||||
else {
|
||||
echo.error("The actor is not factionable.")
|
||||
error.execute("The actor is not factionable.")
|
||||
System.err.println("[GetFactioning] The actor is not factionable.")
|
||||
}
|
||||
}
|
||||
@@ -85,7 +86,7 @@ class GetFactioning : ConsoleCommand {
|
||||
}
|
||||
else {
|
||||
if (!args[1].isNum()) {
|
||||
echo.error("Invalid actor ID input.")
|
||||
error.execute("Invalid actor ID input.")
|
||||
System.err.println("[GetFactioning] Invalid actor ID input.")
|
||||
return
|
||||
}
|
||||
@@ -94,7 +95,7 @@ class GetFactioning : ConsoleCommand {
|
||||
printOutFactioning(actorID)
|
||||
}
|
||||
catch (e: IllegalArgumentException) {
|
||||
echo.error("${args[1]}: no actor with this ID.")
|
||||
error.execute("${args[1]}: no actor with this ID.")
|
||||
System.err.println("[GetFactioning] ${args[1]}: no actor with this ID.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,6 +54,7 @@ internal class SetAV : ConsoleCommand {
|
||||
}
|
||||
|
||||
val echo = Echo()
|
||||
val error = Error()
|
||||
|
||||
// setav <id, or blank for player> <av> <val>
|
||||
if (args.size != 4 && args.size != 3) {
|
||||
@@ -64,7 +65,7 @@ internal class SetAV : ConsoleCommand {
|
||||
|
||||
// check if av is number
|
||||
if (args[1].isNum()) {
|
||||
echo.error("Illegal ActorValue ${args[1]}: ActorValue cannot be a number.")
|
||||
error.execute("Illegal ActorValue ${args[1]}: ActorValue cannot be a number.")
|
||||
System.err.println("[SetAV] Illegal ActorValue ${args[1]}: ActorValue cannot be a number.")
|
||||
return
|
||||
}
|
||||
@@ -81,7 +82,7 @@ internal class SetAV : ConsoleCommand {
|
||||
|
||||
// check if av is number
|
||||
if (args[2].isNum()) {
|
||||
echo.error("Illegal ActorValue ${args[2]}: ActorValue cannot be a number.")
|
||||
error.execute("Illegal ActorValue ${args[2]}: ActorValue cannot be a number.")
|
||||
System.err.println("[SetAV] Illegal ActorValue ${args[2]}: ActorValue cannot be a number.")
|
||||
return
|
||||
}
|
||||
@@ -92,7 +93,7 @@ internal class SetAV : ConsoleCommand {
|
||||
}
|
||||
catch (e: IllegalArgumentException) {
|
||||
if (args.size == 4) {
|
||||
echo.error("${args[1]}: no actor with this ID.")
|
||||
error.execute("${args[1]}: no actor with this ID.")
|
||||
System.err.println("[SetAV] ${args[1]}: no actor with this ID.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,18 +9,7 @@ import net.torvald.terrarum.Terrarum
|
||||
class SetTime : ConsoleCommand {
|
||||
override fun execute(args: Array<String>) {
|
||||
if (args.size == 2) {
|
||||
val lowercaseTime = args[1].toLowerCase()
|
||||
val timeToSet =
|
||||
if (args[1].length >= 4) {
|
||||
lowercaseTime.substringBefore('h').toInt() * WorldTime.HOUR_SEC +
|
||||
lowercaseTime.substringAfter('h').toInt() * WorldTime.MINUTE_SEC
|
||||
}
|
||||
else if (args[1].endsWith("h", true)) {
|
||||
lowercaseTime.substring(0, args[1].length - 1).toInt() * WorldTime.HOUR_SEC
|
||||
}
|
||||
else {
|
||||
lowercaseTime.toInt()
|
||||
}
|
||||
val timeToSet = WorldTime.parseTime(args[1])
|
||||
|
||||
Terrarum.game.map.worldTime.setTime(timeToSet)
|
||||
|
||||
|
||||
@@ -6,8 +6,14 @@ import net.torvald.terrarum.Terrarum
|
||||
* Created by minjaesong on 16-03-20.
|
||||
*/
|
||||
class SetTimeDelta : ConsoleCommand {
|
||||
|
||||
val HARD_LIMIT = 60
|
||||
|
||||
override fun execute(args: Array<String>) {
|
||||
if (args.size == 2) {
|
||||
if (args[1].toInt() > HARD_LIMIT)
|
||||
Error().execute("Delta too large -- acceptable delta is 0-60.")
|
||||
|
||||
Terrarum.game.map.worldTime.setTimeDelta(args[1].toInt())
|
||||
if (Terrarum.game.map.worldTime.timeDelta == 0)
|
||||
Echo().execute("時間よ止まれ!ザ・ワルド!!")
|
||||
|
||||
@@ -91,7 +91,7 @@ class ActorInventory() {
|
||||
var weight = 0f
|
||||
|
||||
for (item in itemList.entries) {
|
||||
weight += ItemPropCodex.getItem(item.key).mass * item.value
|
||||
weight += ItemPropCodex.getProp(item.key).mass * item.value
|
||||
}
|
||||
|
||||
return weight
|
||||
|
||||
@@ -1,15 +1,25 @@
|
||||
package net.torvald.terrarum.gameactors
|
||||
|
||||
import net.torvald.terrarum.itemproperties.ItemPropCodex
|
||||
import net.torvald.terrarum.tileproperties.TilePropCodex
|
||||
import org.newdawn.slick.GameContainer
|
||||
import org.newdawn.slick.Graphics
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 16-03-15.
|
||||
*/
|
||||
class DroppedItem constructor() : ActorWithBody() {
|
||||
class DroppedItem constructor(itemID: Int) : ActorWithBody() {
|
||||
|
||||
init {
|
||||
if (itemID >= ItemPropCodex.ITEM_UNIQUE_MAX)
|
||||
throw RuntimeException("Attempted to create DroppedItem actor of a real actor; the real actor must be dropped instead.")
|
||||
|
||||
isVisible = true
|
||||
|
||||
mass = if (itemID < 4096)
|
||||
TilePropCodex.getProp(itemID).density / 1000f
|
||||
else
|
||||
ItemPropCodex.getProp(itemID).mass
|
||||
}
|
||||
|
||||
override fun update(gc: GameContainer, delta_t: Int) {
|
||||
@@ -17,6 +27,6 @@ class DroppedItem constructor() : ActorWithBody() {
|
||||
}
|
||||
|
||||
override fun drawBody(gc: GameContainer, g: Graphics) {
|
||||
drawBody(gc, g)
|
||||
|
||||
}
|
||||
}
|
||||
@@ -149,5 +149,17 @@ class WorldTime {
|
||||
val MINUTE_SEC: Int = 60
|
||||
val HOUR_MIN: Int = 60
|
||||
val GAME_MIN_TO_REAL_SEC: Float = 60f
|
||||
|
||||
fun parseTime(s: String): Int =
|
||||
if (s.length >= 4) {
|
||||
s.toLowerCase().substringBefore('h').toInt() * WorldTime.HOUR_SEC +
|
||||
s.toLowerCase().substringAfter('h').toInt() * WorldTime.MINUTE_SEC
|
||||
}
|
||||
else if (s.endsWith("h", true)) {
|
||||
s.toLowerCase().substring(0, s.length - 1).toInt() * WorldTime.HOUR_SEC
|
||||
}
|
||||
else {
|
||||
s.toInt()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,9 +19,8 @@ object ItemPropCodex {
|
||||
*/
|
||||
private lateinit var itemCodex: Array<InventoryItem>
|
||||
|
||||
@JvmStatic val ITEM_UNIQUE_MAX = 32768
|
||||
const val ITEM_UNIQUE_MAX = 32768
|
||||
|
||||
@JvmStatic
|
||||
fun buildItemProp() {
|
||||
itemCodex = arrayOf<InventoryItem>()
|
||||
|
||||
@@ -29,14 +28,14 @@ object ItemPropCodex {
|
||||
|
||||
}
|
||||
|
||||
fun getItem(code: Int): InventoryItem {
|
||||
if (code < ITEM_UNIQUE_MAX)
|
||||
fun getProp(code: Int): InventoryItem {
|
||||
if (code < ITEM_UNIQUE_MAX) // generic item
|
||||
return itemCodex[code]
|
||||
else {
|
||||
val a = Terrarum.game.getActorByID(code)
|
||||
val a = Terrarum.game.getActorByID(code) // actor item
|
||||
if (a is CanBeAnItem) return a.itemData
|
||||
|
||||
throw NullPointerException()
|
||||
throw IllegalArgumentException("Attempted to get item data of actor that cannot be an item. ($a)")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user