From c10823ddce0beb7bc642b5d52a657cf3f6fd6513 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sun, 29 Dec 2024 19:49:27 +0900 Subject: [PATCH] postal system wip --- .../gameworld/GamePostalService.kt | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/net/torvald/terrarum/modulebasegame/gameworld/GamePostalService.kt diff --git a/src/net/torvald/terrarum/modulebasegame/gameworld/GamePostalService.kt b/src/net/torvald/terrarum/modulebasegame/gameworld/GamePostalService.kt new file mode 100644 index 000000000..345d8bd26 --- /dev/null +++ b/src/net/torvald/terrarum/modulebasegame/gameworld/GamePostalService.kt @@ -0,0 +1,66 @@ +package net.torvald.terrarum.modulebasegame.gameworld + +import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory +import kotlin.math.ceil + +/** + * Note: this implementation of the postal service won't reach outside a world; the attempt to interworld-delivery + * must be made when the other world is being loaded (perhaps using the world loading hook) + * + * Created by minjaesong on 2024-12-29. + */ +class GamePostalService { + + + companion object { + private val reXmlTag = Regex("""<[^>]+>""") + private val reIndents = Regex("""^ +""") + private val reComments = Regex("""""") + + private fun removeTags(xml: String): String { + return xml.replace(reXmlTag, "").replace(reIndents, "").replace(reComments, "") + } + + fun calculateBasePostage(post: Post): Int { + val baseUnits = when (post.postContents.type) { + "text" -> { + post.postContents.contentsRaw.length / 1200.0 + } + "btex" -> { + removeTags(post.postContents.contentsRaw).length / 1200.0 + } + else -> { + TODO() + } + } + + val paperUnit = ceil(baseUnits) + val parcelWeight = ceil(post.parcel?.totalWeight ?: 0.0) // 1 credit per 1 kg + + val extraCharge = when (post.postType) { + 0 -> 1.0 + 128 -> 0.0 + else -> 1.0 + } + + return ceil((paperUnit + parcelWeight) * extraCharge).toInt() + } + } +} + +data class Post( + val sender: String, // an identifier for an actor, an entity, etc.; actor identifier is always "UUID:" + val receiver: String, // an identifier for an actor, an entity, etc.; actor identifier is always "UUID:" + val postmarkDate: Long, // world TIME_T + val postType: Int = 0, // 0: regular post; 128: sent by system/NPCs; may be extended with future additions such as registered post + val postContents: PostContents, + val parcel: FixtureInventory?, +) { + @Transient val basePostage = GamePostalService.calculateBasePostage(this) +} + +data class PostContents( + val type: String, // "text", "btex" + val contentsRaw: String, // plain text for "text"; xml for "btex" + val contentsExtra: Any? = null +) \ No newline at end of file