feat(game): start a wild battle when a player steps in grass (#68)
* feat(game): start a wild battle when a player steps in grass * fix(codegen): key species by national dex so the client shows the right mon
This commit is contained in:
1 parent
300bc52531
commit
109cf5f814
18 files changed
+488
-9
No files matched your search
@@ -108,7 +108,10 @@ constructor(
|
||||
finishBattle(battle)
|
||||
}
|
||||
|
||||
private fun startWildBattle(session: SessionContext, dexId: Int, level: Int) {
|
||||
/** True while the character has a battle running, so callers can skip starting another. */
|
||||
fun inBattle(charId: Long): Boolean = battles.byChar(charId) != null
|
||||
|
||||
fun startWildBattle(session: SessionContext, dexId: Int, level: Int) {
|
||||
val charId = session.attributes[PLAYER_STATE]?.characterId ?: return
|
||||
if (battles.byChar(charId) != null) {
|
||||
session.send(notice("You are already in a battle."))
|
||||
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package de.fiereu.openmmo.server.game.services
|
||||
|
||||
import de.fiereu.network.SessionContext
|
||||
import de.fiereu.openmmo.common.enums.EncounterMethod
|
||||
import de.fiereu.openmmo.common.enums.TileBehavior
|
||||
import de.fiereu.openmmo.maps.MapDef
|
||||
import de.fiereu.openmmo.maps.WildEncounterSlot
|
||||
import de.fiereu.openmmo.maps.WildEncounterTable
|
||||
import de.fiereu.openmmo.server.game.storage.CharacterStore
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
import kotlin.random.Random
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
// The GBA rolls a land encounter out of 2880 with the table rate scaled by 16.
|
||||
private const val ENCOUNTER_ROLL_MAX = 2880
|
||||
private const val ENCOUNTER_RATE_SCALE = 16
|
||||
|
||||
/** Rolls a wild encounter when a player steps onto grass and starts the battle if one is met. */
|
||||
@Singleton
|
||||
class EncounterService
|
||||
@Inject
|
||||
constructor(
|
||||
private val characterStore: CharacterStore,
|
||||
private val battleService: BattleService,
|
||||
) {
|
||||
|
||||
private val random: Random = Random.Default
|
||||
|
||||
/** Called after a completed step. Starts a wild battle if the tile and roll call for one. */
|
||||
fun onStep(session: SessionContext, charId: Long, map: MapDef, x: Int, y: Int) {
|
||||
if (battleService.inBattle(charId)) return
|
||||
val tile = map.tileAt(x, y) ?: return
|
||||
if (!isLandEncounterTile(tile.behavior)) return
|
||||
// TODO: Add water and fishing wild encounters
|
||||
// Water encounters should fire while surfing over water tiles and fishing when a rod is used.
|
||||
// Both need the surf and rod features to exist first. Once they do, branch here on the tile
|
||||
// behavior and roll the WATER or FISHING table the same way land is rolled below.
|
||||
val table = map.encounterTable(EncounterMethod.LAND) ?: return
|
||||
if (!hasUsablePartyMon(charId)) return
|
||||
if (!rollsEncounter(table)) return
|
||||
val slot = pickSlot(table) ?: return
|
||||
val level = random.nextInt(slot.minLevel, slot.maxLevel + 1)
|
||||
log.info {
|
||||
"Wild encounter for char=$charId at ($x, $y): species ${slot.speciesId} level $level"
|
||||
}
|
||||
battleService.startWildBattle(session, slot.speciesId, level)
|
||||
}
|
||||
|
||||
private fun isLandEncounterTile(behavior: TileBehavior): Boolean =
|
||||
behavior == TileBehavior.TALL_GRASS || behavior == TileBehavior.LONG_GRASS
|
||||
|
||||
private fun hasUsablePartyMon(charId: Long): Boolean =
|
||||
characterStore.getCharacter(charId)?.pokemon?.any { it.hp > 0 } ?: false
|
||||
|
||||
private fun rollsEncounter(table: WildEncounterTable): Boolean {
|
||||
val chance = (table.encounterRate * ENCOUNTER_RATE_SCALE).coerceAtMost(ENCOUNTER_ROLL_MAX)
|
||||
return random.nextInt(ENCOUNTER_ROLL_MAX) < chance
|
||||
}
|
||||
|
||||
/** Picks a slot weighted by its encounter rate. */
|
||||
private fun pickSlot(table: WildEncounterTable): WildEncounterSlot? {
|
||||
val total = table.slots.sumOf { it.weight }
|
||||
if (total <= 0) return null
|
||||
var roll = random.nextInt(total)
|
||||
for (slot in table.slots) {
|
||||
roll -= slot.weight
|
||||
if (roll < 0) return slot
|
||||
}
|
||||
return table.slots.lastOrNull()
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ constructor(
|
||||
private val presenceService: PresenceService,
|
||||
private val mapManager: MapManager,
|
||||
private val characterStore: CharacterStore,
|
||||
private val encounterService: EncounterService,
|
||||
) {
|
||||
|
||||
/** One step. The client sends the tile it left and the direction, the server derives the rest. */
|
||||
@@ -120,6 +121,8 @@ constructor(
|
||||
ctx,
|
||||
EntityMovePacket(entityId = charId, x = toX, y = toY, direction = msg.direction),
|
||||
)
|
||||
|
||||
encounterService.onStep(ctx, charId, currentMap, toX, toY)
|
||||
}
|
||||
|
||||
/** Snap the client back to the position the server considers authoritative. */
|
||||
|
||||
Reference in new issue
Block a user