Skip to content

Add a race ​

A race is an entry of races in your rules: an id, a name, and what its players start with. Units and upgrades name their race in race; anything with no race is everyone's.

ts
// in src/rules.ts
import { defineRace, type Rules } from '@v12-engine/engine'

export const rules: Rules = {
	id: 'my-game',
	name: 'My game',
	races: [
		defineRace({
			id: 'human',
			name: 'Human',
			description: 'Peasants, footmen and a town hall',
			// a town hall at the start location, five peasants in a row on the side of the closest gold mine
			startingBase: { hall: 'townhall', workers: { type: 'peasant', count: 5 } }
		}),
		defineRace({
			id: 'orc',
			name: 'Orc',
			// three peons, a grunt with them, and a pit the orcs dig on the closest gold mine to gather from it
			startingBase: { hall: 'greatHall', workers: { type: 'peon', count: 3 }, units: [{ type: 'grunt', count: 1 }], onMine: 'goldPit' },
			// what its players start with of each resource, over the game's
			resources: { gold: 400 }
		})
	],
	units: [
		{ id: 'townhall', name: 'Town hall', kind: 'building', race: 'human', radius: 2, footprint: { w: 4, d: 4 }, stats: { health: 1500 }, sight: 9 },
		{ id: 'greatHall', name: 'Great hall', kind: 'building', race: 'orc', radius: 2, footprint: { w: 4, d: 4 }, stats: { health: 1600 }, sight: 9 },
		// of no race: everyone's
		{
			id: 'goldmine',
			name: 'Gold mine',
			kind: 'resource',
			radius: 2,
			footprint: { w: 4, d: 4 },
			stats: { health: 1 },
			sight: 0,
			resource: { id: 'gold', amount: 12500, slots: 5, entered: true }
		}
		// the peasants, peons, grunts, footmen and the gold pit go here too
	],
	victory: null
}

Done: the lobby offers Human, Orc and Random. An Orc player starts with a great hall, three peons, a grunt and a gold pit on the nearest mine.

Start some other way ​

Give startingBase a function instead of data:

ts
startingBase: { spawn(world, player, at) { /* ... */ } }

Make a race play differently ​

Give it hooks (they run only for its players) and onStart(world, player). See Economy.

In the editor ​

  1. Set editable: 'all' (or editable: { races: 'all' }) in your rules. The Data panel lists the races.
  2. Pick a race and edit its Start: the hall, the workers' type and count, what goes on the mine, extra units.
  3. Click Keep to write data.json:
json
{ "races": { "orc": { "start": { "workers": { "count": 4 }, "units": [{ "type": "raider" }] } } } }
  1. To make a new race from data alone, click Copy as new. It keeps the original's hooks and onStart unless you turn them off under inherits:
json
{ "races": { "trolls": { "copyOf": "orc", "name": "Trolls", "start": { "hall": "hut" }, "inherits": { "hooks": false } } } }

Reference ​

FieldWhat it does
startingBase.hallPlaced at the player's start.
startingBase.workers{ type, count }, in a row 5 cells from the hall (distance), 0.35 rad apart (spread), facing the nearest gold mine within 20 cells.
startingBase.unitsMore units after the workers.
startingBase.onMinePlaced on the nearest mine.
resourcesStarting resources, over the game's.
MapPlayer.race, GameMap.racesA map fixes a player's race or narrows the choice.
raceOf(world, player)Which race a player plays.

Every id must be a unit type of your game; a typo is refused as the game is put together (checkRaces). A mod adds a race with game.races.add.