Start a battle
The server owns every battle. Every function of this page is server only and imported from@rpgjs/turn-battle/server. They work the same in standalone RPG mode (the server runs in the browser) and in MMORPG mode.
startTurnBattle resolves with a BattleResult (outcome, exp, gold, items, bonuses, turns, and winners in PvP). Before it resolves, the result is written on each player: HP, SP, items spent, EXP, gold and drops (nothing between players).
Where a battle starts
A battle starts wherever your server code has theRpgPlayer: an event, a touch, a zone, a player hook, an item, a script.
From an event (action key)
The player walks to the event and presses the action key:onAction runs (see Create an event).
this.remove() takes the event off the map. In MMORPG mode, a shared event disappears for everyone; make it a scenario event (mode: EventMode.Scenario) when each player must beat their own ogre (see Event modes).
From a touch (an enemy walks into the player)
onPlayerTouch(player) runs when the player and the event collide, whoever moved (see Event touch and map variables for onTouch, which also runs for events).
From a zone (random encounters)
A shape is an invisible zone on the map. A player hookonInShape(player, shape) runs when a player enters one (see Create shape and Server player hooks). Create the zone in Tiled, or from the onLoad map hook with map.createShape:
background and bgm given here are used when the troop has none of its own.
From a player hook
Any player hook has the player. For example, an ambush the first time a player enters a map, withonJoinMap:
From an item
TheonUse hook of an item runs on the server when the player uses it (see Items):
From a script
A script of@rpgjs/script plays a troop with $player.startBattle(troopId, { kind }), which calls the studioBattle hooks of the modules of the game (see Scripts: Battles). Implement them with startTurnBattle:
studioBattle is not declared on the RpgServer type, hence the as RpgServer.
The result
startTurnBattle resolves when the battle is over, after the result screen (in a group battle, once every player has left it). The BattleResult:
You do not add the rewards yourself: before the promise resolves, the server has written on each fighting player their HP and SP, the items they spent, EXP, gold and drops. HP is written as at least 1, so a defeated player is not dead on the map: what a defeat means is up to your game.
onStart and onEnd (see Customization) run for every battle, whatever started it: use them for what must happen after any battle (a quest counter, a log).
StartTurnBattleOptions (third argument, or the group options)
Group battles (
TurnBattleGroupOptions) add players, troop, versus, spectators, background and bgm. Every player must be on the leader’s map: a battle lives in the room of its map.
Solo (standalone RPG)
In a solo game the server runs inside the game, in the browser, and there is only one player: callstartTurnBattle(player, troop) from any of the places above. Nothing else changes: the same code runs in an MMORPG.
- No turn timer by default (
turnTimerapplies as soon as more than one player fights). - The player’s companions come from the
partyserver option, or from theparty/heroesoptions of one battle. - A troop can forbid escaping (
canEscape: false, see Battle data).
Multiplayer (MMORPG)
The server is shared, and a battle lives in the room of its map. Three rules hold for every group battle:- Everyone must be connected and on the leader’s map.
startTurnBattlethrows otherwise. Pick partners and opponents amongplayer.getCurrentMap()?.getPlayers(), and keep only those for whichp.isConnected()andsameBattleMap(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. - A player is in one battle at a time. A player who fights or watches is busy: test
isInTurnBattle(p)before adding them to a group. - Each player commands their own hero and companions. The others’ units are shown but cannot be commanded. A 30-second turn timer runs by default as soon as more than one player fights.
The turn-based and tactical battles keep separate registries:
isInTurnBattle does not see a tactical battle. When both packages are installed, test isInTacticalBattle too.Co-op: fight with the players around
The players near the leader join the battle.map.getPlayers() lists the players of the map (see RpgMap):
onStart / onEnd run for each one. The party and hero server options receive a PartyContext (players, index, side, pvp): use it to give fewer companions in co-op. To let each partner accept first, ask them with showChoices as in the challenge below.
Player versus player: a challenge
Ask the other player before starting:showChoices resolves with the chosen { text, value }, or null. Check again after the answer, since the other player may have moved, left or started another battle meanwhile.
challenge(player, rival) from wherever your game lets a player pick another one: an arena NPC, a GUI action, a command of the chat. In player versus player:
- there is no troop by default;
troopadds monsters, andbackground/bgmset the scene of a battle without a troop; - a duel without a troop cannot be escaped; a side that has nobody left fighting forfeits;
BattleResult.winnersholds the ids of the winning side;- HP and SP are not written back on the players: they keep their HP and SP from before the duel.
player.t(...), see i18n).
Spectators
Spectators see every action live but command nothing, and leave with the back control. Give them at the start withspectators, or let a player join a running battle with watchTurnBattle:
watchTurnBattle resolves when the spectator leaves, and false at once when there is nothing to watch, the spectator is busy or on another map, or a turnBattle.onSpectate hook refused (a private fight). getTurnBattle(battleId) and watchTurnBattle(spectator, battleId) also take the id of a battle (getTurnBattle(player)?.id).
Taking a player out
stopTurnBattle(player) takes a fighter or a spectator out of their battle, as a disconnection does, and resolves once their battle screen is closed:
Who owns what
It works the same in standalone RPG mode (the server runs in the browser) and in MMORPG mode. The limit gauges are saved on the player (prop
turnBattle, not sent to the client).
Disconnection
When a fighter disconnects (orstopTurnBattle takes them out), the battle goes on without them:
- the AI takes over their units (hero and companions) until the end of the battle;
- nothing is written on the player who left: no HP, SP, items, EXP, gold or drops, and
onEnddoes not run for them; - in player versus player, a side that has nobody left forfeits, and the other side wins.