Compare commits

...
Author SHA1 Message Date
Paul 9a0f389c6b experimenting with entity bound battle code 2024-07-08 20:16:03 -04:00
14 changed files with 79 additions and 7 deletions

No files matched your search

@@ -48,6 +48,7 @@ object CobblemonCommands {
PokemonRestartCommand.register(dispatcher) PokemonRestartCommand.register(dispatcher)
BedrockParticleCommand.register(dispatcher) BedrockParticleCommand.register(dispatcher)
OpenDialogueCommand.register(dispatcher) OpenDialogueCommand.register(dispatcher)
ChallengeCommand.register(dispatcher)
// Possibly lock down registration if and only if under dev environment or running in an environment // Possibly lock down registration if and only if under dev environment or running in an environment
// with a certain system environment variable set // with a certain system environment variable set
@@ -64,7 +64,9 @@ class ActivateInstruction(val instructionSet: InstructionSet, val message: Battl
val actionEffect = status?.getActionEffect() ?: return@dispatch GO val actionEffect = status?.getActionEffect() ?: return@dispatch GO
val providers = mutableListOf<Any>(battle) val providers = mutableListOf<Any>(battle)
val pokemon = message.battlePokemon(0, battle) ?: return@dispatch GO val pokemon = message.battlePokemon(0, battle) ?: return@dispatch GO
if (!SwitchInstruction.isVirtual) {
pokemon.effectedPokemon.entity?.let { UsersProvider(it) }?.let(providers::add) pokemon.effectedPokemon.entity?.let { UsersProvider(it) }?.let(providers::add)
}
val context = ActionEffectContext( val context = ActionEffectContext(
actionEffect = actionEffect, actionEffect = actionEffect,
runtime = runtime, runtime = runtime,
@@ -49,7 +49,9 @@ class BoostInstruction(val instructionSet: InstructionSet, val message: BattleMe
val actionEffect = if (isBoost) BOOST_EFFECT else UNBOOST_EFFECT val actionEffect = if (isBoost) BOOST_EFFECT else UNBOOST_EFFECT
val providers = mutableListOf<Any>(battle) val providers = mutableListOf<Any>(battle)
val pokemon = message.battlePokemon(0, battle) ?: return@dispatch GO val pokemon = message.battlePokemon(0, battle) ?: return@dispatch GO
if (!SwitchInstruction.isVirtual) {
pokemon.effectedPokemon.entity?.let { UsersProvider(it) }?.let(providers::add) pokemon.effectedPokemon.entity?.let { UsersProvider(it) }?.let(providers::add)
}
val context = ActionEffectContext( val context = ActionEffectContext(
actionEffect = actionEffect, actionEffect = actionEffect,
runtime = runtime, runtime = runtime,
@@ -53,7 +53,9 @@ class CantInstruction(val message: BattleMessage): ActionEffectInstruction {
val status = Statuses.getStatus(effectID) val status = Statuses.getStatus(effectID)
val actionEffect = status?.getActionEffect() ?: return@dispatch GO val actionEffect = status?.getActionEffect() ?: return@dispatch GO
val providers = mutableListOf<Any>(battle) val providers = mutableListOf<Any>(battle)
if (!SwitchInstruction.isVirtual) {
pokemon.effectedPokemon.entity?.let { UsersProvider(it) }?.let(providers::add) pokemon.effectedPokemon.entity?.let { UsersProvider(it) }?.let(providers::add)
}
val context = ActionEffectContext( val context = ActionEffectContext(
actionEffect = actionEffect, actionEffect = actionEffect,
@@ -107,7 +107,9 @@ class DamageInstruction(
} }
val actionEffect = status?.getActionEffect() ?: return@dispatch GO val actionEffect = status?.getActionEffect() ?: return@dispatch GO
val providers = mutableListOf<Any>(battle) val providers = mutableListOf<Any>(battle)
if (!SwitchInstruction.isVirtual) {
battlePokemon.effectedPokemon.entity?.let { UsersProvider(it) }?.let(providers::add) battlePokemon.effectedPokemon.entity?.let { UsersProvider(it) }?.let(providers::add)
}
val context = ActionEffectContext( val context = ActionEffectContext(
actionEffect = actionEffect, actionEffect = actionEffect,
@@ -132,7 +134,7 @@ class DamageInstruction(
val pokemonName = battlePokemon.getName() val pokemonName = battlePokemon.getName()
val pokemonEntity = battlePokemon.entity val pokemonEntity = battlePokemon.entity
//Play recoil animation if the pokemon recoiling isnt dead //Play recoil animation if the pokemon recoiling isnt dead
if (!causedFaint && pokemonEntity != null) { if (!causedFaint && pokemonEntity != null && !SwitchInstruction.isVirtual) {
val pkt = PlayPoseableAnimationPacket(pokemonEntity.id, setOf("recoil"), emptySet()) val pkt = PlayPoseableAnimationPacket(pokemonEntity.id, setOf("recoil"), emptySet())
pkt.sendToPlayersAround( pkt.sendToPlayersAround(
x = pokemonEntity.x, x = pokemonEntity.x,
@@ -143,7 +145,7 @@ class DamageInstruction(
) )
} }
//Play hit particle //Play hit particle
if (pokemonEntity != null) { if (pokemonEntity != null && !SwitchInstruction.isVirtual) {
RunPosableMoLangPacket(pokemonEntity.id, setOf("q.particle('cobblemon:hit', 'target')")).sendToPlayersAround( RunPosableMoLangPacket(pokemonEntity.id, setOf("q.particle('cobblemon:hit', 'target')")).sendToPlayersAround(
x = pokemonEntity.x, x = pokemonEntity.x,
y = pokemonEntity.y, y = pokemonEntity.y,
@@ -47,7 +47,7 @@ class DragInstruction(val instructionSet: InstructionSet, val battleActor: Battl
val entity = if (battleActor is EntityBackedBattleActor<*>) battleActor.entity else null val entity = if (battleActor is EntityBackedBattleActor<*>) battleActor.entity else null
setOf( setOf(
BattleDispatch { BattleDispatch {
if (entity != null) { if (entity != null && !SwitchInstruction.isVirtual) {
SwitchInstruction.createEntitySwitch(battle, battleActor, entity, pnx, activePokemon, pokemon, illusion, imposter) SwitchInstruction.createEntitySwitch(battle, battleActor, entity, pnx, activePokemon, pokemon, illusion, imposter)
} else { } else {
SwitchInstruction.createNonEntitySwitch(battle, battleActor, pnx, activePokemon, pokemon, illusion) SwitchInstruction.createNonEntitySwitch(battle, battleActor, pnx, activePokemon, pokemon, illusion)
@@ -38,8 +38,10 @@ class EndItemInstruction(val message: BattleMessage): InterpreterInstruction {
battle.minorBattleActions[battlePokemon.uuid] = message battle.minorBattleActions[battlePokemon.uuid] = message
battlePokemon.contextManager.remove(itemEffect.id, BattleContext.Type.ITEM) battlePokemon.contextManager.remove(itemEffect.id, BattleContext.Type.ITEM)
if (message.hasOptionalArgument("eat")) { if (message.hasOptionalArgument("eat")) {
if (!SwitchInstruction.isVirtual) {
battlePokemon.entity?.playSound(CobblemonSounds.BERRY_EAT, 1F, 1F) battlePokemon.entity?.playSound(CobblemonSounds.BERRY_EAT, 1F, 1F)
} }
} }
} }
} }
}
@@ -86,6 +86,7 @@ class MoveInstruction(
battle.majorBattleActions[userPokemon.uuid] = message battle.majorBattleActions[userPokemon.uuid] = message
val providers = mutableListOf<Any>(battle) val providers = mutableListOf<Any>(battle)
if (SwitchInstruction.isVirtual) return@dispatch GO
userPokemon.effectedPokemon.entity?.let { UsersProvider(it) }?.let(providers::add) userPokemon.effectedPokemon.entity?.let { UsersProvider(it) }?.let(providers::add)
targetPokemon?.effectedPokemon?.entity?.let { TargetsProvider(it) }?.let(providers::add) targetPokemon?.effectedPokemon?.entity?.let { TargetsProvider(it) }?.let(providers::add)
val runtime = MoLangRuntime().also { val runtime = MoLangRuntime().also {
@@ -32,8 +32,10 @@ class ReplaceInstruction(val message: BattleMessage): InterpreterInstruction {
val pokemon = message.battlePokemon(0, battle) ?: return val pokemon = message.battlePokemon(0, battle) ?: return
battle.dispatchGo { battle.dispatchGo {
if (!SwitchInstruction.isVirtual) {
val entity = pokemon.entity val entity = pokemon.entity
entity?.let { it.effects.mockEffect?.end(it) } entity?.let { it.effects.mockEffect?.end(it) }
}
battle.sendSidedUpdate( battle.sendSidedUpdate(
source = actor, source = actor,
@@ -48,7 +48,7 @@ class SwitchInstruction(val instructionSet: InstructionSet, val battleActor: Bat
activePokemon.battlePokemon = pokemon activePokemon.battlePokemon = pokemon
activePokemon.illusion = illusion activePokemon.illusion = illusion
val pokemonEntity = pokemon.entity val pokemonEntity = pokemon.entity
if (pokemonEntity == null && entity != null) { if (pokemonEntity == null && entity != null && !isVirtual) {
val targetPos = battleActor.getSide().getOppositeSide().actors.filterIsInstance<EntityBackedBattleActor<*>>().firstOrNull()?.entity?.pos?.let { pos -> val targetPos = battleActor.getSide().getOppositeSide().actors.filterIsInstance<EntityBackedBattleActor<*>>().firstOrNull()?.entity?.pos?.let { pos ->
val offset = pos.subtract(entity.pos) val offset = pos.subtract(entity.pos)
val idealPos = entity.pos.add(offset.multiply(0.33)) val idealPos = entity.pos.add(offset.multiply(0.33))
@@ -87,7 +87,7 @@ class SwitchInstruction(val instructionSet: InstructionSet, val battleActor: Bat
setOf( setOf(
BattleDispatch { BattleDispatch {
if (entity != null) { if (entity != null && !isVirtual) {
createEntitySwitch(battle, actor, entity, pnx, activePokemon, pokemon, illusion, imposter) createEntitySwitch(battle, actor, entity, pnx, activePokemon, pokemon, illusion, imposter)
} else { } else {
createNonEntitySwitch(battle, actor, pnx, activePokemon, pokemon, illusion) createNonEntitySwitch(battle, actor, pnx, activePokemon, pokemon, illusion)
@@ -99,6 +99,8 @@ class SwitchInstruction(val instructionSet: InstructionSet, val battleActor: Bat
} }
companion object{ companion object{
val isVirtual = true
fun createEntitySwitch( fun createEntitySwitch(
battle: PokemonBattle, battle: PokemonBattle,
actor: BattleActor, actor: BattleActor,
@@ -40,16 +40,19 @@ class TransformInstruction(val battle: PokemonBattle, val message: BattleMessage
ShowdownInterpreter.broadcastOptionalAbility(battle, effect, pokemon) ShowdownInterpreter.broadcastOptionalAbility(battle, effect, pokemon)
battle.dispatch { battle.dispatch {
if (SwitchInstruction.isVirtual) return@dispatch GO
val entity = pokemon.entity ?: return@dispatch GO val entity = pokemon.entity ?: return@dispatch GO
val future = TransformEffect(targetPokemon.effectedPokemon, battle.started).start(entity) val future = TransformEffect(targetPokemon.effectedPokemon, battle.started).start(entity)
UntilDispatch { future?.isDone != false } UntilDispatch { future?.isDone != false }
} }
battle.dispatchWaiting { battle.dispatchWaiting {
val mock = pokemon.entity?.effects?.mockEffect?.mock
val pokemonName = pokemon.getName() val pokemonName = pokemon.getName()
val targetPokemonName = targetPokemon.getName() val targetPokemonName = targetPokemon.getName()
if (!SwitchInstruction.isVirtual) {
val mock = pokemon.entity?.effects?.mockEffect?.mock
mock?.let { mock?.let {
battle.sendSidedUpdate( battle.sendSidedUpdate(
source = actor, source = actor,
@@ -57,6 +60,7 @@ class TransformInstruction(val battle: PokemonBattle, val message: BattleMessage
opponentPacket = BattleTransformPokemonPacket(pnx, pokemon, it, false) opponentPacket = BattleTransformPokemonPacket(pnx, pokemon, it, false)
) )
} }
}
val lang = battleLang("transform", pokemonName, targetPokemonName) val lang = battleLang("transform", pokemonName, targetPokemonName)
battle.broadcastChatMessage(lang) battle.broadcastChatMessage(lang)
@@ -10,6 +10,7 @@ package com.cobblemon.mod.common.battles.runner
import com.cobblemon.mod.common.api.battles.model.PokemonBattle import com.cobblemon.mod.common.api.battles.model.PokemonBattle
import com.cobblemon.mod.common.battles.runner.graal.GraalShowdownService import com.cobblemon.mod.common.battles.runner.graal.GraalShowdownService
import com.cobblemon.mod.common.battles.runner.socket.SocketShowdownService
import com.google.gson.JsonArray import com.google.gson.JsonArray
import java.util.UUID import java.util.UUID
@@ -36,6 +37,6 @@ interface ShowdownService {
companion object { companion object {
val service: ShowdownService by lazy { GraalShowdownService() } val service: ShowdownService by lazy { SocketShowdownService() }
} }
} }
@@ -9,6 +9,7 @@
package com.cobblemon.mod.common.battles.runner.socket package com.cobblemon.mod.common.battles.runner.socket
import com.cobblemon.mod.common.Cobblemon import com.cobblemon.mod.common.Cobblemon
import com.cobblemon.mod.common.CobblemonImplementation
import com.cobblemon.mod.common.api.battles.model.PokemonBattle import com.cobblemon.mod.common.api.battles.model.PokemonBattle
import com.cobblemon.mod.common.api.pokemon.PokemonSpecies import com.cobblemon.mod.common.api.pokemon.PokemonSpecies
import com.cobblemon.mod.common.battles.BagItems import com.cobblemon.mod.common.battles.BagItems
@@ -16,9 +17,11 @@ import com.cobblemon.mod.common.battles.ShowdownInterpreter
import com.cobblemon.mod.common.battles.runner.ShowdownService import com.cobblemon.mod.common.battles.runner.ShowdownService
import com.cobblemon.mod.common.pokemon.FormData import com.cobblemon.mod.common.pokemon.FormData
import com.cobblemon.mod.common.pokemon.Species import com.cobblemon.mod.common.pokemon.Species
import com.cobblemon.mod.common.util.server
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.JsonArray import com.google.gson.JsonArray
import java.io.BufferedReader import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader import java.io.InputStreamReader
import java.io.OutputStreamWriter import java.io.OutputStreamWriter
import java.net.InetAddress import java.net.InetAddress
@@ -83,7 +86,9 @@ class SocketShowdownService(val host: String = "localhost", val port: Int = 1846
private fun readMessage(): String { private fun readMessage(): String {
val payloadSize = read(reader, 8).toInt() val payloadSize = read(reader, 8).toInt()
println("Reading payload of size: $payloadSize")
val payload = read(reader, payloadSize) val payload = read(reader, payloadSize)
println("Response: $payload")
return payload return payload
} }
@@ -113,8 +118,19 @@ class SocketShowdownService(val host: String = "localhost", val port: Int = 1846
writer.write(">getCobbledMoves\n") writer.write(">getCobbledMoves\n")
writer.flush() writer.flush()
val response = readMessage() val response = readMessage()
try {
return gson.fromJson(response, JsonArray::class.java) return gson.fromJson(response, JsonArray::class.java)
} }
catch (e: Exception) {
println("Error, JSON logged to logs/showdown.log")
File("./showdown.log").writer().use {
server()?.getFile("./logs/showdown.log")?.writer().use { writer ->
writer?.write(response)
}
}
}
return JsonArray()
}
override fun getItemIds(): JsonArray { override fun getItemIds(): JsonArray {
writer.write(">getCobbledItemIds") writer.write(">getCobbledItemIds")
@@ -0,0 +1,35 @@
package com.cobblemon.mod.common.command
import com.cobblemon.mod.common.battles.BattleBuilder
import com.cobblemon.mod.common.util.party
import com.mojang.brigadier.Command
import com.mojang.brigadier.CommandDispatcher
import com.mojang.brigadier.context.CommandContext
import net.minecraft.command.argument.EntityArgumentType
import net.minecraft.server.command.CommandManager
import net.minecraft.server.command.ServerCommandSource
object ChallengeCommand {
fun register(dispatcher : CommandDispatcher<ServerCommandSource>) {
val command = CommandManager.literal("battle")
.then(
CommandManager
.argument("target", EntityArgumentType.player())
.executes(::execute)
)
dispatcher.register(command)
}
private fun execute(context: CommandContext<ServerCommandSource>) : Int {
val player = context.source.playerOrThrow
val target = EntityArgumentType.getPlayer(context, "target")
val leadingPlayerPokemon = player.party().filterNotNull().firstOrNull() ?: return 0
val leadingTargetPokemon = target.party().filterNotNull().firstOrNull() ?: return 0
BattleBuilder.pvp1v1(player, target, leadingPlayerPokemon.uuid, leadingTargetPokemon.uuid)
return Command.SINGLE_SUCCESS
}
}