diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/battles/model/PokemonBattle.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/battles/model/PokemonBattle.kt index f47eaa8d1e..75695634c6 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/battles/model/PokemonBattle.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/battles/model/PokemonBattle.kt @@ -15,10 +15,7 @@ import com.cobblemon.mod.common.Cobblemon import com.cobblemon.mod.common.Cobblemon.LOGGER import com.cobblemon.mod.common.CobblemonNetwork import com.cobblemon.mod.common.api.battles.interpreter.BattleMessage -import com.cobblemon.mod.common.api.battles.model.actor.ActorType -import com.cobblemon.mod.common.api.battles.model.actor.BattleActor -import com.cobblemon.mod.common.api.battles.model.actor.EntityBackedBattleActor -import com.cobblemon.mod.common.api.battles.model.actor.FleeableBattleActor +import com.cobblemon.mod.common.api.battles.model.actor.* import com.cobblemon.mod.common.api.events.CobblemonEvents import com.cobblemon.mod.common.api.events.battles.BattleFledEvent import com.cobblemon.mod.common.api.molang.MoLangFunctions.asMoLangValue @@ -75,7 +72,8 @@ open class PokemonBattle( val format: BattleFormat, val side1: BattleSide, val side2: BattleSide, - val service: ShowdownService + val service: ShowdownService = ShowdownService.get(), + val battleId: UUID = UUID.randomUUID() ) { /** Whether logging will be silenced for this battle. */ var mute = true @@ -110,8 +108,6 @@ open class PokemonBattle( val players = playerUUIDs.mapNotNull { it.getPlayer() } val spectators = mutableSetOf() - val battleId = UUID.randomUUID() - val showdownMessages = mutableListOf() val battleLog = mutableListOf() val chatLog = mutableListOf() @@ -502,6 +498,7 @@ open class PokemonBattle( } fun checkForInputDispatch() { + actors.filter { it is UnionBattleActor }.forEach { it.mustChoose = false } if (checkForfeit()) return // ignore actors that are still choosing, their choices don't matter anymore val readyToInput = (actors.any { !it.mustChoose && it.responses.isNotEmpty() } && actors.none { it.mustChoose }) if (readyToInput && captureActions.isEmpty()) { diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/battles/model/actor/BattleActor.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/battles/model/actor/BattleActor.kt index cef9942f66..7deb40ad0e 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/battles/model/actor/BattleActor.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/battles/model/actor/BattleActor.kt @@ -73,7 +73,7 @@ abstract class BattleActor( open fun isForPlayer(serverPlayerEntity: ServerPlayer) = serverPlayerEntity.uuid in getPlayerUUIDs() open fun isForPokemon(pokemonEntity: PokemonEntity) = activePokemon.any { it.battlePokemon?.effectedPokemon?.entity == pokemonEntity } - fun turn() { + open fun turn() { val request = request ?: return responses.clear() if(activePokemon.any { it.isAlive() }) { @@ -90,7 +90,7 @@ abstract class BattleActor( } } - fun upkeep() { + open fun upkeep() { val request = request ?: return val forceSwitchPokemon = request.forceSwitch.mapIndexedNotNull { index, b -> if (b) activePokemon[index] else null } if (forceSwitchPokemon.isEmpty()) { @@ -125,7 +125,7 @@ abstract class BattleActor( battle.checkForInputDispatch() } - fun writeShowdownResponse() { + open fun writeShowdownResponse() { val showdownMessages = mutableListOf() var index = 0 request!!.iterate(activePokemon) { activeBattlePokemon, showdownMoveSet, forceSwitch -> @@ -159,4 +159,4 @@ abstract class BattleActor( open fun win(otherWinners: List, losers: List) {} open fun lose(winners: List, otherLosers: List) {} -} \ No newline at end of file +} diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/battles/model/actor/UnionBattleActor.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/battles/model/actor/UnionBattleActor.kt new file mode 100644 index 0000000000..1b08db40f2 --- /dev/null +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/battles/model/actor/UnionBattleActor.kt @@ -0,0 +1,25 @@ +package com.cobblemon.mod.common.api.battles.model.actor + +import com.cobblemon.mod.common.battles.pokemon.BattlePokemon +import com.cobblemon.mod.common.entity.npc.NPCBattleActor +import com.cobblemon.mod.common.entity.npc.NPCEntity + +class UnionBattleActor(npc: NPCEntity, pokemonList: List) : NPCBattleActor(npc, pokemonList.toMutableList(), 1) { + + override fun writeShowdownResponse() {} + + override fun onChoiceRequested() { + this.mustChoose = false + } + + override fun turn() { + super.turn() + this.mustChoose = false + } + + override fun upkeep() { + super.upkeep() + this.mustChoose = false + } + +} diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/battles/BattleBuilder.kt b/common/src/main/kotlin/com/cobblemon/mod/common/battles/BattleBuilder.kt index 511993f4cb..1d757a4d67 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/battles/BattleBuilder.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/battles/BattleBuilder.kt @@ -61,7 +61,8 @@ object BattleBuilder { cloneParties: Boolean = false, healFirst: Boolean = false, partyAccessor: (ServerPlayer) -> PartyStore = { it.party() }, - service: ShowdownService = ShowdownService.get() + service: ShowdownService = ShowdownService.get(), + battleId: UUID = UUID.randomUUID() ): BattleStartResult { val adjustLevel = battleFormat.adjustLevel @@ -119,7 +120,8 @@ object BattleBuilder { battleFormat = battleFormat, side1 = BattleSide(player1Actor), side2 = BattleSide(player2Actor), - service = service + service = service, + battleId = battleId ).ifSuccessful { it.battlePartyStores.addAll(battlePartyStores) } @@ -136,7 +138,8 @@ object BattleBuilder { cloneParties: Boolean = false, healFirst: Boolean = false, partyAccessor: (ServerPlayer) -> PartyStore = { it.party() }, - service: ShowdownService = ShowdownService.get() + service: ShowdownService = ShowdownService.get(), + battleId: UUID = UUID.randomUUID() ): BattleStartResult { val adjustLevel = battleFormat.adjustLevel val teams = players.mapIndexed { index, it -> @@ -218,7 +221,8 @@ object BattleBuilder { battleFormat = battleFormat, side1 = BattleSide(playerActors[0], playerActors[1]), side2 = BattleSide(playerActors[2], playerActors[3]), - service = service + service = service, + battleId = battleId ).ifSuccessful { it.battlePartyStores.addAll(battlePartyStores) } @@ -251,7 +255,8 @@ object BattleBuilder { healFirst: Boolean = false, fleeDistance: Float = Cobblemon.config.defaultFleeDistance, party: PartyStore = player.party(), - service: ShowdownService = ShowdownService.get() + service: ShowdownService = ShowdownService.get(), + battleId: UUID = UUID.randomUUID() ): BattleStartResult { val playerTeam = party.toBattleTeam(clone = cloneParties, healPokemon = healFirst, leadingPokemon = leadingPokemon).sortedBy { it.health <= 0 } val playerActor = PlayerBattleActor(player.uuid, playerTeam) @@ -293,7 +298,8 @@ object BattleBuilder { battleFormat = battleFormat, side1 = BattleSide(playerActor), side2 = BattleSide(wildActor), - service = service + service = service, + battleId = battleId ).ifSuccessful { if (!cloneParties) { pokemonEntity.battleId = it.battleId @@ -324,7 +330,8 @@ object BattleBuilder { cloneParties: Boolean = false, healFirst: Boolean = false, party: PartyStore = player.party(), - service: ShowdownService = ShowdownService.get() + service: ShowdownService = ShowdownService.get(), + battleId: UUID = UUID.randomUUID() ): BattleStartResult { val playerTeam = party.toBattleTeam(clone = cloneParties, healPokemon = healFirst, leadingPokemon = leadingPokemon) val playerActor = PlayerBattleActor(player.uuid, playerTeam) @@ -372,7 +379,8 @@ object BattleBuilder { battleFormat = battleFormat, side1 = BattleSide(playerActor), side2 = BattleSide(npcActor), - service = service + service = service, + battleId = battleId ).ifSuccessful { battle -> npcEntity.entityData.update(NPCEntity.BATTLE_IDS) { it + battle.battleId } } diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/battles/BattleRegistry.kt b/common/src/main/kotlin/com/cobblemon/mod/common/battles/BattleRegistry.kt index 55211633ae..84f8886a59 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/battles/BattleRegistry.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/battles/BattleRegistry.kt @@ -125,7 +125,7 @@ object BattleRegistry { return team.joinToString("]") } - private fun startShowdown(battle: PokemonBattle) { + private fun startShowdown(battle: PokemonBattle, sendMessages: Boolean = true) { // Build request message val messages = mutableListOf() @@ -176,7 +176,9 @@ object BattleRegistry { } // Compiles the request and sends it off - battle.service.startBattle(battle, messages.toTypedArray()) + if (sendMessages) { + battle.service.startBattle(battle, messages.toTypedArray()) + } } /** @@ -197,12 +199,14 @@ object BattleRegistry { side1: BattleSide, side2: BattleSide, canPreempt: Boolean = true, - service: ShowdownService = ShowdownService.get() + service: ShowdownService = ShowdownService.get(), + battleId: UUID = UUID.randomUUID(), + sendMessages: Boolean = true ): BattleStartResult { - val battle = PokemonBattle(battleFormat, side1, side2, service) + val battle = PokemonBattle(battleFormat, side1, side2, service, battleId) val start: () -> Unit = { battleMap[battle.battleId] = battle - startShowdown(battle) + startShowdown(battle, sendMessages) } if (!canPreempt) start().also { return SuccessfulBattleStart(battle) } diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/battles/runner/remote/UnionShowdownService.kt b/common/src/main/kotlin/com/cobblemon/mod/common/battles/runner/remote/UnionShowdownService.kt index b8f14e3907..1337ed3aab 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/battles/runner/remote/UnionShowdownService.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/battles/runner/remote/UnionShowdownService.kt @@ -2,18 +2,22 @@ package com.cobblemon.mod.common.battles.runner.remote import com.cobblemon.mod.common.Cobblemon import com.cobblemon.mod.common.api.battles.model.PokemonBattle +import com.cobblemon.mod.common.api.battles.model.actor.UnionBattleActor import com.cobblemon.mod.common.api.npc.NPCClasses import com.cobblemon.mod.common.api.pokemon.PokemonSpecies -import com.cobblemon.mod.common.battles.BagItems -import com.cobblemon.mod.common.battles.BattleBuilder -import com.cobblemon.mod.common.battles.ShowdownInterpreter +import com.cobblemon.mod.common.api.storage.party.PartyStore +import com.cobblemon.mod.common.battles.* +import com.cobblemon.mod.common.battles.actor.PlayerBattleActor +import com.cobblemon.mod.common.battles.pokemon.BattlePokemon import com.cobblemon.mod.common.battles.runner.ShowdownService import com.cobblemon.mod.common.battles.runner.ShowdownStatus import com.cobblemon.mod.common.entity.npc.NPCEntity +import com.cobblemon.mod.common.pokemon.Pokemon import com.cobblemon.mod.common.util.HttpService import com.cobblemon.mod.common.util.cobblemonResource import com.cobblemon.mod.common.util.server import com.google.gson.JsonArray +import com.google.gson.JsonParser import net.minecraft.resources.ResourceLocation import java.net.URI import java.net.http.HttpClient @@ -223,17 +227,114 @@ class UnionShowdownService( super.onOpen(webSocket) } + fun startBattle(instruction: String) { + val arguments = instruction.replace("|start-battle|", "").split("|") + if (arguments.size != 5) return + val battleId = UUID.fromString(arguments[0]) + val playerIdA = UUID.fromString(arguments[1]) + val playerIdB = UUID.fromString(arguments[2]) + val teamA = JsonParser.parseString(arguments[3]).asJsonObject + val teamB = JsonParser.parseString(arguments[4]).asJsonObject + + val playerA = server()?.playerList?.getPlayer(playerIdA) + val playerB = server()?.playerList?.getPlayer(playerIdB) + + if (playerA == null && playerB == null) { + Cobblemon.LOGGER.warn("Tried to start battle with no players present on server") + return + } + + val registryAccess = server()?.registryAccess() ?: return + + val playerAParty = PartyStore(UUID.randomUUID()).loadFromJSON(teamA, registryAccess) + val playerBParty = PartyStore(UUID.randomUUID()).loadFromJSON(teamB, registryAccess) + val playerATeam = toShowdownBattleTeam(playerAParty.toList()).onEach { + it.effectedPokemon.level = 100 + it.effectedPokemon.heal() + } + val playerBTeam = toShowdownBattleTeam(playerBParty.toList()).onEach { + it.effectedPokemon.level = 100 + it.effectedPokemon.heal() + } + + if (playerA != null && playerB != null) { + val playerActorA = PlayerBattleActor(playerIdA, playerATeam) + val playerActorB = PlayerBattleActor(playerIdB, playerBTeam) + BattleRegistry.startBattle( + battleId = battleId, + battleFormat = BattleFormat.GEN_9_SINGLES, + side1 = BattleSide(playerActorA), + side2 = BattleSide(playerActorB), + sendMessages = false, + service = service + ) + } + else if (playerA != null && playerB == null) { + val playerActorA = PlayerBattleActor(playerIdA, playerATeam) + val npc = NPCEntity(playerA.level()) + npc.moveTo(playerA.x + 5, playerA.y, playerA.z, playerA.yRot, playerA.xRot) + npc.npc = NPCClasses.getByIdentifier(cobblemonResource("ai_test"))!! + npc.initialize(1) + npc.uuid = playerIdB + playerA.level().addFreshEntity(npc) + val playerActorB = UnionBattleActor(npc, playerBTeam) + BattleRegistry.startBattle( + battleId = battleId, + battleFormat = BattleFormat.GEN_9_SINGLES, + side1 = BattleSide(playerActorA), + side2 = BattleSide(playerActorB), + sendMessages = false, + service = service + ) + } + else { + val npc = NPCEntity(playerB!!.level()) + npc.moveTo(playerB.x + 5, playerB.y, playerB.z, playerB.yRot, playerB.xRot) + npc.npc = NPCClasses.getByIdentifier(cobblemonResource("ai_test"))!! + npc.initialize(1) + npc.uuid = playerIdA + playerB.level().addFreshEntity(npc) + val playerActorA = UnionBattleActor(npc, playerATeam) + val playerActorB = PlayerBattleActor(playerIdB, playerBTeam) + BattleRegistry.startBattle( + battleId = battleId, + battleFormat = BattleFormat.GEN_9_SINGLES, + side1 = BattleSide(playerActorA), + side2 = BattleSide(playerActorB), + sendMessages = false, + service = service + ) + } + } + + fun toShowdownBattleTeam(team: List): List { + return team.map { pokemon -> + BattlePokemon( + originalPokemon = pokemon, + effectedPokemon = pokemon.clone(newUUID = false), + postBattleEntityOperation = { entity -> entity.discard() } + ) + } + } + override fun onText(webSocket: WebSocket, data: CharSequence, last: Boolean): CompletionStage<*>? { if (last) { builder.append(data) val message = builder.toString() service.addToShowdownLog(source = "Showdown", message = message) - val battleIdRegex = Regex("^\\[([^]]+)]") - val match = battleIdRegex.find(message) ?: return null - val battleId = match.groupValues[1] - val instruction = message.replace("[$battleId]", "") - ShowdownInterpreter.interpretMessage(UUID.fromString(battleId), instruction) + if (data.startsWith("|start-battle|")) { + startBattle(data.toString()) + } + else { + val battleIdRegex = Regex("^\\[([^]]+)]") + val match = battleIdRegex.find(message) ?: return null + + val battleId = match.groupValues[1] + val instruction = message.replace("[$battleId]", "") + ShowdownInterpreter.interpretMessage(UUID.fromString(battleId), instruction) + } + builder.clear() } else { diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/debug/ShowdownDebugGUI.kt b/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/debug/ShowdownDebugGUI.kt index 33f4179a57..72c78a1ce5 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/debug/ShowdownDebugGUI.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/debug/ShowdownDebugGUI.kt @@ -79,10 +79,10 @@ class ShowdownDebugGUI : Screen(lang("ui.debug.showdown")), CobblemonRenderable ) val startNPCBattleButton = addRenderableWidget( - Button.builder(Component.literal("Create NPC Battle")) { button -> + Button.builder(Component.literal("Queue")) { button -> CobblemonNetwork.sendToServer(ShowdownActionPacket( service = activeShowdownService, - action = ShowdownActionPacket.Action.START_NPC_BATTLE + action = ShowdownActionPacket.Action.QUEUE )) }.bounds(10, 70, 100, 20).build() ) diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/entity/npc/NPCBattleActor.kt b/common/src/main/kotlin/com/cobblemon/mod/common/entity/npc/NPCBattleActor.kt index f1873f63bc..66a174a8e1 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/entity/npc/NPCBattleActor.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/entity/npc/NPCBattleActor.kt @@ -23,7 +23,7 @@ import com.cobblemon.mod.common.util.effectiveName import com.cobblemon.mod.common.util.update import java.util.concurrent.CompletableFuture -class NPCBattleActor( +open class NPCBattleActor( val npc: NPCEntity, pokemonList: List, val skill: Int @@ -84,4 +84,4 @@ class NPCBattleActor( // } // } } -} \ No newline at end of file +} diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/net/messages/server/debug/showdown/ShowdownActionPacket.kt b/common/src/main/kotlin/com/cobblemon/mod/common/net/messages/server/debug/showdown/ShowdownActionPacket.kt index 96a7a9ba31..8fed211906 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/net/messages/server/debug/showdown/ShowdownActionPacket.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/net/messages/server/debug/showdown/ShowdownActionPacket.kt @@ -44,6 +44,6 @@ class ShowdownActionPacket(val service: ResourceLocation, val action: Action) : * Enum representing possible actions for the Showdown service. */ enum class Action { - START, STOP, RESTART, START_NPC_BATTLE + START, STOP, RESTART, QUEUE } } diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/net/serverhandling/debug/showdown/ShowdownActionHandler.kt b/common/src/main/kotlin/com/cobblemon/mod/common/net/serverhandling/debug/showdown/ShowdownActionHandler.kt index f7edb17706..e6383ec895 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/net/serverhandling/debug/showdown/ShowdownActionHandler.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/net/serverhandling/debug/showdown/ShowdownActionHandler.kt @@ -2,12 +2,12 @@ package com.cobblemon.mod.common.net.serverhandling.debug.showdown import com.cobblemon.mod.common.Cobblemon import com.cobblemon.mod.common.api.net.ServerNetworkPacketHandler -import com.cobblemon.mod.common.api.npc.NPCClasses -import com.cobblemon.mod.common.battles.BattleBuilder +import com.cobblemon.mod.common.battles.BattleRegistry.packTeam +import com.cobblemon.mod.common.battles.pokemon.BattlePokemon import com.cobblemon.mod.common.battles.runner.ShowdownService -import com.cobblemon.mod.common.entity.npc.NPCEntity import com.cobblemon.mod.common.net.messages.server.debug.showdown.ShowdownActionPacket -import com.cobblemon.mod.common.util.cobblemonResource +import com.cobblemon.mod.common.util.party +import com.google.gson.JsonObject import net.minecraft.server.MinecraftServer import net.minecraft.server.level.ServerPlayer @@ -37,17 +37,15 @@ object ShowdownActionHandler : ServerNetworkPacketHandler ShowdownActionPacket.Action.START -> service.openConnection() ShowdownActionPacket.Action.STOP -> service.closeConnection() ShowdownActionPacket.Action.RESTART -> service.restartConnection() - ShowdownActionPacket.Action.START_NPC_BATTLE -> { - val npc = NPCEntity(player.level()) - npc.moveTo(player.x + 5, player.y, player.z, player.yRot, player.xRot) - npc.npc = NPCClasses.getByIdentifier(cobblemonResource("ai_test"))!! - npc.initialize(1) - player.level().addFreshEntity(npc) - BattleBuilder.pvn( - player = player, - npcEntity = npc, - service = service - ) + ShowdownActionPacket.Action.QUEUE -> { + val cobblemonTeam = player.party().saveToJSON(JsonObject(), player.registryAccess()) + val showdownTeam = player.party().map { BattlePokemon(it, it.clone(false)) { entity -> entity.discard() } } + showdownTeam.forEach { + it.effectedPokemon.level = 100 + it.effectedPokemon.heal() + } + val showdownTeamStr = showdownTeam.packTeam() + service.send(">queue-battle ${player.uuid} $cobblemonTeam@|@$showdownTeamStr") } } } diff --git a/gradle/build-logic/src/main/kotlin/cobblemon.platform-conventions.gradle.kts b/gradle/build-logic/src/main/kotlin/cobblemon.platform-conventions.gradle.kts index e84c4573a3..3e6b3162b4 100644 --- a/gradle/build-logic/src/main/kotlin/cobblemon.platform-conventions.gradle.kts +++ b/gradle/build-logic/src/main/kotlin/cobblemon.platform-conventions.gradle.kts @@ -16,9 +16,9 @@ val bundle: Configuration by configurations.creating { loom { val clientConfig = runConfigs.getByName("client") clientConfig.runDir = "runClient" - clientConfig.programArg("--username=AshKetchum") +// clientConfig.programArg("--username=GaryOak") //This is AshKetchum's UUID so you get an Ash Ketchum skin - clientConfig.programArg("--uuid=93e4e551-589a-41cb-ab2d-435266c8e035") +// clientConfig.programArg("--uuid=93e4e551-589a-41cb-ab2d-435266c8e035") val serverConfig = runConfigs.getByName("server") serverConfig.runDir = "runServer" }