fixed a bug where a dynamic item would not get saved/loaded at all

This commit is contained in:
minjaesong
2022-02-22 17:12:49 +09:00
parent 1787ad7cdd
commit df6950c0b8
30 changed files with 206 additions and 89 deletions

View File

@@ -133,9 +133,10 @@ class TitleScreen(batch: SpriteBatch) : IngameInstance(batch) {
try {
val reader = java.io.FileReader(ModMgr.getFile("basegame", "demoworld"))
val file = ModMgr.getFile("basegame", "demoworld")
val reader = java.io.FileReader(file)
//ReadWorld.readWorldAndSetNewWorld(Terrarum.ingame!! as TerrarumIngame, reader)
val world = ReadWorld.readLayerFormat(reader)
val world = ReadWorld.readLayerFormat(reader, file)
demoWorld = world
printdbg(this, "Demo world loaded")
}

View File

@@ -0,0 +1,20 @@
package net.torvald.terrarum.modulebasegame.console
import net.torvald.terrarum.*
import net.torvald.terrarum.console.ConsoleCommand
import net.torvald.terrarum.console.Echo
import net.torvald.terrarum.serialise.Common
/**
* Created by minjaesong on 2022-02-22.
*/
object DynToStatic : ConsoleCommand {
override fun execute(args: Array<String>) {
ItemCodex.dynamicToStaticTable.forEach { (d,s) ->
Echo("$ccG$d$ccW$ccY$s")
}
}
override fun printUsage() {
}
}

View File

@@ -7,6 +7,7 @@ import net.torvald.terrarum.console.Echo
import net.torvald.terrarum.modulebasegame.TerrarumIngame
import net.torvald.terrarum.serialise.ReadActor
import net.torvald.terrarum.serialise.ReadWorld
import java.io.File
import java.io.IOException
/**
@@ -16,8 +17,9 @@ object ImportWorld : ConsoleCommand {
override fun execute(args: Array<String>) {
if (args.size == 2) {
try {
val reader = java.io.FileReader(App.defaultDir + "/Exports/${args[1]}.json")
ReadWorld.readWorldAndSetNewWorld(Terrarum.ingame!! as TerrarumIngame, reader)
val file = File(App.defaultDir + "/Exports/${args[1]}.json")
val reader = java.io.FileReader(file)
ReadWorld.readWorldAndSetNewWorld(Terrarum.ingame!! as TerrarumIngame, reader, file)
Echo("Importworld: imported a world from ${args[1]}.json")
}
catch (e: IOException) {

View File

@@ -28,6 +28,8 @@ class ActorInventory() : FixtureInventory() {
/**
* List of all equipped items (tools, armours, rings, necklaces, etc.)
*
* It's your responsibility to make sure currently equipped item also exists in the `super.itemList`
*
* The ItemID must be `dynamicID`
*/
val itemEquipped = Array<ItemID?>(GameItem.EquipPosition.INDEX_MAX) { null }
@@ -62,7 +64,7 @@ class ActorInventory() : FixtureInventory() {
}
}
fun getQuickslotItem(slot: Int): InventoryPair? = invSearchByDynamicID(quickSlot[slot])
fun getQuickslotItem(slot: Int): InventoryPair? = searchByID(quickSlot[slot])
fun consumeItem(item: GameItem) {
val actor = this.actor as Actor

View File

@@ -4,10 +4,9 @@ import net.torvald.terrarum.ItemCodex
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.gameitems.GameItem
import net.torvald.terrarum.gameitems.ItemID
import net.torvald.terrarum.lock
import net.torvald.terrarum.modulebasegame.TerrarumIngame
import net.torvald.util.SortedArrayList
import java.math.BigInteger
import java.util.concurrent.locks.ReentrantLock
/**
* Created by minjaesong on 2021-03-16.
@@ -32,7 +31,7 @@ open class FixtureInventory() {
/**
* Sorted by referenceID.
*/
val itemList = ArrayList<InventoryPair>()
val itemList = SortedArrayList<InventoryPair>()
var wallet = BigInteger("0") // unified currency for whole civs; Dwarf Fortress approach seems too complicated
fun isEmpty() = getTotalCount() == 0L
@@ -66,7 +65,7 @@ open class FixtureInventory() {
// If we already have the item, increment the amount
// If not, add item with specified amount
val existingItem = invSearchByDynamicID(item.dynamicID)
val existingItem = searchByID(item.dynamicID)
// if the item already exists
if (existingItem != null) {
@@ -80,7 +79,7 @@ open class FixtureInventory() {
else {
itemList.add(InventoryPair(item.dynamicID, count))
}
insertionSortLastElem(itemList)
// insertionSortLastElem(itemList)
}
open fun remove(itemID: ItemID, count: Long) = remove(ItemCodex[itemID]!!, count) {}
@@ -102,7 +101,7 @@ open class FixtureInventory() {
val existingItem = invSearchByDynamicID(item.dynamicID)
val existingItem = searchByID(item.dynamicID)
if (existingItem != null) { // if the item already exists
val newCount = existingItem.qty - count
@@ -171,28 +170,14 @@ open class FixtureInventory() {
if (itemList.size == 0)
false
else
itemList.binarySearch(id, DYNAMIC_ID) >= 0
fun invSearchByDynamicID(id: ItemID?): InventoryPair? {
itemList.contains(InventoryPair(id, 1))
fun searchByID(id: ItemID?): InventoryPair? {
if (itemList.size == 0 || id == null)
return null
val index = itemList.binarySearch(id, DYNAMIC_ID)
if (index < 0)
return null
else
return itemList[index]
return itemList.searchFor(id) { it.itm }
}
protected fun invSearchByStaticID(id: ItemID?): InventoryPair? {
if (itemList.size == 0 || id == null)
return null
val index = itemList.binarySearch(id, STATIC_ID)
if (index < 0)
return null
else
return itemList[index]
}
protected fun insertionSortLastElem(arr: ArrayList<InventoryPair>) {
/*protected fun insertionSortLastElem(arr: ArrayList<InventoryPair>) {
ReentrantLock().lock {
var j = arr.lastIndex - 1
val x = arr.last()
@@ -202,7 +187,7 @@ open class FixtureInventory() {
}
arr[j + 1] = x
}
}
}*/
@Transient private val STATIC_ID = 41324534
@Transient private val DYNAMIC_ID = 181643953
protected fun ArrayList<InventoryPair>.binarySearch(ID: ItemID, searchMode: Int): Int {
@@ -229,7 +214,7 @@ open class FixtureInventory() {
}
}
class InventoryPair {
class InventoryPair : Comparable<InventoryPair> {
var itm: ItemID = ""; private set
var qty: Long = 0
@@ -244,4 +229,5 @@ class InventoryPair {
operator fun component1() = itm
operator fun component2() = qty
override fun compareTo(other: InventoryPair) = this.itm.compareTo(other.itm)
}

View File

@@ -11,6 +11,9 @@ import net.torvald.terrarum.App
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.gameactors.AVKey
import net.torvald.terrarum.gameitems.GameItem
import net.torvald.terrarum.gameitems.ItemID
import net.torvald.terrarum.itemproperties.ItemRemapTable
import net.torvald.terrarum.itemproperties.ItemTable
import net.torvald.terrarum.savegame.DiskSkimmer
import net.torvald.terrarum.savegame.SimpleFileSystem
import net.torvald.terrarum.utils.PlayerLastStatus
@@ -33,6 +36,9 @@ class IngamePlayer : ActorHumanoid, HasAssembledSprite {
val uuid = UUID.randomUUID()
var worldCurrentlyPlaying: UUID = UUID(0L,0L) // only filled up on save and load; DO NOT USE THIS
internal val dynamicItemInventory = ItemTable()
internal val dynamicToStaticTable = ItemRemapTable()
@Transient override var spriteHeadTexture: TextureRegion? = null

View File

@@ -111,6 +111,7 @@ object PickaxeCore {
* Created by minjaesong on 2017-07-17.
*/
class PickaxeCopper(originalID: ItemID) : GameItem(originalID) {
internal constructor() : this("-uninitialised-")
override val originalName = "PACKAGED_PICK"
override var baseToolSize: Double? = BASE_MASS_AND_SIZE
@@ -138,6 +139,7 @@ class PickaxeCopper(originalID: ItemID) : GameItem(originalID) {
* Created by minjaesong on 2019-03-10.
*/
class PickaxeIron(originalID: ItemID) : GameItem(originalID) {
internal constructor() : this("-uninitialised-")
override val originalName = "PACKAGED_PICK"
override var baseToolSize: Double? = BASE_MASS_AND_SIZE
@@ -165,6 +167,7 @@ class PickaxeIron(originalID: ItemID) : GameItem(originalID) {
* Created by minjaesong on 2019-03-10.
*/
class PickaxeSteel(originalID: ItemID) : GameItem(originalID) {
internal constructor() : this("-uninitialised-")
override val originalName = "PACKAGED_PICK"
override var baseToolSize: Double? = BASE_MASS_AND_SIZE

View File

@@ -26,5 +26,8 @@ object WeaponMeleeCore {
}
abstract class WeaponMeleeBase(originalID: ItemID) : GameItem(originalID) {
internal constructor() : this("-uninitialised-")
abstract val velocityMod: Double
}

View File

@@ -19,7 +19,7 @@ object AmmoMeterProxy {
else {
meter.vitalGetterVal = {
if (currentItem.stackable && currentItem.maxDurability == GameItem.DURABILITY_NA) {
actor.inventory.invSearchByDynamicID(currentItem.dynamicID)!!.qty.toFloat()
actor.inventory.searchByID(currentItem.dynamicID)!!.qty.toFloat()
}
else
currentItem.durability

View File

@@ -10,9 +10,7 @@ import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.CELLS_HO
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.CELLS_VRT
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.INVENTORY_CELLS_OFFSET_X
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.INVENTORY_CELLS_OFFSET_Y
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.INVEN_DEBUG_MODE
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.controlHelpHeight
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.internalHeight
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.internalWidth
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryItemGrid.Companion.createInvCellGenericKeyDownFun
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryItemGrid.Companion.createInvCellGenericTouchDownFun
@@ -136,7 +134,7 @@ internal class UIInventoryCells(
)
// debug text
batch.color = Color.LIGHT_GRAY
if (INVEN_DEBUG_MODE) {
if (App.IS_DEVELOPMENT_BUILD) {
App.fontSmallNumbers.draw(batch,
"${full.actor.inventory.capacity}/${full.actor.inventory.maxCapacity}",
encumbBarTextXPos,

View File

@@ -38,8 +38,6 @@ class UIInventoryFull(
val CELL_COL = Toolkit.Theme.COL_CELL_FILL
const val INVEN_DEBUG_MODE = false
const val REQUIRED_MARGIN: Int = 138 // hard-coded value. Don't know the details. Range: [91-146]. I chose MAX-8 because cell gap is 8
const val CELLS_HOR = 10
val CELLS_VRT: Int; get() = (App.scr.height - REQUIRED_MARGIN - 134 + UIItemInventoryItemGrid.listGap) / // 134 is another magic number

View File

@@ -125,7 +125,7 @@ class UIItemInventoryEquippedView(
itemGrid[k].equippedSlot = null
}
else {
val itemRecord = it.invSearchByDynamicID(item)!!
val itemRecord = it.searchByID(item)!!
itemGrid[k].item = ItemCodex[item]
itemGrid[k].amount = itemRecord.qty

View File

@@ -13,7 +13,6 @@ import net.torvald.terrarum.modulebasegame.TerrarumIngame
import net.torvald.terrarum.modulebasegame.gameactors.ActorInventory
import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory
import net.torvald.terrarum.modulebasegame.gameactors.InventoryPair
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.INVEN_DEBUG_MODE
import net.torvald.terrarum.ui.Toolkit
import net.torvald.terrarum.ui.UICanvas
import net.torvald.terrarum.ui.UIItem
@@ -162,6 +161,9 @@ class UIItemInventoryItemGrid(
listRebuildFun()
}
}
// COMMON variables because more than one instance of this can be up on the screen
private val tooltipShowing = HashMap<UIItemInventoryItemGrid, Boolean>()
}
private val itemGrid = Array<UIItemInventoryCellBase>(horizontalCells * verticalCells) {
@@ -347,8 +349,8 @@ class UIItemInventoryItemGrid(
override fun update(delta: Float) {
super.update(delta)
var tooltipSet = false
tooltipShowing[this] = false
items.forEach {
@@ -356,20 +358,21 @@ class UIItemInventoryItemGrid(
// set tooltip accordingly
if (isCompactMode && it.item != null && it.mouseUp && !tooltipSet) {
if ((App.IS_DEVELOPMENT_BUILD || isCompactMode) && it.item != null && it.mouseUp && tooltipShowing[this] != true) {
INGAME.setTooltipMessage(
if (INVEN_DEBUG_MODE) {
it.item?.name + " (${it.item?.originalID}${if (it.item?.originalID == it.item?.dynamicID) "" else "/${it.item?.dynamicID}"})"
if (App.IS_DEVELOPMENT_BUILD) {
it.item?.name + "\n(${it.item?.originalID}${if (it.item?.originalID == it.item?.dynamicID) "" else "/${it.item?.dynamicID}"})"
}
else {
it.item?.name
}
)
tooltipSet = true
tooltipShowing[this] = true
}
}
if (!tooltipSet) {
if (tooltipShowing.values.all { !it }) {
INGAME.setTooltipMessage(null)
}
@@ -461,7 +464,11 @@ class UIItemInventoryItemGrid(
}
override fun dispose() {
tooltipShowing.remove(this)
}
override fun hide() {
tooltipShowing.remove(this)
}
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {