Skip to content

Commit

Permalink
Make stackable items and add alternative door sounds (#516)
Browse files Browse the repository at this point in the history
* Rename Postgresql to DatabaseStorage

* Add missing heals data

* Fix overriding npc attack animations

* Fix clear weak not removing pending weak actions

* Add item on item maximum for making stackable items #502

* Add materials to doors for different sounds #511
  • Loading branch information
GregHib committed Apr 11, 2024
1 parent 886b11f commit eb6aa4a
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 30 deletions.
10 changes: 5 additions & 5 deletions data/definitions/items.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10350,7 +10350,7 @@ premade_drunk_dragon:
id: 2032
price: 35
limit: 1000
heals: 40
heals: 50
excess: cocktail_glass
weight: 0.45
examine: "A premade Drunk Dragon."
Expand Down Expand Up @@ -10378,7 +10378,7 @@ premade_short_green_guy:
id: 2038
price: 30
limit: 1000
heals: 30
heals: 50
excess: cocktail_glass
weight: 0.45
examine: "A premade Short Green Guy."
Expand All @@ -10387,7 +10387,7 @@ premade_wizard_blizzard:
id: 2040
price: 27
limit: 1000
heals: 30
heals: 50
excess: cocktail_glass
weight: 0.45
examine: "A Premade Wizard Blizzard."
Expand Down Expand Up @@ -18905,7 +18905,7 @@ nettles:
kept: "Reclaim"
cup_of_tea_nettle_tea:
id: 4242
heals: 0
heals: 30
excess: empty_cup
tradeable: false
weight: 0.15
Expand Down Expand Up @@ -31197,7 +31197,7 @@ tinderbox_33:
id: 7156
braindeath_rum:
id: 7157
heals: 0
heals: 14
tradeable: false
weight: 0.907
examine: "I think it is eating through the bottle."
Expand Down
8 changes: 4 additions & 4 deletions data/definitions/sounds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ equip_silverlight: 2990
equip_jester: 3395
equip_surok_ring: 3525
equip_darkbow: 3738
close_door: 80
open_door: 81
close_gate: 70
open_gate: 71
door_close: 80
door_open: 81
gate_close: 70
gate_open: 71
fell_tree: 2734
drop_item: 2739
pickup_item: 2582
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import world.gregs.voidps.engine.data.SafeStorage
import world.gregs.voidps.engine.data.SaveQueue
import world.gregs.voidps.engine.data.definition.*
import world.gregs.voidps.engine.data.json.FileStorage
import world.gregs.voidps.engine.data.sql.PostgresStorage
import world.gregs.voidps.engine.data.sql.DatabaseStorage
import world.gregs.voidps.engine.entity.character.npc.NPCs
import world.gregs.voidps.engine.entity.character.npc.hunt.Hunting
import world.gregs.voidps.engine.entity.character.player.Players
Expand Down Expand Up @@ -52,15 +52,15 @@ val engineModule = module {
// IO
single { Yaml(YamlReaderConfiguration(2, 8, VERY_FAST_LOAD_FACTOR)) }
single { if (getProperty("storage", "") == "database") {
PostgresStorage.connect(
DatabaseStorage.connect(
getProperty("database_username"),
getProperty("database_password"),
getProperty("database_driver"),
getProperty("database_jdbc_url"),
getProperty("database_pool", "2").toInt(),
)
val definitions: ItemDefinitions = get()
PostgresStorage { definitions.get(it) }
DatabaseStorage { definitions.get(it) }
} else {
val saves = File(getProperty<String>("savePath"))
if (!saves.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import world.gregs.voidps.engine.entity.item.Item
* @param message to send
* @param failure message
* @param question override for make-x question
* @param maximum the maximum number of make-x allowed
*/
data class ItemOnItemDefinition(
val skill: Skill? = null,
Expand All @@ -42,7 +43,8 @@ data class ItemOnItemDefinition(
val sound: String = "",
val message: String = "",
val failure: String = "",
val question: String = "How many would you like to $type?"
val question: String = "How many would you like to $type?",
val maximum: Int = -1
) {

companion object {
Expand All @@ -66,7 +68,8 @@ data class ItemOnItemDefinition(
sound = map["sound"] as? String ?: EMPTY.sound,
message = map["message"] as? String ?: EMPTY.message,
failure = map["failure"] as? String ?: EMPTY.failure,
question = map["question"] as? String ?: "How many would you like to ${map["type"] as? String ?: EMPTY.type}?"
question = map["question"] as? String ?: "How many would you like to ${map["type"] as? String ?: EMPTY.type}?",
maximum = map["maximum"] as? Int ?: EMPTY.maximum
)

val EMPTY = ItemOnItemDefinition(Skill.Attack)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import world.gregs.voidps.engine.entity.character.player.skill.Skill
import world.gregs.voidps.engine.entity.item.Item
import world.gregs.voidps.type.Tile

class PostgresStorage(
class DatabaseStorage(
private val definitions: (String) -> ItemDefinition = { ItemDefinition.EMPTY }
) : AccountStorage {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class ActionQueue(private val character: Character) : CoroutineScope {
character.suspension = null
action = null
}
pending.removeIf { it.priority == ActionPriority.Weak }
queue.removeIf {
if (it.priority == ActionPriority.Weak) {
it.cancel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
import org.testcontainers.containers.PostgreSQLContainer
import world.gregs.voidps.engine.data.sql.PostgresStorage
import world.gregs.voidps.engine.data.sql.DatabaseStorage

/**
* Starts a postgres database via docker if available or an in-memory emulation if not
Expand All @@ -24,14 +24,14 @@ interface DatabaseTest {
@BeforeEach
fun setup() {
transaction {
SchemaUtils.create(*PostgresStorage.tables, inBatch = true)
SchemaUtils.create(*DatabaseStorage.tables, inBatch = true)
}
}

@AfterEach
fun tidy() {
transaction {
SchemaUtils.drop(*PostgresStorage.tables, inBatch = true)
SchemaUtils.drop(*DatabaseStorage.tables, inBatch = true)
}
}

Expand Down Expand Up @@ -63,7 +63,7 @@ interface DatabaseTest {
driverClassName = "org.testcontainers.jdbc.ContainerDatabaseDriver",
dialect = "testcontainers"
)
PostgresStorage.connect(
DatabaseStorage.connect(
username = "root",
password = "password",
driver = "org.testcontainers.jdbc.ContainerDatabaseDriver",
Expand All @@ -76,7 +76,7 @@ interface DatabaseTest {
db = EmbeddedPostgres.builder()
.setPort(TEST_PORT)
.start()
PostgresStorage.connect(
DatabaseStorage.connect(
username = "postgres",
password = "password",
driver = "org.postgresql.Driver",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import org.junit.jupiter.api.assertThrows
import world.gregs.voidps.engine.data.AccountStorageTest
import world.gregs.voidps.engine.data.DatabaseTest

class PostgresStorageTest : AccountStorageTest(), DatabaseTest {
class DatabaseStorageTest : AccountStorageTest(), DatabaseTest {

override val storage = PostgresStorage()
override val storage = DatabaseStorage()

@Test
fun `Saving variable with invalid format throws exception`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ fun makeImmediately(player: Player, overlaps: List<ItemOnItemDefinition>, maximu
if (overlaps.size != 1) {
return false
}
val stackable = overlaps.first().remove.all { inventory.stackable(it.id) } && overlaps.first().one.all { inventory.stackable(it.id) }
val definition = overlaps.first()
val stackable = definition.maximum == -1 &&definition.remove.all { inventory.stackable(it.id) } && definition.one.all { inventory.stackable(it.id) }
return stackable || maximum == 1 || player["selecting_amount", false] || player.inCombat
}

Expand All @@ -152,6 +153,9 @@ fun getMaximum(overlaps: List<ItemOnItemDefinition>, player: Player): Int {
if (min > max) {
max = min
}
if (overlap.maximum < max) {
max = overlap.maximum
}
}
return max
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fun attackAnimation(npc: NPC): String {
if (npc.race.isNotEmpty()) {
return "${npc.race}_attack"
}
return npc.def.getOrNull("hit_anim") ?: ""
return npc.def["attack_anim", ""]
}

fun attackSound(npc: NPC): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,54 +58,64 @@ object Door {
fun closeDoor(player: Player, door: GameObject, def: ObjectDefinition = door.def, ticks: Int = doorResetDelay, collision: Boolean = true): Boolean {
val double = DoubleDoor.get(door, def, 1)
if (resetExisting(door, double)) {
player.playSound(if (def.isGate()) "close_gate" else "close_door")
closeSound(player, def, gate = def.isGate())
return true
}

// Single door
if (double == null && door.id.endsWith("_opened")) {
replace(door, def, "_opened", "_closed", 0, 3, ticks, collision)
player.playSound("close_door")
closeSound(player, def, gate = false)
return true
}

// Double doors
if (double != null && door.id.endsWith("_opened") && double.id.endsWith("_opened")) {
DoubleDoor.close(door, def, double, ticks, collision)
player.playSound("close_door")
closeSound(player, def, gate = false)
return true
}
player.message("The ${def.name.lowercase()} won't budge.")
return false
}

private fun closeSound(player: Player, definition: ObjectDefinition, gate: Boolean) {
val material = if (definition.contains("material")) "${definition["material", "wood"]}_" else ""
player.playSound(if (gate) "${material}gate_close" else "${material}door_close")
}

/**
* Attempt to open [door]
*/
fun openDoor(player: Player, door: GameObject, def: ObjectDefinition = door.def, ticks: Int = doorResetDelay, collision: Boolean = true): Boolean {
val double = DoubleDoor.get(door, def, 0)
if (resetExisting(door, double)) {
player.playSound(if (def.isGate()) "open_gate" else "open_door")
openSound(player, def, gate = def.isGate())
return true
}

// Single door
if (double == null && door.id.endsWith("_closed")) {
replace(door, def, "_closed", "_opened", 1, 1, ticks, collision)
player.playSound("open_door")
openSound(player, def, gate = false)
return true
}

// Double doors
if (double != null && door.id.endsWith("_closed") && double.id.endsWith("_closed")) {
DoubleDoor.open(door, def, double, ticks, collision)
player.playSound("open_door")
openSound(player, def, gate = false)
return true
}
player.message("The ${def.name.lowercase()} won't budge.")
return false
}

private fun openSound(player: Player, definition: ObjectDefinition, gate: Boolean) {
val material = if (definition.contains("material")) "${definition["material", "wood"]}_" else ""
player.playSound(if (gate) "${material}gate_open" else "${material}door_open")
}

private fun resetExisting(obj: GameObject, double: GameObject?): Boolean {
val objects: GameObjects = get()
if (double == null && objects.timers.execute(obj)) {
Expand Down

0 comments on commit eb6aa4a

Please sign in to comment.