Skip to main content

Start a battle

The server owns every battle. These functions are server only, imported from @rpgjs/tactical-rpg/server. They work the same in standalone RPG mode (the server runs in the browser) and in MMORPG mode (an authoritative server).
startTacticalBattle throws when a player is already in a tactical battle (fighting or watching), when a player is not on the leader’s map, or when the encounter (or one of its units) is unknown. Check isInTacticalBattle(player) before starting one from a trigger that can fire twice.

StartTacticalBattleOptions

The group form (TacticalBattleGroupOptions) adds players, encounter, versus and spectators.

Where a battle starts

A battle starts wherever your server code has the RpgPlayer.

From an event (action key)

this.remove() takes the event off the map: for everyone with a shared event, for this player only with a scenario event (see Event modes).

From a touch

onPlayerTouch(player) runs when the player and the event collide:

From a zone

A player hook onInShape(player, shape) runs when a player enters a zone of the map (see Create shape). A zone is a good place to fix the grid with origin, so the battle always happens on the same ground:

From a script

A script of @rpgjs/script plays a troop with $player.startBattle(id, { kind: "tactical" }), which calls the startTactical hook of the studioBattle hooks (see Scripts: Battles). Implement it with startTacticalBattle; a player who leaves a tactical battle (abandon) counts as an escape for the script:
studioBattle is not declared on the RpgServer type, hence the as RpgServer.

The result

startTacticalBattle resolves when the battle ends, with the TacticalResult as the first side lives it: When the battle ends, the HP, SP and items spent by each player’s hero are written back on the player (never in PvP, never for a player who left), before tactical.onEnd runs. HP is written as at least 1. A tactical battle gives no EXP, gold or drops by itself: reward the victory in your game.

Fleeing and surrendering

Against the AI (solo or co-op), the heroes leave a battle by fleeing (escape): the battle is lost, and only when the encounter’s rules.canEscape is not false. In a duel there is no fleeing: a side surrenders, at any time, and the other side wins. A surrender outside a duel is refused (tactical.error.forbidden).

Solo (standalone RPG)

In a solo game the server runs inside the game, in the browser: call startTacticalBattle(player, encounter) from any of the places above. The same code runs in an MMORPG.
  • A battle played alone has no phase timer.
  • The player commands every allied unit: the hero (hero: true) and the units of the encounter.
  • The enemy side is played by the AI, with enemyDelay between two actions.

Multiplayer (MMORPG)

A battle lives in the room of its map. For every group battle:
  1. Everyone must be connected and on the leader’s map. startTacticalBattle throws otherwise. Pick partners and opponents among player.getCurrentMap()?.getPlayers(), and keep only those for which p.isConnected() and sameBattleMap(p, leader) are true: a player who disconnects stays in that list for a few minutes (their session waits for a reconnection), and the list can hold a player who is still joining or leaving the map.
  2. A player is in one tactical battle at a time. Test isInTacticalBattle(p) before adding them to a group.
  3. Each player commands the units of their seat. The seat of a placement is the index of the commanding player in its side; without it, units are dealt in turn. A side has turnSeconds (60 by default) to play its phase.
The tactical and turn-based battles keep separate registries: isInTacticalBattle does not see a turn-based battle. When both packages are installed, test isInTurnBattle too.

Co-op

Each player brings their hero: give one hero: true placement per seat. A seat that no player takes leaves its hero out, so the same encounter serves one to three players:
Start it with the players around the leader (map.getPlayers(), see RpgMap):

Player versus player: a challenge

In PvP, the versus players command the enemy side of the encounter instead of the AI: a hero: true placement among enemies is the hero of a versus player. The goal of the encounter is ignored: a duel is won when the other side is down or surrenders.
Ask the other player first; showChoices resolves with the chosen { text, value }, or null. Check again after the answer:
Nothing is written back on the players after a duel. The texts are hard-coded to keep the example short: in a game, take them from the i18n service (see i18n).

Spectators

Give spectators at the start with spectators, or let a player join a running battle with watchTacticalBattle. A spectator sees every action live but commands nothing, and leaves with the escape control.
watchTacticalBattle resolves when the spectator leaves, and false when there is nothing to watch, the spectator is busy or on another map, or a tactical.onSpectate hook refused.

Taking a player out

stopTacticalBattle resolves once the HUD of the player is closed.

Who owns what

It works the same in standalone RPG mode (the server runs in the browser) and in MMORPG mode (an authoritative server). Without provideTacticalServer, startTacticalBattle is not available; without provideTacticalClient, the battle has no HUD and no grid on the client.

Disconnection

When a player leaves a battle (disconnection, or stopTacticalBattle), their units go to a partner of the same side, who now commands them (the others see tactical.partnerLeft), and the cells they held are freed. A side with no player left loses: in a duel, the other side wins by forfeit; when every player has left, the battle ends. A player who left is not written back (abandon outcome).